joachim99@2
|
1 #!/usr/bin/perl
|
joachim99@2
|
2 # a script for use by autoconf to make the Makefiles
|
joachim99@2
|
3 # from the Makefile.in's
|
joachim99@2
|
4 #
|
joachim99@2
|
5 # the original autoconf mechanism first splits all substitutions into groups
|
joachim99@2
|
6 # of ca. 90, and than invokes sed for _every_ Makefile.in and every group
|
joachim99@2
|
7 # (so around 2-3 times per Makefile.in). So this takes forever, as sed
|
joachim99@2
|
8 # has to recompile the regexps every time.
|
joachim99@2
|
9 #
|
joachim99@2
|
10 # this script does better. It changes all Makefile.ins in one process.
|
joachim99@2
|
11 # in kdelibs the time for building Makefile went down from 2:59 min to 13 sec!
|
joachim99@2
|
12 #
|
joachim99@2
|
13 # written by Michael Matz <matz@kde.org>
|
joachim99@2
|
14 # adapted by Dirk Mueller <mueller@kde.org>
|
joachim99@2
|
15 #
|
joachim99@2
|
16 # the first part was done by looking at the config.status files generated
|
joachim99@2
|
17 # by configure.
|
joachim99@2
|
18 #
|
joachim99@2
|
19
|
joachim99@2
|
20 my $ac_subs=$ARGV[0];
|
joachim99@2
|
21 my $ac_sacfiles = $ARGV[1];
|
joachim99@2
|
22 my $ac_given_srcdir=$ARGV[2];
|
joachim99@2
|
23 my $ac_given_INSTALL=$ARGV[3];
|
joachim99@2
|
24
|
joachim99@2
|
25 #print "ac_subs=$ac_subs\n";
|
joachim99@2
|
26 #print "ac_sacfiles=$ac_sacfiles\n";
|
joachim99@2
|
27 #print "ac_given_srcdir=$ac_given_srcdir\n";
|
joachim99@2
|
28 #print "ac_given_INSTALL=$ac_given_INSTALL\n";
|
joachim99@2
|
29
|
joachim99@2
|
30 my ($srcdir, $top_srcdir);
|
joachim99@2
|
31 my $INSTALL;
|
joachim99@2
|
32 my $bad_perl = ($] < 5.005);
|
joachim99@2
|
33
|
joachim99@2
|
34 open(CF, "< $ac_subs") || die "can't open $ac_subs: $!";
|
joachim99@2
|
35 my @subs = <CF>;
|
joachim99@2
|
36 close(CF);
|
joachim99@2
|
37 chomp @subs;
|
joachim99@2
|
38 @comp_match=();
|
joachim99@2
|
39 @comp_subs=();
|
joachim99@2
|
40
|
joachim99@2
|
41 if ($bad_perl) {
|
joachim99@2
|
42 print "Using perl older than version 5.005\n";
|
joachim99@2
|
43 foreach my $pat (@subs) {
|
joachim99@2
|
44 if ( ($pat =~ /s%([^%]*)%([^%]*)%g/ )
|
joachim99@2
|
45 || ($pat =~ m%/([^/]*)/([^/]*)/g% )
|
joachim99@2
|
46 || ($pat =~ /s%([^%]*)%([^%]*)%;t/ )
|
joachim99@2
|
47 || ($pat =~ m%/([^/]*)/([^/]*)/;t% )
|
joachim99@2
|
48 || ($pat =~ /s,([^,]*),(.*),;t/)
|
joachim99@2
|
49 ) {
|
joachim99@2
|
50 # form : s%bla%blubb%g
|
joachim99@2
|
51 # or s%bla%blubb%;t t (autoconf > 2.13 and < 2.52 ?)
|
joachim99@2
|
52 # or s,bla,blubb,;t t (autoconf 2.52)
|
joachim99@2
|
53 my $srch = $1;
|
joachim99@2
|
54 my $repl = $2;
|
joachim99@2
|
55 $repl =~ s/\\(.)/$1/g;
|
joachim99@2
|
56 push @comp_subs, make_closure($srch, $repl);
|
joachim99@2
|
57
|
joachim99@2
|
58 } elsif ( ($pat =~ /%([^%]*)%d/ )
|
joachim99@2
|
59 || ($pat =~ m%/([^/]*)/d% )
|
joachim99@2
|
60 ) {
|
joachim99@2
|
61 push @comp_subs, make_closure($1, "");
|
joachim99@2
|
62 } else {
|
joachim99@2
|
63 die "Uhh. Malformed pattern in $ac_subs ($pat)"
|
joachim99@2
|
64 unless ( $pat =~ /^\s*$/ ); # ignore white lines
|
joachim99@2
|
65 }
|
joachim99@2
|
66 }
|
joachim99@2
|
67 } else {
|
joachim99@2
|
68 foreach my $pat (@subs) {
|
joachim99@2
|
69 if ( ($pat =~ /s%([^%]*)%([^%]*)%g/ ) ||
|
joachim99@2
|
70 ($pat =~ /s%([^%]*)%([^%]*)%;t/ ) ||
|
joachim99@2
|
71 ($pat =~ /s,([^,]*),(.*),;t/) ) {
|
joachim99@2
|
72 # form : s%bla%blubb%g
|
joachim99@2
|
73 # or s%bla%blubb%;t t (autoconf > 2.13 and < 2.52 ?)
|
joachim99@2
|
74 # or s,bla,blubb,;t t (autoconf 2.52)
|
joachim99@2
|
75 my $srch = $1;
|
joachim99@2
|
76 my $repl = $2;
|
joachim99@2
|
77 push @comp_match, eval "qr/\Q$srch\E/"; # compile match pattern
|
joachim99@2
|
78 $repl =~ s/\\(.)/$1/g;
|
joachim99@2
|
79 push @comp_subs, $repl;
|
joachim99@2
|
80 } elsif ( ($pat =~ /%([^%]*)%d/ )
|
joachim99@2
|
81 || ($pat =~ m%/([^/]*)/d% )
|
joachim99@2
|
82 ) {
|
joachim99@2
|
83 push @comp_match, eval "qr/\Q$1\E/";
|
joachim99@2
|
84 push @comp_subs, "";
|
joachim99@2
|
85 } else {
|
joachim99@2
|
86 die "Uhh. Malformed pattern in $ac_cs_root.subs ($pat)"
|
joachim99@2
|
87 unless ( $pat =~ /^\s*$/ ); # ignore white lines
|
joachim99@2
|
88 }
|
joachim99@2
|
89 }
|
joachim99@2
|
90 }
|
joachim99@2
|
91 undef @subs;
|
joachim99@2
|
92
|
joachim99@2
|
93 # read the list of files to be patched, form:
|
joachim99@2
|
94 # ./Makefile arts/Makefile arts/examples/Makefile arts/flow/Makefile
|
joachim99@2
|
95
|
joachim99@2
|
96 open(CF, "< $ac_sacfiles") || die "can't open $ac_sacfiles: $!";
|
joachim99@2
|
97 my @ac_files = <CF>;
|
joachim99@2
|
98 close(CF);
|
joachim99@2
|
99 chomp @ac_files;
|
joachim99@2
|
100
|
joachim99@2
|
101
|
joachim99@2
|
102 my $ac_file;
|
joachim99@2
|
103 foreach $ac_file (@ac_files) {
|
joachim99@2
|
104 next if $ac_file =~ /\.\./;
|
joachim99@2
|
105 next if $ac_file =~ /^\s*$/;
|
joachim99@2
|
106 my $ac_file_in;
|
joachim99@2
|
107 my ($ac_dir, $ac_dots, $ac_dir_suffix);
|
joachim99@2
|
108
|
joachim99@2
|
109 if ($ac_file =~ /.*:.*/ ) {
|
joachim99@2
|
110 ($ac_file_in = $ac_file) =~ s%[^:]*:%%;
|
joachim99@2
|
111 $ac_file =~ s%:.*%%;
|
joachim99@2
|
112 } else {
|
joachim99@2
|
113 $ac_file_in = $ac_file.".in";
|
joachim99@2
|
114 }
|
joachim99@2
|
115
|
joachim99@2
|
116 # Adjust a relative srcdir, top_srcdir, and INSTALL for subdirectories.
|
joachim99@2
|
117
|
joachim99@2
|
118 # Remove last slash and all that follows it. Not all systems have dirname.
|
joachim99@2
|
119 ($ac_dir = $ac_file) =~ s%/[^/][^/]*$%%;
|
joachim99@2
|
120 if ( ($ac_dir ne $ac_file) && ($ac_dir ne ".")) {
|
joachim99@2
|
121 # The file is in a subdirectory.
|
joachim99@2
|
122 if (! -d "$ac_dir") { mkdir "$ac_dir", 0777; }
|
joachim99@2
|
123 ($ac_dir_suffix = $ac_dir) =~ s%^./%%;
|
joachim99@2
|
124 $ac_dir_suffix="/".$ac_dir_suffix;
|
joachim99@2
|
125 # A "../" for each directory in $ac_dir_suffix.
|
joachim99@2
|
126 ($ac_dots = $ac_dir_suffix) =~ s%/[^/]*%../%g;
|
joachim99@2
|
127 } else {
|
joachim99@2
|
128 $ac_dir_suffix="";
|
joachim99@2
|
129 $ac_dots="";
|
joachim99@2
|
130 }
|
joachim99@2
|
131
|
joachim99@2
|
132 if ($ac_given_srcdir eq ".") {
|
joachim99@2
|
133 $srcdir=".";
|
joachim99@2
|
134 if ($ac_dots) {
|
joachim99@2
|
135 ( $top_srcdir = $ac_dots) =~ s%/$%%;
|
joachim99@2
|
136 } else { $top_srcdir="."; }
|
joachim99@2
|
137 } elsif ($ac_given_srcdir =~ m%^/%) {
|
joachim99@2
|
138 $srcdir=$ac_given_srcdir.$ac_dir_suffix;
|
joachim99@2
|
139 $top_srcdir = $ac_given_srcdir;
|
joachim99@2
|
140 } else {
|
joachim99@2
|
141 $srcdir = $ac_dots.$ac_given_srcdir.$ac_dir_suffix;
|
joachim99@2
|
142 $top_srcdir = $ac_dots.$ac_given_srcdir;
|
joachim99@2
|
143 }
|
joachim99@2
|
144
|
joachim99@2
|
145 if ($ac_given_INSTALL) {
|
joachim99@2
|
146 if ($ac_given_INSTALL =~ m%^/% ) {
|
joachim99@2
|
147 $INSTALL = $ac_given_INSTALL;
|
joachim99@2
|
148 } else {
|
joachim99@2
|
149 $INSTALL = $ac_dots.$ac_given_INSTALL;
|
joachim99@2
|
150 }
|
joachim99@2
|
151 }
|
joachim99@2
|
152
|
joachim99@2
|
153 print "fast creating $ac_file\n";
|
joachim99@2
|
154 unlink $ac_file;
|
joachim99@2
|
155 my $ac_comsub="";
|
joachim99@2
|
156 my $fname=$ac_file_in;
|
joachim99@2
|
157 $fname =~ s%.*/%%;
|
joachim99@2
|
158 my $configure_input="Generated automatically from $fname by config.pl.";
|
joachim99@2
|
159 if ($ac_file =~ /.*[Mm]akefile.*/) {
|
joachim99@2
|
160 $ac_comsub="# ".$configure_input."\n"; # for the first line in $ac_file
|
joachim99@2
|
161 }
|
joachim99@2
|
162
|
joachim99@2
|
163 my $ac_file_inputs;
|
joachim99@2
|
164 ($ac_file_inputs = $ac_file_in) =~ s%^%$ac_given_srcdir/%;
|
joachim99@2
|
165 $ac_file_inputs =~ s%:% $ac_given_srcdir/%g;
|
joachim99@2
|
166
|
joachim99@2
|
167 patch_file($ac_file, $ac_file_inputs, $ac_comsub);
|
joachim99@2
|
168 }
|
joachim99@2
|
169
|
joachim99@2
|
170 sub patch_file {
|
joachim99@2
|
171 my ($outf, $infiles, $firstline) = @_;
|
joachim99@2
|
172 my $filedata;
|
joachim99@2
|
173 my @infiles=split(' ', $infiles);
|
joachim99@2
|
174 my $i=0;
|
joachim99@2
|
175
|
joachim99@2
|
176 if ($firstline) {
|
joachim99@2
|
177 $filedata = $firstline;
|
joachim99@2
|
178 }
|
joachim99@2
|
179 foreach my $name (@infiles) {
|
joachim99@2
|
180 if (open(CF, "< $name")) {
|
joachim99@2
|
181 while (<CF>) {
|
joachim99@2
|
182 $filedata .= $_;
|
joachim99@2
|
183 }
|
joachim99@2
|
184 close(CF);
|
joachim99@2
|
185 } else {
|
joachim99@2
|
186 print STDERR "can't open $name: $!"."\n";
|
joachim99@2
|
187 }
|
joachim99@2
|
188 }
|
joachim99@2
|
189
|
joachim99@2
|
190 $filedata =~ s%\@configure_input\@%$configure_input%g;
|
joachim99@2
|
191 $filedata =~ s%\@srcdir\@%$srcdir%g;
|
joachim99@2
|
192 $filedata =~ s%\@top_srcdir\@%$top_srcdir%g;
|
joachim99@2
|
193 $filedata =~ s%\@INSTALL\@%$INSTALL%g;
|
joachim99@2
|
194
|
joachim99@2
|
195 if ($bad_perl) {
|
joachim99@2
|
196 while ($i <= $#comp_subs) {
|
joachim99@2
|
197 my $ref = $comp_subs[$i];
|
joachim99@2
|
198 &$ref(\$filedata);
|
joachim99@2
|
199 $i++;
|
joachim99@2
|
200 }
|
joachim99@2
|
201 } else {
|
joachim99@2
|
202 while ($i <= $#comp_match) {
|
joachim99@2
|
203 $filedata =~ s/$comp_match[$i]/$comp_subs[$i]/g;
|
joachim99@2
|
204 $i++;
|
joachim99@2
|
205 }
|
joachim99@2
|
206 }
|
joachim99@2
|
207 open(CF, "> $outf") || die "can't create $outf: $!";
|
joachim99@2
|
208 print CF $filedata;
|
joachim99@2
|
209 close(CF);
|
joachim99@2
|
210 }
|
joachim99@2
|
211
|
joachim99@2
|
212 sub make_closure {
|
joachim99@2
|
213 my ($pat, $sub) = @_;
|
joachim99@2
|
214 $pat =~ s/\@/\\@/g; # @bla@ -> \@bla\@
|
joachim99@2
|
215 $pat =~ s/\$/\\\$/g; # $bla -> \$bla
|
joachim99@2
|
216 $sub =~ s/\@/\\@/g;
|
joachim99@2
|
217 $sub =~ s/\$/\\\$/g;
|
joachim99@2
|
218 my $ret = eval "return sub { my \$ref=shift; \$\$ref =~ s%$pat%$sub%g; }";
|
joachim99@2
|
219 if ($@) {
|
joachim99@2
|
220 print "can't create CODE: $@\n";
|
joachim99@2
|
221 }
|
joachim99@2
|
222 return $ret;
|
joachim99@2
|
223 }
|