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