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