#! /usr/bin/env perl
# Copyright 1998-2022 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
# Determine the operating system and run ./Configure. Far descendant from
# Apache's minarch and GuessOS.
package OpenSSL::config;
use strict;
use warnings;
use Getopt::Std;
use File::Basename;
use File::Spec;
use IPC::Cmd;
use POSIX;
use Config;
use Carp;
# These control our behavior.
my $DRYRUN;
my $VERBOSE;
my $WHERE = dirname($0);
my $WAIT = 1;
# Machine type, etc., used to determine the platform
my $MACHINE;
my $RELEASE;
my $SYSTEM;
my $VERSION;
my $CCVENDOR;
my $CCVER;
my $CL_ARCH;
my $GCC_BITS;
my $GCC_ARCH;
# Some environment variables; they will affect Configure
my $CONFIG_OPTIONS = $ENV{CONFIG_OPTIONS} // '';
my $CC;
my $CROSS_COMPILE;
# For determine_compiler_settings, the list of known compilers
my @c_compilers = qw(clang gcc cc);
# Methods to determine compiler version. The expected output is one of
# MAJOR or MAJOR.MINOR or MAJOR.MINOR.PATCH... or false if the compiler
# isn't of the given brand.
# This is a list to ensure that gnu comes last, as we've made it a fallback
my @cc_version =
(
clang => sub {
return undef unless IPC::Cmd::can_run("$CROSS_COMPILE$CC");
my $v = `$CROSS_COMPILE$CC -v 2>&1`;
$v =~ m/(?:(?:clang|LLVM) version|.*based on LLVM)\s+([0-9]+\.[0-9]+)/;
return $1;
},
gnu => sub {
return undef unless IPC::Cmd::can_run("$CROSS_COMPILE$CC");
my $nul = File::Spec->devnull();
my $v = `$CROSS_COMPILE$CC -dumpversion 2> $nul`;
# Strip off whatever prefix egcs prepends the number with.
# Hopefully, this will work for any future prefixes as well.
$v =~ s/^[a-zA-Z]*\-//;
return $v;
},
);
# This is what we will set as the target for calling Configure.
my $options = '';
# Pattern matches against "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}"
# The patterns are assumed to be wrapped like this: /^(${pattern})$/
my $guess_patterns = [
[ 'A\/UX:.*', 'm68k-apple-aux3' ],
[ 'AIX:[3-9]:4:.*', '${MACHINE}-ibm-aix' ],
[ 'AIX:.*?:[5-9]:.*', '${MACHINE}-ibm-aix' ],
[ 'AIX:.*', '${MACHINE}-ibm-aix3' ],
[ 'HI-UX:.*', '${MACHINE}-hi-hiux' ],
[ 'HP-UX:.*',
sub {
my $HPUXVER = $RELEASE;
$HPUXVER = s/[^.]*.[0B]*//;
# HPUX 10 and 11 targets are unified
return "${MACHINE}-hp-hpux1x" if $HPUXVER =~ m@1[0-9]@;
return "${MACHINE}-hp-hpux";
}
],
[ 'IRIX:6\..*', 'mips3-sgi-irix' ],
[ 'IRIX64:.*', 'mips4-sgi-irix64' ],
[ 'Linux:[2-9]\..*', '${MACHINE}-whatever-linux2' ],
[ 'Linux:1\..*', '${MACHINE}-whatever-linux1' ],
[ 'GNU.*', 'hurd-x86' ],
[ 'LynxOS:.*', '${MACHINE}-lynx-lynxos' ],
# BSD/OS always says 386
[ 'BSD\/OS:4\..*', 'i486-whatever-bsdi4' ],
# Order is important, this has to appear before 'BSD\/386:'
[ 'BSD/386:.*?:.*?:.*486.*|BSD/OS:.*?:.*?:.*?:.*486.*',
sub {
my $BSDVAR = `/sbin/sysctl -n hw.model`;
return "i586-whatever-bsdi" if $BSDVAR =~ m@Pentium@;
return "i386-whatever-bsdi";
}
],
[ 'BSD\/386:.*|BSD\/OS:.*', '${MACHINE}-whatever-bsdi' ],
# Order is important, this has to appear before 'FreeBSD:'
[ 'FreeBSD:.*?:.*?:.*386.*',
sub {
my $VERS = $RELEASE;
$VERS =~ s/[-(].*//;
my $MACH = `sysctl -n hw.model`;
$MACH = "i386" if $MACH =~ m@386@;
$MACH = "i486" if $MACH =~ m@486@;
$MACH = "i686" if $MACH =~ m@Pentium II@;
$MACH = "i586" if $MACH =~ m@Pentium@;
$MACH = "$MACHINE" if $MACH !~ /i.86/;
my $ARCH = 'whatever';
$ARCH = "pc" if $MACH =~ m@i[0-9]86@;
return "${MACH}-${ARCH}-freebsd${VERS}";
}
],
[ 'DragonFly:.*', '${MACHINE}-whatever-dragonfly' ],
[ 'FreeBSD:.*'