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