xref: /freebsd/crypto/openssl/Configure (revision 610a21fd82f731685fabbf6da5387e981b07fbbd)
1e71b7053SJung-uk Kim#! /usr/bin/env perl
2e71b7053SJung-uk Kim# -*- mode: perl; -*-
36935a639SJung-uk Kim# Copyright 2016-2019 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
27*610a21fdSJung-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, ...)
7274664626SKris Kennaway# -<xxx> +<xxx> compiler options are passed through
73e71b7053SJung-uk Kim# -static       while -static is also a pass-through compiler option (and
74e71b7053SJung-uk Kim#               as such is limited to environments where it's actually
75e71b7053SJung-uk Kim#               meaningful), it triggers a number configuration options,
76*610a21fdSJung-uk Kim#               namely no-pic, no-shared and no-threads. It is
77e71b7053SJung-uk Kim#               argued that the only reason to produce statically linked
78e71b7053SJung-uk Kim#               binaries (and in context it means executables linked with
79e71b7053SJung-uk Kim#               -static flag, and not just executables linked with static
80e71b7053SJung-uk Kim#               libcrypto.a) is to eliminate dependency on specific run-time,
81e71b7053SJung-uk Kim#               a.k.a. libc version. The mentioned config options are meant
82e71b7053SJung-uk Kim#               to achieve just that. Unfortunately on Linux it's impossible
83e71b7053SJung-uk Kim#               to eliminate the dependency completely for openssl executable
84e71b7053SJung-uk Kim#               because of getaddrinfo and gethostbyname calls, which can
85e71b7053SJung-uk Kim#               invoke dynamically loadable library facility anyway to meet
86e71b7053SJung-uk Kim#               the lookup requests. For this reason on Linux statically
87e71b7053SJung-uk Kim#               linked openssl executable has rather debugging value than
88e71b7053SJung-uk Kim#               production quality.
8974664626SKris Kennaway#
90ddd58736SKris Kennaway# DEBUG_SAFESTACK use type-safe stacks to enforce type-safety on stack items
91ddd58736SKris Kennaway#               provided to stack calls. Generates unique stack functions for
92ddd58736SKris Kennaway#               each possible stack type.
9374664626SKris Kennaway# BN_LLONG      use the type 'long long' in crypto/bn/bn.h
9474664626SKris Kennaway# RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
953b4e3dcbSSimon L. B. Nielsen# Following are set automatically by this script
963b4e3dcbSSimon L. B. Nielsen#
97e71b7053SJung-uk Kim# MD5_ASM       use some extra md5 assembler,
98e71b7053SJung-uk Kim# SHA1_ASM      use some extra sha1 assembler, must define L_ENDIAN for x86
99e71b7053SJung-uk Kim# RMD160_ASM    use some extra ripemd160 assembler,
1003b4e3dcbSSimon L. B. Nielsen# SHA256_ASM    sha256_block is implemented in assembler
1013b4e3dcbSSimon L. B. Nielsen# SHA512_ASM    sha512_block is implemented in assembler
102e71b7053SJung-uk Kim# AES_ASM       AES_[en|de]crypt is implemented in assembler
10374664626SKris Kennaway
104*610a21fdSJung-uk Kim# Minimum warning options... any contributions to OpenSSL should at least
105*610a21fdSJung-uk Kim# get past these.  Note that we only use these with C compilers, not with
106*610a21fdSJung-uk Kim# C++ compilers.
107db522d3aSSimon L. B. Nielsen
108e71b7053SJung-uk Kim# DEBUG_UNUSED enables __owur (warn unused result) checks.
109e71b7053SJung-uk Kim# -DPEDANTIC complements -pedantic and is meant to mask code that
110e71b7053SJung-uk Kim# is not strictly standard-compliant and/or implementation-specific,
111e71b7053SJung-uk Kim# e.g. inline assembly, disregards to alignment requirements, such
112e71b7053SJung-uk Kim# that -pedantic would complain about. Incidentally -DPEDANTIC has
113e71b7053SJung-uk Kim# to be used even in sanitized builds, because sanitizer too is
114e71b7053SJung-uk Kim# supposed to and does take notice of non-standard behaviour. Then
115e71b7053SJung-uk Kim# -pedantic with pre-C9x compiler would also complain about 'long
116e71b7053SJung-uk Kim# long' not being supported. As 64-bit algorithms are common now,
117e71b7053SJung-uk Kim# it grew impossible to resolve this without sizeable additional
118e71b7053SJung-uk Kim# code, so we just tell compiler to be pedantic about everything
119e71b7053SJung-uk Kim# but 'long long' type.
120db522d3aSSimon L. B. Nielsen
121*610a21fdSJung-uk Kimmy @gcc_devteam_warn = qw(
122*610a21fdSJung-uk Kim    -DDEBUG_UNUSED
123*610a21fdSJung-uk Kim    -DPEDANTIC -pedantic -Wno-long-long
124*610a21fdSJung-uk Kim    -Wall
125*610a21fdSJung-uk Kim    -Wextra
126*610a21fdSJung-uk Kim    -Wno-unused-parameter
127*610a21fdSJung-uk Kim    -Wno-missing-field-initializers
128*610a21fdSJung-uk Kim    -Wswitch
129*610a21fdSJung-uk Kim    -Wsign-compare
130*610a21fdSJung-uk Kim    -Wshadow
131*610a21fdSJung-uk Kim    -Wformat
132*610a21fdSJung-uk Kim    -Wtype-limits
133*610a21fdSJung-uk Kim    -Wundef
134*610a21fdSJung-uk Kim    -Werror
135*610a21fdSJung-uk Kim    -Wmissing-prototypes
136*610a21fdSJung-uk Kim    -Wstrict-prototypes
137*610a21fdSJung-uk Kim);
1387bded2dbSJung-uk Kim
1397bded2dbSJung-uk Kim# These are used in addition to $gcc_devteam_warn when the compiler is clang.
1407bded2dbSJung-uk Kim# TODO(openssl-team): fix problems and investigate if (at least) the
141e71b7053SJung-uk Kim# following warnings can also be enabled:
142e71b7053SJung-uk Kim#       -Wcast-align
143e71b7053SJung-uk Kim#       -Wunreachable-code -- no, too ugly/compiler-specific
144e71b7053SJung-uk Kim#       -Wlanguage-extension-token -- no, we use asm()
145e71b7053SJung-uk Kim#       -Wunused-macros -- no, too tricky for BN and _XOPEN_SOURCE etc
146e71b7053SJung-uk Kim#       -Wextended-offsetof -- no, needed in CMS ASN1 code
147*610a21fdSJung-uk Kimmy @clang_devteam_warn = qw(
148*610a21fdSJung-uk Kim    -Wswitch-default
149*610a21fdSJung-uk Kim    -Wno-parentheses-equality
150*610a21fdSJung-uk Kim    -Wno-language-extension-token
151*610a21fdSJung-uk Kim    -Wno-extended-offsetof
152*610a21fdSJung-uk Kim    -Wconditional-uninitialized
153*610a21fdSJung-uk Kim    -Wincompatible-pointer-types-discards-qualifiers
154*610a21fdSJung-uk Kim    -Wno-unknown-warning-option
155*610a21fdSJung-uk Kim    -Wmissing-variable-declarations
156*610a21fdSJung-uk Kim);
1577bded2dbSJung-uk Kim
158e71b7053SJung-uk Kim# This adds backtrace information to the memory leak info.  Is only used
159e71b7053SJung-uk Kim# when crypto-mdebug-backtrace is enabled.
160e71b7053SJung-uk Kimmy $memleak_devteam_backtrace = "-rdynamic";
1618180e704SJung-uk Kim
1626a599222SSimon L. B. Nielsenmy $strict_warnings = 0;
1636a599222SSimon L. B. Nielsen
1643b4e3dcbSSimon L. B. Nielsen# As for $BSDthreads. Idea is to maintain "collective" set of flags,
1653b4e3dcbSSimon L. B. Nielsen# which would cover all BSD flavors. -pthread applies to them all,
1663b4e3dcbSSimon L. B. Nielsen# but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
1673b4e3dcbSSimon L. B. Nielsen# -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
1683b4e3dcbSSimon L. B. Nielsen# which has to be accompanied by explicit -D_THREAD_SAFE and
1693b4e3dcbSSimon L. B. Nielsen# sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
1703b4e3dcbSSimon L. B. Nielsen# seems to be sufficient?
171e71b7053SJung-uk Kimour $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
17274664626SKris Kennaway
17374664626SKris Kennaway#
174e71b7053SJung-uk Kim# API compatibility name to version number mapping.
17574664626SKris Kennaway#
176e71b7053SJung-uk Kimmy $maxapi = "1.1.0";           # API for "no-deprecated" builds
177e71b7053SJung-uk Kimmy $apitable = {
178e71b7053SJung-uk Kim    "1.1.0" => "0x10100000L",
179e71b7053SJung-uk Kim    "1.0.0" => "0x10000000L",
180e71b7053SJung-uk Kim    "0.9.8" => "0x00908000L",
181e71b7053SJung-uk Kim};
182e71b7053SJung-uk Kim
183e71b7053SJung-uk Kimour %table = ();
184e71b7053SJung-uk Kimour %config = ();
185e71b7053SJung-uk Kimour %withargs = ();
186e71b7053SJung-uk Kimour $now_printing;      # set to current entry's name in print_table_entry
187e71b7053SJung-uk Kim                        # (todo: right thing would be to encapsulate name
188e71b7053SJung-uk Kim                        # into %target [class] and make print_table_entry
189e71b7053SJung-uk Kim                        # a method)
190e71b7053SJung-uk Kim
191e71b7053SJung-uk Kim# Forward declarations ###############################################
192e71b7053SJung-uk Kim
193e71b7053SJung-uk Kim# read_config(filename)
194c1803d78SJacques Vidrine#
195e71b7053SJung-uk Kim# Reads a configuration file and populates %table with the contents
196e71b7053SJung-uk Kim# (which the configuration file places in %targets).
197e71b7053SJung-uk Kimsub read_config;
19874664626SKris Kennaway
199e71b7053SJung-uk Kim# resolve_config(target)
20074664626SKris Kennaway#
201e71b7053SJung-uk Kim# Resolves all the late evaluations, inheritances and so on for the
202e71b7053SJung-uk Kim# chosen target and any target it inherits from.
203e71b7053SJung-uk Kimsub resolve_config;
20474664626SKris Kennaway
20574664626SKris Kennaway
206e71b7053SJung-uk Kim# Information collection #############################################
20774664626SKris Kennaway
208e71b7053SJung-uk Kim# Unified build supports separate build dir
209e71b7053SJung-uk Kimmy $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
210e71b7053SJung-uk Kimmy $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
211e71b7053SJung-uk Kimmy $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
21274664626SKris Kennaway
213e71b7053SJung-uk Kimmy $local_config_envname = 'OPENSSL_LOCAL_CONFIG_DIR';
214a21b1b38SKris Kennaway
215e71b7053SJung-uk Kim$config{sourcedir} = abs2rel($srcdir);
216e71b7053SJung-uk Kim$config{builddir} = abs2rel($blddir);
21774664626SKris Kennaway
218e71b7053SJung-uk Kim# Collect reconfiguration information if needed
219e71b7053SJung-uk Kimmy @argvcopy=@ARGV;
22074664626SKris Kennaway
221e71b7053SJung-uk Kimif (grep /^reconf(igure)?$/, @argvcopy) {
222e71b7053SJung-uk Kim    die "reconfiguring with other arguments present isn't supported"
223e71b7053SJung-uk Kim        if scalar @argvcopy > 1;
224e71b7053SJung-uk Kim    if (-f "./configdata.pm") {
225e71b7053SJung-uk Kim        my $file = "./configdata.pm";
226e71b7053SJung-uk Kim        unless (my $return = do $file) {
227e71b7053SJung-uk Kim            die "couldn't parse $file: $@" if $@;
228e71b7053SJung-uk Kim            die "couldn't do $file: $!"    unless defined $return;
229e71b7053SJung-uk Kim            die "couldn't run $file"       unless $return;
2301f13597dSJung-uk Kim        }
2313b4e3dcbSSimon L. B. Nielsen
232e71b7053SJung-uk Kim        @argvcopy = defined($configdata::config{perlargv}) ?
233e71b7053SJung-uk Kim            @{$configdata::config{perlargv}} : ();
234e71b7053SJung-uk Kim        die "Incorrect data to reconfigure, please do a normal configuration\n"
235e71b7053SJung-uk Kim            if (grep(/^reconf/,@argvcopy));
236e71b7053SJung-uk Kim        $config{perlenv} = $configdata::config{perlenv} // {};
237e71b7053SJung-uk Kim    } else {
238e71b7053SJung-uk Kim        die "Insufficient data to reconfigure, please do a normal configuration\n";
239e71b7053SJung-uk Kim    }
240e71b7053SJung-uk Kim}
2413b4e3dcbSSimon L. B. Nielsen
242e71b7053SJung-uk Kim$config{perlargv} = [ @argvcopy ];
243e71b7053SJung-uk Kim
244e71b7053SJung-uk Kim# Collect version numbers
245e71b7053SJung-uk Kim$config{version} = "unknown";
246e71b7053SJung-uk Kim$config{version_num} = "unknown";
247e71b7053SJung-uk Kim$config{shlib_version_number} = "unknown";
248e71b7053SJung-uk Kim$config{shlib_version_history} = "unknown";
249e71b7053SJung-uk Kim
250e71b7053SJung-uk Kimcollect_information(
251e71b7053SJung-uk Kim    collect_from_file(catfile($srcdir,'include/openssl/opensslv.h')),
252e71b7053SJung-uk Kim    qr/OPENSSL.VERSION.TEXT.*OpenSSL (\S+) / => sub { $config{version} = $1; },
253e71b7053SJung-uk Kim    qr/OPENSSL.VERSION.NUMBER.*(0x\S+)/      => sub { $config{version_num}=$1 },
254e71b7053SJung-uk Kim    qr/SHLIB_VERSION_NUMBER *"([^"]+)"/      => sub { $config{shlib_version_number}=$1 },
255e71b7053SJung-uk Kim    qr/SHLIB_VERSION_HISTORY *"([^"]*)"/     => sub { $config{shlib_version_history}=$1 }
256e71b7053SJung-uk Kim    );
257e71b7053SJung-uk Kimif ($config{shlib_version_history} ne "") { $config{shlib_version_history} .= ":"; }
258e71b7053SJung-uk Kim
259e71b7053SJung-uk Kim($config{major}, $config{minor})
260e71b7053SJung-uk Kim    = ($config{version} =~ /^([0-9]+)\.([0-9\.]+)/);
261e71b7053SJung-uk Kim($config{shlib_major}, $config{shlib_minor})
262e71b7053SJung-uk Kim    = ($config{shlib_version_number} =~ /^([0-9]+)\.([0-9\.]+)/);
263e71b7053SJung-uk Kimdie "erroneous version information in opensslv.h: ",
264e71b7053SJung-uk Kim    "$config{major}, $config{minor}, $config{shlib_major}, $config{shlib_minor}\n"
265e71b7053SJung-uk Kim    if ($config{major} eq "" || $config{minor} eq ""
266e71b7053SJung-uk Kim        || $config{shlib_major} eq "" ||  $config{shlib_minor} eq "");
267e71b7053SJung-uk Kim
268e71b7053SJung-uk Kim# Collect target configurations
269e71b7053SJung-uk Kim
270e71b7053SJung-uk Kimmy $pattern = catfile(dirname($0), "Configurations", "*.conf");
271e71b7053SJung-uk Kimforeach (sort glob($pattern)) {
272e71b7053SJung-uk Kim    &read_config($_);
273e71b7053SJung-uk Kim}
274e71b7053SJung-uk Kim
275e71b7053SJung-uk Kimif (defined env($local_config_envname)) {
276e71b7053SJung-uk Kim    if ($^O eq 'VMS') {
277e71b7053SJung-uk Kim        # VMS environment variables are logical names,
278e71b7053SJung-uk Kim        # which can be used as is
279e71b7053SJung-uk Kim        $pattern = $local_config_envname . ':' . '*.conf';
280e71b7053SJung-uk Kim    } else {
281e71b7053SJung-uk Kim        $pattern = catfile(env($local_config_envname), '*.conf');
282e71b7053SJung-uk Kim    }
283e71b7053SJung-uk Kim
284e71b7053SJung-uk Kim    foreach (sort glob($pattern)) {
285e71b7053SJung-uk Kim        &read_config($_);
286e71b7053SJung-uk Kim    }
287e71b7053SJung-uk Kim}
288e71b7053SJung-uk Kim
289e71b7053SJung-uk Kim# Save away perl command information
290e71b7053SJung-uk Kim$config{perl_cmd} = $^X;
291e71b7053SJung-uk Kim$config{perl_version} = $Config{version};
292e71b7053SJung-uk Kim$config{perl_archname} = $Config{archname};
293e71b7053SJung-uk Kim
294e71b7053SJung-uk Kim$config{prefix}="";
295e71b7053SJung-uk Kim$config{openssldir}="";
296e71b7053SJung-uk Kim$config{processor}="";
297e71b7053SJung-uk Kim$config{libdir}="";
298e71b7053SJung-uk Kimmy $auto_threads=1;    # enable threads automatically? true by default
299e71b7053SJung-uk Kimmy $default_ranlib;
300e71b7053SJung-uk Kim
301e71b7053SJung-uk Kim# Top level directories to build
302e71b7053SJung-uk Kim$config{dirs} = [ "crypto", "ssl", "engines", "apps", "test", "util", "tools", "fuzz" ];
303e71b7053SJung-uk Kim# crypto/ subdirectories to build
304e71b7053SJung-uk Kim$config{sdirs} = [
305e71b7053SJung-uk Kim    "objects",
306e71b7053SJung-uk Kim    "md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305", "blake2", "siphash", "sm3",
307e71b7053SJung-uk Kim    "des", "aes", "rc2", "rc4", "rc5", "idea", "aria", "bf", "cast", "camellia", "seed", "sm4", "chacha", "modes",
308e71b7053SJung-uk Kim    "bn", "ec", "rsa", "dsa", "dh", "sm2", "dso", "engine",
309e71b7053SJung-uk Kim    "buffer", "bio", "stack", "lhash", "rand", "err",
310e71b7053SJung-uk Kim    "evp", "asn1", "pem", "x509", "x509v3", "conf", "txt_db", "pkcs7", "pkcs12", "comp", "ocsp", "ui",
311e71b7053SJung-uk Kim    "cms", "ts", "srp", "cmac", "ct", "async", "kdf", "store"
312e71b7053SJung-uk Kim    ];
313e71b7053SJung-uk Kim# test/ subdirectories to build
314e71b7053SJung-uk Kim$config{tdirs} = [ "ossl_shim" ];
315e71b7053SJung-uk Kim
316e71b7053SJung-uk Kim# Known TLS and DTLS protocols
317e71b7053SJung-uk Kimmy @tls = qw(ssl3 tls1 tls1_1 tls1_2 tls1_3);
318e71b7053SJung-uk Kimmy @dtls = qw(dtls1 dtls1_2);
319e71b7053SJung-uk Kim
320e71b7053SJung-uk Kim# Explicitly known options that are possible to disable.  They can
321e71b7053SJung-uk Kim# be regexps, and will be used like this: /^no-${option}$/
322e71b7053SJung-uk Kim# For developers: keep it sorted alphabetically
323e71b7053SJung-uk Kim
324e71b7053SJung-uk Kimmy @disablables = (
325e71b7053SJung-uk Kim    "afalgeng",
326e71b7053SJung-uk Kim    "aria",
327e71b7053SJung-uk Kim    "asan",
328e71b7053SJung-uk Kim    "asm",
329e71b7053SJung-uk Kim    "async",
330e71b7053SJung-uk Kim    "autoalginit",
331e71b7053SJung-uk Kim    "autoerrinit",
332e71b7053SJung-uk Kim    "autoload-config",
333e71b7053SJung-uk Kim    "bf",
334e71b7053SJung-uk Kim    "blake2",
335*610a21fdSJung-uk Kim    "buildtest-c\\+\\+",
336e71b7053SJung-uk Kim    "camellia",
337e71b7053SJung-uk Kim    "capieng",
338e71b7053SJung-uk Kim    "cast",
339e71b7053SJung-uk Kim    "chacha",
340e71b7053SJung-uk Kim    "cmac",
341e71b7053SJung-uk Kim    "cms",
342e71b7053SJung-uk Kim    "comp",
343e71b7053SJung-uk Kim    "crypto-mdebug",
344e71b7053SJung-uk Kim    "crypto-mdebug-backtrace",
345e71b7053SJung-uk Kim    "ct",
346e71b7053SJung-uk Kim    "deprecated",
347e71b7053SJung-uk Kim    "des",
348e71b7053SJung-uk Kim    "devcryptoeng",
349e71b7053SJung-uk Kim    "dgram",
350e71b7053SJung-uk Kim    "dh",
351e71b7053SJung-uk Kim    "dsa",
352e71b7053SJung-uk Kim    "dtls",
353e71b7053SJung-uk Kim    "dynamic-engine",
354e71b7053SJung-uk Kim    "ec",
355e71b7053SJung-uk Kim    "ec2m",
356e71b7053SJung-uk Kim    "ecdh",
357e71b7053SJung-uk Kim    "ecdsa",
358e71b7053SJung-uk Kim    "ec_nistp_64_gcc_128",
359e71b7053SJung-uk Kim    "egd",
360e71b7053SJung-uk Kim    "engine",
361e71b7053SJung-uk Kim    "err",
362e71b7053SJung-uk Kim    "external-tests",
363e71b7053SJung-uk Kim    "filenames",
364e71b7053SJung-uk Kim    "fuzz-libfuzzer",
365e71b7053SJung-uk Kim    "fuzz-afl",
366e71b7053SJung-uk Kim    "gost",
367e71b7053SJung-uk Kim    "heartbeats",
368e71b7053SJung-uk Kim    "hw(-.+)?",
369e71b7053SJung-uk Kim    "idea",
370e71b7053SJung-uk Kim    "makedepend",
371e71b7053SJung-uk Kim    "md2",
372e71b7053SJung-uk Kim    "md4",
373e71b7053SJung-uk Kim    "mdc2",
374e71b7053SJung-uk Kim    "msan",
375e71b7053SJung-uk Kim    "multiblock",
376e71b7053SJung-uk Kim    "nextprotoneg",
3776935a639SJung-uk Kim    "pinshared",
378e71b7053SJung-uk Kim    "ocb",
379e71b7053SJung-uk Kim    "ocsp",
380e71b7053SJung-uk Kim    "pic",
381e71b7053SJung-uk Kim    "poly1305",
382e71b7053SJung-uk Kim    "posix-io",
383e71b7053SJung-uk Kim    "psk",
384e71b7053SJung-uk Kim    "rc2",
385e71b7053SJung-uk Kim    "rc4",
386e71b7053SJung-uk Kim    "rc5",
387e71b7053SJung-uk Kim    "rdrand",
388e71b7053SJung-uk Kim    "rfc3779",
389e71b7053SJung-uk Kim    "rmd160",
390e71b7053SJung-uk Kim    "scrypt",
391e71b7053SJung-uk Kim    "sctp",
392e71b7053SJung-uk Kim    "seed",
393e71b7053SJung-uk Kim    "shared",
394e71b7053SJung-uk Kim    "siphash",
395e71b7053SJung-uk Kim    "sm2",
396e71b7053SJung-uk Kim    "sm3",
397e71b7053SJung-uk Kim    "sm4",
398e71b7053SJung-uk Kim    "sock",
399e71b7053SJung-uk Kim    "srp",
400e71b7053SJung-uk Kim    "srtp",
401e71b7053SJung-uk Kim    "sse2",
402e71b7053SJung-uk Kim    "ssl",
403e71b7053SJung-uk Kim    "ssl-trace",
404e71b7053SJung-uk Kim    "static-engine",
405e71b7053SJung-uk Kim    "stdio",
406e71b7053SJung-uk Kim    "tests",
407e71b7053SJung-uk Kim    "threads",
408e71b7053SJung-uk Kim    "tls",
409e71b7053SJung-uk Kim    "ts",
410e71b7053SJung-uk Kim    "ubsan",
411e71b7053SJung-uk Kim    "ui-console",
412e71b7053SJung-uk Kim    "unit-test",
413e71b7053SJung-uk Kim    "whirlpool",
414e71b7053SJung-uk Kim    "weak-ssl-ciphers",
415e71b7053SJung-uk Kim    "zlib",
416e71b7053SJung-uk Kim    "zlib-dynamic",
417e71b7053SJung-uk Kim    );
418e71b7053SJung-uk Kimforeach my $proto ((@tls, @dtls))
419e71b7053SJung-uk Kim        {
420e71b7053SJung-uk Kim        push(@disablables, $proto);
421e71b7053SJung-uk Kim        push(@disablables, "$proto-method") unless $proto eq "tls1_3";
422e71b7053SJung-uk Kim        }
423e71b7053SJung-uk Kim
424e71b7053SJung-uk Kimmy %deprecated_disablables = (
425e71b7053SJung-uk Kim    "ssl2" => undef,
426e71b7053SJung-uk Kim    "buf-freelists" => undef,
427e71b7053SJung-uk Kim    "ripemd" => "rmd160",
428e71b7053SJung-uk Kim    "ui" => "ui-console",
429*610a21fdSJung-uk Kim    "dso" => "",                # Empty string means we're silent about it
430e71b7053SJung-uk Kim    );
431e71b7053SJung-uk Kim
432e71b7053SJung-uk Kim# All of the following are disabled by default:
433e71b7053SJung-uk Kim
434e71b7053SJung-uk Kimour %disabled = ( # "what"         => "comment"
435e71b7053SJung-uk Kim                  "asan"                => "default",
436*610a21fdSJung-uk Kim                  "buildtest-c++"       => "default",
437e71b7053SJung-uk Kim                  "crypto-mdebug"       => "default",
438e71b7053SJung-uk Kim                  "crypto-mdebug-backtrace" => "default",
439e71b7053SJung-uk Kim                  "devcryptoeng"        => "default",
4401f13597dSJung-uk Kim                  "ec_nistp_64_gcc_128" => "default",
441e71b7053SJung-uk Kim                  "egd"                 => "default",
442e71b7053SJung-uk Kim                  "external-tests"      => "default",
443e71b7053SJung-uk Kim                  "fuzz-libfuzzer"      => "default",
444e71b7053SJung-uk Kim                  "fuzz-afl"            => "default",
445e71b7053SJung-uk Kim                  "heartbeats"          => "default",
4461f13597dSJung-uk Kim                  "md2"                 => "default",
447e71b7053SJung-uk Kim                  "msan"                => "default",
4483b4e3dcbSSimon L. B. Nielsen                  "rc5"                 => "default",
4491f13597dSJung-uk Kim                  "sctp"                => "default",
4507bded2dbSJung-uk Kim                  "ssl-trace"           => "default",
451e71b7053SJung-uk Kim                  "ssl3"                => "default",
452e71b7053SJung-uk Kim                  "ssl3-method"         => "default",
453e71b7053SJung-uk Kim                  "ubsan"               => "default",
454a93cbc2bSJung-uk Kim                  "unit-test"           => "default",
4554c6a0400SJung-uk Kim                  "weak-ssl-ciphers"    => "default",
4563b4e3dcbSSimon L. B. Nielsen                  "zlib"                => "default",
457e71b7053SJung-uk Kim                  "zlib-dynamic"        => "default",
4583b4e3dcbSSimon L. B. Nielsen                );
4593b4e3dcbSSimon L. B. Nielsen
460e71b7053SJung-uk Kim# Note: => pair form used for aesthetics, not to truly make a hash table
461e71b7053SJung-uk Kimmy @disable_cascades = (
462e71b7053SJung-uk Kim    # "what"            => [ "cascade", ... ]
463e71b7053SJung-uk Kim    sub { $config{processor} eq "386" }
464e71b7053SJung-uk Kim                        => [ "sse2" ],
465e71b7053SJung-uk Kim    "ssl"               => [ "ssl3" ],
466e71b7053SJung-uk Kim    "ssl3-method"       => [ "ssl3" ],
467e71b7053SJung-uk Kim    "zlib"              => [ "zlib-dynamic" ],
468e71b7053SJung-uk Kim    "des"               => [ "mdc2" ],
469e71b7053SJung-uk Kim    "ec"                => [ "ecdsa", "ecdh" ],
470e71b7053SJung-uk Kim
471e71b7053SJung-uk Kim    "dgram"             => [ "dtls", "sctp" ],
472e71b7053SJung-uk Kim    "sock"              => [ "dgram" ],
473e71b7053SJung-uk Kim    "dtls"              => [ @dtls ],
474e71b7053SJung-uk Kim    sub { 0 == scalar grep { !$disabled{$_} } @dtls }
475e71b7053SJung-uk Kim                        => [ "dtls" ],
476e71b7053SJung-uk Kim
477e71b7053SJung-uk Kim    "tls"               => [ @tls ],
478e71b7053SJung-uk Kim    sub { 0 == scalar grep { !$disabled{$_} } @tls }
479e71b7053SJung-uk Kim                        => [ "tls" ],
480e71b7053SJung-uk Kim
481e71b7053SJung-uk Kim    "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
482e71b7053SJung-uk Kim
483e71b7053SJung-uk Kim    # Without position independent code, there can be no shared libraries or DSOs
484e71b7053SJung-uk Kim    "pic"               => [ "shared" ],
485e71b7053SJung-uk Kim    "shared"            => [ "dynamic-engine" ],
486e71b7053SJung-uk Kim    "engine"            => [ "afalgeng", "devcryptoeng" ],
487e71b7053SJung-uk Kim
488e71b7053SJung-uk Kim    # no-autoalginit is only useful when building non-shared
489e71b7053SJung-uk Kim    "autoalginit"       => [ "shared", "apps" ],
490e71b7053SJung-uk Kim
491e71b7053SJung-uk Kim    "stdio"             => [ "apps", "capieng", "egd" ],
492e71b7053SJung-uk Kim    "apps"              => [ "tests" ],
493e71b7053SJung-uk Kim    "tests"             => [ "external-tests" ],
494e71b7053SJung-uk Kim    "comp"              => [ "zlib" ],
495e71b7053SJung-uk Kim    "ec"                => [ "tls1_3", "sm2" ],
496e71b7053SJung-uk Kim    "sm3"               => [ "sm2" ],
497e71b7053SJung-uk Kim    sub { !$disabled{"unit-test"} } => [ "heartbeats" ],
498e71b7053SJung-uk Kim
499e71b7053SJung-uk Kim    sub { !$disabled{"msan"} } => [ "asm" ],
500e71b7053SJung-uk Kim    );
501e71b7053SJung-uk Kim
502e71b7053SJung-uk Kim# Avoid protocol support holes.  Also disable all versions below N, if version
503e71b7053SJung-uk Kim# N is disabled while N+1 is enabled.
504e71b7053SJung-uk Kim#
505e71b7053SJung-uk Kimmy @list = (reverse @tls);
506e71b7053SJung-uk Kimwhile ((my $first, my $second) = (shift @list, shift @list)) {
507e71b7053SJung-uk Kim    last unless @list;
508e71b7053SJung-uk Kim    push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
509e71b7053SJung-uk Kim                              => [ @list ] );
510e71b7053SJung-uk Kim    unshift @list, $second;
511e71b7053SJung-uk Kim}
512e71b7053SJung-uk Kimmy @list = (reverse @dtls);
513e71b7053SJung-uk Kimwhile ((my $first, my $second) = (shift @list, shift @list)) {
514e71b7053SJung-uk Kim    last unless @list;
515e71b7053SJung-uk Kim    push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
516e71b7053SJung-uk Kim                              => [ @list ] );
517e71b7053SJung-uk Kim    unshift @list, $second;
518e71b7053SJung-uk Kim}
519db522d3aSSimon L. B. Nielsen
520db522d3aSSimon L. B. Nielsen# Explicit "no-..." options will be collected in %disabled along with the defaults.
521e71b7053SJung-uk Kim# To remove something from %disabled, use "enable-foo".
522db522d3aSSimon L. B. Nielsen# For symmetry, "disable-foo" is a synonym for "no-foo".
523db522d3aSSimon L. B. Nielsen
52474664626SKris Kennaway&usage if ($#ARGV < 0);
52574664626SKris Kennaway
526e71b7053SJung-uk Kim# For the "make variables" CINCLUDES and CDEFINES, we support lists with
527e71b7053SJung-uk Kim# platform specific list separators.  Users from those platforms should
528e71b7053SJung-uk Kim# recognise those separators from how you set up the PATH to find executables.
529e71b7053SJung-uk Kim# The default is the Unix like separator, :, but as an exception, we also
530e71b7053SJung-uk Kim# support the space as separator.
531e71b7053SJung-uk Kimmy $list_separator_re =
532e71b7053SJung-uk Kim    { VMS           => qr/(?<!\^),/,
533e71b7053SJung-uk Kim      MSWin32       => qr/(?<!\\);/ } -> {$^O} // qr/(?<!\\)[:\s]/;
534e71b7053SJung-uk Kim# All the "make variables" we support
535e71b7053SJung-uk Kim# Some get pre-populated for the sake of backward compatibility
536e71b7053SJung-uk Kim# (we supported those before the change to "make variable" support.
537e71b7053SJung-uk Kimmy %user = (
538e71b7053SJung-uk Kim    AR          => env('AR'),
539e71b7053SJung-uk Kim    ARFLAGS     => [],
540e71b7053SJung-uk Kim    AS          => undef,
541e71b7053SJung-uk Kim    ASFLAGS     => [],
542e71b7053SJung-uk Kim    CC          => env('CC'),
543*610a21fdSJung-uk Kim    CFLAGS      => [ env('CFLAGS') || () ],
544e71b7053SJung-uk Kim    CXX         => env('CXX'),
545*610a21fdSJung-uk Kim    CXXFLAGS    => [ env('CXXFLAGS') || () ],
546e71b7053SJung-uk Kim    CPP         => undef,
547*610a21fdSJung-uk Kim    CPPFLAGS    => [ env('CPPFLAGS') || () ],  # -D, -I, -Wp,
548e71b7053SJung-uk Kim    CPPDEFINES  => [],  # Alternative for -D
549e71b7053SJung-uk Kim    CPPINCLUDES => [],  # Alternative for -I
550e71b7053SJung-uk Kim    CROSS_COMPILE => env('CROSS_COMPILE'),
551e71b7053SJung-uk Kim    HASHBANGPERL=> env('HASHBANGPERL') || env('PERL'),
552e71b7053SJung-uk Kim    LD          => undef,
553*610a21fdSJung-uk Kim    LDFLAGS     => [ env('LDFLAGS') || () ],  # -L, -Wl,
554*610a21fdSJung-uk Kim    LDLIBS      => [ env('LDLIBS') || () ],  # -l
555e71b7053SJung-uk Kim    MT          => undef,
556e71b7053SJung-uk Kim    MTFLAGS     => [],
557e71b7053SJung-uk Kim    PERL        => env('PERL') || ($^O ne "VMS" ? $^X : "perl"),
558e71b7053SJung-uk Kim    RANLIB      => env('RANLIB'),
559e71b7053SJung-uk Kim    RC          => env('RC') || env('WINDRES'),
560*610a21fdSJung-uk Kim    RCFLAGS     => [ env('RCFLAGS') || () ],
561e71b7053SJung-uk Kim    RM          => undef,
562e71b7053SJung-uk Kim   );
563e71b7053SJung-uk Kim# Info about what "make variables" may be prefixed with the cross compiler
564e71b7053SJung-uk Kim# prefix.  This should NEVER mention any such variable with a list for value.
565e71b7053SJung-uk Kimmy @user_crossable = qw ( AR AS CC CXX CPP LD MT RANLIB RC );
566e71b7053SJung-uk Kim# The same but for flags given as Configure options.  These are *additional*
567e71b7053SJung-uk Kim# input, as opposed to the VAR=string option that override the corresponding
568e71b7053SJung-uk Kim# config target attributes
569e71b7053SJung-uk Kimmy %useradd = (
570e71b7053SJung-uk Kim    CPPDEFINES  => [],
571e71b7053SJung-uk Kim    CPPINCLUDES => [],
572e71b7053SJung-uk Kim    CPPFLAGS    => [],
573e71b7053SJung-uk Kim    CFLAGS      => [],
574e71b7053SJung-uk Kim    CXXFLAGS    => [],
575e71b7053SJung-uk Kim    LDFLAGS     => [],
576e71b7053SJung-uk Kim    LDLIBS      => [],
577*610a21fdSJung-uk Kim    RCFLAGS     => [],
578e71b7053SJung-uk Kim   );
579ddd58736SKris Kennaway
580e71b7053SJung-uk Kimmy %user_synonyms = (
581e71b7053SJung-uk Kim    HASHBANGPERL=> 'PERL',
582e71b7053SJung-uk Kim    RC          => 'WINDRES',
583e71b7053SJung-uk Kim   );
584ddd58736SKris Kennaway
585e71b7053SJung-uk Kim# Some target attributes have been renamed, this is the translation table
586e71b7053SJung-uk Kimmy %target_attr_translate =(
587e71b7053SJung-uk Kim    ar          => 'AR',
588e71b7053SJung-uk Kim    as          => 'AS',
589e71b7053SJung-uk Kim    cc          => 'CC',
590e71b7053SJung-uk Kim    cxx         => 'CXX',
591e71b7053SJung-uk Kim    cpp         => 'CPP',
592e71b7053SJung-uk Kim    hashbangperl => 'HASHBANGPERL',
593e71b7053SJung-uk Kim    ld          => 'LD',
594e71b7053SJung-uk Kim    mt          => 'MT',
595e71b7053SJung-uk Kim    ranlib      => 'RANLIB',
596e71b7053SJung-uk Kim    rc          => 'RC',
597e71b7053SJung-uk Kim    rm          => 'RM',
598e71b7053SJung-uk Kim   );
599e71b7053SJung-uk Kim
600e71b7053SJung-uk Kim# Initialisers coming from 'config' scripts
601e71b7053SJung-uk Kim$config{defines} = [ split(/$list_separator_re/, env('__CNF_CPPDEFINES')) ];
602e71b7053SJung-uk Kim$config{includes} = [ split(/$list_separator_re/, env('__CNF_CPPINCLUDES')) ];
603e71b7053SJung-uk Kim$config{cppflags} = [ env('__CNF_CPPFLAGS') || () ];
604e71b7053SJung-uk Kim$config{cflags} = [ env('__CNF_CFLAGS') || () ];
605e71b7053SJung-uk Kim$config{cxxflags} = [ env('__CNF_CXXFLAGS') || () ];
606e71b7053SJung-uk Kim$config{lflags} = [ env('__CNF_LDFLAGS') || () ];
607e71b7053SJung-uk Kim$config{ex_libs} = [ env('__CNF_LDLIBS') || () ];
608e71b7053SJung-uk Kim
609e71b7053SJung-uk Kim$config{openssl_api_defines}=[];
610e71b7053SJung-uk Kim$config{openssl_algorithm_defines}=[];
611e71b7053SJung-uk Kim$config{openssl_thread_defines}=[];
612e71b7053SJung-uk Kim$config{openssl_sys_defines}=[];
613e71b7053SJung-uk Kim$config{openssl_other_defines}=[];
614e71b7053SJung-uk Kim$config{options}="";
615e71b7053SJung-uk Kim$config{build_type} = "release";
616e71b7053SJung-uk Kimmy $target="";
617e71b7053SJung-uk Kim
618e71b7053SJung-uk Kimmy %cmdvars = ();               # Stores FOO='blah' type arguments
619e71b7053SJung-uk Kimmy %unsupported_options = ();
620e71b7053SJung-uk Kimmy %deprecated_options = ();
621e71b7053SJung-uk Kim# If you change this, update apps/version.c
622e71b7053SJung-uk Kimmy @known_seed_sources = qw(getrandom devrandom os egd none rdcpu librandom);
623e71b7053SJung-uk Kimmy @seed_sources = ();
624e71b7053SJung-uk Kimwhile (@argvcopy)
625ddd58736SKris Kennaway        {
626e71b7053SJung-uk Kim        $_ = shift @argvcopy;
627ddd58736SKris Kennaway
628e71b7053SJung-uk Kim        # Support env variable assignments among the options
629e71b7053SJung-uk Kim        if (m|^(\w+)=(.+)?$|)
63074664626SKris Kennaway                {
631e71b7053SJung-uk Kim                $cmdvars{$1} = $2;
632e71b7053SJung-uk Kim                # Every time a variable is given as a configuration argument,
633e71b7053SJung-uk Kim                # it acts as a reset if the variable.
634e71b7053SJung-uk Kim                if (exists $user{$1})
635e71b7053SJung-uk Kim                        {
636e71b7053SJung-uk Kim                        $user{$1} = ref $user{$1} eq "ARRAY" ? [] : undef;
637e71b7053SJung-uk Kim                        }
638e71b7053SJung-uk Kim                #if (exists $useradd{$1})
639e71b7053SJung-uk Kim                #       {
640e71b7053SJung-uk Kim                #       $useradd{$1} = [];
641e71b7053SJung-uk Kim                #       }
642e71b7053SJung-uk Kim                next;
643e71b7053SJung-uk Kim                }
644e71b7053SJung-uk Kim
645e71b7053SJung-uk Kim        # VMS is a case insensitive environment, and depending on settings
646e71b7053SJung-uk Kim        # out of our control, we may receive options uppercased.  Let's
647e71b7053SJung-uk Kim        # downcase at least the part before any equal sign.
648e71b7053SJung-uk Kim        if ($^O eq "VMS")
649e71b7053SJung-uk Kim                {
650e71b7053SJung-uk Kim                s/^([^=]*)/lc($1)/e;
651e71b7053SJung-uk Kim                }
652e71b7053SJung-uk Kim
653e71b7053SJung-uk Kim        # some people just can't read the instructions, clang people have to...
654e71b7053SJung-uk Kim        s/^-no-(?!integrated-as)/no-/;
6553b4e3dcbSSimon L. B. Nielsen
6563b4e3dcbSSimon L. B. Nielsen        # rewrite some options in "enable-..." form
6573b4e3dcbSSimon L. B. Nielsen        s /^-?-?shared$/enable-shared/;
6581f13597dSJung-uk Kim        s /^sctp$/enable-sctp/;
6593b4e3dcbSSimon L. B. Nielsen        s /^threads$/enable-threads/;
6603b4e3dcbSSimon L. B. Nielsen        s /^zlib$/enable-zlib/;
6613b4e3dcbSSimon L. B. Nielsen        s /^zlib-dynamic$/enable-zlib-dynamic/;
6623b4e3dcbSSimon L. B. Nielsen
663e71b7053SJung-uk Kim        if (/^(no|disable|enable)-(.+)$/)
664e71b7053SJung-uk Kim                {
665e71b7053SJung-uk Kim                my $word = $2;
666e71b7053SJung-uk Kim                if (!exists $deprecated_disablables{$word}
667e71b7053SJung-uk Kim                        && !grep { $word =~ /^${_}$/ } @disablables)
668e71b7053SJung-uk Kim                        {
669e71b7053SJung-uk Kim                        $unsupported_options{$_} = 1;
670e71b7053SJung-uk Kim                        next;
671e71b7053SJung-uk Kim                        }
672e71b7053SJung-uk Kim                }
6733b4e3dcbSSimon L. B. Nielsen        if (/^no-(.+)$/ || /^disable-(.+)$/)
6743b4e3dcbSSimon L. B. Nielsen                {
675e71b7053SJung-uk Kim                foreach my $proto ((@tls, @dtls))
676db522d3aSSimon L. B. Nielsen                        {
677e71b7053SJung-uk Kim                        if ($1 eq "$proto-method")
6783b4e3dcbSSimon L. B. Nielsen                                {
679e71b7053SJung-uk Kim                                $disabled{"$proto"} = "option($proto-method)";
680e71b7053SJung-uk Kim                                last;
681e71b7053SJung-uk Kim                                }
682e71b7053SJung-uk Kim                        }
683e71b7053SJung-uk Kim                if ($1 eq "dtls")
684e71b7053SJung-uk Kim                        {
685e71b7053SJung-uk Kim                        foreach my $proto (@dtls)
686e71b7053SJung-uk Kim                                {
687e71b7053SJung-uk Kim                                $disabled{$proto} = "option(dtls)";
688e71b7053SJung-uk Kim                                }
689e71b7053SJung-uk Kim                        $disabled{"dtls"} = "option(dtls)";
690e71b7053SJung-uk Kim                        }
691e71b7053SJung-uk Kim                elsif ($1 eq "ssl")
692e71b7053SJung-uk Kim                        {
693e71b7053SJung-uk Kim                        # Last one of its kind
6943b4e3dcbSSimon L. B. Nielsen                        $disabled{"ssl3"} = "option(ssl)";
6953b4e3dcbSSimon L. B. Nielsen                        }
6963b4e3dcbSSimon L. B. Nielsen                elsif ($1 eq "tls")
6973b4e3dcbSSimon L. B. Nielsen                        {
698e71b7053SJung-uk Kim                        # XXX: Tests will fail if all SSL/TLS
699e71b7053SJung-uk Kim                        # protocols are disabled.
700e71b7053SJung-uk Kim                        foreach my $proto (@tls)
701751d2991SJung-uk Kim                                {
702e71b7053SJung-uk Kim                                $disabled{$proto} = "option(tls)";
703e71b7053SJung-uk Kim                                }
704e71b7053SJung-uk Kim                        }
705e71b7053SJung-uk Kim                elsif ($1 eq "static-engine")
706e71b7053SJung-uk Kim                        {
707e71b7053SJung-uk Kim                        delete $disabled{"dynamic-engine"};
708e71b7053SJung-uk Kim                        }
709e71b7053SJung-uk Kim                elsif ($1 eq "dynamic-engine")
710e71b7053SJung-uk Kim                        {
711e71b7053SJung-uk Kim                        $disabled{"dynamic-engine"} = "option";
712e71b7053SJung-uk Kim                        }
713e71b7053SJung-uk Kim                elsif (exists $deprecated_disablables{$1})
714e71b7053SJung-uk Kim                        {
715*610a21fdSJung-uk Kim                        if ($deprecated_disablables{$1} ne "")
716*610a21fdSJung-uk Kim                                {
717e71b7053SJung-uk Kim                                $deprecated_options{$_} = 1;
718e71b7053SJung-uk Kim                                if (defined $deprecated_disablables{$1})
719e71b7053SJung-uk Kim                                        {
720e71b7053SJung-uk Kim                                        $disabled{$deprecated_disablables{$1}} = "option";
721e71b7053SJung-uk Kim                                        }
722751d2991SJung-uk Kim                                }
723*610a21fdSJung-uk Kim                        }
7243b4e3dcbSSimon L. B. Nielsen                else
7253b4e3dcbSSimon L. B. Nielsen                        {
7263b4e3dcbSSimon L. B. Nielsen                        $disabled{$1} = "option";
7273b4e3dcbSSimon L. B. Nielsen                        }
728e71b7053SJung-uk Kim                # No longer an automatic choice
729e71b7053SJung-uk Kim                $auto_threads = 0 if ($1 eq "threads");
7303b4e3dcbSSimon L. B. Nielsen                }
731e71b7053SJung-uk Kim        elsif (/^enable-(.+)$/)
7323b4e3dcbSSimon L. B. Nielsen                {
733e71b7053SJung-uk Kim                if ($1 eq "static-engine")
734e71b7053SJung-uk Kim                        {
735e71b7053SJung-uk Kim                        $disabled{"dynamic-engine"} = "option";
736e71b7053SJung-uk Kim                        }
737e71b7053SJung-uk Kim                elsif ($1 eq "dynamic-engine")
738e71b7053SJung-uk Kim                        {
739e71b7053SJung-uk Kim                        delete $disabled{"dynamic-engine"};
740e71b7053SJung-uk Kim                        }
741e71b7053SJung-uk Kim                elsif ($1 eq "zlib-dynamic")
742e71b7053SJung-uk Kim                        {
743e71b7053SJung-uk Kim                        delete $disabled{"zlib"};
744e71b7053SJung-uk Kim                        }
745db522d3aSSimon L. B. Nielsen                my $algo = $1;
746db522d3aSSimon L. B. Nielsen                delete $disabled{$algo};
7473b4e3dcbSSimon L. B. Nielsen
748e71b7053SJung-uk Kim                # No longer an automatic choice
749e71b7053SJung-uk Kim                $auto_threads = 0 if ($1 eq "threads");
7503b4e3dcbSSimon L. B. Nielsen                }
751e71b7053SJung-uk Kim        elsif (/^--strict-warnings$/)
7526a599222SSimon L. B. Nielsen                {
753*610a21fdSJung-uk Kim                # Pretend that our strict flags is a C flag, and replace it
754*610a21fdSJung-uk Kim                # with the proper flags later on
755*610a21fdSJung-uk Kim                push @{$useradd{CFLAGS}}, '--ossl-strict-warnings';
7566a599222SSimon L. B. Nielsen                $strict_warnings=1;
7576a599222SSimon L. B. Nielsen                }
758e71b7053SJung-uk Kim        elsif (/^--debug$/)
759ddd58736SKris Kennaway                {
760e71b7053SJung-uk Kim                $config{build_type} = "debug";
761ddd58736SKris Kennaway                }
762e71b7053SJung-uk Kim        elsif (/^--release$/)
763e71b7053SJung-uk Kim                {
764e71b7053SJung-uk Kim                $config{build_type} = "release";
765ddd58736SKris Kennaway                }
76674664626SKris Kennaway        elsif (/^386$/)
767e71b7053SJung-uk Kim                { $config{processor}=386; }
768db522d3aSSimon L. B. Nielsen        elsif (/^fips$/)
769db522d3aSSimon L. B. Nielsen                {
770e71b7053SJung-uk Kim                die "FIPS mode not supported\n";
771db522d3aSSimon L. B. Nielsen                }
77274664626SKris Kennaway        elsif (/^rsaref$/)
77374664626SKris Kennaway                {
7745c87c606SMark Murray                # No RSAref support any more since it's not needed.
7755c87c606SMark Murray                # The check for the option is there so scripts aren't
7765c87c606SMark Murray                # broken
77774664626SKris Kennaway                }
778e71b7053SJung-uk Kim        elsif (/^nofipscanistercheck$/)
779e71b7053SJung-uk Kim                {
780e71b7053SJung-uk Kim                die "FIPS mode not supported\n";
781e71b7053SJung-uk Kim                }
78274664626SKris Kennaway        elsif (/^[-+]/)
78374664626SKris Kennaway                {
7847bded2dbSJung-uk Kim                if (/^--prefix=(.*)$/)
78574664626SKris Kennaway                        {
786e71b7053SJung-uk Kim                        $config{prefix}=$1;
787e71b7053SJung-uk Kim                        die "Directory given with --prefix MUST be absolute\n"
788e71b7053SJung-uk Kim                                unless file_name_is_absolute($config{prefix});
789e71b7053SJung-uk Kim                        }
790e71b7053SJung-uk Kim                elsif (/^--api=(.*)$/)
791e71b7053SJung-uk Kim                        {
792e71b7053SJung-uk Kim                        $config{api}=$1;
79374664626SKris Kennaway                        }
7946a599222SSimon L. B. Nielsen                elsif (/^--libdir=(.*)$/)
7956a599222SSimon L. B. Nielsen                        {
796e71b7053SJung-uk Kim                        $config{libdir}=$1;
7976a599222SSimon L. B. Nielsen                        }
79874664626SKris Kennaway                elsif (/^--openssldir=(.*)$/)
79974664626SKris Kennaway                        {
800e71b7053SJung-uk Kim                        $config{openssldir}=$1;
8015c87c606SMark Murray                        }
8023b4e3dcbSSimon L. B. Nielsen                elsif (/^--with-zlib-lib=(.*)$/)
8033b4e3dcbSSimon L. B. Nielsen                        {
804e71b7053SJung-uk Kim                        $withargs{zlib_lib}=$1;
8053b4e3dcbSSimon L. B. Nielsen                        }
8063b4e3dcbSSimon L. B. Nielsen                elsif (/^--with-zlib-include=(.*)$/)
8073b4e3dcbSSimon L. B. Nielsen                        {
808e71b7053SJung-uk Kim                        $withargs{zlib_include}=$1;
8093b4e3dcbSSimon L. B. Nielsen                        }
810e71b7053SJung-uk Kim                elsif (/^--with-fuzzer-lib=(.*)$/)
8111f13597dSJung-uk Kim                        {
812e71b7053SJung-uk Kim                        $withargs{fuzzer_lib}=$1;
8131f13597dSJung-uk Kim                        }
814e71b7053SJung-uk Kim                elsif (/^--with-fuzzer-include=(.*)$/)
815db522d3aSSimon L. B. Nielsen                        {
816e71b7053SJung-uk Kim                        $withargs{fuzzer_include}=$1;
817db522d3aSSimon L. B. Nielsen                        }
818e71b7053SJung-uk Kim                elsif (/^--with-rand-seed=(.*)$/)
819db522d3aSSimon L. B. Nielsen                        {
820e71b7053SJung-uk Kim                        foreach my $x (split(m|,|, $1))
821e71b7053SJung-uk Kim                            {
822e71b7053SJung-uk Kim                            die "Unknown --with-rand-seed choice $x\n"
823e71b7053SJung-uk Kim                                if ! grep { $x eq $_ } @known_seed_sources;
824e71b7053SJung-uk Kim                            push @seed_sources, $x;
825e71b7053SJung-uk Kim                            }
826db522d3aSSimon L. B. Nielsen                        }
8271f13597dSJung-uk Kim                elsif (/^--cross-compile-prefix=(.*)$/)
8281f13597dSJung-uk Kim                        {
829e71b7053SJung-uk Kim                        $user{CROSS_COMPILE}=$1;
8301f13597dSJung-uk Kim                        }
831e71b7053SJung-uk Kim                elsif (/^--config=(.*)$/)
83274664626SKris Kennaway                        {
833e71b7053SJung-uk Kim                        read_config $1;
834e71b7053SJung-uk Kim                        }
835e71b7053SJung-uk Kim                elsif (/^-l(.*)$/)
836e71b7053SJung-uk Kim                        {
837e71b7053SJung-uk Kim                        push @{$useradd{LDLIBS}}, $_;
838e71b7053SJung-uk Kim                        }
839e71b7053SJung-uk Kim                elsif (/^-framework$/)
840e71b7053SJung-uk Kim                        {
841e71b7053SJung-uk Kim                        push @{$useradd{LDLIBS}}, $_, shift(@argvcopy);
842e71b7053SJung-uk Kim                        }
843e71b7053SJung-uk Kim                elsif (/^-L(.*)$/ or /^-Wl,/)
844e71b7053SJung-uk Kim                        {
845e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_;
846e71b7053SJung-uk Kim                        }
847e71b7053SJung-uk Kim                elsif (/^-rpath$/ or /^-R$/)
848e71b7053SJung-uk Kim                        # -rpath is the OSF1 rpath flag
849e71b7053SJung-uk Kim                        # -R is the old Solaris rpath flag
850e71b7053SJung-uk Kim                        {
851e71b7053SJung-uk Kim                        my $rpath = shift(@argvcopy) || "";
852e71b7053SJung-uk Kim                        $rpath .= " " if $rpath ne "";
853e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_, $rpath;
854e71b7053SJung-uk Kim                        }
855e71b7053SJung-uk Kim                elsif (/^-static$/)
856e71b7053SJung-uk Kim                        {
857e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_;
858e71b7053SJung-uk Kim                        }
859e71b7053SJung-uk Kim                elsif (/^-D(.*)$/)
860e71b7053SJung-uk Kim                        {
861e71b7053SJung-uk Kim                        push @{$useradd{CPPDEFINES}}, $1;
862e71b7053SJung-uk Kim                        }
863e71b7053SJung-uk Kim                elsif (/^-I(.*)$/)
864e71b7053SJung-uk Kim                        {
865e71b7053SJung-uk Kim                        push @{$useradd{CPPINCLUDES}}, $1;
866e71b7053SJung-uk Kim                        }
867e71b7053SJung-uk Kim                elsif (/^-Wp,$/)
868e71b7053SJung-uk Kim                        {
869e71b7053SJung-uk Kim                        push @{$useradd{CPPFLAGS}}, $1;
8707bded2dbSJung-uk Kim                        }
8717bded2dbSJung-uk Kim                else    # common if (/^[-+]/), just pass down...
8727bded2dbSJung-uk Kim                        {
8737bded2dbSJung-uk Kim                        $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
874e71b7053SJung-uk Kim                        push @{$useradd{CFLAGS}}, $_;
875e71b7053SJung-uk Kim                        push @{$useradd{CXXFLAGS}}, $_;
87674664626SKris Kennaway                        }
87774664626SKris Kennaway                }
87874664626SKris Kennaway        else
87974664626SKris Kennaway                {
8803b4e3dcbSSimon L. B. Nielsen                die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
88174664626SKris Kennaway                $target=$_;
88274664626SKris Kennaway                }
8833b4e3dcbSSimon L. B. Nielsen        unless ($_ eq $target || /^no-/ || /^disable-/)
8845c87c606SMark Murray                {
885e71b7053SJung-uk Kim                # "no-..." follows later after implied deactivations
886e71b7053SJung-uk Kim                # have been derived.  (Don't take this too seriously,
8873b4e3dcbSSimon L. B. Nielsen                # we really only write OPTIONS to the Makefile out of
8883b4e3dcbSSimon L. B. Nielsen                # nostalgia.)
8893b4e3dcbSSimon L. B. Nielsen
890e71b7053SJung-uk Kim                if ($config{options} eq "")
891e71b7053SJung-uk Kim                        { $config{options} = $_; }
8923b4e3dcbSSimon L. B. Nielsen                else
893e71b7053SJung-uk Kim                        { $config{options} .= " ".$_; }
8943b4e3dcbSSimon L. B. Nielsen                }
8955c87c606SMark Murray        }
8965c87c606SMark Murray
897e71b7053SJung-uk Kimif (defined($config{api}) && !exists $apitable->{$config{api}}) {
898e71b7053SJung-uk Kim        die "***** Unsupported api compatibility level: $config{api}\n",
899e71b7053SJung-uk Kim}
9003b4e3dcbSSimon L. B. Nielsen
901e71b7053SJung-uk Kimif (keys %deprecated_options)
9025c87c606SMark Murray        {
903e71b7053SJung-uk Kim        warn "***** Deprecated options: ",
904e71b7053SJung-uk Kim                join(", ", keys %deprecated_options), "\n";
905e71b7053SJung-uk Kim        }
906e71b7053SJung-uk Kimif (keys %unsupported_options)
907e71b7053SJung-uk Kim        {
908e71b7053SJung-uk Kim        die "***** Unsupported options: ",
909e71b7053SJung-uk Kim                join(", ", keys %unsupported_options), "\n";
9105c87c606SMark Murray        }
9115c87c606SMark Murray
912e71b7053SJung-uk Kim# If any %useradd entry has been set, we must check that the "make
913e71b7053SJung-uk Kim# variables" haven't been set.  We start by checking of any %useradd entry
914e71b7053SJung-uk Kim# is set.
915e71b7053SJung-uk Kimif (grep { scalar @$_ > 0 } values %useradd) {
916e71b7053SJung-uk Kim    # Hash of env / make variables names.  The possible values are:
917e71b7053SJung-uk Kim    # 1 - "make vars"
918e71b7053SJung-uk Kim    # 2 - %useradd entry set
919e71b7053SJung-uk Kim    # 3 - both set
920e71b7053SJung-uk Kim    my %detected_vars =
921e71b7053SJung-uk Kim        map { my $v = 0;
922e71b7053SJung-uk Kim              $v += 1 if $cmdvars{$_};
923e71b7053SJung-uk Kim              $v += 2 if @{$useradd{$_}};
924e71b7053SJung-uk Kim              $_ => $v }
925e71b7053SJung-uk Kim        keys %useradd;
926e71b7053SJung-uk Kim
927e71b7053SJung-uk Kim    # If any of the corresponding "make variables" is set, we error
928e71b7053SJung-uk Kim    if (grep { $_ & 1 } values %detected_vars) {
929e71b7053SJung-uk Kim        my $names = join(', ', grep { $detected_vars{$_} > 0 }
930e71b7053SJung-uk Kim                               sort keys %detected_vars);
931e71b7053SJung-uk Kim        die <<"_____";
932e71b7053SJung-uk Kim***** Mixing make variables and additional compiler/linker flags as
933e71b7053SJung-uk Kim***** configure command line option is not permitted.
934e71b7053SJung-uk Kim***** Affected make variables: $names
935e71b7053SJung-uk Kim_____
936e71b7053SJung-uk Kim    }
9375c87c606SMark Murray}
9385c87c606SMark Murray
939e71b7053SJung-uk Kim# Check through all supported command line variables to see if any of them
940e71b7053SJung-uk Kim# were set, and canonicalise the values we got.  If no compiler or linker
941e71b7053SJung-uk Kim# flag or anything else that affects %useradd was set, we also check the
942e71b7053SJung-uk Kim# environment for values.
943e71b7053SJung-uk Kimmy $anyuseradd =
944e71b7053SJung-uk Kim    grep { defined $_ && (ref $_ ne 'ARRAY' || @$_) } values %useradd;
945e71b7053SJung-uk Kimforeach (keys %user) {
946e71b7053SJung-uk Kim    my $value = $cmdvars{$_};
947e71b7053SJung-uk Kim    $value //= env($_) unless $anyuseradd;
948e71b7053SJung-uk Kim    $value //=
949e71b7053SJung-uk Kim        defined $user_synonyms{$_} ? $cmdvars{$user_synonyms{$_}} : undef;
950e71b7053SJung-uk Kim    $value //= defined $user_synonyms{$_} ? env($user_synonyms{$_}) : undef
951e71b7053SJung-uk Kim        unless $anyuseradd;
952e71b7053SJung-uk Kim
953e71b7053SJung-uk Kim    if (defined $value) {
954e71b7053SJung-uk Kim        if (ref $user{$_} eq 'ARRAY') {
955e71b7053SJung-uk Kim            $user{$_} = [ split /$list_separator_re/, $value ];
956e71b7053SJung-uk Kim        } elsif (!defined $user{$_}) {
957e71b7053SJung-uk Kim            $user{$_} = $value;
958e71b7053SJung-uk Kim        }
959e71b7053SJung-uk Kim    }
9603b4e3dcbSSimon L. B. Nielsen}
9613b4e3dcbSSimon L. B. Nielsen
962e71b7053SJung-uk Kimif (grep { /-rpath\b/ } ($user{LDFLAGS} ? @{$user{LDFLAGS}} : ())
963e71b7053SJung-uk Kim    && !$disabled{shared}
964e71b7053SJung-uk Kim    && !($disabled{asan} && $disabled{msan} && $disabled{ubsan})) {
965e71b7053SJung-uk Kim    die "***** Cannot simultaneously use -rpath, shared libraries, and\n",
966e71b7053SJung-uk Kim        "***** any of asan, msan or ubsan\n";
9673b4e3dcbSSimon L. B. Nielsen}
9683b4e3dcbSSimon L. B. Nielsen
969*610a21fdSJung-uk Kimsub disable {
970*610a21fdSJung-uk Kim    my $disable_type = shift;
971*610a21fdSJung-uk Kim
972*610a21fdSJung-uk Kim    for (@_) {
973*610a21fdSJung-uk Kim        $disabled{$_} = $disable_type;
974*610a21fdSJung-uk Kim    }
975*610a21fdSJung-uk Kim
976*610a21fdSJung-uk Kim    my @tocheckfor = (@_ ? @_ : keys %disabled);
977e71b7053SJung-uk Kim    while (@tocheckfor) {
978e71b7053SJung-uk Kim        my %new_tocheckfor = ();
979e71b7053SJung-uk Kim        my @cascade_copy = (@disable_cascades);
980e71b7053SJung-uk Kim        while (@cascade_copy) {
981*610a21fdSJung-uk Kim            my ($test, $descendents) =
982*610a21fdSJung-uk Kim                (shift @cascade_copy, shift @cascade_copy);
983e71b7053SJung-uk Kim            if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
984e71b7053SJung-uk Kim                foreach (grep { !defined($disabled{$_}) } @$descendents) {
985*610a21fdSJung-uk Kim                    $new_tocheckfor{$_} = 1; $disabled{$_} = "cascade";
986e71b7053SJung-uk Kim                }
987e71b7053SJung-uk Kim            }
988e71b7053SJung-uk Kim        }
989e71b7053SJung-uk Kim        @tocheckfor = (keys %new_tocheckfor);
9903b4e3dcbSSimon L. B. Nielsen    }
991*610a21fdSJung-uk Kim}
992*610a21fdSJung-uk Kimdisable();                     # First cascade run
9933b4e3dcbSSimon L. B. Nielsen
994e71b7053SJung-uk Kimour $die = sub { die @_; };
99574664626SKris Kennawayif ($target eq "TABLE") {
996e71b7053SJung-uk Kim    local $die = sub { warn @_; };
997e71b7053SJung-uk Kim    foreach (sort keys %table) {
998e71b7053SJung-uk Kim        print_table_entry($_, "TABLE");
99974664626SKris Kennaway    }
100074664626SKris Kennaway    exit 0;
100174664626SKris Kennaway}
100274664626SKris Kennaway
1003f579bf8eSKris Kennawayif ($target eq "LIST") {
1004f579bf8eSKris Kennaway    foreach (sort keys %table) {
1005e71b7053SJung-uk Kim        print $_,"\n" unless $table{$_}->{template};
1006f579bf8eSKris Kennaway    }
1007f579bf8eSKris Kennaway    exit 0;
1008f579bf8eSKris Kennaway}
1009f579bf8eSKris Kennaway
1010e71b7053SJung-uk Kimif ($target eq "HASH") {
1011e71b7053SJung-uk Kim    local $die = sub { warn @_; };
1012e71b7053SJung-uk Kim    print "%table = (\n";
1013e71b7053SJung-uk Kim    foreach (sort keys %table) {
1014e71b7053SJung-uk Kim        print_table_entry($_, "HASH");
1015e71b7053SJung-uk Kim    }
1016e71b7053SJung-uk Kim    exit 0;
1017e71b7053SJung-uk Kim}
1018e71b7053SJung-uk Kim
1019e71b7053SJung-uk Kimprint "Configuring OpenSSL version $config{version} ($config{version_num}) ";
1020e71b7053SJung-uk Kimprint "for $target\n";
1021e71b7053SJung-uk Kim
1022e71b7053SJung-uk Kimif (scalar(@seed_sources) == 0) {
1023e71b7053SJung-uk Kim    print "Using os-specific seed configuration\n";
1024e71b7053SJung-uk Kim    push @seed_sources, 'os';
1025e71b7053SJung-uk Kim}
1026e71b7053SJung-uk Kimif (scalar(grep { $_ eq 'none' } @seed_sources) > 0) {
1027e71b7053SJung-uk Kim    die "Cannot seed with none and anything else" if scalar(@seed_sources) > 1;
1028e71b7053SJung-uk Kim    warn <<_____ if scalar(@seed_sources) == 1;
1029e71b7053SJung-uk Kim
1030c9cf7b5cSJung-uk Kim============================== WARNING ===============================
1031c9cf7b5cSJung-uk KimYou have selected the --with-rand-seed=none option, which effectively
1032c9cf7b5cSJung-uk Kimdisables automatic reseeding of the OpenSSL random generator.
1033c9cf7b5cSJung-uk KimAll operations depending on the random generator such as creating keys
1034c9cf7b5cSJung-uk Kimwill not work unless the random generator is seeded manually by the
1035c9cf7b5cSJung-uk Kimapplication.
1036c9cf7b5cSJung-uk Kim
1037c9cf7b5cSJung-uk KimPlease read the 'Note on random number generation' section in the
1038c9cf7b5cSJung-uk KimINSTALL instructions and the RAND_DRBG(7) manual page for more details.
1039c9cf7b5cSJung-uk Kim============================== WARNING ===============================
1040c9cf7b5cSJung-uk Kim
1041e71b7053SJung-uk Kim_____
1042e71b7053SJung-uk Kim}
1043e71b7053SJung-uk Kimpush @{$config{openssl_other_defines}},
1044e71b7053SJung-uk Kim     map { (my $x = $_) =~ tr|[\-a-z]|[_A-Z]|; "OPENSSL_RAND_SEED_$x" }
1045e71b7053SJung-uk Kim        @seed_sources;
1046e71b7053SJung-uk Kim
1047e71b7053SJung-uk Kim# Backward compatibility?
1048c1803d78SJacques Vidrineif ($target =~ m/^CygWin32(-.*)$/) {
1049c1803d78SJacques Vidrine    $target = "Cygwin".$1;
1050c1803d78SJacques Vidrine}
1051c1803d78SJacques Vidrine
1052e71b7053SJung-uk Kim# Support for legacy targets having a name starting with 'debug-'
1053e71b7053SJung-uk Kimmy ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
1054e71b7053SJung-uk Kimif ($d) {
1055e71b7053SJung-uk Kim    $config{build_type} = "debug";
1056ddd58736SKris Kennaway
1057e71b7053SJung-uk Kim    # If we do not find debug-foo in the table, the target is set to foo.
1058e71b7053SJung-uk Kim    if (!$table{$target}) {
1059e71b7053SJung-uk Kim        $target = $t;
10601f13597dSJung-uk Kim    }
1061e71b7053SJung-uk Kim}
10621f13597dSJung-uk Kim
1063e71b7053SJung-uk Kim&usage if !$table{$target} || $table{$target}->{template};
1064e71b7053SJung-uk Kim
1065e71b7053SJung-uk Kim$config{target} = $target;
1066e71b7053SJung-uk Kimmy %target = resolve_config($target);
1067e71b7053SJung-uk Kim
1068e71b7053SJung-uk Kimforeach (keys %target_attr_translate) {
1069e71b7053SJung-uk Kim    $target{$target_attr_translate{$_}} = $target{$_}
1070e71b7053SJung-uk Kim        if $target{$_};
1071e71b7053SJung-uk Kim    delete $target{$_};
1072e71b7053SJung-uk Kim}
1073e71b7053SJung-uk Kim
1074e71b7053SJung-uk Kim%target = ( %{$table{DEFAULTS}}, %target );
1075e71b7053SJung-uk Kim
1076e71b7053SJung-uk Kimmy %conf_files = map { $_ => 1 } (@{$target{_conf_fname_int}});
1077e71b7053SJung-uk Kim$config{conf_files} = [ sort keys %conf_files ];
1078e71b7053SJung-uk Kim
1079*610a21fdSJung-uk Kim# Using sub disable within these loops may prove fragile, so we run
1080*610a21fdSJung-uk Kim# a cascade afterwards
1081e71b7053SJung-uk Kimforeach my $feature (@{$target{disable}}) {
1082e71b7053SJung-uk Kim    if (exists $deprecated_disablables{$feature}) {
1083e71b7053SJung-uk Kim        warn "***** config $target disables deprecated feature $feature\n";
1084e71b7053SJung-uk Kim    } elsif (!grep { $feature eq $_ } @disablables) {
1085e71b7053SJung-uk Kim        die "***** config $target disables unknown feature $feature\n";
1086e71b7053SJung-uk Kim    }
1087e71b7053SJung-uk Kim    $disabled{$feature} = 'config';
1088e71b7053SJung-uk Kim}
1089e71b7053SJung-uk Kimforeach my $feature (@{$target{enable}}) {
10906935a639SJung-uk Kim    if ("default" eq ($disabled{$feature} // "")) {
1091e71b7053SJung-uk Kim        if (exists $deprecated_disablables{$feature}) {
1092e71b7053SJung-uk Kim            warn "***** config $target enables deprecated feature $feature\n";
1093e71b7053SJung-uk Kim        } elsif (!grep { $feature eq $_ } @disablables) {
1094e71b7053SJung-uk Kim            die "***** config $target enables unknown feature $feature\n";
1095e71b7053SJung-uk Kim        }
10966935a639SJung-uk Kim        delete $disabled{$feature};
1097e71b7053SJung-uk Kim    }
1098e71b7053SJung-uk Kim}
1099*610a21fdSJung-uk Kimdisable();                      # Run a cascade now
1100e71b7053SJung-uk Kim
1101e71b7053SJung-uk Kim$target{CXXFLAGS}//=$target{CFLAGS} if $target{CXX};
1102e71b7053SJung-uk Kim$target{cxxflags}//=$target{cflags} if $target{CXX};
1103e71b7053SJung-uk Kim$target{exe_extension}="";
1104e71b7053SJung-uk Kim$target{exe_extension}=".exe" if ($config{target} eq "DJGPP"
1105e71b7053SJung-uk Kim                                  || $config{target} =~ /^(?:Cygwin|mingw)/);
1106e71b7053SJung-uk Kim$target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
1107e71b7053SJung-uk Kim
1108e71b7053SJung-uk Kim($target{shared_extension_simple}=$target{shared_extension})
1109e71b7053SJung-uk Kim    =~ s|\.\$\(SHLIB_VERSION_NUMBER\)||
1110e71b7053SJung-uk Kim    unless defined($target{shared_extension_simple});
1111e71b7053SJung-uk Kim$target{dso_extension}//=$target{shared_extension_simple};
1112e71b7053SJung-uk Kim($target{shared_import_extension}=$target{shared_extension_simple}.".a")
1113e71b7053SJung-uk Kim    if ($config{target} =~ /^(?:Cygwin|mingw)/);
1114e71b7053SJung-uk Kim
1115e71b7053SJung-uk Kim# Fill %config with values from %user, and in case those are undefined or
1116e71b7053SJung-uk Kim# empty, use values from %target (acting as a default).
1117e71b7053SJung-uk Kimforeach (keys %user) {
1118e71b7053SJung-uk Kim    my $ref_type = ref $user{$_};
1119e71b7053SJung-uk Kim
1120e71b7053SJung-uk Kim    # Temporary function.  Takes an intended ref type (empty string or "ARRAY")
1121e71b7053SJung-uk Kim    # and a value that's to be coerced into that type.
1122e71b7053SJung-uk Kim    my $mkvalue = sub {
1123e71b7053SJung-uk Kim        my $type = shift;
1124e71b7053SJung-uk Kim        my $value = shift;
1125e71b7053SJung-uk Kim        my $undef_p = shift;
1126e71b7053SJung-uk Kim
1127e71b7053SJung-uk Kim        die "Too many arguments for \$mkvalue" if @_;
1128e71b7053SJung-uk Kim
1129e71b7053SJung-uk Kim        while (ref $value eq 'CODE') {
1130e71b7053SJung-uk Kim            $value = $value->();
1131e71b7053SJung-uk Kim        }
1132e71b7053SJung-uk Kim
1133e71b7053SJung-uk Kim        if ($type eq 'ARRAY') {
1134e71b7053SJung-uk Kim            return undef unless defined $value;
1135e71b7053SJung-uk Kim            return undef if ref $value ne 'ARRAY' && !$value;
1136e71b7053SJung-uk Kim            return undef if ref $value eq 'ARRAY' && !@$value;
1137e71b7053SJung-uk Kim            return [ $value ] unless ref $value eq 'ARRAY';
1138e71b7053SJung-uk Kim        }
1139e71b7053SJung-uk Kim        return undef unless $value;
1140e71b7053SJung-uk Kim        return $value;
1141e71b7053SJung-uk Kim    };
1142e71b7053SJung-uk Kim
1143e71b7053SJung-uk Kim    $config{$_} =
1144e71b7053SJung-uk Kim        $mkvalue->($ref_type, $user{$_})
1145e71b7053SJung-uk Kim        || $mkvalue->($ref_type, $target{$_});
1146e71b7053SJung-uk Kim    delete $config{$_} unless defined $config{$_};
1147e71b7053SJung-uk Kim}
1148e71b7053SJung-uk Kim
1149*610a21fdSJung-uk Kim# Finish up %config by appending things the user gave us on the command line
1150*610a21fdSJung-uk Kim# apart from "make variables"
1151*610a21fdSJung-uk Kimforeach (keys %useradd) {
1152*610a21fdSJung-uk Kim    # The must all be lists, so we assert that here
1153*610a21fdSJung-uk Kim    die "internal error: \$useradd{$_} isn't an ARRAY\n"
1154*610a21fdSJung-uk Kim        unless ref $useradd{$_} eq 'ARRAY';
1155*610a21fdSJung-uk Kim
1156*610a21fdSJung-uk Kim    if (defined $config{$_}) {
1157*610a21fdSJung-uk Kim        push @{$config{$_}}, @{$useradd{$_}};
1158*610a21fdSJung-uk Kim    } else {
1159*610a21fdSJung-uk Kim        $config{$_} = [ @{$useradd{$_}} ];
1160*610a21fdSJung-uk Kim    }
1161*610a21fdSJung-uk Kim}
1162*610a21fdSJung-uk Kim# At this point, we can forget everything about %user and %useradd,
1163*610a21fdSJung-uk Kim# because it's now all been merged into the corresponding $config entry
1164*610a21fdSJung-uk Kim
1165e71b7053SJung-uk Kim# Allow overriding the build file name
1166e71b7053SJung-uk Kim$config{build_file} = env('BUILDFILE') || $target{build_file} || "Makefile";
1167e71b7053SJung-uk Kim
1168e71b7053SJung-uk Kimmy %disabled_info = ();         # For configdata.pm
1169e71b7053SJung-uk Kimforeach my $what (sort keys %disabled) {
1170e71b7053SJung-uk Kim    $config{options} .= " no-$what";
1171e71b7053SJung-uk Kim
1172*610a21fdSJung-uk Kim    if (!grep { $what eq $_ } ( 'buildtest-c++', 'threads', 'shared', 'pic',
1173e71b7053SJung-uk Kim                                'dynamic-engine', 'makedepend',
1174e71b7053SJung-uk Kim                                'zlib-dynamic', 'zlib', 'sse2' )) {
1175e71b7053SJung-uk Kim        (my $WHAT = uc $what) =~ s|-|_|g;
1176e71b7053SJung-uk Kim
1177e71b7053SJung-uk Kim        # Fix up C macro end names
1178e71b7053SJung-uk Kim        $WHAT = "RMD160" if $what eq "ripemd";
1179e71b7053SJung-uk Kim
11801f13597dSJung-uk Kim        # fix-up crypto/directory name(s)
1181e71b7053SJung-uk Kim        $what = "ripemd" if $what eq "rmd160";
1182e71b7053SJung-uk Kim        $what = "whrlpool" if $what eq "whirlpool";
11831f13597dSJung-uk Kim
1184e71b7053SJung-uk Kim        my $macro = $disabled_info{$what}->{macro} = "OPENSSL_NO_$WHAT";
1185e71b7053SJung-uk Kim
1186e71b7053SJung-uk Kim        if ((grep { $what eq $_ } @{$config{sdirs}})
1187e71b7053SJung-uk Kim                && $what ne 'async' && $what ne 'err') {
1188e71b7053SJung-uk Kim            @{$config{sdirs}} = grep { $what ne $_} @{$config{sdirs}};
1189e71b7053SJung-uk Kim            $disabled_info{$what}->{skipped} = [ catdir('crypto', $what) ];
1190e71b7053SJung-uk Kim
1191e71b7053SJung-uk Kim            if ($what ne 'engine') {
1192e71b7053SJung-uk Kim                push @{$config{openssl_algorithm_defines}}, $macro;
1193e71b7053SJung-uk Kim            } else {
1194e71b7053SJung-uk Kim                @{$config{dirs}} = grep !/^engines$/, @{$config{dirs}};
1195e71b7053SJung-uk Kim                push @{$disabled_info{engine}->{skipped}}, catdir('engines');
1196e71b7053SJung-uk Kim                push @{$config{openssl_other_defines}}, $macro;
11971f13597dSJung-uk Kim            }
1198e71b7053SJung-uk Kim        } else {
1199e71b7053SJung-uk Kim            push @{$config{openssl_other_defines}}, $macro;
12001f13597dSJung-uk Kim        }
12011f13597dSJung-uk Kim
1202e71b7053SJung-uk Kim    }
12031f13597dSJung-uk Kim}
12041f13597dSJung-uk Kim
1205e71b7053SJung-uk Kim# Make sure build_scheme is consistent.
1206e71b7053SJung-uk Kim$target{build_scheme} = [ $target{build_scheme} ]
1207e71b7053SJung-uk Kim    if ref($target{build_scheme}) ne "ARRAY";
1208e71b7053SJung-uk Kim
1209e71b7053SJung-uk Kimmy ($builder, $builder_platform, @builder_opts) =
1210e71b7053SJung-uk Kim    @{$target{build_scheme}};
1211e71b7053SJung-uk Kim
1212e71b7053SJung-uk Kimforeach my $checker (($builder_platform."-".$target{build_file}."-checker.pm",
1213e71b7053SJung-uk Kim                      $builder_platform."-checker.pm")) {
1214e71b7053SJung-uk Kim    my $checker_path = catfile($srcdir, "Configurations", $checker);
1215e71b7053SJung-uk Kim    if (-f $checker_path) {
1216e71b7053SJung-uk Kim        my $fn = $ENV{CONFIGURE_CHECKER_WARN}
1217e71b7053SJung-uk Kim            ? sub { warn $@; } : sub { die $@; };
1218e71b7053SJung-uk Kim        if (! do $checker_path) {
1219e71b7053SJung-uk Kim            if ($@) {
1220e71b7053SJung-uk Kim                $fn->($@);
1221e71b7053SJung-uk Kim            } elsif ($!) {
1222e71b7053SJung-uk Kim                $fn->($!);
1223e71b7053SJung-uk Kim            } else {
1224e71b7053SJung-uk Kim                $fn->("The detected tools didn't match the platform\n");
1225e71b7053SJung-uk Kim            }
1226e71b7053SJung-uk Kim        }
1227e71b7053SJung-uk Kim        last;
1228e71b7053SJung-uk Kim    }
1229e71b7053SJung-uk Kim}
1230e71b7053SJung-uk Kim
1231e71b7053SJung-uk Kimpush @{$config{defines}}, "NDEBUG"    if $config{build_type} eq "release";
1232e71b7053SJung-uk Kim
1233e71b7053SJung-uk Kimif ($target =~ /^mingw/ && `$config{CC} --target-help 2>&1` =~ m/-mno-cygwin/m)
12341f13597dSJung-uk Kim        {
1235e71b7053SJung-uk Kim        push @{$config{cflags}}, "-mno-cygwin";
1236e71b7053SJung-uk Kim        push @{$config{cxxflags}}, "-mno-cygwin" if $config{CXX};
1237e71b7053SJung-uk Kim        push @{$config{shared_ldflag}}, "-mno-cygwin";
12381f13597dSJung-uk Kim        }
12391f13597dSJung-uk Kim
1240e71b7053SJung-uk Kimif ($target =~ /linux.*-mips/ && !$disabled{asm}
1241*610a21fdSJung-uk Kim        && !grep { $_ !~ /-m(ips|arch=)/ } (@{$config{CFLAGS}})) {
12427bded2dbSJung-uk Kim        # minimally required architecture flags for assembly modules
1243e71b7053SJung-uk Kim        my $value;
1244e71b7053SJung-uk Kim        $value = '-mips2' if ($target =~ /mips32/);
1245e71b7053SJung-uk Kim        $value = '-mips3' if ($target =~ /mips64/);
1246e71b7053SJung-uk Kim        unshift @{$config{cflags}}, $value;
1247e71b7053SJung-uk Kim        unshift @{$config{cxxflags}}, $value if $config{CXX};
1248e71b7053SJung-uk Kim}
1249e71b7053SJung-uk Kim
1250e71b7053SJung-uk Kim# If threads aren't disabled, check how possible they are
1251e71b7053SJung-uk Kimunless ($disabled{threads}) {
1252e71b7053SJung-uk Kim    if ($auto_threads) {
1253e71b7053SJung-uk Kim        # Enabled by default, disable it forcibly if unavailable
1254e71b7053SJung-uk Kim        if ($target{thread_scheme} eq "(unknown)") {
1255*610a21fdSJung-uk Kim            disable("unavailable", 'threads');
1256e71b7053SJung-uk Kim        }
1257e71b7053SJung-uk Kim    } else {
1258e71b7053SJung-uk Kim        # The user chose to enable threads explicitly, let's see
1259e71b7053SJung-uk Kim        # if there's a chance that's possible
1260e71b7053SJung-uk Kim        if ($target{thread_scheme} eq "(unknown)") {
1261e71b7053SJung-uk Kim            # If the user asked for "threads" and we don't have internal
1262e71b7053SJung-uk Kim            # knowledge how to do it, [s]he is expected to provide any
1263e71b7053SJung-uk Kim            # system-dependent compiler options that are necessary.  We
1264e71b7053SJung-uk Kim            # can't truly check that the given options are correct, but
1265e71b7053SJung-uk Kim            # we expect the user to know what [s]He is doing.
1266*610a21fdSJung-uk Kim            if (!@{$config{CFLAGS}} && !@{$config{CPPDEFINES}}) {
1267e71b7053SJung-uk Kim                die "You asked for multi-threading support, but didn't\n"
1268e71b7053SJung-uk Kim                    ,"provide any system-specific compiler options\n";
1269e71b7053SJung-uk Kim            }
1270e71b7053SJung-uk Kim        }
1271e71b7053SJung-uk Kim    }
1272e71b7053SJung-uk Kim}
1273e71b7053SJung-uk Kim
1274e71b7053SJung-uk Kim# If threads still aren't disabled, add a C macro to ensure the source
1275e71b7053SJung-uk Kim# code knows about it.  Any other flag is taken care of by the configs.
1276e71b7053SJung-uk Kimunless($disabled{threads}) {
1277e71b7053SJung-uk Kim    push @{$config{openssl_thread_defines}}, "OPENSSL_THREADS";
1278e71b7053SJung-uk Kim}
1279e71b7053SJung-uk Kim
1280e71b7053SJung-uk Kim# With "deprecated" disable all deprecated features.
1281e71b7053SJung-uk Kimif (defined($disabled{"deprecated"})) {
1282e71b7053SJung-uk Kim        $config{api} = $maxapi;
12837bded2dbSJung-uk Kim}
12847bded2dbSJung-uk Kim
1285fceca8a3SJacques Vidrinemy $no_shared_warn=0;
1286e71b7053SJung-uk Kimif ($target{shared_target} eq "")
12875c87c606SMark Murray        {
1288e71b7053SJung-uk Kim        $no_shared_warn = 1
1289e71b7053SJung-uk Kim            if (!$disabled{shared} || !$disabled{"dynamic-engine"});
1290*610a21fdSJung-uk Kim        disable('no-shared-target', 'pic');
12915c87c606SMark Murray        }
12925c87c606SMark Murray
1293e71b7053SJung-uk Kimif ($disabled{"dynamic-engine"}) {
1294e71b7053SJung-uk Kim        push @{$config{openssl_other_defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
1295e71b7053SJung-uk Kim        $config{dynamic_engines} = 0;
1296e71b7053SJung-uk Kim} else {
1297e71b7053SJung-uk Kim        push @{$config{openssl_other_defines}}, "OPENSSL_NO_STATIC_ENGINE";
1298e71b7053SJung-uk Kim        $config{dynamic_engines} = 1;
1299ddd58736SKris Kennaway}
1300ddd58736SKris Kennaway
1301e71b7053SJung-uk Kimunless ($disabled{asan}) {
1302e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fsanitize=address";
130374664626SKris Kennaway}
130474664626SKris Kennaway
1305e71b7053SJung-uk Kimunless ($disabled{ubsan}) {
1306e71b7053SJung-uk Kim    # -DPEDANTIC or -fnosanitize=alignment may also be required on some
1307e71b7053SJung-uk Kim    # platforms.
1308e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fsanitize=undefined", "-fno-sanitize-recover=all";
130974664626SKris Kennaway}
131074664626SKris Kennaway
1311e71b7053SJung-uk Kimunless ($disabled{msan}) {
1312e71b7053SJung-uk Kim  push @{$config{cflags}}, "-fsanitize=memory";
13135c87c606SMark Murray}
13145c87c606SMark Murray
1315e71b7053SJung-uk Kimunless ($disabled{"fuzz-libfuzzer"} && $disabled{"fuzz-afl"}
1316e71b7053SJung-uk Kim        && $disabled{asan} && $disabled{ubsan} && $disabled{msan}) {
1317e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fno-omit-frame-pointer", "-g";
1318e71b7053SJung-uk Kim    push @{$config{cxxflags}}, "-fno-omit-frame-pointer", "-g" if $config{CXX};
131974664626SKris Kennaway}
1320db522d3aSSimon L. B. Nielsen#
1321db522d3aSSimon L. B. Nielsen# Platform fix-ups
1322db522d3aSSimon L. B. Nielsen#
1323db522d3aSSimon L. B. Nielsen
1324e71b7053SJung-uk Kim# This saves the build files from having to check
1325e71b7053SJung-uk Kimif ($disabled{pic})
1326db522d3aSSimon L. B. Nielsen        {
1327e71b7053SJung-uk Kim        foreach (qw(shared_cflag shared_cxxflag shared_cppflag
1328e71b7053SJung-uk Kim                    shared_defines shared_includes shared_ldflag
1329e71b7053SJung-uk Kim                    module_cflags module_cxxflags module_cppflags
1330e71b7053SJung-uk Kim                    module_defines module_includes module_lflags))
1331e71b7053SJung-uk Kim                {
1332e71b7053SJung-uk Kim                delete $config{$_};
1333e71b7053SJung-uk Kim                $target{$_} = "";
1334ddd58736SKris Kennaway                }
1335aeb5019cSJung-uk Kim        }
1336aeb5019cSJung-uk Kimelse
1337aeb5019cSJung-uk Kim        {
1338e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "OPENSSL_PIC";
1339aeb5019cSJung-uk Kim        }
1340e71b7053SJung-uk Kim
1341e71b7053SJung-uk Kimif ($target{sys_id} ne "")
134274664626SKris Kennaway        {
1343e71b7053SJung-uk Kim        push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
13443b4e3dcbSSimon L. B. Nielsen        }
1345e71b7053SJung-uk Kim
1346e71b7053SJung-uk Kimunless ($disabled{asm}) {
1347e71b7053SJung-uk Kim    $target{cpuid_asm_src}=$table{DEFAULTS}->{cpuid_asm_src} if ($config{processor} eq "386");
1348e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_CPUID_OBJ" if ($target{cpuid_asm_src} ne "mem_clr.c");
1349e71b7053SJung-uk Kim
1350e71b7053SJung-uk Kim    $target{bn_asm_src} =~ s/\w+-gf2m.c// if (defined($disabled{ec2m}));
1351e71b7053SJung-uk Kim
1352e71b7053SJung-uk Kim    # bn-586 is the only one implementing bn_*_part_words
1353e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_PART_WORDS" if ($target{bn_asm_src} =~ /bn-586/);
1354e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_IA32_SSE2" if (!$disabled{sse2} && $target{bn_asm_src} =~ /86/);
1355e71b7053SJung-uk Kim
1356e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_MONT" if ($target{bn_asm_src} =~ /-mont/);
1357e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_MONT5" if ($target{bn_asm_src} =~ /-mont5/);
1358e71b7053SJung-uk Kim    push @{$config{lib_defines}}, "OPENSSL_BN_ASM_GF2m" if ($target{bn_asm_src} =~ /-gf2m/);
13596935a639SJung-uk Kim    push @{$config{lib_defines}}, "BN_DIV3W" if ($target{bn_asm_src} =~ /-div3w/);
1360e71b7053SJung-uk Kim
1361e71b7053SJung-uk Kim    if ($target{sha1_asm_src}) {
1362e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "SHA1_ASM"   if ($target{sha1_asm_src} =~ /sx86/ || $target{sha1_asm_src} =~ /sha1/);
1363e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "SHA256_ASM" if ($target{sha1_asm_src} =~ /sha256/);
1364e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "SHA512_ASM" if ($target{sha1_asm_src} =~ /sha512/);
136574664626SKris Kennaway    }
1366e71b7053SJung-uk Kim    if ($target{keccak1600_asm_src} ne $table{DEFAULTS}->{keccak1600_asm_src}) {
1367e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "KECCAK1600_ASM";
136874664626SKris Kennaway    }
1369e71b7053SJung-uk Kim    if ($target{rc4_asm_src} ne $table{DEFAULTS}->{rc4_asm_src}) {
1370e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "RC4_ASM";
137174664626SKris Kennaway    }
1372e71b7053SJung-uk Kim    if ($target{md5_asm_src}) {
1373e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "MD5_ASM";
1374e71b7053SJung-uk Kim    }
1375e71b7053SJung-uk Kim    $target{cast_asm_src}=$table{DEFAULTS}->{cast_asm_src} unless $disabled{pic}; # CAST assembler is not PIC
1376e71b7053SJung-uk Kim    if ($target{rmd160_asm_src}) {
1377e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "RMD160_ASM";
1378e71b7053SJung-uk Kim    }
1379e71b7053SJung-uk Kim    if ($target{aes_asm_src}) {
1380e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "AES_ASM" if ($target{aes_asm_src} =~ m/\baes-/);;
1381e71b7053SJung-uk Kim        # aes-ctr.fake is not a real file, only indication that assembler
13821f13597dSJung-uk Kim        # module implements AES_ctr32_encrypt...
1383e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "AES_CTR_ASM" if ($target{aes_asm_src} =~ s/\s*aes-ctr\.fake//);
1384e71b7053SJung-uk Kim        # aes-xts.fake indicates presence of AES_xts_[en|de]crypt...
1385e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "AES_XTS_ASM" if ($target{aes_asm_src} =~ s/\s*aes-xts\.fake//);
1386e71b7053SJung-uk Kim        $target{aes_asm_src} =~ s/\s*(vpaes|aesni)-x86\.s//g if ($disabled{sse2});
1387e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "VPAES_ASM" if ($target{aes_asm_src} =~ m/vpaes/);
1388e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "BSAES_ASM" if ($target{aes_asm_src} =~ m/bsaes/);
13893b4e3dcbSSimon L. B. Nielsen    }
1390e71b7053SJung-uk Kim    if ($target{wp_asm_src} =~ /mmx/) {
1391e71b7053SJung-uk Kim        if ($config{processor} eq "386") {
1392e71b7053SJung-uk Kim            $target{wp_asm_src}=$table{DEFAULTS}->{wp_asm_src};
1393e71b7053SJung-uk Kim        } elsif (!$disabled{"whirlpool"}) {
1394e71b7053SJung-uk Kim            push @{$config{lib_defines}}, "WHIRLPOOL_ASM";
13956be8ae07SJacques Vidrine        }
13961f13597dSJung-uk Kim    }
1397e71b7053SJung-uk Kim    if ($target{modes_asm_src} =~ /ghash-/) {
1398e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "GHASH_ASM";
13991f13597dSJung-uk Kim    }
1400e71b7053SJung-uk Kim    if ($target{ec_asm_src} =~ /ecp_nistz256/) {
1401e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "ECP_NISTZ256_ASM";
14021f13597dSJung-uk Kim    }
1403e71b7053SJung-uk Kim    if ($target{ec_asm_src} =~ /x25519/) {
1404e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "X25519_ASM";
1405e71b7053SJung-uk Kim    }
1406e71b7053SJung-uk Kim    if ($target{padlock_asm_src} ne $table{DEFAULTS}->{padlock_asm_src}) {
1407*610a21fdSJung-uk Kim        push @{$config{dso_defines}}, "PADLOCK_ASM";
1408e71b7053SJung-uk Kim    }
1409e71b7053SJung-uk Kim    if ($target{poly1305_asm_src} ne "") {
1410e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "POLY1305_ASM";
1411e71b7053SJung-uk Kim    }
14127bded2dbSJung-uk Kim}
14136be8ae07SJacques Vidrine
1414*610a21fdSJung-uk Kimmy %predefined_C = compiler_predefined($config{CROSS_COMPILE}.$config{CC});
1415*610a21fdSJung-uk Kimmy %predefined_CXX = $config{CXX}
1416*610a21fdSJung-uk Kim    ? compiler_predefined($config{CROSS_COMPILE}.$config{CXX})
1417*610a21fdSJung-uk Kim    : ();
14184f20a5a2SJacques Vidrine
1419e71b7053SJung-uk Kim# Check for makedepend capabilities.
1420e71b7053SJung-uk Kimif (!$disabled{makedepend}) {
1421e71b7053SJung-uk Kim    if ($config{target} =~ /^(VC|vms)-/) {
1422e71b7053SJung-uk Kim        # For VC- and vms- targets, there's nothing more to do here.  The
1423e71b7053SJung-uk Kim        # functionality is hard coded in the corresponding build files for
1424e71b7053SJung-uk Kim        # cl (Windows) and CC/DECC (VMS).
1425*610a21fdSJung-uk Kim    } elsif (($predefined_C{__GNUC__} // -1) >= 3
1426*610a21fdSJung-uk Kim             && !($predefined_C{__APPLE_CC__} && !$predefined_C{__clang__})) {
1427e71b7053SJung-uk Kim        # We know that GNU C version 3 and up as well as all clang
1428e71b7053SJung-uk Kim        # versions support dependency generation, but Xcode did not
1429e71b7053SJung-uk Kim        # handle $cc -M before clang support (but claims __GNUC__ = 3)
1430e71b7053SJung-uk Kim        $config{makedepprog} = "\$(CROSS_COMPILE)$config{CC}";
1431e71b7053SJung-uk Kim    } else {
1432e71b7053SJung-uk Kim        # In all other cases, we look for 'makedepend', and disable the
1433e71b7053SJung-uk Kim        # capability if not found.
1434e71b7053SJung-uk Kim        $config{makedepprog} = which('makedepend');
1435*610a21fdSJung-uk Kim        disable('unavailable', 'makedepend') unless $config{makedepprog};
143674664626SKris Kennaway    }
143774664626SKris Kennaway}
143874664626SKris Kennaway
1439*610a21fdSJung-uk Kimif (!$disabled{asm} && !$predefined_C{__MACH__} && $^O ne 'VMS') {
1440e71b7053SJung-uk Kim    # probe for -Wa,--noexecstack option...
1441*610a21fdSJung-uk Kim    if ($predefined_C{__clang__}) {
1442e71b7053SJung-uk Kim        # clang has builtin assembler, which doesn't recognize --help,
1443e71b7053SJung-uk Kim        # but it apparently recognizes the option in question on all
1444e71b7053SJung-uk Kim        # supported platforms even when it's meaningless. In other words
1445e71b7053SJung-uk Kim        # probe would fail, but probed option always accepted...
1446e71b7053SJung-uk Kim        push @{$config{cflags}}, "-Wa,--noexecstack", "-Qunused-arguments";
1447e71b7053SJung-uk Kim    } else {
1448e71b7053SJung-uk Kim        my $cc = $config{CROSS_COMPILE}.$config{CC};
1449e71b7053SJung-uk Kim        open(PIPE, "$cc -Wa,--help -c -o null.$$.o -x assembler /dev/null 2>&1 |");
1450dee36b4fSJung-uk Kim        while(<PIPE>) {
1451e71b7053SJung-uk Kim            if (m/--noexecstack/) {
1452e71b7053SJung-uk Kim                push @{$config{cflags}}, "-Wa,--noexecstack";
1453e71b7053SJung-uk Kim                last;
1454e71b7053SJung-uk Kim            }
1455dee36b4fSJung-uk Kim        }
1456dee36b4fSJung-uk Kim        close(PIPE);
1457e71b7053SJung-uk Kim        unlink("null.$$.o");
1458e71b7053SJung-uk Kim    }
1459e71b7053SJung-uk Kim}
146080815a77SJung-uk Kim
1461e71b7053SJung-uk Kim# Deal with bn_ops ###################################################
1462e71b7053SJung-uk Kim
1463e71b7053SJung-uk Kim$config{bn_ll}                  =0;
1464e71b7053SJung-uk Kim$config{export_var_as_fn}       =0;
1465e71b7053SJung-uk Kimmy $def_int="unsigned int";
1466e71b7053SJung-uk Kim$config{rc4_int}                =$def_int;
1467e71b7053SJung-uk Kim($config{b64l},$config{b64},$config{b32})=(0,0,1);
1468e71b7053SJung-uk Kim
1469e71b7053SJung-uk Kimmy $count = 0;
1470e71b7053SJung-uk Kimforeach (sort split(/\s+/,$target{bn_ops})) {
1471e71b7053SJung-uk Kim    $count++ if /SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT/;
1472e71b7053SJung-uk Kim    $config{export_var_as_fn}=1                 if $_ eq 'EXPORT_VAR_AS_FN';
1473e71b7053SJung-uk Kim    $config{bn_ll}=1                            if $_ eq 'BN_LLONG';
1474e71b7053SJung-uk Kim    $config{rc4_int}="unsigned char"            if $_ eq 'RC4_CHAR';
1475e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1476e71b7053SJung-uk Kim        =(0,1,0)                                if $_ eq 'SIXTY_FOUR_BIT';
1477e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1478e71b7053SJung-uk Kim        =(1,0,0)                                if $_ eq 'SIXTY_FOUR_BIT_LONG';
1479e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1480e71b7053SJung-uk Kim        =(0,0,1)                                if $_ eq 'THIRTY_TWO_BIT';
1481e71b7053SJung-uk Kim}
1482e71b7053SJung-uk Kimdie "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
1483e71b7053SJung-uk Kim    if $count > 1;
1484e71b7053SJung-uk Kim
1485e71b7053SJung-uk Kim
1486e71b7053SJung-uk Kim# Hack cflags for better warnings (dev option) #######################
1487e71b7053SJung-uk Kim
1488e71b7053SJung-uk Kim# "Stringify" the C and C++ flags string.  This permits it to be made part of
1489e71b7053SJung-uk Kim# a string and works as well on command lines.
1490e71b7053SJung-uk Kim$config{cflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1491e71b7053SJung-uk Kim                        @{$config{cflags}} ];
1492e71b7053SJung-uk Kim$config{cxxflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1493e71b7053SJung-uk Kim                          @{$config{cxxflags}} ] if $config{CXX};
1494e71b7053SJung-uk Kim
1495e71b7053SJung-uk Kimif (defined($config{api})) {
1496e71b7053SJung-uk Kim    $config{openssl_api_defines} = [ "OPENSSL_MIN_API=".$apitable->{$config{api}} ];
1497e71b7053SJung-uk Kim    my $apiflag = sprintf("OPENSSL_API_COMPAT=%s", $apitable->{$config{api}});
1498e71b7053SJung-uk Kim    push @{$config{defines}}, $apiflag;
1499dea77ea6SJung-uk Kim}
1500dea77ea6SJung-uk Kim
1501*610a21fdSJung-uk Kimmy @strict_warnings_collection=();
150280815a77SJung-uk Kimif ($strict_warnings)
150380815a77SJung-uk Kim        {
15046a599222SSimon L. B. Nielsen        my $wopt;
1505*610a21fdSJung-uk Kim        my $gccver = $predefined_C{__GNUC__} // -1;
1506e71b7053SJung-uk Kim
1507*610a21fdSJung-uk Kim        warn "WARNING --strict-warnings requires gcc[>=4] or gcc-alike"
1508e71b7053SJung-uk Kim            unless $gccver >= 4;
1509*610a21fdSJung-uk Kim        push @strict_warnings_collection, @gcc_devteam_warn;
1510*610a21fdSJung-uk Kim        push @strict_warnings_collection, @clang_devteam_warn
1511*610a21fdSJung-uk Kim            if (defined($predefined_C{__clang__}));
15126a599222SSimon L. B. Nielsen        }
1513*610a21fdSJung-uk Kim
1514*610a21fdSJung-uk Kimif (grep { $_ eq '-static' } @{$config{LDFLAGS}}) {
1515*610a21fdSJung-uk Kim    disable('static', 'pic', 'threads');
15167bded2dbSJung-uk Kim}
1517*610a21fdSJung-uk Kim
1518*610a21fdSJung-uk Kim$config{CFLAGS} = [ map { $_ eq '--ossl-strict-warnings'
1519*610a21fdSJung-uk Kim                              ? @strict_warnings_collection
1520*610a21fdSJung-uk Kim                              : ( $_ ) }
1521*610a21fdSJung-uk Kim                    @{$config{CFLAGS}} ];
15226a599222SSimon L. B. Nielsen
1523e71b7053SJung-uk Kimunless ($disabled{"crypto-mdebug-backtrace"})
152474664626SKris Kennaway        {
1525e71b7053SJung-uk Kim        foreach my $wopt (split /\s+/, $memleak_devteam_backtrace)
15266a599222SSimon L. B. Nielsen                {
1527e71b7053SJung-uk Kim                push @{$config{cflags}}, $wopt
1528e71b7053SJung-uk Kim                        unless grep { $_ eq $wopt } @{$config{cflags}};
15296a599222SSimon L. B. Nielsen                }
1530e71b7053SJung-uk Kim        if ($target =~ /^BSD-/)
1531a21b1b38SKris Kennaway                {
1532e71b7053SJung-uk Kim                push @{$config{ex_libs}}, "-lexecinfo";
15334f20a5a2SJacques Vidrine                }
153474664626SKris Kennaway        }
153574664626SKris Kennaway
1536e71b7053SJung-uk Kimunless ($disabled{afalgeng}) {
1537e71b7053SJung-uk Kim    $config{afalgeng}="";
1538e71b7053SJung-uk Kim    if (grep { $_ eq 'afalgeng' } @{$target{enable}}) {
1539e71b7053SJung-uk Kim        my $minver = 4*10000 + 1*100 + 0;
1540e71b7053SJung-uk Kim        if ($config{CROSS_COMPILE} eq "") {
1541e71b7053SJung-uk Kim            my $verstr = `uname -r`;
1542e71b7053SJung-uk Kim            my ($ma, $mi1, $mi2) = split("\\.", $verstr);
1543e71b7053SJung-uk Kim            ($mi2) = $mi2 =~ /(\d+)/;
1544e71b7053SJung-uk Kim            my $ver = $ma*10000 + $mi1*100 + $mi2;
1545e71b7053SJung-uk Kim            if ($ver < $minver) {
1546*610a21fdSJung-uk Kim                disable('too-old-kernel', 'afalgeng');
1547e71b7053SJung-uk Kim            } else {
1548e71b7053SJung-uk Kim                push @{$config{engdirs}}, "afalg";
1549e71b7053SJung-uk Kim            }
1550e71b7053SJung-uk Kim        } else {
1551*610a21fdSJung-uk Kim            disable('cross-compiling', 'afalgeng');
1552e71b7053SJung-uk Kim        }
1553e71b7053SJung-uk Kim    } else {
1554*610a21fdSJung-uk Kim        disable('not-linux', 'afalgeng');
1555e71b7053SJung-uk Kim    }
1556e71b7053SJung-uk Kim}
1557f579bf8eSKris Kennaway
1558e71b7053SJung-uk Kimpush @{$config{openssl_other_defines}}, "OPENSSL_NO_AFALGENG" if ($disabled{afalgeng});
1559e71b7053SJung-uk Kim
1560*610a21fdSJung-uk Kim# Get the extra flags used when building shared libraries and modules.  We
1561*610a21fdSJung-uk Kim# do this late because some of them depend on %disabled.
1562e71b7053SJung-uk Kim
1563*610a21fdSJung-uk Kim# Make the flags to build DSOs the same as for shared libraries unless they
1564*610a21fdSJung-uk Kim# are already defined
1565*610a21fdSJung-uk Kim$target{module_cflags} = $target{shared_cflag} unless defined $target{module_cflags};
1566*610a21fdSJung-uk Kim$target{module_cxxflags} = $target{shared_cxxflag} unless defined $target{module_cxxflags};
1567*610a21fdSJung-uk Kim$target{module_ldflags} = $target{shared_ldflag} unless defined $target{module_ldflags};
1568*610a21fdSJung-uk Kim{
1569*610a21fdSJung-uk Kim    my $shared_info_pl =
1570*610a21fdSJung-uk Kim        catfile(dirname($0), "Configurations", "shared-info.pl");
1571*610a21fdSJung-uk Kim    my %shared_info = read_eval_file($shared_info_pl);
1572*610a21fdSJung-uk Kim    push @{$target{_conf_fname_int}}, $shared_info_pl;
1573*610a21fdSJung-uk Kim    my $si = $target{shared_target};
1574*610a21fdSJung-uk Kim    while (ref $si ne "HASH") {
1575*610a21fdSJung-uk Kim        last if ! defined $si;
1576*610a21fdSJung-uk Kim        if (ref $si eq "CODE") {
1577*610a21fdSJung-uk Kim            $si = $si->();
1578e71b7053SJung-uk Kim        } else {
1579*610a21fdSJung-uk Kim            $si = $shared_info{$si};
1580e71b7053SJung-uk Kim        }
1581e71b7053SJung-uk Kim    }
1582e71b7053SJung-uk Kim
1583*610a21fdSJung-uk Kim    # Some of the 'shared_target' values don't have any entries in
1584*610a21fdSJung-uk Kim    # %shared_info.  That's perfectly fine, AS LONG AS the build file
1585*610a21fdSJung-uk Kim    # template knows how to handle this.  That is currently the case for
1586*610a21fdSJung-uk Kim    # Windows and VMS.
1587*610a21fdSJung-uk Kim    if (defined $si) {
1588*610a21fdSJung-uk Kim        # Just as above, copy certain shared_* attributes to the corresponding
1589*610a21fdSJung-uk Kim        # module_ attribute unless the latter is already defined
1590*610a21fdSJung-uk Kim        $si->{module_cflags} = $si->{shared_cflag} unless defined $si->{module_cflags};
1591*610a21fdSJung-uk Kim        $si->{module_cxxflags} = $si->{shared_cxxflag} unless defined $si->{module_cxxflags};
1592*610a21fdSJung-uk Kim        $si->{module_ldflags} = $si->{shared_ldflag} unless defined $si->{module_ldflags};
1593*610a21fdSJung-uk Kim        foreach (sort keys %$si) {
1594*610a21fdSJung-uk Kim            $target{$_} = defined $target{$_}
1595*610a21fdSJung-uk Kim                ? add($si->{$_})->($target{$_})
1596*610a21fdSJung-uk Kim                : $si->{$_};
1597*610a21fdSJung-uk Kim        }
1598*610a21fdSJung-uk Kim    }
1599*610a21fdSJung-uk Kim}
1600*610a21fdSJung-uk Kim
1601*610a21fdSJung-uk Kim# ALL MODIFICATIONS TO %disabled, %config and %target MUST BE DONE FROM HERE ON
1602e71b7053SJung-uk Kim
1603e71b7053SJung-uk Kim# If we use the unified build, collect information from build.info files
1604e71b7053SJung-uk Kimmy %unified_info = ();
1605e71b7053SJung-uk Kim
1606e71b7053SJung-uk Kimmy $buildinfo_debug = defined($ENV{CONFIGURE_DEBUG_BUILDINFO});
1607e71b7053SJung-uk Kimif ($builder eq "unified") {
1608e71b7053SJung-uk Kim    use with_fallback qw(Text::Template);
1609e71b7053SJung-uk Kim
1610e71b7053SJung-uk Kim    sub cleandir {
1611e71b7053SJung-uk Kim        my $base = shift;
1612e71b7053SJung-uk Kim        my $dir = shift;
1613e71b7053SJung-uk Kim        my $relativeto = shift || ".";
1614e71b7053SJung-uk Kim
1615e71b7053SJung-uk Kim        $dir = catdir($base,$dir) unless isabsolute($dir);
1616e71b7053SJung-uk Kim
1617e71b7053SJung-uk Kim        # Make sure the directories we're building in exists
1618e71b7053SJung-uk Kim        mkpath($dir);
1619e71b7053SJung-uk Kim
1620e71b7053SJung-uk Kim        my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
1621e71b7053SJung-uk Kim        #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
1622e71b7053SJung-uk Kim        return $res;
1623e71b7053SJung-uk Kim    }
1624e71b7053SJung-uk Kim
1625e71b7053SJung-uk Kim    sub cleanfile {
1626e71b7053SJung-uk Kim        my $base = shift;
1627e71b7053SJung-uk Kim        my $file = shift;
1628e71b7053SJung-uk Kim        my $relativeto = shift || ".";
1629e71b7053SJung-uk Kim
1630e71b7053SJung-uk Kim        $file = catfile($base,$file) unless isabsolute($file);
1631e71b7053SJung-uk Kim
1632e71b7053SJung-uk Kim        my $d = dirname($file);
1633e71b7053SJung-uk Kim        my $f = basename($file);
1634e71b7053SJung-uk Kim
1635e71b7053SJung-uk Kim        # Make sure the directories we're building in exists
1636e71b7053SJung-uk Kim        mkpath($d);
1637e71b7053SJung-uk Kim
1638e71b7053SJung-uk Kim        my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
1639e71b7053SJung-uk Kim        #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
1640e71b7053SJung-uk Kim        return $res;
1641e71b7053SJung-uk Kim    }
1642e71b7053SJung-uk Kim
1643e71b7053SJung-uk Kim    # Store the name of the template file we will build the build file from
1644e71b7053SJung-uk Kim    # in %config.  This may be useful for the build file itself.
1645e71b7053SJung-uk Kim    my @build_file_template_names =
1646e71b7053SJung-uk Kim        ( $builder_platform."-".$target{build_file}.".tmpl",
1647e71b7053SJung-uk Kim          $target{build_file}.".tmpl" );
1648e71b7053SJung-uk Kim    my @build_file_templates = ();
1649e71b7053SJung-uk Kim
1650e71b7053SJung-uk Kim    # First, look in the user provided directory, if given
1651e71b7053SJung-uk Kim    if (defined env($local_config_envname)) {
1652e71b7053SJung-uk Kim        @build_file_templates =
1653e71b7053SJung-uk Kim            map {
1654e71b7053SJung-uk Kim                if ($^O eq 'VMS') {
1655e71b7053SJung-uk Kim                    # VMS environment variables are logical names,
1656e71b7053SJung-uk Kim                    # which can be used as is
1657e71b7053SJung-uk Kim                    $local_config_envname . ':' . $_;
1658e71b7053SJung-uk Kim                } else {
1659e71b7053SJung-uk Kim                    catfile(env($local_config_envname), $_);
1660e71b7053SJung-uk Kim                }
1661e71b7053SJung-uk Kim            }
1662e71b7053SJung-uk Kim            @build_file_template_names;
1663e71b7053SJung-uk Kim    }
1664e71b7053SJung-uk Kim    # Then, look in our standard directory
1665e71b7053SJung-uk Kim    push @build_file_templates,
1666e71b7053SJung-uk Kim        ( map { cleanfile($srcdir, catfile("Configurations", $_), $blddir) }
1667e71b7053SJung-uk Kim          @build_file_template_names );
1668e71b7053SJung-uk Kim
1669e71b7053SJung-uk Kim    my $build_file_template;
1670e71b7053SJung-uk Kim    for $_ (@build_file_templates) {
1671e71b7053SJung-uk Kim        $build_file_template = $_;
1672e71b7053SJung-uk Kim        last if -f $build_file_template;
1673e71b7053SJung-uk Kim
1674e71b7053SJung-uk Kim        $build_file_template = undef;
1675e71b7053SJung-uk Kim    }
1676e71b7053SJung-uk Kim    if (!defined $build_file_template) {
1677e71b7053SJung-uk Kim        die "*** Couldn't find any of:\n", join("\n", @build_file_templates), "\n";
1678e71b7053SJung-uk Kim    }
1679e71b7053SJung-uk Kim    $config{build_file_templates}
1680e71b7053SJung-uk Kim      = [ cleanfile($srcdir, catfile("Configurations", "common0.tmpl"),
1681e71b7053SJung-uk Kim                    $blddir),
1682e71b7053SJung-uk Kim          $build_file_template,
1683e71b7053SJung-uk Kim          cleanfile($srcdir, catfile("Configurations", "common.tmpl"),
1684e71b7053SJung-uk Kim                    $blddir) ];
1685e71b7053SJung-uk Kim
1686e71b7053SJung-uk Kim    my @build_infos = ( [ ".", "build.info" ] );
1687e71b7053SJung-uk Kim    foreach (@{$config{dirs}}) {
1688e71b7053SJung-uk Kim        push @build_infos, [ $_, "build.info" ]
1689e71b7053SJung-uk Kim            if (-f catfile($srcdir, $_, "build.info"));
1690e71b7053SJung-uk Kim    }
1691e71b7053SJung-uk Kim    foreach (@{$config{sdirs}}) {
1692e71b7053SJung-uk Kim        push @build_infos, [ catdir("crypto", $_), "build.info" ]
1693e71b7053SJung-uk Kim            if (-f catfile($srcdir, "crypto", $_, "build.info"));
1694e71b7053SJung-uk Kim    }
1695e71b7053SJung-uk Kim    foreach (@{$config{engdirs}}) {
1696e71b7053SJung-uk Kim        push @build_infos, [ catdir("engines", $_), "build.info" ]
1697e71b7053SJung-uk Kim            if (-f catfile($srcdir, "engines", $_, "build.info"));
1698e71b7053SJung-uk Kim    }
1699e71b7053SJung-uk Kim    foreach (@{$config{tdirs}}) {
1700e71b7053SJung-uk Kim        push @build_infos, [ catdir("test", $_), "build.info" ]
1701e71b7053SJung-uk Kim            if (-f catfile($srcdir, "test", $_, "build.info"));
1702e71b7053SJung-uk Kim    }
1703e71b7053SJung-uk Kim
1704e71b7053SJung-uk Kim    $config{build_infos} = [ ];
1705e71b7053SJung-uk Kim
1706e71b7053SJung-uk Kim    my %ordinals = ();
1707e71b7053SJung-uk Kim    foreach (@build_infos) {
1708e71b7053SJung-uk Kim        my $sourced = catdir($srcdir, $_->[0]);
1709e71b7053SJung-uk Kim        my $buildd = catdir($blddir, $_->[0]);
1710e71b7053SJung-uk Kim
1711e71b7053SJung-uk Kim        mkpath($buildd);
1712e71b7053SJung-uk Kim
1713e71b7053SJung-uk Kim        my $f = $_->[1];
1714e71b7053SJung-uk Kim        # The basic things we're trying to build
1715e71b7053SJung-uk Kim        my @programs = ();
1716e71b7053SJung-uk Kim        my @programs_install = ();
1717e71b7053SJung-uk Kim        my @libraries = ();
1718e71b7053SJung-uk Kim        my @libraries_install = ();
1719e71b7053SJung-uk Kim        my @engines = ();
1720e71b7053SJung-uk Kim        my @engines_install = ();
1721e71b7053SJung-uk Kim        my @scripts = ();
1722e71b7053SJung-uk Kim        my @scripts_install = ();
1723e71b7053SJung-uk Kim        my @extra = ();
1724e71b7053SJung-uk Kim        my @overrides = ();
1725e71b7053SJung-uk Kim        my @intermediates = ();
1726e71b7053SJung-uk Kim        my @rawlines = ();
1727e71b7053SJung-uk Kim
1728e71b7053SJung-uk Kim        my %sources = ();
1729e71b7053SJung-uk Kim        my %shared_sources = ();
1730e71b7053SJung-uk Kim        my %includes = ();
1731e71b7053SJung-uk Kim        my %depends = ();
1732e71b7053SJung-uk Kim        my %renames = ();
1733e71b7053SJung-uk Kim        my %sharednames = ();
1734e71b7053SJung-uk Kim        my %generate = ();
1735e71b7053SJung-uk Kim
1736e71b7053SJung-uk Kim        # We want to detect configdata.pm in the source tree, so we
1737e71b7053SJung-uk Kim        # don't use it if the build tree is different.
1738e71b7053SJung-uk Kim        my $src_configdata = cleanfile($srcdir, "configdata.pm", $blddir);
1739e71b7053SJung-uk Kim
1740e71b7053SJung-uk Kim        push @{$config{build_infos}}, catfile(abs2rel($sourced, $blddir), $f);
1741e71b7053SJung-uk Kim        my $template =
1742e71b7053SJung-uk Kim            Text::Template->new(TYPE => 'FILE',
1743e71b7053SJung-uk Kim                                SOURCE => catfile($sourced, $f),
1744e71b7053SJung-uk Kim                                PREPEND => qq{use lib "$FindBin::Bin/util/perl";});
1745e71b7053SJung-uk Kim        die "Something went wrong with $sourced/$f: $!\n" unless $template;
1746e71b7053SJung-uk Kim        my @text =
1747e71b7053SJung-uk Kim            split /^/m,
1748e71b7053SJung-uk Kim            $template->fill_in(HASH => { config => \%config,
1749e71b7053SJung-uk Kim                                         target => \%target,
1750e71b7053SJung-uk Kim                                         disabled => \%disabled,
1751e71b7053SJung-uk Kim                                         withargs => \%withargs,
1752e71b7053SJung-uk Kim                                         builddir => abs2rel($buildd, $blddir),
1753e71b7053SJung-uk Kim                                         sourcedir => abs2rel($sourced, $blddir),
1754e71b7053SJung-uk Kim                                         buildtop => abs2rel($blddir, $blddir),
1755e71b7053SJung-uk Kim                                         sourcetop => abs2rel($srcdir, $blddir) },
1756e71b7053SJung-uk Kim                               DELIMITERS => [ "{-", "-}" ]);
1757e71b7053SJung-uk Kim
1758e71b7053SJung-uk Kim        # The top item of this stack has the following values
1759e71b7053SJung-uk Kim        # -2 positive already run and we found ELSE (following ELSIF should fail)
1760e71b7053SJung-uk Kim        # -1 positive already run (skip until ENDIF)
1761e71b7053SJung-uk Kim        # 0 negatives so far (if we're at a condition, check it)
1762e71b7053SJung-uk Kim        # 1 last was positive (don't skip lines until next ELSE, ELSIF or ENDIF)
1763e71b7053SJung-uk Kim        # 2 positive ELSE (following ELSIF should fail)
1764e71b7053SJung-uk Kim        my @skip = ();
1765e71b7053SJung-uk Kim        collect_information(
1766e71b7053SJung-uk Kim            collect_from_array([ @text ],
1767e71b7053SJung-uk Kim                               qr/\\$/ => sub { my $l1 = shift; my $l2 = shift;
1768e71b7053SJung-uk Kim                                                $l1 =~ s/\\$//; $l1.$l2 }),
1769e71b7053SJung-uk Kim            # Info we're looking for
1770e71b7053SJung-uk Kim            qr/^\s*IF\[((?:\\.|[^\\\]])*)\]\s*$/
1771e71b7053SJung-uk Kim            => sub {
1772e71b7053SJung-uk Kim                if (! @skip || $skip[$#skip] > 0) {
1773e71b7053SJung-uk Kim                    push @skip, !! $1;
1774e71b7053SJung-uk Kim                } else {
1775e71b7053SJung-uk Kim                    push @skip, -1;
1776e71b7053SJung-uk Kim                }
1777e71b7053SJung-uk Kim            },
1778e71b7053SJung-uk Kim            qr/^\s*ELSIF\[((?:\\.|[^\\\]])*)\]\s*$/
1779e71b7053SJung-uk Kim            => sub { die "ELSIF out of scope" if ! @skip;
1780e71b7053SJung-uk Kim                     die "ELSIF following ELSE" if abs($skip[$#skip]) == 2;
1781e71b7053SJung-uk Kim                     $skip[$#skip] = -1 if $skip[$#skip] != 0;
1782e71b7053SJung-uk Kim                     $skip[$#skip] = !! $1
1783e71b7053SJung-uk Kim                         if $skip[$#skip] == 0; },
1784e71b7053SJung-uk Kim            qr/^\s*ELSE\s*$/
1785e71b7053SJung-uk Kim            => sub { die "ELSE out of scope" if ! @skip;
1786e71b7053SJung-uk Kim                     $skip[$#skip] = -2 if $skip[$#skip] != 0;
1787e71b7053SJung-uk Kim                     $skip[$#skip] = 2 if $skip[$#skip] == 0; },
1788e71b7053SJung-uk Kim            qr/^\s*ENDIF\s*$/
1789e71b7053SJung-uk Kim            => sub { die "ENDIF out of scope" if ! @skip;
1790e71b7053SJung-uk Kim                     pop @skip; },
1791e71b7053SJung-uk Kim            qr/^\s*PROGRAMS(_NO_INST)?\s*=\s*(.*)\s*$/
1792e71b7053SJung-uk Kim            => sub {
1793e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1794e71b7053SJung-uk Kim                    my $install = $1;
1795e71b7053SJung-uk Kim                    my @x = tokenize($2);
1796e71b7053SJung-uk Kim                    push @programs, @x;
1797e71b7053SJung-uk Kim                    push @programs_install, @x unless $install;
1798e71b7053SJung-uk Kim                }
1799e71b7053SJung-uk Kim            },
1800e71b7053SJung-uk Kim            qr/^\s*LIBS(_NO_INST)?\s*=\s*(.*)\s*$/
1801e71b7053SJung-uk Kim            => sub {
1802e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1803e71b7053SJung-uk Kim                    my $install = $1;
1804e71b7053SJung-uk Kim                    my @x = tokenize($2);
1805e71b7053SJung-uk Kim                    push @libraries, @x;
1806e71b7053SJung-uk Kim                    push @libraries_install, @x unless $install;
1807e71b7053SJung-uk Kim                }
1808e71b7053SJung-uk Kim            },
1809e71b7053SJung-uk Kim            qr/^\s*ENGINES(_NO_INST)?\s*=\s*(.*)\s*$/
1810e71b7053SJung-uk Kim            => sub {
1811e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1812e71b7053SJung-uk Kim                    my $install = $1;
1813e71b7053SJung-uk Kim                    my @x = tokenize($2);
1814e71b7053SJung-uk Kim                    push @engines, @x;
1815e71b7053SJung-uk Kim                    push @engines_install, @x unless $install;
1816e71b7053SJung-uk Kim                }
1817e71b7053SJung-uk Kim            },
1818e71b7053SJung-uk Kim            qr/^\s*SCRIPTS(_NO_INST)?\s*=\s*(.*)\s*$/
1819e71b7053SJung-uk Kim            => sub {
1820e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
1821e71b7053SJung-uk Kim                    my $install = $1;
1822e71b7053SJung-uk Kim                    my @x = tokenize($2);
1823e71b7053SJung-uk Kim                    push @scripts, @x;
1824e71b7053SJung-uk Kim                    push @scripts_install, @x unless $install;
1825e71b7053SJung-uk Kim                }
1826e71b7053SJung-uk Kim            },
1827e71b7053SJung-uk Kim            qr/^\s*EXTRA\s*=\s*(.*)\s*$/
1828e71b7053SJung-uk Kim            => sub { push @extra, tokenize($1)
1829e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1830e71b7053SJung-uk Kim            qr/^\s*OVERRIDES\s*=\s*(.*)\s*$/
1831e71b7053SJung-uk Kim            => sub { push @overrides, tokenize($1)
1832e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1833e71b7053SJung-uk Kim
1834e71b7053SJung-uk Kim            qr/^\s*ORDINALS\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/,
1835e71b7053SJung-uk Kim            => sub { push @{$ordinals{$1}}, tokenize($2)
1836e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1837e71b7053SJung-uk Kim            qr/^\s*SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1838e71b7053SJung-uk Kim            => sub { push @{$sources{$1}}, tokenize($2)
1839e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1840e71b7053SJung-uk Kim            qr/^\s*SHARED_SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1841e71b7053SJung-uk Kim            => sub { push @{$shared_sources{$1}}, tokenize($2)
1842e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1843e71b7053SJung-uk Kim            qr/^\s*INCLUDE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1844e71b7053SJung-uk Kim            => sub { push @{$includes{$1}}, tokenize($2)
1845e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1846e71b7053SJung-uk Kim            qr/^\s*DEPEND\[((?:\\.|[^\\\]])*)\]\s*=\s*(.*)\s*$/
1847e71b7053SJung-uk Kim            => sub { push @{$depends{$1}}, tokenize($2)
1848e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1849e71b7053SJung-uk Kim            qr/^\s*GENERATE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1850e71b7053SJung-uk Kim            => sub { push @{$generate{$1}}, $2
1851e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1852e71b7053SJung-uk Kim            qr/^\s*RENAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1853e71b7053SJung-uk Kim            => sub { push @{$renames{$1}}, tokenize($2)
1854e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1855e71b7053SJung-uk Kim            qr/^\s*SHARED_NAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1856e71b7053SJung-uk Kim            => sub { push @{$sharednames{$1}}, tokenize($2)
1857e71b7053SJung-uk Kim                         if !@skip || $skip[$#skip] > 0 },
1858e71b7053SJung-uk Kim            qr/^\s*BEGINRAW\[((?:\\.|[^\\\]])+)\]\s*$/
1859e71b7053SJung-uk Kim            => sub {
1860e71b7053SJung-uk Kim                my $lineiterator = shift;
1861e71b7053SJung-uk Kim                my $target_kind = $1;
1862e71b7053SJung-uk Kim                while (defined $lineiterator->()) {
1863e71b7053SJung-uk Kim                    s|\R$||;
1864e71b7053SJung-uk Kim                    if (/^\s*ENDRAW\[((?:\\.|[^\\\]])+)\]\s*$/) {
1865e71b7053SJung-uk Kim                        die "ENDRAW doesn't match BEGINRAW"
1866e71b7053SJung-uk Kim                            if $1 ne $target_kind;
1867e71b7053SJung-uk Kim                        last;
1868e71b7053SJung-uk Kim                    }
1869e71b7053SJung-uk Kim                    next if @skip && $skip[$#skip] <= 0;
1870e71b7053SJung-uk Kim                    push @rawlines,  $_
1871e71b7053SJung-uk Kim                        if ($target_kind eq $target{build_file}
1872e71b7053SJung-uk Kim                            || $target_kind eq $target{build_file}."(".$builder_platform.")");
1873e71b7053SJung-uk Kim                }
1874e71b7053SJung-uk Kim            },
1875e71b7053SJung-uk Kim            qr/^\s*(?:#.*)?$/ => sub { },
1876e71b7053SJung-uk Kim            "OTHERWISE" => sub { die "Something wrong with this line:\n$_\nat $sourced/$f" },
1877e71b7053SJung-uk Kim            "BEFORE" => sub {
1878e71b7053SJung-uk Kim                if ($buildinfo_debug) {
1879e71b7053SJung-uk Kim                    print STDERR "DEBUG: Parsing ",join(" ", @_),"\n";
1880e71b7053SJung-uk Kim                    print STDERR "DEBUG: ... before parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
1881e71b7053SJung-uk Kim                }
1882e71b7053SJung-uk Kim            },
1883e71b7053SJung-uk Kim            "AFTER" => sub {
1884e71b7053SJung-uk Kim                if ($buildinfo_debug) {
1885e71b7053SJung-uk Kim                    print STDERR "DEBUG: .... after parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
1886e71b7053SJung-uk Kim                }
1887e71b7053SJung-uk Kim            },
1888e71b7053SJung-uk Kim            );
1889e71b7053SJung-uk Kim        die "runaway IF?" if (@skip);
1890e71b7053SJung-uk Kim
1891e71b7053SJung-uk Kim        foreach (keys %renames) {
1892e71b7053SJung-uk Kim            die "$_ renamed to more than one thing: "
1893e71b7053SJung-uk Kim                ,join(" ", @{$renames{$_}}),"\n"
1894e71b7053SJung-uk Kim                if scalar @{$renames{$_}} > 1;
1895e71b7053SJung-uk Kim            my $dest = cleanfile($buildd, $_, $blddir);
1896e71b7053SJung-uk Kim            my $to = cleanfile($buildd, $renames{$_}->[0], $blddir);
1897e71b7053SJung-uk Kim            die "$dest renamed to more than one thing: "
1898e71b7053SJung-uk Kim                ,$unified_info{rename}->{$dest}, $to
1899e71b7053SJung-uk Kim                unless !defined($unified_info{rename}->{$dest})
1900e71b7053SJung-uk Kim                or $unified_info{rename}->{$dest} eq $to;
1901e71b7053SJung-uk Kim            $unified_info{rename}->{$dest} = $to;
1902e71b7053SJung-uk Kim        }
1903e71b7053SJung-uk Kim
1904e71b7053SJung-uk Kim        foreach (@programs) {
1905e71b7053SJung-uk Kim            my $program = cleanfile($buildd, $_, $blddir);
1906e71b7053SJung-uk Kim            if ($unified_info{rename}->{$program}) {
1907e71b7053SJung-uk Kim                $program = $unified_info{rename}->{$program};
1908e71b7053SJung-uk Kim            }
1909e71b7053SJung-uk Kim            $unified_info{programs}->{$program} = 1;
1910e71b7053SJung-uk Kim        }
1911e71b7053SJung-uk Kim
1912e71b7053SJung-uk Kim        foreach (@programs_install) {
1913e71b7053SJung-uk Kim            my $program = cleanfile($buildd, $_, $blddir);
1914e71b7053SJung-uk Kim            if ($unified_info{rename}->{$program}) {
1915e71b7053SJung-uk Kim                $program = $unified_info{rename}->{$program};
1916e71b7053SJung-uk Kim            }
1917e71b7053SJung-uk Kim            $unified_info{install}->{programs}->{$program} = 1;
1918e71b7053SJung-uk Kim        }
1919e71b7053SJung-uk Kim
1920e71b7053SJung-uk Kim        foreach (@libraries) {
1921e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
1922e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
1923e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
1924e71b7053SJung-uk Kim            }
1925e71b7053SJung-uk Kim            $unified_info{libraries}->{$library} = 1;
1926e71b7053SJung-uk Kim        }
1927e71b7053SJung-uk Kim
1928e71b7053SJung-uk Kim        foreach (@libraries_install) {
1929e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
1930e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
1931e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
1932e71b7053SJung-uk Kim            }
1933e71b7053SJung-uk Kim            $unified_info{install}->{libraries}->{$library} = 1;
1934e71b7053SJung-uk Kim        }
1935e71b7053SJung-uk Kim
1936e71b7053SJung-uk Kim        die <<"EOF" if scalar @engines and !$config{dynamic_engines};
1937e71b7053SJung-uk KimENGINES can only be used if configured with 'dynamic-engine'.
1938e71b7053SJung-uk KimThis is usually a fault in a build.info file.
1939e71b7053SJung-uk KimEOF
1940e71b7053SJung-uk Kim        foreach (@engines) {
1941e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
1942e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
1943e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
1944e71b7053SJung-uk Kim            }
1945e71b7053SJung-uk Kim            $unified_info{engines}->{$library} = 1;
1946e71b7053SJung-uk Kim        }
1947e71b7053SJung-uk Kim
1948e71b7053SJung-uk Kim        foreach (@engines_install) {
1949e71b7053SJung-uk Kim            my $library = cleanfile($buildd, $_, $blddir);
1950e71b7053SJung-uk Kim            if ($unified_info{rename}->{$library}) {
1951e71b7053SJung-uk Kim                $library = $unified_info{rename}->{$library};
1952e71b7053SJung-uk Kim            }
1953e71b7053SJung-uk Kim            $unified_info{install}->{engines}->{$library} = 1;
1954e71b7053SJung-uk Kim        }
1955e71b7053SJung-uk Kim
1956e71b7053SJung-uk Kim        foreach (@scripts) {
1957e71b7053SJung-uk Kim            my $script = cleanfile($buildd, $_, $blddir);
1958e71b7053SJung-uk Kim            if ($unified_info{rename}->{$script}) {
1959e71b7053SJung-uk Kim                $script = $unified_info{rename}->{$script};
1960e71b7053SJung-uk Kim            }
1961e71b7053SJung-uk Kim            $unified_info{scripts}->{$script} = 1;
1962e71b7053SJung-uk Kim        }
1963e71b7053SJung-uk Kim
1964e71b7053SJung-uk Kim        foreach (@scripts_install) {
1965e71b7053SJung-uk Kim            my $script = cleanfile($buildd, $_, $blddir);
1966e71b7053SJung-uk Kim            if ($unified_info{rename}->{$script}) {
1967e71b7053SJung-uk Kim                $script = $unified_info{rename}->{$script};
1968e71b7053SJung-uk Kim            }
1969e71b7053SJung-uk Kim            $unified_info{install}->{scripts}->{$script} = 1;
1970e71b7053SJung-uk Kim        }
1971e71b7053SJung-uk Kim
1972e71b7053SJung-uk Kim        foreach (@extra) {
1973e71b7053SJung-uk Kim            my $extra = cleanfile($buildd, $_, $blddir);
1974e71b7053SJung-uk Kim            $unified_info{extra}->{$extra} = 1;
1975e71b7053SJung-uk Kim        }
1976e71b7053SJung-uk Kim
1977e71b7053SJung-uk Kim        foreach (@overrides) {
1978e71b7053SJung-uk Kim            my $override = cleanfile($buildd, $_, $blddir);
1979e71b7053SJung-uk Kim            $unified_info{overrides}->{$override} = 1;
1980e71b7053SJung-uk Kim        }
1981e71b7053SJung-uk Kim
1982e71b7053SJung-uk Kim        push @{$unified_info{rawlines}}, @rawlines;
1983e71b7053SJung-uk Kim
1984e71b7053SJung-uk Kim        unless ($disabled{shared}) {
1985e71b7053SJung-uk Kim            # Check sharednames.
1986e71b7053SJung-uk Kim            foreach (keys %sharednames) {
1987e71b7053SJung-uk Kim                my $dest = cleanfile($buildd, $_, $blddir);
1988e71b7053SJung-uk Kim                if ($unified_info{rename}->{$dest}) {
1989e71b7053SJung-uk Kim                    $dest = $unified_info{rename}->{$dest};
1990e71b7053SJung-uk Kim                }
1991e71b7053SJung-uk Kim                die "shared_name for $dest with multiple values: "
1992e71b7053SJung-uk Kim                    ,join(" ", @{$sharednames{$_}}),"\n"
1993e71b7053SJung-uk Kim                    if scalar @{$sharednames{$_}} > 1;
1994e71b7053SJung-uk Kim                my $to = cleanfile($buildd, $sharednames{$_}->[0], $blddir);
1995e71b7053SJung-uk Kim                die "shared_name found for a library $dest that isn't defined\n"
1996e71b7053SJung-uk Kim                    unless $unified_info{libraries}->{$dest};
1997e71b7053SJung-uk Kim                die "shared_name for $dest with multiple values: "
1998e71b7053SJung-uk Kim                    ,$unified_info{sharednames}->{$dest}, ", ", $to
1999e71b7053SJung-uk Kim                    unless !defined($unified_info{sharednames}->{$dest})
2000e71b7053SJung-uk Kim                    or $unified_info{sharednames}->{$dest} eq $to;
2001e71b7053SJung-uk Kim                $unified_info{sharednames}->{$dest} = $to;
2002e71b7053SJung-uk Kim            }
2003e71b7053SJung-uk Kim
2004e71b7053SJung-uk Kim            # Additionally, we set up sharednames for libraries that don't
2005e71b7053SJung-uk Kim            # have any, as themselves.  Only for libraries that aren't
2006e71b7053SJung-uk Kim            # explicitly static.
2007e71b7053SJung-uk Kim            foreach (grep !/\.a$/, keys %{$unified_info{libraries}}) {
2008e71b7053SJung-uk Kim                if (!defined $unified_info{sharednames}->{$_}) {
2009e71b7053SJung-uk Kim                    $unified_info{sharednames}->{$_} = $_
2010e71b7053SJung-uk Kim                }
2011e71b7053SJung-uk Kim            }
2012e71b7053SJung-uk Kim
2013e71b7053SJung-uk Kim            # Check that we haven't defined any library as both shared and
2014e71b7053SJung-uk Kim            # explicitly static.  That is forbidden.
2015e71b7053SJung-uk Kim            my @doubles = ();
2016e71b7053SJung-uk Kim            foreach (grep /\.a$/, keys %{$unified_info{libraries}}) {
2017e71b7053SJung-uk Kim                (my $l = $_) =~ s/\.a$//;
2018e71b7053SJung-uk Kim                push @doubles, $l if defined $unified_info{sharednames}->{$l};
2019e71b7053SJung-uk Kim            }
2020e71b7053SJung-uk Kim            die "these libraries are both explicitly static and shared:\n  ",
2021e71b7053SJung-uk Kim                join(" ", @doubles), "\n"
2022e71b7053SJung-uk Kim                if @doubles;
2023e71b7053SJung-uk Kim        }
2024e71b7053SJung-uk Kim
2025e71b7053SJung-uk Kim        foreach (keys %sources) {
2026e71b7053SJung-uk Kim            my $dest = $_;
2027e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2028e71b7053SJung-uk Kim            if ($unified_info{rename}->{$ddest}) {
2029e71b7053SJung-uk Kim                $ddest = $unified_info{rename}->{$ddest};
2030e71b7053SJung-uk Kim            }
2031e71b7053SJung-uk Kim            foreach (@{$sources{$dest}}) {
2032e71b7053SJung-uk Kim                my $s = cleanfile($sourced, $_, $blddir);
2033e71b7053SJung-uk Kim
2034e71b7053SJung-uk Kim                # If it isn't in the source tree, we assume it's generated
2035e71b7053SJung-uk Kim                # in the build tree
2036e71b7053SJung-uk Kim                if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
2037e71b7053SJung-uk Kim                    $s = cleanfile($buildd, $_, $blddir);
2038e71b7053SJung-uk Kim                }
2039e71b7053SJung-uk Kim                # We recognise C++, C and asm files
2040e71b7053SJung-uk Kim                if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2041e71b7053SJung-uk Kim                    my $o = $_;
2042e71b7053SJung-uk Kim                    $o =~ s/\.[csS]$/.o/; # C and assembler
2043e71b7053SJung-uk Kim                    $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2044e71b7053SJung-uk Kim                    $o = cleanfile($buildd, $o, $blddir);
2045e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$o} = 1;
2046e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2047e71b7053SJung-uk Kim                } elsif ($s =~ /\.rc$/) {
2048e71b7053SJung-uk Kim                    # We also recognise resource files
2049e71b7053SJung-uk Kim                    my $o = $_;
2050e71b7053SJung-uk Kim                    $o =~ s/\.rc$/.res/; # Resource configuration
2051e71b7053SJung-uk Kim                    my $o = cleanfile($buildd, $o, $blddir);
2052e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$o} = 1;
2053e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2054e71b7053SJung-uk Kim                } else {
2055e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$s} = 1;
2056e71b7053SJung-uk Kim                }
2057e71b7053SJung-uk Kim            }
2058e71b7053SJung-uk Kim        }
2059e71b7053SJung-uk Kim
2060e71b7053SJung-uk Kim        foreach (keys %shared_sources) {
2061e71b7053SJung-uk Kim            my $dest = $_;
2062e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2063e71b7053SJung-uk Kim            if ($unified_info{rename}->{$ddest}) {
2064e71b7053SJung-uk Kim                $ddest = $unified_info{rename}->{$ddest};
2065e71b7053SJung-uk Kim            }
2066e71b7053SJung-uk Kim            foreach (@{$shared_sources{$dest}}) {
2067e71b7053SJung-uk Kim                my $s = cleanfile($sourced, $_, $blddir);
2068e71b7053SJung-uk Kim
2069e71b7053SJung-uk Kim                # If it isn't in the source tree, we assume it's generated
2070e71b7053SJung-uk Kim                # in the build tree
2071e71b7053SJung-uk Kim                if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
2072e71b7053SJung-uk Kim                    $s = cleanfile($buildd, $_, $blddir);
2073e71b7053SJung-uk Kim                }
2074e71b7053SJung-uk Kim
2075e71b7053SJung-uk Kim                if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2076e71b7053SJung-uk Kim                    # We recognise C++, C and asm files
2077e71b7053SJung-uk Kim                    my $o = $_;
2078e71b7053SJung-uk Kim                    $o =~ s/\.[csS]$/.o/; # C and assembler
2079e71b7053SJung-uk Kim                    $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2080e71b7053SJung-uk Kim                    $o = cleanfile($buildd, $o, $blddir);
2081e71b7053SJung-uk Kim                    $unified_info{shared_sources}->{$ddest}->{$o} = 1;
2082e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2083e71b7053SJung-uk Kim                } elsif ($s =~ /\.rc$/) {
2084e71b7053SJung-uk Kim                    # We also recognise resource files
2085e71b7053SJung-uk Kim                    my $o = $_;
2086e71b7053SJung-uk Kim                    $o =~ s/\.rc$/.res/; # Resource configuration
2087e71b7053SJung-uk Kim                    my $o = cleanfile($buildd, $o, $blddir);
2088e71b7053SJung-uk Kim                    $unified_info{shared_sources}->{$ddest}->{$o} = 1;
2089e71b7053SJung-uk Kim                    $unified_info{sources}->{$o}->{$s} = 1;
2090e71b7053SJung-uk Kim                } elsif ($s =~ /\.(def|map|opt)$/) {
2091e71b7053SJung-uk Kim                    # We also recognise .def / .map / .opt files
2092e71b7053SJung-uk Kim                    # We know they are generated files
2093e71b7053SJung-uk Kim                    my $def = cleanfile($buildd, $s, $blddir);
2094e71b7053SJung-uk Kim                    $unified_info{shared_sources}->{$ddest}->{$def} = 1;
2095e71b7053SJung-uk Kim                } else {
2096e71b7053SJung-uk Kim                    die "unrecognised source file type for shared library: $s\n";
2097e71b7053SJung-uk Kim                }
2098e71b7053SJung-uk Kim            }
2099e71b7053SJung-uk Kim        }
2100e71b7053SJung-uk Kim
2101e71b7053SJung-uk Kim        foreach (keys %generate) {
2102e71b7053SJung-uk Kim            my $dest = $_;
2103e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2104e71b7053SJung-uk Kim            if ($unified_info{rename}->{$ddest}) {
2105e71b7053SJung-uk Kim                $ddest = $unified_info{rename}->{$ddest};
2106e71b7053SJung-uk Kim            }
2107e71b7053SJung-uk Kim            die "more than one generator for $dest: "
2108e71b7053SJung-uk Kim                    ,join(" ", @{$generate{$_}}),"\n"
2109e71b7053SJung-uk Kim                    if scalar @{$generate{$_}} > 1;
2110e71b7053SJung-uk Kim            my @generator = split /\s+/, $generate{$dest}->[0];
2111e71b7053SJung-uk Kim            $generator[0] = cleanfile($sourced, $generator[0], $blddir),
2112e71b7053SJung-uk Kim            $unified_info{generate}->{$ddest} = [ @generator ];
2113e71b7053SJung-uk Kim        }
2114e71b7053SJung-uk Kim
2115e71b7053SJung-uk Kim        foreach (keys %depends) {
2116e71b7053SJung-uk Kim            my $dest = $_;
2117e71b7053SJung-uk Kim            my $ddest = $dest eq "" ? "" : cleanfile($sourced, $_, $blddir);
2118e71b7053SJung-uk Kim
2119e71b7053SJung-uk Kim            # If the destination doesn't exist in source, it can only be
2120e71b7053SJung-uk Kim            # a generated file in the build tree.
2121e71b7053SJung-uk Kim            if ($ddest ne "" && ($ddest eq $src_configdata || ! -f $ddest)) {
2122e71b7053SJung-uk Kim                $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            }
2127e71b7053SJung-uk Kim            foreach (@{$depends{$dest}}) {
2128e71b7053SJung-uk Kim                my $d = cleanfile($sourced, $_, $blddir);
2129e71b7053SJung-uk Kim
2130e71b7053SJung-uk Kim                # If we know it's generated, or assume it is because we can't
2131e71b7053SJung-uk Kim                # find it in the source tree, we set file we depend on to be
2132e71b7053SJung-uk Kim                # in the build tree rather than the source tree, and assume
2133e71b7053SJung-uk Kim                # and that there are lines to build it in a BEGINRAW..ENDRAW
2134e71b7053SJung-uk Kim                # section or in the Makefile template.
2135e71b7053SJung-uk Kim                if ($d eq $src_configdata
2136e71b7053SJung-uk Kim                    || ! -f $d
2137e71b7053SJung-uk Kim                    || (grep { $d eq $_ }
2138e71b7053SJung-uk Kim                        map { cleanfile($srcdir, $_, $blddir) }
2139e71b7053SJung-uk Kim                        grep { /\.h$/ } keys %{$unified_info{generate}})) {
2140e71b7053SJung-uk Kim                    $d = cleanfile($buildd, $_, $blddir);
2141e71b7053SJung-uk Kim                }
2142e71b7053SJung-uk Kim                # Take note if the file to depend on is being renamed
2143e71b7053SJung-uk Kim                # Take extra care with files ending with .a, they should
2144e71b7053SJung-uk Kim                # be treated without that extension, and the extension
2145e71b7053SJung-uk Kim                # should be added back after treatment.
2146e71b7053SJung-uk Kim                $d =~ /(\.a)?$/;
2147e71b7053SJung-uk Kim                my $e = $1 // "";
2148e71b7053SJung-uk Kim                $d = $`;
2149e71b7053SJung-uk Kim                if ($unified_info{rename}->{$d}) {
2150e71b7053SJung-uk Kim                    $d = $unified_info{rename}->{$d};
2151e71b7053SJung-uk Kim                }
2152e71b7053SJung-uk Kim                $d .= $e;
2153e71b7053SJung-uk Kim                $unified_info{depends}->{$ddest}->{$d} = 1;
2154e71b7053SJung-uk Kim            }
2155e71b7053SJung-uk Kim        }
2156e71b7053SJung-uk Kim
2157e71b7053SJung-uk Kim        foreach (keys %includes) {
2158e71b7053SJung-uk Kim            my $dest = $_;
2159e71b7053SJung-uk Kim            my $ddest = cleanfile($sourced, $_, $blddir);
2160e71b7053SJung-uk Kim
2161e71b7053SJung-uk Kim            # If the destination doesn't exist in source, it can only be
2162e71b7053SJung-uk Kim            # a generated file in the build tree.
2163e71b7053SJung-uk Kim            if ($ddest eq $src_configdata || ! -f $ddest) {
2164e71b7053SJung-uk Kim                $ddest = cleanfile($buildd, $_, $blddir);
2165e71b7053SJung-uk Kim                if ($unified_info{rename}->{$ddest}) {
2166e71b7053SJung-uk Kim                    $ddest = $unified_info{rename}->{$ddest};
2167e71b7053SJung-uk Kim                }
2168e71b7053SJung-uk Kim            }
2169e71b7053SJung-uk Kim            foreach (@{$includes{$dest}}) {
2170e71b7053SJung-uk Kim                my $is = cleandir($sourced, $_, $blddir);
2171e71b7053SJung-uk Kim                my $ib = cleandir($buildd, $_, $blddir);
2172e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$ddest}->{source}}, $is
2173e71b7053SJung-uk Kim                    unless grep { $_ eq $is } @{$unified_info{includes}->{$ddest}->{source}};
2174e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$ddest}->{build}}, $ib
2175e71b7053SJung-uk Kim                    unless grep { $_ eq $ib } @{$unified_info{includes}->{$ddest}->{build}};
2176e71b7053SJung-uk Kim            }
2177e71b7053SJung-uk Kim        }
2178e71b7053SJung-uk Kim    }
2179e71b7053SJung-uk Kim
2180e71b7053SJung-uk Kim    my $ordinals_text = join(', ', sort keys %ordinals);
2181e71b7053SJung-uk Kim    warn <<"EOF" if $ordinals_text;
2182e71b7053SJung-uk Kim
2183e71b7053SJung-uk KimWARNING: ORDINALS were specified for $ordinals_text
2184e71b7053SJung-uk KimThey are ignored and should be replaced with a combination of GENERATE,
2185e71b7053SJung-uk KimDEPEND and SHARED_SOURCE.
2186e71b7053SJung-uk KimEOF
2187e71b7053SJung-uk Kim
2188e71b7053SJung-uk Kim    # Massage the result
2189e71b7053SJung-uk Kim
2190c9cf7b5cSJung-uk Kim    # If the user configured no-shared, we allow no shared sources
2191c9cf7b5cSJung-uk Kim    if ($disabled{shared}) {
2192c9cf7b5cSJung-uk Kim        foreach (keys %{$unified_info{shared_sources}}) {
2193c9cf7b5cSJung-uk Kim            foreach (keys %{$unified_info{shared_sources}->{$_}}) {
2194c9cf7b5cSJung-uk Kim                delete $unified_info{sources}->{$_};
2195c9cf7b5cSJung-uk Kim            }
2196c9cf7b5cSJung-uk Kim        }
2197c9cf7b5cSJung-uk Kim        $unified_info{shared_sources} = {};
2198c9cf7b5cSJung-uk Kim    }
2199c9cf7b5cSJung-uk Kim
2200e71b7053SJung-uk Kim    # If we depend on a header file or a perl module, add an inclusion of
2201e71b7053SJung-uk Kim    # its directory to allow smoothe inclusion
2202e71b7053SJung-uk Kim    foreach my $dest (keys %{$unified_info{depends}}) {
2203e71b7053SJung-uk Kim        next if $dest eq "";
2204e71b7053SJung-uk Kim        foreach my $d (keys %{$unified_info{depends}->{$dest}}) {
2205e71b7053SJung-uk Kim            next unless $d =~ /\.(h|pm)$/;
2206e71b7053SJung-uk Kim            my $i = dirname($d);
2207e71b7053SJung-uk Kim            my $spot =
2208e71b7053SJung-uk Kim                $d eq "configdata.pm" || defined($unified_info{generate}->{$d})
2209e71b7053SJung-uk Kim                ? 'build' : 'source';
2210e71b7053SJung-uk Kim            push @{$unified_info{includes}->{$dest}->{$spot}}, $i
2211e71b7053SJung-uk Kim                unless grep { $_ eq $i } @{$unified_info{includes}->{$dest}->{$spot}};
2212e71b7053SJung-uk Kim        }
2213e71b7053SJung-uk Kim    }
2214e71b7053SJung-uk Kim
2215e71b7053SJung-uk Kim    # Trickle down includes placed on libraries, engines and programs to
2216e71b7053SJung-uk Kim    # their sources (i.e. object files)
2217e71b7053SJung-uk Kim    foreach my $dest (keys %{$unified_info{engines}},
2218e71b7053SJung-uk Kim                      keys %{$unified_info{libraries}},
2219e71b7053SJung-uk Kim                      keys %{$unified_info{programs}}) {
2220e71b7053SJung-uk Kim        foreach my $k (("source", "build")) {
2221e71b7053SJung-uk Kim            next unless defined($unified_info{includes}->{$dest}->{$k});
2222e71b7053SJung-uk Kim            my @incs = reverse @{$unified_info{includes}->{$dest}->{$k}};
2223e71b7053SJung-uk Kim            foreach my $obj (grep /\.o$/,
2224c9cf7b5cSJung-uk Kim                             (keys %{$unified_info{sources}->{$dest} // {}},
2225c9cf7b5cSJung-uk Kim                              keys %{$unified_info{shared_sources}->{$dest} // {}})) {
2226e71b7053SJung-uk Kim                foreach my $inc (@incs) {
2227e71b7053SJung-uk Kim                    unshift @{$unified_info{includes}->{$obj}->{$k}}, $inc
2228e71b7053SJung-uk Kim                        unless grep { $_ eq $inc } @{$unified_info{includes}->{$obj}->{$k}};
2229e71b7053SJung-uk Kim                }
2230e71b7053SJung-uk Kim            }
2231e71b7053SJung-uk Kim        }
2232e71b7053SJung-uk Kim        delete $unified_info{includes}->{$dest};
2233e71b7053SJung-uk Kim    }
2234e71b7053SJung-uk Kim
2235e71b7053SJung-uk Kim    ### Make unified_info a bit more efficient
2236e71b7053SJung-uk Kim    # One level structures
2237e71b7053SJung-uk Kim    foreach (("programs", "libraries", "engines", "scripts", "extra", "overrides")) {
2238e71b7053SJung-uk Kim        $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
2239e71b7053SJung-uk Kim    }
2240e71b7053SJung-uk Kim    # Two level structures
2241e71b7053SJung-uk Kim    foreach my $l1 (("install", "sources", "shared_sources", "ldadd", "depends")) {
2242e71b7053SJung-uk Kim        foreach my $l2 (sort keys %{$unified_info{$l1}}) {
2243e71b7053SJung-uk Kim            $unified_info{$l1}->{$l2} =
2244e71b7053SJung-uk Kim                [ sort keys %{$unified_info{$l1}->{$l2}} ];
2245e71b7053SJung-uk Kim        }
2246e71b7053SJung-uk Kim    }
2247e71b7053SJung-uk Kim    # Includes
2248e71b7053SJung-uk Kim    foreach my $dest (sort keys %{$unified_info{includes}}) {
2249e71b7053SJung-uk Kim        if (defined($unified_info{includes}->{$dest}->{build})) {
2250e71b7053SJung-uk Kim            my @source_includes = ();
2251e71b7053SJung-uk Kim            @source_includes = ( @{$unified_info{includes}->{$dest}->{source}} )
2252e71b7053SJung-uk Kim                if defined($unified_info{includes}->{$dest}->{source});
2253e71b7053SJung-uk Kim            $unified_info{includes}->{$dest} =
2254e71b7053SJung-uk Kim                [ @{$unified_info{includes}->{$dest}->{build}} ];
2255e71b7053SJung-uk Kim            foreach my $inc (@source_includes) {
2256e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$dest}}, $inc
2257e71b7053SJung-uk Kim                    unless grep { $_ eq $inc } @{$unified_info{includes}->{$dest}};
2258e71b7053SJung-uk Kim            }
2259e71b7053SJung-uk Kim        } else {
2260e71b7053SJung-uk Kim            $unified_info{includes}->{$dest} =
2261e71b7053SJung-uk Kim                [ @{$unified_info{includes}->{$dest}->{source}} ];
2262e71b7053SJung-uk Kim        }
2263e71b7053SJung-uk Kim    }
2264c9cf7b5cSJung-uk Kim
2265c9cf7b5cSJung-uk Kim    # For convenience collect information regarding directories where
2266c9cf7b5cSJung-uk Kim    # files are generated, those generated files and the end product
2267c9cf7b5cSJung-uk Kim    # they end up in where applicable.  Then, add build rules for those
2268c9cf7b5cSJung-uk Kim    # directories
2269c9cf7b5cSJung-uk Kim    my %loopinfo = ( "lib" => [ @{$unified_info{libraries}} ],
2270c9cf7b5cSJung-uk Kim                     "dso" => [ @{$unified_info{engines}} ],
2271c9cf7b5cSJung-uk Kim                     "bin" => [ @{$unified_info{programs}} ],
2272c9cf7b5cSJung-uk Kim                     "script" => [ @{$unified_info{scripts}} ] );
2273c9cf7b5cSJung-uk Kim    foreach my $type (keys %loopinfo) {
2274c9cf7b5cSJung-uk Kim        foreach my $product (@{$loopinfo{$type}}) {
2275c9cf7b5cSJung-uk Kim            my %dirs = ();
2276c9cf7b5cSJung-uk Kim            my $pd = dirname($product);
2277c9cf7b5cSJung-uk Kim
2278c9cf7b5cSJung-uk Kim            foreach (@{$unified_info{sources}->{$product} // []},
2279c9cf7b5cSJung-uk Kim                     @{$unified_info{shared_sources}->{$product} // []}) {
2280c9cf7b5cSJung-uk Kim                my $d = dirname($_);
2281c9cf7b5cSJung-uk Kim
2282c9cf7b5cSJung-uk Kim                # We don't want to create targets for source directories
2283c9cf7b5cSJung-uk Kim                # when building out of source
2284c9cf7b5cSJung-uk Kim                next if ($config{sourcedir} ne $config{builddir}
2285c9cf7b5cSJung-uk Kim                             && $d =~ m|^\Q$config{sourcedir}\E|);
2286c9cf7b5cSJung-uk Kim                # We already have a "test" target, and the current directory
2287c9cf7b5cSJung-uk Kim                # is just silly to make a target for
2288c9cf7b5cSJung-uk Kim                next if $d eq "test" || $d eq ".";
2289c9cf7b5cSJung-uk Kim
2290c9cf7b5cSJung-uk Kim                $dirs{$d} = 1;
2291c9cf7b5cSJung-uk Kim                push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
2292c9cf7b5cSJung-uk Kim                    if $d ne $pd;
2293c9cf7b5cSJung-uk Kim            }
2294c9cf7b5cSJung-uk Kim            foreach (keys %dirs) {
2295c9cf7b5cSJung-uk Kim                push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
2296c9cf7b5cSJung-uk Kim                    $product;
2297c9cf7b5cSJung-uk Kim            }
2298c9cf7b5cSJung-uk Kim        }
2299c9cf7b5cSJung-uk Kim    }
2300e71b7053SJung-uk Kim}
2301e71b7053SJung-uk Kim
2302e71b7053SJung-uk Kim# For the schemes that need it, we provide the old *_obj configs
2303e71b7053SJung-uk Kim# from the *_asm_obj ones
2304e71b7053SJung-uk Kimforeach (grep /_(asm|aux)_src$/, keys %target) {
2305e71b7053SJung-uk Kim    my $src = $_;
2306e71b7053SJung-uk Kim    (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
2307e71b7053SJung-uk Kim    $target{$obj} = $target{$src};
2308e71b7053SJung-uk Kim    $target{$obj} =~ s/\.[csS]\b/.o/g; # C and assembler
2309e71b7053SJung-uk Kim    $target{$obj} =~ s/\.(cc|cpp)\b/_cc.o/g; # C++
2310e71b7053SJung-uk Kim}
2311e71b7053SJung-uk Kim
2312e71b7053SJung-uk Kim# Write down our configuration where it fits #########################
2313e71b7053SJung-uk Kim
2314e71b7053SJung-uk Kimprint "Creating configdata.pm\n";
2315e71b7053SJung-uk Kimopen(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
2316e71b7053SJung-uk Kimprint OUT <<"EOF";
2317e71b7053SJung-uk Kim#! $config{HASHBANGPERL}
2318e71b7053SJung-uk Kim
2319e71b7053SJung-uk Kimpackage configdata;
2320e71b7053SJung-uk Kim
2321e71b7053SJung-uk Kimuse strict;
2322e71b7053SJung-uk Kimuse warnings;
2323e71b7053SJung-uk Kim
2324e71b7053SJung-uk Kimuse Exporter;
2325e71b7053SJung-uk Kim#use vars qw(\@ISA \@EXPORT);
2326e71b7053SJung-uk Kimour \@ISA = qw(Exporter);
2327e71b7053SJung-uk Kimour \@EXPORT = qw(\%config \%target \%disabled \%withargs \%unified_info \@disablables);
2328e71b7053SJung-uk Kim
2329e71b7053SJung-uk KimEOF
2330e71b7053SJung-uk Kimprint OUT "our %config = (\n";
2331e71b7053SJung-uk Kimforeach (sort keys %config) {
2332e71b7053SJung-uk Kim    if (ref($config{$_}) eq "ARRAY") {
2333e71b7053SJung-uk Kim        print OUT "  ", $_, " => [ ", join(", ",
2334e71b7053SJung-uk Kim                                           map { quotify("perl", $_) }
2335e71b7053SJung-uk Kim                                           @{$config{$_}}), " ],\n";
2336e71b7053SJung-uk Kim    } elsif (ref($config{$_}) eq "HASH") {
2337e71b7053SJung-uk Kim        print OUT "  ", $_, " => {";
2338e71b7053SJung-uk Kim        if (scalar keys %{$config{$_}} > 0) {
2339db522d3aSSimon L. B. Nielsen            print OUT "\n";
2340e71b7053SJung-uk Kim            foreach my $key (sort keys %{$config{$_}}) {
2341e71b7053SJung-uk Kim                print OUT "      ",
2342e71b7053SJung-uk Kim                    join(" => ",
2343e71b7053SJung-uk Kim                         quotify("perl", $key),
2344e71b7053SJung-uk Kim                         defined $config{$_}->{$key}
2345e71b7053SJung-uk Kim                             ? quotify("perl", $config{$_}->{$key})
2346e71b7053SJung-uk Kim                             : "undef");
2347e71b7053SJung-uk Kim                print OUT ",\n";
23486a599222SSimon L. B. Nielsen            }
2349e71b7053SJung-uk Kim            print OUT "  ";
23506a599222SSimon L. B. Nielsen        }
2351e71b7053SJung-uk Kim        print OUT "},\n";
235274664626SKris Kennaway    } else {
2353e71b7053SJung-uk Kim        print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
2354ed7112f0SJung-uk Kim    }
2355e71b7053SJung-uk Kim}
2356e71b7053SJung-uk Kimprint OUT <<"EOF";
2357e71b7053SJung-uk Kim);
2358e71b7053SJung-uk Kim
2359e71b7053SJung-uk KimEOF
2360e71b7053SJung-uk Kimprint OUT "our %target = (\n";
2361e71b7053SJung-uk Kimforeach (sort keys %target) {
2362e71b7053SJung-uk Kim    if (ref($target{$_}) eq "ARRAY") {
2363e71b7053SJung-uk Kim        print OUT "  ", $_, " => [ ", join(", ",
2364e71b7053SJung-uk Kim                                           map { quotify("perl", $_) }
2365e71b7053SJung-uk Kim                                           @{$target{$_}}), " ],\n";
236674664626SKris Kennaway    } else {
2367e71b7053SJung-uk Kim        print OUT "  ", $_, " => ", quotify("perl", $target{$_}), ",\n"
236874664626SKris Kennaway    }
2369e71b7053SJung-uk Kim}
2370e71b7053SJung-uk Kimprint OUT <<"EOF";
2371e71b7053SJung-uk Kim);
2372e71b7053SJung-uk Kim
2373e71b7053SJung-uk KimEOF
2374e71b7053SJung-uk Kimprint OUT "our \%available_protocols = (\n";
2375e71b7053SJung-uk Kimprint OUT "  tls => [ ", join(", ", map { quotify("perl", $_) } @tls), " ],\n";
2376e71b7053SJung-uk Kimprint OUT "  dtls => [ ", join(", ", map { quotify("perl", $_) } @dtls), " ],\n";
2377e71b7053SJung-uk Kimprint OUT <<"EOF";
2378e71b7053SJung-uk Kim);
2379e71b7053SJung-uk Kim
2380e71b7053SJung-uk KimEOF
2381e71b7053SJung-uk Kimprint OUT "our \@disablables = (\n";
2382e71b7053SJung-uk Kimforeach (@disablables) {
2383e71b7053SJung-uk Kim    print OUT "  ", quotify("perl", $_), ",\n";
2384e71b7053SJung-uk Kim}
2385e71b7053SJung-uk Kimprint OUT <<"EOF";
2386e71b7053SJung-uk Kim);
2387e71b7053SJung-uk Kim
2388e71b7053SJung-uk KimEOF
2389e71b7053SJung-uk Kimprint OUT "our \%disabled = (\n";
2390e71b7053SJung-uk Kimforeach (sort keys %disabled) {
2391e71b7053SJung-uk Kim    print OUT "  ", quotify("perl", $_), " => ", quotify("perl", $disabled{$_}), ",\n";
2392e71b7053SJung-uk Kim}
2393e71b7053SJung-uk Kimprint OUT <<"EOF";
2394e71b7053SJung-uk Kim);
2395e71b7053SJung-uk Kim
2396e71b7053SJung-uk KimEOF
2397e71b7053SJung-uk Kimprint OUT "our %withargs = (\n";
2398e71b7053SJung-uk Kimforeach (sort keys %withargs) {
2399e71b7053SJung-uk Kim    if (ref($withargs{$_}) eq "ARRAY") {
2400e71b7053SJung-uk Kim        print OUT "  ", $_, " => [ ", join(", ",
2401e71b7053SJung-uk Kim                                           map { quotify("perl", $_) }
2402e71b7053SJung-uk Kim                                           @{$withargs{$_}}), " ],\n";
2403e71b7053SJung-uk Kim    } else {
2404e71b7053SJung-uk Kim        print OUT "  ", $_, " => ", quotify("perl", $withargs{$_}), ",\n"
2405e71b7053SJung-uk Kim    }
2406e71b7053SJung-uk Kim}
2407e71b7053SJung-uk Kimprint OUT <<"EOF";
2408e71b7053SJung-uk Kim);
2409e71b7053SJung-uk Kim
2410e71b7053SJung-uk KimEOF
2411e71b7053SJung-uk Kimif ($builder eq "unified") {
2412e71b7053SJung-uk Kim    my $recurse;
2413e71b7053SJung-uk Kim    $recurse = sub {
2414e71b7053SJung-uk Kim        my $indent = shift;
2415e71b7053SJung-uk Kim        foreach (@_) {
2416e71b7053SJung-uk Kim            if (ref $_ eq "ARRAY") {
2417e71b7053SJung-uk Kim                print OUT " "x$indent, "[\n";
2418e71b7053SJung-uk Kim                foreach (@$_) {
2419e71b7053SJung-uk Kim                    $recurse->($indent + 4, $_);
2420e71b7053SJung-uk Kim                }
2421e71b7053SJung-uk Kim                print OUT " "x$indent, "],\n";
2422e71b7053SJung-uk Kim            } elsif (ref $_ eq "HASH") {
2423e71b7053SJung-uk Kim                my %h = %$_;
2424e71b7053SJung-uk Kim                print OUT " "x$indent, "{\n";
2425e71b7053SJung-uk Kim                foreach (sort keys %h) {
2426e71b7053SJung-uk Kim                    if (ref $h{$_} eq "") {
2427e71b7053SJung-uk Kim                        print OUT " "x($indent + 4), quotify("perl", $_), " => ", quotify("perl", $h{$_}), ",\n";
2428e71b7053SJung-uk Kim                    } else {
2429e71b7053SJung-uk Kim                        print OUT " "x($indent + 4), quotify("perl", $_), " =>\n";
2430e71b7053SJung-uk Kim                        $recurse->($indent + 8, $h{$_});
2431e71b7053SJung-uk Kim                    }
2432e71b7053SJung-uk Kim                }
2433e71b7053SJung-uk Kim                print OUT " "x$indent, "},\n";
2434e71b7053SJung-uk Kim            } else {
2435e71b7053SJung-uk Kim                print OUT " "x$indent, quotify("perl", $_), ",\n";
2436e71b7053SJung-uk Kim            }
2437e71b7053SJung-uk Kim        }
2438e71b7053SJung-uk Kim    };
2439e71b7053SJung-uk Kim    print OUT "our %unified_info = (\n";
2440e71b7053SJung-uk Kim    foreach (sort keys %unified_info) {
2441e71b7053SJung-uk Kim        if (ref $unified_info{$_} eq "") {
2442e71b7053SJung-uk Kim            print OUT " "x4, quotify("perl", $_), " => ", quotify("perl", $unified_info{$_}), ",\n";
2443e71b7053SJung-uk Kim        } else {
2444e71b7053SJung-uk Kim            print OUT " "x4, quotify("perl", $_), " =>\n";
2445e71b7053SJung-uk Kim            $recurse->(8, $unified_info{$_});
2446e71b7053SJung-uk Kim        }
2447e71b7053SJung-uk Kim    }
2448e71b7053SJung-uk Kim    print OUT <<"EOF";
2449e71b7053SJung-uk Kim);
2450e71b7053SJung-uk Kim
2451e71b7053SJung-uk KimEOF
2452e71b7053SJung-uk Kim}
2453e71b7053SJung-uk Kimprint OUT
2454e71b7053SJung-uk Kim    "# The following data is only used when this files is use as a script\n";
2455e71b7053SJung-uk Kimprint OUT "my \@makevars = (\n";
2456e71b7053SJung-uk Kimforeach (sort keys %user) {
2457e71b7053SJung-uk Kim    print OUT "    '",$_,"',\n";
2458e71b7053SJung-uk Kim}
2459e71b7053SJung-uk Kimprint OUT ");\n";
2460e71b7053SJung-uk Kimprint OUT "my \%disabled_info = (\n";
2461e71b7053SJung-uk Kimforeach my $what (sort keys %disabled_info) {
2462e71b7053SJung-uk Kim    print OUT "    '$what' => {\n";
2463e71b7053SJung-uk Kim    foreach my $info (sort keys %{$disabled_info{$what}}) {
2464e71b7053SJung-uk Kim        if (ref $disabled_info{$what}->{$info} eq 'ARRAY') {
2465e71b7053SJung-uk Kim            print OUT "        $info => [ ",
2466e71b7053SJung-uk Kim                join(', ', map { "'$_'" } @{$disabled_info{$what}->{$info}}),
2467e71b7053SJung-uk Kim                " ],\n";
2468e71b7053SJung-uk Kim        } else {
2469e71b7053SJung-uk Kim            print OUT "        $info => '", $disabled_info{$what}->{$info},
2470e71b7053SJung-uk Kim                "',\n";
2471e71b7053SJung-uk Kim        }
2472e71b7053SJung-uk Kim    }
2473e71b7053SJung-uk Kim    print OUT "    },\n";
2474e71b7053SJung-uk Kim}
2475e71b7053SJung-uk Kimprint OUT ");\n";
2476e71b7053SJung-uk Kimprint OUT 'my @user_crossable = qw( ', join (' ', @user_crossable), " );\n";
2477e71b7053SJung-uk Kimprint OUT << 'EOF';
2478e71b7053SJung-uk Kim# If run directly, we can give some answers, and even reconfigure
2479e71b7053SJung-uk Kimunless (caller) {
2480e71b7053SJung-uk Kim    use Getopt::Long;
2481e71b7053SJung-uk Kim    use File::Spec::Functions;
2482e71b7053SJung-uk Kim    use File::Basename;
2483e71b7053SJung-uk Kim    use Pod::Usage;
2484e71b7053SJung-uk Kim
2485e71b7053SJung-uk Kim    my $here = dirname($0);
2486e71b7053SJung-uk Kim
2487e71b7053SJung-uk Kim    my $dump = undef;
2488e71b7053SJung-uk Kim    my $cmdline = undef;
2489e71b7053SJung-uk Kim    my $options = undef;
2490e71b7053SJung-uk Kim    my $target = undef;
2491e71b7053SJung-uk Kim    my $envvars = undef;
2492e71b7053SJung-uk Kim    my $makevars = undef;
2493e71b7053SJung-uk Kim    my $buildparams = undef;
2494e71b7053SJung-uk Kim    my $reconf = undef;
2495e71b7053SJung-uk Kim    my $verbose = undef;
2496e71b7053SJung-uk Kim    my $help = undef;
2497e71b7053SJung-uk Kim    my $man = undef;
2498e71b7053SJung-uk Kim    GetOptions('dump|d'                 => \$dump,
2499e71b7053SJung-uk Kim               'command-line|c'         => \$cmdline,
2500e71b7053SJung-uk Kim               'options|o'              => \$options,
2501e71b7053SJung-uk Kim               'target|t'               => \$target,
2502e71b7053SJung-uk Kim               'environment|e'          => \$envvars,
2503e71b7053SJung-uk Kim               'make-variables|m'       => \$makevars,
2504e71b7053SJung-uk Kim               'build-parameters|b'     => \$buildparams,
2505e71b7053SJung-uk Kim               'reconfigure|reconf|r'   => \$reconf,
2506e71b7053SJung-uk Kim               'verbose|v'              => \$verbose,
2507e71b7053SJung-uk Kim               'help'                   => \$help,
2508e71b7053SJung-uk Kim               'man'                    => \$man)
2509e71b7053SJung-uk Kim        or die "Errors in command line arguments\n";
2510e71b7053SJung-uk Kim
2511e71b7053SJung-uk Kim    unless ($dump || $cmdline || $options || $target || $envvars || $makevars
2512e71b7053SJung-uk Kim            || $buildparams || $reconf || $verbose || $help || $man) {
2513e71b7053SJung-uk Kim        print STDERR <<"_____";
2514e71b7053SJung-uk KimYou must give at least one option.
2515e71b7053SJung-uk KimFor more information, do '$0 --help'
2516e71b7053SJung-uk Kim_____
2517e71b7053SJung-uk Kim        exit(2);
2518e71b7053SJung-uk Kim    }
2519e71b7053SJung-uk Kim
2520e71b7053SJung-uk Kim    if ($help) {
2521e71b7053SJung-uk Kim        pod2usage(-exitval => 0,
2522e71b7053SJung-uk Kim                  -verbose => 1);
2523e71b7053SJung-uk Kim    }
2524e71b7053SJung-uk Kim    if ($man) {
2525e71b7053SJung-uk Kim        pod2usage(-exitval => 0,
2526e71b7053SJung-uk Kim                  -verbose => 2);
2527e71b7053SJung-uk Kim    }
2528e71b7053SJung-uk Kim    if ($dump || $cmdline) {
2529e71b7053SJung-uk Kim        print "\nCommand line (with current working directory = $here):\n\n";
2530e71b7053SJung-uk Kim        print '    ',join(' ',
2531e71b7053SJung-uk Kim                          $config{PERL},
2532e71b7053SJung-uk Kim                          catfile($config{sourcedir}, 'Configure'),
2533e71b7053SJung-uk Kim                          @{$config{perlargv}}), "\n";
2534e71b7053SJung-uk Kim        print "\nPerl information:\n\n";
2535e71b7053SJung-uk Kim        print '    ',$config{perl_cmd},"\n";
2536e71b7053SJung-uk Kim        print '    ',$config{perl_version},' for ',$config{perl_archname},"\n";
2537e71b7053SJung-uk Kim    }
2538e71b7053SJung-uk Kim    if ($dump || $options) {
2539e71b7053SJung-uk Kim        my $longest = 0;
2540e71b7053SJung-uk Kim        my $longest2 = 0;
2541e71b7053SJung-uk Kim        foreach my $what (@disablables) {
2542e71b7053SJung-uk Kim            $longest = length($what) if $longest < length($what);
2543e71b7053SJung-uk Kim            $longest2 = length($disabled{$what})
2544e71b7053SJung-uk Kim                if $disabled{$what} && $longest2 < length($disabled{$what});
2545e71b7053SJung-uk Kim        }
2546e71b7053SJung-uk Kim        print "\nEnabled features:\n\n";
2547e71b7053SJung-uk Kim        foreach my $what (@disablables) {
2548e71b7053SJung-uk Kim            print "    $what\n" unless $disabled{$what};
2549e71b7053SJung-uk Kim        }
2550e71b7053SJung-uk Kim        print "\nDisabled features:\n\n";
2551e71b7053SJung-uk Kim        foreach my $what (@disablables) {
2552e71b7053SJung-uk Kim            if ($disabled{$what}) {
2553e71b7053SJung-uk Kim                print "    $what", ' ' x ($longest - length($what) + 1),
2554e71b7053SJung-uk Kim                    "[$disabled{$what}]", ' ' x ($longest2 - length($disabled{$what}) + 1);
2555e71b7053SJung-uk Kim                print $disabled_info{$what}->{macro}
2556e71b7053SJung-uk Kim                    if $disabled_info{$what}->{macro};
2557e71b7053SJung-uk Kim                print ' (skip ',
2558e71b7053SJung-uk Kim                    join(', ', @{$disabled_info{$what}->{skipped}}),
2559e71b7053SJung-uk Kim                    ')'
2560e71b7053SJung-uk Kim                    if $disabled_info{$what}->{skipped};
2561e71b7053SJung-uk Kim                print "\n";
2562e71b7053SJung-uk Kim            }
2563e71b7053SJung-uk Kim        }
2564e71b7053SJung-uk Kim    }
2565e71b7053SJung-uk Kim    if ($dump || $target) {
2566e71b7053SJung-uk Kim        print "\nConfig target attributes:\n\n";
2567e71b7053SJung-uk Kim        foreach (sort keys %target) {
2568e71b7053SJung-uk Kim            next if $_ =~ m|^_| || $_ eq 'template';
2569e71b7053SJung-uk Kim            my $quotify = sub {
2570e71b7053SJung-uk Kim                map { (my $x = $_) =~ s|([\\\$\@"])|\\$1|g; "\"$x\""} @_;
2571e71b7053SJung-uk Kim            };
2572e71b7053SJung-uk Kim            print '    ', $_, ' => ';
2573e71b7053SJung-uk Kim            if (ref($target{$_}) eq "ARRAY") {
2574e71b7053SJung-uk Kim                print '[ ', join(', ', $quotify->(@{$target{$_}})), " ],\n";
2575e71b7053SJung-uk Kim            } else {
2576e71b7053SJung-uk Kim                print $quotify->($target{$_}), ",\n"
2577e71b7053SJung-uk Kim            }
2578e71b7053SJung-uk Kim        }
2579e71b7053SJung-uk Kim    }
2580e71b7053SJung-uk Kim    if ($dump || $envvars) {
2581e71b7053SJung-uk Kim        print "\nRecorded environment:\n\n";
2582e71b7053SJung-uk Kim        foreach (sort keys %{$config{perlenv}}) {
2583e71b7053SJung-uk Kim            print '    ',$_,' = ',($config{perlenv}->{$_} || ''),"\n";
2584e71b7053SJung-uk Kim        }
2585e71b7053SJung-uk Kim    }
2586e71b7053SJung-uk Kim    if ($dump || $makevars) {
2587e71b7053SJung-uk Kim        print "\nMakevars:\n\n";
2588e71b7053SJung-uk Kim        foreach my $var (@makevars) {
2589e71b7053SJung-uk Kim            my $prefix = '';
2590e71b7053SJung-uk Kim            $prefix = $config{CROSS_COMPILE}
2591e71b7053SJung-uk Kim                if grep { $var eq $_ } @user_crossable;
2592e71b7053SJung-uk Kim            $prefix //= '';
2593e71b7053SJung-uk Kim            print '    ',$var,' ' x (16 - length $var),'= ',
2594e71b7053SJung-uk Kim                (ref $config{$var} eq 'ARRAY'
2595e71b7053SJung-uk Kim                 ? join(' ', @{$config{$var}})
2596e71b7053SJung-uk Kim                 : $prefix.$config{$var}),
2597e71b7053SJung-uk Kim                "\n"
2598e71b7053SJung-uk Kim                if defined $config{$var};
2599e71b7053SJung-uk Kim        }
2600e71b7053SJung-uk Kim
2601e71b7053SJung-uk Kim        my @buildfile = ($config{builddir}, $config{build_file});
2602e71b7053SJung-uk Kim        unshift @buildfile, $here
2603e71b7053SJung-uk Kim            unless file_name_is_absolute($config{builddir});
2604e71b7053SJung-uk Kim        my $buildfile = canonpath(catdir(@buildfile));
2605e71b7053SJung-uk Kim        print <<"_____";
2606e71b7053SJung-uk Kim
2607e71b7053SJung-uk KimNOTE: These variables only represent the configuration view.  The build file
2608e71b7053SJung-uk Kimtemplate may have processed these variables further, please have a look at the
2609e71b7053SJung-uk Kimbuild file for more exact data:
2610e71b7053SJung-uk Kim    $buildfile
2611e71b7053SJung-uk Kim_____
2612e71b7053SJung-uk Kim    }
2613e71b7053SJung-uk Kim    if ($dump || $buildparams) {
2614e71b7053SJung-uk Kim        my @buildfile = ($config{builddir}, $config{build_file});
2615e71b7053SJung-uk Kim        unshift @buildfile, $here
2616e71b7053SJung-uk Kim            unless file_name_is_absolute($config{builddir});
2617e71b7053SJung-uk Kim        print "\nbuild file:\n\n";
2618e71b7053SJung-uk Kim        print "    ", canonpath(catfile(@buildfile)),"\n";
2619e71b7053SJung-uk Kim
2620e71b7053SJung-uk Kim        print "\nbuild file templates:\n\n";
2621e71b7053SJung-uk Kim        foreach (@{$config{build_file_templates}}) {
2622e71b7053SJung-uk Kim            my @tmpl = ($_);
2623e71b7053SJung-uk Kim            unshift @tmpl, $here
2624e71b7053SJung-uk Kim                unless file_name_is_absolute($config{sourcedir});
2625e71b7053SJung-uk Kim            print '    ',canonpath(catfile(@tmpl)),"\n";
2626e71b7053SJung-uk Kim        }
2627e71b7053SJung-uk Kim    }
2628e71b7053SJung-uk Kim    if ($reconf) {
2629e71b7053SJung-uk Kim        if ($verbose) {
2630e71b7053SJung-uk Kim            print 'Reconfiguring with: ', join(' ',@{$config{perlargv}}), "\n";
2631e71b7053SJung-uk Kim            foreach (sort keys %{$config{perlenv}}) {
2632e71b7053SJung-uk Kim                print '    ',$_,' = ',($config{perlenv}->{$_} || ""),"\n";
26335c87c606SMark Murray            }
263474664626SKris Kennaway        }
263574664626SKris Kennaway
2636e71b7053SJung-uk Kim        chdir $here;
2637e71b7053SJung-uk Kim        exec $^X,catfile($config{sourcedir}, 'Configure'),'reconf';
26383b4e3dcbSSimon L. B. Nielsen    }
2639e71b7053SJung-uk Kim}
26403b4e3dcbSSimon L. B. Nielsen
2641e71b7053SJung-uk Kim1;
26423b4e3dcbSSimon L. B. Nielsen
2643e71b7053SJung-uk Kim__END__
2644e71b7053SJung-uk Kim
2645e71b7053SJung-uk Kim=head1 NAME
2646e71b7053SJung-uk Kim
2647e71b7053SJung-uk Kimconfigdata.pm - configuration data for OpenSSL builds
2648e71b7053SJung-uk Kim
2649e71b7053SJung-uk Kim=head1 SYNOPSIS
2650e71b7053SJung-uk Kim
2651e71b7053SJung-uk KimInteractive:
2652e71b7053SJung-uk Kim
2653e71b7053SJung-uk Kim  perl configdata.pm [options]
2654e71b7053SJung-uk Kim
2655e71b7053SJung-uk KimAs data bank module:
2656e71b7053SJung-uk Kim
2657e71b7053SJung-uk Kim  use configdata;
2658e71b7053SJung-uk Kim
2659e71b7053SJung-uk Kim=head1 DESCRIPTION
2660e71b7053SJung-uk Kim
2661e71b7053SJung-uk KimThis module can be used in two modes, interactively and as a module containing
2662e71b7053SJung-uk Kimall the data recorded by OpenSSL's Configure script.
2663e71b7053SJung-uk Kim
2664e71b7053SJung-uk KimWhen used interactively, simply run it as any perl script, with at least one
2665e71b7053SJung-uk Kimoption, and you will get the information you ask for.  See L</OPTIONS> below.
2666e71b7053SJung-uk Kim
2667e71b7053SJung-uk KimWhen loaded as a module, you get a few databanks with useful information to
2668e71b7053SJung-uk Kimperform build related tasks.  The databanks are:
2669e71b7053SJung-uk Kim
2670e71b7053SJung-uk Kim    %config             Configured things.
2671e71b7053SJung-uk Kim    %target             The OpenSSL config target with all inheritances
2672e71b7053SJung-uk Kim                        resolved.
2673e71b7053SJung-uk Kim    %disabled           The features that are disabled.
2674e71b7053SJung-uk Kim    @disablables        The list of features that can be disabled.
2675e71b7053SJung-uk Kim    %withargs           All data given through --with-THING options.
2676e71b7053SJung-uk Kim    %unified_info       All information that was computed from the build.info
2677e71b7053SJung-uk Kim                        files.
2678e71b7053SJung-uk Kim
2679e71b7053SJung-uk Kim=head1 OPTIONS
2680e71b7053SJung-uk Kim
2681e71b7053SJung-uk Kim=over 4
2682e71b7053SJung-uk Kim
2683e71b7053SJung-uk Kim=item B<--help>
2684e71b7053SJung-uk Kim
2685e71b7053SJung-uk KimPrint a brief help message and exit.
2686e71b7053SJung-uk Kim
2687e71b7053SJung-uk Kim=item B<--man>
2688e71b7053SJung-uk Kim
2689e71b7053SJung-uk KimPrint the manual page and exit.
2690e71b7053SJung-uk Kim
2691e71b7053SJung-uk Kim=item B<--dump> | B<-d>
2692e71b7053SJung-uk Kim
2693e71b7053SJung-uk KimPrint all relevant configuration data.  This is equivalent to B<--command-line>
2694e71b7053SJung-uk KimB<--options> B<--target> B<--environment> B<--make-variables>
2695e71b7053SJung-uk KimB<--build-parameters>.
2696e71b7053SJung-uk Kim
2697e71b7053SJung-uk Kim=item B<--command-line> | B<-c>
2698e71b7053SJung-uk Kim
2699e71b7053SJung-uk KimPrint the current configuration command line.
2700e71b7053SJung-uk Kim
2701e71b7053SJung-uk Kim=item B<--options> | B<-o>
2702e71b7053SJung-uk Kim
2703e71b7053SJung-uk KimPrint the features, both enabled and disabled, and display defined macro and
2704e71b7053SJung-uk Kimskipped directories where applicable.
2705e71b7053SJung-uk Kim
2706e71b7053SJung-uk Kim=item B<--target> | B<-t>
2707e71b7053SJung-uk Kim
2708e71b7053SJung-uk KimPrint the config attributes for this config target.
2709e71b7053SJung-uk Kim
2710e71b7053SJung-uk Kim=item B<--environment> | B<-e>
2711e71b7053SJung-uk Kim
2712e71b7053SJung-uk KimPrint the environment variables and their values at the time of configuration.
2713e71b7053SJung-uk Kim
2714e71b7053SJung-uk Kim=item B<--make-variables> | B<-m>
2715e71b7053SJung-uk Kim
2716e71b7053SJung-uk KimPrint the main make variables generated in the current configuration
2717e71b7053SJung-uk Kim
2718e71b7053SJung-uk Kim=item B<--build-parameters> | B<-b>
2719e71b7053SJung-uk Kim
2720e71b7053SJung-uk KimPrint the build parameters, i.e. build file and build file templates.
2721e71b7053SJung-uk Kim
2722e71b7053SJung-uk Kim=item B<--reconfigure> | B<--reconf> | B<-r>
2723e71b7053SJung-uk Kim
2724e71b7053SJung-uk KimRedo the configuration.
2725e71b7053SJung-uk Kim
2726e71b7053SJung-uk Kim=item B<--verbose> | B<-v>
2727e71b7053SJung-uk Kim
2728e71b7053SJung-uk KimVerbose output.
2729e71b7053SJung-uk Kim
2730e71b7053SJung-uk Kim=back
2731e71b7053SJung-uk Kim
2732e71b7053SJung-uk Kim=cut
2733e71b7053SJung-uk Kim
27343b4e3dcbSSimon L. B. NielsenEOF
27353b4e3dcbSSimon L. B. Nielsenclose(OUT);
2736e71b7053SJung-uk Kimif ($builder_platform eq 'unix') {
2737e71b7053SJung-uk Kim    my $mode = (0755 & ~umask);
2738e71b7053SJung-uk Kim    chmod $mode, 'configdata.pm'
2739e71b7053SJung-uk Kim        or warn sprintf("WARNING: Couldn't change mode for 'configdata.pm' to 0%03o: %s\n",$mode,$!);
27403b4e3dcbSSimon L. B. Nielsen}
27413b4e3dcbSSimon L. B. Nielsen
2742e71b7053SJung-uk Kimmy %builders = (
2743e71b7053SJung-uk Kim    unified => sub {
2744e71b7053SJung-uk Kim        print 'Creating ',$target{build_file},"\n";
2745e71b7053SJung-uk Kim        run_dofile(catfile($blddir, $target{build_file}),
2746e71b7053SJung-uk Kim                   @{$config{build_file_templates}});
2747e71b7053SJung-uk Kim    },
2748e71b7053SJung-uk Kim    );
274974664626SKris Kennaway
2750e71b7053SJung-uk Kim$builders{$builder}->($builder_platform, @builder_opts);
275174664626SKris Kennaway
2752e71b7053SJung-uk Kim$SIG{__DIE__} = $orig_death_handler;
2753e71b7053SJung-uk Kim
2754e71b7053SJung-uk Kimprint <<"EOF" if ($disabled{threads} eq "unavailable");
275574664626SKris Kennaway
275674664626SKris KennawayThe library could not be configured for supporting multi-threaded
275774664626SKris Kennawayapplications as the compiler options required on this system are not known.
275874664626SKris KennawaySee file INSTALL for details if you need multi-threading.
275974664626SKris KennawayEOF
276074664626SKris Kennaway
2761e71b7053SJung-uk Kimprint <<"EOF" if ($no_shared_warn);
2762fceca8a3SJacques Vidrine
2763e71b7053SJung-uk KimThe options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
2764e71b7053SJung-uk Kimplatform, so we will pretend you gave the option 'no-pic', which also disables
2765e71b7053SJung-uk Kim'shared' and 'dynamic-engine'.  If you know how to implement shared libraries
2766e71b7053SJung-uk Kimor position independent code, please let us know (but please first make sure
2767e71b7053SJung-uk Kimyou have tried with a current version of OpenSSL).
27688180e704SJung-uk KimEOF
27698180e704SJung-uk Kim
2770e71b7053SJung-uk Kimprint <<"EOF";
27718180e704SJung-uk Kim
2772e71b7053SJung-uk Kim**********************************************************************
2773e71b7053SJung-uk Kim***                                                                ***
2774c9cf7b5cSJung-uk Kim***   OpenSSL has been successfully configured                     ***
2775c9cf7b5cSJung-uk Kim***                                                                ***
2776c9cf7b5cSJung-uk Kim***   If you encounter a problem while building, please open an    ***
2777c9cf7b5cSJung-uk Kim***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
2778c9cf7b5cSJung-uk Kim***   and include the output from the following command:           ***
2779e71b7053SJung-uk Kim***                                                                ***
2780e71b7053SJung-uk Kim***       perl configdata.pm --dump                                ***
2781e71b7053SJung-uk Kim***                                                                ***
2782c9cf7b5cSJung-uk Kim***   (If you are new to OpenSSL, you might want to consult the    ***
2783c9cf7b5cSJung-uk Kim***   'Troubleshooting' section in the INSTALL file first)         ***
2784c9cf7b5cSJung-uk Kim***                                                                ***
2785e71b7053SJung-uk Kim**********************************************************************
2786fceca8a3SJacques VidrineEOF
2787fceca8a3SJacques Vidrine
278874664626SKris Kennawayexit(0);
278974664626SKris Kennaway
2790e71b7053SJung-uk Kim######################################################################
2791e71b7053SJung-uk Kim#
2792e71b7053SJung-uk Kim# Helpers and utility functions
2793e71b7053SJung-uk Kim#
2794e71b7053SJung-uk Kim
2795e71b7053SJung-uk Kim# Death handler, to print a helpful message in case of failure #######
2796e71b7053SJung-uk Kim#
2797e71b7053SJung-uk Kimsub death_handler {
2798e71b7053SJung-uk Kim    die @_ if $^S;              # To prevent the added message in eval blocks
2799e71b7053SJung-uk Kim    my $build_file = $target{build_file} // "build file";
2800e71b7053SJung-uk Kim    my @message = ( <<"_____", @_ );
2801e71b7053SJung-uk Kim
2802e71b7053SJung-uk KimFailure!  $build_file wasn't produced.
2803e71b7053SJung-uk KimPlease read INSTALL and associated NOTES files.  You may also have to look over
2804e71b7053SJung-uk Kimyour available compiler tool chain or change your configuration.
2805e71b7053SJung-uk Kim
2806e71b7053SJung-uk Kim_____
2807e71b7053SJung-uk Kim
2808e71b7053SJung-uk Kim    # Dying is terminal, so it's ok to reset the signal handler here.
2809e71b7053SJung-uk Kim    $SIG{__DIE__} = $orig_death_handler;
2810e71b7053SJung-uk Kim    die @message;
2811e71b7053SJung-uk Kim}
2812e71b7053SJung-uk Kim
2813e71b7053SJung-uk Kim# Configuration file reading #########################################
2814e71b7053SJung-uk Kim
2815e71b7053SJung-uk Kim# Note: All of the helper functions are for lazy evaluation.  They all
2816e71b7053SJung-uk Kim# return a CODE ref, which will return the intended value when evaluated.
2817e71b7053SJung-uk Kim# Thus, whenever there's mention of a returned value, it's about that
2818e71b7053SJung-uk Kim# intended value.
2819e71b7053SJung-uk Kim
2820e71b7053SJung-uk Kim# Helper function to implement conditional inheritance depending on the
2821e71b7053SJung-uk Kim# value of $disabled{asm}.  Used in inherit_from values as follows:
2822e71b7053SJung-uk Kim#
2823e71b7053SJung-uk Kim#      inherit_from => [ "template", asm("asm_tmpl") ]
2824e71b7053SJung-uk Kim#
2825e71b7053SJung-uk Kimsub asm {
2826e71b7053SJung-uk Kim    my @x = @_;
2827e71b7053SJung-uk Kim    sub {
2828e71b7053SJung-uk Kim        $disabled{asm} ? () : @x;
2829e71b7053SJung-uk Kim    }
2830e71b7053SJung-uk Kim}
2831e71b7053SJung-uk Kim
2832e71b7053SJung-uk Kim# Helper function to implement conditional value variants, with a default
2833e71b7053SJung-uk Kim# plus additional values based on the value of $config{build_type}.
2834e71b7053SJung-uk Kim# Arguments are given in hash table form:
2835e71b7053SJung-uk Kim#
2836e71b7053SJung-uk Kim#       picker(default => "Basic string: ",
2837e71b7053SJung-uk Kim#              debug   => "debug",
2838e71b7053SJung-uk Kim#              release => "release")
2839e71b7053SJung-uk Kim#
2840e71b7053SJung-uk Kim# When configuring with --debug, the resulting string will be
2841e71b7053SJung-uk Kim# "Basic string: debug", and when not, it will be "Basic string: release"
2842e71b7053SJung-uk Kim#
2843e71b7053SJung-uk Kim# This can be used to create variants of sets of flags according to the
2844e71b7053SJung-uk Kim# build type:
2845e71b7053SJung-uk Kim#
2846e71b7053SJung-uk Kim#       cflags => picker(default => "-Wall",
2847e71b7053SJung-uk Kim#                        debug   => "-g -O0",
2848e71b7053SJung-uk Kim#                        release => "-O3")
2849e71b7053SJung-uk Kim#
2850e71b7053SJung-uk Kimsub picker {
2851e71b7053SJung-uk Kim    my %opts = @_;
2852e71b7053SJung-uk Kim    return sub { add($opts{default} || (),
2853e71b7053SJung-uk Kim                     $opts{$config{build_type}} || ())->(); }
2854e71b7053SJung-uk Kim}
2855e71b7053SJung-uk Kim
2856e71b7053SJung-uk Kim# Helper function to combine several values of different types into one.
2857e71b7053SJung-uk Kim# This is useful if you want to combine a string with the result of a
2858e71b7053SJung-uk Kim# lazy function, such as:
2859e71b7053SJung-uk Kim#
2860e71b7053SJung-uk Kim#       cflags => combine("-Wall", sub { $disabled{zlib} ? () : "-DZLIB" })
2861e71b7053SJung-uk Kim#
2862e71b7053SJung-uk Kimsub combine {
2863e71b7053SJung-uk Kim    my @stuff = @_;
2864e71b7053SJung-uk Kim    return sub { add(@stuff)->(); }
2865e71b7053SJung-uk Kim}
2866e71b7053SJung-uk Kim
2867e71b7053SJung-uk Kim# Helper function to implement conditional values depending on the value
2868e71b7053SJung-uk Kim# of $disabled{threads}.  Can be used as follows:
2869e71b7053SJung-uk Kim#
2870e71b7053SJung-uk Kim#       cflags => combine("-Wall", threads("-pthread"))
2871e71b7053SJung-uk Kim#
2872e71b7053SJung-uk Kimsub threads {
2873e71b7053SJung-uk Kim    my @flags = @_;
2874e71b7053SJung-uk Kim    return sub { add($disabled{threads} ? () : @flags)->(); }
2875e71b7053SJung-uk Kim}
2876e71b7053SJung-uk Kim
2877e71b7053SJung-uk Kimsub shared {
2878e71b7053SJung-uk Kim    my @flags = @_;
2879e71b7053SJung-uk Kim    return sub { add($disabled{shared} ? () : @flags)->(); }
2880e71b7053SJung-uk Kim}
2881e71b7053SJung-uk Kim
2882e71b7053SJung-uk Kimour $add_called = 0;
2883e71b7053SJung-uk Kim# Helper function to implement adding values to already existing configuration
2884e71b7053SJung-uk Kim# values.  It handles elements that are ARRAYs, CODEs and scalars
2885e71b7053SJung-uk Kimsub _add {
2886e71b7053SJung-uk Kim    my $separator = shift;
2887e71b7053SJung-uk Kim
2888e71b7053SJung-uk Kim    # If there's any ARRAY in the collection of values OR the separator
2889e71b7053SJung-uk Kim    # is undef, we will return an ARRAY of combined values, otherwise a
2890e71b7053SJung-uk Kim    # string of joined values with $separator as the separator.
2891e71b7053SJung-uk Kim    my $found_array = !defined($separator);
2892e71b7053SJung-uk Kim
2893e71b7053SJung-uk Kim    my @values =
2894e71b7053SJung-uk Kim        map {
2895e71b7053SJung-uk Kim            my $res = $_;
2896e71b7053SJung-uk Kim            while (ref($res) eq "CODE") {
2897e71b7053SJung-uk Kim                $res = $res->();
2898e71b7053SJung-uk Kim            }
2899e71b7053SJung-uk Kim            if (defined($res)) {
2900e71b7053SJung-uk Kim                if (ref($res) eq "ARRAY") {
2901e71b7053SJung-uk Kim                    $found_array = 1;
2902e71b7053SJung-uk Kim                    @$res;
2903e71b7053SJung-uk Kim                } else {
2904e71b7053SJung-uk Kim                    $res;
2905e71b7053SJung-uk Kim                }
2906e71b7053SJung-uk Kim            } else {
2907e71b7053SJung-uk Kim                ();
2908e71b7053SJung-uk Kim            }
2909e71b7053SJung-uk Kim    } (@_);
2910e71b7053SJung-uk Kim
2911e71b7053SJung-uk Kim    $add_called = 1;
2912e71b7053SJung-uk Kim
2913e71b7053SJung-uk Kim    if ($found_array) {
2914e71b7053SJung-uk Kim        [ @values ];
2915e71b7053SJung-uk Kim    } else {
2916e71b7053SJung-uk Kim        join($separator, grep { defined($_) && $_ ne "" } @values);
2917e71b7053SJung-uk Kim    }
2918e71b7053SJung-uk Kim}
2919e71b7053SJung-uk Kimsub add_before {
2920e71b7053SJung-uk Kim    my $separator = " ";
2921e71b7053SJung-uk Kim    if (ref($_[$#_]) eq "HASH") {
2922e71b7053SJung-uk Kim        my $opts = pop;
2923e71b7053SJung-uk Kim        $separator = $opts->{separator};
2924e71b7053SJung-uk Kim    }
2925e71b7053SJung-uk Kim    my @x = @_;
2926e71b7053SJung-uk Kim    sub { _add($separator, @x, @_) };
2927e71b7053SJung-uk Kim}
2928e71b7053SJung-uk Kimsub add {
2929e71b7053SJung-uk Kim    my $separator = " ";
2930e71b7053SJung-uk Kim    if (ref($_[$#_]) eq "HASH") {
2931e71b7053SJung-uk Kim        my $opts = pop;
2932e71b7053SJung-uk Kim        $separator = $opts->{separator};
2933e71b7053SJung-uk Kim    }
2934e71b7053SJung-uk Kim    my @x = @_;
2935e71b7053SJung-uk Kim    sub { _add($separator, @_, @x) };
2936e71b7053SJung-uk Kim}
2937e71b7053SJung-uk Kim
2938e71b7053SJung-uk Kimsub read_eval_file {
2939e71b7053SJung-uk Kim    my $fname = shift;
2940e71b7053SJung-uk Kim    my $content;
2941e71b7053SJung-uk Kim    my @result;
2942e71b7053SJung-uk Kim
2943e71b7053SJung-uk Kim    open F, "< $fname" or die "Can't open '$fname': $!\n";
2944e71b7053SJung-uk Kim    {
2945e71b7053SJung-uk Kim        undef local $/;
2946e71b7053SJung-uk Kim        $content = <F>;
2947e71b7053SJung-uk Kim    }
2948e71b7053SJung-uk Kim    close F;
2949e71b7053SJung-uk Kim    {
2950e71b7053SJung-uk Kim        local $@;
2951e71b7053SJung-uk Kim
2952e71b7053SJung-uk Kim        @result = ( eval $content );
2953e71b7053SJung-uk Kim        warn $@ if $@;
2954e71b7053SJung-uk Kim    }
2955e71b7053SJung-uk Kim    return wantarray ? @result : $result[0];
2956e71b7053SJung-uk Kim}
2957e71b7053SJung-uk Kim
2958e71b7053SJung-uk Kim# configuration reader, evaluates the input file as a perl script and expects
2959e71b7053SJung-uk Kim# it to fill %targets with target configurations.  Those are then added to
2960e71b7053SJung-uk Kim# %table.
2961e71b7053SJung-uk Kimsub read_config {
2962e71b7053SJung-uk Kim    my $fname = shift;
2963e71b7053SJung-uk Kim    my %targets;
2964e71b7053SJung-uk Kim
2965e71b7053SJung-uk Kim    {
2966e71b7053SJung-uk Kim        # Protect certain tables from tampering
2967e71b7053SJung-uk Kim        local %table = ();
2968e71b7053SJung-uk Kim
2969e71b7053SJung-uk Kim        %targets = read_eval_file($fname);
2970e71b7053SJung-uk Kim    }
2971e71b7053SJung-uk Kim    my %preexisting = ();
2972e71b7053SJung-uk Kim    foreach (sort keys %targets) {
2973e71b7053SJung-uk Kim        $preexisting{$_} = 1 if $table{$_};
2974e71b7053SJung-uk Kim    }
2975e71b7053SJung-uk Kim    die <<"EOF",
2976e71b7053SJung-uk KimThe following config targets from $fname
2977e71b7053SJung-uk Kimshadow pre-existing config targets with the same name:
2978e71b7053SJung-uk KimEOF
2979e71b7053SJung-uk Kim        map { "  $_\n" } sort keys %preexisting
2980e71b7053SJung-uk Kim        if %preexisting;
2981e71b7053SJung-uk Kim
2982e71b7053SJung-uk Kim
2983e71b7053SJung-uk Kim    # For each target, check that it's configured with a hash table.
2984e71b7053SJung-uk Kim    foreach (keys %targets) {
2985e71b7053SJung-uk Kim        if (ref($targets{$_}) ne "HASH") {
2986e71b7053SJung-uk Kim            if (ref($targets{$_}) eq "") {
2987e71b7053SJung-uk Kim                warn "Deprecated target configuration for $_, ignoring...\n";
2988e71b7053SJung-uk Kim            } else {
2989e71b7053SJung-uk Kim                warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
2990e71b7053SJung-uk Kim            }
2991e71b7053SJung-uk Kim            delete $targets{$_};
2992e71b7053SJung-uk Kim        } else {
2993e71b7053SJung-uk Kim            $targets{$_}->{_conf_fname_int} = add([ $fname ]);
2994e71b7053SJung-uk Kim        }
2995e71b7053SJung-uk Kim    }
2996e71b7053SJung-uk Kim
2997e71b7053SJung-uk Kim    %table = (%table, %targets);
2998e71b7053SJung-uk Kim
2999e71b7053SJung-uk Kim}
3000e71b7053SJung-uk Kim
3001e71b7053SJung-uk Kim# configuration resolver.  Will only resolve all the lazy evaluation
3002e71b7053SJung-uk Kim# codeblocks for the chosen target and all those it inherits from,
3003e71b7053SJung-uk Kim# recursively
3004e71b7053SJung-uk Kimsub resolve_config {
3005e71b7053SJung-uk Kim    my $target = shift;
3006e71b7053SJung-uk Kim    my @breadcrumbs = @_;
3007e71b7053SJung-uk Kim
3008e71b7053SJung-uk Kim#    my $extra_checks = defined($ENV{CONFIGURE_EXTRA_CHECKS});
3009e71b7053SJung-uk Kim
3010e71b7053SJung-uk Kim    if (grep { $_ eq $target } @breadcrumbs) {
3011e71b7053SJung-uk Kim        die "inherit_from loop!  target backtrace:\n  "
3012e71b7053SJung-uk Kim            ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
3013e71b7053SJung-uk Kim    }
3014e71b7053SJung-uk Kim
3015e71b7053SJung-uk Kim    if (!defined($table{$target})) {
3016e71b7053SJung-uk Kim        warn "Warning! target $target doesn't exist!\n";
3017e71b7053SJung-uk Kim        return ();
3018e71b7053SJung-uk Kim    }
3019e71b7053SJung-uk Kim    # Recurse through all inheritances.  They will be resolved on the
3020e71b7053SJung-uk Kim    # fly, so when this operation is done, they will all just be a
3021e71b7053SJung-uk Kim    # bunch of attributes with string values.
3022e71b7053SJung-uk Kim    # What we get here, though, are keys with references to lists of
3023e71b7053SJung-uk Kim    # the combined values of them all.  We will deal with lists after
3024e71b7053SJung-uk Kim    # this stage is done.
3025e71b7053SJung-uk Kim    my %combined_inheritance = ();
3026e71b7053SJung-uk Kim    if ($table{$target}->{inherit_from}) {
3027e71b7053SJung-uk Kim        my @inherit_from =
3028e71b7053SJung-uk Kim            map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
3029e71b7053SJung-uk Kim        foreach (@inherit_from) {
3030e71b7053SJung-uk Kim            my %inherited_config = resolve_config($_, $target, @breadcrumbs);
3031e71b7053SJung-uk Kim
3032e71b7053SJung-uk Kim            # 'template' is a marker that's considered private to
3033e71b7053SJung-uk Kim            # the config that had it.
3034e71b7053SJung-uk Kim            delete $inherited_config{template};
3035e71b7053SJung-uk Kim
3036e71b7053SJung-uk Kim            foreach (keys %inherited_config) {
3037e71b7053SJung-uk Kim                if (!$combined_inheritance{$_}) {
3038e71b7053SJung-uk Kim                    $combined_inheritance{$_} = [];
3039e71b7053SJung-uk Kim                }
3040e71b7053SJung-uk Kim                push @{$combined_inheritance{$_}}, $inherited_config{$_};
3041e71b7053SJung-uk Kim            }
3042e71b7053SJung-uk Kim        }
3043e71b7053SJung-uk Kim    }
3044e71b7053SJung-uk Kim
3045e71b7053SJung-uk Kim    # We won't need inherit_from in this target any more, since we've
3046e71b7053SJung-uk Kim    # resolved all the inheritances that lead to this
3047e71b7053SJung-uk Kim    delete $table{$target}->{inherit_from};
3048e71b7053SJung-uk Kim
3049e71b7053SJung-uk Kim    # Now is the time to deal with those lists.  Here's the place to
3050e71b7053SJung-uk Kim    # decide what shall be done with those lists, all based on the
3051e71b7053SJung-uk Kim    # values of the target we're currently dealing with.
3052e71b7053SJung-uk Kim    # - If a value is a coderef, it will be executed with the list of
3053e71b7053SJung-uk Kim    #   inherited values as arguments.
3054e71b7053SJung-uk Kim    # - If the corresponding key doesn't have a value at all or is the
3055e71b7053SJung-uk Kim    #   empty string, the inherited value list will be run through the
3056e71b7053SJung-uk Kim    #   default combiner (below), and the result becomes this target's
3057e71b7053SJung-uk Kim    #   value.
3058e71b7053SJung-uk Kim    # - Otherwise, this target's value is assumed to be a string that
3059e71b7053SJung-uk Kim    #   will simply override the inherited list of values.
3060e71b7053SJung-uk Kim    my $default_combiner = add();
3061e71b7053SJung-uk Kim
3062e71b7053SJung-uk Kim    my %all_keys =
3063e71b7053SJung-uk Kim        map { $_ => 1 } (keys %combined_inheritance,
3064e71b7053SJung-uk Kim                         keys %{$table{$target}});
3065e71b7053SJung-uk Kim
3066e71b7053SJung-uk Kim    sub process_values {
3067e71b7053SJung-uk Kim        my $object    = shift;
3068e71b7053SJung-uk Kim        my $inherited = shift;  # Always a [ list ]
3069e71b7053SJung-uk Kim        my $target    = shift;
3070e71b7053SJung-uk Kim        my $entry     = shift;
3071e71b7053SJung-uk Kim
3072e71b7053SJung-uk Kim        $add_called = 0;
3073e71b7053SJung-uk Kim
3074e71b7053SJung-uk Kim        while(ref($object) eq "CODE") {
3075e71b7053SJung-uk Kim            $object = $object->(@$inherited);
3076e71b7053SJung-uk Kim        }
3077e71b7053SJung-uk Kim        if (!defined($object)) {
3078e71b7053SJung-uk Kim            return ();
3079e71b7053SJung-uk Kim        }
3080e71b7053SJung-uk Kim        elsif (ref($object) eq "ARRAY") {
3081e71b7053SJung-uk Kim            local $add_called;  # To make sure recursive calls don't affect it
3082e71b7053SJung-uk Kim            return [ map { process_values($_, $inherited, $target, $entry) }
3083e71b7053SJung-uk Kim                     @$object ];
3084e71b7053SJung-uk Kim        } elsif (ref($object) eq "") {
3085e71b7053SJung-uk Kim            return $object;
3086e71b7053SJung-uk Kim        } else {
3087e71b7053SJung-uk Kim            die "cannot handle reference type ",ref($object)
3088e71b7053SJung-uk Kim                ," found in target ",$target," -> ",$entry,"\n";
3089e71b7053SJung-uk Kim        }
3090e71b7053SJung-uk Kim    }
3091e71b7053SJung-uk Kim
3092e71b7053SJung-uk Kim    foreach (sort keys %all_keys) {
3093e71b7053SJung-uk Kim        my $previous = $combined_inheritance{$_};
3094e71b7053SJung-uk Kim
3095e71b7053SJung-uk Kim        # Current target doesn't have a value for the current key?
3096e71b7053SJung-uk Kim        # Assign it the default combiner, the rest of this loop body
3097e71b7053SJung-uk Kim        # will handle it just like any other coderef.
3098e71b7053SJung-uk Kim        if (!exists $table{$target}->{$_}) {
3099e71b7053SJung-uk Kim            $table{$target}->{$_} = $default_combiner;
3100e71b7053SJung-uk Kim        }
3101e71b7053SJung-uk Kim
3102e71b7053SJung-uk Kim        $table{$target}->{$_} = process_values($table{$target}->{$_},
3103e71b7053SJung-uk Kim                                               $combined_inheritance{$_},
3104e71b7053SJung-uk Kim                                               $target, $_);
3105e71b7053SJung-uk Kim        unless(defined($table{$target}->{$_})) {
3106e71b7053SJung-uk Kim            delete $table{$target}->{$_};
3107e71b7053SJung-uk Kim        }
3108e71b7053SJung-uk Kim#        if ($extra_checks &&
3109e71b7053SJung-uk Kim#            $previous && !($add_called ||  $previous ~~ $table{$target}->{$_})) {
3110e71b7053SJung-uk Kim#            warn "$_ got replaced in $target\n";
3111e71b7053SJung-uk Kim#        }
3112e71b7053SJung-uk Kim    }
3113e71b7053SJung-uk Kim
3114e71b7053SJung-uk Kim    # Finally done, return the result.
3115e71b7053SJung-uk Kim    return %{$table{$target}};
3116e71b7053SJung-uk Kim}
3117e71b7053SJung-uk Kim
311874664626SKris Kennawaysub usage
311974664626SKris Kennaway        {
312074664626SKris Kennaway        print STDERR $usage;
3121f579bf8eSKris Kennaway        print STDERR "\npick os/compiler from:\n";
312274664626SKris Kennaway        my $j=0;
312374664626SKris Kennaway        my $i;
3124f579bf8eSKris Kennaway        my $k=0;
312574664626SKris Kennaway        foreach $i (sort keys %table)
312674664626SKris Kennaway                {
3127e71b7053SJung-uk Kim                next if $table{$i}->{template};
312874664626SKris Kennaway                next if $i =~ /^debug/;
3129f579bf8eSKris Kennaway                $k += length($i) + 1;
3130f579bf8eSKris Kennaway                if ($k > 78)
3131f579bf8eSKris Kennaway                        {
3132f579bf8eSKris Kennaway                        print STDERR "\n";
3133f579bf8eSKris Kennaway                        $k=length($i);
3134f579bf8eSKris Kennaway                        }
3135f579bf8eSKris Kennaway                print STDERR $i . " ";
313674664626SKris Kennaway                }
313774664626SKris Kennaway        foreach $i (sort keys %table)
313874664626SKris Kennaway                {
3139e71b7053SJung-uk Kim                next if $table{$i}->{template};
314074664626SKris Kennaway                next if $i !~ /^debug/;
3141f579bf8eSKris Kennaway                $k += length($i) + 1;
3142f579bf8eSKris Kennaway                if ($k > 78)
3143f579bf8eSKris Kennaway                        {
314474664626SKris Kennaway                        print STDERR "\n";
3145f579bf8eSKris Kennaway                        $k=length($i);
3146f579bf8eSKris Kennaway                        }
3147f579bf8eSKris Kennaway                print STDERR $i . " ";
3148f579bf8eSKris Kennaway                }
3149f579bf8eSKris Kennaway        print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n";
315074664626SKris Kennaway        exit(1);
315174664626SKris Kennaway        }
315274664626SKris Kennaway
3153e71b7053SJung-uk Kimsub run_dofile
3154e71b7053SJung-uk Kim{
3155e71b7053SJung-uk Kim    my $out = shift;
3156e71b7053SJung-uk Kim    my @templates = @_;
3157e71b7053SJung-uk Kim
3158e71b7053SJung-uk Kim    unlink $out || warn "Can't remove $out, $!"
3159e71b7053SJung-uk Kim        if -f $out;
3160e71b7053SJung-uk Kim    foreach (@templates) {
3161e71b7053SJung-uk Kim        die "Can't open $_, $!" unless -f $_;
3162e71b7053SJung-uk Kim    }
3163e71b7053SJung-uk Kim    my $perlcmd = (quotify("maybeshell", $config{PERL}))[0];
3164e71b7053SJung-uk Kim    my $cmd = "$perlcmd \"-I.\" \"-Mconfigdata\" \"$dofile\" -o\"Configure\" \"".join("\" \"",@templates)."\" > \"$out.new\"";
3165e71b7053SJung-uk Kim    #print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
3166e71b7053SJung-uk Kim    system($cmd);
3167e71b7053SJung-uk Kim    exit 1 if $? != 0;
3168e71b7053SJung-uk Kim    rename("$out.new", $out) || die "Can't rename $out.new, $!";
3169e71b7053SJung-uk Kim}
3170e71b7053SJung-uk Kim
3171e71b7053SJung-uk Kimsub compiler_predefined {
3172e71b7053SJung-uk Kim    state %predefined;
3173e71b7053SJung-uk Kim    my $cc = shift;
3174e71b7053SJung-uk Kim
3175e71b7053SJung-uk Kim    return () if $^O eq 'VMS';
3176e71b7053SJung-uk Kim
3177e71b7053SJung-uk Kim    die 'compiler_predefined called without a compiler command'
3178e71b7053SJung-uk Kim        unless $cc;
3179e71b7053SJung-uk Kim
3180e71b7053SJung-uk Kim    if (! $predefined{$cc}) {
3181e71b7053SJung-uk Kim
3182e71b7053SJung-uk Kim        $predefined{$cc} = {};
3183e71b7053SJung-uk Kim
3184e71b7053SJung-uk Kim        # collect compiler pre-defines from gcc or gcc-alike...
3185e71b7053SJung-uk Kim        open(PIPE, "$cc -dM -E -x c /dev/null 2>&1 |");
3186e71b7053SJung-uk Kim        while (my $l = <PIPE>) {
3187e71b7053SJung-uk Kim            $l =~ m/^#define\s+(\w+(?:\(\w+\))?)(?:\s+(.+))?/ or last;
3188e71b7053SJung-uk Kim            $predefined{$cc}->{$1} = $2 // '';
3189e71b7053SJung-uk Kim        }
3190e71b7053SJung-uk Kim        close(PIPE);
3191e71b7053SJung-uk Kim    }
3192e71b7053SJung-uk Kim
3193e71b7053SJung-uk Kim    return %{$predefined{$cc}};
3194e71b7053SJung-uk Kim}
3195e71b7053SJung-uk Kim
319674664626SKris Kennawaysub which
319774664626SKris Kennaway{
319874664626SKris Kennaway    my ($name)=@_;
3199e71b7053SJung-uk Kim
3200e71b7053SJung-uk Kim    if (eval { require IPC::Cmd; 1; }) {
3201e71b7053SJung-uk Kim        IPC::Cmd->import();
3202e71b7053SJung-uk Kim        return scalar IPC::Cmd::can_run($name);
3203e71b7053SJung-uk Kim    } else {
3204e71b7053SJung-uk Kim        # if there is $directories component in splitpath,
3205e71b7053SJung-uk Kim        # then it's not something to test with $PATH...
3206e71b7053SJung-uk Kim        return $name if (File::Spec->splitpath($name))[1];
3207e71b7053SJung-uk Kim
3208e71b7053SJung-uk Kim        foreach (File::Spec->path()) {
3209e71b7053SJung-uk Kim            my $fullpath = catfile($_, "$name$target{exe_extension}");
3210e71b7053SJung-uk Kim            if (-f $fullpath and -x $fullpath) {
3211e71b7053SJung-uk Kim                return $fullpath;
3212e71b7053SJung-uk Kim            }
321374664626SKris Kennaway        }
321474664626SKris Kennaway    }
321574664626SKris Kennaway}
321674664626SKris Kennaway
3217e71b7053SJung-uk Kimsub env
321874664626SKris Kennaway{
3219e71b7053SJung-uk Kim    my $name = shift;
3220e71b7053SJung-uk Kim    my %opts = @_;
322174664626SKris Kennaway
3222e71b7053SJung-uk Kim    unless ($opts{cacheonly}) {
3223e71b7053SJung-uk Kim        # Note that if $ENV{$name} doesn't exist or is undefined,
3224e71b7053SJung-uk Kim        # $config{perlenv}->{$name} will be created with the value
3225e71b7053SJung-uk Kim        # undef.  This is intentional.
3226e71b7053SJung-uk Kim
3227e71b7053SJung-uk Kim        $config{perlenv}->{$name} = $ENV{$name}
3228e71b7053SJung-uk Kim            if ! exists $config{perlenv}->{$name};
322974664626SKris Kennaway    }
3230e71b7053SJung-uk Kim    return $config{perlenv}->{$name};
323174664626SKris Kennaway}
323274664626SKris Kennaway
3233e71b7053SJung-uk Kim# Configuration printer ##############################################
3234e71b7053SJung-uk Kim
323574664626SKris Kennawaysub print_table_entry
323674664626SKris Kennaway{
3237e71b7053SJung-uk Kim    local $now_printing = shift;
3238e71b7053SJung-uk Kim    my %target = resolve_config($now_printing);
3239e71b7053SJung-uk Kim    my $type = shift;
324074664626SKris Kennaway
3241e71b7053SJung-uk Kim    # Don't print the templates
3242e71b7053SJung-uk Kim    return if $target{template};
324374664626SKris Kennaway
3244e71b7053SJung-uk Kim    my @sequence = (
3245e71b7053SJung-uk Kim        "sys_id",
3246e71b7053SJung-uk Kim        "cpp",
3247e71b7053SJung-uk Kim        "cppflags",
3248e71b7053SJung-uk Kim        "defines",
3249e71b7053SJung-uk Kim        "includes",
3250e71b7053SJung-uk Kim        "cc",
3251e71b7053SJung-uk Kim        "cflags",
3252e71b7053SJung-uk Kim        "unistd",
3253e71b7053SJung-uk Kim        "ld",
3254e71b7053SJung-uk Kim        "lflags",
3255e71b7053SJung-uk Kim        "loutflag",
3256e71b7053SJung-uk Kim        "ex_libs",
3257e71b7053SJung-uk Kim        "bn_ops",
3258e71b7053SJung-uk Kim        "apps_aux_src",
3259e71b7053SJung-uk Kim        "cpuid_asm_src",
3260e71b7053SJung-uk Kim        "uplink_aux_src",
3261e71b7053SJung-uk Kim        "bn_asm_src",
3262e71b7053SJung-uk Kim        "ec_asm_src",
3263e71b7053SJung-uk Kim        "des_asm_src",
3264e71b7053SJung-uk Kim        "aes_asm_src",
3265e71b7053SJung-uk Kim        "bf_asm_src",
3266e71b7053SJung-uk Kim        "md5_asm_src",
3267e71b7053SJung-uk Kim        "cast_asm_src",
3268e71b7053SJung-uk Kim        "sha1_asm_src",
3269e71b7053SJung-uk Kim        "rc4_asm_src",
3270e71b7053SJung-uk Kim        "rmd160_asm_src",
3271e71b7053SJung-uk Kim        "rc5_asm_src",
3272e71b7053SJung-uk Kim        "wp_asm_src",
3273e71b7053SJung-uk Kim        "cmll_asm_src",
3274e71b7053SJung-uk Kim        "modes_asm_src",
3275e71b7053SJung-uk Kim        "padlock_asm_src",
3276e71b7053SJung-uk Kim        "chacha_asm_src",
3277e71b7053SJung-uk Kim        "poly1035_asm_src",
3278e71b7053SJung-uk Kim        "thread_scheme",
3279e71b7053SJung-uk Kim        "perlasm_scheme",
3280e71b7053SJung-uk Kim        "dso_scheme",
3281e71b7053SJung-uk Kim        "shared_target",
3282e71b7053SJung-uk Kim        "shared_cflag",
3283e71b7053SJung-uk Kim        "shared_defines",
3284e71b7053SJung-uk Kim        "shared_ldflag",
3285e71b7053SJung-uk Kim        "shared_rcflag",
3286e71b7053SJung-uk Kim        "shared_extension",
3287e71b7053SJung-uk Kim        "dso_extension",
3288e71b7053SJung-uk Kim        "obj_extension",
3289e71b7053SJung-uk Kim        "exe_extension",
3290e71b7053SJung-uk Kim        "ranlib",
3291e71b7053SJung-uk Kim        "ar",
3292e71b7053SJung-uk Kim        "arflags",
3293e71b7053SJung-uk Kim        "aroutflag",
3294e71b7053SJung-uk Kim        "rc",
3295e71b7053SJung-uk Kim        "rcflags",
3296e71b7053SJung-uk Kim        "rcoutflag",
3297e71b7053SJung-uk Kim        "mt",
3298e71b7053SJung-uk Kim        "mtflags",
3299e71b7053SJung-uk Kim        "mtinflag",
3300e71b7053SJung-uk Kim        "mtoutflag",
3301e71b7053SJung-uk Kim        "multilib",
3302e71b7053SJung-uk Kim        "build_scheme",
3303e71b7053SJung-uk Kim        );
330474664626SKris Kennaway
3305e71b7053SJung-uk Kim    if ($type eq "TABLE") {
3306e71b7053SJung-uk Kim        print "\n";
3307e71b7053SJung-uk Kim        print "*** $now_printing\n";
3308e71b7053SJung-uk Kim        foreach (@sequence) {
3309e71b7053SJung-uk Kim            if (ref($target{$_}) eq "ARRAY") {
3310e71b7053SJung-uk Kim                printf "\$%-12s = %s\n", $_, join(" ", @{$target{$_}});
3311e71b7053SJung-uk Kim            } else {
3312e71b7053SJung-uk Kim                printf "\$%-12s = %s\n", $_, $target{$_};
3313c1803d78SJacques Vidrine            }
3314c1803d78SJacques Vidrine        }
3315e71b7053SJung-uk Kim    } elsif ($type eq "HASH") {
3316e71b7053SJung-uk Kim        my $largest =
3317e71b7053SJung-uk Kim            length((sort { length($a) <=> length($b) } @sequence)[-1]);
3318e71b7053SJung-uk Kim        print "    '$now_printing' => {\n";
3319e71b7053SJung-uk Kim        foreach (@sequence) {
3320e71b7053SJung-uk Kim            if ($target{$_}) {
3321e71b7053SJung-uk Kim                if (ref($target{$_}) eq "ARRAY") {
3322e71b7053SJung-uk Kim                    print "      '",$_,"'"," " x ($largest - length($_))," => [ ",join(", ", map { "'$_'" } @{$target{$_}})," ],\n";
3323e71b7053SJung-uk Kim                } else {
3324e71b7053SJung-uk Kim                    print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
3325e71b7053SJung-uk Kim                }
3326e71b7053SJung-uk Kim            }
3327e71b7053SJung-uk Kim        }
3328e71b7053SJung-uk Kim        print "    },\n";
3329e71b7053SJung-uk Kim    }
3330c1803d78SJacques Vidrine}
33316cf8931aSJung-uk Kim
3332e71b7053SJung-uk Kim# Utility routines ###################################################
3333e71b7053SJung-uk Kim
3334e71b7053SJung-uk Kim# On VMS, if the given file is a logical name, File::Spec::Functions
3335e71b7053SJung-uk Kim# will consider it an absolute path.  There are cases when we want a
3336e71b7053SJung-uk Kim# purely syntactic check without checking the environment.
3337e71b7053SJung-uk Kimsub isabsolute {
3338e71b7053SJung-uk Kim    my $file = shift;
3339e71b7053SJung-uk Kim
3340e71b7053SJung-uk Kim    # On non-platforms, we just use file_name_is_absolute().
3341e71b7053SJung-uk Kim    return file_name_is_absolute($file) unless $^O eq "VMS";
3342e71b7053SJung-uk Kim
3343e71b7053SJung-uk Kim    # If the file spec includes a device or a directory spec,
3344e71b7053SJung-uk Kim    # file_name_is_absolute() is perfectly safe.
3345e71b7053SJung-uk Kim    return file_name_is_absolute($file) if $file =~ m|[:\[]|;
3346e71b7053SJung-uk Kim
3347e71b7053SJung-uk Kim    # Here, we know the given file spec isn't absolute
3348e71b7053SJung-uk Kim    return 0;
3349e71b7053SJung-uk Kim}
3350e71b7053SJung-uk Kim
3351e71b7053SJung-uk Kim# Makes a directory absolute and cleans out /../ in paths like foo/../bar
3352e71b7053SJung-uk Kim# On some platforms, this uses rel2abs(), while on others, realpath() is used.
3353e71b7053SJung-uk Kim# realpath() requires that at least all path components except the last is an
3354e71b7053SJung-uk Kim# existing directory.  On VMS, the last component of the directory spec must
3355e71b7053SJung-uk Kim# exist.
3356e71b7053SJung-uk Kimsub absolutedir {
3357e71b7053SJung-uk Kim    my $dir = shift;
3358e71b7053SJung-uk Kim
3359e71b7053SJung-uk Kim    # realpath() is quite buggy on VMS.  It uses LIB$FID_TO_NAME, which
3360e71b7053SJung-uk Kim    # will return the volume name for the device, no matter what.  Also,
3361e71b7053SJung-uk Kim    # it will return an incorrect directory spec if the argument is a
3362e71b7053SJung-uk Kim    # directory that doesn't exist.
3363e71b7053SJung-uk Kim    if ($^O eq "VMS") {
3364e71b7053SJung-uk Kim        return rel2abs($dir);
3365e71b7053SJung-uk Kim    }
3366e71b7053SJung-uk Kim
3367e71b7053SJung-uk Kim    # We use realpath() on Unix, since no other will properly clean out
3368e71b7053SJung-uk Kim    # a directory spec.
3369e71b7053SJung-uk Kim    use Cwd qw/realpath/;
3370e71b7053SJung-uk Kim
3371e71b7053SJung-uk Kim    return realpath($dir);
3372e71b7053SJung-uk Kim}
3373e71b7053SJung-uk Kim
3374e71b7053SJung-uk Kimsub quotify {
3375e71b7053SJung-uk Kim    my %processors = (
3376e71b7053SJung-uk Kim        perl    => sub { my $x = shift;
3377e71b7053SJung-uk Kim                         $x =~ s/([\\\$\@"])/\\$1/g;
3378e71b7053SJung-uk Kim                         return '"'.$x.'"'; },
3379e71b7053SJung-uk Kim        maybeshell => sub { my $x = shift;
3380e71b7053SJung-uk Kim                            (my $y = $x) =~ s/([\\\"])/\\$1/g;
3381e71b7053SJung-uk Kim                            if ($x ne $y || $x =~ m|\s|) {
3382e71b7053SJung-uk Kim                                return '"'.$y.'"';
3383e71b7053SJung-uk Kim                            } else {
3384e71b7053SJung-uk Kim                                return $x;
3385e71b7053SJung-uk Kim                            }
3386e71b7053SJung-uk Kim                        },
3387e71b7053SJung-uk Kim        );
3388e71b7053SJung-uk Kim    my $for = shift;
3389e71b7053SJung-uk Kim    my $processor =
3390e71b7053SJung-uk Kim        defined($processors{$for}) ? $processors{$for} : sub { shift; };
3391e71b7053SJung-uk Kim
3392e71b7053SJung-uk Kim    return map { $processor->($_); } @_;
3393e71b7053SJung-uk Kim}
3394e71b7053SJung-uk Kim
3395e71b7053SJung-uk Kim# collect_from_file($filename, $line_concat_cond_re, $line_concat)
3396e71b7053SJung-uk Kim# $filename is a file name to read from
3397e71b7053SJung-uk Kim# $line_concat_cond_re is a regexp detecting a line continuation ending
3398e71b7053SJung-uk Kim# $line_concat is a CODEref that takes care of concatenating two lines
3399e71b7053SJung-uk Kimsub collect_from_file {
3400e71b7053SJung-uk Kim    my $filename = shift;
3401e71b7053SJung-uk Kim    my $line_concat_cond_re = shift;
3402e71b7053SJung-uk Kim    my $line_concat = shift;
3403e71b7053SJung-uk Kim
3404e71b7053SJung-uk Kim    open my $fh, $filename || die "unable to read $filename: $!\n";
3405e71b7053SJung-uk Kim    return sub {
3406e71b7053SJung-uk Kim        my $saved_line = "";
3407e71b7053SJung-uk Kim        $_ = "";
3408e71b7053SJung-uk Kim        while (<$fh>) {
3409e71b7053SJung-uk Kim            s|\R$||;
3410e71b7053SJung-uk Kim            if (defined $line_concat) {
3411e71b7053SJung-uk Kim                $_ = $line_concat->($saved_line, $_);
3412e71b7053SJung-uk Kim                $saved_line = "";
3413e71b7053SJung-uk Kim            }
3414e71b7053SJung-uk Kim            if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
3415e71b7053SJung-uk Kim                $saved_line = $_;
3416e71b7053SJung-uk Kim                next;
3417e71b7053SJung-uk Kim            }
3418e71b7053SJung-uk Kim            return $_;
3419e71b7053SJung-uk Kim        }
3420e71b7053SJung-uk Kim        die "$filename ending with continuation line\n" if $_;
3421e71b7053SJung-uk Kim        close $fh;
3422e71b7053SJung-uk Kim        return undef;
3423e71b7053SJung-uk Kim    }
3424e71b7053SJung-uk Kim}
3425e71b7053SJung-uk Kim
3426e71b7053SJung-uk Kim# collect_from_array($array, $line_concat_cond_re, $line_concat)
3427e71b7053SJung-uk Kim# $array is an ARRAYref of lines
3428e71b7053SJung-uk Kim# $line_concat_cond_re is a regexp detecting a line continuation ending
3429e71b7053SJung-uk Kim# $line_concat is a CODEref that takes care of concatenating two lines
3430e71b7053SJung-uk Kimsub collect_from_array {
3431e71b7053SJung-uk Kim    my $array = shift;
3432e71b7053SJung-uk Kim    my $line_concat_cond_re = shift;
3433e71b7053SJung-uk Kim    my $line_concat = shift;
3434e71b7053SJung-uk Kim    my @array = (@$array);
3435e71b7053SJung-uk Kim
3436e71b7053SJung-uk Kim    return sub {
3437e71b7053SJung-uk Kim        my $saved_line = "";
3438e71b7053SJung-uk Kim        $_ = "";
3439e71b7053SJung-uk Kim        while (defined($_ = shift @array)) {
3440e71b7053SJung-uk Kim            s|\R$||;
3441e71b7053SJung-uk Kim            if (defined $line_concat) {
3442e71b7053SJung-uk Kim                $_ = $line_concat->($saved_line, $_);
3443e71b7053SJung-uk Kim                $saved_line = "";
3444e71b7053SJung-uk Kim            }
3445e71b7053SJung-uk Kim            if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
3446e71b7053SJung-uk Kim                $saved_line = $_;
3447e71b7053SJung-uk Kim                next;
3448e71b7053SJung-uk Kim            }
3449e71b7053SJung-uk Kim            return $_;
3450e71b7053SJung-uk Kim        }
3451e71b7053SJung-uk Kim        die "input text ending with continuation line\n" if $_;
3452e71b7053SJung-uk Kim        return undef;
3453e71b7053SJung-uk Kim    }
3454e71b7053SJung-uk Kim}
3455e71b7053SJung-uk Kim
3456e71b7053SJung-uk Kim# collect_information($lineiterator, $line_continue, $regexp => $CODEref, ...)
3457e71b7053SJung-uk Kim# $lineiterator is a CODEref that delivers one line at a time.
3458e71b7053SJung-uk Kim# All following arguments are regex/CODEref pairs, where the regexp detects a
3459e71b7053SJung-uk Kim# line and the CODEref does something with the result of the regexp.
3460e71b7053SJung-uk Kimsub collect_information {
3461e71b7053SJung-uk Kim    my $lineiterator = shift;
3462e71b7053SJung-uk Kim    my %collectors = @_;
3463e71b7053SJung-uk Kim
3464e71b7053SJung-uk Kim    while(defined($_ = $lineiterator->())) {
3465e71b7053SJung-uk Kim        s|\R$||;
3466e71b7053SJung-uk Kim        my $found = 0;
3467e71b7053SJung-uk Kim        if ($collectors{"BEFORE"}) {
3468e71b7053SJung-uk Kim            $collectors{"BEFORE"}->($_);
3469e71b7053SJung-uk Kim        }
3470e71b7053SJung-uk Kim        foreach my $re (keys %collectors) {
3471e71b7053SJung-uk Kim            if ($re !~ /^OTHERWISE|BEFORE|AFTER$/ && /$re/) {
3472e71b7053SJung-uk Kim                $collectors{$re}->($lineiterator);
3473e71b7053SJung-uk Kim                $found = 1;
3474e71b7053SJung-uk Kim            };
3475e71b7053SJung-uk Kim        }
3476e71b7053SJung-uk Kim        if ($collectors{"OTHERWISE"}) {
3477e71b7053SJung-uk Kim            $collectors{"OTHERWISE"}->($lineiterator, $_)
3478e71b7053SJung-uk Kim                unless $found || !defined $collectors{"OTHERWISE"};
3479e71b7053SJung-uk Kim        }
3480e71b7053SJung-uk Kim        if ($collectors{"AFTER"}) {
3481e71b7053SJung-uk Kim            $collectors{"AFTER"}->($_);
3482e71b7053SJung-uk Kim        }
3483e71b7053SJung-uk Kim    }
3484e71b7053SJung-uk Kim}
3485e71b7053SJung-uk Kim
3486e71b7053SJung-uk Kim# tokenize($line)
3487e71b7053SJung-uk Kim# $line is a line of text to split up into tokens
3488e71b7053SJung-uk Kim# returns a list of tokens
3489e71b7053SJung-uk Kim#
3490e71b7053SJung-uk Kim# Tokens are divided by spaces.  If the tokens include spaces, they
3491e71b7053SJung-uk Kim# have to be quoted with single or double quotes.  Double quotes
3492e71b7053SJung-uk Kim# inside a double quoted token must be escaped.  Escaping is done
3493e71b7053SJung-uk Kim# with backslash.
3494e71b7053SJung-uk Kim# Basically, the same quoting rules apply for " and ' as in any
3495e71b7053SJung-uk Kim# Unix shell.
3496e71b7053SJung-uk Kimsub tokenize {
3497e71b7053SJung-uk Kim    my $line = my $debug_line = shift;
3498e71b7053SJung-uk Kim    my @result = ();
3499e71b7053SJung-uk Kim
3500e71b7053SJung-uk Kim    while ($line =~ s|^\s+||, $line ne "") {
3501e71b7053SJung-uk Kim        my $token = "";
3502e71b7053SJung-uk Kim        while ($line ne "" && $line !~ m|^\s|) {
3503e71b7053SJung-uk Kim            if ($line =~ m/^"((?:[^"\\]+|\\.)*)"/) {
3504e71b7053SJung-uk Kim                $token .= $1;
3505e71b7053SJung-uk Kim                $line = $';
3506e71b7053SJung-uk Kim            } elsif ($line =~ m/^'([^']*)'/) {
3507e71b7053SJung-uk Kim                $token .= $1;
3508e71b7053SJung-uk Kim                $line = $';
3509e71b7053SJung-uk Kim            } elsif ($line =~ m/^(\S+)/) {
3510e71b7053SJung-uk Kim                $token .= $1;
3511e71b7053SJung-uk Kim                $line = $';
3512e71b7053SJung-uk Kim            }
3513e71b7053SJung-uk Kim        }
3514e71b7053SJung-uk Kim        push @result, $token;
3515e71b7053SJung-uk Kim    }
3516e71b7053SJung-uk Kim
3517e71b7053SJung-uk Kim    if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
3518e71b7053SJung-uk Kim        print STDERR "DEBUG[tokenize]: Parsed '$debug_line' into:\n";
3519e71b7053SJung-uk Kim        print STDERR "DEBUG[tokenize]: ('", join("', '", @result), "')\n";
3520e71b7053SJung-uk Kim    }
3521e71b7053SJung-uk Kim    return @result;
35226cf8931aSJung-uk Kim}
3523