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_core_headers = $(gnulib_orderonly)
97 gnulib_src_headers = $(gnulib_core_headers)
98 gnulib_headers = $(gnulib_src_headers)
105 # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
106 # Since these files are (hopefully!) not created by make, they should
108 drop if (/^MOSTLYCLEANFILES.*core/);
110 # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
111 # useful for non-recursive builds. Strip them out.
112 drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
114 # Rewrite automake hook targets to be more generic.
115 if (s/^(.*)-local:/\1-gnulib:/) {
116 print ".PHONY: $1-gnulib\n";
117 print "$1-local: $1-gnulib\n";
120 # Don't let these targets get confused with filenames below.
124 # We need to mangle filenames in make variables; prepending a lib/ on
125 # relative paths. The following should catch all variable assignments
126 # that need mangling.
127 if (/^([[:word:]]+)[[:space:]]*\+?=/) {
130 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
131 $_ = mangle_variable($_);
135 # BUILT_SOURCES has similar problems to recursive make: inadequate
136 # dependencies lead to incorrect builds. Collect them into an
137 # ordinary variable so we can deal with them later.
138 s/BUILT_SOURCES/gnulib_core_headers/;
140 # Targets are similar to variables: the target and its dependencies
141 # need to be mangled.
143 $_ = mangle_target($_);
146 # When using conditional-dependencies, *CLEANFILES can end up
147 # depending on the configuration. This means that "make distclean"
148 # may not actually delete everything if the configuration changes
149 # after building the package. Stash all the variables for later so
150 # they can be moved outside of any conditional.
152 push(@cleanfiles, $_);
156 # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
157 s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
158 s:\$\(builddir\):\$\(top_builddir\)/lib:g;
159 } continue { print };
162 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
163 $(gnulib_lt_objects): $(gnulib_src_headers)
167 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
168 # prepend lib/ if and only if they're not empty. Unfortunately, make is not
169 # powerful to do this, so we need to put this transformation into configure
170 # itself by defining a new autoconf macro.
171 if (defined $m4output) {
172 my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
173 my @vars = $lc->get_unique;
175 open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
178 dnl This file was generated by fix-gnulib.pl
180 dnl Usage: DX_FIX_GNULIB([symbol-prefix])
181 dnl where symbol-prefix is the application-specific symbol prefix to apply
182 dnl to Gnulib's symbols. Defaults to \${PACKAGE}__.
183 dnl top-level source directory; e.g. lib.
184 AC_DEFUN([$m4macro], [dnl
188 GLSYM_PREFIX='ifelse([$1], [], [${PACKAGE}__], [$1])'
190 AC_SUBST([GLSYM_PREFIX])
192 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
193 set x $gl_objvar; shift
195 while test ${#} -gt 0; do
196 gl_objvar="$gl_objvar lib/${1}"; shift
201 print M4OUT "$_=\${$_:+lib/\$$_}\n";