]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/perl/OpenSSL/config.pm
Update copyright year
[thirdparty/openssl.git] / util / perl / OpenSSL / config.pm
CommitLineData
4901b570 1#! /usr/bin/env perl
8020d79b 2# Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
4901b570
RS
3#
4# Licensed under the Apache License 2.0 (the "License"). You may not use
5# this file except in compliance with the License. You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9# Determine the operating system and run ./Configure. Far descendant from
10# Apache's minarch and GuessOS.
11
12package OpenSSL::config;
13
14use strict;
15use warnings;
16use Getopt::Std;
17use File::Basename;
a3310b18 18use IPC::Cmd;
69aa579e 19use POSIX;
a3310b18 20use Carp;
4901b570
RS
21
22# These control our behavior.
23my $DRYRUN;
24my $VERBOSE;
4901b570 25my $WHERE = dirname($0);
ecb09baf 26my $WAIT = 1;
4901b570
RS
27
28# Machine type, etc., used to determine the platform
29my $MACHINE;
30my $RELEASE;
31my $SYSTEM;
32my $VERSION;
a3310b18 33my $CCVENDOR;
4901b570 34my $CCVER;
4901b570
RS
35my $GCC_BITS;
36my $GCC_ARCH;
37
38# Some environment variables; they will affect Configure
4901b570 39my $CONFIG_OPTIONS = $ENV{CONFIG_OPTIONS} // '';
e39795af
RL
40my $CC;
41my $CROSS_COMPILE;
4901b570 42
a3310b18
RL
43# For determine_compiler_settings, the list of known compilers
44my @c_compilers = qw(clang gcc cc);
45# Methods to determine compiler version. The expected output is one of
46# MAJOR or MAJOR.MINOR or MAJOR.MINOR.PATCH... or false if the compiler
47# isn't of the given brand.
48# This is a list to ensure that gnu comes last, as we've made it a fallback
49my @cc_version =
50 (
51 clang => sub {
52 my $v = `$CROSS_COMPILE$CC -v 2>&1`;
cd84d883 53 $v =~ m/(?:(?:clang|LLVM) version|.*based on LLVM)\s+([0-9]+\.[0-9]+)/;
a3310b18
RL
54 return $1;
55 },
56 gnu => sub {
57 my $v = `$CROSS_COMPILE$CC -dumpversion 2>/dev/null`;
58 # Strip off whatever prefix egcs prepends the number with.
59 # Hopefully, this will work for any future prefixes as well.
60 $v =~ s/^[a-zA-Z]*\-//;
61 return $v;
62 },
63 );
64
4901b570
RS
65# This is what we will set as the target for calling Configure.
66my $options = '';
67
4901b570 68# Pattern matches against "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}"
bfa68462
RL
69# The patterns are assumed to be wrapped like this: /^(${pattern})$/
70my $guess_patterns = [
71 [ 'A\/UX:.*', 'm68k-apple-aux3' ],
72 [ 'AIX:[3-9]:4:.*', '${MACHINE}-ibm-aix' ],
73 [ 'AIX:.*?:[5-9]:.*', '${MACHINE}-ibm-aix' ],
74 [ 'AIX:.*', '${MACHINE}-ibm-aix3' ],
75 [ 'HI-UX:.*', '${MACHINE}-hi-hiux' ],
76 [ 'HP-UX:.*',
77 sub {
78 my $HPUXVER = $RELEASE;
79 $HPUXVER = s/[^.]*.[0B]*//;
80 # HPUX 10 and 11 targets are unified
81 return "${MACHINE}-hp-hpux1x" if $HPUXVER =~ m@1[0-9]@;
82 return "${MACHINE}-hp-hpux";
83 }
84 ],
85 [ 'IRIX:6\..*', 'mips3-sgi-irix' ],
86 [ 'IRIX64:.*', 'mips4-sgi-irix64' ],
87 [ 'Linux:[2-9]\..*', '${MACHINE}-whatever-linux2' ],
88 [ 'Linux:1\..*', '${MACHINE}-whatever-linux1' ],
89 [ 'GNU.*', 'hurd-x86' ],
90 [ 'LynxOS:.*', '${MACHINE}-lynx-lynxos' ],
4901b570 91 # BSD/OS always says 386
bfa68462
RL
92 [ 'BSD\/OS:4\..*', 'i486-whatever-bsdi4' ],
93 # Order is important, this has to appear before 'BSD\/386:'
94 [ 'BSD/386:.*?:.*?:.*486.*|BSD/OS:.*?:.*?:.*?:.*486.*',
95 sub {
96 my $BSDVAR = `/sbin/sysctl -n hw.model`;
97 return "i586-whatever-bsdi" if $BSDVAR =~ m@Pentium@;
98 return "i386-whatever-bsdi";
99 }
100 ],
101 [ 'BSD\/386:.*|BSD\/OS:.*', '${MACHINE}-whatever-bsdi' ],
102 # Order is important, this has to appear before 'FreeBSD:'
103 [ 'FreeBSD:.*?:.*?:.*386.*',
104 sub {
105 my $VERS = $RELEASE;
106 $VERS =~ s/[-(].*//;
107 my $MACH = `sysctl -n hw.model`;
108 $MACH = "i386" if $MACH =~ m@386@;
109 $MACH = "i486" if $MACH =~ m@486@;
110 $MACH = "i686" if $MACH =~ m@Pentium II@;
111 $MACH = "i586" if $MACH =~ m@Pentium@;
112 $MACH = "$MACHINE" if $MACH !~ /i.86/;
113 my $ARCH = 'whatever';
114 $ARCH = "pc" if $MACH =~ m@i[0-9]86@;
115 return "${MACH}-${ARCH}-freebsd${VERS}";
116 }
117 ],
118 [ 'DragonFly:.*', '${MACHINE}-whatever-dragonfly' ],
119 [ 'FreeBSD:.*', '${MACHINE}-whatever-freebsd' ],
120 [ 'Haiku:.*', '${MACHINE}-whatever-haiku' ],
121 # Order is important, this has to appear before 'NetBSD:.*'
122 [ 'NetBSD:.*?:.*?:.*386.*',
123 sub {
124 my $hw = `/usr/sbin/sysctl -n hw.model || /sbin/sysctl -n hw.model`;
125 $hw =~ s@.*(.)86-class.*@i${1}86@;
126 return "${hw}-whatever-netbsd";
127 }
128 ],
129 [ 'NetBSD:.*', '${MACHINE}-whatever-netbsd' ],
130 [ 'OpenBSD:.*', '${MACHINE}-whatever-openbsd' ],
131 [ 'OpenUNIX:.*', '${MACHINE}-unknown-OpenUNIX${VERSION}' ],
132 [ 'OSF1:.*?:.*?:.*alpha.*',
133 sub {
134 my $OSFMAJOR = $RELEASE;
135 $OSFMAJOR =~ 's/^V([0-9]*)\..*$/\1/';
136 return "${MACHINE}-dec-tru64" if $OSFMAJOR =~ m@[45]@;
137 return "${MACHINE}-dec-osf";
138 }
139 ],
140 [ 'Paragon.*?:.*', 'i860-intel-osf1' ],
141 [ 'Rhapsody:.*', 'ppc-apple-rhapsody' ],
142 [ 'Darwin:.*?:.*?:Power.*', 'ppc-apple-darwin' ],
8758f4e6 143 [ 'Darwin:.*', '${MACHINE}-apple-darwin' ],
bfa68462
RL
144 [ 'SunOS:5\..*', '${MACHINE}-whatever-solaris2' ],
145 [ 'SunOS:.*', '${MACHINE}-sun-sunos4' ],
146 [ 'UNIX_System_V:4\..*?:.*', '${MACHINE}-whatever-sysv4' ],
147 [ 'VOS:.*?:.*?:i786', 'i386-stratus-vos' ],
148 [ 'VOS:.*?:.*?:.*', 'hppa1.1-stratus-vos' ],
149 [ '.*?:4.*?:R4.*?:m88k', '${MACHINE}-whatever-sysv4' ],
150 [ 'DYNIX\/ptx:4.*?:.*', '${MACHINE}-whatever-sysv4' ],
151 [ '.*?:4\.0:3\.0:3[34]..(,.*)?', 'i486-ncr-sysv4' ],
152 [ 'ULTRIX:.*', '${MACHINE}-unknown-ultrix' ],
153 [ 'POSIX-BC.*', 'BS2000-siemens-sysv4' ],
154 [ 'machten:.*', '${MACHINE}-tenon-${SYSTEM}' ],
155 [ 'library:.*', '${MACHINE}-ncr-sysv4' ],
156 [ 'ConvexOS:.*?:11\.0:.*', '${MACHINE}-v11-${SYSTEM}' ],
157 [ 'MINGW64.*?:.*?:.*?:x86_64', '${MACHINE}-whatever-mingw64' ],
158 [ 'MINGW.*', '${MACHINE}-whatever-mingw' ],
159 [ 'CYGWIN.*', '${MACHINE}-pc-cygwin' ],
160 [ 'vxworks.*', '${MACHINE}-whatever-vxworks' ],
69aa579e 161
4232a9e5
RL
162 # Note: there's also NEO and NSR, but they are old and unsupported
163 [ 'NONSTOP_KERNEL:.*:NSE-.*?', 'nse-tandem-nsk${RELEASE}' ],
164 [ 'NONSTOP_KERNEL:.*:NSV-.*?', 'nsv-tandem-nsk${RELEASE}' ],
165 [ 'NONSTOP_KERNEL:.*:NSX-.*?', 'nsx-tandem-nsk${RELEASE}' ],
166
bfa68462 167 [ sub { -d '/usr/apollo' }, 'whatever-apollo-whatever' ],
4901b570
RS
168];
169
4901b570
RS
170# Run a command, return true if exit zero else false.
171# Multiple args are glued together into a pipeline.
172# Name comes from OpenSSL tests, often written as "ok(run(...."
173sub okrun {
174 my $command = join(' | ', @_);
175 my $status = system($command) >> 8;
176 return $status == 0;
177}
178
179# Give user a chance to abort/interrupt if interactive if interactive.
180sub maybe_abort {
181 if ( $WAIT && -t 1 ) {
182 eval {
183 local $SIG{ALRM} = sub { die "Timeout"; };
184 local $| = 1;
185 alarm(5);
186 print "You have about five seconds to abort: ";
187 my $ignored = <STDIN>;
188 alarm(0);
189 };
190 print "\n" if $@ =~ /Timeout/;
191 }
192}
193
4901b570
RS
194# Look for ISC/SCO with its unique uname program
195sub is_sco_uname {
9e1094ad
RL
196 return undef unless IPC::Cmd::can_run('uname');
197
4901b570
RS
198 open UNAME, "uname -X 2>/dev/null|" or return '';
199 my $line = "";
8d67621d 200 my $os = "";
4901b570
RS
201 while ( <UNAME> ) {
202 chop;
203 $line = $_ if m@^Release@;
8d67621d 204 $os = $_ if m@^System@;
4901b570
RS
205 }
206 close UNAME;
9e1094ad 207
8d67621d 208 return undef if $line eq '' or $os eq 'System = SunOS';
9e1094ad 209
3eb84c62 210 my @fields = split(/\s+/, $line);
9e1094ad 211 return $fields[2];
4901b570
RS
212}
213
214sub get_sco_type {
215 my $REL = shift;
216
217 if ( -f "/etc/kconfig" ) {
218 return "${MACHINE}-whatever-isc4" if $REL eq '4.0' || $REL eq '4.1';
219 } else {
220 return "whatever-whatever-sco3" if $REL eq '3.2v4.2';
221 return "whatever-whatever-sco5" if $REL =~ m@3\.2v5\.0.*@;
222 if ( $REL eq "4.2MP" ) {
223 return "whatever-whatever-unixware20" if $VERSION =~ m@2\.0.*@;
224 return "whatever-whatever-unixware21" if $VERSION =~ m@2\.1.*@;
225 return "whatever-whatever-unixware2" if $VERSION =~ m@2.*@;
226 }
227 return "whatever-whatever-unixware1" if $REL eq "4.2";
228 if ( $REL =~ m@5.*@ ) {
229 # We hardcode i586 in place of ${MACHINE} for the following
230 # reason: even though Pentium is minimum requirement for
231 # platforms in question, ${MACHINE} gets always assigned to
232 # i386. This means i386 gets passed to Configure, which will
233 # cause bad assembler code to be generated.
234 return "i586-sco-unixware7" if $VERSION =~ m@[678].*@;
235 }
236 }
237}
238
239# Return the cputype-vendor-osversion
240sub guess_system {
69aa579e
RL
241 ($SYSTEM, undef, $RELEASE, $VERSION, $MACHINE) = POSIX::uname();
242 my $sys = "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}";
8d67621d 243
4901b570
RS
244 # Special-cases for ISC, SCO, Unixware
245 my $REL = is_sco_uname();
9e1094ad 246 if ( defined $REL ) {
4901b570 247 my $result = get_sco_type($REL);
019e3a0b 248 return eval "\"$result\"" if $result ne '';
4901b570
RS
249 }
250
251 # Now pattern-match
4901b570
RS
252
253 # Simple cases
bfa68462 254 foreach my $tuple ( @$guess_patterns ) {
4901b570 255 my $pat = @$tuple[0];
bfa68462
RL
256 my $check = ref $pat eq 'CODE' ? $pat->($sys) : $sys =~ /^(${pat})$/;
257 next unless $check;
4901b570 258
bfa68462
RL
259 my $result = @$tuple[1];
260 $result = $result->() if ref $result eq 'CODE';
019e3a0b 261 return eval "\"$result\"";
4901b570
RS
262 }
263
264 # Oh well.
265 return "${MACHINE}-whatever-${SYSTEM}";
266}
267
a3310b18
RL
268# We would use List::Util::pair() for this... unfortunately, that function
269# only appeared in perl v5.19.3, and we claim to support perl v5.10 and on.
270# Therefore, we implement a quick cheap variant of our own.
271sub _pairs (@) {
272 croak "Odd number of arguments" if @_ & 1;
273
274 my @pairlist = ();
275
276 while (@_) {
277 my $x = [ shift, shift ];
278 push @pairlist, $x;
279 }
280 return @pairlist;
281}
282
4901b570
RS
283# Figure out CC, GCCVAR, etc.
284sub determine_compiler_settings {
9e1094ad
RL
285 # Make a copy and don't touch it. That helps determine if we're finding
286 # the compiler here (false), or if it was set by the user (true.
a3310b18
RL
287 my $cc = $CC;
288
289 # Set certain default
290 $CCVER = 0; # Unknown
291 $CCVENDOR = ''; # Dunno, don't care (unless found later)
292
293 # Find a compiler if we don't already have one
294 if ( ! $cc ) {
295 foreach (@c_compilers) {
296 next unless IPC::Cmd::can_run("$CROSS_COMPILE$_");
297 $CC = $_;
298 last;
299 }
300 }
301
9e1094ad
RL
302 if ( $CC ) {
303 # Find the compiler vendor and version number for certain compilers
304 foreach my $pair (_pairs @cc_version) {
305 # Try to get the version number.
306 # Failure gets us undef or an empty string
307 my ( $k, $v ) = @$pair;
308 $v = $v->();
309
310 # If we got a version number, process it
311 if ($v) {
312 $CCVENDOR = $k;
313
314 # The returned version is expected to be one of
315 #
316 # MAJOR
317 # MAJOR.MINOR
318 # MAJOR.MINOR.{whatever}
319 #
320 # We don't care what comes after MAJOR.MINOR. All we need is
321 # to have them calculated into a single number, using this
322 # formula:
323 #
324 # MAJOR * 100 + MINOR
325 # Here are a few examples of what we should get:
326 #
327 # 2.95.1 => 295
328 # 3.1 => 301
329 # 9 => 900
330 my @numbers = split /\./, $v;
331 my @factors = (100, 1);
332 while (@numbers && @factors) {
333 $CCVER += shift(@numbers) * shift(@factors)
334 }
335 last;
a3310b18 336 }
a3310b18
RL
337 }
338 }
339
9e1094ad 340 # Vendor specific overrides, only if we didn't determine the compiler here
a3310b18 341 if ( ! $cc ) {
f627561c
RL
342 if ( $SYSTEM eq 'OpenVMS' ) {
343 my $v = `CC/VERSION NLA0:`;
344 if ($? == 0) {
345 my ($vendor, $version) =
346 ( $v =~ m/^([A-Z]+) C V([0-9\.-]+) on / );
347 my ($major, $minor, $patch) =
348 ( $version =~ m/^([0-9]+)\.([0-9]+)-0*?(0|[1-9][0-9]*)$/ );
349 $CC = 'CC';
350 $CCVENDOR = $vendor;
351 $CCVER = ( $major * 100 + $minor ) * 100 + $patch;
352 }
353 }
354
a3310b18
RL
355 if ( ${SYSTEM} eq 'AIX' ) {
356 # favor vendor cc over gcc
357 if (IPC::Cmd::can_run('cc')) {
358 $CC = 'cc';
359 $CCVENDOR = ''; # Determine later
360 $CCVER = 0;
361 }
362 }
363
364 if ( $SYSTEM eq "SunOS" ) {
8d67621d 365 # check for Oracle Developer Studio, expected output is "cc: blah-blah C x.x blah-blah"
a3310b18 366 my $v = `(cc -V 2>&1) 2>/dev/null | egrep -e '^cc: .* C [0-9]\.[0-9]'`;
8d67621d
JL
367 my @numbers =
368 ( $v =~ m/^.* C ([0-9]+)\.([0-9]+) .*/ );
a3310b18
RL
369 my @factors = (100, 1);
370 $v = 0;
371 while (@numbers && @factors) {
372 $v += shift(@numbers) * shift(@factors)
373 }
374
8d67621d 375 if ($v > 500) {
a3310b18 376 $CC = 'cc';
8d67621d 377 $CCVENDOR = 'sun';
a3310b18 378 $CCVER = $v;
a3310b18 379 }
4901b570
RS
380 }
381 }
382
9e1094ad
RL
383 # If no C compiler has been determined at this point, we die. Hard.
384 die <<_____
385ERROR!
386No C compiler found, please specify one with the environment variable CC,
387or configure with an explicit configuration target.
388_____
389 unless $CC;
390
a3310b18 391 # On some systems, we assume a cc vendor if it's not already determined
4901b570 392
a3310b18
RL
393 if ( ! $CCVENDOR ) {
394 $CCVENDOR = 'aix' if $SYSTEM eq 'AIX';
395 $CCVENDOR = 'sun' if $SYSTEM eq 'SunOS';
396 }
397
398 # Some systems need to know extra details
399
400 if ( $SYSTEM eq "HP-UX" && $CCVENDOR eq 'gnu' ) {
4901b570
RS
401 # By default gcc is a ILP32 compiler (with long long == 64).
402 $GCC_BITS = "32";
a3310b18 403 if ( $CCVER >= 300 ) {
4901b570
RS
404 # PA64 support only came in with gcc 3.0.x.
405 # We check if the preprocessor symbol __LP64__ is defined.
406 if ( okrun('echo __LP64__',
a3310b18
RL
407 "$CC -v -E -x c - 2>/dev/null",
408 'grep "^__LP64__" 2>&1 >/dev/null') ) {
4901b570
RS
409 # __LP64__ has slipped through, it therefore is not defined
410 } else {
411 $GCC_BITS = '64';
412 }
413 }
4901b570
RS
414 }
415
a3310b18
RL
416 if ( $SYSTEM eq "SunOS" && $CCVENDOR eq 'gnu' ) {
417 if ( $CCVER >= 300 ) {
4901b570
RS
418 # 64-bit ABI isn't officially supported in gcc 3.0, but seems
419 # to be working; at the very least 'make test' passes.
a3310b18
RL
420 if ( okrun("$CC -v -E -x c /dev/null 2>&1",
421 'grep __arch64__ >/dev/null') ) {
4901b570
RS
422 $GCC_ARCH = "-m64"
423 } else {
424 $GCC_ARCH = "-m32"
425 }
426 }
a3310b18
RL
427 }
428
429 if ($VERBOSE) {
430 my $vendor = $CCVENDOR ? $CCVENDOR : "(undetermined)";
431 my $version = $CCVER ? $CCVER : "(undetermined)";
432 print "C compiler: $CC\n";
433 print "C compiler vendor: $vendor\n";
434 print "C compiler version: $version\n";
4901b570
RS
435 }
436}
437
e39795af
RL
438my $map_patterns =
439 [ [ 'uClinux.*64.*', { target => 'uClinux-dist64' } ],
440 [ 'uClinux.*', { target => 'uClinux-dist' } ],
441 [ 'mips3-sgi-irix', { target => 'irix-mips3' } ],
442 [ 'mips4-sgi-irix64',
443 sub {
444 print <<EOF;
4901b570
RS
445WARNING! To build 64-bit package, do this:
446 $WHERE/Configure irix64-mips4-$CC
447EOF
e39795af
RL
448 maybe_abort();
449 return { target => "irix-mips3" };
450 }
451 ],
452 [ 'ppc-apple-rhapsody', { target => "rhapsody-ppc" } ],
453 [ 'ppc-apple-darwin.*',
454 sub {
21488844 455 my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
e39795af
RL
456 my $ISA64 = `sysctl -n hw.optional.64bitops 2>/dev/null`;
457 if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
458 print <<EOF;
4901b570
RS
459WARNING! To build 64-bit package, do this:
460 $WHERE/Configure darwin64-ppc-cc
461EOF
e39795af
RL
462 maybe_abort();
463 }
464 return { target => "darwin64-ppc" }
465 if $ISA64 == 1 && $KERNEL_BITS eq '64';
466 return { target => "darwin-ppc" };
4901b570 467 }
e39795af
RL
468 ],
469 [ 'i.86-apple-darwin.*',
470 sub {
21488844 471 my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
e39795af
RL
472 my $ISA64 = `sysctl -n hw.optional.x86_64 2>/dev/null`;
473 if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
474 print <<EOF;
4901b570 475WARNING! To build 64-bit package, do this:
e39795af 476 KERNEL_BITS=64 $WHERE/Configure \[\[ options \]\]
4901b570 477EOF
e39795af
RL
478 maybe_abort();
479 }
480 return { target => "darwin64-x86_64" }
481 if $ISA64 == 1 && $KERNEL_BITS eq '64';
482 return { target => "darwin-i386" };
4901b570 483 }
e39795af
RL
484 ],
485 [ 'x86_64-apple-darwin.*',
486 sub {
21488844 487 my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
e39795af 488 return { target => "darwin-i386" } if $KERNEL_BITS eq '32';
4901b570 489
e39795af 490 print <<EOF;
4901b570 491WARNING! To build 32-bit package, do this:
e39795af 492 KERNEL_BITS=32 $WHERE/Configure \[\[ options \]\]
4901b570 493EOF
e39795af
RL
494 maybe_abort();
495 return { target => "darwin64-x86_64" };
496 }
497 ],
8758f4e6 498 [ 'arm64-apple-darwin.*', { target => "darwin64-arm64" } ],
e39795af
RL
499 [ 'armv6\+7-.*-iphoneos',
500 { target => "iphoneos-cross",
501 cflags => [ qw(-arch armv6 -arch armv7) ],
502 cxxflags => [ qw(-arch armv6 -arch armv7) ] }
503 ],
504 [ 'arm64-.*-iphoneos|.*-.*-ios64',
505 { target => "ios64-cross" }
506 ],
507 [ '.*-.*-iphoneos',
508 sub { return { target => "iphoneos-cross",
509 cflags => [ "-arch ${MACHINE}" ],
510 cxxflags => [ "-arch ${MACHINE}" ] }; }
511 ],
512 [ 'alpha-.*-linux2.*',
513 sub {
514 my $ISA = `awk '/cpu model/{print \$4;exit(0);}' /proc/cpuinfo`;
515 $ISA //= 'generic';
516 my %config = ();
517 if ( $CCVENDOR eq "gnu" ) {
518 if ( $ISA =~ 'EV5|EV45' ) {
519 %config = ( cflags => [ '-mcpu=ev5' ],
520 cxxflags => [ '-mcpu=ev5' ] );
521 } elsif ( $ISA =~ 'EV56|PCA56' ) {
522 %config = ( cflags => [ '-mcpu=ev56' ],
523 cxxflags => [ '-mcpu=ev56' ] );
524 } else {
525 %config = ( cflags => [ '-mcpu=ev6' ],
526 cxxflags => [ '-mcpu=ev6' ] );
527 }
4901b570 528 }
e39795af
RL
529 return { target => "linux-alpha",
530 %config };
4901b570 531 }
e39795af
RL
532 ],
533 [ 'ppc64-.*-linux2',
534 sub {
21488844 535 my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
e39795af
RL
536 if ( $KERNEL_BITS eq '' ) {
537 print <<EOF;
4901b570
RS
538WARNING! To build 64-bit package, do this:
539 $WHERE/Configure linux-ppc64
540EOF
e39795af
RL
541 maybe_abort();
542 }
543 return { target => "linux-ppc64" } if $KERNEL_BITS eq '64';
544
545 my %config = ();
546 if (!okrun('echo __LP64__',
547 'gcc -E -x c - 2>/dev/null',
548 'grep "^__LP64__" 2>&1 >/dev/null') ) {
549 %config = ( cflags => [ '-m32' ],
550 cxxflags => [ '-m32' ] );
551 }
552 return { target => "linux-ppc",
553 %config };
4901b570 554 }
e39795af
RL
555 ],
556 [ 'ppc64le-.*-linux2', { target => "linux-ppc64le" } ],
557 [ 'ppc-.*-linux2', { target => "linux-ppc" } ],
558 [ 'mips64.*-*-linux2',
559 sub {
560 print <<EOF;
4901b570
RS
561WARNING! To build 64-bit package, do this:
562 $WHERE/Configure linux64-mips64
563EOF
e39795af
RL
564 maybe_abort();
565 return { target => "linux-mips64" };
566 }
567 ],
568 [ 'mips.*-.*-linux2', { target => "linux-mips32" } ],
569 [ 'ppc60x-.*-vxworks.*', { target => "vxworks-ppc60x" } ],
570 [ 'ppcgen-.*-vxworks.*', { target => "vxworks-ppcgen" } ],
571 [ 'pentium-.*-vxworks.*', { target => "vxworks-pentium" } ],
572 [ 'simlinux-.*-vxworks.*', { target => "vxworks-simlinux" } ],
573 [ 'mips-.*-vxworks.*', { target => "vxworks-mips" } ],
574 [ 'e2k-.*-linux.*', { target => "linux-generic64",
575 defines => [ 'L_ENDIAN' ] } ],
576 [ 'ia64-.*-linux.', { target => "linux-ia64" } ],
577 [ 'sparc64-.*-linux2',
578 sub {
579 print <<EOF;
4901b570
RS
580WARNING! If you *know* that your GNU C supports 64-bit/V9 ABI and you
581 want to build 64-bit library, do this:
582 $WHERE/Configure linux64-sparcv9
583EOF
e39795af
RL
584 maybe_abort();
585 return { target => "linux-sparcv9" };
586 }
587 ],
588 [ 'sparc-.*-linux2',
589 sub {
590 my $KARCH = `awk '/^type/{print \$3;exit(0);}' /proc/cpuinfo`;
591 $KARCH //= "sun4";
592 return { target => "linux-sparcv9" } if $KARCH =~ 'sun4u.*';
593 return { target => "linux-sparcv8" } if $KARCH =~ 'sun4[md]';
594 return { target => "linux-generic32",
595 defines => [ 'L_ENDIAN' ] };
596 }
597 ],
598 [ 'parisc.*-.*-linux2',
599 sub {
600 # 64-bit builds under parisc64 linux are not supported and
601 # compiler is expected to generate 32-bit objects...
602 my $CPUARCH =
603 `awk '/cpu family/{print substr(\$5,1,3); exit(0);}' /proc/cpuinfo`;
604 my $CPUSCHEDULE =
605 `awk '/^cpu.[ ]*: PA/{print substr(\$3,3); exit(0);}' /proc/cpuinfo`;
606 # TODO XXX Model transformations
607 # 0. CPU Architecture for the 1.1 processor has letter suffixes.
608 # We strip that off assuming no further arch. identification
609 # will ever be used by GCC.
610 # 1. I'm most concerned about whether is a 7300LC is closer to a
611 # 7100 versus a 7100LC.
612 # 2. The variant 64-bit processors cause concern should GCC support
613 # explicit schedulers for these chips in the future.
614 # PA7300LC -> 7100LC (1.1)
615 # PA8200 -> 8000 (2.0)
616 # PA8500 -> 8000 (2.0)
617 # PA8600 -> 8000 (2.0)
618 $CPUSCHEDULE =~ s/7300LC/7100LC/;
619 $CPUSCHEDULE =~ s/8.00/8000/;
620 return
621 { target => "linux-generic32",
622 defines => [ 'B_ENDIAN' ],
623 cflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ],
624 cxxflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ]
625 };
626 }
627 ],
628 [ 'armv[1-3].*-.*-linux2', { target => "linux-generic32" } ],
629 [ 'armv[7-9].*-.*-linux2', { target => "linux-armv4",
e39795af
RL
630 cflags => [ '-march=armv7-a' ],
631 cxxflags => [ '-march=armv7-a' ] } ],
632 [ 'arm.*-.*-linux2', { target => "linux-armv4" } ],
633 [ 'aarch64-.*-linux2', { target => "linux-aarch64" } ],
634 [ 'sh.*b-.*-linux2', { target => "linux-generic32",
635 defines => [ 'B_ENDIAN' ] } ],
636 [ 'sh.*-.*-linux2', { target => "linux-generic32",
637 defines => [ 'L_ENDIAN' ] } ],
638 [ 'm68k.*-.*-linux2', { target => "linux-generic32",
639 defines => [ 'B_ENDIAN' ] } ],
640 [ 's390-.*-linux2', { target => "linux-generic32",
641 defines => [ 'B_ENDIAN' ] } ],
642 [ 's390x-.*-linux2',
643 sub {
644 # Disabled until a glibc bug is fixed; see Configure.
645 if (0
646 || okrun('egrep -e \'^features.* highgprs\' /proc/cpuinfo >/dev/null') )
647 {
648 print <<EOF;
4901b570
RS
649WARNING! To build "highgprs" 32-bit package, do this:
650 $WHERE/Configure linux32-s390x
651EOF
e39795af
RL
652 maybe_abort();
653 }
654 return { target => "linux64-s390x" };
4901b570 655 }
e39795af
RL
656 ],
657 [ 'x86_64-.*-linux.',
658 sub {
659 return { target => "linux-x32" }
660 if okrun("$CC -dM -E -x c /dev/null 2>&1",
661 'grep -q ILP32 >/dev/null');
662 return { target => "linux-x86_64" };
663 }
664 ],
665 [ '.*86-.*-linux2',
666 sub {
667 # On machines where the compiler understands -m32, prefer a
668 # config target that uses it
669 return { target => "linux-x86" }
670 if okrun("$CC -m32 -E -x c /dev/null >/dev/null 2>&1");
671 return { target => "linux-elf" };
672 }
673 ],
674 [ '.*86-.*-linux1', { target => "linux-aout" } ],
e6760e3e 675 [ 'riscv64-.*-linux.', { target => "linux64-riscv64" } ],
e39795af
RL
676 [ '.*-.*-linux.', { target => "linux-generic32" } ],
677 [ 'sun4[uv].*-.*-solaris2',
678 sub {
679 my $KERNEL_BITS = $ENV{KERNEL_BITS};
680 my $ISA64 = `isainfo 2>/dev/null | grep sparcv9`;
8d67621d
JL
681 my $KB = $KERNEL_BITS // '64';
682 if ( $ISA64 ne "" && $KB eq '64' ) {
e39795af
RL
683 if ( $CCVENDOR eq "sun" && $CCVER >= 500 ) {
684 print <<EOF;
8d67621d
JL
685WARNING! To build 32-bit package, do this:
686 $WHERE/Configure solaris-sparcv9-cc
4901b570 687EOF
e39795af
RL
688 maybe_abort();
689 } elsif ( $CCVENDOR eq "gnu" && $GCC_ARCH eq "-m64" ) {
690 # $GCC_ARCH denotes default ABI chosen by compiler driver
691 # (first one found on the $PATH). I assume that user
692 # expects certain consistency with the rest of his builds
693 # and therefore switch over to 64-bit. <appro>
694 print <<EOF;
4901b570
RS
695WARNING! To build 32-bit package, do this:
696 $WHERE/Configure solaris-sparcv9-gcc
697EOF
e39795af 698 maybe_abort();
8d67621d 699 return { target => "solaris64-sparcv9-gcc" };
e39795af
RL
700 } elsif ( $GCC_ARCH eq "-m32" ) {
701 print <<EOF;
4901b570
RS
702NOTICE! If you *know* that your GNU C supports 64-bit/V9 ABI and you wish
703 to build 64-bit library, do this:
704 $WHERE/Configure solaris64-sparcv9-gcc
705EOF
e39795af
RL
706 maybe_abort();
707 }
4901b570 708 }
8d67621d
JL
709 return { target => "solaris64-sparcv9-cc" }
710 if $ISA64 ne "" && $KB eq '64';
711 return { target => "solaris-sparcv9-cc" };
4901b570 712 }
e39795af
RL
713 ],
714 [ 'sun4m-.*-solaris2', { target => "solaris-sparcv8" } ],
715 [ 'sun4d-.*-solaris2', { target => "solaris-sparcv8" } ],
716 [ 'sun4.*-.*-solaris2', { target => "solaris-sparcv7" } ],
717 [ '.*86.*-.*-solaris2',
718 sub {
719 my $KERNEL_BITS = $ENV{KERNEL_BITS};
720 my $ISA64 = `isainfo 2>/dev/null | grep amd64`;
721 my $KB = $KERNEL_BITS // '64';
5800d041
MC
722 if ($ISA64 ne "" && $KB eq '64') {
723 return { target => "solaris64-x86_64-gcc" } if $CCVENDOR eq "gnu";
724 return { target => "solaris64-x86_64-cc" };
725 }
e39795af
RL
726 my $REL = uname('-r');
727 $REL =~ s/5\.//;
728 my @tmp_disable = ();
729 push @tmp_disable, 'sse2' if int($REL) < 10;
5800d041
MC
730 #There is no solaris-x86-cc target
731 return { target => "solaris-x86-gcc",
e39795af 732 disable => [ @tmp_disable ] };
4901b570 733 }
e39795af
RL
734 ],
735 # We don't have any sunos target in Configurations/*.conf, so why here?
736 [ '.*-.*-sunos4', { target => "sunos" } ],
737 [ '.*86.*-.*-bsdi4', { target => "BSD-x86-elf",
738 lflags => [ '-ldl' ],
739 disable => [ 'sse2' ] } ],
740 [ 'alpha.*-.*-.*bsd.*', { target => "BSD-generic64",
741 defines => [ 'L_ENDIAN' ] } ],
742 [ 'powerpc64-.*-.*bsd.*', { target => "BSD-generic64",
743 defines => [ 'B_ENDIAN' ] } ],
744 [ 'sparc64-.*-.*bsd.*', { target => "BSD-sparc64" } ],
745 [ 'ia64-.*-.*bsd.*', { target => "BSD-ia64" } ],
746 [ 'x86_64-.*-dragonfly.*', { target => "BSD-x86_64" } ],
747 [ 'amd64-.*-.*bsd.*', { target => "BSD-x86_64" } ],
748 [ '.*86.*-.*-.*bsd.*',
749 sub {
750 # mimic ld behaviour when it's looking for libc...
751 my $libc;
752 if ( -l "/usr/lib/libc.so" ) {
753 $libc = "/usr/lib/libc.so";
754 } else {
755 # ld searches for highest libc.so.* and so do we
756 $libc =
757 `(ls /usr/lib/libc.so.* /lib/libc.so.* | tail -1) 2>/dev/null`;
758 }
759 my $what = `file -L $libc 2>/dev/null`;
760 return { target => "BSD-x86-elf" } if $what =~ /ELF/;
761 return { target => "BSD-x86",
762 disable => [ 'sse2' ] };
4901b570 763 }
e39795af
RL
764 ],
765 [ '.*-.*-.*bsd.*', { target => "BSD-generic32" } ],
766 [ 'x86_64-.*-haiku', { target => "haiku-x86_64" } ],
767 [ '.*-.*-haiku', { target => "haiku-x86" } ],
768 [ '.*-.*-osf', { target => "osf1-alpha" } ],
769 [ '.*-.*-tru64', { target => "tru64-alpha" } ],
770 [ '.*-.*-[Uu]nix[Ww]are7',
771 sub {
772 return { target => "unixware-7",
773 disable => [ 'sse2' ] } if $CCVENDOR eq "gnu";
774 return { target => "unixware-7",
775 defines => [ '__i386__' ] };
776 }
777 ],
778 [ '.*-.*-[Uu]nix[Ww]are20.*', { target => "unixware-2.0",
779 disable => [ 'sse2', 'sha512' ] } ],
780 [ '.*-.*-[Uu]nix[Ww]are21.*', { target => "unixware-2.1",
781 disable => [ 'sse2', 'sha512' ] } ],
782 [ '.*-.*-vos', { target => "vos",
783 disable => [ 'threads', 'shared', 'asm',
784 'dso' ] } ],
785 [ 'BS2000-siemens-sysv4', { target => "BS2000-OSD" } ],
786 [ 'i[3456]86-.*-cygwin', { target => "Cygwin-x86" } ],
787 [ '.*-.*-cygwin',
788 sub { return { target => "Cygwin-${MACHINE}" } } ],
789 [ 'x86-.*-android|i.86-.*-android', { target => "android-x86" } ],
790 [ 'armv[7-9].*-.*-android', { target => "android-armeabi",
791 cflags => [ '-march=armv7-a' ],
792 cxxflags => [ '-march=armv7-a' ] } ],
793 [ 'arm.*-.*-android', { target => "android-armeabi" } ],
794 [ '.*-hpux1.*',
795 sub {
796 my $KERNEL_BITS = $ENV{KERNEL_BITS};
797 my %common_return = ( defines => [ '_REENTRANT' ] );
798 $KERNEL_BITS ||= `getconf KERNEL_BITS 2>/dev/null` // '32';
799 # See <sys/unistd.h> for further info on CPU_VERSION.
800 my $CPU_VERSION = `getconf CPU_VERSION 2>/dev/null` // 0;
801 if ( $CPU_VERSION >= 768 ) {
802 # IA-64 CPU
803 return { target => "hpux64-ia64",
804 %common_return }
805 if $KERNEL_BITS eq '64' && ! $CCVENDOR;
806 return { target => "hpux-ia64",
807 %common_return };
808 }
809 if ( $CPU_VERSION >= 532 ) {
810 # PA-RISC 2.x CPU
811 # PA-RISC 2.0 is no longer supported as separate 32-bit
812 # target. This is compensated for by run-time detection
813 # in most critical assembly modules and taking advantage
814 # of 2.0 architecture in PA-RISC 1.1 build.
815 my $target = ($CCVENDOR eq "gnu" && $GCC_BITS eq '64')
816 ? "hpux64-parisc2"
817 : "hpux-parisc1_1";
818 if ( $KERNEL_BITS eq '64' && ! $CCVENDOR ) {
819 print <<EOF;
4901b570
RS
820WARNING! To build 64-bit package, do this:
821 $WHERE/Configure hpux64-parisc2-cc
822EOF
e39795af
RL
823 maybe_abort();
824 }
825 return { target => $target,
826 %common_return };
4901b570 827 }
e39795af
RL
828 # PA-RISC 1.1+ CPU?
829 return { target => "hpux-parisc1_1",
830 %common_return } if $CPU_VERSION >= 528;
831 # PA-RISC 1.0 CPU
832 return { target => "hpux-parisc",
833 %common_return } if $CPU_VERSION >= 523;
834 # Motorola(?) CPU
835 return { target => "hpux",
836 %common_return };
4901b570 837 }
e39795af
RL
838 ],
839 [ '.*-hpux', { target => "hpux-parisc" } ],
840 [ '.*-aix',
841 sub {
842 my %config = ();
843 my $KERNEL_BITS = $ENV{KERNEL_BITS};
844 $KERNEL_BITS ||= `getconf KERNEL_BITMODE 2>/dev/null`;
845 $KERNEL_BITS ||= '32';
846 my $OBJECT_MODE = $ENV{OBJECT_MODE};
847 $OBJECT_MODE ||= 32;
848 $config{target} = "aix";
4901b570
RS
849 if ( $OBJECT_MODE == 64 ) {
850 print 'Your $OBJECT_MODE was found to be set to 64';
e39795af
RL
851 $config{target} = "aix64";
852 } else {
853 if ( $CCVENDOR ne 'gnu' && $KERNEL_BITS eq '64' ) {
854 print <<EOF;
4901b570
RS
855WARNING! To build 64-bit package, do this:
856 $WHERE/Configure aix64-cc
857EOF
e39795af
RL
858 maybe_abort();
859 }
4901b570 860 }
e39795af 861 if ( okrun(
52f5407d 862 "(lsattr -E -O -l `lsdev -c processor|awk '{print \$1;exit}'`",
e39795af
RL
863 'grep -i powerpc) >/dev/null 2>&1') ) {
864 # this applies even to Power3 and later, as they return
865 # PowerPC_POWER[345]
866 } else {
867 $config{disable} = [ 'asm' ];
868 }
869 return %config;
4901b570 870 }
e39795af 871 ],
b2bed3c6
RL
872
873 # Windows values found by looking at Perl 5's win32/win32.c
874 [ 'amd64-.*?-Windows NT', { target => 'VC-WIN64A' } ],
875 [ 'ia64-.*?-Windows NT', { target => 'VC-WIN64I' } ],
876 [ 'x86-.*?-Windows NT', { target => 'VC-WIN32' } ],
877
878 # VMS values found by observation on existing machinery.
879 # Unfortunately, the machine part is a bit... overdone. It seems,
880 # though, that 'Alpha' exists in that part for Alphas, making it
881 # distinguishable from Itanium. It will be interesting to see what
882 # we'll get in the upcoming x86_64 port...
883 [ '.*Alpha.*?-.*?-OpenVMS', { target => 'vms-alpha' } ],
884 [ '.*?-.*?-OpenVMS', { target => 'vms-ia64' } ],
885
4232a9e5
RL
886 # TODO: There are a few more choices among OpenSSL config targets, but
887 # reaching them involves a bit more than just a host tripet. Select
888 # environment variables could do the job to cover for more granular
889 # build options such as data model (ILP32 or LP64), thread support
890 # model (PUT, SPT or nothing), target execution environment (OSS or
891 # GUARDIAN). And still, there must be some kind of default when
892 # nothing else is said.
893 #
894 # nsv is a virtual x86 environment, equivalent to nsx, so we enforce
895 # the latter.
896 [ 'nse-tandem-nsk.*', { target => 'nonstop-nse' } ],
897 [ 'nsv-tandem-nsk.*', { target => 'nonstop-nsx' } ],
898 [ 'nsx-tandem-nsk.*', { target => 'nonstop-nsx' } ],
899
e39795af
RL
900 ];
901
902# Map GUESSOS into OpenSSL terminology.
903# Returns a hash table with diverse entries, most importantly 'target',
904# but also other entries that are fitting for Configure's %config
905# and MACHINE.
906# It would be nice to fix this so that this weren't necessary. :( XXX
907sub map_guess {
908 my $GUESSOS = shift;
909
910 foreach my $tuple ( @$map_patterns ) {
911 my $pat = @$tuple[0];
912 next if $GUESSOS !~ /^${pat}$/;
913 my $result = @$tuple[1];
914 $result = $result->() if ref $result eq 'CODE';
915 return %$result;
4901b570
RS
916 }
917
918 # Last case, return "z" from x-y-z
919 my @fields = split(/-/, $GUESSOS);
a3310b18 920 return ( target => $fields[2] );
4901b570
RS
921}
922
923# gcc < 2.8 does not support -march=ultrasparc
924sub check_solaris_sparc8 {
925 my $OUT = shift;
a3310b18
RL
926 if ( $CCVENDOR eq 'gnu' && $CCVER < 208 ) {
927 if ( $OUT eq 'solaris-sparcv9-gcc' ) {
928 print <<EOF;
4901b570
RS
929WARNING! Downgrading to solaris-sparcv8-gcc
930 Upgrade to gcc-2.8 or later.
931EOF
a3310b18
RL
932 maybe_abort();
933 return 'solaris-sparcv8-gcc';
934 }
935 if ( $OUT eq "linux-sparcv9" ) {
936 print <<EOF;
4901b570
RS
937WARNING! Downgrading to linux-sparcv8
938 Upgrade to gcc-2.8 or later.
939EOF
a3310b18
RL
940 maybe_abort();
941 return 'linux-sparcv8';
942 }
4901b570
RS
943 }
944 return $OUT;
945}
946
4901b570
RS
947###
948### MAIN PROCESSING
949###
950
4901b570 951sub get_platform {
e39795af
RL
952 my %options = @_;
953
4901b570 954 $VERBOSE = 1 if defined $options{verbose};
4901b570 955 $WAIT = 0 if defined $options{nowait};
e39795af
RL
956 $CC = $options{CC};
957 $CROSS_COMPILE = $options{CROSS_COMPILE} // '';
4901b570 958
e39795af
RL
959 my $GUESSOS = guess_system();
960 determine_compiler_settings();
4901b570 961
e39795af
RL
962 my %ret = map_guess($GUESSOS);
963 $ret{target} = check_solaris_sparc8($ret{target});
964 return %ret;
4901b570
RS
965}
966
9671;