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