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