]> git.saurik.com Git - bison.git/blob - etc/prefix-gnulib-mk
maint: include <config.h> first
[bison.git] / etc / prefix-gnulib-mk
1 #! /usr/bin/perl -w
2
3 use strict;
4 use IO::File;
5
6 my $prefix = "lib/";
7
8 # contents ($FILE_NAME)
9 # ---------------------
10 sub contents ($)
11 {
12 my ($file) = @_;
13 local $/; # Turn on slurp-mode.
14 my $f = new IO::File "< $file" or die "$file";
15 my $contents = $f->getline or die "$file";
16 $f->close;
17 return $contents;
18 }
19
20 # prefix_word ($WORD)
21 # -------------------
22 # Do not prefix special words such as variable dereferences. Also,
23 # "Makefile" is really "Makefile", since precisely there is no
24 # lib/Makefile.
25 sub prefix_word ($)
26 {
27 local ($_) = @_;
28 $_ = $prefix . $_
29 unless m{^\$\(\w+\)} || $_ eq "Makefile";
30 return $_;
31 }
32
33
34 # prefix_words ($TEXT)
35 # --------------------
36 sub prefix_words ($)
37 {
38 local ($_) = @_;
39 s{(\S+)}{prefix_word($1)}gem;
40 return $_;
41 }
42
43
44 # prefix_assignment ($LHS-AND-ASSIGN-OP, $RHS)
45 # --------------------------------------------
46 sub prefix_assignment ($$)
47 {
48 my ($lhs_and_assign_op, $rhs) = @_;
49 my $res;
50
51 # Some variables are initialized by gnulib.mk, and we don't want
52 # that. Change '=' to '+='.
53 if ($lhs_and_assign_op =~ /^(SUBDIRS|EXTRA_DIST|BUILT_SOURCES|SUFFIXES|MOSTLYCLEANFILES|CLEANFILES|DISTCLEANFILES|MAINTAINERCLEANFILES|AM_CFLAGS|AM_CPPFLAGS|AM_GNU_GETTEXT) =/)
54 {
55 $lhs_and_assign_op =~ s/=/+=/;
56 }
57 # We don't want to inherit gnulib's AUTOMAKE_OPTIONS, comment them.
58 elsif ($lhs_and_assign_op =~ /^AUTOMAKE_OPTIONS =/)
59 {
60 $lhs_and_assign_op =~ s/^/# /;
61 }
62 # Don't touch suffixes.
63 elsif ($lhs_and_assign_op =~ /^SUFFIXES /)
64 {
65 }
66 # The words are (probably) paths to files in lib/: prefix them.
67 else
68 {
69 $rhs = prefix_words($rhs)
70 }
71
72 # Variables which name depend on the location: libbison_a_SOURCES =>
73 # lib_libbison_a_SOURCES.
74 $lhs_and_assign_op =~ s/(libbison)/lib_$1/g;
75
76 return $lhs_and_assign_op . $rhs;
77 }
78
79 # prefix $CONTENTS
80 # ----------------
81 # $CONTENTS is a Makefile content. Post-process it so that each file-name
82 # is prefixed with $prefix (e.g., "lib/").
83 #
84 # Relies heavily on the regularity of the file generated by gnulib-tool.
85 sub prefix ($)
86 {
87 # Work on $_.
88 local ($_) = @_;
89
90 # Prefix all the occurrence of files in rules. If there is nothing
91 # after in the :, it's probably a phony target, or a suffix rule.
92 # Don't touch it.
93 s{^([-\w+/]+\.[-\w.]+ *: *\S.*)$}
94 {prefix_words($1)}gem;
95
96 # Prefix files in variables.
97 s{^([\w.]+\s*\+?=)(.*)$}
98 {prefix_assignment($1, $2)}gem;
99
100 # These three guys escape all the other regular rules.
101 s{(charset\.alias|ref-add\.sed|ref-del\.sed)}{$prefix$1}g;
102 # Unfortunately, as a result we sometimes have lib/lib.
103 s{($prefix){2}}{$1}g;
104
105 # $(srcdir) is actually $(top_srcdir)/lib.
106 s{\$\(srcdir\)}{\$(top_srcdir)/lib}g;
107
108 # Sometimes, t-$@ is used instead of $@-t, which, of course, does
109 # not work when we have a $@ with a directory in it.
110 s{t-\$\@}{\$\@-t}g;
111
112 # Some AC_SUBST patterns remain and would better be Make macros.
113 s{\@(MKDIR_P)\@}{\$($1)}g;
114
115 # Adjust paths in mkdir.
116 s{(\$\(MKDIR_P\))\s*(\w+)}{$1 $prefix$2}g;
117
118 return $_;
119 }
120
121 # process ($IN)
122 # -------------
123 sub process ($)
124 {
125 my ($file) = @_;
126 my ($bak) = "$file.bak";
127 rename ($file, $bak) or die;
128 my $contents = contents ($bak);
129 $contents = prefix ($contents);
130 my $out = new IO::File(">$file") or die;
131 print $out $contents;
132 }
133
134 process ("${prefix}gnulib.mk")
135
136
137 ### Setup "GNU" style for perl-mode and cperl-mode.
138 ## Local Variables:
139 ## perl-indent-level: 2
140 ## perl-continued-statement-offset: 2
141 ## perl-continued-brace-offset: 0
142 ## perl-brace-offset: 0
143 ## perl-brace-imaginary-offset: 0
144 ## perl-label-offset: -2
145 ## cperl-indent-level: 2
146 ## cperl-brace-offset: 0
147 ## cperl-continued-brace-offset: 0
148 ## cperl-label-offset: -2
149 ## cperl-extra-newline-before-brace: t
150 ## cperl-merge-trailing-else: nil
151 ## cperl-continued-statement-offset: 2
152 ## End: