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