Use read/write locking on Windows
[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 'OpenVMS' ) {
341             my $v = `CC/VERSION NLA0:`;
342             if ($? == 0) {
343                 my ($vendor, $version) =
344                     ( $v =~ m/^([A-Z]+) C V([0-9\.-]+) on / );
345                 my ($major, $minor, $patch) =
346                     ( $version =~ m/^([0-9]+)\.([0-9]+)-0*?(0|[1-9][0-9]*)$/ );
347                 $CC = 'CC';
348                 $CCVENDOR = $vendor;
349                 $CCVER = ( $major * 100 + $minor ) * 100 + $patch;
350             }
351         }
352
353         if ( ${SYSTEM} eq 'AIX' ) {
354             # favor vendor cc over gcc
355             if (IPC::Cmd::can_run('cc')) {
356                 $CC = 'cc';
357                 $CCVENDOR = ''; # Determine later
358                 $CCVER = 0;
359             }
360         }
361
362         if ( $SYSTEM eq "SunOS" ) {
363             # check for WorkShop C, expected output is "cc: blah-blah C x.x"
364             my $v = `(cc -V 2>&1) 2>/dev/null | egrep -e '^cc: .* C [0-9]\.[0-9]'`;
365             chomp $v;
366             $v =~ s/.* C \([0-9]\)\.\([0-9]\).*/$1.$2/;
367             my @numbers = split /\./, $v;
368             my @factors = (100, 1);
369             $v = 0;
370             while (@numbers && @factors) {
371                 $v += shift(@numbers) * shift(@factors)
372             }
373
374             if ( $v > 40000 &&  $MACHINE ne 'i86pc' ) {
375                 $CC = 'cc';
376                 $CCVENDOR = ''; # Determine later
377                 $CCVER = $v;
378
379                 if ( $CCVER == 50000 ) {
380                     print <<'EOF';
381 WARNING! Found WorkShop C 5.0.
382          Make sure you have patch #107357-01 or later applied.
383 EOF
384                     maybe_abort();
385                 }
386             }
387         }
388     }
389
390     # If no C compiler has been determined at this point, we die.  Hard.
391     die <<_____
392 ERROR!
393 No C compiler found, please specify one with the environment variable CC,
394 or configure with an explicit configuration target.
395 _____
396         unless $CC;
397
398     # On some systems, we assume a cc vendor if it's not already determined
399
400     if ( ! $CCVENDOR ) {
401         $CCVENDOR = 'aix' if $SYSTEM eq 'AIX';
402         $CCVENDOR = 'sun' if $SYSTEM eq 'SunOS';
403     }
404
405     # Some systems need to know extra details
406
407     if ( $SYSTEM eq "HP-UX" && $CCVENDOR eq 'gnu' ) {
408         # By default gcc is a ILP32 compiler (with long long == 64).
409         $GCC_BITS = "32";
410         if ( $CCVER >= 300 ) {
411             # PA64 support only came in with gcc 3.0.x.
412             # We check if the preprocessor symbol __LP64__ is defined.
413             if ( okrun('echo __LP64__',
414                        "$CC -v -E -x c - 2>/dev/null",
415                        'grep "^__LP64__" 2>&1 >/dev/null') ) {
416                 # __LP64__ has slipped through, it therefore is not defined
417             } else {
418                 $GCC_BITS = '64';
419             }
420         }
421     }
422
423     if ( $SYSTEM eq "SunOS" && $CCVENDOR eq 'gnu' ) {
424         if ( $CCVER >= 300 ) {
425             # 64-bit ABI isn't officially supported in gcc 3.0, but seems
426             # to be working; at the very least 'make test' passes.
427             if ( okrun("$CC -v -E -x c /dev/null 2>&1",
428                        'grep __arch64__ >/dev/null') ) {
429                 $GCC_ARCH = "-m64"
430             } else {
431                 $GCC_ARCH = "-m32"
432             }
433         }
434     }
435
436     if ($VERBOSE) {
437         my $vendor = $CCVENDOR ? $CCVENDOR : "(undetermined)";
438         my $version = $CCVER ? $CCVER : "(undetermined)";
439         print "C compiler: $CC\n";
440         print "C compiler vendor: $vendor\n";
441         print "C compiler version: $version\n";
442     }
443 }
444
445 my $map_patterns =
446     [ [ 'uClinux.*64.*',          { target => 'uClinux-dist64' } ],
447       [ 'uClinux.*',              { target => 'uClinux-dist' } ],
448       [ 'mips3-sgi-irix',         { target => 'irix-mips3' } ],
449       [ 'mips4-sgi-irix64',
450         sub {
451             print <<EOF;
452 WARNING! To build 64-bit package, do this:
453          $WHERE/Configure irix64-mips4-$CC
454 EOF
455             maybe_abort();
456             return { target => "irix-mips3" };
457         }
458       ],
459       [ 'ppc-apple-rhapsody',     { target => "rhapsody-ppc" } ],
460       [ 'ppc-apple-darwin.*',
461         sub {
462             my $KERNEL_BITS = $ENV{KERNEL_BITS};
463             my $ISA64 = `sysctl -n hw.optional.64bitops 2>/dev/null`;
464             if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
465                 print <<EOF;
466 WARNING! To build 64-bit package, do this:
467          $WHERE/Configure darwin64-ppc-cc
468 EOF
469                 maybe_abort();
470             }
471             return { target => "darwin64-ppc" }
472                 if $ISA64 == 1 && $KERNEL_BITS eq '64';
473             return { target => "darwin-ppc" };
474         }
475       ],
476       [ 'i.86-apple-darwin.*',
477         sub {
478             my $KERNEL_BITS = $ENV{KERNEL_BITS};
479             my $ISA64 = `sysctl -n hw.optional.x86_64 2>/dev/null`;
480             if ( $ISA64 == 1 && $KERNEL_BITS eq '' ) {
481                 print <<EOF;
482 WARNING! To build 64-bit package, do this:
483          KERNEL_BITS=64 $WHERE/Configure \[\[ options \]\]
484 EOF
485                 maybe_abort();
486             }
487             return { target => "darwin64-x86_64" }
488                 if $ISA64 == 1 && $KERNEL_BITS eq '64';
489             return { target => "darwin-i386" };
490         }
491       ],
492       [ 'x86_64-apple-darwin.*',
493         sub {
494             my $KERNEL_BITS = $ENV{KERNEL_BITS};
495             return { target => "darwin-i386" } if $KERNEL_BITS eq '32';
496
497             print <<EOF;
498 WARNING! To build 32-bit package, do this:
499          KERNEL_BITS=32 $WHERE/Configure \[\[ options \]\]
500 EOF
501             maybe_abort();
502             return { target => "darwin64-x86_64" };
503         }
504       ],
505       [ 'arm64-apple-darwin.*', { target => "darwin64-arm64" } ],
506       [ 'armv6\+7-.*-iphoneos',
507         { target => "iphoneos-cross",
508           cflags => [ qw(-arch armv6 -arch armv7) ],
509           cxxflags => [ qw(-arch armv6 -arch armv7) ] }
510       ],
511       [ 'arm64-.*-iphoneos|.*-.*-ios64',
512         { target => "ios64-cross" }
513       ],
514       [ '.*-.*-iphoneos',
515         sub { return { target => "iphoneos-cross",
516                        cflags => [ "-arch ${MACHINE}" ],
517                        cxxflags => [ "-arch ${MACHINE}" ] }; }
518       ],
519       [ 'alpha-.*-linux2.*',
520         sub {
521             my $ISA = `awk '/cpu model/{print \$4;exit(0);}' /proc/cpuinfo`;
522             $ISA //= 'generic';
523             my %config = ();
524             if ( $CCVENDOR eq "gnu" ) {
525                 if ( $ISA =~ 'EV5|EV45' ) {
526                     %config = ( cflags => [ '-mcpu=ev5' ],
527                                 cxxflags =>  [ '-mcpu=ev5' ] );
528                 } elsif ( $ISA =~ 'EV56|PCA56' ) {
529                     %config = ( cflags => [ '-mcpu=ev56' ],
530                                 cxxflags =>  [ '-mcpu=ev56' ] );
531                 } else {
532                     %config = ( cflags => [ '-mcpu=ev6' ],
533                                 cxxflags =>  [ '-mcpu=ev6' ] );
534                 }
535             }
536             return { target => "linux-alpha",
537                      %config };
538         }
539       ],
540       [ 'ppc64-.*-linux2',
541         sub {
542             my $KERNEL_BITS = $ENV{KERNEL_BITS};
543             if ( $KERNEL_BITS eq '' ) {
544                 print <<EOF;
545 WARNING! To build 64-bit package, do this:
546          $WHERE/Configure linux-ppc64
547 EOF
548                 maybe_abort();
549             }
550             return { target => "linux-ppc64" } if $KERNEL_BITS eq '64';
551
552             my %config = ();
553             if (!okrun('echo __LP64__',
554                        'gcc -E -x c - 2>/dev/null',
555                        'grep "^__LP64__" 2>&1 >/dev/null') ) {
556                 %config = ( cflags => [ '-m32' ],
557                             cxxflags =>  [ '-m32' ] );
558             }
559             return { target => "linux-ppc",
560                      %config };
561         }
562       ],
563       [ 'ppc64le-.*-linux2',      { target => "linux-ppc64le" } ],
564       [ 'ppc-.*-linux2',          { target => "linux-ppc" } ],
565       [ 'mips64.*-*-linux2',
566         sub {
567             print <<EOF;
568 WARNING! To build 64-bit package, do this:
569          $WHERE/Configure linux64-mips64
570 EOF
571             maybe_abort();
572             return { target => "linux-mips64" };
573         }
574       ],
575       [ 'mips.*-.*-linux2',       { target => "linux-mips32" } ],
576       [ 'ppc60x-.*-vxworks.*',    { target => "vxworks-ppc60x" } ],
577       [ 'ppcgen-.*-vxworks.*',    { target => "vxworks-ppcgen" } ],
578       [ 'pentium-.*-vxworks.*',   { target => "vxworks-pentium" } ],
579       [ 'simlinux-.*-vxworks.*',  { target => "vxworks-simlinux" } ],
580       [ 'mips-.*-vxworks.*',      { target => "vxworks-mips" } ],
581       [ 'e2k-.*-linux.*',         { target => "linux-generic64",
582                                     defines => [ 'L_ENDIAN' ] } ],
583       [ 'ia64-.*-linux.',         { target => "linux-ia64" } ],
584       [ 'sparc64-.*-linux2',
585         sub {
586             print <<EOF;
587 WARNING! If you *know* that your GNU C supports 64-bit/V9 ABI and you
588          want to build 64-bit library, do this:
589          $WHERE/Configure linux64-sparcv9
590 EOF
591             maybe_abort();
592             return { target => "linux-sparcv9" };
593         }
594       ],
595       [ 'sparc-.*-linux2',
596         sub {
597             my $KARCH = `awk '/^type/{print \$3;exit(0);}' /proc/cpuinfo`;
598             $KARCH //= "sun4";
599             return { target => "linux-sparcv9" } if $KARCH =~ 'sun4u.*';
600             return { target => "linux-sparcv8" } if $KARCH =~ 'sun4[md]';
601             return { target => "linux-generic32",
602                      defines => [ 'L_ENDIAN' ] };
603         }
604       ],
605       [ 'parisc.*-.*-linux2',
606         sub {
607             # 64-bit builds under parisc64 linux are not supported and
608             # compiler is expected to generate 32-bit objects...
609             my $CPUARCH =
610                 `awk '/cpu family/{print substr(\$5,1,3); exit(0);}' /proc/cpuinfo`;
611             my $CPUSCHEDULE =
612                 `awk '/^cpu.[   ]*: PA/{print substr(\$3,3); exit(0);}' /proc/cpuinfo`;
613             # TODO XXX  Model transformations
614             # 0. CPU Architecture for the 1.1 processor has letter suffixes.
615             #    We strip that off assuming no further arch. identification
616             #    will ever be used by GCC.
617             # 1. I'm most concerned about whether is a 7300LC is closer to a
618             #    7100 versus a 7100LC.
619             # 2. The variant 64-bit processors cause concern should GCC support
620             #    explicit schedulers for these chips in the future.
621             #         PA7300LC -> 7100LC (1.1)
622             #         PA8200   -> 8000   (2.0)
623             #         PA8500   -> 8000   (2.0)
624             #         PA8600   -> 8000   (2.0)
625             $CPUSCHEDULE =~ s/7300LC/7100LC/;
626             $CPUSCHEDULE =~ s/8.00/8000/;
627             return
628                 { target => "linux-generic32",
629                   defines => [ 'B_ENDIAN' ],
630                   cflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ],
631                   cxxflags => [ "-mschedule=$CPUSCHEDULE", "-march=$CPUARCH" ]
632                 };
633         }
634       ],
635       [ 'armv[1-3].*-.*-linux2',  { target => "linux-generic32" } ],
636       [ 'armv[7-9].*-.*-linux2',  { target => "linux-armv4",
637                                     cflags => [ '-march=armv7-a' ],
638                                     cxxflags => [ '-march=armv7-a' ] } ],
639       [ 'arm.*-.*-linux2',        { target => "linux-armv4" } ],
640       [ 'aarch64-.*-linux2',      { target => "linux-aarch64" } ],
641       [ 'sh.*b-.*-linux2',        { target => "linux-generic32",
642                                     defines => [ 'B_ENDIAN' ] } ],
643       [ 'sh.*-.*-linux2',         { target => "linux-generic32",
644                                     defines => [ 'L_ENDIAN' ] } ],
645       [ 'm68k.*-.*-linux2',       { target => "linux-generic32",
646                                     defines => [ 'B_ENDIAN' ] } ],
647       [ 's390-.*-linux2',         { target => "linux-generic32",
648                                     defines => [ 'B_ENDIAN' ] } ],
649       [ 's390x-.*-linux2',
650         sub {
651             # Disabled until a glibc bug is fixed; see Configure.
652             if (0
653                 || okrun('egrep -e \'^features.* highgprs\' /proc/cpuinfo >/dev/null') )
654                 {
655                     print <<EOF;
656 WARNING! To build "highgprs" 32-bit package, do this:
657          $WHERE/Configure linux32-s390x
658 EOF
659                     maybe_abort();
660                 }
661             return { target => "linux64-s390x" };
662         }
663       ],
664       [ 'x86_64-.*-linux.',
665         sub {
666             return { target => "linux-x32" }
667                 if okrun("$CC -dM -E -x c /dev/null 2>&1",
668                          'grep -q ILP32 >/dev/null');
669             return { target => "linux-x86_64" };
670         }
671       ],
672       [ '.*86-.*-linux2',
673         sub {
674             # On machines where the compiler understands -m32, prefer a
675             # config target that uses it
676             return { target => "linux-x86" }
677                 if okrun("$CC -m32 -E -x c /dev/null >/dev/null 2>&1");
678             return { target => "linux-elf" };
679         }
680       ],
681       [ '.*86-.*-linux1',         { target => "linux-aout" } ],
682       [ '.*-.*-linux.',           { target => "linux-generic32" } ],
683       [ 'sun4[uv].*-.*-solaris2',
684         sub {
685             my $KERNEL_BITS = $ENV{KERNEL_BITS};
686             my $ISA64 = `isainfo 2>/dev/null | grep sparcv9`;
687             if ( $ISA64 ne "" && $KERNEL_BITS eq '' ) {
688                 if ( $CCVENDOR eq "sun" && $CCVER >= 500 ) {
689                     print <<EOF;
690 WARNING! To build 64-bit package, do this:
691          $WHERE/Configure solaris64-sparcv9-cc
692 EOF
693                     maybe_abort();
694                 } elsif ( $CCVENDOR eq "gnu" && $GCC_ARCH eq "-m64" ) {
695                     # $GCC_ARCH denotes default ABI chosen by compiler driver
696                     # (first one found on the $PATH). I assume that user
697                     # expects certain consistency with the rest of his builds
698                     # and therefore switch over to 64-bit. <appro>
699                     print <<EOF;
700 WARNING! To build 32-bit package, do this:
701          $WHERE/Configure solaris-sparcv9-gcc
702 EOF
703                     maybe_abort();
704                     return { target => "solaris64-sparcv9" };
705                 } elsif ( $GCC_ARCH eq "-m32" ) {
706                     print <<EOF;
707 NOTICE! If you *know* that your GNU C supports 64-bit/V9 ABI and you wish
708         to build 64-bit library, do this:
709         $WHERE/Configure solaris64-sparcv9-gcc
710 EOF
711                     maybe_abort();
712                 }
713             }
714             return { target => "solaris64-sparcv9" }
715                 if $ISA64 ne "" && $KERNEL_BITS eq '64';
716             return { target => "solaris-sparcv9" };
717         }
718       ],
719       [ 'sun4m-.*-solaris2',      { target => "solaris-sparcv8" } ],
720       [ 'sun4d-.*-solaris2',      { target => "solaris-sparcv8" } ],
721       [ 'sun4.*-.*-solaris2',     { target => "solaris-sparcv7" } ],
722       [ '.*86.*-.*-solaris2',
723         sub {
724             my $KERNEL_BITS = $ENV{KERNEL_BITS};
725             my $ISA64 = `isainfo 2>/dev/null | grep amd64`;
726             my $KB = $KERNEL_BITS // '64';
727             if ($ISA64 ne "" && $KB eq '64') {
728                 return { target => "solaris64-x86_64-gcc" } if $CCVENDOR eq "gnu";
729                 return { target => "solaris64-x86_64-cc" };
730             }
731             my $REL = uname('-r');
732             $REL =~ s/5\.//;
733             my @tmp_disable = ();
734             push @tmp_disable, 'sse2' if int($REL) < 10;
735             #There is no solaris-x86-cc target
736             return { target => "solaris-x86-gcc",
737                      disable => [ @tmp_disable ] };
738         }
739       ],
740       # We don't have any sunos target in Configurations/*.conf, so why here?
741       [ '.*-.*-sunos4',           { target => "sunos" } ],
742       [ '.*86.*-.*-bsdi4',        { target => "BSD-x86-elf",
743                                     lflags => [ '-ldl' ],
744                                     disable => [ 'sse2' ] } ],
745       [ 'alpha.*-.*-.*bsd.*',     { target => "BSD-generic64",
746                                     defines => [ 'L_ENDIAN' ] } ],
747       [ 'powerpc64-.*-.*bsd.*',   { target => "BSD-generic64",
748                                     defines => [ 'B_ENDIAN' ] } ],
749       [ 'sparc64-.*-.*bsd.*',     { target => "BSD-sparc64" } ],
750       [ 'ia64-.*-.*bsd.*',        { target => "BSD-ia64" } ],
751       [ 'x86_64-.*-dragonfly.*',  { target => "BSD-x86_64" } ],
752       [ 'amd64-.*-.*bsd.*',       { target => "BSD-x86_64" } ],
753       [ '.*86.*-.*-.*bsd.*',
754         sub {
755             # mimic ld behaviour when it's looking for libc...
756             my $libc;
757             if ( -l "/usr/lib/libc.so" ) {
758                 $libc = "/usr/lib/libc.so";
759             } else {
760                 # ld searches for highest libc.so.* and so do we
761                 $libc =
762                     `(ls /usr/lib/libc.so.* /lib/libc.so.* | tail -1) 2>/dev/null`;
763             }
764             my $what = `file -L $libc 2>/dev/null`;
765             return { target => "BSD-x86-elf" } if $what =~ /ELF/;
766             return { target => "BSD-x86",
767                      disable => [ 'sse2' ] };
768         }
769       ],
770       [ '.*-.*-.*bsd.*',          { target => "BSD-generic32" } ],
771       [ 'x86_64-.*-haiku',        { target => "haiku-x86_64" } ],
772       [ '.*-.*-haiku',            { target => "haiku-x86" } ],
773       [ '.*-.*-osf',              { target => "osf1-alpha" } ],
774       [ '.*-.*-tru64',            { target => "tru64-alpha" } ],
775       [ '.*-.*-[Uu]nix[Ww]are7',
776         sub {
777             return { target => "unixware-7",
778                      disable => [ 'sse2' ] } if $CCVENDOR eq "gnu";
779             return { target => "unixware-7",
780                      defines => [ '__i386__' ] };
781         }
782       ],
783       [ '.*-.*-[Uu]nix[Ww]are20.*', { target => "unixware-2.0",
784                                       disable => [ 'sse2', 'sha512' ] } ],
785       [ '.*-.*-[Uu]nix[Ww]are21.*', { target => "unixware-2.1",
786                                       disable => [ 'sse2', 'sha512' ] } ],
787       [ '.*-.*-vos',              { target => "vos",
788                                     disable => [ 'threads', 'shared', 'asm',
789                                                  'dso' ] } ],
790       [ 'BS2000-siemens-sysv4',   { target => "BS2000-OSD" } ],
791       [ 'i[3456]86-.*-cygwin',    { target => "Cygwin-x86" } ],
792       [ '.*-.*-cygwin',
793         sub { return { target => "Cygwin-${MACHINE}" } } ],
794       [ 'x86-.*-android|i.86-.*-android', { target => "android-x86" } ],
795       [ 'armv[7-9].*-.*-android', { target => "android-armeabi",
796                                     cflags => [ '-march=armv7-a' ],
797                                     cxxflags => [ '-march=armv7-a' ] } ],
798       [ 'arm.*-.*-android',       { target => "android-armeabi" } ],
799       [ '.*-hpux1.*',
800         sub {
801             my $KERNEL_BITS = $ENV{KERNEL_BITS};
802             my %common_return = ( defines => [ '_REENTRANT' ] );
803             $KERNEL_BITS ||= `getconf KERNEL_BITS 2>/dev/null` // '32';
804             # See <sys/unistd.h> for further info on CPU_VERSION.
805             my $CPU_VERSION = `getconf CPU_VERSION 2>/dev/null` // 0;
806             if ( $CPU_VERSION >= 768 ) {
807                 # IA-64 CPU
808                 return { target => "hpux64-ia64",
809                          %common_return }
810                     if $KERNEL_BITS eq '64' && ! $CCVENDOR;
811                 return { target => "hpux-ia64",
812                          %common_return };
813             }
814             if ( $CPU_VERSION >= 532 ) {
815                 # PA-RISC 2.x CPU
816                 # PA-RISC 2.0 is no longer supported as separate 32-bit
817                 # target. This is compensated for by run-time detection
818                 # in most critical assembly modules and taking advantage
819                 # of 2.0 architecture in PA-RISC 1.1 build.
820                 my $target = ($CCVENDOR eq "gnu" && $GCC_BITS eq '64')
821                     ? "hpux64-parisc2"
822                     : "hpux-parisc1_1";
823                 if ( $KERNEL_BITS eq '64' && ! $CCVENDOR ) {
824                     print <<EOF;
825 WARNING! To build 64-bit package, do this:
826          $WHERE/Configure hpux64-parisc2-cc
827 EOF
828                     maybe_abort();
829                 }
830                 return { target => $target,
831                          %common_return };
832             }
833             # PA-RISC 1.1+ CPU?
834             return { target => "hpux-parisc1_1",
835                      %common_return } if $CPU_VERSION >= 528;
836             # PA-RISC 1.0 CPU
837             return { target => "hpux-parisc",
838                      %common_return } if $CPU_VERSION >= 523;
839             # Motorola(?) CPU
840             return { target => "hpux",
841                      %common_return };
842         }
843       ],
844       [ '.*-hpux',                { target => "hpux-parisc" } ],
845       [ '.*-aix',
846         sub {
847             my %config = ();
848             my $KERNEL_BITS = $ENV{KERNEL_BITS};
849             $KERNEL_BITS ||= `getconf KERNEL_BITMODE 2>/dev/null`;
850             $KERNEL_BITS ||= '32';
851             my $OBJECT_MODE = $ENV{OBJECT_MODE};
852             $OBJECT_MODE ||= 32;
853             $config{target} = "aix";
854             if ( $OBJECT_MODE == 64 ) {
855                 print 'Your $OBJECT_MODE was found to be set to 64';
856                 $config{target} = "aix64";
857             } else {
858                 if ( $CCVENDOR ne 'gnu' && $KERNEL_BITS eq '64' ) {
859                     print <<EOF;
860 WARNING! To build 64-bit package, do this:
861          $WHERE/Configure aix64-cc
862 EOF
863                     maybe_abort();
864                 }
865             }
866             if ( okrun(
867                        "lsattr -E -O -l `lsdev -c processor|awk '{print \$1;exit}'`",
868                        'grep -i powerpc) >/dev/null 2>&1') ) {
869                 # this applies even to Power3 and later, as they return
870                 # PowerPC_POWER[345]
871             } else {
872                 $config{disable} = [ 'asm' ];
873             }
874             return %config;
875         }
876       ],
877
878       # Windows values found by looking at Perl 5's win32/win32.c
879       [ 'amd64-.*?-Windows NT',   { target => 'VC-WIN64A' } ],
880       [ 'ia64-.*?-Windows NT',    { target => 'VC-WIN64I' } ],
881       [ 'x86-.*?-Windows NT',     { target => 'VC-WIN32'  } ],
882
883       # VMS values found by observation on existing machinery.
884       # Unfortunately, the machine part is a bit...  overdone.  It seems,
885       # though, that 'Alpha' exists in that part for Alphas, making it
886       # distinguishable from Itanium.  It will be interesting to see what
887       # we'll get in the upcoming x86_64 port...
888       [ '.*Alpha.*?-.*?-OpenVMS', { target => 'vms-alpha' } ],
889       [ '.*?-.*?-OpenVMS',        { target => 'vms-ia64'  } ],
890
891       # TODO: There are a few more choices among OpenSSL config targets, but
892       # reaching them involves a bit more than just a host tripet.  Select
893       # environment variables could do the job to cover for more granular
894       # build options such as data model (ILP32 or LP64), thread support
895       # model (PUT, SPT or nothing), target execution environment (OSS or
896       # GUARDIAN).  And still, there must be some kind of default when
897       # nothing else is said.
898       #
899       # nsv is a virtual x86 environment, equivalent to nsx, so we enforce
900       # the latter.
901       [ 'nse-tandem-nsk.*',       { target => 'nonstop-nse' } ],
902       [ 'nsv-tandem-nsk.*',       { target => 'nonstop-nsx' } ],
903       [ 'nsx-tandem-nsk.*',       { target => 'nonstop-nsx' } ],
904
905     ];
906
907 # Map GUESSOS into OpenSSL terminology.
908 # Returns a hash table with diverse entries, most importantly 'target',
909 # but also other entries that are fitting for Configure's %config
910 # and MACHINE.
911 # It would be nice to fix this so that this weren't necessary. :( XXX
912 sub map_guess {
913     my $GUESSOS = shift;
914
915     foreach my $tuple ( @$map_patterns ) {
916         my $pat = @$tuple[0];
917         next if $GUESSOS !~ /^${pat}$/;
918         my $result = @$tuple[1];
919         $result = $result->() if ref $result eq 'CODE';
920         return %$result;
921     }
922
923     # Last case, return "z" from x-y-z
924     my @fields = split(/-/, $GUESSOS);
925     return ( target => $fields[2] );
926 }
927
928 # gcc < 2.8 does not support -march=ultrasparc
929 sub check_solaris_sparc8 {
930     my $OUT = shift;
931     if ( $CCVENDOR eq 'gnu' && $CCVER < 208 ) {
932         if ( $OUT eq 'solaris-sparcv9-gcc' ) {
933             print <<EOF;
934 WARNING! Downgrading to solaris-sparcv8-gcc
935          Upgrade to gcc-2.8 or later.
936 EOF
937             maybe_abort();
938             return 'solaris-sparcv8-gcc';
939         }
940         if ( $OUT eq "linux-sparcv9" ) {
941             print <<EOF;
942 WARNING! Downgrading to linux-sparcv8
943          Upgrade to gcc-2.8 or later.
944 EOF
945             maybe_abort();
946             return 'linux-sparcv8';
947         }
948     }
949     return $OUT;
950 }
951
952 ###
953 ###   MAIN PROCESSING
954 ###
955
956 sub get_platform {
957     my %options = @_;
958
959     $VERBOSE = 1 if defined $options{verbose};
960     $WAIT = 0 if defined $options{nowait};
961     $CC = $options{CC};
962     $CROSS_COMPILE = $options{CROSS_COMPILE} // '';
963
964     my $GUESSOS = guess_system();
965     determine_compiler_settings();
966
967     my %ret = map_guess($GUESSOS);
968     $ret{target} = check_solaris_sparc8($ret{target});
969     return %ret;
970 }
971
972 1;