This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.003_01: config_h.SH
[perl5.git] / configpm
CommitLineData
a0d0e21e 1#!./miniperl -w
8990e307 2
75f92628 3$config_pm = $ARGV[0] || 'lib/Config.pm';
8990e307
LW
4@ARGV = "./config.sh";
5
a0d0e21e 6# list names to put first (and hence lookup fastest)
3c81428c
PP
7@fast = qw(archname osname osvers prefix libs libpth
8 dynamic_ext static_ext extensions dlsrc so
9 sig_name cc ccflags cppflags
10 privlibexp archlibexp installprivlib installarchlib
a0d0e21e 11 sharpbang startsh shsharp
3c81428c 12);
a0d0e21e 13
fec02dd3
AD
14# names of things which may need to have slashes changed to double-colons
15@extensions = qw(dynamic_ext static_ext extensions known_extensions);
16
a0d0e21e
LW
17
18open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n";
a5f75d66 19$myver = $];
3c81428c 20
a0d0e21e 21print CONFIG <<"ENDOFBEG";
8990e307 22package Config;
3c81428c 23use Exporter ();
8990e307
LW
24\@ISA = (Exporter);
25\@EXPORT = qw(%Config);
3c81428c 26\@EXPORT_OK = qw(myconfig config_sh config_vars);
8990e307 27
a5f75d66
AD
28\$] == $myver
29 or die "Perl lib version ($myver) doesn't match executable version (\$])\\n";
8990e307 30
a0d0e21e
LW
31# This file was created by configpm when Perl was built. Any changes
32# made to this file will be lost the next time perl is built.
33
8990e307
LW
34ENDOFBEG
35
16d20bd9 36
a0d0e21e 37@fast{@fast} = @fast;
fec02dd3 38@extensions{@extensions} = @extensions;
a0d0e21e
LW
39@non_v=();
40@v_fast=();
41@v_others=();
42
85e6fe83 43while (<>) {
a0d0e21e
LW
44 next if m:^#!/bin/sh:;
45 # Catch CONFIG=true and PATCHLEVEL=n line from Configure.
46 s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
47 unless (m/^(\w+)='(.*)'\s*$/){
48 push(@non_v, "#$_"); # not a name='value' line
49 next;
50 }
fec02dd3
AD
51 $name = $1;
52 if ($extensions{$name}) { s,/,::,g }
53 if (!$fast{$name}){ push(@v_others, $_); next; }
a0d0e21e
LW
54 push(@v_fast,$_);
55}
56
57foreach(@non_v){ print CONFIG $_ }
58
59print CONFIG "\n",
3c81428c 60 "my \$config_sh = <<'!END!';\n",
a0d0e21e 61 join("", @v_fast, sort @v_others),
3c81428c
PP
62 "!END!\n\n";
63
64# copy config summary format from the myconfig script
65
66print CONFIG "my \$summary = <<'!END!';\n";
67
68open(MYCONFIG,"<myconfig") || die "open myconfig failed: $!";
691 while( ($_=<MYCONFIG>) !~ /^Summary of/);
70do { print CONFIG $_ } until ($_ = <MYCONFIG>) =~ /^\s*$/;
71close(MYCONFIG);
a0d0e21e 72
3c81428c
PP
73print CONFIG "\n!END!\n", <<'EOT';
74my $summary_expanded = 0;
75
76sub myconfig {
77 return $summary if $summary_expanded;
78 $summary =~ s/\$(\w+)/$Config{$1}/ge;
79 $summary_expanded = 1;
80 $summary;
81}
82EOT
83
84# ----
a0d0e21e
LW
85
86print CONFIG <<'ENDOFEND';
87
88tie %Config, Config;
89sub TIEHASH { bless {} }
90sub FETCH {
91 # check for cached value (which maybe undef so we use exists not defined)
92 return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
93
94 my($value); # search for the item in the big $config_sh string
95 return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
96
97 $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}".
98 $_[0]->{$_[1]} = $value; # cache it
99 return $value;
100}
101
3c81428c
PP
102my $prevpos = 0;
103
a0d0e21e
LW
104sub FIRSTKEY {
105 $prevpos = 0;
3c81428c 106 my($key) = $config_sh =~ m/^(.*?)=/;
a0d0e21e
LW
107 $key;
108}
109
110sub NEXTKEY {
3c81428c
PP
111 my $pos = index($config_sh, "\n", $prevpos) + 1;
112 my $len = index($config_sh, "=", $pos) - $pos;
a0d0e21e 113 $prevpos = $pos;
3c81428c 114 $len > 0 ? substr($config_sh, $pos, $len) : undef;
85e6fe83 115}
a0d0e21e 116
3c81428c 117sub EXISTS {
a0d0e21e
LW
118 exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
119}
120
3c81428c
PP
121sub STORE { die "\%Config::Config is read-only\n" }
122sub DELETE { &STORE }
123sub CLEAR { &STORE }
a0d0e21e 124
3c81428c
PP
125
126sub config_sh {
127 $config_sh
748a9306 128}
3c81428c
PP
129sub config_vars {
130 foreach(@_){
131 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
132 $v='undef' unless defined $v;
133 print "$_='$v';\n";
134 }
135}
136
1371;
138__END__
748a9306 139
3c81428c 140=head1 NAME
a0d0e21e 141
3c81428c
PP
142Config - access Perl configuration information
143
144=head1 SYNOPSIS
145
146 use Config;
147 if ($Config{'cc'} =~ /gcc/) {
148 print "built by gcc\n";
149 }
150
151 use Config qw(myconfig config_sh config_vars);
152
153 print myconfig();
154
155 print config_sh();
156
157 config_vars(qw(osname archname));
158
159
160=head1 DESCRIPTION
161
162The Config module contains all the information that was available to
163the C<Configure> program at Perl build time (over 900 values).
164
165Shell variables from the F<config.sh> file (written by Configure) are
166stored in the readonly-variable C<%Config>, indexed by their names.
167
168Values stored in config.sh as 'undef' are returned as undefined
169values. The perl C<exists> function can be used to check is a
170named variable exists.
171
172=over 4
173
174=item myconfig()
175
176Returns a textual summary of the major perl configuration values.
177See also C<-V> in L<perlrun/Switches>.
178
179=item config_sh()
180
181Returns the entire perl configuration information in the form of the
182original config.sh shell variable assignment script.
183
184=item config_vars(@names)
185
186Prints to STDOUT the values of the named configuration variable. Each is
187printed on a separate line in the form:
188
189 name='value';
190
191Names which are unknown are output as C<name='UNKNOWN';>.
192See also C<-V:name> in L<perlrun/Switches>.
193
194=back
195
196=head1 EXAMPLE
197
198Here's a more sophisticated example of using %Config:
199
200 use Config;
201
202 defined $Config{sig_name} || die "No sigs?";
203 foreach $name (split(' ', $Config{sig_name})) {
204 $signo{$name} = $i;
205 $signame[$i] = $name;
206 $i++;
207 }
208
209 print "signal #17 = $signame[17]\n";
210 if ($signo{ALRM}) {
211 print "SIGALRM is $signo{ALRM}\n";
212 }
213
214=head1 WARNING
215
216Because this information is not stored within the perl executable
217itself it is possible (but unlikely) that the information does not
218relate to the actual perl binary which is being used to access it.
219
220The Config module is installed into the architecture and version
221specific library directory ($Config{installarchlib}) and it checks the
222perl version number when loaded.
223
224=head1 NOTE
225
226This module contains a good example of how to use tie to implement a
227cache and an example of how to make a tied variable readonly to those
228outside of it.
229
230=cut
a0d0e21e 231
a0d0e21e
LW
232ENDOFEND
233
234close(CONFIG);
235
236# Now do some simple tests on the Config.pm file we have created
237unshift(@INC,'lib');
238require $config_pm;
239import Config;
240
241die "$0: $config_pm not valid"
242 unless $Config{'CONFIG'} eq 'true';
243
244die "$0: error processing $config_pm"
245 if defined($Config{'an impossible name'})
246 or $Config{'CONFIG'} ne 'true' # test cache
247 ;
248
249die "$0: error processing $config_pm"
250 if eval '$Config{"cc"} = 1'
251 or eval 'delete $Config{"cc"}'
252 ;
253
254
85e6fe83 255exit 0;