Make $Config{byteorder} more magical so that it is
[p5sagit/p5-mst-13.2.git] / configpm
CommitLineData
a0d0e21e 1#!./miniperl -w
8990e307 2
ebc74a4b 3my $config_pm = $ARGV[0] || 'lib/Config.pm';
3b5ca523 4my $glossary = $ARGV[1] || 'Porting/Glossary';
8990e307 5@ARGV = "./config.sh";
6
a0d0e21e 7# list names to put first (and hence lookup fastest)
3c81428c 8@fast = qw(archname osname osvers prefix libs libpth
9 dynamic_ext static_ext extensions dlsrc so
743c51bc 10 sig_name sig_num cc ccflags cppflags
3c81428c 11 privlibexp archlibexp installprivlib installarchlib
a0d0e21e 12 sharpbang startsh shsharp
3c81428c 13);
a0d0e21e 14
fec02dd3 15# names of things which may need to have slashes changed to double-colons
16@extensions = qw(dynamic_ext static_ext extensions known_extensions);
17
a0d0e21e 18
19open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n";
dd101d75 20$myver = sprintf "v%vd", $^V;
3c81428c 21
e3d0cac0 22print CONFIG <<'ENDOFBEG_NOQ', <<"ENDOFBEG";
8990e307 23package Config;
3c81428c 24use Exporter ();
e3d0cac0 25@EXPORT = qw(%Config);
26@EXPORT_OK = qw(myconfig config_sh config_vars);
27
28# Define our own import method to avoid pulling in the full Exporter:
29sub import {
30 my $pkg = shift;
31 @_ = @EXPORT unless @_;
32 my @func = grep {$_ ne '%Config'} @_;
4365a961 33 local $Exporter::ExportLevel = 1;
e3d0cac0 34 Exporter::import('Config', @func) if @func;
35 return if @func == @_;
36 my $callpkg = caller(0);
37 *{"$callpkg\::Config"} = \%Config;
38}
39
40ENDOFBEG_NOQ
de98c553 41die "Perl lib version ($myver) doesn't match executable version (\$])"
42 unless \$^V;
43
dd101d75 44\$^V eq $myver
45 or die "Perl lib version ($myver) doesn't match executable version (" .
46 (sprintf "v%vd",\$^V) . ")";
8990e307 47
a0d0e21e 48# This file was created by configpm when Perl was built. Any changes
49# made to this file will be lost the next time perl is built.
50
8990e307 51ENDOFBEG
52
16d20bd9 53
a0d0e21e 54@fast{@fast} = @fast;
fec02dd3 55@extensions{@extensions} = @extensions;
a0d0e21e 56@non_v=();
57@v_fast=();
58@v_others=();
44a8e56a 59$in_v = 0;
a0d0e21e 60
85e6fe83 61while (<>) {
a0d0e21e 62 next if m:^#!/bin/sh:;
2000072c 63 # Catch CONFIGDOTSH=true and PERL_VERSION=n line from Configure.
a0d0e21e 64 s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
cceca5ed 65 my ($k,$v) = ($1,$2);
2000072c 66 # grandfather PATCHLEVEL and SUBVERSION and CONFIG
cceca5ed 67 if ($k) {
68 if ($k eq 'PERL_VERSION') {
69 push @v_others, "PATCHLEVEL='$v'\n";
70 }
71 elsif ($k eq 'PERL_SUBVERSION') {
72 push @v_others, "SUBVERSION='$v'\n";
73 }
2000072c 74 elsif ($k eq 'CONFIGDOTSH') {
75 push @v_others, "CONFIG='$v'\n";
76 }
cceca5ed 77 }
435ec615 78 # We can delimit things in config.sh with either ' or ".
79 unless ($in_v or m/^(\w+)=(['"])(.*\n)/){
a0d0e21e 80 push(@non_v, "#$_"); # not a name='value' line
81 next;
82 }
435ec615 83 $quote = $2;
44a8e56a 84 if ($in_v) { $val .= $_; }
435ec615 85 else { ($name,$val) = ($1,$3); }
86 $in_v = $val !~ /$quote\n/;
44a8e56a 87 next if $in_v;
fec02dd3 88 if ($extensions{$name}) { s,/,::,g }
435ec615 89 if (!$fast{$name}){ push(@v_others, "$name=$quote$val"); next; }
90 push(@v_fast,"$name=$quote$val");
a0d0e21e 91}
92
93foreach(@non_v){ print CONFIG $_ }
94
95print CONFIG "\n",
3c81428c 96 "my \$config_sh = <<'!END!';\n",
a0d0e21e 97 join("", @v_fast, sort @v_others),
3c81428c 98 "!END!\n\n";
99
a6c40364 100# copy config summary format from the myconfig.SH script
3c81428c 101
102print CONFIG "my \$summary = <<'!END!';\n";
103
3b5ca523 104open(MYCONFIG,"<myconfig.SH") || die "open myconfig.SH failed: $!";
54310121 1051 while defined($_ = <MYCONFIG>) && !/^Summary of/;
106do { print CONFIG $_ } until !defined($_ = <MYCONFIG>) || /^\s*$/;
3c81428c 107close(MYCONFIG);
a0d0e21e 108
3c81428c 109print CONFIG "\n!END!\n", <<'EOT';
110my $summary_expanded = 0;
111
112sub myconfig {
113 return $summary if $summary_expanded;
ca8cad5c 114 $summary =~ s{\$(\w+)}
115 { my $c = $Config{$1}; defined($c) ? $c : 'undef' }ge;
3c81428c 116 $summary_expanded = 1;
117 $summary;
118}
119EOT
120
121# ----
a0d0e21e 122
123print CONFIG <<'ENDOFEND';
124
a0d0e21e 125sub FETCH {
aa1bdcb8 126 # check for cached value (which may be undef so we use exists not defined)
a0d0e21e 127 return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
aa1bdcb8 128
129 # Search for it in the big string
435ec615 130 my($value, $start, $marker, $quote_type);
46f36567 131
435ec615 132 $quote_type = "'";
46f36567 133 if ($_[1] eq 'byteorder') {
134 my $t = $Config{ivtype};
135 my $s = $Config{ivsize};
136 my $f = $t eq 'long' ? 'L!' : $s == 8 ? 'Q': 'I';
137 my $i = unpack($f, pack('C*', map { ord() } 1..$s));
138 if ($s == 4 || $s == 8) {
139 $value = join('', unpack('a'x$s, pack($f, $i)));
140 } else {
141 $value = '?'x$s;
142 }
143 } else {
144 $marker = "$_[1]=";
145 # return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
146 # Check for the common case, ' delimeted
147 $start = index($config_sh, "\n$marker$quote_type");
148 # If that failed, check for " delimited
149 if ($start == -1) {
150 $quote_type = '"';
151 $start = index($config_sh, "\n$marker$quote_type");
152 }
153 return undef if ( ($start == -1) && # in case it's first
154 (substr($config_sh, 0, length($marker)) ne $marker) );
155 if ($start == -1) {
156 # It's the very first thing we found. Skip $start forward
157 # and figure out the quote mark after the =.
158 $start = length($marker) + 1;
159 $quote_type = substr($config_sh, $start - 1, 1);
160 }
161 else {
162 $start += length($marker) + 2;
163 }
164 $value = substr($config_sh, $start,
165 index($config_sh, "$quote_type\n", $start) - $start);
435ec615 166 }
435ec615 167 # If we had a double-quote, we'd better eval it so escape
168 # sequences and such can be interpolated. Since the incoming
169 # value is supposed to follow shell rules and not perl rules,
170 # we escape any perl variable markers
171 if ($quote_type eq '"') {
46f36567 172 $value =~ s/\$/\\\$/g;
173 $value =~ s/\@/\\\@/g;
174 eval "\$value = \"$value\"";
435ec615 175 }
176 #$value = sprintf($value) if $quote_type eq '"';
46f36567 177 # So we can say "if $Config{'foo'}".
178 $value = undef if $value eq 'undef';
a0d0e21e 179 $_[0]->{$_[1]} = $value; # cache it
180 return $value;
181}
182
3c81428c 183my $prevpos = 0;
184
a0d0e21e 185sub FIRSTKEY {
186 $prevpos = 0;
aa1bdcb8 187 # my($key) = $config_sh =~ m/^(.*?)=/;
188 substr($config_sh, 0, index($config_sh, '=') );
189 # $key;
a0d0e21e 190}
191
192sub NEXTKEY {
435ec615 193 # Find out how the current key's quoted so we can skip to its end.
194 my $quote = substr($config_sh, index($config_sh, "=", $prevpos)+1, 1);
195 my $pos = index($config_sh, qq($quote\n), $prevpos) + 2;
3c81428c 196 my $len = index($config_sh, "=", $pos) - $pos;
a0d0e21e 197 $prevpos = $pos;
3c81428c 198 $len > 0 ? substr($config_sh, $pos, $len) : undef;
85e6fe83 199}
a0d0e21e 200
3c81428c 201sub EXISTS {
aa1bdcb8 202 # exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
203 exists($_[0]->{$_[1]}) or
204 index($config_sh, "\n$_[1]='") != -1 or
435ec615 205 substr($config_sh, 0, length($_[1])+2) eq "$_[1]='" or
206 index($config_sh, "\n$_[1]=\"") != -1 or
207 substr($config_sh, 0, length($_[1])+2) eq "$_[1]=\"";
a0d0e21e 208}
209
3c81428c 210sub STORE { die "\%Config::Config is read-only\n" }
211sub DELETE { &STORE }
212sub CLEAR { &STORE }
a0d0e21e 213
3c81428c 214
215sub config_sh {
216 $config_sh
748a9306 217}
9193ea20 218
219sub config_re {
220 my $re = shift;
221 my @matches = ($config_sh =~ /^$re=.*\n/mg);
222 @matches ? (print @matches) : print "$re: not found\n";
223}
224
3c81428c 225sub config_vars {
226 foreach(@_){
9193ea20 227 config_re($_), next if /\W/;
3c81428c 228 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
229 $v='undef' unless defined $v;
230 print "$_='$v';\n";
231 }
232}
233
9193ea20 234ENDOFEND
235
236if ($^O eq 'os2') {
237 print CONFIG <<'ENDOFSET';
238my %preconfig;
239if ($OS2::is_aout) {
240 my ($value, $v) = $config_sh =~ m/^used_aout='(.*)'\s*$/m;
241 for (split ' ', $value) {
242 ($v) = $config_sh =~ m/^aout_$_='(.*)'\s*$/m;
243 $preconfig{$_} = $v eq 'undef' ? undef : $v;
244 }
245}
246sub TIEHASH { bless {%preconfig} }
247ENDOFSET
248} else {
249 print CONFIG <<'ENDOFSET';
250sub TIEHASH { bless {} }
251ENDOFSET
252}
253
254print CONFIG <<'ENDOFTAIL';
255
fb73857a 256# avoid Config..Exporter..UNIVERSAL search for DESTROY then AUTOLOAD
257sub DESTROY { }
258
9193ea20 259tie %Config, 'Config';
260
3c81428c 2611;
262__END__
748a9306 263
3c81428c 264=head1 NAME
a0d0e21e 265
3c81428c 266Config - access Perl configuration information
267
268=head1 SYNOPSIS
269
270 use Config;
271 if ($Config{'cc'} =~ /gcc/) {
272 print "built by gcc\n";
273 }
274
275 use Config qw(myconfig config_sh config_vars);
276
277 print myconfig();
278
279 print config_sh();
280
281 config_vars(qw(osname archname));
282
283
284=head1 DESCRIPTION
285
286The Config module contains all the information that was available to
287the C<Configure> program at Perl build time (over 900 values).
288
289Shell variables from the F<config.sh> file (written by Configure) are
290stored in the readonly-variable C<%Config>, indexed by their names.
291
292Values stored in config.sh as 'undef' are returned as undefined
1fef88e7 293values. The perl C<exists> function can be used to check if a
3c81428c 294named variable exists.
295
296=over 4
297
298=item myconfig()
299
300Returns a textual summary of the major perl configuration values.
301See also C<-V> in L<perlrun/Switches>.
302
303=item config_sh()
304
305Returns the entire perl configuration information in the form of the
306original config.sh shell variable assignment script.
307
308=item config_vars(@names)
309
310Prints to STDOUT the values of the named configuration variable. Each is
311printed on a separate line in the form:
312
313 name='value';
314
315Names which are unknown are output as C<name='UNKNOWN';>.
316See also C<-V:name> in L<perlrun/Switches>.
317
318=back
319
320=head1 EXAMPLE
321
322Here's a more sophisticated example of using %Config:
323
324 use Config;
743c51bc 325 use strict;
326
327 my %sig_num;
328 my @sig_name;
329 unless($Config{sig_name} && $Config{sig_num}) {
330 die "No sigs?";
331 } else {
332 my @names = split ' ', $Config{sig_name};
333 @sig_num{@names} = split ' ', $Config{sig_num};
334 foreach (@names) {
335 $sig_name[$sig_num{$_}] ||= $_;
336 }
337 }
3c81428c 338
743c51bc 339 print "signal #17 = $sig_name[17]\n";
340 if ($sig_num{ALRM}) {
341 print "SIGALRM is $sig_num{ALRM}\n";
3c81428c 342 }
343
344=head1 WARNING
345
346Because this information is not stored within the perl executable
347itself it is possible (but unlikely) that the information does not
348relate to the actual perl binary which is being used to access it.
349
350The Config module is installed into the architecture and version
351specific library directory ($Config{installarchlib}) and it checks the
352perl version number when loaded.
353
435ec615 354The values stored in config.sh may be either single-quoted or
355double-quoted. Double-quoted strings are handy for those cases where you
356need to include escape sequences in the strings. To avoid runtime variable
357interpolation, any C<$> and C<@> characters are replaced by C<\$> and
358C<\@>, respectively. This isn't foolproof, of course, so don't embed C<\$>
359or C<\@> in double-quoted strings unless you're willing to deal with the
360consequences. (The slashes will end up escaped and the C<$> or C<@> will
361trigger variable interpolation)
362
ebc74a4b 363=head1 GLOSSARY
364
365Most C<Config> variables are determined by the C<Configure> script
366on platforms supported by it (which is most UNIX platforms). Some
367platforms have custom-made C<Config> variables, and may thus not have
368some of the variables described below, or may have extraneous variables
369specific to that particular port. See the port specific documentation
370in such cases.
371
ebc74a4b 372ENDOFTAIL
373
374open(GLOS, "<$glossary") or die "Can't open $glossary: $!";
fb87c415 375%seen = ();
376$text = 0;
377$/ = '';
378
379sub process {
380 s/\A(\w*)\s+\(([\w.]+)\):\s*\n(\t?)/=item C<$1>\n\nFrom F<$2>:\n\n/m;
381 my $c = substr $1, 0, 1;
382 unless ($seen{$c}++) {
383 print CONFIG <<EOF if $text;
384=back
ebc74a4b 385
fb87c415 386EOF
387 print CONFIG <<EOF;
388=head2 $c
389
390=over
391
392EOF
393 $text = 1;
394 }
395 s/n't/n\00t/g; # leave can't, won't etc untouched
396 s/^\t\s+(.*)/\n\t$1\n/gm; # Indented lines ===> paragraphs
397 s/^(?<!\n\n)\t(.*)/$1/gm; # Not indented lines ===> text
398 s{([\'\"])(?=[^\'\"\s]*[./][^\'\"\s]*\1)([^\'\"\s]+)\1}(F<$2>)g; # '.o'
399 s{([\'\"])([^\'\"\s]+)\1}(C<$2>)g; # "date" command
400 s{\'([A-Za-z_\- *=/]+)\'}(C<$1>)g; # 'ln -s'
401 s{
402 (?<! [\w./<\'\"] ) # Only standalone file names
403 (?! e \. g \. ) # Not e.g.
404 (?! \. \. \. ) # Not ...
405 (?! \d ) # Not 5.004
406 ( [\w./]* [./] [\w./]* ) # Require . or / inside
407 (?<! \. (?= \s ) ) # Do not include trailing dot
408 (?! [\w/] ) # Include all of it
409 }
410 (F<$1>)xg; # /usr/local
411 s/((?<=\s)~\w*)/F<$1>/g; # ~name
412 s/(?<![.<\'\"])\b([A-Z_]{2,})\b(?![\'\"])/C<$1>/g; # UNISTD
413 s/(?<![.<\'\"])\b(?!the\b)(\w+)\s+macro\b/C<$1> macro/g; # FILE_cnt macro
414 s/n[\0]t/n't/g; # undo can't, won't damage
ebc74a4b 415}
416
fb87c415 417<GLOS>; # Skip the preamble
418while (<GLOS>) {
419 process;
420 print CONFIG;
421}
ebc74a4b 422
423print CONFIG <<'ENDOFTAIL';
424
425=back
426
3c81428c 427=head1 NOTE
428
429This module contains a good example of how to use tie to implement a
430cache and an example of how to make a tied variable readonly to those
431outside of it.
432
433=cut
a0d0e21e 434
9193ea20 435ENDOFTAIL
a0d0e21e 436
437close(CONFIG);
ebc74a4b 438close(GLOS);
a0d0e21e 439
440# Now do some simple tests on the Config.pm file we have created
441unshift(@INC,'lib');
442require $config_pm;
443import Config;
444
445die "$0: $config_pm not valid"
2000072c 446 unless $Config{'CONFIGDOTSH'} eq 'true';
a0d0e21e 447
448die "$0: error processing $config_pm"
449 if defined($Config{'an impossible name'})
2000072c 450 or $Config{'CONFIGDOTSH'} ne 'true' # test cache
a0d0e21e 451 ;
452
453die "$0: error processing $config_pm"
454 if eval '$Config{"cc"} = 1'
455 or eval 'delete $Config{"cc"}'
456 ;
457
458
85e6fe83 459exit 0;