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