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