1#!perl -w
2use strict;
3use FindExt;
4
5# take a semicolon separated path list and turn it into a quoted
6# list of paths that Text::Parsewords will grok
7sub mungepath {
8    my $p = shift;
9    # remove leading/trailing semis/spaces
10    $p =~ s/^[ ;]+//;
11    $p =~ s/[ ;]+$//;
12    $p =~ s/'/"/g;
13    my @p = map { $_ = "\"$_\"" if /\s/ and !/^".*"$/; $_ } split /;/, $p;
14    return join(' ', @p);
15}
16
17# generate an array of option strings from command-line args
18# or an option file
19#    -- added by BKS, 10-17-1999 to fix command-line overflow problems
20sub loadopts {
21    if ($ARGV[0] =~ /--cfgsh-option-file/) {
22	shift @ARGV;
23	my $optfile = shift @ARGV;
24	local (*OPTF);
25	open OPTF, '<', $optfile or die "Can't open $optfile: $!\n";
26	my @opts;
27	chomp(my $line = <OPTF>);
28	my @vars = split(/\t+~\t+/, $line);
29	for (@vars) {
30	    push(@opts, $_) unless (/^\s*$/);
31	}
32	close OPTF;
33	return \@opts;
34    }
35    else {
36	return \@ARGV;
37    }
38}
39
40my %opt;
41
42my $optref = loadopts();
43while (@{$optref} && $optref->[0] =~ /^([\w_]+)=(.*)$/) {
44    $opt{$1}=$2;
45    shift(@{$optref});
46}
47
48FindExt::scan_ext("../cpan");
49FindExt::scan_ext("../dist");
50FindExt::scan_ext("../ext");
51FindExt::set_static_extensions(split ' ', $opt{static_ext});
52
53$opt{nonxs_ext}        = join(' ',FindExt::nonxs_ext()) || ' ';
54$opt{static_ext}       = join(' ',FindExt::static_ext()) || ' ';
55$opt{dynamic_ext}      = join(' ',FindExt::dynamic_ext()) || ' ';
56$opt{extensions}       = join(' ',FindExt::extensions()) || ' ';
57$opt{known_extensions} = join(' ',FindExt::known_extensions()) || ' ';
58
59my $pl_h = '../patchlevel.h';
60
61if (-e $pl_h) {
62    open PL, "<", $pl_h or die "Can't open $pl_h: $!";
63    while (<PL>) {
64	if (/^#\s*define\s+(PERL_\w+)\s+([\d.]+)/) {
65	    $opt{$1} = $2;
66	}
67    }
68    close PL;
69}
70else {
71    die "Can't find $pl_h: $!";
72}
73
74my $patch_file = '../.patch';
75
76if (-e $patch_file) {
77    open my $fh, "<", $patch_file or die "Can't open $patch_file: $!";
78    chomp($opt{PERL_PATCHLEVEL} = <$fh>);
79    close $fh;
80}
81
82$opt{version} = "$opt{PERL_REVISION}.$opt{PERL_VERSION}.$opt{PERL_SUBVERSION}";
83$opt{version_patchlevel_string} = "version $opt{PERL_VERSION} subversion $opt{PERL_SUBVERSION}";
84$opt{version_patchlevel_string} .= " patch $opt{PERL_PATCHLEVEL}" if exists $opt{PERL_PATCHLEVEL};
85
86my $ver = `ver 2>nul`;
87$opt{osvers} = $ver =~ /\b(\d+(?:\.\d+)+)\b/ ? $1 : '4.0';
88
89if (exists $opt{cc}) {
90    # cl version detection borrowed from Test::Smoke's configsmoke.pl
91    if ($opt{cc} =~ /\b(?:cl|icl)/) { #MSVC can come as clarm.exe, icl=Intel C
92        my $output = `$opt{cc} 2>&1`;
93        $opt{ccversion} = $output =~ /\b(\d+(?:\.\d+)+)\b/ ? $1 : '?';
94    }
95    elsif ($opt{cc} =~ /\bgcc\b/) {
96        chomp($opt{gccversion} = `$opt{cc} -dumpversion`);
97    }
98}
99
100$opt{cf_by} = $ENV{USERNAME} unless $opt{cf_by};
101if (!$opt{cf_email}) {
102    my $computername = eval{(gethostbyname('localhost'))[0]};
103# gethostbyname might not be implemented in miniperl
104    $computername = $ENV{COMPUTERNAME} if $@;
105    $opt{cf_email} = $opt{cf_by} . '@' . $computername;
106}
107$opt{usemymalloc} = 'y' if $opt{d_mymalloc} eq 'define';
108
109$opt{libpth} = mungepath($opt{libpth}) if exists $opt{libpth};
110$opt{incpath} = mungepath($opt{incpath}) if exists $opt{incpath};
111
112my $int64;
113if ($opt{cc} =~ /\b(?:cl|icl)/) {
114    $int64  = '__int64';
115}
116elsif ($opt{cc} =~ /\bgcc\b/) {
117    $int64  = 'long long';
118}
119
120# set large files options
121if ($opt{uselargefiles} eq 'define') {
122    $opt{lseeksize} = 8;
123    $opt{lseektype} = $int64;
124}
125else {
126    $opt{lseeksize} = 4;
127    $opt{lseektype} = 'long';
128}
129
130# set 64-bit options
131if ($opt{WIN64} eq 'define') {
132    $opt{d_atoll} = 'define';
133    $opt{d_strtoll} = 'define';
134    $opt{d_strtoull} = 'define';
135    $opt{ptrsize} = 8;
136    $opt{sizesize} = 8;
137    $opt{ssizetype} = $int64;
138    $opt{st_ino_size} = 8;
139}
140else {
141    $opt{d_atoll} = 'undef';
142    $opt{d_strtoll} = 'undef';
143    $opt{d_strtoull} = 'undef';
144    $opt{ptrsize} = 4;
145    $opt{sizesize} = 4;
146    $opt{ssizetype} = 'int';
147    $opt{st_ino_size} = 4;
148}
149
150# set 64-bit-int options
151if ($opt{use64bitint} eq 'define') {
152    if ($opt{uselongdouble} eq 'define') {
153        $opt{d_nv_preserves_uv} = 'define';
154        $opt{nv_preserves_uv_bits} = 64;
155    }
156    else {
157        $opt{d_nv_preserves_uv} = 'undef';
158        $opt{nv_preserves_uv_bits} = 53;
159    }
160    $opt{ivdformat} = qq{"I64d"};
161    $opt{ivsize} = 8;
162    $opt{ivtype} = $int64;
163    $opt{sPRIXU64} = qq{"I64X"};
164    $opt{sPRId64} = qq{"I64d"};
165    $opt{sPRIi64} = qq{"I64i"};
166    $opt{sPRIo64} = qq{"I64o"};
167    $opt{sPRIu64} = qq{"I64u"};
168    $opt{sPRIx64} = qq{"I64x"};
169    $opt{uvXUformat} = qq{"I64X"};
170    $opt{uvoformat} = qq{"I64o"};
171    $opt{uvsize} = 8;
172    $opt{uvtype} = qq{unsigned $int64};
173    $opt{uvuformat} = qq{"I64u"};
174    $opt{uvxformat} = qq{"I64x"};
175}
176else {
177    $opt{d_nv_preserves_uv} = 'define';
178    $opt{ivdformat} = '"ld"';
179    $opt{ivsize} = 4;
180    $opt{ivtype} = 'long';
181    $opt{nv_preserves_uv_bits} = 32;
182    $opt{sPRIXU64} = '"lX"';
183    $opt{sPRId64} = '"ld"';
184    $opt{sPRIi64} = '"li"';
185    $opt{sPRIo64} = '"lo"';
186    $opt{sPRIu64} = '"lu"';
187    $opt{sPRIx64} = '"lx"';
188    $opt{uvXUformat} = '"lX"';
189    $opt{uvoformat} = '"lo"';
190    $opt{uvsize} = 4;
191    $opt{uvtype} = 'unsigned long';
192    $opt{uvuformat} = '"lu"';
193    $opt{uvxformat} = '"lx"';
194}
195
196unless ($opt{cc} =~ /\bcl/) {
197    if ($opt{WIN64} eq 'define') {
198        $opt{longdblsize} = 16;
199        $opt{longdblinfbytes} = '0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00';
200        $opt{longdblnanbytes} = '0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00';
201    }
202    else {
203        $opt{longdblsize} = 12;
204        $opt{longdblinfbytes} = '0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0x7f, 0x00, 0x00';
205        $opt{longdblnanbytes} = '0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00';
206    }
207}
208
209# set long double options
210if ($opt{uselongdouble} eq 'define') {
211    $opt{d_Gconvert} = 'sprintf((b),"%.*""Lg",(n),(x))';
212    $opt{d_PRIEUldbl} = 'define';
213    $opt{d_PRIFUldbl} = 'define';
214    $opt{d_PRIGUldbl} = 'define';
215    $opt{d_modflproto} = 'define';
216    $opt{d_strtold} = 'define';
217    $opt{d_PRIeldbl} = 'define';
218    $opt{d_PRIfldbl} = 'define';
219    $opt{d_PRIgldbl} = 'define';
220    $opt{d_SCNfldbl} = 'define';
221    $opt{nvsize} = $opt{longdblsize};
222    $opt{nvtype} = 'long double';
223    $opt{nv_overflows_integers_at} = '256.0*256.0*256.0*256.0*256.0*256.0*256.0*2.0*2.0*2.0*2.0*2.0*2.0*2.0*2.0';
224    $opt{nvEUformat} = '"LE"';
225    $opt{nvFUformat} = '"LF"';
226    $opt{nvGUformat} = '"LG"';
227    $opt{nveformat} = '"Le"';
228    $opt{nvfformat} = '"Lf"';
229    $opt{nvgformat} = '"Lg"';
230    $opt{longdblkind} = 3;
231    $opt{longdblmantbits} = 64;
232}
233else {
234    $opt{d_Gconvert} = 'sprintf((b),"%.*g",(n),(x))';
235    $opt{d_PRIEUldbl} = 'undef';
236    $opt{d_PRIFUldbl} = 'undef';
237    $opt{d_PRIGUldbl} = 'undef';
238
239    if($opt{cc} =~ /\b(?:cl|icl)/) {
240        $opt{d_modflproto} = 'undef';
241    }
242    else {
243        $opt{d_modflproto} = 'define';
244    }
245
246    $opt{d_strtold} = 'undef';
247    $opt{d_PRIeldbl} = 'undef';
248    $opt{d_PRIfldbl} = 'undef';
249    $opt{d_PRIgldbl} = 'undef';
250    $opt{d_SCNfldbl} = 'undef';
251    $opt{nvsize} = 8;
252    $opt{nvtype} = 'double';
253    $opt{nv_overflows_integers_at} = '256.0*256.0*256.0*256.0*256.0*256.0*2.0*2.0*2.0*2.0*2.0';
254    $opt{nvEUformat} = '"E"';
255    $opt{nvFUformat} = '"F"';
256    $opt{nvGUformat} = '"G"';
257    $opt{nveformat} = '"e"';
258    $opt{nvfformat} = '"f"';
259    $opt{nvgformat} = '"g"';
260}
261
262# change some configuration variables based on compiler version
263if ($opt{cc} =~ /\bcl/ and $opt{ccversion} =~ /^(\d+)/) {
264    my $ccversion = $1;
265    if ($ccversion < 13) { # VC6
266	$opt{ar} ='lib';
267    }
268    if ($ccversion >= 14) { # VC8+
269	$opt{sGMTIME_max} = 32535291599;
270	$opt{sLOCALTIME_max} = 32535244799;
271    }
272    if ($ccversion >= 16) { # VC10+
273	$opt{i_stdint} = 'define';
274    }
275    if ($ccversion >= 19) { # VC14+
276	$opt{stdio_base} = 'PERLIO_FILE_base(fp)';
277	$opt{stdio_bufsiz} = '(PERLIO_FILE_cnt(fp) + PERLIO_FILE_ptr(fp) - PERLIO_FILE_base(fp))';
278	$opt{stdio_cnt} = 'PERLIO_FILE_cnt(fp)';
279	$opt{stdio_ptr} = 'PERLIO_FILE_ptr(fp)';
280	$opt{i_stdbool} = 'define';
281    }
282}
283# find out which MSVC this ICC is using
284elsif ($opt{cc} =~ /\bicl/) {
285    my $output = `cl 2>&1`;
286    my $num_ver = $output =~ /\b(\d+(?:\.\d+)+)\b/ ? $1 : '?';
287    if ($num_ver =~ /^(\d+)/ && $1 >= 14) { # VC8+
288	$opt{sGMTIME_max} = 32535291599;
289	$opt{sLOCALTIME_max} = 32535244799;
290    }
291    if ($num_ver =~ /^(\d+)/ && $1 >= 16) { # VC10+
292	$opt{i_stdint} = 'define';
293    }
294    if ($num_ver =~ /^(\d+)/ && $1 >= 19) { # VC14+
295	$opt{stdio_base} = 'PERLIO_FILE_base(fp)';
296	$opt{stdio_bufsiz} = '(PERLIO_FILE_cnt(fp) + PERLIO_FILE_ptr(fp) - PERLIO_FILE_base(fp))';
297	$opt{stdio_cnt} = 'PERLIO_FILE_cnt(fp)';
298	$opt{stdio_ptr} = 'PERLIO_FILE_ptr(fp)';
299	$opt{i_stdbool} = 'define';
300    }
301    $opt{ar} ='xilib';
302}
303
304if ($opt{useithreads} eq 'define' && $opt{ccflags} =~ /-DPERL_IMPLICIT_SYS\b/) {
305    $opt{d_pseudofork} = 'define';
306}
307
308if ($opt{usecplusplus} eq 'define') {
309    $opt{d_cplusplus} = 'define';
310    $opt{extern_C} = 'extern "C"';
311}
312
313#if the fields above are defined, they override the defaults in the premade
314#config file
315while (<>) {
316    s/~([\w_]+)~/exists $opt{$1} ? $opt{$1} : ''/eg;
317    if (/^([\w_]+)=(.*)$/) {
318	my($k,$v) = ($1,$2);
319	# this depends on cf_time being empty in the template (or we'll
320	# get a loop)
321	if ($k eq 'cf_time') {
322	    $_ = "$k='" . localtime(time) . "'\n" if $v =~ /^\s*'\s*'/;
323	}
324	elsif (exists $opt{$k}) {
325	    $_ = "$k='$opt{$k}'\n";
326	}
327    }
328    print;
329}
330