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