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";
95 # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
96 # Since these files are (hopefully!) not created by make, they should
98 drop if (/^MOSTLYCLEANFILES.*core/);
100 # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
101 # useful for non-recursive builds. Strip them out.
102 drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
104 # Rewrite automake hook targets to be more generic.
105 if (s/^(.*)-local:/\1-gnulib:/) {
106 print ".PHONY: $1-gnulib\n";
107 print "$1-local: $1-gnulib\n";
110 # Don't let these targets get confused with filenames below.
114 # We need to mangle filenames in make variables; prepending a lib/ on
115 # relative paths. The following should catch all variable assignments
116 # that need mangling.
117 if (/^([[:word:]]+)[[:space:]]*\+?=/) {
120 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
121 $_ = mangle_variable($_);
125 # Targets are similar to variables: the target and its dependencies
126 # need to be mangled.
128 $_ = mangle_target($_);
131 # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
132 s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
133 s:\$\(builddir\):\$\(top_builddir\)/lib:g;
134 } continue { print };
136 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
137 # prepend lib/ if and only if they're not empty. Unfortunately, make is not
138 # powerful to do this, so we need to put this transformation into configure
139 # itself by defining a new autoconf macro.
140 if (defined $m4output) {
141 my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
142 my @vars = $lc->get_unique;
144 open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
146 print M4OUT "dnl This file was generated by fix-gnulib.pl\n";
147 print M4OUT "AC_DEFUN([$m4macro], [dnl\n";
149 print M4OUT "$_=\${$_:+lib/\$$_}\n";