joachim99@2: #!/usr/bin/perl joachim99@2: # a script for use by autoconf to make the Makefiles joachim99@2: # from the Makefile.in's joachim99@2: # joachim99@2: # the original autoconf mechanism first splits all substitutions into groups joachim99@2: # of ca. 90, and than invokes sed for _every_ Makefile.in and every group joachim99@2: # (so around 2-3 times per Makefile.in). So this takes forever, as sed joachim99@2: # has to recompile the regexps every time. joachim99@2: # joachim99@2: # this script does better. It changes all Makefile.ins in one process. joachim99@2: # in kdelibs the time for building Makefile went down from 2:59 min to 13 sec! joachim99@2: # joachim99@2: # written by Michael Matz joachim99@2: # adapted by Dirk Mueller joachim99@2: # joachim99@2: # the first part was done by looking at the config.status files generated joachim99@2: # by configure. joachim99@2: # joachim99@2: joachim99@2: my $ac_subs=$ARGV[0]; joachim99@2: my $ac_sacfiles = $ARGV[1]; joachim99@2: my $ac_given_srcdir=$ARGV[2]; joachim99@2: my $ac_given_INSTALL=$ARGV[3]; joachim99@2: joachim99@2: #print "ac_subs=$ac_subs\n"; joachim99@2: #print "ac_sacfiles=$ac_sacfiles\n"; joachim99@2: #print "ac_given_srcdir=$ac_given_srcdir\n"; joachim99@2: #print "ac_given_INSTALL=$ac_given_INSTALL\n"; joachim99@2: joachim99@2: my ($srcdir, $top_srcdir); joachim99@2: my $INSTALL; joachim99@2: my $bad_perl = ($] < 5.005); joachim99@2: joachim99@2: open(CF, "< $ac_subs") || die "can't open $ac_subs: $!"; joachim99@2: my @subs = ; joachim99@2: close(CF); joachim99@2: chomp @subs; joachim99@2: @comp_match=(); joachim99@2: @comp_subs=(); joachim99@2: joachim99@2: if ($bad_perl) { joachim99@2: print "Using perl older than version 5.005\n"; joachim99@2: foreach my $pat (@subs) { joachim99@2: if ( ($pat =~ /s%([^%]*)%([^%]*)%g/ ) joachim99@2: || ($pat =~ m%/([^/]*)/([^/]*)/g% ) joachim99@2: || ($pat =~ /s%([^%]*)%([^%]*)%;t/ ) joachim99@2: || ($pat =~ m%/([^/]*)/([^/]*)/;t% ) joachim99@2: || ($pat =~ /s,([^,]*),(.*),;t/) joachim99@2: ) { joachim99@2: # form : s%bla%blubb%g joachim99@2: # or s%bla%blubb%;t t (autoconf > 2.13 and < 2.52 ?) joachim99@2: # or s,bla,blubb,;t t (autoconf 2.52) joachim99@2: my $srch = $1; joachim99@2: my $repl = $2; joachim99@2: $repl =~ s/\\(.)/$1/g; joachim99@2: push @comp_subs, make_closure($srch, $repl); joachim99@2: joachim99@2: } elsif ( ($pat =~ /%([^%]*)%d/ ) joachim99@2: || ($pat =~ m%/([^/]*)/d% ) joachim99@2: ) { joachim99@2: push @comp_subs, make_closure($1, ""); joachim99@2: } else { joachim99@2: die "Uhh. Malformed pattern in $ac_subs ($pat)" joachim99@2: unless ( $pat =~ /^\s*$/ ); # ignore white lines joachim99@2: } joachim99@2: } joachim99@2: } else { joachim99@2: foreach my $pat (@subs) { joachim99@2: if ( ($pat =~ /s%([^%]*)%([^%]*)%g/ ) || joachim99@2: ($pat =~ /s%([^%]*)%([^%]*)%;t/ ) || joachim99@2: ($pat =~ /s,([^,]*),(.*),;t/) ) { joachim99@2: # form : s%bla%blubb%g joachim99@2: # or s%bla%blubb%;t t (autoconf > 2.13 and < 2.52 ?) joachim99@2: # or s,bla,blubb,;t t (autoconf 2.52) joachim99@2: my $srch = $1; joachim99@2: my $repl = $2; joachim99@2: push @comp_match, eval "qr/\Q$srch\E/"; # compile match pattern joachim99@2: $repl =~ s/\\(.)/$1/g; joachim99@2: push @comp_subs, $repl; joachim99@2: } elsif ( ($pat =~ /%([^%]*)%d/ ) joachim99@2: || ($pat =~ m%/([^/]*)/d% ) joachim99@2: ) { joachim99@2: push @comp_match, eval "qr/\Q$1\E/"; joachim99@2: push @comp_subs, ""; joachim99@2: } else { joachim99@2: die "Uhh. Malformed pattern in $ac_cs_root.subs ($pat)" joachim99@2: unless ( $pat =~ /^\s*$/ ); # ignore white lines joachim99@2: } joachim99@2: } joachim99@2: } joachim99@2: undef @subs; joachim99@2: joachim99@2: # read the list of files to be patched, form: joachim99@2: # ./Makefile arts/Makefile arts/examples/Makefile arts/flow/Makefile joachim99@2: joachim99@2: open(CF, "< $ac_sacfiles") || die "can't open $ac_sacfiles: $!"; joachim99@2: my @ac_files = ; joachim99@2: close(CF); joachim99@2: chomp @ac_files; joachim99@2: joachim99@2: joachim99@2: my $ac_file; joachim99@2: foreach $ac_file (@ac_files) { joachim99@2: next if $ac_file =~ /\.\./; joachim99@2: next if $ac_file =~ /^\s*$/; joachim99@2: my $ac_file_in; joachim99@2: my ($ac_dir, $ac_dots, $ac_dir_suffix); joachim99@2: joachim99@2: if ($ac_file =~ /.*:.*/ ) { joachim99@2: ($ac_file_in = $ac_file) =~ s%[^:]*:%%; joachim99@2: $ac_file =~ s%:.*%%; joachim99@2: } else { joachim99@2: $ac_file_in = $ac_file.".in"; joachim99@2: } joachim99@2: joachim99@2: # Adjust a relative srcdir, top_srcdir, and INSTALL for subdirectories. joachim99@2: joachim99@2: # Remove last slash and all that follows it. Not all systems have dirname. joachim99@2: ($ac_dir = $ac_file) =~ s%/[^/][^/]*$%%; joachim99@2: if ( ($ac_dir ne $ac_file) && ($ac_dir ne ".")) { joachim99@2: # The file is in a subdirectory. joachim99@2: if (! -d "$ac_dir") { mkdir "$ac_dir", 0777; } joachim99@2: ($ac_dir_suffix = $ac_dir) =~ s%^./%%; joachim99@2: $ac_dir_suffix="/".$ac_dir_suffix; joachim99@2: # A "../" for each directory in $ac_dir_suffix. joachim99@2: ($ac_dots = $ac_dir_suffix) =~ s%/[^/]*%../%g; joachim99@2: } else { joachim99@2: $ac_dir_suffix=""; joachim99@2: $ac_dots=""; joachim99@2: } joachim99@2: joachim99@2: if ($ac_given_srcdir eq ".") { joachim99@2: $srcdir="."; joachim99@2: if ($ac_dots) { joachim99@2: ( $top_srcdir = $ac_dots) =~ s%/$%%; joachim99@2: } else { $top_srcdir="."; } joachim99@2: } elsif ($ac_given_srcdir =~ m%^/%) { joachim99@2: $srcdir=$ac_given_srcdir.$ac_dir_suffix; joachim99@2: $top_srcdir = $ac_given_srcdir; joachim99@2: } else { joachim99@2: $srcdir = $ac_dots.$ac_given_srcdir.$ac_dir_suffix; joachim99@2: $top_srcdir = $ac_dots.$ac_given_srcdir; joachim99@2: } joachim99@2: joachim99@2: if ($ac_given_INSTALL) { joachim99@2: if ($ac_given_INSTALL =~ m%^/% ) { joachim99@2: $INSTALL = $ac_given_INSTALL; joachim99@2: } else { joachim99@2: $INSTALL = $ac_dots.$ac_given_INSTALL; joachim99@2: } joachim99@2: } joachim99@2: joachim99@2: print "fast creating $ac_file\n"; joachim99@2: unlink $ac_file; joachim99@2: my $ac_comsub=""; joachim99@2: my $fname=$ac_file_in; joachim99@2: $fname =~ s%.*/%%; joachim99@2: my $configure_input="Generated automatically from $fname by config.pl."; joachim99@2: if ($ac_file =~ /.*[Mm]akefile.*/) { joachim99@2: $ac_comsub="# ".$configure_input."\n"; # for the first line in $ac_file joachim99@2: } joachim99@2: joachim99@2: my $ac_file_inputs; joachim99@2: ($ac_file_inputs = $ac_file_in) =~ s%^%$ac_given_srcdir/%; joachim99@2: $ac_file_inputs =~ s%:% $ac_given_srcdir/%g; joachim99@2: joachim99@2: patch_file($ac_file, $ac_file_inputs, $ac_comsub); joachim99@2: } joachim99@2: joachim99@2: sub patch_file { joachim99@2: my ($outf, $infiles, $firstline) = @_; joachim99@2: my $filedata; joachim99@2: my @infiles=split(' ', $infiles); joachim99@2: my $i=0; joachim99@2: joachim99@2: if ($firstline) { joachim99@2: $filedata = $firstline; joachim99@2: } joachim99@2: foreach my $name (@infiles) { joachim99@2: if (open(CF, "< $name")) { joachim99@2: while () { joachim99@2: $filedata .= $_; joachim99@2: } joachim99@2: close(CF); joachim99@2: } else { joachim99@2: print STDERR "can't open $name: $!"."\n"; joachim99@2: } joachim99@2: } joachim99@2: joachim99@2: $filedata =~ s%\@configure_input\@%$configure_input%g; joachim99@2: $filedata =~ s%\@srcdir\@%$srcdir%g; joachim99@2: $filedata =~ s%\@top_srcdir\@%$top_srcdir%g; joachim99@2: $filedata =~ s%\@INSTALL\@%$INSTALL%g; joachim99@2: joachim99@2: if ($bad_perl) { joachim99@2: while ($i <= $#comp_subs) { joachim99@2: my $ref = $comp_subs[$i]; joachim99@2: &$ref(\$filedata); joachim99@2: $i++; joachim99@2: } joachim99@2: } else { joachim99@2: while ($i <= $#comp_match) { joachim99@2: $filedata =~ s/$comp_match[$i]/$comp_subs[$i]/g; joachim99@2: $i++; joachim99@2: } joachim99@2: } joachim99@2: open(CF, "> $outf") || die "can't create $outf: $!"; joachim99@2: print CF $filedata; joachim99@2: close(CF); joachim99@2: } joachim99@2: joachim99@2: sub make_closure { joachim99@2: my ($pat, $sub) = @_; joachim99@2: $pat =~ s/\@/\\@/g; # @bla@ -> \@bla\@ joachim99@2: $pat =~ s/\$/\\\$/g; # $bla -> \$bla joachim99@2: $sub =~ s/\@/\\@/g; joachim99@2: $sub =~ s/\$/\\\$/g; joachim99@2: my $ret = eval "return sub { my \$ref=shift; \$\$ref =~ s%$pat%$sub%g; }"; joachim99@2: if ($@) { joachim99@2: print "can't create CODE: $@\n"; joachim99@2: } joachim99@2: return $ret; joachim99@2: }