Home | History | Annotate | Line # | Download | only in dist
Configure revision 1.20
      1  1.18  christos #! /usr/bin/env perl
      2  1.18  christos # -*- mode: perl; -*-
      3  1.19  christos # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
      4  1.18  christos #
      5  1.18  christos # Licensed under the OpenSSL license (the "License").  You may not use
      6  1.18  christos # this file except in compliance with the License.  You can obtain a copy
      7  1.18  christos # in the file LICENSE in the source distribution or at
      8  1.18  christos # https://www.openssl.org/source/license.html
      9  1.18  christos 
     10   1.1  christos ##  Configure -- OpenSSL source tree configuration script
     11   1.1  christos 
     12  1.18  christos use 5.10.0;
     13   1.1  christos use strict;
     14  1.19  christos use FindBin;
     15  1.19  christos use lib "$FindBin::Bin/util/perl";
     16  1.18  christos use File::Basename;
     17  1.18  christos use File::Spec::Functions qw/:DEFAULT abs2rel rel2abs/;
     18  1.18  christos use File::Path qw/mkpath/;
     19  1.19  christos use OpenSSL::Glob;
     20   1.1  christos 
     21   1.1  christos # see INSTALL for instructions.
     22   1.1  christos 
     23  1.20  christos my $orig_death_handler = $SIG{__DIE__};
     24  1.20  christos $SIG{__DIE__} = \&death_handler;
     25  1.20  christos 
     26  1.18  christos my $usage="Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-dso] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
     27   1.1  christos 
     28   1.1  christos # Options:
     29   1.1  christos #
     30  1.18  christos # --config      add the given configuration file, which will be read after
     31  1.18  christos #               any "Configurations*" files that are found in the same
     32  1.18  christos #               directory as this script.
     33  1.18  christos # --prefix      prefix for the OpenSSL installation, which includes the
     34  1.18  christos #               directories bin, lib, include, share/man, share/doc/openssl
     35  1.18  christos #               This becomes the value of INSTALLTOP in Makefile
     36  1.18  christos #               (Default: /usr/local)
     37  1.18  christos # --openssldir  OpenSSL data area, such as openssl.cnf, certificates and keys.
     38  1.18  christos #               If it's a relative directory, it will be added on the directory
     39  1.18  christos #               given with --prefix.
     40  1.18  christos #               This becomes the value of OPENSSLDIR in Makefile and in C.
     41  1.18  christos #               (Default: PREFIX/ssl)
     42   1.1  christos #
     43  1.18  christos # --cross-compile-prefix Add specified prefix to binutils components.
     44   1.1  christos #
     45  1.18  christos # --api         One of 0.9.8, 1.0.0 or 1.1.0.  Do not compile support for
     46  1.18  christos #               interfaces deprecated as of the specified OpenSSL version.
     47   1.1  christos #
     48   1.1  christos # no-hw-xxx     do not compile support for specific crypto hardware.
     49   1.1  christos #               Generic OpenSSL-style methods relating to this support
     50   1.1  christos #               are always compiled but return NULL if the hardware
     51   1.1  christos #               support isn't compiled.
     52   1.1  christos # no-hw         do not compile support for any crypto hardware.
     53   1.1  christos # [no-]threads  [don't] try to create a library that is suitable for
     54   1.1  christos #               multithreaded applications (default is "threads" if we
     55   1.1  christos #               know how to do it)
     56   1.1  christos # [no-]shared	[don't] try to create shared libraries when supported.
     57  1.18  christos # [no-]pic      [don't] try to build position independent code when supported.
     58  1.18  christos #               If disabled, it also disables shared and dynamic-engine.
     59   1.1  christos # no-asm        do not use assembler
     60   1.1  christos # no-dso        do not compile in any native shared-library methods. This
     61   1.1  christos #               will ensure that all methods just return NULL.
     62  1.18  christos # no-egd        do not compile support for the entropy-gathering daemon APIs
     63   1.1  christos # [no-]zlib     [don't] compile support for zlib compression.
     64   1.1  christos # zlib-dynamic	Like "zlib", but the zlib library is expected to be a shared
     65   1.1  christos #		library and will be loaded in run-time by the OpenSSL library.
     66   1.3  christos # sctp          include SCTP support
     67  1.15  christos # enable-weak-ssl-ciphers
     68  1.18  christos #               Enable weak ciphers that are disabled by default.
     69  1.18  christos # 386           generate 80386 code in assembly modules
     70  1.18  christos # no-sse2       disables IA-32 SSE2 code in assembly modules, the above
     71  1.18  christos #               mentioned '386' option implies this one
     72   1.1  christos # no-<cipher>   build without specified algorithm (rsa, idea, rc5, ...)
     73  1.18  christos # -<xxx> +<xxx> compiler options are passed through
     74  1.18  christos # -static       while -static is also a pass-through compiler option (and
     75  1.18  christos #               as such is limited to environments where it's actually
     76  1.18  christos #               meaningful), it triggers a number configuration options,
     77  1.18  christos #               namely no-dso, no-pic, no-shared and no-threads. It is
     78  1.18  christos #               argued that the only reason to produce statically linked
     79  1.18  christos #               binaries (and in context it means executables linked with
     80  1.18  christos #               -static flag, and not just executables linked with static
     81  1.18  christos #               libcrypto.a) is to eliminate dependency on specific run-time,
     82  1.18  christos #               a.k.a. libc version. The mentioned config options are meant
     83  1.18  christos #               to achieve just that. Unfortunately on Linux it's impossible
     84  1.18  christos #               to eliminate the dependency completely for openssl executable
     85  1.18  christos #               because of getaddrinfo and gethostbyname calls, which can
     86  1.18  christos #               invoke dynamically loadable library facility anyway to meet
     87  1.18  christos #               the lookup requests. For this reason on Linux statically
     88  1.18  christos #               linked openssl executable has rather debugging value than
     89  1.18  christos #               production quality.
     90   1.1  christos #
     91   1.1  christos # DEBUG_SAFESTACK use type-safe stacks to enforce type-safety on stack items
     92   1.1  christos #		provided to stack calls. Generates unique stack functions for
     93   1.1  christos #		each possible stack type.
     94   1.1  christos # BN_LLONG	use the type 'long long' in crypto/bn/bn.h
     95   1.1  christos # RC4_CHAR	use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
     96   1.1  christos # Following are set automatically by this script
     97   1.1  christos #
     98  1.18  christos # MD5_ASM	use some extra md5 assembler,
     99  1.18  christos # SHA1_ASM	use some extra sha1 assembler, must define L_ENDIAN for x86
    100  1.18  christos # RMD160_ASM	use some extra ripemd160 assembler,
    101   1.1  christos # SHA256_ASM	sha256_block is implemented in assembler
    102   1.1  christos # SHA512_ASM	sha512_block is implemented in assembler
    103  1.18  christos # AES_ASM	AES_[en|de]crypt is implemented in assembler
    104   1.1  christos 
    105   1.1  christos # Minimum warning options... any contributions to OpenSSL should at least get
    106  1.18  christos # past these.
    107   1.1  christos 
    108  1.18  christos # DEBUG_UNUSED enables __owur (warn unused result) checks.
    109  1.18  christos my $gcc_devteam_warn = "-DDEBUG_UNUSED"
    110  1.18  christos         # -DPEDANTIC complements -pedantic and is meant to mask code that
    111  1.18  christos         # is not strictly standard-compliant and/or implementation-specific,
    112  1.18  christos         # e.g. inline assembly, disregards to alignment requirements, such
    113  1.18  christos         # that -pedantic would complain about. Incidentally -DPEDANTIC has
    114  1.18  christos         # to be used even in sanitized builds, because sanitizer too is
    115  1.18  christos         # supposed to and does take notice of non-standard behaviour. Then
    116  1.18  christos         # -pedantic with pre-C9x compiler would also complain about 'long
    117  1.18  christos         # long' not being supported. As 64-bit algorithms are common now,
    118  1.18  christos         # it grew impossible to resolve this without sizeable additional
    119  1.18  christos         # code, so we just tell compiler to be pedantic about everything
    120  1.18  christos         # but 'long long' type.
    121  1.18  christos         . " -DPEDANTIC -pedantic -Wno-long-long"
    122  1.18  christos         . " -Wall"
    123  1.18  christos         . " -Wextra"
    124  1.18  christos         . " -Wno-unused-parameter"
    125  1.18  christos         . " -Wno-missing-field-initializers"
    126  1.18  christos         . " -Wsign-compare"
    127  1.18  christos         . " -Wmissing-prototypes"
    128  1.18  christos         . " -Wshadow"
    129  1.18  christos         . " -Wformat"
    130  1.18  christos         . " -Wtype-limits"
    131  1.18  christos         . " -Wundef"
    132  1.18  christos         . " -Werror"
    133  1.18  christos         ;
    134  1.16       spz 
    135  1.16       spz # These are used in addition to $gcc_devteam_warn when the compiler is clang.
    136  1.16       spz # TODO(openssl-team): fix problems and investigate if (at least) the
    137  1.18  christos # following warnings can also be enabled:
    138  1.18  christos #       -Wswitch-enum
    139  1.18  christos #       -Wcast-align
    140  1.18  christos #       -Wunreachable-code
    141  1.18  christos #       -Wlanguage-extension-token -- no, we use asm()
    142  1.18  christos #       -Wunused-macros -- no, too tricky for BN and _XOPEN_SOURCE etc
    143  1.18  christos #       -Wextended-offsetof -- no, needed in CMS ASN1 code
    144  1.18  christos my $clang_devteam_warn = ""
    145  1.18  christos         . " -Qunused-arguments"
    146  1.18  christos         . " -Wno-language-extension-token"
    147  1.18  christos         . " -Wno-extended-offsetof"
    148  1.18  christos         . " -Wconditional-uninitialized"
    149  1.18  christos         . " -Wincompatible-pointer-types-discards-qualifiers"
    150  1.18  christos         . " -Wmissing-variable-declarations"
    151  1.18  christos         ;
    152  1.18  christos 
    153  1.18  christos # This adds backtrace information to the memory leak info.  Is only used
    154  1.18  christos # when crypto-mdebug-backtrace is enabled.
    155  1.18  christos my $memleak_devteam_backtrace = "-rdynamic";
    156  1.14  christos 
    157   1.2       spz my $strict_warnings = 0;
    158   1.2       spz 
    159   1.1  christos # As for $BSDthreads. Idea is to maintain "collective" set of flags,
    160  1.18  christos # which would cover all BSD flavors. -pthread applies to them all,
    161   1.1  christos # but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
    162   1.1  christos # -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
    163   1.1  christos # which has to be accompanied by explicit -D_THREAD_SAFE and
    164   1.1  christos # sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
    165   1.1  christos # seems to be sufficient?
    166  1.18  christos our $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
    167  1.18  christos 
    168  1.18  christos #
    169  1.18  christos # API compatibility name to version number mapping.
    170  1.18  christos #
    171  1.18  christos my $maxapi = "1.1.0";           # API for "no-deprecated" builds
    172  1.18  christos my $apitable = {
    173  1.18  christos     "1.1.0" => "0x10100000L",
    174  1.18  christos     "1.0.0" => "0x10000000L",
    175  1.18  christos     "0.9.8" => "0x00908000L",
    176  1.18  christos };
    177  1.18  christos 
    178  1.18  christos our %table = ();
    179  1.18  christos our %config = ();
    180  1.18  christos our %withargs = ();
    181  1.18  christos 
    182  1.18  christos # Forward declarations ###############################################
    183  1.18  christos 
    184  1.18  christos # read_config(filename)
    185  1.18  christos #
    186  1.18  christos # Reads a configuration file and populates %table with the contents
    187  1.18  christos # (which the configuration file places in %targets).
    188  1.18  christos sub read_config;
    189  1.18  christos 
    190  1.18  christos # resolve_config(target)
    191  1.18  christos #
    192  1.18  christos # Resolves all the late evaluations, inheritances and so on for the
    193  1.18  christos # chosen target and any target it inherits from.
    194  1.18  christos sub resolve_config;
    195  1.18  christos 
    196  1.18  christos 
    197  1.18  christos # Information collection #############################################
    198  1.18  christos 
    199  1.18  christos # Unified build supports separate build dir
    200  1.18  christos my $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
    201  1.18  christos my $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
    202  1.18  christos my $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
    203  1.18  christos 
    204  1.18  christos my $local_config_envname = 'OPENSSL_LOCAL_CONFIG_DIR';
    205  1.18  christos 
    206  1.18  christos $config{sourcedir} = abs2rel($srcdir);
    207  1.18  christos $config{builddir} = abs2rel($blddir);
    208  1.18  christos 
    209  1.18  christos # Collect reconfiguration information if needed
    210  1.18  christos my @argvcopy=@ARGV;
    211  1.18  christos 
    212  1.18  christos if (grep /^reconf(igure)?$/, @argvcopy) {
    213  1.18  christos     if (-f "./configdata.pm") {
    214  1.18  christos 	my $file = "./configdata.pm";
    215  1.18  christos 	unless (my $return = do $file) {
    216  1.18  christos 	    die "couldn't parse $file: $@" if $@;
    217  1.18  christos             die "couldn't do $file: $!"    unless defined $return;
    218  1.18  christos             die "couldn't run $file"       unless $return;
    219  1.18  christos 	}
    220  1.18  christos 
    221  1.18  christos 	@argvcopy = defined($configdata::config{perlargv}) ?
    222  1.18  christos 	    @{$configdata::config{perlargv}} : ();
    223  1.18  christos 	die "Incorrect data to reconfigure, please do a normal configuration\n"
    224  1.18  christos 	    if (grep(/^reconf/,@argvcopy));
    225  1.18  christos 	$ENV{CROSS_COMPILE} = $configdata::config{cross_compile_prefix}
    226  1.18  christos 	    if defined($configdata::config{cross_compile_prefix});
    227  1.18  christos 	$ENV{CC} = $configdata::config{cc}
    228  1.18  christos 	    if defined($configdata::config{cc});
    229  1.18  christos 	$ENV{BUILDFILE} = $configdata::config{build_file}
    230  1.18  christos 	    if defined($configdata::config{build_file});
    231  1.18  christos 	$ENV{$local_config_envname} = $configdata::config{local_config_dir}
    232  1.18  christos 	    if defined($configdata::config{local_config_dir});
    233  1.18  christos 
    234  1.18  christos 	print "Reconfiguring with: ", join(" ",@argvcopy), "\n";
    235  1.18  christos 	print "    CROSS_COMPILE = ",$ENV{CROSS_COMPILE},"\n"
    236  1.18  christos 	    if $ENV{CROSS_COMPILE};
    237  1.18  christos 	print "    CC = ",$ENV{CC},"\n" if $ENV{CC};
    238  1.18  christos 	print "    BUILDFILE = ",$ENV{BUILDFILE},"\n" if $ENV{BUILDFILE};
    239  1.18  christos 	print "    $local_config_envname = ",$ENV{$local_config_envname},"\n"
    240  1.18  christos 	    if $ENV{$local_config_envname};
    241  1.18  christos     } else {
    242  1.18  christos 	die "Insufficient data to reconfigure, please do a normal configuration\n";
    243  1.18  christos     }
    244  1.18  christos }
    245  1.18  christos 
    246  1.18  christos $config{perlargv} = [ @argvcopy ];
    247   1.1  christos 
    248  1.18  christos # Collect version numbers
    249  1.18  christos $config{version} = "unknown";
    250  1.18  christos $config{version_num} = "unknown";
    251  1.18  christos $config{shlib_version_number} = "unknown";
    252  1.18  christos $config{shlib_version_history} = "unknown";
    253  1.18  christos 
    254  1.18  christos collect_information(
    255  1.18  christos     collect_from_file(catfile($srcdir,'include/openssl/opensslv.h')),
    256  1.18  christos     qr/OPENSSL.VERSION.TEXT.*OpenSSL (\S+) / => sub { $config{version} = $1; },
    257  1.18  christos     qr/OPENSSL.VERSION.NUMBER.*(0x\S+)/	     => sub { $config{version_num}=$1 },
    258  1.18  christos     qr/SHLIB_VERSION_NUMBER *"([^"]+)"/	     => sub { $config{shlib_version_number}=$1 },
    259  1.18  christos     qr/SHLIB_VERSION_HISTORY *"([^"]*)"/     => sub { $config{shlib_version_history}=$1 }
    260  1.18  christos     );
    261  1.18  christos if ($config{shlib_version_history} ne "") { $config{shlib_version_history} .= ":"; }
    262  1.18  christos 
    263  1.18  christos ($config{major}, $config{minor})
    264  1.18  christos     = ($config{version} =~ /^([0-9]+)\.([0-9\.]+)/);
    265  1.18  christos ($config{shlib_major}, $config{shlib_minor})
    266  1.18  christos     = ($config{shlib_version_number} =~ /^([0-9]+)\.([0-9\.]+)/);
    267  1.18  christos die "erroneous version information in opensslv.h: ",
    268  1.18  christos     "$config{major}, $config{minor}, $config{shlib_major}, $config{shlib_minor}\n"
    269  1.18  christos     if ($config{major} eq "" || $config{minor} eq ""
    270  1.18  christos 	|| $config{shlib_major} eq "" ||  $config{shlib_minor} eq "");
    271  1.18  christos 
    272  1.18  christos # Collect target configurations
    273  1.18  christos 
    274  1.18  christos my $pattern = catfile(dirname($0), "Configurations", "*.conf");
    275  1.18  christos foreach (sort glob($pattern)) {
    276  1.18  christos     &read_config($_);
    277  1.18  christos }
    278   1.1  christos 
    279  1.18  christos if (defined $ENV{$local_config_envname}) {
    280  1.18  christos     if ($^O eq 'VMS') {
    281  1.18  christos         # VMS environment variables are logical names,
    282  1.18  christos         # which can be used as is
    283  1.18  christos         $pattern = $local_config_envname . ':' . '*.conf';
    284  1.18  christos     } else {
    285  1.18  christos         $pattern = catfile($ENV{$local_config_envname}, '*.conf');
    286  1.18  christos     }
    287  1.18  christos 
    288  1.18  christos     foreach (sort glob($pattern)) {
    289  1.18  christos         &read_config($_);
    290  1.18  christos     }
    291  1.18  christos }
    292   1.1  christos 
    293   1.1  christos 
    294  1.18  christos print "Configuring OpenSSL version $config{version} ($config{version_num})\n";
    295  1.18  christos 
    296  1.18  christos $config{prefix}="";
    297  1.18  christos $config{openssldir}="";
    298  1.18  christos $config{processor}="";
    299  1.18  christos $config{libdir}="";
    300  1.18  christos $config{cross_compile_prefix}="";
    301  1.18  christos $config{fipslibdir}="/usr/local/ssl/fips-2.0/lib/";
    302  1.18  christos my $nofipscanistercheck=0;
    303  1.18  christos $config{baseaddr}="0xFB00000";
    304  1.18  christos my $auto_threads=1;    # enable threads automatically? true by default
    305   1.1  christos my $default_ranlib;
    306  1.18  christos $config{fips}=0;
    307   1.1  christos 
    308  1.18  christos # Top level directories to build
    309  1.18  christos $config{dirs} = [ "crypto", "ssl", "engines", "apps", "test", "util", "tools", "fuzz" ];
    310  1.18  christos # crypto/ subdirectories to build
    311  1.18  christos $config{sdirs} = [
    312  1.18  christos     "objects",
    313  1.18  christos     "md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305", "blake2",
    314  1.18  christos     "des", "aes", "rc2", "rc4", "rc5", "idea", "bf", "cast", "camellia", "seed", "chacha", "modes",
    315  1.18  christos     "bn", "ec", "rsa", "dsa", "dh", "dso", "engine",
    316  1.18  christos     "buffer", "bio", "stack", "lhash", "rand", "err",
    317  1.18  christos     "evp", "asn1", "pem", "x509", "x509v3", "conf", "txt_db", "pkcs7", "pkcs12", "comp", "ocsp", "ui",
    318  1.18  christos     "cms", "ts", "srp", "cmac", "ct", "async", "kdf"
    319  1.18  christos     ];
    320  1.18  christos 
    321  1.18  christos # Known TLS and DTLS protocols
    322  1.18  christos my @tls = qw(ssl3 tls1 tls1_1 tls1_2);
    323  1.18  christos my @dtls = qw(dtls1 dtls1_2);
    324  1.18  christos 
    325  1.18  christos # Explicitly known options that are possible to disable.  They can
    326  1.18  christos # be regexps, and will be used like this: /^no-${option}$/
    327  1.18  christos # For developers: keep it sorted alphabetically
    328  1.18  christos 
    329  1.18  christos my @disablables = (
    330  1.18  christos     "afalgeng",
    331  1.18  christos     "asan",
    332  1.18  christos     "asm",
    333  1.18  christos     "async",
    334  1.18  christos     "autoalginit",
    335  1.18  christos     "autoerrinit",
    336  1.18  christos     "bf",
    337  1.18  christos     "blake2",
    338  1.18  christos     "camellia",
    339  1.18  christos     "capieng",
    340  1.18  christos     "cast",
    341  1.18  christos     "chacha",
    342  1.18  christos     "cmac",
    343  1.18  christos     "cms",
    344  1.18  christos     "comp",
    345  1.18  christos     "crypto-mdebug",
    346  1.18  christos     "crypto-mdebug-backtrace",
    347  1.18  christos     "ct",
    348  1.18  christos     "deprecated",
    349  1.18  christos     "des",
    350  1.18  christos     "dgram",
    351  1.18  christos     "dh",
    352  1.18  christos     "dsa",
    353  1.18  christos     "dso",
    354  1.18  christos     "dtls",
    355  1.18  christos     "dynamic-engine",
    356  1.18  christos     "ec",
    357  1.18  christos     "ec2m",
    358  1.18  christos     "ecdh",
    359  1.18  christos     "ecdsa",
    360  1.18  christos     "ec_nistp_64_gcc_128",
    361  1.18  christos     "egd",
    362  1.18  christos     "engine",
    363  1.18  christos     "err",
    364  1.18  christos     "filenames",
    365  1.18  christos     "fuzz-libfuzzer",
    366  1.18  christos     "fuzz-afl",
    367  1.18  christos     "gost",
    368  1.18  christos     "heartbeats",
    369  1.18  christos     "hw(-.+)?",
    370  1.18  christos     "idea",
    371  1.18  christos     "makedepend",
    372  1.18  christos     "md2",
    373  1.18  christos     "md4",
    374  1.18  christos     "mdc2",
    375  1.18  christos     "msan",
    376  1.18  christos     "multiblock",
    377  1.18  christos     "nextprotoneg",
    378  1.18  christos     "ocb",
    379  1.18  christos     "ocsp",
    380  1.18  christos     "pic",
    381  1.18  christos     "poly1305",
    382  1.18  christos     "posix-io",
    383  1.18  christos     "psk",
    384  1.18  christos     "rc2",
    385  1.18  christos     "rc4",
    386  1.18  christos     "rc5",
    387  1.18  christos     "rdrand",
    388  1.18  christos     "rfc3779",
    389  1.18  christos     "rmd160",
    390  1.18  christos     "scrypt",
    391  1.18  christos     "sctp",
    392  1.18  christos     "seed",
    393  1.18  christos     "shared",
    394  1.18  christos     "sock",
    395  1.18  christos     "srp",
    396  1.18  christos     "srtp",
    397  1.18  christos     "sse2",
    398  1.18  christos     "ssl",
    399  1.18  christos     "ssl-trace",
    400  1.18  christos     "static-engine",
    401  1.18  christos     "stdio",
    402  1.18  christos     "threads",
    403  1.18  christos     "tls",
    404  1.18  christos     "ts",
    405  1.18  christos     "ubsan",
    406  1.18  christos     "ui",
    407  1.18  christos     "unit-test",
    408  1.18  christos     "whirlpool",
    409  1.18  christos     "weak-ssl-ciphers",
    410  1.18  christos     "zlib",
    411  1.18  christos     "zlib-dynamic",
    412  1.18  christos     );
    413  1.18  christos foreach my $proto ((@tls, @dtls))
    414  1.18  christos 	{
    415  1.18  christos 	push(@disablables, $proto);
    416  1.18  christos 	push(@disablables, "$proto-method");
    417  1.18  christos 	}
    418  1.18  christos 
    419  1.18  christos my %deprecated_disablables = (
    420  1.18  christos     "ssl2" => undef,
    421  1.18  christos     "buf-freelists" => undef,
    422  1.18  christos     "ripemd" => "rmd160"
    423  1.18  christos     );
    424   1.1  christos 
    425   1.1  christos # All of the following is disabled by default (RC5 was enabled before 0.9.8):
    426   1.1  christos 
    427  1.18  christos our %disabled = ( # "what"         => "comment"
    428  1.18  christos                   "asan"		=> "default",
    429  1.18  christos 		  "crypto-mdebug"       => "default",
    430  1.18  christos 		  "crypto-mdebug-backtrace" => "default",
    431  1.18  christos 		  "ec_nistp_64_gcc_128" => "default",
    432  1.18  christos 		  "egd"                 => "default",
    433  1.18  christos 		  "fuzz-libfuzzer"	=> "default",
    434  1.18  christos 		  "fuzz-afl"		=> "default",
    435  1.18  christos 		  "heartbeats"          => "default",
    436  1.18  christos 		  "md2"                 => "default",
    437  1.18  christos                   "msan"                => "default",
    438  1.18  christos 		  "rc5"                 => "default",
    439  1.18  christos 		  "sctp"                => "default",
    440  1.18  christos 		  "ssl-trace"           => "default",
    441  1.18  christos 		  "ssl3"                => "default",
    442  1.18  christos 		  "ssl3-method"         => "default",
    443  1.18  christos                   "ubsan"		=> "default",
    444  1.18  christos 		  "unit-test"           => "default",
    445  1.18  christos 		  "weak-ssl-ciphers"    => "default",
    446  1.18  christos 		  "zlib"                => "default",
    447  1.18  christos 		  "zlib-dynamic"        => "default",
    448  1.18  christos 		);
    449  1.18  christos 
    450  1.18  christos # Note: => pair form used for aesthetics, not to truly make a hash table
    451  1.18  christos my @disable_cascades = (
    452  1.18  christos     # "what"		=> [ "cascade", ... ]
    453  1.18  christos     sub { $config{processor} eq "386" }
    454  1.18  christos 			=> [ "sse2" ],
    455  1.18  christos     "ssl"		=> [ "ssl3" ],
    456  1.18  christos     "ssl3-method"	=> [ "ssl3" ],
    457  1.18  christos     "zlib"		=> [ "zlib-dynamic" ],
    458  1.18  christos     "des"		=> [ "mdc2" ],
    459  1.18  christos     "ec"		=> [ "ecdsa", "ecdh" ],
    460  1.18  christos 
    461  1.18  christos     "dgram"		=> [ "dtls", "sctp" ],
    462  1.18  christos     "sock"		=> [ "dgram" ],
    463  1.18  christos     "dtls"		=> [ @dtls ],
    464  1.18  christos     sub { 0 == scalar grep { !$disabled{$_} } @dtls }
    465  1.18  christos 			=> [ "dtls" ],
    466  1.18  christos 
    467  1.18  christos     "tls"		=> [ @tls ],
    468  1.18  christos     sub { 0 == scalar grep { !$disabled{$_} } @tls }
    469  1.18  christos 			=> [ "tls" ],
    470  1.18  christos 
    471  1.18  christos     "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
    472  1.18  christos 
    473  1.18  christos     # Without DSO, we can't load dynamic engines, so don't build them dynamic
    474  1.18  christos     "dso"               => [ "dynamic-engine" ],
    475  1.18  christos 
    476  1.18  christos     # Without position independent code, there can be no shared libraries or DSOs
    477  1.18  christos     "pic"               => [ "shared" ],
    478  1.18  christos     "shared"            => [ "dynamic-engine" ],
    479  1.18  christos     "engine"            => [ "afalgeng" ],
    480  1.18  christos 
    481  1.18  christos     # no-autoalginit is only useful when building non-shared
    482  1.18  christos     "autoalginit"       => [ "shared", "apps" ],
    483  1.18  christos 
    484  1.18  christos     "stdio"             => [ "apps", "capieng", "egd" ],
    485  1.18  christos     "apps"              => [ "tests" ],
    486  1.18  christos     "comp"		=> [ "zlib" ],
    487  1.18  christos     sub { !$disabled{"unit-test"} } => [ "heartbeats" ],
    488  1.18  christos 
    489  1.18  christos     sub { !$disabled{"msan"} } => [ "asm" ],
    490  1.18  christos     );
    491  1.18  christos 
    492  1.18  christos # Avoid protocol support holes.  Also disable all versions below N, if version
    493  1.18  christos # N is disabled while N+1 is enabled.
    494  1.18  christos #
    495  1.18  christos my @list = (reverse @tls);
    496  1.18  christos while ((my $first, my $second) = (shift @list, shift @list)) {
    497  1.18  christos     last unless @list;
    498  1.18  christos     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
    499  1.18  christos 			      => [ @list ] );
    500  1.18  christos     unshift @list, $second;
    501  1.18  christos }
    502  1.18  christos my @list = (reverse @dtls);
    503  1.18  christos while ((my $first, my $second) = (shift @list, shift @list)) {
    504  1.18  christos     last unless @list;
    505  1.18  christos     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
    506  1.18  christos 			      => [ @list ] );
    507  1.18  christos     unshift @list, $second;
    508  1.18  christos }
    509   1.1  christos 
    510   1.1  christos # Explicit "no-..." options will be collected in %disabled along with the defaults.
    511  1.18  christos # To remove something from %disabled, use "enable-foo".
    512   1.1  christos # For symmetry, "disable-foo" is a synonym for "no-foo".
    513   1.1  christos 
    514   1.1  christos &usage if ($#ARGV < 0);
    515   1.1  christos 
    516  1.18  christos my $user_cflags="";
    517  1.18  christos my @user_defines=();
    518  1.18  christos $config{openssl_api_defines}=[];
    519  1.18  christos $config{openssl_algorithm_defines}=[];
    520  1.18  christos $config{openssl_thread_defines}=[];
    521  1.18  christos $config{openssl_sys_defines}=[];
    522  1.18  christos $config{openssl_other_defines}=[];
    523  1.18  christos my $libs="";
    524  1.18  christos my $target="";
    525  1.18  christos $config{options}="";
    526  1.18  christos $config{build_type} = "release";
    527  1.18  christos 
    528  1.18  christos my %unsupported_options = ();
    529  1.18  christos my %deprecated_options = ();
    530  1.18  christos while (@argvcopy)
    531  1.18  christos 	{
    532  1.18  christos 	$_ = shift @argvcopy;
    533  1.18  christos 	# VMS is a case insensitive environment, and depending on settings
    534  1.18  christos 	# out of our control, we may receive options uppercased.  Let's
    535  1.18  christos 	# downcase at least the part before any equal sign.
    536  1.18  christos 	if ($^O eq "VMS")
    537  1.18  christos 		{
    538  1.18  christos 		s/^([^=]*)/lc($1)/e;
    539  1.18  christos 		}
    540  1.18  christos 	s /^-no-/no-/; # some people just can't read the instructions
    541  1.18  christos 
    542  1.18  christos 	# rewrite some options in "enable-..." form
    543  1.18  christos 	s /^-?-?shared$/enable-shared/;
    544  1.18  christos 	s /^sctp$/enable-sctp/;
    545  1.18  christos 	s /^threads$/enable-threads/;
    546  1.18  christos 	s /^zlib$/enable-zlib/;
    547  1.18  christos 	s /^zlib-dynamic$/enable-zlib-dynamic/;
    548  1.18  christos 
    549  1.18  christos         if (/^(no|disable|enable)-(.+)$/)
    550  1.18  christos                 {
    551  1.18  christos                 my $word = $2;
    552  1.18  christos                 if (!exists $deprecated_disablables{$word}
    553  1.18  christos                         && !grep { $word =~ /^${_}$/ } @disablables)
    554  1.18  christos                         {
    555  1.18  christos                         $unsupported_options{$_} = 1;
    556  1.18  christos                         next;
    557  1.18  christos                         }
    558  1.18  christos                 }
    559  1.18  christos         if (/^no-(.+)$/ || /^disable-(.+)$/)
    560  1.18  christos                 {
    561  1.18  christos                 foreach my $proto ((@tls, @dtls))
    562  1.18  christos                         {
    563  1.18  christos                         if ($1 eq "$proto-method")
    564  1.18  christos                                 {
    565  1.18  christos                                 $disabled{"$proto"} = "option($proto-method)";
    566  1.18  christos                                 last;
    567  1.18  christos                                 }
    568  1.18  christos                         }
    569  1.18  christos                 if ($1 eq "dtls")
    570  1.18  christos                         {
    571  1.18  christos                         foreach my $proto (@dtls)
    572  1.18  christos                                 {
    573  1.18  christos                                 $disabled{$proto} = "option(dtls)";
    574  1.18  christos                                 }
    575  1.18  christos                         $disabled{"dtls"} = "option(dtls)";
    576  1.18  christos                         }
    577  1.18  christos                 elsif ($1 eq "ssl")
    578  1.18  christos                         {
    579  1.18  christos                         # Last one of its kind
    580  1.18  christos                         $disabled{"ssl3"} = "option(ssl)";
    581  1.18  christos                         }
    582  1.18  christos                 elsif ($1 eq "tls")
    583  1.18  christos                         {
    584  1.18  christos                         # XXX: Tests will fail if all SSL/TLS
    585  1.18  christos                         # protocols are disabled.
    586  1.18  christos                         foreach my $proto (@tls)
    587  1.18  christos                                 {
    588  1.18  christos                                 $disabled{$proto} = "option(tls)";
    589  1.18  christos                                 }
    590  1.18  christos                         }
    591  1.18  christos                 elsif ($1 eq "static-engine")
    592  1.18  christos                         {
    593  1.18  christos                         delete $disabled{"dynamic-engine"};
    594  1.18  christos                         }
    595  1.18  christos                 elsif ($1 eq "dynamic-engine")
    596  1.18  christos                         {
    597  1.18  christos                         $disabled{"dynamic-engine"} = "option";
    598  1.18  christos                         }
    599  1.18  christos                 elsif (exists $deprecated_disablables{$1})
    600  1.18  christos                         {
    601  1.18  christos                         $deprecated_options{$_} = 1;
    602  1.18  christos                         if (defined $deprecated_disablables{$1})
    603  1.18  christos                                 {
    604  1.18  christos                                 $disabled{$deprecated_disablables{$1}} = "option";
    605  1.18  christos                                 }
    606  1.18  christos                         }
    607  1.18  christos                 else
    608  1.18  christos                         {
    609  1.18  christos                         $disabled{$1} = "option";
    610  1.18  christos                         }
    611  1.18  christos 		# No longer an automatic choice
    612  1.18  christos 		$auto_threads = 0 if ($1 eq "threads");
    613  1.18  christos 		}
    614  1.18  christos 	elsif (/^enable-(.+)$/)
    615  1.18  christos 		{
    616  1.18  christos                 if ($1 eq "static-engine")
    617  1.18  christos                         {
    618  1.18  christos                         $disabled{"dynamic-engine"} = "option";
    619  1.18  christos                         }
    620  1.18  christos                 elsif ($1 eq "dynamic-engine")
    621  1.18  christos                         {
    622  1.18  christos                         delete $disabled{"dynamic-engine"};
    623  1.18  christos                         }
    624  1.18  christos                 elsif ($1 eq "zlib-dynamic")
    625  1.18  christos                         {
    626  1.18  christos                         delete $disabled{"zlib"};
    627  1.18  christos                         }
    628  1.18  christos 		my $algo = $1;
    629  1.18  christos 		delete $disabled{$algo};
    630  1.18  christos 
    631  1.18  christos 		# No longer an automatic choice
    632  1.18  christos 		$auto_threads = 0 if ($1 eq "threads");
    633  1.18  christos 		}
    634  1.18  christos 	elsif (/^--strict-warnings$/)
    635  1.18  christos 		{
    636  1.18  christos 		$strict_warnings = 1;
    637  1.18  christos 		}
    638  1.18  christos 	elsif (/^--debug$/)
    639  1.18  christos 		{
    640  1.18  christos 		$config{build_type} = "debug";
    641  1.18  christos 		}
    642  1.18  christos 	elsif (/^--release$/)
    643  1.18  christos 		{
    644  1.18  christos 		$config{build_type} = "release";
    645  1.18  christos 		}
    646  1.18  christos 	elsif (/^386$/)
    647  1.18  christos 		{ $config{processor}=386; }
    648  1.18  christos 	elsif (/^fips$/)
    649  1.18  christos 		{
    650  1.18  christos 		$config{fips}=1;
    651  1.18  christos 		}
    652  1.18  christos 	elsif (/^rsaref$/)
    653  1.18  christos 		{
    654  1.18  christos 		# No RSAref support any more since it's not needed.
    655  1.18  christos 		# The check for the option is there so scripts aren't
    656  1.18  christos 		# broken
    657  1.18  christos 		}
    658  1.18  christos 	elsif (/^nofipscanistercheck$/)
    659  1.18  christos 		{
    660  1.18  christos 		$config{fips} = 1;
    661  1.18  christos 		$nofipscanistercheck = 1;
    662  1.18  christos 		}
    663  1.18  christos 	elsif (/^[-+]/)
    664  1.18  christos 		{
    665  1.18  christos 		if (/^--prefix=(.*)$/)
    666  1.18  christos 			{
    667  1.18  christos 			$config{prefix}=$1;
    668  1.18  christos 			die "Directory given with --prefix MUST be absolute\n"
    669  1.18  christos 				unless file_name_is_absolute($config{prefix});
    670  1.18  christos 			}
    671  1.18  christos 		elsif (/^--api=(.*)$/)
    672  1.18  christos 			{
    673  1.18  christos 			$config{api}=$1;
    674  1.18  christos 			}
    675  1.18  christos 		elsif (/^--libdir=(.*)$/)
    676  1.18  christos 			{
    677  1.18  christos 			$config{libdir}=$1;
    678  1.18  christos 			}
    679  1.18  christos 		elsif (/^--openssldir=(.*)$/)
    680  1.18  christos 			{
    681  1.18  christos 			$config{openssldir}=$1;
    682  1.18  christos 			}
    683  1.18  christos 		elsif (/^--with-zlib-lib=(.*)$/)
    684  1.18  christos 			{
    685  1.18  christos 			$withargs{zlib_lib}=$1;
    686  1.18  christos 			}
    687  1.18  christos 		elsif (/^--with-zlib-include=(.*)$/)
    688  1.18  christos 			{
    689  1.18  christos 			$withargs{zlib_include}=$1;
    690  1.18  christos 			}
    691  1.18  christos 		elsif (/^--with-fuzzer-lib=(.*)$/)
    692  1.18  christos 			{
    693  1.18  christos 			$withargs{fuzzer_lib}=$1;
    694  1.18  christos 			}
    695  1.18  christos 		elsif (/^--with-fuzzer-include=(.*)$/)
    696  1.18  christos 			{
    697  1.18  christos 			$withargs{fuzzer_include}=$1;
    698  1.18  christos 			}
    699  1.18  christos 		elsif (/^--with-fipslibdir=(.*)$/)
    700  1.18  christos 			{
    701  1.18  christos 			$config{fipslibdir}="$1/";
    702  1.18  christos 			}
    703  1.18  christos 		elsif (/^--with-baseaddr=(.*)$/)
    704  1.18  christos 			{
    705  1.18  christos 			$config{baseaddr}="$1";
    706  1.18  christos 			}
    707  1.18  christos 		elsif (/^--cross-compile-prefix=(.*)$/)
    708  1.18  christos 			{
    709  1.18  christos 			$config{cross_compile_prefix}=$1;
    710  1.18  christos 			}
    711  1.18  christos 		elsif (/^--config=(.*)$/)
    712  1.18  christos 			{
    713  1.18  christos 			read_config $1;
    714  1.18  christos 			}
    715  1.18  christos 		elsif (/^-[lL](.*)$/ or /^-Wl,/)
    716  1.18  christos 			{
    717  1.18  christos 			$libs.=$_." ";
    718  1.18  christos 			}
    719  1.18  christos 		elsif (/^-rpath$/ or /^-R$/)
    720  1.18  christos 			# -rpath is the OSF1 rpath flag
    721  1.18  christos 			# -R is the old Solaris rpath flag
    722  1.18  christos 			{
    723  1.18  christos 			my $rpath = shift(@argvcopy) || "";
    724  1.18  christos 			$rpath .= " " if $rpath ne "";
    725  1.18  christos 			$libs.=$_." ".$rpath;
    726  1.18  christos 			}
    727  1.18  christos 		elsif (/^-static$/)
    728  1.18  christos 			{
    729  1.18  christos 			$libs.=$_." ";
    730  1.18  christos 			$disabled{"dso"} = "forced";
    731  1.18  christos 			$disabled{"pic"} = "forced";
    732  1.18  christos 			$disabled{"shared"} = "forced";
    733  1.18  christos 			$disabled{"threads"} = "forced";
    734   1.1  christos 			}
    735  1.18  christos 		elsif (/^-D(.*)$/)
    736   1.1  christos 			{
    737  1.18  christos 			push @user_defines, $1;
    738   1.1  christos 			}
    739  1.18  christos 		else	# common if (/^[-+]/), just pass down...
    740   1.1  christos 			{
    741  1.18  christos 			$_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
    742  1.18  christos 			$user_cflags.=" ".$_;
    743   1.1  christos 			}
    744  1.18  christos 		}
    745  1.18  christos 	else
    746  1.18  christos 		{
    747  1.18  christos 		die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
    748  1.18  christos 		$target=$_;
    749  1.18  christos 		}
    750  1.18  christos 	unless ($_ eq $target || /^no-/ || /^disable-/)
    751  1.18  christos 		{
    752  1.18  christos 		# "no-..." follows later after implied disactivations
    753  1.18  christos 		# have been derived.  (Don't take this too seriously,
    754  1.18  christos 		# we really only write OPTIONS to the Makefile out of
    755  1.18  christos 		# nostalgia.)
    756   1.1  christos 
    757  1.18  christos 		if ($config{options} eq "")
    758  1.18  christos 			{ $config{options} = $_; }
    759  1.18  christos 		else
    760  1.18  christos 			{ $config{options} .= " ".$_; }
    761   1.1  christos 		}
    762  1.20  christos 	}
    763   1.1  christos 
    764  1.20  christos if (defined($config{api}) && !exists $apitable->{$config{api}}) {
    765  1.20  christos 	die "***** Unsupported api compatibility level: $config{api}\n",
    766  1.20  christos }
    767   1.1  christos 
    768  1.20  christos if (keys %deprecated_options)
    769  1.20  christos 	{
    770  1.20  christos 	warn "***** Deprecated options: ",
    771  1.20  christos 		join(", ", keys %deprecated_options), "\n";
    772  1.20  christos 	}
    773  1.20  christos if (keys %unsupported_options)
    774  1.20  christos 	{
    775  1.20  christos 	die "***** Unsupported options: ",
    776  1.20  christos 		join(", ", keys %unsupported_options), "\n";
    777   1.1  christos 	}
    778   1.1  christos 
    779  1.18  christos if ($libs =~ /(^|\s)-Wl,-rpath,/
    780  1.18  christos     && !$disabled{shared}
    781  1.18  christos     && !($disabled{asan} && $disabled{msan} && $disabled{ubsan})) {
    782  1.18  christos     die "***** Cannot simultaneously use -rpath, shared libraries, and\n",
    783  1.18  christos 	"***** any of asan, msan or ubsan\n";
    784  1.18  christos }
    785   1.1  christos 
    786  1.18  christos if ($config{fips})
    787   1.1  christos 	{
    788  1.18  christos 	delete $disabled{"shared"} if ($disabled{"shared"} =~ /^default/);
    789   1.1  christos 	}
    790  1.18  christos else
    791   1.1  christos 	{
    792  1.18  christos 	@{$config{dirs}} = grep !/^fips$/, @{$config{dirs}};
    793   1.1  christos 	}
    794   1.1  christos 
    795  1.18  christos my @tocheckfor = (keys %disabled);
    796  1.18  christos while (@tocheckfor) {
    797  1.18  christos     my %new_tocheckfor = ();
    798  1.18  christos     my @cascade_copy = (@disable_cascades);
    799  1.18  christos     while (@cascade_copy) {
    800  1.18  christos 	my ($test, $descendents) = (shift @cascade_copy, shift @cascade_copy);
    801  1.18  christos 	if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
    802  1.18  christos 	    foreach(grep { !defined($disabled{$_}) } @$descendents) {
    803  1.18  christos 		$new_tocheckfor{$_} = 1; $disabled{$_} = "forced";
    804  1.18  christos 	    }
    805   1.2       spz 	}
    806  1.18  christos     }
    807  1.18  christos     @tocheckfor = (keys %new_tocheckfor);
    808  1.18  christos }
    809   1.2       spz 
    810  1.18  christos our $die = sub { die @_; };
    811   1.1  christos if ($target eq "TABLE") {
    812  1.18  christos     local $die = sub { warn @_; };
    813  1.18  christos     foreach (sort keys %table) {
    814  1.18  christos 	print_table_entry($_, "TABLE");
    815  1.18  christos     }
    816  1.18  christos     exit 0;
    817   1.1  christos }
    818   1.1  christos 
    819   1.1  christos if ($target eq "LIST") {
    820  1.18  christos     foreach (sort keys %table) {
    821  1.18  christos 	print $_,"\n" unless $table{$_}->{template};
    822  1.18  christos     }
    823  1.18  christos     exit 0;
    824  1.18  christos }
    825  1.18  christos 
    826  1.18  christos if ($target eq "HASH") {
    827  1.18  christos     local $die = sub { warn @_; };
    828  1.18  christos     print "%table = (\n";
    829  1.18  christos     foreach (sort keys %table) {
    830  1.18  christos 	print_table_entry($_, "HASH");
    831  1.18  christos     }
    832  1.18  christos     exit 0;
    833   1.1  christos }
    834   1.1  christos 
    835  1.18  christos # Backward compatibility?
    836   1.1  christos if ($target =~ m/^CygWin32(-.*)$/) {
    837  1.18  christos     $target = "Cygwin".$1;
    838   1.1  christos }
    839   1.1  christos 
    840   1.1  christos foreach (sort (keys %disabled))
    841   1.1  christos 	{
    842  1.18  christos 	$config{options} .= " no-$_";
    843   1.1  christos 
    844   1.1  christos 	printf "    no-%-12s %-10s", $_, "[$disabled{$_}]";
    845   1.1  christos 
    846   1.1  christos 	if (/^dso$/)
    847  1.18  christos 		{ }
    848   1.1  christos 	elsif (/^threads$/)
    849  1.18  christos 		{ }
    850   1.1  christos 	elsif (/^shared$/)
    851  1.18  christos 		{ }
    852  1.18  christos 	elsif (/^pic$/)
    853  1.18  christos 		{ }
    854   1.1  christos 	elsif (/^zlib$/)
    855  1.18  christos 		{ }
    856  1.18  christos 	elsif (/^dynamic-engine$/)
    857  1.18  christos 		{ }
    858  1.18  christos 	elsif (/^makedepend$/)
    859   1.1  christos 		{ }
    860   1.1  christos 	elsif (/^zlib-dynamic$/)
    861   1.1  christos 		{ }
    862   1.1  christos 	elsif (/^sse2$/)
    863  1.19  christos 		{ }
    864  1.18  christos 	elsif (/^engine$/)
    865  1.18  christos 		{
    866  1.18  christos 		@{$config{dirs}} = grep !/^engines$/, @{$config{dirs}};
    867  1.18  christos 		@{$config{sdirs}} = grep !/^engine$/, @{$config{sdirs}};
    868  1.18  christos 		push @{$config{openssl_other_defines}}, "OPENSSL_NO_ENGINE";
    869  1.18  christos 		print " OPENSSL_NO_ENGINE (skip engines)";
    870  1.18  christos 		}
    871   1.1  christos 	else
    872   1.1  christos 		{
    873  1.18  christos 		my ($WHAT, $what);
    874  1.18  christos 
    875  1.18  christos 		($WHAT = $what = $_) =~ tr/[\-a-z]/[_A-Z]/;
    876   1.1  christos 
    877  1.18  christos 		# Fix up C macro end names
    878  1.18  christos 		$WHAT = "RMD160" if $what eq "ripemd";
    879  1.18  christos 
    880  1.18  christos 		# fix-up crypto/directory name(s)
    881  1.18  christos 		$what = "ripemd" if $what eq "rmd160";
    882  1.18  christos 		$what = "whrlpool" if $what eq "whirlpool";
    883  1.18  christos 
    884  1.18  christos 		if ($what ne "async" && $what ne "err"
    885  1.18  christos 		    && grep { $_ eq $what } @{$config{sdirs}})
    886   1.1  christos 			{
    887  1.18  christos 			push @{$config{openssl_algorithm_defines}}, "OPENSSL_NO_$WHAT";
    888  1.18  christos 			@{$config{sdirs}} = grep { $_ ne $what} @{$config{sdirs}};
    889  1.18  christos 
    890  1.18  christos 			print " OPENSSL_NO_$WHAT (skip dir)";
    891   1.1  christos 			}
    892   1.1  christos 		else
    893   1.1  christos 			{
    894  1.18  christos 			push @{$config{openssl_other_defines}}, "OPENSSL_NO_$WHAT";
    895  1.18  christos 			print " OPENSSL_NO_$WHAT";
    896   1.1  christos 			}
    897   1.1  christos 		}
    898   1.1  christos 
    899   1.1  christos 	print "\n";
    900   1.1  christos 	}
    901   1.1  christos 
    902  1.18  christos print "Configuring for $target\n";
    903  1.18  christos 
    904  1.18  christos # Support for legacy targets having a name starting with 'debug-'
    905  1.18  christos my ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
    906  1.18  christos if ($d) {
    907  1.18  christos     $config{build_type} = "debug";
    908  1.18  christos 
    909  1.18  christos     # If we do not find debug-foo in the table, the target is set to foo.
    910  1.18  christos     if (!$table{$target}) {
    911  1.18  christos 	$target = $t;
    912  1.18  christos     }
    913  1.18  christos }
    914  1.20  christos 
    915  1.20  christos &usage if !$table{$target} || $table{$target}->{template};
    916  1.20  christos 
    917  1.18  christos $config{target} = $target;
    918  1.18  christos my %target = resolve_config($target);
    919  1.18  christos 
    920  1.18  christos my %conf_files = map { $_ => 1 } (@{$target{_conf_fname_int}});
    921  1.18  christos $config{conf_files} = [ sort keys %conf_files ];
    922  1.18  christos %target = ( %{$table{DEFAULTS}}, %target );
    923  1.18  christos 
    924  1.18  christos $target{exe_extension}="";
    925  1.18  christos $target{exe_extension}=".exe" if ($config{target} eq "DJGPP"
    926  1.18  christos                                   || $config{target} =~ /^(?:Cygwin|mingw)/);
    927  1.18  christos $target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
    928  1.18  christos 
    929  1.18  christos ($target{shared_extension_simple}=$target{shared_extension})
    930  1.18  christos     =~ s|\.\$\(SHLIB_MAJOR\)\.\$\(SHLIB_MINOR\)||;
    931  1.18  christos $target{dso_extension}=$target{shared_extension_simple};
    932  1.18  christos ($target{shared_import_extension}=$target{shared_extension_simple}.".a")
    933  1.18  christos     if ($config{target} =~ /^(?:Cygwin|mingw)/);
    934  1.18  christos 
    935  1.18  christos 
    936  1.18  christos $config{cross_compile_prefix} = $ENV{'CROSS_COMPILE'}
    937  1.18  christos     if $config{cross_compile_prefix} eq "";
    938  1.18  christos 
    939  1.18  christos # Allow overriding the names of some tools.  USE WITH CARE
    940  1.18  christos # Note: only Unix cares about HASHBANGPERL...  that explains
    941  1.18  christos # the default string.
    942  1.18  christos $config{perl} =    $ENV{'PERL'}    || ($^O ne "VMS" ? $^X : "perl");
    943  1.18  christos $config{hashbangperl} =
    944  1.18  christos     $ENV{'HASHBANGPERL'}           || $ENV{'PERL'}     || "/usr/bin/env perl";
    945  1.18  christos $target{cc} =      $ENV{'CC'}      || $target{cc}      || "cc";
    946  1.18  christos $target{ranlib} =  $ENV{'RANLIB'}  || $target{ranlib}  ||
    947  1.18  christos                    (which("$config{cross_compile_prefix}ranlib") ?
    948  1.18  christos                           "\$(CROSS_COMPILE)ranlib" : "true");
    949  1.18  christos $target{ar} =      $ENV{'AR'}      || $target{ar}      || "ar";
    950  1.18  christos $target{nm} =      $ENV{'NM'}      || $target{nm}      || "nm";
    951  1.18  christos $target{rc} =
    952  1.18  christos     $ENV{'RC'}  || $ENV{'WINDRES'} || $target{rc}      || "windres";
    953  1.18  christos 
    954  1.18  christos # Allow overriding the build file name
    955  1.18  christos $target{build_file} = $ENV{BUILDFILE} || $target{build_file} || "Makefile";
    956  1.18  christos 
    957  1.18  christos # Cache information necessary for reconfiguration
    958  1.18  christos $config{cc} = $target{cc};
    959  1.18  christos $config{build_file} = $target{build_file};
    960  1.18  christos 
    961  1.18  christos # For cflags, lflags, plib_lflags, ex_libs and defines, add the debug_
    962  1.18  christos # or release_ attributes.
    963  1.18  christos # Do it in such a way that no spurious space is appended (hence the grep).
    964  1.18  christos $config{defines} = [];
    965  1.18  christos $config{cflags} = "";
    966  1.18  christos $config{ex_libs} = "";
    967  1.18  christos $config{shared_ldflag} = "";
    968  1.18  christos 
    969  1.18  christos # Make sure build_scheme is consistent.
    970  1.18  christos $target{build_scheme} = [ $target{build_scheme} ]
    971  1.18  christos     if ref($target{build_scheme}) ne "ARRAY";
    972  1.18  christos 
    973  1.18  christos my ($builder, $builder_platform, @builder_opts) =
    974  1.18  christos     @{$target{build_scheme}};
    975  1.18  christos 
    976  1.18  christos foreach my $checker (($builder_platform."-".$target{build_file}."-checker.pm",
    977  1.18  christos                       $builder_platform."-checker.pm")) {
    978  1.18  christos     my $checker_path = catfile($srcdir, "Configurations", $checker);
    979  1.18  christos     if (-f $checker_path) {
    980  1.18  christos         my $fn = $ENV{CONFIGURE_CHECKER_WARN}
    981  1.18  christos             ? sub { warn $@; } : sub { die $@; };
    982  1.18  christos         if (! do $checker_path) {
    983  1.18  christos             if ($@) {
    984  1.18  christos                 $fn->($@);
    985  1.18  christos             } elsif ($!) {
    986  1.18  christos                 $fn->($!);
    987  1.18  christos             } else {
    988  1.18  christos                 $fn->("The detected tools didn't match the platform\n");
    989  1.18  christos             }
    990  1.18  christos         }
    991  1.18  christos         last;
    992  1.18  christos     }
    993  1.18  christos }
    994   1.1  christos 
    995  1.18  christos push @{$config{defines}}, "NDEBUG"    if $config{build_type} eq "release";
    996   1.1  christos 
    997  1.18  christos if ($target =~ /^mingw/ && `$target{cc} --target-help 2>&1` =~ m/-mno-cygwin/m)
    998   1.2       spz 	{
    999  1.18  christos 	$config{cflags} .= " -mno-cygwin";
   1000  1.18  christos 	$config{shared_ldflag} .= " -mno-cygwin";
   1001   1.2       spz 	}
   1002   1.2       spz 
   1003  1.18  christos if ($target =~ /linux.*-mips/ && !$disabled{asm} && $user_cflags !~ /-m(ips|arch=)/) {
   1004  1.16       spz 	# minimally required architecture flags for assembly modules
   1005  1.18  christos 	$config{cflags}="-mips2 $config{cflags}" if ($target =~ /mips32/);
   1006  1.18  christos 	$config{cflags}="-mips3 $config{cflags}" if ($target =~ /mips64/);
   1007  1.16       spz }
   1008  1.16       spz 
   1009   1.1  christos my $no_shared_warn=0;
   1010   1.1  christos my $no_user_cflags=0;
   1011  1.18  christos my $no_user_defines=0;
   1012   1.1  christos 
   1013   1.1  christos # The DSO code currently always implements all functions so that no
   1014   1.1  christos # applications will have to worry about that from a compilation point
   1015   1.1  christos # of view. However, the "method"s may return zero unless that platform
   1016   1.1  christos # has support compiled in for them. Currently each method is enabled
   1017   1.1  christos # by a define "DSO_<name>" ... we translate the "dso_scheme" config
   1018   1.1  christos # string entry into using the following logic;
   1019  1.18  christos if (!$disabled{dso} && $target{dso_scheme} ne "")
   1020   1.1  christos 	{
   1021  1.18  christos 	$target{dso_scheme} =~ tr/[a-z]/[A-Z]/;
   1022  1.18  christos 	if ($target{dso_scheme} eq "DLFCN")
   1023   1.1  christos 		{
   1024  1.18  christos 		unshift @{$config{defines}}, "DSO_DLFCN", "HAVE_DLFCN_H";
   1025   1.1  christos 		}
   1026  1.18  christos 	elsif ($target{dso_scheme} eq "DLFCN_NO_H")
   1027   1.1  christos 		{
   1028  1.18  christos 		unshift @{$config{defines}}, "DSO_DLFCN";
   1029   1.1  christos 		}
   1030   1.1  christos 	else
   1031   1.1  christos 		{
   1032  1.18  christos 		unshift @{$config{defines}}, "DSO_$target{dso_scheme}";
   1033   1.1  christos 		}
   1034   1.1  christos 	}
   1035   1.1  christos 
   1036  1.18  christos $config{ex_libs}="$libs$config{ex_libs}" if ($libs ne "");
   1037  1.18  christos 
   1038  1.18  christos if ($disabled{asm})
   1039   1.1  christos 	{
   1040  1.18  christos 	if ($config{fips})
   1041  1.18  christos 		{
   1042  1.18  christos 		@{$config{defines}} = grep !/^[BL]_ENDIAN$/, @{$config{defines}};
   1043  1.18  christos 		@{$target{defines}} = grep !/^[BL]_ENDIAN$/, @{$target{defines}};
   1044   1.1  christos 		}
   1045   1.1  christos 	}
   1046   1.1  christos 
   1047  1.18  christos # If threads aren't disabled, check how possible they are
   1048  1.18  christos unless ($disabled{threads}) {
   1049  1.18  christos     if ($auto_threads) {
   1050  1.18  christos         # Enabled by default, disable it forcibly if unavailable
   1051  1.18  christos         if ($target{thread_scheme} eq "(unknown)") {
   1052  1.18  christos             $disabled{threads} = "unavailable";
   1053  1.18  christos         }
   1054  1.18  christos     } else {
   1055  1.18  christos         # The user chose to enable threads explicitly, let's see
   1056  1.18  christos         # if there's a chance that's possible
   1057  1.18  christos         if ($target{thread_scheme} eq "(unknown)") {
   1058  1.18  christos             # If the user asked for "threads" and we don't have internal
   1059  1.18  christos             # knowledge how to do it, [s]he is expected to provide any
   1060  1.18  christos             # system-dependent compiler options that are necessary.  We
   1061  1.18  christos             # can't truly check that the given options are correct, but
   1062  1.18  christos             # we expect the user to know what [s]He is doing.
   1063  1.18  christos             if ($no_user_cflags && $no_user_defines) {
   1064  1.18  christos                 die "You asked for multi-threading support, but didn't\n"
   1065  1.18  christos                     ,"provide any system-specific compiler options\n";
   1066  1.18  christos             }
   1067  1.18  christos         }
   1068  1.18  christos     }
   1069  1.18  christos }
   1070  1.18  christos 
   1071  1.18  christos # If threads still aren't disabled, add a C macro to ensure the source
   1072  1.18  christos # code knows about it.  Any other flag is taken care of by the configs.
   1073  1.18  christos unless($disabled{threads}) {
   1074  1.18  christos     foreach (("defines", "openssl_thread_defines")) {
   1075  1.18  christos         push @{$config{$_}}, "OPENSSL_THREADS";
   1076  1.18  christos     }
   1077  1.18  christos }
   1078   1.1  christos 
   1079  1.18  christos # With "deprecated" disable all deprecated features.
   1080  1.18  christos if (defined($disabled{"deprecated"})) {
   1081  1.18  christos         $config{api} = $maxapi;
   1082  1.18  christos }
   1083   1.1  christos 
   1084  1.18  christos if ($target{shared_target} eq "")
   1085   1.1  christos 	{
   1086  1.18  christos 	$no_shared_warn = 1
   1087  1.18  christos 	    if ((!$disabled{shared} || !$disabled{"dynamic-engine"})
   1088  1.18  christos 		&& !$config{fips});
   1089  1.18  christos 	$disabled{shared} = "no-shared-target";
   1090  1.18  christos 	$disabled{pic} = $disabled{shared} = $disabled{"dynamic-engine"} =
   1091  1.18  christos 	    "no-shared-target";
   1092   1.1  christos 	}
   1093   1.1  christos 
   1094  1.18  christos if ($disabled{"dynamic-engine"}) {
   1095  1.18  christos         push @{$config{defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
   1096  1.18  christos         $config{dynamic_engines} = 0;
   1097  1.18  christos } else {
   1098  1.18  christos         push @{$config{defines}}, "OPENSSL_NO_STATIC_ENGINE";
   1099  1.18  christos         $config{dynamic_engines} = 1;
   1100  1.18  christos }
   1101   1.1  christos 
   1102  1.18  christos unless ($disabled{"fuzz-libfuzzer"}) {
   1103  1.18  christos     $config{cflags} .= "-fsanitize-coverage=edge,indirect-calls ";
   1104  1.18  christos }
   1105   1.1  christos 
   1106  1.18  christos unless ($disabled{asan}) {
   1107  1.18  christos     $config{cflags} .= "-fsanitize=address ";
   1108  1.18  christos }
   1109   1.1  christos 
   1110  1.18  christos unless ($disabled{ubsan}) {
   1111  1.18  christos     # -DPEDANTIC or -fnosanitize=alignment may also be required on some
   1112  1.18  christos     # platforms.
   1113  1.18  christos     $config{cflags} .= "-fsanitize=undefined -fno-sanitize-recover=all ";
   1114  1.18  christos }
   1115   1.1  christos 
   1116  1.18  christos unless ($disabled{msan}) {
   1117  1.18  christos   $config{cflags} .= "-fsanitize=memory ";
   1118  1.18  christos }
   1119   1.1  christos 
   1120  1.18  christos unless ($disabled{"fuzz-libfuzzer"} && $disabled{"fuzz-afl"}
   1121  1.18  christos         && $disabled{asan} && $disabled{ubsan} && $disabled{msan}) {
   1122  1.18  christos     $config{cflags} .= "-fno-omit-frame-pointer -g ";
   1123  1.18  christos }
   1124   1.1  christos #
   1125   1.1  christos # Platform fix-ups
   1126   1.1  christos #
   1127   1.1  christos 
   1128  1.18  christos # This saves the build files from having to check
   1129  1.18  christos if ($disabled{pic})
   1130   1.1  christos 	{
   1131  1.18  christos 	$target{shared_cflag} = $target{shared_ldflag} =
   1132  1.18  christos 		$target{shared_rcflag} = "";
   1133   1.1  christos 	}
   1134  1.18  christos else
   1135   1.1  christos 	{
   1136  1.18  christos 	push @{$config{defines}}, "OPENSSL_PIC";
   1137   1.1  christos 	}
   1138   1.1  christos 
   1139  1.18  christos if ($target{sys_id} ne "")
   1140   1.1  christos 	{
   1141  1.18  christos 	push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
   1142   1.1  christos 	}
   1143   1.1  christos 
   1144  1.18  christos unless ($disabled{asm}) {
   1145  1.18  christos     $target{cpuid_asm_src}=$table{DEFAULTS}->{cpuid_asm_src} if ($config{processor} eq "386");
   1146  1.18  christos     $target{bn_asm_src} =~ s/\w+-gf2m.c// if (defined($disabled{ec2m}));
   1147  1.18  christos 
   1148  1.18  christos     # bn-586 is the only one implementing bn_*_part_words
   1149  1.18  christos     push @{$config{defines}}, "OPENSSL_BN_ASM_PART_WORDS" if ($target{bn_asm_src} =~ /bn-586/);
   1150  1.19  christos     push @{$config{defines}}, "OPENSSL_IA32_SSE2" if (!$disabled{sse2} && $target{bn_asm_src} =~ /86/);
   1151  1.18  christos 
   1152  1.18  christos     push @{$config{defines}}, "OPENSSL_BN_ASM_MONT" if ($target{bn_asm_src} =~ /-mont/);
   1153  1.18  christos     push @{$config{defines}}, "OPENSSL_BN_ASM_MONT5" if ($target{bn_asm_src} =~ /-mont5/);
   1154  1.18  christos     push @{$config{defines}}, "OPENSSL_BN_ASM_GF2m" if ($target{bn_asm_src} =~ /-gf2m/);
   1155  1.18  christos 
   1156  1.18  christos     if ($config{fips}) {
   1157  1.18  christos 	push @{$config{openssl_other_defines}}, "OPENSSL_FIPS";
   1158  1.18  christos     }
   1159  1.18  christos 
   1160  1.18  christos     if ($target{sha1_asm_src}) {
   1161  1.18  christos 	push @{$config{defines}}, "SHA1_ASM"   if ($target{sha1_asm_src} =~ /sx86/ || $target{sha1_asm_src} =~ /sha1/);
   1162  1.18  christos 	push @{$config{defines}}, "SHA256_ASM" if ($target{sha1_asm_src} =~ /sha256/);
   1163  1.18  christos 	push @{$config{defines}}, "SHA512_ASM" if ($target{sha1_asm_src} =~ /sha512/);
   1164  1.18  christos     }
   1165  1.18  christos     if ($target{rc4_asm_src} ne $table{DEFAULTS}->{rc4_asm_src}) {
   1166  1.18  christos 	push @{$config{defines}}, "RC4_ASM";
   1167  1.18  christos     }
   1168  1.18  christos     if ($target{md5_asm_src}) {
   1169  1.18  christos 	push @{$config{defines}}, "MD5_ASM";
   1170  1.18  christos     }
   1171  1.18  christos     $target{cast_asm_src}=$table{DEFAULTS}->{cast_asm_src} unless $disabled{pic}; # CAST assembler is not PIC
   1172  1.18  christos     if ($target{rmd160_asm_src}) {
   1173  1.18  christos 	push @{$config{defines}}, "RMD160_ASM";
   1174  1.18  christos     }
   1175  1.18  christos     if ($target{aes_asm_src}) {
   1176  1.18  christos 	push @{$config{defines}}, "AES_ASM" if ($target{aes_asm_src} =~ m/\baes-/);;
   1177  1.18  christos 	# aes-ctr.fake is not a real file, only indication that assembler
   1178  1.18  christos 	# module implements AES_ctr32_encrypt...
   1179  1.18  christos 	push @{$config{defines}}, "AES_CTR_ASM" if ($target{aes_asm_src} =~ s/\s*aes-ctr\.fake//);
   1180  1.18  christos 	# aes-xts.fake indicates presence of AES_xts_[en|de]crypt...
   1181  1.18  christos 	push @{$config{defines}}, "AES_XTS_ASM" if ($target{aes_asm_src} =~ s/\s*aes-xts\.fake//);
   1182  1.19  christos 	$target{aes_asm_src} =~ s/\s*(vpaes|aesni)-x86\.s//g if ($disabled{sse2});
   1183  1.18  christos 	push @{$config{defines}}, "VPAES_ASM" if ($target{aes_asm_src} =~ m/vpaes/);
   1184  1.18  christos 	push @{$config{defines}}, "BSAES_ASM" if ($target{aes_asm_src} =~ m/bsaes/);
   1185  1.18  christos     }
   1186  1.18  christos     if ($target{wp_asm_src} =~ /mmx/) {
   1187  1.18  christos         if ($config{processor} eq "386") {
   1188  1.18  christos 	    $target{wp_asm_src}=$table{DEFAULTS}->{wp_asm_src};
   1189  1.18  christos 	} elsif (!$disabled{"whirlpool"}) {
   1190  1.18  christos 	    push @{$config{defines}}, "WHIRLPOOL_ASM";
   1191  1.18  christos 	}
   1192  1.18  christos     }
   1193  1.18  christos     if ($target{modes_asm_src} =~ /ghash-/) {
   1194  1.18  christos 	push @{$config{defines}}, "GHASH_ASM";
   1195  1.18  christos     }
   1196  1.18  christos     if ($target{ec_asm_src} =~ /ecp_nistz256/) {
   1197  1.18  christos 	push @{$config{defines}}, "ECP_NISTZ256_ASM";
   1198  1.18  christos     }
   1199  1.18  christos     if ($target{padlock_asm_src} ne $table{DEFAULTS}->{padlock_asm_src}) {
   1200  1.18  christos 	push @{$config{defines}}, "PADLOCK_ASM";
   1201  1.18  christos     }
   1202  1.18  christos     if ($target{poly1305_asm_src} ne "") {
   1203  1.18  christos 	push @{$config{defines}}, "POLY1305_ASM";
   1204  1.18  christos     }
   1205  1.18  christos }
   1206   1.1  christos 
   1207  1.18  christos my %predefined;
   1208   1.1  christos 
   1209  1.18  christos if ($^O ne "VMS") {
   1210  1.18  christos     my $cc = "$config{cross_compile_prefix}$target{cc}";
   1211   1.1  christos 
   1212  1.18  christos     # collect compiler pre-defines from gcc or gcc-alike...
   1213  1.18  christos     open(PIPE, "$cc -dM -E -x c /dev/null 2>&1 |");
   1214  1.18  christos     while (<PIPE>) {
   1215  1.18  christos 	m/^#define\s+(\w+(?:\(\w+\))?)(?:\s+(.+))?/ or last;
   1216  1.18  christos 	$predefined{$1} = $2 // "";
   1217  1.18  christos     }
   1218  1.18  christos     close(PIPE);
   1219  1.18  christos 
   1220  1.18  christos     if (!$disabled{makedepend}) {
   1221  1.18  christos 	# We know that GNU C version 3 and up as well as all clang
   1222  1.20  christos 	# versions support dependency generation, but Xcode did not
   1223  1.20  christos 	# handle $cc -M before clang support (but claims __GNUC__ = 3)
   1224  1.20  christos 	if (($predefined{__GNUC__} // -1) >= 3
   1225  1.20  christos 		&& !($predefined{__APPLE_CC__} && !$predefined{__clang__})) {
   1226  1.18  christos 	    $config{makedepprog} = $cc;
   1227  1.18  christos 	} else {
   1228  1.18  christos 	    $config{makedepprog} = which('makedepend');
   1229  1.18  christos 	    $disabled{makedepend} = "unavailable" unless $config{makedepprog};
   1230  1.18  christos 	}
   1231  1.18  christos     }
   1232  1.18  christos }
   1233   1.1  christos 
   1234   1.2       spz 
   1235   1.1  christos 
   1236  1.18  christos # Deal with bn_ops ###################################################
   1237   1.1  christos 
   1238  1.18  christos $config{bn_ll}			=0;
   1239  1.18  christos $config{export_var_as_fn}	=0;
   1240  1.18  christos my $def_int="unsigned int";
   1241  1.18  christos $config{rc4_int}		=$def_int;
   1242  1.18  christos ($config{b64l},$config{b64},$config{b32})=(0,0,1);
   1243  1.18  christos 
   1244  1.18  christos my $count = 0;
   1245  1.18  christos foreach (sort split(/\s+/,$target{bn_ops})) {
   1246  1.18  christos     $count++ if /SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT/;
   1247  1.18  christos     $config{export_var_as_fn}=1                 if $_ eq 'EXPORT_VAR_AS_FN';
   1248  1.18  christos     $config{bn_ll}=1				if $_ eq 'BN_LLONG';
   1249  1.18  christos     $config{rc4_int}="unsigned char"		if $_ eq 'RC4_CHAR';
   1250  1.18  christos     ($config{b64l},$config{b64},$config{b32})
   1251  1.18  christos 	=(0,1,0)				if $_ eq 'SIXTY_FOUR_BIT';
   1252  1.18  christos     ($config{b64l},$config{b64},$config{b32})
   1253  1.18  christos 	=(1,0,0)				if $_ eq 'SIXTY_FOUR_BIT_LONG';
   1254  1.18  christos     ($config{b64l},$config{b64},$config{b32})
   1255  1.18  christos 	=(0,0,1)				if $_ eq 'THIRTY_TWO_BIT';
   1256  1.18  christos }
   1257  1.18  christos die "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
   1258  1.18  christos     if $count > 1;
   1259   1.1  christos 
   1260   1.1  christos 
   1261  1.18  christos # Hack cflags for better warnings (dev option) #######################
   1262   1.1  christos 
   1263  1.18  christos # "Stringify" the C flags string.  This permits it to be made part of a string
   1264  1.18  christos # and works as well on command lines.
   1265  1.18  christos $config{cflags} =~ s/([\\\"])/\\$1/g;
   1266   1.1  christos 
   1267  1.18  christos if (defined($config{api})) {
   1268  1.18  christos     $config{openssl_api_defines} = [ "OPENSSL_MIN_API=".$apitable->{$config{api}} ];
   1269  1.18  christos     my $apiflag = sprintf("OPENSSL_API_COMPAT=%s", $apitable->{$config{api}});
   1270  1.18  christos     push @{$config{defines}}, $apiflag;
   1271  1.18  christos }
   1272  1.16       spz 
   1273   1.2       spz if ($strict_warnings)
   1274   1.2       spz 	{
   1275   1.2       spz 	my $wopt;
   1276  1.18  christos 	die "ERROR --strict-warnings requires gcc or gcc-alike"
   1277  1.18  christos             unless defined($predefined{__GNUC__});
   1278   1.2       spz 	foreach $wopt (split /\s+/, $gcc_devteam_warn)
   1279   1.2       spz 		{
   1280  1.18  christos 		$config{cflags} .= " $wopt" unless ($config{cflags} =~ /(?:^|\s)$wopt(?:\s|$)/)
   1281  1.13  christos 		}
   1282  1.18  christos 	if (defined($predefined{__clang__}))
   1283  1.13  christos 		{
   1284  1.13  christos 		foreach $wopt (split /\s+/, $clang_devteam_warn)
   1285  1.13  christos 			{
   1286  1.18  christos 			$config{cflags} .= " $wopt" unless ($config{cflags} =~ /(?:^|\s)$wopt(?:\s|$)/)
   1287  1.13  christos 			}
   1288   1.2       spz 		}
   1289   1.2       spz 	}
   1290   1.2       spz 
   1291  1.18  christos unless ($disabled{"crypto-mdebug-backtrace"})
   1292   1.1  christos 	{
   1293  1.18  christos 	foreach my $wopt (split /\s+/, $memleak_devteam_backtrace)
   1294  1.18  christos 		{
   1295  1.18  christos 		$config{cflags} .= " $wopt" unless ($config{cflags} =~ /(?:^|\s)$wopt(?:\s|$)/)
   1296   1.1  christos 		}
   1297  1.18  christos 	if ($target =~ /^BSD-/)
   1298   1.1  christos 		{
   1299  1.18  christos 		$config{ex_libs} .= " -lexecinfo";
   1300   1.1  christos 		}
   1301   1.1  christos 	}
   1302   1.1  christos 
   1303  1.18  christos if ($user_cflags ne "") { $config{cflags}="$config{cflags}$user_cflags"; }
   1304  1.18  christos else                    { $no_user_cflags=1;  }
   1305  1.18  christos if (@user_defines) { $config{defines}=[ @{$config{defines}}, @user_defines ]; }
   1306  1.18  christos else               { $no_user_defines=1;    }
   1307  1.18  christos 
   1308  1.18  christos # ALL MODIFICATIONS TO %config and %target MUST BE DONE FROM HERE ON
   1309  1.18  christos 
   1310  1.18  christos unless ($disabled{afalgeng}) {
   1311  1.18  christos     $config{afalgeng}="";
   1312  1.18  christos     if ($target =~ m/^linux/) {
   1313  1.18  christos         my $minver = 4*10000 + 1*100 + 0;
   1314  1.18  christos         if ($config{cross_compile_prefix} eq "") {
   1315  1.18  christos             my $verstr = `uname -r`;
   1316  1.18  christos             my ($ma, $mi1, $mi2) = split("\\.", $verstr);
   1317  1.18  christos             ($mi2) = $mi2 =~ /(\d+)/;
   1318  1.18  christos             my $ver = $ma*10000 + $mi1*100 + $mi2;
   1319  1.18  christos             if ($ver < $minver) {
   1320  1.18  christos                 $disabled{afalgeng} = "too-old-kernel";
   1321  1.18  christos             } else {
   1322  1.18  christos                 push @{$config{engdirs}}, "afalg";
   1323  1.18  christos             }
   1324  1.18  christos         } else {
   1325  1.18  christos             $disabled{afalgeng} = "cross-compiling";
   1326  1.18  christos         }
   1327  1.18  christos     } else {
   1328  1.18  christos         $disabled{afalgeng}  = "not-linux";
   1329  1.18  christos     }
   1330  1.18  christos }
   1331  1.18  christos 
   1332  1.18  christos push @{$config{openssl_other_defines}}, "OPENSSL_NO_AFALGENG" if ($disabled{afalgeng});
   1333  1.18  christos 
   1334  1.18  christos # If we use the unified build, collect information from build.info files
   1335  1.18  christos my %unified_info = ();
   1336  1.18  christos 
   1337  1.18  christos my $buildinfo_debug = defined($ENV{CONFIGURE_DEBUG_BUILDINFO});
   1338  1.18  christos if ($builder eq "unified") {
   1339  1.18  christos     use with_fallback qw(Text::Template);
   1340  1.18  christos 
   1341  1.18  christos     sub cleandir {
   1342  1.18  christos         my $base = shift;
   1343  1.18  christos         my $dir = shift;
   1344  1.18  christos         my $relativeto = shift || ".";
   1345  1.18  christos 
   1346  1.18  christos         $dir = catdir($base,$dir) unless isabsolute($dir);
   1347  1.18  christos 
   1348  1.18  christos         # Make sure the directories we're building in exists
   1349  1.18  christos         mkpath($dir);
   1350  1.18  christos 
   1351  1.18  christos         my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
   1352  1.18  christos         #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
   1353  1.18  christos         return $res;
   1354  1.18  christos     }
   1355  1.18  christos 
   1356  1.18  christos     sub cleanfile {
   1357  1.18  christos         my $base = shift;
   1358  1.18  christos         my $file = shift;
   1359  1.18  christos         my $relativeto = shift || ".";
   1360  1.18  christos 
   1361  1.18  christos         $file = catfile($base,$file) unless isabsolute($file);
   1362  1.18  christos 
   1363  1.18  christos         my $d = dirname($file);
   1364  1.18  christos         my $f = basename($file);
   1365  1.18  christos 
   1366  1.18  christos         # Make sure the directories we're building in exists
   1367  1.18  christos         mkpath($d);
   1368  1.18  christos 
   1369  1.18  christos         my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
   1370  1.18  christos         #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
   1371  1.18  christos         return $res;
   1372  1.18  christos     }
   1373  1.18  christos 
   1374  1.18  christos     # Store the name of the template file we will build the build file from
   1375  1.18  christos     # in %config.  This may be useful for the build file itself.
   1376  1.18  christos     my @build_file_template_names =
   1377  1.18  christos 	( $builder_platform."-".$target{build_file}.".tmpl",
   1378  1.18  christos 	  $target{build_file}.".tmpl" );
   1379  1.18  christos     my @build_file_templates = ();
   1380  1.18  christos 
   1381  1.18  christos     # First, look in the user provided directory, if given
   1382  1.18  christos     if (defined $ENV{$local_config_envname}) {
   1383  1.18  christos 	@build_file_templates =
   1384  1.18  christos 	    map {
   1385  1.18  christos 		if ($^O eq 'VMS') {
   1386  1.18  christos 		    # VMS environment variables are logical names,
   1387  1.18  christos 		    # which can be used as is
   1388  1.18  christos 		    $local_config_envname . ':' . $_;
   1389  1.18  christos 		} else {
   1390  1.18  christos 		    catfile($ENV{$local_config_envname}, $_);
   1391  1.18  christos 		}
   1392  1.18  christos 	    }
   1393  1.18  christos 	    @build_file_template_names;
   1394  1.18  christos     }
   1395  1.18  christos     # Then, look in our standard directory
   1396  1.18  christos     push @build_file_templates,
   1397  1.18  christos 	( map { cleanfile($srcdir, catfile("Configurations", $_), $blddir) }
   1398  1.18  christos 	  @build_file_template_names );
   1399  1.18  christos 
   1400  1.18  christos     my $build_file_template;
   1401  1.18  christos     for $_ (@build_file_templates) {
   1402  1.18  christos 	$build_file_template = $_;
   1403  1.18  christos         last if -f $build_file_template;
   1404  1.18  christos 
   1405  1.18  christos         $build_file_template = undef;
   1406  1.18  christos     }
   1407  1.18  christos     if (!defined $build_file_template) {
   1408  1.18  christos 	die "*** Couldn't find any of:\n", join("\n", @build_file_templates), "\n";
   1409  1.18  christos     }
   1410  1.18  christos     $config{build_file_templates}
   1411  1.18  christos       = [ $build_file_template,
   1412  1.18  christos           cleanfile($srcdir, catfile("Configurations", "common.tmpl"),
   1413  1.18  christos                     $blddir) ];
   1414  1.18  christos 
   1415  1.18  christos     my @build_infos = ( [ ".", "build.info" ] );
   1416  1.18  christos     foreach (@{$config{dirs}}) {
   1417  1.18  christos         push @build_infos, [ $_, "build.info" ]
   1418  1.18  christos             if (-f catfile($srcdir, $_, "build.info"));
   1419  1.18  christos     }
   1420  1.18  christos     foreach (@{$config{sdirs}}) {
   1421  1.18  christos         push @build_infos, [ catdir("crypto", $_), "build.info" ]
   1422  1.18  christos             if (-f catfile($srcdir, "crypto", $_, "build.info"));
   1423  1.18  christos     }
   1424  1.18  christos     foreach (@{$config{engdirs}}) {
   1425  1.18  christos         push @build_infos, [ catdir("engines", $_), "build.info" ]
   1426  1.18  christos             if (-f catfile($srcdir, "engines", $_, "build.info"));
   1427  1.18  christos     }
   1428  1.18  christos 
   1429  1.18  christos     $config{build_infos} = [ ];
   1430  1.18  christos 
   1431  1.18  christos     foreach (@build_infos) {
   1432  1.18  christos         my $sourced = catdir($srcdir, $_->[0]);
   1433  1.18  christos         my $buildd = catdir($blddir, $_->[0]);
   1434  1.18  christos 
   1435  1.18  christos         mkpath($buildd);
   1436  1.18  christos 
   1437  1.18  christos         my $f = $_->[1];
   1438  1.18  christos         # The basic things we're trying to build
   1439  1.18  christos         my @programs = ();
   1440  1.18  christos         my @programs_install = ();
   1441  1.18  christos         my @libraries = ();
   1442  1.18  christos         my @libraries_install = ();
   1443  1.18  christos         my @engines = ();
   1444  1.18  christos         my @engines_install = ();
   1445  1.18  christos         my @scripts = ();
   1446  1.18  christos         my @scripts_install = ();
   1447  1.18  christos         my @extra = ();
   1448  1.18  christos         my @overrides = ();
   1449  1.18  christos         my @intermediates = ();
   1450  1.18  christos         my @rawlines = ();
   1451  1.18  christos 
   1452  1.18  christos         my %ordinals = ();
   1453  1.18  christos         my %sources = ();
   1454  1.18  christos         my %shared_sources = ();
   1455  1.18  christos         my %includes = ();
   1456  1.18  christos         my %depends = ();
   1457  1.18  christos         my %renames = ();
   1458  1.18  christos         my %sharednames = ();
   1459  1.18  christos         my %generate = ();
   1460  1.18  christos 
   1461  1.19  christos         # We want to detect configdata.pm in the source tree, so we
   1462  1.19  christos         # don't use it if the build tree is different.
   1463  1.19  christos         my $src_configdata = cleanfile($srcdir, "configdata.pm", $blddir);
   1464  1.19  christos 
   1465  1.18  christos         push @{$config{build_infos}}, catfile(abs2rel($sourced, $blddir), $f);
   1466  1.19  christos         my $template =
   1467  1.19  christos             Text::Template->new(TYPE => 'FILE',
   1468  1.19  christos                                 SOURCE => catfile($sourced, $f),
   1469  1.19  christos                                 PREPEND => qq{use lib "$FindBin::Bin/util/perl";});
   1470  1.18  christos         die "Something went wrong with $sourced/$f: $!\n" unless $template;
   1471  1.18  christos         my @text =
   1472  1.18  christos             split /^/m,
   1473  1.18  christos             $template->fill_in(HASH => { config => \%config,
   1474  1.18  christos                                          target => \%target,
   1475  1.18  christos                                          disabled => \%disabled,
   1476  1.18  christos                                          withargs => \%withargs,
   1477  1.18  christos                                          builddir => abs2rel($buildd, $blddir),
   1478  1.18  christos                                          sourcedir => abs2rel($sourced, $blddir),
   1479  1.18  christos                                          buildtop => abs2rel($blddir, $blddir),
   1480  1.18  christos                                          sourcetop => abs2rel($srcdir, $blddir) },
   1481  1.18  christos                                DELIMITERS => [ "{-", "-}" ]);
   1482  1.18  christos 
   1483  1.18  christos         # The top item of this stack has the following values
   1484  1.18  christos         # -2 positive already run and we found ELSE (following ELSIF should fail)
   1485  1.18  christos         # -1 positive already run (skip until ENDIF)
   1486  1.18  christos         # 0 negatives so far (if we're at a condition, check it)
   1487  1.18  christos         # 1 last was positive (don't skip lines until next ELSE, ELSIF or ENDIF)
   1488  1.18  christos         # 2 positive ELSE (following ELSIF should fail)
   1489  1.18  christos         my @skip = ();
   1490  1.18  christos         collect_information(
   1491  1.18  christos             collect_from_array([ @text ],
   1492  1.18  christos                                qr/\\$/ => sub { my $l1 = shift; my $l2 = shift;
   1493  1.18  christos                                                 $l1 =~ s/\\$//; $l1.$l2 }),
   1494  1.18  christos             # Info we're looking for
   1495  1.18  christos             qr/^\s*IF\[((?:\\.|[^\\\]])*)\]\s*$/
   1496  1.18  christos             => sub {
   1497  1.18  christos                 if (! @skip || $skip[$#skip] > 0) {
   1498  1.18  christos                     push @skip, !! $1;
   1499  1.18  christos                 } else {
   1500  1.18  christos                     push @skip, -1;
   1501  1.18  christos                 }
   1502  1.18  christos             },
   1503  1.18  christos             qr/^\s*ELSIF\[((?:\\.|[^\\\]])*)\]\s*$/
   1504  1.18  christos             => sub { die "ELSIF out of scope" if ! @skip;
   1505  1.18  christos                      die "ELSIF following ELSE" if abs($skip[$#skip]) == 2;
   1506  1.18  christos                      $skip[$#skip] = -1 if $skip[$#skip] != 0;
   1507  1.18  christos                      $skip[$#skip] = !! $1
   1508  1.18  christos                          if $skip[$#skip] == 0; },
   1509  1.18  christos             qr/^\s*ELSE\s*$/
   1510  1.18  christos             => sub { die "ELSE out of scope" if ! @skip;
   1511  1.18  christos                      $skip[$#skip] = -2 if $skip[$#skip] != 0;
   1512  1.18  christos                      $skip[$#skip] = 2 if $skip[$#skip] == 0; },
   1513  1.18  christos             qr/^\s*ENDIF\s*$/
   1514  1.18  christos             => sub { die "ENDIF out of scope" if ! @skip;
   1515  1.18  christos                      pop @skip; },
   1516  1.18  christos             qr/^\s*PROGRAMS(_NO_INST)?\s*=\s*(.*)\s*$/
   1517  1.18  christos             => sub {
   1518  1.18  christos                 if (!@skip || $skip[$#skip] > 0) {
   1519  1.18  christos                     my $install = $1;
   1520  1.18  christos                     my @x = tokenize($2);
   1521  1.18  christos                     push @programs, @x;
   1522  1.18  christos                     push @programs_install, @x unless $install;
   1523  1.18  christos                 }
   1524  1.18  christos             },
   1525  1.18  christos             qr/^\s*LIBS(_NO_INST)?\s*=\s*(.*)\s*$/
   1526  1.18  christos             => sub {
   1527  1.18  christos                 if (!@skip || $skip[$#skip] > 0) {
   1528  1.18  christos                     my $install = $1;
   1529  1.18  christos                     my @x = tokenize($2);
   1530  1.18  christos                     push @libraries, @x;
   1531  1.18  christos                     push @libraries_install, @x unless $install;
   1532  1.18  christos                 }
   1533  1.18  christos             },
   1534  1.18  christos             qr/^\s*ENGINES(_NO_INST)?\s*=\s*(.*)\s*$/
   1535  1.18  christos             => sub {
   1536  1.18  christos                 if (!@skip || $skip[$#skip] > 0) {
   1537  1.18  christos                     my $install = $1;
   1538  1.18  christos                     my @x = tokenize($2);
   1539  1.18  christos                     push @engines, @x;
   1540  1.18  christos                     push @engines_install, @x unless $install;
   1541  1.18  christos                 }
   1542  1.18  christos             },
   1543  1.18  christos             qr/^\s*SCRIPTS(_NO_INST)?\s*=\s*(.*)\s*$/
   1544  1.18  christos             => sub {
   1545  1.18  christos                 if (!@skip || $skip[$#skip] > 0) {
   1546  1.18  christos                     my $install = $1;
   1547  1.18  christos                     my @x = tokenize($2);
   1548  1.18  christos                     push @scripts, @x;
   1549  1.18  christos                     push @scripts_install, @x unless $install;
   1550  1.18  christos                 }
   1551  1.18  christos             },
   1552  1.18  christos             qr/^\s*EXTRA\s*=\s*(.*)\s*$/
   1553  1.18  christos             => sub { push @extra, tokenize($1)
   1554  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1555  1.18  christos             qr/^\s*OVERRIDES\s*=\s*(.*)\s*$/
   1556  1.18  christos             => sub { push @overrides, tokenize($1)
   1557  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1558  1.18  christos 
   1559  1.18  christos             qr/^\s*ORDINALS\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/,
   1560  1.18  christos             => sub { push @{$ordinals{$1}}, tokenize($2)
   1561  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1562  1.18  christos             qr/^\s*SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
   1563  1.18  christos             => sub { push @{$sources{$1}}, tokenize($2)
   1564  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1565  1.18  christos             qr/^\s*SHARED_SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
   1566  1.18  christos             => sub { push @{$shared_sources{$1}}, tokenize($2)
   1567  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1568  1.18  christos             qr/^\s*INCLUDE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
   1569  1.18  christos             => sub { push @{$includes{$1}}, tokenize($2)
   1570  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1571  1.18  christos             qr/^\s*DEPEND\[((?:\\.|[^\\\]])*)\]\s*=\s*(.*)\s*$/
   1572  1.18  christos             => sub { push @{$depends{$1}}, tokenize($2)
   1573  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1574  1.18  christos             qr/^\s*GENERATE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
   1575  1.18  christos             => sub { push @{$generate{$1}}, $2
   1576  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1577  1.18  christos             qr/^\s*RENAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
   1578  1.18  christos             => sub { push @{$renames{$1}}, tokenize($2)
   1579  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1580  1.18  christos             qr/^\s*SHARED_NAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
   1581  1.18  christos             => sub { push @{$sharednames{$1}}, tokenize($2)
   1582  1.18  christos                          if !@skip || $skip[$#skip] > 0 },
   1583  1.18  christos             qr/^\s*BEGINRAW\[((?:\\.|[^\\\]])+)\]\s*$/
   1584  1.18  christos             => sub {
   1585  1.18  christos                 my $lineiterator = shift;
   1586  1.18  christos                 my $target_kind = $1;
   1587  1.18  christos                 while (defined $lineiterator->()) {
   1588  1.18  christos                     s|\R$||;
   1589  1.18  christos                     if (/^\s*ENDRAW\[((?:\\.|[^\\\]])+)\]\s*$/) {
   1590  1.18  christos                         die "ENDRAW doesn't match BEGINRAW"
   1591  1.18  christos                             if $1 ne $target_kind;
   1592  1.18  christos                         last;
   1593  1.18  christos                     }
   1594  1.18  christos                     next if @skip && $skip[$#skip] <= 0;
   1595  1.18  christos                     push @rawlines,  $_
   1596  1.18  christos                         if ($target_kind eq $target{build_file}
   1597  1.18  christos                             || $target_kind eq $target{build_file}."(".$builder_platform.")");
   1598  1.18  christos                 }
   1599  1.18  christos             },
   1600  1.18  christos             qr/^(?:#.*|\s*)$/ => sub { },
   1601  1.18  christos             "OTHERWISE" => sub { die "Something wrong with this line:\n$_\nat $sourced/$f" },
   1602  1.18  christos             "BEFORE" => sub {
   1603  1.18  christos                 if ($buildinfo_debug) {
   1604  1.18  christos                     print STDERR "DEBUG: Parsing ",join(" ", @_),"\n";
   1605  1.18  christos                     print STDERR "DEBUG: ... before parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
   1606  1.18  christos                 }
   1607  1.18  christos             },
   1608  1.18  christos             "AFTER" => sub {
   1609  1.18  christos                 if ($buildinfo_debug) {
   1610  1.18  christos                     print STDERR "DEBUG: .... after parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
   1611  1.18  christos                 }
   1612  1.18  christos             },
   1613  1.18  christos             );
   1614  1.18  christos         die "runaway IF?" if (@skip);
   1615  1.18  christos 
   1616  1.18  christos         foreach (keys %renames) {
   1617  1.18  christos             die "$_ renamed to more than one thing: "
   1618  1.18  christos                 ,join(" ", @{$renames{$_}}),"\n"
   1619  1.18  christos                 if scalar @{$renames{$_}} > 1;
   1620  1.18  christos             my $dest = cleanfile($buildd, $_, $blddir);
   1621  1.18  christos             my $to = cleanfile($buildd, $renames{$_}->[0], $blddir);
   1622  1.18  christos             die "$dest renamed to more than one thing: "
   1623  1.18  christos                 ,$unified_info{rename}->{$dest}, $to
   1624  1.18  christos                 unless !defined($unified_info{rename}->{$dest})
   1625  1.18  christos                 or $unified_info{rename}->{$dest} eq $to;
   1626  1.18  christos             $unified_info{rename}->{$dest} = $to;
   1627  1.18  christos         }
   1628  1.18  christos 
   1629  1.18  christos         foreach (@programs) {
   1630  1.18  christos             my $program = cleanfile($buildd, $_, $blddir);
   1631  1.18  christos             if ($unified_info{rename}->{$program}) {
   1632  1.18  christos                 $program = $unified_info{rename}->{$program};
   1633  1.18  christos             }
   1634  1.18  christos             $unified_info{programs}->{$program} = 1;
   1635  1.18  christos         }
   1636  1.18  christos 
   1637  1.18  christos         foreach (@programs_install) {
   1638  1.18  christos             my $program = cleanfile($buildd, $_, $blddir);
   1639  1.18  christos             if ($unified_info{rename}->{$program}) {
   1640  1.18  christos                 $program = $unified_info{rename}->{$program};
   1641  1.18  christos             }
   1642  1.18  christos             $unified_info{install}->{programs}->{$program} = 1;
   1643  1.18  christos         }
   1644  1.18  christos 
   1645  1.18  christos         foreach (@libraries) {
   1646  1.18  christos             my $library = cleanfile($buildd, $_, $blddir);
   1647  1.18  christos             if ($unified_info{rename}->{$library}) {
   1648  1.18  christos                 $library = $unified_info{rename}->{$library};
   1649  1.18  christos             }
   1650  1.18  christos             $unified_info{libraries}->{$library} = 1;
   1651  1.18  christos         }
   1652  1.18  christos 
   1653  1.18  christos         foreach (@libraries_install) {
   1654  1.18  christos             my $library = cleanfile($buildd, $_, $blddir);
   1655  1.18  christos             if ($unified_info{rename}->{$library}) {
   1656  1.18  christos                 $library = $unified_info{rename}->{$library};
   1657  1.18  christos             }
   1658  1.18  christos             $unified_info{install}->{libraries}->{$library} = 1;
   1659  1.18  christos         }
   1660  1.18  christos 
   1661  1.18  christos         die <<"EOF" if scalar @engines and !$config{dynamic_engines};
   1662  1.18  christos ENGINES can only be used if configured with 'dynamic-engine'.
   1663  1.18  christos This is usually a fault in a build.info file.
   1664  1.18  christos EOF
   1665  1.18  christos         foreach (@engines) {
   1666  1.18  christos             my $library = cleanfile($buildd, $_, $blddir);
   1667  1.18  christos             if ($unified_info{rename}->{$library}) {
   1668  1.18  christos                 $library = $unified_info{rename}->{$library};
   1669  1.18  christos             }
   1670  1.18  christos             $unified_info{engines}->{$library} = 1;
   1671  1.18  christos         }
   1672  1.18  christos 
   1673  1.18  christos         foreach (@engines_install) {
   1674  1.18  christos             my $library = cleanfile($buildd, $_, $blddir);
   1675  1.18  christos             if ($unified_info{rename}->{$library}) {
   1676  1.18  christos                 $library = $unified_info{rename}->{$library};
   1677  1.18  christos             }
   1678  1.18  christos             $unified_info{install}->{engines}->{$library} = 1;
   1679  1.18  christos         }
   1680  1.18  christos 
   1681  1.18  christos         foreach (@scripts) {
   1682  1.18  christos             my $script = cleanfile($buildd, $_, $blddir);
   1683  1.18  christos             if ($unified_info{rename}->{$script}) {
   1684  1.18  christos                 $script = $unified_info{rename}->{$script};
   1685  1.18  christos             }
   1686  1.18  christos             $unified_info{scripts}->{$script} = 1;
   1687  1.18  christos         }
   1688  1.18  christos 
   1689  1.18  christos         foreach (@scripts_install) {
   1690  1.18  christos             my $script = cleanfile($buildd, $_, $blddir);
   1691  1.18  christos             if ($unified_info{rename}->{$script}) {
   1692  1.18  christos                 $script = $unified_info{rename}->{$script};
   1693  1.18  christos             }
   1694  1.18  christos             $unified_info{install}->{scripts}->{$script} = 1;
   1695  1.18  christos         }
   1696  1.18  christos 
   1697  1.18  christos         foreach (@extra) {
   1698  1.18  christos             my $extra = cleanfile($buildd, $_, $blddir);
   1699  1.18  christos             $unified_info{extra}->{$extra} = 1;
   1700  1.18  christos         }
   1701  1.18  christos 
   1702  1.18  christos         foreach (@overrides) {
   1703  1.18  christos             my $override = cleanfile($buildd, $_, $blddir);
   1704  1.18  christos             $unified_info{overrides}->{$override} = 1;
   1705  1.18  christos         }
   1706  1.18  christos 
   1707  1.18  christos         push @{$unified_info{rawlines}}, @rawlines;
   1708  1.18  christos 
   1709  1.18  christos         unless ($disabled{shared}) {
   1710  1.18  christos             # Check sharednames.
   1711  1.18  christos             foreach (keys %sharednames) {
   1712  1.18  christos                 my $dest = cleanfile($buildd, $_, $blddir);
   1713  1.18  christos                 if ($unified_info{rename}->{$dest}) {
   1714  1.18  christos                     $dest = $unified_info{rename}->{$dest};
   1715  1.18  christos                 }
   1716  1.18  christos                 die "shared_name for $dest with multiple values: "
   1717  1.18  christos                     ,join(" ", @{$sharednames{$_}}),"\n"
   1718  1.18  christos                     if scalar @{$sharednames{$_}} > 1;
   1719  1.18  christos                 my $to = cleanfile($buildd, $sharednames{$_}->[0], $blddir);
   1720  1.18  christos                 die "shared_name found for a library $dest that isn't defined\n"
   1721  1.18  christos                     unless $unified_info{libraries}->{$dest};
   1722  1.18  christos                 die "shared_name for $dest with multiple values: "
   1723  1.18  christos                     ,$unified_info{sharednames}->{$dest}, ", ", $to
   1724  1.18  christos                     unless !defined($unified_info{sharednames}->{$dest})
   1725  1.18  christos                     or $unified_info{sharednames}->{$dest} eq $to;
   1726  1.18  christos                 $unified_info{sharednames}->{$dest} = $to;
   1727  1.18  christos             }
   1728  1.18  christos 
   1729  1.18  christos             # Additionally, we set up sharednames for libraries that don't
   1730  1.18  christos             # have any, as themselves.
   1731  1.18  christos             foreach (keys %{$unified_info{libraries}}) {
   1732  1.18  christos                 if (!defined $unified_info{sharednames}->{$_}) {
   1733  1.18  christos                     $unified_info{sharednames}->{$_} = $_
   1734  1.18  christos                 }
   1735  1.18  christos             }
   1736  1.18  christos         }
   1737  1.18  christos 
   1738  1.18  christos         foreach (keys %ordinals) {
   1739  1.18  christos             my $dest = $_;
   1740  1.18  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   1741  1.18  christos             if ($unified_info{rename}->{$ddest}) {
   1742  1.18  christos                 $ddest = $unified_info{rename}->{$ddest};
   1743  1.18  christos             }
   1744  1.18  christos             foreach (@{$ordinals{$dest}}) {
   1745  1.18  christos                 my %known_ordinals =
   1746  1.18  christos                     (
   1747  1.18  christos                      crypto =>
   1748  1.18  christos                      cleanfile($sourced, catfile("util", "libcrypto.num"), $blddir),
   1749  1.18  christos                      ssl =>
   1750  1.18  christos                      cleanfile($sourced, catfile("util", "libssl.num"), $blddir)
   1751  1.18  christos                     );
   1752  1.18  christos                 my $o = $known_ordinals{$_};
   1753  1.18  christos                 die "Ordinals for $ddest defined more than once\n"
   1754  1.18  christos                     if $unified_info{ordinals}->{$ddest};
   1755  1.18  christos                 $unified_info{ordinals}->{$ddest} = [ $_, $o ];
   1756  1.18  christos             }
   1757  1.18  christos         }
   1758  1.18  christos 
   1759  1.18  christos         foreach (keys %sources) {
   1760  1.18  christos             my $dest = $_;
   1761  1.18  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   1762  1.18  christos             if ($unified_info{rename}->{$ddest}) {
   1763  1.18  christos                 $ddest = $unified_info{rename}->{$ddest};
   1764  1.18  christos             }
   1765  1.18  christos             foreach (@{$sources{$dest}}) {
   1766  1.18  christos                 my $s = cleanfile($sourced, $_, $blddir);
   1767  1.18  christos 
   1768  1.18  christos                 # If it isn't in the source tree, we assume it's generated
   1769  1.18  christos                 # in the build tree
   1770  1.19  christos                 if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
   1771  1.18  christos                     $s = cleanfile($buildd, $_, $blddir);
   1772  1.18  christos                 }
   1773  1.18  christos                 # We recognise C and asm files
   1774  1.18  christos                 if ($s =~ /\.[csS]\b$/) {
   1775  1.18  christos                     (my $o = $_) =~ s/\.[csS]\b$/.o/;
   1776  1.18  christos                     $o = cleanfile($buildd, $o, $blddir);
   1777  1.18  christos                     $unified_info{sources}->{$ddest}->{$o} = 1;
   1778  1.18  christos                     $unified_info{sources}->{$o}->{$s} = 1;
   1779  1.18  christos                 } else {
   1780  1.18  christos                     $unified_info{sources}->{$ddest}->{$s} = 1;
   1781  1.18  christos                 }
   1782  1.18  christos             }
   1783  1.18  christos         }
   1784  1.18  christos 
   1785  1.18  christos         foreach (keys %shared_sources) {
   1786  1.18  christos             my $dest = $_;
   1787  1.18  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   1788  1.18  christos             if ($unified_info{rename}->{$ddest}) {
   1789  1.18  christos                 $ddest = $unified_info{rename}->{$ddest};
   1790  1.18  christos             }
   1791  1.18  christos             foreach (@{$shared_sources{$dest}}) {
   1792  1.18  christos                 my $s = cleanfile($sourced, $_, $blddir);
   1793  1.18  christos 
   1794  1.18  christos                 # If it isn't in the source tree, we assume it's generated
   1795  1.18  christos                 # in the build tree
   1796  1.19  christos                 if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
   1797  1.18  christos                     $s = cleanfile($buildd, $_, $blddir);
   1798  1.18  christos                 }
   1799  1.18  christos                 # We recognise C and asm files
   1800  1.18  christos                 if ($s =~ /\.[csS]\b$/) {
   1801  1.18  christos                     (my $o = $_) =~ s/\.[csS]\b$/.o/;
   1802  1.18  christos                     $o = cleanfile($buildd, $o, $blddir);
   1803  1.18  christos                     $unified_info{shared_sources}->{$ddest}->{$o} = 1;
   1804  1.18  christos                     $unified_info{sources}->{$o}->{$s} = 1;
   1805  1.18  christos                 } else {
   1806  1.18  christos                     die "unrecognised source file type for shared library: $s\n";
   1807  1.18  christos                 }
   1808  1.18  christos             }
   1809  1.18  christos         }
   1810  1.18  christos 
   1811  1.18  christos         foreach (keys %generate) {
   1812  1.18  christos             my $dest = $_;
   1813  1.18  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   1814  1.18  christos             if ($unified_info{rename}->{$ddest}) {
   1815  1.18  christos                 $ddest = $unified_info{rename}->{$ddest};
   1816  1.18  christos             }
   1817  1.18  christos             die "more than one generator for $dest: "
   1818  1.18  christos                     ,join(" ", @{$generate{$_}}),"\n"
   1819  1.18  christos                     if scalar @{$generate{$_}} > 1;
   1820  1.18  christos             my @generator = split /\s+/, $generate{$dest}->[0];
   1821  1.18  christos             $generator[0] = cleanfile($sourced, $generator[0], $blddir),
   1822  1.18  christos             $unified_info{generate}->{$ddest} = [ @generator ];
   1823  1.18  christos         }
   1824  1.18  christos 
   1825  1.18  christos         foreach (keys %depends) {
   1826  1.18  christos             my $dest = $_;
   1827  1.18  christos             my $ddest = $dest eq "" ? "" : cleanfile($sourced, $_, $blddir);
   1828  1.18  christos 
   1829  1.18  christos             # If the destination doesn't exist in source, it can only be
   1830  1.18  christos             # a generated file in the build tree.
   1831  1.19  christos             if ($ddest ne "" && ($ddest eq $src_configdata || ! -f $ddest)) {
   1832  1.18  christos                 $ddest = cleanfile($buildd, $_, $blddir);
   1833  1.18  christos                 if ($unified_info{rename}->{$ddest}) {
   1834  1.18  christos                     $ddest = $unified_info{rename}->{$ddest};
   1835  1.18  christos                 }
   1836  1.18  christos             }
   1837  1.18  christos             foreach (@{$depends{$dest}}) {
   1838  1.18  christos                 my $d = cleanfile($sourced, $_, $blddir);
   1839  1.18  christos 
   1840  1.18  christos                 # If we know it's generated, or assume it is because we can't
   1841  1.18  christos                 # find it in the source tree, we set file we depend on to be
   1842  1.18  christos                 # in the build tree rather than the source tree, and assume
   1843  1.18  christos                 # and that there are lines to build it in a BEGINRAW..ENDRAW
   1844  1.18  christos                 # section or in the Makefile template.
   1845  1.19  christos                 if ($d eq $src_configdata
   1846  1.19  christos                     || ! -f $d
   1847  1.18  christos                     || (grep { $d eq $_ }
   1848  1.18  christos                         map { cleanfile($srcdir, $_, $blddir) }
   1849  1.18  christos                         grep { /\.h$/ } keys %{$unified_info{generate}})) {
   1850  1.18  christos                     $d = cleanfile($buildd, $_, $blddir);
   1851  1.18  christos                 }
   1852  1.18  christos                 # Take note if the file to depend on is being renamed
   1853  1.18  christos                 if ($unified_info{rename}->{$d}) {
   1854  1.18  christos                     $d = $unified_info{rename}->{$d};
   1855  1.18  christos                 }
   1856  1.18  christos                 $unified_info{depends}->{$ddest}->{$d} = 1;
   1857  1.18  christos             }
   1858  1.18  christos         }
   1859  1.18  christos 
   1860  1.18  christos         foreach (keys %includes) {
   1861  1.18  christos             my $dest = $_;
   1862  1.18  christos             my $ddest = cleanfile($sourced, $_, $blddir);
   1863  1.18  christos 
   1864  1.18  christos             # If the destination doesn't exist in source, it can only be
   1865  1.18  christos             # a generated file in the build tree.
   1866  1.19  christos             if ($ddest eq $src_configdata || ! -f $ddest) {
   1867  1.18  christos                 $ddest = cleanfile($buildd, $_, $blddir);
   1868  1.18  christos                 if ($unified_info{rename}->{$ddest}) {
   1869  1.18  christos                     $ddest = $unified_info{rename}->{$ddest};
   1870  1.18  christos                 }
   1871  1.18  christos             }
   1872  1.18  christos             foreach (@{$includes{$dest}}) {
   1873  1.18  christos                 my $is = cleandir($sourced, $_, $blddir);
   1874  1.18  christos                 my $ib = cleandir($buildd, $_, $blddir);
   1875  1.18  christos                 push @{$unified_info{includes}->{$ddest}->{source}}, $is
   1876  1.18  christos                     unless grep { $_ eq $is } @{$unified_info{includes}->{$ddest}->{source}};
   1877  1.18  christos                 push @{$unified_info{includes}->{$ddest}->{build}}, $ib
   1878  1.18  christos                     unless grep { $_ eq $ib } @{$unified_info{includes}->{$ddest}->{build}};
   1879  1.18  christos             }
   1880  1.18  christos         }
   1881  1.18  christos     }
   1882  1.18  christos 
   1883  1.19  christos     # Massage the result
   1884  1.19  christos 
   1885  1.19  christos     # If we depend on a header file or a perl module, add an inclusion of
   1886  1.19  christos     # its directory to allow smoothe inclusion
   1887  1.19  christos     foreach my $dest (keys %{$unified_info{depends}}) {
   1888  1.19  christos         next if $dest eq "";
   1889  1.19  christos         foreach my $d (keys %{$unified_info{depends}->{$dest}}) {
   1890  1.19  christos             next unless $d =~ /\.(h|pm)$/;
   1891  1.19  christos             my $i = dirname($d);
   1892  1.19  christos             my $spot =
   1893  1.19  christos                 $d eq "configdata.pm" || defined($unified_info{generate}->{$d})
   1894  1.19  christos                 ? 'build' : 'source';
   1895  1.19  christos             push @{$unified_info{includes}->{$dest}->{$spot}}, $i
   1896  1.19  christos                 unless grep { $_ eq $i } @{$unified_info{includes}->{$dest}->{$spot}};
   1897  1.19  christos         }
   1898  1.19  christos     }
   1899  1.19  christos 
   1900  1.19  christos     # Trickle down includes placed on libraries, engines and programs to
   1901  1.19  christos     # their sources (i.e. object files)
   1902  1.19  christos     foreach my $dest (keys %{$unified_info{engines}},
   1903  1.19  christos                       keys %{$unified_info{libraries}},
   1904  1.19  christos                       keys %{$unified_info{programs}}) {
   1905  1.19  christos         foreach my $k (("source", "build")) {
   1906  1.19  christos             next unless defined($unified_info{includes}->{$dest}->{$k});
   1907  1.19  christos             my @incs = reverse @{$unified_info{includes}->{$dest}->{$k}};
   1908  1.19  christos             foreach my $obj (grep /\.o$/,
   1909  1.19  christos                              (keys %{$unified_info{sources}->{$dest}},
   1910  1.19  christos                               keys %{$unified_info{shared_sources}->{$dest}})) {
   1911  1.19  christos                 foreach my $inc (@incs) {
   1912  1.19  christos                     unshift @{$unified_info{includes}->{$obj}->{$k}}, $inc
   1913  1.19  christos                         unless grep { $_ eq $inc } @{$unified_info{includes}->{$obj}->{$k}};
   1914  1.19  christos                 }
   1915  1.19  christos             }
   1916  1.19  christos         }
   1917  1.19  christos         delete $unified_info{includes}->{$dest};
   1918  1.19  christos     }
   1919  1.19  christos 
   1920  1.18  christos     ### Make unified_info a bit more efficient
   1921  1.18  christos     # One level structures
   1922  1.18  christos     foreach (("programs", "libraries", "engines", "scripts", "extra", "overrides")) {
   1923  1.18  christos         $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
   1924  1.18  christos     }
   1925  1.18  christos     # Two level structures
   1926  1.18  christos     foreach my $l1 (("install", "sources", "shared_sources", "ldadd", "depends")) {
   1927  1.18  christos         foreach my $l2 (sort keys %{$unified_info{$l1}}) {
   1928  1.18  christos             $unified_info{$l1}->{$l2} =
   1929  1.18  christos                 [ sort keys %{$unified_info{$l1}->{$l2}} ];
   1930  1.18  christos         }
   1931  1.18  christos     }
   1932  1.18  christos     # Includes
   1933  1.18  christos     foreach my $dest (sort keys %{$unified_info{includes}}) {
   1934  1.18  christos         if (defined($unified_info{includes}->{$dest}->{build})) {
   1935  1.19  christos             my @source_includes = ();
   1936  1.19  christos             @source_includes = ( @{$unified_info{includes}->{$dest}->{source}} )
   1937  1.19  christos                 if defined($unified_info{includes}->{$dest}->{source});
   1938  1.18  christos             $unified_info{includes}->{$dest} =
   1939  1.18  christos                 [ @{$unified_info{includes}->{$dest}->{build}} ];
   1940  1.18  christos             foreach my $inc (@source_includes) {
   1941  1.18  christos                 push @{$unified_info{includes}->{$dest}}, $inc
   1942  1.18  christos                     unless grep { $_ eq $inc } @{$unified_info{includes}->{$dest}};
   1943  1.18  christos             }
   1944  1.18  christos         } else {
   1945  1.18  christos             $unified_info{includes}->{$dest} =
   1946  1.18  christos                 [ @{$unified_info{includes}->{$dest}->{source}} ];
   1947  1.18  christos         }
   1948  1.18  christos     }
   1949  1.18  christos }
   1950  1.18  christos 
   1951  1.18  christos # For the schemes that need it, we provide the old *_obj configs
   1952  1.18  christos # from the *_asm_obj ones
   1953  1.18  christos foreach (grep /_(asm|aux)_src$/, keys %target) {
   1954  1.18  christos     my $src = $_;
   1955  1.18  christos     (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
   1956  1.18  christos     ($target{$obj} = $target{$src}) =~ s/\.[csS]\b/.o/g;
   1957  1.18  christos }
   1958  1.18  christos 
   1959  1.18  christos # Write down our configuration where it fits #########################
   1960  1.18  christos 
   1961  1.18  christos open(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
   1962  1.18  christos print OUT <<"EOF";
   1963  1.18  christos package configdata;
   1964  1.18  christos 
   1965  1.18  christos use strict;
   1966  1.18  christos use warnings;
   1967   1.1  christos 
   1968  1.18  christos use Exporter;
   1969  1.18  christos #use vars qw(\@ISA \@EXPORT);
   1970  1.18  christos our \@ISA = qw(Exporter);
   1971  1.18  christos our \@EXPORT = qw(\%config \%target \%disabled \%withargs \%unified_info \@disablables);
   1972   1.1  christos 
   1973  1.18  christos EOF
   1974  1.18  christos print OUT "our %config = (\n";
   1975  1.18  christos foreach (sort keys %config) {
   1976  1.18  christos     if (ref($config{$_}) eq "ARRAY") {
   1977  1.18  christos 	print OUT "  ", $_, " => [ ", join(", ",
   1978  1.18  christos 					   map { quotify("perl", $_) }
   1979  1.18  christos 					   @{$config{$_}}), " ],\n";
   1980  1.18  christos     } else {
   1981  1.18  christos 	print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
   1982  1.18  christos     }
   1983  1.18  christos }
   1984  1.18  christos print OUT <<"EOF";
   1985  1.18  christos );
   1986  1.18  christos 
   1987  1.18  christos EOF
   1988  1.18  christos print OUT "our %target = (\n";
   1989  1.18  christos foreach (sort keys %target) {
   1990  1.18  christos     if (ref($target{$_}) eq "ARRAY") {
   1991  1.18  christos 	print OUT "  ", $_, " => [ ", join(", ",
   1992  1.18  christos 					   map { quotify("perl", $_) }
   1993  1.18  christos 					   @{$target{$_}}), " ],\n";
   1994  1.18  christos     } else {
   1995  1.18  christos 	print OUT "  ", $_, " => ", quotify("perl", $target{$_}), ",\n"
   1996  1.18  christos     }
   1997  1.18  christos }
   1998  1.18  christos print OUT <<"EOF";
   1999  1.18  christos );
   2000   1.1  christos 
   2001  1.18  christos EOF
   2002  1.18  christos print OUT "our \%available_protocols = (\n";
   2003  1.18  christos print OUT "  tls => [ ", join(", ", map { quotify("perl", $_) } @tls), " ],\n";
   2004  1.18  christos print OUT "  dtls => [ ", join(", ", map { quotify("perl", $_) } @dtls), " ],\n";
   2005  1.18  christos print OUT <<"EOF";
   2006  1.18  christos );
   2007   1.1  christos 
   2008  1.18  christos EOF
   2009  1.18  christos print OUT "our \@disablables = (\n";
   2010  1.18  christos foreach (@disablables) {
   2011  1.18  christos     print OUT "  ", quotify("perl", $_), ",\n";
   2012  1.18  christos }
   2013  1.18  christos print OUT <<"EOF";
   2014  1.18  christos );
   2015   1.1  christos 
   2016  1.18  christos EOF
   2017  1.18  christos print OUT "our \%disabled = (\n";
   2018  1.18  christos foreach (sort keys %disabled) {
   2019  1.18  christos     print OUT "  ", quotify("perl", $_), " => ", quotify("perl", $disabled{$_}), ",\n";
   2020  1.18  christos }
   2021  1.18  christos print OUT <<"EOF";
   2022  1.18  christos );
   2023   1.1  christos 
   2024   1.1  christos EOF
   2025  1.18  christos print OUT "our %withargs = (\n";
   2026  1.18  christos foreach (sort keys %withargs) {
   2027  1.18  christos     if (ref($withargs{$_}) eq "ARRAY") {
   2028  1.18  christos 	print OUT "  ", $_, " => [ ", join(", ",
   2029  1.18  christos 					   map { quotify("perl", $_) }
   2030  1.18  christos 					   @{$withargs{$_}}), " ],\n";
   2031  1.18  christos     } else {
   2032  1.18  christos 	print OUT "  ", $_, " => ", quotify("perl", $withargs{$_}), ",\n"
   2033  1.18  christos     }
   2034  1.18  christos }
   2035  1.18  christos print OUT <<"EOF";
   2036  1.18  christos );
   2037  1.18  christos 
   2038   1.1  christos EOF
   2039  1.18  christos if ($builder eq "unified") {
   2040  1.18  christos     my $recurse;
   2041  1.18  christos     $recurse = sub {
   2042  1.18  christos         my $indent = shift;
   2043  1.18  christos         foreach (@_) {
   2044  1.18  christos             if (ref $_ eq "ARRAY") {
   2045  1.18  christos                 print OUT " "x$indent, "[\n";
   2046  1.18  christos                 foreach (@$_) {
   2047  1.18  christos                     $recurse->($indent + 4, $_);
   2048  1.18  christos                 }
   2049  1.18  christos                 print OUT " "x$indent, "],\n";
   2050  1.18  christos             } elsif (ref $_ eq "HASH") {
   2051  1.18  christos                 my %h = %$_;
   2052  1.18  christos                 print OUT " "x$indent, "{\n";
   2053  1.18  christos                 foreach (sort keys %h) {
   2054  1.18  christos                     if (ref $h{$_} eq "") {
   2055  1.18  christos                         print OUT " "x($indent + 4), quotify("perl", $_), " => ", quotify("perl", $h{$_}), ",\n";
   2056  1.18  christos                     } else {
   2057  1.18  christos                         print OUT " "x($indent + 4), quotify("perl", $_), " =>\n";
   2058  1.18  christos                         $recurse->($indent + 8, $h{$_});
   2059  1.18  christos                     }
   2060  1.18  christos                 }
   2061  1.18  christos                 print OUT " "x$indent, "},\n";
   2062  1.18  christos             } else {
   2063  1.18  christos                 print OUT " "x$indent, quotify("perl", $_), ",\n";
   2064  1.18  christos             }
   2065  1.18  christos         }
   2066  1.18  christos     };
   2067  1.18  christos     print OUT "our %unified_info = (\n";
   2068  1.18  christos     foreach (sort keys %unified_info) {
   2069  1.18  christos         if (ref $unified_info{$_} eq "") {
   2070  1.18  christos             print OUT " "x4, quotify("perl", $_), " => ", quotify("perl", $unified_info{$_}), ",\n";
   2071  1.18  christos         } else {
   2072  1.18  christos             print OUT " "x4, quotify("perl", $_), " =>\n";
   2073  1.18  christos             $recurse->(8, $unified_info{$_});
   2074  1.18  christos         }
   2075  1.18  christos     }
   2076  1.18  christos     print OUT <<"EOF";
   2077  1.18  christos );
   2078  1.18  christos 
   2079  1.18  christos EOF
   2080  1.18  christos }
   2081  1.18  christos print OUT "1;\n";
   2082  1.18  christos close(OUT);
   2083  1.18  christos 
   2084  1.18  christos 
   2085  1.18  christos print "CC            =$config{cross_compile_prefix}$target{cc}\n";
   2086  1.18  christos print "CFLAG         =$target{cflags} $config{cflags}\n";
   2087  1.18  christos print "SHARED_CFLAG  =$target{shared_cflag}\n";
   2088  1.18  christos print "DEFINES       =",join(" ", @{$target{defines}}, @{$config{defines}}),"\n";
   2089  1.18  christos print "LFLAG         =$target{lflags}\n";
   2090  1.18  christos print "PLIB_LFLAG    =$target{plib_lflags}\n";
   2091  1.18  christos print "EX_LIBS       =$target{ex_libs} $config{ex_libs}\n";
   2092  1.18  christos print "APPS_OBJ      =$target{apps_obj}\n";
   2093  1.18  christos print "CPUID_OBJ     =$target{cpuid_obj}\n";
   2094  1.18  christos print "UPLINK_OBJ    =$target{uplink_obj}\n";
   2095  1.18  christos print "BN_ASM        =$target{bn_obj}\n";
   2096  1.18  christos print "EC_ASM        =$target{ec_obj}\n";
   2097  1.18  christos print "DES_ENC       =$target{des_obj}\n";
   2098  1.18  christos print "AES_ENC       =$target{aes_obj}\n";
   2099  1.18  christos print "BF_ENC        =$target{bf_obj}\n";
   2100  1.18  christos print "CAST_ENC      =$target{cast_obj}\n";
   2101  1.18  christos print "RC4_ENC       =$target{rc4_obj}\n";
   2102  1.18  christos print "RC5_ENC       =$target{rc5_obj}\n";
   2103  1.18  christos print "MD5_OBJ_ASM   =$target{md5_obj}\n";
   2104  1.18  christos print "SHA1_OBJ_ASM  =$target{sha1_obj}\n";
   2105  1.18  christos print "RMD160_OBJ_ASM=$target{rmd160_obj}\n";
   2106  1.18  christos print "CMLL_ENC      =$target{cmll_obj}\n";
   2107  1.18  christos print "MODES_OBJ     =$target{modes_obj}\n";
   2108  1.18  christos print "PADLOCK_OBJ   =$target{padlock_obj}\n";
   2109  1.18  christos print "CHACHA_ENC    =$target{chacha_obj}\n";
   2110  1.18  christos print "POLY1305_OBJ  =$target{poly1305_obj}\n";
   2111  1.18  christos print "BLAKE2_OBJ    =$target{blake2_obj}\n";
   2112  1.18  christos print "PROCESSOR     =$config{processor}\n";
   2113  1.18  christos print "RANLIB        =", $target{ranlib} eq '$(CROSS_COMPILE)ranlib' ?
   2114  1.18  christos                              "$config{cross_compile_prefix}ranlib" :
   2115  1.18  christos                              "$target{ranlib}", "\n";
   2116  1.18  christos print "ARFLAGS       =$target{arflags}\n";
   2117  1.18  christos print "PERL          =$config{perl}\n";
   2118  1.18  christos print "\n";
   2119  1.18  christos print "SIXTY_FOUR_BIT_LONG mode\n" if $config{b64l};
   2120  1.18  christos print "SIXTY_FOUR_BIT mode\n" if $config{b64};
   2121  1.18  christos print "THIRTY_TWO_BIT mode\n" if $config{b32};
   2122  1.18  christos print "BN_LLONG mode\n" if $config{bn_ll};
   2123  1.18  christos print "RC4 uses $config{rc4_int}\n" if $config{rc4_int} ne $def_int;
   2124  1.18  christos 
   2125  1.18  christos my %builders = (
   2126  1.18  christos     unified => sub {
   2127  1.18  christos         run_dofile(catfile($blddir, $target{build_file}),
   2128  1.18  christos                    @{$config{build_file_templates}});
   2129  1.18  christos     },
   2130  1.18  christos     );
   2131  1.18  christos 
   2132  1.18  christos $builders{$builder}->($builder_platform, @builder_opts);
   2133  1.18  christos 
   2134  1.20  christos $SIG{__DIE__} = $orig_death_handler;
   2135  1.20  christos 
   2136  1.18  christos print <<"EOF";
   2137   1.1  christos 
   2138   1.1  christos Configured for $target.
   2139   1.1  christos EOF
   2140   1.1  christos 
   2141  1.18  christos print <<"EOF" if ($disabled{threads} eq "unavailable");
   2142   1.1  christos 
   2143   1.1  christos The library could not be configured for supporting multi-threaded
   2144   1.1  christos applications as the compiler options required on this system are not known.
   2145   1.1  christos See file INSTALL for details if you need multi-threading.
   2146   1.1  christos EOF
   2147   1.1  christos 
   2148  1.18  christos print <<"EOF" if ($no_shared_warn);
   2149   1.1  christos 
   2150  1.18  christos The options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
   2151  1.18  christos platform, so we will pretend you gave the option 'no-pic', which also disables
   2152  1.18  christos 'shared' and 'dynamic-engine'.  If you know how to implement shared libraries
   2153  1.18  christos or position independent code, please let us know (but please first make sure
   2154  1.18  christos you have tried with a current version of OpenSSL).
   2155  1.14  christos EOF
   2156  1.14  christos 
   2157   1.1  christos exit(0);
   2158   1.1  christos 
   2159  1.18  christos ######################################################################
   2160  1.18  christos #
   2161  1.18  christos # Helpers and utility functions
   2162  1.18  christos #
   2163  1.18  christos 
   2164  1.20  christos # Death handler, to print a helpful message in case of failure #######
   2165  1.20  christos #
   2166  1.20  christos sub death_handler {
   2167  1.20  christos     die @_ if $^S;              # To prevent the added message in eval blocks
   2168  1.20  christos     my $build_file = $target{build_file} // "build file";
   2169  1.20  christos     my @message = ( <<"_____", @_ );
   2170  1.20  christos 
   2171  1.20  christos Failure!  $build_file wasn't produced.
   2172  1.20  christos Please read INSTALL and associated NOTES files.  You may also have to look over
   2173  1.20  christos your available compiler tool chain or change your configuration.
   2174  1.20  christos 
   2175  1.20  christos _____
   2176  1.20  christos 
   2177  1.20  christos     # Dying is terminal, so it's ok to reset the signal handler here.
   2178  1.20  christos     $SIG{__DIE__} = $orig_death_handler;
   2179  1.20  christos     die @message;
   2180  1.20  christos }
   2181  1.20  christos 
   2182  1.18  christos # Configuration file reading #########################################
   2183  1.18  christos 
   2184  1.18  christos # Note: All of the helper functions are for lazy evaluation.  They all
   2185  1.18  christos # return a CODE ref, which will return the intended value when evaluated.
   2186  1.18  christos # Thus, whenever there's mention of a returned value, it's about that
   2187  1.18  christos # intended value.
   2188  1.18  christos 
   2189  1.18  christos # Helper function to implement conditional inheritance depending on the
   2190  1.18  christos # value of $disabled{asm}.  Used in inherit_from values as follows:
   2191  1.18  christos #
   2192  1.18  christos #      inherit_from => [ "template", asm("asm_tmpl") ]
   2193  1.18  christos #
   2194  1.18  christos sub asm {
   2195  1.18  christos     my @x = @_;
   2196  1.18  christos     sub {
   2197  1.18  christos 	$disabled{asm} ? () : @x;
   2198  1.18  christos     }
   2199  1.18  christos }
   2200  1.18  christos 
   2201  1.18  christos # Helper function to implement conditional value variants, with a default
   2202  1.18  christos # plus additional values based on the value of $config{build_type}.
   2203  1.18  christos # Arguments are given in hash table form:
   2204  1.18  christos #
   2205  1.18  christos #       picker(default => "Basic string: ",
   2206  1.18  christos #              debug   => "debug",
   2207  1.18  christos #              release => "release")
   2208  1.18  christos #
   2209  1.18  christos # When configuring with --debug, the resulting string will be
   2210  1.18  christos # "Basic string: debug", and when not, it will be "Basic string: release"
   2211  1.18  christos #
   2212  1.18  christos # This can be used to create variants of sets of flags according to the
   2213  1.18  christos # build type:
   2214  1.18  christos #
   2215  1.18  christos #       cflags => picker(default => "-Wall",
   2216  1.18  christos #                        debug   => "-g -O0",
   2217  1.18  christos #                        release => "-O3")
   2218  1.18  christos #
   2219  1.18  christos sub picker {
   2220  1.18  christos     my %opts = @_;
   2221  1.18  christos     return sub { add($opts{default} || (),
   2222  1.18  christos                      $opts{$config{build_type}} || ())->(); }
   2223  1.18  christos }
   2224  1.18  christos 
   2225  1.18  christos # Helper function to combine several values of different types into one.
   2226  1.18  christos # This is useful if you want to combine a string with the result of a
   2227  1.18  christos # lazy function, such as:
   2228  1.18  christos #
   2229  1.18  christos #       cflags => combine("-Wall", sub { $disabled{zlib} ? () : "-DZLIB" })
   2230  1.18  christos #
   2231  1.18  christos sub combine {
   2232  1.18  christos     my @stuff = @_;
   2233  1.18  christos     return sub { add(@stuff)->(); }
   2234  1.18  christos }
   2235  1.18  christos 
   2236  1.18  christos # Helper function to implement conditional values depending on the value
   2237  1.18  christos # of $disabled{threads}.  Can be used as follows:
   2238  1.18  christos #
   2239  1.18  christos #       cflags => combine("-Wall", threads("-pthread"))
   2240  1.18  christos #
   2241  1.18  christos sub threads {
   2242  1.18  christos     my @flags = @_;
   2243  1.18  christos     return sub { add($disabled{threads} ? () : @flags)->(); }
   2244  1.18  christos }
   2245  1.18  christos 
   2246  1.18  christos 
   2247  1.18  christos 
   2248  1.18  christos our $add_called = 0;
   2249  1.18  christos # Helper function to implement adding values to already existing configuration
   2250  1.18  christos # values.  It handles elements that are ARRAYs, CODEs and scalars
   2251  1.18  christos sub _add {
   2252  1.18  christos     my $separator = shift;
   2253  1.18  christos 
   2254  1.18  christos     # If there's any ARRAY in the collection of values OR the separator
   2255  1.18  christos     # is undef, we will return an ARRAY of combined values, otherwise a
   2256  1.18  christos     # string of joined values with $separator as the separator.
   2257  1.18  christos     my $found_array = !defined($separator);
   2258  1.18  christos 
   2259  1.18  christos     my @values =
   2260  1.18  christos 	map {
   2261  1.18  christos 	    my $res = $_;
   2262  1.18  christos 	    while (ref($res) eq "CODE") {
   2263  1.18  christos 		$res = $res->();
   2264  1.18  christos 	    }
   2265  1.18  christos 	    if (defined($res)) {
   2266  1.18  christos 		if (ref($res) eq "ARRAY") {
   2267  1.18  christos 		    $found_array = 1;
   2268  1.18  christos 		    @$res;
   2269  1.18  christos 		} else {
   2270  1.18  christos 		    $res;
   2271  1.18  christos 		}
   2272  1.18  christos 	    } else {
   2273  1.18  christos 		();
   2274  1.18  christos 	    }
   2275  1.18  christos     } (@_);
   2276  1.18  christos 
   2277  1.18  christos     $add_called = 1;
   2278  1.18  christos 
   2279  1.18  christos     if ($found_array) {
   2280  1.18  christos 	[ @values ];
   2281  1.18  christos     } else {
   2282  1.18  christos 	join($separator, grep { defined($_) && $_ ne "" } @values);
   2283  1.18  christos     }
   2284  1.18  christos }
   2285  1.18  christos sub add_before {
   2286  1.18  christos     my $separator = " ";
   2287  1.18  christos     if (ref($_[$#_]) eq "HASH") {
   2288  1.18  christos         my $opts = pop;
   2289  1.18  christos         $separator = $opts->{separator};
   2290  1.18  christos     }
   2291  1.18  christos     my @x = @_;
   2292  1.18  christos     sub { _add($separator, @x, @_) };
   2293  1.18  christos }
   2294  1.18  christos sub add {
   2295  1.18  christos     my $separator = " ";
   2296  1.18  christos     if (ref($_[$#_]) eq "HASH") {
   2297  1.18  christos         my $opts = pop;
   2298  1.18  christos         $separator = $opts->{separator};
   2299  1.18  christos     }
   2300  1.18  christos     my @x = @_;
   2301  1.18  christos     sub { _add($separator, @_, @x) };
   2302  1.18  christos }
   2303  1.18  christos 
   2304  1.18  christos # configuration reader, evaluates the input file as a perl script and expects
   2305  1.18  christos # it to fill %targets with target configurations.  Those are then added to
   2306  1.18  christos # %table.
   2307  1.18  christos sub read_config {
   2308  1.18  christos     my $fname = shift;
   2309  1.18  christos     open(CONFFILE, "< $fname")
   2310  1.18  christos 	or die "Can't open configuration file '$fname'!\n";
   2311  1.18  christos     my $x = $/;
   2312  1.18  christos     undef $/;
   2313  1.18  christos     my $content = <CONFFILE>;
   2314  1.18  christos     $/ = $x;
   2315  1.18  christos     close(CONFFILE);
   2316  1.18  christos     my %targets = ();
   2317  1.18  christos     {
   2318  1.18  christos 	# Protect certain tables from tampering
   2319  1.18  christos 	local %table = %::table;
   2320  1.18  christos 
   2321  1.18  christos 	eval $content;
   2322  1.18  christos 	warn $@ if $@;
   2323  1.18  christos     }
   2324  1.19  christos     my %preexisting = ();
   2325  1.19  christos     foreach (sort keys %targets) {
   2326  1.19  christos         $preexisting{$_} = 1 if $table{$_};
   2327  1.19  christos     }
   2328  1.19  christos     die <<"EOF",
   2329  1.19  christos The following config targets from $fname
   2330  1.19  christos shadow pre-existing config targets with the same name:
   2331  1.19  christos EOF
   2332  1.19  christos         map { "  $_\n" } sort keys %preexisting
   2333  1.19  christos         if %preexisting;
   2334  1.19  christos 
   2335  1.18  christos 
   2336  1.18  christos     # For each target, check that it's configured with a hash table.
   2337  1.18  christos     foreach (keys %targets) {
   2338  1.18  christos 	if (ref($targets{$_}) ne "HASH") {
   2339  1.18  christos 	    if (ref($targets{$_}) eq "") {
   2340  1.18  christos 		warn "Deprecated target configuration for $_, ignoring...\n";
   2341  1.18  christos 	    } else {
   2342  1.18  christos 		warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
   2343  1.18  christos 	    }
   2344  1.18  christos 	    delete $targets{$_};
   2345  1.18  christos 	} else {
   2346  1.18  christos             $targets{$_}->{_conf_fname_int} = add([ $fname ]);
   2347  1.18  christos         }
   2348  1.18  christos     }
   2349  1.18  christos 
   2350  1.18  christos     %table = (%table, %targets);
   2351  1.18  christos 
   2352  1.18  christos }
   2353  1.18  christos 
   2354  1.18  christos # configuration resolver.  Will only resolve all the lazy evaluation
   2355  1.18  christos # codeblocks for the chosen target and all those it inherits from,
   2356  1.18  christos # recursively
   2357  1.18  christos sub resolve_config {
   2358  1.18  christos     my $target = shift;
   2359  1.18  christos     my @breadcrumbs = @_;
   2360  1.18  christos 
   2361  1.18  christos #    my $extra_checks = defined($ENV{CONFIGURE_EXTRA_CHECKS});
   2362  1.18  christos 
   2363  1.18  christos     if (grep { $_ eq $target } @breadcrumbs) {
   2364  1.18  christos 	die "inherit_from loop!  target backtrace:\n  "
   2365  1.18  christos 	    ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
   2366  1.18  christos     }
   2367  1.18  christos 
   2368  1.18  christos     if (!defined($table{$target})) {
   2369  1.18  christos 	warn "Warning! target $target doesn't exist!\n";
   2370  1.18  christos 	return ();
   2371  1.18  christos     }
   2372  1.18  christos     # Recurse through all inheritances.  They will be resolved on the
   2373  1.18  christos     # fly, so when this operation is done, they will all just be a
   2374  1.18  christos     # bunch of attributes with string values.
   2375  1.18  christos     # What we get here, though, are keys with references to lists of
   2376  1.18  christos     # the combined values of them all.  We will deal with lists after
   2377  1.18  christos     # this stage is done.
   2378  1.18  christos     my %combined_inheritance = ();
   2379  1.18  christos     if ($table{$target}->{inherit_from}) {
   2380  1.18  christos 	my @inherit_from =
   2381  1.18  christos 	    map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
   2382  1.18  christos 	foreach (@inherit_from) {
   2383  1.18  christos 	    my %inherited_config = resolve_config($_, $target, @breadcrumbs);
   2384  1.18  christos 
   2385  1.18  christos 	    # 'template' is a marker that's considered private to
   2386  1.18  christos 	    # the config that had it.
   2387  1.18  christos 	    delete $inherited_config{template};
   2388  1.18  christos 
   2389  1.18  christos 	    foreach (keys %inherited_config) {
   2390  1.18  christos 		if (!$combined_inheritance{$_}) {
   2391  1.18  christos 		    $combined_inheritance{$_} = [];
   2392  1.18  christos 		}
   2393  1.18  christos 		push @{$combined_inheritance{$_}}, $inherited_config{$_};
   2394  1.18  christos 	    }
   2395  1.18  christos 	}
   2396  1.18  christos     }
   2397  1.18  christos 
   2398  1.18  christos     # We won't need inherit_from in this target any more, since we've
   2399  1.18  christos     # resolved all the inheritances that lead to this
   2400  1.18  christos     delete $table{$target}->{inherit_from};
   2401  1.18  christos 
   2402  1.18  christos     # Now is the time to deal with those lists.  Here's the place to
   2403  1.18  christos     # decide what shall be done with those lists, all based on the
   2404  1.18  christos     # values of the target we're currently dealing with.
   2405  1.18  christos     # - If a value is a coderef, it will be executed with the list of
   2406  1.18  christos     #   inherited values as arguments.
   2407  1.18  christos     # - If the corresponding key doesn't have a value at all or is the
   2408  1.18  christos     #   empty string, the inherited value list will be run through the
   2409  1.18  christos     #   default combiner (below), and the result becomes this target's
   2410  1.18  christos     #   value.
   2411  1.18  christos     # - Otherwise, this target's value is assumed to be a string that
   2412  1.18  christos     #   will simply override the inherited list of values.
   2413  1.18  christos     my $default_combiner = add();
   2414  1.18  christos 
   2415  1.18  christos     my %all_keys =
   2416  1.18  christos 	map { $_ => 1 } (keys %combined_inheritance,
   2417  1.18  christos 			 keys %{$table{$target}});
   2418  1.18  christos 
   2419  1.18  christos     sub process_values {
   2420  1.18  christos 	my $object    = shift;
   2421  1.18  christos 	my $inherited = shift;  # Always a [ list ]
   2422  1.18  christos 	my $target    = shift;
   2423  1.18  christos 	my $entry     = shift;
   2424  1.18  christos 
   2425  1.18  christos         $add_called = 0;
   2426  1.18  christos 
   2427  1.18  christos         while(ref($object) eq "CODE") {
   2428  1.18  christos             $object = $object->(@$inherited);
   2429  1.18  christos         }
   2430  1.18  christos         if (!defined($object)) {
   2431  1.18  christos             return ();
   2432  1.18  christos         }
   2433  1.18  christos         elsif (ref($object) eq "ARRAY") {
   2434  1.18  christos             local $add_called;  # To make sure recursive calls don't affect it
   2435  1.18  christos             return [ map { process_values($_, $inherited, $target, $entry) }
   2436  1.18  christos                      @$object ];
   2437  1.18  christos         } elsif (ref($object) eq "") {
   2438  1.18  christos             return $object;
   2439  1.18  christos         } else {
   2440  1.18  christos             die "cannot handle reference type ",ref($object)
   2441  1.18  christos                 ," found in target ",$target," -> ",$entry,"\n";
   2442  1.18  christos         }
   2443  1.18  christos     }
   2444  1.18  christos 
   2445  1.18  christos     foreach (sort keys %all_keys) {
   2446  1.18  christos         my $previous = $combined_inheritance{$_};
   2447  1.18  christos 
   2448  1.18  christos 	# Current target doesn't have a value for the current key?
   2449  1.18  christos 	# Assign it the default combiner, the rest of this loop body
   2450  1.18  christos 	# will handle it just like any other coderef.
   2451  1.18  christos 	if (!exists $table{$target}->{$_}) {
   2452  1.18  christos 	    $table{$target}->{$_} = $default_combiner;
   2453  1.18  christos 	}
   2454  1.18  christos 
   2455  1.18  christos 	$table{$target}->{$_} = process_values($table{$target}->{$_},
   2456  1.18  christos 					       $combined_inheritance{$_},
   2457  1.18  christos 					       $target, $_);
   2458  1.18  christos         unless(defined($table{$target}->{$_})) {
   2459  1.18  christos             delete $table{$target}->{$_};
   2460  1.18  christos         }
   2461  1.18  christos #        if ($extra_checks &&
   2462  1.18  christos #            $previous && !($add_called ||  $previous ~~ $table{$target}->{$_})) {
   2463  1.18  christos #            warn "$_ got replaced in $target\n";
   2464  1.18  christos #        }
   2465  1.18  christos     }
   2466  1.18  christos 
   2467  1.18  christos     # Finally done, return the result.
   2468  1.18  christos     return %{$table{$target}};
   2469  1.18  christos }
   2470  1.18  christos 
   2471   1.1  christos sub usage
   2472   1.1  christos 	{
   2473   1.1  christos 	print STDERR $usage;
   2474   1.1  christos 	print STDERR "\npick os/compiler from:\n";
   2475   1.1  christos 	my $j=0;
   2476   1.1  christos 	my $i;
   2477   1.1  christos         my $k=0;
   2478   1.1  christos 	foreach $i (sort keys %table)
   2479   1.1  christos 		{
   2480  1.18  christos 		next if $table{$i}->{template};
   2481   1.1  christos 		next if $i =~ /^debug/;
   2482   1.1  christos 		$k += length($i) + 1;
   2483   1.1  christos 		if ($k > 78)
   2484   1.1  christos 			{
   2485   1.1  christos 			print STDERR "\n";
   2486   1.1  christos 			$k=length($i);
   2487   1.1  christos 			}
   2488   1.1  christos 		print STDERR $i . " ";
   2489   1.1  christos 		}
   2490   1.1  christos 	foreach $i (sort keys %table)
   2491   1.1  christos 		{
   2492  1.18  christos 		next if $table{$i}->{template};
   2493   1.1  christos 		next if $i !~ /^debug/;
   2494   1.1  christos 		$k += length($i) + 1;
   2495   1.1  christos 		if ($k > 78)
   2496   1.1  christos 			{
   2497   1.1  christos 			print STDERR "\n";
   2498   1.1  christos 			$k=length($i);
   2499   1.1  christos 			}
   2500   1.1  christos 		print STDERR $i . " ";
   2501   1.1  christos 		}
   2502   1.1  christos 	print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n";
   2503   1.1  christos 	exit(1);
   2504   1.1  christos 	}
   2505   1.1  christos 
   2506  1.18  christos sub run_dofile
   2507  1.18  christos {
   2508  1.18  christos     my $out = shift;
   2509  1.18  christos     my @templates = @_;
   2510  1.18  christos 
   2511  1.18  christos     unlink $out || warn "Can't remove $out, $!"
   2512  1.18  christos         if -f $out;
   2513  1.18  christos     foreach (@templates) {
   2514  1.18  christos         die "Can't open $_, $!" unless -f $_;
   2515  1.18  christos     }
   2516  1.18  christos     my $perlcmd = (quotify("maybeshell", $config{perl}))[0];
   2517  1.18  christos     my $cmd = "$perlcmd \"-I.\" \"-Mconfigdata\" \"$dofile\" -o\"Configure\" \"".join("\" \"",@templates)."\" > \"$out.new\"";
   2518  1.18  christos     #print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
   2519  1.18  christos     system($cmd);
   2520  1.18  christos     exit 1 if $? != 0;
   2521  1.18  christos     rename("$out.new", $out) || die "Can't rename $out.new, $!";
   2522  1.18  christos }
   2523  1.18  christos 
   2524   1.1  christos sub which
   2525  1.18  christos {
   2526  1.18  christos     my ($name)=@_;
   2527   1.1  christos 
   2528  1.18  christos     if (eval { require IPC::Cmd; 1; }) {
   2529  1.18  christos         IPC::Cmd->import();
   2530  1.18  christos         return scalar IPC::Cmd::can_run($name);
   2531  1.18  christos     } else {
   2532  1.18  christos         # if there is $directories component in splitpath,
   2533  1.18  christos         # then it's not something to test with $PATH...
   2534  1.18  christos         return $name if (File::Spec->splitpath($name))[1];
   2535  1.18  christos 
   2536  1.18  christos         foreach (File::Spec->path()) {
   2537  1.18  christos             my $fullpath = catfile($_, "$name$target{exe_extension}");
   2538  1.18  christos             if (-f $fullpath and -x $fullpath) {
   2539  1.18  christos                 return $fullpath;
   2540  1.18  christos             }
   2541  1.18  christos         }
   2542  1.18  christos     }
   2543  1.18  christos }
   2544   1.1  christos 
   2545  1.18  christos # Configuration printer ##############################################
   2546   1.1  christos 
   2547   1.1  christos sub print_table_entry
   2548  1.18  christos {
   2549  1.18  christos     my $target = shift;
   2550  1.18  christos     my %target = resolve_config($target);
   2551  1.18  christos     my $type = shift;
   2552  1.18  christos 
   2553  1.18  christos     # Don't print the templates
   2554  1.18  christos     return if $target{template};
   2555  1.18  christos 
   2556  1.18  christos     my @sequence = (
   2557  1.18  christos 	"sys_id",
   2558  1.18  christos 	"cc",
   2559  1.18  christos 	"cflags",
   2560  1.18  christos 	"defines",
   2561  1.18  christos 	"unistd",
   2562  1.18  christos 	"ld",
   2563  1.18  christos 	"lflags",
   2564  1.18  christos 	"loutflag",
   2565  1.18  christos 	"plib_lflags",
   2566  1.18  christos 	"ex_libs",
   2567  1.18  christos 	"bn_ops",
   2568  1.18  christos 	"apps_aux_src",
   2569  1.18  christos 	"cpuid_asm_src",
   2570  1.18  christos 	"uplink_aux_src",
   2571  1.18  christos 	"bn_asm_src",
   2572  1.18  christos 	"ec_asm_src",
   2573  1.18  christos 	"des_asm_src",
   2574  1.18  christos 	"aes_asm_src",
   2575  1.18  christos 	"bf_asm_src",
   2576  1.18  christos 	"md5_asm_src",
   2577  1.18  christos 	"cast_asm_src",
   2578  1.18  christos 	"sha1_asm_src",
   2579  1.18  christos 	"rc4_asm_src",
   2580  1.18  christos 	"rmd160_asm_src",
   2581  1.18  christos 	"rc5_asm_src",
   2582  1.18  christos 	"wp_asm_src",
   2583  1.18  christos 	"cmll_asm_src",
   2584  1.18  christos 	"modes_asm_src",
   2585  1.18  christos 	"padlock_asm_src",
   2586  1.18  christos 	"chacha_asm_src",
   2587  1.18  christos 	"poly1035_asm_src",
   2588  1.18  christos 	"thread_scheme",
   2589  1.18  christos 	"perlasm_scheme",
   2590  1.18  christos 	"dso_scheme",
   2591  1.18  christos 	"shared_target",
   2592  1.18  christos 	"shared_cflag",
   2593  1.18  christos 	"shared_defines",
   2594  1.18  christos 	"shared_ldflag",
   2595  1.18  christos 	"shared_rcflag",
   2596  1.18  christos 	"shared_extension",
   2597  1.18  christos 	"dso_extension",
   2598  1.18  christos 	"obj_extension",
   2599  1.18  christos 	"exe_extension",
   2600  1.18  christos 	"ranlib",
   2601  1.18  christos 	"ar",
   2602  1.18  christos 	"arflags",
   2603  1.18  christos 	"aroutflag",
   2604  1.18  christos 	"rc",
   2605  1.18  christos 	"rcflags",
   2606  1.18  christos 	"rcoutflag",
   2607  1.18  christos 	"mt",
   2608  1.18  christos 	"mtflags",
   2609  1.18  christos 	"mtinflag",
   2610  1.18  christos 	"mtoutflag",
   2611  1.18  christos 	"multilib",
   2612  1.18  christos 	"build_scheme",
   2613  1.18  christos 	);
   2614   1.1  christos 
   2615  1.18  christos     if ($type eq "TABLE") {
   2616  1.18  christos 	print "\n";
   2617  1.18  christos 	print "*** $target\n";
   2618  1.18  christos         foreach (@sequence) {
   2619  1.18  christos             if (ref($target{$_}) eq "ARRAY") {
   2620  1.18  christos                 printf "\$%-12s = %s\n", $_, join(" ", @{$target{$_}});
   2621  1.18  christos             } else {
   2622  1.18  christos                 printf "\$%-12s = %s\n", $_, $target{$_};
   2623  1.18  christos             }
   2624  1.18  christos         }
   2625  1.18  christos     } elsif ($type eq "HASH") {
   2626  1.18  christos 	my $largest =
   2627  1.18  christos 	    length((sort { length($a) <=> length($b) } @sequence)[-1]);
   2628  1.18  christos 	print "    '$target' => {\n";
   2629  1.18  christos 	foreach (@sequence) {
   2630  1.18  christos 	    if ($target{$_}) {
   2631  1.18  christos                 if (ref($target{$_}) eq "ARRAY") {
   2632  1.18  christos                     print "      '",$_,"'"," " x ($largest - length($_))," => [ ",join(", ", map { "'$_'" } @{$target{$_}})," ],\n";
   2633  1.18  christos                 } else {
   2634  1.18  christos                     print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
   2635  1.18  christos                 }
   2636  1.18  christos 	    }
   2637   1.1  christos 	}
   2638  1.18  christos 	print "    },\n";
   2639  1.18  christos     }
   2640  1.18  christos }
   2641  1.18  christos 
   2642  1.18  christos # Utility routines ###################################################
   2643  1.18  christos 
   2644  1.18  christos # On VMS, if the given file is a logical name, File::Spec::Functions
   2645  1.18  christos # will consider it an absolute path.  There are cases when we want a
   2646  1.18  christos # purely syntactic check without checking the environment.
   2647  1.18  christos sub isabsolute {
   2648  1.18  christos     my $file = shift;
   2649  1.18  christos 
   2650  1.18  christos     # On non-platforms, we just use file_name_is_absolute().
   2651  1.18  christos     return file_name_is_absolute($file) unless $^O eq "VMS";
   2652  1.18  christos 
   2653  1.18  christos     # If the file spec includes a device or a directory spec,
   2654  1.18  christos     # file_name_is_absolute() is perfectly safe.
   2655  1.18  christos     return file_name_is_absolute($file) if $file =~ m|[:\[]|;
   2656  1.18  christos 
   2657  1.18  christos     # Here, we know the given file spec isn't absolute
   2658  1.18  christos     return 0;
   2659  1.18  christos }
   2660  1.18  christos 
   2661  1.18  christos # Makes a directory absolute and cleans out /../ in paths like foo/../bar
   2662  1.18  christos # On some platforms, this uses rel2abs(), while on others, realpath() is used.
   2663  1.18  christos # realpath() requires that at least all path components except the last is an
   2664  1.18  christos # existing directory.  On VMS, the last component of the directory spec must
   2665  1.18  christos # exist.
   2666  1.18  christos sub absolutedir {
   2667  1.18  christos     my $dir = shift;
   2668  1.18  christos 
   2669  1.18  christos     # realpath() is quite buggy on VMS.  It uses LIB$FID_TO_NAME, which
   2670  1.18  christos     # will return the volume name for the device, no matter what.  Also,
   2671  1.18  christos     # it will return an incorrect directory spec if the argument is a
   2672  1.18  christos     # directory that doesn't exist.
   2673  1.18  christos     if ($^O eq "VMS") {
   2674  1.18  christos         return rel2abs($dir);
   2675  1.18  christos     }
   2676  1.18  christos 
   2677  1.18  christos     # We use realpath() on Unix, since no other will properly clean out
   2678  1.18  christos     # a directory spec.
   2679  1.18  christos     use Cwd qw/realpath/;
   2680  1.18  christos 
   2681  1.18  christos     return realpath($dir);
   2682  1.18  christos }
   2683   1.1  christos 
   2684  1.18  christos sub quotify {
   2685  1.18  christos     my %processors = (
   2686  1.18  christos 	perl    => sub { my $x = shift;
   2687  1.18  christos 			 $x =~ s/([\\\$\@"])/\\$1/g;
   2688  1.18  christos 			 return '"'.$x.'"'; },
   2689  1.18  christos 	maybeshell => sub { my $x = shift;
   2690  1.18  christos 			    (my $y = $x) =~ s/([\\\"])/\\$1/g;
   2691  1.18  christos 			    if ($x ne $y || $x =~ m|\s|) {
   2692  1.18  christos 				return '"'.$y.'"';
   2693  1.18  christos 			    } else {
   2694  1.18  christos 				return $x;
   2695  1.18  christos 			    }
   2696  1.18  christos 			},
   2697  1.18  christos 	);
   2698  1.18  christos     my $for = shift;
   2699  1.18  christos     my $processor =
   2700  1.18  christos 	defined($processors{$for}) ? $processors{$for} : sub { shift; };
   2701   1.1  christos 
   2702  1.18  christos     return map { $processor->($_); } @_;
   2703  1.18  christos }
   2704   1.1  christos 
   2705  1.18  christos # collect_from_file($filename, $line_concat_cond_re, $line_concat)
   2706  1.18  christos # $filename is a file name to read from
   2707  1.18  christos # $line_concat_cond_re is a regexp detecting a line continuation ending
   2708  1.18  christos # $line_concat is a CODEref that takes care of concatenating two lines
   2709  1.18  christos sub collect_from_file {
   2710  1.18  christos     my $filename = shift;
   2711  1.18  christos     my $line_concat_cond_re = shift;
   2712  1.18  christos     my $line_concat = shift;
   2713  1.18  christos 
   2714  1.18  christos     open my $fh, $filename || die "unable to read $filename: $!\n";
   2715  1.18  christos     return sub {
   2716  1.18  christos         my $saved_line = "";
   2717  1.18  christos         $_ = "";
   2718  1.18  christos         while (<$fh>) {
   2719  1.18  christos             s|\R$||;
   2720  1.18  christos             if (defined $line_concat) {
   2721  1.18  christos                 $_ = $line_concat->($saved_line, $_);
   2722  1.18  christos                 $saved_line = "";
   2723  1.18  christos             }
   2724  1.18  christos             if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
   2725  1.18  christos                 $saved_line = $_;
   2726  1.18  christos                 next;
   2727  1.18  christos             }
   2728  1.18  christos             return $_;
   2729  1.18  christos         }
   2730  1.18  christos         die "$filename ending with continuation line\n" if $_;
   2731  1.18  christos         close $fh;
   2732  1.18  christos         return undef;
   2733  1.18  christos     }
   2734  1.18  christos }
   2735  1.18  christos 
   2736  1.18  christos # collect_from_array($array, $line_concat_cond_re, $line_concat)
   2737  1.18  christos # $array is an ARRAYref of lines
   2738  1.18  christos # $line_concat_cond_re is a regexp detecting a line continuation ending
   2739  1.18  christos # $line_concat is a CODEref that takes care of concatenating two lines
   2740  1.18  christos sub collect_from_array {
   2741  1.18  christos     my $array = shift;
   2742  1.18  christos     my $line_concat_cond_re = shift;
   2743  1.18  christos     my $line_concat = shift;
   2744  1.18  christos     my @array = (@$array);
   2745  1.18  christos 
   2746  1.18  christos     return sub {
   2747  1.18  christos         my $saved_line = "";
   2748  1.18  christos         $_ = "";
   2749  1.18  christos         while (defined($_ = shift @array)) {
   2750  1.18  christos             s|\R$||;
   2751  1.18  christos             if (defined $line_concat) {
   2752  1.18  christos                 $_ = $line_concat->($saved_line, $_);
   2753  1.18  christos                 $saved_line = "";
   2754  1.18  christos             }
   2755  1.18  christos             if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
   2756  1.18  christos                 $saved_line = $_;
   2757  1.18  christos                 next;
   2758  1.18  christos             }
   2759  1.18  christos             return $_;
   2760  1.18  christos         }
   2761  1.18  christos         die "input text ending with continuation line\n" if $_;
   2762  1.18  christos         return undef;
   2763  1.18  christos     }
   2764  1.18  christos }
   2765  1.18  christos 
   2766  1.18  christos # collect_information($lineiterator, $line_continue, $regexp => $CODEref, ...)
   2767  1.18  christos # $lineiterator is a CODEref that delivers one line at a time.
   2768  1.18  christos # All following arguments are regex/CODEref pairs, where the regexp detects a
   2769  1.18  christos # line and the CODEref does something with the result of the regexp.
   2770  1.18  christos sub collect_information {
   2771  1.18  christos     my $lineiterator = shift;
   2772  1.18  christos     my %collectors = @_;
   2773  1.18  christos 
   2774  1.18  christos     while(defined($_ = $lineiterator->())) {
   2775  1.18  christos         s|\R$||;
   2776  1.18  christos         my $found = 0;
   2777  1.18  christos         if ($collectors{"BEFORE"}) {
   2778  1.18  christos             $collectors{"BEFORE"}->($_);
   2779  1.18  christos         }
   2780  1.18  christos         foreach my $re (keys %collectors) {
   2781  1.18  christos             if ($re !~ /^OTHERWISE|BEFORE|AFTER$/ && /$re/) {
   2782  1.18  christos                 $collectors{$re}->($lineiterator);
   2783  1.18  christos                 $found = 1;
   2784  1.18  christos             };
   2785  1.18  christos         }
   2786  1.18  christos         if ($collectors{"OTHERWISE"}) {
   2787  1.18  christos             $collectors{"OTHERWISE"}->($lineiterator, $_)
   2788  1.18  christos                 unless $found || !defined $collectors{"OTHERWISE"};
   2789  1.18  christos         }
   2790  1.18  christos         if ($collectors{"AFTER"}) {
   2791  1.18  christos             $collectors{"AFTER"}->($_);
   2792  1.18  christos         }
   2793  1.18  christos     }
   2794  1.18  christos }
   2795   1.1  christos 
   2796  1.18  christos # tokenize($line)
   2797  1.18  christos # $line is a line of text to split up into tokens
   2798  1.18  christos # returns a list of tokens
   2799  1.18  christos #
   2800  1.18  christos # Tokens are divided by spaces.  If the tokens include spaces, they
   2801  1.18  christos # have to be quoted with single or double quotes.  Double quotes
   2802  1.18  christos # inside a double quoted token must be escaped.  Escaping is done
   2803  1.18  christos # with backslash.
   2804  1.18  christos # Basically, the same quoting rules apply for " and ' as in any
   2805  1.18  christos # Unix shell.
   2806  1.18  christos sub tokenize {
   2807  1.18  christos     my $line = my $debug_line = shift;
   2808  1.18  christos     my @result = ();
   2809  1.18  christos 
   2810  1.18  christos     while ($line =~ s|^\s+||, $line ne "") {
   2811  1.18  christos         my $token = "";
   2812  1.18  christos         while ($line ne "" && $line !~ m|^\s|) {
   2813  1.18  christos             if ($line =~ m/^"((?:[^"\\]+|\\.)*)"/) {
   2814  1.18  christos                 $token .= $1;
   2815  1.18  christos                 $line = $';
   2816  1.18  christos             } elsif ($line =~ m/^'([^']*)'/) {
   2817  1.18  christos                 $token .= $1;
   2818  1.18  christos                 $line = $';
   2819  1.18  christos             } elsif ($line =~ m/^(\S+)/) {
   2820  1.18  christos                 $token .= $1;
   2821  1.18  christos                 $line = $';
   2822  1.18  christos             }
   2823  1.18  christos         }
   2824  1.18  christos         push @result, $token;
   2825  1.18  christos     }
   2826  1.17       spz 
   2827  1.18  christos     if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
   2828  1.18  christos 	print STDERR "DEBUG[tokenize]: Parsed '$debug_line' into:\n";
   2829  1.18  christos 	print STDERR "DEBUG[tokenize]: ('", join("', '", @result), "')\n";
   2830  1.18  christos     }
   2831  1.18  christos     return @result;
   2832  1.18  christos }
   2833