Home | History | Annotate | Line # | Download | only in dist
      1  1.1  christos #! /usr/bin/env perl
      2  1.1  christos # -*- mode: perl; -*-
      3  1.1  christos # Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
      4  1.1  christos #
      5  1.1  christos # Licensed under the Apache License 2.0 (the "License").  You may not use
      6  1.1  christos # this file except in compliance with the License.  You can obtain a copy
      7  1.1  christos # in the file LICENSE in the source distribution or at
      8  1.1  christos # https://www.openssl.org/source/license.html
      9  1.1  christos 
     10  1.1  christos ##  Configure -- OpenSSL source tree configuration script
     11  1.1  christos 
     12  1.1  christos use 5.10.0;
     13  1.1  christos use strict;
     14  1.1  christos use Config;
     15  1.1  christos use FindBin;
     16  1.1  christos use lib "$FindBin::Bin/util/perl";
     17  1.1  christos use File::Basename;
     18  1.1  christos use File::Spec::Functions qw/:DEFAULT abs2rel rel2abs splitdir/;
     19  1.1  christos use File::Path qw/mkpath/;
     20  1.1  christos use OpenSSL::fallback "$FindBin::Bin/external/perl/MODULES.txt";
     21  1.1  christos use OpenSSL::Glob;
     22  1.1  christos use OpenSSL::Template;
     23  1.1  christos use OpenSSL::config;
     24  1.1  christos 
     25  1.1  christos # see INSTALL.md for instructions.
     26  1.1  christos 
     27  1.1  christos my $orig_death_handler = $SIG{__DIE__};
     28  1.1  christos $SIG{__DIE__} = \&death_handler;
     29  1.1  christos 
     30  1.1  christos my $usage="Usage: Configure [no-<feature> ...] [enable-<feature> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]thread-pool] [[no-]default-thread-pool] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
     31  1.1  christos 
     32  1.1  christos my $banner = <<"EOF";
     33  1.1  christos 
     34  1.1  christos **********************************************************************
     35  1.1  christos ***                                                                ***
     36  1.1  christos ***   OpenSSL has been successfully configured                     ***
     37  1.1  christos ***                                                                ***
     38  1.1  christos ***   If you encounter a problem while building, please open an    ***
     39  1.1  christos ***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
     40  1.1  christos ***   and include the output from the following command:           ***
     41  1.1  christos ***                                                                ***
     42  1.1  christos ***       perl configdata.pm --dump                                ***
     43  1.1  christos ***                                                                ***
     44  1.1  christos ***   (If you are new to OpenSSL, you might want to consult the    ***
     45  1.1  christos ***   'Troubleshooting' section in the INSTALL.md file first)      ***
     46  1.1  christos ***                                                                ***
     47  1.1  christos **********************************************************************
     48  1.1  christos EOF
     49  1.1  christos 
     50  1.1  christos # Options:
     51  1.1  christos #
     52  1.1  christos # --config      add the given configuration file, which will be read after
     53  1.1  christos #               any "Configurations*" files that are found in the same
     54  1.1  christos #               directory as this script.
     55  1.1  christos # --prefix      prefix for the OpenSSL installation, which includes the
     56  1.1  christos #               directories bin, lib, include, share/man, share/doc/openssl
     57  1.1  christos #               This becomes the value of INSTALLTOP in Makefile
     58  1.1  christos #               (Default: /usr/local)
     59  1.1  christos # --openssldir  OpenSSL data area, such as openssl.cnf, certificates and keys.
     60  1.1  christos #               If it's a relative directory, it will be added on the directory
     61  1.1  christos #               given with --prefix.
     62  1.1  christos #               This becomes the value of OPENSSLDIR in Makefile and in C.
     63  1.1  christos #               (Default: PREFIX/ssl)
     64  1.1  christos # --banner=".." Output specified text instead of default completion banner
     65  1.1  christos #
     66  1.1  christos # -w            Don't wait after showing a Configure warning
     67  1.1  christos #
     68  1.1  christos # --cross-compile-prefix Add specified prefix to binutils components.
     69  1.1  christos #
     70  1.1  christos # --api         One of 0.9.8, 1.0.0, 1.0.1, 1.0.2, 1.1.0, 1.1.1, or 3.0
     71  1.1  christos #               Define the public APIs as they were for that version
     72  1.1  christos #               including patch releases.  If 'no-deprecated' is also
     73  1.1  christos #               given, do not compile support for interfaces deprecated
     74  1.1  christos #               up to and including the specified OpenSSL version.
     75  1.1  christos #
     76  1.1  christos # no-hw-xxx     do not compile support for specific crypto hardware.
     77  1.1  christos #               Generic OpenSSL-style methods relating to this support
     78  1.1  christos #               are always compiled but return NULL if the hardware
     79  1.1  christos #               support isn't compiled.
     80  1.1  christos #
     81  1.1  christos # enable-demos  Enable the building of the example code in the demos directory
     82  1.1  christos # enable-h3demo Enable the http3 demo, which currently only links to the
     83  1.1  christos #               external nghttp3 library on unix platforms
     84  1.1  christos #
     85  1.1  christos # enable-hqinterop
     86  1.1  christos #               Enable the building of the hq-interop code for construction
     87  1.1  christos #               of the interop container
     88  1.1  christos #
     89  1.1  christos # no-hw         do not compile support for any crypto hardware.
     90  1.1  christos # [no-]threads  [don't] try to create a library that is suitable for
     91  1.1  christos #               multithreaded applications (default is "threads" if we
     92  1.1  christos #               know how to do it)
     93  1.1  christos # [no-]thread-pool
     94  1.1  christos #               [don't] allow thread pool functionality
     95  1.1  christos # [no-]default-thread-pool
     96  1.1  christos #               [don't] allow default thread pool functionality
     97  1.1  christos # [no-]shared   [don't] try to create shared libraries when supported.
     98  1.1  christos # [no-]pic      [don't] try to build position independent code when supported.
     99  1.1  christos #               If disabled, it also disables shared and dynamic-engine.
    100  1.1  christos # no-asm        do not use assembler
    101  1.1  christos # no-egd        do not compile support for the entropy-gathering daemon APIs
    102  1.1  christos # [no-]zlib     [don't] compile support for zlib compression.
    103  1.1  christos # zlib-dynamic  Like "zlib", but the zlib library is expected to be a shared
    104  1.1  christos #               library and will be loaded at run-time by the OpenSSL library.
    105  1.1  christos # sctp          include SCTP support
    106  1.1  christos # no-quic       disable QUIC support
    107  1.1  christos # no-uplink     Don't build support for UPLINK interface.
    108  1.1  christos # enable-weak-ssl-ciphers
    109  1.1  christos #               Enable weak ciphers that are disabled by default.
    110  1.1  christos # 386           generate 80386 code in assembly modules
    111  1.1  christos # no-sse2       disables IA-32 SSE2 code in assembly modules, the above
    112  1.1  christos #               mentioned '386' option implies this one
    113  1.1  christos # no-<cipher>   build without specified algorithm (dsa, idea, rc5, ...)
    114  1.1  christos # -<xxx> +<xxx> All options which are unknown to the 'Configure' script are
    115  1.1  christos # /<xxx>        passed through to the compiler. Unix-style options beginning
    116  1.1  christos #               with a '-' or '+' are recognized, as well as Windows-style
    117  1.1  christos #               options beginning with a '/'. If the option contains arguments
    118  1.1  christos #               separated by spaces, then the URL-style notation %20 can be
    119  1.1  christos #               used for the space character in order to avoid having to quote
    120  1.1  christos #               the option. For example, -opt%20arg gets expanded to -opt arg.
    121  1.1  christos #               In fact, any ASCII character can be encoded as %xx using its
    122  1.1  christos #               hexadecimal encoding.
    123  1.1  christos # -static       while -static is also a pass-through compiler option (and
    124  1.1  christos #               as such is limited to environments where it's actually
    125  1.1  christos #               meaningful), it triggers a number configuration options,
    126  1.1  christos #               namely no-pic, no-shared and no-threads. It is
    127  1.1  christos #               argued that the only reason to produce statically linked
    128  1.1  christos #               binaries (and in context it means executables linked with
    129  1.1  christos #               -static flag, and not just executables linked with static
    130  1.1  christos #               libcrypto.a) is to eliminate dependency on specific run-time,
    131  1.1  christos #               a.k.a. libc version. The mentioned config options are meant
    132  1.1  christos #               to achieve just that. Unfortunately on Linux it's impossible
    133  1.1  christos #               to eliminate the dependency completely for openssl executable
    134  1.1  christos #               because of getaddrinfo and gethostbyname calls, which can
    135  1.1  christos #               invoke dynamically loadable library facility anyway to meet
    136  1.1  christos #               the lookup requests. For this reason on Linux statically
    137  1.1  christos #               linked openssl executable has rather debugging value than
    138  1.1  christos #               production quality.
    139  1.1  christos #
    140  1.1  christos # BN_LLONG      use the type 'long long' in crypto/bn/bn.h
    141  1.1  christos # RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
    142  1.1  christos # Following are set automatically by this script
    143  1.1  christos #
    144  1.1  christos # MD5_ASM       use some extra md5 assembler,
    145  1.1  christos # SHA1_ASM      use some extra sha1 assembler, must define L_ENDIAN for x86
    146  1.1  christos # RMD160_ASM    use some extra ripemd160 assembler,
    147  1.1  christos # SHA256_ASM    sha256_block is implemented in assembler
    148  1.1  christos # SHA512_ASM    sha512_block is implemented in assembler
    149  1.1  christos # AES_ASM       AES_[en|de]crypt is implemented in assembler
    150  1.1  christos 
    151  1.1  christos # Minimum warning options... any contributions to OpenSSL should at least
    152  1.1  christos # get past these.  Note that we only use these with C compilers, not with
    153  1.1  christos # C++ compilers.
    154  1.1  christos 
    155  1.1  christos # -DPEDANTIC complements -pedantic and is meant to mask code that
    156  1.1  christos # is not strictly standard-compliant and/or implementation-specific,
    157  1.1  christos # e.g. inline assembly, disregards to alignment requirements, such
    158  1.1  christos # that -pedantic would complain about. Incidentally -DPEDANTIC has
    159  1.1  christos # to be used even in sanitized builds, because sanitizer too is
    160  1.1  christos # supposed to and does take notice of non-standard behaviour. Then
    161  1.1  christos # -pedantic with pre-C9x compiler would also complain about 'long
    162  1.1  christos # long' not being supported. As 64-bit algorithms are common now,
    163  1.1  christos # it grew impossible to resolve this without sizeable additional
    164  1.1  christos # code, so we just tell compiler to be pedantic about everything
    165  1.1  christos # but 'long long' type.
    166  1.1  christos 
    167  1.1  christos my @gcc_devteam_warn = qw(
    168  1.1  christos     -DPEDANTIC -pedantic -Wno-long-long -DUNUSEDRESULT_DEBUG
    169  1.1  christos     -Wall
    170  1.1  christos     -Wmissing-declarations
    171  1.1  christos     -Wextra
    172  1.1  christos     -Wno-unused-parameter
    173  1.1  christos     -Wno-missing-field-initializers
    174  1.1  christos     -Wno-unterminated-string-initialization
    175  1.1  christos     -Wswitch
    176  1.1  christos     -Wsign-compare
    177  1.1  christos     -Wshadow
    178  1.1  christos     -Wformat
    179  1.1  christos     -Wno-type-limits
    180  1.1  christos     -Wundef
    181  1.1  christos     -Werror
    182  1.1  christos     -Wmissing-prototypes
    183  1.1  christos     -Wstrict-prototypes
    184  1.1  christos );
    185  1.1  christos 
    186  1.1  christos # These are used in addition to $gcc_devteam_warn when the compiler is clang.
    187  1.1  christos # TODO(openssl-team): fix problems and investigate if (at least) the
    188  1.1  christos # following warnings can also be enabled:
    189  1.1  christos #       -Wcast-align
    190  1.1  christos #       -Wunreachable-code -- no, too ugly/compiler-specific
    191  1.1  christos #       -Wlanguage-extension-token -- no, we use asm()
    192  1.1  christos #       -Wunused-macros -- no, too tricky for BN and _XOPEN_SOURCE etc
    193  1.1  christos #       -Wextended-offsetof -- no, needed in CMS ASN1 code
    194  1.1  christos my @clang_devteam_warn = qw(
    195  1.1  christos     -Wno-unknown-warning-option
    196  1.1  christos     -Wno-parentheses-equality
    197  1.1  christos     -Wno-language-extension-token
    198  1.1  christos     -Wno-extended-offsetof
    199  1.1  christos     -Wno-missing-braces
    200  1.1  christos     -Wno-tautological-constant-out-of-range-compare
    201  1.1  christos     -Wconditional-uninitialized
    202  1.1  christos     -Wincompatible-pointer-types-discards-qualifiers
    203  1.1  christos     -Wmissing-variable-declarations
    204  1.1  christos );
    205  1.1  christos 
    206  1.1  christos my @cl_devteam_warn = qw(
    207  1.1  christos     /WX
    208  1.1  christos );
    209  1.1  christos 
    210  1.1  christos my $strict_warnings = 0;
    211  1.1  christos 
    212  1.1  christos # As for $BSDthreads. Idea is to maintain "collective" set of flags,
    213  1.1  christos # which would cover all BSD flavors. -pthread applies to them all,
    214  1.1  christos # but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
    215  1.1  christos # -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
    216  1.1  christos # which has to be accompanied by explicit -D_THREAD_SAFE and
    217  1.1  christos # sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
    218  1.1  christos # seems to be sufficient?
    219  1.1  christos our $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
    220  1.1  christos 
    221  1.1  christos #
    222  1.1  christos # API compatibility name to version number mapping.
    223  1.1  christos #
    224  1.1  christos my $apitable = {
    225  1.1  christos     # This table expresses when API additions or changes can occur.
    226  1.1  christos     # The numbering used changes from 3.0 and on because we updated
    227  1.1  christos     # (solidified) our version numbering scheme at that point.
    228  1.1  christos 
    229  1.1  christos     # From 3.0 and on, we internalise the given version number in decimal
    230  1.1  christos     # as MAJOR * 10000 + MINOR * 100 + 0
    231  1.1  christos     "3.0.0" => 30000,
    232  1.1  christos     "3.0"   => 30000,
    233  1.1  christos 
    234  1.1  christos     # Note that before 3.0, we didn't have the same version number scheme.
    235  1.1  christos     # Still, the numbering we use here covers what we need.
    236  1.1  christos     "1.1.1" => 10101,
    237  1.1  christos     "1.1.0" => 10100,
    238  1.1  christos     "1.0.2" => 10002,
    239  1.1  christos     "1.0.1" => 10001,
    240  1.1  christos     "1.0.0" => 10000,
    241  1.1  christos     "0.9.8" =>   908,
    242  1.1  christos };
    243  1.1  christos 
    244  1.1  christos # For OpenSSL::config::get_platform
    245  1.1  christos my %guess_opts = ();
    246  1.1  christos 
    247  1.1  christos my $dryrun = 0;
    248  1.1  christos 
    249  1.1  christos our %table = ();
    250  1.1  christos our %config = ();
    251  1.1  christos our %withargs = ();
    252  1.1  christos our $now_printing;      # set to current entry's name in print_table_entry
    253  1.1  christos                         # (todo: right thing would be to encapsulate name
    254  1.1  christos                         # into %target [class] and make print_table_entry
    255  1.1  christos                         # a method)
    256  1.1  christos 
    257  1.1  christos # Forward declarations ###############################################
    258  1.1  christos 
    259  1.1  christos # read_config(filename)
    260  1.1  christos #
    261  1.1  christos # Reads a configuration file and populates %table with the contents
    262  1.1  christos # (which the configuration file places in %targets).
    263  1.1  christos sub read_config;
    264  1.1  christos 
    265  1.1  christos # resolve_config(target)
    266  1.1  christos #
    267  1.1  christos # Resolves all the late evaluations, inheritances and so on for the
    268  1.1  christos # chosen target and any target it inherits from.
    269  1.1  christos sub resolve_config;
    270  1.1  christos 
    271  1.1  christos 
    272  1.1  christos # Information collection #############################################
    273  1.1  christos 
    274  1.1  christos # Unified build supports separate build dir
    275  1.1  christos my $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
    276  1.1  christos my $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
    277  1.1  christos 
    278  1.1  christos # File::Spec::Unix doesn't detect case insensitivity, so we make sure to
    279  1.1  christos # check if the source and build directory are really the same, and make
    280  1.1  christos # them so.  This avoids all kinds of confusion later on.
    281  1.1  christos # We must check @File::Spec::ISA rather than using File::Spec->isa() to
    282  1.1  christos # know if File::Spec ended up loading File::Spec::Unix.
    283  1.1  christos $srcdir = $blddir
    284  1.1  christos     if (grep(/::Unix$/, @File::Spec::ISA)
    285  1.1  christos         && samedir($srcdir, $blddir));
    286  1.1  christos 
    287  1.1  christos my $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
    288  1.1  christos 
    289  1.1  christos my $local_config_envname = 'OPENSSL_LOCAL_CONFIG_DIR';
    290  1.1  christos 
    291  1.1  christos $config{sourcedir} = abs2rel($srcdir, $blddir);
    292  1.1  christos $config{builddir} = abs2rel($blddir, $blddir);
    293  1.1  christos # echo -n 'holy hand grenade of antioch' | openssl sha256
    294  1.1  christos $config{FIPSKEY} =
    295  1.1  christos     'f4556650ac31d35461610bac4ed81b1a181b2d8a43ea2854cbae22ca74560813';
    296  1.1  christos 
    297  1.1  christos # Collect reconfiguration information if needed
    298  1.1  christos my @argvcopy=@ARGV;
    299  1.1  christos 
    300  1.1  christos if (grep /^reconf(igure)?$/, @argvcopy) {
    301  1.1  christos     die "reconfiguring with other arguments present isn't supported"
    302  1.1  christos         if scalar @argvcopy > 1;
    303  1.1  christos     if (-f "./configdata.pm") {
    304  1.1  christos         my $file = "./configdata.pm";
    305  1.1  christos         unless (my $return = do $file) {
    306  1.1  christos             die "couldn't parse $file: $@" if $@;
    307  1.1  christos             die "couldn't do $file: $!"    unless defined $return;
    308  1.1  christos             die "couldn't run $file"       unless $return;
    309  1.1  christos         }
    310  1.1  christos 
    311  1.1  christos         @argvcopy = defined($configdata::config{perlargv}) ?
    312  1.1  christos             @{$configdata::config{perlargv}} : ();
    313  1.1  christos         die "Incorrect data to reconfigure, please do a normal configuration\n"
    314  1.1  christos             if (grep(/^reconf/,@argvcopy));
    315  1.1  christos         $config{perlenv} = $configdata::config{perlenv} // {};
    316  1.1  christos     } else {
    317  1.1  christos         die "Insufficient data to reconfigure, please do a normal configuration\n";
    318  1.1  christos     }
    319  1.1  christos }
    320  1.1  christos 
    321  1.1  christos $config{perlargv} = [ @argvcopy ];
    322  1.1  christos 
    323  1.1  christos # Historical: if known directories in crypto/ have been removed, it means
    324  1.1  christos # that those sub-systems are disabled.
    325  1.1  christos # (the other option would be to removed them from the SUBDIRS statement in
    326  1.1  christos # crypto/build.info)
    327  1.1  christos # We reverse the input list for cosmetic purely reasons, to compensate that
    328  1.1  christos # 'unshift' adds at the front of the list (i.e. in reverse input order).
    329  1.1  christos foreach ( reverse sort( 'aes', 'aria', 'bf', 'camellia', 'cast', 'des', 'dh',
    330  1.1  christos                         'dsa', 'ec', 'hmac', 'idea', 'md2', 'md5', 'mdc2',
    331  1.1  christos                         'rc2', 'rc4', 'rc5', 'ripemd', 'seed', 'sha',
    332  1.1  christos                         'sm2', 'sm3', 'sm4') ) {
    333  1.1  christos     unshift @argvcopy, "no-$_" if ! -d catdir($srcdir, 'crypto', $_);
    334  1.1  christos }
    335  1.1  christos 
    336  1.1  christos # Collect version numbers
    337  1.1  christos my %version = ();
    338  1.1  christos 
    339  1.1  christos collect_information(
    340  1.1  christos     collect_from_file(catfile($srcdir,'VERSION.dat')),
    341  1.1  christos     qr/\s*(\w+)\s*=\s*(.*?)\s*$/ =>
    342  1.1  christos         sub {
    343  1.1  christos             # Only define it if there is a value at all
    344  1.1  christos             if ($2 ne '') {
    345  1.1  christos                 my $k = $1;
    346  1.1  christos                 my $v = $2;
    347  1.1  christos                 # Some values are quoted.  Trim the quotes
    348  1.1  christos                 $v = $1 if $v =~ /^"(.*)"$/;
    349  1.1  christos                 $version{uc $k} = $v;
    350  1.1  christos             }
    351  1.1  christos         },
    352  1.1  christos     "OTHERWISE" =>
    353  1.1  christos         sub { die "Something wrong with this line:\n$_\nin $srcdir/VERSION.dat" },
    354  1.1  christos     );
    355  1.1  christos 
    356  1.1  christos $config{major} = $version{MAJOR} // 'unknown';
    357  1.1  christos $config{minor} = $version{MINOR} // 'unknown';
    358  1.1  christos $config{patch} = $version{PATCH} // 'unknown';
    359  1.1  christos $config{prerelease} =
    360  1.1  christos     defined $version{PRE_RELEASE_TAG} ? "-$version{PRE_RELEASE_TAG}" : '';
    361  1.1  christos $config{build_metadata} =
    362  1.1  christos     defined $version{BUILD_METADATA} ? "+$version{BUILD_METADATA}" : '';
    363  1.1  christos $config{shlib_version} = $version{SHLIB_VERSION} // 'unknown';
    364  1.1  christos $config{release_date} = $version{RELEASE_DATE} // 'xx XXX xxxx';
    365  1.1  christos 
    366  1.1  christos $config{version} = "$config{major}.$config{minor}.$config{patch}";
    367  1.1  christos $config{full_version} = "$config{version}$config{prerelease}$config{build_metadata}";
    368  1.1  christos 
    369  1.1  christos die "erroneous version information in VERSION.dat: ",
    370  1.1  christos     "$config{version}, $config{shlib_version}\n"
    371  1.1  christos     unless (defined $version{MAJOR}
    372  1.1  christos             && defined $version{MINOR}
    373  1.1  christos             && defined $version{PATCH}
    374  1.1  christos             && defined $version{SHLIB_VERSION});
    375  1.1  christos 
    376  1.1  christos # Collect target configurations
    377  1.1  christos 
    378  1.1  christos my $pattern = catfile(dirname($0), "Configurations", "*.conf");
    379  1.1  christos foreach (sort glob($pattern)) {
    380  1.1  christos     &read_config($_);
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos if (defined env($local_config_envname)) {
    384  1.1  christos     if ($^O eq 'VMS') {
    385  1.1  christos         # VMS environment variables are logical names,
    386  1.1  christos         # which can be used as is
    387  1.1  christos         $pattern = $local_config_envname . ':' . '*.conf';
    388  1.1  christos     } else {
    389  1.1  christos         $pattern = catfile(env($local_config_envname), '*.conf');
    390  1.1  christos     }
    391  1.1  christos 
    392  1.1  christos     foreach (sort glob($pattern)) {
    393  1.1  christos         &read_config($_);
    394  1.1  christos     }
    395  1.1  christos }
    396  1.1  christos 
    397  1.1  christos # Fail if no configuration is apparent
    398  1.1  christos if (!%table) {
    399  1.1  christos     print "Failed to find any os/compiler configurations. Please make sure the Configurations directory is included.\n";
    400  1.1  christos     &usage;
    401  1.1  christos }
    402  1.1  christos 
    403  1.1  christos # Save away perl command information
    404  1.1  christos $config{perl_cmd} = $^X;
    405  1.1  christos $config{perl_version} = $Config{version};
    406  1.1  christos $config{perl_archname} = $Config{archname};
    407  1.1  christos 
    408  1.1  christos $config{prefix}="";
    409  1.1  christos $config{openssldir}="";
    410  1.1  christos $config{processor}="";
    411  1.1  christos $config{libdir}="";
    412  1.1  christos my $auto_threads=1;    # enable threads automatically? true by default
    413  1.1  christos my $default_ranlib;
    414  1.1  christos 
    415  1.1  christos # Known TLS and DTLS protocols
    416  1.1  christos my @tls = qw(ssl3 tls1 tls1_1 tls1_2 tls1_3);
    417  1.1  christos my @dtls = qw(dtls1 dtls1_2);
    418  1.1  christos 
    419  1.1  christos # Explicitly known options that are possible to disable.  They can
    420  1.1  christos # be regexps, and will be used like this: /^no-${option}$/
    421  1.1  christos # For developers: keep it sorted alphabetically
    422  1.1  christos 
    423  1.1  christos my @disablables = (
    424  1.1  christos     "acvp-tests",
    425  1.1  christos     "afalgeng",
    426  1.1  christos     "apps",
    427  1.1  christos     "argon2",
    428  1.1  christos     "aria",
    429  1.1  christos     "asan",
    430  1.1  christos     "asm",
    431  1.1  christos     "async",
    432  1.1  christos     "atexit",
    433  1.1  christos     "autoalginit",
    434  1.1  christos     "autoerrinit",
    435  1.1  christos     "autoload-config",
    436  1.1  christos     "bf",
    437  1.1  christos     "blake2",
    438  1.1  christos     "brotli",
    439  1.1  christos     "brotli-dynamic",
    440  1.1  christos     "buildtest-c++",
    441  1.1  christos     "bulk",
    442  1.1  christos     "cached-fetch",
    443  1.1  christos     "camellia",
    444  1.1  christos     "capieng",
    445  1.1  christos     "winstore",
    446  1.1  christos     "cast",
    447  1.1  christos     "chacha",
    448  1.1  christos     "cmac",
    449  1.1  christos     "cmp",
    450  1.1  christos     "cms",
    451  1.1  christos     "comp",
    452  1.1  christos     "crypto-mdebug",
    453  1.1  christos     "ct",
    454  1.1  christos     "default-thread-pool",
    455  1.1  christos     "demos",
    456  1.1  christos     "h3demo",
    457  1.1  christos     "hqinterop",
    458  1.1  christos     "deprecated",
    459  1.1  christos     "des",
    460  1.1  christos     "devcryptoeng",
    461  1.1  christos     "dgram",
    462  1.1  christos     "dh",
    463  1.1  christos     "docs",
    464  1.1  christos     "dsa",
    465  1.1  christos     "dso",
    466  1.1  christos     "dtls",
    467  1.1  christos     "dynamic-engine",
    468  1.1  christos     "ec",
    469  1.1  christos     "ec2m",
    470  1.1  christos     "ec_nistp_64_gcc_128",
    471  1.1  christos     "ecdh",
    472  1.1  christos     "ecdsa",
    473  1.1  christos     "ecx",
    474  1.1  christos     "egd",
    475  1.1  christos     "engine",
    476  1.1  christos     "err",
    477  1.1  christos     "external-tests",
    478  1.1  christos     "filenames",
    479  1.1  christos     "fips",
    480  1.1  christos     "fips-securitychecks",
    481  1.1  christos     "fips-post",
    482  1.1  christos     "fips-jitter",
    483  1.1  christos     "fuzz-afl",
    484  1.1  christos     "fuzz-libfuzzer",
    485  1.1  christos     "gost",
    486  1.1  christos     "http",
    487  1.1  christos     "idea",
    488  1.1  christos     "integrity-only-ciphers",
    489  1.1  christos     "jitter",
    490  1.1  christos     "ktls",
    491  1.1  christos     "legacy",
    492  1.1  christos     "loadereng",
    493  1.1  christos     "makedepend",
    494  1.1  christos     "md2",
    495  1.1  christos     "md4",
    496  1.1  christos     "mdc2",
    497  1.1  christos     "ml-dsa",
    498  1.1  christos     "ml-kem",
    499  1.1  christos     "module",
    500  1.1  christos     "msan",
    501  1.1  christos     "multiblock",
    502  1.1  christos     "nextprotoneg",
    503  1.1  christos     "ocb",
    504  1.1  christos     "ocsp",
    505  1.1  christos     "padlockeng",
    506  1.1  christos     "pic",
    507  1.1  christos     "pie",
    508  1.1  christos     "pinshared",
    509  1.1  christos     "poly1305",
    510  1.1  christos     "posix-io",
    511  1.1  christos     "psk",
    512  1.1  christos     "quic",
    513  1.1  christos     "unstable-qlog",
    514  1.1  christos     "rc2",
    515  1.1  christos     "rc4",
    516  1.1  christos     "rc5",
    517  1.1  christos     "rdrand",
    518  1.1  christos     "rfc3779",
    519  1.1  christos     "rmd160",
    520  1.1  christos     "scrypt",
    521  1.1  christos     "sctp",
    522  1.1  christos     "secure-memory",
    523  1.1  christos     "seed",
    524  1.1  christos     "shared",
    525  1.1  christos     "siphash",
    526  1.1  christos     "siv",
    527  1.1  christos     "slh-dsa",
    528  1.1  christos     "sm2",
    529  1.1  christos     "sm2-precomp",
    530  1.1  christos     "sm3",
    531  1.1  christos     "sm4",
    532  1.1  christos     "sock",
    533  1.1  christos     "srp",
    534  1.1  christos     "srtp",
    535  1.1  christos     "sse2",
    536  1.1  christos     "ssl",
    537  1.1  christos     "ssl-trace",
    538  1.1  christos     "static-engine",
    539  1.1  christos     "stdio",
    540  1.1  christos     "sslkeylog",
    541  1.1  christos     "tests",
    542  1.1  christos     "tfo",
    543  1.1  christos     "thread-pool",
    544  1.1  christos     "threads",
    545  1.1  christos     "tls",
    546  1.1  christos     "tls-deprecated-ec",
    547  1.1  christos     "trace",
    548  1.1  christos     "ts",
    549  1.1  christos     "ubsan",
    550  1.1  christos     "ui-console",
    551  1.1  christos     "unit-test",
    552  1.1  christos     "uplink",
    553  1.1  christos     "weak-ssl-ciphers",
    554  1.1  christos     "whirlpool",
    555  1.1  christos     "zlib",
    556  1.1  christos     "zlib-dynamic",
    557  1.1  christos     "zstd",
    558  1.1  christos     "zstd-dynamic",
    559  1.1  christos     );
    560  1.1  christos foreach my $proto ((@tls, @dtls))
    561  1.1  christos         {
    562  1.1  christos         push(@disablables, $proto);
    563  1.1  christos         push(@disablables, "$proto-method") unless $proto eq "tls1_3";
    564  1.1  christos         }
    565  1.1  christos 
    566  1.1  christos # Internal disablables, for aliasing purposes.  They serve no special
    567  1.1  christos # purpose here, but allow scripts to get to know them through configdata.pm,
    568  1.1  christos # where these are merged with @disablables.
    569  1.1  christos # The actual aliasing mechanism is done via %disable_cascades
    570  1.1  christos my @disablables_int = qw(
    571  1.1  christos     crmf
    572  1.1  christos     );
    573  1.1  christos 
    574  1.1  christos my %deprecated_disablables = (
    575  1.1  christos     "ssl2" => undef,
    576  1.1  christos     "buf-freelists" => undef,
    577  1.1  christos     "crypto-mdebug-backtrace" => undef,
    578  1.1  christos     "hw" => "hw",               # causes cascade, but no macro
    579  1.1  christos     "hw-padlock" => "padlockeng",
    580  1.1  christos     "ripemd" => "rmd160",
    581  1.1  christos     "ui" => "ui-console",
    582  1.1  christos     "heartbeats" => undef,
    583  1.1  christos     );
    584  1.1  christos 
    585  1.1  christos # All of the following are disabled by default:
    586  1.1  christos 
    587  1.1  christos our %disabled = ( # "what"         => "comment"
    588  1.1  christos                   "fips"                => "default",
    589  1.1  christos                   "fips-jitter"         => "default",
    590  1.1  christos                   "asan"                => "default",
    591  1.1  christos                   "brotli"              => "default",
    592  1.1  christos                   "brotli-dynamic"      => "default",
    593  1.1  christos                   "buildtest-c++"       => "default",
    594  1.1  christos                   "crypto-mdebug"       => "default",
    595  1.1  christos                   "crypto-mdebug-backtrace" => "default",
    596  1.1  christos                   "demos"               => "default",
    597  1.1  christos                   "h3demo"              => "default",
    598  1.1  christos                   "hqinterop"           => "default",
    599  1.1  christos                   "devcryptoeng"        => "default",
    600  1.1  christos                   "ec_nistp_64_gcc_128" => "default",
    601  1.1  christos                   "egd"                 => "default",
    602  1.1  christos                   "external-tests"      => "default",
    603  1.1  christos                   "fuzz-afl"            => "default",
    604  1.1  christos                   "fuzz-libfuzzer"      => "default",
    605  1.1  christos                   "pie"                 => "default",
    606  1.1  christos                   "jitter"              => "default",
    607  1.1  christos                   "ktls"                => "default",
    608  1.1  christos                   "md2"                 => "default",
    609  1.1  christos                   "msan"                => "default",
    610  1.1  christos                   "rc5"                 => "default",
    611  1.1  christos                   "sctp"                => "default",
    612  1.1  christos                   "ssl3"                => "default",
    613  1.1  christos                   "ssl3-method"         => "default",
    614  1.1  christos                   "sslkeylog"           => "default",
    615  1.1  christos                   "tfo"                 => "default",
    616  1.1  christos                   "trace"               => "default",
    617  1.1  christos                   "ubsan"               => "default",
    618  1.1  christos                   "unit-test"           => "default",
    619  1.1  christos                   "weak-ssl-ciphers"    => "default",
    620  1.1  christos                   "zlib"                => "default",
    621  1.1  christos                   "zlib-dynamic"        => "default",
    622  1.1  christos                   "zstd"                => "default",
    623  1.1  christos                   "zstd-dynamic"        => "default",
    624  1.1  christos                 );
    625  1.1  christos 
    626  1.1  christos # Note: => pair form used for aesthetics, not to truly make a hash table
    627  1.1  christos my @disable_cascades = (
    628  1.1  christos     # "what"            => [ "cascade", ... ]
    629  1.1  christos     "bulk"              => [ "shared", "dso",
    630  1.1  christos                              "aria", "async", "atexit", "autoload-config",
    631  1.1  christos                              "blake2", "bf", "camellia", "cast", "chacha",
    632  1.1  christos                              "cmac", "cms", "cmp", "comp", "ct",
    633  1.1  christos                              "des", "dgram", "dh", "dsa",
    634  1.1  christos                              "ec", "engine",
    635  1.1  christos                              "filenames",
    636  1.1  christos                              "idea", "ktls",
    637  1.1  christos                              "md4", "ml-dsa", "ml-kem", "multiblock",
    638  1.1  christos                              "nextprotoneg", "ocsp", "ocb", "poly1305", "psk",
    639  1.1  christos                              "rc2", "rc4", "rmd160",
    640  1.1  christos                              "seed", "siphash", "siv",
    641  1.1  christos                              "sm3", "sm4", "srp",
    642  1.1  christos                              "srtp", "ssl3-method", "ssl-trace",
    643  1.1  christos                              "tfo",
    644  1.1  christos                              "ts", "ui-console", "whirlpool",
    645  1.1  christos                              "fips-securitychecks" ],
    646  1.1  christos     sub { $config{processor} eq "386" }
    647  1.1  christos                         => [ "sse2" ],
    648  1.1  christos     "ssl"               => [ "ssl3" ],
    649  1.1  christos     "ssl3-method"       => [ "ssl3" ],
    650  1.1  christos     "zlib"              => [ "zlib-dynamic" ],
    651  1.1  christos     "brotli"            => [ "brotli-dynamic" ],
    652  1.1  christos     "zstd"              => [ "zstd-dynamic" ],
    653  1.1  christos     "des"               => [ "mdc2" ],
    654  1.1  christos     "deprecated"        => [ "tls-deprecated-ec" ],
    655  1.1  christos     "ec"                => [ qw(ec2m ecdsa ecdh sm2 gost ecx tls-deprecated-ec) ],
    656  1.1  christos     "dgram"             => [ "dtls", "quic", "sctp" ],
    657  1.1  christos     "sock"              => [ "dgram", "tfo" ],
    658  1.1  christos     "dtls"              => [ @dtls ],
    659  1.1  christos     sub { 0 == scalar grep { !$disabled{$_} } @dtls }
    660  1.1  christos                         => [ "dtls" ],
    661  1.1  christos 
    662  1.1  christos     "tls"               => [ @tls ],
    663  1.1  christos     sub { 0 == scalar grep { !$disabled{$_} } @tls }
    664  1.1  christos                         => [ "tls" ],
    665  1.1  christos     "tls1_3"            => [ "quic" ],
    666  1.1  christos     "quic"              => [ "unstable-qlog" ],
    667  1.1  christos 
    668  1.1  christos     "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
    669  1.1  christos 
    670  1.1  christos     "module"            => [ "dynamic-engine", "fips" ],
    671  1.1  christos 
    672  1.1  christos     # Without shared libraries, dynamic engines aren't possible.
    673  1.1  christos     # This is due to them having to link with libcrypto and register features
    674  1.1  christos     # using the ENGINE functionality, and since that relies on global tables,
    675  1.1  christos     # those *have* to be exactly the same as the ones accessed from the app,
    676  1.1  christos     # which cannot be guaranteed if shared libraries aren't present.
    677  1.1  christos     # (note that even with shared libraries, both the app and dynamic engines
    678  1.1  christos     # must be linked with the same library)
    679  1.1  christos     "shared"            => [ "dynamic-engine", "uplink" ],
    680  1.1  christos     "dso"               => [ "dynamic-engine", "module" ],
    681  1.1  christos     # Other modules don't necessarily have to link with libcrypto, so shared
    682  1.1  christos     # libraries do not have to be a condition to produce those.
    683  1.1  christos 
    684  1.1  christos     # Without position independent code, there can be no shared libraries
    685  1.1  christos     # or modules.
    686  1.1  christos     "pic"               => [ "shared", "module" ],
    687  1.1  christos 
    688  1.1  christos     "engine"            => [ "dynamic-engine", grep(/eng$/, @disablables) ],
    689  1.1  christos     "dynamic-engine"    => [ "loadereng" ],
    690  1.1  christos     "hw"                => [ "padlockeng" ],
    691  1.1  christos 
    692  1.1  christos     # no-autoalginit is only useful when building non-shared
    693  1.1  christos     "autoalginit"       => [ "shared", "apps", "fips" ],
    694  1.1  christos 
    695  1.1  christos     "stdio"             => [ "apps", "capieng", "egd" ],
    696  1.1  christos     "apps"              => [ "tests" ],
    697  1.1  christos     "tests"             => [ "external-tests" ],
    698  1.1  christos     "comp"              => [ "zlib", "brotli", "zstd" ],
    699  1.1  christos     "sm3"               => [ "sm2" ],
    700  1.1  christos     sub { !$disabled{"unit-test"} } => [ "heartbeats" ],
    701  1.1  christos 
    702  1.1  christos     sub { !$disabled{"msan"} } => [ "asm" ],
    703  1.1  christos 
    704  1.1  christos     "cmac"              => [ "siv" ],
    705  1.1  christos     "legacy"            => [ "md2" ],
    706  1.1  christos 
    707  1.1  christos     "cmp"               => [ "crmf" ],
    708  1.1  christos 
    709  1.1  christos     "fips"              => [ "fips-securitychecks", "fips-post", "acvp-tests",
    710  1.1  christos                              "fips-jitter" ],
    711  1.1  christos 
    712  1.1  christos     "threads"           => [ "thread-pool" ],
    713  1.1  christos     "thread-pool"       => [ "default-thread-pool" ],
    714  1.1  christos 
    715  1.1  christos     "blake2"            => [ "argon2" ],
    716  1.1  christos 
    717  1.1  christos     "deprecated-3.0"    => [ "engine", "srp" ],
    718  1.1  christos 
    719  1.1  christos     "http"              => [ "ocsp" ]
    720  1.1  christos     );
    721  1.1  christos 
    722  1.1  christos # Avoid protocol support holes.  Also disable all versions below N, if version
    723  1.1  christos # N is disabled while N+1 is enabled.
    724  1.1  christos #
    725  1.1  christos my @list = (reverse @tls);
    726  1.1  christos while ((my $first, my $second) = (shift @list, shift @list)) {
    727  1.1  christos     last unless @list;
    728  1.1  christos     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
    729  1.1  christos                               => [ @list ] );
    730  1.1  christos     unshift @list, $second;
    731  1.1  christos }
    732  1.1  christos my @list = (reverse @dtls);
    733  1.1  christos while ((my $first, my $second) = (shift @list, shift @list)) {
    734  1.1  christos     last unless @list;
    735  1.1  christos     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
    736  1.1  christos                               => [ @list ] );
    737  1.1  christos     unshift @list, $second;
    738  1.1  christos }
    739  1.1  christos 
    740  1.1  christos # Explicit "no-..." options will be collected in %disabled along with the defaults.
    741  1.1  christos # To remove something from %disabled, use "enable-foo".
    742  1.1  christos # For symmetry, "disable-foo" is a synonym for "no-foo".
    743  1.1  christos 
    744  1.1  christos # For the "make variables" CPPINCLUDES and CPPDEFINES, we support lists with
    745  1.1  christos # platform specific list separators.  Users from those platforms should
    746  1.1  christos # recognise those separators from how you set up the PATH to find executables.
    747  1.1  christos # The default is the Unix like separator, :, but as an exception, we also
    748  1.1  christos # support the space as separator.
    749  1.1  christos my $list_separator_re =
    750  1.1  christos     { VMS           => qr/(?<!\^),/,
    751  1.1  christos       MSWin32       => qr/(?<!\\);/ } -> {$^O} // qr/(?<!\\)[:\s]/;
    752  1.1  christos # All the "make variables" we support
    753  1.1  christos # Some get pre-populated for the sake of backward compatibility
    754  1.1  christos # (we supported those before the change to "make variable" support.
    755  1.1  christos my %user = (
    756  1.1  christos     AR          => env('AR'),
    757  1.1  christos     ARFLAGS     => [],
    758  1.1  christos     AS          => undef,
    759  1.1  christos     ASFLAGS     => [],
    760  1.1  christos     CC          => env('CC'),
    761  1.1  christos     CFLAGS      => [ env('CFLAGS') || () ],
    762  1.1  christos     CXX         => env('CXX'),
    763  1.1  christos     CXXFLAGS    => [ env('CXXFLAGS') || () ],
    764  1.1  christos     CPP         => undef,
    765  1.1  christos     CPPFLAGS    => [ env('CPPFLAGS') || () ],  # -D, -I, -Wp,
    766  1.1  christos     CPPDEFINES  => [],  # Alternative for -D
    767  1.1  christos     CPPINCLUDES => [],  # Alternative for -I
    768  1.1  christos     CROSS_COMPILE => env('CROSS_COMPILE'),
    769  1.1  christos     HASHBANGPERL=> env('HASHBANGPERL') || env('PERL'),
    770  1.1  christos     LD          => undef,
    771  1.1  christos     LDFLAGS     => [ env('LDFLAGS') || () ],  # -L, -Wl,
    772  1.1  christos     LDLIBS      => [ env('LDLIBS') || () ],  # -l
    773  1.1  christos     MT          => undef,
    774  1.1  christos     MTFLAGS     => [],
    775  1.1  christos     PERL        => env('PERL') || ($^O ne "VMS" ? $^X : "perl"),
    776  1.1  christos     RANLIB      => env('RANLIB'),
    777  1.1  christos     RC          => env('RC') || env('WINDRES'),
    778  1.1  christos     RCFLAGS     => [ env('RCFLAGS') || () ],
    779  1.1  christos     OBJCOPY     => undef,
    780  1.1  christos     RM          => undef,
    781  1.1  christos    );
    782  1.1  christos # Info about what "make variables" may be prefixed with the cross compiler
    783  1.1  christos # prefix.  This should NEVER mention any such variable with a list for value.
    784  1.1  christos my @user_crossable = qw ( AR AS CC CXX CPP LD MT RANLIB RC );
    785  1.1  christos # The same but for flags given as Configure options.  These are *additional*
    786  1.1  christos # input, as opposed to the VAR=string option that override the corresponding
    787  1.1  christos # config target attributes
    788  1.1  christos my %useradd = (
    789  1.1  christos     ASFLAGS     => [],
    790  1.1  christos     CPPDEFINES  => [],
    791  1.1  christos     CPPINCLUDES => [],
    792  1.1  christos     CPPFLAGS    => [],
    793  1.1  christos     CFLAGS      => [],
    794  1.1  christos     CXXFLAGS    => [],
    795  1.1  christos     LDFLAGS     => [],
    796  1.1  christos     LDLIBS      => [],
    797  1.1  christos     RCFLAGS     => [],
    798  1.1  christos    );
    799  1.1  christos 
    800  1.1  christos my %user_synonyms = (
    801  1.1  christos     HASHBANGPERL=> 'PERL',
    802  1.1  christos     RC          => 'WINDRES',
    803  1.1  christos    );
    804  1.1  christos 
    805  1.1  christos # Some target attributes have been renamed, this is the translation table
    806  1.1  christos my %target_attr_translate =(
    807  1.1  christos     ar          => 'AR',
    808  1.1  christos     as          => 'AS',
    809  1.1  christos     cc          => 'CC',
    810  1.1  christos     cxx         => 'CXX',
    811  1.1  christos     cpp         => 'CPP',
    812  1.1  christos     hashbangperl => 'HASHBANGPERL',
    813  1.1  christos     ld          => 'LD',
    814  1.1  christos     mt          => 'MT',
    815  1.1  christos     ranlib      => 'RANLIB',
    816  1.1  christos     rc          => 'RC',
    817  1.1  christos     rm          => 'RM',
    818  1.1  christos    );
    819  1.1  christos 
    820  1.1  christos # Initialisers coming from 'config' scripts
    821  1.1  christos $config{defines} = [ split(/$list_separator_re/, env('__CNF_CPPDEFINES')) ];
    822  1.1  christos $config{includes} = [ split(/$list_separator_re/, env('__CNF_CPPINCLUDES')) ];
    823  1.1  christos $config{cppflags} = [ env('__CNF_CPPFLAGS') || () ];
    824  1.1  christos $config{cflags} = [ env('__CNF_CFLAGS') || () ];
    825  1.1  christos $config{cxxflags} = [ env('__CNF_CXXFLAGS') || () ];
    826  1.1  christos $config{lflags} = [ env('__CNF_LDFLAGS') || () ];
    827  1.1  christos $config{ex_libs} = [ env('__CNF_LDLIBS') || () ];
    828  1.1  christos 
    829  1.1  christos $config{openssl_api_defines}=[];
    830  1.1  christos $config{openssl_sys_defines}=[];
    831  1.1  christos $config{openssl_feature_defines}=[];
    832  1.1  christos $config{options}="";
    833  1.1  christos $config{build_type} = "release";
    834  1.1  christos my $target="";
    835  1.1  christos 
    836  1.1  christos my %cmdvars = ();               # Stores FOO='blah' type arguments
    837  1.1  christos my %unsupported_options = ();
    838  1.1  christos my %deprecated_options = ();
    839  1.1  christos # If you change this, update apps/version.c
    840  1.1  christos my @known_seed_sources = qw(getrandom devrandom os egd none rdcpu);
    841  1.1  christos my @seed_sources = ();
    842  1.1  christos while (@argvcopy)
    843  1.1  christos         {
    844  1.1  christos         $_ = shift @argvcopy;
    845  1.1  christos 
    846  1.1  christos         # Support env variable assignments among the options
    847  1.1  christos         if (m|^(\w+)=(.+)?$|)
    848  1.1  christos                 {
    849  1.1  christos                 $cmdvars{$1} = $2;
    850  1.1  christos                 # Every time a variable is given as a configuration argument,
    851  1.1  christos                 # it acts as a reset if the variable.
    852  1.1  christos                 if (exists $user{$1})
    853  1.1  christos                         {
    854  1.1  christos                         $user{$1} = ref $user{$1} eq "ARRAY" ? [] : undef;
    855  1.1  christos                         }
    856  1.1  christos                 #if (exists $useradd{$1})
    857  1.1  christos                 #       {
    858  1.1  christos                 #       $useradd{$1} = [];
    859  1.1  christos                 #       }
    860  1.1  christos                 next;
    861  1.1  christos                 }
    862  1.1  christos 
    863  1.1  christos         # VMS is a case insensitive environment, and depending on settings
    864  1.1  christos         # out of our control, we may receive options uppercased.  Let's
    865  1.1  christos         # downcase at least the part before any equal sign.
    866  1.1  christos         if ($^O eq "VMS")
    867  1.1  christos                 {
    868  1.1  christos                 s/^([^=]*)/lc($1)/e;
    869  1.1  christos                 }
    870  1.1  christos 
    871  1.1  christos         # some people just can't read the instructions, clang people have to...
    872  1.1  christos         s/^-no-(?!integrated-as)/no-/;
    873  1.1  christos 
    874  1.1  christos         # rewrite some options in "enable-..." form
    875  1.1  christos         s /^-?-?shared$/enable-shared/;
    876  1.1  christos         s /^sctp$/enable-sctp/;
    877  1.1  christos         s /^threads$/enable-threads/;
    878  1.1  christos         s /^zlib$/enable-zlib/;
    879  1.1  christos         s /^zlib-dynamic$/enable-zlib-dynamic/;
    880  1.1  christos         s /^fips$/enable-fips/;
    881  1.1  christos 
    882  1.1  christos         if (/^(no|disable|enable)-(.+)$/)
    883  1.1  christos                 {
    884  1.1  christos                 my $word = $2;
    885  1.1  christos                 if ($word !~ m|hw(?:-.+)| # special treatment for hw regexp opt
    886  1.1  christos                         && !exists $deprecated_disablables{$word}
    887  1.1  christos                         && !grep { $word eq $_ } @disablables)
    888  1.1  christos                         {
    889  1.1  christos                         $unsupported_options{$_} = 1;
    890  1.1  christos                         next;
    891  1.1  christos                         }
    892  1.1  christos                 }
    893  1.1  christos         if (/^no-(.+)$/ || /^disable-(.+)$/)
    894  1.1  christos                 {
    895  1.1  christos                 foreach my $proto ((@tls, @dtls))
    896  1.1  christos                         {
    897  1.1  christos                         if ($1 eq "$proto-method")
    898  1.1  christos                                 {
    899  1.1  christos                                 $disabled{"$proto"} = "option($proto-method)";
    900  1.1  christos                                 last;
    901  1.1  christos                                 }
    902  1.1  christos                         }
    903  1.1  christos                 if ($1 eq "dtls")
    904  1.1  christos                         {
    905  1.1  christos                         foreach my $proto (@dtls)
    906  1.1  christos                                 {
    907  1.1  christos                                 $disabled{$proto} = "option(dtls)";
    908  1.1  christos                                 }
    909  1.1  christos                         $disabled{"dtls"} = "option(dtls)";
    910  1.1  christos                         }
    911  1.1  christos                 elsif ($1 eq "ssl")
    912  1.1  christos                         {
    913  1.1  christos                         # Last one of its kind
    914  1.1  christos                         $disabled{"ssl3"} = "option(ssl)";
    915  1.1  christos                         }
    916  1.1  christos                 elsif ($1 eq "tls")
    917  1.1  christos                         {
    918  1.1  christos                         # XXX: Tests will fail if all SSL/TLS
    919  1.1  christos                         # protocols are disabled.
    920  1.1  christos                         foreach my $proto (@tls)
    921  1.1  christos                                 {
    922  1.1  christos                                 $disabled{$proto} = "option(tls)";
    923  1.1  christos                                 }
    924  1.1  christos                         }
    925  1.1  christos                 elsif ($1 eq "static-engine")
    926  1.1  christos                         {
    927  1.1  christos                         delete $disabled{"dynamic-engine"};
    928  1.1  christos                         }
    929  1.1  christos                 elsif ($1 eq "dynamic-engine")
    930  1.1  christos                         {
    931  1.1  christos                         $disabled{"dynamic-engine"} = "option";
    932  1.1  christos                         }
    933  1.1  christos                 elsif (exists $deprecated_disablables{$1})
    934  1.1  christos                         {
    935  1.1  christos                         $deprecated_options{$_} = 1;
    936  1.1  christos                         if (defined $deprecated_disablables{$1})
    937  1.1  christos                                 {
    938  1.1  christos                                 $disabled{$deprecated_disablables{$1}} = "option";
    939  1.1  christos                                 }
    940  1.1  christos                         }
    941  1.1  christos                 elsif ($1 =~ m|hw(?:-.+)|) # deprecate hw options in regexp form
    942  1.1  christos                         {
    943  1.1  christos                         $deprecated_options{$_} = 1;
    944  1.1  christos                         }
    945  1.1  christos                 else
    946  1.1  christos                         {
    947  1.1  christos                         $disabled{$1} = "option";
    948  1.1  christos                         }
    949  1.1  christos                 # No longer an automatic choice
    950  1.1  christos                 $auto_threads = 0 if ($1 eq "threads");
    951  1.1  christos                 }
    952  1.1  christos         elsif (/^enable-(.+)$/)
    953  1.1  christos                 {
    954  1.1  christos                 if ($1 eq "static-engine")
    955  1.1  christos                         {
    956  1.1  christos                         $disabled{"dynamic-engine"} = "option";
    957  1.1  christos                         }
    958  1.1  christos                 elsif ($1 eq "dynamic-engine")
    959  1.1  christos                         {
    960  1.1  christos                         delete $disabled{"dynamic-engine"};
    961  1.1  christos                         }
    962  1.1  christos                 elsif ($1 eq "zlib-dynamic")
    963  1.1  christos                         {
    964  1.1  christos                         delete $disabled{"zlib"};
    965  1.1  christos                         }
    966  1.1  christos                 elsif ($1 eq "brotli-dynamic")
    967  1.1  christos                         {
    968  1.1  christos                         delete $disabled{"brotli"};
    969  1.1  christos                         }
    970  1.1  christos                 elsif ($1 eq "pie")
    971  1.1  christos                         {
    972  1.1  christos                         delete $disabled{"pie"};
    973  1.1  christos                         }
    974  1.1  christos                 elsif ($1 eq "zstd-dynamic")
    975  1.1  christos                         {
    976  1.1  christos                         delete $disabled{"zstd"};
    977  1.1  christos                         }
    978  1.1  christos                 elsif ($1 eq "fips-jitter")
    979  1.1  christos                         {
    980  1.1  christos                         delete $disabled{"fips"};
    981  1.1  christos                         delete $disabled{"jitter"};
    982  1.1  christos                         }
    983  1.1  christos                 my $algo = $1;
    984  1.1  christos                 delete $disabled{$algo};
    985  1.1  christos 
    986  1.1  christos                 # No longer an automatic choice
    987  1.1  christos                 $auto_threads = 0 if ($1 eq "threads");
    988  1.1  christos                 }
    989  1.1  christos         elsif (/^-d$/)          # From older 'config'
    990  1.1  christos                 {
    991  1.1  christos                 $config{build_type} = "debug";
    992  1.1  christos                 }
    993  1.1  christos         elsif (/^-v$/)          # From older 'config'
    994  1.1  christos                 {
    995  1.1  christos                 $guess_opts{verbose} = 1;
    996  1.1  christos                 }
    997  1.1  christos         elsif (/^-w$/)
    998  1.1  christos                 {
    999  1.1  christos                 $guess_opts{nowait} = 1;
   1000  1.1  christos                 }
   1001  1.1  christos         elsif (/^-t$/)          # From older 'config'
   1002  1.1  christos                 {
   1003  1.1  christos                 $dryrun = 1;
   1004  1.1  christos                 }
   1005  1.1  christos         elsif (/^--strict-warnings$/)
   1006  1.1  christos                 {
   1007  1.1  christos                 # Pretend that our strict flags is a C flag, and replace it
   1008  1.1  christos                 # with the proper flags later on
   1009  1.1  christos                 push @{$useradd{CFLAGS}}, '--ossl-strict-warnings';
   1010  1.1  christos                 $strict_warnings=1;
   1011  1.1  christos                 }
   1012  1.1  christos         elsif (/^--debug$/)
   1013  1.1  christos                 {
   1014  1.1  christos                 $config{build_type} = "debug";
   1015  1.1  christos                 }
   1016  1.1  christos         elsif (/^--release$/)
   1017  1.1  christos                 {
   1018  1.1  christos                 $config{build_type} = "release";
   1019  1.1  christos                 }
   1020  1.1  christos         elsif (/^386$/)
   1021  1.1  christos                 { $config{processor}=386; }
   1022  1.1  christos         elsif (/^rsaref$/)
   1023  1.1  christos                 {
   1024  1.1  christos                 # No RSAref support any more since it's not needed.
   1025  1.1  christos                 # The check for the option is there so scripts aren't
   1026  1.1  christos                 # broken
   1027  1.1  christos                 }
   1028  1.1  christos         elsif (m|^[-+/]|)
   1029  1.1  christos                 {
   1030  1.1  christos                 if (/^--prefix=(.*)$/)
   1031  1.1  christos                         {
   1032  1.1  christos                         $config{prefix}=$1;
   1033  1.1  christos                         }
   1034  1.1  christos                 elsif (/^--api=(.*)$/)
   1035  1.1  christos                         {
   1036  1.1  christos                         my $api = $1;
   1037  1.1  christos                         die "Unknown API compatibility level $api"
   1038  1.1  christos                                 unless defined $apitable->{$api};
   1039  1.1  christos                         $config{api}=$apitable->{$api};
   1040  1.1  christos                         }
   1041  1.1  christos                 elsif (/^--libdir=(.*)$/)
   1042  1.1  christos                         {
   1043  1.1  christos                         $config{libdir}=$1;
   1044  1.1  christos                         }
   1045  1.1  christos                 elsif (/^--openssldir=(.*)$/)
   1046  1.1  christos                         {
   1047  1.1  christos                         $config{openssldir}=$1;
   1048  1.1  christos                         }
   1049  1.1  christos                 elsif (/^--with-jitter-include=(.*)$/)
   1050  1.1  christos                         {
   1051  1.1  christos                         $withargs{jitter_include}=$1;
   1052  1.1  christos                         }
   1053  1.1  christos                 elsif (/^--with-jitter-lib=(.*)$/)
   1054  1.1  christos                         {
   1055  1.1  christos                         $withargs{jitter_lib}=$1;
   1056  1.1  christos                         }
   1057  1.1  christos                 elsif (/^--with-zlib-lib=(.*)$/)
   1058  1.1  christos                         {
   1059  1.1  christos                         $withargs{zlib_lib}=$1;
   1060  1.1  christos                         }
   1061  1.1  christos                 elsif (/^--with-zlib-include=(.*)$/)
   1062  1.1  christos                         {
   1063  1.1  christos                         $withargs{zlib_include}=$1;
   1064  1.1  christos                         }
   1065  1.1  christos                 elsif (/^--with-brotli-lib=(.*)$/)
   1066  1.1  christos                         {
   1067  1.1  christos                         $withargs{brotli_lib}=$1;
   1068  1.1  christos                         }
   1069  1.1  christos                 elsif (/^--with-brotli-include=(.*)$/)
   1070  1.1  christos                         {
   1071  1.1  christos                         $withargs{brotli_include}=$1;
   1072  1.1  christos                         }
   1073  1.1  christos                 elsif (/^--with-zstd-lib=(.*)$/)
   1074  1.1  christos                         {
   1075  1.1  christos                         $withargs{zstd_lib}=$1;
   1076  1.1  christos                         }
   1077  1.1  christos                 elsif (/^--with-zstd-include=(.*)$/)
   1078  1.1  christos                         {
   1079  1.1  christos                         $withargs{zstd_include}=$1;
   1080  1.1  christos                         }
   1081  1.1  christos                 elsif (/^--with-fuzzer-lib=(.*)$/)
   1082  1.1  christos                         {
   1083  1.1  christos                         $withargs{fuzzer_lib}=$1;
   1084  1.1  christos                         }
   1085  1.1  christos                 elsif (/^--with-fuzzer-include=(.*)$/)
   1086  1.1  christos                         {
   1087  1.1  christos                         $withargs{fuzzer_include}=$1;
   1088  1.1  christos                         }
   1089  1.1  christos                 elsif (/^--with-rand-seed=(.*)$/)
   1090  1.1  christos                         {
   1091  1.1  christos                         foreach my $x (split(m|,|, $1))
   1092  1.1  christos                             {
   1093  1.1  christos                             die "Unknown --with-rand-seed choice $x\n"
   1094  1.1  christos                                 if ! grep { $x eq $_ } @known_seed_sources;
   1095  1.1  christos                             push @seed_sources, $x;
   1096  1.1  christos                             }
   1097  1.1  christos                         }
   1098  1.1  christos                 elsif (/^--fips-key=(.*)$/)
   1099  1.1  christos                         {
   1100  1.1  christos                         $user{FIPSKEY}=lc($1);
   1101  1.1  christos                         die "Non-hex character in FIPS key\n"
   1102  1.1  christos                            if $user{FIPSKEY} =~ /[^a-f0-9]/;
   1103  1.1  christos                         die "FIPS key must have even number of characters\n"
   1104  1.1  christos                            if length $1 & 1;
   1105  1.1  christos                         die "FIPS key too long (64 bytes max)\n"
   1106  1.1  christos                            if length $1 > 64;
   1107  1.1  christos                         }
   1108  1.1  christos                 elsif (/^--banner=(.*)$/)
   1109  1.1  christos                         {
   1110  1.1  christos                         $banner = $1 . "\n";
   1111  1.1  christos                         }
   1112  1.1  christos                 elsif (/^--cross-compile-prefix=(.*)$/)
   1113  1.1  christos                         {
   1114  1.1  christos                         $user{CROSS_COMPILE}=$1;
   1115  1.1  christos                         }
   1116  1.1  christos                 elsif (/^--config=(.*)$/)
   1117  1.1  christos                         {
   1118  1.1  christos                         read_config $1;
   1119  1.1  christos                         }
   1120  1.1  christos                 elsif (/^-l(.*)$/)
   1121  1.1  christos                         {
   1122  1.1  christos                         push @{$useradd{LDLIBS}}, $_;
   1123  1.1  christos                         }
   1124  1.1  christos                 elsif (/^-framework$/)
   1125  1.1  christos                         {
   1126  1.1  christos                         push @{$useradd{LDLIBS}}, $_, shift(@argvcopy);
   1127  1.1  christos                         }
   1128  1.1  christos                 elsif (/^-L(.*)$/ or /^-Wl,/)
   1129  1.1  christos                         {
   1130  1.1  christos                         push @{$useradd{LDFLAGS}}, $_;
   1131  1.1  christos                         }
   1132  1.1  christos                 elsif (/^-rpath$/ or /^-R$/)
   1133  1.1  christos                         # -rpath is the OSF1 rpath flag
   1134  1.1  christos                         # -R is the old Solaris rpath flag
   1135  1.1  christos                         {
   1136  1.1  christos                         my $rpath = shift(@argvcopy) || "";
   1137  1.1  christos                         $rpath .= " " if $rpath ne "";
   1138  1.1  christos                         push @{$useradd{LDFLAGS}}, $_, $rpath;
   1139  1.1  christos                         }
   1140  1.1  christos                 elsif (/^-static$/)
   1141  1.1  christos                         {
   1142  1.1  christos                         push @{$useradd{LDFLAGS}}, $_;
   1143  1.1  christos                         }
   1144  1.1  christos                 elsif (m|^[-/]D(.*)$|)
   1145  1.1  christos                         {
   1146  1.1  christos                         push @{$useradd{CPPDEFINES}}, $1;
   1147  1.1  christos                         }
   1148  1.1  christos                 elsif (m|^[-/]I(.*)$|)
   1149  1.1  christos                         {
   1150  1.1  christos                         push @{$useradd{CPPINCLUDES}}, $1;
   1151  1.1  christos                         }
   1152  1.1  christos                 elsif (/^-Wp,$/)
   1153  1.1  christos                         {
   1154  1.1  christos                         push @{$useradd{CPPFLAGS}}, $1;
   1155  1.1  christos                         }
   1156  1.1  christos                 else    # common if (/^[-+]/), just pass down...
   1157  1.1  christos                         {
   1158  1.1  christos                         # Treat %xx as an ASCII code (e.g. replace %20 by a space character).
   1159  1.1  christos                         # This provides a simple way to pass options with arguments separated
   1160  1.1  christos                         # by spaces without quoting (e.g. -opt%20arg translates to -opt arg).
   1161  1.1  christos                         $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
   1162  1.1  christos                         push @{$useradd{CFLAGS}}, $_;
   1163  1.1  christos                         push @{$useradd{CXXFLAGS}}, $_;
   1164  1.1  christos                         }
   1165  1.1  christos                 }
   1166  1.1  christos         elsif (m|^/|)
   1167  1.1  christos                 {
   1168  1.1  christos                 # Treat %xx as an ASCII code (e.g. replace %20 by a space character).
   1169  1.1  christos                 # This provides a simple way to pass options with arguments separated
   1170  1.1  christos                 # by spaces without quoting (e.g. /opt%20arg translates to /opt arg).
   1171  1.1  christos                 $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
   1172  1.1  christos                 push @{$useradd{CFLAGS}}, $_;
   1173  1.1  christos                 push @{$useradd{CXXFLAGS}}, $_;
   1174  1.1  christos                 }
   1175  1.1  christos         else
   1176  1.1  christos                 {
   1177  1.1  christos                 die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
   1178  1.1  christos                 $target=$_;
   1179  1.1  christos                 }
   1180  1.1  christos         unless ($_ eq $target || /^no-/ || /^disable-/)
   1181  1.1  christos                 {
   1182  1.1  christos                 # "no-..." follows later after implied deactivations
   1183  1.1  christos                 # have been derived.  (Don't take this too seriously,
   1184  1.1  christos                 # we really only write OPTIONS to the Makefile out of
   1185  1.1  christos                 # nostalgia.)
   1186  1.1  christos 
   1187  1.1  christos                 if ($config{options} eq "")
   1188  1.1  christos                         { $config{options} = $_; }
   1189  1.1  christos                 else
   1190  1.1  christos                         { $config{options} .= " ".$_; }
   1191  1.1  christos                 }
   1192  1.1  christos         }
   1193  1.1  christos 
   1194  1.1  christos if (keys %deprecated_options)
   1195  1.1  christos         {
   1196  1.1  christos         warn "***** Deprecated options: ",
   1197  1.1  christos                 join(", ", keys %deprecated_options), "\n";
   1198  1.1  christos         }
   1199  1.1  christos if (keys %unsupported_options)
   1200  1.1  christos         {
   1201  1.1  christos         die "***** Unsupported options: ",
   1202  1.1  christos                 join(", ", keys %unsupported_options), "\n";
   1203  1.1  christos         }
   1204  1.1  christos 
   1205  1.1  christos # If any %useradd entry has been set, we must check that the "make
   1206  1.1  christos # variables" haven't been set.  We start by checking of any %useradd entry
   1207  1.1  christos # is set.
   1208  1.1  christos if (grep { scalar @$_ > 0 } values %useradd) {
   1209  1.1  christos     # Hash of env / make variables names.  The possible values are:
   1210  1.1  christos     # 1 - "make vars"
   1211  1.1  christos     # 2 - %useradd entry set
   1212  1.1  christos     # 3 - both set
   1213  1.1  christos     my %detected_vars =
   1214  1.1  christos         map { my $v = 0;
   1215  1.1  christos               $v += 1 if $cmdvars{$_};
   1216  1.1  christos               $v += 2 if @{$useradd{$_}};
   1217  1.1  christos               $_ => $v }
   1218  1.1  christos         keys %useradd;
   1219  1.1  christos 
   1220  1.1  christos     # If any of the corresponding "make variables" is set, we error
   1221  1.1  christos     if (grep { $_ & 1 } values %detected_vars) {
   1222  1.1  christos         my $names = join(', ', grep { $detected_vars{$_} > 0 }
   1223  1.1  christos                                sort keys %detected_vars);
   1224  1.1  christos         die <<"_____";
   1225  1.1  christos ***** Mixing make variables and additional compiler/linker flags as
   1226  1.1  christos ***** configure command line option is not permitted.
   1227  1.1  christos ***** Affected make variables: $names
   1228  1.1  christos _____
   1229  1.1  christos     }
   1230  1.1  christos }
   1231  1.1  christos 
   1232  1.1  christos # Check through all supported command line variables to see if any of them
   1233  1.1  christos # were set, and canonicalise the values we got.  If no compiler or linker
   1234  1.1  christos # flag or anything else that affects %useradd was set, we also check the
   1235  1.1  christos # environment for values.
   1236  1.1  christos my $anyuseradd =
   1237  1.1  christos     grep { defined $_ && (ref $_ ne 'ARRAY' || @$_) } values %useradd;
   1238  1.1  christos foreach (keys %user) {
   1239  1.1  christos     my $value = $cmdvars{$_};
   1240  1.1  christos     $value //= env($_) unless $anyuseradd;
   1241  1.1  christos     $value //=
   1242  1.1  christos         defined $user_synonyms{$_} ? $cmdvars{$user_synonyms{$_}} : undef;
   1243  1.1  christos     $value //= defined $user_synonyms{$_} ? env($user_synonyms{$_}) : undef
   1244  1.1  christos         unless $anyuseradd;
   1245  1.1  christos 
   1246  1.1  christos     if (defined $value) {
   1247  1.1  christos         if (ref $user{$_} eq 'ARRAY') {
   1248  1.1  christos             if ($_ eq 'CPPDEFINES' || $_ eq 'CPPINCLUDES') {
   1249  1.1  christos                 $user{$_} = [ split /$list_separator_re/, $value ];
   1250  1.1  christos             } else {
   1251  1.1  christos                 $user{$_} = [ $value ];
   1252  1.1  christos             }
   1253  1.1  christos         } elsif (!defined $user{$_}) {
   1254  1.1  christos             $user{$_} = $value;
   1255  1.1  christos         }
   1256  1.1  christos     }
   1257  1.1  christos }
   1258  1.1  christos 
   1259  1.1  christos if (grep { /-rpath\b/ } ($user{LDFLAGS} ? @{$user{LDFLAGS}} : ())
   1260  1.1  christos     && !$disabled{shared}
   1261  1.1  christos     && !($disabled{asan} && $disabled{msan} && $disabled{ubsan})) {
   1262  1.1  christos     die "***** Cannot simultaneously use -rpath, shared libraries, and\n",
   1263  1.1  christos         "***** any of asan, msan or ubsan\n";
   1264  1.1  christos }
   1265  1.1  christos 
   1266  1.1  christos # If no target was given, try guessing.
   1267  1.1  christos unless ($target) {
   1268  1.1  christos     my %system_config = OpenSSL::config::get_platform(%guess_opts, %user);
   1269  1.1  christos 
   1270  1.1  christos     # The $system_config{disable} is used to populate %disabled with
   1271  1.1  christos     # entries that aren't already there.
   1272  1.1  christos     foreach ( @{$system_config{disable} // []} ) {
   1273  1.1  christos         $disabled{$_} = 'system' unless defined $disabled{$_};
   1274  1.1  christos     }
   1275  1.1  christos     delete $system_config{disable};
   1276  1.1  christos 
   1277  1.1  christos     # Override config entries with stuff from the guesser.
   1278  1.1  christos     # It's assumed that this really is nothing new.
   1279  1.1  christos     %config = ( %config, %system_config );
   1280  1.1  christos     $target = $system_config{target};
   1281  1.1  christos }
   1282  1.1  christos 
   1283  1.1  christos sub disable {
   1284  1.1  christos     my $disable_type = shift;
   1285  1.1  christos 
   1286  1.1  christos     for (@_) {
   1287  1.1  christos         $disabled{$_} = $disable_type;
   1288  1.1  christos     }
   1289  1.1  christos 
   1290  1.1  christos     my @tocheckfor = (@_ ? @_ : keys %disabled);
   1291  1.1  christos     while (@tocheckfor) {
   1292  1.1  christos         my %new_tocheckfor = ();
   1293  1.1  christos         my @cascade_copy = (@disable_cascades);
   1294  1.1  christos         while (@cascade_copy) {
   1295  1.1  christos             my ($test, $descendents) =
   1296  1.1  christos                 (shift @cascade_copy, shift @cascade_copy);
   1297  1.1  christos             if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
   1298  1.1  christos                 foreach (grep { !defined($disabled{$_}) } @$descendents) {
   1299  1.1  christos                     $new_tocheckfor{$_} = 1; $disabled{$_} = "cascade";
   1300  1.1  christos                 }
   1301  1.1  christos             }
   1302  1.1  christos         }
   1303  1.1  christos         @tocheckfor = (keys %new_tocheckfor);
   1304  1.1  christos     }
   1305  1.1  christos }
   1306  1.1  christos disable();                     # First cascade run
   1307  1.1  christos 
   1308  1.1  christos our $die = sub { die @_; };
   1309  1.1  christos if ($target eq "TABLE") {
   1310  1.1  christos     local $die = sub { warn @_; };
   1311  1.1  christos     foreach (sort keys %table) {
   1312  1.1  christos         print_table_entry($_, "TABLE");
   1313  1.1  christos     }
   1314  1.1  christos     exit 0;
   1315  1.1  christos }
   1316  1.1  christos 
   1317  1.1  christos if ($target eq "LIST") {
   1318  1.1  christos     foreach (sort keys %table) {
   1319  1.1  christos         print $_,"\n" unless $table{$_}->{template};
   1320  1.1  christos     }
   1321  1.1  christos     exit 0;
   1322  1.1  christos }
   1323  1.1  christos 
   1324  1.1  christos if ($target eq "HASH") {
   1325  1.1  christos     local $die = sub { warn @_; };
   1326  1.1  christos     print "%table = (\n";
   1327  1.1  christos     foreach (sort keys %table) {
   1328  1.1  christos         print_table_entry($_, "HASH");
   1329  1.1  christos     }
   1330  1.1  christos     exit 0;
   1331  1.1  christos }
   1332  1.1  christos 
   1333  1.1  christos print "Configuring OpenSSL version $config{full_version} ";
   1334  1.1  christos print "for target $target\n";
   1335  1.1  christos 
   1336  1.1  christos if (scalar(@seed_sources) == 0) {
   1337  1.1  christos     print "Using os-specific seed configuration\n";
   1338  1.1  christos     push @seed_sources, 'os';
   1339  1.1  christos }
   1340  1.1  christos if (scalar(grep { $_ eq 'egd' } @seed_sources) > 0) {
   1341  1.1  christos     delete $disabled{'egd'};
   1342  1.1  christos }
   1343  1.1  christos if (scalar(grep { $_ eq 'none' } @seed_sources) > 0) {
   1344  1.1  christos     die "Cannot seed with none and anything else" if scalar(@seed_sources) > 1;
   1345  1.1  christos     warn <<_____ if scalar(@seed_sources) == 1;
   1346  1.1  christos 
   1347  1.1  christos ============================== WARNING ===============================
   1348  1.1  christos You have selected the --with-rand-seed=none option, which effectively
   1349  1.1  christos disables automatic reseeding of the OpenSSL SEED-SRC random generator.
   1350  1.1  christos All operations depending on the random generator such as creating keys
   1351  1.1  christos will not work unless the random generator is seeded manually by the
   1352  1.1  christos application.
   1353  1.1  christos 
   1354  1.1  christos Instead of manually seeding, a different random generator can be set
   1355  1.1  christos at runtime in openssl.cnf or configured at build time with
   1356  1.1  christos -DOPENSSL_DEFAULT_SEED_SRC.
   1357  1.1  christos 
   1358  1.1  christos Please read the 'Note on random number generation' section in the
   1359  1.1  christos INSTALL.md instructions and the RAND_DRBG(7) manual page for more
   1360  1.1  christos details.
   1361  1.1  christos ============================== WARNING ===============================
   1362  1.1  christos 
   1363  1.1  christos _____
   1364  1.1  christos }
   1365  1.1  christos push @{$config{openssl_feature_defines}},
   1366  1.1  christos      map { (my $x = $_) =~ tr|[\-a-z]|[_A-Z]|; "OPENSSL_RAND_SEED_$x" }
   1367  1.1  christos         @seed_sources;
   1368  1.1  christos 
   1369  1.1  christos my $provider_string = $disabled{"fips-post"} ? "non-compliant FIPS Provider" : "FIPS Provider";
   1370  1.1  christos 
   1371  1.1  christos $config{FIPS_VENDOR} =
   1372  1.1  christos     (defined $version{FIPS_VENDOR} ? "$version{FIPS_VENDOR} $provider_string for OpenSSL" : "OpenSSL $provider_string");
   1373  1.1  christos 
   1374  1.1  christos # Backward compatibility?
   1375  1.1  christos if ($target =~ m/^CygWin32(-.*)$/) {
   1376  1.1  christos     $target = "Cygwin".$1;
   1377  1.1  christos }
   1378  1.1  christos 
   1379  1.1  christos # Support for legacy targets having a name starting with 'debug-'
   1380  1.1  christos my ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
   1381  1.1  christos if ($d) {
   1382  1.1  christos     $config{build_type} = "debug";
   1383  1.1  christos 
   1384  1.1  christos     # If we do not find debug-foo in the table, the target is set to foo.
   1385  1.1  christos     if (!$table{$target}) {
   1386  1.1  christos         $target = $t;
   1387  1.1  christos     }
   1388  1.1  christos }
   1389  1.1  christos 
   1390  1.1  christos if ($target) {
   1391  1.1  christos     # It's possible that we have different config targets for specific
   1392  1.1  christos     # toolchains, so we try to detect them, and go for the plain config
   1393  1.1  christos     # target if not.
   1394  1.1  christos     my $found;
   1395  1.1  christos     foreach ( ( "$target-$user{CC}", "$target", undef ) ) {
   1396  1.1  christos         $found=$_ if $table{$_} && !$table{$_}->{template};
   1397  1.1  christos         last if $found;
   1398  1.1  christos     }
   1399  1.1  christos     $target = $found;
   1400  1.1  christos } else {
   1401  1.1  christos     # If we don't have a config target now, we try the C compiler as we
   1402  1.1  christos     # fallback
   1403  1.1  christos     my $cc = $user{CC} // 'cc';
   1404  1.1  christos     $target = $cc if $table{$cc} && !$table{$cc}->{template};
   1405  1.1  christos }
   1406  1.1  christos 
   1407  1.1  christos &usage unless $target;
   1408  1.1  christos 
   1409  1.1  christos exit 0 if $dryrun;              # From older 'config'
   1410  1.1  christos 
   1411  1.1  christos $config{target} = $target;
   1412  1.1  christos my %target = resolve_config($target);
   1413  1.1  christos 
   1414  1.1  christos foreach (keys %target_attr_translate) {
   1415  1.1  christos     $target{$target_attr_translate{$_}} = $target{$_}
   1416  1.1  christos         if $target{$_};
   1417  1.1  christos     delete $target{$_};
   1418  1.1  christos }
   1419  1.1  christos 
   1420  1.1  christos %target = ( %{$table{DEFAULTS}}, %target );
   1421  1.1  christos 
   1422  1.1  christos my %conf_files = map { $_ => 1 } (@{$target{_conf_fname_int}});
   1423  1.1  christos $config{conf_files} = [ sort keys %conf_files ];
   1424  1.1  christos 
   1425  1.1  christos # Using sub disable within these loops may prove fragile, so we run
   1426  1.1  christos # a cascade afterwards
   1427  1.1  christos foreach my $feature (@{$target{disable}}) {
   1428  1.1  christos     if (exists $deprecated_disablables{$feature}) {
   1429  1.1  christos         warn "***** config $target disables deprecated feature $feature\n";
   1430  1.1  christos     } elsif (!grep { $feature eq $_ } @disablables) {
   1431  1.1  christos         die "***** config $target disables unknown feature $feature\n";
   1432  1.1  christos     }
   1433  1.1  christos     $disabled{$feature} = 'config';
   1434  1.1  christos }
   1435  1.1  christos foreach my $feature (@{$target{enable}}) {
   1436  1.1  christos     if ("default" eq ($disabled{$feature} // "")) {
   1437  1.1  christos         if (exists $deprecated_disablables{$feature}) {
   1438  1.1  christos             warn "***** config $target enables deprecated feature $feature\n";
   1439  1.1  christos         } elsif (!grep { $feature eq $_ } @disablables) {
   1440  1.1  christos             die "***** config $target enables unknown feature $feature\n";
   1441  1.1  christos         }
   1442  1.1  christos         delete $disabled{$feature};
   1443  1.1  christos     }
   1444  1.1  christos }
   1445  1.1  christos 
   1446  1.1  christos # If uplink_arch isn't defined, disable uplink
   1447  1.1  christos $disabled{uplink} = 'no uplink_arch' unless (defined $target{uplink_arch});
   1448  1.1  christos # If asm_arch isn't defined, disable asm
   1449  1.1  christos $disabled{asm} = 'no asm_arch' unless (defined $target{asm_arch});
   1450  1.1  christos 
   1451  1.1  christos disable();                      # Run a cascade now
   1452  1.1  christos 
   1453  1.1  christos $target{CXXFLAGS}//=$target{CFLAGS} if $target{CXX};
   1454  1.1  christos $target{cxxflags}//=$target{cflags} if $target{CXX};
   1455  1.1  christos $target{exe_extension}=".exe" if ($config{target} eq "DJGPP");
   1456  1.1  christos $target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
   1457  1.1  christos 
   1458  1.1  christos # Fill %config with values from %user, and in case those are undefined or
   1459  1.1  christos # empty, use values from %target (acting as a default).
   1460  1.1  christos foreach (keys %user) {
   1461  1.1  christos     my $ref_type = ref $user{$_};
   1462  1.1  christos 
   1463  1.1  christos     # Temporary function.  Takes an intended ref type (empty string or "ARRAY")
   1464  1.1  christos     # and a value that's to be coerced into that type.
   1465  1.1  christos     my $mkvalue = sub {
   1466  1.1  christos         my $type = shift;
   1467  1.1  christos         my $value = shift;
   1468  1.1  christos         my $undef_p = shift;
   1469  1.1  christos 
   1470  1.1  christos         die "Too many arguments for \$mkvalue" if @_;
   1471  1.1  christos 
   1472  1.1  christos         while (ref $value eq 'CODE') {
   1473  1.1  christos             $value = $value->();
   1474  1.1  christos         }
   1475  1.1  christos 
   1476  1.1  christos         if ($type eq 'ARRAY') {
   1477  1.1  christos             return undef unless defined $value;
   1478  1.1  christos             return undef if ref $value ne 'ARRAY' && !$value;
   1479  1.1  christos             return undef if ref $value eq 'ARRAY' && !@$value;
   1480  1.1  christos             return [ $value ] unless ref $value eq 'ARRAY';
   1481  1.1  christos         }
   1482  1.1  christos         return undef unless $value;
   1483  1.1  christos         return $value;
   1484  1.1  christos     };
   1485  1.1  christos 
   1486  1.1  christos     $config{$_} =
   1487  1.1  christos         $mkvalue->($ref_type, $user{$_})
   1488  1.1  christos         || $mkvalue->($ref_type, $target{$_});
   1489  1.1  christos     delete $config{$_} unless defined $config{$_};
   1490  1.1  christos }
   1491  1.1  christos 
   1492  1.1  christos # Finish up %config by appending things the user gave us on the command line
   1493  1.1  christos # apart from "make variables"
   1494  1.1  christos foreach (keys %useradd) {
   1495  1.1  christos     # The must all be lists, so we assert that here
   1496  1.1  christos     die "internal error: \$useradd{$_} isn't an ARRAY\n"
   1497  1.1  christos         unless ref $useradd{$_} eq 'ARRAY';
   1498  1.1  christos 
   1499  1.1  christos     if (defined $config{$_}) {
   1500  1.1  christos         push @{$config{$_}}, @{$useradd{$_}};
   1501  1.1  christos     } else {
   1502  1.1  christos         $config{$_} = [ @{$useradd{$_}} ];
   1503  1.1  christos     }
   1504  1.1  christos }
   1505  1.1  christos # At this point, we can forget everything about %user and %useradd,
   1506  1.1  christos # because it's now all been merged into the corresponding $config entry
   1507  1.1  christos 
   1508  1.1  christos if ($config{prefix} && !$config{CROSS_COMPILE}) {
   1509  1.1  christos     die "Directory given with --prefix MUST be absolute\n"
   1510  1.1  christos         unless file_name_is_absolute($config{prefix});
   1511  1.1  christos }
   1512  1.1  christos 
   1513  1.1  christos if (grep { $_ =~ /(?:^|\s)-static(?:\s|$)/ } @{$config{LDFLAGS}}) {
   1514  1.1  christos     disable('static', 'pic', 'threads');
   1515  1.1  christos }
   1516  1.1  christos 
   1517  1.1  christos # Allow overriding the build file name
   1518  1.1  christos $config{build_file} = env('BUILDFILE') || $target{build_file} || "Makefile";
   1519  1.1  christos 
   1520  1.1  christos # Make sure build_scheme is consistent.
   1521  1.1  christos $target{build_scheme} = [ $target{build_scheme} ]
   1522  1.1  christos     if ref($target{build_scheme}) ne "ARRAY";
   1523  1.1  christos 
   1524  1.1  christos my ($builder, $builder_platform, @builder_opts) =
   1525  1.1  christos     @{$target{build_scheme}};
   1526  1.1  christos 
   1527  1.1  christos foreach my $checker (($builder_platform."-".$config{build_file}."-checker.pm",
   1528  1.1  christos                       $builder_platform."-checker.pm")) {
   1529  1.1  christos     my $checker_path = catfile($srcdir, "Configurations", $checker);
   1530  1.1  christos     if (-f $checker_path) {
   1531  1.1  christos         my $fn = $ENV{CONFIGURE_CHECKER_WARN}
   1532  1.1  christos             ? sub { warn $@; } : sub { die $@; };
   1533  1.1  christos         if (! do $checker_path) {
   1534  1.1  christos             if ($@) {
   1535  1.1  christos                 $fn->($@);
   1536  1.1  christos             } elsif ($!) {
   1537  1.1  christos                 $fn->($!);
   1538  1.1  christos             } else {
   1539  1.1  christos                 $fn->("The detected tools didn't match the platform\n");
   1540  1.1  christos             }
   1541  1.1  christos         }
   1542  1.1  christos         last;
   1543  1.1  christos     }
   1544  1.1  christos }
   1545  1.1  christos 
   1546  1.1  christos push @{$config{defines}}, "NDEBUG"    if $config{build_type} eq "release";
   1547  1.1  christos 
   1548  1.1  christos if ($target =~ /^mingw/ && `$config{CC} --target-help 2>&1` =~ m/-mno-cygwin/m)
   1549  1.1  christos         {
   1550  1.1  christos         push @{$config{cflags}}, "-mno-cygwin";
   1551  1.1  christos         push @{$config{cxxflags}}, "-mno-cygwin" if $config{CXX};
   1552  1.1  christos         push @{$config{shared_ldflag}}, "-mno-cygwin";
   1553  1.1  christos         }
   1554  1.1  christos 
   1555  1.1  christos if ($target =~ /linux.*-mips/ && !$disabled{asm}
   1556  1.1  christos         && !grep { $_ =~ /-m(ips|arch=)/ } (@{$config{CFLAGS}})) {
   1557  1.1  christos         # minimally required architecture flags for assembly modules
   1558  1.1  christos         my $value;
   1559  1.1  christos         $value = '-mips2' if ($target =~ /mips32/);
   1560  1.1  christos         $value = '-mips3' if ($target =~ /mips64/);
   1561  1.1  christos         unshift @{$config{cflags}}, $value;
   1562  1.1  christos         unshift @{$config{cxxflags}}, $value if $config{CXX};
   1563  1.1  christos }
   1564  1.1  christos 
   1565  1.1  christos # If threads aren't disabled, check how possible they are
   1566  1.1  christos unless ($disabled{threads}) {
   1567  1.1  christos     if ($auto_threads) {
   1568  1.1  christos         # Enabled by default, disable it forcibly if unavailable
   1569  1.1  christos         if ($target{thread_scheme} eq "(unknown)") {
   1570  1.1  christos             disable("unavailable", 'threads');
   1571  1.1  christos         }
   1572  1.1  christos     } else {
   1573  1.1  christos         # The user chose to enable threads explicitly, let's see
   1574  1.1  christos         # if there's a chance that's possible
   1575  1.1  christos         if ($target{thread_scheme} eq "(unknown)") {
   1576  1.1  christos             # If the user asked for "threads" and we don't have internal
   1577  1.1  christos             # knowledge how to do it, [s]he is expected to provide any
   1578  1.1  christos             # system-dependent compiler options that are necessary.  We
   1579  1.1  christos             # can't truly check that the given options are correct, but
   1580  1.1  christos             # we expect the user to know what [s]He is doing.
   1581  1.1  christos             if (!@{$config{CFLAGS}} && !@{$config{CPPDEFINES}}) {
   1582  1.1  christos                 die "You asked for multi-threading support, but didn't\n"
   1583  1.1  christos                     ,"provide any system-specific compiler options\n";
   1584  1.1  christos             }
   1585  1.1  christos         }
   1586  1.1  christos     }
   1587  1.1  christos }
   1588  1.1  christos 
   1589  1.1  christos # Find out if clang's sanitizers have been enabled with -fsanitize
   1590  1.1  christos # flags and ensure that the corresponding %disabled elements area
   1591  1.1  christos # removed to reflect that the sanitizers are indeed enabled.
   1592  1.1  christos my %detected_sanitizers = ();
   1593  1.1  christos foreach (grep /^-fsanitize=/, @{$config{CFLAGS} || []}) {
   1594  1.1  christos     (my $checks = $_) =~ s/^-fsanitize=//;
   1595  1.1  christos     foreach (split /,/, $checks) {
   1596  1.1  christos         my $d = { address       => 'asan',
   1597  1.1  christos                   undefined     => 'ubsan',
   1598  1.1  christos                   memory        => 'msan' } -> {$_};
   1599  1.1  christos         next unless defined $d;
   1600  1.1  christos 
   1601  1.1  christos         $detected_sanitizers{$d} = 1;
   1602  1.1  christos         if (defined $disabled{$d}) {
   1603  1.1  christos             die "***** Conflict between disabling $d and enabling $_ sanitizer"
   1604  1.1  christos                 if $disabled{$d} ne "default";
   1605  1.1  christos             delete $disabled{$d};
   1606  1.1  christos         }
   1607  1.1  christos     }
   1608  1.1  christos }
   1609  1.1  christos 
   1610  1.1  christos # If threads still aren't disabled, add a C macro to ensure the source
   1611  1.1  christos # code knows about it.  Any other flag is taken care of by the configs.
   1612  1.1  christos unless($disabled{threads}) {
   1613  1.1  christos     push @{$config{openssl_feature_defines}}, "OPENSSL_THREADS";
   1614  1.1  christos }
   1615  1.1  christos 
   1616  1.1  christos if ($disabled{"unstable-qlog"}) {
   1617  1.1  christos     $disabled{"qlog"} = 1;
   1618  1.1  christos }
   1619  1.1  christos 
   1620  1.1  christos my $no_shared_warn=0;
   1621  1.1  christos if (($target{shared_target} // '') eq "")
   1622  1.1  christos         {
   1623  1.1  christos         $no_shared_warn = 1
   1624  1.1  christos             if (!$disabled{shared} || !$disabled{"dynamic-engine"});
   1625  1.1  christos         disable('no-shared-target', 'pic');
   1626  1.1  christos         }
   1627  1.1  christos 
   1628  1.1  christos if ($disabled{"dynamic-engine"}) {
   1629  1.1  christos         $config{dynamic_engines} = 0;
   1630  1.1  christos } else {
   1631  1.1  christos         $config{dynamic_engines} = 1;
   1632  1.1  christos }
   1633  1.1  christos 
   1634  1.1  christos unless ($disabled{asan} || defined $detected_sanitizers{asan}) {
   1635  1.1  christos     push @{$config{cflags}}, "-fsanitize=address";
   1636  1.1  christos }
   1637  1.1  christos 
   1638  1.1  christos unless ($disabled{ubsan} || defined $detected_sanitizers{ubsan}) {
   1639  1.1  christos     push @{$config{cflags}}, "-fsanitize=undefined", "-fno-sanitize-recover=all", "-DPEDANTIC";
   1640  1.1  christos }
   1641  1.1  christos 
   1642  1.1  christos unless ($disabled{msan} || defined $detected_sanitizers{msan}) {
   1643  1.1  christos   push @{$config{cflags}}, "-fsanitize=memory";
   1644  1.1  christos }
   1645  1.1  christos 
   1646  1.1  christos unless ($disabled{"fuzz-libfuzzer"} && $disabled{"fuzz-afl"}
   1647  1.1  christos         && $disabled{asan} && $disabled{ubsan} && $disabled{msan}) {
   1648  1.1  christos     push @{$config{cflags}}, "-fno-omit-frame-pointer", "-g";
   1649  1.1  christos     push @{$config{cxxflags}}, "-fno-omit-frame-pointer", "-g" if $config{CXX};
   1650  1.1  christos }
   1651  1.1  christos #
   1652  1.1  christos # Platform fix-ups
   1653  1.1  christos #
   1654  1.1  christos 
   1655  1.1  christos # This saves the build files from having to check
   1656  1.1  christos if ($disabled{pic})
   1657  1.1  christos         {
   1658  1.1  christos         foreach (qw(shared_cflag shared_cxxflag shared_cppflag
   1659  1.1  christos                     shared_defines shared_includes shared_ldflag
   1660  1.1  christos                     module_cflags module_cxxflags module_cppflags
   1661  1.1  christos                     module_defines module_includes module_lflags))
   1662  1.1  christos                 {
   1663  1.1  christos                 delete $config{$_};
   1664  1.1  christos                 $target{$_} = "";
   1665  1.1  christos                 }
   1666  1.1  christos         }
   1667  1.1  christos else
   1668  1.1  christos         {
   1669  1.1  christos         push @{$config{lib_defines}}, "OPENSSL_PIC";
   1670  1.1  christos         }
   1671  1.1  christos 
   1672  1.1  christos if ($target{sys_id} ne "")
   1673  1.1  christos         {
   1674  1.1  christos         push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
   1675  1.1  christos         }
   1676  1.1  christos 
   1677  1.1  christos my %predefined_C = compiler_predefined($config{CROSS_COMPILE}.$config{CC});
   1678  1.1  christos my %predefined_CXX = $config{CXX}
   1679  1.1  christos     ? compiler_predefined($config{CROSS_COMPILE}.$config{CXX})
   1680  1.1  christos     : ();
   1681  1.1  christos 
   1682  1.1  christos unless ($disabled{asm}) {
   1683  1.1  christos     # big endian systems can use ELFv2 ABI
   1684  1.1  christos     if ($target eq "linux-ppc64" || $target eq "BSD-ppc64") {
   1685  1.1  christos         $target{perlasm_scheme} = "linux64v2" if ($predefined_C{_CALL_ELF} == 2);
   1686  1.1  christos     }
   1687  1.1  christos }
   1688  1.1  christos 
   1689  1.1  christos # Check for makedepend capabilities.
   1690  1.1  christos if (!$disabled{makedepend}) {
   1691  1.1  christos     # If the attribute makedep_scheme is defined, then we assume that the
   1692  1.1  christos     # config target and its associated build file are programmed to deal
   1693  1.1  christos     # with it.
   1694  1.1  christos     # If makedep_scheme is undefined, we go looking for GCC compatible
   1695  1.1  christos     # dependency making, and if that's not available, we try to fall back
   1696  1.1  christos     # on 'makedepend'.
   1697  1.1  christos     if ($target{makedep_scheme}) {
   1698  1.1  christos         $config{makedep_scheme} = $target{makedep_scheme};
   1699  1.1  christos         # If the makedepcmd attribute is defined, copy it.  If not, the
   1700  1.1  christos         # build files will have to fend for themselves.
   1701  1.1  christos         $config{makedepcmd} = $target{makedepcmd} if $target{makedepcmd};
   1702  1.1  christos     } elsif (($predefined_C{__GNUC__} // -1) >= 3
   1703  1.1  christos              && !($predefined_C{__APPLE_CC__} && !$predefined_C{__clang__})) {
   1704  1.1  christos         # We know that GNU C version 3 and up as well as all clang
   1705  1.1  christos         # versions support dependency generation, but Xcode did not
   1706  1.1  christos         # handle $cc -M before clang support (but claims __GNUC__ = 3)
   1707  1.1  christos         $config{makedep_scheme} = 'gcc';
   1708  1.1  christos     } else {
   1709  1.1  christos         # In all other cases, we look for 'makedepend', and set the
   1710  1.1  christos         # makedep_scheme value if we found it.
   1711  1.1  christos         $config{makedepcmd} = which('makedepend');
   1712  1.1  christos         $config{makedep_scheme} = 'makedepend' if $config{makedepcmd};
   1713  1.1  christos     }
   1714  1.1  christos 
   1715  1.1  christos     # If no depend scheme is set, we disable makedepend
   1716  1.1  christos     disable('unavailable', 'makedepend') unless $config{makedep_scheme};
   1717  1.1  christos }
   1718  1.1  christos 
   1719  1.1  christos if (!$disabled{asm} && !$predefined_C{__MACH__} && $^O ne 'VMS' && !$predefined_C{_AIX}) {
   1720  1.1  christos     # probe for -Wa,--noexecstack option...
   1721  1.1  christos     if ($predefined_C{__clang__}) {
   1722  1.1  christos         # clang has builtin assembler, which doesn't recognize --help,
   1723  1.1  christos         # but it apparently recognizes the option in question on all
   1724  1.1  christos         # supported platforms even when it's meaningless. In other words
   1725  1.1  christos         # probe would fail, but probed option always accepted...
   1726  1.1  christos         push @{$config{cflags}}, "-Wa,--noexecstack", "-Qunused-arguments";
   1727  1.1  christos     } else {
   1728  1.1  christos         my $cc = $config{CROSS_COMPILE}.$config{CC};
   1729  1.1  christos         open(PIPE, "$cc -Wa,--help -c -o null.$$.o -x assembler /dev/null 2>&1 |");
   1730  1.1  christos         while(<PIPE>) {
   1731  1.1  christos             if (m/--noexecstack/) {
   1732  1.1  christos                 push @{$config{cflags}}, "-Wa,--noexecstack";
   1733  1.1  christos                 last;
   1734  1.1  christos             }
   1735  1.1  christos         }
   1736  1.1  christos         close(PIPE);
   1737  1.1  christos         unlink("null.$$.o");
   1738  1.1  christos     }
   1739  1.1  christos }
   1740  1.1  christos 
   1741  1.1  christos # Deal with bn_ops ###################################################
   1742  1.1  christos 
   1743  1.1  christos $config{bn_ll}                  =0;
   1744  1.1  christos my $def_int="unsigned int";
   1745  1.1  christos $config{rc4_int}                =$def_int;
   1746  1.1  christos ($config{b64l},$config{b64},$config{b32})=(0,0,1);
   1747  1.1  christos 
   1748  1.1  christos my $count = 0;
   1749  1.1  christos foreach (sort split(/\s+/,$target{bn_ops})) {
   1750  1.1  christos     $count++ if /SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT/;
   1751  1.1  christos     $config{bn_ll}=1                            if $_ eq 'BN_LLONG';
   1752  1.1  christos     $config{rc4_int}="unsigned char"            if $_ eq 'RC4_CHAR';
   1753  1.1  christos     ($config{b64l},$config{b64},$config{b32})
   1754  1.1  christos         =(0,1,0)                                if $_ eq 'SIXTY_FOUR_BIT';
   1755  1.1  christos     ($config{b64l},$config{b64},$config{b32})
   1756  1.1  christos         =(1,0,0)                                if $_ eq 'SIXTY_FOUR_BIT_LONG';
   1757  1.1  christos     ($config{b64l},$config{b64},$config{b32})
   1758  1.1  christos         =(0,0,1)                                if $_ eq 'THIRTY_TWO_BIT';
   1759  1.1  christos }
   1760  1.1  christos die "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
   1761  1.1  christos     if $count > 1;
   1762  1.1  christos 
   1763  1.1  christos $config{api} = $config{major} * 10000 + $config{minor} * 100
   1764  1.1  christos     unless $config{api};
   1765  1.1  christos foreach (keys %$apitable) {
   1766  1.1  christos     $disabled{"deprecated-$_"} = "deprecation"
   1767  1.1  christos         if $disabled{deprecated} && $config{api} >= $apitable->{$_};
   1768  1.1  christos }
   1769  1.1  christos 
   1770  1.1  christos disable();                      # Run a cascade now
   1771  1.1  christos 
   1772  1.1  christos # Hack cflags for better warnings (dev option) #######################
   1773  1.1  christos 
   1774  1.1  christos # "Stringify" the C and C++ flags string.  This permits it to be made part of
   1775  1.1  christos # a string and works as well on command lines.
   1776  1.1  christos $config{cflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
   1777  1.1  christos                         @{$config{cflags}} ];
   1778  1.1  christos $config{cxxflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
   1779  1.1  christos                           @{$config{cxxflags}} ] if $config{CXX};
   1780  1.1  christos 
   1781  1.1  christos $config{openssl_api_defines} = [
   1782  1.1  christos     "OPENSSL_CONFIGURED_API=".$config{api},
   1783  1.1  christos ];
   1784  1.1  christos 
   1785  1.1  christos my @strict_warnings_collection=();
   1786  1.1  christos if ($strict_warnings)
   1787  1.1  christos         {
   1788  1.1  christos         my $wopt;
   1789  1.1  christos         my $gccver = $predefined_C{__GNUC__} // -1;
   1790  1.1  christos 
   1791  1.1  christos         if ($gccver >= 4)
   1792  1.1  christos                 {
   1793  1.1  christos                 push @strict_warnings_collection, @gcc_devteam_warn;
   1794  1.1  christos                 push @strict_warnings_collection, @clang_devteam_warn
   1795  1.1  christos                     if (defined($predefined_C{__clang__}));
   1796  1.1  christos                 }
   1797  1.1  christos         elsif ($config{target} =~ /^VC-/)
   1798  1.1  christos                 {
   1799  1.1  christos                 push @strict_warnings_collection, @cl_devteam_warn;
   1800  1.1  christos                 }
   1801  1.1  christos         else
   1802  1.1  christos                 {
   1803  1.1  christos                 warn "WARNING --strict-warnings requires gcc[>=4] or gcc-alike, or MSVC"
   1804  1.1  christos                 }
   1805  1.1  christos         }
   1806  1.1  christos 
   1807  1.1  christos $config{CFLAGS} = [ map { $_ eq '--ossl-strict-warnings'
   1808  1.1  christos                               ? @strict_warnings_collection
   1809  1.1  christos                               : ( $_ ) }
   1810  1.1  christos                     @{$config{CFLAGS}} ];
   1811  1.1  christos 
   1812  1.1  christos unless ($disabled{afalgeng}) {
   1813  1.1  christos     $config{afalgeng}="";
   1814  1.1  christos     if (grep { $_ eq 'afalgeng' } @{$target{enable}}) {
   1815  1.1  christos         push @{$config{engdirs}}, "afalg";
   1816  1.1  christos     } else {
   1817  1.1  christos         disable('not-linux', 'afalgeng');
   1818  1.1  christos     }
   1819  1.1  christos }
   1820  1.1  christos 
   1821  1.1  christos unless ($disabled{devcryptoeng}) {
   1822  1.1  christos     if ($target =~ m/^BSD/) {
   1823  1.1  christos         my $maxver = 5*100 + 7;
   1824  1.1  christos         my $sysstr = `uname -s`;
   1825  1.1  christos         my $verstr = `uname -r`;
   1826  1.1  christos         $sysstr =~ s|\R$||;
   1827  1.1  christos         $verstr =~ s|\R$||;
   1828  1.1  christos         my ($ma, $mi, @rest) = split m|\.|, $verstr;
   1829  1.1  christos         my $ver = $ma*100 + $mi;
   1830  1.1  christos         if ($sysstr eq 'OpenBSD' && $ver >= $maxver) {
   1831  1.1  christos             disable('too-new-kernel', 'devcryptoeng');
   1832  1.1  christos         }
   1833  1.1  christos     }
   1834  1.1  christos }
   1835  1.1  christos 
   1836  1.1  christos unless ($disabled{ktls}) {
   1837  1.1  christos     $config{ktls}="";
   1838  1.1  christos     my $cc = $config{CROSS_COMPILE}.$config{CC};
   1839  1.1  christos     if ($target =~ m/^linux/) {
   1840  1.1  christos         system("printf '#include <sys/types.h>\n#include <linux/tls.h>' | $cc -E - >/dev/null 2>&1");
   1841  1.1  christos         if ($? != 0) {
   1842  1.1  christos             disable('too-old-kernel', 'ktls');
   1843  1.1  christos         }
   1844  1.1  christos     } elsif ($target =~ m/^BSD/) {
   1845  1.1  christos         system("printf '#include <sys/types.h>\n#include <sys/ktls.h>' | $cc -E - >/dev/null 2>&1");
   1846  1.1  christos         if ($? != 0) {
   1847  1.1  christos             disable('too-old-freebsd', 'ktls');
   1848  1.1  christos         }
   1849  1.1  christos     } else {
   1850  1.1  christos         disable('not-linux-or-freebsd', 'ktls');
   1851  1.1  christos     }
   1852  1.1  christos }
   1853  1.1  christos 
   1854  1.1  christos unless ($disabled{winstore}) {
   1855  1.1  christos     unless ($target =~ /^(?:Cygwin|mingw|VC-|BC-)/) {
   1856  1.1  christos         disable('not-windows', 'winstore');
   1857  1.1  christos     }
   1858  1.1  christos }
   1859  1.1  christos 
   1860  1.1  christos push @{$config{openssl_other_defines}}, "OPENSSL_NO_KTLS" if ($disabled{ktls});
   1861  1.1  christos 
   1862  1.1  christos # Get the extra flags used when building shared libraries and modules.  We
   1863  1.1  christos # do this late because some of them depend on %disabled.
   1864  1.1  christos 
   1865  1.1  christos # Make the flags to build DSOs the same as for shared libraries unless they
   1866  1.1  christos # are already defined
   1867  1.1  christos $target{module_cflags} = $target{shared_cflag} unless defined $target{module_cflags};
   1868  1.1  christos $target{module_cxxflags} = $target{shared_cxxflag} unless defined $target{module_cxxflags};
   1869  1.1  christos $target{module_ldflags} = $target{shared_ldflag} unless defined $target{module_ldflags};
   1870  1.1  christos {
   1871  1.1  christos     my $shared_info_pl =
   1872  1.1  christos         catfile(dirname($0), "Configurations", "shared-info.pl");
   1873  1.1  christos     my %shared_info = read_eval_file($shared_info_pl);
   1874  1.1  christos     push @{$target{_conf_fname_int}}, $shared_info_pl;
   1875  1.1  christos     my $si = $target{shared_target};
   1876  1.1  christos     while (ref $si ne "HASH") {
   1877  1.1  christos         last if ! defined $si;
   1878  1.1  christos         if (ref $si eq "CODE") {
   1879  1.1  christos             $si = $si->();
   1880  1.1  christos         } else {
   1881  1.1  christos             $si = $shared_info{$si};
   1882  1.1  christos         }
   1883  1.1  christos     }
   1884  1.1  christos 
   1885  1.1  christos     # Some of the 'shared_target' values don't have any entries in
   1886  1.1  christos     # %shared_info.  That's perfectly fine, AS LONG AS the build file
   1887  1.1  christos     # template knows how to handle this.  That is currently the case for
   1888  1.1  christos     # Windows and VMS.
   1889  1.1  christos     if (defined $si) {
   1890  1.1  christos         # Just as above, copy certain shared_* attributes to the corresponding
   1891  1.1  christos         # module_ attribute unless the latter is already defined
   1892  1.1  christos         $si->{module_cflags} = $si->{shared_cflag} unless defined $si->{module_cflags};
   1893  1.1  christos         $si->{module_cxxflags} = $si->{shared_cxxflag} unless defined $si->{module_cxxflags};
   1894  1.1  christos         $si->{module_ldflags} = $si->{shared_ldflag} unless defined $si->{module_ldflags};
   1895  1.1  christos         foreach (sort keys %$si) {
   1896  1.1  christos             $target{$_} = defined $target{$_}
   1897  1.1  christos                 ? add($si->{$_})->($target{$_})
   1898  1.1  christos                 : $si->{$_};
   1899  1.1  christos         }
   1900  1.1  christos     }
   1901  1.1  christos }
   1902  1.1  christos 
   1903  1.1  christos # ALL MODIFICATIONS TO %disabled, %config and %target MUST BE DONE FROM HERE ON
   1904  1.1  christos 
   1905  1.1  christos ######################################################################
   1906  1.1  christos # Build up information for skipping certain directories depending on disabled
   1907  1.1  christos # features, as well as setting up macros for disabled features.
   1908  1.1  christos 
   1909  1.1  christos # This is a tentative database of directories to skip.  Some entries may not
   1910  1.1  christos # correspond to anything real, but that's ok, they will simply be ignored.
   1911  1.1  christos # The actual processing of these entries is done in the build.info lookup
   1912  1.1  christos # loop further down.
   1913  1.1  christos #
   1914  1.1  christos # The key is a Unix formatted path in the source tree, the value is an index
   1915  1.1  christos # into %disabled_info, so any existing path gets added to a corresponding
   1916  1.1  christos # 'skipped' entry in there with the list of skipped directories.
   1917  1.1  christos my %skipdir = ();
   1918  1.1  christos my %disabled_info = ();         # For configdata.pm
   1919  1.1  christos foreach my $what (sort keys %disabled) {
   1920  1.1  christos     # There are deprecated disablables that translate to themselves.
   1921  1.1  christos     # They cause disabling cascades, but should otherwise not register.
   1922  1.1  christos     next if $deprecated_disablables{$what};
   1923  1.1  christos     # The generated $disabled{"deprecated-x.y"} entries are special
   1924  1.1  christos     # and treated properly elsewhere
   1925  1.1  christos     next if $what =~ m|^deprecated-|;
   1926  1.1  christos 
   1927  1.1  christos     $config{options} .= " no-$what";
   1928  1.1  christos 
   1929  1.1  christos     if (!grep { $what eq $_ } ( 'buildtest-c++', 'fips', 'threads', 'shared',
   1930  1.1  christos                                 'module', 'pic', 'dynamic-engine', 'makedepend',
   1931  1.1  christos                                 'sse2', 'legacy' )) {
   1932  1.1  christos         (my $WHAT = uc $what) =~ s|-|_|g;
   1933  1.1  christos         my $skipdir = $what;
   1934  1.1  christos 
   1935  1.1  christos         # fix-up crypto/directory name(s)
   1936  1.1  christos         $skipdir = "ripemd" if $what eq "rmd160";
   1937  1.1  christos         $skipdir = "whrlpool" if $what eq "whirlpool";
   1938  1.1  christos 
   1939  1.1  christos         my $macro = $disabled_info{$what}->{macro} = "OPENSSL_NO_$WHAT";
   1940  1.1  christos         push @{$config{openssl_feature_defines}}, $macro;
   1941  1.1  christos 
   1942  1.1  christos         $skipdir{engines} = $what if $what eq 'engine';
   1943  1.1  christos         $skipdir{"crypto/$skipdir"} = $what
   1944  1.1  christos             unless $what eq 'async' || $what eq 'err' || $what eq 'dso' || $what eq 'http';
   1945  1.1  christos     }
   1946  1.1  christos }
   1947  1.1  christos 
   1948  1.1  christos if ($disabled{"dynamic-engine"}) {
   1949  1.1  christos     push @{$config{openssl_feature_defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
   1950  1.1  christos } else {
   1951  1.1  christos     push @{$config{openssl_feature_defines}}, "OPENSSL_NO_STATIC_ENGINE";
   1952  1.1  christos }
   1953  1.1  christos 
   1954  1.1  christos # If we use the unified build, collect information from build.info files
   1955  1.1  christos my %unified_info = ();
   1956  1.1  christos 
   1957  1.1  christos my $buildinfo_debug = defined($ENV{CONFIGURE_DEBUG_BUILDINFO});
   1958  1.1  christos if ($builder eq "unified") {
   1959  1.1  christos     use Text::Template 1.46;
   1960  1.1  christos 
   1961  1.1  christos     sub cleandir {
   1962  1.1  christos         my $base = shift;
   1963  1.1  christos         my $dir = shift;
   1964  1.1  christos         my $relativeto = shift || ".";
   1965  1.1  christos         my $no_mkpath = shift // 0;
   1966  1.1  christos 
   1967  1.1  christos         $dir = catdir($base,$dir) unless isabsolute($dir);
   1968  1.1  christos 
   1969  1.1  christos         # Make sure the directories we're building in exists
   1970  1.1  christos         mkpath($dir) unless $no_mkpath;
   1971  1.1  christos 
   1972  1.1  christos         my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
   1973  1.1  christos         #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
   1974  1.1  christos         return $res;
   1975  1.1  christos     }
   1976  1.1  christos 
   1977  1.1  christos     sub cleanfile {
   1978  1.1  christos         my $base = shift;
   1979  1.1  christos         my $file = shift;
   1980  1.1  christos         my $relativeto = shift || ".";
   1981  1.1  christos         my $no_mkpath = shift // 0;
   1982  1.1  christos 
   1983  1.1  christos         $file = catfile($base,$file) unless isabsolute($file);
   1984  1.1  christos 
   1985  1.1  christos         my $d = dirname($file);
   1986  1.1  christos         my $f = basename($file);
   1987  1.1  christos 
   1988  1.1  christos         # Make sure the directories we're building in exists
   1989  1.1  christos         mkpath($d) unless $no_mkpath;
   1990  1.1  christos 
   1991  1.1  christos         my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
   1992  1.1  christos         #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
   1993  1.1  christos         return $res;
   1994  1.1  christos     }
   1995  1.1  christos 
   1996  1.1  christos     # Store the name of the template file we will build the build file from
   1997  1.1  christos     # in %config.  This may be useful for the build file itself.
   1998  1.1  christos     my @build_file_template_names =
   1999  1.1  christos         ( $builder_platform."-".$config{build_file}.".tmpl",
   2000  1.1  christos           $config{build_file}.".tmpl" );
   2001  1.1  christos     my @build_file_templates = ();
   2002  1.1  christos 
   2003  1.1  christos     # First, look in the user provided directory, if given
   2004  1.1  christos     if (defined env($local_config_envname)) {
   2005  1.1  christos         @build_file_templates =
   2006  1.1  christos             map {
   2007  1.1  christos                 if ($^O eq 'VMS') {
   2008  1.1  christos                     # VMS environment variables are logical names,
   2009  1.1  christos                     # which can be used as is
   2010  1.1  christos                     $local_config_envname . ':' . $_;
   2011  1.1  christos                 } else {
   2012  1.1  christos                     catfile(env($local_config_envname), $_);
   2013  1.1  christos                 }
   2014  1.1  christos             }
   2015  1.1  christos             @build_file_template_names;
   2016  1.1  christos     }
   2017  1.1  christos     # Then, look in our standard directory
   2018  1.1  christos     push @build_file_templates,
   2019  1.1  christos         ( map { cleanfile($srcdir, catfile("Configurations", $_), $blddir, 1) }
   2020  1.1  christos           @build_file_template_names );
   2021  1.1  christos 
   2022  1.1  christos     my $build_file_template;
   2023  1.1  christos     for $_ (@build_file_templates) {
   2024  1.1  christos         $build_file_template = $_;
   2025  1.1  christos         last if -f $build_file_template;
   2026  1.1  christos 
   2027  1.1  christos         $build_file_template = undef;
   2028  1.1  christos     }
   2029  1.1  christos     if (!defined $build_file_template) {
   2030  1.1  christos         die "*** Couldn't find any of:\n", join("\n", @build_file_templates), "\n";
   2031  1.1  christos     }
   2032  1.1  christos     $config{build_file_templates}
   2033  1.1  christos       = [ cleanfile($srcdir, catfile("Configurations", "common0.tmpl"),
   2034  1.1  christos                     $blddir, 1),
   2035  1.1  christos            $build_file_template ];
   2036  1.1  christos 
   2037  1.1  christos     my @build_dirs = ( [ ] );   # current directory
   2038  1.1  christos 
   2039  1.1  christos     $config{build_infos} = [ ];
   2040  1.1  christos 
   2041  1.1  christos     # We want to detect configdata.pm in the source tree, so we
   2042  1.1  christos     # don't use it if the build tree is different.
   2043  1.1  christos     my $src_configdata = cleanfile($srcdir, "configdata.pm", $blddir, 1);
   2044  1.1  christos 
   2045  1.1  christos     # Any source file that we recognise is placed in this hash table, with
   2046  1.1  christos     # the list of its intended destinations as value.  When everything has
   2047  1.1  christos     # been collected, there's a routine that checks that these source files
   2048  1.1  christos     # exist, or if they are generated, that the generator exists.
   2049  1.1  christos     my %check_exist = ();
   2050  1.1  christos     my %check_generate = ();
   2051  1.1  christos 
   2052  1.1  christos     my %ordinals = ();
   2053  1.1  christos     while (@build_dirs) {
   2054  1.1  christos         my @curd = @{shift @build_dirs};
   2055  1.1  christos         my $sourced = catdir($srcdir, @curd);
   2056  1.1  christos         my $buildd = catdir($blddir, @curd);
   2057  1.1  christos 
   2058  1.1  christos         my $unixdir = join('/', @curd);
   2059  1.1  christos         if (exists $skipdir{$unixdir}) {
   2060  1.1  christos             my $what = $skipdir{$unixdir};
   2061  1.1  christos             push @{$disabled_info{$what}->{skipped}}, catdir(@curd);
   2062  1.1  christos             next;
   2063  1.1  christos         }
   2064  1.1  christos 
   2065  1.1  christos         mkpath($buildd);
   2066  1.1  christos 
   2067  1.1  christos         my $f = 'build.info';
   2068  1.1  christos         # The basic things we're trying to build
   2069  1.1  christos         my @programs = ();
   2070  1.1  christos         my @libraries = ();
   2071  1.1  christos         my @modules = ();
   2072  1.1  christos         my @scripts = ();
   2073  1.1  christos 
   2074  1.1  christos         my %sources = ();
   2075  1.1  christos         my %shared_sources = ();
   2076  1.1  christos         my %includes = ();
   2077  1.1  christos         my %defines = ();
   2078  1.1  christos         my %depends = ();
   2079  1.1  christos         my %generate = ();
   2080  1.1  christos         my %imagedocs = ();
   2081  1.1  christos         my %htmldocs = ();
   2082  1.1  christos         my %mandocs = ();
   2083  1.1  christos 
   2084  1.1  christos         # Support for $variablename in build.info files.
   2085  1.1  christos         # Embedded perl code is the ultimate master, still.  If its output
   2086  1.1  christos         # contains a dollar sign, it had better be escaped, or it will be
   2087  1.1  christos         # taken for a variable name prefix.
   2088  1.1  christos         my %variables = ();
   2089  1.1  christos         # Variable name syntax
   2090  1.1  christos         my $variable_name_re = qr/(?P<VARIABLE>[[:alpha:]][[:alnum:]_]*)/;
   2091  1.1  christos         # Value modifier syntaxes
   2092  1.1  christos         my $variable_subst_re = qr/\/(?P<RE>(?:\\\/|.)*?)\/(?P<SUBST>.*?)/;
   2093  1.1  christos         # Variable reference
   2094  1.1  christos         my $variable_simple_re = qr/(?<!\\)\$${variable_name_re}/;
   2095  1.1  christos         my $variable_w_mod_re =
   2096  1.1  christos             qr/(?<!\\)\$\{${variable_name_re}(?P<MOD>(?:\\\/|.)*?)\}/;
   2097  1.1  christos         # Tie it all together
   2098  1.1  christos         my $variable_re = qr/${variable_simple_re}|${variable_w_mod_re}/;
   2099  1.1  christos 
   2100  1.1  christos         my $expand_variables = sub {
   2101  1.1  christos             my $value = '';
   2102  1.1  christos             my $value_rest = shift;
   2103  1.1  christos 
   2104  1.1  christos             if ($ENV{CONFIGURE_DEBUG_VARIABLE_EXPAND}) {
   2105  1.1  christos                 print STDERR
   2106  1.1  christos                     "DEBUG[\$expand_variables] Parsed '$value_rest' ...\n"
   2107  1.1  christos             }
   2108  1.1  christos 
   2109  1.1  christos             while ($value_rest =~ /${variable_re}/) {
   2110  1.1  christos                 # We must save important regexp values, because the next
   2111  1.1  christos                 # regexp clears them
   2112  1.1  christos                 my $mod = $+{MOD};
   2113  1.1  christos                 my $variable_value = $variables{$+{VARIABLE}};
   2114  1.1  christos 
   2115  1.1  christos                 $value_rest = $';
   2116  1.1  christos                 $value .= $`;
   2117  1.1  christos 
   2118  1.1  christos                 # Process modifier expressions, if present
   2119  1.1  christos                 if (defined $mod) {
   2120  1.1  christos                     if ($mod =~ /^${variable_subst_re}$/) {
   2121  1.1  christos                         my $re = $+{RE};
   2122  1.1  christos                         my $subst = $+{SUBST};
   2123  1.1  christos 
   2124  1.1  christos                         $variable_value =~ s/\Q$re\E/$subst/g;
   2125  1.1  christos 
   2126  1.1  christos                         if ($ENV{CONFIGURE_DEBUG_VARIABLE_EXPAND}) {
   2127  1.1  christos                             print STDERR
   2128  1.1  christos                                 "DEBUG[\$expand_variables] ... and substituted ",
   2129  1.1  christos                                 "'$re' with '$subst'\n";
   2130  1.1  christos                         }
   2131  1.1  christos                     }
   2132  1.1  christos                 }
   2133  1.1  christos 
   2134  1.1  christos                 $value .= $variable_value;
   2135  1.1  christos             }
   2136  1.1  christos             if ($ENV{CONFIGURE_DEBUG_VARIABLE_EXPAND}) {
   2137  1.1  christos                 print STDERR
   2138  1.1  christos                     "DEBUG[\$expand_variables] ... into: '$value$value_rest'\n";
   2139  1.1  christos             }
   2140  1.1  christos             return $value . $value_rest;
   2141  1.1  christos         };
   2142  1.1  christos 
   2143  1.1  christos         # Support for attributes in build.info files
   2144  1.1  christos         my %attributes = ();
   2145  1.1  christos         my $handle_attributes = sub {
   2146  1.1  christos             my $attr_str = shift;
   2147  1.1  christos             my $ref = shift;
   2148  1.1  christos             my @goals = @_;
   2149  1.1  christos 
   2150  1.1  christos             return unless defined $attr_str;
   2151  1.1  christos 
   2152  1.1  christos             my @a = tokenize($attr_str, qr|\s*,\s*|);
   2153  1.1  christos             foreach my $a (@a) {
   2154  1.1  christos                 my $ac = 1;
   2155  1.1  christos                 my $ak = $a;
   2156  1.1  christos                 my $av = 1;
   2157  1.1  christos                 if ($a =~ m|^(!)?(.*?)\s* = \s*(.*?)$|x) {
   2158  1.1  christos                     $ac = ! $1;
   2159  1.1  christos                     $ak = $2;
   2160  1.1  christos                     $av = $3;
   2161  1.1  christos                 }
   2162  1.1  christos                 foreach my $g (@goals) {
   2163  1.1  christos                     if ($ac) {
   2164  1.1  christos                         $$ref->{$g}->{$ak} = $av;
   2165  1.1  christos                     } else {
   2166  1.1  christos                         delete $$ref->{$g}->{$ak};
   2167  1.1  christos                     }
   2168  1.1  christos                 }
   2169  1.1  christos             }
   2170  1.1  christos         };
   2171  1.1  christos 
   2172  1.1  christos         # Support for pushing values on multiple indexes of a given hash
   2173  1.1  christos         # array.
   2174  1.1  christos         my $push_to = sub {
   2175  1.1  christos             my $valueref = shift;
   2176  1.1  christos             my $index_str = shift; # May be undef or empty
   2177  1.1  christos             my $attrref = shift;   # May be undef
   2178  1.1  christos             my $attr_str = shift;
   2179  1.1  christos             my @values = @_;
   2180  1.1  christos 
   2181  1.1  christos             if (defined $index_str) {
   2182  1.1  christos                 my @indexes = ( '' );
   2183  1.1  christos                 if ($index_str !~ m|^\s*$|) {
   2184  1.1  christos                     @indexes = tokenize($index_str);
   2185  1.1  christos                 }
   2186  1.1  christos                 foreach (@indexes) {
   2187  1.1  christos                     push @{$valueref->{$_}}, @values;
   2188  1.1  christos                     if (defined $attrref) {
   2189  1.1  christos                         $handle_attributes->($attr_str, \$$attrref->{$_},
   2190  1.1  christos                                              @values);
   2191  1.1  christos                     }
   2192  1.1  christos                 }
   2193  1.1  christos             } else {
   2194  1.1  christos                 push @$valueref, @values;
   2195  1.1  christos                 $handle_attributes->($attr_str, $attrref, @values)
   2196  1.1  christos                     if defined $attrref;
   2197  1.1  christos             }
   2198  1.1  christos         };
   2199  1.1  christos 
   2200  1.1  christos         if ($buildinfo_debug) {
   2201  1.1  christos             print STDERR "DEBUG: Reading ",catfile($sourced, $f),"\n";
   2202  1.1  christos         }
   2203  1.1  christos         push @{$config{build_infos}}, catfile(abs2rel($sourced, $blddir), $f);
   2204  1.1  christos         my $template =
   2205  1.1  christos             Text::Template->new(TYPE => 'FILE',
   2206  1.1  christos                                 SOURCE => catfile($sourced, $f),
   2207  1.1  christos                                 PREPEND => qq{use lib "$FindBin::Bin/util/perl";});
   2208  1.1  christos         die "Something went wrong with $sourced/$f: $!\n" unless $template;
   2209  1.1  christos         my @text =
   2210  1.1  christos             split /^/m,
   2211  1.1  christos             $template->fill_in(HASH => { config => \%config,
   2212  1.1  christos                                          target => \%target,
   2213  1.1  christos                                          disabled => \%disabled,
   2214  1.1  christos                                          withargs => \%withargs,
   2215  1.1  christos                                          builddir => abs2rel($buildd, $blddir),
   2216  1.1  christos                                          sourcedir => abs2rel($sourced, $blddir),
   2217  1.1  christos                                          buildtop => abs2rel($blddir, $blddir),
   2218  1.1  christos                                          sourcetop => abs2rel($srcdir, $blddir) },
   2219  1.1  christos                                DELIMITERS => [ "{-", "-}" ]);
   2220  1.1  christos 
   2221  1.1  christos         # The top item of this stack has the following values
   2222  1.1  christos         # -2 positive already run and we found ELSE (following ELSIF should fail)
   2223  1.1  christos         # -1 positive already run (skip until ENDIF)
   2224  1.1  christos         # 0 negatives so far (if we're at a condition, check it)
   2225  1.1  christos         # 1 last was positive (don't skip lines until next ELSE, ELSIF or ENDIF)
   2226  1.1  christos         # 2 positive ELSE (following ELSIF should fail)
   2227  1.1  christos         my @skip = ();
   2228  1.1  christos 
   2229  1.1  christos         # A few useful generic regexps
   2230  1.1  christos         my $index_re = qr/\[\s*(?P<INDEX>(?:\\.|.)*?)\s*\]/;
   2231  1.1  christos         my $cond_re = qr/\[\s*(?P<COND>(?:\\.|.)*?)\s*\]/;
   2232  1.1  christos         my $attribs_re = qr/(?:\{\s*(?P<ATTRIBS>(?:\\.|.)*?)\s*\})?/;
   2233  1.1  christos         my $value_re = qr/(?P<VALUE>.*?)/;
   2234  1.1  christos         collect_information(
   2235  1.1  christos             collect_from_array([ @text ],
   2236  1.1  christos                                qr/\\$/ => sub { my $l1 = shift; my $l2 = shift;
   2237  1.1  christos                                                 $l1 =~ s/\\$//; $l1.$l2 }),
   2238  1.1  christos             # Info we're looking for
   2239  1.1  christos             qr/^\s* IF ${cond_re} \s*$/x
   2240  1.1  christos             => sub {
   2241  1.1  christos                 if (! @skip || $skip[$#skip] > 0) {
   2242  1.1  christos                     push @skip, !! $expand_variables->($+{COND});
   2243  1.1  christos                 } else {
   2244  1.1  christos                     push @skip, -1;
   2245  1.1  christos                 }
   2246  1.1  christos             },
   2247  1.1  christos             qr/^\s* ELSIF ${cond_re} \s*$/x
   2248  1.1  christos             => sub { die "ELSIF out of scope" if ! @skip;
   2249  1.1  christos                      die "ELSIF following ELSE" if abs($skip[$#skip]) == 2;
   2250  1.1  christos                      $skip[$#skip] = -1 if $skip[$#skip] != 0;
   2251  1.1  christos                      $skip[$#skip] = !! $expand_variables->($+{COND})
   2252  1.1  christos                          if $skip[$#skip] == 0; },
   2253  1.1  christos             qr/^\s* ELSE \s*$/x
   2254  1.1  christos             => sub { die "ELSE out of scope" if ! @skip;
   2255  1.1  christos                      $skip[$#skip] = -2 if $skip[$#skip] != 0;
   2256  1.1  christos                      $skip[$#skip] = 2 if $skip[$#skip] == 0; },
   2257  1.1  christos             qr/^\s* ENDIF \s*$/x
   2258  1.1  christos             => sub { die "ENDIF out of scope" if ! @skip;
   2259  1.1  christos                      pop @skip; },
   2260  1.1  christos             qr/^\s* ${variable_re} \s* = \s* ${value_re} \s* $/x
   2261  1.1  christos             => sub {
   2262  1.1  christos                 if (!@skip || $skip[$#skip] > 0) {
   2263  1.1  christos                     $variables{$+{VARIABLE}} = $expand_variables->($+{VALUE});
   2264  1.1  christos                 }
   2265  1.1  christos             },
   2266  1.1  christos             qr/^\s* SUBDIRS \s* = \s* ${value_re} \s* $/x
   2267  1.1  christos             => sub {
   2268  1.1  christos                 if (!@skip || $skip[$#skip] > 0) {
   2269  1.1  christos                     foreach (tokenize($expand_variables->($+{VALUE}))) {
   2270  1.1  christos                         push @build_dirs, [ @curd, splitdir($_, 1) ];
   2271  1.1  christos                     }
   2272  1.1  christos                 }
   2273  1.1  christos             },
   2274  1.1  christos             qr/^\s* PROGRAMS ${attribs_re} \s* =  \s* ${value_re} \s* $/x
   2275  1.1  christos             => sub { $push_to->(\@programs, undef,
   2276  1.1  christos                                 \$attributes{programs}, $+{ATTRIBS},
   2277  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2278  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2279  1.1  christos             qr/^\s* LIBS ${attribs_re} \s* =  \s* ${value_re} \s* $/x
   2280  1.1  christos             => sub { $push_to->(\@libraries, undef,
   2281  1.1  christos                                 \$attributes{libraries}, $+{ATTRIBS},
   2282  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2283  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2284  1.1  christos             qr/^\s* MODULES ${attribs_re} \s* =  \s* ${value_re} \s* $/x
   2285  1.1  christos             => sub { $push_to->(\@modules, undef,
   2286  1.1  christos                                 \$attributes{modules}, $+{ATTRIBS},
   2287  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2288  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2289  1.1  christos             qr/^\s* SCRIPTS ${attribs_re} \s* = \s* ${value_re} \s* $/x
   2290  1.1  christos             => sub { $push_to->(\@scripts, undef,
   2291  1.1  christos                                 \$attributes{scripts}, $+{ATTRIBS},
   2292  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2293  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2294  1.1  christos             qr/^\s* IMAGEDOCS ${index_re} \s* = \s* ${value_re} \s* $/x
   2295  1.1  christos             => sub { $push_to->(\%imagedocs, $expand_variables->($+{INDEX}),
   2296  1.1  christos                                 undef, undef,
   2297  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2298  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2299  1.1  christos             qr/^\s* HTMLDOCS ${index_re} \s* = \s* ${value_re} \s* $/x
   2300  1.1  christos             => sub { $push_to->(\%htmldocs, $expand_variables->($+{INDEX}),
   2301  1.1  christos                                 undef, undef,
   2302  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2303  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2304  1.1  christos             qr/^\s* MANDOCS ${index_re} \s* = \s* ${value_re} \s* $/x
   2305  1.1  christos             => sub { $push_to->(\%mandocs, $expand_variables->($+{INDEX}),
   2306  1.1  christos                                 undef, undef,
   2307  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2308  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2309  1.1  christos             qr/^\s* SOURCE ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
   2310  1.1  christos             => sub { $push_to->(\%sources, $expand_variables->($+{INDEX}),
   2311  1.1  christos                                 \$attributes{sources}, $+{ATTRIBS},
   2312  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2313  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2314  1.1  christos             qr/^\s* SHARED_SOURCE ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
   2315  1.1  christos             => sub { $push_to->(\%shared_sources, $expand_variables->($+{INDEX}),
   2316  1.1  christos                                 \$attributes{sources}, $+{ATTRIBS},
   2317  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2318  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2319  1.1  christos             qr/^\s* INCLUDE ${index_re} \s* = \s* ${value_re} \s* $/x
   2320  1.1  christos             => sub { $push_to->(\%includes, $expand_variables->($+{INDEX}),
   2321  1.1  christos                                 undef, undef,
   2322  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2323  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2324  1.1  christos             qr/^\s* DEFINE ${index_re} \s* = \s* ${value_re} \s* $/x
   2325  1.1  christos             => sub { $push_to->(\%defines, $expand_variables->($+{INDEX}),
   2326  1.1  christos                                 undef, undef,
   2327  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2328  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2329  1.1  christos             qr/^\s* DEPEND ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
   2330  1.1  christos             => sub { $push_to->(\%depends, $expand_variables->($+{INDEX}),
   2331  1.1  christos                                 \$attributes{depends}, $+{ATTRIBS},
   2332  1.1  christos                                 tokenize($expand_variables->($+{VALUE})))
   2333  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2334  1.1  christos             qr/^\s* GENERATE ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
   2335  1.1  christos             => sub { $push_to->(\%generate, $expand_variables->($+{INDEX}),
   2336  1.1  christos                                 \$attributes{generate}, $+{ATTRIBS},
   2337  1.1  christos                                 $expand_variables->($+{VALUE}))
   2338  1.1  christos                          if !@skip || $skip[$#skip] > 0; },
   2339  1.1  christos             qr/^\s* (?:\#.*)? $/x => sub { },
   2340  1.1  christos             "OTHERWISE" => sub { die "Something wrong with this line:\n$_\nat $sourced/$f" },
   2341  1.1  christos             "BEFORE" => sub {
   2342  1.1  christos                 if ($buildinfo_debug) {
   2343  1.1  christos                     print STDERR "DEBUG: Parsing ",join(" ", @_),"\n";
   2344  1.1  christos                     print STDERR "DEBUG: ... before parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
   2345  1.1  christos                 }
   2346  1.1  christos             },
   2347  1.1  christos             "AFTER" => sub {
   2348  1.1  christos                 if ($buildinfo_debug) {
   2349  1.1  christos                     print STDERR "DEBUG: .... after parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
   2350  1.1  christos                 }
   2351  1.1  christos             },
   2352  1.1  christos             );
   2353  1.1  christos         die "runaway IF?" if (@skip);
   2354  1.1  christos 
   2355  1.1  christos         if (grep { defined $attributes{modules}->{$_}->{engine} } keys %attributes
   2356  1.1  christos                 and !$config{dynamic_engines}) {
   2357  1.1  christos             die <<"EOF"
   2358  1.1  christos ENGINES can only be used if configured with 'dynamic-engine'.
   2359  1.1  christos This is usually a fault in a build.info file.
   2360  1.1  christos EOF
   2361  1.1  christos         }
   2362  1.1  christos 
   2363  1.1  christos         {
   2364  1.1  christos             my %infos = ( programs  => [ @programs  ],
   2365  1.1  christos                           libraries => [ @libraries ],
   2366  1.1  christos                           modules   => [ @modules   ],
   2367  1.1  christos                           scripts   => [ @scripts   ] );
   2368  1.1  christos             foreach my $k (keys %infos) {
   2369  1.1  christos                 foreach (@{$infos{$k}}) {
   2370  1.1  christos                     my $item = cleanfile($buildd, $_, $blddir);
   2371  1.1  christos                     $unified_info{$k}->{$item} = 1;
   2372  1.1  christos 
   2373  1.1  christos                     # Fix up associated attributes
   2374  1.1  christos                     $unified_info{attributes}->{$k}->{$item} =
   2375  1.1  christos                         $attributes{$k}->{$_}
   2376  1.1  christos                         if defined $attributes{$k}->{$_};
   2377  1.1  christos                 }
   2378  1.1  christos             }
   2379  1.1  christos         }
   2380  1.1  christos 
   2381  1.1  christos         # Check that we haven't defined any library as both shared and
   2382  1.1  christos         # explicitly static.  That is forbidden.
   2383  1.1  christos         my @doubles = ();
   2384  1.1  christos         foreach (grep /\.a$/, keys %{$unified_info{libraries}}) {
   2385  1.1  christos             (my $l = $_) =~ s/\.a$//;
   2386  1.1  christos             push @doubles, $l if defined $unified_info{libraries}->{$l};
   2387  1.1  christos         }
   2388  1.1  christos         die "these libraries are both explicitly static and shared:\n  ",
   2389  1.1  christos             join(" ", @doubles), "\n"
   2390  1.1  christos             if @doubles;
   2391  1.1  christos 
   2392  1.1  christos         foreach (keys %sources) {
   2393  1.1  christos             my $dest = $_;
   2394  1.1  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   2395  1.1  christos             foreach (@{$sources{$dest}}) {
   2396  1.1  christos                 my $s = cleanfile($sourced, $_, $blddir, 1);
   2397  1.1  christos 
   2398  1.1  christos                 # If it's generated or we simply don't find it in the source
   2399  1.1  christos                 # tree, we assume it's in the build tree.
   2400  1.1  christos                 if ($s eq $src_configdata || $generate{$_} || ! -f $s) {
   2401  1.1  christos                     $s = cleanfile($buildd, $_, $blddir);
   2402  1.1  christos                 }
   2403  1.1  christos                 my $o = $_;
   2404  1.1  christos                 # We recognise C++, C and asm files
   2405  1.1  christos                 if ($s =~ /\.(cc|cpp|c|s|S)$/) {
   2406  1.1  christos                     push @{$check_exist{$s}}, $ddest;
   2407  1.1  christos                     $o =~ s/\.[csS]$/.o/; # C and assembler
   2408  1.1  christos                     $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
   2409  1.1  christos                     $o = cleanfile($buildd, $o, $blddir);
   2410  1.1  christos                     $unified_info{sources}->{$ddest}->{$o} = -1;
   2411  1.1  christos                     $unified_info{sources}->{$o}->{$s} = -1;
   2412  1.1  christos                 } elsif ($s =~ /\.rc$/) {
   2413  1.1  christos                     # We also recognise resource files
   2414  1.1  christos                     push @{$check_exist{$s}}, $ddest;
   2415  1.1  christos                     $o =~ s/\.rc$/.res/; # Resource configuration
   2416  1.1  christos                     $o = cleanfile($buildd, $o, $blddir);
   2417  1.1  christos                     $unified_info{sources}->{$ddest}->{$o} = -1;
   2418  1.1  christos                     $unified_info{sources}->{$o}->{$s} = -1;
   2419  1.1  christos                 } else {
   2420  1.1  christos                     push @{$check_exist{$s}}, $ddest;
   2421  1.1  christos                     $unified_info{sources}->{$ddest}->{$s} = 1;
   2422  1.1  christos                 }
   2423  1.1  christos                 # Fix up associated attributes
   2424  1.1  christos                 if ($o ne $_) {
   2425  1.1  christos                     $unified_info{attributes}->{sources}->{$ddest}->{$o} =
   2426  1.1  christos                         $unified_info{attributes}->{sources}->{$o}->{$s} =
   2427  1.1  christos                         $attributes{sources}->{$dest}->{$_}
   2428  1.1  christos                         if defined $attributes{sources}->{$dest}->{$_};
   2429  1.1  christos                 } else {
   2430  1.1  christos                     $unified_info{attributes}->{sources}->{$ddest}->{$s} =
   2431  1.1  christos                         $attributes{sources}->{$dest}->{$_}
   2432  1.1  christos                         if defined $attributes{sources}->{$dest}->{$_};
   2433  1.1  christos                 }
   2434  1.1  christos             }
   2435  1.1  christos         }
   2436  1.1  christos 
   2437  1.1  christos         foreach (keys %shared_sources) {
   2438  1.1  christos             my $dest = $_;
   2439  1.1  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   2440  1.1  christos             foreach (@{$shared_sources{$dest}}) {
   2441  1.1  christos                 my $s = cleanfile($sourced, $_, $blddir, 1);
   2442  1.1  christos 
   2443  1.1  christos                 # If it's generated or we simply don't find it in the source
   2444  1.1  christos                 # tree, we assume it's in the build tree.
   2445  1.1  christos                 if ($s eq $src_configdata || $generate{$_} || ! -f $s) {
   2446  1.1  christos                     $s = cleanfile($buildd, $_, $blddir);
   2447  1.1  christos                 }
   2448  1.1  christos 
   2449  1.1  christos                 my $o = $_;
   2450  1.1  christos                 if ($s =~ /\.(cc|cpp|c|s|S)$/) {
   2451  1.1  christos                     # We recognise C++, C and asm files
   2452  1.1  christos                     push @{$check_exist{$s}}, $ddest;
   2453  1.1  christos                     $o =~ s/\.[csS]$/.o/; # C and assembler
   2454  1.1  christos                     $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
   2455  1.1  christos                     $o = cleanfile($buildd, $o, $blddir);
   2456  1.1  christos                     $unified_info{shared_sources}->{$ddest}->{$o} = -1;
   2457  1.1  christos                     $unified_info{sources}->{$o}->{$s} = -1;
   2458  1.1  christos                 } elsif ($s =~ /\.rc$/) {
   2459  1.1  christos                     # We also recognise resource files
   2460  1.1  christos                     push @{$check_exist{$s}}, $ddest;
   2461  1.1  christos                     $o =~ s/\.rc$/.res/; # Resource configuration
   2462  1.1  christos                     $o = cleanfile($buildd, $o, $blddir);
   2463  1.1  christos                     $unified_info{shared_sources}->{$ddest}->{$o} = -1;
   2464  1.1  christos                     $unified_info{sources}->{$o}->{$s} = -1;
   2465  1.1  christos                 } elsif ($s =~ /\.ld$/) {
   2466  1.1  christos                     # We also recognise linker scripts (or corresponding)
   2467  1.1  christos                     # We know they are generated files
   2468  1.1  christos                     push @{$check_exist{$s}}, $ddest;
   2469  1.1  christos                     $o = cleanfile($buildd, $_, $blddir);
   2470  1.1  christos                     $unified_info{shared_sources}->{$ddest}->{$o} = 1;
   2471  1.1  christos                 } else {
   2472  1.1  christos                     die "unrecognised source file type for shared library: $s\n";
   2473  1.1  christos                 }
   2474  1.1  christos                 # Fix up associated attributes
   2475  1.1  christos                 if ($o ne $_) {
   2476  1.1  christos                     $unified_info{attributes}->{shared_sources}->{$ddest}->{$o} =
   2477  1.1  christos                         $unified_info{attributes}->{sources}->{$o}->{$s} =
   2478  1.1  christos                         $attributes{sources}->{$dest}->{$_}
   2479  1.1  christos                         if defined $attributes{sources}->{$dest}->{$_};
   2480  1.1  christos                 } else {
   2481  1.1  christos                     $unified_info{attributes}->{shared_sources}->{$ddest}->{$o} =
   2482  1.1  christos                         $attributes{sources}->{$dest}->{$_}
   2483  1.1  christos                         if defined $attributes{sources}->{$dest}->{$_};
   2484  1.1  christos                 }
   2485  1.1  christos             }
   2486  1.1  christos         }
   2487  1.1  christos 
   2488  1.1  christos         foreach (keys %generate) {
   2489  1.1  christos             my $dest = $_;
   2490  1.1  christos             my $ddest = cleanfile($buildd, $_, $blddir);
   2491  1.1  christos             die "more than one generator for $dest: "
   2492  1.1  christos                 ,join(" ", @{$generate{$_}}),"\n"
   2493  1.1  christos                 if scalar @{$generate{$_}} > 1;
   2494  1.1  christos             my @generator = split /\s+/, $generate{$dest}->[0];
   2495  1.1  christos             my $gen = $generator[0];
   2496  1.1  christos             $generator[0] = cleanfile($sourced, $gen, $blddir, 1);
   2497  1.1  christos 
   2498  1.1  christos             # If the generator is itself generated, it's in the build tree
   2499  1.1  christos             if ($generate{$gen} || ! -f $generator[0]) {
   2500  1.1  christos                 $generator[0] = cleanfile($buildd, $gen, $blddir);
   2501  1.1  christos             }
   2502  1.1  christos             $check_generate{$ddest}->{$generator[0]}++;
   2503  1.1  christos 
   2504  1.1  christos             $unified_info{generate}->{$ddest} = [ @generator ];
   2505  1.1  christos             # Fix up associated attributes
   2506  1.1  christos             $unified_info{attributes}->{generate}->{$ddest} =
   2507  1.1  christos                 $attributes{generate}->{$dest}->{$gen}
   2508  1.1  christos                 if defined $attributes{generate}->{$dest}->{$gen};
   2509  1.1  christos         }
   2510  1.1  christos 
   2511  1.1  christos         foreach (keys %depends) {
   2512  1.1  christos             my $dest = $_;
   2513  1.1  christos             my $ddest = $dest;
   2514  1.1  christos 
   2515  1.1  christos             if ($dest =~ /^\|(.*)\|$/) {
   2516  1.1  christos                 # Collect the raw target
   2517  1.1  christos                 $unified_info{targets}->{$1} = 1;
   2518  1.1  christos                 $ddest = $1;
   2519  1.1  christos             } elsif ($dest eq '') {
   2520  1.1  christos                 $ddest = '';
   2521  1.1  christos             } else {
   2522  1.1  christos                 $ddest = cleanfile($sourced, $dest, $blddir, 1);
   2523  1.1  christos 
   2524  1.1  christos                 # If the destination doesn't exist in source, it can only be
   2525  1.1  christos                 # a generated file in the build tree.
   2526  1.1  christos                 if ($ddest eq $src_configdata || ! -f $ddest) {
   2527  1.1  christos                     $ddest = cleanfile($buildd, $dest, $blddir);
   2528  1.1  christos                 }
   2529  1.1  christos             }
   2530  1.1  christos             foreach my $f (@{$depends{$dest}}) {
   2531  1.1  christos                 # If the dependency destination is generated, dependencies
   2532  1.1  christos                 # may have an extra syntax to separate the intended inclusion
   2533  1.1  christos                 # directory from the module to be loaded: a | instead of a
   2534  1.1  christos                 # / as directory separator.
   2535  1.1  christos                 # Do note that this has to be handled in the build file
   2536  1.1  christos                 # template as well.
   2537  1.1  christos                 # $i = inclusion path in source directory
   2538  1.1  christos                 # $i2 = inclusion path in build directory
   2539  1.1  christos                 # $m = module path (within the inclusion path)
   2540  1.1  christos                 # $i = full module path in source directory
   2541  1.1  christos                 # $i2 = full module path in build directory
   2542  1.1  christos                 my $i; my $i2; my $m; my $d; my $d2;
   2543  1.1  christos                 if ($unified_info{generate}->{$ddest}
   2544  1.1  christos                     && $f =~ m/^(.*?)\|(.*)$/) {
   2545  1.1  christos                     $i = $1;
   2546  1.1  christos                     $m = $2;
   2547  1.1  christos                     # We must be very careful to modify $i last
   2548  1.1  christos                     $d = cleanfile($sourced, "$i/$m", $blddir, 1);
   2549  1.1  christos                     $d2 = cleanfile($buildd, "$i/$m", $blddir);
   2550  1.1  christos                     $i2 = cleandir($buildd, $i, $blddir);
   2551  1.1  christos                     $i = cleandir($sourced, $i, $blddir, 1);
   2552  1.1  christos                 } else {
   2553  1.1  christos                     $d = cleanfile($sourced, $f, $blddir, 1);
   2554  1.1  christos                     $d2 = cleanfile($buildd, $f, $blddir);
   2555  1.1  christos                 }
   2556  1.1  christos 
   2557  1.1  christos                 # If we know it's generated, or assume it is because we can't
   2558  1.1  christos                 # find it in the source tree, we set file we depend on to be
   2559  1.1  christos                 # in the build tree rather than the source tree.
   2560  1.1  christos                 if ($d eq $src_configdata
   2561  1.1  christos                     || (grep { $d2 eq $_ }
   2562  1.1  christos                         keys %{$unified_info{generate}})
   2563  1.1  christos                     || ! -f $d) {
   2564  1.1  christos                     $d = $d2;
   2565  1.1  christos                     $i = $i2;
   2566  1.1  christos                 }
   2567  1.1  christos                 if ($i) {
   2568  1.1  christos                     # Put together the computed inclusion dir with the
   2569  1.1  christos                     # original module name.  Do note that we conserve the
   2570  1.1  christos                     # Unixly path syntax for the module path.
   2571  1.1  christos                     $d = "$i|$m";
   2572  1.1  christos                 }
   2573  1.1  christos                 $unified_info{depends}->{$ddest}->{$d} = 1;
   2574  1.1  christos 
   2575  1.1  christos                 # Fix up associated attributes
   2576  1.1  christos                 $unified_info{attributes}->{depends}->{$ddest}->{$d} =
   2577  1.1  christos                     $attributes{depends}->{$dest}->{$f}
   2578  1.1  christos                     if defined $attributes{depends}->{$dest}->{$f};
   2579  1.1  christos             }
   2580  1.1  christos         }
   2581  1.1  christos 
   2582  1.1  christos         foreach (keys %includes) {
   2583  1.1  christos             my $dest = $_;
   2584  1.1  christos             my $ddest = cleanfile($sourced, $_, $blddir, 1);
   2585  1.1  christos 
   2586  1.1  christos             # If the destination doesn't exist in source, it can only be
   2587  1.1  christos             # a generated file in the build tree.
   2588  1.1  christos             if ($ddest eq $src_configdata || ! -f $ddest) {
   2589  1.1  christos                 $ddest = cleanfile($buildd, $_, $blddir);
   2590  1.1  christos             }
   2591  1.1  christos             foreach (@{$includes{$dest}}) {
   2592  1.1  christos                 my $is = cleandir($sourced, $_, $blddir, 1);
   2593  1.1  christos                 my $ib = cleandir($buildd, $_, $blddir);
   2594  1.1  christos                 push @{$unified_info{includes}->{$ddest}->{source}}, $is
   2595  1.1  christos                     unless grep { $_ eq $is } @{$unified_info{includes}->{$ddest}->{source}};
   2596  1.1  christos                 push @{$unified_info{includes}->{$ddest}->{build}}, $ib
   2597  1.1  christos                     unless grep { $_ eq $ib } @{$unified_info{includes}->{$ddest}->{build}};
   2598  1.1  christos             }
   2599  1.1  christos         }
   2600  1.1  christos 
   2601  1.1  christos         foreach my $dest (keys %defines) {
   2602  1.1  christos             my $ddest;
   2603  1.1  christos 
   2604  1.1  christos             if ($dest ne "") {
   2605  1.1  christos                 $ddest = cleanfile($sourced, $dest, $blddir, 1);
   2606  1.1  christos 
   2607  1.1  christos                 # If the destination doesn't exist in source, it can only
   2608  1.1  christos                 # be a generated file in the build tree.
   2609  1.1  christos                 if (! -f $ddest) {
   2610  1.1  christos                     $ddest = cleanfile($buildd, $dest, $blddir);
   2611  1.1  christos                 }
   2612  1.1  christos             }
   2613  1.1  christos             foreach my $v (@{$defines{$dest}}) {
   2614  1.1  christos                 $v =~ m|^([^=]*)(=.*)?$|;
   2615  1.1  christos                 die "0 length macro name not permitted\n" if $1 eq "";
   2616  1.1  christos                 if ($dest ne "") {
   2617  1.1  christos                     die "$1 defined more than once\n"
   2618  1.1  christos                         if defined $unified_info{defines}->{$ddest}->{$1};
   2619  1.1  christos                     $unified_info{defines}->{$ddest}->{$1} = $2;
   2620  1.1  christos                 } else {
   2621  1.1  christos                     die "$1 defined more than once\n"
   2622  1.1  christos                         if grep { $v eq $_ } @{$config{defines}};
   2623  1.1  christos                     push @{$config{defines}}, $v;
   2624  1.1  christos                 }
   2625  1.1  christos             }
   2626  1.1  christos         }
   2627  1.1  christos 
   2628  1.1  christos         foreach my $section (keys %imagedocs) {
   2629  1.1  christos             foreach (@{$imagedocs{$section}}) {
   2630  1.1  christos                 my $imagedocs = cleanfile($buildd, $_, $blddir);
   2631  1.1  christos                 $unified_info{imagedocs}->{$section}->{$imagedocs} = 1;
   2632  1.1  christos             }
   2633  1.1  christos         }
   2634  1.1  christos 
   2635  1.1  christos         foreach my $section (keys %htmldocs) {
   2636  1.1  christos             foreach (@{$htmldocs{$section}}) {
   2637  1.1  christos                 my $htmldocs = cleanfile($buildd, $_, $blddir);
   2638  1.1  christos                 $unified_info{htmldocs}->{$section}->{$htmldocs} = 1;
   2639  1.1  christos             }
   2640  1.1  christos         }
   2641  1.1  christos 
   2642  1.1  christos         foreach my $section (keys %mandocs) {
   2643  1.1  christos             foreach (@{$mandocs{$section}}) {
   2644  1.1  christos                 my $mandocs = cleanfile($buildd, $_, $blddir);
   2645  1.1  christos                 $unified_info{mandocs}->{$section}->{$mandocs} = 1;
   2646  1.1  christos             }
   2647  1.1  christos         }
   2648  1.1  christos     }
   2649  1.1  christos 
   2650  1.1  christos     my $ordinals_text = join(', ', sort keys %ordinals);
   2651  1.1  christos     warn <<"EOF" if $ordinals_text;
   2652  1.1  christos 
   2653  1.1  christos WARNING: ORDINALS were specified for $ordinals_text
   2654  1.1  christos They are ignored and should be replaced with a combination of GENERATE,
   2655  1.1  christos DEPEND and SHARED_SOURCE.
   2656  1.1  christos EOF
   2657  1.1  christos 
   2658  1.1  christos     # Check that each generated file is only generated once
   2659  1.1  christos     my $ambiguous_generation = 0;
   2660  1.1  christos     foreach (sort keys %check_generate) {
   2661  1.1  christos         my @generators = sort keys %{$check_generate{$_}};
   2662  1.1  christos         my $generators_txt = join(', ', @generators);
   2663  1.1  christos         if (scalar @generators > 1) {
   2664  1.1  christos             warn "$_ is GENERATEd by more than one generator ($generators_txt)\n";
   2665  1.1  christos             $ambiguous_generation++;
   2666  1.1  christos         }
   2667  1.1  christos         if ($check_generate{$_}->{$generators[0]} > 1) {
   2668  1.1  christos             warn "INFO: $_ has more than one GENERATE declaration (same generator)\n"
   2669  1.1  christos         }
   2670  1.1  christos     }
   2671  1.1  christos     die "There are ambiguous source file generations\n"
   2672  1.1  christos         if $ambiguous_generation > 0;
   2673  1.1  christos 
   2674  1.1  christos     # All given source files should exist, or if generated, their
   2675  1.1  christos     # generator should exist.  This loop ensures this is true.
   2676  1.1  christos     my $missing = 0;
   2677  1.1  christos     foreach my $orig (sort keys %check_exist) {
   2678  1.1  christos         foreach my $dest (@{$check_exist{$orig}}) {
   2679  1.1  christos             if ($orig ne $src_configdata) {
   2680  1.1  christos                 if ($orig =~ /\.a$/) {
   2681  1.1  christos                     # Static library names may be used as sources, so we
   2682  1.1  christos                     # need to detect those and give them special treatment.
   2683  1.1  christos                     unless (grep { $_ eq $orig }
   2684  1.1  christos                             keys %{$unified_info{libraries}}) {
   2685  1.1  christos                         warn "$orig is given as source for $dest, but no such library is built\n";
   2686  1.1  christos                         $missing++;
   2687  1.1  christos                     }
   2688  1.1  christos                 } else {
   2689  1.1  christos                     # A source may be generated, and its generator may be
   2690  1.1  christos                     # generated as well.  We therefore loop to dig out the
   2691  1.1  christos                     # first generator.
   2692  1.1  christos                     my $gen = $orig;
   2693  1.1  christos 
   2694  1.1  christos                     while (my @next = keys %{$check_generate{$gen}}) {
   2695  1.1  christos                         $gen = $next[0];
   2696  1.1  christos                     }
   2697  1.1  christos 
   2698  1.1  christos                     if (! -f $gen) {
   2699  1.1  christos                         if ($gen ne $orig) {
   2700  1.1  christos                             $missing++;
   2701  1.1  christos                             warn "$orig is given as source for $dest, but its generator (leading to $gen) is missing\n";
   2702  1.1  christos                         } else {
   2703  1.1  christos                             $missing++;
   2704  1.1  christos                             warn "$orig is given as source for $dest, but is missing\n";
   2705  1.1  christos                         }
   2706  1.1  christos                     }
   2707  1.1  christos                 }
   2708  1.1  christos             }
   2709  1.1  christos         }
   2710  1.1  christos     }
   2711  1.1  christos     die "There are files missing\n" if $missing > 0;
   2712  1.1  christos 
   2713  1.1  christos     # Go through the sources of all libraries and check that the same basename
   2714  1.1  christos     # doesn't appear more than once.  Some static library archivers depend on
   2715  1.1  christos     # them being unique.
   2716  1.1  christos     {
   2717  1.1  christos         my $err = 0;
   2718  1.1  christos         foreach my $prod (keys %{$unified_info{libraries}}) {
   2719  1.1  christos             my @prod_sources =
   2720  1.1  christos                 map { keys %{$unified_info{sources}->{$_}} }
   2721  1.1  christos                 keys %{$unified_info{sources}->{$prod}};
   2722  1.1  christos             my %srccnt = ();
   2723  1.1  christos 
   2724  1.1  christos             # Count how many times a given each source basename
   2725  1.1  christos             # appears for each product.
   2726  1.1  christos             foreach my $src (@prod_sources) {
   2727  1.1  christos                 $srccnt{basename $src}++;
   2728  1.1  christos             }
   2729  1.1  christos 
   2730  1.1  christos             foreach my $src (keys %srccnt) {
   2731  1.1  christos                 if ((my $cnt = $srccnt{$src}) > 1) {
   2732  1.1  christos                     print STDERR "$src appears $cnt times for the product $prod\n";
   2733  1.1  christos                     $err++
   2734  1.1  christos                 }
   2735  1.1  christos             }
   2736  1.1  christos         }
   2737  1.1  christos         die if $err > 0;
   2738  1.1  christos     }
   2739  1.1  christos 
   2740  1.1  christos     # Massage the result
   2741  1.1  christos 
   2742  1.1  christos     # If we depend on a header file or a perl module, add an inclusion of
   2743  1.1  christos     # its directory to allow smoothe inclusion
   2744  1.1  christos     foreach my $dest (keys %{$unified_info{depends}}) {
   2745  1.1  christos         next if $dest eq "";
   2746  1.1  christos         foreach my $d (keys %{$unified_info{depends}->{$dest}}) {
   2747  1.1  christos             next unless $d =~ /\.(h|pm)$/;
   2748  1.1  christos             # Take into account when a dependency uses the inclusion|module
   2749  1.1  christos             # syntax
   2750  1.1  christos             my $i = $d =~ m/\|/ ? $` : dirname($d);
   2751  1.1  christos             my $spot =
   2752  1.1  christos                 $d eq "configdata.pm" || defined($unified_info{generate}->{$d})
   2753  1.1  christos                 ? 'build' : 'source';
   2754  1.1  christos             push @{$unified_info{includes}->{$dest}->{$spot}}, $i
   2755  1.1  christos                 unless grep { $_ eq $i } @{$unified_info{includes}->{$dest}->{$spot}};
   2756  1.1  christos         }
   2757  1.1  christos     }
   2758  1.1  christos 
   2759  1.1  christos     # Go through all intermediary files and change their names to something that
   2760  1.1  christos     # reflects what they will be built for.  Note that for some source files,
   2761  1.1  christos     # this leads to duplicate object files because they are used multiple times.
   2762  1.1  christos     # the goal is to rename all object files according to this scheme:
   2763  1.1  christos     #    {productname}-{midfix}-{origobjname}.[o|res]
   2764  1.1  christos     # the {midfix} is a keyword indicating the type of product, which is mostly
   2765  1.1  christos     # valuable for libraries since they come in two forms.
   2766  1.1  christos     #
   2767  1.1  christos     # This also reorganises the {sources} and {shared_sources} so that the
   2768  1.1  christos     # former only contains ALL object files that are supposed to end up in
   2769  1.1  christos     # static libraries and programs, while the latter contains ALL object files
   2770  1.1  christos     # that are supposed to end up in shared libraries and DSOs.
   2771  1.1  christos     # The main reason for having two different source structures is to allow
   2772  1.1  christos     # the same name to be used for the static and the shared variants of a
   2773  1.1  christos     # library.
   2774  1.1  christos     {
   2775  1.1  christos         # Take copies so we don't get interference from added stuff
   2776  1.1  christos         my %unified_copy = ();
   2777  1.1  christos         foreach (('sources', 'shared_sources')) {
   2778  1.1  christos             $unified_copy{$_} = { %{$unified_info{$_}} }
   2779  1.1  christos                 if defined($unified_info{$_});
   2780  1.1  christos             delete $unified_info{$_};
   2781  1.1  christos         }
   2782  1.1  christos         foreach my $prodtype (('programs', 'libraries', 'modules', 'scripts')) {
   2783  1.1  christos             # $intent serves multi purposes:
   2784  1.1  christos             # - give a prefix for the new object files names
   2785  1.1  christos             # - in the case of libraries, rearrange the object files so static
   2786  1.1  christos             #   libraries use the 'sources' structure exclusively, while shared
   2787  1.1  christos             #   libraries use the 'shared_sources' structure exclusively.
   2788  1.1  christos             my $intent = {
   2789  1.1  christos                 programs  => { bin    => { src => [ 'sources' ],
   2790  1.1  christos                                            dst => 'sources' } },
   2791  1.1  christos                 libraries => { lib    => { src => [ 'sources' ],
   2792  1.1  christos                                            dst => 'sources' },
   2793  1.1  christos                                shlib  => { prodselect =>
   2794  1.1  christos                                                sub { grep !/\.a$/, @_ },
   2795  1.1  christos                                            src => [ 'sources',
   2796  1.1  christos                                                     'shared_sources' ],
   2797  1.1  christos                                            dst => 'shared_sources' } },
   2798  1.1  christos                 modules   => { dso    => { src => [ 'sources' ],
   2799  1.1  christos                                            dst => 'sources' } },
   2800  1.1  christos                 scripts   => { script => { src => [ 'sources' ],
   2801  1.1  christos                                            dst => 'sources' } }
   2802  1.1  christos                } -> {$prodtype};
   2803  1.1  christos             foreach my $kind (keys %$intent) {
   2804  1.1  christos                 next if ($intent->{$kind}->{dst} eq 'shared_sources'
   2805  1.1  christos                              && $disabled{shared});
   2806  1.1  christos 
   2807  1.1  christos                 my @src = @{$intent->{$kind}->{src}};
   2808  1.1  christos                 my $dst = $intent->{$kind}->{dst};
   2809  1.1  christos                 my $prodselect = $intent->{$kind}->{prodselect} // sub { @_ };
   2810  1.1  christos                 foreach my $prod ($prodselect->(keys %{$unified_info{$prodtype}})) {
   2811  1.1  christos                     # %prod_sources has all applicable objects as keys, and
   2812  1.1  christos                     # their corresponding sources as values
   2813  1.1  christos                     my %prod_sources =
   2814  1.1  christos                         map { $_ => [ keys %{$unified_copy{sources}->{$_}} ] }
   2815  1.1  christos                         map { keys %{$unified_copy{$_}->{$prod}} }
   2816  1.1  christos                         @src;
   2817  1.1  christos                     foreach (keys %prod_sources) {
   2818  1.1  christos                         # Only affect object files and resource files,
   2819  1.1  christos                         # the others simply get a new value
   2820  1.1  christos                         # (+1 instead of -1)
   2821  1.1  christos                         if ($_ =~ /\.(o|res)$/) {
   2822  1.1  christos                             (my $prodname = $prod) =~ s|\.a$||;
   2823  1.1  christos                             my $newobj =
   2824  1.1  christos                                 catfile(dirname($_),
   2825  1.1  christos                                         basename($prodname)
   2826  1.1  christos                                             . '-' . $kind
   2827  1.1  christos                                             . '-' . basename($_));
   2828  1.1  christos                             $unified_info{$dst}->{$prod}->{$newobj} = 1;
   2829  1.1  christos                             foreach my $src (@{$prod_sources{$_}}) {
   2830  1.1  christos                                 $unified_info{sources}->{$newobj}->{$src} = 1;
   2831  1.1  christos                                 # Adjust source attributes
   2832  1.1  christos                                 my $attrs = $unified_info{attributes}->{sources};
   2833  1.1  christos                                 if (defined $attrs->{$prod}
   2834  1.1  christos                                     && defined $attrs->{$prod}->{$_}) {
   2835  1.1  christos                                     $attrs->{$prod}->{$newobj} =
   2836  1.1  christos                                         $attrs->{$prod}->{$_};
   2837  1.1  christos                                     delete $attrs->{$prod}->{$_};
   2838  1.1  christos                                 }
   2839  1.1  christos                                 foreach my $objsrc (keys %{$attrs->{$_} // {}}) {
   2840  1.1  christos                                     $attrs->{$newobj}->{$objsrc} =
   2841  1.1  christos                                         $attrs->{$_}->{$objsrc};
   2842  1.1  christos                                     delete $attrs->{$_}->{$objsrc};
   2843  1.1  christos                                 }
   2844  1.1  christos                             }
   2845  1.1  christos                             # Adjust dependencies
   2846  1.1  christos                             foreach my $deps (keys %{$unified_info{depends}->{$_}}) {
   2847  1.1  christos                                 $unified_info{depends}->{$_}->{$deps} = -1;
   2848  1.1  christos                                 $unified_info{depends}->{$newobj}->{$deps} = 1;
   2849  1.1  christos                             }
   2850  1.1  christos                             # Adjust includes
   2851  1.1  christos                             foreach my $k (('source', 'build')) {
   2852  1.1  christos                                 next unless
   2853  1.1  christos                                     defined($unified_info{includes}->{$_}->{$k});
   2854  1.1  christos                                 my @incs = @{$unified_info{includes}->{$_}->{$k}};
   2855  1.1  christos                                 $unified_info{includes}->{$newobj}->{$k} = [ @incs ];
   2856  1.1  christos                             }
   2857  1.1  christos                         } else {
   2858  1.1  christos                             $unified_info{$dst}->{$prod}->{$_} = 1;
   2859  1.1  christos                         }
   2860  1.1  christos                     }
   2861  1.1  christos                 }
   2862  1.1  christos             }
   2863  1.1  christos         }
   2864  1.1  christos     }
   2865  1.1  christos 
   2866  1.1  christos     # At this point, we have a number of sources with the value -1.  They
   2867  1.1  christos     # aren't part of the local build and are probably meant for a different
   2868  1.1  christos     # platform, and can therefore be cleaned away.  That happens when making
   2869  1.1  christos     # %unified_info more efficient below.
   2870  1.1  christos 
   2871  1.1  christos     ### Make unified_info a bit more efficient
   2872  1.1  christos     # One level structures
   2873  1.1  christos     foreach (("programs", "libraries", "modules", "scripts", "targets")) {
   2874  1.1  christos         $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
   2875  1.1  christos     }
   2876  1.1  christos     # Two level structures
   2877  1.1  christos     foreach my $l1 (("sources", "shared_sources", "ldadd", "depends",
   2878  1.1  christos                      "imagedocs", "htmldocs", "mandocs")) {
   2879  1.1  christos         foreach my $l2 (sort keys %{$unified_info{$l1}}) {
   2880  1.1  christos             my @items =
   2881  1.1  christos                 sort
   2882  1.1  christos                 grep { $unified_info{$l1}->{$l2}->{$_} > 0 }
   2883  1.1  christos                 keys %{$unified_info{$l1}->{$l2}};
   2884  1.1  christos             if (@items) {
   2885  1.1  christos                 $unified_info{$l1}->{$l2} = [ @items ];
   2886  1.1  christos             } else {
   2887  1.1  christos                 delete $unified_info{$l1}->{$l2};
   2888  1.1  christos             }
   2889  1.1  christos         }
   2890  1.1  christos     }
   2891  1.1  christos     # Defines
   2892  1.1  christos     foreach my $dest (sort keys %{$unified_info{defines}}) {
   2893  1.1  christos         $unified_info{defines}->{$dest}
   2894  1.1  christos             = [ map { $_.$unified_info{defines}->{$dest}->{$_} }
   2895  1.1  christos                 sort keys %{$unified_info{defines}->{$dest}} ];
   2896  1.1  christos     }
   2897  1.1  christos     # Includes
   2898  1.1  christos     foreach my $dest (sort keys %{$unified_info{includes}}) {
   2899  1.1  christos         if (defined($unified_info{includes}->{$dest}->{build})) {
   2900  1.1  christos             my @source_includes = ();
   2901  1.1  christos             @source_includes = ( @{$unified_info{includes}->{$dest}->{source}} )
   2902  1.1  christos                 if defined($unified_info{includes}->{$dest}->{source});
   2903  1.1  christos             $unified_info{includes}->{$dest} =
   2904  1.1  christos                 [ @{$unified_info{includes}->{$dest}->{build}} ];
   2905  1.1  christos             foreach my $inc (@source_includes) {
   2906  1.1  christos                 push @{$unified_info{includes}->{$dest}}, $inc
   2907  1.1  christos                     unless grep { $_ eq $inc } @{$unified_info{includes}->{$dest}};
   2908  1.1  christos             }
   2909  1.1  christos         } elsif (defined($unified_info{includes}->{$dest}->{source})) {
   2910  1.1  christos             $unified_info{includes}->{$dest} =
   2911  1.1  christos                 [ @{$unified_info{includes}->{$dest}->{source}} ];
   2912  1.1  christos         } else {
   2913  1.1  christos             delete $unified_info{includes}->{$dest};
   2914  1.1  christos         }
   2915  1.1  christos     }
   2916  1.1  christos 
   2917  1.1  christos     # For convenience collect information regarding directories where
   2918  1.1  christos     # files are generated, those generated files and the end product
   2919  1.1  christos     # they end up in where applicable.  Then, add build rules for those
   2920  1.1  christos     # directories
   2921  1.1  christos     my %loopinfo = ( "lib" => [ @{$unified_info{libraries}} ],
   2922  1.1  christos                      "dso" => [ @{$unified_info{modules}} ],
   2923  1.1  christos                      "bin" => [ @{$unified_info{programs}} ],
   2924  1.1  christos                      "script" => [ @{$unified_info{scripts}} ],
   2925  1.1  christos                      "docs" => [ (map { @{$unified_info{imagedocs}->{$_} // []} }
   2926  1.1  christos                                   keys %{$unified_info{imagedocs} // {}}),
   2927  1.1  christos                                  (map { @{$unified_info{htmldocs}->{$_} // []} }
   2928  1.1  christos                                   keys %{$unified_info{htmldocs} // {}}),
   2929  1.1  christos                                  (map { @{$unified_info{mandocs}->{$_} // []} }
   2930  1.1  christos                                   keys %{$unified_info{mandocs} // {}}) ] );
   2931  1.1  christos     foreach my $type (sort keys %loopinfo) {
   2932  1.1  christos         foreach my $product (@{$loopinfo{$type}}) {
   2933  1.1  christos             my %dirs = ();
   2934  1.1  christos             my $pd = dirname($product);
   2935  1.1  christos 
   2936  1.1  christos             foreach (@{$unified_info{sources}->{$product} // []},
   2937  1.1  christos                      @{$unified_info{shared_sources}->{$product} // []}) {
   2938  1.1  christos                 my $d = dirname($_);
   2939  1.1  christos 
   2940  1.1  christos                 # We don't want to create targets for source directories
   2941  1.1  christos                 # when building out of source
   2942  1.1  christos                 next if ($config{sourcedir} ne $config{builddir}
   2943  1.1  christos                              && $d =~ m|^\Q$config{sourcedir}\E|);
   2944  1.1  christos                 # We already have a "test" target, and the current directory
   2945  1.1  christos                 # is just silly to make a target for
   2946  1.1  christos                 next if $d eq "test" || $d eq ".";
   2947  1.1  christos 
   2948  1.1  christos                 $dirs{$d} = 1;
   2949  1.1  christos                 push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
   2950  1.1  christos                     if $d ne $pd;
   2951  1.1  christos             }
   2952  1.1  christos             foreach (sort keys %dirs) {
   2953  1.1  christos                 push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
   2954  1.1  christos                     $product;
   2955  1.1  christos             }
   2956  1.1  christos         }
   2957  1.1  christos     }
   2958  1.1  christos }
   2959  1.1  christos 
   2960  1.1  christos # For the schemes that need it, we provide the old *_obj configs
   2961  1.1  christos # from the *_asm_obj ones
   2962  1.1  christos foreach (grep /_(asm|aux)_src$/, keys %target) {
   2963  1.1  christos     my $src = $_;
   2964  1.1  christos     (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
   2965  1.1  christos     $target{$obj} = $target{$src};
   2966  1.1  christos     $target{$obj} =~ s/\.[csS]\b/.o/g; # C and assembler
   2967  1.1  christos     $target{$obj} =~ s/\.(cc|cpp)\b/_cc.o/g; # C++
   2968  1.1  christos }
   2969  1.1  christos 
   2970  1.1  christos # Write down our configuration where it fits #########################
   2971  1.1  christos 
   2972  1.1  christos my %template_vars = (
   2973  1.1  christos     config => \%config,
   2974  1.1  christos     target => \%target,
   2975  1.1  christos     disablables => \@disablables,
   2976  1.1  christos     disablables_int => \@disablables_int,
   2977  1.1  christos     disabled => \%disabled,
   2978  1.1  christos     withargs => \%withargs,
   2979  1.1  christos     unified_info => \%unified_info,
   2980  1.1  christos     tls => \@tls,
   2981  1.1  christos     dtls => \@dtls,
   2982  1.1  christos     makevars => [ sort keys %user ],
   2983  1.1  christos     disabled_info => \%disabled_info,
   2984  1.1  christos     user_crossable => \@user_crossable,
   2985  1.1  christos );
   2986  1.1  christos my $configdata_outname = 'configdata.pm';
   2987  1.1  christos open CONFIGDATA, ">$configdata_outname.new"
   2988  1.1  christos     or die "Trying to create $configdata_outname.new: $!";
   2989  1.1  christos my $configdata_tmplname = cleanfile($srcdir, "configdata.pm.in", $blddir, 1);
   2990  1.1  christos my $configdata_tmpl =
   2991  1.1  christos     OpenSSL::Template->new(TYPE => 'FILE', SOURCE => $configdata_tmplname);
   2992  1.1  christos $configdata_tmpl->fill_in(
   2993  1.1  christos     FILENAME => $configdata_tmplname,
   2994  1.1  christos     OUTPUT => \*CONFIGDATA,
   2995  1.1  christos     HASH => { %template_vars,
   2996  1.1  christos               autowarntext => [
   2997  1.1  christos                   'WARNING: do not edit!',
   2998  1.1  christos                   "Generated by Configure from $configdata_tmplname",
   2999  1.1  christos               ] }
   3000  1.1  christos ) or die $Text::Template::ERROR;
   3001  1.1  christos close CONFIGDATA;
   3002  1.1  christos 
   3003  1.1  christos rename "$configdata_outname.new", $configdata_outname;
   3004  1.1  christos if ($builder_platform eq 'unix') {
   3005  1.1  christos     my $mode = (0755 & ~umask);
   3006  1.1  christos     chmod $mode, 'configdata.pm'
   3007  1.1  christos         or warn sprintf("WARNING: Couldn't change mode for 'configdata.pm' to 0%03o: %s\n",$mode,$!);
   3008  1.1  christos }
   3009  1.1  christos print "Created $configdata_outname\n";
   3010  1.1  christos 
   3011  1.1  christos print "Running $configdata_outname\n";
   3012  1.1  christos my $perlcmd = (quotify("maybeshell", $config{PERL}))[0];
   3013  1.1  christos my $cmd = "$perlcmd $configdata_outname";
   3014  1.1  christos #print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
   3015  1.1  christos system($cmd);
   3016  1.1  christos exit 1 if $? != 0;
   3017  1.1  christos 
   3018  1.1  christos $SIG{__DIE__} = $orig_death_handler;
   3019  1.1  christos 
   3020  1.1  christos print <<"EOF" if ($disabled{threads} eq "unavailable");
   3021  1.1  christos 
   3022  1.1  christos The library could not be configured for supporting multi-threaded
   3023  1.1  christos applications as the compiler options required on this system are not known.
   3024  1.1  christos See file INSTALL.md for details if you need multi-threading.
   3025  1.1  christos EOF
   3026  1.1  christos 
   3027  1.1  christos print <<"EOF" if ($no_shared_warn);
   3028  1.1  christos 
   3029  1.1  christos The options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
   3030  1.1  christos platform, so we will pretend you gave the option 'no-pic', which also disables
   3031  1.1  christos 'shared' and 'dynamic-engine'.  If you know how to implement shared libraries
   3032  1.1  christos or position independent code, please let us know (but please first make sure
   3033  1.1  christos you have tried with a current version of OpenSSL).
   3034  1.1  christos EOF
   3035  1.1  christos 
   3036  1.1  christos print $banner;
   3037  1.1  christos 
   3038  1.1  christos exit(0);
   3039  1.1  christos 
   3040  1.1  christos ######################################################################
   3041  1.1  christos #
   3042  1.1  christos # Helpers and utility functions
   3043  1.1  christos #
   3044  1.1  christos 
   3045  1.1  christos # Death handler, to print a helpful message in case of failure #######
   3046  1.1  christos #
   3047  1.1  christos sub death_handler {
   3048  1.1  christos     die @_ if $^S;              # To prevent the added message in eval blocks
   3049  1.1  christos     my $build_file = $config{build_file} // "build file";
   3050  1.1  christos     my @message = ( <<"_____", @_ );
   3051  1.1  christos 
   3052  1.1  christos Failure!  $build_file wasn't produced.
   3053  1.1  christos Please read INSTALL.md and associated NOTES-* files.  You may also have to
   3054  1.1  christos look over your available compiler tool chain or change your configuration.
   3055  1.1  christos 
   3056  1.1  christos _____
   3057  1.1  christos 
   3058  1.1  christos     # Dying is terminal, so it's ok to reset the signal handler here.
   3059  1.1  christos     $SIG{__DIE__} = $orig_death_handler;
   3060  1.1  christos     die @message;
   3061  1.1  christos }
   3062  1.1  christos 
   3063  1.1  christos # Configuration file reading #########################################
   3064  1.1  christos 
   3065  1.1  christos # Note: All of the helper functions are for lazy evaluation.  They all
   3066  1.1  christos # return a CODE ref, which will return the intended value when evaluated.
   3067  1.1  christos # Thus, whenever there's mention of a returned value, it's about that
   3068  1.1  christos # intended value.
   3069  1.1  christos 
   3070  1.1  christos # Helper function to implement conditional value variants, with a default
   3071  1.1  christos # plus additional values based on the value of $config{build_type}.
   3072  1.1  christos # Arguments are given in hash table form:
   3073  1.1  christos #
   3074  1.1  christos #       picker(default => "Basic string: ",
   3075  1.1  christos #              debug   => "debug",
   3076  1.1  christos #              release => "release")
   3077  1.1  christos #
   3078  1.1  christos # When configuring with --debug, the resulting string will be
   3079  1.1  christos # "Basic string: debug", and when not, it will be "Basic string: release"
   3080  1.1  christos #
   3081  1.1  christos # This can be used to create variants of sets of flags according to the
   3082  1.1  christos # build type:
   3083  1.1  christos #
   3084  1.1  christos #       cflags => picker(default => "-Wall",
   3085  1.1  christos #                        debug   => "-g -O0",
   3086  1.1  christos #                        release => "-O3")
   3087  1.1  christos #
   3088  1.1  christos sub picker {
   3089  1.1  christos     my %opts = @_;
   3090  1.1  christos     return sub { add($opts{default} || (),
   3091  1.1  christos                      $opts{$config{build_type}} || ())->(); }
   3092  1.1  christos }
   3093  1.1  christos 
   3094  1.1  christos # Helper function to combine several values of different types into one.
   3095  1.1  christos # This is useful if you want to combine a string with the result of a
   3096  1.1  christos # lazy function, such as:
   3097  1.1  christos #
   3098  1.1  christos #       cflags => combine("-Wall", sub { $disabled{zlib} ? () : "-DZLIB" })
   3099  1.1  christos #
   3100  1.1  christos sub combine {
   3101  1.1  christos     my @stuff = @_;
   3102  1.1  christos     return sub { add(@stuff)->(); }
   3103  1.1  christos }
   3104  1.1  christos 
   3105  1.1  christos # Helper function to implement conditional values depending on the value
   3106  1.1  christos # of $disabled{threads}.  Can be used as follows:
   3107  1.1  christos #
   3108  1.1  christos #       cflags => combine("-Wall", threads("-pthread"))
   3109  1.1  christos #
   3110  1.1  christos sub threads {
   3111  1.1  christos     my @flags = @_;
   3112  1.1  christos     return sub { add($disabled{threads} ? () : @flags)->(); }
   3113  1.1  christos }
   3114  1.1  christos 
   3115  1.1  christos sub shared {
   3116  1.1  christos     my @flags = @_;
   3117  1.1  christos     return sub { add($disabled{shared} ? () : @flags)->(); }
   3118  1.1  christos }
   3119  1.1  christos 
   3120  1.1  christos our $add_called = 0;
   3121  1.1  christos # Helper function to implement adding values to already existing configuration
   3122  1.1  christos # values.  It handles elements that are ARRAYs, CODEs and scalars
   3123  1.1  christos sub _add {
   3124  1.1  christos     my $separator = shift;
   3125  1.1  christos 
   3126  1.1  christos     # If there's any ARRAY in the collection of values OR the separator
   3127  1.1  christos     # is undef, we will return an ARRAY of combined values, otherwise a
   3128  1.1  christos     # string of joined values with $separator as the separator.
   3129  1.1  christos     my $found_array = !defined($separator);
   3130  1.1  christos 
   3131  1.1  christos     my @values =
   3132  1.1  christos         map {
   3133  1.1  christos             my $res = $_;
   3134  1.1  christos             while (ref($res) eq "CODE") {
   3135  1.1  christos                 $res = $res->();
   3136  1.1  christos             }
   3137  1.1  christos             if (defined($res)) {
   3138  1.1  christos                 if (ref($res) eq "ARRAY") {
   3139  1.1  christos                     $found_array = 1;
   3140  1.1  christos                     @$res;
   3141  1.1  christos                 } else {
   3142  1.1  christos                     $res;
   3143  1.1  christos                 }
   3144  1.1  christos             } else {
   3145  1.1  christos                 ();
   3146  1.1  christos             }
   3147  1.1  christos     } (@_);
   3148  1.1  christos 
   3149  1.1  christos     $add_called = 1;
   3150  1.1  christos 
   3151  1.1  christos     if ($found_array) {
   3152  1.1  christos         [ @values ];
   3153  1.1  christos     } else {
   3154  1.1  christos         join($separator, grep { defined($_) && $_ ne "" } @values);
   3155  1.1  christos     }
   3156  1.1  christos }
   3157  1.1  christos sub add_before {
   3158  1.1  christos     my $separator = " ";
   3159  1.1  christos     if (ref($_[$#_]) eq "HASH") {
   3160  1.1  christos         my $opts = pop;
   3161  1.1  christos         $separator = $opts->{separator};
   3162  1.1  christos     }
   3163  1.1  christos     my @x = @_;
   3164  1.1  christos     sub { _add($separator, @x, @_) };
   3165  1.1  christos }
   3166  1.1  christos sub add {
   3167  1.1  christos     my $separator = " ";
   3168  1.1  christos     if (ref($_[$#_]) eq "HASH") {
   3169  1.1  christos         my $opts = pop;
   3170  1.1  christos         $separator = $opts->{separator};
   3171  1.1  christos     }
   3172  1.1  christos     my @x = @_;
   3173  1.1  christos     sub { _add($separator, @_, @x) };
   3174  1.1  christos }
   3175  1.1  christos 
   3176  1.1  christos sub read_eval_file {
   3177  1.1  christos     my $fname = shift;
   3178  1.1  christos     my $content;
   3179  1.1  christos     my @result;
   3180  1.1  christos 
   3181  1.1  christos     open F, "< $fname" or die "Can't open '$fname': $!\n";
   3182  1.1  christos     {
   3183  1.1  christos         undef local $/;
   3184  1.1  christos         $content = <F>;
   3185  1.1  christos     }
   3186  1.1  christos     close F;
   3187  1.1  christos     {
   3188  1.1  christos         local $@;
   3189  1.1  christos 
   3190  1.1  christos         @result = ( eval $content );
   3191  1.1  christos         warn $@ if $@;
   3192  1.1  christos     }
   3193  1.1  christos     return wantarray ? @result : $result[0];
   3194  1.1  christos }
   3195  1.1  christos 
   3196  1.1  christos # configuration reader, evaluates the input file as a perl script and expects
   3197  1.1  christos # it to fill %targets with target configurations.  Those are then added to
   3198  1.1  christos # %table.
   3199  1.1  christos sub read_config {
   3200  1.1  christos     my $fname = shift;
   3201  1.1  christos     my %targets;
   3202  1.1  christos 
   3203  1.1  christos     {
   3204  1.1  christos         # Protect certain tables from tampering
   3205  1.1  christos         local %table = ();
   3206  1.1  christos 
   3207  1.1  christos         %targets = read_eval_file($fname);
   3208  1.1  christos     }
   3209  1.1  christos     my %preexisting = ();
   3210  1.1  christos     foreach (sort keys %targets) {
   3211  1.1  christos         $preexisting{$_} = 1 if $table{$_};
   3212  1.1  christos     }
   3213  1.1  christos     die <<"EOF",
   3214  1.1  christos The following config targets from $fname
   3215  1.1  christos shadow pre-existing config targets with the same name:
   3216  1.1  christos EOF
   3217  1.1  christos         map { "  $_\n" } sort keys %preexisting
   3218  1.1  christos         if %preexisting;
   3219  1.1  christos 
   3220  1.1  christos 
   3221  1.1  christos     # For each target, check that it's configured with a hash table.
   3222  1.1  christos     foreach (keys %targets) {
   3223  1.1  christos         if (ref($targets{$_}) ne "HASH") {
   3224  1.1  christos             if (ref($targets{$_}) eq "") {
   3225  1.1  christos                 warn "Deprecated target configuration for $_, ignoring...\n";
   3226  1.1  christos             } else {
   3227  1.1  christos                 warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
   3228  1.1  christos             }
   3229  1.1  christos             delete $targets{$_};
   3230  1.1  christos         } else {
   3231  1.1  christos             $targets{$_}->{_conf_fname_int} = add([ $fname ]);
   3232  1.1  christos         }
   3233  1.1  christos     }
   3234  1.1  christos 
   3235  1.1  christos     %table = (%table, %targets);
   3236  1.1  christos 
   3237  1.1  christos }
   3238  1.1  christos 
   3239  1.1  christos # configuration resolver.  Will only resolve all the lazy evaluation
   3240  1.1  christos # codeblocks for the chosen target and all those it inherits from,
   3241  1.1  christos # recursively
   3242  1.1  christos sub resolve_config {
   3243  1.1  christos     my $target = shift;
   3244  1.1  christos     my @breadcrumbs = @_;
   3245  1.1  christos 
   3246  1.1  christos #    my $extra_checks = defined($ENV{CONFIGURE_EXTRA_CHECKS});
   3247  1.1  christos 
   3248  1.1  christos     if (grep { $_ eq $target } @breadcrumbs) {
   3249  1.1  christos         die "inherit_from loop!  target backtrace:\n  "
   3250  1.1  christos             ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
   3251  1.1  christos     }
   3252  1.1  christos 
   3253  1.1  christos     if (!defined($table{$target})) {
   3254  1.1  christos         warn "Warning! target $target doesn't exist!\n";
   3255  1.1  christos         return ();
   3256  1.1  christos     }
   3257  1.1  christos     # Recurse through all inheritances.  They will be resolved on the
   3258  1.1  christos     # fly, so when this operation is done, they will all just be a
   3259  1.1  christos     # bunch of attributes with string values.
   3260  1.1  christos     # What we get here, though, are keys with references to lists of
   3261  1.1  christos     # the combined values of them all.  We will deal with lists after
   3262  1.1  christos     # this stage is done.
   3263  1.1  christos     my %combined_inheritance = ();
   3264  1.1  christos     if ($table{$target}->{inherit_from}) {
   3265  1.1  christos         my @inherit_from =
   3266  1.1  christos             map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
   3267  1.1  christos         foreach (@inherit_from) {
   3268  1.1  christos             my %inherited_config = resolve_config($_, $target, @breadcrumbs);
   3269  1.1  christos 
   3270  1.1  christos             # 'template' is a marker that's considered private to
   3271  1.1  christos             # the config that had it.
   3272  1.1  christos             delete $inherited_config{template};
   3273  1.1  christos 
   3274  1.1  christos             foreach (keys %inherited_config) {
   3275  1.1  christos                 if (!$combined_inheritance{$_}) {
   3276  1.1  christos                     $combined_inheritance{$_} = [];
   3277  1.1  christos                 }
   3278  1.1  christos                 push @{$combined_inheritance{$_}}, $inherited_config{$_};
   3279  1.1  christos             }
   3280  1.1  christos         }
   3281  1.1  christos     }
   3282  1.1  christos 
   3283  1.1  christos     # We won't need inherit_from in this target any more, since we've
   3284  1.1  christos     # resolved all the inheritances that lead to this
   3285  1.1  christos     delete $table{$target}->{inherit_from};
   3286  1.1  christos 
   3287  1.1  christos     # Now is the time to deal with those lists.  Here's the place to
   3288  1.1  christos     # decide what shall be done with those lists, all based on the
   3289  1.1  christos     # values of the target we're currently dealing with.
   3290  1.1  christos     # - If a value is a coderef, it will be executed with the list of
   3291  1.1  christos     #   inherited values as arguments.
   3292  1.1  christos     # - If the corresponding key doesn't have a value at all or is the
   3293  1.1  christos     #   empty string, the inherited value list will be run through the
   3294  1.1  christos     #   default combiner (below), and the result becomes this target's
   3295  1.1  christos     #   value.
   3296  1.1  christos     # - Otherwise, this target's value is assumed to be a string that
   3297  1.1  christos     #   will simply override the inherited list of values.
   3298  1.1  christos     my $default_combiner = add();
   3299  1.1  christos 
   3300  1.1  christos     my %all_keys =
   3301  1.1  christos         map { $_ => 1 } (keys %combined_inheritance,
   3302  1.1  christos                          keys %{$table{$target}});
   3303  1.1  christos 
   3304  1.1  christos     sub process_values {
   3305  1.1  christos         my $object    = shift;
   3306  1.1  christos         my $inherited = shift;  # Always a [ list ]
   3307  1.1  christos         my $target    = shift;
   3308  1.1  christos         my $entry     = shift;
   3309  1.1  christos 
   3310  1.1  christos         $add_called = 0;
   3311  1.1  christos 
   3312  1.1  christos         while(ref($object) eq "CODE") {
   3313  1.1  christos             $object = $object->(@$inherited);
   3314  1.1  christos         }
   3315  1.1  christos         if (!defined($object)) {
   3316  1.1  christos             return ();
   3317  1.1  christos         }
   3318  1.1  christos         elsif (ref($object) eq "ARRAY") {
   3319  1.1  christos             local $add_called;  # To make sure recursive calls don't affect it
   3320  1.1  christos             return [ map { process_values($_, $inherited, $target, $entry) }
   3321  1.1  christos                      @$object ];
   3322  1.1  christos         } elsif (ref($object) eq "") {
   3323  1.1  christos             return $object;
   3324  1.1  christos         } else {
   3325  1.1  christos             die "cannot handle reference type ",ref($object)
   3326  1.1  christos                 ," found in target ",$target," -> ",$entry,"\n";
   3327  1.1  christos         }
   3328  1.1  christos     }
   3329  1.1  christos 
   3330  1.1  christos     foreach my $key (sort keys %all_keys) {
   3331  1.1  christos         my $previous = $combined_inheritance{$key};
   3332  1.1  christos 
   3333  1.1  christos         # Current target doesn't have a value for the current key?
   3334  1.1  christos         # Assign it the default combiner, the rest of this loop body
   3335  1.1  christos         # will handle it just like any other coderef.
   3336  1.1  christos         if (!exists $table{$target}->{$key}) {
   3337  1.1  christos             $table{$target}->{$key} = $default_combiner;
   3338  1.1  christos         }
   3339  1.1  christos 
   3340  1.1  christos         $table{$target}->{$key} = process_values($table{$target}->{$key},
   3341  1.1  christos                                                $combined_inheritance{$key},
   3342  1.1  christos                                                $target, $key);
   3343  1.1  christos         unless(defined($table{$target}->{$key})) {
   3344  1.1  christos             delete $table{$target}->{$key};
   3345  1.1  christos         }
   3346  1.1  christos #        if ($extra_checks &&
   3347  1.1  christos #            $previous && !($add_called ||  $previous ~~ $table{$target}->{$key})) {
   3348  1.1  christos #            warn "$key got replaced in $target\n";
   3349  1.1  christos #        }
   3350  1.1  christos     }
   3351  1.1  christos 
   3352  1.1  christos     # Finally done, return the result.
   3353  1.1  christos     return %{$table{$target}};
   3354  1.1  christos }
   3355  1.1  christos 
   3356  1.1  christos sub usage
   3357  1.1  christos         {
   3358  1.1  christos         print STDERR $usage;
   3359  1.1  christos         print STDERR "\npick os/compiler from:\n";
   3360  1.1  christos         my $j=0;
   3361  1.1  christos         my $i;
   3362  1.1  christos         my $k=0;
   3363  1.1  christos         foreach $i (sort keys %table)
   3364  1.1  christos                 {
   3365  1.1  christos                 next if $table{$i}->{template};
   3366  1.1  christos                 next if $i =~ /^debug/;
   3367  1.1  christos                 $k += length($i) + 1;
   3368  1.1  christos                 if ($k > 78)
   3369  1.1  christos                         {
   3370  1.1  christos                         print STDERR "\n";
   3371  1.1  christos                         $k=length($i);
   3372  1.1  christos                         }
   3373  1.1  christos                 print STDERR $i . " ";
   3374  1.1  christos                 }
   3375  1.1  christos         foreach $i (sort keys %table)
   3376  1.1  christos                 {
   3377  1.1  christos                 next if $table{$i}->{template};
   3378  1.1  christos                 next if $i !~ /^debug/;
   3379  1.1  christos                 $k += length($i) + 1;
   3380  1.1  christos                 if ($k > 78)
   3381  1.1  christos                         {
   3382  1.1  christos                         print STDERR "\n";
   3383  1.1  christos                         $k=length($i);
   3384  1.1  christos                         }
   3385  1.1  christos                 print STDERR $i . " ";
   3386  1.1  christos                 }
   3387  1.1  christos         exit(1);
   3388  1.1  christos         }
   3389  1.1  christos 
   3390  1.1  christos sub compiler_predefined {
   3391  1.1  christos     state %predefined;
   3392  1.1  christos     my $cc = shift;
   3393  1.1  christos 
   3394  1.1  christos     return () if $^O eq 'VMS';
   3395  1.1  christos 
   3396  1.1  christos     die 'compiler_predefined called without a compiler command'
   3397  1.1  christos         unless $cc;
   3398  1.1  christos 
   3399  1.1  christos     if (! $predefined{$cc}) {
   3400  1.1  christos 
   3401  1.1  christos         $predefined{$cc} = {};
   3402  1.1  christos 
   3403  1.1  christos         # collect compiler pre-defines from gcc or gcc-alike...
   3404  1.1  christos         open(PIPE, "$cc -dM -E -x c /dev/null 2>&1 |");
   3405  1.1  christos         while (my $l = <PIPE>) {
   3406  1.1  christos             $l =~ m/^#define\s+(\w+(?:\(\w+\))?)(?:\s+(.+))?/ or last;
   3407  1.1  christos             $predefined{$cc}->{$1} = $2 // '';
   3408  1.1  christos         }
   3409  1.1  christos         close(PIPE);
   3410  1.1  christos     }
   3411  1.1  christos 
   3412  1.1  christos     return %{$predefined{$cc}};
   3413  1.1  christos }
   3414  1.1  christos 
   3415  1.1  christos sub which
   3416  1.1  christos {
   3417  1.1  christos     my ($name)=@_;
   3418  1.1  christos 
   3419  1.1  christos     if (eval { require IPC::Cmd; 1; }) {
   3420  1.1  christos         IPC::Cmd->import();
   3421  1.1  christos         return scalar IPC::Cmd::can_run($name);
   3422  1.1  christos     } else {
   3423  1.1  christos         # if there is $directories component in splitpath,
   3424  1.1  christos         # then it's not something to test with $PATH...
   3425  1.1  christos         return $name if (File::Spec->splitpath($name))[1];
   3426  1.1  christos 
   3427  1.1  christos         foreach (File::Spec->path()) {
   3428  1.1  christos             my $fullpath = catfile($_, "$name$target{exe_extension}");
   3429  1.1  christos             if (-f $fullpath and -x $fullpath) {
   3430  1.1  christos                 return $fullpath;
   3431  1.1  christos             }
   3432  1.1  christos         }
   3433  1.1  christos     }
   3434  1.1  christos }
   3435  1.1  christos 
   3436  1.1  christos sub env
   3437  1.1  christos {
   3438  1.1  christos     my $name = shift;
   3439  1.1  christos     my %opts = @_;
   3440  1.1  christos 
   3441  1.1  christos     unless ($opts{cacheonly}) {
   3442  1.1  christos         # Note that if $ENV{$name} doesn't exist or is undefined,
   3443  1.1  christos         # $config{perlenv}->{$name} will be created with the value
   3444  1.1  christos         # undef.  This is intentional.
   3445  1.1  christos 
   3446  1.1  christos         $config{perlenv}->{$name} = $ENV{$name}
   3447  1.1  christos             if ! exists $config{perlenv}->{$name};
   3448  1.1  christos     }
   3449  1.1  christos     return $config{perlenv}->{$name};
   3450  1.1  christos }
   3451  1.1  christos 
   3452  1.1  christos # Configuration printer ##############################################
   3453  1.1  christos 
   3454  1.1  christos sub print_table_entry
   3455  1.1  christos {
   3456  1.1  christos     local $now_printing = shift;
   3457  1.1  christos     my %target = resolve_config($now_printing);
   3458  1.1  christos     my $type = shift;
   3459  1.1  christos 
   3460  1.1  christos     # Don't print the templates
   3461  1.1  christos     return if $target{template};
   3462  1.1  christos 
   3463  1.1  christos     my @sequence = (
   3464  1.1  christos         "sys_id",
   3465  1.1  christos         "cpp",
   3466  1.1  christos         "cppflags",
   3467  1.1  christos         "defines",
   3468  1.1  christos         "includes",
   3469  1.1  christos         "cc",
   3470  1.1  christos         "cflags",
   3471  1.1  christos         "ld",
   3472  1.1  christos         "lflags",
   3473  1.1  christos         "loutflag",
   3474  1.1  christos         "ex_libs",
   3475  1.1  christos         "bn_ops",
   3476  1.1  christos         "enable",
   3477  1.1  christos         "disable",
   3478  1.1  christos         "poly1035_asm_src",
   3479  1.1  christos         "thread_scheme",
   3480  1.1  christos         "perlasm_scheme",
   3481  1.1  christos         "dso_scheme",
   3482  1.1  christos         "shared_target",
   3483  1.1  christos         "shared_cflag",
   3484  1.1  christos         "shared_defines",
   3485  1.1  christos         "shared_ldflag",
   3486  1.1  christos         "shared_rcflag",
   3487  1.1  christos         "shared_extension",
   3488  1.1  christos         "dso_extension",
   3489  1.1  christos         "obj_extension",
   3490  1.1  christos         "exe_extension",
   3491  1.1  christos         "ranlib",
   3492  1.1  christos         "ar",
   3493  1.1  christos         "arflags",
   3494  1.1  christos         "aroutflag",
   3495  1.1  christos         "rc",
   3496  1.1  christos         "rcflags",
   3497  1.1  christos         "rcoutflag",
   3498  1.1  christos         "mt",
   3499  1.1  christos         "mtflags",
   3500  1.1  christos         "mtinflag",
   3501  1.1  christos         "mtoutflag",
   3502  1.1  christos         "multilib",
   3503  1.1  christos         "build_scheme",
   3504  1.1  christos         );
   3505  1.1  christos 
   3506  1.1  christos     if ($type eq "TABLE") {
   3507  1.1  christos         print "\n";
   3508  1.1  christos         print "*** $now_printing\n";
   3509  1.1  christos         foreach (@sequence) {
   3510  1.1  christos             if (ref($target{$_}) eq "ARRAY") {
   3511  1.1  christos                 printf "\$%-12s = %s\n", $_, join(" ", @{$target{$_}});
   3512  1.1  christos             } else {
   3513  1.1  christos                 printf "\$%-12s = %s\n", $_, $target{$_};
   3514  1.1  christos             }
   3515  1.1  christos         }
   3516  1.1  christos     } elsif ($type eq "HASH") {
   3517  1.1  christos         my $largest =
   3518  1.1  christos             length((sort { length($a) <=> length($b) } @sequence)[-1]);
   3519  1.1  christos         print "    '$now_printing' => {\n";
   3520  1.1  christos         foreach (@sequence) {
   3521  1.1  christos             if ($target{$_}) {
   3522  1.1  christos                 if (ref($target{$_}) eq "ARRAY") {
   3523  1.1  christos                     print "      '",$_,"'"," " x ($largest - length($_))," => [ ",join(", ", map { "'$_'" } @{$target{$_}})," ],\n";
   3524  1.1  christos                 } else {
   3525  1.1  christos                     print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
   3526  1.1  christos                 }
   3527  1.1  christos             }
   3528  1.1  christos         }
   3529  1.1  christos         print "    },\n";
   3530  1.1  christos     }
   3531  1.1  christos }
   3532  1.1  christos 
   3533  1.1  christos # Utility routines ###################################################
   3534  1.1  christos 
   3535  1.1  christos # On VMS, if the given file is a logical name, File::Spec::Functions
   3536  1.1  christos # will consider it an absolute path.  There are cases when we want a
   3537  1.1  christos # purely syntactic check without checking the environment.
   3538  1.1  christos sub isabsolute {
   3539  1.1  christos     my $file = shift;
   3540  1.1  christos 
   3541  1.1  christos     # On non-platforms, we just use file_name_is_absolute().
   3542  1.1  christos     return file_name_is_absolute($file) unless $^O eq "VMS";
   3543  1.1  christos 
   3544  1.1  christos     # If the file spec includes a device or a directory spec,
   3545  1.1  christos     # file_name_is_absolute() is perfectly safe.
   3546  1.1  christos     return file_name_is_absolute($file) if $file =~ m|[:\[]|;
   3547  1.1  christos 
   3548  1.1  christos     # Here, we know the given file spec isn't absolute
   3549  1.1  christos     return 0;
   3550  1.1  christos }
   3551  1.1  christos 
   3552  1.1  christos # Makes a directory absolute and cleans out /../ in paths like foo/../bar
   3553  1.1  christos # On some platforms, this uses rel2abs(), while on others, realpath() is used.
   3554  1.1  christos # realpath() requires that at least all path components except the last is an
   3555  1.1  christos # existing directory.  On VMS, the last component of the directory spec must
   3556  1.1  christos # exist.
   3557  1.1  christos sub absolutedir {
   3558  1.1  christos     my $dir = shift;
   3559  1.1  christos 
   3560  1.1  christos     # realpath() is quite buggy on VMS.  It uses LIB$FID_TO_NAME, which
   3561  1.1  christos     # will return the volume name for the device, no matter what.  Also,
   3562  1.1  christos     # it will return an incorrect directory spec if the argument is a
   3563  1.1  christos     # directory that doesn't exist.
   3564  1.1  christos     if ($^O eq "VMS") {
   3565  1.1  christos         return rel2abs($dir);
   3566  1.1  christos     }
   3567  1.1  christos 
   3568  1.1  christos     # realpath() on Windows seems to check if the directory actually exists,
   3569  1.1  christos     # which isn't what is wanted here.  All we want to know is if a directory
   3570  1.1  christos     # spec is absolute, not if it exists.
   3571  1.1  christos     if ($^O eq "MSWin32") {
   3572  1.1  christos         return rel2abs($dir);
   3573  1.1  christos     }
   3574  1.1  christos 
   3575  1.1  christos     # We use realpath() on Unix, since no other will properly clean out
   3576  1.1  christos     # a directory spec.
   3577  1.1  christos     use Cwd qw/realpath/;
   3578  1.1  christos 
   3579  1.1  christos     return realpath($dir);
   3580  1.1  christos }
   3581  1.1  christos 
   3582  1.1  christos # Check if all paths are one and the same, using stat.  They must both exist
   3583  1.1  christos # We need this for the cases when File::Spec doesn't detect case insensitivity
   3584  1.1  christos # (File::Spec::Unix assumes case sensitivity)
   3585  1.1  christos sub samedir {
   3586  1.1  christos     die "samedir expects two arguments\n" unless scalar @_ == 2;
   3587  1.1  christos 
   3588  1.1  christos     my @stat0 = stat($_[0]);    # First argument
   3589  1.1  christos     my @stat1 = stat($_[1]);    # Second argument
   3590  1.1  christos 
   3591  1.1  christos     die "Couldn't stat $_[0]" unless @stat0;
   3592  1.1  christos     die "Couldn't stat $_[1]" unless @stat1;
   3593  1.1  christos 
   3594  1.1  christos     # Compare device number
   3595  1.1  christos     return 0 unless ($stat0[0] == $stat1[0]);
   3596  1.1  christos     # Compare "inode".  The perl manual recommends comparing as
   3597  1.1  christos     # string rather than as number.
   3598  1.1  christos     return 0 unless ($stat0[1] eq $stat1[1]);
   3599  1.1  christos 
   3600  1.1  christos     return 1;                   # All the same
   3601  1.1  christos }
   3602  1.1  christos 
   3603  1.1  christos sub quotify {
   3604  1.1  christos     my %processors = (
   3605  1.1  christos         perl    => sub { my $x = shift;
   3606  1.1  christos                          $x =~ s/([\\\$\@"])/\\$1/g;
   3607  1.1  christos                          return '"'.$x.'"'; },
   3608  1.1  christos         maybeshell => sub { my $x = shift;
   3609  1.1  christos                             (my $y = $x) =~ s/([\\\"])/\\$1/g;
   3610  1.1  christos                             if ($x ne $y || $x =~ m|\s|) {
   3611  1.1  christos                                 return '"'.$y.'"';
   3612  1.1  christos                             } else {
   3613  1.1  christos                                 return $x;
   3614  1.1  christos                             }
   3615  1.1  christos                         },
   3616  1.1  christos         );
   3617  1.1  christos     my $for = shift;
   3618  1.1  christos     my $processor =
   3619  1.1  christos         defined($processors{$for}) ? $processors{$for} : sub { shift; };
   3620  1.1  christos 
   3621  1.1  christos     return map { $processor->($_); } @_;
   3622  1.1  christos }
   3623  1.1  christos 
   3624  1.1  christos # collect_from_file($filename, $line_concat_cond_re, $line_concat)
   3625  1.1  christos # $filename is a file name to read from
   3626  1.1  christos # $line_concat_cond_re is a regexp detecting a line continuation ending
   3627  1.1  christos # $line_concat is a CODEref that takes care of concatenating two lines
   3628  1.1  christos sub collect_from_file {
   3629  1.1  christos     my $filename = shift;
   3630  1.1  christos     my $line_concat_cond_re = shift;
   3631  1.1  christos     my $line_concat = shift;
   3632  1.1  christos 
   3633  1.1  christos     open my $fh, $filename || die "unable to read $filename: $!\n";
   3634  1.1  christos     return sub {
   3635  1.1  christos         my $saved_line = "";
   3636  1.1  christos         $_ = "";
   3637  1.1  christos         while (<$fh>) {
   3638  1.1  christos             s|\R$||;
   3639  1.1  christos             if (defined $line_concat) {
   3640  1.1  christos                 $_ = $line_concat->($saved_line, $_);
   3641  1.1  christos                 $saved_line = "";
   3642  1.1  christos             }
   3643  1.1  christos             if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
   3644  1.1  christos                 $saved_line = $_;
   3645  1.1  christos                 next;
   3646  1.1  christos             }
   3647  1.1  christos             return $_;
   3648  1.1  christos         }
   3649  1.1  christos         die "$filename ending with continuation line\n" if $_;
   3650  1.1  christos         close $fh;
   3651  1.1  christos         return undef;
   3652  1.1  christos     }
   3653  1.1  christos }
   3654  1.1  christos 
   3655  1.1  christos # collect_from_array($array, $line_concat_cond_re, $line_concat)
   3656  1.1  christos # $array is an ARRAYref of lines
   3657  1.1  christos # $line_concat_cond_re is a regexp detecting a line continuation ending
   3658  1.1  christos # $line_concat is a CODEref that takes care of concatenating two lines
   3659  1.1  christos sub collect_from_array {
   3660  1.1  christos     my $array = shift;
   3661  1.1  christos     my $line_concat_cond_re = shift;
   3662  1.1  christos     my $line_concat = shift;
   3663  1.1  christos     my @array = (@$array);
   3664  1.1  christos 
   3665  1.1  christos     return sub {
   3666  1.1  christos         my $saved_line = "";
   3667  1.1  christos         $_ = "";
   3668  1.1  christos         while (defined($_ = shift @array)) {
   3669  1.1  christos             s|\R$||;
   3670  1.1  christos             if (defined $line_concat) {
   3671  1.1  christos                 $_ = $line_concat->($saved_line, $_);
   3672  1.1  christos                 $saved_line = "";
   3673  1.1  christos             }
   3674  1.1  christos             if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
   3675  1.1  christos                 $saved_line = $_;
   3676  1.1  christos                 next;
   3677  1.1  christos             }
   3678  1.1  christos             return $_;
   3679  1.1  christos         }
   3680  1.1  christos         die "input text ending with continuation line\n" if $_;
   3681  1.1  christos         return undef;
   3682  1.1  christos     }
   3683  1.1  christos }
   3684  1.1  christos 
   3685  1.1  christos # collect_information($lineiterator, $line_continue, $regexp => $CODEref, ...)
   3686  1.1  christos # $lineiterator is a CODEref that delivers one line at a time.
   3687  1.1  christos # All following arguments are regex/CODEref pairs, where the regexp detects a
   3688  1.1  christos # line and the CODEref does something with the result of the regexp.
   3689  1.1  christos sub collect_information {
   3690  1.1  christos     my $lineiterator = shift;
   3691  1.1  christos     my %collectors = @_;
   3692  1.1  christos 
   3693  1.1  christos     while(defined($_ = $lineiterator->())) {
   3694  1.1  christos         s|\R$||;
   3695  1.1  christos         my $found = 0;
   3696  1.1  christos         if ($collectors{"BEFORE"}) {
   3697  1.1  christos             $collectors{"BEFORE"}->($_);
   3698  1.1  christos         }
   3699  1.1  christos         foreach my $re (keys %collectors) {
   3700  1.1  christos             if ($re !~ /^OTHERWISE|BEFORE|AFTER$/ && /$re/) {
   3701  1.1  christos                 $collectors{$re}->($lineiterator);
   3702  1.1  christos                 $found = 1;
   3703  1.1  christos             };
   3704  1.1  christos         }
   3705  1.1  christos         if ($collectors{"OTHERWISE"}) {
   3706  1.1  christos             $collectors{"OTHERWISE"}->($lineiterator, $_)
   3707  1.1  christos                 unless $found || !defined $collectors{"OTHERWISE"};
   3708  1.1  christos         }
   3709  1.1  christos         if ($collectors{"AFTER"}) {
   3710  1.1  christos             $collectors{"AFTER"}->($_);
   3711  1.1  christos         }
   3712  1.1  christos     }
   3713  1.1  christos }
   3714  1.1  christos 
   3715  1.1  christos # tokenize($line)
   3716  1.1  christos # tokenize($line,$separator)
   3717  1.1  christos # $line is a line of text to split up into tokens
   3718  1.1  christos # $separator [optional] is a regular expression that separates the tokens,
   3719  1.1  christos # the default being spaces.  Do not use quotes of any kind as separators,
   3720  1.1  christos # that will give undefined results.
   3721  1.1  christos # Returns a list of tokens.
   3722  1.1  christos #
   3723  1.1  christos # Tokens are divided by separator (spaces by default).  If the tokens include
   3724  1.1  christos # the separators, they have to be quoted with single or double quotes.
   3725  1.1  christos # Double quotes inside a double quoted token must be escaped.  Escaping is done
   3726  1.1  christos # with backslash.
   3727  1.1  christos # Basically, the same quoting rules apply for " and ' as in any
   3728  1.1  christos # Unix shell.
   3729  1.1  christos sub tokenize {
   3730  1.1  christos     my $line = my $debug_line = shift;
   3731  1.1  christos     my $separator = shift // qr|\s+|;
   3732  1.1  christos     my @result = ();
   3733  1.1  christos 
   3734  1.1  christos     if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
   3735  1.1  christos         print STDERR "DEBUG[tokenize]: \$separator = $separator\n";
   3736  1.1  christos     }
   3737  1.1  christos 
   3738  1.1  christos     while ($line =~ s|^${separator}||, $line ne "") {
   3739  1.1  christos         my $token = "";
   3740  1.1  christos     again:
   3741  1.1  christos         $line =~ m/^(.*?)(${separator}|"|'|$)/;
   3742  1.1  christos         $token .= $1;
   3743  1.1  christos         $line = $2.$';
   3744  1.1  christos 
   3745  1.1  christos         if ($line =~ m/^"((?:[^"\\]+|\\.)*)"/) {
   3746  1.1  christos             $token .= $1;
   3747  1.1  christos             $line = $';
   3748  1.1  christos             goto again;
   3749  1.1  christos         } elsif ($line =~ m/^'([^']*)'/) {
   3750  1.1  christos             $token .= $1;
   3751  1.1  christos             $line = $';
   3752  1.1  christos             goto again;
   3753  1.1  christos         }
   3754  1.1  christos         push @result, $token;
   3755  1.1  christos     }
   3756  1.1  christos 
   3757  1.1  christos     if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
   3758  1.1  christos         print STDERR "DEBUG[tokenize]: Parsed '$debug_line' into:\n";
   3759  1.1  christos         print STDERR "DEBUG[tokenize]: ('", join("', '", @result), "')\n";
   3760  1.1  christos     }
   3761  1.1  christos     return @result;
   3762  1.1  christos }
   3763