3 # Copyright © 2011-2012 Nick Bowler
5 # Prepare the Gnulib tree for inclusion into a non-recursive automake build.
7 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
8 # This is free software: you are free to do what the fuck you want to.
9 # There is NO WARRANTY, to the extent permitted by law.
20 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
22 "o|output=s" => \$output,
23 "i|input=s" => \$input,
26 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
27 open STDIN, "<", $input or die "$input: $!\n" if (defined $input);
29 my $printed_header = 0;
30 my (%allvars, %sourcevars);
40 $file =~ m|(?:.+/)?([^/]+)/?|;
47 if ($word =~ /^\$\(([[:word:].]+)\)$/) {
48 # Don't touch variables now, but record them for later.
50 } elsif ($word =~ /^\$\((?:top_)?(?:srcdir|builddir)\)/) {
51 # Do nothing. Generic transformation will take care of
52 # $(srcdir) and $(builddir).
53 } elsif ($word =~ /^[[:word:].+\/-]+$/) {
54 # Fix up things that look like filenames.
57 print STDERR "$0:$line: warning: unrecognized source file: $word\n";
66 $raw =~ /([^=]+=)[[:space:]]*(.*)/s;
67 my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
69 return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
75 $raw =~ /([^:]+):[[:space:]]*(.*)/s;
76 my @left = split(/[[:space:]]+/, $1);
77 my @right = split(/[[:space:]]+/, $2);
79 @left = map(mangle_file($_), @left);
80 @right = map(mangle_file($_), @right);
82 return join(" ", @left) . ": " . join(" ", @right) . "\n";
88 # Combine line splices.
96 if (!$printed_header) {
97 print "# Postprocessed by ", basename($0), "\n\n";
99 # BEGIN AUTOMAKE/M4 POLYGLOT \
100 m4_unquote(m4_argn([2], [
101 .PHONY: # Automake code follows
103 # This trick should define gnulib_orderonly to | iff we're using GNU make.
104 gnulib_have_orderonly = $(findstring order-only,$(.FEATURES))
105 gnulib_orderonly = $(gnulib_have_orderonly:order-only=|)
106 gnulib_core_headers = $(gnulib_orderonly)
107 gnulib_src_headers = $(gnulib_core_headers)
108 gnulib_headers = $(gnulib_src_headers)
115 # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
116 # Since these files are (hopefully!) not created by make, they should
118 drop if (/^MOSTLYCLEANFILES.*core/);
120 # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
121 # useful for non-recursive builds. Strip them out.
122 drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
124 # Library dependencies are added automatically to libgnu.la by
125 # gnulib-tool. Unfortunately, this means that everything linking
126 # against libgnu.la is forced to pull in the same deps, even if they're
127 # unneeded. Furthermore, a libtool linker flag reordering bug prevents
128 # --as-needed from stripping out the useless deps, so it's better to
129 # handle them all manually.
132 # Rewrite automake hook targets to be more generic.
133 if (s/^(.*)-local:/\1-gnulib:/) {
134 print ".PHONY: $1-gnulib\n";
135 print "$1-local: $1-gnulib\n";
138 # Don't let these targets get confused with filenames below.
142 # We need to mangle filenames in make variables; prepending a lib/ on
143 # relative paths. The following should catch all variable assignments
144 # that need mangling.
145 if (/^([[:word:]]+)[[:space:]]*\+?=/) {
148 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
149 $_ = mangle_variable($_);
153 # BUILT_SOURCES has similar problems to recursive make: inadequate
154 # dependencies lead to incorrect builds. Collect them into an
155 # ordinary variable so we can deal with them later.
156 s/BUILT_SOURCES/gnulib_core_headers/;
158 # Targets are similar to variables: the target and its dependencies
159 # need to be mangled.
161 $_ = mangle_target($_);
164 # When using conditional-dependencies, *CLEANFILES can end up
165 # depending on the configuration. This means that "make distclean"
166 # may not actually delete everything if the configuration changes
167 # after building the package. Stash all the variables for later so
168 # they can be moved outside of any conditional.
170 push(@cleanfiles, $_);
174 # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
175 s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
176 s:\$\(builddir\):\$\(top_builddir\)/lib:g;
177 } continue { s/(\n.)/\\\1/g; print; };
180 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
181 $(gnulib_lt_objects): $(gnulib_src_headers)
185 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
186 # prepend lib/ if and only if they're not empty. Unfortunately, make is not
187 # powerful to do this, so we need to put this transformation into configure
188 # itself by defining a new autoconf macro.
190 my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
191 my @vars = $lc->get_unique;
195 ], [dnl M4 code follows
197 AC_SUBST([GLSRC], [lib])
198 AC_DEFUN_ONCE([DX_GLSYM_PREFIX], [AC_SUBST([GLSYM_PREFIX], [$1])])
199 AC_CONFIG_COMMANDS_PRE([DX_GLSYM_PREFIX([${PACKAGE}__])])
201 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
202 set x $gl_objvar; shift
204 while test ${#} -gt 0; do
205 gl_objvar="$gl_objvar lib/${1}"; shift
211 print "$_=\${$_:+lib/\$$_}\n";
218 # END AUTOMAKE/M4 POLYGLOT