3 # following options are recognized:
4 # --no-glossary - no glossary file inclusion, for compactness
5 # --cross=PALTFORM - crosscompiling for PLATFORM
7 # %known_opts enumerates allowed opts as well as specifies default and initial values
13 my %specified_opts = (
14 (map {/^--([\-_\w]+)=(.*)$/} @ARGV), # --opt=smth
15 (map {/^no-?(.*)$/i?($1=>0):($_=>1)} map {/^--([\-_\w]+)$/} @ARGV), # --opt --no-opt --noopt
18 die "option '$_' is not recognized" for grep {!exists $known_opts{$_}} keys %specified_opts;
19 @ARGV = grep {!/^--/} @ARGV;
22 my $glossary = $ARGV[1] || 'Porting/Glossary';
25 # creating cross-platform config file
27 mkdir "xlib/$opts{cross}";
28 $config_pm = $ARGV[0] || "xlib/$opts{cross}/Config.pm";
31 $config_pm = $ARGV[0] || 'lib/Config.pm';
34 @ARGV = "./config.sh";
36 # list names to put first (and hence lookup fastest)
37 @fast = qw(archname osname osvers prefix libs libpth
38 dynamic_ext static_ext extensions dlsrc so
39 sig_name sig_num cc ccflags cppflags
40 privlibexp archlibexp installprivlib installarchlib
41 sharpbang startsh shsharp
44 # names of things which may need to have slashes changed to double-colons
45 @extensions = qw(dynamic_ext static_ext extensions known_extensions);
48 open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n";
49 $myver = sprintf "v%vd", $^V;
51 print CONFIG <<'ENDOFBEG_NOQ', <<"ENDOFBEG";
54 @EXPORT = qw(%Config);
55 @EXPORT_OK = qw(myconfig config_sh config_vars);
57 # Define our own import method to avoid pulling in the full Exporter:
60 @_ = @EXPORT unless @_;
61 my @func = grep {$_ ne '%Config'} @_;
62 local $Exporter::ExportLevel = 1;
63 Exporter::import('Config', @func) if @func;
64 return if @func == @_;
65 my $callpkg = caller(0);
66 *{"$callpkg\::Config"} = \%Config;
70 die "Perl lib version ($myver) doesn't match executable version (\$])"
74 or die "Perl lib version ($myver) doesn't match executable version (" .
75 (sprintf "v%vd",\$^V) . ")";
77 # This file was created by configpm when Perl was built. Any changes
78 # made to this file will be lost the next time perl is built.
84 @extensions{@extensions} = @extensions;
91 next if m:^#!/bin/sh:;
92 # Catch PERL_CONFIG_SH=true and PERL_VERSION=n line from Configure.
93 s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
95 # grandfather PATCHLEVEL and SUBVERSION and CONFIG
97 if ($k eq 'PERL_VERSION') {
98 push @v_others, "PATCHLEVEL='$v'\n";
100 elsif ($k eq 'PERL_SUBVERSION') {
101 push @v_others, "SUBVERSION='$v'\n";
103 elsif ($k eq 'PERL_CONFIG_SH') {
104 push @v_others, "CONFIG='$v'\n";
107 # We can delimit things in config.sh with either ' or ".
108 unless ($in_v or m/^(\w+)=(['"])(.*\n)/){
109 push(@non_v, "#$_"); # not a name='value' line
113 if ($in_v) { $val .= $_; }
114 else { ($name,$val) = ($1,$3); }
115 $in_v = $val !~ /$quote\n/;
117 if ($extensions{$name}) { s,/,::,g }
118 if (!$fast{$name}){ push(@v_others, "$name=$quote$val"); next; }
119 push(@v_fast,"$name=$quote$val");
122 foreach(@non_v){ print CONFIG $_ }
125 "my \$config_sh = <<'!END!';\n",
126 join("", @v_fast, sort @v_others),
129 # copy config summary format from the myconfig.SH script
131 print CONFIG "my \$summary = <<'!END!';\n";
133 open(MYCONFIG,"<myconfig.SH") || die "open myconfig.SH failed: $!";
134 1 while defined($_ = <MYCONFIG>) && !/^Summary of/;
135 do { print CONFIG $_ } until !defined($_ = <MYCONFIG>) || /^\s*$/;
138 print CONFIG "\n!END!\n", <<'EOT';
139 my $summary_expanded = 0;
142 return $summary if $summary_expanded;
143 $summary =~ s{\$(\w+)}
144 { my $c = $Config{$1}; defined($c) ? $c : 'undef' }ge;
145 $summary_expanded = 1;
152 print CONFIG <<'ENDOFEND';
155 # check for cached value (which may be undef so we use exists not defined)
156 return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
158 # Search for it in the big string
159 my($value, $start, $marker, $quote_type);
163 if ($_[1] eq 'byteorder') {
164 # byteorder does exist on its own but we overlay a virtual
165 # dynamically recomputed value.
166 my $t = $Config{ivtype};
167 my $s = $Config{ivsize};
168 my $f = $t eq 'long' ? 'L!' : $s == 8 ? 'Q': 'I';
169 if ($s == 4 || $s == 8) {
171 foreach my $c (reverse(2..$s)) { $i |= ord($c); $i <<= 8 }
173 $value = join('', unpack('a'x$s, pack($f, $i)));
177 } elsif ($_[1] =~ /^((?:cc|ld)flags|libs(?:wanted)?)_nolargefiles/) {
178 # These are purely virtual, they do not exist, but need to
179 # be computed on demand for largefile-incapable extensions.
180 my $key = "${1}_uselargefiles";
181 $value = $Config{$1};
182 my $withlargefiles = $Config{$key};
183 if ($key =~ /^(?:cc|ld)flags_/) {
184 $value =~ s/\Q$withlargefiles\E\b//;
185 } elsif ($key =~ /^libs/) {
186 my @lflibswanted = split(' ', $Config{libswanted_uselargefiles});
189 @lflibswanted{@lflibswanted} = ();
190 if ($key =~ /^libs_/) {
191 my @libs = grep { /^-l(.+)/ &&
192 not exists $lflibswanted{$1} }
193 split(' ', $Config{libs});
194 $Config{libs} = join(' ', @libs);
195 } elsif ($key =~ /^libswanted_/) {
196 my @libswanted = grep { not exists $lflibswanted{$_} }
197 split(' ', $Config{libswanted});
198 $Config{libswanted} = join(' ', @libswanted);
204 # return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
205 # Check for the common case, ' delimeted
206 $start = index($config_sh, "\n$marker$quote_type");
207 # If that failed, check for " delimited
210 $start = index($config_sh, "\n$marker$quote_type");
212 return undef if ( ($start == -1) && # in case it's first
213 (substr($config_sh, 0, length($marker)) ne $marker) );
215 # It's the very first thing we found. Skip $start forward
216 # and figure out the quote mark after the =.
217 $start = length($marker) + 1;
218 $quote_type = substr($config_sh, $start - 1, 1);
221 $start += length($marker) + 2;
223 $value = substr($config_sh, $start,
224 index($config_sh, "$quote_type\n", $start) - $start);
226 # If we had a double-quote, we'd better eval it so escape
227 # sequences and such can be interpolated. Since the incoming
228 # value is supposed to follow shell rules and not perl rules,
229 # we escape any perl variable markers
230 if ($quote_type eq '"') {
231 $value =~ s/\$/\\\$/g;
232 $value =~ s/\@/\\\@/g;
233 eval "\$value = \"$value\"";
235 #$value = sprintf($value) if $quote_type eq '"';
236 # So we can say "if $Config{'foo'}".
237 $value = undef if $value eq 'undef';
238 $_[0]->{$_[1]} = $value; # cache it
246 # my($key) = $config_sh =~ m/^(.*?)=/;
247 substr($config_sh, 0, index($config_sh, '=') );
252 # Find out how the current key's quoted so we can skip to its end.
253 my $quote = substr($config_sh, index($config_sh, "=", $prevpos)+1, 1);
254 my $pos = index($config_sh, qq($quote\n), $prevpos) + 2;
255 my $len = index($config_sh, "=", $pos) - $pos;
257 $len > 0 ? substr($config_sh, $pos, $len) : undef;
261 # exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
262 exists($_[0]->{$_[1]}) or
263 index($config_sh, "\n$_[1]='") != -1 or
264 substr($config_sh, 0, length($_[1])+2) eq "$_[1]='" or
265 index($config_sh, "\n$_[1]=\"") != -1 or
266 substr($config_sh, 0, length($_[1])+2) eq "$_[1]=\"" or
267 $_[1] =~ /^(?:(?:cc|ld)flags|libs(?:wanted)?)_nolargefiles$/;
270 sub STORE { die "\%Config::Config is read-only\n" }
271 sub DELETE { &STORE }
281 my @matches = grep /^$re=/, split /^/, $config_sh;
282 @matches ? (print @matches) : print "$re: not found\n";
287 config_re($_), next if /\W/;
288 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
289 $v='undef' unless defined $v;
297 print CONFIG <<'ENDOFSET';
300 my ($value, $v) = $config_sh =~ m/^used_aout='(.*)'\s*$/m;
301 for (split ' ', $value) {
302 ($v) = $config_sh =~ m/^aout_$_='(.*)'\s*$/m;
303 $preconfig{$_} = $v eq 'undef' ? undef : $v;
306 $preconfig{d_fork} = undef unless $OS2::can_fork; # Some funny cases can't
307 sub TIEHASH { bless {%preconfig} }
309 # Extract the name of the DLL from the makefile to avoid duplication
310 my ($f) = grep -r, qw(GNUMakefile Makefile);
312 if (open my $fh, '<', $f) {
314 $dll = $1, last if /^PERL_DLL_BASE\s*=\s*(\S*)\s*$/;
317 print CONFIG <<ENDOFSET if $dll;
318 \$preconfig{dll_name} = '$dll';
321 print CONFIG <<'ENDOFSET';
322 sub TIEHASH { bless {} }
326 print CONFIG <<'ENDOFTAIL';
328 # avoid Config..Exporter..UNIVERSAL search for DESTROY then AUTOLOAD
331 tie %Config, 'Config';
338 Config - access Perl configuration information
343 if ($Config{'cc'} =~ /gcc/) {
344 print "built by gcc\n";
347 use Config qw(myconfig config_sh config_vars);
353 config_vars(qw(osname archname));
358 The Config module contains all the information that was available to
359 the C<Configure> program at Perl build time (over 900 values).
361 Shell variables from the F<config.sh> file (written by Configure) are
362 stored in the readonly-variable C<%Config>, indexed by their names.
364 Values stored in config.sh as 'undef' are returned as undefined
365 values. The perl C<exists> function can be used to check if a
366 named variable exists.
372 Returns a textual summary of the major perl configuration values.
373 See also C<-V> in L<perlrun/Switches>.
377 Returns the entire perl configuration information in the form of the
378 original config.sh shell variable assignment script.
380 =item config_vars(@names)
382 Prints to STDOUT the values of the named configuration variable. Each is
383 printed on a separate line in the form:
387 Names which are unknown are output as C<name='UNKNOWN';>.
388 See also C<-V:name> in L<perlrun/Switches>.
394 Here's a more sophisticated example of using %Config:
401 unless($Config{sig_name} && $Config{sig_num}) {
404 my @names = split ' ', $Config{sig_name};
405 @sig_num{@names} = split ' ', $Config{sig_num};
407 $sig_name[$sig_num{$_}] ||= $_;
411 print "signal #17 = $sig_name[17]\n";
412 if ($sig_num{ALRM}) {
413 print "SIGALRM is $sig_num{ALRM}\n";
418 Because this information is not stored within the perl executable
419 itself it is possible (but unlikely) that the information does not
420 relate to the actual perl binary which is being used to access it.
422 The Config module is installed into the architecture and version
423 specific library directory ($Config{installarchlib}) and it checks the
424 perl version number when loaded.
426 The values stored in config.sh may be either single-quoted or
427 double-quoted. Double-quoted strings are handy for those cases where you
428 need to include escape sequences in the strings. To avoid runtime variable
429 interpolation, any C<$> and C<@> characters are replaced by C<\$> and
430 C<\@>, respectively. This isn't foolproof, of course, so don't embed C<\$>
431 or C<\@> in double-quoted strings unless you're willing to deal with the
432 consequences. (The slashes will end up escaped and the C<$> or C<@> will
433 trigger variable interpolation)
437 Most C<Config> variables are determined by the C<Configure> script
438 on platforms supported by it (which is most UNIX platforms). Some
439 platforms have custom-made C<Config> variables, and may thus not have
440 some of the variables described below, or may have extraneous variables
441 specific to that particular port. See the port specific documentation
446 if ($opts{glossary}) {
447 open(GLOS, "<$glossary") or die "Can't open $glossary: $!";
454 if (s/\A(\w*)\s+\(([\w.]+)\):\s*\n(\t?)/=item C<$1>\n\nFrom F<$2>:\n\n/m) {
455 my $c = substr $1, 0, 1;
456 unless ($seen{$c}++) {
457 print CONFIG <<EOF if $text;
470 elsif (!$text || !/\A\t/) {
471 warn "Expected a Configure variable header",
472 ($text ? " or another paragraph of description" : () );
474 s/n't/n\00t/g; # leave can't, won't etc untouched
475 s/^\t\s+(.*)/\n$1/gm; # Indented lines ===> new paragraph
476 s/^(?<!\n\n)\t(.*)/$1/gm; # Not indented lines ===> text
477 s{([\'\"])(?=[^\'\"\s]*[./][^\'\"\s]*\1)([^\'\"\s]+)\1}(F<$2>)g; # '.o'
478 s{([\'\"])([^\'\"\s]+)\1}(C<$2>)g; # "date" command
479 s{\'([A-Za-z_\- *=/]+)\'}(C<$1>)g; # 'ln -s'
481 (?<! [\w./<\'\"] ) # Only standalone file names
482 (?! e \. g \. ) # Not e.g.
483 (?! \. \. \. ) # Not ...
485 (?! read/ ) # Not read/write
486 (?! etc\. ) # Not etc.
489 \$ ? # Allow leading $
490 [\w./]* [./] [\w./]* # Require . or / inside
492 (?<! \. (?= [\s)] ) ) # Do not include trailing dot
493 (?! [\w/] ) # Include all of it
495 (F<$1>)xg; # /usr/local
496 s/((?<=\s)~\w*)/F<$1>/g; # ~name
497 s/(?<![.<\'\"])\b([A-Z_]{2,})\b(?![\'\"])/C<$1>/g; # UNISTD
498 s/(?<![.<\'\"])\b(?!the\b)(\w+)\s+macro\b/C<$1> macro/g; # FILE_cnt macro
499 s/n[\0]t/n't/g; # undo can't, won't damage
502 if ($opts{glossary}) {
503 <GLOS>; # Skip the "DO NOT EDIT"
504 <GLOS>; # Skip the preamble
511 print CONFIG <<'ENDOFTAIL';
517 This module contains a good example of how to use tie to implement a
518 cache and an example of how to make a tied variable readonly to those
528 # Now create Cross.pm if needed
530 open CROSS, ">lib/Cross.pm" or die "Can not open >lib/Cross.pm: $!";
532 # typical invocation:
533 # perl -MCross Makefile.PL
534 # perl -MCross=wince -V:cc
538 my ($package,$platform) = @_;
539 unless (defined $platform) {
540 # if $platform is not specified, then use last one when
541 # 'configpm; was invoked with --cross option
542 $platform = '***replace-marker***';
544 @INC = map {/\blib\b/?(do{local $_=$_;s/\blib\b/xlib\/$platform/;$_},$_):($_)} @INC;
545 $::Cross::platform = $platform;
550 $cross =~ s/\*\*\*replace-marker\*\*\*/$opts{cross}/g;
556 # Now do some simple tests on the Config.pm file we have created
561 die "$0: $config_pm not valid"
562 unless $Config{'PERL_CONFIG_SH'} eq 'true';
564 die "$0: error processing $config_pm"
565 if defined($Config{'an impossible name'})
566 or $Config{'PERL_CONFIG_SH'} ne 'true' # test cache
569 die "$0: error processing $config_pm"
570 if eval '$Config{"cc"} = 1'
571 or eval 'delete $Config{"cc"}'