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