]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-gnulib.pl
fix-gnulib: Eliminate dependency on List::Compare.
[dxcommon.git] / scripts / fix-gnulib.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright © 2011-2012 Nick Bowler
4 #
5 # Prepare the Gnulib tree for inclusion into a non-recursive automake build.
6 # While the output of gnulib-tool is "include"-able if the --makefile-name
7 # option is used, it is still not suitable for non-recursive use for a couple
8 # reasons; chief among them is that filenames are not relative to the top
9 # source directory.
10 #
11 # This script postprocesses the gnulib-tool output to produce something that
12 # is intended to be suitable for inclusion into such non-recursive build
13 # environments.  Since the integration involves both configure.ac and
14 # Makefile.am, the output must be included into _both_.  Supposing the output
15 # is written to lib/gnulib.mk, you would add:
16 #
17 #   m4_include([lib/gnulib.mk]) # to configure.ac, after any call to gl_INIT
18 #   include $(top_srcdir)/lib/gnulib.mk # to Makefile.am
19 #
20 # You must also arrange for the Gnulib-generated header files to be built
21 # before the object files which depend on them; the most robust way to do this
22 # is by explicit prerequisites, for example:
23 #
24 #   bin_PROGRAMS = foo
25 #   $(foo_OBJECTS): $(gnulib_headers)
26 #
27 # The $(gnulib_headers) variable will expand to GNU-make order-only
28 # prerequisites when available, avoiding spurious incremental rebuilds when
29 # unused headers are changed.  If this feature is not available, it will
30 # expand to ordinary prerequisites.  It is therefore only appropriate for use
31 # in target prerequisites; the $(gnulib_raw_headers) variable may be used in
32 # other contexts when only the list of header files is required.
33 #
34 # This script also provides machinery for Gnulib symbol renaming via the
35 # glconfig.mk Makefile.am snippet; use of this feature is optional.
36 #
37 # Most of the specific transformations are documented below.
38 #
39 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
40 # This is free software: you are free to do what the fuck you want to.
41 # There is NO WARRANTY, to the extent permitted by law.
42
43 use strict;
44 use Getopt::Long;
45
46 my $output   = undef;
47 my $input    = undef;
48
49 my $line     = 0;
50
51 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
52 GetOptions(
53         "o|output=s"   => \$output,
54         "i|input=s"    => \$input,
55 );
56
57 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
58 open STDIN,  "<", $input  or die "$input: $!\n"  if (defined $input);
59
60 my $printed_header = 0;
61 my @cleanfiles;
62
63 # Hashes to record make variables used in the automake source.  The allvars
64 # hash contains variables actually assigned in the Makefile, sourcevars
65 # contains variables used as filenames.  Keys are the variable name, and
66 # the value is always set to 1.
67 my (%allvars, %sourcevars);
68
69 sub drop {
70         undef $_;
71         next;
72 }
73
74 sub basename {
75         my $file = shift;
76         $file =~ m|(?:.+/)?([^/]+)/?|;
77         return $1;
78 }
79
80 sub mangle_file {
81         my $word = shift;
82
83         if ($word =~ /^\$\(([[:word:].]+)\)$/) {
84                 # Don't touch variables now, but record them for later.
85                 $sourcevars{$1} = 1;
86         } elsif ($word =~ /^\$\((?:top_)?(?:srcdir|builddir)\)/) {
87                 # Do nothing.  Generic transformation will take care of
88                 # $(srcdir) and $(builddir).
89         } elsif ($word =~ /^[[:word:].+\/-]+$/) {
90                 # Mangle the filename.  Assume that all relevant files start
91                 # with a non-dot (so we don't transform suffix rules) and
92                 # contain at least one dot (so we don't transform phony rules).
93                 $word = "lib/$word" if ($word =~ /^[^.]+\./);
94         } else {
95                 print STDERR "$0:$line: warning: unrecognized source file: $word\n";
96         }
97
98         return "$word";
99 }
100
101 sub mangle_variable {
102         my $raw = shift;
103
104         $raw =~ /([^=]+=)[[:space:]]*(.*)/s;
105         my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
106
107         return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
108 }
109
110 sub mangle_target {
111         my $raw = shift;
112
113         $raw =~ /([^:]+):[[:space:]]*(.*)/s;
114         my @left  = split(/[[:space:]]+/, $1);
115         my @right = split(/[[:space:]]+/, $2);
116
117         @left  = map(mangle_file($_), @left);
118         @right = map(mangle_file($_), @right);
119
120         return join(" ", @left) . ": " . join(" ", @right) . "\n";
121 }
122
123 while (<STDIN>) {
124         $line++;
125
126         # Combine line splices.
127         while (s/\\$//) {
128                 $line++;
129                 $_ = $_ . <STDIN>
130         }
131
132         next if (/^#/);
133
134         if (!$printed_header) {
135                 print "# Postprocessed by ", basename($0), "\n\n";
136                 print <<'EOF';
137 # BEGIN AUTOMAKE/M4 POLYGLOT \
138 m4_unquote(m4_argn([2], [
139 .PHONY: # Automake code follows
140
141 # This trick should define gnulib_orderonly to | iff we're using GNU make.
142 gnulib_have_orderonly = $(findstring order-only,$(.FEATURES))
143 gnulib_orderonly = $(gnulib_have_orderonly:order-only=|)
144 gnulib_core_headers =
145 gnulib_raw_headers = $(gnulib_core_headers)
146 gnulib_headers = $(gnulib_orderonly) $(gnulib_raw_headers)
147
148 # Oddly, gnulib tries to add to MOSTLYCLEANDIRS (which is *not* an automake
149 # variable) without defining it.
150 MOSTLYCLEANDIRS =
151 EOF
152
153                 $printed_header = 1;
154                 drop;
155         }
156
157         # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
158         # Since these files are (hopefully!) not created by make, they should
159         # not be cleaned.
160         drop if (/^MOSTLYCLEANFILES.*core/);
161
162         # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
163         # useful for non-recursive builds.  Strip them out.
164         drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
165
166         # Library dependencies are added automatically to libgnu.la by
167         # gnulib-tool.  Unfortunately, this means that everything linking
168         # against libgnu.la is forced to pull in the same deps, even if they're
169         # unneeded.  Furthermore, a libtool linker flag reordering bug prevents
170         # --as-needed from stripping out the useless deps, so it's better to
171         # handle them all manually.
172         drop if (/LDFLAGS/);
173
174         # Current uses of SUFFIXES in gnulib are pointless since Automake will
175         # figure it out all on its own.  Strip it out.
176         drop if (/SUFFIXES/);
177
178         # Rewrite automake hook targets to be more generic.
179         if (s/^(.*)-local:/\1-gnulib:/) {
180                 print ".PHONY: $1-gnulib\n";
181                 print "$1-local: $1-gnulib\n";
182                 s/$1-generic//;
183         }
184
185         # We need to mangle filenames in make variables; prepending a lib/ on
186         # relative paths.  The following should catch all variable assignments
187         # that need mangling.
188         if (/^([[:word:]]+)[[:space:]]*\+?=/) {
189                 $allvars{$1} = 1;
190
191                 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
192                         $_ = mangle_variable($_);
193                 }
194         }
195
196         # BUILT_SOURCES has similar problems to recursive make: inadequate
197         # dependencies lead to incorrect builds.  Collect them into an
198         # ordinary variable so we can deal with them later.
199         s/BUILT_SOURCES/gnulib_core_headers/;
200
201         # Targets are similar to variables: the target and its dependencies
202         # need to be mangled.
203         if (/^[^\t].*:/) {
204                 $_ = mangle_target($_);
205         }
206
207         # MKDIR_P commands need to be fixed up; in principle Gnulib could also
208         # be patched here to use $(@D) instead (and thus automatically benefit
209         # from the target being fixed up), but this will do for now.
210         s/^(\t.*\$\(MKDIR_P\)) ([[:alnum:]]+)$/\1 lib\/\2/;
211
212         # When using conditional-dependencies, *CLEANFILES can end up
213         # depending on the configuration.  This means that "make distclean"
214         # may not actually delete everything if the configuration changes
215         # after building the package.  Stash all the variables for later so
216         # they can be moved outside of any conditional.
217         if (/(CLEANFILES|CLEANDIRS)[[:space:]]*\+=/) {
218                 push(@cleanfiles, $_);
219                 drop;
220         }
221
222         # Change t-$@ to $@-t as the former will break if $@ has a directory
223         # component.
224         s/t-\$@/\$\@-t/g;
225
226         # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
227         s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
228         s:\$\(builddir\):\$\(top_builddir\)/lib:g;
229 } continue { s/(\n.)/\\\1/g; print; };
230
231 print <<'EOF';
232 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
233 $(gnulib_lt_objects): $(gnulib_headers)
234 EOF
235 print @cleanfiles;
236
237 print <<'EOF';
238 if FALSE
239 ], [dnl M4 code follows
240
241 AC_SUBST([GLSRC], [lib])
242 AC_CONFIG_LIBOBJ_DIR([lib])
243
244 AC_DEFUN_ONCE([DX_GLSYM_PREFIX], [dnl
245 AC_REQUIRE([DX_EXPORTED_SH])
246 AC_SUBST([GLSYM_PREFIX], [$1])
247 ])
248 AC_CONFIG_COMMANDS_PRE([DX_GLSYM_PREFIX([${PACKAGE}__])])
249
250 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
251 set x $gl_objvar; shift
252 gl_objvar=
253 while test ${#} -gt 0; do
254         gl_objvar="$gl_objvar lib/${1}"; shift
255 done
256 ])
257 EOF
258
259 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
260 # prepend lib/ if and only if they're not empty.  Unfortunately, make is not
261 # powerful to do this, so we need to put this transformation into configure
262 # itself by defining a new autoconf macro.
263
264 foreach (keys %sourcevars) {
265         print "$_=\${$_:+lib/\$$_}\n" unless $allvars{$_};
266 }
267
268 print <<'EOF';
269 ], [
270 endif
271 # ]))dnl
272 # END AUTOMAKE/M4 POLYGLOT
273 EOF