]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-gnulib.pl
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / scripts / fix-gnulib.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright © 2011-2014, 2020-2023 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
12 # that is intended to be suitable for inclusion into such non-recursive
13 # build environments.  Since the integration involves both configure.ac
14 # and Makefile.am, the output must be included into _both_.  Supposing
15 # the output 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
22 # this 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
31 # use in target prerequisites; the $(gnulib_raw_headers) variable may be
32 # used in 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 $use_libtool = undef;
50 my $for_library = undef;
51
52 my $line     = 0;
53
54 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
55 GetOptions(
56         "o|output=s"   => \$output,
57         "i|input=s"    => \$input,
58         "library"      => sub { $for_library = 1; },
59         "program"      => sub { $for_library = 0; },
60 );
61
62 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
63 open STDIN,  "<", $input  or die "$input: $!\n"  if (defined $input);
64
65 my $printed_header = 0;
66 my @cleanfiles;
67
68 # Hashes to record make variables used in the automake source.  The allvars
69 # hash contains variables actually assigned in the Makefile, sourcevars
70 # contains variables used as filenames.  Keys are the variable name, and
71 # the value is always set to 1.
72 my (%allvars, %sourcevars);
73
74 # Collected names of subdirectories that may need to be created at build time.
75 # The keys are directory names, the values are targets.
76 my %gl_dirstamps;
77
78 # State to drop MKDIR_P lines that have been replaced by dirstamps.
79 my ($have_dirstamp) = (0);
80
81 sub drop {
82         undef $_;
83         next;
84 }
85
86 sub basename {
87         my $file = shift;
88         $file =~ m|(?:.+/)?([^/]+)/?|;
89         return $1;
90 }
91
92 sub mangle_file {
93         my $word = shift;
94
95         if ($word =~ /^\$\(([[:word:].]+)\)$/) {
96                 # Don't touch variables now, but record them for later.
97                 $sourcevars{$1} = 1;
98         } elsif ($word =~ /^\$\((?:top_)?(?:srcdir|builddir)\)/) {
99                 # Do nothing.  Generic transformation will take care of
100                 # $(srcdir) and $(builddir).
101         } elsif ($word =~ /^[[:word:].+\/-]+$/) {
102                 # Mangle the filename.  Assume that all relevant files start
103                 # with a non-dot (so we don't transform suffix rules) and
104                 # contain at least one dot (so we don't transform phony rules).
105                 $word = "lib/$word" if ($word =~ /^[^.]+\./);
106         } else {
107                 print STDERR "$0:$line: warning: unrecognized source file: $word\n";
108         }
109
110         return "$word";
111 }
112
113 sub mangle_variable {
114         my $raw = shift;
115
116         $raw =~ /([^=]+=)[[:space:]]*(.*)/s;
117         my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
118
119         return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
120 }
121
122 sub mangle_target {
123         my $raw = shift;
124
125         $raw =~ /([^:]+):[[:space:]]*(.*)/s;
126         my @left  = split(/[[:space:]]+/, $1);
127         my @right = split(/[[:space:]]+/, $2);
128
129         @left  = map(mangle_file($_), @left);
130         @right = map(mangle_file($_), @right);
131
132         my @dirstamps = get_dirstamps(@left);
133
134         return join(" ", @left) . ": " . join(" ", @dirstamps, @right) . "\n";
135 }
136
137 sub get_dirstamps {
138         my %h;
139
140         foreach (@_) {
141                 next unless $_[0] =~ m|^(lib(/.*)?)/[^/]*$|;
142
143                 $h{$gl_dirstamps{$1} = "$1/\$(am__dirstamp)"} = 1;
144         }
145
146         return keys %h;
147 }
148
149 while (<STDIN>) {
150         $line++;
151
152         # Combine line splices.
153         while (s/\\$//) {
154                 $line++;
155                 $_ = $_ . <STDIN>
156         }
157
158         next if (/^#/);
159
160         if (!$printed_header) {
161                 print "# Postprocessed by ", basename($0), "\n\n";
162                 print <<'EOF';
163 # BEGIN AUTOMAKE/M4 POLYGLOT \
164 m4_unquote(m4_argn([2], [
165 .PHONY: # Automake code follows
166
167 # This trick should define gnulib_orderonly to | iff we're using GNU make.
168 gnulib_make_features = $(.FEATURES)
169 gnulib_have_orderonly = $(findstring order-only,${gnulib_make_features})
170 gnulib_orderonly = $(gnulib_have_orderonly:order-only=|)
171 gnulib_core_headers =
172 gnulib_raw_headers = $(gnulib_core_headers)
173 gnulib_headers = $(gnulib_orderonly) $(gnulib_raw_headers)
174
175 # Oddly, gnulib tries to add to MOSTLYCLEANDIRS (which is *not* an automake
176 # variable) without defining it.
177 MOSTLYCLEANDIRS =
178 EOF
179
180                 $printed_header = 1;
181                 drop;
182         }
183
184         # Locate the libgnu definition to determine whether or not the user is
185         # using libtool mode in gnulib-tool.  Default to program mode if they
186         # are not, which will avoid pulling in the glsym dependencies.
187         #
188         # Convert noinst to EXTRA, that way libgnu will not be built unless
189         # something actually depends on it (which is typically the case).
190         if (/^noinst_LIBRARIES.*libgnu.a/) {
191                 s/^noinst/EXTRA/;
192                 $for_library = 0 unless defined $for_library;
193                 $use_libtool = 0;
194         }
195         if (/^noinst_LTLIBRARIES.*libgnu.la/) {
196                 s/^noinst/EXTRA/;
197                 $for_library = 1 unless defined $for_library;
198                 $use_libtool = 1;
199         }
200
201         # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
202         # Since these files are (hopefully!) not created by make, they should
203         # not be cleaned.
204         drop if (/^MOSTLYCLEANFILES.*core/);
205
206         # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
207         # useful for non-recursive builds.  Strip them out.
208         drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
209
210         # We don't care about upstream warning flags that just result in adding
211         # massive amounts of additional build rules for no reason.
212         if (/_CFLAGS/) {
213                 s/ *\$\(GL_CFLAG_GNULIB_WARNINGS\)// if /_CFLAGS\s*=/;
214         }
215
216         # Drop superfluous CFLAGS assignments (which may be created by above
217         # transformation).
218         drop if /_CFLAGS\s*=\s*\$\(AM_CFLAGS\)\s*$/;
219
220         # Library dependencies are added automatically to libgnu.la by
221         # gnulib-tool.  Unfortunately, this means that everything linking
222         # against libgnu.la is forced to pull in the same deps, even if they're
223         # unneeded.  Furthermore, a libtool linker flag reordering bug prevents
224         # --as-needed from stripping out the useless deps, so it's better to
225         # handle them all manually.
226         drop if (/LDFLAGS/);
227
228         # Current uses of SUFFIXES in gnulib are pointless since Automake will
229         # figure it out all on its own.  Strip it out.
230         drop if (/SUFFIXES/);
231
232         # Rewrite automake hook targets to be more generic.
233         if (s/^(.*)-local:/\1-gnulib:/) {
234                 print ".PHONY: $1-gnulib\n";
235                 print "$1-local: $1-gnulib\n";
236                 s/$1-generic//;
237         }
238
239         # We need to mangle filenames in make variables; prepending a lib/ on
240         # relative paths.  The following should catch all variable assignments
241         # that need mangling.
242         if (/^([[:word:]]+)[[:space:]]*\+?=/) {
243                 $allvars{$1} = 1;
244
245                 if ($1 =~ /(_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H)$/) {
246                         $_ = mangle_variable($_);
247                 }
248         }
249
250         # BUILT_SOURCES has similar problems to recursive make: inadequate
251         # dependencies lead to incorrect builds.  Collect them into an
252         # ordinary variable so we can deal with them later.
253         s/BUILT_SOURCES/gnulib_core_headers/;
254
255         # Targets are similar to variables: the target and its dependencies
256         # need to be mangled.
257         if (/^([^\t:]*):/) {
258                 $_ = mangle_target($_);
259                 $have_dirstamp = /am__dirstamp/;
260         }
261
262         # MKDIR_P commands need to be fixed up; in principle Gnulib could also
263         # be patched here to use $(@D) instead (and thus automatically benefit
264         # from the target being fixed up), but this will do for now.
265         s/^(\t.*\$\(MKDIR_P\)) ([[:alnum:]]+)$/\1 lib\/\2/;
266
267         # When using conditional-dependencies, *CLEANFILES can end up
268         # depending on the configuration.  This means that "make distclean"
269         # may not actually delete everything if the configuration changes
270         # after building the package.  Stash all the variables for later so
271         # they can be moved outside of any conditional.
272         if (/(CLEANFILES|CLEANDIRS)[[:space:]]*\+=/) {
273                 push(@cleanfiles, $_);
274                 drop;
275         }
276
277         # Change t-$@ to $@-t as the former will break if $@ has a directory
278         # component.
279         s/t-\$@/\$\@-t/g;
280
281         # $(srcdir), $(builddir) and %reldir% need to be fixed up.
282         s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
283         s:\$\(builddir\):\$\(top_builddir\)/lib:g;
284         s:%reldir%:lib:g;
285
286         # If we installed a dirstamp prerequisite for this target, don't
287         # emit the mkdir line which creates the output directory.
288         if ($have_dirstamp && m|\$[({]MKDIR_P[})][ '"]*lib/|) {
289                 drop unless s/^(\t\$[({](AM_V_GEN|gl_V_at)[})]).*/\1:/;
290         }
291         undef $have_dirstamp if /^\t/;
292 } continue { s/(\n.)/\\\1/g; print; };
293
294 # Define a bunch of fake programs which will ensure Automake produces the
295 # necessary dirstamp rules, as unfortunately we cannot know in advance which
296 # will be generated, and the usual Automake behaviour where generated rules
297 # are suppressed by rules in Makefile.am doesn't actaully work for these.
298 print <<EOF foreach (keys %gl_dirstamps);
299 EXTRA_PROGRAMS += $_/gl-dirstamp
300 ${\(y|/|_|r)}_gl_dirstamp_SOURCES =
301 EOF
302
303 print <<'EOF' if ($use_libtool);
304 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
305 gnulib_objects = $(gnulib_lt_objects)
306 gnulib_all_symfiles = $(gnulib_lt_objects:.lo=.glsym)
307 $(gnulib_objects): $(gnulib_headers)
308 EOF
309 print <<'EOF' if (!$use_libtool);
310 gnulib_objects = $(libgnu_a_OBJECTS) $(gl_LIBOBJS)
311 gnulib_all_symfiles = $(gnulib_objects:.@OBJEXT@=.glsym)
312 $(gnulib_objects): $(gnulib_headers)
313 EOF
314
315 print @cleanfiles;
316
317 print <<'EOF';
318 if FALSE
319 ], [dnl M4 code follows
320
321 AC_SUBST([GLSRC], [lib])
322 AC_CONFIG_LIBOBJ_DIR([lib])
323
324 AC_DEFUN_ONCE([DX_GLSYM_PREFIX],
325 [AC_REQUIRE([DX_AUTOMAKE_COMPAT])AC_REQUIRE([DX_EXPORTED_SH])dnl
326 AC_SUBST([GLSYM_PREFIX], [$1])dnl
327 AC_SUBST([gnulib_symfiles], ['$(gnulib_all_symfiles)'])])
328 EOF
329
330 print <<'EOF' if ($for_library);
331 AC_CONFIG_COMMANDS_PRE([DX_GLSYM_PREFIX([${PACKAGE}__])])
332 EOF
333
334 print <<'EOF';
335 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
336 set x $gl_objvar; shift
337 gl_objvar=
338 while test ${#} -gt 0; do
339         gl_objvar="$gl_objvar lib/${1}"; shift
340 done
341 ])
342 EOF
343
344 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
345 # prepend lib/ if and only if they're not empty.  Unfortunately, make is not
346 # powerful to do this, so we need to put this transformation into configure
347 # itself by defining a new autoconf macro.
348
349 foreach (keys %sourcevars) {
350         print "$_=\${$_:+lib/\$$_}\n" unless $allvars{$_};
351 }
352
353 print <<'EOF';
354 ], [
355 endif
356 # ]))dnl
357 # END AUTOMAKE/M4 POLYGLOT
358 EOF