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