This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[dummy merge]
[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 7@fast = qw(archname osname osvers prefix libs libpth
8 dynamic_ext static_ext extensions dlsrc so
743c51bc 9 sig_name sig_num cc ccflags cppflags
3c81428c 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 28\$] == $myver
9193ea20 29 or die "Perl lib version ($myver) doesn't match executable version (\$])";
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=();
44a8e56a 42$in_v = 0;
a0d0e21e 43
85e6fe83 44while (<>) {
a0d0e21e
LW
45 next if m:^#!/bin/sh:;
46 # Catch CONFIG=true and PATCHLEVEL=n line from Configure.
47 s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
44a8e56a 48 unless ($in_v or m/^(\w+)='(.*\n)/){
a0d0e21e
LW
49 push(@non_v, "#$_"); # not a name='value' line
50 next;
51 }
44a8e56a 52 if ($in_v) { $val .= $_; }
53 else { ($name,$val) = ($1,$2); }
54 $in_v = $val !~ /'\n/;
55 next if $in_v;
fec02dd3 56 if ($extensions{$name}) { s,/,::,g }
44a8e56a 57 if (!$fast{$name}){ push(@v_others, "$name='$val"); next; }
58 push(@v_fast,"$name='$val");
a0d0e21e
LW
59}
60
61foreach(@non_v){ print CONFIG $_ }
62
63print CONFIG "\n",
3c81428c 64 "my \$config_sh = <<'!END!';\n",
a0d0e21e 65 join("", @v_fast, sort @v_others),
3c81428c 66 "!END!\n\n";
67
68# copy config summary format from the myconfig script
69
70print CONFIG "my \$summary = <<'!END!';\n";
71
72open(MYCONFIG,"<myconfig") || die "open myconfig failed: $!";
731 while( ($_=<MYCONFIG>) !~ /^Summary of/);
74do { print CONFIG $_ } until ($_ = <MYCONFIG>) =~ /^\s*$/;
75close(MYCONFIG);
a0d0e21e 76
3c81428c 77print CONFIG "\n!END!\n", <<'EOT';
78my $summary_expanded = 0;
79
80sub myconfig {
81 return $summary if $summary_expanded;
82 $summary =~ s/\$(\w+)/$Config{$1}/ge;
83 $summary_expanded = 1;
84 $summary;
85}
86EOT
87
88# ----
a0d0e21e
LW
89
90print CONFIG <<'ENDOFEND';
91
a0d0e21e 92sub FETCH {
aa1bdcb8 93 # check for cached value (which may be undef so we use exists not defined)
a0d0e21e 94 return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
aa1bdcb8
TP
95
96 # Search for it in the big string
97 my($value, $start, $marker);
98 $marker = "$_[1]='";
99 # return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
100 $start = index($config_sh, "\n$marker");
101 return undef if ( ($start == -1) && # in case it's first
102 (substr($config_sh, 0, length($marker)) ne $marker) );
103 if ($start == -1) { $start = length($marker) }
104 else { $start += length($marker) + 1 }
105 $value = substr($config_sh, $start,
44a8e56a 106 index($config_sh, qq('\n), $start) - $start);
a0d0e21e
LW
107
108 $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}".
109 $_[0]->{$_[1]} = $value; # cache it
110 return $value;
111}
112
3c81428c 113my $prevpos = 0;
114
a0d0e21e
LW
115sub FIRSTKEY {
116 $prevpos = 0;
aa1bdcb8
TP
117 # my($key) = $config_sh =~ m/^(.*?)=/;
118 substr($config_sh, 0, index($config_sh, '=') );
119 # $key;
a0d0e21e
LW
120}
121
122sub NEXTKEY {
44a8e56a 123 my $pos = index($config_sh, qq('\n), $prevpos) + 2;
3c81428c 124 my $len = index($config_sh, "=", $pos) - $pos;
a0d0e21e 125 $prevpos = $pos;
3c81428c 126 $len > 0 ? substr($config_sh, $pos, $len) : undef;
85e6fe83 127}
a0d0e21e 128
3c81428c 129sub EXISTS {
aa1bdcb8
TP
130 # exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
131 exists($_[0]->{$_[1]}) or
132 index($config_sh, "\n$_[1]='") != -1 or
133 substr($config_sh, 0, length($_[1])+2) eq "$_[1]='";
a0d0e21e
LW
134}
135
3c81428c 136sub STORE { die "\%Config::Config is read-only\n" }
137sub DELETE { &STORE }
138sub CLEAR { &STORE }
a0d0e21e 139
3c81428c 140
141sub config_sh {
142 $config_sh
748a9306 143}
9193ea20 144
145sub config_re {
146 my $re = shift;
147 my @matches = ($config_sh =~ /^$re=.*\n/mg);
148 @matches ? (print @matches) : print "$re: not found\n";
149}
150
3c81428c 151sub config_vars {
152 foreach(@_){
9193ea20 153 config_re($_), next if /\W/;
3c81428c 154 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
155 $v='undef' unless defined $v;
156 print "$_='$v';\n";
157 }
158}
159
9193ea20 160ENDOFEND
161
162if ($^O eq 'os2') {
163 print CONFIG <<'ENDOFSET';
164my %preconfig;
165if ($OS2::is_aout) {
166 my ($value, $v) = $config_sh =~ m/^used_aout='(.*)'\s*$/m;
167 for (split ' ', $value) {
168 ($v) = $config_sh =~ m/^aout_$_='(.*)'\s*$/m;
169 $preconfig{$_} = $v eq 'undef' ? undef : $v;
170 }
171}
172sub TIEHASH { bless {%preconfig} }
173ENDOFSET
174} else {
175 print CONFIG <<'ENDOFSET';
176sub TIEHASH { bless {} }
177ENDOFSET
178}
179
180print CONFIG <<'ENDOFTAIL';
181
182tie %Config, 'Config';
183
3c81428c 1841;
185__END__
748a9306 186
3c81428c 187=head1 NAME
a0d0e21e 188
3c81428c 189Config - access Perl configuration information
190
191=head1 SYNOPSIS
192
193 use Config;
194 if ($Config{'cc'} =~ /gcc/) {
195 print "built by gcc\n";
196 }
197
198 use Config qw(myconfig config_sh config_vars);
199
200 print myconfig();
201
202 print config_sh();
203
204 config_vars(qw(osname archname));
205
206
207=head1 DESCRIPTION
208
209The Config module contains all the information that was available to
210the C<Configure> program at Perl build time (over 900 values).
211
212Shell variables from the F<config.sh> file (written by Configure) are
213stored in the readonly-variable C<%Config>, indexed by their names.
214
215Values stored in config.sh as 'undef' are returned as undefined
1fef88e7 216values. The perl C<exists> function can be used to check if a
3c81428c 217named variable exists.
218
219=over 4
220
221=item myconfig()
222
223Returns a textual summary of the major perl configuration values.
224See also C<-V> in L<perlrun/Switches>.
225
226=item config_sh()
227
228Returns the entire perl configuration information in the form of the
229original config.sh shell variable assignment script.
230
231=item config_vars(@names)
232
233Prints to STDOUT the values of the named configuration variable. Each is
234printed on a separate line in the form:
235
236 name='value';
237
238Names which are unknown are output as C<name='UNKNOWN';>.
239See also C<-V:name> in L<perlrun/Switches>.
240
241=back
242
243=head1 EXAMPLE
244
245Here's a more sophisticated example of using %Config:
246
247 use Config;
743c51bc
W
248 use strict;
249
250 my %sig_num;
251 my @sig_name;
252 unless($Config{sig_name} && $Config{sig_num}) {
253 die "No sigs?";
254 } else {
255 my @names = split ' ', $Config{sig_name};
256 @sig_num{@names} = split ' ', $Config{sig_num};
257 foreach (@names) {
258 $sig_name[$sig_num{$_}] ||= $_;
259 }
260 }
3c81428c 261
743c51bc
W
262 print "signal #17 = $sig_name[17]\n";
263 if ($sig_num{ALRM}) {
264 print "SIGALRM is $sig_num{ALRM}\n";
3c81428c 265 }
266
267=head1 WARNING
268
269Because this information is not stored within the perl executable
270itself it is possible (but unlikely) that the information does not
271relate to the actual perl binary which is being used to access it.
272
273The Config module is installed into the architecture and version
274specific library directory ($Config{installarchlib}) and it checks the
275perl version number when loaded.
276
277=head1 NOTE
278
279This module contains a good example of how to use tie to implement a
280cache and an example of how to make a tied variable readonly to those
281outside of it.
282
283=cut
a0d0e21e 284
9193ea20 285ENDOFTAIL
a0d0e21e
LW
286
287close(CONFIG);
288
289# Now do some simple tests on the Config.pm file we have created
290unshift(@INC,'lib');
291require $config_pm;
292import Config;
293
294die "$0: $config_pm not valid"
295 unless $Config{'CONFIG'} eq 'true';
296
297die "$0: error processing $config_pm"
298 if defined($Config{'an impossible name'})
299 or $Config{'CONFIG'} ne 'true' # test cache
300 ;
301
302die "$0: error processing $config_pm"
303 if eval '$Config{"cc"} = 1'
304 or eval 'delete $Config{"cc"}'
305 ;
306
307
85e6fe83 308exit 0;