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