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