1 eval '(exit $?0)' && eval 'exec perl -wS "$0" "$@"'
2 & eval 'exec perl -wS "$0" $argv:q'
8 use File::Basename; # for dirname
10 my $VERSION = '2012-01-21 17:13'; # UTC
11 (my $ME = $0) =~ s|.*/||;
19 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
22 print $STREAM "Try '$ME --help' for more information.\n";
27 Usage: $ME --lib-name=NAME FILE
28 or: $ME [--help|--version]
29 Rewrite a gnulib-tool-generated FILE like lib/gnulib.mk to work with
30 automake's subdir-objects.
34 This option must be specified:
36 --lib-name=NAME library name, often "lib\$project"
38 The following are optional:
40 --help display this help and exit
41 --version output version information and exit
48 # contents ($FILE_NAME)
49 # ---------------------
53 local $/; # Turn on slurp-mode.
54 my $f = new IO::File "< $file" or die "$file";
55 my $contents = $f->getline or die "$file";
62 # Do not prefix special words such as variable dereferences. Also,
63 # "Makefile" is really "Makefile", since precisely there is no
69 unless (/^-/ || m{^\$\(\w+\)} || $_ eq "Makefile" || $_ eq '\\'
75 # prefix_words ($TEXT)
76 # --------------------
80 s{(\S+)}{prefix_word($1)}gem;
85 # prefix_assignment ($LHS-AND-ASSIGN-OP, $RHS)
86 # --------------------------------------------
87 sub prefix_assignment ($$)
89 my ($lhs_and_assign_op, $rhs) = @_;
92 # Some variables are initialized by gnulib.mk, and we don't want
93 # that. Change '=' to '+='.
94 if ($lhs_and_assign_op =~ /^(GPERF|V_GPERF.*) =$/)
96 # Do not change the RHS, which specifies the GPERF program.
98 elsif ($lhs_and_assign_op =~
99 /^(SUBDIRS|EXTRA_DIST|BUILT_SOURCES|SUFFIXES|MOSTLYCLEANFILES
100 |CLEANFILES|DISTCLEANFILES|MAINTAINERCLEANFILES|AM_CFLAGS
101 |AM_CPPFLAGS|AM_GNU_GETTEXT)\ =/x)
103 $lhs_and_assign_op =~ s/=/+=/;
105 # We don't want to inherit gnulib's AUTOMAKE_OPTIONS, comment them.
106 elsif ($lhs_and_assign_op =~ /^AUTOMAKE_OPTIONS =/)
108 $lhs_and_assign_op =~ s/^/# /;
110 elsif ($lhs_and_assign_op =~ /^SUFFIXES /)
112 # Elide any SUFFIXES assignment or concatenation.
113 $lhs_and_assign_op =~ s/^/# /;
115 # The words are (probably) paths to files in lib/: prefix them.
118 $rhs = prefix_words($rhs)
121 # Variables which name depend on the location: libbison_a_SOURCES =>
122 # lib_libbison_a_SOURCES.
123 $lhs_and_assign_op =~ s/($lib_name)/lib_$1/g;
125 return $lhs_and_assign_op . $rhs;
130 # $CONTENTS is a Makefile content. Post-process it so that each file-name
131 # is prefixed with $prefix (e.g., "lib/").
133 # Relies heavily on the regularity of the file generated by gnulib-tool.
139 # Prefix all the occurrence of files in rules. If there is nothing
140 # after in the :, it's probably a phony target, or a suffix rule.
142 s{^([-\w+/]+\.[-\w.]+ *: *\S.*)$}
143 {prefix_words($1)}gem;
145 # Prefix files in variables.
146 s{^([\w.]+\s*\+?=)(.*)$}
147 {prefix_assignment($1, $2)}gem;
149 # These three guys escape all the other regular rules.
150 # Require the leading white space to avoid inserting the prefix
151 # on a line like this:
152 # charset_alias = $(DESTDIR)$(libdir)/charset.alias
153 # With $(libdir), it would be erroneous.
154 s{(\s)(charset\.alias|ref-add\.sed|ref-del\.sed)}{$1$prefix$2}g;
155 # Unfortunately, as a result we sometimes have lib/lib.
156 s{($prefix){2}}{$1}g;
158 # $(srcdir)/ is actually $(top_srcdir)/$prefix/.
159 # The trailing slash is required to avoid matching this rule:
160 # test '$(srcdir)' = . || rm -f $(top_builddir)/GNUmakefile
161 s{\$\(srcdir\)/}{\$(top_srcdir)/$prefix}g;
163 # Sometimes, t-$@ is used instead of $@-t, which, of course, does
164 # not work when we have a $@ with a directory in it.
167 # Some AC_SUBST patterns remain and would better be Make macros.
168 s{\@(MKDIR_P)\@}{\$($1)}g;
170 # Adjust paths in mkdir.
171 s{(\$\(MKDIR_P\))\s*(\w+)}{$1 $prefix$2}g;
181 my ($bak) = "$file.bak";
182 rename ($file, $bak) or die "$ME: rename $file $bak failed: $!\n";
183 my $contents = contents ($bak);
184 $contents = prefix ($contents);
185 my $out = new IO::File(">$file")
186 or die "$ME: $file: failed to open for writing: $!\n";
187 print $out $contents;
193 'lib-name=s' => \$lib_name,
194 help => sub { usage 0 },
195 version => sub { print "$ME version $VERSION\n"; exit },
200 or (warn "$ME: no library name; use --lib-name=NAME\n"), $fail = 1;
202 # There must be exactly one argument.
204 and (warn "$ME: missing FILE argument\n"), $fail = 1;
206 and (warn "$ME: too many arguments:\n", join ("\n", @ARGV), "\n"),
212 $prefix = (dirname $file) . '/';
213 warn "prefix=$prefix\n";
218 ### Setup "GNU" style for perl-mode and cperl-mode.
220 ## perl-indent-level: 2
221 ## perl-continued-statement-offset: 2
222 ## perl-continued-brace-offset: 0
223 ## perl-brace-offset: 0
224 ## perl-brace-imaginary-offset: 0
225 ## perl-label-offset: -2
226 ## cperl-indent-level: 2
227 ## cperl-brace-offset: 0
228 ## cperl-continued-brace-offset: 0
229 ## cperl-label-offset: -2
230 ## cperl-extra-newline-before-brace: t
231 ## cperl-merge-trailing-else: nil
232 ## cperl-continued-statement-offset: 2
233 ## eval: (add-hook 'write-file-hooks 'time-stamp)
234 ## time-stamp-start: "my $VERSION = '"
235 ## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
236 ## time-stamp-time-zone: "UTC0"
237 ## time-stamp-end: "'; # UTC"