6 use File
::Basename
; # for dirname
8 my $VERSION = '2012-01-21 17:13'; # UTC
9 (my $ME = $0) =~ s
|.*/||;
17 my $STREAM = ($exit_code == 0 ?
*STDOUT
: *STDERR
);
20 print $STREAM "Try '$ME --help' for more information.\n";
25 Usage: $ME --lib-name=NAME FILE
26 or: $ME [--help|--version]
27 Rewrite a gnulib-tool-generated FILE like lib/gnulib.mk to work with
28 automake's subdir-objects.
32 This option must be specified:
34 --lib-name=NAME library name, often "lib\$project"
36 The following are optional:
38 --help display this help and exit
39 --version output version information and exit
46 # contents ($FILE_NAME)
47 # ---------------------
51 local $/; # Turn on slurp-mode.
52 my $f = new IO
::File
"< $file" or die "$file";
53 my $contents = $f->getline or die "$file";
60 # Do not prefix special words such as variable dereferences. Also,
61 # "Makefile" is really "Makefile", since precisely there is no
67 unless /^-/ || m{^\$\(\w+\)} || $_ eq "Makefile" || $_ eq '\\';
72 # prefix_words ($TEXT)
73 # --------------------
77 s{(\S+)}{prefix_word($1)}gem;
82 # prefix_assignment ($LHS-AND-ASSIGN-OP, $RHS)
83 # --------------------------------------------
84 sub prefix_assignment
($$)
86 my ($lhs_and_assign_op, $rhs) = @_;
89 # Some variables are initialized by gnulib.mk, and we don't want
90 # that. Change '=' to '+='.
91 if ($lhs_and_assign_op =~ /^(SUBDIRS|EXTRA_DIST|BUILT_SOURCES|SUFFIXES|MOSTLYCLEANFILES|CLEANFILES|DISTCLEANFILES|MAINTAINERCLEANFILES|AM_CFLAGS|AM_CPPFLAGS|AM_GNU_GETTEXT) =/)
93 $lhs_and_assign_op =~ s/=/+=/;
95 # We don't want to inherit gnulib's AUTOMAKE_OPTIONS, comment them.
96 elsif ($lhs_and_assign_op =~ /^AUTOMAKE_OPTIONS =/)
98 $lhs_and_assign_op =~ s/^/# /;
100 # Don't touch suffixes.
101 elsif ($lhs_and_assign_op =~ /^SUFFIXES /)
104 # The words are (probably) paths to files in lib/: prefix them.
107 $rhs = prefix_words
($rhs)
110 # Variables which name depend on the location: libbison_a_SOURCES =>
111 # lib_libbison_a_SOURCES.
112 $lhs_and_assign_op =~ s/($lib_name)/lib_$1/g;
114 return $lhs_and_assign_op . $rhs;
119 # $CONTENTS is a Makefile content. Post-process it so that each file-name
120 # is prefixed with $prefix (e.g., "lib/").
122 # Relies heavily on the regularity of the file generated by gnulib-tool.
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.
131 s
{^([-\w
+/]+\
.[-\w
.]+ *: *\S
.*)$}
132 {prefix_words
($1)}gem
;
134 # Prefix files in variables.
135 s
{^([\w
.]+\s
*\
+?
=)(.*)$}
136 {prefix_assignment
($1, $2)}gem
;
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.
141 s{($prefix){2}}{$1}g;
143 # $(srcdir) is actually $(top_srcdir)/lib.
144 s{\$\(srcdir\)}{\$(top_srcdir)/lib}g;
146 # Sometimes, t-$@ is used instead of $@-t, which, of course, does
147 # not work when we have a $@ with a directory in it.
150 # Some AC_SUBST patterns remain and would better be Make macros.
151 s{\@(MKDIR_P)\@}{\$($1)}g;
153 # Adjust paths in mkdir.
154 s{(\$\(MKDIR_P\))\s*(\w+)}{$1 $prefix$2}g;
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;
175 'lib-name=s' => \
$lib_name,
176 help
=> sub { usage
0 },
177 version
=> sub { print "$ME version $VERSION\n"; exit },
182 or (warn "$ME: no library name; use --lib-name=NAME\n"), $fail = 1;
184 # There must be exactly one argument.
186 and (warn "$ME: missing FILE argument\n"), $fail = 1;
188 and (warn "$ME: too many arguments:\n", join ("\n", @ARGV), "\n"),
194 $prefix = (dirname
$file) . '/';
195 warn "prefix=$prefix\n";
200 ### Setup "GNU" style for perl-mode and cperl-mode.
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
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"