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