]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-gnulib.pl
Move all Gnulib CLEANFILES variables to the end of the makefile.
[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_headers = $(gnulib_orderonly)
97 EOF
98
99                 $printed_header = 1;
100                 drop;
101         }
102
103         # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
104         # Since these files are (hopefully!) not created by make, they should
105         # not be cleaned.
106         drop if (/^MOSTLYCLEANFILES.*core/);
107
108         # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
109         # useful for non-recursive builds.  Strip them out.
110         drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
111
112         # Rewrite automake hook targets to be more generic.
113         if (s/^(.*)-local:/\1-gnulib:/) {
114                 print ".PHONY: $1-gnulib\n";
115                 print "$1-local: $1-gnulib\n";
116                 s/$1-generic//;
117
118                 # Don't let these targets get confused with filenames below.
119                 next;
120         }
121
122         # We need to mangle filenames in make variables; prepending a lib/ on
123         # relative paths.  The following should catch all variable assignments
124         # that need mangling.
125         if (/^([[:word:]]+)[[:space:]]*\+?=/) {
126                 $allvars{$1} = 1;
127
128                 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
129                         $_ = mangle_variable($_);
130                 }
131         }
132
133         # BUILT_SOURCES has similar problems to recursive make: inadequate
134         # dependencies lead to incorrect builds.  Collect them into an
135         # ordinary variable so we can deal with them later.
136         s/BUILT_SOURCES/gnulib_headers/;
137
138         # Targets are similar to variables: the target and its dependencies
139         # need to be mangled.
140         if (/:/) {
141                 $_ = mangle_target($_);
142         }
143
144         # When using conditional-dependencies, *CLEANFILES can end up
145         # depending on the configuration.  This means that "make distclean"
146         # may not actually delete everything if the configuration changes
147         # after building the package.  Stash all the variables for later so
148         # they can be moved outside of any conditional.
149         if (/CLEANFILES/) {
150                 push(@cleanfiles, $_);
151                 drop;
152         }
153
154         # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
155         s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
156         s:\$\(builddir\):\$\(top_builddir\)/lib:g;
157 } continue { print };
158
159 print <<'EOF';
160 gnulib_lt_objects = $(libgnu_la_OBJECTS) $(gl_LTLIBOBJS)
161 $(gnulib_lt_objects): $(gnulib_headers)
162 EOF
163 print @cleanfiles;
164
165 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
166 # prepend lib/ if and only if they're not empty.  Unfortunately, make is not
167 # powerful to do this, so we need to put this transformation into configure
168 # itself by defining a new autoconf macro.
169 if (defined $m4output) {
170         my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
171         my @vars = $lc->get_unique;
172
173         open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
174
175         print M4OUT "dnl This file was generated by fix-gnulib.pl\n";
176         print M4OUT "AC_DEFUN([$m4macro], [dnl\n";
177         print M4OUT <<'EOF';
178 m4_foreach([gl_objvar], [[gl_LIBOBJS], [gl_LTLIBOBJS]], [dnl
179 set x $gl_objvar; shift
180 gl_objvar=
181 while test ${#} -gt 0; do
182         gl_objvar="$gl_objvar lib/${1}"; shift
183 done
184 ])
185 EOF
186
187         foreach (@vars) {
188                 print M4OUT "$_=\${$_:+lib/\$$_}\n";
189         }
190         print M4OUT "])\n";
191
192         close M4OUT;
193 }