Replace CONFIG_NOWAIT env var with -w option
[openssl.git] / util / perl / OpenSSL / config.pm
1 #! /usr/bin/env perl
2 # Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
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
12 package OpenSSL::config;
13
14 use strict;
15 use warnings;
16 use Getopt::Std;
17 use File::Basename;
18 use IPC::Cmd;
19 use POSIX;
20 use Carp;
21
22 # These control our behavior.
23 my $DRYRUN;
24 my $VERBOSE;
25 my $WHERE = dirname($0);
26 my $WAIT = 1;
27
28 # Machine type, etc., used to determine the platform
29 my $MACHINE;
30 my $RELEASE;
31 my $SYSTEM;
32 my $VERSION;
33 my $CCVENDOR;
34 my $CCVER;
35 my $GCC_BITS;
36 my $GCC_ARCH;
37
38 # Some environment variables; they will affect Configure
39 my $CONFIG_OPTIONS = $ENV{CONFIG_OPTIONS} // '';
40 my $CC;
41 my $CROSS_COMPILE;
42
43 # For determine_compiler_settings, the list of known compilers
44 my @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
49 my @cc_version =
50     (
51      clang => sub {
52          my $v = `$CROSS_COMPILE$CC -v 2>&1`;
53          $v =~ m/(?:(?:clang|LLVM) version|.*based on LLVM)\s+([0-9]+\.[0-9]+)/;
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
65 # This is what we will set as the target for calling Configure.
66 my $options = '';
67
68 # Pattern matches against "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}"
69 # The patterns are assumed to be wrapped like this: /^(${pattern})$/
70 my $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' ],
91     # BSD/OS always says 386
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' ],
143     [ 'Darwin:.*',                  '${MACHINE}-apple-darwin' ],
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' ],
161
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
167     [ sub { -d '/usr/apollo' },     'whatever-apollo-whatever' ],
168 ];
169
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(...."
173 sub 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.
180 sub 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
194 # Look for ISC/SCO with its unique uname program
195 sub is_sco_uname {
196     return undef unless IPC::Cmd::can_run('uname');
197
198     open UNAME, "uname -X 2>/dev/null|" or return '';
199     my $line = "";
200     my $os = "";
201     while ( <UNAME> ) {
202         chop;
203         $line = $_ if m@^Release@;
204         $os = $_ if m@^System@;
205     }
206     close UNAME;
207
208     return undef if $line eq '' or $os eq 'System = SunOS';
209
210     my @fields = split(/\s+/, $line);
211     return $fields[2];
212 }
213
214 sub 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
240 sub guess_system {
241     ($SYSTEM, undef, $RELEASE, $VERSION, $MACHINE) = POSIX::uname();
242     my $sys = "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}";
243     
244     # Special-cases for ISC, SCO, Unixware
245     my $REL = is_sco_uname();
246     if ( defined $REL ) {
247         my $result = get_sco_type($REL);
248         return eval "\"$result\"" if $result ne '';
249     }
250
251     # Now pattern-match
252
253     # Simple cases
254     foreach my $tuple ( @$guess_patterns ) {
255         my $pat = @$tuple[0];
256         my $check = ref $pat eq 'CODE' ? $pat->($sys) : $sys =~ /^(${pat})$/;
257         next unless $check;
258
259         my $result = @$tuple[1];
260         $result = $result->() if ref $result eq 'CODE';
261         return eval "\"$result\"";
262     }
263
264     # Oh well.
265     return "${MACHINE}-whatever-${SYSTEM}";
266 }
267
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.
271 sub _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
283 # Figure out CC, GCCVAR, etc.
284 sub determine_compiler_settings {
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.
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
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;
336             }
337         }
338     }
339
340     # Vendor specific overrides, only if we didn't determine the compiler here
341     if ( ! $cc ) {
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
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" ) {
365             # check for Oracle Developer Studio, expected output is "cc: blah-blah C x.x blah-blah"
366             my $v = `(cc -V 2>&1) 2>/dev/null | egrep -e '^cc: .* C [0-9]\.[0-9]'`;
367             my @numbers = 
368                     ( $v =~ m/^.* C ([0-9]+)\.([0-9]+) .*/ );
369             my @factors = (100, 1);
370             $v = 0;
371             while (@numbers && @factors) {
372                 $v += shift(@numbers) * shift(@factors)
373             }
374
375             if ($v > 500) {
376                 $CC = 'cc';
377                 $CCVENDOR = 'sun';
378                 $CCVER = $v;
379             }
380         }
381     }
382
383     # If no C compiler has been determined at this point, we die.  Hard.
384     die <<_____
385 ERROR!
386 No C compiler found, please specify one with the environment variable CC,
387 or configure with an explicit configuration target.
388 _____
389         unless $CC;
390
391     # On some systems, we assume a cc vendor if it's not already determined
392
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' ) {
401         # By default gcc is a ILP32 compiler (with long long == 64).
402         $GCC_BITS = "32";
403         if ( $CCVER >= 300 ) {
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__',
407                        "$CC -v -E -x c - 2>/dev/null",
408                        'grep "^__LP64__" 2>&1 >/dev/null') ) {
409                 # __LP64__ has slipped through, it therefore is not defined
410             } else {
411                 $GCC_BITS = '64';
412             }
413         }
414     }
415
416     if ( $SYSTEM eq "SunOS" && $CCVENDOR eq 'gnu' ) {
417         if ( $CCVER >= 300 ) {
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.
420             if ( okrun("$CC -v -E -x c /dev/null 2>&1",
421                        'grep __arch64__ >/dev/null') ) {
422                 $GCC_ARCH = "-m64"
423             } else {
424                 $GCC_ARCH = "-m32"
425             }
426         }
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";
435     }
436 }
437
438 my $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;
445 WARNING! To build 64-bit package, do this:
446          $WHERE/Configure irix64-mips4-$CC
447 EOF
448             maybe_abort();
449             return { target => "irix-mips3" };
450         }
451       ],
452       [ 'ppc-apple-rhapsody',     { target => "rhapsody-ppc" } ],
453       [ 'ppc-apple-darwin.*',
454         sub {
455             my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
456             my $ISA64 = `sysctl -n hw.optional.64bitops 2>/dev/null`;
457             if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
458                 print <<EOF;
459 WARNING! To build 64-bit package, do this:
460          $WHERE/Configure darwin64-ppc-cc
461 EOF
462                 maybe_abort();
463             }
464             return { target => "darwin64-ppc" }
465                 if $ISA64 == 1 && $KERNEL_BITS eq '64';
466             return { target => "darwin-ppc" };
467         }
468       ],
469       [ 'i.86-apple-darwin.*',
470         sub {
471             my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
472             my $ISA64 = `sysctl -n hw.optional.x86_64 2>/dev/null`;
473             if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
474                 print <<EOF;
475 WARNING! To build 64-bit package, do this:
476          KERNEL_BITS=64 $WHERE/Configure \[\[ options \]\]
477 EOF
478                 maybe_abort();
479             }
480             return { target => "darwin64-x86_64" }
481                 if $ISA64 == 1 && $KERNEL_BITS eq '64';
482             return { target => "darwin-i386" };
483         }
484       ],
485       [ 'x86_64-apple-darwin.*',
486         sub {
487             my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
488             return { target => "darwin-i386" } if $KERNEL_BITS eq '32';
489
490             print <<EOF;
491 WARNING! To build 32-bit package, do this:
492          KERNEL_BITS=32 $WHERE/Configure \[\[ options \]\]
493 EOF
494             maybe_abort();
495             return { target => "darwin64-x86_64" };
496         }
497       ],
498       [ 'arm64-apple-darwin.*', { target => "darwin64-arm64" } ],
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                 }
528             }
529             return { target => "linux-alpha",
530                      %config };
531         }
532       ],
533       [ 'ppc64-.*-linux2',
534         sub {
535             my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
536             if ( $KERNEL_BITS eq '' ) {
537                 print <<EOF;
538 WARNING! To build 64-bit package, do this:
539          $WHERE/Configure linux-ppc64
540 EOF
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 };
554         }
555       ],
556       [ 'ppc64le-.*-linux2',      { target => "linux-ppc64le" } ],
557       [ 'ppc-.*-linux2',          { target => "linux-ppc" } ],
558       [ 'mips64.*-*-linux2',
559         sub {
560             print <<EOF;
561 WARNING! To build 64-bit package, do this:
562          $WHERE/Configure linux64-mips64
563 EOF
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;
580 WARNING! 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
583 EOF
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",
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;
649 WARNING! To build "highgprs" 32-bit package, do this:
650          $WHERE/Configure linux32-s390x
651 EOF
652                     maybe_abort();
653                 }
654             return { target => "linux64-s390x" };
655         }
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" } ],
675       [ 'riscv64-.*-linux.',      { target => "linux64-riscv64" } ],
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`;
681             my $KB = $KERNEL_BITS // '64';
682             if ( $ISA64 ne "" && $KB eq '64' ) {
683                 if ( $CCVENDOR eq "sun" && $CCVER >= 500 ) {
684                     print <<EOF;
685 WARNING! To build 32-bit package, do this:
686          $WHERE/Configure solaris-sparcv9-cc
687 EOF
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;
695 WARNING! To build 32-bit package, do this:
696          $WHERE/Configure solaris-sparcv9-gcc
697 EOF
698                     maybe_abort();
699                     return { target => "solaris64-sparcv9-gcc" };
700                 } elsif ( $GCC_ARCH eq "-m32" ) {
701                     print <<EOF;
702 NOTICE! 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
705 EOF
706                     maybe_abort();
707                 }
708             }
709             return { target => "solaris64-sparcv9-cc" }
710                 if $ISA64 ne "" && $KB eq '64';
711             return { target => "solaris-sparcv9-cc" };
712         }
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';
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             }
726             my $REL = uname('-r');
727             $REL =~ s/5\.//;
728             my @tmp_disable = ();
729             push @tmp_disable, 'sse2' if int($REL) < 10;
730             #There is no solaris-x86-cc target
731             return { target => "solaris-x86-gcc",
732                      disable => [ @tmp_disable ] };
733         }
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' ] };
763         }
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;
820 WARNING! To build 64-bit package, do this:
821          $WHERE/Configure hpux64-parisc2-cc
822 EOF
823                     maybe_abort();
824                 }
825                 return { target => $target,
826                          %common_return };
827             }
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 };
837         }
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";
849             if ( $OBJECT_MODE == 64 ) {
850                 print 'Your $OBJECT_MODE was found to be set to 64';
851                 $config{target} = "aix64";
852             } else {
853                 if ( $CCVENDOR ne 'gnu' && $KERNEL_BITS eq '64' ) {
854                     print <<EOF;
855 WARNING! To build 64-bit package, do this:
856          $WHERE/Configure aix64-cc
857 EOF
858                     maybe_abort();
859                 }
860             }
861             if ( okrun(
862                        "(lsattr -E -O -l `lsdev -c processor|awk '{print \$1;exit}'`",
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;
870         }
871       ],
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
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
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
907 sub 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;
916     }
917
918     # Last case, return "z" from x-y-z
919     my @fields = split(/-/, $GUESSOS);
920     return ( target => $fields[2] );
921 }
922
923 # gcc < 2.8 does not support -march=ultrasparc
924 sub check_solaris_sparc8 {
925     my $OUT = shift;
926     if ( $CCVENDOR eq 'gnu' && $CCVER < 208 ) {
927         if ( $OUT eq 'solaris-sparcv9-gcc' ) {
928             print <<EOF;
929 WARNING! Downgrading to solaris-sparcv8-gcc
930          Upgrade to gcc-2.8 or later.
931 EOF
932             maybe_abort();
933             return 'solaris-sparcv8-gcc';
934         }
935         if ( $OUT eq "linux-sparcv9" ) {
936             print <<EOF;
937 WARNING! Downgrading to linux-sparcv8
938          Upgrade to gcc-2.8 or later.
939 EOF
940             maybe_abort();
941             return 'linux-sparcv8';
942         }
943     }
944     return $OUT;
945 }
946
947 ###
948 ###   MAIN PROCESSING
949 ###
950
951 sub get_platform {
952     my %options = @_;
953
954     $VERBOSE = 1 if defined $options{verbose};
955     $WAIT = 0 if defined $options{nowait};
956     $CC = $options{CC};
957     $CROSS_COMPILE = $options{CROSS_COMPILE} // '';
958
959     my $GUESSOS = guess_system();
960     determine_compiler_settings();
961
962     my %ret = map_guess($GUESSOS);
963     $ret{target} = check_solaris_sparc8($ret{target});
964     return %ret;
965 }
966
967 1;