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