xref: /freebsd/crypto/openssl/Configure (revision 88e852c0b5c872b1a3234515623104ae61b60773)
1e71b7053SJung-uk Kim#! /usr/bin/env perl
2e71b7053SJung-uk Kim# -*- mode: perl; -*-
3*88e852c0SJung-uk Kim# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4e71b7053SJung-uk Kim#
5e71b7053SJung-uk Kim# Licensed under the OpenSSL license (the "License").  You may not use
6e71b7053SJung-uk Kim# this file except in compliance with the License.  You can obtain a copy
7e71b7053SJung-uk Kim# in the file LICENSE in the source distribution or at
8e71b7053SJung-uk Kim# https://www.openssl.org/source/license.html
974664626SKris Kennaway
10e71b7053SJung-uk Kim##  Configure -- OpenSSL source tree configuration script
11e71b7053SJung-uk Kim
12e71b7053SJung-uk Kimuse 5.10.0;
131f13597dSJung-uk Kimuse strict;
14e71b7053SJung-uk Kimuse Config;
15e71b7053SJung-uk Kimuse FindBin;
16e71b7053SJung-uk Kimuse lib "$FindBin::Bin/util/perl";
17e71b7053SJung-uk Kimuse File::Basename;
18e71b7053SJung-uk Kimuse File::Spec::Functions qw/:DEFAULT abs2rel rel2abs/;
19e71b7053SJung-uk Kimuse File::Path qw/mkpath/;
20e71b7053SJung-uk Kimuse OpenSSL::Glob;
2174664626SKris Kennaway
2274664626SKris Kennaway# see INSTALL for instructions.
2374664626SKris Kennaway
24e71b7053SJung-uk Kimmy $orig_death_handler = $SIG{__DIE__};
25e71b7053SJung-uk Kim$SIG{__DIE__} = \&death_handler;
26e71b7053SJung-uk Kim
27610a21fdSJung-uk Kimmy $usage="Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
2874664626SKris Kennaway
2974664626SKris Kennaway# Options:
3074664626SKris Kennaway#
31e71b7053SJung-uk Kim# --config      add the given configuration file, which will be read after
32e71b7053SJung-uk Kim#               any "Configurations*" files that are found in the same
33e71b7053SJung-uk Kim#               directory as this script.
34e71b7053SJung-uk Kim# --prefix      prefix for the OpenSSL installation, which includes the
35e71b7053SJung-uk Kim#               directories bin, lib, include, share/man, share/doc/openssl
36e71b7053SJung-uk Kim#               This becomes the value of INSTALLTOP in Makefile
37e71b7053SJung-uk Kim#               (Default: /usr/local)
38e71b7053SJung-uk Kim# --openssldir  OpenSSL data area, such as openssl.cnf, certificates and keys.
39e71b7053SJung-uk Kim#               If it's a relative directory, it will be added on the directory
40e71b7053SJung-uk Kim#               given with --prefix.
41e71b7053SJung-uk Kim#               This becomes the value of OPENSSLDIR in Makefile and in C.
42e71b7053SJung-uk Kim#               (Default: PREFIX/ssl)
43c1803d78SJacques Vidrine#
441f13597dSJung-uk Kim# --cross-compile-prefix Add specified prefix to binutils components.
451f13597dSJung-uk Kim#
46e71b7053SJung-uk Kim# --api         One of 0.9.8, 1.0.0 or 1.1.0.  Do not compile support for
47e71b7053SJung-uk Kim#               interfaces deprecated as of the specified OpenSSL version.
48e71b7053SJung-uk Kim#
495c87c606SMark Murray# no-hw-xxx     do not compile support for specific crypto hardware.
505c87c606SMark Murray#               Generic OpenSSL-style methods relating to this support
515c87c606SMark Murray#               are always compiled but return NULL if the hardware
525c87c606SMark Murray#               support isn't compiled.
535c87c606SMark Murray# no-hw         do not compile support for any crypto hardware.
5474664626SKris Kennaway# [no-]threads  [don't] try to create a library that is suitable for
5574664626SKris Kennaway#               multithreaded applications (default is "threads" if we
5674664626SKris Kennaway#               know how to do it)
57ddd58736SKris Kennaway# [no-]shared   [don't] try to create shared libraries when supported.
58e71b7053SJung-uk Kim# [no-]pic      [don't] try to build position independent code when supported.
59e71b7053SJung-uk Kim#               If disabled, it also disables shared and dynamic-engine.
6074664626SKris Kennaway# no-asm        do not use assembler
61e71b7053SJung-uk Kim# no-egd        do not compile support for the entropy-gathering daemon APIs
625c87c606SMark Murray# [no-]zlib     [don't] compile support for zlib compression.
635c87c606SMark Murray# zlib-dynamic  Like "zlib", but the zlib library is expected to be a shared
645c87c606SMark Murray#               library and will be loaded in run-time by the OpenSSL library.
651f13597dSJung-uk Kim# sctp          include SCTP support
664c6a0400SJung-uk Kim# enable-weak-ssl-ciphers
67e71b7053SJung-uk Kim#               Enable weak ciphers that are disabled by default.
686cf8931aSJung-uk Kim# 386           generate 80386 code in assembly modules
696cf8931aSJung-uk Kim# no-sse2       disables IA-32 SSE2 code in assembly modules, the above
706cf8931aSJung-uk Kim#               mentioned '386' option implies this one
7174664626SKris Kennaway# no-<cipher>   build without specified algorithm (rsa, idea, rc5, ...)
7217f01e99SJung-uk Kim# -<xxx> +<xxx> All options which are unknown to the 'Configure' script are
7317f01e99SJung-uk Kim# /<xxx>        passed through to the compiler. Unix-style options beginning
7417f01e99SJung-uk Kim#               with a '-' or '+' are recognized, as well as Windows-style
7517f01e99SJung-uk Kim#               options beginning with a '/'. If the option contains arguments
7617f01e99SJung-uk Kim#               separated by spaces, then the URL-style notation %20 can be
7717f01e99SJung-uk Kim#               used for the space character in order to avoid having to quote
7817f01e99SJung-uk Kim#               the option. For example, -opt%20arg gets expanded to -opt arg.
7917f01e99SJung-uk Kim#               In fact, any ASCII character can be encoded as %xx using its
8017f01e99SJung-uk Kim#               hexadecimal encoding.
81e71b7053SJung-uk Kim# -static       while -static is also a pass-through compiler option (and
82e71b7053SJung-uk Kim#               as such is limited to environments where it's actually
83e71b7053SJung-uk Kim#               meaningful), it triggers a number configuration options,
84610a21fdSJung-uk Kim#               namely no-pic, no-shared and no-threads. It is
85e71b7053SJung-uk Kim#               argued that the only reason to produce statically linked
86e71b7053SJung-uk Kim#               binaries (and in context it means executables linked with
87e71b7053SJung-uk Kim#               -static flag, and not just executables linked with static
88e71b7053SJung-uk Kim#               libcrypto.a) is to eliminate dependency on specific run-time,
89e71b7053SJung-uk Kim#               a.k.a. libc version. The mentioned config options are meant
90e71b7053SJung-uk Kim#               to achieve just that. Unfortunately on Linux it's impossible
91e71b7053SJung-uk Kim#               to eliminate the dependency completely for openssl executable
92e71b7053SJung-uk Kim#               because of getaddrinfo and gethostbyname calls, which can
93e71b7053SJung-uk Kim#               invoke dynamically loadable library facility anyway to meet
94e71b7053SJung-uk Kim#               the lookup requests. For this reason on Linux statically
95e71b7053SJung-uk Kim#               linked openssl executable has rather debugging value than
96e71b7053SJung-uk Kim#               production quality.
9774664626SKris Kennaway#
9874664626SKris Kennaway# BN_LLONG      use the type 'long long' in crypto/bn/bn.h
9974664626SKris Kennaway# RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
1003b4e3dcbSSimon L. B. Nielsen# Following are set automatically by this script
1013b4e3dcbSSimon L. B. Nielsen#
102e71b7053SJung-uk Kim# MD5_ASM       use some extra md5 assembler,
103e71b7053SJung-uk Kim# SHA1_ASM      use some extra sha1 assembler, must define L_ENDIAN for x86
104e71b7053SJung-uk Kim# RMD160_ASM    use some extra ripemd160 assembler,
1053b4e3dcbSSimon L. B. Nielsen# SHA256_ASM    sha256_block is implemented in assembler
1063b4e3dcbSSimon L. B. Nielsen# SHA512_ASM    sha512_block is implemented in assembler
107e71b7053SJung-uk Kim# AES_ASM       AES_[en|de]crypt is implemented in assembler
10874664626SKris Kennaway
109610a21fdSJung-uk Kim# Minimum warning options... any contributions to OpenSSL should at least
110610a21fdSJung-uk Kim# get past these.  Note that we only use these with C compilers, not with
111610a21fdSJung-uk Kim# C++ compilers.
112db522d3aSSimon L. B. Nielsen
113e71b7053SJung-uk Kim# DEBUG_UNUSED enables __owur (warn unused result) checks.
114e71b7053SJung-uk Kim# -DPEDANTIC complements -pedantic and is meant to mask code that
115e71b7053SJung-uk Kim# is not strictly standard-compliant and/or implementation-specific,
116e71b7053SJung-uk Kim# e.g. inline assembly, disregards to alignment requirements, such
117e71b7053SJung-uk Kim# that -pedantic would complain about. Incidentally -DPEDANTIC has
118e71b7053SJung-uk Kim# to be used even in sanitized builds, because sanitizer too is
119e71b7053SJung-uk Kim# supposed to and does take notice of non-standard behaviour. Then
120e71b7053SJung-uk Kim# -pedantic with pre-C9x compiler would also complain about 'long
121e71b7053SJung-uk Kim# long' not being supported. As 64-bit algorithms are common now,
122e71b7053SJung-uk Kim# it grew impossible to resolve this without sizeable additional
123e71b7053SJung-uk Kim# code, so we just tell compiler to be pedantic about everything
124e71b7053SJung-uk Kim# but 'long long' type.
125db522d3aSSimon L. B. Nielsen
126610a21fdSJung-uk Kimmy @gcc_devteam_warn = qw(
127610a21fdSJung-uk Kim    -DDEBUG_UNUSED
128610a21fdSJung-uk Kim    -DPEDANTIC -pedantic -Wno-long-long
129610a21fdSJung-uk Kim    -Wall
130610a21fdSJung-uk Kim    -Wextra
131610a21fdSJung-uk Kim    -Wno-unused-parameter
132610a21fdSJung-uk Kim    -Wno-missing-field-initializers
133610a21fdSJung-uk Kim    -Wswitch
134610a21fdSJung-uk Kim    -Wsign-compare
135610a21fdSJung-uk Kim    -Wshadow
136610a21fdSJung-uk Kim    -Wformat
137610a21fdSJung-uk Kim    -Wtype-limits
138610a21fdSJung-uk Kim    -Wundef
139610a21fdSJung-uk Kim    -Werror
140610a21fdSJung-uk Kim    -Wmissing-prototypes
141610a21fdSJung-uk Kim    -Wstrict-prototypes
142610a21fdSJung-uk Kim);
1437bded2dbSJung-uk Kim
1447bded2dbSJung-uk Kim# These are used in addition to $gcc_devteam_warn when the compiler is clang.
1457bded2dbSJung-uk Kim# TODO(openssl-team): fix problems and investigate if (at least) the
146e71b7053SJung-uk Kim# following warnings can also be enabled:
147e71b7053SJung-uk Kim#       -Wcast-align
148e71b7053SJung-uk Kim#       -Wunreachable-code -- no, too ugly/compiler-specific
149e71b7053SJung-uk Kim#       -Wlanguage-extension-token -- no, we use asm()
150e71b7053SJung-uk Kim#       -Wunused-macros -- no, too tricky for BN and _XOPEN_SOURCE etc
151e71b7053SJung-uk Kim#       -Wextended-offsetof -- no, needed in CMS ASN1 code
152610a21fdSJung-uk Kimmy @clang_devteam_warn = qw(
153da327cd2SJung-uk Kim    -Wno-unknown-warning-option
154610a21fdSJung-uk Kim    -Wswitch-default
155610a21fdSJung-uk Kim    -Wno-parentheses-equality
156610a21fdSJung-uk Kim    -Wno-language-extension-token
157610a21fdSJung-uk Kim    -Wno-extended-offsetof
158610a21fdSJung-uk Kim    -Wconditional-uninitialized
159610a21fdSJung-uk Kim    -Wincompatible-pointer-types-discards-qualifiers
160610a21fdSJung-uk Kim    -Wmissing-variable-declarations
161610a21fdSJung-uk Kim);
1627bded2dbSJung-uk Kim
16317f01e99SJung-uk Kimmy @cl_devteam_warn = qw(
16417f01e99SJung-uk Kim    /WX
16517f01e99SJung-uk Kim);
16617f01e99SJung-uk Kim
167e71b7053SJung-uk Kim# This adds backtrace information to the memory leak info.  Is only used
168e71b7053SJung-uk Kim# when crypto-mdebug-backtrace is enabled.
169e71b7053SJung-uk Kimmy $memleak_devteam_backtrace = "-rdynamic";
1708180e704SJung-uk Kim
1716a599222SSimon L. B. Nielsenmy $strict_warnings = 0;
1726a599222SSimon L. B. Nielsen
1733b4e3dcbSSimon L. B. Nielsen# As for $BSDthreads. Idea is to maintain "collective" set of flags,
1743b4e3dcbSSimon L. B. Nielsen# which would cover all BSD flavors. -pthread applies to them all,
1753b4e3dcbSSimon L. B. Nielsen# but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
1763b4e3dcbSSimon L. B. Nielsen# -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
1773b4e3dcbSSimon L. B. Nielsen# which has to be accompanied by explicit -D_THREAD_SAFE and
1783b4e3dcbSSimon L. B. Nielsen# sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
1793b4e3dcbSSimon L. B. Nielsen# seems to be sufficient?
180e71b7053SJung-uk Kimour $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
18174664626SKris Kennaway
18274664626SKris Kennaway#
183e71b7053SJung-uk Kim# API compatibility name to version number mapping.
18474664626SKris Kennaway#
185e71b7053SJung-uk Kimmy $maxapi = "1.1.0";           # API for "no-deprecated" builds
186e71b7053SJung-uk Kimmy $apitable = {
187e71b7053SJung-uk Kim    "1.1.0" => "0x10100000L",
188e71b7053SJung-uk Kim    "1.0.0" => "0x10000000L",
189e71b7053SJung-uk Kim    "0.9.8" => "0x00908000L",
190e71b7053SJung-uk Kim};
191e71b7053SJung-uk Kim
192e71b7053SJung-uk Kimour %table = ();
193e71b7053SJung-uk Kimour %config = ();
194e71b7053SJung-uk Kimour %withargs = ();
195e71b7053SJung-uk Kimour $now_printing;      # set to current entry's name in print_table_entry
196e71b7053SJung-uk Kim                        # (todo: right thing would be to encapsulate name
197e71b7053SJung-uk Kim                        # into %target [class] and make print_table_entry
198e71b7053SJung-uk Kim                        # a method)
199e71b7053SJung-uk Kim
200e71b7053SJung-uk Kim# Forward declarations ###############################################
201e71b7053SJung-uk Kim
202e71b7053SJung-uk Kim# read_config(filename)
203c1803d78SJacques Vidrine#
204e71b7053SJung-uk Kim# Reads a configuration file and populates %table with the contents
205e71b7053SJung-uk Kim# (which the configuration file places in %targets).
206e71b7053SJung-uk Kimsub read_config;
20774664626SKris Kennaway
208e71b7053SJung-uk Kim# resolve_config(target)
20974664626SKris Kennaway#
210e71b7053SJung-uk Kim# Resolves all the late evaluations, inheritances and so on for the
211e71b7053SJung-uk Kim# chosen target and any target it inherits from.
212e71b7053SJung-uk Kimsub resolve_config;
21374664626SKris Kennaway
21474664626SKris Kennaway
215e71b7053SJung-uk Kim# Information collection #############################################
21674664626SKris Kennaway
217e71b7053SJung-uk Kim# Unified build supports separate build dir
218e71b7053SJung-uk Kimmy $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
219e71b7053SJung-uk Kimmy $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
22058f35182SJung-uk Kim
22158f35182SJung-uk Kim# File::Spec::Unix doesn't detect case insensitivity, so we make sure to
22258f35182SJung-uk Kim# check if the source and build directory are really the same, and make
22358f35182SJung-uk Kim# them so.  This avoids all kinds of confusion later on.
22458f35182SJung-uk Kim# We must check @File::Spec::ISA rather than using File::Spec->isa() to
22558f35182SJung-uk Kim# know if File::Spec ended up loading File::Spec::Unix.
22658f35182SJung-uk Kim$srcdir = $blddir
22758f35182SJung-uk Kim    if (grep(/::Unix$/, @File::Spec::ISA)
22858f35182SJung-uk Kim        && samedir($srcdir, $blddir));
22958f35182SJung-uk Kim
230e71b7053SJung-uk Kimmy $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
23174664626SKris Kennaway
232e71b7053SJung-uk Kimmy $local_config_envname = 'OPENSSL_LOCAL_CONFIG_DIR';
233a21b1b38SKris Kennaway
23458f35182SJung-uk Kim$config{sourcedir} = abs2rel($srcdir, $blddir);
23558f35182SJung-uk Kim$config{builddir} = abs2rel($blddir, $blddir);
23674664626SKris Kennaway
237e71b7053SJung-uk Kim# Collect reconfiguration information if needed
238e71b7053SJung-uk Kimmy @argvcopy=@ARGV;
23974664626SKris Kennaway
240e71b7053SJung-uk Kimif (grep /^reconf(igure)?$/, @argvcopy) {
241e71b7053SJung-uk Kim    die "reconfiguring with other arguments present isn't supported"
242e71b7053SJung-uk Kim        if scalar @argvcopy > 1;
243e71b7053SJung-uk Kim    if (-f "./configdata.pm") {
244e71b7053SJung-uk Kim        my $file = "./configdata.pm";
245e71b7053SJung-uk Kim        unless (my $return = do $file) {
246e71b7053SJung-uk Kim            die "couldn't parse $file: $@" if $@;
247e71b7053SJung-uk Kim            die "couldn't do $file: $!"    unless defined $return;
248e71b7053SJung-uk Kim            die "couldn't run $file"       unless $return;
2491f13597dSJung-uk Kim        }
2503b4e3dcbSSimon L. B. Nielsen
251e71b7053SJung-uk Kim        @argvcopy = defined($configdata::config{perlargv}) ?
252e71b7053SJung-uk Kim            @{$configdata::config{perlargv}} : ();
253e71b7053SJung-uk Kim        die "Incorrect data to reconfigure, please do a normal configuration\n"
254e71b7053SJung-uk Kim            if (grep(/^reconf/,@argvcopy));
255e71b7053SJung-uk Kim        $config{perlenv} = $configdata::config{perlenv} // {};
256e71b7053SJung-uk Kim    } else {
257e71b7053SJung-uk Kim        die "Insufficient data to reconfigure, please do a normal configuration\n";
258e71b7053SJung-uk Kim    }
259e71b7053SJung-uk Kim}
2603b4e3dcbSSimon L. B. Nielsen
261e71b7053SJung-uk Kim$config{perlargv} = [ @argvcopy ];
262e71b7053SJung-uk Kim
263e71b7053SJung-uk Kim# Collect version numbers
264e71b7053SJung-uk Kim$config{version} = "unknown";
265e71b7053SJung-uk Kim$config{version_num} = "unknown";
266e71b7053SJung-uk Kim$config{shlib_version_number} = "unknown";
267e71b7053SJung-uk Kim$config{shlib_version_history} = "unknown";
268e71b7053SJung-uk Kim
269e71b7053SJung-uk Kimcollect_information(
270e71b7053SJung-uk Kim    collect_from_file(catfile($srcdir,'include/openssl/opensslv.h')),
271e71b7053SJung-uk Kim    qr/OPENSSL.VERSION.TEXT.*OpenSSL (\S+) / => sub { $config{version} = $1; },
272e71b7053SJung-uk Kim    qr/OPENSSL.VERSION.NUMBER.*(0x\S+)/      => sub { $config{version_num}=$1 },
273e71b7053SJung-uk Kim    qr/SHLIB_VERSION_NUMBER *"([^"]+)"/      => sub { $config{shlib_version_number}=$1 },
274e71b7053SJung-uk Kim    qr/SHLIB_VERSION_HISTORY *"([^"]*)"/     => sub { $config{shlib_version_history}=$1 }
275e71b7053SJung-uk Kim    );
276e71b7053SJung-uk Kimif ($config{shlib_version_history} ne "") { $config{shlib_version_history} .= ":"; }
277e71b7053SJung-uk Kim
278e71b7053SJung-uk Kim($config{major}, $config{minor})
279e71b7053SJung-uk Kim    = ($config{version} =~ /^([0-9]+)\.([0-9\.]+)/);
280e71b7053SJung-uk Kim($config{shlib_major}, $config{shlib_minor})
281e71b7053SJung-uk Kim    = ($config{shlib_version_number} =~ /^([0-9]+)\.([0-9\.]+)/);
282e71b7053SJung-uk Kimdie "erroneous version information in opensslv.h: ",
283e71b7053SJung-uk Kim    "$config{major}, $config{minor}, $config{shlib_major}, $config{shlib_minor}\n"
284e71b7053SJung-uk Kim    if ($config{major} eq "" || $config{minor} eq ""
285e71b7053SJung-uk Kim        || $config{shlib_major} eq "" ||  $config{shlib_minor} eq "");
286e71b7053SJung-uk Kim
287e71b7053SJung-uk Kim# Collect target configurations
288e71b7053SJung-uk Kim
289e71b7053SJung-uk Kimmy $pattern = catfile(dirname($0), "Configurations", "*.conf");
290e71b7053SJung-uk Kimforeach (sort glob($pattern)) {
291e71b7053SJung-uk Kim    &read_config($_);
292e71b7053SJung-uk Kim}
293e71b7053SJung-uk Kim
294e71b7053SJung-uk Kimif (defined env($local_config_envname)) {
295e71b7053SJung-uk Kim    if ($^O eq 'VMS') {
296e71b7053SJung-uk Kim        # VMS environment variables are logical names,
297e71b7053SJung-uk Kim        # which can be used as is
298e71b7053SJung-uk Kim        $pattern = $local_config_envname . ':' . '*.conf';
299e71b7053SJung-uk Kim    } else {
300e71b7053SJung-uk Kim        $pattern = catfile(env($local_config_envname), '*.conf');
301e71b7053SJung-uk Kim    }
302e71b7053SJung-uk Kim
303e71b7053SJung-uk Kim    foreach (sort glob($pattern)) {
304e71b7053SJung-uk Kim        &read_config($_);
305e71b7053SJung-uk Kim    }
306e71b7053SJung-uk Kim}
307e71b7053SJung-uk Kim
308e71b7053SJung-uk Kim# Save away perl command information
309e71b7053SJung-uk Kim$config{perl_cmd} = $^X;
310e71b7053SJung-uk Kim$config{perl_version} = $Config{version};
311e71b7053SJung-uk Kim$config{perl_archname} = $Config{archname};
312e71b7053SJung-uk Kim
313e71b7053SJung-uk Kim$config{prefix}="";
314e71b7053SJung-uk Kim$config{openssldir}="";
315e71b7053SJung-uk Kim$config{processor}="";
316e71b7053SJung-uk Kim$config{libdir}="";
317e71b7053SJung-uk Kimmy $auto_threads=1;    # enable threads automatically? true by default
318e71b7053SJung-uk Kimmy $default_ranlib;
319e71b7053SJung-uk Kim
320e71b7053SJung-uk Kim# Top level directories to build
321e71b7053SJung-uk Kim$config{dirs} = [ "crypto", "ssl", "engines", "apps", "test", "util", "tools", "fuzz" ];
322e71b7053SJung-uk Kim# crypto/ subdirectories to build
323e71b7053SJung-uk Kim$config{sdirs} = [
324e71b7053SJung-uk Kim    "objects",
325e71b7053SJung-uk Kim    "md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305", "blake2", "siphash", "sm3",
326e71b7053SJung-uk Kim    "des", "aes", "rc2", "rc4", "rc5", "idea", "aria", "bf", "cast", "camellia", "seed", "sm4", "chacha", "modes",
327e71b7053SJung-uk Kim    "bn", "ec", "rsa", "dsa", "dh", "sm2", "dso", "engine",
328e71b7053SJung-uk Kim    "buffer", "bio", "stack", "lhash", "rand", "err",
329e71b7053SJung-uk Kim    "evp", "asn1", "pem", "x509", "x509v3", "conf", "txt_db", "pkcs7", "pkcs12", "comp", "ocsp", "ui",
330e71b7053SJung-uk Kim    "cms", "ts", "srp", "cmac", "ct", "async", "kdf", "store"
331e71b7053SJung-uk Kim    ];
332e71b7053SJung-uk Kim# test/ subdirectories to build
333e71b7053SJung-uk Kim$config{tdirs} = [ "ossl_shim" ];
334e71b7053SJung-uk Kim
335e71b7053SJung-uk Kim# Known TLS and DTLS protocols
336e71b7053SJung-uk Kimmy @tls = qw(ssl3 tls1 tls1_1 tls1_2 tls1_3);
337e71b7053SJung-uk Kimmy @dtls = qw(dtls1 dtls1_2);
338e71b7053SJung-uk Kim
339e71b7053SJung-uk Kim# Explicitly known options that are possible to disable.  They can
340e71b7053SJung-uk Kim# be regexps, and will be used like this: /^no-${option}$/
341e71b7053SJung-uk Kim# For developers: keep it sorted alphabetically
342e71b7053SJung-uk Kim
343e71b7053SJung-uk Kimmy @disablables = (
344e71b7053SJung-uk Kim    "afalgeng",
345e71b7053SJung-uk Kim    "aria",
346e71b7053SJung-uk Kim    "asan",
347e71b7053SJung-uk Kim    "asm",
348e71b7053SJung-uk Kim    "async",
349e71b7053SJung-uk Kim    "autoalginit",
350e71b7053SJung-uk Kim    "autoerrinit",
351e71b7053SJung-uk Kim    "autoload-config",
352e71b7053SJung-uk Kim    "bf",
353e71b7053SJung-uk Kim    "blake2",
354610a21fdSJung-uk Kim    "buildtest-c\\+\\+",
355e71b7053SJung-uk Kim    "camellia",
356e71b7053SJung-uk Kim    "capieng",
357e71b7053SJung-uk Kim    "cast",
358e71b7053SJung-uk Kim    "chacha",
359e71b7053SJung-uk Kim    "cmac",
360e71b7053SJung-uk Kim    "cms",
361e71b7053SJung-uk Kim    "comp",
362e71b7053SJung-uk Kim    "crypto-mdebug",
363e71b7053SJung-uk Kim    "crypto-mdebug-backtrace",
364e71b7053SJung-uk Kim    "ct",
365e71b7053SJung-uk Kim    "deprecated",
366e71b7053SJung-uk Kim    "des",
367e71b7053SJung-uk Kim    "devcryptoeng",
368e71b7053SJung-uk Kim    "dgram",
369e71b7053SJung-uk Kim    "dh",
370e71b7053SJung-uk Kim    "dsa",
37117f01e99SJung-uk Kim    "dso",
372e71b7053SJung-uk Kim    "dtls",
373e71b7053SJung-uk Kim    "dynamic-engine",
374e71b7053SJung-uk Kim    "ec",
375e71b7053SJung-uk Kim    "ec2m",
376e71b7053SJung-uk Kim    "ecdh",
377e71b7053SJung-uk Kim    "ecdsa",
378e71b7053SJung-uk Kim    "ec_nistp_64_gcc_128",
379e71b7053SJung-uk Kim    "egd",
380e71b7053SJung-uk Kim    "engine",
381e71b7053SJung-uk Kim    "err",
382e71b7053SJung-uk Kim    "external-tests",
383e71b7053SJung-uk Kim    "filenames",
384e71b7053SJung-uk Kim    "fuzz-libfuzzer",
385e71b7053SJung-uk Kim    "fuzz-afl",
386e71b7053SJung-uk Kim    "gost",
387e71b7053SJung-uk Kim    "heartbeats",
388e71b7053SJung-uk Kim    "hw(-.+)?",
389e71b7053SJung-uk Kim    "idea",
390aa906e2aSJohn Baldwin    "ktls",
391e71b7053SJung-uk Kim    "makedepend",
392e71b7053SJung-uk Kim    "md2",
393e71b7053SJung-uk Kim    "md4",
394e71b7053SJung-uk Kim    "mdc2",
395e71b7053SJung-uk Kim    "msan",
396e71b7053SJung-uk Kim    "multiblock",
397e71b7053SJung-uk Kim    "nextprotoneg",
3986935a639SJung-uk Kim    "pinshared",
399e71b7053SJung-uk Kim    "ocb",
400e71b7053SJung-uk Kim    "ocsp",
401e71b7053SJung-uk Kim    "pic",
402e71b7053SJung-uk Kim    "poly1305",
403e71b7053SJung-uk Kim    "posix-io",
404e71b7053SJung-uk Kim    "psk",
405e71b7053SJung-uk Kim    "rc2",
406e71b7053SJung-uk Kim    "rc4",
407e71b7053SJung-uk Kim    "rc5",
408e71b7053SJung-uk Kim    "rdrand",
409e71b7053SJung-uk Kim    "rfc3779",
410e71b7053SJung-uk Kim    "rmd160",
411e71b7053SJung-uk Kim    "scrypt",
412e71b7053SJung-uk Kim    "sctp",
413e71b7053SJung-uk Kim    "seed",
414e71b7053SJung-uk Kim    "shared",
415e71b7053SJung-uk Kim    "siphash",
416e71b7053SJung-uk Kim    "sm2",
417e71b7053SJung-uk Kim    "sm3",
418e71b7053SJung-uk Kim    "sm4",
419e71b7053SJung-uk Kim    "sock",
420e71b7053SJung-uk Kim    "srp",
421e71b7053SJung-uk Kim    "srtp",
422e71b7053SJung-uk Kim    "sse2",
423e71b7053SJung-uk Kim    "ssl",
424e71b7053SJung-uk Kim    "ssl-trace",
425e71b7053SJung-uk Kim    "static-engine",
426e71b7053SJung-uk Kim    "stdio",
427e71b7053SJung-uk Kim    "tests",
428e71b7053SJung-uk Kim    "threads",
429e71b7053SJung-uk Kim    "tls",
430e71b7053SJung-uk Kim    "ts",
431e71b7053SJung-uk Kim    "ubsan",
432e71b7053SJung-uk Kim    "ui-console",
433e71b7053SJung-uk Kim    "unit-test",
434e71b7053SJung-uk Kim    "whirlpool",
435e71b7053SJung-uk Kim    "weak-ssl-ciphers",
436e71b7053SJung-uk Kim    "zlib",
437e71b7053SJung-uk Kim    "zlib-dynamic",
438e71b7053SJung-uk Kim    );
439e71b7053SJung-uk Kimforeach my $proto ((@tls, @dtls))
440e71b7053SJung-uk Kim        {
441e71b7053SJung-uk Kim        push(@disablables, $proto);
442e71b7053SJung-uk Kim        push(@disablables, "$proto-method") unless $proto eq "tls1_3";
443e71b7053SJung-uk Kim        }
444e71b7053SJung-uk Kim
445e71b7053SJung-uk Kimmy %deprecated_disablables = (
446e71b7053SJung-uk Kim    "ssl2" => undef,
447e71b7053SJung-uk Kim    "buf-freelists" => undef,
448e71b7053SJung-uk Kim    "ripemd" => "rmd160",
449e71b7053SJung-uk Kim    "ui" => "ui-console",
450e71b7053SJung-uk Kim    );
451e71b7053SJung-uk Kim
452e71b7053SJung-uk Kim# All of the following are disabled by default:
453e71b7053SJung-uk Kim
454e71b7053SJung-uk Kimour %disabled = ( # "what"         => "comment"
455e71b7053SJung-uk Kim                  "asan"                => "default",
456610a21fdSJung-uk Kim                  "buildtest-c++"       => "default",
457e71b7053SJung-uk Kim                  "crypto-mdebug"       => "default",
458e71b7053SJung-uk Kim                  "crypto-mdebug-backtrace" => "default",
459e71b7053SJung-uk Kim                  "devcryptoeng"        => "default",
4601f13597dSJung-uk Kim                  "ec_nistp_64_gcc_128" => "default",
461e71b7053SJung-uk Kim                  "egd"                 => "default",
462e71b7053SJung-uk Kim                  "external-tests"      => "default",
463e71b7053SJung-uk Kim                  "fuzz-libfuzzer"      => "default",
464e71b7053SJung-uk Kim                  "fuzz-afl"            => "default",
465e71b7053SJung-uk Kim                  "heartbeats"          => "default",
4661f13597dSJung-uk Kim                  "md2"                 => "default",
467e71b7053SJung-uk Kim                  "msan"                => "default",
4683b4e3dcbSSimon L. B. Nielsen                  "rc5"                 => "default",
4691f13597dSJung-uk Kim                  "sctp"                => "default",
4707bded2dbSJung-uk Kim                  "ssl-trace"           => "default",
471e71b7053SJung-uk Kim                  "ssl3"                => "default",
472e71b7053SJung-uk Kim                  "ssl3-method"         => "default",
473e71b7053SJung-uk Kim                  "ubsan"               => "default",
474a93cbc2bSJung-uk Kim                  "unit-test"           => "default",
4754c6a0400SJung-uk Kim                  "weak-ssl-ciphers"    => "default",
4763b4e3dcbSSimon L. B. Nielsen                  "zlib"                => "default",
477e71b7053SJung-uk Kim                  "zlib-dynamic"        => "default",
478aa906e2aSJohn Baldwin		  "ktls"                => "default",
4793b4e3dcbSSimon L. B. Nielsen                );
4803b4e3dcbSSimon L. B. Nielsen
481e71b7053SJung-uk Kim# Note: => pair form used for aesthetics, not to truly make a hash table
482e71b7053SJung-uk Kimmy @disable_cascades = (
483e71b7053SJung-uk Kim    # "what"            => [ "cascade", ... ]
484e71b7053SJung-uk Kim    sub { $config{processor} eq "386" }
485e71b7053SJung-uk Kim                        => [ "sse2" ],
486e71b7053SJung-uk Kim    "ssl"               => [ "ssl3" ],
487e71b7053SJung-uk Kim    "ssl3-method"       => [ "ssl3" ],
488e71b7053SJung-uk Kim    "zlib"              => [ "zlib-dynamic" ],
489e71b7053SJung-uk Kim    "des"               => [ "mdc2" ],
490e71b7053SJung-uk Kim    "ec"                => [ "ecdsa", "ecdh" ],
491e71b7053SJung-uk Kim
492e71b7053SJung-uk Kim    "dgram"             => [ "dtls", "sctp" ],
493e71b7053SJung-uk Kim    "sock"              => [ "dgram" ],
494e71b7053SJung-uk Kim    "dtls"              => [ @dtls ],
495e71b7053SJung-uk Kim    sub { 0 == scalar grep { !$disabled{$_} } @dtls }
496e71b7053SJung-uk Kim                        => [ "dtls" ],
497e71b7053SJung-uk Kim
498e71b7053SJung-uk Kim    "tls"               => [ @tls ],
499e71b7053SJung-uk Kim    sub { 0 == scalar grep { !$disabled{$_} } @tls }
500e71b7053SJung-uk Kim                        => [ "tls" ],
501e71b7053SJung-uk Kim
502e71b7053SJung-uk Kim    "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
503e71b7053SJung-uk Kim
504e71b7053SJung-uk Kim    # Without position independent code, there can be no shared libraries or DSOs
505e71b7053SJung-uk Kim    "pic"               => [ "shared" ],
506e71b7053SJung-uk Kim    "shared"            => [ "dynamic-engine" ],
50717f01e99SJung-uk Kim    "dso"               => [ "dynamic-engine" ],
508e71b7053SJung-uk Kim    "engine"            => [ "afalgeng", "devcryptoeng" ],
509e71b7053SJung-uk Kim
510e71b7053SJung-uk Kim    # no-autoalginit is only useful when building non-shared
511e71b7053SJung-uk Kim    "autoalginit"       => [ "shared", "apps" ],
512e71b7053SJung-uk Kim
513e71b7053SJung-uk Kim    "stdio"             => [ "apps", "capieng", "egd" ],
514e71b7053SJung-uk Kim    "apps"              => [ "tests" ],
515e71b7053SJung-uk Kim    "tests"             => [ "external-tests" ],
516e71b7053SJung-uk Kim    "comp"              => [ "zlib" ],
517e71b7053SJung-uk Kim    "ec"                => [ "tls1_3", "sm2" ],
518e71b7053SJung-uk Kim    "sm3"               => [ "sm2" ],
519e71b7053SJung-uk Kim    sub { !$disabled{"unit-test"} } => [ "heartbeats" ],
520e71b7053SJung-uk Kim
521e71b7053SJung-uk Kim    sub { !$disabled{"msan"} } => [ "asm" ],
522e71b7053SJung-uk Kim    );
523e71b7053SJung-uk Kim
524e71b7053SJung-uk Kim# Avoid protocol support holes.  Also disable all versions below N, if version
525e71b7053SJung-uk Kim# N is disabled while N+1 is enabled.
526e71b7053SJung-uk Kim#
527e71b7053SJung-uk Kimmy @list = (reverse @tls);
528e71b7053SJung-uk Kimwhile ((my $first, my $second) = (shift @list, shift @list)) {
529e71b7053SJung-uk Kim    last unless @list;
530e71b7053SJung-uk Kim    push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
531e71b7053SJung-uk Kim                              => [ @list ] );
532e71b7053SJung-uk Kim    unshift @list, $second;
533e71b7053SJung-uk Kim}
534e71b7053SJung-uk Kimmy @list = (reverse @dtls);
535e71b7053SJung-uk Kimwhile ((my $first, my $second) = (shift @list, shift @list)) {
536e71b7053SJung-uk Kim    last unless @list;
537e71b7053SJung-uk Kim    push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
538e71b7053SJung-uk Kim                              => [ @list ] );
539e71b7053SJung-uk Kim    unshift @list, $second;
540e71b7053SJung-uk Kim}
541db522d3aSSimon L. B. Nielsen
542db522d3aSSimon L. B. Nielsen# Explicit "no-..." options will be collected in %disabled along with the defaults.
543e71b7053SJung-uk Kim# To remove something from %disabled, use "enable-foo".
544db522d3aSSimon L. B. Nielsen# For symmetry, "disable-foo" is a synonym for "no-foo".
545db522d3aSSimon L. B. Nielsen
54674664626SKris Kennaway&usage if ($#ARGV < 0);
54774664626SKris Kennaway
54817f01e99SJung-uk Kim# For the "make variables" CPPINCLUDES and CPPDEFINES, we support lists with
549e71b7053SJung-uk Kim# platform specific list separators.  Users from those platforms should
550e71b7053SJung-uk Kim# recognise those separators from how you set up the PATH to find executables.
551e71b7053SJung-uk Kim# The default is the Unix like separator, :, but as an exception, we also
552e71b7053SJung-uk Kim# support the space as separator.
553e71b7053SJung-uk Kimmy $list_separator_re =
554e71b7053SJung-uk Kim    { VMS           => qr/(?<!\^),/,
555e71b7053SJung-uk Kim      MSWin32       => qr/(?<!\\);/ } -> {$^O} // qr/(?<!\\)[:\s]/;
556e71b7053SJung-uk Kim# All the "make variables" we support
557e71b7053SJung-uk Kim# Some get pre-populated for the sake of backward compatibility
558e71b7053SJung-uk Kim# (we supported those before the change to "make variable" support.
559e71b7053SJung-uk Kimmy %user = (
560e71b7053SJung-uk Kim    AR          => env('AR'),
561e71b7053SJung-uk Kim    ARFLAGS     => [],
562e71b7053SJung-uk Kim    AS          => undef,
563e71b7053SJung-uk Kim    ASFLAGS     => [],
564e71b7053SJung-uk Kim    CC          => env('CC'),
565610a21fdSJung-uk Kim    CFLAGS      => [ env('CFLAGS') || () ],
566e71b7053SJung-uk Kim    CXX         => env('CXX'),
567610a21fdSJung-uk Kim    CXXFLAGS    => [ env('CXXFLAGS') || () ],
568e71b7053SJung-uk Kim    CPP         => undef,
569610a21fdSJung-uk Kim    CPPFLAGS    => [ env('CPPFLAGS') || () ],  # -D, -I, -Wp,
570e71b7053SJung-uk Kim    CPPDEFINES  => [],  # Alternative for -D
571e71b7053SJung-uk Kim    CPPINCLUDES => [],  # Alternative for -I
572e71b7053SJung-uk Kim    CROSS_COMPILE => env('CROSS_COMPILE'),
573e71b7053SJung-uk Kim    HASHBANGPERL=> env('HASHBANGPERL') || env('PERL'),
574e71b7053SJung-uk Kim    LD          => undef,
575610a21fdSJung-uk Kim    LDFLAGS     => [ env('LDFLAGS') || () ],  # -L, -Wl,
576610a21fdSJung-uk Kim    LDLIBS      => [ env('LDLIBS') || () ],  # -l
577e71b7053SJung-uk Kim    MT          => undef,
578e71b7053SJung-uk Kim    MTFLAGS     => [],
579e71b7053SJung-uk Kim    PERL        => env('PERL') || ($^O ne "VMS" ? $^X : "perl"),
580e71b7053SJung-uk Kim    RANLIB      => env('RANLIB'),
581e71b7053SJung-uk Kim    RC          => env('RC') || env('WINDRES'),
582610a21fdSJung-uk Kim    RCFLAGS     => [ env('RCFLAGS') || () ],
583e71b7053SJung-uk Kim    RM          => undef,
584e71b7053SJung-uk Kim   );
585e71b7053SJung-uk Kim# Info about what "make variables" may be prefixed with the cross compiler
586e71b7053SJung-uk Kim# prefix.  This should NEVER mention any such variable with a list for value.
587e71b7053SJung-uk Kimmy @user_crossable = qw ( AR AS CC CXX CPP LD MT RANLIB RC );
588e71b7053SJung-uk Kim# The same but for flags given as Configure options.  These are *additional*
589e71b7053SJung-uk Kim# input, as opposed to the VAR=string option that override the corresponding
590e71b7053SJung-uk Kim# config target attributes
591e71b7053SJung-uk Kimmy %useradd = (
592e71b7053SJung-uk Kim    CPPDEFINES  => [],
593e71b7053SJung-uk Kim    CPPINCLUDES => [],
594e71b7053SJung-uk Kim    CPPFLAGS    => [],
595e71b7053SJung-uk Kim    CFLAGS      => [],
596e71b7053SJung-uk Kim    CXXFLAGS    => [],
597e71b7053SJung-uk Kim    LDFLAGS     => [],
598e71b7053SJung-uk Kim    LDLIBS      => [],
599610a21fdSJung-uk Kim    RCFLAGS     => [],
600e71b7053SJung-uk Kim   );
601ddd58736SKris Kennaway
602e71b7053SJung-uk Kimmy %user_synonyms = (
603e71b7053SJung-uk Kim    HASHBANGPERL=> 'PERL',
604e71b7053SJung-uk Kim    RC          => 'WINDRES',
605e71b7053SJung-uk Kim   );
606ddd58736SKris Kennaway
607e71b7053SJung-uk Kim# Some target attributes have been renamed, this is the translation table
608e71b7053SJung-uk Kimmy %target_attr_translate =(
609e71b7053SJung-uk Kim    ar          => 'AR',
610e71b7053SJung-uk Kim    as          => 'AS',
611e71b7053SJung-uk Kim    cc          => 'CC',
612e71b7053SJung-uk Kim    cxx         => 'CXX',
613e71b7053SJung-uk Kim    cpp         => 'CPP',
614e71b7053SJung-uk Kim    hashbangperl => 'HASHBANGPERL',
615e71b7053SJung-uk Kim    ld          => 'LD',
616e71b7053SJung-uk Kim    mt          => 'MT',
617e71b7053SJung-uk Kim    ranlib      => 'RANLIB',
618e71b7053SJung-uk Kim    rc          => 'RC',
619e71b7053SJung-uk Kim    rm          => 'RM',
620e71b7053SJung-uk Kim   );
621e71b7053SJung-uk Kim
622e71b7053SJung-uk Kim# Initialisers coming from 'config' scripts
623e71b7053SJung-uk Kim$config{defines} = [ split(/$list_separator_re/, env('__CNF_CPPDEFINES')) ];
624e71b7053SJung-uk Kim$config{includes} = [ split(/$list_separator_re/, env('__CNF_CPPINCLUDES')) ];
625e71b7053SJung-uk Kim$config{cppflags} = [ env('__CNF_CPPFLAGS') || () ];
626e71b7053SJung-uk Kim$config{cflags} = [ env('__CNF_CFLAGS') || () ];
627e71b7053SJung-uk Kim$config{cxxflags} = [ env('__CNF_CXXFLAGS') || () ];
628e71b7053SJung-uk Kim$config{lflags} = [ env('__CNF_LDFLAGS') || () ];
629e71b7053SJung-uk Kim$config{ex_libs} = [ env('__CNF_LDLIBS') || () ];
630e71b7053SJung-uk Kim
631e71b7053SJung-uk Kim$config{openssl_api_defines}=[];
632e71b7053SJung-uk Kim$config{openssl_algorithm_defines}=[];
633e71b7053SJung-uk Kim$config{openssl_thread_defines}=[];
634e71b7053SJung-uk Kim$config{openssl_sys_defines}=[];
635e71b7053SJung-uk Kim$config{openssl_other_defines}=[];
636e71b7053SJung-uk Kim$config{options}="";
637e71b7053SJung-uk Kim$config{build_type} = "release";
638e71b7053SJung-uk Kimmy $target="";
639e71b7053SJung-uk Kim
640e71b7053SJung-uk Kimmy %cmdvars = ();               # Stores FOO='blah' type arguments
641e71b7053SJung-uk Kimmy %unsupported_options = ();
642e71b7053SJung-uk Kimmy %deprecated_options = ();
643e71b7053SJung-uk Kim# If you change this, update apps/version.c
644e71b7053SJung-uk Kimmy @known_seed_sources = qw(getrandom devrandom os egd none rdcpu librandom);
645e71b7053SJung-uk Kimmy @seed_sources = ();
646e71b7053SJung-uk Kimwhile (@argvcopy)
647ddd58736SKris Kennaway        {
648e71b7053SJung-uk Kim        $_ = shift @argvcopy;
649ddd58736SKris Kennaway
650e71b7053SJung-uk Kim        # Support env variable assignments among the options
651e71b7053SJung-uk Kim        if (m|^(\w+)=(.+)?$|)
65274664626SKris Kennaway                {
653e71b7053SJung-uk Kim                $cmdvars{$1} = $2;
654e71b7053SJung-uk Kim                # Every time a variable is given as a configuration argument,
655e71b7053SJung-uk Kim                # it acts as a reset if the variable.
656e71b7053SJung-uk Kim                if (exists $user{$1})
657e71b7053SJung-uk Kim                        {
658e71b7053SJung-uk Kim                        $user{$1} = ref $user{$1} eq "ARRAY" ? [] : undef;
659e71b7053SJung-uk Kim                        }
660e71b7053SJung-uk Kim                #if (exists $useradd{$1})
661e71b7053SJung-uk Kim                #       {
662e71b7053SJung-uk Kim                #       $useradd{$1} = [];
663e71b7053SJung-uk Kim                #       }
664e71b7053SJung-uk Kim                next;
665e71b7053SJung-uk Kim                }
666e71b7053SJung-uk Kim
667e71b7053SJung-uk Kim        # VMS is a case insensitive environment, and depending on settings
668e71b7053SJung-uk Kim        # out of our control, we may receive options uppercased.  Let's
669e71b7053SJung-uk Kim        # downcase at least the part before any equal sign.
670e71b7053SJung-uk Kim        if ($^O eq "VMS")
671e71b7053SJung-uk Kim                {
672e71b7053SJung-uk Kim                s/^([^=]*)/lc($1)/e;
673e71b7053SJung-uk Kim                }
674e71b7053SJung-uk Kim
675e71b7053SJung-uk Kim        # some people just can't read the instructions, clang people have to...
676e71b7053SJung-uk Kim        s/^-no-(?!integrated-as)/no-/;
6773b4e3dcbSSimon L. B. Nielsen
6783b4e3dcbSSimon L. B. Nielsen        # rewrite some options in "enable-..." form
6793b4e3dcbSSimon L. B. Nielsen        s /^-?-?shared$/enable-shared/;
6801f13597dSJung-uk Kim        s /^sctp$/enable-sctp/;
6813b4e3dcbSSimon L. B. Nielsen        s /^threads$/enable-threads/;
6823b4e3dcbSSimon L. B. Nielsen        s /^zlib$/enable-zlib/;
6833b4e3dcbSSimon L. B. Nielsen        s /^zlib-dynamic$/enable-zlib-dynamic/;
6843b4e3dcbSSimon L. B. Nielsen
685e71b7053SJung-uk Kim        if (/^(no|disable|enable)-(.+)$/)
686e71b7053SJung-uk Kim                {
687e71b7053SJung-uk Kim                my $word = $2;
688e71b7053SJung-uk Kim                if (!exists $deprecated_disablables{$word}
689e71b7053SJung-uk Kim                        && !grep { $word =~ /^${_}$/ } @disablables)
690e71b7053SJung-uk Kim                        {
691e71b7053SJung-uk Kim                        $unsupported_options{$_} = 1;
692e71b7053SJung-uk Kim                        next;
693e71b7053SJung-uk Kim                        }
694e71b7053SJung-uk Kim                }
6953b4e3dcbSSimon L. B. Nielsen        if (/^no-(.+)$/ || /^disable-(.+)$/)
6963b4e3dcbSSimon L. B. Nielsen                {
697e71b7053SJung-uk Kim                foreach my $proto ((@tls, @dtls))
698db522d3aSSimon L. B. Nielsen                        {
699e71b7053SJung-uk Kim                        if ($1 eq "$proto-method")
7003b4e3dcbSSimon L. B. Nielsen                                {
701e71b7053SJung-uk Kim                                $disabled{"$proto"} = "option($proto-method)";
702e71b7053SJung-uk Kim                                last;
703e71b7053SJung-uk Kim                                }
704e71b7053SJung-uk Kim                        }
705e71b7053SJung-uk Kim                if ($1 eq "dtls")
706e71b7053SJung-uk Kim                        {
707e71b7053SJung-uk Kim                        foreach my $proto (@dtls)
708e71b7053SJung-uk Kim                                {
709e71b7053SJung-uk Kim                                $disabled{$proto} = "option(dtls)";
710e71b7053SJung-uk Kim                                }
711e71b7053SJung-uk Kim                        $disabled{"dtls"} = "option(dtls)";
712e71b7053SJung-uk Kim                        }
713e71b7053SJung-uk Kim                elsif ($1 eq "ssl")
714e71b7053SJung-uk Kim                        {
715e71b7053SJung-uk Kim                        # Last one of its kind
7163b4e3dcbSSimon L. B. Nielsen                        $disabled{"ssl3"} = "option(ssl)";
7173b4e3dcbSSimon L. B. Nielsen                        }
7183b4e3dcbSSimon L. B. Nielsen                elsif ($1 eq "tls")
7193b4e3dcbSSimon L. B. Nielsen                        {
720e71b7053SJung-uk Kim                        # XXX: Tests will fail if all SSL/TLS
721e71b7053SJung-uk Kim                        # protocols are disabled.
722e71b7053SJung-uk Kim                        foreach my $proto (@tls)
723751d2991SJung-uk Kim                                {
724e71b7053SJung-uk Kim                                $disabled{$proto} = "option(tls)";
725e71b7053SJung-uk Kim                                }
726e71b7053SJung-uk Kim                        }
727e71b7053SJung-uk Kim                elsif ($1 eq "static-engine")
728e71b7053SJung-uk Kim                        {
729e71b7053SJung-uk Kim                        delete $disabled{"dynamic-engine"};
730e71b7053SJung-uk Kim                        }
731e71b7053SJung-uk Kim                elsif ($1 eq "dynamic-engine")
732e71b7053SJung-uk Kim                        {
733e71b7053SJung-uk Kim                        $disabled{"dynamic-engine"} = "option";
734e71b7053SJung-uk Kim                        }
735e71b7053SJung-uk Kim                elsif (exists $deprecated_disablables{$1})
736e71b7053SJung-uk Kim                        {
737610a21fdSJung-uk Kim                        if ($deprecated_disablables{$1} ne "")
738610a21fdSJung-uk Kim                                {
739e71b7053SJung-uk Kim                                $deprecated_options{$_} = 1;
740e71b7053SJung-uk Kim                                if (defined $deprecated_disablables{$1})
741e71b7053SJung-uk Kim                                        {
742e71b7053SJung-uk Kim                                        $disabled{$deprecated_disablables{$1}} = "option";
743e71b7053SJung-uk Kim                                        }
744751d2991SJung-uk Kim                                }
745610a21fdSJung-uk Kim                        }
7463b4e3dcbSSimon L. B. Nielsen                else
7473b4e3dcbSSimon L. B. Nielsen                        {
7483b4e3dcbSSimon L. B. Nielsen                        $disabled{$1} = "option";
7493b4e3dcbSSimon L. B. Nielsen                        }
750e71b7053SJung-uk Kim                # No longer an automatic choice
751e71b7053SJung-uk Kim                $auto_threads = 0 if ($1 eq "threads");
7523b4e3dcbSSimon L. B. Nielsen                }
753e71b7053SJung-uk Kim        elsif (/^enable-(.+)$/)
7543b4e3dcbSSimon L. B. Nielsen                {
755e71b7053SJung-uk Kim                if ($1 eq "static-engine")
756e71b7053SJung-uk Kim                        {
757e71b7053SJung-uk Kim                        $disabled{"dynamic-engine"} = "option";
758e71b7053SJung-uk Kim                        }
759e71b7053SJung-uk Kim                elsif ($1 eq "dynamic-engine")
760e71b7053SJung-uk Kim                        {
761e71b7053SJung-uk Kim                        delete $disabled{"dynamic-engine"};
762e71b7053SJung-uk Kim                        }
763e71b7053SJung-uk Kim                elsif ($1 eq "zlib-dynamic")
764e71b7053SJung-uk Kim                        {
765e71b7053SJung-uk Kim                        delete $disabled{"zlib"};
766e71b7053SJung-uk Kim                        }
767db522d3aSSimon L. B. Nielsen                my $algo = $1;
768db522d3aSSimon L. B. Nielsen                delete $disabled{$algo};
7693b4e3dcbSSimon L. B. Nielsen
770e71b7053SJung-uk Kim                # No longer an automatic choice
771e71b7053SJung-uk Kim                $auto_threads = 0 if ($1 eq "threads");
7723b4e3dcbSSimon L. B. Nielsen                }
773e71b7053SJung-uk Kim        elsif (/^--strict-warnings$/)
7746a599222SSimon L. B. Nielsen                {
775610a21fdSJung-uk Kim                # Pretend that our strict flags is a C flag, and replace it
776610a21fdSJung-uk Kim                # with the proper flags later on
777610a21fdSJung-uk Kim                push @{$useradd{CFLAGS}}, '--ossl-strict-warnings';
7786a599222SSimon L. B. Nielsen                $strict_warnings=1;
7796a599222SSimon L. B. Nielsen                }
780e71b7053SJung-uk Kim        elsif (/^--debug$/)
781ddd58736SKris Kennaway                {
782e71b7053SJung-uk Kim                $config{build_type} = "debug";
783ddd58736SKris Kennaway                }
784e71b7053SJung-uk Kim        elsif (/^--release$/)
785e71b7053SJung-uk Kim                {
786e71b7053SJung-uk Kim                $config{build_type} = "release";
787ddd58736SKris Kennaway                }
78874664626SKris Kennaway        elsif (/^386$/)
789e71b7053SJung-uk Kim                { $config{processor}=386; }
790db522d3aSSimon L. B. Nielsen        elsif (/^fips$/)
791db522d3aSSimon L. B. Nielsen                {
792e71b7053SJung-uk Kim                die "FIPS mode not supported\n";
793db522d3aSSimon L. B. Nielsen                }
79474664626SKris Kennaway        elsif (/^rsaref$/)
79574664626SKris Kennaway                {
7965c87c606SMark Murray                # No RSAref support any more since it's not needed.
7975c87c606SMark Murray                # The check for the option is there so scripts aren't
7985c87c606SMark Murray                # broken
79974664626SKris Kennaway                }
800e71b7053SJung-uk Kim        elsif (/^nofipscanistercheck$/)
801e71b7053SJung-uk Kim                {
802e71b7053SJung-uk Kim                die "FIPS mode not supported\n";
803e71b7053SJung-uk Kim                }
80417f01e99SJung-uk Kim        elsif (m|^[-+/]|)
80574664626SKris Kennaway                {
8067bded2dbSJung-uk Kim                if (/^--prefix=(.*)$/)
80774664626SKris Kennaway                        {
808e71b7053SJung-uk Kim                        $config{prefix}=$1;
809e71b7053SJung-uk Kim                        die "Directory given with --prefix MUST be absolute\n"
810e71b7053SJung-uk Kim                                unless file_name_is_absolute($config{prefix});
811e71b7053SJung-uk Kim                        }
812e71b7053SJung-uk Kim                elsif (/^--api=(.*)$/)
813e71b7053SJung-uk Kim                        {
814e71b7053SJung-uk Kim                        $config{api}=$1;
81574664626SKris Kennaway                        }
8166a599222SSimon L. B. Nielsen                elsif (/^--libdir=(.*)$/)
8176a599222SSimon L. B. Nielsen                        {
818e71b7053SJung-uk Kim                        $config{libdir}=$1;
8196a599222SSimon L. B. Nielsen                        }
82074664626SKris Kennaway                elsif (/^--openssldir=(.*)$/)
82174664626SKris Kennaway                        {
822e71b7053SJung-uk Kim                        $config{openssldir}=$1;
8235c87c606SMark Murray                        }
8243b4e3dcbSSimon L. B. Nielsen                elsif (/^--with-zlib-lib=(.*)$/)
8253b4e3dcbSSimon L. B. Nielsen                        {
826e71b7053SJung-uk Kim                        $withargs{zlib_lib}=$1;
8273b4e3dcbSSimon L. B. Nielsen                        }
8283b4e3dcbSSimon L. B. Nielsen                elsif (/^--with-zlib-include=(.*)$/)
8293b4e3dcbSSimon L. B. Nielsen                        {
830e71b7053SJung-uk Kim                        $withargs{zlib_include}=$1;
8313b4e3dcbSSimon L. B. Nielsen                        }
832e71b7053SJung-uk Kim                elsif (/^--with-fuzzer-lib=(.*)$/)
8331f13597dSJung-uk Kim                        {
834e71b7053SJung-uk Kim                        $withargs{fuzzer_lib}=$1;
8351f13597dSJung-uk Kim                        }
836e71b7053SJung-uk Kim                elsif (/^--with-fuzzer-include=(.*)$/)
837db522d3aSSimon L. B. Nielsen                        {
838e71b7053SJung-uk Kim                        $withargs{fuzzer_include}=$1;
839db522d3aSSimon L. B. Nielsen                        }
840e71b7053SJung-uk Kim                elsif (/^--with-rand-seed=(.*)$/)
841db522d3aSSimon L. B. Nielsen                        {
842e71b7053SJung-uk Kim                        foreach my $x (split(m|,|, $1))
843e71b7053SJung-uk Kim                            {
844e71b7053SJung-uk Kim                            die "Unknown --with-rand-seed choice $x\n"
845e71b7053SJung-uk Kim                                if ! grep { $x eq $_ } @known_seed_sources;
846e71b7053SJung-uk Kim                            push @seed_sources, $x;
847e71b7053SJung-uk Kim                            }
848db522d3aSSimon L. B. Nielsen                        }
8491f13597dSJung-uk Kim                elsif (/^--cross-compile-prefix=(.*)$/)
8501f13597dSJung-uk Kim                        {
851e71b7053SJung-uk Kim                        $user{CROSS_COMPILE}=$1;
8521f13597dSJung-uk Kim                        }
853e71b7053SJung-uk Kim                elsif (/^--config=(.*)$/)
85474664626SKris Kennaway                        {
855e71b7053SJung-uk Kim                        read_config $1;
856e71b7053SJung-uk Kim                        }
857e71b7053SJung-uk Kim                elsif (/^-l(.*)$/)
858e71b7053SJung-uk Kim                        {
859e71b7053SJung-uk Kim                        push @{$useradd{LDLIBS}}, $_;
860e71b7053SJung-uk Kim                        }
861e71b7053SJung-uk Kim                elsif (/^-framework$/)
862e71b7053SJung-uk Kim                        {
863e71b7053SJung-uk Kim                        push @{$useradd{LDLIBS}}, $_, shift(@argvcopy);
864e71b7053SJung-uk Kim                        }
865e71b7053SJung-uk Kim                elsif (/^-L(.*)$/ or /^-Wl,/)
866e71b7053SJung-uk Kim                        {
867e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_;
868e71b7053SJung-uk Kim                        }
869e71b7053SJung-uk Kim                elsif (/^-rpath$/ or /^-R$/)
870e71b7053SJung-uk Kim                        # -rpath is the OSF1 rpath flag
871e71b7053SJung-uk Kim                        # -R is the old Solaris rpath flag
872e71b7053SJung-uk Kim                        {
873e71b7053SJung-uk Kim                        my $rpath = shift(@argvcopy) || "";
874e71b7053SJung-uk Kim                        $rpath .= " " if $rpath ne "";
875e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_, $rpath;
876e71b7053SJung-uk Kim                        }
877e71b7053SJung-uk Kim                elsif (/^-static$/)
878e71b7053SJung-uk Kim                        {
879e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_;
880e71b7053SJung-uk Kim                        }
88117f01e99SJung-uk Kim                elsif (m|^[-/]D(.*)$|)
882e71b7053SJung-uk Kim                        {
883e71b7053SJung-uk Kim                        push @{$useradd{CPPDEFINES}}, $1;
884e71b7053SJung-uk Kim                        }
88517f01e99SJung-uk Kim                elsif (m|^[-/]I(.*)$|)
886e71b7053SJung-uk Kim                        {
887e71b7053SJung-uk Kim                        push @{$useradd{CPPINCLUDES}}, $1;
888e71b7053SJung-uk Kim                        }
889e71b7053SJung-uk Kim                elsif (/^-Wp,$/)
890e71b7053SJung-uk Kim                        {
891e71b7053SJung-uk Kim                        push @{$useradd{CPPFLAGS}}, $1;
8927bded2dbSJung-uk Kim                        }
8937bded2dbSJung-uk Kim                else    # common if (/^[-+]/), just pass down...
8947bded2dbSJung-uk Kim                        {
89517f01e99SJung-uk Kim                        # Treat %xx as an ASCII code (e.g. replace %20 by a space character).
89617f01e99SJung-uk Kim                        # This provides a simple way to pass options with arguments separated
89717f01e99SJung-uk Kim                        # by spaces without quoting (e.g. -opt%20arg translates to -opt arg).
8987bded2dbSJung-uk Kim                        $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
899e71b7053SJung-uk Kim                        push @{$useradd{CFLAGS}}, $_;
900e71b7053SJung-uk Kim                        push @{$useradd{CXXFLAGS}}, $_;
90174664626SKris Kennaway                        }
90274664626SKris Kennaway                }
90317f01e99SJung-uk Kim        elsif (m|^/|)
90417f01e99SJung-uk Kim                {
90517f01e99SJung-uk Kim                # Treat %xx as an ASCII code (e.g. replace %20 by a space character).
90617f01e99SJung-uk Kim                # This provides a simple way to pass options with arguments separated
90717f01e99SJung-uk Kim                # by spaces without quoting (e.g. /opt%20arg translates to /opt arg).
90817f01e99SJung-uk Kim                $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
90917f01e99SJung-uk Kim                push @{$useradd{CFLAGS}}, $_;
91017f01e99SJung-uk Kim                push @{$useradd{CXXFLAGS}}, $_;
91117f01e99SJung-uk Kim                }
91274664626SKris Kennaway        else
91374664626SKris Kennaway                {
9143b4e3dcbSSimon L. B. Nielsen                die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
91574664626SKris Kennaway                $target=$_;
91674664626SKris Kennaway                }
9173b4e3dcbSSimon L. B. Nielsen        unless ($_ eq $target || /^no-/ || /^disable-/)
9185c87c606SMark Murray                {
919e71b7053SJung-uk Kim                # "no-..." follows later after implied deactivations
920e71b7053SJung-uk Kim                # have been derived.  (Don't take this too seriously,
9213b4e3dcbSSimon L. B. Nielsen                # we really only write OPTIONS to the Makefile out of
9223b4e3dcbSSimon L. B. Nielsen                # nostalgia.)
9233b4e3dcbSSimon L. B. Nielsen
924e71b7053SJung-uk Kim                if ($config{options} eq "")
925e71b7053SJung-uk Kim                        { $config{options} = $_; }
9263b4e3dcbSSimon L. B. Nielsen                else
927e71b7053SJung-uk Kim                        { $config{options} .= " ".$_; }
9283b4e3dcbSSimon L. B. Nielsen                }
9295c87c606SMark Murray        }
9305c87c606SMark Murray
931e71b7053SJung-uk Kimif (defined($config{api}) && !exists $apitable->{$config{api}}) {
932e71b7053SJung-uk Kim        die "***** Unsupported api compatibility level: $config{api}\n",
933e71b7053SJung-uk Kim}
9343b4e3dcbSSimon L. B. Nielsen
935e71b7053SJung-uk Kimif (keys %deprecated_options)
9365c87c606SMark Murray        {
937e71b7053SJung-uk Kim        warn "***** Deprecated options: ",
938e71b7053SJung-uk Kim                join(", ", keys %deprecated_options), "\n";
939e71b7053SJung-uk Kim        }
940e71b7053SJung-uk Kimif (keys %unsupported_options)
941e71b7053SJung-uk Kim        {
942e71b7053SJung-uk Kim        die "***** Unsupported options: ",
943e71b7053SJung-uk Kim                join(", ", keys %unsupported_options), "\n";
9445c87c606SMark Murray        }
9455c87c606SMark Murray
946e71b7053SJung-uk Kim# If any %useradd entry has been set, we must check that the "make
947e71b7053SJung-uk Kim# variables" haven't been set.  We start by checking of any %useradd entry
948e71b7053SJung-uk Kim# is set.
949e71b7053SJung-uk Kimif (grep { scalar @$_ > 0 } values %useradd) {
950e71b7053SJung-uk Kim    # Hash of env / make variables names.  The possible values are:
951e71b7053SJung-uk Kim    # 1 - "make vars"
952e71b7053SJung-uk Kim    # 2 - %useradd entry set
953e71b7053SJung-uk Kim    # 3 - both set
954e71b7053SJung-uk Kim    my %detected_vars =
955e71b7053SJung-uk Kim        map { my $v = 0;
956e71b7053SJung-uk Kim              $v += 1 if $cmdvars{$_};
957e71b7053SJung-uk Kim              $v += 2 if @{$useradd{$_}};
958e71b7053SJung-uk Kim              $_ => $v }
959e71b7053SJung-uk Kim        keys %useradd;
960e71b7053SJung-uk Kim
961e71b7053SJung-uk Kim    # If any of the corresponding "make variables" is set, we error
962e71b7053SJung-uk Kim    if (grep { $_ & 1 } values %detected_vars) {
963e71b7053SJung-uk Kim        my $names = join(', ', grep { $detected_vars{$_} > 0 }
964e71b7053SJung-uk Kim                               sort keys %detected_vars);
965e71b7053SJung-uk Kim        die <<"_____";
966e71b7053SJung-uk Kim***** Mixing make variables and additional compiler/linker flags as
967e71b7053SJung-uk Kim***** configure command line option is not permitted.
968e71b7053SJung-uk Kim***** Affected make variables: $names
969e71b7053SJung-uk Kim_____
970e71b7053SJung-uk Kim    }
9715c87c606SMark Murray}
9725c87c606SMark Murray
973e71b7053SJung-uk Kim# Check through all supported command line variables to see if any of them
974e71b7053SJung-uk Kim# were set, and canonicalise the values we got.  If no compiler or linker
975e71b7053SJung-uk Kim# flag or anything else that affects %useradd was set, we also check the
976e71b7053SJung-uk Kim# environment for values.
977e71b7053SJung-uk Kimmy $anyuseradd =
978e71b7053SJung-uk Kim    grep { defined $_ && (ref $_ ne 'ARRAY' || @$_) } values %useradd;
979e71b7053SJung-uk Kimforeach (keys %user) {
980e71b7053SJung-uk Kim    my $value = $cmdvars{$_};
981e71b7053SJung-uk Kim    $value //= env($_) unless $anyuseradd;
982e71b7053SJung-uk Kim    $value //=
983e71b7053SJung-uk Kim        defined $user_synonyms{$_} ? $cmdvars{$user_synonyms{$_}} : undef;
984e71b7053SJung-uk Kim    $value //= defined $user_synonyms{$_} ? env($user_synonyms{$_}) : undef
985e71b7053SJung-uk Kim        unless $anyuseradd;
986e71b7053SJung-uk Kim
987e71b7053SJung-uk Kim    if (defined $value) {
988e71b7053SJung-uk Kim        if (ref $user{$_} eq 'ARRAY') {
98917f01e99SJung-uk Kim            if ($_ eq 'CPPDEFINES' || $_ eq 'CPPINCLUDES') {
990e71b7053SJung-uk Kim                $user{$_} = [ split /$list_separator_re/, $value ];
99117f01e99SJung-uk Kim            } else {
99217f01e99SJung-uk Kim                $user{$_} = [ $value ];
99317f01e99SJung-uk Kim            }
994e71b7053SJung-uk Kim        } elsif (!defined $user{$_}) {
995e71b7053SJung-uk Kim            $user{$_} = $value;
996e71b7053SJung-uk Kim        }
997e71b7053SJung-uk Kim    }
9983b4e3dcbSSimon L. B. Nielsen}
9993b4e3dcbSSimon L. B. Nielsen
1000e71b7053SJung-uk Kimif (grep { /-rpath\b/ } ($user{LDFLAGS} ? @{$user{LDFLAGS}} : ())
1001e71b7053SJung-uk Kim    && !$disabled{shared}
1002e71b7053SJung-uk Kim    && !($disabled{asan} && $disabled{msan} && $disabled{ubsan})) {
1003e71b7053SJung-uk Kim    die "***** Cannot simultaneously use -rpath, shared libraries, and\n",
1004e71b7053SJung-uk Kim        "***** any of asan, msan or ubsan\n";
10053b4e3dcbSSimon L. B. Nielsen}
10063b4e3dcbSSimon L. B. Nielsen
1007610a21fdSJung-uk Kimsub disable {
1008610a21fdSJung-uk Kim    my $disable_type = shift;
1009610a21fdSJung-uk Kim
1010610a21fdSJung-uk Kim    for (@_) {
1011610a21fdSJung-uk Kim        $disabled{$_} = $disable_type;
1012610a21fdSJung-uk Kim    }
1013610a21fdSJung-uk Kim
1014610a21fdSJung-uk Kim    my @tocheckfor = (@_ ? @_ : keys %disabled);
1015e71b7053SJung-uk Kim    while (@tocheckfor) {
1016e71b7053SJung-uk Kim        my %new_tocheckfor = ();
1017e71b7053SJung-uk Kim        my @cascade_copy = (@disable_cascades);
1018e71b7053SJung-uk Kim        while (@cascade_copy) {
1019610a21fdSJung-uk Kim            my ($test, $descendents) =
1020610a21fdSJung-uk Kim                (shift @cascade_copy, shift @cascade_copy);
1021e71b7053SJung-uk Kim            if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
1022e71b7053SJung-uk Kim                foreach (grep { !defined($disabled{$_}) } @$descendents) {
1023610a21fdSJung-uk Kim                    $new_tocheckfor{$_} = 1; $disabled{$_} = "cascade";
1024e71b7053SJung-uk Kim                }
1025e71b7053SJung-uk Kim            }
1026e71b7053SJung-uk Kim        }
1027e71b7053SJung-uk Kim        @tocheckfor = (keys %new_tocheckfor);
10283b4e3dcbSSimon L. B. Nielsen    }
1029610a21fdSJung-uk Kim}
1030610a21fdSJung-uk Kimdisable();                     # First cascade run
10313b4e3dcbSSimon L. B. Nielsen
1032e71b7053SJung-uk Kimour $die = sub { die @_; };
103374664626SKris Kennawayif ($target eq "TABLE") {
1034e71b7053SJung-uk Kim    local $die = sub { warn @_; };
1035e71b7053SJung-uk Kim    foreach (sort keys %table) {
1036e71b7053SJung-uk Kim        print_table_entry($_, "TABLE");
103774664626SKris Kennaway    }
103874664626SKris Kennaway    exit 0;
103974664626SKris Kennaway}
104074664626SKris Kennaway
1041f579bf8eSKris Kennawayif ($target eq "LIST") {
1042f579bf8eSKris Kennaway    foreach (sort keys %table) {
1043e71b7053SJung-uk Kim        print $_,"\n" unless $table{$_}->{template};
1044f579bf8eSKris Kennaway    }
1045f579bf8eSKris Kennaway    exit 0;
1046f579bf8eSKris Kennaway}
1047f579bf8eSKris Kennaway
1048e71b7053SJung-uk Kimif ($target eq "HASH") {
1049e71b7053SJung-uk Kim    local $die = sub { warn @_; };
1050e71b7053SJung-uk Kim    print "%table = (\n";
1051e71b7053SJung-uk Kim    foreach (sort keys %table) {
1052e71b7053SJung-uk Kim        print_table_entry($_, "HASH");
1053e71b7053SJung-uk Kim    }
1054e71b7053SJung-uk Kim    exit 0;
1055e71b7053SJung-uk Kim}
1056e71b7053SJung-uk Kim
1057e71b7053SJung-uk Kimprint "Configuring OpenSSL version $config{version} ($config{version_num}) ";
1058e71b7053SJung-uk Kimprint "for $target\n";
1059e71b7053SJung-uk Kim
1060e71b7053SJung-uk Kimif (scalar(@seed_sources) == 0) {
1061e71b7053SJung-uk Kim    print "Using os-specific seed configuration\n";
1062e71b7053SJung-uk Kim    push @seed_sources, 'os';
1063e71b7053SJung-uk Kim}
106458f35182SJung-uk Kimif (scalar(grep { $_ eq 'egd' } @seed_sources) > 0) {
106558f35182SJung-uk Kim    delete $disabled{'egd'};
106658f35182SJung-uk Kim}
1067e71b7053SJung-uk Kimif (scalar(grep { $_ eq 'none' } @seed_sources) > 0) {
1068e71b7053SJung-uk Kim    die "Cannot seed with none and anything else" if scalar(@seed_sources) > 1;
1069e71b7053SJung-uk Kim    warn <<_____ if scalar(@seed_sources) == 1;
1070e71b7053SJung-uk Kim
1071c9cf7b5cSJung-uk Kim============================== WARNING ===============================
1072c9cf7b5cSJung-uk KimYou have selected the --with-rand-seed=none option, which effectively
1073c9cf7b5cSJung-uk Kimdisables automatic reseeding of the OpenSSL random generator.
1074c9cf7b5cSJung-uk KimAll operations depending on the random generator such as creating keys
1075c9cf7b5cSJung-uk Kimwill not work unless the random generator is seeded manually by the
1076c9cf7b5cSJung-uk Kimapplication.
1077c9cf7b5cSJung-uk Kim
1078c9cf7b5cSJung-uk KimPlease read the 'Note on random number generation' section in the
1079c9cf7b5cSJung-uk KimINSTALL instructions and the RAND_DRBG(7) manual page for more details.
1080c9cf7b5cSJung-uk Kim============================== WARNING ===============================
1081c9cf7b5cSJung-uk Kim
1082e71b7053SJung-uk Kim_____
1083e71b7053SJung-uk Kim}
1084e71b7053SJung-uk Kimpush @{$config{openssl_other_defines}},
1085e71b7053SJung-uk Kim     map { (my $x = $_) =~ tr|[\-a-z]|[_A-Z]|; "OPENSSL_RAND_SEED_$x" }
1086e71b7053SJung-uk Kim        @seed_sources;
1087e71b7053SJung-uk Kim
1088e71b7053SJung-uk Kim# Backward compatibility?
1089c1803d78SJacques Vidrineif ($target =~ m/^CygWin32(-.*)$/) {
1090c1803d78SJacques Vidrine    $target = "Cygwin".$1;
1091c1803d78SJacques Vidrine}
1092c1803d78SJacques Vidrine
1093e71b7053SJung-uk Kim# Support for legacy targets having a name starting with 'debug-'
1094e71b7053SJung-uk Kimmy ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
1095e71b7053SJung-uk Kimif ($d) {
1096e71b7053SJung-uk Kim    $config{build_type} = "debug";
1097ddd58736SKris Kennaway
1098e71b7053SJung-uk Kim    # If we do not find debug-foo in the table, the target is set to foo.
1099e71b7053SJung-uk Kim    if (!$table{$target}) {
1100e71b7053SJung-uk Kim        $target = $t;
11011f13597dSJung-uk Kim    }
1102e71b7053SJung-uk Kim}
11031f13597dSJung-uk Kim
1104e71b7053SJung-uk Kim&usage if !$table{$target} || $table{$target}->{template};
1105e71b7053SJung-uk Kim
1106e71b7053SJung-uk Kim$config{target} = $target;
1107e71b7053SJung-uk Kimmy %target = resolve_config($target);
1108e71b7053SJung-uk Kim
1109e71b7053SJung-uk Kimforeach (keys %target_attr_translate) {
1110e71b7053SJung-uk Kim    $target{$target_attr_translate{$_}} = $target{$_}
1111e71b7053SJung-uk Kim        if $target{$_};
1112e71b7053SJung-uk Kim    delete $target{$_};
1113e71b7053SJung-uk Kim}
1114e71b7053SJung-uk Kim
1115e71b7053SJung-uk Kim%target = ( %{$table{DEFAULTS}}, %target );
1116e71b7053SJung-uk Kim
1117e71b7053SJung-uk Kimmy %conf_files = map { $_ => 1 } (@{$target{_conf_fname_int}});
1118e71b7053SJung-uk Kim$config{conf_files} = [ sort keys %conf_files ];
1119e71b7053SJung-uk Kim
1120610a21fdSJung-uk Kim# Using sub disable within these loops may prove fragile, so we run
1121610a21fdSJung-uk Kim# a cascade afterwards
1122e71b7053SJung-uk Kimforeach my $feature (@{$target{disable}}) {
1123e71b7053SJung-uk Kim    if (exists $deprecated_disablables{$feature}) {
1124e71b7053SJung-uk Kim        warn "***** config $target disables deprecated feature $feature\n";
1125e71b7053SJung-uk Kim    } elsif (!grep { $feature eq $_ } @disablables) {
1126e71b7053SJung-uk Kim        die "***** config $target disables unknown feature $feature\n";
1127e71b7053SJung-uk Kim    }
1128e71b7053SJung-uk Kim    $disabled{$feature} = 'config';
1129e71b7053SJung-uk Kim}
1130e71b7053SJung-uk Kimforeach my $feature (@{$target{enable}}) {
11316935a639SJung-uk Kim    if ("default" eq ($disabled{$feature} // "")) {
1132e71b7053SJung-uk Kim        if (exists $deprecated_disablables{$feature}) {
1133e71b7053SJung-uk Kim            warn "***** config $target enables deprecated feature $feature\n";
1134e71b7053SJung-uk Kim        } elsif (!grep { $feature eq $_ } @disablables) {
1135e71b7053SJung-uk Kim            die "***** config $target enables unknown feature $feature\n";
1136e71b7053SJung-uk Kim        }
11376935a639SJung-uk Kim        delete $disabled{$feature};
1138e71b7053SJung-uk Kim    }
1139e71b7053SJung-uk Kim}
1140610a21fdSJung-uk Kimdisable();                      # Run a cascade now
1141e71b7053SJung-uk Kim
1142e71b7053SJung-uk Kim$target{CXXFLAGS}//=$target{CFLAGS} if $target{CXX};
1143e71b7053SJung-uk Kim$target{cxxflags}//=$target{cflags} if $target{CXX};
1144e71b7053SJung-uk Kim$target{exe_extension}="";
1145e71b7053SJung-uk Kim$target{exe_extension}=".exe" if ($config{target} eq "DJGPP"
1146e71b7053SJung-uk Kim                                  || $config{target} =~ /^(?:Cygwin|mingw)/);
1147e71b7053SJung-uk Kim$target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
1148e71b7053SJung-uk Kim
1149e71b7053SJung-uk Kim($target{shared_extension_simple}=$target{shared_extension})
1150e71b7053SJung-uk Kim    =~ s|\.\$\(SHLIB_VERSION_NUMBER\)||
1151e71b7053SJung-uk Kim    unless defined($target{shared_extension_simple});
1152e71b7053SJung-uk Kim$target{dso_extension}//=$target{shared_extension_simple};
1153e71b7053SJung-uk Kim($target{shared_import_extension}=$target{shared_extension_simple}.".a")
1154e71b7053SJung-uk Kim    if ($config{target} =~ /^(?:Cygwin|mingw)/);
1155e71b7053SJung-uk Kim
1156e71b7053SJung-uk Kim# Fill %config with values from %user, and in case those are undefined or
1157e71b7053SJung-uk Kim# empty, use values from %target (acting as a default).
1158e71b7053SJung-uk Kimforeach (keys %user) {
1159e71b7053SJung-uk Kim    my $ref_type = ref $user{$_};
1160e71b7053SJung-uk Kim
1161e71b7053SJung-uk Kim    # Temporary function.  Takes an intended ref type (empty string or "ARRAY")
1162e71b7053SJung-uk Kim    # and a value that's to be coerced into that type.
1163e71b7053SJung-uk Kim    my $mkvalue = sub {
1164e71b7053SJung-uk Kim        my $type = shift;
1165e71b7053SJung-uk Kim        my $value = shift;
1166e71b7053SJung-uk Kim        my $undef_p = shift;
1167e71b7053SJung-uk Kim
1168e71b7053SJung-uk Kim        die "Too many arguments for \$mkvalue" if @_;
1169e71b7053SJung-uk Kim
1170e71b7053SJung-uk Kim        while (ref $value eq 'CODE') {
1171e71b7053SJung-uk Kim            $value = $value->();
1172e71b7053SJung-uk Kim        }
1173e71b7053SJung-uk Kim
1174e71b7053SJung-uk Kim        if ($type eq 'ARRAY') {
1175e71b7053SJung-uk Kim            return undef unless defined $value;
1176e71b7053SJung-uk Kim            return undef if ref $value ne 'ARRAY' && !$value;
1177e71b7053SJung-uk Kim            return undef if ref $value eq 'ARRAY' && !@$value;
1178e71b7053SJung-uk Kim            return [ $value ] unless ref $value eq 'ARRAY';
1179e71b7053SJung-uk Kim        }
1180e71b7053SJung-uk Kim        return undef unless $value;
1181e71b7053SJung-uk Kim        return $value;
1182e71b7053SJung-uk Kim    };
1183e71b7053SJung-uk Kim
1184e71b7053SJung-uk Kim    $config{$_} =
1185e71b7053SJung-uk Kim        $mkvalue->($ref_type, $user{$_})
1186e71b7053SJung-uk Kim        || $mkvalue->($ref_type, $target{$_});
1187e71b7053SJung-uk Kim    delete $config{$_} unless defined $config{$_};
1188e71b7053SJung-uk Kim}
1189e71b7053SJung-uk Kim
1190610a21fdSJung-uk Kim# Finish up %config by appending things the user gave us on the command line
1191610a21fdSJung-uk Kim# apart from "make variables"
1192610a21fdSJung-uk Kimforeach (keys %useradd) {
1193610a21fdSJung-uk Kim    # The must all be lists, so we assert that here
1194610a21fdSJung-uk Kim    die "internal error: \$useradd{$_} isn't an ARRAY\n"
1195610a21fdSJung-uk Kim        unless ref $useradd{$_} eq 'ARRAY';
1196610a21fdSJung-uk Kim
1197610a21fdSJung-uk Kim    if (defined $config{$_}) {
1198610a21fdSJung-uk Kim        push @{$config{$_}}, @{$useradd{$_}};
1199610a21fdSJung-uk Kim    } else {
1200610a21fdSJung-uk Kim        $config{$_} = [ @{$useradd{$_}} ];
1201610a21fdSJung-uk Kim    }
1202610a21fdSJung-uk Kim}
1203610a21fdSJung-uk Kim# At this point, we can forget everything about %user and %useradd,
1204610a21fdSJung-uk Kim# because it's now all been merged into the corresponding $config entry
1205610a21fdSJung-uk Kim
1206*88e852c0SJung-uk Kimif (grep { $_ eq '-static' } @{$config{LDFLAGS}}) {
1207*88e852c0SJung-uk Kim    disable('static', 'pic', 'threads');
1208*88e852c0SJung-uk Kim}
1209*88e852c0SJung-uk Kim
1210e71b7053SJung-uk Kim# Allow overriding the build file name
1211e71b7053SJung-uk Kim$config{build_file} = env('BUILDFILE') || $target{build_file} || "Makefile";
1212e71b7053SJung-uk Kim
1213e71b7053SJung-uk Kim# Make sure build_scheme is consistent.
1214e71b7053SJung-uk Kim$target{build_scheme} = [ $target{build_scheme} ]
1215e71b7053SJung-uk Kim    if ref($target{build_scheme}) ne "ARRAY";
1216e71b7053SJung-uk Kim
1217e71b7053SJung-uk Kimmy ($builder, $builder_platform, @builder_opts) =
1218e71b7053SJung-uk Kim    @{$target{build_scheme}};
1219e71b7053SJung-uk Kim
1220e71b7053SJung-uk Kimforeach my $checker (($builder_platform."-".$target{build_file}."-checker.pm",
1221e71b7053SJung-uk Kim                      $builder_platform."-checker.pm")) {
1222e71b7053SJung-uk Kim    my $checker_path = catfile($srcdir, "Configurations", $checker);
1223e71b7053SJung-uk Kim    if (-f $checker_path) {
1224e71b7053SJung-uk Kim        my $fn = $ENV{CONFIGURE_CHECKER_WARN}
1225e71b7053SJung-uk Kim            ? sub { warn $@; } : sub { die $@; };
1226e71b7053SJung-uk Kim        if (! do $checker_path) {
1227e71b7053SJung-uk Kim            if ($@) {
1228e71b7053SJung-uk Kim                $fn->($@);
1229e71b7053SJung-uk Kim            } elsif ($!) {
1230e71b7053SJung-uk Kim                $fn->($!);
1231e71b7053SJung-uk Kim            } else {
1232e71b7053SJung-uk Kim                $fn->("The detected tools didn't match the platform\n");
1233e71b7053SJung-uk Kim            }
1234e71b7053SJung-uk Kim        }
1235e71b7053SJung-uk Kim        last;
1236e71b7053SJung-uk Kim    }
1237e71b7053SJung-uk Kim}
1238e71b7053SJung-uk Kim
1239e71b7053SJung-uk Kimpush @{$config{defines}}, "NDEBUG"    if $config{build_type} eq "release";
1240e71b7053SJung-uk Kim
1241e71b7053SJung-uk Kimif ($target =~ /^mingw/ && `$config{CC} --target-help 2>&1` =~ m/-mno-cygwin/m)
12421f13597dSJung-uk Kim        {
1243e71b7053SJung-uk Kim        push @{$config{cflags}}, "-mno-cygwin";
1244e71b7053SJung-uk Kim        push @{$config{cxxflags}}, "-mno-cygwin" if $config{CXX};
1245e71b7053SJung-uk Kim        push @{$config{shared_ldflag}}, "-mno-cygwin";
12461f13597dSJung-uk Kim        }
12471f13597dSJung-uk Kim
1248e71b7053SJung-uk Kimif ($target =~ /linux.*-mips/ && !$disabled{asm}
1249610a21fdSJung-uk Kim        && !grep { $_ !~ /-m(ips|arch=)/ } (@{$config{CFLAGS}})) {
12507bded2dbSJung-uk Kim        # minimally required architecture flags for assembly modules
1251e71b7053SJung-uk Kim        my $value;
1252e71b7053SJung-uk Kim        $value = '-mips2' if ($target =~ /mips32/);
1253e71b7053SJung-uk Kim        $value = '-mips3' if ($target =~ /mips64/);
1254e71b7053SJung-uk Kim        unshift @{$config{cflags}}, $value;
1255e71b7053SJung-uk Kim        unshift @{$config{cxxflags}}, $value if $config{CXX};
1256e71b7053SJung-uk Kim}
1257e71b7053SJung-uk Kim
1258e71b7053SJung-uk Kim# If threads aren't disabled, check how possible they are
1259e71b7053SJung-uk Kimunless ($disabled{threads}) {
1260e71b7053SJung-uk Kim    if ($auto_threads) {
1261e71b7053SJung-uk Kim        # Enabled by default, disable it forcibly if unavailable
1262e71b7053SJung-uk Kim        if ($target{thread_scheme} eq "(unknown)") {
1263610a21fdSJung-uk Kim            disable("unavailable", 'threads');
1264e71b7053SJung-uk Kim        }
1265e71b7053SJung-uk Kim    } else {
1266e71b7053SJung-uk Kim        # The user chose to enable threads explicitly, let's see
1267e71b7053SJung-uk Kim        # if there's a chance that's possible
1268e71b7053SJung-uk Kim        if ($target{thread_scheme} eq "(unknown)") {
1269e71b7053SJung-uk Kim            # If the user asked for "threads" and we don't have internal
1270e71b7053SJung-uk Kim            # knowledge how to do it, [s]he is expected to provide any
1271e71b7053SJung-uk Kim            # system-dependent compiler options that are necessary.  We
1272e71b7053SJung-uk Kim            # can't truly check that the given options are correct, but
1273e71b7053SJung-uk Kim            # we expect the user to know what [s]He is doing.
1274610a21fdSJung-uk Kim            if (!@{$config{CFLAGS}} && !@{$config{CPPDEFINES}}) {
1275e71b7053SJung-uk Kim                die "You asked for multi-threading support, but didn't\n"
1276e71b7053SJung-uk Kim                    ,"provide any system-specific compiler options\n";
1277e71b7053SJung-uk Kim            }
1278e71b7053SJung-uk Kim        }
1279e71b7053SJung-uk Kim    }
1280e71b7053SJung-uk Kim}
1281e71b7053SJung-uk Kim
1282e71b7053SJung-uk Kim# If threads still aren't disabled, add a C macro to ensure the source
1283e71b7053SJung-uk Kim# code knows about it.  Any other flag is taken care of by the configs.
1284e71b7053SJung-uk Kimunless($disabled{threads}) {
1285e71b7053SJung-uk Kim    push @{$config{openssl_thread_defines}}, "OPENSSL_THREADS";
1286e71b7053SJung-uk Kim}
1287e71b7053SJung-uk Kim
1288e71b7053SJung-uk Kim# With "deprecated" disable all deprecated features.
1289e71b7053SJung-uk Kimif (defined($disabled{"deprecated"})) {
1290e71b7053SJung-uk Kim        $config{api} = $maxapi;
12917bded2dbSJung-uk Kim}
12927bded2dbSJung-uk Kim
1293fceca8a3SJacques Vidrinemy $no_shared_warn=0;
1294e71b7053SJung-uk Kimif ($target{shared_target} eq "")
12955c87c606SMark Murray        {
1296e71b7053SJung-uk Kim        $no_shared_warn = 1
1297e71b7053SJung-uk Kim            if (!$disabled{shared} || !$disabled{"dynamic-engine"});
1298610a21fdSJung-uk Kim        disable('no-shared-target', 'pic');
12995c87c606SMark Murray        }
13005c87c606SMark Murray
1301e71b7053SJung-uk Kimif ($disabled{"dynamic-engine"}) {
1302e71b7053SJung-uk Kim        $config{dynamic_engines} = 0;
1303e71b7053SJung-uk Kim} else {
1304e71b7053SJung-uk Kim        $config{dynamic_engines} = 1;
1305ddd58736SKris Kennaway}
1306ddd58736SKris Kennaway
1307e71b7053SJung-uk Kimunless ($disabled{asan}) {
1308e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fsanitize=address";
130974664626SKris Kennaway}
131074664626SKris Kennaway
1311e71b7053SJung-uk Kimunless ($disabled{ubsan}) {
1312e71b7053SJung-uk Kim    # -DPEDANTIC or -fnosanitize=alignment may also be required on some
1313e71b7053SJung-uk Kim    # platforms.
1314e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fsanitize=undefined", "-fno-sanitize-recover=all";
131574664626SKris Kennaway}
131674664626SKris Kennaway
1317e71b7053SJung-uk Kimunless ($disabled{msan}) {
1318e71b7053SJung-uk Kim  push @{$config{cflags}}, "-fsanitize=memory";
13195c87c606SMark Murray}
13205c87c606SMark Murray
1321e71b7053SJung-uk Kimunless ($disabled{"fuzz-libfuzzer"} && $disabled{"fuzz-afl"}
1322e71b7053SJung-uk Kim        && $disabled{asan} && $disabled{ubsan} && $disabled{msan}) {
1323e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fno-omit-frame-pointer", "-g";
1324e71b7053SJung-uk Kim    push @{$config{cxxflags}}, "-fno-omit-frame-pointer", "-g" if $config{CXX};
132574664626SKris Kennaway}
1326db522d3aSSimon L. B. Nielsen#
1327db522d3aSSimon L. B. Nielsen# Platform fix-ups
1328db522d3aSSimon L. B. Nielsen#
1329db522d3aSSimon L. B. Nielsen
1330e71b7053SJung-uk Kim# This saves the build files from having to check
1331e71b7053SJung-uk Kimif ($disabled{pic})
1332db522d3aSSimon L. B. Nielsen        {
1333e71b7053SJung-uk Kim        foreach (qw(shared_cflag shared_cxxflag shared_cppflag
1334e71b7053SJung-uk Kim                    shared_defines shared_includes shared_ldflag
1335e71b7053SJung-uk Kim                    module_cflags module_cxxflags module_cppflags
1336e71b7053SJung-uk Kim                    module_defines module_includes module_lflags))
1337e71b7053SJung-uk Kim                {
1338e71b7053SJung-uk Kim                delete $config{$_};
1339e71b7053SJung-uk Kim                $target{$_} = "";
1340ddd58736SKris Kennaway                }
1341aeb5019cSJung-uk Kim        }
1342aeb5019cSJung-uk Kimelse
1343aeb5019cSJung-uk Kim        {
1344e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "OPENSSL_PIC";
1345aeb5019cSJung-uk Kim        }
1346e71b7053SJung-uk Kim
1347e71b7053SJung-uk Kimif ($target{sys_id} ne "")
134874664626SKris Kennaway        {
1349e71b7053SJung-uk Kim        push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
13503b4e3dcbSSimon L. B. Nielsen        }
1351e71b7053SJung-uk Kim
1352e71b7053SJung-uk Kimunless ($disabled{asm}) {
1353e71b7053SJung-uk Kim    $target{cpuid_asm_src}=$table{DEFAULTS}->{cpuid_asm_src} if ($config{processor} eq "386");
1354e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_CPUID_OBJ" if ($target{cpuid_asm_src} ne "mem_clr.c");
1355e71b7053SJung-uk Kim
1356e71b7053SJung-uk Kim    $target{bn_asm_src} =~ s/\w+-gf2m.c// if (defined($disabled{ec2m}));
1357e71b7053SJung-uk Kim
1358e71b7053SJung-uk Kim    # bn-586 is the only one implementing bn_*_part_words
1359e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_PART_WORDS" if ($target{bn_asm_src} =~ /bn-586/);
1360e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_IA32_SSE2" if (!$disabled{sse2} && $target{bn_asm_src} =~ /86/);
1361e71b7053SJung-uk Kim
1362e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_MONT" if ($target{bn_asm_src} =~ /-mont/);
1363e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_MONT5" if ($target{bn_asm_src} =~ /-mont5/);
1364e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_GF2m" if ($target{bn_asm_src} =~ /-gf2m/);
13656935a639SJung-uk Kim    push @{$config{lib_defines}}, "BN_DIV3W" if ($target{bn_asm_src} =~ /-div3w/);
1366e71b7053SJung-uk Kim
1367e71b7053SJung-uk Kim    if ($target{sha1_asm_src}) {
1368e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "SHA1_ASM"   if ($target{sha1_asm_src} =~ /sx86/ || $target{sha1_asm_src} =~ /sha1/);
1369e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "SHA256_ASM" if ($target{sha1_asm_src} =~ /sha256/);
1370e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "SHA512_ASM" if ($target{sha1_asm_src} =~ /sha512/);
137174664626SKris Kennaway    }
1372e71b7053SJung-uk Kim    if ($target{keccak1600_asm_src} ne $table{DEFAULTS}->{keccak1600_asm_src}) {
1373e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "KECCAK1600_ASM";
137474664626SKris Kennaway    }
1375e71b7053SJung-uk Kim    if ($target{rc4_asm_src} ne $table{DEFAULTS}->{rc4_asm_src}) {
1376e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "RC4_ASM";
137774664626SKris Kennaway    }
1378e71b7053SJung-uk Kim    if ($target{md5_asm_src}) {
1379e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "MD5_ASM";
1380e71b7053SJung-uk Kim    }
1381e71b7053SJung-uk Kim    $target{cast_asm_src}=$table{DEFAULTS}->{cast_asm_src} unless $disabled{pic}; # CAST assembler is not PIC
1382e71b7053SJung-uk Kim    if ($target{rmd160_asm_src}) {
1383e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "RMD160_ASM";
1384e71b7053SJung-uk Kim    }
1385e71b7053SJung-uk Kim    if ($target{aes_asm_src}) {
1386e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "AES_ASM" if ($target{aes_asm_src} =~ m/\baes-/);;
138717f01e99SJung-uk Kim        push @{$config{lib_defines}}, "AESNI_ASM" if ($target{aes_asm_src} =~ m/\baesni-/);;
1388e71b7053SJung-uk Kim        # aes-ctr.fake is not a real file, only indication that assembler
13891f13597dSJung-uk Kim        # module implements AES_ctr32_encrypt...
1390e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "AES_CTR_ASM" if ($target{aes_asm_src} =~ s/\s*aes-ctr\.fake//);
1391e71b7053SJung-uk Kim        # aes-xts.fake indicates presence of AES_xts_[en|de]crypt...
1392e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "AES_XTS_ASM" if ($target{aes_asm_src} =~ s/\s*aes-xts\.fake//);
1393e71b7053SJung-uk Kim        $target{aes_asm_src} =~ s/\s*(vpaes|aesni)-x86\.s//g if ($disabled{sse2});
1394e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "VPAES_ASM" if ($target{aes_asm_src} =~ m/vpaes/);
1395e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "BSAES_ASM" if ($target{aes_asm_src} =~ m/bsaes/);
13963b4e3dcbSSimon L. B. Nielsen    }
1397e71b7053SJung-uk Kim    if ($target{wp_asm_src} =~ /mmx/) {
1398e71b7053SJung-uk Kim        if ($config{processor} eq "386") {
1399e71b7053SJung-uk Kim            $target{wp_asm_src}=$table{DEFAULTS}->{wp_asm_src};
1400e71b7053SJung-uk Kim        } elsif (!$disabled{"whirlpool"}) {
1401e71b7053SJung-uk Kim            push @{$config{lib_defines}}, "WHIRLPOOL_ASM";
14026be8ae07SJacques Vidrine        }
14031f13597dSJung-uk Kim    }
1404e71b7053SJung-uk Kim    if ($target{modes_asm_src} =~ /ghash-/) {
1405e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "GHASH_ASM";
14061f13597dSJung-uk Kim    }
1407e71b7053SJung-uk Kim    if ($target{ec_asm_src} =~ /ecp_nistz256/) {
1408e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "ECP_NISTZ256_ASM";
14091f13597dSJung-uk Kim    }
1410e71b7053SJung-uk Kim    if ($target{ec_asm_src} =~ /x25519/) {
1411e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "X25519_ASM";
1412e71b7053SJung-uk Kim    }
1413e71b7053SJung-uk Kim    if ($target{padlock_asm_src} ne $table{DEFAULTS}->{padlock_asm_src}) {
1414610a21fdSJung-uk Kim        push @{$config{dso_defines}}, "PADLOCK_ASM";
1415e71b7053SJung-uk Kim    }
1416e71b7053SJung-uk Kim    if ($target{poly1305_asm_src} ne "") {
1417e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "POLY1305_ASM";
1418e71b7053SJung-uk Kim    }
14197bded2dbSJung-uk Kim}
14206be8ae07SJacques Vidrine
1421610a21fdSJung-uk Kimmy %predefined_C = compiler_predefined($config{CROSS_COMPILE}.$config{CC});
1422610a21fdSJung-uk Kimmy %predefined_CXX = $config{CXX}
1423610a21fdSJung-uk Kim    ? compiler_predefined($config{CROSS_COMPILE}.$config{CXX})
1424610a21fdSJung-uk Kim    : ();
14254f20a5a2SJacques Vidrine
1426e71b7053SJung-uk Kim# Check for makedepend capabilities.
1427e71b7053SJung-uk Kimif (!$disabled{makedepend}) {
1428e71b7053SJung-uk Kim    if ($config{target} =~ /^(VC|vms)-/) {
1429e71b7053SJung-uk Kim        # For VC- and vms- targets, there's nothing more to do here.  The
1430e71b7053SJung-uk Kim        # functionality is hard coded in the corresponding build files for
1431e71b7053SJung-uk Kim        # cl (Windows) and CC/DECC (VMS).
1432610a21fdSJung-uk Kim    } elsif (($predefined_C{__GNUC__} // -1) >= 3
1433610a21fdSJung-uk Kim             && !($predefined_C{__APPLE_CC__} && !$predefined_C{__clang__})) {
1434e71b7053SJung-uk Kim        # We know that GNU C version 3 and up as well as all clang
1435e71b7053SJung-uk Kim        # versions support dependency generation, but Xcode did not
1436e71b7053SJung-uk Kim        # handle $cc -M before clang support (but claims __GNUC__ = 3)
1437e71b7053SJung-uk Kim        $config{makedepprog} = "\$(CROSS_COMPILE)$config{CC}";
1438e71b7053SJung-uk Kim    } else {
1439e71b7053SJung-uk Kim        # In all other cases, we look for 'makedepend', and disable the
1440e71b7053SJung-uk Kim        # capability if not found.
1441e71b7053SJung-uk Kim        $config{makedepprog} = which('makedepend');
1442610a21fdSJung-uk Kim        disable('unavailable', 'makedepend') unless $config{makedepprog};
144374664626SKris Kennaway    }
144474664626SKris Kennaway}
144574664626SKris Kennaway
1446610a21fdSJung-uk Kimif (!$disabled{asm} && !$predefined_C{__MACH__} && $^O ne 'VMS') {
1447e71b7053SJung-uk Kim    # probe for -Wa,--noexecstack option...
1448610a21fdSJung-uk Kim    if ($predefined_C{__clang__}) {
1449e71b7053SJung-uk Kim        # clang has builtin assembler, which doesn't recognize --help,
1450e71b7053SJung-uk Kim        # but it apparently recognizes the option in question on all
1451e71b7053SJung-uk Kim        # supported platforms even when it's meaningless. In other words
1452e71b7053SJung-uk Kim        # probe would fail, but probed option always accepted...
1453e71b7053SJung-uk Kim        push @{$config{cflags}}, "-Wa,--noexecstack", "-Qunused-arguments";
1454e71b7053SJung-uk Kim    } else {
1455e71b7053SJung-uk Kim        my $cc = $config{CROSS_COMPILE}.$config{CC};
1456e71b7053SJung-uk Kim        open(PIPE, "$cc -Wa,--help -c -o null.$$.o -x assembler /dev/null 2>&1 |");
1457dee36b4fSJung-uk Kim        while(<PIPE>) {
1458e71b7053SJung-uk Kim            if (m/--noexecstack/) {
1459e71b7053SJung-uk Kim                push @{$config{cflags}}, "-Wa,--noexecstack";
1460e71b7053SJung-uk Kim                last;
1461e71b7053SJung-uk Kim            }
1462dee36b4fSJung-uk Kim        }
1463dee36b4fSJung-uk Kim        close(PIPE);
1464e71b7053SJung-uk Kim        unlink("null.$$.o");
1465e71b7053SJung-uk Kim    }
1466e71b7053SJung-uk Kim}
146780815a77SJung-uk Kim
1468e71b7053SJung-uk Kim# Deal with bn_ops ###################################################
1469e71b7053SJung-uk Kim
1470e71b7053SJung-uk Kim$config{bn_ll}                  =0;
1471e71b7053SJung-uk Kim$config{export_var_as_fn}       =0;
1472e71b7053SJung-uk Kimmy $def_int="unsigned int";
1473e71b7053SJung-uk Kim$config{rc4_int}                =$def_int;
1474e71b7053SJung-uk Kim($config{b64l},$config{b64},$config{b32})=(0,0,1);
1475e71b7053SJung-uk Kim
1476e71b7053SJung-uk Kimmy $count = 0;
1477e71b7053SJung-uk Kimforeach (sort split(/\s+/,$target{bn_ops})) {
1478e71b7053SJung-uk Kim    $count++ if /SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT/;
1479e71b7053SJung-uk Kim    $config{export_var_as_fn}=1                 if $_ eq 'EXPORT_VAR_AS_FN';
1480e71b7053SJung-uk Kim    $config{bn_ll}=1                            if $_ eq 'BN_LLONG';
1481e71b7053SJung-uk Kim    $config{rc4_int}="unsigned char"            if $_ eq 'RC4_CHAR';
1482e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1483e71b7053SJung-uk Kim        =(0,1,0)                                if $_ eq 'SIXTY_FOUR_BIT';
1484e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1485e71b7053SJung-uk Kim        =(1,0,0)                                if $_ eq 'SIXTY_FOUR_BIT_LONG';
1486e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1487e71b7053SJung-uk Kim        =(0,0,1)                                if $_ eq 'THIRTY_TWO_BIT';
1488e71b7053SJung-uk Kim}
1489e71b7053SJung-uk Kimdie "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
1490e71b7053SJung-uk Kim    if $count > 1;
1491e71b7053SJung-uk Kim
1492e71b7053SJung-uk Kim
1493e71b7053SJung-uk Kim# Hack cflags for better warnings (dev option) #######################
1494e71b7053SJung-uk Kim
1495e71b7053SJung-uk Kim# "Stringify" the C and C++ flags string.  This permits it to be made part of
1496e71b7053SJung-uk Kim# a string and works as well on command lines.
1497e71b7053SJung-uk Kim$config{cflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1498e71b7053SJung-uk Kim                        @{$config{cflags}} ];
1499e71b7053SJung-uk Kim$config{cxxflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1500e71b7053SJung-uk Kim                          @{$config{cxxflags}} ] if $config{CXX};
1501e71b7053SJung-uk Kim
1502e71b7053SJung-uk Kimif (defined($config{api})) {
1503e71b7053SJung-uk Kim    $config{openssl_api_defines} = [ "OPENSSL_MIN_API=".$apitable->{$config{api}} ];
1504e71b7053SJung-uk Kim    my $apiflag = sprintf("OPENSSL_API_COMPAT=%s", $apitable->{$config{api}});
1505e71b7053SJung-uk Kim    push @{$config{defines}}, $apiflag;
1506dea77ea6SJung-uk Kim}
1507dea77ea6SJung-uk Kim
1508610a21fdSJung-uk Kimmy @strict_warnings_collection=();
150980815a77SJung-uk Kimif ($strict_warnings)
151080815a77SJung-uk Kim        {
15116a599222SSimon L. B. Nielsen        my $wopt;
1512610a21fdSJung-uk Kim        my $gccver = $predefined_C{__GNUC__} // -1;
1513e71b7053SJung-uk Kim
151417f01e99SJung-uk Kim        if ($gccver >= 4)
151517f01e99SJung-uk Kim                {
1516610a21fdSJung-uk Kim                push @strict_warnings_collection, @gcc_devteam_warn;
1517610a21fdSJung-uk Kim                push @strict_warnings_collection, @clang_devteam_warn
1518610a21fdSJung-uk Kim                    if (defined($predefined_C{__clang__}));
15196a599222SSimon L. B. Nielsen                }
152017f01e99SJung-uk Kim        elsif ($config{target} =~ /^VC-/)
152117f01e99SJung-uk Kim                {
152217f01e99SJung-uk Kim                push @strict_warnings_collection, @cl_devteam_warn;
152317f01e99SJung-uk Kim                }
152417f01e99SJung-uk Kim        else
152517f01e99SJung-uk Kim                {
152617f01e99SJung-uk Kim                warn "WARNING --strict-warnings requires gcc[>=4] or gcc-alike, or MSVC"
152717f01e99SJung-uk Kim                }
152817f01e99SJung-uk Kim        }
1529610a21fdSJung-uk Kim
1530610a21fdSJung-uk Kim$config{CFLAGS} = [ map { $_ eq '--ossl-strict-warnings'
1531610a21fdSJung-uk Kim                              ? @strict_warnings_collection
1532610a21fdSJung-uk Kim                              : ( $_ ) }
1533610a21fdSJung-uk Kim                    @{$config{CFLAGS}} ];
15346a599222SSimon L. B. Nielsen
1535e71b7053SJung-uk Kimunless ($disabled{"crypto-mdebug-backtrace"})
153674664626SKris Kennaway        {
1537e71b7053SJung-uk Kim        foreach my $wopt (split /\s+/, $memleak_devteam_backtrace)
15386a599222SSimon L. B. Nielsen                {
1539e71b7053SJung-uk Kim                push @{$config{cflags}}, $wopt
1540e71b7053SJung-uk Kim                        unless grep { $_ eq $wopt } @{$config{cflags}};
15416a599222SSimon L. B. Nielsen                }
1542e71b7053SJung-uk Kim        if ($target =~ /^BSD-/)
1543a21b1b38SKris Kennaway                {
1544e71b7053SJung-uk Kim                push @{$config{ex_libs}}, "-lexecinfo";
15454f20a5a2SJacques Vidrine                }
154674664626SKris Kennaway        }
154774664626SKris Kennaway
1548e71b7053SJung-uk Kimunless ($disabled{afalgeng}) {
1549e71b7053SJung-uk Kim    $config{afalgeng}="";
1550e71b7053SJung-uk Kim    if (grep { $_ eq 'afalgeng' } @{$target{enable}}) {
1551e71b7053SJung-uk Kim        my $minver = 4*10000 + 1*100 + 0;
1552e71b7053SJung-uk Kim        if ($config{CROSS_COMPILE} eq "") {
1553e71b7053SJung-uk Kim            my $verstr = `uname -r`;
1554e71b7053SJung-uk Kim            my ($ma, $mi1, $mi2) = split("\\.", $verstr);
1555e71b7053SJung-uk Kim            ($mi2) = $mi2 =~ /(\d+)/;
1556e71b7053SJung-uk Kim            my $ver = $ma*10000 + $mi1*100 + $mi2;
1557e71b7053SJung-uk Kim            if ($ver < $minver) {
1558610a21fdSJung-uk Kim                disable('too-old-kernel', 'afalgeng');
1559e71b7053SJung-uk Kim            } else {
1560e71b7053SJung-uk Kim                push @{$config{engdirs}}, "afalg";
1561e71b7053SJung-uk Kim            }
1562e71b7053SJung-uk Kim        } else {
1563610a21fdSJung-uk Kim            disable('cross-compiling', 'afalgeng');
1564e71b7053SJung-uk Kim        }
1565e71b7053SJung-uk Kim    } else {
1566610a21fdSJung-uk Kim        disable('not-linux', 'afalgeng');
1567e71b7053SJung-uk Kim    }
1568e71b7053SJung-uk Kim}
1569f579bf8eSKris Kennaway
157017f01e99SJung-uk Kimunless ($disabled{devcryptoeng}) {
157117f01e99SJung-uk Kim    if ($target =~ m/^BSD/) {
157217f01e99SJung-uk Kim        my $maxver = 5*100 + 7;
157317f01e99SJung-uk Kim        my $sysstr = `uname -s`;
157417f01e99SJung-uk Kim        my $verstr = `uname -r`;
157517f01e99SJung-uk Kim        $sysstr =~ s|\R$||;
157617f01e99SJung-uk Kim        $verstr =~ s|\R$||;
157717f01e99SJung-uk Kim        my ($ma, $mi, @rest) = split m|\.|, $verstr;
157817f01e99SJung-uk Kim        my $ver = $ma*100 + $mi;
157917f01e99SJung-uk Kim        if ($sysstr eq 'OpenBSD' && $ver >= $maxver) {
158017f01e99SJung-uk Kim            disable('too-new-kernel', 'devcryptoeng');
158117f01e99SJung-uk Kim        }
158217f01e99SJung-uk Kim    }
158317f01e99SJung-uk Kim}
1584e71b7053SJung-uk Kim
1585aa906e2aSJohn Baldwinunless ($disabled{ktls}) {
1586aa906e2aSJohn Baldwin    $config{ktls}="";
1587aa906e2aSJohn Baldwin    if ($target =~ m/^linux/) {
1588aa906e2aSJohn Baldwin        my $usr = "/usr/$config{cross_compile_prefix}";
1589aa906e2aSJohn Baldwin        chop($usr);
1590aa906e2aSJohn Baldwin        if ($config{cross_compile_prefix} eq "") {
1591aa906e2aSJohn Baldwin            $usr = "/usr";
1592aa906e2aSJohn Baldwin        }
1593aa906e2aSJohn Baldwin        my $minver = (4 << 16) + (13 << 8) + 0;
1594aa906e2aSJohn Baldwin        my @verstr = split(" ",`cat $usr/include/linux/version.h | grep LINUX_VERSION_CODE`);
1595aa906e2aSJohn Baldwin
1596aa906e2aSJohn Baldwin        if ($verstr[2] < $minver) {
1597aa906e2aSJohn Baldwin            disable('too-old-kernel', 'ktls');
1598aa906e2aSJohn Baldwin        }
1599aa906e2aSJohn Baldwin    } elsif ($target =~ m/^BSD/) {
1600aa906e2aSJohn Baldwin        my $cc = $config{CROSS_COMPILE}.$config{CC};
1601aa906e2aSJohn Baldwin        system("printf '#include <sys/types.h>\n#include <sys/ktls.h>' | $cc -E - >/dev/null 2>&1");
1602aa906e2aSJohn Baldwin        if ($? != 0) {
1603aa906e2aSJohn Baldwin            disable('too-old-freebsd', 'ktls');
1604aa906e2aSJohn Baldwin        }
1605aa906e2aSJohn Baldwin    } else {
1606aa906e2aSJohn Baldwin        disable('not-linux-or-freebsd', 'ktls');
1607aa906e2aSJohn Baldwin    }
1608aa906e2aSJohn Baldwin}
1609aa906e2aSJohn Baldwin
1610aa906e2aSJohn Baldwinpush @{$config{openssl_other_defines}}, "OPENSSL_NO_KTLS" if ($disabled{ktls});
1611aa906e2aSJohn Baldwin
1612610a21fdSJung-uk Kim# Get the extra flags used when building shared libraries and modules.  We
1613610a21fdSJung-uk Kim# do this late because some of them depend on %disabled.
1614e71b7053SJung-uk Kim
1615610a21fdSJung-uk Kim# Make the flags to build DSOs the same as for shared libraries unless they
1616610a21fdSJung-uk Kim# are already defined
1617610a21fdSJung-uk Kim$target{module_cflags} = $target{shared_cflag} unless defined $target{module_cflags};
1618610a21fdSJung-uk Kim$target{module_cxxflags} = $target{shared_cxxflag} unless defined $target{module_cxxflags};
1619610a21fdSJung-uk Kim$target{module_ldflags} = $target{shared_ldflag} unless defined $target{module_ldflags};
1620610a21fdSJung-uk Kim{
1621610a21fdSJung-uk Kim    my $shared_info_pl =
1622610a21fdSJung-uk Kim        catfile(dirname($0), "Configurations", "shared-info.pl");
1623610a21fdSJung-uk Kim    my %shared_info = read_eval_file($shared_info_pl);
1624610a21fdSJung-uk Kim    push @{$target{_conf_fname_int}}, $shared_info_pl;
1625610a21fdSJung-uk Kim    my $si = $target{shared_target};
1626610a21fdSJung-uk Kim    while (ref $si ne "HASH") {
1627610a21fdSJung-uk Kim        last if ! defined $si;
1628610a21fdSJung-uk Kim        if (ref $si eq "CODE") {
1629610a21fdSJung-uk Kim            $si = $si->();
1630e71b7053SJung-uk Kim        } else {
1631610a21fdSJung-uk Kim            $si = $shared_info{$si};
1632e71b7053SJung-uk Kim        }
1633e71b7053SJung-uk Kim    }
1634e71b7053SJung-uk Kim
1635610a21fdSJung-uk Kim    # Some of the 'shared_target' values don't have any entries in
1636610a21fdSJung-uk Kim    # %shared_info.  That's perfectly fine, AS LONG AS the build file
1637610a21fdSJung-uk Kim    # template knows how to handle this.  That is currently the case for
1638610a21fdSJung-uk Kim    # Windows and VMS.
1639610a21fdSJung-uk Kim    if (defined $si) {
1640610a21fdSJung-uk Kim        # Just as above, copy certain shared_* attributes to the corresponding
1641610a21fdSJung-uk Kim        # module_ attribute unless the latter is already defined
1642610a21fdSJung-uk Kim        $si->{module_cflags} = $si->{shared_cflag} unless defined $si->{module_cflags};
1643610a21fdSJung-uk Kim        $si->{module_cxxflags} = $si->{shared_cxxflag} unless defined $si->{module_cxxflags};
1644610a21fdSJung-uk Kim        $si->{module_ldflags} = $si->{shared_ldflag} unless defined $si->{module_ldflags};
1645610a21fdSJung-uk Kim        foreach (sort keys %$si) {
1646610a21fdSJung-uk Kim            $target{$_} = defined $target{$_}
1647610a21fdSJung-uk Kim                ? add($si->{$_})->($target{$_})
1648610a21fdSJung-uk Kim                : $si->{$_};
1649610a21fdSJung-uk Kim        }
1650610a21fdSJung-uk Kim    }
1651610a21fdSJung-uk Kim}
1652610a21fdSJung-uk Kim
1653610a21fdSJung-uk Kim# ALL MODIFICATIONS TO %disabled, %config and %target MUST BE DONE FROM HERE ON
1654e71b7053SJung-uk Kim
165517f01e99SJung-uk Kimmy %disabled_info = ();         # For configdata.pm
165617f01e99SJung-uk Kimforeach my $what (sort keys %disabled) {
165717f01e99SJung-uk Kim    $config{options} .= " no-$what";
165817f01e99SJung-uk Kim
165917f01e99SJung-uk Kim    if (!grep { $what eq $_ } ( 'buildtest-c++', 'threads', 'shared', 'pic',
166017f01e99SJung-uk Kim                                'dynamic-engine', 'makedepend',
166117f01e99SJung-uk Kim                                'zlib-dynamic', 'zlib', 'sse2' )) {
166217f01e99SJung-uk Kim        (my $WHAT = uc $what) =~ s|-|_|g;
166317f01e99SJung-uk Kim
166417f01e99SJung-uk Kim        # Fix up C macro end names
166517f01e99SJung-uk Kim        $WHAT = "RMD160" if $what eq "ripemd";
166617f01e99SJung-uk Kim
166717f01e99SJung-uk Kim        # fix-up crypto/directory name(s)
166817f01e99SJung-uk Kim        $what = "ripemd" if $what eq "rmd160";
166917f01e99SJung-uk Kim        $what = "whrlpool" if $what eq "whirlpool";
167017f01e99SJung-uk Kim
167117f01e99SJung-uk Kim        my $macro = $disabled_info{$what}->{macro} = "OPENSSL_NO_$WHAT";
167217f01e99SJung-uk Kim
167317f01e99SJung-uk Kim        if ((grep { $what eq $_ } @{$config{sdirs}})
167417f01e99SJung-uk Kim                && $what ne 'async' && $what ne 'err' && $what ne 'dso') {
167517f01e99SJung-uk Kim            @{$config{sdirs}} = grep { $what ne $_} @{$config{sdirs}};
167617f01e99SJung-uk Kim            $disabled_info{$what}->{skipped} = [ catdir('crypto', $what) ];
167717f01e99SJung-uk Kim
167817f01e99SJung-uk Kim            if ($what ne 'engine') {
167917f01e99SJung-uk Kim                push @{$config{openssl_algorithm_defines}}, $macro;
168017f01e99SJung-uk Kim            } else {
168117f01e99SJung-uk Kim                @{$config{dirs}} = grep !/^engines$/, @{$config{dirs}};
168217f01e99SJung-uk Kim                push @{$disabled_info{engine}->{skipped}}, catdir('engines');
168317f01e99SJung-uk Kim                push @{$config{openssl_other_defines}}, $macro;
168417f01e99SJung-uk Kim            }
168517f01e99SJung-uk Kim        } else {
168617f01e99SJung-uk Kim            push @{$config{openssl_other_defines}}, $macro;
168717f01e99SJung-uk Kim        }
168817f01e99SJung-uk Kim
168917f01e99SJung-uk Kim    }
169017f01e99SJung-uk Kim}
169117f01e99SJung-uk Kim
169217f01e99SJung-uk Kimif ($disabled{"dynamic-engine"}) {
169317f01e99SJung-uk Kim    push @{$config{openssl_other_defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
169417f01e99SJung-uk Kim} else {
169517f01e99SJung-uk Kim    push @{$config{openssl_other_defines}}, "OPENSSL_NO_STATIC_ENGINE";
169617f01e99SJung-uk Kim}
169717f01e99SJung-uk Kim
1698e71b7053SJung-uk Kim# If we use the unified build, collect information from build.info files
1699e71b7053SJung-uk Kimmy %unified_info = ();
1700e71b7053SJung-uk Kim
1701e71b7053SJung-uk Kimmy $buildinfo_debug = defined($ENV{CONFIGURE_DEBUG_BUILDINFO});
1702e71b7053SJung-uk Kimif ($builder eq "unified") {
1703e71b7053SJung-uk Kim    use with_fallback qw(Text::Template);
1704e71b7053SJung-uk Kim
1705e71b7053SJung-uk Kim    sub cleandir {
1706e71b7053SJung-uk Kim        my $base = shift;
1707e71b7053SJung-uk Kim        my $dir = shift;
1708e71b7053SJung-uk Kim        my $relativeto = shift || ".";
1709e71b7053SJung-uk Kim
1710e71b7053SJung-uk Kim        $dir = catdir($base,$dir) unless isabsolute($dir);
1711e71b7053SJung-uk Kim
1712e71b7053SJung-uk Kim        # Make sure the directories we're building in exists
1713e71b7053SJung-uk Kim        mkpath($dir);
1714e71b7053SJung-uk Kim
1715e71b7053SJung-uk Kim        my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
1716e71b7053SJung-uk Kim        #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
1717e71b7053SJung-uk Kim        return $res;
1718e71b7053SJung-uk Kim    }
1719e71b7053SJung-uk Kim
1720e71b7053SJung-uk Kim    sub cleanfile {
1721e71b7053SJung-uk Kim        my $base = shift;
1722e71b7053SJung-uk Kim        my $file = shift;
1723e71b7053SJung-uk Kim        my $relativeto = shift || ".";
1724e71b7053SJung-uk Kim
1725e71b7053SJung-uk Kim        $file = catfile($base,$file) unless isabsolute($file);
1726e71b7053SJung-uk Kim
1727e71b7053SJung-uk Kim        my $d = dirname($file);
1728e71b7053SJung-uk Kim        my $f = basename($file);
1729e71b7053SJung-uk Kim
1730e71b7053SJung-uk Kim        # Make sure the directories we're building in exists
1731e71b7053SJung-uk Kim        mkpath($d);
1732e71b7053SJung-uk Kim
1733e71b7053SJung-uk Kim        my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
1734e71b7053SJung-uk Kim        #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
1735e71b7053SJung-uk Kim        return $res;
1736e71b7053SJung-uk Kim    }
1737e71b7053SJung-uk Kim
1738e71b7053SJung-uk Kim    # Store the name of the template file we will build the build file from
1739e71b7053SJung-uk Kim    # in %config.  This may be useful for the build file itself.
1740e71b7053SJung-uk Kim    my @build_file_template_names =
1741e71b7053SJung-uk Kim        ( $builder_platform."-".$target{build_file}.".tmpl",
1742e71b7053SJung-uk Kim          $target{build_file}.".tmpl" );
1743e71b7053SJung-uk Kim    my @build_file_templates = ();
1744e71b7053SJung-uk Kim
1745e71b7053SJung-uk Kim    # First, look in the user provided directory, if given
1746e71b7053SJung-uk Kim    if (defined env($local_config_envname)) {
1747e71b7053SJung-uk Kim        @build_file_templates =
1748e71b7053SJung-uk Kim            map {
1749e71b7053SJung-uk Kim                if ($^O eq 'VMS') {
1750e71b7053SJung-uk Kim                    # VMS environment variables are logical names,
1751e71b7053SJung-uk Kim                    # which can be used as is
1752e71b7053SJung-uk Kim                    $local_config_envname . ':' . $_;
1753e71b7053SJung-uk Kim                } else {
1754e71b7053SJung-uk Kim                    catfile(env($local_config_envname), $_);
1755e71b7053SJung-uk Kim                }
1756e71b7053SJung-uk Kim            }
1757e71b7053SJung-uk Kim            @build_file_template_names;
1758e71b7053SJung-uk Kim    }
1759e71b7053SJung-uk Kim    # Then, look in our standard directory
1760e71b7053SJung-uk Kim    push @build_file_templates,
1761e71b7053SJung-uk Kim        ( map { cleanfile($srcdir, catfile("Configurations", $_), $blddir) }
1762e71b7053SJung-uk Kim          @build_file_template_names );
1763e71b7053SJung-uk Kim
1764e71b7053SJung-uk Kim    my $build_file_template;
1765e71b7053SJung-uk Kim    for $_ (@build_file_templates) {
1766e71b7053SJung-uk Kim        $build_file_template = $_;
1767e71b7053SJung-uk Kim        last if -f $build_file_template;
1768e71b7053SJung-uk Kim
1769e71b7053SJung-uk Kim        $build_file_template = undef;
1770e71b7053SJung-uk Kim    }
1771e71b7053SJung-uk Kim    if (!defined $build_file_template) {
1772e71b7053SJung-uk Kim        die "*** Couldn't find any of:\n", join("\n", @build_file_templates), "\n";
1773e71b7053SJung-uk Kim    }
1774e71b7053SJung-uk Kim    $config{build_file_templates}
1775e71b7053SJung-uk Kim      = [ cleanfile($srcdir, catfile("Configurations", "common0.tmpl"),
1776e71b7053SJung-uk Kim                    $blddir),
1777e71b7053SJung-uk Kim          $build_file_template,
1778e71b7053SJung-uk Kim          cleanfile($srcdir, catfile("Configurations", "common.tmpl"),
1779e71b7053SJung-uk Kim                    $blddir) ];
1780e71b7053SJung-uk Kim
1781e71b7053SJung-uk Kim    my @build_infos = ( [ ".", "build.info" ] );
1782e71b7053SJung-uk Kim    foreach (@{$config{dirs}}) {
1783e71b7053SJung-uk Kim        push @build_infos, [ $_, "build.info" ]
1784e71b7053SJung-uk Kim            if (-f catfile($srcdir, $_, "build.info"));
1785e71b7053SJung-uk Kim    }
1786e71b7053SJung-uk Kim    foreach (@{$config{sdirs}}) {
1787e71b7053SJung-uk Kim        push @build_infos, [ catdir("crypto", $_), "build.info" ]
1788e71b7053SJung-uk Kim            if (-f catfile($srcdir, "crypto", $_, "build.info"));
1789e71b7053SJung-uk Kim    }
1790e71b7053SJung-uk Kim    foreach (@{$config{engdirs}}) {
1791e71b7053SJung-uk Kim        push @build_infos, [ catdir("engines", $_), "build.info" ]
1792e71b7053SJung-uk Kim            if (-f catfile($srcdir, "engines", $_, "build.info"));
1793e71b7053SJung-uk Kim    }
1794e71b7053SJung-uk Kim    foreach (@{$config{tdirs}}) {
1795e71b7053SJung-uk Kim        push @build_infos, [ catdir("test", $_), "build.info" ]
1796e71b7053SJung-uk Kim            if (-f catfile($srcdir, "test", $_, "build.info"));
1797e71b7053SJung-uk Kim    }
1798e71b7053SJung-uk Kim
1799e71b7053SJung-uk Kim    $config{build_infos} = [ ];
1800e71b7053SJung-uk Kim
1801e71b7053SJung-uk Kim    my %ordinals = ();
1802e71b7053SJung-uk Kim    foreach (@build_infos) {
1803e71b7053SJung-uk Kim        my $sourced = catdir($srcdir, $_->[0]);
1804e71b7053SJung-uk Kim        my $buildd = catdir($blddir, $_->[0]);
1805e71b7053SJung-uk Kim
1806e71b7053SJung-uk Kim        mkpath($buildd);
1807e71b7053SJung-uk Kim
1808e71b7053SJung-uk Kim        my $f = $_->[1];
1809e71b7053SJung-uk Kim        # The basic things we're trying to build
1810e71b7053SJung-uk Kim        my @programs = ();
1811e71b7053SJung-uk Kim        my @programs_install = ();
1812e71b7053SJung-uk Kim        my @libraries = ();
1813e71b7053SJung-uk Kim        my @libraries_install = ();
1814e71b7053SJung-uk Kim        my @engines = ();
1815e71b7053SJung-uk Kim        my @engines_install = ();
1816e71b7053SJung-uk Kim        my @scripts = ();
1817e71b7053SJung-uk Kim        my @scripts_install = ();
1818e71b7053SJung-uk Kim        my @extra = ();
1819e71b7053SJung-uk Kim        my @overrides = ();
1820e71b7053SJung-uk Kim        my @intermediates = ();
1821e71b7053SJung-uk Kim        my @rawlines = ();
1822e71b7053SJung-uk Kim
1823e71b7053SJung-uk Kim        my %sources = ();
1824e71b7053SJung-uk Kim        my %shared_sources = ();
1825e71b7053SJung-uk Kim        my %includes = ();
1826e71b7053SJung-uk Kim        my %depends = ();
1827e71b7053SJung-uk Kim        my %renames = ();
1828e71b7053SJung-uk Kim        my %sharednames = ();
1829e71b7053SJung-uk Kim        my %generate = ();
1830e71b7053SJung-uk Kim
1831e71b7053SJung-uk Kim        # We want to detect configdata.pm in the source tree, so we
1832e71b7053SJung-uk Kim        # don't use it if the build tree is different.
1833e71b7053SJung-uk Kim        my $src_configdata = cleanfile($srcdir, "configdata.pm", $blddir);
1834e71b7053SJung-uk Kim
1835e71b7053SJung-uk Kim        push @{$config{build_infos}}, catfile(abs2rel($sourced, $blddir), $f);
1836e71b7053SJung-uk Kim        my $template =
1837e71b7053SJung-uk Kim            Text::Template->new(TYPE => 'FILE',
1838e71b7053SJung-uk Kim                                SOURCE => catfile($sourced, $f),
1839e71b7053SJung-uk Kim                                PREPEND => qq{use lib "$FindBin::Bin/util/perl";});
1840e71b7053SJung-uk Kim        die "Something went wrong with $sourced/$f: $!\n" unless $template;
1841e71b7053SJung-uk Kim        my @text =
1842e71b7053SJung-uk Kim            split /^/m,
1843e71b7053SJung-uk Kim            $template->fill_in(HASH => { config => \%config,
1844e71b7053SJung-uk Kim                                         target => \%target,
1845e71b7053SJung-uk Kim                                         disabled => \%disabled,
1846e71b7053SJung-uk Kim                                         withargs => \%withargs,
1847e71b7053SJung-uk Kim                                         builddir => abs2rel($buildd, $blddir),
1848e71b7053SJung-uk Kim                                         sourcedir => abs2rel($sourced, $blddir),
1849e71b7053SJung-uk Kim                                         buildtop => abs2rel($blddir, $blddir),
1850e71b7053SJung-uk Kim                                         sourcetop => abs2rel($srcdir, $blddir) },
1851e71b7053SJung-uk Kim                               DELIMITERS => [ "{-", "-}" ]);
1852e71b7053SJung-uk Kim
1853e71b7053SJung-uk Kim        # The top item of this stack has the following values
1854e71b7053SJung-uk Kim        # -2 positive already run and we found ELSE (following ELSIF should fail)
1855e71b7053SJung-uk Kim        # -1 positive already run (skip until ENDIF)
1856e71b7053SJung-uk Kim        # 0 negatives so far (if we're at a condition, check it)
1857e71b7053SJung-uk Kim        # 1 last was positive (don't skip lines until next ELSE, ELSIF or ENDIF)
1858e71b7053SJung-uk Kim        # 2 positive ELSE (following ELSIF should fail)
1859e71b7053SJung-uk Kim        my @skip = ();
1860e71b7053SJung-uk Kim        collect_information(
1861e71b7053SJung-uk Kim            collect_from_array([ @text ],
1862e71b7053SJung-uk Kim                               qr/\\$/ => sub { my $l1 = shift; my $l2 = shift;
1863e71b7053SJung-uk Kim                                                $l1 =~ s/\\$//; $l1.$l2 }),
1864e71b7053SJung-uk Kim            # Info we're looking for
1865e71b7053SJung-uk Kim            qr/^\s*IF\[((?:\\.|[^\\\]])*)\]\s*$/
1866e71b7053SJung-uk Kim            => sub {
1867e71b7053SJung-uk Kim                if (! @skip || $skip[$#skip] > 0) {
1868e71b7053SJung-uk Kim                    push @skip, !! $1;
1869e71b7053SJung-uk Kim                } else {
1870e71b7053SJung-uk Kim                    push @skip, -1;
1871e71b7053SJung-uk Kim                }
1872e71b7053SJung-uk Kim            },
1873e71b7053SJung-uk Kim            qr/^\s*ELSIF\[((?:\\.|[^\\\]])*)\]\s*$/
1874e71b7053SJung-uk Kim            => sub { die "ELSIF out of scope" if ! @skip;
1875e71b7053SJung-uk Kim                     die "ELSIF following ELSE" if abs($skip[$#skip]) == 2;
1876e71b7053SJung-uk Kim                     $skip[$#skip] = -1 if $skip[$#skip] != 0;
1877e71b7053SJung-uk Kim                     $skip[$#skip] = !! $1
1878e71b7053SJung-uk Kim                         if $skip[$#skip] == 0; },
1879e71b7053SJung-uk Kim            qr/^\s*ELSE\s*$/
1880e71b7053SJung-uk Kim            => sub { die "ELSE out of scope" if ! @skip;
1881e71b7053SJung-uk Kim                     $skip[$#skip] = -2 if $skip[$#skip] != 0;
1882e71b7053SJung-uk Kim                     $skip[$#skip] = 2 if $skip[$#skip] == 0; },
1883e71b7053SJung-uk Kim            qr/^\s*ENDIF\s*$/
1884e71b7053SJung-uk Kim            => sub { die "ENDIF out of scope" if ! @skip;
1885e71b7053SJung-uk Kim                     pop @skip; },
1886e71b7053SJung-uk Kim            qr/^\s*PROGRAMS(_NO_INST)?\s*=\s*(.*)\s*$/
1887e71b7053SJung-uk Kim            => sub {
1888e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1889e71b7053SJung-uk Kim                    my $install = $1;
1890e71b7053SJung-uk Kim                    my @x = tokenize($2);
1891e71b7053SJung-uk Kim                    push @programs, @x;
1892e71b7053SJung-uk Kim                    push @programs_install, @x unless $install;
1893e71b7053SJung-uk Kim                }
1894e71b7053SJung-uk Kim            },
1895e71b7053SJung-uk Kim            qr/^\s*LIBS(_NO_INST)?\s*=\s*(.*)\s*$/
1896e71b7053SJung-uk Kim            => sub {
1897e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1898e71b7053SJung-uk Kim                    my $install = $1;
1899e71b7053SJung-uk Kim                    my @x = tokenize($2);
1900e71b7053SJung-uk Kim                    push @libraries, @x;
1901e71b7053SJung-uk Kim                    push @libraries_install, @x unless $install;
1902e71b7053SJung-uk Kim                }
1903e71b7053SJung-uk Kim            },
1904e71b7053SJung-uk Kim            qr/^\s*ENGINES(_NO_INST)?\s*=\s*(.*)\s*$/
1905e71b7053SJung-uk Kim            => sub {
1906e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1907e71b7053SJung-uk Kim                    my $install = $1;
1908e71b7053SJung-uk Kim                    my @x = tokenize($2);
1909e71b7053SJung-uk Kim                    push @engines, @x;
1910e71b7053SJung-uk Kim                    push @engines_install, @x unless $install;
1911e71b7053SJung-uk Kim                }
1912e71b7053SJung-uk Kim            },
1913e71b7053SJung-uk Kim            qr/^\s*SCRIPTS(_NO_INST)?\s*=\s*(.*)\s*$/
1914e71b7053SJung-uk Kim            => sub {
1915e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1916e71b7053SJung-uk Kim                    my $install = $1;
1917e71b7053SJung-uk Kim                    my @x = tokenize($2);
1918e71b7053SJung-uk Kim                    push @scripts, @x;
1919e71b7053SJung-uk Kim                    push @scripts_install, @x unless $install;
1920e71b7053SJung-uk Kim                }
1921e71b7053SJung-uk Kim            },
1922e71b7053SJung-uk Kim            qr/^\s*EXTRA\s*=\s*(.*)\s*$/
1923e71b7053SJung-uk Kim            => sub { push @extra, tokenize($1)
1924e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1925e71b7053SJung-uk Kim            qr/^\s*OVERRIDES\s*=\s*(.*)\s*$/
1926e71b7053SJung-uk Kim            => sub { push @overrides, tokenize($1)
1927e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1928e71b7053SJung-uk Kim
1929e71b7053SJung-uk Kim            qr/^\s*ORDINALS\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/,
1930e71b7053SJung-uk Kim            => sub { push @{$ordinals{$1}}, tokenize($2)
1931e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1932e71b7053SJung-uk Kim            qr/^\s*SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1933e71b7053SJung-uk Kim            => sub { push @{$sources{$1}}, tokenize($2)
1934e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1935e71b7053SJung-uk Kim            qr/^\s*SHARED_SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1936e71b7053SJung-uk Kim            => sub { push @{$shared_sources{$1}}, tokenize($2)
1937e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1938e71b7053SJung-uk Kim            qr/^\s*INCLUDE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1939e71b7053SJung-uk Kim            => sub { push @{$includes{$1}}, tokenize($2)
1940e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1941e71b7053SJung-uk Kim            qr/^\s*DEPEND\[((?:\\.|[^\\\]])*)\]\s*=\s*(.*)\s*$/
1942e71b7053SJung-uk Kim            => sub { push @{$depends{$1}}, tokenize($2)
1943e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1944e71b7053SJung-uk Kim            qr/^\s*GENERATE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1945e71b7053SJung-uk Kim            => sub { push @{$generate{$1}}, $2
1946e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1947e71b7053SJung-uk Kim            qr/^\s*RENAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1948e71b7053SJung-uk Kim            => sub { push @{$renames{$1}}, tokenize($2)
1949e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1950e71b7053SJung-uk Kim            qr/^\s*SHARED_NAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1951e71b7053SJung-uk Kim            => sub { push @{$sharednames{$1}}, tokenize($2)
1952e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1953e71b7053SJung-uk Kim            qr/^\s*BEGINRAW\[((?:\\.|[^\\\]])+)\]\s*$/
1954e71b7053SJung-uk Kim            => sub {
1955e71b7053SJung-uk Kim                my $lineiterator = shift;
1956e71b7053SJung-uk Kim                my $target_kind = $1;
1957e71b7053SJung-uk Kim                while (defined $lineiterator->()) {
1958e71b7053SJung-uk Kim                    s|\R$||;
1959e71b7053SJung-uk Kim                    if (/^\s*ENDRAW\[((?:\\.|[^\\\]])+)\]\s*$/) {
1960e71b7053SJung-uk Kim                        die "ENDRAW doesn't match BEGINRAW"
1961e71b7053SJung-uk Kim                            if $1 ne $target_kind;
1962e71b7053SJung-uk Kim                        last;
1963e71b7053SJung-uk Kim                    }
1964e71b7053SJung-uk Kim                    next if @skip && $skip[$#skip] <= 0;
1965e71b7053SJung-uk Kim                    push @rawlines,  $_
1966e71b7053SJung-uk Kim                        if ($target_kind eq $target{build_file}
1967e71b7053SJung-uk Kim                            || $target_kind eq $target{build_file}."(".$builder_platform.")");
1968e71b7053SJung-uk Kim                }
1969e71b7053SJung-uk Kim            },
1970e71b7053SJung-uk Kim            qr/^\s*(?:#.*)?$/ => sub { },
1971e71b7053SJung-uk Kim            "OTHERWISE" => sub { die "Something wrong with this line:\n$_\nat $sourced/$f" },
1972e71b7053SJung-uk Kim            "BEFORE" => sub {
1973e71b7053SJung-uk Kim                if ($buildinfo_debug) {
1974e71b7053SJung-uk Kim                    print STDERR "DEBUG: Parsing ",join(" ", @_),"\n";
1975e71b7053SJung-uk Kim                    print STDERR "DEBUG: ... before parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
1976e71b7053SJung-uk Kim                }
1977e71b7053SJung-uk Kim            },
1978e71b7053SJung-uk Kim            "AFTER" => sub {
1979e71b7053SJung-uk Kim                if ($buildinfo_debug) {
1980e71b7053SJung-uk Kim                    print STDERR "DEBUG: .... after parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
1981e71b7053SJung-uk Kim                }
1982e71b7053SJung-uk Kim            },
1983e71b7053SJung-uk Kim            );
1984e71b7053SJung-uk Kim        die "runaway IF?" if (@skip);
1985e71b7053SJung-uk Kim
1986e71b7053SJung-uk Kim        foreach (keys %renames) {
1987e71b7053SJung-uk Kim            die "$_ renamed to more than one thing: "
1988e71b7053SJung-uk Kim                ,join(" ", @{$renames{$_}}),"\n"
1989e71b7053SJung-uk Kim                if scalar @{$renames{$_}} > 1;
1990e71b7053SJung-uk Kim            my $dest = cleanfile($buildd, $_, $blddir);
1991e71b7053SJung-uk Kim            my $to = cleanfile($buildd, $renames{$_}->[0], $blddir);
1992e71b7053SJung-uk Kim            die "$dest renamed to more than one thing: "
1993e71b7053SJung-uk Kim                ,$unified_info{rename}->{$dest}, $to
1994e71b7053SJung-uk Kim                unless !defined($unified_info{rename}->{$dest})
1995e71b7053SJung-uk Kim                or $unified_info{rename}->{$dest} eq $to;
1996e71b7053SJung-uk Kim            $unified_info{rename}->{$dest} = $to;
1997e71b7053SJung-uk Kim        }
1998e71b7053SJung-uk Kim
1999e71b7053SJung-uk Kim        foreach (@programs) {
2000e71b7053SJung-uk Kim            my $program = cleanfile($buildd, $_, $blddir);
2001e71b7053SJung-uk Kim            if ($unified_info{rename}->{$program}) {
2002e71b7053SJung-uk Kim                $program = $unified_info{rename}->{$program};
2003e71b7053SJung-uk Kim            }
2004e71b7053SJung-uk Kim            $unified_info{programs}->{$program} = 1;
2005e71b7053SJung-uk Kim        }
2006e71b7053SJung-uk Kim
2007e71b7053SJung-uk Kim        foreach (@programs_install) {
2008e71b7053SJung-uk Kim            my $program = cleanfile($buildd, $_, $blddir);
2009e71b7053SJung-uk Kim            if ($unified_info{rename}->{$program}) {
2010e71b7053SJung-uk Kim                $program = $unified_info{rename}->{$program};
2011e71b7053SJung-uk Kim            }
2012e71b7053SJung-uk Kim            $unified_info{install}->{programs}->{$program} = 1;
2013e71b7053SJung-uk Kim        }
2014e71b7053SJung-uk Kim
2015e71b7053SJung-uk Kim        foreach (@libraries) {
2016e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
2017e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
2018e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
2019e71b7053SJung-uk Kim            }
2020e71b7053SJung-uk Kim            $unified_info{libraries}->{$library} = 1;
2021e71b7053SJung-uk Kim        }
2022e71b7053SJung-uk Kim
2023e71b7053SJung-uk Kim        foreach (@libraries_install) {
2024e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
2025e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
2026e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
2027e71b7053SJung-uk Kim            }
2028e71b7053SJung-uk Kim            $unified_info{install}->{libraries}->{$library} = 1;
2029e71b7053SJung-uk Kim        }
2030e71b7053SJung-uk Kim
2031e71b7053SJung-uk Kim        die <<"EOF" if scalar @engines and !$config{dynamic_engines};
2032e71b7053SJung-uk KimENGINES can only be used if configured with 'dynamic-engine'.
2033e71b7053SJung-uk KimThis is usually a fault in a build.info file.
2034e71b7053SJung-uk KimEOF
2035e71b7053SJung-uk Kim        foreach (@engines) {
2036e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
2037e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
2038e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
2039e71b7053SJung-uk Kim            }
2040e71b7053SJung-uk Kim            $unified_info{engines}->{$library} = 1;
2041e71b7053SJung-uk Kim        }
2042e71b7053SJung-uk Kim
2043e71b7053SJung-uk Kim        foreach (@engines_install) {
2044e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
2045e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
2046e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
2047e71b7053SJung-uk Kim            }
2048e71b7053SJung-uk Kim            $unified_info{install}->{engines}->{$library} = 1;
2049e71b7053SJung-uk Kim        }
2050e71b7053SJung-uk Kim
2051e71b7053SJung-uk Kim        foreach (@scripts) {
2052e71b7053SJung-uk Kim            my $script = cleanfile($buildd, $_, $blddir);
2053e71b7053SJung-uk Kim            if ($unified_info{rename}->{$script}) {
2054e71b7053SJung-uk Kim                $script = $unified_info{rename}->{$script};
2055e71b7053SJung-uk Kim            }
2056e71b7053SJung-uk Kim            $unified_info{scripts}->{$script} = 1;
2057e71b7053SJung-uk Kim        }
2058e71b7053SJung-uk Kim
2059e71b7053SJung-uk Kim        foreach (@scripts_install) {
2060e71b7053SJung-uk Kim            my $script = cleanfile($buildd, $_, $blddir);
2061e71b7053SJung-uk Kim            if ($unified_info{rename}->{$script}) {
2062e71b7053SJung-uk Kim                $script = $unified_info{rename}->{$script};
2063e71b7053SJung-uk Kim            }
2064e71b7053SJung-uk Kim            $unified_info{install}->{scripts}->{$script} = 1;
2065e71b7053SJung-uk Kim        }
2066e71b7053SJung-uk Kim
2067e71b7053SJung-uk Kim        foreach (@extra) {
2068e71b7053SJung-uk Kim            my $extra = cleanfile($buildd, $_, $blddir);
2069e71b7053SJung-uk Kim            $unified_info{extra}->{$extra} = 1;
2070e71b7053SJung-uk Kim        }
2071e71b7053SJung-uk Kim
2072e71b7053SJung-uk Kim        foreach (@overrides) {
2073e71b7053SJung-uk Kim            my $override = cleanfile($buildd, $_, $blddir);
2074e71b7053SJung-uk Kim            $unified_info{overrides}->{$override} = 1;
2075e71b7053SJung-uk Kim        }
2076e71b7053SJung-uk Kim
2077e71b7053SJung-uk Kim        push @{$unified_info{rawlines}}, @rawlines;
2078e71b7053SJung-uk Kim
2079e71b7053SJung-uk Kim        unless ($disabled{shared}) {
2080e71b7053SJung-uk Kim            # Check sharednames.
2081e71b7053SJung-uk Kim            foreach (keys %sharednames) {
2082e71b7053SJung-uk Kim                my $dest = cleanfile($buildd, $_, $blddir);
2083e71b7053SJung-uk Kim                if ($unified_info{rename}->{$dest}) {
2084e71b7053SJung-uk Kim                    $dest = $unified_info{rename}->{$dest};
2085e71b7053SJung-uk Kim                }
2086e71b7053SJung-uk Kim                die "shared_name for $dest with multiple values: "
2087e71b7053SJung-uk Kim                    ,join(" ", @{$sharednames{$_}}),"\n"
2088e71b7053SJung-uk Kim                    if scalar @{$sharednames{$_}} > 1;
2089e71b7053SJung-uk Kim                my $to = cleanfile($buildd, $sharednames{$_}->[0], $blddir);
2090e71b7053SJung-uk Kim                die "shared_name found for a library $dest that isn't defined\n"
2091e71b7053SJung-uk Kim                    unless $unified_info{libraries}->{$dest};
2092e71b7053SJung-uk Kim                die "shared_name for $dest with multiple values: "
2093e71b7053SJung-uk Kim                    ,$unified_info{sharednames}->{$dest}, ", ", $to
2094e71b7053SJung-uk Kim                    unless !defined($unified_info{sharednames}->{$dest})
2095e71b7053SJung-uk Kim                    or $unified_info{sharednames}->{$dest} eq $to;
2096e71b7053SJung-uk Kim                $unified_info{sharednames}->{$dest} = $to;
2097e71b7053SJung-uk Kim            }
2098e71b7053SJung-uk Kim
2099e71b7053SJung-uk Kim            # Additionally, we set up sharednames for libraries that don't
2100e71b7053SJung-uk Kim            # have any, as themselves.  Only for libraries that aren't
2101e71b7053SJung-uk Kim            # explicitly static.
2102e71b7053SJung-uk Kim            foreach (grep !/\.a$/, keys %{$unified_info{libraries}}) {
2103e71b7053SJung-uk Kim                if (!defined $unified_info{sharednames}->{$_}) {
2104e71b7053SJung-uk Kim                    $unified_info{sharednames}->{$_} = $_
2105e71b7053SJung-uk Kim                }
2106e71b7053SJung-uk Kim            }
2107e71b7053SJung-uk Kim
2108e71b7053SJung-uk Kim            # Check that we haven't defined any library as both shared and
2109e71b7053SJung-uk Kim            # explicitly static.  That is forbidden.
2110e71b7053SJung-uk Kim            my @doubles = ();
2111e71b7053SJung-uk Kim            foreach (grep /\.a$/, keys %{$unified_info{libraries}}) {
2112e71b7053SJung-uk Kim                (my $l = $_) =~ s/\.a$//;
2113e71b7053SJung-uk Kim                push @doubles, $l if defined $unified_info{sharednames}->{$l};
2114e71b7053SJung-uk Kim            }
2115e71b7053SJung-uk Kim            die "these libraries are both explicitly static and shared:\n  ",
2116e71b7053SJung-uk Kim                join(" ", @doubles), "\n"
2117e71b7053SJung-uk Kim                if @doubles;
2118e71b7053SJung-uk Kim        }
2119e71b7053SJung-uk Kim
2120e71b7053SJung-uk Kim        foreach (keys %sources) {
2121e71b7053SJung-uk Kim            my $dest = $_;
2122e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2123e71b7053SJung-uk Kim            if ($unified_info{rename}->{$ddest}) {
2124e71b7053SJung-uk Kim                $ddest = $unified_info{rename}->{$ddest};
2125e71b7053SJung-uk Kim            }
2126e71b7053SJung-uk Kim            foreach (@{$sources{$dest}}) {
2127e71b7053SJung-uk Kim                my $s = cleanfile($sourced, $_, $blddir);
2128e71b7053SJung-uk Kim
2129e71b7053SJung-uk Kim                # If it isn't in the source tree, we assume it's generated
2130e71b7053SJung-uk Kim                # in the build tree
2131e71b7053SJung-uk Kim                if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
2132e71b7053SJung-uk Kim                    $s = cleanfile($buildd, $_, $blddir);
2133e71b7053SJung-uk Kim                }
2134e71b7053SJung-uk Kim                # We recognise C++, C and asm files
2135e71b7053SJung-uk Kim                if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2136e71b7053SJung-uk Kim                    my $o = $_;
2137e71b7053SJung-uk Kim                    $o =~ s/\.[csS]$/.o/; # C and assembler
2138e71b7053SJung-uk Kim                    $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2139e71b7053SJung-uk Kim                    $o = cleanfile($buildd, $o, $blddir);
2140e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$o} = 1;
2141e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2142e71b7053SJung-uk Kim                } elsif ($s =~ /\.rc$/) {
2143e71b7053SJung-uk Kim                    # We also recognise resource files
2144e71b7053SJung-uk Kim                    my $o = $_;
2145e71b7053SJung-uk Kim                    $o =~ s/\.rc$/.res/; # Resource configuration
2146e71b7053SJung-uk Kim                    my $o = cleanfile($buildd, $o, $blddir);
2147e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$o} = 1;
2148e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2149e71b7053SJung-uk Kim                } else {
2150e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$s} = 1;
2151e71b7053SJung-uk Kim                }
2152e71b7053SJung-uk Kim            }
2153e71b7053SJung-uk Kim        }
2154e71b7053SJung-uk Kim
2155e71b7053SJung-uk Kim        foreach (keys %shared_sources) {
2156e71b7053SJung-uk Kim            my $dest = $_;
2157e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2158e71b7053SJung-uk Kim            if ($unified_info{rename}->{$ddest}) {
2159e71b7053SJung-uk Kim                $ddest = $unified_info{rename}->{$ddest};
2160e71b7053SJung-uk Kim            }
2161e71b7053SJung-uk Kim            foreach (@{$shared_sources{$dest}}) {
2162e71b7053SJung-uk Kim                my $s = cleanfile($sourced, $_, $blddir);
2163e71b7053SJung-uk Kim
2164e71b7053SJung-uk Kim                # If it isn't in the source tree, we assume it's generated
2165e71b7053SJung-uk Kim                # in the build tree
2166e71b7053SJung-uk Kim                if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
2167e71b7053SJung-uk Kim                    $s = cleanfile($buildd, $_, $blddir);
2168e71b7053SJung-uk Kim                }
2169e71b7053SJung-uk Kim
2170e71b7053SJung-uk Kim                if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2171e71b7053SJung-uk Kim                    # We recognise C++, C and asm files
2172e71b7053SJung-uk Kim                    my $o = $_;
2173e71b7053SJung-uk Kim                    $o =~ s/\.[csS]$/.o/; # C and assembler
2174e71b7053SJung-uk Kim                    $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2175e71b7053SJung-uk Kim                    $o = cleanfile($buildd, $o, $blddir);
2176e71b7053SJung-uk Kim                    $unified_info{shared_sources}->{$ddest}->{$o} = 1;
2177e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2178e71b7053SJung-uk Kim                } elsif ($s =~ /\.rc$/) {
2179e71b7053SJung-uk Kim                    # We also recognise resource files
2180e71b7053SJung-uk Kim                    my $o = $_;
2181e71b7053SJung-uk Kim                    $o =~ s/\.rc$/.res/; # Resource configuration
2182e71b7053SJung-uk Kim                    my $o = cleanfile($buildd, $o, $blddir);
2183e71b7053SJung-uk Kim                    $unified_info{shared_sources}->{$ddest}->{$o} = 1;
2184e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2185e71b7053SJung-uk Kim                } elsif ($s =~ /\.(def|map|opt)$/) {
2186e71b7053SJung-uk Kim                    # We also recognise .def / .map / .opt files
2187e71b7053SJung-uk Kim                    # We know they are generated files
2188e71b7053SJung-uk Kim                    my $def = cleanfile($buildd, $s, $blddir);
2189e71b7053SJung-uk Kim                    $unified_info{shared_sources}->{$ddest}->{$def} = 1;
2190e71b7053SJung-uk Kim                } else {
2191e71b7053SJung-uk Kim                    die "unrecognised source file type for shared library: $s\n";
2192e71b7053SJung-uk Kim                }
2193e71b7053SJung-uk Kim            }
2194e71b7053SJung-uk Kim        }
2195e71b7053SJung-uk Kim
2196e71b7053SJung-uk Kim        foreach (keys %generate) {
2197e71b7053SJung-uk Kim            my $dest = $_;
2198e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2199e71b7053SJung-uk Kim            if ($unified_info{rename}->{$ddest}) {
2200e71b7053SJung-uk Kim                $ddest = $unified_info{rename}->{$ddest};
2201e71b7053SJung-uk Kim            }
2202e71b7053SJung-uk Kim            die "more than one generator for $dest: "
2203e71b7053SJung-uk Kim                    ,join(" ", @{$generate{$_}}),"\n"
2204e71b7053SJung-uk Kim                    if scalar @{$generate{$_}} > 1;
2205e71b7053SJung-uk Kim            my @generator = split /\s+/, $generate{$dest}->[0];
2206e71b7053SJung-uk Kim            $generator[0] = cleanfile($sourced, $generator[0], $blddir),
2207e71b7053SJung-uk Kim            $unified_info{generate}->{$ddest} = [ @generator ];
2208e71b7053SJung-uk Kim        }
2209e71b7053SJung-uk Kim
2210e71b7053SJung-uk Kim        foreach (keys %depends) {
2211e71b7053SJung-uk Kim            my $dest = $_;
2212e71b7053SJung-uk Kim            my $ddest = $dest eq "" ? "" : cleanfile($sourced, $_, $blddir);
2213e71b7053SJung-uk Kim
2214e71b7053SJung-uk Kim            # If the destination doesn't exist in source, it can only be
2215e71b7053SJung-uk Kim            # a generated file in the build tree.
2216e71b7053SJung-uk Kim            if ($ddest ne "" && ($ddest eq $src_configdata || ! -f $ddest)) {
2217e71b7053SJung-uk Kim                $ddest = cleanfile($buildd, $_, $blddir);
2218e71b7053SJung-uk Kim                if ($unified_info{rename}->{$ddest}) {
2219e71b7053SJung-uk Kim                    $ddest = $unified_info{rename}->{$ddest};
2220e71b7053SJung-uk Kim                }
2221e71b7053SJung-uk Kim            }
2222e71b7053SJung-uk Kim            foreach (@{$depends{$dest}}) {
2223e71b7053SJung-uk Kim                my $d = cleanfile($sourced, $_, $blddir);
2224e71b7053SJung-uk Kim
2225e71b7053SJung-uk Kim                # If we know it's generated, or assume it is because we can't
2226e71b7053SJung-uk Kim                # find it in the source tree, we set file we depend on to be
2227e71b7053SJung-uk Kim                # in the build tree rather than the source tree, and assume
2228e71b7053SJung-uk Kim                # and that there are lines to build it in a BEGINRAW..ENDRAW
2229e71b7053SJung-uk Kim                # section or in the Makefile template.
2230e71b7053SJung-uk Kim                if ($d eq $src_configdata
2231e71b7053SJung-uk Kim                    || ! -f $d
2232e71b7053SJung-uk Kim                    || (grep { $d eq $_ }
2233e71b7053SJung-uk Kim                        map { cleanfile($srcdir, $_, $blddir) }
2234e71b7053SJung-uk Kim                        grep { /\.h$/ } keys %{$unified_info{generate}})) {
2235e71b7053SJung-uk Kim                    $d = cleanfile($buildd, $_, $blddir);
2236e71b7053SJung-uk Kim                }
2237e71b7053SJung-uk Kim                # Take note if the file to depend on is being renamed
2238e71b7053SJung-uk Kim                # Take extra care with files ending with .a, they should
2239e71b7053SJung-uk Kim                # be treated without that extension, and the extension
2240e71b7053SJung-uk Kim                # should be added back after treatment.
2241e71b7053SJung-uk Kim                $d =~ /(\.a)?$/;
2242e71b7053SJung-uk Kim                my $e = $1 // "";
2243e71b7053SJung-uk Kim                $d = $`;
2244e71b7053SJung-uk Kim                if ($unified_info{rename}->{$d}) {
2245e71b7053SJung-uk Kim                    $d = $unified_info{rename}->{$d};
2246e71b7053SJung-uk Kim                }
2247e71b7053SJung-uk Kim                $d .= $e;
2248e71b7053SJung-uk Kim                $unified_info{depends}->{$ddest}->{$d} = 1;
2249e71b7053SJung-uk Kim            }
2250e71b7053SJung-uk Kim        }
2251e71b7053SJung-uk Kim
2252e71b7053SJung-uk Kim        foreach (keys %includes) {
2253e71b7053SJung-uk Kim            my $dest = $_;
2254e71b7053SJung-uk Kim            my $ddest = cleanfile($sourced, $_, $blddir);
2255e71b7053SJung-uk Kim
2256e71b7053SJung-uk Kim            # If the destination doesn't exist in source, it can only be
2257e71b7053SJung-uk Kim            # a generated file in the build tree.
2258e71b7053SJung-uk Kim            if ($ddest eq $src_configdata || ! -f $ddest) {
2259e71b7053SJung-uk Kim                $ddest = cleanfile($buildd, $_, $blddir);
2260e71b7053SJung-uk Kim                if ($unified_info{rename}->{$ddest}) {
2261e71b7053SJung-uk Kim                    $ddest = $unified_info{rename}->{$ddest};
2262e71b7053SJung-uk Kim                }
2263e71b7053SJung-uk Kim            }
2264e71b7053SJung-uk Kim            foreach (@{$includes{$dest}}) {
2265e71b7053SJung-uk Kim                my $is = cleandir($sourced, $_, $blddir);
2266e71b7053SJung-uk Kim                my $ib = cleandir($buildd, $_, $blddir);
2267e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$ddest}->{source}}, $is
2268e71b7053SJung-uk Kim                    unless grep { $_ eq $is } @{$unified_info{includes}->{$ddest}->{source}};
2269e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$ddest}->{build}}, $ib
2270e71b7053SJung-uk Kim                    unless grep { $_ eq $ib } @{$unified_info{includes}->{$ddest}->{build}};
2271e71b7053SJung-uk Kim            }
2272e71b7053SJung-uk Kim        }
2273e71b7053SJung-uk Kim    }
2274e71b7053SJung-uk Kim
2275e71b7053SJung-uk Kim    my $ordinals_text = join(', ', sort keys %ordinals);
2276e71b7053SJung-uk Kim    warn <<"EOF" if $ordinals_text;
2277e71b7053SJung-uk Kim
2278e71b7053SJung-uk KimWARNING: ORDINALS were specified for $ordinals_text
2279e71b7053SJung-uk KimThey are ignored and should be replaced with a combination of GENERATE,
2280e71b7053SJung-uk KimDEPEND and SHARED_SOURCE.
2281e71b7053SJung-uk KimEOF
2282e71b7053SJung-uk Kim
2283e71b7053SJung-uk Kim    # Massage the result
2284e71b7053SJung-uk Kim
2285c9cf7b5cSJung-uk Kim    # If the user configured no-shared, we allow no shared sources
2286c9cf7b5cSJung-uk Kim    if ($disabled{shared}) {
2287c9cf7b5cSJung-uk Kim        foreach (keys %{$unified_info{shared_sources}}) {
2288c9cf7b5cSJung-uk Kim            foreach (keys %{$unified_info{shared_sources}->{$_}}) {
2289c9cf7b5cSJung-uk Kim                delete $unified_info{sources}->{$_};
2290c9cf7b5cSJung-uk Kim            }
2291c9cf7b5cSJung-uk Kim        }
2292c9cf7b5cSJung-uk Kim        $unified_info{shared_sources} = {};
2293c9cf7b5cSJung-uk Kim    }
2294c9cf7b5cSJung-uk Kim
2295e71b7053SJung-uk Kim    # If we depend on a header file or a perl module, add an inclusion of
2296e71b7053SJung-uk Kim    # its directory to allow smoothe inclusion
2297e71b7053SJung-uk Kim    foreach my $dest (keys %{$unified_info{depends}}) {
2298e71b7053SJung-uk Kim        next if $dest eq "";
2299e71b7053SJung-uk Kim        foreach my $d (keys %{$unified_info{depends}->{$dest}}) {
2300e71b7053SJung-uk Kim            next unless $d =~ /\.(h|pm)$/;
2301e71b7053SJung-uk Kim            my $i = dirname($d);
2302e71b7053SJung-uk Kim            my $spot =
2303e71b7053SJung-uk Kim                $d eq "configdata.pm" || defined($unified_info{generate}->{$d})
2304e71b7053SJung-uk Kim                ? 'build' : 'source';
2305e71b7053SJung-uk Kim            push @{$unified_info{includes}->{$dest}->{$spot}}, $i
2306e71b7053SJung-uk Kim                unless grep { $_ eq $i } @{$unified_info{includes}->{$dest}->{$spot}};
2307e71b7053SJung-uk Kim        }
2308e71b7053SJung-uk Kim    }
2309e71b7053SJung-uk Kim
2310e71b7053SJung-uk Kim    # Trickle down includes placed on libraries, engines and programs to
2311e71b7053SJung-uk Kim    # their sources (i.e. object files)
2312e71b7053SJung-uk Kim    foreach my $dest (keys %{$unified_info{engines}},
2313e71b7053SJung-uk Kim                      keys %{$unified_info{libraries}},
2314e71b7053SJung-uk Kim                      keys %{$unified_info{programs}}) {
2315e71b7053SJung-uk Kim        foreach my $k (("source", "build")) {
2316e71b7053SJung-uk Kim            next unless defined($unified_info{includes}->{$dest}->{$k});
2317e71b7053SJung-uk Kim            my @incs = reverse @{$unified_info{includes}->{$dest}->{$k}};
2318e71b7053SJung-uk Kim            foreach my $obj (grep /\.o$/,
2319c9cf7b5cSJung-uk Kim                             (keys %{$unified_info{sources}->{$dest} // {}},
2320c9cf7b5cSJung-uk Kim                              keys %{$unified_info{shared_sources}->{$dest} // {}})) {
2321e71b7053SJung-uk Kim                foreach my $inc (@incs) {
2322e71b7053SJung-uk Kim                    unshift @{$unified_info{includes}->{$obj}->{$k}}, $inc
2323e71b7053SJung-uk Kim                        unless grep { $_ eq $inc } @{$unified_info{includes}->{$obj}->{$k}};
2324e71b7053SJung-uk Kim                }
2325e71b7053SJung-uk Kim            }
2326e71b7053SJung-uk Kim        }
2327e71b7053SJung-uk Kim        delete $unified_info{includes}->{$dest};
2328e71b7053SJung-uk Kim    }
2329e71b7053SJung-uk Kim
2330e71b7053SJung-uk Kim    ### Make unified_info a bit more efficient
2331e71b7053SJung-uk Kim    # One level structures
2332e71b7053SJung-uk Kim    foreach (("programs", "libraries", "engines", "scripts", "extra", "overrides")) {
2333e71b7053SJung-uk Kim        $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
2334e71b7053SJung-uk Kim    }
2335e71b7053SJung-uk Kim    # Two level structures
2336e71b7053SJung-uk Kim    foreach my $l1 (("install", "sources", "shared_sources", "ldadd", "depends")) {
2337e71b7053SJung-uk Kim        foreach my $l2 (sort keys %{$unified_info{$l1}}) {
2338e71b7053SJung-uk Kim            $unified_info{$l1}->{$l2} =
2339e71b7053SJung-uk Kim                [ sort keys %{$unified_info{$l1}->{$l2}} ];
2340e71b7053SJung-uk Kim        }
2341e71b7053SJung-uk Kim    }
2342e71b7053SJung-uk Kim    # Includes
2343e71b7053SJung-uk Kim    foreach my $dest (sort keys %{$unified_info{includes}}) {
2344e71b7053SJung-uk Kim        if (defined($unified_info{includes}->{$dest}->{build})) {
2345e71b7053SJung-uk Kim            my @source_includes = ();
2346e71b7053SJung-uk Kim            @source_includes = ( @{$unified_info{includes}->{$dest}->{source}} )
2347e71b7053SJung-uk Kim                if defined($unified_info{includes}->{$dest}->{source});
2348e71b7053SJung-uk Kim            $unified_info{includes}->{$dest} =
2349e71b7053SJung-uk Kim                [ @{$unified_info{includes}->{$dest}->{build}} ];
2350e71b7053SJung-uk Kim            foreach my $inc (@source_includes) {
2351e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$dest}}, $inc
2352e71b7053SJung-uk Kim                    unless grep { $_ eq $inc } @{$unified_info{includes}->{$dest}};
2353e71b7053SJung-uk Kim            }
2354e71b7053SJung-uk Kim        } else {
2355e71b7053SJung-uk Kim            $unified_info{includes}->{$dest} =
2356e71b7053SJung-uk Kim                [ @{$unified_info{includes}->{$dest}->{source}} ];
2357e71b7053SJung-uk Kim        }
2358e71b7053SJung-uk Kim    }
2359c9cf7b5cSJung-uk Kim
2360c9cf7b5cSJung-uk Kim    # For convenience collect information regarding directories where
2361c9cf7b5cSJung-uk Kim    # files are generated, those generated files and the end product
2362c9cf7b5cSJung-uk Kim    # they end up in where applicable.  Then, add build rules for those
2363c9cf7b5cSJung-uk Kim    # directories
2364c9cf7b5cSJung-uk Kim    my %loopinfo = ( "lib" => [ @{$unified_info{libraries}} ],
2365c9cf7b5cSJung-uk Kim                     "dso" => [ @{$unified_info{engines}} ],
2366c9cf7b5cSJung-uk Kim                     "bin" => [ @{$unified_info{programs}} ],
2367c9cf7b5cSJung-uk Kim                     "script" => [ @{$unified_info{scripts}} ] );
2368c9cf7b5cSJung-uk Kim    foreach my $type (keys %loopinfo) {
2369c9cf7b5cSJung-uk Kim        foreach my $product (@{$loopinfo{$type}}) {
2370c9cf7b5cSJung-uk Kim            my %dirs = ();
2371c9cf7b5cSJung-uk Kim            my $pd = dirname($product);
2372c9cf7b5cSJung-uk Kim
2373c9cf7b5cSJung-uk Kim            foreach (@{$unified_info{sources}->{$product} // []},
2374c9cf7b5cSJung-uk Kim                     @{$unified_info{shared_sources}->{$product} // []}) {
2375c9cf7b5cSJung-uk Kim                my $d = dirname($_);
2376c9cf7b5cSJung-uk Kim
2377c9cf7b5cSJung-uk Kim                # We don't want to create targets for source directories
2378c9cf7b5cSJung-uk Kim                # when building out of source
2379c9cf7b5cSJung-uk Kim                next if ($config{sourcedir} ne $config{builddir}
2380c9cf7b5cSJung-uk Kim                             && $d =~ m|^\Q$config{sourcedir}\E|);
2381c9cf7b5cSJung-uk Kim                # We already have a "test" target, and the current directory
2382c9cf7b5cSJung-uk Kim                # is just silly to make a target for
2383c9cf7b5cSJung-uk Kim                next if $d eq "test" || $d eq ".";
2384c9cf7b5cSJung-uk Kim
2385c9cf7b5cSJung-uk Kim                $dirs{$d} = 1;
2386c9cf7b5cSJung-uk Kim                push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
2387c9cf7b5cSJung-uk Kim                    if $d ne $pd;
2388c9cf7b5cSJung-uk Kim            }
2389c9cf7b5cSJung-uk Kim            foreach (keys %dirs) {
2390c9cf7b5cSJung-uk Kim                push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
2391c9cf7b5cSJung-uk Kim                    $product;
2392c9cf7b5cSJung-uk Kim            }
2393c9cf7b5cSJung-uk Kim        }
2394c9cf7b5cSJung-uk Kim    }
2395e71b7053SJung-uk Kim}
2396e71b7053SJung-uk Kim
2397e71b7053SJung-uk Kim# For the schemes that need it, we provide the old *_obj configs
2398e71b7053SJung-uk Kim# from the *_asm_obj ones
2399e71b7053SJung-uk Kimforeach (grep /_(asm|aux)_src$/, keys %target) {
2400e71b7053SJung-uk Kim    my $src = $_;
2401e71b7053SJung-uk Kim    (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
2402e71b7053SJung-uk Kim    $target{$obj} = $target{$src};
2403e71b7053SJung-uk Kim    $target{$obj} =~ s/\.[csS]\b/.o/g; # C and assembler
2404e71b7053SJung-uk Kim    $target{$obj} =~ s/\.(cc|cpp)\b/_cc.o/g; # C++
2405e71b7053SJung-uk Kim}
2406e71b7053SJung-uk Kim
2407e71b7053SJung-uk Kim# Write down our configuration where it fits #########################
2408e71b7053SJung-uk Kim
2409e71b7053SJung-uk Kimprint "Creating configdata.pm\n";
2410e71b7053SJung-uk Kimopen(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
2411e71b7053SJung-uk Kimprint OUT <<"EOF";
2412e71b7053SJung-uk Kim#! $config{HASHBANGPERL}
2413e71b7053SJung-uk Kim
2414e71b7053SJung-uk Kimpackage configdata;
2415e71b7053SJung-uk Kim
2416e71b7053SJung-uk Kimuse strict;
2417e71b7053SJung-uk Kimuse warnings;
2418e71b7053SJung-uk Kim
2419e71b7053SJung-uk Kimuse Exporter;
2420e71b7053SJung-uk Kim#use vars qw(\@ISA \@EXPORT);
2421e71b7053SJung-uk Kimour \@ISA = qw(Exporter);
2422e71b7053SJung-uk Kimour \@EXPORT = qw(\%config \%target \%disabled \%withargs \%unified_info \@disablables);
2423e71b7053SJung-uk Kim
2424e71b7053SJung-uk KimEOF
2425e71b7053SJung-uk Kimprint OUT "our %config = (\n";
2426e71b7053SJung-uk Kimforeach (sort keys %config) {
2427e71b7053SJung-uk Kim    if (ref($config{$_}) eq "ARRAY") {
2428e71b7053SJung-uk Kim        print OUT "  ", $_, " => [ ", join(", ",
2429e71b7053SJung-uk Kim                                           map { quotify("perl", $_) }
2430e71b7053SJung-uk Kim                                           @{$config{$_}}), " ],\n";
2431e71b7053SJung-uk Kim    } elsif (ref($config{$_}) eq "HASH") {
2432e71b7053SJung-uk Kim        print OUT "  ", $_, " => {";
2433e71b7053SJung-uk Kim        if (scalar keys %{$config{$_}} > 0) {
2434db522d3aSSimon L. B. Nielsen            print OUT "\n";
2435e71b7053SJung-uk Kim            foreach my $key (sort keys %{$config{$_}}) {
2436e71b7053SJung-uk Kim                print OUT "      ",
2437e71b7053SJung-uk Kim                    join(" => ",
2438e71b7053SJung-uk Kim                         quotify("perl", $key),
2439e71b7053SJung-uk Kim                         defined $config{$_}->{$key}
2440e71b7053SJung-uk Kim                             ? quotify("perl", $config{$_}->{$key})
2441e71b7053SJung-uk Kim                             : "undef");
2442e71b7053SJung-uk Kim                print OUT ",\n";
24436a599222SSimon L. B. Nielsen            }
2444e71b7053SJung-uk Kim            print OUT "  ";
24456a599222SSimon L. B. Nielsen        }
2446e71b7053SJung-uk Kim        print OUT "},\n";
244774664626SKris Kennaway    } else {
2448e71b7053SJung-uk Kim        print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
2449ed7112f0SJung-uk Kim    }
2450e71b7053SJung-uk Kim}
2451e71b7053SJung-uk Kimprint OUT <<"EOF";
2452e71b7053SJung-uk Kim);
2453e71b7053SJung-uk Kim
2454e71b7053SJung-uk KimEOF
2455e71b7053SJung-uk Kimprint OUT "our %target = (\n";
2456e71b7053SJung-uk Kimforeach (sort keys %target) {
2457e71b7053SJung-uk Kim    if (ref($target{$_}) eq "ARRAY") {
2458e71b7053SJung-uk Kim        print OUT "  ", $_, " => [ ", join(", ",
2459e71b7053SJung-uk Kim                                           map { quotify("perl", $_) }
2460e71b7053SJung-uk Kim                                           @{$target{$_}}), " ],\n";
246174664626SKris Kennaway    } else {
2462e71b7053SJung-uk Kim        print OUT "  ", $_, " => ", quotify("perl", $target{$_}), ",\n"
246374664626SKris Kennaway    }
2464e71b7053SJung-uk Kim}
2465e71b7053SJung-uk Kimprint OUT <<"EOF";
2466e71b7053SJung-uk Kim);
2467e71b7053SJung-uk Kim
2468e71b7053SJung-uk KimEOF
2469e71b7053SJung-uk Kimprint OUT "our \%available_protocols = (\n";
2470e71b7053SJung-uk Kimprint OUT "  tls => [ ", join(", ", map { quotify("perl", $_) } @tls), " ],\n";
2471e71b7053SJung-uk Kimprint OUT "  dtls => [ ", join(", ", map { quotify("perl", $_) } @dtls), " ],\n";
2472e71b7053SJung-uk Kimprint OUT <<"EOF";
2473e71b7053SJung-uk Kim);
2474e71b7053SJung-uk Kim
2475e71b7053SJung-uk KimEOF
2476e71b7053SJung-uk Kimprint OUT "our \@disablables = (\n";
2477e71b7053SJung-uk Kimforeach (@disablables) {
2478e71b7053SJung-uk Kim    print OUT "  ", quotify("perl", $_), ",\n";
2479e71b7053SJung-uk Kim}
2480e71b7053SJung-uk Kimprint OUT <<"EOF";
2481e71b7053SJung-uk Kim);
2482e71b7053SJung-uk Kim
2483e71b7053SJung-uk KimEOF
2484e71b7053SJung-uk Kimprint OUT "our \%disabled = (\n";
2485e71b7053SJung-uk Kimforeach (sort keys %disabled) {
2486e71b7053SJung-uk Kim    print OUT "  ", quotify("perl", $_), " => ", quotify("perl", $disabled{$_}), ",\n";
2487e71b7053SJung-uk Kim}
2488e71b7053SJung-uk Kimprint OUT <<"EOF";
2489e71b7053SJung-uk Kim);
2490e71b7053SJung-uk Kim
2491e71b7053SJung-uk KimEOF
2492e71b7053SJung-uk Kimprint OUT "our %withargs = (\n";
2493e71b7053SJung-uk Kimforeach (sort keys %withargs) {
2494e71b7053SJung-uk Kim    if (ref($withargs{$_}) eq "ARRAY") {
2495e71b7053SJung-uk Kim        print OUT "  ", $_, " => [ ", join(", ",
2496e71b7053SJung-uk Kim                                           map { quotify("perl", $_) }
2497e71b7053SJung-uk Kim                                           @{$withargs{$_}}), " ],\n";
2498e71b7053SJung-uk Kim    } else {
2499e71b7053SJung-uk Kim        print OUT "  ", $_, " => ", quotify("perl", $withargs{$_}), ",\n"
2500e71b7053SJung-uk Kim    }
2501e71b7053SJung-uk Kim}
2502e71b7053SJung-uk Kimprint OUT <<"EOF";
2503e71b7053SJung-uk Kim);
2504e71b7053SJung-uk Kim
2505e71b7053SJung-uk KimEOF
2506e71b7053SJung-uk Kimif ($builder eq "unified") {
2507e71b7053SJung-uk Kim    my $recurse;
2508e71b7053SJung-uk Kim    $recurse = sub {
2509e71b7053SJung-uk Kim        my $indent = shift;
2510e71b7053SJung-uk Kim        foreach (@_) {
2511e71b7053SJung-uk Kim            if (ref $_ eq "ARRAY") {
2512e71b7053SJung-uk Kim                print OUT " "x$indent, "[\n";
2513e71b7053SJung-uk Kim                foreach (@$_) {
2514e71b7053SJung-uk Kim                    $recurse->($indent + 4, $_);
2515e71b7053SJung-uk Kim                }
2516e71b7053SJung-uk Kim                print OUT " "x$indent, "],\n";
2517e71b7053SJung-uk Kim            } elsif (ref $_ eq "HASH") {
2518e71b7053SJung-uk Kim                my %h = %$_;
2519e71b7053SJung-uk Kim                print OUT " "x$indent, "{\n";
2520e71b7053SJung-uk Kim                foreach (sort keys %h) {
2521e71b7053SJung-uk Kim                    if (ref $h{$_} eq "") {
2522e71b7053SJung-uk Kim                        print OUT " "x($indent + 4), quotify("perl", $_), " => ", quotify("perl", $h{$_}), ",\n";
2523e71b7053SJung-uk Kim                    } else {
2524e71b7053SJung-uk Kim                        print OUT " "x($indent + 4), quotify("perl", $_), " =>\n";
2525e71b7053SJung-uk Kim                        $recurse->($indent + 8, $h{$_});
2526e71b7053SJung-uk Kim                    }
2527e71b7053SJung-uk Kim                }
2528e71b7053SJung-uk Kim                print OUT " "x$indent, "},\n";
2529e71b7053SJung-uk Kim            } else {
2530e71b7053SJung-uk Kim                print OUT " "x$indent, quotify("perl", $_), ",\n";
2531e71b7053SJung-uk Kim            }
2532e71b7053SJung-uk Kim        }
2533e71b7053SJung-uk Kim    };
2534e71b7053SJung-uk Kim    print OUT "our %unified_info = (\n";
2535e71b7053SJung-uk Kim    foreach (sort keys %unified_info) {
2536e71b7053SJung-uk Kim        if (ref $unified_info{$_} eq "") {
2537e71b7053SJung-uk Kim            print OUT " "x4, quotify("perl", $_), " => ", quotify("perl", $unified_info{$_}), ",\n";
2538e71b7053SJung-uk Kim        } else {
2539e71b7053SJung-uk Kim            print OUT " "x4, quotify("perl", $_), " =>\n";
2540e71b7053SJung-uk Kim            $recurse->(8, $unified_info{$_});
2541e71b7053SJung-uk Kim        }
2542e71b7053SJung-uk Kim    }
2543e71b7053SJung-uk Kim    print OUT <<"EOF";
2544e71b7053SJung-uk Kim);
2545e71b7053SJung-uk Kim
2546e71b7053SJung-uk KimEOF
2547e71b7053SJung-uk Kim}
2548e71b7053SJung-uk Kimprint OUT
2549e71b7053SJung-uk Kim    "# The following data is only used when this files is use as a script\n";
2550e71b7053SJung-uk Kimprint OUT "my \@makevars = (\n";
2551e71b7053SJung-uk Kimforeach (sort keys %user) {
2552e71b7053SJung-uk Kim    print OUT "    '",$_,"',\n";
2553e71b7053SJung-uk Kim}
2554e71b7053SJung-uk Kimprint OUT ");\n";
2555e71b7053SJung-uk Kimprint OUT "my \%disabled_info = (\n";
2556e71b7053SJung-uk Kimforeach my $what (sort keys %disabled_info) {
2557e71b7053SJung-uk Kim    print OUT "    '$what' => {\n";
2558e71b7053SJung-uk Kim    foreach my $info (sort keys %{$disabled_info{$what}}) {
2559e71b7053SJung-uk Kim        if (ref $disabled_info{$what}->{$info} eq 'ARRAY') {
2560e71b7053SJung-uk Kim            print OUT "        $info => [ ",
2561e71b7053SJung-uk Kim                join(', ', map { "'$_'" } @{$disabled_info{$what}->{$info}}),
2562e71b7053SJung-uk Kim                " ],\n";
2563e71b7053SJung-uk Kim        } else {
2564e71b7053SJung-uk Kim            print OUT "        $info => '", $disabled_info{$what}->{$info},
2565e71b7053SJung-uk Kim                "',\n";
2566e71b7053SJung-uk Kim        }
2567e71b7053SJung-uk Kim    }
2568e71b7053SJung-uk Kim    print OUT "    },\n";
2569e71b7053SJung-uk Kim}
2570e71b7053SJung-uk Kimprint OUT ");\n";
2571e71b7053SJung-uk Kimprint OUT 'my @user_crossable = qw( ', join (' ', @user_crossable), " );\n";
2572e71b7053SJung-uk Kimprint OUT << 'EOF';
2573e71b7053SJung-uk Kim# If run directly, we can give some answers, and even reconfigure
2574e71b7053SJung-uk Kimunless (caller) {
2575e71b7053SJung-uk Kim    use Getopt::Long;
2576e71b7053SJung-uk Kim    use File::Spec::Functions;
2577e71b7053SJung-uk Kim    use File::Basename;
2578e71b7053SJung-uk Kim    use Pod::Usage;
2579e71b7053SJung-uk Kim
2580e71b7053SJung-uk Kim    my $here = dirname($0);
2581e71b7053SJung-uk Kim
2582e71b7053SJung-uk Kim    my $dump = undef;
2583e71b7053SJung-uk Kim    my $cmdline = undef;
2584e71b7053SJung-uk Kim    my $options = undef;
2585e71b7053SJung-uk Kim    my $target = undef;
2586e71b7053SJung-uk Kim    my $envvars = undef;
2587e71b7053SJung-uk Kim    my $makevars = undef;
2588e71b7053SJung-uk Kim    my $buildparams = undef;
2589e71b7053SJung-uk Kim    my $reconf = undef;
2590e71b7053SJung-uk Kim    my $verbose = undef;
2591e71b7053SJung-uk Kim    my $help = undef;
2592e71b7053SJung-uk Kim    my $man = undef;
2593e71b7053SJung-uk Kim    GetOptions('dump|d'                 => \$dump,
2594e71b7053SJung-uk Kim               'command-line|c'         => \$cmdline,
2595e71b7053SJung-uk Kim               'options|o'              => \$options,
2596e71b7053SJung-uk Kim               'target|t'               => \$target,
2597e71b7053SJung-uk Kim               'environment|e'          => \$envvars,
2598e71b7053SJung-uk Kim               'make-variables|m'       => \$makevars,
2599e71b7053SJung-uk Kim               'build-parameters|b'     => \$buildparams,
2600e71b7053SJung-uk Kim               'reconfigure|reconf|r'   => \$reconf,
2601e71b7053SJung-uk Kim               'verbose|v'              => \$verbose,
2602e71b7053SJung-uk Kim               'help'                   => \$help,
2603e71b7053SJung-uk Kim               'man'                    => \$man)
2604e71b7053SJung-uk Kim        or die "Errors in command line arguments\n";
2605e71b7053SJung-uk Kim
2606e71b7053SJung-uk Kim    unless ($dump || $cmdline || $options || $target || $envvars || $makevars
2607e71b7053SJung-uk Kim            || $buildparams || $reconf || $verbose || $help || $man) {
2608e71b7053SJung-uk Kim        print STDERR <<"_____";
2609e71b7053SJung-uk KimYou must give at least one option.
2610e71b7053SJung-uk KimFor more information, do '$0 --help'
2611e71b7053SJung-uk Kim_____
2612e71b7053SJung-uk Kim        exit(2);
2613e71b7053SJung-uk Kim    }
2614e71b7053SJung-uk Kim
2615e71b7053SJung-uk Kim    if ($help) {
2616e71b7053SJung-uk Kim        pod2usage(-exitval => 0,
2617e71b7053SJung-uk Kim                  -verbose => 1);
2618e71b7053SJung-uk Kim    }
2619e71b7053SJung-uk Kim    if ($man) {
2620e71b7053SJung-uk Kim        pod2usage(-exitval => 0,
2621e71b7053SJung-uk Kim                  -verbose => 2);
2622e71b7053SJung-uk Kim    }
2623e71b7053SJung-uk Kim    if ($dump || $cmdline) {
2624e71b7053SJung-uk Kim        print "\nCommand line (with current working directory = $here):\n\n";
2625e71b7053SJung-uk Kim        print '    ',join(' ',
2626e71b7053SJung-uk Kim                          $config{PERL},
2627e71b7053SJung-uk Kim                          catfile($config{sourcedir}, 'Configure'),
2628e71b7053SJung-uk Kim                          @{$config{perlargv}}), "\n";
2629e71b7053SJung-uk Kim        print "\nPerl information:\n\n";
2630e71b7053SJung-uk Kim        print '    ',$config{perl_cmd},"\n";
2631e71b7053SJung-uk Kim        print '    ',$config{perl_version},' for ',$config{perl_archname},"\n";
2632e71b7053SJung-uk Kim    }
2633e71b7053SJung-uk Kim    if ($dump || $options) {
2634e71b7053SJung-uk Kim        my $longest = 0;
2635e71b7053SJung-uk Kim        my $longest2 = 0;
2636e71b7053SJung-uk Kim        foreach my $what (@disablables) {
2637e71b7053SJung-uk Kim            $longest = length($what) if $longest < length($what);
2638e71b7053SJung-uk Kim            $longest2 = length($disabled{$what})
2639e71b7053SJung-uk Kim                if $disabled{$what} && $longest2 < length($disabled{$what});
2640e71b7053SJung-uk Kim        }
2641e71b7053SJung-uk Kim        print "\nEnabled features:\n\n";
2642e71b7053SJung-uk Kim        foreach my $what (@disablables) {
2643*88e852c0SJung-uk Kim            print "    $what\n"
2644*88e852c0SJung-uk Kim                unless grep { $_ =~ /^${what}$/ } keys %disabled;
2645e71b7053SJung-uk Kim        }
2646e71b7053SJung-uk Kim        print "\nDisabled features:\n\n";
2647e71b7053SJung-uk Kim        foreach my $what (@disablables) {
2648*88e852c0SJung-uk Kim            my @what2 = grep { $_ =~ /^${what}$/ } keys %disabled;
2649*88e852c0SJung-uk Kim            my $what3 = $what2[0];
2650*88e852c0SJung-uk Kim            if ($what3) {
2651*88e852c0SJung-uk Kim                print "    $what3", ' ' x ($longest - length($what3) + 1),
2652*88e852c0SJung-uk Kim                    "[$disabled{$what3}]", ' ' x ($longest2 - length($disabled{$what3}) + 1);
2653*88e852c0SJung-uk Kim                print $disabled_info{$what3}->{macro}
2654*88e852c0SJung-uk Kim                    if $disabled_info{$what3}->{macro};
2655e71b7053SJung-uk Kim                print ' (skip ',
2656*88e852c0SJung-uk Kim                    join(', ', @{$disabled_info{$what3}->{skipped}}),
2657e71b7053SJung-uk Kim                    ')'
2658*88e852c0SJung-uk Kim                    if $disabled_info{$what3}->{skipped};
2659e71b7053SJung-uk Kim                print "\n";
2660e71b7053SJung-uk Kim            }
2661e71b7053SJung-uk Kim        }
2662e71b7053SJung-uk Kim    }
2663e71b7053SJung-uk Kim    if ($dump || $target) {
2664e71b7053SJung-uk Kim        print "\nConfig target attributes:\n\n";
2665e71b7053SJung-uk Kim        foreach (sort keys %target) {
2666e71b7053SJung-uk Kim            next if $_ =~ m|^_| || $_ eq 'template';
2667e71b7053SJung-uk Kim            my $quotify = sub {
2668e71b7053SJung-uk Kim                map { (my $x = $_) =~ s|([\\\$\@"])|\\$1|g; "\"$x\""} @_;
2669e71b7053SJung-uk Kim            };
2670e71b7053SJung-uk Kim            print '    ', $_, ' => ';
2671e71b7053SJung-uk Kim            if (ref($target{$_}) eq "ARRAY") {
2672e71b7053SJung-uk Kim                print '[ ', join(', ', $quotify->(@{$target{$_}})), " ],\n";
2673e71b7053SJung-uk Kim            } else {
2674e71b7053SJung-uk Kim                print $quotify->($target{$_}), ",\n"
2675e71b7053SJung-uk Kim            }
2676e71b7053SJung-uk Kim        }
2677e71b7053SJung-uk Kim    }
2678e71b7053SJung-uk Kim    if ($dump || $envvars) {
2679e71b7053SJung-uk Kim        print "\nRecorded environment:\n\n";
2680e71b7053SJung-uk Kim        foreach (sort keys %{$config{perlenv}}) {
2681e71b7053SJung-uk Kim            print '    ',$_,' = ',($config{perlenv}->{$_} || ''),"\n";
2682e71b7053SJung-uk Kim        }
2683e71b7053SJung-uk Kim    }
2684e71b7053SJung-uk Kim    if ($dump || $makevars) {
2685e71b7053SJung-uk Kim        print "\nMakevars:\n\n";
2686e71b7053SJung-uk Kim        foreach my $var (@makevars) {
2687e71b7053SJung-uk Kim            my $prefix = '';
2688e71b7053SJung-uk Kim            $prefix = $config{CROSS_COMPILE}
2689e71b7053SJung-uk Kim                if grep { $var eq $_ } @user_crossable;
2690e71b7053SJung-uk Kim            $prefix //= '';
2691e71b7053SJung-uk Kim            print '    ',$var,' ' x (16 - length $var),'= ',
2692e71b7053SJung-uk Kim                (ref $config{$var} eq 'ARRAY'
2693e71b7053SJung-uk Kim                 ? join(' ', @{$config{$var}})
2694e71b7053SJung-uk Kim                 : $prefix.$config{$var}),
2695e71b7053SJung-uk Kim                "\n"
2696e71b7053SJung-uk Kim                if defined $config{$var};
2697e71b7053SJung-uk Kim        }
2698e71b7053SJung-uk Kim
2699e71b7053SJung-uk Kim        my @buildfile = ($config{builddir}, $config{build_file});
2700e71b7053SJung-uk Kim        unshift @buildfile, $here
2701e71b7053SJung-uk Kim            unless file_name_is_absolute($config{builddir});
2702e71b7053SJung-uk Kim        my $buildfile = canonpath(catdir(@buildfile));
2703e71b7053SJung-uk Kim        print <<"_____";
2704e71b7053SJung-uk Kim
2705e71b7053SJung-uk KimNOTE: These variables only represent the configuration view.  The build file
2706e71b7053SJung-uk Kimtemplate may have processed these variables further, please have a look at the
2707e71b7053SJung-uk Kimbuild file for more exact data:
2708e71b7053SJung-uk Kim    $buildfile
2709e71b7053SJung-uk Kim_____
2710e71b7053SJung-uk Kim    }
2711e71b7053SJung-uk Kim    if ($dump || $buildparams) {
2712e71b7053SJung-uk Kim        my @buildfile = ($config{builddir}, $config{build_file});
2713e71b7053SJung-uk Kim        unshift @buildfile, $here
2714e71b7053SJung-uk Kim            unless file_name_is_absolute($config{builddir});
2715e71b7053SJung-uk Kim        print "\nbuild file:\n\n";
2716e71b7053SJung-uk Kim        print "    ", canonpath(catfile(@buildfile)),"\n";
2717e71b7053SJung-uk Kim
2718e71b7053SJung-uk Kim        print "\nbuild file templates:\n\n";
2719e71b7053SJung-uk Kim        foreach (@{$config{build_file_templates}}) {
2720e71b7053SJung-uk Kim            my @tmpl = ($_);
2721e71b7053SJung-uk Kim            unshift @tmpl, $here
2722e71b7053SJung-uk Kim                unless file_name_is_absolute($config{sourcedir});
2723e71b7053SJung-uk Kim            print '    ',canonpath(catfile(@tmpl)),"\n";
2724e71b7053SJung-uk Kim        }
2725e71b7053SJung-uk Kim    }
2726e71b7053SJung-uk Kim    if ($reconf) {
2727e71b7053SJung-uk Kim        if ($verbose) {
2728e71b7053SJung-uk Kim            print 'Reconfiguring with: ', join(' ',@{$config{perlargv}}), "\n";
2729e71b7053SJung-uk Kim            foreach (sort keys %{$config{perlenv}}) {
2730e71b7053SJung-uk Kim                print '    ',$_,' = ',($config{perlenv}->{$_} || ""),"\n";
27315c87c606SMark Murray            }
273274664626SKris Kennaway        }
273374664626SKris Kennaway
2734e71b7053SJung-uk Kim        chdir $here;
2735e71b7053SJung-uk Kim        exec $^X,catfile($config{sourcedir}, 'Configure'),'reconf';
27363b4e3dcbSSimon L. B. Nielsen    }
2737e71b7053SJung-uk Kim}
27383b4e3dcbSSimon L. B. Nielsen
2739e71b7053SJung-uk Kim1;
27403b4e3dcbSSimon L. B. Nielsen
2741e71b7053SJung-uk Kim__END__
2742e71b7053SJung-uk Kim
2743e71b7053SJung-uk Kim=head1 NAME
2744e71b7053SJung-uk Kim
2745e71b7053SJung-uk Kimconfigdata.pm - configuration data for OpenSSL builds
2746e71b7053SJung-uk Kim
2747e71b7053SJung-uk Kim=head1 SYNOPSIS
2748e71b7053SJung-uk Kim
2749e71b7053SJung-uk KimInteractive:
2750e71b7053SJung-uk Kim
2751e71b7053SJung-uk Kim  perl configdata.pm [options]
2752e71b7053SJung-uk Kim
2753e71b7053SJung-uk KimAs data bank module:
2754e71b7053SJung-uk Kim
2755e71b7053SJung-uk Kim  use configdata;
2756e71b7053SJung-uk Kim
2757e71b7053SJung-uk Kim=head1 DESCRIPTION
2758e71b7053SJung-uk Kim
2759e71b7053SJung-uk KimThis module can be used in two modes, interactively and as a module containing
2760e71b7053SJung-uk Kimall the data recorded by OpenSSL's Configure script.
2761e71b7053SJung-uk Kim
2762e71b7053SJung-uk KimWhen used interactively, simply run it as any perl script, with at least one
2763e71b7053SJung-uk Kimoption, and you will get the information you ask for.  See L</OPTIONS> below.
2764e71b7053SJung-uk Kim
2765e71b7053SJung-uk KimWhen loaded as a module, you get a few databanks with useful information to
2766e71b7053SJung-uk Kimperform build related tasks.  The databanks are:
2767e71b7053SJung-uk Kim
2768e71b7053SJung-uk Kim    %config             Configured things.
2769e71b7053SJung-uk Kim    %target             The OpenSSL config target with all inheritances
2770e71b7053SJung-uk Kim                        resolved.
2771e71b7053SJung-uk Kim    %disabled           The features that are disabled.
2772e71b7053SJung-uk Kim    @disablables        The list of features that can be disabled.
2773e71b7053SJung-uk Kim    %withargs           All data given through --with-THING options.
2774e71b7053SJung-uk Kim    %unified_info       All information that was computed from the build.info
2775e71b7053SJung-uk Kim                        files.
2776e71b7053SJung-uk Kim
2777e71b7053SJung-uk Kim=head1 OPTIONS
2778e71b7053SJung-uk Kim
2779e71b7053SJung-uk Kim=over 4
2780e71b7053SJung-uk Kim
2781e71b7053SJung-uk Kim=item B<--help>
2782e71b7053SJung-uk Kim
2783e71b7053SJung-uk KimPrint a brief help message and exit.
2784e71b7053SJung-uk Kim
2785e71b7053SJung-uk Kim=item B<--man>
2786e71b7053SJung-uk Kim
2787e71b7053SJung-uk KimPrint the manual page and exit.
2788e71b7053SJung-uk Kim
2789e71b7053SJung-uk Kim=item B<--dump> | B<-d>
2790e71b7053SJung-uk Kim
2791e71b7053SJung-uk KimPrint all relevant configuration data.  This is equivalent to B<--command-line>
2792e71b7053SJung-uk KimB<--options> B<--target> B<--environment> B<--make-variables>
2793e71b7053SJung-uk KimB<--build-parameters>.
2794e71b7053SJung-uk Kim
2795e71b7053SJung-uk Kim=item B<--command-line> | B<-c>
2796e71b7053SJung-uk Kim
2797e71b7053SJung-uk KimPrint the current configuration command line.
2798e71b7053SJung-uk Kim
2799e71b7053SJung-uk Kim=item B<--options> | B<-o>
2800e71b7053SJung-uk Kim
2801e71b7053SJung-uk KimPrint the features, both enabled and disabled, and display defined macro and
2802e71b7053SJung-uk Kimskipped directories where applicable.
2803e71b7053SJung-uk Kim
2804e71b7053SJung-uk Kim=item B<--target> | B<-t>
2805e71b7053SJung-uk Kim
2806e71b7053SJung-uk KimPrint the config attributes for this config target.
2807e71b7053SJung-uk Kim
2808e71b7053SJung-uk Kim=item B<--environment> | B<-e>
2809e71b7053SJung-uk Kim
2810e71b7053SJung-uk KimPrint the environment variables and their values at the time of configuration.
2811e71b7053SJung-uk Kim
2812e71b7053SJung-uk Kim=item B<--make-variables> | B<-m>
2813e71b7053SJung-uk Kim
2814e71b7053SJung-uk KimPrint the main make variables generated in the current configuration
2815e71b7053SJung-uk Kim
2816e71b7053SJung-uk Kim=item B<--build-parameters> | B<-b>
2817e71b7053SJung-uk Kim
2818e71b7053SJung-uk KimPrint the build parameters, i.e. build file and build file templates.
2819e71b7053SJung-uk Kim
2820e71b7053SJung-uk Kim=item B<--reconfigure> | B<--reconf> | B<-r>
2821e71b7053SJung-uk Kim
2822e71b7053SJung-uk KimRedo the configuration.
2823e71b7053SJung-uk Kim
2824e71b7053SJung-uk Kim=item B<--verbose> | B<-v>
2825e71b7053SJung-uk Kim
2826e71b7053SJung-uk KimVerbose output.
2827e71b7053SJung-uk Kim
2828e71b7053SJung-uk Kim=back
2829e71b7053SJung-uk Kim
2830e71b7053SJung-uk Kim=cut
2831e71b7053SJung-uk Kim
28323b4e3dcbSSimon L. B. NielsenEOF
28333b4e3dcbSSimon L. B. Nielsenclose(OUT);
2834e71b7053SJung-uk Kimif ($builder_platform eq 'unix') {
2835e71b7053SJung-uk Kim    my $mode = (0755 & ~umask);
2836e71b7053SJung-uk Kim    chmod $mode, 'configdata.pm'
2837e71b7053SJung-uk Kim        or warn sprintf("WARNING: Couldn't change mode for 'configdata.pm' to 0%03o: %s\n",$mode,$!);
28383b4e3dcbSSimon L. B. Nielsen}
28393b4e3dcbSSimon L. B. Nielsen
2840e71b7053SJung-uk Kimmy %builders = (
2841e71b7053SJung-uk Kim    unified => sub {
2842e71b7053SJung-uk Kim        print 'Creating ',$target{build_file},"\n";
2843e71b7053SJung-uk Kim        run_dofile(catfile($blddir, $target{build_file}),
2844e71b7053SJung-uk Kim                   @{$config{build_file_templates}});
2845e71b7053SJung-uk Kim    },
2846e71b7053SJung-uk Kim    );
284774664626SKris Kennaway
2848e71b7053SJung-uk Kim$builders{$builder}->($builder_platform, @builder_opts);
284974664626SKris Kennaway
2850e71b7053SJung-uk Kim$SIG{__DIE__} = $orig_death_handler;
2851e71b7053SJung-uk Kim
2852e71b7053SJung-uk Kimprint <<"EOF" if ($disabled{threads} eq "unavailable");
285374664626SKris Kennaway
285474664626SKris KennawayThe library could not be configured for supporting multi-threaded
285574664626SKris Kennawayapplications as the compiler options required on this system are not known.
285674664626SKris KennawaySee file INSTALL for details if you need multi-threading.
285774664626SKris KennawayEOF
285874664626SKris Kennaway
2859e71b7053SJung-uk Kimprint <<"EOF" if ($no_shared_warn);
2860fceca8a3SJacques Vidrine
2861e71b7053SJung-uk KimThe options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
2862e71b7053SJung-uk Kimplatform, so we will pretend you gave the option 'no-pic', which also disables
2863e71b7053SJung-uk Kim'shared' and 'dynamic-engine'.  If you know how to implement shared libraries
2864e71b7053SJung-uk Kimor position independent code, please let us know (but please first make sure
2865e71b7053SJung-uk Kimyou have tried with a current version of OpenSSL).
28668180e704SJung-uk KimEOF
28678180e704SJung-uk Kim
2868e71b7053SJung-uk Kimprint <<"EOF";
28698180e704SJung-uk Kim
2870e71b7053SJung-uk Kim**********************************************************************
2871e71b7053SJung-uk Kim***                                                                ***
2872c9cf7b5cSJung-uk Kim***   OpenSSL has been successfully configured                     ***
2873c9cf7b5cSJung-uk Kim***                                                                ***
2874c9cf7b5cSJung-uk Kim***   If you encounter a problem while building, please open an    ***
2875c9cf7b5cSJung-uk Kim***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
2876c9cf7b5cSJung-uk Kim***   and include the output from the following command:           ***
2877e71b7053SJung-uk Kim***                                                                ***
2878e71b7053SJung-uk Kim***       perl configdata.pm --dump                                ***
2879e71b7053SJung-uk Kim***                                                                ***
2880c9cf7b5cSJung-uk Kim***   (If you are new to OpenSSL, you might want to consult the    ***
2881c9cf7b5cSJung-uk Kim***   'Troubleshooting' section in the INSTALL file first)         ***
2882c9cf7b5cSJung-uk Kim***                                                                ***
2883e71b7053SJung-uk Kim**********************************************************************
2884fceca8a3SJacques VidrineEOF
2885fceca8a3SJacques Vidrine
288674664626SKris Kennawayexit(0);
288774664626SKris Kennaway
2888e71b7053SJung-uk Kim######################################################################
2889e71b7053SJung-uk Kim#
2890e71b7053SJung-uk Kim# Helpers and utility functions
2891e71b7053SJung-uk Kim#
2892e71b7053SJung-uk Kim
2893e71b7053SJung-uk Kim# Death handler, to print a helpful message in case of failure #######
2894e71b7053SJung-uk Kim#
2895e71b7053SJung-uk Kimsub death_handler {
2896e71b7053SJung-uk Kim    die @_ if $^S;              # To prevent the added message in eval blocks
2897e71b7053SJung-uk Kim    my $build_file = $target{build_file} // "build file";
2898e71b7053SJung-uk Kim    my @message = ( <<"_____", @_ );
2899e71b7053SJung-uk Kim
2900e71b7053SJung-uk KimFailure!  $build_file wasn't produced.
2901e71b7053SJung-uk KimPlease read INSTALL and associated NOTES files.  You may also have to look over
2902e71b7053SJung-uk Kimyour available compiler tool chain or change your configuration.
2903e71b7053SJung-uk Kim
2904e71b7053SJung-uk Kim_____
2905e71b7053SJung-uk Kim
2906e71b7053SJung-uk Kim    # Dying is terminal, so it's ok to reset the signal handler here.
2907e71b7053SJung-uk Kim    $SIG{__DIE__} = $orig_death_handler;
2908e71b7053SJung-uk Kim    die @message;
2909e71b7053SJung-uk Kim}
2910e71b7053SJung-uk Kim
2911e71b7053SJung-uk Kim# Configuration file reading #########################################
2912e71b7053SJung-uk Kim
2913e71b7053SJung-uk Kim# Note: All of the helper functions are for lazy evaluation.  They all
2914e71b7053SJung-uk Kim# return a CODE ref, which will return the intended value when evaluated.
2915e71b7053SJung-uk Kim# Thus, whenever there's mention of a returned value, it's about that
2916e71b7053SJung-uk Kim# intended value.
2917e71b7053SJung-uk Kim
2918e71b7053SJung-uk Kim# Helper function to implement conditional inheritance depending on the
2919e71b7053SJung-uk Kim# value of $disabled{asm}.  Used in inherit_from values as follows:
2920e71b7053SJung-uk Kim#
2921e71b7053SJung-uk Kim#      inherit_from => [ "template", asm("asm_tmpl") ]
2922e71b7053SJung-uk Kim#
2923e71b7053SJung-uk Kimsub asm {
2924e71b7053SJung-uk Kim    my @x = @_;
2925e71b7053SJung-uk Kim    sub {
2926e71b7053SJung-uk Kim        $disabled{asm} ? () : @x;
2927e71b7053SJung-uk Kim    }
2928e71b7053SJung-uk Kim}
2929e71b7053SJung-uk Kim
2930e71b7053SJung-uk Kim# Helper function to implement conditional value variants, with a default
2931e71b7053SJung-uk Kim# plus additional values based on the value of $config{build_type}.
2932e71b7053SJung-uk Kim# Arguments are given in hash table form:
2933e71b7053SJung-uk Kim#
2934e71b7053SJung-uk Kim#       picker(default => "Basic string: ",
2935e71b7053SJung-uk Kim#              debug   => "debug",
2936e71b7053SJung-uk Kim#              release => "release")
2937e71b7053SJung-uk Kim#
2938e71b7053SJung-uk Kim# When configuring with --debug, the resulting string will be
2939e71b7053SJung-uk Kim# "Basic string: debug", and when not, it will be "Basic string: release"
2940e71b7053SJung-uk Kim#
2941e71b7053SJung-uk Kim# This can be used to create variants of sets of flags according to the
2942e71b7053SJung-uk Kim# build type:
2943e71b7053SJung-uk Kim#
2944e71b7053SJung-uk Kim#       cflags => picker(default => "-Wall",
2945e71b7053SJung-uk Kim#                        debug   => "-g -O0",
2946e71b7053SJung-uk Kim#                        release => "-O3")
2947e71b7053SJung-uk Kim#
2948e71b7053SJung-uk Kimsub picker {
2949e71b7053SJung-uk Kim    my %opts = @_;
2950e71b7053SJung-uk Kim    return sub { add($opts{default} || (),
2951e71b7053SJung-uk Kim                     $opts{$config{build_type}} || ())->(); }
2952e71b7053SJung-uk Kim}
2953e71b7053SJung-uk Kim
2954e71b7053SJung-uk Kim# Helper function to combine several values of different types into one.
2955e71b7053SJung-uk Kim# This is useful if you want to combine a string with the result of a
2956e71b7053SJung-uk Kim# lazy function, such as:
2957e71b7053SJung-uk Kim#
2958e71b7053SJung-uk Kim#       cflags => combine("-Wall", sub { $disabled{zlib} ? () : "-DZLIB" })
2959e71b7053SJung-uk Kim#
2960e71b7053SJung-uk Kimsub combine {
2961e71b7053SJung-uk Kim    my @stuff = @_;
2962e71b7053SJung-uk Kim    return sub { add(@stuff)->(); }
2963e71b7053SJung-uk Kim}
2964e71b7053SJung-uk Kim
2965e71b7053SJung-uk Kim# Helper function to implement conditional values depending on the value
2966e71b7053SJung-uk Kim# of $disabled{threads}.  Can be used as follows:
2967e71b7053SJung-uk Kim#
2968e71b7053SJung-uk Kim#       cflags => combine("-Wall", threads("-pthread"))
2969e71b7053SJung-uk Kim#
2970e71b7053SJung-uk Kimsub threads {
2971e71b7053SJung-uk Kim    my @flags = @_;
2972e71b7053SJung-uk Kim    return sub { add($disabled{threads} ? () : @flags)->(); }
2973e71b7053SJung-uk Kim}
2974e71b7053SJung-uk Kim
2975e71b7053SJung-uk Kimsub shared {
2976e71b7053SJung-uk Kim    my @flags = @_;
2977e71b7053SJung-uk Kim    return sub { add($disabled{shared} ? () : @flags)->(); }
2978e71b7053SJung-uk Kim}
2979e71b7053SJung-uk Kim
2980e71b7053SJung-uk Kimour $add_called = 0;
2981e71b7053SJung-uk Kim# Helper function to implement adding values to already existing configuration
2982e71b7053SJung-uk Kim# values.  It handles elements that are ARRAYs, CODEs and scalars
2983e71b7053SJung-uk Kimsub _add {
2984e71b7053SJung-uk Kim    my $separator = shift;
2985e71b7053SJung-uk Kim
2986e71b7053SJung-uk Kim    # If there's any ARRAY in the collection of values OR the separator
2987e71b7053SJung-uk Kim    # is undef, we will return an ARRAY of combined values, otherwise a
2988e71b7053SJung-uk Kim    # string of joined values with $separator as the separator.
2989e71b7053SJung-uk Kim    my $found_array = !defined($separator);
2990e71b7053SJung-uk Kim
2991e71b7053SJung-uk Kim    my @values =
2992e71b7053SJung-uk Kim        map {
2993e71b7053SJung-uk Kim            my $res = $_;
2994e71b7053SJung-uk Kim            while (ref($res) eq "CODE") {
2995e71b7053SJung-uk Kim                $res = $res->();
2996e71b7053SJung-uk Kim            }
2997e71b7053SJung-uk Kim            if (defined($res)) {
2998e71b7053SJung-uk Kim                if (ref($res) eq "ARRAY") {
2999e71b7053SJung-uk Kim                    $found_array = 1;
3000e71b7053SJung-uk Kim                    @$res;
3001e71b7053SJung-uk Kim                } else {
3002e71b7053SJung-uk Kim                    $res;
3003e71b7053SJung-uk Kim                }
3004e71b7053SJung-uk Kim            } else {
3005e71b7053SJung-uk Kim                ();
3006e71b7053SJung-uk Kim            }
3007e71b7053SJung-uk Kim    } (@_);
3008e71b7053SJung-uk Kim
3009e71b7053SJung-uk Kim    $add_called = 1;
3010e71b7053SJung-uk Kim
3011e71b7053SJung-uk Kim    if ($found_array) {
3012e71b7053SJung-uk Kim        [ @values ];
3013e71b7053SJung-uk Kim    } else {
3014e71b7053SJung-uk Kim        join($separator, grep { defined($_) && $_ ne "" } @values);
3015e71b7053SJung-uk Kim    }
3016e71b7053SJung-uk Kim}
3017e71b7053SJung-uk Kimsub add_before {
3018e71b7053SJung-uk Kim    my $separator = " ";
3019e71b7053SJung-uk Kim    if (ref($_[$#_]) eq "HASH") {
3020e71b7053SJung-uk Kim        my $opts = pop;
3021e71b7053SJung-uk Kim        $separator = $opts->{separator};
3022e71b7053SJung-uk Kim    }
3023e71b7053SJung-uk Kim    my @x = @_;
3024e71b7053SJung-uk Kim    sub { _add($separator, @x, @_) };
3025e71b7053SJung-uk Kim}
3026e71b7053SJung-uk Kimsub add {
3027e71b7053SJung-uk Kim    my $separator = " ";
3028e71b7053SJung-uk Kim    if (ref($_[$#_]) eq "HASH") {
3029e71b7053SJung-uk Kim        my $opts = pop;
3030e71b7053SJung-uk Kim        $separator = $opts->{separator};
3031e71b7053SJung-uk Kim    }
3032e71b7053SJung-uk Kim    my @x = @_;
3033e71b7053SJung-uk Kim    sub { _add($separator, @_, @x) };
3034e71b7053SJung-uk Kim}
3035e71b7053SJung-uk Kim
3036e71b7053SJung-uk Kimsub read_eval_file {
3037e71b7053SJung-uk Kim    my $fname = shift;
3038e71b7053SJung-uk Kim    my $content;
3039e71b7053SJung-uk Kim    my @result;
3040e71b7053SJung-uk Kim
3041e71b7053SJung-uk Kim    open F, "< $fname" or die "Can't open '$fname': $!\n";
3042e71b7053SJung-uk Kim    {
3043e71b7053SJung-uk Kim        undef local $/;
3044e71b7053SJung-uk Kim        $content = <F>;
3045e71b7053SJung-uk Kim    }
3046e71b7053SJung-uk Kim    close F;
3047e71b7053SJung-uk Kim    {
3048e71b7053SJung-uk Kim        local $@;
3049e71b7053SJung-uk Kim
3050e71b7053SJung-uk Kim        @result = ( eval $content );
3051e71b7053SJung-uk Kim        warn $@ if $@;
3052e71b7053SJung-uk Kim    }
3053e71b7053SJung-uk Kim    return wantarray ? @result : $result[0];
3054e71b7053SJung-uk Kim}
3055e71b7053SJung-uk Kim
3056e71b7053SJung-uk Kim# configuration reader, evaluates the input file as a perl script and expects
3057e71b7053SJung-uk Kim# it to fill %targets with target configurations.  Those are then added to
3058e71b7053SJung-uk Kim# %table.
3059e71b7053SJung-uk Kimsub read_config {
3060e71b7053SJung-uk Kim    my $fname = shift;
3061e71b7053SJung-uk Kim    my %targets;
3062e71b7053SJung-uk Kim
3063e71b7053SJung-uk Kim    {
3064e71b7053SJung-uk Kim        # Protect certain tables from tampering
3065e71b7053SJung-uk Kim        local %table = ();
3066e71b7053SJung-uk Kim
3067e71b7053SJung-uk Kim        %targets = read_eval_file($fname);
3068e71b7053SJung-uk Kim    }
3069e71b7053SJung-uk Kim    my %preexisting = ();
3070e71b7053SJung-uk Kim    foreach (sort keys %targets) {
3071e71b7053SJung-uk Kim        $preexisting{$_} = 1 if $table{$_};
3072e71b7053SJung-uk Kim    }
3073e71b7053SJung-uk Kim    die <<"EOF",
3074e71b7053SJung-uk KimThe following config targets from $fname
3075e71b7053SJung-uk Kimshadow pre-existing config targets with the same name:
3076e71b7053SJung-uk KimEOF
3077e71b7053SJung-uk Kim        map { "  $_\n" } sort keys %preexisting
3078e71b7053SJung-uk Kim        if %preexisting;
3079e71b7053SJung-uk Kim
3080e71b7053SJung-uk Kim
3081e71b7053SJung-uk Kim    # For each target, check that it's configured with a hash table.
3082e71b7053SJung-uk Kim    foreach (keys %targets) {
3083e71b7053SJung-uk Kim        if (ref($targets{$_}) ne "HASH") {
3084e71b7053SJung-uk Kim            if (ref($targets{$_}) eq "") {
3085e71b7053SJung-uk Kim                warn "Deprecated target configuration for $_, ignoring...\n";
3086e71b7053SJung-uk Kim            } else {
3087e71b7053SJung-uk Kim                warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
3088e71b7053SJung-uk Kim            }
3089e71b7053SJung-uk Kim            delete $targets{$_};
3090e71b7053SJung-uk Kim        } else {
3091e71b7053SJung-uk Kim            $targets{$_}->{_conf_fname_int} = add([ $fname ]);
3092e71b7053SJung-uk Kim        }
3093e71b7053SJung-uk Kim    }
3094e71b7053SJung-uk Kim
3095e71b7053SJung-uk Kim    %table = (%table, %targets);
3096e71b7053SJung-uk Kim
3097e71b7053SJung-uk Kim}
3098e71b7053SJung-uk Kim
3099e71b7053SJung-uk Kim# configuration resolver.  Will only resolve all the lazy evaluation
3100e71b7053SJung-uk Kim# codeblocks for the chosen target and all those it inherits from,
3101e71b7053SJung-uk Kim# recursively
3102e71b7053SJung-uk Kimsub resolve_config {
3103e71b7053SJung-uk Kim    my $target = shift;
3104e71b7053SJung-uk Kim    my @breadcrumbs = @_;
3105e71b7053SJung-uk Kim
3106e71b7053SJung-uk Kim#    my $extra_checks = defined($ENV{CONFIGURE_EXTRA_CHECKS});
3107e71b7053SJung-uk Kim
3108e71b7053SJung-uk Kim    if (grep { $_ eq $target } @breadcrumbs) {
3109e71b7053SJung-uk Kim        die "inherit_from loop!  target backtrace:\n  "
3110e71b7053SJung-uk Kim            ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
3111e71b7053SJung-uk Kim    }
3112e71b7053SJung-uk Kim
3113e71b7053SJung-uk Kim    if (!defined($table{$target})) {
3114e71b7053SJung-uk Kim        warn "Warning! target $target doesn't exist!\n";
3115e71b7053SJung-uk Kim        return ();
3116e71b7053SJung-uk Kim    }
3117e71b7053SJung-uk Kim    # Recurse through all inheritances.  They will be resolved on the
3118e71b7053SJung-uk Kim    # fly, so when this operation is done, they will all just be a
3119e71b7053SJung-uk Kim    # bunch of attributes with string values.
3120e71b7053SJung-uk Kim    # What we get here, though, are keys with references to lists of
3121e71b7053SJung-uk Kim    # the combined values of them all.  We will deal with lists after
3122e71b7053SJung-uk Kim    # this stage is done.
3123e71b7053SJung-uk Kim    my %combined_inheritance = ();
3124e71b7053SJung-uk Kim    if ($table{$target}->{inherit_from}) {
3125e71b7053SJung-uk Kim        my @inherit_from =
3126e71b7053SJung-uk Kim            map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
3127e71b7053SJung-uk Kim        foreach (@inherit_from) {
3128e71b7053SJung-uk Kim            my %inherited_config = resolve_config($_, $target, @breadcrumbs);
3129e71b7053SJung-uk Kim
3130e71b7053SJung-uk Kim            # 'template' is a marker that's considered private to
3131e71b7053SJung-uk Kim            # the config that had it.
3132e71b7053SJung-uk Kim            delete $inherited_config{template};
3133e71b7053SJung-uk Kim
3134e71b7053SJung-uk Kim            foreach (keys %inherited_config) {
3135e71b7053SJung-uk Kim                if (!$combined_inheritance{$_}) {
3136e71b7053SJung-uk Kim                    $combined_inheritance{$_} = [];
3137e71b7053SJung-uk Kim                }
3138e71b7053SJung-uk Kim                push @{$combined_inheritance{$_}}, $inherited_config{$_};
3139e71b7053SJung-uk Kim            }
3140e71b7053SJung-uk Kim        }
3141e71b7053SJung-uk Kim    }
3142e71b7053SJung-uk Kim
3143e71b7053SJung-uk Kim    # We won't need inherit_from in this target any more, since we've
3144e71b7053SJung-uk Kim    # resolved all the inheritances that lead to this
3145e71b7053SJung-uk Kim    delete $table{$target}->{inherit_from};
3146e71b7053SJung-uk Kim
3147e71b7053SJung-uk Kim    # Now is the time to deal with those lists.  Here's the place to
3148e71b7053SJung-uk Kim    # decide what shall be done with those lists, all based on the
3149e71b7053SJung-uk Kim    # values of the target we're currently dealing with.
3150e71b7053SJung-uk Kim    # - If a value is a coderef, it will be executed with the list of
3151e71b7053SJung-uk Kim    #   inherited values as arguments.
3152e71b7053SJung-uk Kim    # - If the corresponding key doesn't have a value at all or is the
3153e71b7053SJung-uk Kim    #   empty string, the inherited value list will be run through the
3154e71b7053SJung-uk Kim    #   default combiner (below), and the result becomes this target's
3155e71b7053SJung-uk Kim    #   value.
3156e71b7053SJung-uk Kim    # - Otherwise, this target's value is assumed to be a string that
3157e71b7053SJung-uk Kim    #   will simply override the inherited list of values.
3158e71b7053SJung-uk Kim    my $default_combiner = add();
3159e71b7053SJung-uk Kim
3160e71b7053SJung-uk Kim    my %all_keys =
3161e71b7053SJung-uk Kim        map { $_ => 1 } (keys %combined_inheritance,
3162e71b7053SJung-uk Kim                         keys %{$table{$target}});
3163e71b7053SJung-uk Kim
3164e71b7053SJung-uk Kim    sub process_values {
3165e71b7053SJung-uk Kim        my $object    = shift;
3166e71b7053SJung-uk Kim        my $inherited = shift;  # Always a [ list ]
3167e71b7053SJung-uk Kim        my $target    = shift;
3168e71b7053SJung-uk Kim        my $entry     = shift;
3169e71b7053SJung-uk Kim
3170e71b7053SJung-uk Kim        $add_called = 0;
3171e71b7053SJung-uk Kim
3172e71b7053SJung-uk Kim        while(ref($object) eq "CODE") {
3173e71b7053SJung-uk Kim            $object = $object->(@$inherited);
3174e71b7053SJung-uk Kim        }
3175e71b7053SJung-uk Kim        if (!defined($object)) {
3176e71b7053SJung-uk Kim            return ();
3177e71b7053SJung-uk Kim        }
3178e71b7053SJung-uk Kim        elsif (ref($object) eq "ARRAY") {
3179e71b7053SJung-uk Kim            local $add_called;  # To make sure recursive calls don't affect it
3180e71b7053SJung-uk Kim            return [ map { process_values($_, $inherited, $target, $entry) }
3181e71b7053SJung-uk Kim                     @$object ];
3182e71b7053SJung-uk Kim        } elsif (ref($object) eq "") {
3183e71b7053SJung-uk Kim            return $object;
3184e71b7053SJung-uk Kim        } else {
3185e71b7053SJung-uk Kim            die "cannot handle reference type ",ref($object)
3186e71b7053SJung-uk Kim                ," found in target ",$target," -> ",$entry,"\n";
3187e71b7053SJung-uk Kim        }
3188e71b7053SJung-uk Kim    }
3189e71b7053SJung-uk Kim
3190e71b7053SJung-uk Kim    foreach (sort keys %all_keys) {
3191e71b7053SJung-uk Kim        my $previous = $combined_inheritance{$_};
3192e71b7053SJung-uk Kim
3193e71b7053SJung-uk Kim        # Current target doesn't have a value for the current key?
3194e71b7053SJung-uk Kim        # Assign it the default combiner, the rest of this loop body
3195e71b7053SJung-uk Kim        # will handle it just like any other coderef.
3196e71b7053SJung-uk Kim        if (!exists $table{$target}->{$_}) {
3197e71b7053SJung-uk Kim            $table{$target}->{$_} = $default_combiner;
3198e71b7053SJung-uk Kim        }
3199e71b7053SJung-uk Kim
3200e71b7053SJung-uk Kim        $table{$target}->{$_} = process_values($table{$target}->{$_},
3201e71b7053SJung-uk Kim                                               $combined_inheritance{$_},
3202e71b7053SJung-uk Kim                                               $target, $_);
3203e71b7053SJung-uk Kim        unless(defined($table{$target}->{$_})) {
3204e71b7053SJung-uk Kim            delete $table{$target}->{$_};
3205e71b7053SJung-uk Kim        }
3206e71b7053SJung-uk Kim#        if ($extra_checks &&
3207e71b7053SJung-uk Kim#            $previous && !($add_called ||  $previous ~~ $table{$target}->{$_})) {
3208e71b7053SJung-uk Kim#            warn "$_ got replaced in $target\n";
3209e71b7053SJung-uk Kim#        }
3210e71b7053SJung-uk Kim    }
3211e71b7053SJung-uk Kim
3212e71b7053SJung-uk Kim    # Finally done, return the result.
3213e71b7053SJung-uk Kim    return %{$table{$target}};
3214e71b7053SJung-uk Kim}
3215e71b7053SJung-uk Kim
321674664626SKris Kennawaysub usage
321774664626SKris Kennaway        {
321874664626SKris Kennaway        print STDERR $usage;
3219f579bf8eSKris Kennaway        print STDERR "\npick os/compiler from:\n";
322074664626SKris Kennaway        my $j=0;
322174664626SKris Kennaway        my $i;
3222f579bf8eSKris Kennaway        my $k=0;
322374664626SKris Kennaway        foreach $i (sort keys %table)
322474664626SKris Kennaway                {
3225e71b7053SJung-uk Kim                next if $table{$i}->{template};
322674664626SKris Kennaway                next if $i =~ /^debug/;
3227f579bf8eSKris Kennaway                $k += length($i) + 1;
3228f579bf8eSKris Kennaway                if ($k > 78)
3229f579bf8eSKris Kennaway                        {
3230f579bf8eSKris Kennaway                        print STDERR "\n";
3231f579bf8eSKris Kennaway                        $k=length($i);
3232f579bf8eSKris Kennaway                        }
3233f579bf8eSKris Kennaway                print STDERR $i . " ";
323474664626SKris Kennaway                }
323574664626SKris Kennaway        foreach $i (sort keys %table)
323674664626SKris Kennaway                {
3237e71b7053SJung-uk Kim                next if $table{$i}->{template};
323874664626SKris Kennaway                next if $i !~ /^debug/;
3239f579bf8eSKris Kennaway                $k += length($i) + 1;
3240f579bf8eSKris Kennaway                if ($k > 78)
3241f579bf8eSKris Kennaway                        {
324274664626SKris Kennaway                        print STDERR "\n";
3243f579bf8eSKris Kennaway                        $k=length($i);
3244f579bf8eSKris Kennaway                        }
3245f579bf8eSKris Kennaway                print STDERR $i . " ";
3246f579bf8eSKris Kennaway                }
3247f579bf8eSKris Kennaway        print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n";
324874664626SKris Kennaway        exit(1);
324974664626SKris Kennaway        }
325074664626SKris Kennaway
3251e71b7053SJung-uk Kimsub run_dofile
3252e71b7053SJung-uk Kim{
3253e71b7053SJung-uk Kim    my $out = shift;
3254e71b7053SJung-uk Kim    my @templates = @_;
3255e71b7053SJung-uk Kim
3256e71b7053SJung-uk Kim    unlink $out || warn "Can't remove $out, $!"
3257e71b7053SJung-uk Kim        if -f $out;
3258e71b7053SJung-uk Kim    foreach (@templates) {
3259e71b7053SJung-uk Kim        die "Can't open $_, $!" unless -f $_;
3260e71b7053SJung-uk Kim    }
3261e71b7053SJung-uk Kim    my $perlcmd = (quotify("maybeshell", $config{PERL}))[0];
3262e71b7053SJung-uk Kim    my $cmd = "$perlcmd \"-I.\" \"-Mconfigdata\" \"$dofile\" -o\"Configure\" \"".join("\" \"",@templates)."\" > \"$out.new\"";
3263e71b7053SJung-uk Kim    #print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
3264e71b7053SJung-uk Kim    system($cmd);
3265e71b7053SJung-uk Kim    exit 1 if $? != 0;
3266e71b7053SJung-uk Kim    rename("$out.new", $out) || die "Can't rename $out.new, $!";
3267e71b7053SJung-uk Kim}
3268e71b7053SJung-uk Kim
3269e71b7053SJung-uk Kimsub compiler_predefined {
3270e71b7053SJung-uk Kim    state %predefined;
3271e71b7053SJung-uk Kim    my $cc = shift;
3272e71b7053SJung-uk Kim
3273e71b7053SJung-uk Kim    return () if $^O eq 'VMS';
3274e71b7053SJung-uk Kim
3275e71b7053SJung-uk Kim    die 'compiler_predefined called without a compiler command'
3276e71b7053SJung-uk Kim        unless $cc;
3277e71b7053SJung-uk Kim
3278e71b7053SJung-uk Kim    if (! $predefined{$cc}) {
3279e71b7053SJung-uk Kim
3280e71b7053SJung-uk Kim        $predefined{$cc} = {};
3281e71b7053SJung-uk Kim
3282e71b7053SJung-uk Kim        # collect compiler pre-defines from gcc or gcc-alike...
3283e71b7053SJung-uk Kim        open(PIPE, "$cc -dM -E -x c /dev/null 2>&1 |");
3284e71b7053SJung-uk Kim        while (my $l = <PIPE>) {
3285e71b7053SJung-uk Kim            $l =~ m/^#define\s+(\w+(?:\(\w+\))?)(?:\s+(.+))?/ or last;
3286e71b7053SJung-uk Kim            $predefined{$cc}->{$1} = $2 // '';
3287e71b7053SJung-uk Kim        }
3288e71b7053SJung-uk Kim        close(PIPE);
3289e71b7053SJung-uk Kim    }
3290e71b7053SJung-uk Kim
3291e71b7053SJung-uk Kim    return %{$predefined{$cc}};
3292e71b7053SJung-uk Kim}
3293e71b7053SJung-uk Kim
329474664626SKris Kennawaysub which
329574664626SKris Kennaway{
329674664626SKris Kennaway    my ($name)=@_;
3297e71b7053SJung-uk Kim
3298e71b7053SJung-uk Kim    if (eval { require IPC::Cmd; 1; }) {
3299e71b7053SJung-uk Kim        IPC::Cmd->import();
3300e71b7053SJung-uk Kim        return scalar IPC::Cmd::can_run($name);
3301e71b7053SJung-uk Kim    } else {
3302e71b7053SJung-uk Kim        # if there is $directories component in splitpath,
3303e71b7053SJung-uk Kim        # then it's not something to test with $PATH...
3304e71b7053SJung-uk Kim        return $name if (File::Spec->splitpath($name))[1];
3305e71b7053SJung-uk Kim
3306e71b7053SJung-uk Kim        foreach (File::Spec->path()) {
3307e71b7053SJung-uk Kim            my $fullpath = catfile($_, "$name$target{exe_extension}");
3308e71b7053SJung-uk Kim            if (-f $fullpath and -x $fullpath) {
3309e71b7053SJung-uk Kim                return $fullpath;
3310e71b7053SJung-uk Kim            }
331174664626SKris Kennaway        }
331274664626SKris Kennaway    }
331374664626SKris Kennaway}
331474664626SKris Kennaway
3315e71b7053SJung-uk Kimsub env
331674664626SKris Kennaway{
3317e71b7053SJung-uk Kim    my $name = shift;
3318e71b7053SJung-uk Kim    my %opts = @_;
331974664626SKris Kennaway
3320e71b7053SJung-uk Kim    unless ($opts{cacheonly}) {
3321e71b7053SJung-uk Kim        # Note that if $ENV{$name} doesn't exist or is undefined,
3322e71b7053SJung-uk Kim        # $config{perlenv}->{$name} will be created with the value
3323e71b7053SJung-uk Kim        # undef.  This is intentional.
3324e71b7053SJung-uk Kim
3325e71b7053SJung-uk Kim        $config{perlenv}->{$name} = $ENV{$name}
3326e71b7053SJung-uk Kim            if ! exists $config{perlenv}->{$name};
332774664626SKris Kennaway    }
3328e71b7053SJung-uk Kim    return $config{perlenv}->{$name};
332974664626SKris Kennaway}
333074664626SKris Kennaway
3331e71b7053SJung-uk Kim# Configuration printer ##############################################
3332e71b7053SJung-uk Kim
333374664626SKris Kennawaysub print_table_entry
333474664626SKris Kennaway{
3335e71b7053SJung-uk Kim    local $now_printing = shift;
3336e71b7053SJung-uk Kim    my %target = resolve_config($now_printing);
3337e71b7053SJung-uk Kim    my $type = shift;
333874664626SKris Kennaway
3339e71b7053SJung-uk Kim    # Don't print the templates
3340e71b7053SJung-uk Kim    return if $target{template};
334174664626SKris Kennaway
3342e71b7053SJung-uk Kim    my @sequence = (
3343e71b7053SJung-uk Kim        "sys_id",
3344e71b7053SJung-uk Kim        "cpp",
3345e71b7053SJung-uk Kim        "cppflags",
3346e71b7053SJung-uk Kim        "defines",
3347e71b7053SJung-uk Kim        "includes",
3348e71b7053SJung-uk Kim        "cc",
3349e71b7053SJung-uk Kim        "cflags",
3350e71b7053SJung-uk Kim        "unistd",
3351e71b7053SJung-uk Kim        "ld",
3352e71b7053SJung-uk Kim        "lflags",
3353e71b7053SJung-uk Kim        "loutflag",
3354e71b7053SJung-uk Kim        "ex_libs",
3355e71b7053SJung-uk Kim        "bn_ops",
3356e71b7053SJung-uk Kim        "apps_aux_src",
3357e71b7053SJung-uk Kim        "cpuid_asm_src",
3358e71b7053SJung-uk Kim        "uplink_aux_src",
3359e71b7053SJung-uk Kim        "bn_asm_src",
3360e71b7053SJung-uk Kim        "ec_asm_src",
3361e71b7053SJung-uk Kim        "des_asm_src",
3362e71b7053SJung-uk Kim        "aes_asm_src",
3363e71b7053SJung-uk Kim        "bf_asm_src",
3364e71b7053SJung-uk Kim        "md5_asm_src",
3365e71b7053SJung-uk Kim        "cast_asm_src",
3366e71b7053SJung-uk Kim        "sha1_asm_src",
3367e71b7053SJung-uk Kim        "rc4_asm_src",
3368e71b7053SJung-uk Kim        "rmd160_asm_src",
3369e71b7053SJung-uk Kim        "rc5_asm_src",
3370e71b7053SJung-uk Kim        "wp_asm_src",
3371e71b7053SJung-uk Kim        "cmll_asm_src",
3372e71b7053SJung-uk Kim        "modes_asm_src",
3373e71b7053SJung-uk Kim        "padlock_asm_src",
3374e71b7053SJung-uk Kim        "chacha_asm_src",
3375e71b7053SJung-uk Kim        "poly1035_asm_src",
3376e71b7053SJung-uk Kim        "thread_scheme",
3377e71b7053SJung-uk Kim        "perlasm_scheme",
3378e71b7053SJung-uk Kim        "dso_scheme",
3379e71b7053SJung-uk Kim        "shared_target",
3380e71b7053SJung-uk Kim        "shared_cflag",
3381e71b7053SJung-uk Kim        "shared_defines",
3382e71b7053SJung-uk Kim        "shared_ldflag",
3383e71b7053SJung-uk Kim        "shared_rcflag",
3384e71b7053SJung-uk Kim        "shared_extension",
3385e71b7053SJung-uk Kim        "dso_extension",
3386e71b7053SJung-uk Kim        "obj_extension",
3387e71b7053SJung-uk Kim        "exe_extension",
3388e71b7053SJung-uk Kim        "ranlib",
3389e71b7053SJung-uk Kim        "ar",
3390e71b7053SJung-uk Kim        "arflags",
3391e71b7053SJung-uk Kim        "aroutflag",
3392e71b7053SJung-uk Kim        "rc",
3393e71b7053SJung-uk Kim        "rcflags",
3394e71b7053SJung-uk Kim        "rcoutflag",
3395e71b7053SJung-uk Kim        "mt",
3396e71b7053SJung-uk Kim        "mtflags",
3397e71b7053SJung-uk Kim        "mtinflag",
3398e71b7053SJung-uk Kim        "mtoutflag",
3399e71b7053SJung-uk Kim        "multilib",
3400e71b7053SJung-uk Kim        "build_scheme",
3401e71b7053SJung-uk Kim        );
340274664626SKris Kennaway
3403e71b7053SJung-uk Kim    if ($type eq "TABLE") {
3404e71b7053SJung-uk Kim        print "\n";
3405e71b7053SJung-uk Kim        print "*** $now_printing\n";
3406e71b7053SJung-uk Kim        foreach (@sequence) {
3407e71b7053SJung-uk Kim            if (ref($target{$_}) eq "ARRAY") {
3408e71b7053SJung-uk Kim                printf "\$%-12s = %s\n", $_, join(" ", @{$target{$_}});
3409e71b7053SJung-uk Kim            } else {
3410e71b7053SJung-uk Kim                printf "\$%-12s = %s\n", $_, $target{$_};
3411c1803d78SJacques Vidrine            }
3412c1803d78SJacques Vidrine        }
3413e71b7053SJung-uk Kim    } elsif ($type eq "HASH") {
3414e71b7053SJung-uk Kim        my $largest =
3415e71b7053SJung-uk Kim            length((sort { length($a) <=> length($b) } @sequence)[-1]);
3416e71b7053SJung-uk Kim        print "    '$now_printing' => {\n";
3417e71b7053SJung-uk Kim        foreach (@sequence) {
3418e71b7053SJung-uk Kim            if ($target{$_}) {
3419e71b7053SJung-uk Kim                if (ref($target{$_}) eq "ARRAY") {
3420e71b7053SJung-uk Kim                    print "      '",$_,"'"," " x ($largest - length($_))," => [ ",join(", ", map { "'$_'" } @{$target{$_}})," ],\n";
3421e71b7053SJung-uk Kim                } else {
3422e71b7053SJung-uk Kim                    print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
3423e71b7053SJung-uk Kim                }
3424e71b7053SJung-uk Kim            }
3425e71b7053SJung-uk Kim        }
3426e71b7053SJung-uk Kim        print "    },\n";
3427e71b7053SJung-uk Kim    }
3428c1803d78SJacques Vidrine}
34296cf8931aSJung-uk Kim
3430e71b7053SJung-uk Kim# Utility routines ###################################################
3431e71b7053SJung-uk Kim
3432e71b7053SJung-uk Kim# On VMS, if the given file is a logical name, File::Spec::Functions
3433e71b7053SJung-uk Kim# will consider it an absolute path.  There are cases when we want a
3434e71b7053SJung-uk Kim# purely syntactic check without checking the environment.
3435e71b7053SJung-uk Kimsub isabsolute {
3436e71b7053SJung-uk Kim    my $file = shift;
3437e71b7053SJung-uk Kim
3438e71b7053SJung-uk Kim    # On non-platforms, we just use file_name_is_absolute().
3439e71b7053SJung-uk Kim    return file_name_is_absolute($file) unless $^O eq "VMS";
3440e71b7053SJung-uk Kim
3441e71b7053SJung-uk Kim    # If the file spec includes a device or a directory spec,
3442e71b7053SJung-uk Kim    # file_name_is_absolute() is perfectly safe.
3443e71b7053SJung-uk Kim    return file_name_is_absolute($file) if $file =~ m|[:\[]|;
3444e71b7053SJung-uk Kim
3445e71b7053SJung-uk Kim    # Here, we know the given file spec isn't absolute
3446e71b7053SJung-uk Kim    return 0;
3447e71b7053SJung-uk Kim}
3448e71b7053SJung-uk Kim
3449e71b7053SJung-uk Kim# Makes a directory absolute and cleans out /../ in paths like foo/../bar
3450e71b7053SJung-uk Kim# On some platforms, this uses rel2abs(), while on others, realpath() is used.
3451e71b7053SJung-uk Kim# realpath() requires that at least all path components except the last is an
3452e71b7053SJung-uk Kim# existing directory.  On VMS, the last component of the directory spec must
3453e71b7053SJung-uk Kim# exist.
3454e71b7053SJung-uk Kimsub absolutedir {
3455e71b7053SJung-uk Kim    my $dir = shift;
3456e71b7053SJung-uk Kim
3457e71b7053SJung-uk Kim    # realpath() is quite buggy on VMS.  It uses LIB$FID_TO_NAME, which
3458e71b7053SJung-uk Kim    # will return the volume name for the device, no matter what.  Also,
3459e71b7053SJung-uk Kim    # it will return an incorrect directory spec if the argument is a
3460e71b7053SJung-uk Kim    # directory that doesn't exist.
3461e71b7053SJung-uk Kim    if ($^O eq "VMS") {
3462e71b7053SJung-uk Kim        return rel2abs($dir);
3463e71b7053SJung-uk Kim    }
3464e71b7053SJung-uk Kim
3465e71b7053SJung-uk Kim    # We use realpath() on Unix, since no other will properly clean out
3466e71b7053SJung-uk Kim    # a directory spec.
3467e71b7053SJung-uk Kim    use Cwd qw/realpath/;
3468e71b7053SJung-uk Kim
3469e71b7053SJung-uk Kim    return realpath($dir);
3470e71b7053SJung-uk Kim}
3471e71b7053SJung-uk Kim
347258f35182SJung-uk Kim# Check if all paths are one and the same, using stat.  They must both exist
347358f35182SJung-uk Kim# We need this for the cases when File::Spec doesn't detect case insensitivity
347458f35182SJung-uk Kim# (File::Spec::Unix assumes case sensitivity)
347558f35182SJung-uk Kimsub samedir {
347658f35182SJung-uk Kim    die "samedir expects two arguments\n" unless scalar @_ == 2;
347758f35182SJung-uk Kim
347858f35182SJung-uk Kim    my @stat0 = stat($_[0]);    # First argument
347958f35182SJung-uk Kim    my @stat1 = stat($_[1]);    # Second argument
348058f35182SJung-uk Kim
348158f35182SJung-uk Kim    die "Couldn't stat $_[0]" unless @stat0;
348258f35182SJung-uk Kim    die "Couldn't stat $_[1]" unless @stat1;
348358f35182SJung-uk Kim
348458f35182SJung-uk Kim    # Compare device number
348558f35182SJung-uk Kim    return 0 unless ($stat0[0] == $stat1[0]);
348658f35182SJung-uk Kim    # Compare "inode".  The perl manual recommends comparing as
348758f35182SJung-uk Kim    # string rather than as number.
348858f35182SJung-uk Kim    return 0 unless ($stat0[1] eq $stat1[1]);
348958f35182SJung-uk Kim
349058f35182SJung-uk Kim    return 1;                   # All the same
349158f35182SJung-uk Kim}
349258f35182SJung-uk Kim
3493e71b7053SJung-uk Kimsub quotify {
3494e71b7053SJung-uk Kim    my %processors = (
3495e71b7053SJung-uk Kim        perl    => sub { my $x = shift;
3496e71b7053SJung-uk Kim                         $x =~ s/([\\\$\@"])/\\$1/g;
3497e71b7053SJung-uk Kim                         return '"'.$x.'"'; },
3498e71b7053SJung-uk Kim        maybeshell => sub { my $x = shift;
3499e71b7053SJung-uk Kim                            (my $y = $x) =~ s/([\\\"])/\\$1/g;
3500e71b7053SJung-uk Kim                            if ($x ne $y || $x =~ m|\s|) {
3501e71b7053SJung-uk Kim                                return '"'.$y.'"';
3502e71b7053SJung-uk Kim                            } else {
3503e71b7053SJung-uk Kim                                return $x;
3504e71b7053SJung-uk Kim                            }
3505e71b7053SJung-uk Kim                        },
3506e71b7053SJung-uk Kim        );
3507e71b7053SJung-uk Kim    my $for = shift;
3508e71b7053SJung-uk Kim    my $processor =
3509e71b7053SJung-uk Kim        defined($processors{$for}) ? $processors{$for} : sub { shift; };
3510e71b7053SJung-uk Kim
3511e71b7053SJung-uk Kim    return map { $processor->($_); } @_;
3512e71b7053SJung-uk Kim}
3513e71b7053SJung-uk Kim
3514e71b7053SJung-uk Kim# collect_from_file($filename, $line_concat_cond_re, $line_concat)
3515e71b7053SJung-uk Kim# $filename is a file name to read from
3516e71b7053SJung-uk Kim# $line_concat_cond_re is a regexp detecting a line continuation ending
3517e71b7053SJung-uk Kim# $line_concat is a CODEref that takes care of concatenating two lines
3518e71b7053SJung-uk Kimsub collect_from_file {
3519e71b7053SJung-uk Kim    my $filename = shift;
3520e71b7053SJung-uk Kim    my $line_concat_cond_re = shift;
3521e71b7053SJung-uk Kim    my $line_concat = shift;
3522e71b7053SJung-uk Kim
3523e71b7053SJung-uk Kim    open my $fh, $filename || die "unable to read $filename: $!\n";
3524e71b7053SJung-uk Kim    return sub {
3525e71b7053SJung-uk Kim        my $saved_line = "";
3526e71b7053SJung-uk Kim        $_ = "";
3527e71b7053SJung-uk Kim        while (<$fh>) {
3528e71b7053SJung-uk Kim            s|\R$||;
3529e71b7053SJung-uk Kim            if (defined $line_concat) {
3530e71b7053SJung-uk Kim                $_ = $line_concat->($saved_line, $_);
3531e71b7053SJung-uk Kim                $saved_line = "";
3532e71b7053SJung-uk Kim            }
3533e71b7053SJung-uk Kim            if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
3534e71b7053SJung-uk Kim                $saved_line = $_;
3535e71b7053SJung-uk Kim                next;
3536e71b7053SJung-uk Kim            }
3537e71b7053SJung-uk Kim            return $_;
3538e71b7053SJung-uk Kim        }
3539e71b7053SJung-uk Kim        die "$filename ending with continuation line\n" if $_;
3540e71b7053SJung-uk Kim        close $fh;
3541e71b7053SJung-uk Kim        return undef;
3542e71b7053SJung-uk Kim    }
3543e71b7053SJung-uk Kim}
3544e71b7053SJung-uk Kim
3545e71b7053SJung-uk Kim# collect_from_array($array, $line_concat_cond_re, $line_concat)
3546e71b7053SJung-uk Kim# $array is an ARRAYref of lines
3547e71b7053SJung-uk Kim# $line_concat_cond_re is a regexp detecting a line continuation ending
3548e71b7053SJung-uk Kim# $line_concat is a CODEref that takes care of concatenating two lines
3549e71b7053SJung-uk Kimsub collect_from_array {
3550e71b7053SJung-uk Kim    my $array = shift;
3551e71b7053SJung-uk Kim    my $line_concat_cond_re = shift;
3552e71b7053SJung-uk Kim    my $line_concat = shift;
3553e71b7053SJung-uk Kim    my @array = (@$array);
3554e71b7053SJung-uk Kim
3555e71b7053SJung-uk Kim    return sub {
3556e71b7053SJung-uk Kim        my $saved_line = "";
3557e71b7053SJung-uk Kim        $_ = "";
3558e71b7053SJung-uk Kim        while (defined($_ = shift @array)) {
3559e71b7053SJung-uk Kim            s|\R$||;
3560e71b7053SJung-uk Kim            if (defined $line_concat) {
3561e71b7053SJung-uk Kim                $_ = $line_concat->($saved_line, $_);
3562e71b7053SJung-uk Kim                $saved_line = "";
3563e71b7053SJung-uk Kim            }
3564e71b7053SJung-uk Kim            if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
3565e71b7053SJung-uk Kim                $saved_line = $_;
3566e71b7053SJung-uk Kim                next;
3567e71b7053SJung-uk Kim            }
3568e71b7053SJung-uk Kim            return $_;
3569e71b7053SJung-uk Kim        }
3570e71b7053SJung-uk Kim        die "input text ending with continuation line\n" if $_;
3571e71b7053SJung-uk Kim        return undef;
3572e71b7053SJung-uk Kim    }
3573e71b7053SJung-uk Kim}
3574e71b7053SJung-uk Kim
3575e71b7053SJung-uk Kim# collect_information($lineiterator, $line_continue, $regexp => $CODEref, ...)
3576e71b7053SJung-uk Kim# $lineiterator is a CODEref that delivers one line at a time.
3577e71b7053SJung-uk Kim# All following arguments are regex/CODEref pairs, where the regexp detects a
3578e71b7053SJung-uk Kim# line and the CODEref does something with the result of the regexp.
3579e71b7053SJung-uk Kimsub collect_information {
3580e71b7053SJung-uk Kim    my $lineiterator = shift;
3581e71b7053SJung-uk Kim    my %collectors = @_;
3582e71b7053SJung-uk Kim
3583e71b7053SJung-uk Kim    while(defined($_ = $lineiterator->())) {
3584e71b7053SJung-uk Kim        s|\R$||;
3585e71b7053SJung-uk Kim        my $found = 0;
3586e71b7053SJung-uk Kim        if ($collectors{"BEFORE"}) {
3587e71b7053SJung-uk Kim            $collectors{"BEFORE"}->($_);
3588e71b7053SJung-uk Kim        }
3589e71b7053SJung-uk Kim        foreach my $re (keys %collectors) {
3590e71b7053SJung-uk Kim            if ($re !~ /^OTHERWISE|BEFORE|AFTER$/ && /$re/) {
3591e71b7053SJung-uk Kim                $collectors{$re}->($lineiterator);
3592e71b7053SJung-uk Kim                $found = 1;
3593e71b7053SJung-uk Kim            };
3594e71b7053SJung-uk Kim        }
3595e71b7053SJung-uk Kim        if ($collectors{"OTHERWISE"}) {
3596e71b7053SJung-uk Kim            $collectors{"OTHERWISE"}->($lineiterator, $_)
3597e71b7053SJung-uk Kim                unless $found || !defined $collectors{"OTHERWISE"};
3598e71b7053SJung-uk Kim        }
3599e71b7053SJung-uk Kim        if ($collectors{"AFTER"}) {
3600e71b7053SJung-uk Kim            $collectors{"AFTER"}->($_);
3601e71b7053SJung-uk Kim        }
3602e71b7053SJung-uk Kim    }
3603e71b7053SJung-uk Kim}
3604e71b7053SJung-uk Kim
3605e71b7053SJung-uk Kim# tokenize($line)
3606e71b7053SJung-uk Kim# $line is a line of text to split up into tokens
3607e71b7053SJung-uk Kim# returns a list of tokens
3608e71b7053SJung-uk Kim#
3609e71b7053SJung-uk Kim# Tokens are divided by spaces.  If the tokens include spaces, they
3610e71b7053SJung-uk Kim# have to be quoted with single or double quotes.  Double quotes
3611e71b7053SJung-uk Kim# inside a double quoted token must be escaped.  Escaping is done
3612e71b7053SJung-uk Kim# with backslash.
3613e71b7053SJung-uk Kim# Basically, the same quoting rules apply for " and ' as in any
3614e71b7053SJung-uk Kim# Unix shell.
3615e71b7053SJung-uk Kimsub tokenize {
3616e71b7053SJung-uk Kim    my $line = my $debug_line = shift;
3617e71b7053SJung-uk Kim    my @result = ();
3618e71b7053SJung-uk Kim
3619e71b7053SJung-uk Kim    while ($line =~ s|^\s+||, $line ne "") {
3620e71b7053SJung-uk Kim        my $token = "";
3621e71b7053SJung-uk Kim        while ($line ne "" && $line !~ m|^\s|) {
3622e71b7053SJung-uk Kim            if ($line =~ m/^"((?:[^"\\]+|\\.)*)"/) {
3623e71b7053SJung-uk Kim                $token .= $1;
3624e71b7053SJung-uk Kim                $line = $';
3625e71b7053SJung-uk Kim            } elsif ($line =~ m/^'([^']*)'/) {
3626e71b7053SJung-uk Kim                $token .= $1;
3627e71b7053SJung-uk Kim                $line = $';
3628e71b7053SJung-uk Kim            } elsif ($line =~ m/^(\S+)/) {
3629e71b7053SJung-uk Kim                $token .= $1;
3630e71b7053SJung-uk Kim                $line = $';
3631e71b7053SJung-uk Kim            }
3632e71b7053SJung-uk Kim        }
3633e71b7053SJung-uk Kim        push @result, $token;
3634e71b7053SJung-uk Kim    }
3635e71b7053SJung-uk Kim
3636e71b7053SJung-uk Kim    if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
3637e71b7053SJung-uk Kim        print STDERR "DEBUG[tokenize]: Parsed '$debug_line' into:\n";
3638e71b7053SJung-uk Kim        print STDERR "DEBUG[tokenize]: ('", join("', '", @result), "')\n";
3639e71b7053SJung-uk Kim    }
3640e71b7053SJung-uk Kim    return @result;
36416cf8931aSJung-uk Kim}
3642