This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / configpm
CommitLineData
a0d0e21e 1#!./miniperl -w
de7128ac
NC
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
b9f36698
CB
39use strict;
40use vars qw(%Config $Config_SH_expanded);
8990e307 41
b9f36698
CB
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
NC
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 = (
b9f36698
CB
60 'cross' => '', # --cross=PLATFORM - crosscompiling for PLATFORM
61 'glossary' => 1, # --no-glossary - no glossary file inclusion,
5435c704 62 # for compactness
b9f36698 63 'heavy' => '', # pathname of the Config_heavy.pl file
18f68570 64);
18f68570 65
5435c704
NC
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) {
de7128ac
NC
78 warn "option '$opt' is not recognized";
79 usage;
5435c704
NC
80 }
81 @ARGV = grep {!/^--/} @ARGV;
82
83 return %opts;
84}
18f68570 85
5435c704
NC
86
87my %Opts = opts();
88
b9f36698 89my ($Config_PM, $Config_heavy);
5435c704
NC
90my $Glossary = $ARGV[1] || 'Porting/Glossary';
91
92if ($Opts{cross}) {
18f68570
VK
93 # creating cross-platform config file
94 mkdir "xlib";
5435c704
NC
95 mkdir "xlib/$Opts{cross}";
96 $Config_PM = $ARGV[0] || "xlib/$Opts{cross}/Config.pm";
18f68570
VK
97}
98else {
5435c704 99 $Config_PM = $ARGV[0] || 'lib/Config.pm';
18f68570 100}
b9f36698
CB
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
de7128ac
NC
110my $config_txt;
111my $heavy_txt;
b9f36698 112
de7128ac 113$heavy_txt .= <<'ENDOFBEG';
b9f36698
CB
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
37cd0abf 123my $myver = sprintf "v%vd", $^V;
a0d0e21e 124
de7128ac 125$config_txt .= sprintf <<'ENDOFBEG', ($myver) x 3;
5435c704
NC
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;
b9f36698
CB
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);
135
136# Need to stub all the functions to make code such as print Config::config_sh
137# keep working
a48f8c77 138
b9f36698
CB
139sub myconfig;
140sub config_sh;
141sub config_vars;
142sub config_re;
143
144my %%Export_Cache = map {($_ => 1)} (@Config::EXPORT, @Config::EXPORT_OK);
145
146our %%Config;
e3d0cac0
IZ
147
148# Define our own import method to avoid pulling in the full Exporter:
149sub import {
a48f8c77 150 my $pkg = shift;
b9f36698 151 @_ = @Config::EXPORT unless @_;
5435c704 152
a48f8c77
MS
153 my @funcs = grep $_ ne '%%Config', @_;
154 my $export_Config = @funcs < @_ ? 1 : 0;
5435c704 155
b9f36698 156 no strict 'refs';
a48f8c77
MS
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
MS
164 *{"$callpkg\::Config"} = \%%Config if $export_Config;
165 return;
e3d0cac0
IZ
166}
167
5435c704
NC
168die "Perl lib version (%s) doesn't match executable version ($])"
169 unless $^V;
de98c553 170
37cd0abf
JH
171$^V eq %s
172 or die "Perl lib version (%s) doesn't match executable version (" .
173 sprintf("v%%vd",$^V) . ")";
a0d0e21e 174
8990e307
LW
175ENDOFBEG
176
16d20bd9 177
5435c704 178my @non_v = ();
5435c704
NC
179my @v_others = ();
180my $in_v = 0;
181my %Data = ();
182
5435c704 183
b9f36698
CB
184my %seen_quotes;
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
GS
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
JH
204 push @v_others, "CONFIG='$v'\n";
205 }
cceca5ed 206 }
5435c704 207
435ec615
HM
208 # We can delimit things in config.sh with either ' or ".
209 unless ($in_v or m/^(\w+)=(['"])(.*\n)/){
a0d0e21e
LW
210 push(@non_v, "#$_"); # not a name='value' line
211 next;
212 }
b9f36698 213 my $quote = $2;
5435c704
NC
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";
b9f36698
CB
228 push(@v_others, $line);
229 $seen_quotes{$quote}++;
230 }
231 close CONFIG_SH;
232}
233
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");
5435c704 257 }
b9f36698
CB
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\"";
5435c704 294 }
b9f36698 295EOT
5435c704 296}
b9f36698
CB
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
1499d750
JC
307# Calculation for the keys for byteorder
308# This is somewhat grim, but I need to run fetch_string here.
b9f36698 309our $Config_SH_expanded = join "\n", '', @v_others;
1499d750
JC
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";
b9f36698 326
1499d750
JC
327my \$i = 0;
328foreach my \$c ($list) { \$i |= ord(\$c); \$i <<= 8 }
329\$i |= ord(1);
b9f36698 330our \$byteorder = join('', unpack('$format', pack('$f', \$i)));
1499d750
JC
331EOT
332} else {
b9f36698 333 $byteorder_code = "our \$byteorder = '?'x$s;\n";
1499d750
JC
334}
335
63fe74dd
NC
336my @need_relocation;
337
338if (fetch_string({},'userelocatableinc')) {
de7128ac
NC
339 foreach my $what (qw(prefixexp
340
341 archlibexp
342 html1direxp
343 html3direxp
344 man1direxp
345 man3direxp
63fe74dd 346 privlibexp
de7128ac 347 scriptdirexp
63fe74dd 348 sitearchexp
de7128ac
NC
349 sitebinexp
350 sitehtml1direxp
351 sitehtml3direxp
63fe74dd 352 sitelibexp
de7128ac
NC
353 siteman1direxp
354 siteman3direxp
355 sitescriptexp
63fe74dd 356 vendorarchexp
de7128ac
NC
357 vendorbinexp
358 vendorhtml1direxp
359 vendorhtml3direxp
63fe74dd 360 vendorlibexp
de7128ac
NC
361 vendorman1direxp
362 vendorman3direxp
363 vendorscriptexp
364
365 siteprefixexp
366 sitelib_stem
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 )) {
63fe74dd
NC
395 push @need_relocation, $what if fetch_string({}, $what) =~ m!^\.\.\./!;
396 }
397}
398
399my %need_relocation;
400@need_relocation{@need_relocation} = @need_relocation;
401
402# This can have .../ anywhere:
403if (fetch_string({}, 'otherlibdirs') =~ m!\.\.\./!) {
404 $need_relocation{otherlibdirs} = 'otherlibdirs';
405}
406
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
435if (%need_relocation) {
436 my $relocations_in_common;
437 # otherlibdirs only features in the hash
438 foreach (keys %need_relocation) {
439 $relocations_in_common++ if $Common{$_};
440 }
441 if ($relocations_in_common) {
de7128ac 442 $config_txt .= $relocation_code;
63fe74dd 443 } else {
de7128ac 444 $heavy_txt .= $relocation_code;
63fe74dd
NC
445 }
446}
447
de7128ac 448$heavy_txt .= join('', @non_v) . "\n";
3c81428c 449
5435c704 450# copy config summary format from the myconfig.SH script
de7128ac 451$heavy_txt .= "our \$summary = <<'!END!';\n";
3b5ca523 452open(MYCONFIG,"<myconfig.SH") || die "open myconfig.SH failed: $!";
54310121 4531 while defined($_ = <MYCONFIG>) && !/^Summary of/;
de7128ac 454do { $heavy_txt .= $_ } until !defined($_ = <MYCONFIG>) || /^\s*$/;
3c81428c 455close(MYCONFIG);
a0d0e21e 456
de7128ac 457$heavy_txt .= "\n!END!\n" . <<'EOT';
f16a1137 458my $summary_expanded;
3c81428c 459
460sub myconfig {
f16a1137
EM
461 return $summary_expanded if $summary_expanded;
462 ($summary_expanded = $summary) =~ s{\$(\w+)}
b9f36698 463 { my $c = $Config::Config{$1}; defined($c) ? $c : 'undef' }ge;
f16a1137 464 $summary_expanded;
3c81428c 465}
5435c704 466
1499d750
JC
467local *_ = \my $a;
468$_ = <<'!END!';
3c81428c 469EOT
470
de7128ac 471$heavy_txt .= join('', sort @v_others) . "!END!\n";
5435c704 472
b9f36698
CB
473# Only need the dynamic byteorder code in Config.pm if 'byteorder' is one of
474# the precached keys
475if ($Common{byteorder}) {
de7128ac 476 $config_txt .= $byteorder_code;
b9f36698 477} else {
de7128ac 478 $heavy_txt .= $byteorder_code;
b9f36698 479}
1499d750 480
63fe74dd 481if (@need_relocation) {
de7128ac
NC
482$heavy_txt .= 'foreach my $what (qw(' . join (' ', @need_relocation) .
483 ")) {\n" . <<'EOT';
63fe74dd
NC
484 s/^($what=)(['"])(.*?)\2/$1 . $2 . relocate_inc($3) . $2/me;
485}
486EOT
487# Currently it only makes sense to do the ... relocation on Unix, so there's
488# no need to emulate the "which separator for this platform" logic in perl.c -
489# ':' will always be applicable
490if ($need_relocation{otherlibdirs}) {
de7128ac 491$heavy_txt .= << 'EOT';
63fe74dd
NC
492s{^(otherlibdirs=)(['"])(.*?)\2}
493 {$1 . $2 . join ':', map {relocate_inc($_)} split ':', $3 . $2}me;
494EOT
495}
496}
497
de7128ac 498$heavy_txt .= <<'EOT';
b9f36698 499s/(byteorder=)(['"]).*?\2/$1$2$Config::byteorder$2/m;
a0d0e21e 500
b9f36698 501my $config_sh_len = length $_;
a0d0e21e 502
f50f38a8 503our $Config_SH_expanded = "\n$_" . << 'EOVIRTUAL';
b9f36698 504EOT
5435c704 505
b9f36698
CB
506foreach my $prefix (qw(ccflags ldflags)) {
507 my $value = fetch_string ({}, $prefix);
508 my $withlargefiles = fetch_string ({}, $prefix . "_uselargefiles");
63fe74dd
NC
509 if (defined $withlargefiles) {
510 $value =~ s/\Q$withlargefiles\E\b//;
de7128ac 511 $heavy_txt .= "${prefix}_nolargefiles='$value'\n";
63fe74dd 512 }
b9f36698
CB
513}
514
515foreach my $prefix (qw(libs libswanted)) {
516 my $value = fetch_string ({}, $prefix);
63fe74dd
NC
517 my $withlf = fetch_string ({}, 'libswanted_uselargefiles');
518 next unless defined $withlf;
b9f36698
CB
519 my @lflibswanted
520 = split(' ', fetch_string ({}, 'libswanted_uselargefiles'));
521 if (@lflibswanted) {
522 my %lflibswanted;
523 @lflibswanted{@lflibswanted} = ();
524 if ($prefix eq 'libs') {
525 my @libs = grep { /^-l(.+)/ &&
526 not exists $lflibswanted{$1} }
527 split(' ', fetch_string ({}, 'libs'));
528 $value = join(' ', @libs);
529 } else {
530 my @libswanted = grep { not exists $lflibswanted{$_} }
531 split(' ', fetch_string ({}, 'libswanted'));
532 $value = join(' ', @libswanted);
4b2ec495 533 }
435ec615 534 }
de7128ac 535 $heavy_txt .= "${prefix}_nolargefiles='$value'\n";
5435c704
NC
536}
537
de7128ac 538$heavy_txt .= "EOVIRTUAL\n";
b9f36698 539
de7128ac 540$heavy_txt .= $fetch_string;
b9f36698 541
de7128ac 542$config_txt .= <<'ENDOFEND';
b9f36698
CB
543
544sub FETCH {
5435c704
NC
545 my($self, $key) = @_;
546
547 # check for cached value (which may be undef so we use exists not defined)
548 return $self->{$key} if exists $self->{$key};
549
b9f36698 550 return $self->fetch_string($key);
a0d0e21e 551}
b9f36698
CB
552ENDOFEND
553
de7128ac 554$heavy_txt .= <<'ENDOFEND';
b9f36698 555
3c81428c 556my $prevpos = 0;
557
a0d0e21e
LW
558sub FIRSTKEY {
559 $prevpos = 0;
b9f36698 560 substr($Config_SH_expanded, 1, index($Config_SH_expanded, '=') - 1 );
a0d0e21e
LW
561}
562
563sub NEXTKEY {
b9f36698
CB
564ENDOFEND
565if ($seen_quotes{'"'}) {
de7128ac 566$heavy_txt .= <<'ENDOFEND';
435ec615 567 # Find out how the current key's quoted so we can skip to its end.
b9f36698
CB
568 my $quote = substr($Config_SH_expanded,
569 index($Config_SH_expanded, "=", $prevpos)+1, 1);
570 my $pos = index($Config_SH_expanded, qq($quote\n), $prevpos) + 2;
571ENDOFEND
572} else {
573 # Just ' quotes, so it's much easier.
de7128ac 574$heavy_txt .= <<'ENDOFEND';
b9f36698
CB
575 my $pos = index($Config_SH_expanded, qq('\n), $prevpos) + 2;
576ENDOFEND
577}
de7128ac 578$heavy_txt .= <<'ENDOFEND';
b9f36698 579 my $len = index($Config_SH_expanded, "=", $pos) - $pos;
a0d0e21e 580 $prevpos = $pos;
b9f36698 581 $len > 0 ? substr($Config_SH_expanded, $pos, $len) : undef;
85e6fe83 582}
a0d0e21e 583
b9f36698 584sub EXISTS {
5435c704
NC
585 return 1 if exists($_[0]->{$_[1]});
586
b9f36698
CB
587 return(index($Config_SH_expanded, "\n$_[1]='") != -1
588ENDOFEND
589if ($seen_quotes{'"'}) {
de7128ac 590$heavy_txt .= <<'ENDOFEND';
b9f36698
CB
591 or index($Config_SH_expanded, "\n$_[1]=\"") != -1
592ENDOFEND
593}
de7128ac 594$heavy_txt .= <<'ENDOFEND';
5435c704 595 );
a0d0e21e
LW
596}
597
3c81428c 598sub STORE { die "\%Config::Config is read-only\n" }
5435c704
NC
599*DELETE = \&STORE;
600*CLEAR = \&STORE;
a0d0e21e 601
3c81428c 602
603sub config_sh {
b9f36698 604 substr $Config_SH_expanded, 1, $config_sh_len;
748a9306 605}
9193ea20 606
607sub config_re {
608 my $re = shift;
b9f36698
CB
609 return map { chomp; $_ } grep eval{ /^(?:$re)=/ }, split /^/,
610 $Config_SH_expanded;
9193ea20 611}
612
3c81428c 613sub config_vars {
1499d750 614 # implements -V:cfgvar option (see perlrun -V:)
a48f8c77 615 foreach (@_) {
1499d750 616 # find optional leading, trailing colons; and query-spec
137fa866 617 my ($notag,$qry,$lncont) = m/^(:)?(.*?)(:)?$/; # flags fore and aft,
1499d750
JC
618 # map colon-flags to print decorations
619 my $prfx = $notag ? '': "$qry="; # tag-prefix for print
620 my $lnend = $lncont ? ' ' : ";\n"; # line ending for print
137fa866 621
1499d750 622 # all config-vars are by definition \w only, any \W means regex
137fa866
JC
623 if ($qry =~ /\W/) {
624 my @matches = config_re($qry);
625 print map "$_$lnend", @matches ? @matches : "$qry: not found" if !$notag;
626 print map { s/\w+=//; "$_$lnend" } @matches ? @matches : "$qry: not found" if $notag;
a48f8c77 627 } else {
b9f36698
CB
628 my $v = (exists $Config::Config{$qry}) ? $Config::Config{$qry}
629 : 'UNKNOWN';
a48f8c77 630 $v = 'undef' unless defined $v;
137fa866 631 print "${prfx}'${v}'$lnend";
a48f8c77 632 }
3c81428c 633 }
634}
635
b9f36698
CB
636# Called by the real AUTOLOAD
637sub launcher {
638 undef &AUTOLOAD;
639 goto \&$Config::AUTOLOAD;
640}
641
6421;
9193ea20 643ENDOFEND
644
645if ($^O eq 'os2') {
de7128ac 646 $config_txt .= <<'ENDOFSET';
9193ea20 647my %preconfig;
648if ($OS2::is_aout) {
b9f36698 649 my ($value, $v) = $Config_SH_expanded =~ m/^used_aout='(.*)'\s*$/m;
9193ea20 650 for (split ' ', $value) {
b9f36698 651 ($v) = $Config_SH_expanded =~ m/^aout_$_='(.*)'\s*$/m;
9193ea20 652 $preconfig{$_} = $v eq 'undef' ? undef : $v;
653 }
654}
764df951 655$preconfig{d_fork} = undef unless $OS2::can_fork; # Some funny cases can't
9193ea20 656sub TIEHASH { bless {%preconfig} }
657ENDOFSET
a48f8c77
MS
658 # Extract the name of the DLL from the makefile to avoid duplication
659 my ($f) = grep -r, qw(GNUMakefile Makefile);
660 my $dll;
661 if (open my $fh, '<', $f) {
662 while (<$fh>) {
663 $dll = $1, last if /^PERL_DLL_BASE\s*=\s*(\S*)\s*$/;
664 }
30500b05 665 }
de7128ac 666 $config_txt .= <<ENDOFSET if $dll;
30500b05
IZ
667\$preconfig{dll_name} = '$dll';
668ENDOFSET
9193ea20 669} else {
de7128ac 670 $config_txt .= <<'ENDOFSET';
5435c704
NC
671sub TIEHASH {
672 bless $_[1], $_[0];
673}
9193ea20 674ENDOFSET
675}
676
b9f36698
CB
677foreach my $key (keys %Common) {
678 my $value = fetch_string ({}, $key);
679 # Is it safe on the LHS of => ?
680 my $qkey = $key =~ /^[A-Za-z_][A-Za-z0-9_]*$/ ? $key : "'$key'";
681 if (defined $value) {
682 # Quote things for a '' string
683 $value =~ s!\\!\\\\!g;
684 $value =~ s!'!\\'!g;
685 $value = "'$value'";
63fe74dd
NC
686 if ($key eq 'otherlibdirs') {
687 $value = "join (':', map {relocate_inc(\$_)} split (':', $value))";
688 } elsif ($need_relocation{$key}) {
689 $value = "relocate_inc($value)";
690 }
b9f36698
CB
691 } else {
692 $value = "undef";
693 }
694 $Common{$key} = "$qkey => $value";
695}
696
697if ($Common{byteorder}) {
698 $Common{byteorder} = 'byteorder => $byteorder';
699}
700my $fast_config = join '', map { " $_,\n" } sort values %Common;
5435c704 701
f50f38a8
NC
702# Sanity check needed to stop an infite loop if Config_heavy.pl fails to define
703# &launcher for some reason (eg it got truncated)
de7128ac 704$config_txt .= sprintf <<'ENDOFTIE', $fast_config;
9193ea20 705
fb73857a 706sub DESTROY { }
707
b9f36698
CB
708sub AUTOLOAD {
709 require 'Config_heavy.pl';
f50f38a8 710 goto \&launcher unless $Config::AUTOLOAD =~ /launcher$/;
b9f36698
CB
711 die "&Config::AUTOLOAD failed on $Config::AUTOLOAD";
712}
713
f50f38a8 714# tie returns the object, so the value returned to require will be true.
5435c704 715tie %%Config, 'Config', {
b9f36698 716%s};
5435c704
NC
717ENDOFTIE
718
748a9306 719
5435c704
NC
720open(CONFIG_POD, ">lib/Config.pod") or die "Can't open lib/Config.pod: $!";
721print CONFIG_POD <<'ENDOFTAIL';
3c81428c 722=head1 NAME
a0d0e21e 723
3c81428c 724Config - access Perl configuration information
725
726=head1 SYNOPSIS
727
728 use Config;
17906824
NC
729 if ($Config{usethreads}) {
730 print "has thread support\n"
3c81428c 731 }
732
a48f8c77 733 use Config qw(myconfig config_sh config_vars config_re);
3c81428c 734
735 print myconfig();
736
737 print config_sh();
738
a48f8c77
MS
739 print config_re();
740
3c81428c 741 config_vars(qw(osname archname));
742
743
744=head1 DESCRIPTION
745
746The Config module contains all the information that was available to
747the C<Configure> program at Perl build time (over 900 values).
748
749Shell variables from the F<config.sh> file (written by Configure) are
750stored in the readonly-variable C<%Config>, indexed by their names.
751
752Values stored in config.sh as 'undef' are returned as undefined
1fef88e7 753values. The perl C<exists> function can be used to check if a
3c81428c 754named variable exists.
755
756=over 4
757
758=item myconfig()
759
760Returns a textual summary of the major perl configuration values.
761See also C<-V> in L<perlrun/Switches>.
762
763=item config_sh()
764
765Returns the entire perl configuration information in the form of the
766original config.sh shell variable assignment script.
767
a48f8c77
MS
768=item config_re($regex)
769
770Like config_sh() but returns, as a list, only the config entries who's
771names match the $regex.
772
3c81428c 773=item config_vars(@names)
774
775Prints to STDOUT the values of the named configuration variable. Each is
776printed on a separate line in the form:
777
778 name='value';
779
780Names which are unknown are output as C<name='UNKNOWN';>.
781See also C<-V:name> in L<perlrun/Switches>.
782
783=back
784
785=head1 EXAMPLE
786
787Here's a more sophisticated example of using %Config:
788
789 use Config;
743c51bc
W
790 use strict;
791
792 my %sig_num;
793 my @sig_name;
794 unless($Config{sig_name} && $Config{sig_num}) {
795 die "No sigs?";
796 } else {
797 my @names = split ' ', $Config{sig_name};
798 @sig_num{@names} = split ' ', $Config{sig_num};
799 foreach (@names) {
800 $sig_name[$sig_num{$_}] ||= $_;
801 }
802 }
3c81428c 803
743c51bc
W
804 print "signal #17 = $sig_name[17]\n";
805 if ($sig_num{ALRM}) {
806 print "SIGALRM is $sig_num{ALRM}\n";
3c81428c 807 }
808
809=head1 WARNING
810
811Because this information is not stored within the perl executable
812itself it is possible (but unlikely) that the information does not
813relate to the actual perl binary which is being used to access it.
814
815The Config module is installed into the architecture and version
816specific library directory ($Config{installarchlib}) and it checks the
817perl version number when loaded.
818
435ec615
HM
819The values stored in config.sh may be either single-quoted or
820double-quoted. Double-quoted strings are handy for those cases where you
821need to include escape sequences in the strings. To avoid runtime variable
822interpolation, any C<$> and C<@> characters are replaced by C<\$> and
823C<\@>, respectively. This isn't foolproof, of course, so don't embed C<\$>
824or C<\@> in double-quoted strings unless you're willing to deal with the
825consequences. (The slashes will end up escaped and the C<$> or C<@> will
826trigger variable interpolation)
827
ebc74a4b
GS
828=head1 GLOSSARY
829
830Most C<Config> variables are determined by the C<Configure> script
831on platforms supported by it (which is most UNIX platforms). Some
832platforms have custom-made C<Config> variables, and may thus not have
833some of the variables described below, or may have extraneous variables
834specific to that particular port. See the port specific documentation
835in such cases.
836
ebc74a4b
GS
837ENDOFTAIL
838
5435c704
NC
839if ($Opts{glossary}) {
840 open(GLOS, "<$Glossary") or die "Can't open $Glossary: $!";
18f68570 841}
b9f36698
CB
842my %seen = ();
843my $text = 0;
fb87c415
IZ
844$/ = '';
845
846sub process {
aade5aff
YST
847 if (s/\A(\w*)\s+\(([\w.]+)\):\s*\n(\t?)/=item C<$1>\n\nFrom F<$2>:\n\n/m) {
848 my $c = substr $1, 0, 1;
849 unless ($seen{$c}++) {
5435c704 850 print CONFIG_POD <<EOF if $text;
fb87c415 851=back
ebc74a4b 852
fb87c415 853EOF
5435c704 854 print CONFIG_POD <<EOF;
fb87c415
IZ
855=head2 $c
856
bbc7dcd2 857=over 4
fb87c415
IZ
858
859EOF
aade5aff
YST
860 $text = 1;
861 }
862 }
863 elsif (!$text || !/\A\t/) {
864 warn "Expected a Configure variable header",
865 ($text ? " or another paragraph of description" : () );
fb87c415
IZ
866 }
867 s/n't/n\00t/g; # leave can't, won't etc untouched
9b22980b 868 s/^\t\s+(.*)/\n$1/gm; # Indented lines ===> new paragraph
fb87c415
IZ
869 s/^(?<!\n\n)\t(.*)/$1/gm; # Not indented lines ===> text
870 s{([\'\"])(?=[^\'\"\s]*[./][^\'\"\s]*\1)([^\'\"\s]+)\1}(F<$2>)g; # '.o'
871 s{([\'\"])([^\'\"\s]+)\1}(C<$2>)g; # "date" command
872 s{\'([A-Za-z_\- *=/]+)\'}(C<$1>)g; # 'ln -s'
873 s{
874 (?<! [\w./<\'\"] ) # Only standalone file names
875 (?! e \. g \. ) # Not e.g.
876 (?! \. \. \. ) # Not ...
877 (?! \d ) # Not 5.004
a1151a3c
RGS
878 (?! read/ ) # Not read/write
879 (?! etc\. ) # Not etc.
880 (?! I/O ) # Not I/O
881 (
882 \$ ? # Allow leading $
883 [\w./]* [./] [\w./]* # Require . or / inside
884 )
885 (?<! \. (?= [\s)] ) ) # Do not include trailing dot
fb87c415
IZ
886 (?! [\w/] ) # Include all of it
887 }
888 (F<$1>)xg; # /usr/local
889 s/((?<=\s)~\w*)/F<$1>/g; # ~name
890 s/(?<![.<\'\"])\b([A-Z_]{2,})\b(?![\'\"])/C<$1>/g; # UNISTD
891 s/(?<![.<\'\"])\b(?!the\b)(\w+)\s+macro\b/C<$1> macro/g; # FILE_cnt macro
892 s/n[\0]t/n't/g; # undo can't, won't damage
ebc74a4b
GS
893}
894
5435c704 895if ($Opts{glossary}) {
7701ffb5
JH
896 <GLOS>; # Skip the "DO NOT EDIT"
897 <GLOS>; # Skip the preamble
18f68570
VK
898 while (<GLOS>) {
899 process;
5435c704 900 print CONFIG_POD;
18f68570 901 }
fb87c415 902}
ebc74a4b 903
5435c704 904print CONFIG_POD <<'ENDOFTAIL';
ebc74a4b
GS
905
906=back
907
3c81428c 908=head1 NOTE
909
910This module contains a good example of how to use tie to implement a
911cache and an example of how to make a tied variable readonly to those
912outside of it.
913
914=cut
a0d0e21e 915
9193ea20 916ENDOFTAIL
a0d0e21e 917
de7128ac 918close(GLOS) if $Opts{glossary};
5435c704 919close(CONFIG_POD);
de7128ac
NC
920print "written lib/Config.pod\n";
921
922my $orig_config_txt = "";
923my $orig_heavy_txt = "";
924{
925 local $/;
926 my $fh;
927 $orig_config_txt = <$fh> if open $fh, "<", $Config_PM;
928 $orig_heavy_txt = <$fh> if open $fh, "<", $Config_heavy;
929}
930
931if ($orig_config_txt ne $config_txt or $orig_heavy_txt ne $heavy_txt) {
932 open CONFIG, ">", $Config_PM or die "Can't open $Config_PM: $!\n";
933 open CONFIG_HEAVY, ">", $Config_heavy or die "Can't open $Config_heavy: $!\n";
934 print CONFIG $config_txt;
935 print CONFIG_HEAVY $heavy_txt;
936 close(CONFIG_HEAVY);
937 close(CONFIG);
938 print "updated $Config_PM\n";
939 print "updated $Config_heavy\n";
940}
941
a0d0e21e 942
18f68570 943# Now create Cross.pm if needed
5435c704 944if ($Opts{cross}) {
18f68570 945 open CROSS, ">lib/Cross.pm" or die "Can not open >lib/Cross.pm: $!";
47bcb90d
VK
946 my $cross = <<'EOS';
947# typical invocation:
948# perl -MCross Makefile.PL
949# perl -MCross=wince -V:cc
950package Cross;
951
952sub import {
953 my ($package,$platform) = @_;
954 unless (defined $platform) {
955 # if $platform is not specified, then use last one when
956 # 'configpm; was invoked with --cross option
957 $platform = '***replace-marker***';
958 }
959 @INC = map {/\blib\b/?(do{local $_=$_;s/\blib\b/xlib\/$platform/;$_},$_):($_)} @INC;
e2a02c1e 960 $::Cross::platform = $platform;
18f68570 961}
47bcb90d 962
18f68570
VK
9631;
964EOS
5435c704 965 $cross =~ s/\*\*\*replace-marker\*\*\*/$Opts{cross}/g;
47bcb90d 966 print CROSS $cross;
18f68570 967 close CROSS;
de7128ac 968 print "written lib/Cross.pm\n";
18f68570
VK
969}
970
a0d0e21e
LW
971# Now do some simple tests on the Config.pm file we have created
972unshift(@INC,'lib');
63fe74dd 973unshift(@INC,'xlib/symbian') if $Opts{cross};
5435c704 974require $Config_PM;
b095ab50 975require $Config_heavy;
a0d0e21e
LW
976import Config;
977
5435c704 978die "$0: $Config_PM not valid"
a02608de 979 unless $Config{'PERL_CONFIG_SH'} eq 'true';
a0d0e21e 980
5435c704 981die "$0: error processing $Config_PM"
a0d0e21e 982 if defined($Config{'an impossible name'})
a02608de 983 or $Config{'PERL_CONFIG_SH'} ne 'true' # test cache
a0d0e21e
LW
984 ;
985
5435c704 986die "$0: error processing $Config_PM"
a0d0e21e
LW
987 if eval '$Config{"cc"} = 1'
988 or eval 'delete $Config{"cc"}'
989 ;
990
991
85e6fe83 992exit 0;
b9f36698
CB
993# Popularity of various entries in %Config, based on a large build and test
994# run of code in the Fotango build system:
995__DATA__
996path_sep: 8490
997d_readlink: 7101
998d_symlink: 7101
999archlibexp: 4318
1000sitearchexp: 4305
1001sitelibexp: 4305
1002privlibexp: 4163
1003ldlibpthname: 4041
1004libpth: 2134
1005archname: 1591
1006exe_ext: 1256
1007scriptdir: 1155
1008version: 1116
1009useithreads: 1002
1010osvers: 982
1011osname: 851
1012inc_version_list: 783
1013dont_use_nlink: 779
1014intsize: 759
1015usevendorprefix: 642
1016dlsrc: 624
1017cc: 541
1018lib_ext: 520
1019so: 512
1020ld: 501
1021ccdlflags: 500
1022ldflags: 495
1023obj_ext: 495
1024cccdlflags: 493
1025lddlflags: 493
1026ar: 492
1027dlext: 492
1028libc: 492
1029ranlib: 492
1030full_ar: 491
1031vendorarchexp: 491
1032vendorlibexp: 491
1033installman1dir: 489
1034installman3dir: 489
1035installsitebin: 489
1036installsiteman1dir: 489
1037installsiteman3dir: 489
1038installvendorman1dir: 489
1039installvendorman3dir: 489
1040d_flexfnam: 474
1041eunicefix: 360
1042d_link: 347
1043installsitearch: 344
1044installscript: 341
1045installprivlib: 337
1046binexp: 336
1047installarchlib: 336
1048installprefixexp: 336
1049installsitelib: 336
1050installstyle: 336
1051installvendorarch: 336
1052installvendorbin: 336
1053installvendorlib: 336
1054man1ext: 336
1055man3ext: 336
1056sh: 336
1057siteprefixexp: 336
1058installbin: 335
1059usedl: 332
1060ccflags: 285
1061startperl: 232
1062optimize: 231
1063usemymalloc: 229
1064cpprun: 228
1065sharpbang: 228
1066perllibs: 225
1067usesfio: 224
1068usethreads: 220
1069perlpath: 218
1070extensions: 217
1071usesocks: 208
1072shellflags: 198
1073make: 191
1074d_pwage: 189
1075d_pwchange: 189
1076d_pwclass: 189
1077d_pwcomment: 189
1078d_pwexpire: 189
1079d_pwgecos: 189
1080d_pwpasswd: 189
1081d_pwquota: 189
1082gccversion: 189
1083libs: 186
1084useshrplib: 186
1085cppflags: 185
1086ptrsize: 185
1087shrpenv: 185
1088static_ext: 185
1089use5005threads: 185
1090uselargefiles: 185
1091alignbytes: 184
1092byteorder: 184
1093ccversion: 184
1094config_args: 184
1095cppminus: 184