This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Rename the macro argument because some preprocessors
[perl5.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
LW
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
AD
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
LW
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
IZ
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
IZ
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
GS
41die "Perl lib version ($myver) doesn't match executable version (\$])"
42 unless \$^V;
43
dd101d75
JH
44\$^V eq $myver
45 or die "Perl lib version ($myver) doesn't match executable version (" .
46 (sprintf "v%vd",\$^V) . ")";
8990e307 47
a0d0e21e
LW
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
LW
51ENDOFBEG
52
16d20bd9 53
a0d0e21e 54@fast{@fast} = @fast;
fec02dd3 55@extensions{@extensions} = @extensions;
a0d0e21e
LW
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
GS
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
JH
74 elsif ($k eq 'CONFIGDOTSH') {
75 push @v_others, "CONFIG='$v'\n";
76 }
cceca5ed 77 }
435ec615
HM
78 # We can delimit things in config.sh with either ' or ".
79 unless ($in_v or m/^(\w+)=(['"])(.*\n)/){
a0d0e21e
LW
80 push(@non_v, "#$_"); # not a name='value' line
81 next;
82 }
435ec615 83 $quote = $2;
44a8e56a 84 if ($in_v) { $val .= $_; }
435ec615
HM
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
HM
89 if (!$fast{$name}){ push(@v_others, "$name=$quote$val"); next; }
90 push(@v_fast,"$name=$quote$val");
a0d0e21e
LW
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
TB
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
LW
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
TP
128
129 # Search for it in the big string
435ec615 130 my($value, $start, $marker, $quote_type);
46f36567 131
435ec615 132 $quote_type = "'";
46f36567
W
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';
46f36567 137 if ($s == 4 || $s == 8) {
ad66e0eb
JH
138 my $i = 0;
139 foreach my $c (reverse(2..$s)) { $i |= ord($c); $i <<= 8 }
140 $i |= ord(1);
46f36567
W
141 $value = join('', unpack('a'x$s, pack($f, $i)));
142 } else {
143 $value = '?'x$s;
144 }
145 } else {
146 $marker = "$_[1]=";
147 # return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
148 # Check for the common case, ' delimeted
149 $start = index($config_sh, "\n$marker$quote_type");
150 # If that failed, check for " delimited
151 if ($start == -1) {
152 $quote_type = '"';
153 $start = index($config_sh, "\n$marker$quote_type");
154 }
155 return undef if ( ($start == -1) && # in case it's first
156 (substr($config_sh, 0, length($marker)) ne $marker) );
157 if ($start == -1) {
158 # It's the very first thing we found. Skip $start forward
159 # and figure out the quote mark after the =.
160 $start = length($marker) + 1;
161 $quote_type = substr($config_sh, $start - 1, 1);
162 }
163 else {
164 $start += length($marker) + 2;
165 }
166 $value = substr($config_sh, $start,
167 index($config_sh, "$quote_type\n", $start) - $start);
435ec615 168 }
435ec615
HM
169 # If we had a double-quote, we'd better eval it so escape
170 # sequences and such can be interpolated. Since the incoming
171 # value is supposed to follow shell rules and not perl rules,
172 # we escape any perl variable markers
173 if ($quote_type eq '"') {
46f36567
W
174 $value =~ s/\$/\\\$/g;
175 $value =~ s/\@/\\\@/g;
176 eval "\$value = \"$value\"";
435ec615
HM
177 }
178 #$value = sprintf($value) if $quote_type eq '"';
46f36567
W
179 # So we can say "if $Config{'foo'}".
180 $value = undef if $value eq 'undef';
a0d0e21e
LW
181 $_[0]->{$_[1]} = $value; # cache it
182 return $value;
183}
184
3c81428c 185my $prevpos = 0;
186
a0d0e21e
LW
187sub FIRSTKEY {
188 $prevpos = 0;
aa1bdcb8
TP
189 # my($key) = $config_sh =~ m/^(.*?)=/;
190 substr($config_sh, 0, index($config_sh, '=') );
191 # $key;
a0d0e21e
LW
192}
193
194sub NEXTKEY {
435ec615
HM
195 # Find out how the current key's quoted so we can skip to its end.
196 my $quote = substr($config_sh, index($config_sh, "=", $prevpos)+1, 1);
197 my $pos = index($config_sh, qq($quote\n), $prevpos) + 2;
3c81428c 198 my $len = index($config_sh, "=", $pos) - $pos;
a0d0e21e 199 $prevpos = $pos;
3c81428c 200 $len > 0 ? substr($config_sh, $pos, $len) : undef;
85e6fe83 201}
a0d0e21e 202
3c81428c 203sub EXISTS {
aa1bdcb8
TP
204 # exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
205 exists($_[0]->{$_[1]}) or
206 index($config_sh, "\n$_[1]='") != -1 or
435ec615
HM
207 substr($config_sh, 0, length($_[1])+2) eq "$_[1]='" or
208 index($config_sh, "\n$_[1]=\"") != -1 or
209 substr($config_sh, 0, length($_[1])+2) eq "$_[1]=\"";
a0d0e21e
LW
210}
211
3c81428c 212sub STORE { die "\%Config::Config is read-only\n" }
213sub DELETE { &STORE }
214sub CLEAR { &STORE }
a0d0e21e 215
3c81428c 216
217sub config_sh {
218 $config_sh
748a9306 219}
9193ea20 220
221sub config_re {
222 my $re = shift;
223 my @matches = ($config_sh =~ /^$re=.*\n/mg);
224 @matches ? (print @matches) : print "$re: not found\n";
225}
226
3c81428c 227sub config_vars {
228 foreach(@_){
9193ea20 229 config_re($_), next if /\W/;
3c81428c 230 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
231 $v='undef' unless defined $v;
232 print "$_='$v';\n";
233 }
234}
235
9193ea20 236ENDOFEND
237
238if ($^O eq 'os2') {
239 print CONFIG <<'ENDOFSET';
240my %preconfig;
241if ($OS2::is_aout) {
242 my ($value, $v) = $config_sh =~ m/^used_aout='(.*)'\s*$/m;
243 for (split ' ', $value) {
244 ($v) = $config_sh =~ m/^aout_$_='(.*)'\s*$/m;
245 $preconfig{$_} = $v eq 'undef' ? undef : $v;
246 }
247}
248sub TIEHASH { bless {%preconfig} }
249ENDOFSET
250} else {
251 print CONFIG <<'ENDOFSET';
252sub TIEHASH { bless {} }
253ENDOFSET
254}
255
256print CONFIG <<'ENDOFTAIL';
257
fb73857a 258# avoid Config..Exporter..UNIVERSAL search for DESTROY then AUTOLOAD
259sub DESTROY { }
260
9193ea20 261tie %Config, 'Config';
262
3c81428c 2631;
264__END__
748a9306 265
3c81428c 266=head1 NAME
a0d0e21e 267
3c81428c 268Config - access Perl configuration information
269
270=head1 SYNOPSIS
271
272 use Config;
273 if ($Config{'cc'} =~ /gcc/) {
274 print "built by gcc\n";
275 }
276
277 use Config qw(myconfig config_sh config_vars);
278
279 print myconfig();
280
281 print config_sh();
282
283 config_vars(qw(osname archname));
284
285
286=head1 DESCRIPTION
287
288The Config module contains all the information that was available to
289the C<Configure> program at Perl build time (over 900 values).
290
291Shell variables from the F<config.sh> file (written by Configure) are
292stored in the readonly-variable C<%Config>, indexed by their names.
293
294Values stored in config.sh as 'undef' are returned as undefined
1fef88e7 295values. The perl C<exists> function can be used to check if a
3c81428c 296named variable exists.
297
298=over 4
299
300=item myconfig()
301
302Returns a textual summary of the major perl configuration values.
303See also C<-V> in L<perlrun/Switches>.
304
305=item config_sh()
306
307Returns the entire perl configuration information in the form of the
308original config.sh shell variable assignment script.
309
310=item config_vars(@names)
311
312Prints to STDOUT the values of the named configuration variable. Each is
313printed on a separate line in the form:
314
315 name='value';
316
317Names which are unknown are output as C<name='UNKNOWN';>.
318See also C<-V:name> in L<perlrun/Switches>.
319
320=back
321
322=head1 EXAMPLE
323
324Here's a more sophisticated example of using %Config:
325
326 use Config;
743c51bc
W
327 use strict;
328
329 my %sig_num;
330 my @sig_name;
331 unless($Config{sig_name} && $Config{sig_num}) {
332 die "No sigs?";
333 } else {
334 my @names = split ' ', $Config{sig_name};
335 @sig_num{@names} = split ' ', $Config{sig_num};
336 foreach (@names) {
337 $sig_name[$sig_num{$_}] ||= $_;
338 }
339 }
3c81428c 340
743c51bc
W
341 print "signal #17 = $sig_name[17]\n";
342 if ($sig_num{ALRM}) {
343 print "SIGALRM is $sig_num{ALRM}\n";
3c81428c 344 }
345
346=head1 WARNING
347
348Because this information is not stored within the perl executable
349itself it is possible (but unlikely) that the information does not
350relate to the actual perl binary which is being used to access it.
351
352The Config module is installed into the architecture and version
353specific library directory ($Config{installarchlib}) and it checks the
354perl version number when loaded.
355
435ec615
HM
356The values stored in config.sh may be either single-quoted or
357double-quoted. Double-quoted strings are handy for those cases where you
358need to include escape sequences in the strings. To avoid runtime variable
359interpolation, any C<$> and C<@> characters are replaced by C<\$> and
360C<\@>, respectively. This isn't foolproof, of course, so don't embed C<\$>
361or C<\@> in double-quoted strings unless you're willing to deal with the
362consequences. (The slashes will end up escaped and the C<$> or C<@> will
363trigger variable interpolation)
364
ebc74a4b
GS
365=head1 GLOSSARY
366
367Most C<Config> variables are determined by the C<Configure> script
368on platforms supported by it (which is most UNIX platforms). Some
369platforms have custom-made C<Config> variables, and may thus not have
370some of the variables described below, or may have extraneous variables
371specific to that particular port. See the port specific documentation
372in such cases.
373
ebc74a4b
GS
374ENDOFTAIL
375
376open(GLOS, "<$glossary") or die "Can't open $glossary: $!";
fb87c415
IZ
377%seen = ();
378$text = 0;
379$/ = '';
380
381sub process {
382 s/\A(\w*)\s+\(([\w.]+)\):\s*\n(\t?)/=item C<$1>\n\nFrom F<$2>:\n\n/m;
383 my $c = substr $1, 0, 1;
384 unless ($seen{$c}++) {
385 print CONFIG <<EOF if $text;
386=back
ebc74a4b 387
fb87c415
IZ
388EOF
389 print CONFIG <<EOF;
390=head2 $c
391
392=over
393
394EOF
395 $text = 1;
396 }
397 s/n't/n\00t/g; # leave can't, won't etc untouched
398 s/^\t\s+(.*)/\n\t$1\n/gm; # Indented lines ===> paragraphs
399 s/^(?<!\n\n)\t(.*)/$1/gm; # Not indented lines ===> text
400 s{([\'\"])(?=[^\'\"\s]*[./][^\'\"\s]*\1)([^\'\"\s]+)\1}(F<$2>)g; # '.o'
401 s{([\'\"])([^\'\"\s]+)\1}(C<$2>)g; # "date" command
402 s{\'([A-Za-z_\- *=/]+)\'}(C<$1>)g; # 'ln -s'
403 s{
404 (?<! [\w./<\'\"] ) # Only standalone file names
405 (?! e \. g \. ) # Not e.g.
406 (?! \. \. \. ) # Not ...
407 (?! \d ) # Not 5.004
408 ( [\w./]* [./] [\w./]* ) # Require . or / inside
409 (?<! \. (?= \s ) ) # Do not include trailing dot
410 (?! [\w/] ) # Include all of it
411 }
412 (F<$1>)xg; # /usr/local
413 s/((?<=\s)~\w*)/F<$1>/g; # ~name
414 s/(?<![.<\'\"])\b([A-Z_]{2,})\b(?![\'\"])/C<$1>/g; # UNISTD
415 s/(?<![.<\'\"])\b(?!the\b)(\w+)\s+macro\b/C<$1> macro/g; # FILE_cnt macro
416 s/n[\0]t/n't/g; # undo can't, won't damage
ebc74a4b
GS
417}
418
fb87c415
IZ
419<GLOS>; # Skip the preamble
420while (<GLOS>) {
421 process;
422 print CONFIG;
423}
ebc74a4b
GS
424
425print CONFIG <<'ENDOFTAIL';
426
427=back
428
3c81428c 429=head1 NOTE
430
431This module contains a good example of how to use tie to implement a
432cache and an example of how to make a tied variable readonly to those
433outside of it.
434
435=cut
a0d0e21e 436
9193ea20 437ENDOFTAIL
a0d0e21e
LW
438
439close(CONFIG);
ebc74a4b 440close(GLOS);
a0d0e21e
LW
441
442# Now do some simple tests on the Config.pm file we have created
443unshift(@INC,'lib');
444require $config_pm;
445import Config;
446
447die "$0: $config_pm not valid"
2000072c 448 unless $Config{'CONFIGDOTSH'} eq 'true';
a0d0e21e
LW
449
450die "$0: error processing $config_pm"
451 if defined($Config{'an impossible name'})
2000072c 452 or $Config{'CONFIGDOTSH'} ne 'true' # test cache
a0d0e21e
LW
453 ;
454
455die "$0: error processing $config_pm"
456 if eval '$Config{"cc"} = 1'
457 or eval 'delete $Config{"cc"}'
458 ;
459
460
85e6fe83 461exit 0;