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