Force macOS 10.15 or later to be 64-bit
[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             # macOS >= 10.15 is 64-bit only
489             my $SW_VERS = `sw_vers -productVersion 2>/dev/null`;
490             if ($SW_VERS =~ /^(\d+)\.(\d+)\.(\d+)$/) {
491                 if ($1 > 10 || ($1 == 10 && $2 >= 15)) {
492                     die "32-bit applications not supported on macOS 10.15 or later\n" if $KERNEL_BITS eq '32';
493                     return { target => "darwin64-x86_64" };
494                 }
495             }
496             return { target => "darwin-i386" } if $KERNEL_BITS eq '32';
497
498             print <<EOF;
499 WARNING! To build 32-bit package, do this:
500          KERNEL_BITS=32 $WHERE/Configure \[\[ options \]\]
501 EOF
502             maybe_abort();
503             return { target => "darwin64-x86_64" };
504         }
505       ],
506       [ 'arm64-apple-darwin.*', { target => "darwin64-arm64" } ],
507       [ 'armv6\+7-.*-iphoneos',
508         { target => "iphoneos-cross",
509           cflags => [ qw(-arch armv6 -arch armv7) ],
510           cxxflags => [ qw(-arch armv6 -arch armv7) ] }
511       ],
512       [ 'arm64-.*-iphoneos|.*-.*-ios64',
513         { target => "ios64-cross" }
514       ],
515       [ '.*-.*-iphoneos',
516         sub { return { target => "iphoneos-cross",
517                        cflags => [ "-arch ${MACHINE}" ],
518                        cxxflags => [ "-arch ${MACHINE}" ] }; }
519       ],
520       [ 'alpha-.*-linux2.*',
521         sub {
522             my $ISA = `awk '/cpu model/{print \$4;exit(0);}' /proc/cpuinfo`;
523             $ISA //= 'generic';
524             my %config = ();
525             if ( $CCVENDOR eq "gnu" ) {
526                 if ( $ISA =~ 'EV5|EV45' ) {
527                     %config = ( cflags => [ '-mcpu=ev5' ],
528                                 cxxflags =>  [ '-mcpu=ev5' ] );
529                 } elsif ( $ISA =~ 'EV56|PCA56' ) {
530                     %config = ( cflags => [ '-mcpu=ev56' ],
531                                 cxxflags =>  [ '-mcpu=ev56' ] );
532                 } else {
533                     %config = ( cflags => [ '-mcpu=ev6' ],
534                                 cxxflags =>  [ '-mcpu=ev6' ] );
535                 }
536             }
537             return { target => "linux-alpha",
538                      %config };
539         }
540       ],
541       [ 'ppc64-.*-linux2',
542         sub {
543             my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
544             if ( $KERNEL_BITS eq '' ) {
545                 print <<EOF;
546 WARNING! To build 64-bit package, do this:
547          $WHERE/Configure linux-ppc64
548 EOF
549                 maybe_abort();
550             }
551             return { target => "linux-ppc64" } if $KERNEL_BITS eq '64';
552
553             my %config = ();
554             if (!okrun('echo __LP64__',
555                        'gcc -E -x c - 2>/dev/null',
556                        'grep "^__LP64__" 2>&1 >/dev/null') ) {
557                 %config = ( cflags => [ '-m32' ],
558                             cxxflags =>  [ '-m32' ] );
559             }
560             return { target => "linux-ppc",
561                      %config };
562         }
563       ],
564       [ 'ppc64le-.*-linux2',      { target => "linux-ppc64le" } ],
565       [ 'ppc-.*-linux2',          { target => "linux-ppc" } ],
566       [ 'mips64.*-*-linux2',
567         sub {
568             print <<EOF;
569 WARNING! To build 64-bit package, do this:
570          $WHERE/Configure linux64-mips64
571 EOF
572             maybe_abort();
573             return { target => "linux-mips64" };
574         }
575       ],
576       [ 'mips.*-.*-linux2',       { target => "linux-mips32" } ],
577       [ 'ppc60x-.*-vxworks.*',    { target => "vxworks-ppc60x" } ],
578       [ 'ppcgen-.*-vxworks.*',    { target => "vxworks-ppcgen" } ],
579       [ 'pentium-.*-vxworks.*',   { target => "vxworks-pentium" } ],
580       [ 'simlinux-.*-vxworks.*',  { target => "vxworks-simlinux" } ],
581       [ 'mips-.*-vxworks.*',      { target => "vxworks-mips" } ],
582       [ 'e2k-.*-linux.*',         { target => "linux-generic64",
583                                     defines => [ 'L_ENDIAN' ] } ],
584       [ 'ia64-.*-linux.',         { target => "linux-ia64" } ],
585       [ 'sparc64-.*-linux2',
586         sub {
587             print <<EOF;
588 WARNING! If you *know* that your GNU C supports 64-bit/V9 ABI and you
589          want to build 64-bit library, do this:
590          $WHERE/Configure linux64-sparcv9
591 EOF
592             maybe_abort();
593             return { target => "linux-sparcv9" };
594         }
595       ],
596       [ 'sparc-.*-linux2',
597         sub {
598             my $KARCH = `awk '/^type/{print \$3;exit(0);}' /proc/cpuinfo`;
599             $KARCH //= "sun4";
600             return { target => "linux-sparcv9" } if $KARCH =~ 'sun4u.*';
601             return { target => "linux-sparcv8" } if $KARCH =~ 'sun4[md]';
602             return { target => "linux-generic32",
603                      defines => [ 'L_ENDIAN' ] };
604         }
605       ],
606       [ 'parisc.*-.*-linux2',
607         sub {
608             # 64-bit builds under parisc64 linux are not supported and
609             # compiler is expected to generate 32-bit objects...
610             my $CPUARCH =
611                 `awk '/cpu family/{print substr(\$5,1,3); exit(0);}' /proc/cpuinfo`;
612             my $CPUSCHEDULE =
613                 `awk '/^cpu.[   ]*: PA/{print substr(\$3,3); exit(0);}' /proc/cpuinfo`;
614             # TODO XXX  Model transformations
615             # 0. CPU Architecture for the 1.1 processor has letter suffixes.
616             #    We strip that off assuming no further arch. identification
617             #    will ever be used by GCC.
618             # 1. I'm most concerned about whether is a 7300LC is closer to a
619             #    7100 versus a 7100LC.
620             # 2. The variant 64-bit processors cause concern should GCC support
621             #    explicit schedulers for these chips in the future.
622             #         PA7300LC -> 7100LC (1.1)
623             #         PA8200   -> 8000   (2.0)
624             #         PA8500   -> 8000   (2.0)
625             #         PA8600   -> 8000   (2.0)
626             $CPUSCHEDULE =~ s/7300LC/7100LC/;
627             $CPUSCHEDULE =~ s/8.00/8000/;
628             return
629                 { target => "linux-generic32",
630                   defines => [ 'B_ENDIAN' ],
631                   cflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ],
632                   cxxflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ]
633                 };
634         }
635       ],
636       [ 'armv[1-3].*-.*-linux2',  { target => "linux-generic32" } ],
637       [ 'armv[7-9].*-.*-linux2',  { target => "linux-armv4",
638                                     cflags => [ '-march=armv7-a' ],
639                                     cxxflags => [ '-march=armv7-a' ] } ],
640       [ 'arm.*-.*-linux2',        { target => "linux-armv4" } ],
641       [ 'aarch64-.*-linux2',      { target => "linux-aarch64" } ],
642       [ 'sh.*b-.*-linux2',        { target => "linux-generic32",
643                                     defines => [ 'B_ENDIAN' ] } ],
644       [ 'sh.*-.*-linux2',         { target => "linux-generic32",
645                                     defines => [ 'L_ENDIAN' ] } ],
646       [ 'm68k.*-.*-linux2',       { target => "linux-generic32",
647                                     defines => [ 'B_ENDIAN' ] } ],
648       [ 's390-.*-linux2',         { target => "linux-generic32",
649                                     defines => [ 'B_ENDIAN' ] } ],
650       [ 's390x-.*-linux2',
651         sub {
652             # Disabled until a glibc bug is fixed; see Configure.
653             if (0
654                 || okrun('egrep -e \'^features.* highgprs\' /proc/cpuinfo >/dev/null') )
655                 {
656                     print <<EOF;
657 WARNING! To build "highgprs" 32-bit package, do this:
658          $WHERE/Configure linux32-s390x
659 EOF
660                     maybe_abort();
661                 }
662             return { target => "linux64-s390x" };
663         }
664       ],
665       [ 'x86_64-.*-linux.',
666         sub {
667             return { target => "linux-x32" }
668                 if okrun("$CC -dM -E -x c /dev/null 2>&1",
669                          'grep -q ILP32 >/dev/null');
670             return { target => "linux-x86_64" };
671         }
672       ],
673       [ '.*86-.*-linux2',
674         sub {
675             # On machines where the compiler understands -m32, prefer a
676             # config target that uses it
677             return { target => "linux-x86" }
678                 if okrun("$CC -m32 -E -x c /dev/null >/dev/null 2>&1");
679             return { target => "linux-elf" };
680         }
681       ],
682       [ '.*86-.*-linux1',         { target => "linux-aout" } ],
683       [ 'riscv64-.*-linux.',      { target => "linux64-riscv64" } ],
684       [ '.*-.*-linux.',           { target => "linux-generic32" } ],
685       [ 'sun4[uv].*-.*-solaris2',
686         sub {
687             my $KERNEL_BITS = $ENV{KERNEL_BITS};
688             my $ISA64 = `isainfo 2>/dev/null | grep sparcv9`;
689             my $KB = $KERNEL_BITS // '64';
690             if ( $ISA64 ne "" && $KB eq '64' ) {
691                 if ( $CCVENDOR eq "sun" && $CCVER >= 500 ) {
692                     print <<EOF;
693 WARNING! To build 32-bit package, do this:
694          $WHERE/Configure solaris-sparcv9-cc
695 EOF
696                     maybe_abort();
697                 } elsif ( $CCVENDOR eq "gnu" && $GCC_ARCH eq "-m64" ) {
698                     # $GCC_ARCH denotes default ABI chosen by compiler driver
699                     # (first one found on the $PATH). I assume that user
700                     # expects certain consistency with the rest of his builds
701                     # and therefore switch over to 64-bit. <appro>
702                     print <<EOF;
703 WARNING! To build 32-bit package, do this:
704          $WHERE/Configure solaris-sparcv9-gcc
705 EOF
706                     maybe_abort();
707                     return { target => "solaris64-sparcv9-gcc" };
708                 } elsif ( $GCC_ARCH eq "-m32" ) {
709                     print <<EOF;
710 NOTICE! If you *know* that your GNU C supports 64-bit/V9 ABI and you wish
711         to build 64-bit library, do this:
712         $WHERE/Configure solaris64-sparcv9-gcc
713 EOF
714                     maybe_abort();
715                 }
716             }
717             return { target => "solaris64-sparcv9-cc" }
718                 if $ISA64 ne "" && $KB eq '64';
719             return { target => "solaris-sparcv9-cc" };
720         }
721       ],
722       [ 'sun4m-.*-solaris2',      { target => "solaris-sparcv8" } ],
723       [ 'sun4d-.*-solaris2',      { target => "solaris-sparcv8" } ],
724       [ 'sun4.*-.*-solaris2',     { target => "solaris-sparcv7" } ],
725       [ '.*86.*-.*-solaris2',
726         sub {
727             my $KERNEL_BITS = $ENV{KERNEL_BITS};
728             my $ISA64 = `isainfo 2>/dev/null | grep amd64`;
729             my $KB = $KERNEL_BITS // '64';
730             if ($ISA64 ne "" && $KB eq '64') {
731                 return { target => "solaris64-x86_64-gcc" } if $CCVENDOR eq "gnu";
732                 return { target => "solaris64-x86_64-cc" };
733             }
734             my $REL = uname('-r');
735             $REL =~ s/5\.//;
736             my @tmp_disable = ();
737             push @tmp_disable, 'sse2' if int($REL) < 10;
738             #There is no solaris-x86-cc target
739             return { target => "solaris-x86-gcc",
740                      disable => [ @tmp_disable ] };
741         }
742       ],
743       # We don't have any sunos target in Configurations/*.conf, so why here?
744       [ '.*-.*-sunos4',           { target => "sunos" } ],
745       [ '.*86.*-.*-bsdi4',        { target => "BSD-x86-elf",
746                                     lflags => [ '-ldl' ],
747                                     disable => [ 'sse2' ] } ],
748       [ 'alpha.*-.*-.*bsd.*',     { target => "BSD-generic64",
749                                     defines => [ 'L_ENDIAN' ] } ],
750       [ 'powerpc64-.*-.*bsd.*',   { target => "BSD-generic64",
751                                     defines => [ 'B_ENDIAN' ] } ],
752       [ 'riscv64-.*-.*bsd.*',     { target => "BSD-riscv64" } ],
753       [ 'sparc64-.*-.*bsd.*',     { target => "BSD-sparc64" } ],
754       [ 'ia64-.*-.*bsd.*',        { target => "BSD-ia64" } ],
755       [ 'x86_64-.*-dragonfly.*',  { target => "BSD-x86_64" } ],
756       [ 'amd64-.*-.*bsd.*',       { target => "BSD-x86_64" } ],
757       [ '.*86.*-.*-.*bsd.*',
758         sub {
759             # mimic ld behaviour when it's looking for libc...
760             my $libc;
761             if ( -l "/usr/lib/libc.so" ) {
762                 $libc = "/usr/lib/libc.so";
763             } else {
764                 # ld searches for highest libc.so.* and so do we
765                 $libc =
766                     `(ls /usr/lib/libc.so.* /lib/libc.so.* | tail -1) 2>/dev/null`;
767             }
768             my $what = `file -L $libc 2>/dev/null`;
769             return { target => "BSD-x86-elf" } if $what =~ /ELF/;
770             return { target => "BSD-x86",
771                      disable => [ 'sse2' ] };
772         }
773       ],
774       [ '.*-.*-.*bsd.*',          { target => "BSD-generic32" } ],
775       [ 'x86_64-.*-haiku',        { target => "haiku-x86_64" } ],
776       [ '.*-.*-haiku',            { target => "haiku-x86" } ],
777       [ '.*-.*-osf',              { target => "osf1-alpha" } ],
778       [ '.*-.*-tru64',            { target => "tru64-alpha" } ],
779       [ '.*-.*-[Uu]nix[Ww]are7',
780         sub {
781             return { target => "unixware-7",
782                      disable => [ 'sse2' ] } if $CCVENDOR eq "gnu";
783             return { target => "unixware-7",
784                      defines => [ '__i386__' ] };
785         }
786       ],
787       [ '.*-.*-[Uu]nix[Ww]are20.*', { target => "unixware-2.0",
788                                       disable => [ 'sse2', 'sha512' ] } ],
789       [ '.*-.*-[Uu]nix[Ww]are21.*', { target => "unixware-2.1",
790                                       disable => [ 'sse2', 'sha512' ] } ],
791       [ '.*-.*-vos',              { target => "vos",
792                                     disable => [ 'threads', 'shared', 'asm',
793                                                  'dso' ] } ],
794       [ 'BS2000-siemens-sysv4',   { target => "BS2000-OSD" } ],
795       [ 'i[3456]86-.*-cygwin',    { target => "Cygwin-x86" } ],
796       [ '.*-.*-cygwin',
797         sub { return { target => "Cygwin-${MACHINE}" } } ],
798       [ 'x86-.*-android|i.86-.*-android', { target => "android-x86" } ],
799       [ 'armv[7-9].*-.*-android', { target => "android-armeabi",
800                                     cflags => [ '-march=armv7-a' ],
801                                     cxxflags => [ '-march=armv7-a' ] } ],
802       [ 'arm.*-.*-android',       { target => "android-armeabi" } ],
803       [ '.*-hpux1.*',
804         sub {
805             my $KERNEL_BITS = $ENV{KERNEL_BITS};
806             my %common_return = ( defines => [ '_REENTRANT' ] );
807             $KERNEL_BITS ||= `getconf KERNEL_BITS 2>/dev/null` // '32';
808             # See <sys/unistd.h> for further info on CPU_VERSION.
809             my $CPU_VERSION = `getconf CPU_VERSION 2>/dev/null` // 0;
810             if ( $CPU_VERSION >= 768 ) {
811                 # IA-64 CPU
812                 return { target => "hpux64-ia64",
813                          %common_return }
814                     if $KERNEL_BITS eq '64' && ! $CCVENDOR;
815                 return { target => "hpux-ia64",
816                          %common_return };
817             }
818             if ( $CPU_VERSION >= 532 ) {
819                 # PA-RISC 2.x CPU
820                 # PA-RISC 2.0 is no longer supported as separate 32-bit
821                 # target. This is compensated for by run-time detection
822                 # in most critical assembly modules and taking advantage
823                 # of 2.0 architecture in PA-RISC 1.1 build.
824                 my $target = ($CCVENDOR eq "gnu" && $GCC_BITS eq '64')
825                     ? "hpux64-parisc2"
826                     : "hpux-parisc1_1";
827                 if ( $KERNEL_BITS eq '64' && ! $CCVENDOR ) {
828                     print <<EOF;
829 WARNING! To build 64-bit package, do this:
830          $WHERE/Configure hpux64-parisc2-cc
831 EOF
832                     maybe_abort();
833                 }
834                 return { target => $target,
835                          %common_return };
836             }
837             # PA-RISC 1.1+ CPU?
838             return { target => "hpux-parisc1_1",
839                      %common_return } if $CPU_VERSION >= 528;
840             # PA-RISC 1.0 CPU
841             return { target => "hpux-parisc",
842                      %common_return } if $CPU_VERSION >= 523;
843             # Motorola(?) CPU
844             return { target => "hpux",
845                      %common_return };
846         }
847       ],
848       [ '.*-hpux',                { target => "hpux-parisc" } ],
849       [ '.*-aix',
850         sub {
851             my %config = ();
852             my $KERNEL_BITS = $ENV{KERNEL_BITS};
853             $KERNEL_BITS ||= `getconf KERNEL_BITMODE 2>/dev/null`;
854             $KERNEL_BITS ||= '32';
855             my $OBJECT_MODE = $ENV{OBJECT_MODE};
856             $OBJECT_MODE ||= 32;
857             $config{target} = "aix";
858             if ( $OBJECT_MODE == 64 ) {
859                 print 'Your $OBJECT_MODE was found to be set to 64';
860                 $config{target} = "aix64";
861             } else {
862                 if ( $CCVENDOR ne 'gnu' && $KERNEL_BITS eq '64' ) {
863                     print <<EOF;
864 WARNING! To build 64-bit package, do this:
865          $WHERE/Configure aix64-cc
866 EOF
867                     maybe_abort();
868                 }
869             }
870             if ( okrun(
871                        "(lsattr -E -O -l `lsdev -c processor|awk '{print \$1;exit}'`",
872                        'grep -i powerpc) >/dev/null 2>&1') ) {
873                 # this applies even to Power3 and later, as they return
874                 # PowerPC_POWER[345]
875             } else {
876                 $config{disable} = [ 'asm' ];
877             }
878             return %config;
879         }
880       ],
881
882       # Windows values found by looking at Perl 5's win32/win32.c
883       [ 'amd64-.*?-Windows NT',   { target => 'VC-WIN64A' } ],
884       [ 'ia64-.*?-Windows NT',    { target => 'VC-WIN64I' } ],
885       [ 'x86-.*?-Windows NT',     { target => 'VC-WIN32'  } ],
886
887       # VMS values found by observation on existing machinery.
888       # Unfortunately, the machine part is a bit...  overdone.  It seems,
889       # though, that 'Alpha' exists in that part for Alphas, making it
890       # distinguishable from Itanium.  It will be interesting to see what
891       # we'll get in the upcoming x86_64 port...
892       [ '.*Alpha.*?-.*?-OpenVMS', { target => 'vms-alpha' } ],
893       [ '.*?-.*?-OpenVMS',        { target => 'vms-ia64'  } ],
894
895       # TODO: There are a few more choices among OpenSSL config targets, but
896       # reaching them involves a bit more than just a host tripet.  Select
897       # environment variables could do the job to cover for more granular
898       # build options such as data model (ILP32 or LP64), thread support
899       # model (PUT, SPT or nothing), target execution environment (OSS or
900       # GUARDIAN).  And still, there must be some kind of default when
901       # nothing else is said.
902       #
903       # nsv is a virtual x86 environment, equivalent to nsx, so we enforce
904       # the latter.
905       [ 'nse-tandem-nsk.*',       { target => 'nonstop-nse' } ],
906       [ 'nsv-tandem-nsk.*',       { target => 'nonstop-nsx' } ],
907       [ 'nsx-tandem-nsk.*',       { target => 'nonstop-nsx' } ],
908
909     ];
910
911 # Map GUESSOS into OpenSSL terminology.
912 # Returns a hash table with diverse entries, most importantly 'target',
913 # but also other entries that are fitting for Configure's %config
914 # and MACHINE.
915 # It would be nice to fix this so that this weren't necessary. :( XXX
916 sub map_guess {
917     my $GUESSOS = shift;
918
919     foreach my $tuple ( @$map_patterns ) {
920         my $pat = @$tuple[0];
921         next if $GUESSOS !~ /^${pat}$/;
922         my $result = @$tuple[1];
923         $result = $result->() if ref $result eq 'CODE';
924         return %$result;
925     }
926
927     # Last case, return "z" from x-y-z
928     my @fields = split(/-/, $GUESSOS);
929     return ( target => $fields[2] );
930 }
931
932 # gcc < 2.8 does not support -march=ultrasparc
933 sub check_solaris_sparc8 {
934     my $OUT = shift;
935     if ( $CCVENDOR eq 'gnu' && $CCVER < 208 ) {
936         if ( $OUT eq 'solaris-sparcv9-gcc' ) {
937             print <<EOF;
938 WARNING! Downgrading to solaris-sparcv8-gcc
939          Upgrade to gcc-2.8 or later.
940 EOF
941             maybe_abort();
942             return 'solaris-sparcv8-gcc';
943         }
944         if ( $OUT eq "linux-sparcv9" ) {
945             print <<EOF;
946 WARNING! Downgrading to linux-sparcv8
947          Upgrade to gcc-2.8 or later.
948 EOF
949             maybe_abort();
950             return 'linux-sparcv8';
951         }
952     }
953     return $OUT;
954 }
955
956 ###
957 ###   MAIN PROCESSING
958 ###
959
960 sub get_platform {
961     my %options = @_;
962
963     $VERBOSE = 1 if defined $options{verbose};
964     $WAIT = 0 if defined $options{nowait};
965     $CC = $options{CC};
966     $CROSS_COMPILE = $options{CROSS_COMPILE} // '';
967
968     my $GUESSOS = guess_system();
969     determine_compiler_settings();
970
971     my %ret = map_guess($GUESSOS);
972     $ret{target} = check_solaris_sparc8($ret{target});
973     return %ret;
974 }
975
976 1;