]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-gnulib.pl
Avoid gratuitous library dependencies linking gnulib.
[dxcommon.git] / scripts / fix-gnulib.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright © 2011 Nick Bowler
4 #
5 # Prepare the Gnulib tree for inclusion into a non-recursive automake build.
6 #
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.
10
11 use strict;
12 use List::Compare;
13 use Getopt::Long;
14
15 my $output   = undef;
16 my $input    = undef;
17 my $m4output = undef;
18 my $m4macro  = "DX_FIX_GNULIB";
19
20 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
21 GetOptions(
22         "o|output=s"   => \$output,
23         "i|input=s"    => \$input,
24         "m|m4output=s" => \$m4output,
25         "M|m4macro=s"  => \$m4macro,
26 );
27
28 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
29 open STDIN,  "<", $input  or die "$input: $!\n"  if (defined $input);
30
31 my $printed_header = 0;
32 my (%allvars, %sourcevars);
33 my @cleanfiles;
34
35 sub drop {
36         undef $_;
37         next;
38 }
39
40 sub basename {
41         my $file = shift;
42         $file =~ m|(?:.+/)?([^/]+)/?|;
43         return $1;
44 }
45
46 sub mangle_file {
47         my $word = shift;
48
49         if ($word =~ /^\$\(([[:word:].]+)\)$/) {
50                 # Don't touch variables now, but record them for later.
51                 $sourcevars{$1} = 1;
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.
57                 $word = "lib/$word";
58         } else {
59                 print STDERR "$0: warning: unrecognized source file: $word\n";
60         }
61
62         return "$word";
63 }
64
65 sub mangle_variable {
66         my $raw = shift;
67
68         $raw =~ /([^=]+=)[[:space:]]*(.*)/;
69         my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
70
71         return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
72 }
73
74 sub mangle_target {
75         my $raw = shift;
76
77         $raw =~ /([^:]+):[[:space:]]*(.*)/;
78         my @left  = split(/[[:space:]]+/, $1);
79         my @right = split(/[[:space:]]+/, $2);
80
81         @left  = map(mangle_file($_), @left);
82         @right = map(mangle_file($_), @right);
83
84         return join(" ", @left) . ": " . join(" ", @right) . "\n";
85 }
86
87 while (<STDIN>) {
88         next if (/^#/);
89
90         if (!$printed_header) {
91                 print "# Postprocessed by ", basename($0), "\n\n";
92                 print <<'EOF';
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)
99 EOF
100
101                 $printed_header = 1;
102                 drop;
103         }
104
105         # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
106         # Since these files are (hopefully!) not created by make, they should
107         # not be cleaned.
108         drop if (/^MOSTLYCLEANFILES.*core/);
109
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)/);
113
114         # Library dependencies are added automatically to libgnu.la by
115         # gnulib-tool.  Unfortunately, this means that everything linking
116         # against libgnu.la is forced to pull in the same deps, even if they're
117         # unneeded.  Furthermore, a libtool linker flag reordering bug prevents
118         # --as-needed from stripping out the useless deps, so it's better to
119         # handle them all manually.
120         drop if (/LDFLAGS/);
121
122         # Rewrite automake hook targets to be more generic.
123         if (s/^(.*)-local:/\1-gnulib:/) {
124                 print ".PHONY: $1-gnulib\n";
125                 print "$1-local: $1-gnulib\n";
126                 s/$1-generic//;
127
128                 # Don't let these targets get confused with filenames below.
129                 next;
130         }
131
132         # We need to mangle filenames in make variables; prepending a lib/ on
133         # relative paths.  The following should catch all variable assignments
134         # that need mangling.
135         if (/^([[:word:]]+)[[:space:]]*\+?=/) {
136                 $allvars{$1} = 1;
137
138                 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
139                         $_ = mangle_variable($_);
140                 }
141         }
142
143         # BUILT_SOURCES has similar problems to recursive make: inadequate
144         # dependencies lead to incorrect builds.  Collect them into an
145         # ordinary variable so we can deal with them later.
146         s/BUILT_SOURCES/gnulib_core_headers/;
147
148         # Targets are similar to variables: the target and its dependencies
149         # need to be mangled.
150         if (/:/) {
151                 $_ = mangle_target($_);
152         }
153
154         # When using conditional-dependencies, *CLEANFILES can end up
155         # depending on the configuration.  This means that "make distclean"
156         # may not actually delete everything if the configuration changes
157         # after building the package.  Stash all the variables for later so
158         # they can be moved outside of any conditional.
159         if (/CLEANFILES/) {
160                 push(@cleanfiles, $_);
161                 drop;
162         }
163
164         # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
165         s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
166         s:\$\(builddir\):\$\(top_builddir\)/lib:g;
167 } continue { print };
168
169 print <<'EOF';
170 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
171 $(gnulib_lt_objects): $(gnulib_src_headers)
172 EOF
173 print @cleanfiles;
174
175 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
176 # prepend lib/ if and only if they're not empty.  Unfortunately, make is not
177 # powerful to do this, so we need to put this transformation into configure
178 # itself by defining a new autoconf macro.
179 if (defined $m4output) {
180         my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
181         my @vars = $lc->get_unique;
182
183         open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
184
185         print M4OUT <<EOF;
186 dnl This file was generated by fix-gnulib.pl
187 dnl
188 dnl Usage: DX_FIX_GNULIB([symbol-prefix])
189 dnl   where symbol-prefix is the application-specific symbol prefix to apply
190 dnl   to Gnulib's symbols.  Defaults to \${PACKAGE}__.
191 dnl   top-level source directory; e.g. lib.
192 AC_DEFUN([$m4macro], [dnl
193 EOF
194         print M4OUT <<'EOF';
195 GLSRC=lib
196 GLSYM_PREFIX='ifelse([$1], [], [${PACKAGE}__], [$1])'
197 AC_SUBST([GLSRC])
198 AC_SUBST([GLSYM_PREFIX])
199
200 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
201 set x $gl_objvar; shift
202 gl_objvar=
203 while test ${#} -gt 0; do
204         gl_objvar="$gl_objvar lib/${1}"; shift
205 done
206 ])
207 EOF
208         foreach (@vars) {
209                 print M4OUT "$_=\${$_:+lib/\$$_}\n";
210         }
211         print M4OUT "])\n";
212
213         close M4OUT;
214 }