xref: /netbsd-src/crypto/external/bsd/openssl/dist/util/perl/OpenSSL/config.pm (revision 5314239046632c8cfa99fce5980f13ac03660386)
1#! /usr/bin/env perl
2# Copyright 1998-2022 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                $CCVENDOR = $k;
325
326                # The returned version is expected to be one of
327                #
328                # MAJOR
329                # MAJOR.MINOR
330                # MAJOR.MINOR.{whatever}
331                #
332                # We don't care what comes after MAJOR.MINOR.  All we need is
333                # to have them calculated into a single number, using this
334                # formula:
335                #
336                # MAJOR * 100 + MINOR
337                # Here are a few examples of what we should get:
338                #
339                # 2.95.1    => 295
340                # 3.1       => 301
341                # 9         => 900
342                my @numbers = split /\./, $v;
343                my @factors = (100, 1);
344                while (@numbers && @factors) {
345                    $CCVER += shift(@numbers) * shift(@factors)
346                }
347                last;
348            }
349        }
350    }
351
352    # Vendor specific overrides, only if we didn't determine the compiler here
353    if ( ! $cc ) {
354        if ( $SYSTEM eq 'OpenVMS' ) {
355            my $v = `CC/VERSION NLA0:`;
356            if ($? == 0) {
357                my ($vendor, $version) =
358                    ( $v =~ m/^([A-Z]+) C V([0-9\.-]+) on / );
359                my ($major, $minor, $patch) =
360                    ( $version =~ m/^([0-9]+)\.([0-9]+)-0*?(0|[1-9][0-9]*)$/ );
361                $CC = 'CC';
362                $CCVENDOR = $vendor;
363                $CCVER = ( $major * 100 + $minor ) * 100 + $patch;
364            }
365        }
366
367        if ( ${SYSTEM} eq 'AIX' ) {
368            # favor vendor cc over gcc
369            if (IPC::Cmd::can_run('cc')) {
370                $CC = 'cc';
371                $CCVENDOR = ''; # Determine later
372                $CCVER = 0;
373            }
374        }
375
376        if ( $SYSTEM eq "SunOS" ) {
377            # check for Oracle Developer Studio, expected output is "cc: blah-blah C x.x blah-blah"
378            my $v = `(cc -V 2>&1) 2>/dev/null | egrep -e '^cc: .* C [0-9]\.[0-9]'`;
379            my @numbers =
380                    ( $v =~ m/^.* C ([0-9]+)\.([0-9]+) .*/ );
381            my @factors = (100, 1);
382            $v = 0;
383            while (@numbers && @factors) {
384                $v += shift(@numbers) * shift(@factors)
385            }
386
387            if ($v > 500) {
388                $CC = 'cc';
389                $CCVENDOR = 'sun';
390                $CCVER = $v;
391            }
392        }
393
394        # 'Windows NT' is the system name according to POSIX::uname()!
395        if ( $SYSTEM eq "Windows NT" ) {
396            # favor vendor cl over gcc
397            if (IPC::Cmd::can_run('cl')) {
398                $CC = 'cl';
399                $CCVENDOR = ''; # Determine later
400                $CCVER = 0;
401
402                my $v = `cl 2>&1`;
403                if ( $v =~ /Microsoft .* Version ([0-9\.]+) for (x86|x64|ARM|ia64)/ ) {
404                    $CCVER = $1;
405                    $CL_ARCH = $2;
406                }
407            }
408        }
409    }
410
411    # If no C compiler has been determined at this point, we die.  Hard.
412    die <<_____
413ERROR!
414No C compiler found, please specify one with the environment variable CC,
415or configure with an explicit configuration target.
416_____
417        unless $CC;
418
419    # On some systems, we assume a cc vendor if it's not already determined
420
421    if ( ! $CCVENDOR ) {
422        $CCVENDOR = 'aix' if $SYSTEM eq 'AIX';
423        $CCVENDOR = 'sun' if $SYSTEM eq 'SunOS';
424    }
425
426    # Some systems need to know extra details
427
428    if ( $SYSTEM eq "HP-UX" && $CCVENDOR eq 'gnu' ) {
429        # By default gcc is a ILP32 compiler (with long long == 64).
430        $GCC_BITS = "32";
431        if ( $CCVER >= 300 ) {
432            # PA64 support only came in with gcc 3.0.x.
433            # We check if the preprocessor symbol __LP64__ is defined.
434            if ( okrun('echo __LP64__',
435                       "$CC -v -E -x c - 2>/dev/null",
436                       'grep "^__LP64__" 2>&1 >/dev/null') ) {
437                # __LP64__ has slipped through, it therefore is not defined
438            } else {
439                $GCC_BITS = '64';
440            }
441        }
442    }
443
444    if ( $SYSTEM eq "SunOS" && $CCVENDOR eq 'gnu' ) {
445        if ( $CCVER >= 300 ) {
446            # 64-bit ABI isn't officially supported in gcc 3.0, but seems
447            # to be working; at the very least 'make test' passes.
448            if ( okrun("$CC -v -E -x c /dev/null 2>&1",
449                       'grep __arch64__ >/dev/null') ) {
450                $GCC_ARCH = "-m64"
451            } else {
452                $GCC_ARCH = "-m32"
453            }
454        }
455    }
456
457    if ($VERBOSE) {
458        my $vendor = $CCVENDOR ? $CCVENDOR : "(undetermined)";
459        my $version = $CCVER ? $CCVER : "(undetermined)";
460        print "C compiler: $CC\n";
461        print "C compiler vendor: $vendor\n";
462        print "C compiler version: $version\n";
463    }
464}
465
466my $map_patterns =
467    [ [ 'uClinux.*64.*',          { target => 'uClinux-dist64' } ],
468      [ 'uClinux.*',              { target => 'uClinux-dist' } ],
469      [ 'mips3-sgi-irix',         { target => 'irix-mips3' } ],
470      [ 'mips4-sgi-irix64',
471        sub {
472            print <<EOF;
473WARNING! To build 64-bit package, do this:
474         $WHERE/Configure irix64-mips4-$CC
475EOF
476            maybe_abort();
477            return { target => "irix-mips3" };
478        }
479      ],
480      [ 'ppc-apple-rhapsody',     { target => "rhapsody-ppc" } ],
481      [ 'ppc-apple-darwin.*',
482        sub {
483            my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
484            my $ISA64 = `sysctl -n hw.optional.64bitops 2>/dev/null`;
485            if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
486                print <<EOF;
487WARNING! To build 64-bit package, do this:
488         $WHERE/Configure darwin64-ppc-cc
489EOF
490                maybe_abort();
491            }
492            return { target => "darwin64-ppc" }
493                if $ISA64 == 1 && $KERNEL_BITS eq '64';
494            return { target => "darwin-ppc" };
495        }
496      ],
497      [ 'i.86-apple-darwin.*',
498        sub {
499            my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
500            my $ISA64 = `sysctl -n hw.optional.x86_64 2>/dev/null`;
501            if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
502                print <<EOF;
503WARNING! To build 64-bit package, do this:
504         KERNEL_BITS=64 $WHERE/Configure \[\[ options \]\]
505EOF
506                maybe_abort();
507            }
508            return { target => "darwin64-x86_64" }
509                if $ISA64 == 1 && $KERNEL_BITS eq '64';
510            return { target => "darwin-i386" };
511        }
512      ],
513      [ 'x86_64-apple-darwin.*',
514        sub {
515            my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
516            # macOS >= 10.15 is 64-bit only
517            my $SW_VERS = `sw_vers -productVersion 2>/dev/null`;
518            if ($SW_VERS =~ /^(\d+)\.(\d+)\.(\d+)$/) {
519                if ($1 > 10 || ($1 == 10 && $2 >= 15)) {
520                    die "32-bit applications not supported on macOS 10.15 or later\n" if $KERNEL_BITS eq '32';
521                    return { target => "darwin64-x86_64" };
522                }
523            }
524            return { target => "darwin-i386" } if $KERNEL_BITS eq '32';
525
526            print <<EOF;
527WARNING! To build 32-bit package, do this:
528         KERNEL_BITS=32 $WHERE/Configure \[\[ options \]\]
529EOF
530            maybe_abort();
531            return { target => "darwin64-x86_64" };
532        }
533      ],
534      [ 'arm64-apple-darwin.*', { target => "darwin64-arm64" } ],
535      [ 'armv6\+7-.*-iphoneos',
536        { target => "iphoneos-cross",
537          cflags => [ qw(-arch armv6 -arch armv7) ],
538          cxxflags => [ qw(-arch armv6 -arch armv7) ] }
539      ],
540      [ 'arm64-.*-iphoneos|.*-.*-ios64',
541        { target => "ios64-cross" }
542      ],
543      [ '.*-.*-iphoneos',
544        sub { return { target => "iphoneos-cross",
545                       cflags => [ "-arch ${MACHINE}" ],
546                       cxxflags => [ "-arch ${MACHINE}" ] }; }
547      ],
548      [ 'alpha-.*-linux2.*',
549        sub {
550            my $ISA = `awk '/cpu model/{print \$4;exit(0);}' /proc/cpuinfo`;
551            $ISA //= 'generic';
552            my %config = ();
553            if ( $CCVENDOR eq "gnu" ) {
554                if ( $ISA =~ 'EV5|EV45' ) {
555                    %config = ( cflags => [ '-mcpu=ev5' ],
556                                cxxflags =>  [ '-mcpu=ev5' ] );
557                } elsif ( $ISA =~ 'EV56|PCA56' ) {
558                    %config = ( cflags => [ '-mcpu=ev56' ],
559                                cxxflags =>  [ '-mcpu=ev56' ] );
560                } else {
561                    %config = ( cflags => [ '-mcpu=ev6' ],
562                                cxxflags =>  [ '-mcpu=ev6' ] );
563                }
564            }
565            return { target => "linux-alpha",
566                     %config };
567        }
568      ],
569      [ 'ppc64-.*-linux2',
570        sub {
571            my $KERNEL_BITS = $ENV{KERNEL_BITS} // '';
572            if ( $KERNEL_BITS eq '' ) {
573                print <<EOF;
574WARNING! To build 64-bit package, do this:
575         $WHERE/Configure linux-ppc64
576EOF
577                maybe_abort();
578            }
579            return { target => "linux-ppc64" } if $KERNEL_BITS eq '64';
580
581            my %config = ();
582            if (!okrun('echo __LP64__',
583                       'gcc -E -x c - 2>/dev/null',
584                       'grep "^__LP64__" 2>&1 >/dev/null') ) {
585                %config = ( cflags => [ '-m32' ],
586                            cxxflags =>  [ '-m32' ] );
587            }
588            return { target => "linux-ppc",
589                     %config };
590        }
591      ],
592      [ 'ppc64le-.*-linux2',      { target => "linux-ppc64le" } ],
593      [ 'ppc-.*-linux2',          { target => "linux-ppc" } ],
594      [ 'mips64.*-*-linux2',
595        sub {
596            print <<EOF;
597WARNING! To build 64-bit package, do this:
598         $WHERE/Configure linux64-mips64
599EOF
600            maybe_abort();
601            return { target => "linux-mips64" };
602        }
603      ],
604      [ 'mips.*-.*-linux2',       { target => "linux-mips32" } ],
605      [ 'ppc60x-.*-vxworks.*',    { target => "vxworks-ppc60x" } ],
606      [ 'ppcgen-.*-vxworks.*',    { target => "vxworks-ppcgen" } ],
607      [ 'pentium-.*-vxworks.*',   { target => "vxworks-pentium" } ],
608      [ 'simlinux-.*-vxworks.*',  { target => "vxworks-simlinux" } ],
609      [ 'mips-.*-vxworks.*',      { target => "vxworks-mips" } ],
610      [ 'e2k-.*-linux.*',         { target => "linux-generic64",
611                                    defines => [ 'L_ENDIAN' ] } ],
612      [ 'ia64-.*-linux.',         { target => "linux-ia64" } ],
613      [ 'sparc64-.*-linux2',
614        sub {
615            print <<EOF;
616WARNING! If you *know* that your GNU C supports 64-bit/V9 ABI and you
617         want to build 64-bit library, do this:
618         $WHERE/Configure linux64-sparcv9
619EOF
620            maybe_abort();
621            return { target => "linux-sparcv9" };
622        }
623      ],
624      [ 'sparc-.*-linux2',
625        sub {
626            my $KARCH = `awk '/^type/{print \$3;exit(0);}' /proc/cpuinfo`;
627            $KARCH //= "sun4";
628            return { target => "linux-sparcv9" } if $KARCH =~ 'sun4u.*';
629            return { target => "linux-sparcv8" } if $KARCH =~ 'sun4[md]';
630            return { target => "linux-generic32",
631                     defines => [ 'L_ENDIAN' ] };
632        }
633      ],
634      [ 'parisc.*-.*-linux2',
635        sub {
636            # 64-bit builds under parisc64 linux are not supported and
637            # compiler is expected to generate 32-bit objects...
638            my $CPUARCH =
639                `awk '/cpu family/{print substr(\$5,1,3); exit(0);}' /proc/cpuinfo`;
640            my $CPUSCHEDULE =
641                `awk '/^cpu.[ 	]*: PA/{print substr(\$3,3); exit(0);}' /proc/cpuinfo`;
642            # TODO XXX  Model transformations
643            # 0. CPU Architecture for the 1.1 processor has letter suffixes.
644            #    We strip that off assuming no further arch. identification
645            #    will ever be used by GCC.
646            # 1. I'm most concerned about whether is a 7300LC is closer to a
647            #    7100 versus a 7100LC.
648            # 2. The variant 64-bit processors cause concern should GCC support
649            #    explicit schedulers for these chips in the future.
650            #         PA7300LC -> 7100LC (1.1)
651            #         PA8200   -> 8000   (2.0)
652            #         PA8500   -> 8000   (2.0)
653            #         PA8600   -> 8000   (2.0)
654            $CPUSCHEDULE =~ s/7300LC/7100LC/;
655            $CPUSCHEDULE =~ s/8.00/8000/;
656            return
657                { target => "linux-generic32",
658                  defines => [ 'B_ENDIAN' ],
659                  cflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ],
660                  cxxflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ]
661                };
662        }
663      ],
664      [ 'armv[1-3].*-.*-linux2',  { target => "linux-generic32" } ],
665      [ 'armv[7-9].*-.*-linux2',  { target => "linux-armv4",
666                                    cflags => [ '-march=armv7-a' ],
667                                    cxxflags => [ '-march=armv7-a' ] } ],
668      [ 'arm.*-.*-linux2',        { target => "linux-armv4" } ],
669      [ 'aarch64-.*-linux2',      { target => "linux-aarch64" } ],
670      [ 'sh.*b-.*-linux2',        { target => "linux-generic32",
671                                    defines => [ 'B_ENDIAN' ] } ],
672      [ 'sh.*-.*-linux2',         { target => "linux-generic32",
673                                    defines => [ 'L_ENDIAN' ] } ],
674      [ 'm68k.*-.*-linux2',       { target => "linux-generic32",
675                                    defines => [ 'B_ENDIAN' ] } ],
676      [ 's390-.*-linux2',         { target => "linux-generic32",
677                                    defines => [ 'B_ENDIAN' ] } ],
678      [ 's390x-.*-linux2',
679        sub {
680            # Disabled until a glibc bug is fixed; see Configure.
681            if (0
682                || okrun('egrep -e \'^features.* highgprs\' /proc/cpuinfo >/dev/null') )
683                {
684                    print <<EOF;
685WARNING! To build "highgprs" 32-bit package, do this:
686         $WHERE/Configure linux32-s390x
687EOF
688                    maybe_abort();
689                }
690            return { target => "linux64-s390x" };
691        }
692      ],
693      [ 'x86_64-.*-linux.',
694        sub {
695            return { target => "linux-x32" }
696                if okrun("$CC -dM -E -x c /dev/null 2>&1",
697                         'grep -q ILP32 >/dev/null');
698            return { target => "linux-x86_64" };
699        }
700      ],
701      [ '.*86-.*-linux2',
702        sub {
703            # On machines where the compiler understands -m32, prefer a
704            # config target that uses it
705            return { target => "linux-x86" }
706                if okrun("$CC -m32 -E -x c /dev/null >/dev/null 2>&1");
707            return { target => "linux-elf" };
708        }
709      ],
710      [ '.*86-.*-linux1',         { target => "linux-aout" } ],
711      [ 'riscv64-.*-linux.',      { target => "linux64-riscv64" } ],
712      [ '.*-.*-linux.',           { target => "linux-generic32" } ],
713      [ 'sun4[uv].*-.*-solaris2',
714        sub {
715            my $KERNEL_BITS = $ENV{KERNEL_BITS};
716            my $ISA64 = `isainfo 2>/dev/null | grep sparcv9`;
717            my $KB = $KERNEL_BITS // '64';
718            if ( $ISA64 ne "" && $KB eq '64' ) {
719                if ( $CCVENDOR eq "sun" && $CCVER >= 500 ) {
720                    print <<EOF;
721WARNING! To build 32-bit package, do this:
722         $WHERE/Configure solaris-sparcv9-cc
723EOF
724                    maybe_abort();
725                } elsif ( $CCVENDOR eq "gnu" && $GCC_ARCH eq "-m64" ) {
726                    # $GCC_ARCH denotes default ABI chosen by compiler driver
727                    # (first one found on the $PATH). I assume that user
728                    # expects certain consistency with the rest of his builds
729                    # and therefore switch over to 64-bit. <appro>
730                    print <<EOF;
731WARNING! To build 32-bit package, do this:
732         $WHERE/Configure solaris-sparcv9-gcc
733EOF
734                    maybe_abort();
735                    return { target => "solaris64-sparcv9-gcc" };
736                } elsif ( $GCC_ARCH eq "-m32" ) {
737                    print <<EOF;
738NOTICE! If you *know* that your GNU C supports 64-bit/V9 ABI and you wish
739        to build 64-bit library, do this:
740        $WHERE/Configure solaris64-sparcv9-gcc
741EOF
742                    maybe_abort();
743                }
744            }
745            return { target => "solaris64-sparcv9-cc" }
746                if $ISA64 ne "" && $KB eq '64';
747            return { target => "solaris-sparcv9-cc" };
748        }
749      ],
750      [ 'sun4m-.*-solaris2',      { target => "solaris-sparcv8" } ],
751      [ 'sun4d-.*-solaris2',      { target => "solaris-sparcv8" } ],
752      [ 'sun4.*-.*-solaris2',     { target => "solaris-sparcv7" } ],
753      [ '.*86.*-.*-solaris2',
754        sub {
755            my $KERNEL_BITS = $ENV{KERNEL_BITS};
756            my $ISA64 = `isainfo 2>/dev/null | grep amd64`;
757            my $KB = $KERNEL_BITS // '64';
758            if ($ISA64 ne "" && $KB eq '64') {
759                return { target => "solaris64-x86_64-gcc" } if $CCVENDOR eq "gnu";
760                return { target => "solaris64-x86_64-cc" };
761            }
762            my $REL = uname('-r');
763            $REL =~ s/5\.//;
764            my @tmp_disable = ();
765            push @tmp_disable, 'sse2' if int($REL) < 10;
766            #There is no solaris-x86-cc target
767            return { target => "solaris-x86-gcc",
768                     disable => [ @tmp_disable ] };
769        }
770      ],
771      # We don't have any sunos target in Configurations/*.conf, so why here?
772      [ '.*-.*-sunos4',           { target => "sunos" } ],
773      [ '.*86.*-.*-bsdi4',        { target => "BSD-x86-elf",
774                                    lflags => [ '-ldl' ],
775                                    disable => [ 'sse2' ] } ],
776      [ 'alpha.*-.*-.*bsd.*',     { target => "BSD-generic64",
777                                    defines => [ 'L_ENDIAN' ] } ],
778      [ 'powerpc64-.*-.*bsd.*',   { target => "BSD-generic64",
779                                    defines => [ 'B_ENDIAN' ] } ],
780      [ 'riscv64-.*-.*bsd.*',     { target => "BSD-riscv64" } ],
781      [ 'sparc64-.*-.*bsd.*',     { target => "BSD-sparc64" } ],
782      [ 'ia64-.*-.*bsd.*',        { target => "BSD-ia64" } ],
783      [ 'x86_64-.*-dragonfly.*',  { target => "BSD-x86_64" } ],
784      [ 'amd64-.*-.*bsd.*',       { target => "BSD-x86_64" } ],
785      [ 'arm64-.*-.*bsd.*',       { target => "BSD-aarch64" } ],
786      [ '.*86.*-.*-.*bsd.*',
787        sub {
788            # mimic ld behaviour when it's looking for libc...
789            my $libc;
790            if ( -l "/usr/lib/libc.so" ) {
791                $libc = "/usr/lib/libc.so";
792            } else {
793                # ld searches for highest libc.so.* and so do we
794                $libc =
795                    `(ls /usr/lib/libc.so.* /lib/libc.so.* | tail -1) 2>/dev/null`;
796            }
797            my $what = `file -L $libc 2>/dev/null`;
798            return { target => "BSD-x86-elf" } if $what =~ /ELF/;
799            return { target => "BSD-x86",
800                     disable => [ 'sse2' ] };
801        }
802      ],
803      [ '.*-.*-.*bsd.*',          { target => "BSD-generic32" } ],
804      [ 'x86_64-.*-haiku',        { target => "haiku-x86_64" } ],
805      [ '.*-.*-haiku',            { target => "haiku-x86" } ],
806      [ '.*-.*-osf',              { target => "osf1-alpha" } ],
807      [ '.*-.*-tru64',            { target => "tru64-alpha" } ],
808      [ '.*-.*-[Uu]nix[Ww]are7',
809        sub {
810            return { target => "unixware-7",
811                     disable => [ 'sse2' ] } if $CCVENDOR eq "gnu";
812            return { target => "unixware-7",
813                     defines => [ '__i386__' ] };
814        }
815      ],
816      [ '.*-.*-[Uu]nix[Ww]are20.*', { target => "unixware-2.0",
817                                      disable => [ 'sse2', 'sha512' ] } ],
818      [ '.*-.*-[Uu]nix[Ww]are21.*', { target => "unixware-2.1",
819                                      disable => [ 'sse2', 'sha512' ] } ],
820      [ '.*-.*-vos',              { target => "vos",
821                                    disable => [ 'threads', 'shared', 'asm',
822                                                 'dso' ] } ],
823      [ 'BS2000-siemens-sysv4',   { target => "BS2000-OSD" } ],
824      [ 'i[3456]86-.*-cygwin',    { target => "Cygwin-x86" } ],
825      [ '.*-.*-cygwin',
826        sub { return { target => "Cygwin-${MACHINE}" } } ],
827      [ 'x86-.*-android|i.86-.*-android', { target => "android-x86" } ],
828      [ 'armv[7-9].*-.*-android', { target => "android-armeabi",
829                                    cflags => [ '-march=armv7-a' ],
830                                    cxxflags => [ '-march=armv7-a' ] } ],
831      [ 'arm.*-.*-android',       { target => "android-armeabi" } ],
832      [ '.*-hpux1.*',
833        sub {
834            my $KERNEL_BITS = $ENV{KERNEL_BITS};
835            my %common_return = ( defines => [ '_REENTRANT' ] );
836            $KERNEL_BITS ||= `getconf KERNEL_BITS 2>/dev/null` // '32';
837            # See <sys/unistd.h> for further info on CPU_VERSION.
838            my $CPU_VERSION = `getconf CPU_VERSION 2>/dev/null` // 0;
839            if ( $CPU_VERSION >= 768 ) {
840                # IA-64 CPU
841                return { target => "hpux64-ia64",
842                         %common_return }
843                    if $KERNEL_BITS eq '64' && ! $CCVENDOR;
844                return { target => "hpux-ia64",
845                         %common_return };
846            }
847            if ( $CPU_VERSION >= 532 ) {
848                # PA-RISC 2.x CPU
849                # PA-RISC 2.0 is no longer supported as separate 32-bit
850                # target. This is compensated for by run-time detection
851                # in most critical assembly modules and taking advantage
852                # of 2.0 architecture in PA-RISC 1.1 build.
853                my $target = ($CCVENDOR eq "gnu" && $GCC_BITS eq '64')
854                    ? "hpux64-parisc2"
855                    : "hpux-parisc1_1";
856                if ( $KERNEL_BITS eq '64' && ! $CCVENDOR ) {
857                    print <<EOF;
858WARNING! To build 64-bit package, do this:
859         $WHERE/Configure hpux64-parisc2-cc
860EOF
861                    maybe_abort();
862                }
863                return { target => $target,
864                         %common_return };
865            }
866            # PA-RISC 1.1+ CPU?
867            return { target => "hpux-parisc1_1",
868                     %common_return } if $CPU_VERSION >= 528;
869            # PA-RISC 1.0 CPU
870            return { target => "hpux-parisc",
871                     %common_return } if $CPU_VERSION >= 523;
872            # Motorola(?) CPU
873            return { target => "hpux",
874                     %common_return };
875        }
876      ],
877      [ '.*-hpux',                { target => "hpux-parisc" } ],
878      [ '.*-aix',
879        sub {
880            my %config = ();
881            my $KERNEL_BITS = $ENV{KERNEL_BITS};
882            $KERNEL_BITS ||= `getconf KERNEL_BITMODE 2>/dev/null`;
883            $KERNEL_BITS ||= '32';
884            my $OBJECT_MODE = $ENV{OBJECT_MODE};
885            $OBJECT_MODE ||= 32;
886            $config{target} = "aix";
887            if ( $OBJECT_MODE == 64 ) {
888                print 'Your $OBJECT_MODE was found to be set to 64';
889                $config{target} = "aix64";
890            } else {
891                if ( $CCVENDOR ne 'gnu' && $KERNEL_BITS eq '64' ) {
892                    print <<EOF;
893WARNING! To build 64-bit package, do this:
894         $WHERE/Configure aix64-cc
895EOF
896                    maybe_abort();
897                }
898            }
899            if ( okrun(
900                       "(lsattr -E -O -l `lsdev -c processor|awk '{print \$1;exit}'`",
901                       'grep -i powerpc) >/dev/null 2>&1') ) {
902                # this applies even to Power3 and later, as they return
903                # PowerPC_POWER[345]
904            } else {
905                $config{disable} = [ 'asm' ];
906            }
907            return { %config };
908        }
909      ],
910
911      # Windows values found by looking at Perl 5's win32/win32.c
912      [ '(amd64|ia64|x86|ARM)-.*?-Windows NT',
913        sub {
914            # If we determined the arch by asking cl, take that value,
915            # otherwise the SYSTEM we got from from POSIX::uname().
916            my $arch = $CL_ARCH // $1;
917            my $config;
918
919            if ($arch) {
920                $config = { 'amd64' => { target => 'VC-WIN64A'    },
921                            'ia64'  => { target => 'VC-WIN64I'    },
922                            'x86'   => { target => 'VC-WIN32'     },
923                            'x64'   => { target => 'VC-WIN64A'    },
924                            'ARM'   => { target => 'VC-WIN64-ARM' },
925                          } -> {$arch};
926                die <<_____ unless defined $config;
927ERROR
928I do not know how to handle ${arch}.
929_____
930            }
931            die <<_____ unless defined $config;
932ERROR
933Could not figure out the architecture.
934_____
935
936            return $config;
937        }
938      ],
939
940      # VMS values found by observation on existing machinery.
941      [ 'VMS_AXP-.*?-OpenVMS',    { target => 'vms-alpha'  } ],
942      [ 'VMS_IA64-.*?-OpenVMS',   { target => 'vms-ia64'   } ],
943      [ 'VMS_x86_64-.*?-OpenVMS', { target => 'vms-x86_64' } ],
944
945      # TODO: There are a few more choices among OpenSSL config targets, but
946      # reaching them involves a bit more than just a host tripet.  Select
947      # environment variables could do the job to cover for more granular
948      # build options such as data model (ILP32 or LP64), thread support
949      # model (PUT, SPT or nothing), target execution environment (OSS or
950      # GUARDIAN).  And still, there must be some kind of default when
951      # nothing else is said.
952      #
953      # nsv is a virtual x86 environment, equivalent to nsx, so we enforce
954      # the latter.
955      [ 'nse-tandem-nsk.*',       { target => 'nonstop-nse' } ],
956      [ 'nsv-tandem-nsk.*',       { target => 'nonstop-nsx' } ],
957      [ 'nsx-tandem-nsk.*',       { target => 'nonstop-nsx' } ],
958
959    ];
960
961# Map GUESSOS into OpenSSL terminology.
962# Returns a hash table with diverse entries, most importantly 'target',
963# but also other entries that are fitting for Configure's %config
964# and MACHINE.
965# It would be nice to fix this so that this weren't necessary. :( XXX
966sub map_guess {
967    my $GUESSOS = shift;
968
969    foreach my $tuple ( @$map_patterns ) {
970        my $pat = @$tuple[0];
971        next if $GUESSOS !~ /^${pat}$/;
972        my $result = @$tuple[1];
973        $result = $result->() if ref $result eq 'CODE';
974        return %$result;
975    }
976
977    # Last case, return "z" from x-y-z
978    my @fields = split(/-/, $GUESSOS);
979    return ( target => $fields[2] );
980}
981
982# gcc < 2.8 does not support -march=ultrasparc
983sub check_solaris_sparc8 {
984    my $OUT = shift;
985    if ( $CCVENDOR eq 'gnu' && $CCVER < 208 ) {
986        if ( $OUT eq 'solaris-sparcv9-gcc' ) {
987            print <<EOF;
988WARNING! Downgrading to solaris-sparcv8-gcc
989         Upgrade to gcc-2.8 or later.
990EOF
991            maybe_abort();
992            return 'solaris-sparcv8-gcc';
993        }
994        if ( $OUT eq "linux-sparcv9" ) {
995            print <<EOF;
996WARNING! Downgrading to linux-sparcv8
997         Upgrade to gcc-2.8 or later.
998EOF
999            maybe_abort();
1000            return 'linux-sparcv8';
1001        }
1002    }
1003    return $OUT;
1004}
1005
1006###
1007###   MAIN PROCESSING
1008###
1009
1010sub get_platform {
1011    my %options = @_;
1012
1013    $VERBOSE = 1 if defined $options{verbose};
1014    $WAIT = 0 if defined $options{nowait};
1015    $CC = $options{CC};
1016    $CROSS_COMPILE = $options{CROSS_COMPILE} // '';
1017
1018    my $GUESSOS = guess_system();
1019    determine_compiler_settings();
1020
1021    my %ret = map_guess($GUESSOS);
1022    $ret{target} = check_solaris_sparc8($ret{target});
1023    return %ret;
1024}
1025
10261;
1027