Commit | Line | Data |
a0d0e21e |
1 | #!./miniperl -w |
8990e307 |
2 | |
75f92628 |
3 | $config_pm = $ARGV[0] || 'lib/Config.pm'; |
8990e307 |
4 | @ARGV = "./config.sh"; |
5 | |
a0d0e21e |
6 | # list names to put first (and hence lookup fastest) |
7 | @fast = qw(osname osvers so libpth archlib |
8 | sharpbang startsh shsharp |
9 | dynamic_ext static_ext extensions dl_src |
10 | sig_name ccflags cppflags intsize); |
11 | |
fec02dd3 |
12 | # names of things which may need to have slashes changed to double-colons |
13 | @extensions = qw(dynamic_ext static_ext extensions known_extensions); |
14 | |
a0d0e21e |
15 | |
16 | open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n"; |
8990e307 |
17 | $myver = sprintf("%.3f", $]); |
a0d0e21e |
18 | print CONFIG <<"ENDOFBEG"; |
8990e307 |
19 | package Config; |
20 | require Exporter; |
21 | \@ISA = (Exporter); |
22 | \@EXPORT = qw(%Config); |
23 | |
24 | \$] == $myver or die sprintf |
25 | "Perl lib version ($myver) doesn't match executable version (%.3f)\\n", \$]; |
26 | |
a0d0e21e |
27 | # This file was created by configpm when Perl was built. Any changes |
28 | # made to this file will be lost the next time perl is built. |
29 | |
8990e307 |
30 | ENDOFBEG |
31 | |
16d20bd9 |
32 | print CONFIG <<'EndOfPod'; |
33 | =head1 NAME |
34 | |
35 | Config - access Perl configuration option |
36 | |
37 | =head1 SYNOPSIS |
38 | |
39 | use Config; |
40 | if ($Config{'cc'} =~ /gcc/) { |
41 | print "built by gcc\n"; |
42 | } |
43 | |
44 | =head1 DESCRIPTION |
45 | |
46 | The Config module contains everything that was available to the |
47 | C<Configure> program at Perl build time. Shell variables from |
48 | F<config.sh> are stored in the readonly-variable C<%Config>, indexed by |
49 | their names. |
50 | |
51 | =head1 EXAMPLE |
52 | |
53 | Here's a more sophisticated example of using %Config: |
54 | |
55 | use Config; |
56 | |
57 | defined $Config{sig_name} || die "No sigs?"; |
58 | foreach $name (split(' ', $Config{sig_name})) { |
59 | $signo{$name} = $i; |
60 | $signame[$i] = $name; |
61 | $i++; |
62 | } |
63 | |
64 | print "signal #17 = $signame[17]\n"; |
65 | if ($signo{ALRM}) { |
66 | print "SIGALRM is $signo{ALRM}\n"; |
67 | } |
68 | |
69 | =head1 NOTE |
70 | |
71 | This module contains a good example of how to make a variable |
72 | readonly to those outside of it. |
73 | |
74 | =cut |
75 | |
76 | EndOfPod |
77 | |
a0d0e21e |
78 | @fast{@fast} = @fast; |
fec02dd3 |
79 | @extensions{@extensions} = @extensions; |
a0d0e21e |
80 | @non_v=(); |
81 | @v_fast=(); |
82 | @v_others=(); |
83 | |
85e6fe83 |
84 | while (<>) { |
a0d0e21e |
85 | next if m:^#!/bin/sh:; |
86 | # Catch CONFIG=true and PATCHLEVEL=n line from Configure. |
87 | s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/; |
88 | unless (m/^(\w+)='(.*)'\s*$/){ |
89 | push(@non_v, "#$_"); # not a name='value' line |
90 | next; |
91 | } |
fec02dd3 |
92 | $name = $1; |
93 | if ($extensions{$name}) { s,/,::,g } |
94 | if (!$fast{$name}){ push(@v_others, $_); next; } |
a0d0e21e |
95 | push(@v_fast,$_); |
96 | } |
97 | |
98 | foreach(@non_v){ print CONFIG $_ } |
99 | |
100 | print CONFIG "\n", |
101 | "\$config_sh=<<'!END!OF!CONFIG!';\n", |
102 | join("", @v_fast, sort @v_others), |
103 | "!END!OF!CONFIG!\n\n"; |
104 | |
105 | |
106 | print CONFIG <<'ENDOFEND'; |
107 | |
108 | tie %Config, Config; |
109 | sub TIEHASH { bless {} } |
110 | sub FETCH { |
111 | # check for cached value (which maybe undef so we use exists not defined) |
112 | return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]}); |
113 | |
114 | my($value); # search for the item in the big $config_sh string |
115 | return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m); |
116 | |
117 | $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}". |
118 | $_[0]->{$_[1]} = $value; # cache it |
119 | return $value; |
120 | } |
121 | |
122 | sub FIRSTKEY { |
123 | $prevpos = 0; |
124 | my $key; |
125 | ($key) = $config_sh =~ m/^(.*)=/; |
126 | $key; |
127 | } |
128 | |
129 | sub NEXTKEY { |
130 | my ($pos, $len); |
131 | $pos = $prevpos; |
132 | $pos = index( $config_sh, "\n", $pos) + 1; |
133 | $prevpos = $pos; |
134 | $len = index( $config_sh, "=", $pos) - $pos; |
135 | $len > 0 ? substr( $config_sh, $pos, $len) : undef; |
85e6fe83 |
136 | } |
a0d0e21e |
137 | |
138 | sub EXISTS{ |
139 | exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m; |
140 | } |
141 | |
142 | sub readonly { die "\%Config::Config is read-only\n" } |
143 | |
748a9306 |
144 | sub myconfig { |
145 | my($output); |
146 | |
147 | $output = <<'END'; |
148 | Summary of my $package (patchlevel $PATCHLEVEL) configuration: |
149 | Platform: |
150 | osname=$osname, osver=$osvers, archname=$archname |
151 | uname='$myuname' |
152 | hint=$hint |
153 | Compiler: |
154 | cc='$cc', optimize='$optimize' |
155 | cppflags='$cppflags' |
156 | ccflags ='$ccflags' |
157 | ldflags ='$ldflags' |
158 | stdchar='$stdchar', d_stdstdio=$d_stdstdio, usevfork=$usevfork |
159 | voidflags=$voidflags, castflags=$castflags, d_casti32=$d_casti32, d_castneg=$d_castneg |
160 | intsize=$intsize, alignbytes=$alignbytes, usemymalloc=$usemymalloc, randbits=$randbits |
161 | Libraries: |
162 | so=$so |
163 | libpth=$libpth |
164 | libs=$libs |
165 | libc=$libc |
166 | Dynamic Linking: |
167 | dlsrc=$dlsrc, dlext=$dlext, d_dlsymun=$d_dlsymun |
168 | cccdlflags='$cccdlflags', ccdlflags='$ccdlflags', lddlflags='$lddlflags' |
169 | |
170 | END |
171 | $output =~ s/\$(\w+)/$Config{$1}/ge; |
172 | $output; |
173 | } |
174 | |
a0d0e21e |
175 | sub STORE { &readonly } |
176 | sub DELETE{ &readonly } |
177 | sub CLEAR { &readonly } |
178 | |
8e07c86e |
179 | sub config_sh { $config_sh } |
a0d0e21e |
180 | |
181 | 1; |
182 | ENDOFEND |
183 | |
184 | close(CONFIG); |
185 | |
186 | # Now do some simple tests on the Config.pm file we have created |
187 | unshift(@INC,'lib'); |
188 | require $config_pm; |
189 | import Config; |
190 | |
191 | die "$0: $config_pm not valid" |
192 | unless $Config{'CONFIG'} eq 'true'; |
193 | |
194 | die "$0: error processing $config_pm" |
195 | if defined($Config{'an impossible name'}) |
196 | or $Config{'CONFIG'} ne 'true' # test cache |
197 | ; |
198 | |
199 | die "$0: error processing $config_pm" |
200 | if eval '$Config{"cc"} = 1' |
201 | or eval 'delete $Config{"cc"}' |
202 | ; |
203 | |
204 | |
85e6fe83 |
205 | exit 0; |