disable WARN and DIE hooks during constant folding
[p5sagit/p5-mst-13.2.git] / configpm
CommitLineData
a0d0e21e 1#!./miniperl -w
962e59f3 2#
3# configpm
4#
5# Copyright (C) 1994, 1995, 1996 1997, 1998, 1999, 2000, 2001,
6# 2002, 2003, 2004, 2005, 2006 Larry Wall and others.
7#
8#
9# Regenerate the files
10#
11# lib/Config.pm
12# lib/Config_heavy.pl
13# lib/Config.pod
14# lib/Cross.pm (optionally)
15#
16# from the contents of the static files
17#
18# Porting/Glossary
19# myconfig.SH
20#
21# and from the contents of the Configure-generated file
22#
23# config.sh
24#
25# It will only update Config.pm and Config_heavy.pl if the contents of
26# either file would be different. Note that *both* files are updated in
27# this case, since for example an extension makefile that has a dependency
28# on Config.pm should trigger even if only Config_heavy.pl has changed.
29
30sub usage { die <<EOF }
31usage: $0 [ options ] [ Config_file ] [ Glossary_file ]
32 --cross=PLATFORM cross-compile for a different platform
33 --no-glossary don't include Porting/Glossary in lib/Config.pod
34 --heavy=FILE alternative name for lib/Config_heavy.pl
35 Config_file alternative name for lib/Config.pm
36 Glossary_file alternative name for Porting/Glossary
37EOF
38
2f4f46ad 39use strict;
40use vars qw(%Config $Config_SH_expanded);
8990e307 41
a8e1d30b 42my $how_many_common = 22;
43
44# commonly used names to precache (and hence lookup fastest)
45my %Common;
46
47while ($how_many_common--) {
48 $_ = <DATA>;
49 chomp;
50 /^(\S+):\s*(\d+)$/ or die "Malformed line '$_'";
51 $Common{$1} = $1;
52}
5435c704 53
54# names of things which may need to have slashes changed to double-colons
55my %Extensions = map {($_,$_)}
56 qw(dynamic_ext static_ext extensions known_extensions);
57
58# allowed opts as well as specifies default and initial values
59my %Allowed_Opts = (
2d9d8159 60 'cross' => '', # --cross=PLATFORM - crosscompiling for PLATFORM
61 'glossary' => 1, # --no-glossary - no glossary file inclusion,
5435c704 62 # for compactness
2d9d8159 63 'heavy' => '', # pathname of the Config_heavy.pl file
18f68570 64);
18f68570 65
5435c704 66sub opts {
67 # user specified options
68 my %given_opts = (
69 # --opt=smth
70 (map {/^--([\-_\w]+)=(.*)$/} @ARGV),
71 # --opt --no-opt --noopt
72 (map {/^no-?(.*)$/i?($1=>0):($_=>1)} map {/^--([\-_\w]+)$/} @ARGV),
73 );
74
75 my %opts = (%Allowed_Opts, %given_opts);
76
77 for my $opt (grep {!exists $Allowed_Opts{$_}} keys %given_opts) {
962e59f3 78 warn "option '$opt' is not recognized";
79 usage;
5435c704 80 }
81 @ARGV = grep {!/^--/} @ARGV;
82
83 return %opts;
84}
18f68570 85
5435c704 86
87my %Opts = opts();
88
2d9d8159 89my ($Config_PM, $Config_heavy);
5435c704 90my $Glossary = $ARGV[1] || 'Porting/Glossary';
91
92if ($Opts{cross}) {
18f68570 93 # creating cross-platform config file
94 mkdir "xlib";
5435c704 95 mkdir "xlib/$Opts{cross}";
96 $Config_PM = $ARGV[0] || "xlib/$Opts{cross}/Config.pm";
18f68570 97}
98else {
5435c704 99 $Config_PM = $ARGV[0] || 'lib/Config.pm';
18f68570 100}
2d9d8159 101if ($Opts{heavy}) {
102 $Config_heavy = $Opts{heavy};
103}
104else {
105 ($Config_heavy = $Config_PM) =~ s!\.pm$!_heavy.pl!;
106 die "Can't automatically determine name for Config_heavy.pl from '$Config_PM'"
107 if $Config_heavy eq $Config_PM;
108}
8990e307 109
962e59f3 110my $config_txt;
111my $heavy_txt;
2d9d8159 112
962e59f3 113$heavy_txt .= <<'ENDOFBEG';
2d9d8159 114# This file was created by configpm when Perl was built. Any changes
115# made to this file will be lost the next time perl is built.
116
117package Config;
118use strict;
119# use warnings; Pulls in Carp
120# use vars pulls in Carp
121ENDOFBEG
fec02dd3 122
9137345a 123my $myver = sprintf "%vd", $^V;
a0d0e21e 124
962e59f3 125$config_txt .= sprintf <<'ENDOFBEG', ($myver) x 3;
5435c704 126# This file was created by configpm when Perl was built. Any changes
127# made to this file will be lost the next time perl is built.
3c81428c 128
8990e307 129package Config;
2f4f46ad 130use strict;
131# use warnings; Pulls in Carp
132# use vars pulls in Carp
133@Config::EXPORT = qw(%%Config);
134@Config::EXPORT_OK = qw(myconfig config_sh config_vars config_re);
a48f8c77 135
43d06990 136# Need to stub all the functions to make code such as print Config::config_sh
137# keep working
138
139sub myconfig;
140sub config_sh;
141sub config_vars;
142sub config_re;
143
2f4f46ad 144my %%Export_Cache = map {($_ => 1)} (@Config::EXPORT, @Config::EXPORT_OK);
145
146our %%Config;
e3d0cac0 147
148# Define our own import method to avoid pulling in the full Exporter:
149sub import {
a48f8c77 150 my $pkg = shift;
2f4f46ad 151 @_ = @Config::EXPORT unless @_;
5435c704 152
a48f8c77 153 my @funcs = grep $_ ne '%%Config', @_;
154 my $export_Config = @funcs < @_ ? 1 : 0;
5435c704 155
2f4f46ad 156 no strict 'refs';
a48f8c77 157 my $callpkg = caller(0);
158 foreach my $func (@funcs) {
159 die sprintf qq{"%%s" is not exported by the %%s module\n},
160 $func, __PACKAGE__ unless $Export_Cache{$func};
161 *{$callpkg.'::'.$func} = \&{$func};
162 }
5435c704 163
a48f8c77 164 *{"$callpkg\::Config"} = \%%Config if $export_Config;
165 return;
e3d0cac0 166}
167
5435c704 168die "Perl lib version (%s) doesn't match executable version ($])"
169 unless $^V;
de98c553 170
5435c704 171$^V eq %s
a48f8c77 172 or die "Perl lib version (%s) doesn't match executable version (" .
173 sprintf("v%%vd",$^V) . ")";
a0d0e21e 174
8990e307 175ENDOFBEG
176
16d20bd9 177
5435c704 178my @non_v = ();
5435c704 179my @v_others = ();
180my $in_v = 0;
181my %Data = ();
182
a0d0e21e 183
1a9ca827 184my %seen_quotes;
2f4f46ad 185{
186 my ($name, $val);
187 open(CONFIG_SH, 'config.sh') || die "Can't open config.sh: $!";
188 while (<CONFIG_SH>) {
a0d0e21e 189 next if m:^#!/bin/sh:;
5435c704 190
a02608de 191 # Catch PERL_CONFIG_SH=true and PERL_VERSION=n line from Configure.
d4de4258 192 s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/ or m/^(\w+)='(.*)'$/;
3905a40f 193 my($k, $v) = ($1, $2);
5435c704 194
2000072c 195 # grandfather PATCHLEVEL and SUBVERSION and CONFIG
cceca5ed 196 if ($k) {
197 if ($k eq 'PERL_VERSION') {
198 push @v_others, "PATCHLEVEL='$v'\n";
199 }
200 elsif ($k eq 'PERL_SUBVERSION') {
201 push @v_others, "SUBVERSION='$v'\n";
202 }
a02608de 203 elsif ($k eq 'PERL_CONFIG_SH') {
2000072c 204 push @v_others, "CONFIG='$v'\n";
205 }
cceca5ed 206 }
5435c704 207
435ec615 208 # We can delimit things in config.sh with either ' or ".
209 unless ($in_v or m/^(\w+)=(['"])(.*\n)/){
a0d0e21e 210 push(@non_v, "#$_"); # not a name='value' line
211 next;
212 }
2f4f46ad 213 my $quote = $2;
5435c704 214 if ($in_v) {
215 $val .= $_;
216 }
217 else {
218 ($name,$val) = ($1,$3);
219 }
435ec615 220 $in_v = $val !~ /$quote\n/;
44a8e56a 221 next if $in_v;
a0d0e21e 222
5435c704 223 s,/,::,g if $Extensions{$name};
a0d0e21e 224
5435c704 225 $val =~ s/$quote\n?\z//;
3c81428c 226
5435c704 227 my $line = "$name=$quote$val$quote\n";
deeea481 228 push(@v_others, $line);
1a9ca827 229 $seen_quotes{$quote}++;
2f4f46ad 230 }
231 close CONFIG_SH;
5435c704 232}
2f4f46ad 233
1a9ca827 234# This is somewhat grim, but I want the code for parsing config.sh here and
235# now so that I can expand $Config{ivsize} and $Config{ivtype}
236
237my $fetch_string = <<'EOT';
238
239# Search for it in the big string
240sub fetch_string {
241 my($self, $key) = @_;
242
243EOT
244
245if ($seen_quotes{'"'}) {
246 # We need the full ' and " code
247 $fetch_string .= <<'EOT';
248 my $quote_type = "'";
249 my $marker = "$key=";
250
251 # Check for the common case, ' delimited
252 my $start = index($Config_SH_expanded, "\n$marker$quote_type");
253 # If that failed, check for " delimited
254 if ($start == -1) {
255 $quote_type = '"';
256 $start = index($Config_SH_expanded, "\n$marker$quote_type");
257 }
258EOT
259} else {
260 $fetch_string .= <<'EOT';
261 # We only have ' delimted.
262 my $start = index($Config_SH_expanded, "\n$key=\'");
263EOT
264}
265$fetch_string .= <<'EOT';
266 # Start can never be -1 now, as we've rigged the long string we're
267 # searching with an initial dummy newline.
268 return undef if $start == -1;
269
270 $start += length($key) + 3;
271
272EOT
273if (!$seen_quotes{'"'}) {
274 # Don't need the full ' and " code, or the eval expansion.
275 $fetch_string .= <<'EOT';
276 my $value = substr($Config_SH_expanded, $start,
277 index($Config_SH_expanded, "'\n", $start)
278 - $start);
279EOT
280} else {
281 $fetch_string .= <<'EOT';
282 my $value = substr($Config_SH_expanded, $start,
283 index($Config_SH_expanded, "$quote_type\n", $start)
284 - $start);
285
286 # If we had a double-quote, we'd better eval it so escape
287 # sequences and such can be interpolated. Since the incoming
288 # value is supposed to follow shell rules and not perl rules,
289 # we escape any perl variable markers
290 if ($quote_type eq '"') {
291 $value =~ s/\$/\\\$/g;
292 $value =~ s/\@/\\\@/g;
293 eval "\$value = \"$value\"";
294 }
295EOT
296}
297$fetch_string .= <<'EOT';
298 # So we can say "if $Config{'foo'}".
299 $value = undef if $value eq 'undef';
300 $self->{$key} = $value; # cache it
301}
302EOT
303
304eval $fetch_string;
305die if $@;
3c81428c 306
8468119f 307# Calculation for the keys for byteorder
308# This is somewhat grim, but I need to run fetch_string here.
deeea481 309our $Config_SH_expanded = join "\n", '', @v_others;
8468119f 310
311my $t = fetch_string ({}, 'ivtype');
312my $s = fetch_string ({}, 'ivsize');
313
314# byteorder does exist on its own but we overlay a virtual
315# dynamically recomputed value.
316
317# However, ivtype and ivsize will not vary for sane fat binaries
318
319my $f = $t eq 'long' ? 'L!' : $s == 8 ? 'Q': 'I';
320
321my $byteorder_code;
322if ($s == 4 || $s == 8) {
323 my $list = join ',', reverse(2..$s);
324 my $format = 'a'x$s;
325 $byteorder_code = <<"EOT";
2855b621 326
8468119f 327my \$i = 0;
328foreach my \$c ($list) { \$i |= ord(\$c); \$i <<= 8 }
329\$i |= ord(1);
2d9d8159 330our \$byteorder = join('', unpack('$format', pack('$f', \$i)));
8468119f 331EOT
332} else {
2d9d8159 333 $byteorder_code = "our \$byteorder = '?'x$s;\n";
8468119f 334}
335
88fe16b2 336my @need_relocation;
337
338if (fetch_string({},'userelocatableinc')) {
4d20abad 339 foreach my $what (qw(prefixexp
340
341 archlibexp
342 html1direxp
343 html3direxp
344 man1direxp
345 man3direxp
91f668c3 346 privlibexp
4d20abad 347 scriptdirexp
91f668c3 348 sitearchexp
4d20abad 349 sitebinexp
350 sitehtml1direxp
351 sitehtml3direxp
91f668c3 352 sitelibexp
4d20abad 353 siteman1direxp
354 siteman3direxp
355 sitescriptexp
91f668c3 356 vendorarchexp
4d20abad 357 vendorbinexp
358 vendorhtml1direxp
359 vendorhtml3direxp
91f668c3 360 vendorlibexp
4d20abad 361 vendorman1direxp
362 vendorman3direxp
363 vendorscriptexp
364
365 siteprefixexp
366 sitelib_stem
1d230ada 367 vendorlib_stem
368
369 installarchlib
370 installhtml1dir
371 installhtml3dir
372 installman1dir
373 installman3dir
374 installprefix
375 installprefixexp
376 installprivlib
377 installscript
378 installsitearch
379 installsitebin
380 installsitehtml1dir
381 installsitehtml3dir
382 installsitelib
383 installsiteman1dir
384 installsiteman3dir
385 installsitescript
386 installvendorarch
387 installvendorbin
388 installvendorhtml1dir
389 installvendorhtml3dir
390 installvendorlib
391 installvendorman1dir
392 installvendorman3dir
393 installvendorscript
394 )) {
88fe16b2 395 push @need_relocation, $what if fetch_string({}, $what) =~ m!^\.\.\./!;
396 }
88fe16b2 397}
398
399my %need_relocation;
400@need_relocation{@need_relocation} = @need_relocation;
401
91f668c3 402# This can have .../ anywhere:
403if (fetch_string({}, 'otherlibdirs') =~ m!\.\.\./!) {
404 $need_relocation{otherlibdirs} = 'otherlibdirs';
405}
406
88fe16b2 407my $relocation_code = <<'EOT';
408
409sub relocate_inc {
410 my $libdir = shift;
411 return $libdir unless $libdir =~ s!^\.\.\./!!;
412 my $prefix = $^X;
413 if ($prefix =~ s!/[^/]*$!!) {
414 while ($libdir =~ m!^\.\./!) {
415 # Loop while $libdir starts "../" and $prefix still has a trailing
416 # directory
417 last unless $prefix =~ s!/([^/]+)$!!;
418 # but bail out if the directory we picked off the end of $prefix is .
419 # or ..
420 if ($1 eq '.' or $1 eq '..') {
421 # Undo! This should be rare, hence code it this way rather than a
422 # check each time before the s!!! above.
423 $prefix = "$prefix/$1";
424 last;
425 }
426 # Remove that leading ../ and loop again
427 substr ($libdir, 0, 3, '');
428 }
429 $libdir = "$prefix/$libdir";
430 }
431 $libdir;
432}
433EOT
434
91f668c3 435if (%need_relocation) {
88fe16b2 436 my $relocations_in_common;
91f668c3 437 # otherlibdirs only features in the hash
438 foreach (keys %need_relocation) {
88fe16b2 439 $relocations_in_common++ if $Common{$_};
440 }
441 if ($relocations_in_common) {
962e59f3 442 $config_txt .= $relocation_code;
88fe16b2 443 } else {
962e59f3 444 $heavy_txt .= $relocation_code;
88fe16b2 445 }
446}
447
962e59f3 448$heavy_txt .= join('', @non_v) . "\n";
3c81428c 449
5435c704 450# copy config summary format from the myconfig.SH script
962e59f3 451$heavy_txt .= "our \$summary = <<'!END!';\n";
3b5ca523 452open(MYCONFIG,"<myconfig.SH") || die "open myconfig.SH failed: $!";
54310121 4531 while defined($_ = <MYCONFIG>) && !/^Summary of/;
962e59f3 454do { $heavy_txt .= $_ } until !defined($_ = <MYCONFIG>) || /^\s*$/;
3c81428c 455close(MYCONFIG);
a0d0e21e 456
962e59f3 457$heavy_txt .= "\n!END!\n" . <<'EOT';
90ec21fb 458my $summary_expanded;
3c81428c 459
460sub myconfig {
90ec21fb 461 return $summary_expanded if $summary_expanded;
462 ($summary_expanded = $summary) =~ s{\$(\w+)}
2d9d8159 463 { my $c = $Config::Config{$1}; defined($c) ? $c : 'undef' }ge;
90ec21fb 464 $summary_expanded;
3c81428c 465}
5435c704 466
8468119f 467local *_ = \my $a;
468$_ = <<'!END!';
3c81428c 469EOT
470
962e59f3 471$heavy_txt .= join('', sort @v_others) . "!END!\n";
2855b621 472
473# Only need the dynamic byteorder code in Config.pm if 'byteorder' is one of
474# the precached keys
475if ($Common{byteorder}) {
962e59f3 476 $config_txt .= $byteorder_code;
2855b621 477} else {
962e59f3 478 $heavy_txt .= $byteorder_code;
2855b621 479}
5435c704 480
88fe16b2 481if (@need_relocation) {
962e59f3 482$heavy_txt .= 'foreach my $what (qw(' . join (' ', @need_relocation) .
483 ")) {\n" . <<'EOT';
86ef366e 484 s<^($what=)(['"])(.*?)\2>
485 <$1 . $2 . relocate_inc($3) . $2 . "\n" .
486 'raw_' . $1 . $2 . $3 . $2>me;
88fe16b2 487}
488EOT
91f668c3 489# Currently it only makes sense to do the ... relocation on Unix, so there's
490# no need to emulate the "which separator for this platform" logic in perl.c -
491# ':' will always be applicable
492if ($need_relocation{otherlibdirs}) {
962e59f3 493$heavy_txt .= << 'EOT';
91f668c3 494s{^(otherlibdirs=)(['"])(.*?)\2}
495 {$1 . $2 . join ':', map {relocate_inc($_)} split ':', $3 . $2}me;
496EOT
497}
88fe16b2 498}
499
962e59f3 500$heavy_txt .= <<'EOT';
2d9d8159 501s/(byteorder=)(['"]).*?\2/$1$2$Config::byteorder$2/m;
43d06990 502
503my $config_sh_len = length $_;
3be00128 504
e935c5a4 505our $Config_SH_expanded = "\n$_" . << 'EOVIRTUAL';
8468119f 506EOT
507
06482b90 508foreach my $prefix (qw(ccflags ldflags)) {
509 my $value = fetch_string ({}, $prefix);
510 my $withlargefiles = fetch_string ({}, $prefix . "_uselargefiles");
27da23d5 511 if (defined $withlargefiles) {
512 $value =~ s/\Q$withlargefiles\E\b//;
962e59f3 513 $heavy_txt .= "${prefix}_nolargefiles='$value'\n";
27da23d5 514 }
06482b90 515}
5435c704 516
06482b90 517foreach my $prefix (qw(libs libswanted)) {
518 my $value = fetch_string ({}, $prefix);
27da23d5 519 my $withlf = fetch_string ({}, 'libswanted_uselargefiles');
520 next unless defined $withlf;
06482b90 521 my @lflibswanted
522 = split(' ', fetch_string ({}, 'libswanted_uselargefiles'));
523 if (@lflibswanted) {
524 my %lflibswanted;
525 @lflibswanted{@lflibswanted} = ();
526 if ($prefix eq 'libs') {
527 my @libs = grep { /^-l(.+)/ &&
528 not exists $lflibswanted{$1} }
529 split(' ', fetch_string ({}, 'libs'));
530 $value = join(' ', @libs);
531 } else {
532 my @libswanted = grep { not exists $lflibswanted{$_} }
533 split(' ', fetch_string ({}, 'libswanted'));
534 $value = join(' ', @libswanted);
4b2ec495 535 }
435ec615 536 }
962e59f3 537 $heavy_txt .= "${prefix}_nolargefiles='$value'\n";
5435c704 538}
539
962e59f3 540$heavy_txt .= "EOVIRTUAL\n";
06482b90 541
962e59f3 542$heavy_txt .= $fetch_string;
06482b90 543
962e59f3 544$config_txt .= <<'ENDOFEND';
06482b90 545
2d9d8159 546sub FETCH {
5435c704 547 my($self, $key) = @_;
548
549 # check for cached value (which may be undef so we use exists not defined)
550 return $self->{$key} if exists $self->{$key};
551
06482b90 552 return $self->fetch_string($key);
a0d0e21e 553}
2d9d8159 554ENDOFEND
555
962e59f3 556$heavy_txt .= <<'ENDOFEND';
1a9ca827 557
3c81428c 558my $prevpos = 0;
559
a0d0e21e 560sub FIRSTKEY {
561 $prevpos = 0;
2ddb7828 562 substr($Config_SH_expanded, 1, index($Config_SH_expanded, '=') - 1 );
a0d0e21e 563}
564
565sub NEXTKEY {
1a9ca827 566ENDOFEND
567if ($seen_quotes{'"'}) {
962e59f3 568$heavy_txt .= <<'ENDOFEND';
435ec615 569 # Find out how the current key's quoted so we can skip to its end.
3be00128 570 my $quote = substr($Config_SH_expanded,
571 index($Config_SH_expanded, "=", $prevpos)+1, 1);
572 my $pos = index($Config_SH_expanded, qq($quote\n), $prevpos) + 2;
1a9ca827 573ENDOFEND
574} else {
575 # Just ' quotes, so it's much easier.
962e59f3 576$heavy_txt .= <<'ENDOFEND';
1a9ca827 577 my $pos = index($Config_SH_expanded, qq('\n), $prevpos) + 2;
578ENDOFEND
579}
962e59f3 580$heavy_txt .= <<'ENDOFEND';
3be00128 581 my $len = index($Config_SH_expanded, "=", $pos) - $pos;
a0d0e21e 582 $prevpos = $pos;
3be00128 583 $len > 0 ? substr($Config_SH_expanded, $pos, $len) : undef;
85e6fe83 584}
a0d0e21e 585
2ddb7828 586sub EXISTS {
5435c704 587 return 1 if exists($_[0]->{$_[1]});
588
1a9ca827 589 return(index($Config_SH_expanded, "\n$_[1]='") != -1
590ENDOFEND
591if ($seen_quotes{'"'}) {
962e59f3 592$heavy_txt .= <<'ENDOFEND';
1a9ca827 593 or index($Config_SH_expanded, "\n$_[1]=\"") != -1
594ENDOFEND
595}
962e59f3 596$heavy_txt .= <<'ENDOFEND';
5435c704 597 );
a0d0e21e 598}
599
3c81428c 600sub STORE { die "\%Config::Config is read-only\n" }
5435c704 601*DELETE = \&STORE;
602*CLEAR = \&STORE;
a0d0e21e 603
3c81428c 604
605sub config_sh {
43d06990 606 substr $Config_SH_expanded, 1, $config_sh_len;
748a9306 607}
9193ea20 608
609sub config_re {
610 my $re = shift;
3be00128 611 return map { chomp; $_ } grep eval{ /^(?:$re)=/ }, split /^/,
612 $Config_SH_expanded;
9193ea20 613}
614
3c81428c 615sub config_vars {
307dc113 616 # implements -V:cfgvar option (see perlrun -V:)
a48f8c77 617 foreach (@_) {
307dc113 618 # find optional leading, trailing colons; and query-spec
4a305f6a 619 my ($notag,$qry,$lncont) = m/^(:)?(.*?)(:)?$/; # flags fore and aft,
307dc113 620 # map colon-flags to print decorations
621 my $prfx = $notag ? '': "$qry="; # tag-prefix for print
622 my $lnend = $lncont ? ' ' : ";\n"; # line ending for print
4a305f6a 623
307dc113 624 # all config-vars are by definition \w only, any \W means regex
4a305f6a 625 if ($qry =~ /\W/) {
626 my @matches = config_re($qry);
627 print map "$_$lnend", @matches ? @matches : "$qry: not found" if !$notag;
628 print map { s/\w+=//; "$_$lnend" } @matches ? @matches : "$qry: not found" if $notag;
a48f8c77 629 } else {
2d9d8159 630 my $v = (exists $Config::Config{$qry}) ? $Config::Config{$qry}
631 : 'UNKNOWN';
a48f8c77 632 $v = 'undef' unless defined $v;
4a305f6a 633 print "${prfx}'${v}'$lnend";
a48f8c77 634 }
3c81428c 635 }
636}
637
2d9d8159 638# Called by the real AUTOLOAD
639sub launcher {
640 undef &AUTOLOAD;
641 goto \&$Config::AUTOLOAD;
642}
643
6441;
9193ea20 645ENDOFEND
646
647if ($^O eq 'os2') {
962e59f3 648 $config_txt .= <<'ENDOFSET';
9193ea20 649my %preconfig;
650if ($OS2::is_aout) {
3be00128 651 my ($value, $v) = $Config_SH_expanded =~ m/^used_aout='(.*)'\s*$/m;
9193ea20 652 for (split ' ', $value) {
3be00128 653 ($v) = $Config_SH_expanded =~ m/^aout_$_='(.*)'\s*$/m;
9193ea20 654 $preconfig{$_} = $v eq 'undef' ? undef : $v;
655 }
656}
764df951 657$preconfig{d_fork} = undef unless $OS2::can_fork; # Some funny cases can't
9193ea20 658sub TIEHASH { bless {%preconfig} }
659ENDOFSET
a48f8c77 660 # Extract the name of the DLL from the makefile to avoid duplication
661 my ($f) = grep -r, qw(GNUMakefile Makefile);
662 my $dll;
663 if (open my $fh, '<', $f) {
664 while (<$fh>) {
665 $dll = $1, last if /^PERL_DLL_BASE\s*=\s*(\S*)\s*$/;
666 }
30500b05 667 }
962e59f3 668 $config_txt .= <<ENDOFSET if $dll;
30500b05 669\$preconfig{dll_name} = '$dll';
670ENDOFSET
9193ea20 671} else {
962e59f3 672 $config_txt .= <<'ENDOFSET';
5435c704 673sub TIEHASH {
674 bless $_[1], $_[0];
675}
9193ea20 676ENDOFSET
677}
678
a8e1d30b 679foreach my $key (keys %Common) {
680 my $value = fetch_string ({}, $key);
681 # Is it safe on the LHS of => ?
682 my $qkey = $key =~ /^[A-Za-z_][A-Za-z0-9_]*$/ ? $key : "'$key'";
683 if (defined $value) {
684 # Quote things for a '' string
685 $value =~ s!\\!\\\\!g;
686 $value =~ s!'!\\'!g;
687 $value = "'$value'";
91f668c3 688 if ($key eq 'otherlibdirs') {
689 $value = "join (':', map {relocate_inc(\$_)} split (':', $value))";
690 } elsif ($need_relocation{$key}) {
88fe16b2 691 $value = "relocate_inc($value)";
692 }
a8e1d30b 693 } else {
694 $value = "undef";
695 }
696 $Common{$key} = "$qkey => $value";
697}
2855b621 698
699if ($Common{byteorder}) {
700 $Common{byteorder} = 'byteorder => $byteorder';
701}
702my $fast_config = join '', map { " $_,\n" } sort values %Common;
5435c704 703
938af39e 704# Sanity check needed to stop an infite loop if Config_heavy.pl fails to define
705# &launcher for some reason (eg it got truncated)
962e59f3 706$config_txt .= sprintf <<'ENDOFTIE', $fast_config;
9193ea20 707
fb73857a 708sub DESTROY { }
709
2d9d8159 710sub AUTOLOAD {
c1b2b415 711 require 'Config_heavy.pl';
938af39e 712 goto \&launcher unless $Config::AUTOLOAD =~ /launcher$/;
2d9d8159 713 die "&Config::AUTOLOAD failed on $Config::AUTOLOAD";
714}
715
2c165900 716# tie returns the object, so the value returned to require will be true.
5435c704 717tie %%Config, 'Config', {
a8e1d30b 718%s};
5435c704 719ENDOFTIE
720
748a9306 721
5435c704 722open(CONFIG_POD, ">lib/Config.pod") or die "Can't open lib/Config.pod: $!";
723print CONFIG_POD <<'ENDOFTAIL';
3c81428c 724=head1 NAME
a0d0e21e 725
3c81428c 726Config - access Perl configuration information
727
728=head1 SYNOPSIS
729
730 use Config;
63f18be6 731 if ($Config{usethreads}) {
732 print "has thread support\n"
3c81428c 733 }
734
a48f8c77 735 use Config qw(myconfig config_sh config_vars config_re);
3c81428c 736
737 print myconfig();
738
739 print config_sh();
740
a48f8c77 741 print config_re();
742
3c81428c 743 config_vars(qw(osname archname));
744
745
746=head1 DESCRIPTION
747
748The Config module contains all the information that was available to
749the C<Configure> program at Perl build time (over 900 values).
750
751Shell variables from the F<config.sh> file (written by Configure) are
752stored in the readonly-variable C<%Config>, indexed by their names.
753
754Values stored in config.sh as 'undef' are returned as undefined
1fef88e7 755values. The perl C<exists> function can be used to check if a
3c81428c 756named variable exists.
757
758=over 4
759
760=item myconfig()
761
762Returns a textual summary of the major perl configuration values.
763See also C<-V> in L<perlrun/Switches>.
764
765=item config_sh()
766
767Returns the entire perl configuration information in the form of the
768original config.sh shell variable assignment script.
769
a48f8c77 770=item config_re($regex)
771
772Like config_sh() but returns, as a list, only the config entries who's
773names match the $regex.
774
3c81428c 775=item config_vars(@names)
776
777Prints to STDOUT the values of the named configuration variable. Each is
778printed on a separate line in the form:
779
780 name='value';
781
782Names which are unknown are output as C<name='UNKNOWN';>.
783See also C<-V:name> in L<perlrun/Switches>.
784
785=back
786
787=head1 EXAMPLE
788
789Here's a more sophisticated example of using %Config:
790
791 use Config;
743c51bc 792 use strict;
793
794 my %sig_num;
795 my @sig_name;
796 unless($Config{sig_name} && $Config{sig_num}) {
797 die "No sigs?";
798 } else {
799 my @names = split ' ', $Config{sig_name};
800 @sig_num{@names} = split ' ', $Config{sig_num};
801 foreach (@names) {
802 $sig_name[$sig_num{$_}] ||= $_;
803 }
804 }
3c81428c 805
743c51bc 806 print "signal #17 = $sig_name[17]\n";
807 if ($sig_num{ALRM}) {
808 print "SIGALRM is $sig_num{ALRM}\n";
3c81428c 809 }
810
811=head1 WARNING
812
813Because this information is not stored within the perl executable
814itself it is possible (but unlikely) that the information does not
815relate to the actual perl binary which is being used to access it.
816
817The Config module is installed into the architecture and version
818specific library directory ($Config{installarchlib}) and it checks the
819perl version number when loaded.
820
435ec615 821The values stored in config.sh may be either single-quoted or
822double-quoted. Double-quoted strings are handy for those cases where you
823need to include escape sequences in the strings. To avoid runtime variable
824interpolation, any C<$> and C<@> characters are replaced by C<\$> and
825C<\@>, respectively. This isn't foolproof, of course, so don't embed C<\$>
826or C<\@> in double-quoted strings unless you're willing to deal with the
827consequences. (The slashes will end up escaped and the C<$> or C<@> will
828trigger variable interpolation)
829
ebc74a4b 830=head1 GLOSSARY
831
832Most C<Config> variables are determined by the C<Configure> script
833on platforms supported by it (which is most UNIX platforms). Some
834platforms have custom-made C<Config> variables, and may thus not have
835some of the variables described below, or may have extraneous variables
836specific to that particular port. See the port specific documentation
837in such cases.
838
ebc74a4b 839ENDOFTAIL
840
5435c704 841if ($Opts{glossary}) {
842 open(GLOS, "<$Glossary") or die "Can't open $Glossary: $!";
18f68570 843}
2f4f46ad 844my %seen = ();
845my $text = 0;
fb87c415 846$/ = '';
847
848sub process {
aade5aff 849 if (s/\A(\w*)\s+\(([\w.]+)\):\s*\n(\t?)/=item C<$1>\n\nFrom F<$2>:\n\n/m) {
850 my $c = substr $1, 0, 1;
851 unless ($seen{$c}++) {
5435c704 852 print CONFIG_POD <<EOF if $text;
fb87c415 853=back
ebc74a4b 854
fb87c415 855EOF
5435c704 856 print CONFIG_POD <<EOF;
fb87c415 857=head2 $c
858
bbc7dcd2 859=over 4
fb87c415 860
861EOF
aade5aff 862 $text = 1;
863 }
864 }
865 elsif (!$text || !/\A\t/) {
866 warn "Expected a Configure variable header",
867 ($text ? " or another paragraph of description" : () );
fb87c415 868 }
869 s/n't/n\00t/g; # leave can't, won't etc untouched
9b22980b 870 s/^\t\s+(.*)/\n$1/gm; # Indented lines ===> new paragraph
fb87c415 871 s/^(?<!\n\n)\t(.*)/$1/gm; # Not indented lines ===> text
872 s{([\'\"])(?=[^\'\"\s]*[./][^\'\"\s]*\1)([^\'\"\s]+)\1}(F<$2>)g; # '.o'
873 s{([\'\"])([^\'\"\s]+)\1}(C<$2>)g; # "date" command
874 s{\'([A-Za-z_\- *=/]+)\'}(C<$1>)g; # 'ln -s'
875 s{
876 (?<! [\w./<\'\"] ) # Only standalone file names
877 (?! e \. g \. ) # Not e.g.
878 (?! \. \. \. ) # Not ...
879 (?! \d ) # Not 5.004
a1151a3c 880 (?! read/ ) # Not read/write
881 (?! etc\. ) # Not etc.
882 (?! I/O ) # Not I/O
883 (
884 \$ ? # Allow leading $
885 [\w./]* [./] [\w./]* # Require . or / inside
886 )
887 (?<! \. (?= [\s)] ) ) # Do not include trailing dot
fb87c415 888 (?! [\w/] ) # Include all of it
889 }
890 (F<$1>)xg; # /usr/local
891 s/((?<=\s)~\w*)/F<$1>/g; # ~name
892 s/(?<![.<\'\"])\b([A-Z_]{2,})\b(?![\'\"])/C<$1>/g; # UNISTD
893 s/(?<![.<\'\"])\b(?!the\b)(\w+)\s+macro\b/C<$1> macro/g; # FILE_cnt macro
894 s/n[\0]t/n't/g; # undo can't, won't damage
ebc74a4b 895}
896
5435c704 897if ($Opts{glossary}) {
7701ffb5 898 <GLOS>; # Skip the "DO NOT EDIT"
899 <GLOS>; # Skip the preamble
18f68570 900 while (<GLOS>) {
901 process;
5435c704 902 print CONFIG_POD;
18f68570 903 }
fb87c415 904}
ebc74a4b 905
5435c704 906print CONFIG_POD <<'ENDOFTAIL';
ebc74a4b 907
908=back
909
3c81428c 910=head1 NOTE
911
912This module contains a good example of how to use tie to implement a
913cache and an example of how to make a tied variable readonly to those
914outside of it.
915
916=cut
a0d0e21e 917
9193ea20 918ENDOFTAIL
a0d0e21e 919
962e59f3 920close(GLOS) if $Opts{glossary};
5435c704 921close(CONFIG_POD);
962e59f3 922print "written lib/Config.pod\n";
923
924my $orig_config_txt = "";
925my $orig_heavy_txt = "";
926{
927 local $/;
928 my $fh;
929 $orig_config_txt = <$fh> if open $fh, "<", $Config_PM;
930 $orig_heavy_txt = <$fh> if open $fh, "<", $Config_heavy;
931}
932
933if ($orig_config_txt ne $config_txt or $orig_heavy_txt ne $heavy_txt) {
934 open CONFIG, ">", $Config_PM or die "Can't open $Config_PM: $!\n";
935 open CONFIG_HEAVY, ">", $Config_heavy or die "Can't open $Config_heavy: $!\n";
936 print CONFIG $config_txt;
937 print CONFIG_HEAVY $heavy_txt;
938 close(CONFIG_HEAVY);
939 close(CONFIG);
940 print "updated $Config_PM\n";
941 print "updated $Config_heavy\n";
942}
943
a0d0e21e 944
18f68570 945# Now create Cross.pm if needed
5435c704 946if ($Opts{cross}) {
18f68570 947 open CROSS, ">lib/Cross.pm" or die "Can not open >lib/Cross.pm: $!";
47bcb90d 948 my $cross = <<'EOS';
949# typical invocation:
950# perl -MCross Makefile.PL
951# perl -MCross=wince -V:cc
952package Cross;
953
954sub import {
955 my ($package,$platform) = @_;
956 unless (defined $platform) {
957 # if $platform is not specified, then use last one when
958 # 'configpm; was invoked with --cross option
959 $platform = '***replace-marker***';
960 }
961 @INC = map {/\blib\b/?(do{local $_=$_;s/\blib\b/xlib\/$platform/;$_},$_):($_)} @INC;
e2a02c1e 962 $::Cross::platform = $platform;
18f68570 963}
47bcb90d 964
18f68570 9651;
966EOS
5435c704 967 $cross =~ s/\*\*\*replace-marker\*\*\*/$Opts{cross}/g;
47bcb90d 968 print CROSS $cross;
18f68570 969 close CROSS;
962e59f3 970 print "written lib/Cross.pm\n";
42d1cefd 971 unshift(@INC,"xlib/$Opts{cross}");
18f68570 972}
973
a0d0e21e 974# Now do some simple tests on the Config.pm file we have created
975unshift(@INC,'lib');
27da23d5 976unshift(@INC,'xlib/symbian') if $Opts{cross};
5435c704 977require $Config_PM;
ae7e4cc1 978require $Config_heavy;
a0d0e21e 979import Config;
980
5435c704 981die "$0: $Config_PM not valid"
a02608de 982 unless $Config{'PERL_CONFIG_SH'} eq 'true';
a0d0e21e 983
5435c704 984die "$0: error processing $Config_PM"
a0d0e21e 985 if defined($Config{'an impossible name'})
a02608de 986 or $Config{'PERL_CONFIG_SH'} ne 'true' # test cache
a0d0e21e 987 ;
988
5435c704 989die "$0: error processing $Config_PM"
a0d0e21e 990 if eval '$Config{"cc"} = 1'
991 or eval 'delete $Config{"cc"}'
992 ;
993
994
85e6fe83 995exit 0;
a8e1d30b 996# Popularity of various entries in %Config, based on a large build and test
997# run of code in the Fotango build system:
998__DATA__
999path_sep: 8490
1000d_readlink: 7101
1001d_symlink: 7101
1002archlibexp: 4318
1003sitearchexp: 4305
1004sitelibexp: 4305
1005privlibexp: 4163
1006ldlibpthname: 4041
1007libpth: 2134
1008archname: 1591
1009exe_ext: 1256
1010scriptdir: 1155
1011version: 1116
1012useithreads: 1002
1013osvers: 982
1014osname: 851
1015inc_version_list: 783
1016dont_use_nlink: 779
1017intsize: 759
1018usevendorprefix: 642
1019dlsrc: 624
1020cc: 541
1021lib_ext: 520
1022so: 512
1023ld: 501
1024ccdlflags: 500
1025ldflags: 495
1026obj_ext: 495
1027cccdlflags: 493
1028lddlflags: 493
1029ar: 492
1030dlext: 492
1031libc: 492
1032ranlib: 492
1033full_ar: 491
1034vendorarchexp: 491
1035vendorlibexp: 491
1036installman1dir: 489
1037installman3dir: 489
1038installsitebin: 489
1039installsiteman1dir: 489
1040installsiteman3dir: 489
1041installvendorman1dir: 489
1042installvendorman3dir: 489
1043d_flexfnam: 474
1044eunicefix: 360
1045d_link: 347
1046installsitearch: 344
1047installscript: 341
1048installprivlib: 337
1049binexp: 336
1050installarchlib: 336
1051installprefixexp: 336
1052installsitelib: 336
1053installstyle: 336
1054installvendorarch: 336
1055installvendorbin: 336
1056installvendorlib: 336
1057man1ext: 336
1058man3ext: 336
1059sh: 336
1060siteprefixexp: 336
1061installbin: 335
1062usedl: 332
1063ccflags: 285
1064startperl: 232
1065optimize: 231
1066usemymalloc: 229
1067cpprun: 228
1068sharpbang: 228
1069perllibs: 225
1070usesfio: 224
1071usethreads: 220
1072perlpath: 218
1073extensions: 217
1074usesocks: 208
1075shellflags: 198
1076make: 191
1077d_pwage: 189
1078d_pwchange: 189
1079d_pwclass: 189
1080d_pwcomment: 189
1081d_pwexpire: 189
1082d_pwgecos: 189
1083d_pwpasswd: 189
1084d_pwquota: 189
1085gccversion: 189
1086libs: 186
1087useshrplib: 186
1088cppflags: 185
1089ptrsize: 185
1090shrpenv: 185
1091static_ext: 185
1092use5005threads: 185
1093uselargefiles: 185
1094alignbytes: 184
1095byteorder: 184
1096ccversion: 184
1097config_args: 184
1098cppminus: 184