]> git.saurik.com Git - bison.git/blame - etc/prefix-gnulib-mk
symtab: refactoring
[bison.git] / etc / prefix-gnulib-mk
CommitLineData
feda5527
AD
1#! /usr/bin/perl -w
2
3use strict;
4use IO::File;
219139f5
JM
5use Getopt::Long;
6use File::Basename; # for dirname
feda5527 7
219139f5
JM
8my $VERSION = '2012-01-21 17:13'; # UTC
9(my $ME = $0) =~ s|.*/||;
10
11my $prefix;
12my $lib_name;
13
14sub usage ($)
15{
16 my ($exit_code) = @_;
17 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
18 if ($exit_code != 0)
19 {
20 print $STREAM "Try '$ME --help' for more information.\n";
21 }
22 else
23 {
24 print $STREAM <<EOF;
25Usage: $ME --lib-name=NAME FILE
26 or: $ME [--help|--version]
27Rewrite a gnulib-tool-generated FILE like lib/gnulib.mk to work with
28automake's subdir-objects.
29
30OPTIONS:
31
32This option must be specified:
33
34 --lib-name=NAME library name, often "lib\$project"
35
36The following are optional:
37
38 --help display this help and exit
39 --version output version information and exit
40
41EOF
42 }
43 exit $exit_code;
44}
feda5527
AD
45
46# contents ($FILE_NAME)
47# ---------------------
48sub contents ($)
49{
50 my ($file) = @_;
e9690142 51 local $/; # Turn on slurp-mode.
feda5527
AD
52 my $f = new IO::File "< $file" or die "$file";
53 my $contents = $f->getline or die "$file";
54 $f->close;
55 return $contents;
56}
57
58# prefix_word ($WORD)
59# -------------------
60# Do not prefix special words such as variable dereferences. Also,
61# "Makefile" is really "Makefile", since precisely there is no
62# lib/Makefile.
63sub prefix_word ($)
64{
65 local ($_) = @_;
66 $_ = $prefix . $_
85ad742d 67 unless /^-/ || m{^\$\(\w+\)} || $_ eq "Makefile" || $_ eq '\\';
feda5527
AD
68 return $_;
69}
70
71
72# prefix_words ($TEXT)
73# --------------------
74sub prefix_words ($)
75{
76 local ($_) = @_;
77 s{(\S+)}{prefix_word($1)}gem;
78 return $_;
79}
80
81
82# prefix_assignment ($LHS-AND-ASSIGN-OP, $RHS)
83# --------------------------------------------
84sub prefix_assignment ($$)
85{
86 my ($lhs_and_assign_op, $rhs) = @_;
87 my $res;
88
89 # Some variables are initialized by gnulib.mk, and we don't want
90 # that. Change '=' to '+='.
11c073b7 91 if ($lhs_and_assign_op =~ /^(SUBDIRS|EXTRA_DIST|BUILT_SOURCES|SUFFIXES|MOSTLYCLEANFILES|CLEANFILES|DISTCLEANFILES|MAINTAINERCLEANFILES|AM_CFLAGS|AM_CPPFLAGS|AM_GNU_GETTEXT) =/)
feda5527
AD
92 {
93 $lhs_and_assign_op =~ s/=/+=/;
94 }
95 # We don't want to inherit gnulib's AUTOMAKE_OPTIONS, comment them.
96 elsif ($lhs_and_assign_op =~ /^AUTOMAKE_OPTIONS =/)
97 {
98 $lhs_and_assign_op =~ s/^/# /;
99 }
100 # Don't touch suffixes.
101 elsif ($lhs_and_assign_op =~ /^SUFFIXES /)
102 {
103 }
104 # The words are (probably) paths to files in lib/: prefix them.
105 else
106 {
107 $rhs = prefix_words($rhs)
108 }
109
110 # Variables which name depend on the location: libbison_a_SOURCES =>
111 # lib_libbison_a_SOURCES.
219139f5 112 $lhs_and_assign_op =~ s/($lib_name)/lib_$1/g;
feda5527 113
feda5527
AD
114 return $lhs_and_assign_op . $rhs;
115}
116
117# prefix $CONTENTS
118# ----------------
119# $CONTENTS is a Makefile content. Post-process it so that each file-name
120# is prefixed with $prefix (e.g., "lib/").
121#
122# Relies heavily on the regularity of the file generated by gnulib-tool.
123sub prefix ($)
124{
125 # Work on $_.
126 local ($_) = @_;
127
128 # Prefix all the occurrence of files in rules. If there is nothing
129 # after in the :, it's probably a phony target, or a suffix rule.
130 # Don't touch it.
8b9e021f 131 s{^([-\w+/]+\.[-\w.]+ *: *\S.*)$}
feda5527
AD
132 {prefix_words($1)}gem;
133
134 # Prefix files in variables.
135 s{^([\w.]+\s*\+?=)(.*)$}
136 {prefix_assignment($1, $2)}gem;
137
138 # These three guys escape all the other regular rules.
139 s{(charset\.alias|ref-add\.sed|ref-del\.sed)}{$prefix$1}g;
140 # Unfortunately, as a result we sometimes have lib/lib.
d1b55e81 141 s{($prefix){2}}{$1}g;
feda5527
AD
142
143 # $(srcdir) is actually $(top_srcdir)/lib.
144 s{\$\(srcdir\)}{\$(top_srcdir)/lib}g;
145
146 # Sometimes, t-$@ is used instead of $@-t, which, of course, does
147 # not work when we have a $@ with a directory in it.
148 s{t-\$\@}{\$\@-t}g;
149
d1b55e81
AD
150 # Some AC_SUBST patterns remain and would better be Make macros.
151 s{\@(MKDIR_P)\@}{\$($1)}g;
152
153 # Adjust paths in mkdir.
154 s{(\$\(MKDIR_P\))\s*(\w+)}{$1 $prefix$2}g;
155
feda5527
AD
156 return $_;
157}
158
159# process ($IN)
160# -------------
161sub process ($)
162{
163 my ($file) = @_;
164 my ($bak) = "$file.bak";
165 rename ($file, $bak) or die;
166 my $contents = contents ($bak);
167 $contents = prefix ($contents);
168 my $out = new IO::File(">$file") or die;
169 print $out $contents;
170}
171
219139f5
JM
172{
173 GetOptions
174 (
175 'lib-name=s' => \$lib_name,
176 help => sub { usage 0 },
177 version => sub { print "$ME version $VERSION\n"; exit },
178 ) or usage 1;
179
180 my $fail = 0;
181 defined $lib_name
182 or (warn "$ME: no library name; use --lib-name=NAME\n"), $fail = 1;
183
184 # There must be exactly one argument.
185 @ARGV == 0
186 and (warn "$ME: missing FILE argument\n"), $fail = 1;
187 1 < @ARGV
188 and (warn "$ME: too many arguments:\n", join ("\n", @ARGV), "\n"),
189 $fail = 1;
190 $fail
191 and usage 1;
192
193 my $file = $ARGV[0];
194 $prefix = (dirname $file) . '/';
195 warn "prefix=$prefix\n";
196
197 process $file;
198}
feda5527
AD
199
200### Setup "GNU" style for perl-mode and cperl-mode.
201## Local Variables:
202## perl-indent-level: 2
203## perl-continued-statement-offset: 2
204## perl-continued-brace-offset: 0
205## perl-brace-offset: 0
206## perl-brace-imaginary-offset: 0
207## perl-label-offset: -2
208## cperl-indent-level: 2
209## cperl-brace-offset: 0
210## cperl-continued-brace-offset: 0
211## cperl-label-offset: -2
212## cperl-extra-newline-before-brace: t
213## cperl-merge-trailing-else: nil
214## cperl-continued-statement-offset: 2
219139f5
JM
215## eval: (add-hook 'write-file-hooks 'time-stamp)
216## time-stamp-start: "my $VERSION = '"
217## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
218## time-stamp-time-zone: "UTC"
219## time-stamp-end: "'; # UTC"
feda5527 220## End: