home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 2001 January / CT_SW0101.ISO / pc / software / kommunik / ftp / kmago112.tgz / kmago112.tar / kmago-1.1.2 / admin / config.pl < prev    next >
Perl Script  |  2001-01-17  |  6KB  |  211 lines

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