3 # Copyright © 2011 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.
18 my $m4macro = "DX_FIX_GNULIB";
20 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
22 "o|output=s" => \$output,
23 "i|input=s" => \$input,
24 "m|m4output=s" => \$m4output,
25 "M|m4macro=s" => \$m4macro,
28 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
29 open STDIN, "<", $input or die "$input: $!\n" if (defined $input);
31 my $printed_header = 0;
32 my (%allvars, %sourcevars);
42 $file =~ m|(?:.+/)?([^/]+)/?|;
49 if ($word =~ /^\$\(([[:word:].]+)\)$/) {
50 # Don't touch variables now, but record them for later.
52 } elsif ($word =~ /^\$\((?:top_)?(?:srcdir|builddir)\)/) {
53 # Do nothing. Generic transformation will take care of
54 # $(srcdir) and $(builddir).
55 } elsif ($word =~ /^[[:word:].+\/-]+$/) {
56 # Fix up things that look like filenames.
59 print STDERR "$0: warning: unrecognized source file: $word\n";
68 $raw =~ /([^=]+=)[[:space:]]*(.*)/;
69 my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
71 return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
77 $raw =~ /([^:]+):[[:space:]]*(.*)/;
78 my @left = split(/[[:space:]]+/, $1);
79 my @right = split(/[[:space:]]+/, $2);
81 @left = map(mangle_file($_), @left);
82 @right = map(mangle_file($_), @right);
84 return join(" ", @left) . ": " . join(" ", @right) . "\n";
90 if (!$printed_header) {
91 print "# Postprocessed by ", basename($0), "\n\n";
93 # This trick should define gnulib_orderonly to | iff we're using GNU make.
94 gnulib_have_orderonly = $(findstring order-only,$(.FEATURES))
95 gnulib_orderonly = $(gnulib_have_orderonly:order-only=|)
96 gnulib_headers = $(gnulib_orderonly)
103 # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
104 # Since these files are (hopefully!) not created by make, they should
106 drop if (/^MOSTLYCLEANFILES.*core/);
108 # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
109 # useful for non-recursive builds. Strip them out.
110 drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
112 # Rewrite automake hook targets to be more generic.
113 if (s/^(.*)-local:/\1-gnulib:/) {
114 print ".PHONY: $1-gnulib\n";
115 print "$1-local: $1-gnulib\n";
118 # Don't let these targets get confused with filenames below.
122 # We need to mangle filenames in make variables; prepending a lib/ on
123 # relative paths. The following should catch all variable assignments
124 # that need mangling.
125 if (/^([[:word:]]+)[[:space:]]*\+?=/) {
128 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
129 $_ = mangle_variable($_);
133 # BUILT_SOURCES has similar problems to recursive make: inadequate
134 # dependencies lead to incorrect builds. Collect them into an
135 # ordinary variable so we can deal with them later.
136 s/BUILT_SOURCES/gnulib_headers/;
138 # Targets are similar to variables: the target and its dependencies
139 # need to be mangled.
141 $_ = mangle_target($_);
144 # When using conditional-dependencies, *CLEANFILES can end up
145 # depending on the configuration. This means that "make distclean"
146 # may not actually delete everything if the configuration changes
147 # after building the package. Stash all the variables for later so
148 # they can be moved outside of any conditional.
150 push(@cleanfiles, $_);
154 # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
155 s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
156 s:\$\(builddir\):\$\(top_builddir\)/lib:g;
157 } continue { print };
160 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
161 $(gnulib_lt_objects): $(gnulib_headers)
165 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
166 # prepend lib/ if and only if they're not empty. Unfortunately, make is not
167 # powerful to do this, so we need to put this transformation into configure
168 # itself by defining a new autoconf macro.
169 if (defined $m4output) {
170 my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
171 my @vars = $lc->get_unique;
173 open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
175 print M4OUT "dnl This file was generated by fix-gnulib.pl\n";
176 print M4OUT "AC_DEFUN([$m4macro], [dnl\n";
178 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
179 set x $gl_objvar; shift
181 while test ${#} -gt 0; do
182 gl_objvar="$gl_objvar lib/${1}"; shift
188 print M4OUT "$_=\${$_:+lib/\$$_}\n";