1#!perl -w 2use strict; 3 4BEGIN { warn "Running ".__FILE__."\n" }; 5BEGIN 6 { 7 require "Config.pm"; 8 die "Config.pm:$@" if $@; 9 Config->import; 10 } 11use File::Compare qw(compare); 12use File::Copy qw(copy); 13use File::Basename qw(fileparse); 14 15my ($name, $dir) = fileparse($0); 16$name =~ s#^(.*)\.PL$#../$1.SH#; 17my %opt; 18while (@ARGV && $ARGV[0] =~ /^([\w_]+)=(.*)$/) 19 { 20 $opt{$1}=$2; 21 shift(@ARGV); 22 } 23 24$opt{CONFIG_H} ||= 'config.h'; 25$opt{CORE_DIR} ||= '../lib/CORE'; 26$opt{CORE_CONFIG_H} ||= $opt{CORE_DIR} . '/config.h'; 27 28warn "Writing $opt{CONFIG_H}\n"; 29 30open(SH, "<", $name) || die "Cannot open $name:$!"; 31while (<SH>) 32 { 33 last if /^\s*sed/; 34 } 35my($term,$file,$pat) = /^\s*sed\s+<<(\S+)\s+>(\S+)\s+(.*)$/; 36 37$file =~ s/^\$(\w+)$/$opt{$1}/g; 38 39my $str = "sub munge\n{\n"; 40 41while ($pat =~ s/-e\s+'([^']*)'\s*//) 42 { 43 my $e = $1; 44 $e =~ s/\\([\(\)])/$1/g; 45 $e =~ s/\\(\d)/\$$1/g; 46 $str .= "$e;\n"; 47 } 48$str .= "}\n"; 49 50eval $str; 51 52die "$str:$@" if $@; 53 54open(H, ">", "$file.new") || die "Cannot open $file.new:$!"; 55binmode(H); 56while (<SH>) 57 { 58 last if /^$term$/o; 59 s/\$([\w_]+)/Config($1)/eg; 60 s/`([^\`]*)`/BackTick($1)/eg; 61 munge(); 62 s/\\\$/\$/g; 63 s#/[ *\*]*\*/#/**/#; 64 s#(.)/\*\*/#$1/ **/# if(/^\/\*/); #avoid "/*" inside comments 65 if (/^\s*#define\s+(PRIVLIB|SITELIB|VENDORLIB)_EXP/) 66 { 67 $_ = '#define '. $1 . '_EXP ('.( 68 $1 eq 'PRIVLIB' ? 'PerlEnv_lib_path' : 69 $1 eq 'SITELIB' ? 'PerlEnv_sitelib_path' : 70 $1 eq 'VENDORLIB' ? 'PerlEnv_vendorlib_path' : 71 die "unknown *LIB_EXP define \"$1\"" 72 ). "(PERL_VERSION_STRING, NULL))\t/**/\n"; 73 } 74 # incpush() handles archlibs, so disable them 75 elsif (/^\s*#define\s+(ARCHLIB|SITEARCH|VENDORARCH)_EXP/) 76 { 77 $_ = "/*#define ". $1 . "_EXP \"\"\t/ **/\n"; 78 } 79 elsif (/^\s*#define\s+CPP(STDIN|RUN)\s+"gcc(.*)"\s*$/) 80 { 81 $_ = "#define CPP" . $1 . " \"" . $opt{ARCHPREFIX} . "gcc" . $2 . "\"\n"; 82 } 83 print H; 84 } 85close(H); 86close(SH); 87 88my $core_config_h = $opt{CORE_CONFIG_H}; 89if (compare("$file.new", $core_config_h)) { 90 mkdir $opt{CORE_DIR} unless -d $opt{CORE_DIR}; 91 chmod(0666,$core_config_h); 92 copy("$file.new",$core_config_h) || die "Cannot copy $file.new $core_config_h:$!"; 93 chmod(0444,$core_config_h); 94} 95 96if (compare("$file.new",$file)) 97 { 98 warn "$file has changed\n"; 99 chmod(0666,$file); 100 unlink($file); 101 rename("$file.new",$file) || die "Cannot rename:$!"; 102 } 103else 104 { 105 unlink ("$file.new"); 106 } 107 108sub Config 109{ 110 my $var = shift; 111 my $val = $Config{$var}; 112 $val = 'undef' unless defined $val; 113 $val =~ s/\\/\\\\/g; 114 return $val; 115} 116 117sub BackTick 118{ 119 my $cmd = shift; 120 if ($cmd =~ /^echo\s+(.*?)\s*\|\s+sed\s+'(.*)'\s*$/) 121 { 122 my($data,$pat) = ($1,$2); 123 $data =~ s/\s+/ /g; 124 eval "\$data =~ $pat"; 125 return $data; 126 } 127 else 128 { 129 die "Cannot handle \`$cmd\`"; 130 } 131 return $cmd; 132} 133