]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-gnulib.pl
6663e6e4b49c333eb95b53fcb10b0657f6d7417c
[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";
91                 $printed_header = 1;
92                 drop;
93         }
94
95         # For some reason, gnulib-tool adds core dumps to "make mostlyclean".
96         # Since these files are (hopefully!) not created by make, they should
97         # not be cleaned.
98         drop if (/^MOSTLYCLEANFILES.*core/);
99
100         # Some modules set AM_CPPFLAGS/AM_CFLAGS/etc. in a manner that is not
101         # useful for non-recursive builds.  Strip them out.
102         drop if (/^(AM_CPPFLAGS|AM_CFLAGS)/);
103
104         # Rewrite automake hook targets to be more generic.
105         if (s/^(.*)-local:/\1-gnulib:/) {
106                 print ".PHONY: $1-gnulib\n";
107                 print "$1-local: $1-gnulib\n";
108                 s/$1-generic//;
109
110                 # Don't let these targets get confused with filenames below.
111                 next;
112         }
113
114         # We need to mangle filenames in make variables; prepending a lib/ on
115         # relative paths.  The following should catch all variable assignments
116         # that need mangling.
117         if (/^([[:word:]]+)[[:space:]]*\+?=/) {
118                 $allvars{$1} = 1;
119
120                 if (/_SOURCES|CLEANFILES|EXTRA_DIST|[[:upper:]]+_H/) {
121                         $_ = mangle_variable($_);
122                 }
123         }
124
125         # Targets are similar to variables: the target and its dependencies
126         # need to be mangled.
127         if (/:/) {
128                 $_ = mangle_target($_);
129         }
130
131         # Finally, references to $(srcdir) and $(builddir) need to be fixed up.
132         s:\$\(srcdir\):\$\(top_srcdir\)/lib:g;
133         s:\$\(builddir\):\$\(top_builddir\)/lib:g;
134 } continue { print };
135
136 # Some filenames are AC_SUBSTed by the Gnulib macros, and thus we need to
137 # prepend lib/ if and only if they're not empty.  Unfortunately, make is not
138 # powerful to do this, so we need to put this transformation into configure
139 # itself by defining a new autoconf macro.
140 if (defined $m4output) {
141         my $lc = List::Compare->new('-u', '-a', \%sourcevars, \%allvars);
142         my @vars = $lc->get_unique;
143
144         open(M4OUT, '>', $m4output) or die "$m4output: $!\n";
145
146         print M4OUT "dnl This file was generated by fix-gnulib.pl\n";
147         print M4OUT "AC_DEFUN([$m4macro], [dnl\n";
148         foreach (@vars) {
149                 print M4OUT "$_=\${$_:+lib/\$$_}\n";
150         }
151         print M4OUT "])\n";
152
153         close M4OUT;
154 }