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);
41 $file =~ m|(?:.+/)?([^/]+)/?|;
48 if ($word =~ /^\$\(([[:word:].]+)\)$/) {
49 # Don't touch variables now, but record them for later.
51 } elsif ($word =~ /^\$\((?:top_)?(?:srcdir|builddir)\)/) {
52 # Do nothing. Generic transformation will take care of
53 # $(srcdir) and $(builddir).
54 } elsif ($word =~ /^[[:word:].+\/-]+$/) {
55 # Fix up things that look like filenames.
58 print STDERR "$0: warning: unrecognized source file: $word\n";
67 $raw =~ /([^=]+=)[[:space:]]*(.*)/;
68 my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
70 return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
76 $raw =~ /([^:]+):[[:space:]]*(.*)/;
77 my @left = split(/[[:space:]]+/, $1);
78 my @right = split(/[[:space:]]+/, $2);
80 @left = map(mangle_file($_), @left);
81 @right = map(mangle_file($_), @right);
83 return join(" ", @left) . ": " . join(" ", @right) . "\n";
89 if (!$printed_header) {
90 print "# Postprocessed by ", basename($0), "\n\n";
92 # This trick should define gnulib_orderonly to | iff we're using GNU make.
93 gnulib_have_orderonly = $(findstring order-only,$(.FEATURES))
94 gnulib_orderonly = $(gnulib_have_orderonly:order-only=|)
95 gnulib_headers = $(gnulib_orderonly)
102 # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
103 # Since these files are (hopefully!) not created by make, they should
105 drop if (/^MOSTLYCLEANFILES.*core/);
107 # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
108 # useful for non-recursive builds. Strip them out.
109 drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
111 # Rewrite automake hook targets to be more generic.
112 if (s/^(.*)-local:/\1-gnulib:/) {
113 print ".PHONY: $1-gnulib\n";
114 print "$1-local: $1-gnulib\n";
117 # Don't let these targets get confused with filenames below.
121 # We need to mangle filenames in make variables; prepending a lib/ on
122 # relative paths. The following should catch all variable assignments
123 # that need mangling.
124 if (/^([[:word:]]+)[[:space:]]*\+?=/) {
127 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
128 $_ = mangle_variable($_);
132 # BUILT_SOURCES has similar problems to recursive make: inadequate
133 # dependencies lead to incorrect builds. Collect them into an
134 # ordinary variable so we can deal with them later.
135 s/BUILT_SOURCES/gnulib_headers/;
137 # Targets are similar to variables: the target and its dependencies
138 # need to be mangled.
140 $_ = mangle_target($_);
143 # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
144 s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
145 s:\$\(builddir\):\$\(top_builddir\)/lib:g;
146 } continue { print };
148 print "\$(libgnu_la_OBJECTS): \$(gnulib_headers)\n";
150 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
151 # prepend lib/ if and only if they're not empty. Unfortunately, make is not
152 # powerful to do this, so we need to put this transformation into configure
153 # itself by defining a new autoconf macro.
154 if (defined $m4output) {
155 my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
156 my @vars = $lc->get_unique;
158 open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
160 print M4OUT "dnl This file was generated by fix-gnulib.pl\n";
161 print M4OUT "AC_DEFUN([$m4macro], [dnl\n";
163 print M4OUT "$_=\${$_:+lib/\$$_}\n";