| 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 | |
| 31 | my $ac_subs=$ARGV[0]; |
| 32 | my $ac_sacfiles = $ARGV[1]; |
| 33 | my $ac_given_srcdir=$ARGV[2]; |
| 34 | my $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 | |
| 41 | my ($srcdir, $top_srcdir); |
| 42 | my $INSTALL; |
| 43 | my $bad_perl = ($] < 5.005); |
| 44 | |
| 45 | open(CF, "< $ac_subs") || die "can't open $ac_subs: $!"; |
| 46 | my @subs = <CF>; |
| 47 | close(CF); |
| 48 | chomp @subs; |
| 49 | @comp_match=(); |
| 50 | @comp_subs=(); |
| 51 | |
| 52 | if ($bad_perl) { |
| 53 | print "Using perl older than version 5.005\n"; |
| 54 | foreach my $pat (@subs) { |
| 55 | if ( ($pat =~ m/s%([^%]*)%([^%]*)%g/ ) |
| 56 | || ($pat =~ m/s%([^%]*)%([^%]*)%;t/ ) |
| 57 | || ($pat =~ m/s,([^,]*),(.*),;t/) |
| 58 | || ($pat =~ m%s/([^/]*)/([^/]*)/g% ) |
| 59 | || ($pat =~ m%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 | } |
| 102 | undef @subs; |
| 103 | |
| 104 | # read the list of files to be patched, form: |
| 105 | # ./Makefile arts/Makefile arts/examples/Makefile arts/flow/Makefile |
| 106 | |
| 107 | open(CF, "< $ac_sacfiles") || die "can't open $ac_sacfiles: $!"; |
| 108 | my @ac_files = <CF>; |
| 109 | close(CF); |
| 110 | chomp @ac_files; |
| 111 | |
| 112 | |
| 113 | my $ac_file; |
| 114 | foreach $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 | |
| 181 | sub patch_file { |
| 182 | my ($outf, $infiles, $identline) = @_; |
| 183 | my $filedata; |
| 184 | my @infiles=split(' ', $infiles); |
| 185 | my $i=0; |
| 186 | |
| 187 | foreach my $name (@infiles) { |
| 188 | if (open(CF, "< $name")) { |
| 189 | while (<CF>) { |
| 190 | $filedata .= $_; |
| 191 | } |
| 192 | close(CF); |
| 193 | } else { |
| 194 | print STDERR "can't open $name: $!"."\n"; |
| 195 | } |
| 196 | } |
| 197 | if ($identline) { |
| 198 | # Put the ident in the second line. For shitty automake 1.6x. |
| 199 | $filedata =~ s%\n%\n$identline%; |
| 200 | } |
| 201 | |
| 202 | $filedata =~ s%\@configure_input\@%$configure_input%g; |
| 203 | $filedata =~ s%\@srcdir\@%$srcdir%g; |
| 204 | $filedata =~ s%\@top_srcdir\@%$top_srcdir%g; |
| 205 | $filedata =~ s%\@INSTALL\@%$INSTALL%g; |
| 206 | |
| 207 | if ($bad_perl) { |
| 208 | while ($i <= $#comp_subs) { |
| 209 | my $ref = $comp_subs[$i]; |
| 210 | &$ref(\$filedata); |
| 211 | $i++; |
| 212 | } |
| 213 | } else { |
| 214 | while ($i <= $#comp_match) { |
| 215 | $filedata =~ s/$comp_match[$i]/$comp_subs[$i]/g; |
| 216 | $i++; |
| 217 | } |
| 218 | } |
| 219 | open(CF, "> $outf") || die "can't create $outf: $!"; |
| 220 | print CF $filedata; |
| 221 | close(CF); |
| 222 | } |
| 223 | |
| 224 | sub make_closure { |
| 225 | my ($pat, $sub) = @_; |
| 226 | $pat =~ s/\@/\\@/g; # @bla@ -> \@bla\@ |
| 227 | $pat =~ s/\$/\\\$/g; # $bla -> \$bla |
| 228 | $sub =~ s/\@/\\@/g; |
| 229 | $sub =~ s/\$/\\\$/g; |
| 230 | my $ret = eval "return sub { my \$ref=shift; \$\$ref =~ s%$pat%$sub%g; }"; |
| 231 | if ($@) { |
| 232 | print "can't create CODE: $@\n"; |
| 233 | } |
| 234 | return $ret; |
| 235 | } |