source: trunk/third/perl/configpm @ 14545

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