]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-gnulib.pl
b93cf22b44dc5fe042057ca4b9ec462c7541c106
[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
34 sub drop {
35         undef $_;
36         next;
37 }
38
39 sub basename {
40         my $file = shift;
41         $file =~ m|(?:.+/)?([^/]+)/?|;
42         return $1;
43 }
44
45 sub mangle_file {
46         my $word = shift;
47
48         if ($word =~ /^\$\(([[:word:].]+)\)$/) {
49                 # Don't touch variables now, but record them for later.
50                 $sourcevars{$1} = 1;
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.
56                 $word = "lib/$word";
57         } else {
58                 print STDERR "$0: warning: unrecognized source file: $word\n";
59         }
60
61         return "$word";
62 }
63
64 sub mangle_variable {
65         my $raw = shift;
66
67         $raw =~ /([^=]+=)[[:space:]]*(.*)/;
68         my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
69
70         return join(" ", ($left, map(mangle_file($_), @right))) . "\n";
71 }
72
73 sub mangle_target {
74         my $raw = shift;
75
76         $raw =~ /([^:]+):[[:space:]]*(.*)/;
77         my @left  = split(/[[:space:]]+/, $1);
78         my @right = split(/[[:space:]]+/, $2);
79
80         @left  = map(mangle_file($_), @left);
81         @right = map(mangle_file($_), @right);
82
83         return join(" ", @left) . ": " . join(" ", @right) . "\n";
84 }
85
86 while (<STDIN>) {
87         next if (/^#/);
88
89         if (!$printed_header) {
90                 print "# Postprocessed by ", basename($0), "\n\n";
91                 print <<'EOF';
92 # This trick should define gnulib_orderonly to | iff we're using GNU make.
93 gnulib_have_orderonly = $(findstring order-only,$(.FEATURES))
94 gnulib_orderonly = $(gnulib_have_orderonly:order-only=|)
95 gnulib_headers = $(gnulib_orderonly)
96 EOF
97
98                 $printed_header = 1;
99                 drop;
100         }
101
102         # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
103         # Since these files are (hopefully!) not created by make, they should
104         # not be cleaned.
105         drop if (/^MOSTLYCLEANFILES.*core/);
106
107         # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
108         # useful for non-recursive builds.  Strip them out.
109         drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
110
111         # Rewrite automake hook targets to be more generic.
112         if (s/^(.*)-local:/\1-gnulib:/) {
113                 print ".PHONY: $1-gnulib\n";
114                 print "$1-local: $1-gnulib\n";
115                 s/$1-generic//;
116
117                 # Don't let these targets get confused with filenames below.
118                 next;
119         }
120
121         # We need to mangle filenames in make variables; prepending a lib/ on
122         # relative paths.  The following should catch all variable assignments
123         # that need mangling.
124         if (/^([[:word:]]+)[[:space:]]*\+?=/) {
125                 $allvars{$1} = 1;
126
127                 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
128                         $_ = mangle_variable($_);
129                 }
130         }
131
132         # BUILT_SOURCES has similar problems to recursive make: inadequate
133         # dependencies lead to incorrect builds.  Collect them into an
134         # ordinary variable so we can deal with them later.
135         s/BUILT_SOURCES/gnulib_headers/;
136
137         # Targets are similar to variables: the target and its dependencies
138         # need to be mangled.
139         if (/:/) {
140                 $_ = mangle_target($_);
141         }
142
143         # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
144         s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
145         s:\$\(builddir\):\$\(top_builddir\)/lib:g;
146 } continue { print };
147
148 print "\$(libgnu_la_OBJECTS): \$(gnulib_headers)\n";
149
150 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
151 # prepend lib/ if and only if they're not empty.  Unfortunately, make is not
152 # powerful to do this, so we need to put this transformation into configure
153 # itself by defining a new autoconf macro.
154 if (defined $m4output) {
155         my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
156         my @vars = $lc->get_unique;
157
158         open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
159
160         print M4OUT "dnl This file was generated by fix-gnulib.pl\n";
161         print M4OUT "AC_DEFUN([$m4macro], [dnl\n";
162         foreach (@vars) {
163                 print M4OUT "$_=\${$_:+lib/\$$_}\n";
164         }
165         print M4OUT "])\n";
166
167         close M4OUT;
168 }