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