xref: /freebsd/crypto/openssl/Configure (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim#! /usr/bin/env perl
2e71b7053SJung-uk Kim# -*- mode: perl; -*-
3640242a5SJung-uk Kim# Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
4e71b7053SJung-uk Kim#
5*b077aed3SPierre Pronchery# Licensed under the Apache License 2.0 (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;
18*b077aed3SPierre Proncheryuse File::Spec::Functions qw/:DEFAULT abs2rel rel2abs splitdir/;
19e71b7053SJung-uk Kimuse File::Path qw/mkpath/;
20*b077aed3SPierre Proncheryuse OpenSSL::fallback "$FindBin::Bin/external/perl/MODULES.txt";
21e71b7053SJung-uk Kimuse OpenSSL::Glob;
22*b077aed3SPierre Proncheryuse OpenSSL::Template;
23*b077aed3SPierre Proncheryuse OpenSSL::config;
2474664626SKris Kennaway
25*b077aed3SPierre Pronchery# see INSTALL.md for instructions.
2674664626SKris Kennaway
27e71b7053SJung-uk Kimmy $orig_death_handler = $SIG{__DIE__};
28e71b7053SJung-uk Kim$SIG{__DIE__} = \&death_handler;
29e71b7053SJung-uk Kim
30610a21fdSJung-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";
3174664626SKris Kennaway
32*b077aed3SPierre Proncherymy $banner = <<"EOF";
33*b077aed3SPierre Pronchery
34*b077aed3SPierre Pronchery**********************************************************************
35*b077aed3SPierre Pronchery***                                                                ***
36*b077aed3SPierre Pronchery***   OpenSSL has been successfully configured                     ***
37*b077aed3SPierre Pronchery***                                                                ***
38*b077aed3SPierre Pronchery***   If you encounter a problem while building, please open an    ***
39*b077aed3SPierre Pronchery***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
40*b077aed3SPierre Pronchery***   and include the output from the following command:           ***
41*b077aed3SPierre Pronchery***                                                                ***
42*b077aed3SPierre Pronchery***       perl configdata.pm --dump                                ***
43*b077aed3SPierre Pronchery***                                                                ***
44*b077aed3SPierre Pronchery***   (If you are new to OpenSSL, you might want to consult the    ***
45*b077aed3SPierre Pronchery***   'Troubleshooting' section in the INSTALL.md file first)      ***
46*b077aed3SPierre Pronchery***                                                                ***
47*b077aed3SPierre Pronchery**********************************************************************
48*b077aed3SPierre ProncheryEOF
49*b077aed3SPierre Pronchery
5074664626SKris Kennaway# Options:
5174664626SKris Kennaway#
52e71b7053SJung-uk Kim# --config      add the given configuration file, which will be read after
53e71b7053SJung-uk Kim#               any "Configurations*" files that are found in the same
54e71b7053SJung-uk Kim#               directory as this script.
55e71b7053SJung-uk Kim# --prefix      prefix for the OpenSSL installation, which includes the
56e71b7053SJung-uk Kim#               directories bin, lib, include, share/man, share/doc/openssl
57e71b7053SJung-uk Kim#               This becomes the value of INSTALLTOP in Makefile
58e71b7053SJung-uk Kim#               (Default: /usr/local)
59e71b7053SJung-uk Kim# --openssldir  OpenSSL data area, such as openssl.cnf, certificates and keys.
60e71b7053SJung-uk Kim#               If it's a relative directory, it will be added on the directory
61e71b7053SJung-uk Kim#               given with --prefix.
62e71b7053SJung-uk Kim#               This becomes the value of OPENSSLDIR in Makefile and in C.
63e71b7053SJung-uk Kim#               (Default: PREFIX/ssl)
64*b077aed3SPierre Pronchery# --banner=".." Output specified text instead of default completion banner
65*b077aed3SPierre Pronchery#
66*b077aed3SPierre Pronchery# -w            Don't wait after showing a Configure warning
67c1803d78SJacques Vidrine#
681f13597dSJung-uk Kim# --cross-compile-prefix Add specified prefix to binutils components.
691f13597dSJung-uk Kim#
70*b077aed3SPierre Pronchery# --api         One of 0.9.8, 1.0.0, 1.0.1, 1.0.2, 1.1.0, 1.1.1, or 3.0
71*b077aed3SPierre Pronchery#               Define the public APIs as they were for that version
72*b077aed3SPierre Pronchery#               including patch releases.  If 'no-deprecated' is also
73*b077aed3SPierre Pronchery#               given, do not compile support for interfaces deprecated
74*b077aed3SPierre Pronchery#               up to and including the specified OpenSSL version.
75e71b7053SJung-uk Kim#
765c87c606SMark Murray# no-hw-xxx     do not compile support for specific crypto hardware.
775c87c606SMark Murray#               Generic OpenSSL-style methods relating to this support
785c87c606SMark Murray#               are always compiled but return NULL if the hardware
795c87c606SMark Murray#               support isn't compiled.
805c87c606SMark Murray# no-hw         do not compile support for any crypto hardware.
8174664626SKris Kennaway# [no-]threads  [don't] try to create a library that is suitable for
8274664626SKris Kennaway#               multithreaded applications (default is "threads" if we
8374664626SKris Kennaway#               know how to do it)
84ddd58736SKris Kennaway# [no-]shared   [don't] try to create shared libraries when supported.
85e71b7053SJung-uk Kim# [no-]pic      [don't] try to build position independent code when supported.
86e71b7053SJung-uk Kim#               If disabled, it also disables shared and dynamic-engine.
8774664626SKris Kennaway# no-asm        do not use assembler
88e71b7053SJung-uk Kim# no-egd        do not compile support for the entropy-gathering daemon APIs
895c87c606SMark Murray# [no-]zlib     [don't] compile support for zlib compression.
905c87c606SMark Murray# zlib-dynamic  Like "zlib", but the zlib library is expected to be a shared
915c87c606SMark Murray#               library and will be loaded in run-time by the OpenSSL library.
921f13597dSJung-uk Kim# sctp          include SCTP support
93*b077aed3SPierre Pronchery# no-uplink     Don't build support for UPLINK interface.
944c6a0400SJung-uk Kim# enable-weak-ssl-ciphers
95e71b7053SJung-uk Kim#               Enable weak ciphers that are disabled by default.
966cf8931aSJung-uk Kim# 386           generate 80386 code in assembly modules
976cf8931aSJung-uk Kim# no-sse2       disables IA-32 SSE2 code in assembly modules, the above
986cf8931aSJung-uk Kim#               mentioned '386' option implies this one
99*b077aed3SPierre Pronchery# no-<cipher>   build without specified algorithm (dsa, idea, rc5, ...)
10017f01e99SJung-uk Kim# -<xxx> +<xxx> All options which are unknown to the 'Configure' script are
10117f01e99SJung-uk Kim# /<xxx>        passed through to the compiler. Unix-style options beginning
10217f01e99SJung-uk Kim#               with a '-' or '+' are recognized, as well as Windows-style
10317f01e99SJung-uk Kim#               options beginning with a '/'. If the option contains arguments
10417f01e99SJung-uk Kim#               separated by spaces, then the URL-style notation %20 can be
10517f01e99SJung-uk Kim#               used for the space character in order to avoid having to quote
10617f01e99SJung-uk Kim#               the option. For example, -opt%20arg gets expanded to -opt arg.
10717f01e99SJung-uk Kim#               In fact, any ASCII character can be encoded as %xx using its
10817f01e99SJung-uk Kim#               hexadecimal encoding.
109e71b7053SJung-uk Kim# -static       while -static is also a pass-through compiler option (and
110e71b7053SJung-uk Kim#               as such is limited to environments where it's actually
111e71b7053SJung-uk Kim#               meaningful), it triggers a number configuration options,
112610a21fdSJung-uk Kim#               namely no-pic, no-shared and no-threads. It is
113e71b7053SJung-uk Kim#               argued that the only reason to produce statically linked
114e71b7053SJung-uk Kim#               binaries (and in context it means executables linked with
115e71b7053SJung-uk Kim#               -static flag, and not just executables linked with static
116e71b7053SJung-uk Kim#               libcrypto.a) is to eliminate dependency on specific run-time,
117e71b7053SJung-uk Kim#               a.k.a. libc version. The mentioned config options are meant
118e71b7053SJung-uk Kim#               to achieve just that. Unfortunately on Linux it's impossible
119e71b7053SJung-uk Kim#               to eliminate the dependency completely for openssl executable
120e71b7053SJung-uk Kim#               because of getaddrinfo and gethostbyname calls, which can
121e71b7053SJung-uk Kim#               invoke dynamically loadable library facility anyway to meet
122e71b7053SJung-uk Kim#               the lookup requests. For this reason on Linux statically
123e71b7053SJung-uk Kim#               linked openssl executable has rather debugging value than
124e71b7053SJung-uk Kim#               production quality.
12574664626SKris Kennaway#
12674664626SKris Kennaway# BN_LLONG      use the type 'long long' in crypto/bn/bn.h
12774664626SKris Kennaway# RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
1283b4e3dcbSSimon L. B. Nielsen# Following are set automatically by this script
1293b4e3dcbSSimon L. B. Nielsen#
130e71b7053SJung-uk Kim# MD5_ASM       use some extra md5 assembler,
131e71b7053SJung-uk Kim# SHA1_ASM      use some extra sha1 assembler, must define L_ENDIAN for x86
132e71b7053SJung-uk Kim# RMD160_ASM    use some extra ripemd160 assembler,
1333b4e3dcbSSimon L. B. Nielsen# SHA256_ASM    sha256_block is implemented in assembler
1343b4e3dcbSSimon L. B. Nielsen# SHA512_ASM    sha512_block is implemented in assembler
135e71b7053SJung-uk Kim# AES_ASM       AES_[en|de]crypt is implemented in assembler
13674664626SKris Kennaway
137610a21fdSJung-uk Kim# Minimum warning options... any contributions to OpenSSL should at least
138610a21fdSJung-uk Kim# get past these.  Note that we only use these with C compilers, not with
139610a21fdSJung-uk Kim# C++ compilers.
140db522d3aSSimon L. B. Nielsen
141e71b7053SJung-uk Kim# -DPEDANTIC complements -pedantic and is meant to mask code that
142e71b7053SJung-uk Kim# is not strictly standard-compliant and/or implementation-specific,
143e71b7053SJung-uk Kim# e.g. inline assembly, disregards to alignment requirements, such
144e71b7053SJung-uk Kim# that -pedantic would complain about. Incidentally -DPEDANTIC has
145e71b7053SJung-uk Kim# to be used even in sanitized builds, because sanitizer too is
146e71b7053SJung-uk Kim# supposed to and does take notice of non-standard behaviour. Then
147e71b7053SJung-uk Kim# -pedantic with pre-C9x compiler would also complain about 'long
148e71b7053SJung-uk Kim# long' not being supported. As 64-bit algorithms are common now,
149e71b7053SJung-uk Kim# it grew impossible to resolve this without sizeable additional
150e71b7053SJung-uk Kim# code, so we just tell compiler to be pedantic about everything
151e71b7053SJung-uk Kim# but 'long long' type.
152db522d3aSSimon L. B. Nielsen
153610a21fdSJung-uk Kimmy @gcc_devteam_warn = qw(
154*b077aed3SPierre Pronchery    -DPEDANTIC -pedantic -Wno-long-long -DUNUSEDRESULT_DEBUG
155610a21fdSJung-uk Kim    -Wall
156*b077aed3SPierre Pronchery    -Wmissing-declarations
157610a21fdSJung-uk Kim    -Wextra
158610a21fdSJung-uk Kim    -Wno-unused-parameter
159610a21fdSJung-uk Kim    -Wno-missing-field-initializers
160610a21fdSJung-uk Kim    -Wswitch
161610a21fdSJung-uk Kim    -Wsign-compare
162610a21fdSJung-uk Kim    -Wshadow
163610a21fdSJung-uk Kim    -Wformat
164610a21fdSJung-uk Kim    -Wtype-limits
165610a21fdSJung-uk Kim    -Wundef
166610a21fdSJung-uk Kim    -Werror
167610a21fdSJung-uk Kim    -Wmissing-prototypes
168610a21fdSJung-uk Kim    -Wstrict-prototypes
169610a21fdSJung-uk Kim);
1707bded2dbSJung-uk Kim
1717bded2dbSJung-uk Kim# These are used in addition to $gcc_devteam_warn when the compiler is clang.
1727bded2dbSJung-uk Kim# TODO(openssl-team): fix problems and investigate if (at least) the
173e71b7053SJung-uk Kim# following warnings can also be enabled:
174e71b7053SJung-uk Kim#       -Wcast-align
175e71b7053SJung-uk Kim#       -Wunreachable-code -- no, too ugly/compiler-specific
176e71b7053SJung-uk Kim#       -Wlanguage-extension-token -- no, we use asm()
177e71b7053SJung-uk Kim#       -Wunused-macros -- no, too tricky for BN and _XOPEN_SOURCE etc
178e71b7053SJung-uk Kim#       -Wextended-offsetof -- no, needed in CMS ASN1 code
179610a21fdSJung-uk Kimmy @clang_devteam_warn = qw(
180da327cd2SJung-uk Kim    -Wno-unknown-warning-option
181610a21fdSJung-uk Kim    -Wswitch-default
182610a21fdSJung-uk Kim    -Wno-parentheses-equality
183610a21fdSJung-uk Kim    -Wno-language-extension-token
184610a21fdSJung-uk Kim    -Wno-extended-offsetof
185610a21fdSJung-uk Kim    -Wconditional-uninitialized
186610a21fdSJung-uk Kim    -Wincompatible-pointer-types-discards-qualifiers
187610a21fdSJung-uk Kim    -Wmissing-variable-declarations
188610a21fdSJung-uk Kim);
1897bded2dbSJung-uk Kim
19017f01e99SJung-uk Kimmy @cl_devteam_warn = qw(
19117f01e99SJung-uk Kim    /WX
19217f01e99SJung-uk Kim);
19317f01e99SJung-uk Kim
1946a599222SSimon L. B. Nielsenmy $strict_warnings = 0;
1956a599222SSimon L. B. Nielsen
1963b4e3dcbSSimon L. B. Nielsen# As for $BSDthreads. Idea is to maintain "collective" set of flags,
1973b4e3dcbSSimon L. B. Nielsen# which would cover all BSD flavors. -pthread applies to them all,
1983b4e3dcbSSimon L. B. Nielsen# but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
1993b4e3dcbSSimon L. B. Nielsen# -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
2003b4e3dcbSSimon L. B. Nielsen# which has to be accompanied by explicit -D_THREAD_SAFE and
2013b4e3dcbSSimon L. B. Nielsen# sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
2023b4e3dcbSSimon L. B. Nielsen# seems to be sufficient?
203e71b7053SJung-uk Kimour $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
20474664626SKris Kennaway
20574664626SKris Kennaway#
206e71b7053SJung-uk Kim# API compatibility name to version number mapping.
20774664626SKris Kennaway#
208e71b7053SJung-uk Kimmy $apitable = {
209*b077aed3SPierre Pronchery    # This table expresses when API additions or changes can occur.
210*b077aed3SPierre Pronchery    # The numbering used changes from 3.0 and on because we updated
211*b077aed3SPierre Pronchery    # (solidified) our version numbering scheme at that point.
212*b077aed3SPierre Pronchery
213*b077aed3SPierre Pronchery    # From 3.0 and on, we internalise the given version number in decimal
214*b077aed3SPierre Pronchery    # as MAJOR * 10000 + MINOR * 100 + 0
215*b077aed3SPierre Pronchery    "3.0.0" => 30000,
216*b077aed3SPierre Pronchery    "3.0"   => 30000,
217*b077aed3SPierre Pronchery
218*b077aed3SPierre Pronchery    # Note that before 3.0, we didn't have the same version number scheme.
219*b077aed3SPierre Pronchery    # Still, the numbering we use here covers what we need.
220*b077aed3SPierre Pronchery    "1.1.1" => 10101,
221*b077aed3SPierre Pronchery    "1.1.0" => 10100,
222*b077aed3SPierre Pronchery    "1.0.2" => 10002,
223*b077aed3SPierre Pronchery    "1.0.1" => 10001,
224*b077aed3SPierre Pronchery    "1.0.0" => 10000,
225*b077aed3SPierre Pronchery    "0.9.8" =>   908,
226e71b7053SJung-uk Kim};
227e71b7053SJung-uk Kim
228*b077aed3SPierre Pronchery# For OpenSSL::config::get_platform
229*b077aed3SPierre Proncherymy %guess_opts = ();
230*b077aed3SPierre Pronchery
231*b077aed3SPierre Proncherymy $dryrun = 0;
232*b077aed3SPierre Pronchery
233e71b7053SJung-uk Kimour %table = ();
234e71b7053SJung-uk Kimour %config = ();
235e71b7053SJung-uk Kimour %withargs = ();
236e71b7053SJung-uk Kimour $now_printing;      # set to current entry's name in print_table_entry
237e71b7053SJung-uk Kim                        # (todo: right thing would be to encapsulate name
238e71b7053SJung-uk Kim                        # into %target [class] and make print_table_entry
239e71b7053SJung-uk Kim                        # a method)
240e71b7053SJung-uk Kim
241e71b7053SJung-uk Kim# Forward declarations ###############################################
242e71b7053SJung-uk Kim
243e71b7053SJung-uk Kim# read_config(filename)
244c1803d78SJacques Vidrine#
245e71b7053SJung-uk Kim# Reads a configuration file and populates %table with the contents
246e71b7053SJung-uk Kim# (which the configuration file places in %targets).
247e71b7053SJung-uk Kimsub read_config;
24874664626SKris Kennaway
249e71b7053SJung-uk Kim# resolve_config(target)
25074664626SKris Kennaway#
251e71b7053SJung-uk Kim# Resolves all the late evaluations, inheritances and so on for the
252e71b7053SJung-uk Kim# chosen target and any target it inherits from.
253e71b7053SJung-uk Kimsub resolve_config;
25474664626SKris Kennaway
25574664626SKris Kennaway
256e71b7053SJung-uk Kim# Information collection #############################################
25774664626SKris Kennaway
258e71b7053SJung-uk Kim# Unified build supports separate build dir
259e71b7053SJung-uk Kimmy $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
260e71b7053SJung-uk Kimmy $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
26158f35182SJung-uk Kim
26258f35182SJung-uk Kim# File::Spec::Unix doesn't detect case insensitivity, so we make sure to
26358f35182SJung-uk Kim# check if the source and build directory are really the same, and make
26458f35182SJung-uk Kim# them so.  This avoids all kinds of confusion later on.
26558f35182SJung-uk Kim# We must check @File::Spec::ISA rather than using File::Spec->isa() to
26658f35182SJung-uk Kim# know if File::Spec ended up loading File::Spec::Unix.
26758f35182SJung-uk Kim$srcdir = $blddir
26858f35182SJung-uk Kim    if (grep(/::Unix$/, @File::Spec::ISA)
26958f35182SJung-uk Kim        && samedir($srcdir, $blddir));
27058f35182SJung-uk Kim
271e71b7053SJung-uk Kimmy $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
27274664626SKris Kennaway
273e71b7053SJung-uk Kimmy $local_config_envname = 'OPENSSL_LOCAL_CONFIG_DIR';
274a21b1b38SKris Kennaway
27558f35182SJung-uk Kim$config{sourcedir} = abs2rel($srcdir, $blddir);
27658f35182SJung-uk Kim$config{builddir} = abs2rel($blddir, $blddir);
277*b077aed3SPierre Pronchery# echo -n 'holy hand grenade of antioch' | openssl sha256
278*b077aed3SPierre Pronchery$config{FIPSKEY} =
279*b077aed3SPierre Pronchery    'f4556650ac31d35461610bac4ed81b1a181b2d8a43ea2854cbae22ca74560813';
28074664626SKris Kennaway
281e71b7053SJung-uk Kim# Collect reconfiguration information if needed
282e71b7053SJung-uk Kimmy @argvcopy=@ARGV;
28374664626SKris Kennaway
284e71b7053SJung-uk Kimif (grep /^reconf(igure)?$/, @argvcopy) {
285e71b7053SJung-uk Kim    die "reconfiguring with other arguments present isn't supported"
286e71b7053SJung-uk Kim        if scalar @argvcopy > 1;
287e71b7053SJung-uk Kim    if (-f "./configdata.pm") {
288e71b7053SJung-uk Kim        my $file = "./configdata.pm";
289e71b7053SJung-uk Kim        unless (my $return = do $file) {
290e71b7053SJung-uk Kim            die "couldn't parse $file: $@" if $@;
291e71b7053SJung-uk Kim            die "couldn't do $file: $!"    unless defined $return;
292e71b7053SJung-uk Kim            die "couldn't run $file"       unless $return;
2931f13597dSJung-uk Kim        }
2943b4e3dcbSSimon L. B. Nielsen
295e71b7053SJung-uk Kim        @argvcopy = defined($configdata::config{perlargv}) ?
296e71b7053SJung-uk Kim            @{$configdata::config{perlargv}} : ();
297e71b7053SJung-uk Kim        die "Incorrect data to reconfigure, please do a normal configuration\n"
298e71b7053SJung-uk Kim            if (grep(/^reconf/,@argvcopy));
299e71b7053SJung-uk Kim        $config{perlenv} = $configdata::config{perlenv} // {};
300e71b7053SJung-uk Kim    } else {
301e71b7053SJung-uk Kim        die "Insufficient data to reconfigure, please do a normal configuration\n";
302e71b7053SJung-uk Kim    }
303e71b7053SJung-uk Kim}
3043b4e3dcbSSimon L. B. Nielsen
305e71b7053SJung-uk Kim$config{perlargv} = [ @argvcopy ];
306e71b7053SJung-uk Kim
307*b077aed3SPierre Pronchery# Historical: if known directories in crypto/ have been removed, it means
308*b077aed3SPierre Pronchery# that those sub-systems are disabled.
309*b077aed3SPierre Pronchery# (the other option would be to removed them from the SUBDIRS statement in
310*b077aed3SPierre Pronchery# crypto/build.info)
311*b077aed3SPierre Pronchery# We reverse the input list for cosmetic purely reasons, to compensate that
312*b077aed3SPierre Pronchery# 'unshift' adds at the front of the list (i.e. in reverse input order).
313*b077aed3SPierre Proncheryforeach ( reverse sort( 'aes', 'aria', 'bf', 'camellia', 'cast', 'des', 'dh',
314*b077aed3SPierre Pronchery                        'dsa', 'ec', 'hmac', 'idea', 'md2', 'md5', 'mdc2',
315*b077aed3SPierre Pronchery                        'rc2', 'rc4', 'rc5', 'ripemd', 'seed', 'sha',
316*b077aed3SPierre Pronchery                        'sm2', 'sm3', 'sm4') ) {
317*b077aed3SPierre Pronchery    unshift @argvcopy, "no-$_" if ! -d catdir($srcdir, 'crypto', $_);
318*b077aed3SPierre Pronchery}
319*b077aed3SPierre Pronchery
320e71b7053SJung-uk Kim# Collect version numbers
321*b077aed3SPierre Proncherymy %version = ();
322e71b7053SJung-uk Kim
323e71b7053SJung-uk Kimcollect_information(
324*b077aed3SPierre Pronchery    collect_from_file(catfile($srcdir,'VERSION.dat')),
325*b077aed3SPierre Pronchery    qr/\s*(\w+)\s*=\s*(.*?)\s*$/ =>
326*b077aed3SPierre Pronchery        sub {
327*b077aed3SPierre Pronchery            # Only define it if there is a value at all
328*b077aed3SPierre Pronchery            if ($2 ne '') {
329*b077aed3SPierre Pronchery                my $k = $1;
330*b077aed3SPierre Pronchery                my $v = $2;
331*b077aed3SPierre Pronchery                # Some values are quoted.  Trim the quotes
332*b077aed3SPierre Pronchery                $v = $1 if $v =~ /^"(.*)"$/;
333*b077aed3SPierre Pronchery                $version{uc $k} = $v;
334*b077aed3SPierre Pronchery            }
335*b077aed3SPierre Pronchery        },
336*b077aed3SPierre Pronchery    "OTHERWISE" =>
337*b077aed3SPierre Pronchery        sub { die "Something wrong with this line:\n$_\nin $srcdir/VERSION.dat" },
338e71b7053SJung-uk Kim    );
339e71b7053SJung-uk Kim
340*b077aed3SPierre Pronchery$config{major} = $version{MAJOR} // 'unknown';
341*b077aed3SPierre Pronchery$config{minor} = $version{MINOR} // 'unknown';
342*b077aed3SPierre Pronchery$config{patch} = $version{PATCH} // 'unknown';
343*b077aed3SPierre Pronchery$config{prerelease} =
344*b077aed3SPierre Pronchery    defined $version{PRE_RELEASE_TAG} ? "-$version{PRE_RELEASE_TAG}" : '';
345*b077aed3SPierre Pronchery$config{build_metadata} =
346*b077aed3SPierre Pronchery    defined $version{BUILD_METADATA} ? "+$version{BUILD_METADATA}" : '';
347*b077aed3SPierre Pronchery$config{shlib_version} = $version{SHLIB_VERSION} // 'unknown';
348*b077aed3SPierre Pronchery$config{release_date} = $version{RELEASE_DATE} // 'xx XXX xxxx';
349*b077aed3SPierre Pronchery
350*b077aed3SPierre Pronchery$config{version} = "$config{major}.$config{minor}.$config{patch}";
351*b077aed3SPierre Pronchery$config{full_version} = "$config{version}$config{prerelease}$config{build_metadata}";
352*b077aed3SPierre Pronchery
353*b077aed3SPierre Proncherydie "erroneous version information in VERSION.dat: ",
354*b077aed3SPierre Pronchery    "$config{version}, $config{shlib_version}\n"
355*b077aed3SPierre Pronchery    unless (defined $version{MAJOR}
356*b077aed3SPierre Pronchery            && defined $version{MINOR}
357*b077aed3SPierre Pronchery            && defined $version{PATCH}
358*b077aed3SPierre Pronchery            && defined $version{SHLIB_VERSION});
359e71b7053SJung-uk Kim
360e71b7053SJung-uk Kim# Collect target configurations
361e71b7053SJung-uk Kim
362e71b7053SJung-uk Kimmy $pattern = catfile(dirname($0), "Configurations", "*.conf");
363e71b7053SJung-uk Kimforeach (sort glob($pattern)) {
364e71b7053SJung-uk Kim    &read_config($_);
365e71b7053SJung-uk Kim}
366e71b7053SJung-uk Kim
367e71b7053SJung-uk Kimif (defined env($local_config_envname)) {
368e71b7053SJung-uk Kim    if ($^O eq 'VMS') {
369e71b7053SJung-uk Kim        # VMS environment variables are logical names,
370e71b7053SJung-uk Kim        # which can be used as is
371e71b7053SJung-uk Kim        $pattern = $local_config_envname . ':' . '*.conf';
372e71b7053SJung-uk Kim    } else {
373e71b7053SJung-uk Kim        $pattern = catfile(env($local_config_envname), '*.conf');
374e71b7053SJung-uk Kim    }
375e71b7053SJung-uk Kim
376e71b7053SJung-uk Kim    foreach (sort glob($pattern)) {
377e71b7053SJung-uk Kim        &read_config($_);
378e71b7053SJung-uk Kim    }
379e71b7053SJung-uk Kim}
380e71b7053SJung-uk Kim
381e71b7053SJung-uk Kim# Save away perl command information
382e71b7053SJung-uk Kim$config{perl_cmd} = $^X;
383e71b7053SJung-uk Kim$config{perl_version} = $Config{version};
384e71b7053SJung-uk Kim$config{perl_archname} = $Config{archname};
385e71b7053SJung-uk Kim
386e71b7053SJung-uk Kim$config{prefix}="";
387e71b7053SJung-uk Kim$config{openssldir}="";
388e71b7053SJung-uk Kim$config{processor}="";
389e71b7053SJung-uk Kim$config{libdir}="";
390e71b7053SJung-uk Kimmy $auto_threads=1;    # enable threads automatically? true by default
391e71b7053SJung-uk Kimmy $default_ranlib;
392e71b7053SJung-uk Kim
393e71b7053SJung-uk Kim# Known TLS and DTLS protocols
394e71b7053SJung-uk Kimmy @tls = qw(ssl3 tls1 tls1_1 tls1_2 tls1_3);
395e71b7053SJung-uk Kimmy @dtls = qw(dtls1 dtls1_2);
396e71b7053SJung-uk Kim
397e71b7053SJung-uk Kim# Explicitly known options that are possible to disable.  They can
398e71b7053SJung-uk Kim# be regexps, and will be used like this: /^no-${option}$/
399e71b7053SJung-uk Kim# For developers: keep it sorted alphabetically
400e71b7053SJung-uk Kim
401e71b7053SJung-uk Kimmy @disablables = (
402*b077aed3SPierre Pronchery    "acvp-tests",
403e71b7053SJung-uk Kim    "afalgeng",
404e71b7053SJung-uk Kim    "aria",
405e71b7053SJung-uk Kim    "asan",
406e71b7053SJung-uk Kim    "asm",
407e71b7053SJung-uk Kim    "async",
408e71b7053SJung-uk Kim    "autoalginit",
409e71b7053SJung-uk Kim    "autoerrinit",
410e71b7053SJung-uk Kim    "autoload-config",
411e71b7053SJung-uk Kim    "bf",
412e71b7053SJung-uk Kim    "blake2",
413*b077aed3SPierre Pronchery    "buildtest-c++",
414*b077aed3SPierre Pronchery    "bulk",
415*b077aed3SPierre Pronchery    "cached-fetch",
416e71b7053SJung-uk Kim    "camellia",
417e71b7053SJung-uk Kim    "capieng",
418e71b7053SJung-uk Kim    "cast",
419e71b7053SJung-uk Kim    "chacha",
420e71b7053SJung-uk Kim    "cmac",
421*b077aed3SPierre Pronchery    "cmp",
422e71b7053SJung-uk Kim    "cms",
423e71b7053SJung-uk Kim    "comp",
424e71b7053SJung-uk Kim    "crypto-mdebug",
425e71b7053SJung-uk Kim    "ct",
426e71b7053SJung-uk Kim    "deprecated",
427e71b7053SJung-uk Kim    "des",
428e71b7053SJung-uk Kim    "devcryptoeng",
429e71b7053SJung-uk Kim    "dgram",
430e71b7053SJung-uk Kim    "dh",
431e71b7053SJung-uk Kim    "dsa",
43217f01e99SJung-uk Kim    "dso",
433e71b7053SJung-uk Kim    "dtls",
434e71b7053SJung-uk Kim    "dynamic-engine",
435e71b7053SJung-uk Kim    "ec",
436e71b7053SJung-uk Kim    "ec2m",
437*b077aed3SPierre Pronchery    "ec_nistp_64_gcc_128",
438e71b7053SJung-uk Kim    "ecdh",
439e71b7053SJung-uk Kim    "ecdsa",
440e71b7053SJung-uk Kim    "egd",
441e71b7053SJung-uk Kim    "engine",
442e71b7053SJung-uk Kim    "err",
443e71b7053SJung-uk Kim    "external-tests",
444e71b7053SJung-uk Kim    "filenames",
445*b077aed3SPierre Pronchery    "fips",
446*b077aed3SPierre Pronchery    "fips-securitychecks",
447e71b7053SJung-uk Kim    "fuzz-afl",
448*b077aed3SPierre Pronchery    "fuzz-libfuzzer",
449e71b7053SJung-uk Kim    "gost",
450e71b7053SJung-uk Kim    "idea",
451aa906e2aSJohn Baldwin    "ktls",
452*b077aed3SPierre Pronchery    "legacy",
453*b077aed3SPierre Pronchery    "loadereng",
454e71b7053SJung-uk Kim    "makedepend",
455e71b7053SJung-uk Kim    "md2",
456e71b7053SJung-uk Kim    "md4",
457e71b7053SJung-uk Kim    "mdc2",
458*b077aed3SPierre Pronchery    "module",
459e71b7053SJung-uk Kim    "msan",
460e71b7053SJung-uk Kim    "multiblock",
461e71b7053SJung-uk Kim    "nextprotoneg",
462e71b7053SJung-uk Kim    "ocb",
463e71b7053SJung-uk Kim    "ocsp",
464*b077aed3SPierre Pronchery    "padlockeng",
465e71b7053SJung-uk Kim    "pic",
466*b077aed3SPierre Pronchery    "pinshared",
467e71b7053SJung-uk Kim    "poly1305",
468e71b7053SJung-uk Kim    "posix-io",
469e71b7053SJung-uk Kim    "psk",
470e71b7053SJung-uk Kim    "rc2",
471e71b7053SJung-uk Kim    "rc4",
472e71b7053SJung-uk Kim    "rc5",
473e71b7053SJung-uk Kim    "rdrand",
474e71b7053SJung-uk Kim    "rfc3779",
475e71b7053SJung-uk Kim    "rmd160",
476e71b7053SJung-uk Kim    "scrypt",
477e71b7053SJung-uk Kim    "sctp",
478*b077aed3SPierre Pronchery    "secure-memory",
479e71b7053SJung-uk Kim    "seed",
480e71b7053SJung-uk Kim    "shared",
481e71b7053SJung-uk Kim    "siphash",
482*b077aed3SPierre Pronchery    "siv",
483e71b7053SJung-uk Kim    "sm2",
484e71b7053SJung-uk Kim    "sm3",
485e71b7053SJung-uk Kim    "sm4",
486e71b7053SJung-uk Kim    "sock",
487e71b7053SJung-uk Kim    "srp",
488e71b7053SJung-uk Kim    "srtp",
489e71b7053SJung-uk Kim    "sse2",
490e71b7053SJung-uk Kim    "ssl",
491e71b7053SJung-uk Kim    "ssl-trace",
492e71b7053SJung-uk Kim    "static-engine",
493e71b7053SJung-uk Kim    "stdio",
494e71b7053SJung-uk Kim    "tests",
495e71b7053SJung-uk Kim    "threads",
496e71b7053SJung-uk Kim    "tls",
497*b077aed3SPierre Pronchery    "trace",
498e71b7053SJung-uk Kim    "ts",
499e71b7053SJung-uk Kim    "ubsan",
500e71b7053SJung-uk Kim    "ui-console",
501e71b7053SJung-uk Kim    "unit-test",
502*b077aed3SPierre Pronchery    "uplink",
503e71b7053SJung-uk Kim    "weak-ssl-ciphers",
504*b077aed3SPierre Pronchery    "whirlpool",
505e71b7053SJung-uk Kim    "zlib",
506e71b7053SJung-uk Kim    "zlib-dynamic",
507e71b7053SJung-uk Kim    );
508e71b7053SJung-uk Kimforeach my $proto ((@tls, @dtls))
509e71b7053SJung-uk Kim        {
510e71b7053SJung-uk Kim        push(@disablables, $proto);
511e71b7053SJung-uk Kim        push(@disablables, "$proto-method") unless $proto eq "tls1_3";
512e71b7053SJung-uk Kim        }
513e71b7053SJung-uk Kim
514*b077aed3SPierre Pronchery# Internal disablables, for aliasing purposes.  They serve no special
515*b077aed3SPierre Pronchery# purpose here, but allow scripts to get to know them through configdata.pm,
516*b077aed3SPierre Pronchery# where these are merged with @disablables.
517*b077aed3SPierre Pronchery# The actual aliasing mechanism is done via %disable_cascades
518*b077aed3SPierre Proncherymy @disablables_int = qw(
519*b077aed3SPierre Pronchery    crmf
520*b077aed3SPierre Pronchery    );
521*b077aed3SPierre Pronchery
522e71b7053SJung-uk Kimmy %deprecated_disablables = (
523e71b7053SJung-uk Kim    "ssl2" => undef,
524e71b7053SJung-uk Kim    "buf-freelists" => undef,
525*b077aed3SPierre Pronchery    "crypto-mdebug-backtrace" => undef,
526*b077aed3SPierre Pronchery    "hw" => "hw",               # causes cascade, but no macro
527*b077aed3SPierre Pronchery    "hw-padlock" => "padlockeng",
528e71b7053SJung-uk Kim    "ripemd" => "rmd160",
529e71b7053SJung-uk Kim    "ui" => "ui-console",
530*b077aed3SPierre Pronchery    "heartbeats" => undef,
531e71b7053SJung-uk Kim    );
532e71b7053SJung-uk Kim
533e71b7053SJung-uk Kim# All of the following are disabled by default:
534e71b7053SJung-uk Kim
535e71b7053SJung-uk Kimour %disabled = ( # "what"         => "comment"
536*b077aed3SPierre Pronchery                  "fips"                => "default",
537e71b7053SJung-uk Kim                  "asan"                => "default",
538610a21fdSJung-uk Kim                  "buildtest-c++"       => "default",
539e71b7053SJung-uk Kim                  "crypto-mdebug"       => "default",
540e71b7053SJung-uk Kim                  "crypto-mdebug-backtrace" => "default",
541e71b7053SJung-uk Kim                  "devcryptoeng"        => "default",
5421f13597dSJung-uk Kim                  "ec_nistp_64_gcc_128" => "default",
543e71b7053SJung-uk Kim                  "egd"                 => "default",
544e71b7053SJung-uk Kim                  "external-tests"      => "default",
545e71b7053SJung-uk Kim                  "fuzz-afl"            => "default",
546*b077aed3SPierre Pronchery                  "fuzz-libfuzzer"      => "default",
547*b077aed3SPierre Pronchery                  "ktls"                => "default",
5481f13597dSJung-uk Kim                  "md2"                 => "default",
549e71b7053SJung-uk Kim                  "msan"                => "default",
5503b4e3dcbSSimon L. B. Nielsen                  "rc5"                 => "default",
5511f13597dSJung-uk Kim                  "sctp"                => "default",
552e71b7053SJung-uk Kim                  "ssl3"                => "default",
553e71b7053SJung-uk Kim                  "ssl3-method"         => "default",
554*b077aed3SPierre Pronchery                  "trace"               => "default",
555e71b7053SJung-uk Kim                  "ubsan"               => "default",
556a93cbc2bSJung-uk Kim                  "unit-test"           => "default",
5574c6a0400SJung-uk Kim                  "weak-ssl-ciphers"    => "default",
5583b4e3dcbSSimon L. B. Nielsen                  "zlib"                => "default",
559e71b7053SJung-uk Kim                  "zlib-dynamic"        => "default",
5603b4e3dcbSSimon L. B. Nielsen                );
5613b4e3dcbSSimon L. B. Nielsen
562e71b7053SJung-uk Kim# Note: => pair form used for aesthetics, not to truly make a hash table
563e71b7053SJung-uk Kimmy @disable_cascades = (
564e71b7053SJung-uk Kim    # "what"            => [ "cascade", ... ]
565*b077aed3SPierre Pronchery    "bulk"              => [ "shared", "dso",
566*b077aed3SPierre Pronchery                             "aria", "async", "autoload-config",
567*b077aed3SPierre Pronchery                             "blake2", "bf", "camellia", "cast", "chacha",
568*b077aed3SPierre Pronchery                             "cmac", "cms", "cmp", "comp", "ct",
569*b077aed3SPierre Pronchery                             "des", "dgram", "dh", "dsa",
570*b077aed3SPierre Pronchery                             "ec", "engine",
571*b077aed3SPierre Pronchery                             "filenames",
572*b077aed3SPierre Pronchery                             "idea", "ktls",
573*b077aed3SPierre Pronchery                             "md4", "multiblock", "nextprotoneg",
574*b077aed3SPierre Pronchery                             "ocsp", "ocb", "poly1305", "psk",
575*b077aed3SPierre Pronchery                             "rc2", "rc4", "rmd160",
576*b077aed3SPierre Pronchery                             "seed", "siphash", "siv",
577*b077aed3SPierre Pronchery                             "sm3", "sm4", "srp",
578*b077aed3SPierre Pronchery                             "srtp", "ssl3-method", "ssl-trace",
579*b077aed3SPierre Pronchery                             "ts", "ui-console", "whirlpool",
580*b077aed3SPierre Pronchery                             "fips-securitychecks" ],
581e71b7053SJung-uk Kim    sub { $config{processor} eq "386" }
582e71b7053SJung-uk Kim                        => [ "sse2" ],
583e71b7053SJung-uk Kim    "ssl"               => [ "ssl3" ],
584e71b7053SJung-uk Kim    "ssl3-method"       => [ "ssl3" ],
585e71b7053SJung-uk Kim    "zlib"              => [ "zlib-dynamic" ],
586e71b7053SJung-uk Kim    "des"               => [ "mdc2" ],
587*b077aed3SPierre Pronchery    "ec"                => [ "ec2m", "ecdsa", "ecdh", "sm2", "gost" ],
588e71b7053SJung-uk Kim    "dgram"             => [ "dtls", "sctp" ],
589e71b7053SJung-uk Kim    "sock"              => [ "dgram" ],
590e71b7053SJung-uk Kim    "dtls"              => [ @dtls ],
591e71b7053SJung-uk Kim    sub { 0 == scalar grep { !$disabled{$_} } @dtls }
592e71b7053SJung-uk Kim                        => [ "dtls" ],
593e71b7053SJung-uk Kim
594e71b7053SJung-uk Kim    "tls"               => [ @tls ],
595e71b7053SJung-uk Kim    sub { 0 == scalar grep { !$disabled{$_} } @tls }
596e71b7053SJung-uk Kim                        => [ "tls" ],
597e71b7053SJung-uk Kim
598e71b7053SJung-uk Kim    "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
599e71b7053SJung-uk Kim
600*b077aed3SPierre Pronchery    # If no modules, then no dynamic engines either
601*b077aed3SPierre Pronchery    "module"            => [ "dynamic-engine" ],
602*b077aed3SPierre Pronchery
603*b077aed3SPierre Pronchery    # Without shared libraries, dynamic engines aren't possible.
604*b077aed3SPierre Pronchery    # This is due to them having to link with libcrypto and register features
605*b077aed3SPierre Pronchery    # using the ENGINE functionality, and since that relies on global tables,
606*b077aed3SPierre Pronchery    # those *have* to be exacty the same as the ones accessed from the app,
607*b077aed3SPierre Pronchery    # which cannot be guaranteed if shared libraries aren't present.
608*b077aed3SPierre Pronchery    # (note that even with shared libraries, both the app and dynamic engines
609*b077aed3SPierre Pronchery    # must be linked with the same library)
610*b077aed3SPierre Pronchery    "shared"            => [ "dynamic-engine", "uplink" ],
611*b077aed3SPierre Pronchery    "dso"               => [ "dynamic-engine", "module" ],
612*b077aed3SPierre Pronchery    # Other modules don't necessarily have to link with libcrypto, so shared
613*b077aed3SPierre Pronchery    # libraries do not have to be a condition to produce those.
614*b077aed3SPierre Pronchery
615*b077aed3SPierre Pronchery    # Without position independent code, there can be no shared libraries
616*b077aed3SPierre Pronchery    # or modules.
617*b077aed3SPierre Pronchery    "pic"               => [ "shared", "module" ],
618*b077aed3SPierre Pronchery
619*b077aed3SPierre Pronchery    "module"            => [ "fips", "dso" ],
620*b077aed3SPierre Pronchery
621*b077aed3SPierre Pronchery    "engine"            => [ "dynamic-engine", grep(/eng$/, @disablables) ],
622*b077aed3SPierre Pronchery    "dynamic-engine"    => [ "loadereng" ],
623*b077aed3SPierre Pronchery    "hw"                => [ "padlockeng" ],
624e71b7053SJung-uk Kim
625e71b7053SJung-uk Kim    # no-autoalginit is only useful when building non-shared
626*b077aed3SPierre Pronchery    "autoalginit"       => [ "shared", "apps", "fips" ],
627e71b7053SJung-uk Kim
628e71b7053SJung-uk Kim    "stdio"             => [ "apps", "capieng", "egd" ],
629e71b7053SJung-uk Kim    "apps"              => [ "tests" ],
630e71b7053SJung-uk Kim    "tests"             => [ "external-tests" ],
631e71b7053SJung-uk Kim    "comp"              => [ "zlib" ],
632e71b7053SJung-uk Kim    "sm3"               => [ "sm2" ],
633e71b7053SJung-uk Kim    sub { !$disabled{"unit-test"} } => [ "heartbeats" ],
634e71b7053SJung-uk Kim
635e71b7053SJung-uk Kim    sub { !$disabled{"msan"} } => [ "asm" ],
636*b077aed3SPierre Pronchery
637*b077aed3SPierre Pronchery    "cmac"              => [ "siv" ],
638*b077aed3SPierre Pronchery    "legacy"            => [ "md2" ],
639*b077aed3SPierre Pronchery
640*b077aed3SPierre Pronchery    "cmp"               => [ "crmf" ],
641*b077aed3SPierre Pronchery
642*b077aed3SPierre Pronchery    "fips"              => [ "fips-securitychecks", "acvp-tests" ],
643*b077aed3SPierre Pronchery
644*b077aed3SPierre Pronchery    "deprecated-3.0"    => [ "engine", "srp" ]
645e71b7053SJung-uk Kim    );
646e71b7053SJung-uk Kim
647e71b7053SJung-uk Kim# Avoid protocol support holes.  Also disable all versions below N, if version
648e71b7053SJung-uk Kim# N is disabled while N+1 is enabled.
649e71b7053SJung-uk Kim#
650e71b7053SJung-uk Kimmy @list = (reverse @tls);
651e71b7053SJung-uk Kimwhile ((my $first, my $second) = (shift @list, shift @list)) {
652e71b7053SJung-uk Kim    last unless @list;
653e71b7053SJung-uk Kim    push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
654e71b7053SJung-uk Kim                              => [ @list ] );
655e71b7053SJung-uk Kim    unshift @list, $second;
656e71b7053SJung-uk Kim}
657e71b7053SJung-uk Kimmy @list = (reverse @dtls);
658e71b7053SJung-uk Kimwhile ((my $first, my $second) = (shift @list, shift @list)) {
659e71b7053SJung-uk Kim    last unless @list;
660e71b7053SJung-uk Kim    push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
661e71b7053SJung-uk Kim                              => [ @list ] );
662e71b7053SJung-uk Kim    unshift @list, $second;
663e71b7053SJung-uk Kim}
664db522d3aSSimon L. B. Nielsen
665db522d3aSSimon L. B. Nielsen# Explicit "no-..." options will be collected in %disabled along with the defaults.
666e71b7053SJung-uk Kim# To remove something from %disabled, use "enable-foo".
667db522d3aSSimon L. B. Nielsen# For symmetry, "disable-foo" is a synonym for "no-foo".
668db522d3aSSimon L. B. Nielsen
66917f01e99SJung-uk Kim# For the "make variables" CPPINCLUDES and CPPDEFINES, we support lists with
670e71b7053SJung-uk Kim# platform specific list separators.  Users from those platforms should
671e71b7053SJung-uk Kim# recognise those separators from how you set up the PATH to find executables.
672e71b7053SJung-uk Kim# The default is the Unix like separator, :, but as an exception, we also
673e71b7053SJung-uk Kim# support the space as separator.
674e71b7053SJung-uk Kimmy $list_separator_re =
675e71b7053SJung-uk Kim    { VMS           => qr/(?<!\^),/,
676e71b7053SJung-uk Kim      MSWin32       => qr/(?<!\\);/ } -> {$^O} // qr/(?<!\\)[:\s]/;
677e71b7053SJung-uk Kim# All the "make variables" we support
678e71b7053SJung-uk Kim# Some get pre-populated for the sake of backward compatibility
679e71b7053SJung-uk Kim# (we supported those before the change to "make variable" support.
680e71b7053SJung-uk Kimmy %user = (
681e71b7053SJung-uk Kim    AR          => env('AR'),
682e71b7053SJung-uk Kim    ARFLAGS     => [],
683e71b7053SJung-uk Kim    AS          => undef,
684e71b7053SJung-uk Kim    ASFLAGS     => [],
685e71b7053SJung-uk Kim    CC          => env('CC'),
686610a21fdSJung-uk Kim    CFLAGS      => [ env('CFLAGS') || () ],
687e71b7053SJung-uk Kim    CXX         => env('CXX'),
688610a21fdSJung-uk Kim    CXXFLAGS    => [ env('CXXFLAGS') || () ],
689e71b7053SJung-uk Kim    CPP         => undef,
690610a21fdSJung-uk Kim    CPPFLAGS    => [ env('CPPFLAGS') || () ],  # -D, -I, -Wp,
691e71b7053SJung-uk Kim    CPPDEFINES  => [],  # Alternative for -D
692e71b7053SJung-uk Kim    CPPINCLUDES => [],  # Alternative for -I
693e71b7053SJung-uk Kim    CROSS_COMPILE => env('CROSS_COMPILE'),
694e71b7053SJung-uk Kim    HASHBANGPERL=> env('HASHBANGPERL') || env('PERL'),
695e71b7053SJung-uk Kim    LD          => undef,
696610a21fdSJung-uk Kim    LDFLAGS     => [ env('LDFLAGS') || () ],  # -L, -Wl,
697610a21fdSJung-uk Kim    LDLIBS      => [ env('LDLIBS') || () ],  # -l
698e71b7053SJung-uk Kim    MT          => undef,
699e71b7053SJung-uk Kim    MTFLAGS     => [],
700e71b7053SJung-uk Kim    PERL        => env('PERL') || ($^O ne "VMS" ? $^X : "perl"),
701e71b7053SJung-uk Kim    RANLIB      => env('RANLIB'),
702e71b7053SJung-uk Kim    RC          => env('RC') || env('WINDRES'),
703610a21fdSJung-uk Kim    RCFLAGS     => [ env('RCFLAGS') || () ],
704e71b7053SJung-uk Kim    RM          => undef,
705e71b7053SJung-uk Kim   );
706e71b7053SJung-uk Kim# Info about what "make variables" may be prefixed with the cross compiler
707e71b7053SJung-uk Kim# prefix.  This should NEVER mention any such variable with a list for value.
708e71b7053SJung-uk Kimmy @user_crossable = qw ( AR AS CC CXX CPP LD MT RANLIB RC );
709e71b7053SJung-uk Kim# The same but for flags given as Configure options.  These are *additional*
710e71b7053SJung-uk Kim# input, as opposed to the VAR=string option that override the corresponding
711e71b7053SJung-uk Kim# config target attributes
712e71b7053SJung-uk Kimmy %useradd = (
713e71b7053SJung-uk Kim    CPPDEFINES  => [],
714e71b7053SJung-uk Kim    CPPINCLUDES => [],
715e71b7053SJung-uk Kim    CPPFLAGS    => [],
716e71b7053SJung-uk Kim    CFLAGS      => [],
717e71b7053SJung-uk Kim    CXXFLAGS    => [],
718e71b7053SJung-uk Kim    LDFLAGS     => [],
719e71b7053SJung-uk Kim    LDLIBS      => [],
720610a21fdSJung-uk Kim    RCFLAGS     => [],
721e71b7053SJung-uk Kim   );
722ddd58736SKris Kennaway
723e71b7053SJung-uk Kimmy %user_synonyms = (
724e71b7053SJung-uk Kim    HASHBANGPERL=> 'PERL',
725e71b7053SJung-uk Kim    RC          => 'WINDRES',
726e71b7053SJung-uk Kim   );
727ddd58736SKris Kennaway
728e71b7053SJung-uk Kim# Some target attributes have been renamed, this is the translation table
729e71b7053SJung-uk Kimmy %target_attr_translate =(
730e71b7053SJung-uk Kim    ar          => 'AR',
731e71b7053SJung-uk Kim    as          => 'AS',
732e71b7053SJung-uk Kim    cc          => 'CC',
733e71b7053SJung-uk Kim    cxx         => 'CXX',
734e71b7053SJung-uk Kim    cpp         => 'CPP',
735e71b7053SJung-uk Kim    hashbangperl => 'HASHBANGPERL',
736e71b7053SJung-uk Kim    ld          => 'LD',
737e71b7053SJung-uk Kim    mt          => 'MT',
738e71b7053SJung-uk Kim    ranlib      => 'RANLIB',
739e71b7053SJung-uk Kim    rc          => 'RC',
740e71b7053SJung-uk Kim    rm          => 'RM',
741e71b7053SJung-uk Kim   );
742e71b7053SJung-uk Kim
743e71b7053SJung-uk Kim# Initialisers coming from 'config' scripts
744e71b7053SJung-uk Kim$config{defines} = [ split(/$list_separator_re/, env('__CNF_CPPDEFINES')) ];
745e71b7053SJung-uk Kim$config{includes} = [ split(/$list_separator_re/, env('__CNF_CPPINCLUDES')) ];
746e71b7053SJung-uk Kim$config{cppflags} = [ env('__CNF_CPPFLAGS') || () ];
747e71b7053SJung-uk Kim$config{cflags} = [ env('__CNF_CFLAGS') || () ];
748e71b7053SJung-uk Kim$config{cxxflags} = [ env('__CNF_CXXFLAGS') || () ];
749e71b7053SJung-uk Kim$config{lflags} = [ env('__CNF_LDFLAGS') || () ];
750e71b7053SJung-uk Kim$config{ex_libs} = [ env('__CNF_LDLIBS') || () ];
751e71b7053SJung-uk Kim
752e71b7053SJung-uk Kim$config{openssl_api_defines}=[];
753e71b7053SJung-uk Kim$config{openssl_sys_defines}=[];
754*b077aed3SPierre Pronchery$config{openssl_feature_defines}=[];
755e71b7053SJung-uk Kim$config{options}="";
756e71b7053SJung-uk Kim$config{build_type} = "release";
757e71b7053SJung-uk Kimmy $target="";
758e71b7053SJung-uk Kim
759e71b7053SJung-uk Kimmy %cmdvars = ();               # Stores FOO='blah' type arguments
760e71b7053SJung-uk Kimmy %unsupported_options = ();
761e71b7053SJung-uk Kimmy %deprecated_options = ();
762e71b7053SJung-uk Kim# If you change this, update apps/version.c
763e71b7053SJung-uk Kimmy @known_seed_sources = qw(getrandom devrandom os egd none rdcpu librandom);
764e71b7053SJung-uk Kimmy @seed_sources = ();
765e71b7053SJung-uk Kimwhile (@argvcopy)
766ddd58736SKris Kennaway        {
767e71b7053SJung-uk Kim        $_ = shift @argvcopy;
768ddd58736SKris Kennaway
769e71b7053SJung-uk Kim        # Support env variable assignments among the options
770e71b7053SJung-uk Kim        if (m|^(\w+)=(.+)?$|)
77174664626SKris Kennaway                {
772e71b7053SJung-uk Kim                $cmdvars{$1} = $2;
773e71b7053SJung-uk Kim                # Every time a variable is given as a configuration argument,
774e71b7053SJung-uk Kim                # it acts as a reset if the variable.
775e71b7053SJung-uk Kim                if (exists $user{$1})
776e71b7053SJung-uk Kim                        {
777e71b7053SJung-uk Kim                        $user{$1} = ref $user{$1} eq "ARRAY" ? [] : undef;
778e71b7053SJung-uk Kim                        }
779e71b7053SJung-uk Kim                #if (exists $useradd{$1})
780e71b7053SJung-uk Kim                #       {
781e71b7053SJung-uk Kim                #       $useradd{$1} = [];
782e71b7053SJung-uk Kim                #       }
783e71b7053SJung-uk Kim                next;
784e71b7053SJung-uk Kim                }
785e71b7053SJung-uk Kim
786e71b7053SJung-uk Kim        # VMS is a case insensitive environment, and depending on settings
787e71b7053SJung-uk Kim        # out of our control, we may receive options uppercased.  Let's
788e71b7053SJung-uk Kim        # downcase at least the part before any equal sign.
789e71b7053SJung-uk Kim        if ($^O eq "VMS")
790e71b7053SJung-uk Kim                {
791e71b7053SJung-uk Kim                s/^([^=]*)/lc($1)/e;
792e71b7053SJung-uk Kim                }
793e71b7053SJung-uk Kim
794e71b7053SJung-uk Kim        # some people just can't read the instructions, clang people have to...
795e71b7053SJung-uk Kim        s/^-no-(?!integrated-as)/no-/;
7963b4e3dcbSSimon L. B. Nielsen
7973b4e3dcbSSimon L. B. Nielsen        # rewrite some options in "enable-..." form
7983b4e3dcbSSimon L. B. Nielsen        s /^-?-?shared$/enable-shared/;
7991f13597dSJung-uk Kim        s /^sctp$/enable-sctp/;
8003b4e3dcbSSimon L. B. Nielsen        s /^threads$/enable-threads/;
8013b4e3dcbSSimon L. B. Nielsen        s /^zlib$/enable-zlib/;
8023b4e3dcbSSimon L. B. Nielsen        s /^zlib-dynamic$/enable-zlib-dynamic/;
803*b077aed3SPierre Pronchery        s /^fips$/enable-fips/;
8043b4e3dcbSSimon L. B. Nielsen
805e71b7053SJung-uk Kim        if (/^(no|disable|enable)-(.+)$/)
806e71b7053SJung-uk Kim                {
807e71b7053SJung-uk Kim                my $word = $2;
808*b077aed3SPierre Pronchery                if ($word !~ m|hw(?:-.+)| # special treatment for hw regexp opt
809*b077aed3SPierre Pronchery                        && !exists $deprecated_disablables{$word}
810*b077aed3SPierre Pronchery                        && !grep { $word eq $_ } @disablables)
811e71b7053SJung-uk Kim                        {
812e71b7053SJung-uk Kim                        $unsupported_options{$_} = 1;
813e71b7053SJung-uk Kim                        next;
814e71b7053SJung-uk Kim                        }
815e71b7053SJung-uk Kim                }
8163b4e3dcbSSimon L. B. Nielsen        if (/^no-(.+)$/ || /^disable-(.+)$/)
8173b4e3dcbSSimon L. B. Nielsen                {
818e71b7053SJung-uk Kim                foreach my $proto ((@tls, @dtls))
819db522d3aSSimon L. B. Nielsen                        {
820e71b7053SJung-uk Kim                        if ($1 eq "$proto-method")
8213b4e3dcbSSimon L. B. Nielsen                                {
822e71b7053SJung-uk Kim                                $disabled{"$proto"} = "option($proto-method)";
823e71b7053SJung-uk Kim                                last;
824e71b7053SJung-uk Kim                                }
825e71b7053SJung-uk Kim                        }
826e71b7053SJung-uk Kim                if ($1 eq "dtls")
827e71b7053SJung-uk Kim                        {
828e71b7053SJung-uk Kim                        foreach my $proto (@dtls)
829e71b7053SJung-uk Kim                                {
830e71b7053SJung-uk Kim                                $disabled{$proto} = "option(dtls)";
831e71b7053SJung-uk Kim                                }
832e71b7053SJung-uk Kim                        $disabled{"dtls"} = "option(dtls)";
833e71b7053SJung-uk Kim                        }
834e71b7053SJung-uk Kim                elsif ($1 eq "ssl")
835e71b7053SJung-uk Kim                        {
836e71b7053SJung-uk Kim                        # Last one of its kind
8373b4e3dcbSSimon L. B. Nielsen                        $disabled{"ssl3"} = "option(ssl)";
8383b4e3dcbSSimon L. B. Nielsen                        }
8393b4e3dcbSSimon L. B. Nielsen                elsif ($1 eq "tls")
8403b4e3dcbSSimon L. B. Nielsen                        {
841e71b7053SJung-uk Kim                        # XXX: Tests will fail if all SSL/TLS
842e71b7053SJung-uk Kim                        # protocols are disabled.
843e71b7053SJung-uk Kim                        foreach my $proto (@tls)
844751d2991SJung-uk Kim                                {
845e71b7053SJung-uk Kim                                $disabled{$proto} = "option(tls)";
846e71b7053SJung-uk Kim                                }
847e71b7053SJung-uk Kim                        }
848e71b7053SJung-uk Kim                elsif ($1 eq "static-engine")
849e71b7053SJung-uk Kim                        {
850e71b7053SJung-uk Kim                        delete $disabled{"dynamic-engine"};
851e71b7053SJung-uk Kim                        }
852e71b7053SJung-uk Kim                elsif ($1 eq "dynamic-engine")
853e71b7053SJung-uk Kim                        {
854e71b7053SJung-uk Kim                        $disabled{"dynamic-engine"} = "option";
855e71b7053SJung-uk Kim                        }
856e71b7053SJung-uk Kim                elsif (exists $deprecated_disablables{$1})
857e71b7053SJung-uk Kim                        {
858e71b7053SJung-uk Kim                        $deprecated_options{$_} = 1;
859e71b7053SJung-uk Kim                        if (defined $deprecated_disablables{$1})
860e71b7053SJung-uk Kim                                {
861e71b7053SJung-uk Kim                                $disabled{$deprecated_disablables{$1}} = "option";
862e71b7053SJung-uk Kim                                }
863751d2991SJung-uk Kim                        }
864*b077aed3SPierre Pronchery                elsif ($1 =~ m|hw(?:-.+)|) # deprecate hw options in regexp form
865*b077aed3SPierre Pronchery                        {
866*b077aed3SPierre Pronchery                        $deprecated_options{$_} = 1;
867610a21fdSJung-uk Kim                        }
8683b4e3dcbSSimon L. B. Nielsen                else
8693b4e3dcbSSimon L. B. Nielsen                        {
8703b4e3dcbSSimon L. B. Nielsen                        $disabled{$1} = "option";
8713b4e3dcbSSimon L. B. Nielsen                        }
872e71b7053SJung-uk Kim                # No longer an automatic choice
873e71b7053SJung-uk Kim                $auto_threads = 0 if ($1 eq "threads");
8743b4e3dcbSSimon L. B. Nielsen                }
875e71b7053SJung-uk Kim        elsif (/^enable-(.+)$/)
8763b4e3dcbSSimon L. B. Nielsen                {
877e71b7053SJung-uk Kim                if ($1 eq "static-engine")
878e71b7053SJung-uk Kim                        {
879e71b7053SJung-uk Kim                        $disabled{"dynamic-engine"} = "option";
880e71b7053SJung-uk Kim                        }
881e71b7053SJung-uk Kim                elsif ($1 eq "dynamic-engine")
882e71b7053SJung-uk Kim                        {
883e71b7053SJung-uk Kim                        delete $disabled{"dynamic-engine"};
884e71b7053SJung-uk Kim                        }
885e71b7053SJung-uk Kim                elsif ($1 eq "zlib-dynamic")
886e71b7053SJung-uk Kim                        {
887e71b7053SJung-uk Kim                        delete $disabled{"zlib"};
888e71b7053SJung-uk Kim                        }
889db522d3aSSimon L. B. Nielsen                my $algo = $1;
890db522d3aSSimon L. B. Nielsen                delete $disabled{$algo};
8913b4e3dcbSSimon L. B. Nielsen
892e71b7053SJung-uk Kim                # No longer an automatic choice
893e71b7053SJung-uk Kim                $auto_threads = 0 if ($1 eq "threads");
8943b4e3dcbSSimon L. B. Nielsen                }
895*b077aed3SPierre Pronchery        elsif (/^-d$/)          # From older 'config'
896*b077aed3SPierre Pronchery                {
897*b077aed3SPierre Pronchery                $config{build_type} = "debug";
898*b077aed3SPierre Pronchery                }
899*b077aed3SPierre Pronchery        elsif (/^-v$/)          # From older 'config'
900*b077aed3SPierre Pronchery                {
901*b077aed3SPierre Pronchery                $guess_opts{verbose} = 1;
902*b077aed3SPierre Pronchery                }
903*b077aed3SPierre Pronchery        elsif (/^-w$/)
904*b077aed3SPierre Pronchery                {
905*b077aed3SPierre Pronchery                $guess_opts{nowait} = 1;
906*b077aed3SPierre Pronchery                }
907*b077aed3SPierre Pronchery        elsif (/^-t$/)          # From older 'config'
908*b077aed3SPierre Pronchery                {
909*b077aed3SPierre Pronchery                $dryrun = 1;
910*b077aed3SPierre Pronchery                }
911e71b7053SJung-uk Kim        elsif (/^--strict-warnings$/)
9126a599222SSimon L. B. Nielsen                {
913610a21fdSJung-uk Kim                # Pretend that our strict flags is a C flag, and replace it
914610a21fdSJung-uk Kim                # with the proper flags later on
915610a21fdSJung-uk Kim                push @{$useradd{CFLAGS}}, '--ossl-strict-warnings';
9166a599222SSimon L. B. Nielsen                $strict_warnings=1;
9176a599222SSimon L. B. Nielsen                }
918e71b7053SJung-uk Kim        elsif (/^--debug$/)
919ddd58736SKris Kennaway                {
920e71b7053SJung-uk Kim                $config{build_type} = "debug";
921ddd58736SKris Kennaway                }
922e71b7053SJung-uk Kim        elsif (/^--release$/)
923e71b7053SJung-uk Kim                {
924e71b7053SJung-uk Kim                $config{build_type} = "release";
925ddd58736SKris Kennaway                }
92674664626SKris Kennaway        elsif (/^386$/)
927e71b7053SJung-uk Kim                { $config{processor}=386; }
92874664626SKris Kennaway        elsif (/^rsaref$/)
92974664626SKris Kennaway                {
9305c87c606SMark Murray                # No RSAref support any more since it's not needed.
9315c87c606SMark Murray                # The check for the option is there so scripts aren't
9325c87c606SMark Murray                # broken
93374664626SKris Kennaway                }
93417f01e99SJung-uk Kim        elsif (m|^[-+/]|)
93574664626SKris Kennaway                {
9367bded2dbSJung-uk Kim                if (/^--prefix=(.*)$/)
93774664626SKris Kennaway                        {
938e71b7053SJung-uk Kim                        $config{prefix}=$1;
939e71b7053SJung-uk Kim                        die "Directory given with --prefix MUST be absolute\n"
940e71b7053SJung-uk Kim                                unless file_name_is_absolute($config{prefix});
941e71b7053SJung-uk Kim                        }
942e71b7053SJung-uk Kim                elsif (/^--api=(.*)$/)
943e71b7053SJung-uk Kim                        {
944*b077aed3SPierre Pronchery                        my $api = $1;
945*b077aed3SPierre Pronchery                        die "Unknown API compatibility level $api"
946*b077aed3SPierre Pronchery                                unless defined $apitable->{$api};
947*b077aed3SPierre Pronchery                        $config{api}=$apitable->{$api};
94874664626SKris Kennaway                        }
9496a599222SSimon L. B. Nielsen                elsif (/^--libdir=(.*)$/)
9506a599222SSimon L. B. Nielsen                        {
951e71b7053SJung-uk Kim                        $config{libdir}=$1;
9526a599222SSimon L. B. Nielsen                        }
95374664626SKris Kennaway                elsif (/^--openssldir=(.*)$/)
95474664626SKris Kennaway                        {
955e71b7053SJung-uk Kim                        $config{openssldir}=$1;
9565c87c606SMark Murray                        }
9573b4e3dcbSSimon L. B. Nielsen                elsif (/^--with-zlib-lib=(.*)$/)
9583b4e3dcbSSimon L. B. Nielsen                        {
959e71b7053SJung-uk Kim                        $withargs{zlib_lib}=$1;
9603b4e3dcbSSimon L. B. Nielsen                        }
9613b4e3dcbSSimon L. B. Nielsen                elsif (/^--with-zlib-include=(.*)$/)
9623b4e3dcbSSimon L. B. Nielsen                        {
963e71b7053SJung-uk Kim                        $withargs{zlib_include}=$1;
9643b4e3dcbSSimon L. B. Nielsen                        }
965e71b7053SJung-uk Kim                elsif (/^--with-fuzzer-lib=(.*)$/)
9661f13597dSJung-uk Kim                        {
967e71b7053SJung-uk Kim                        $withargs{fuzzer_lib}=$1;
9681f13597dSJung-uk Kim                        }
969e71b7053SJung-uk Kim                elsif (/^--with-fuzzer-include=(.*)$/)
970db522d3aSSimon L. B. Nielsen                        {
971e71b7053SJung-uk Kim                        $withargs{fuzzer_include}=$1;
972db522d3aSSimon L. B. Nielsen                        }
973e71b7053SJung-uk Kim                elsif (/^--with-rand-seed=(.*)$/)
974db522d3aSSimon L. B. Nielsen                        {
975e71b7053SJung-uk Kim                        foreach my $x (split(m|,|, $1))
976e71b7053SJung-uk Kim                            {
977e71b7053SJung-uk Kim                            die "Unknown --with-rand-seed choice $x\n"
978e71b7053SJung-uk Kim                                if ! grep { $x eq $_ } @known_seed_sources;
979e71b7053SJung-uk Kim                            push @seed_sources, $x;
980e71b7053SJung-uk Kim                            }
981db522d3aSSimon L. B. Nielsen                        }
982*b077aed3SPierre Pronchery                elsif (/^--fips-key=(.*)$/)
983*b077aed3SPierre Pronchery                        {
984*b077aed3SPierre Pronchery                        $user{FIPSKEY}=lc($1);
985*b077aed3SPierre Pronchery                        die "Non-hex character in FIPS key\n"
986*b077aed3SPierre Pronchery                           if $user{FIPSKEY} =~ /[^a-f0-9]/;
987*b077aed3SPierre Pronchery                        die "FIPS key must have even number of characters\n"
988*b077aed3SPierre Pronchery                           if length $1 & 1;
989*b077aed3SPierre Pronchery                        die "FIPS key too long (64 bytes max)\n"
990*b077aed3SPierre Pronchery                           if length $1 > 64;
991*b077aed3SPierre Pronchery                        }
992*b077aed3SPierre Pronchery                elsif (/^--banner=(.*)$/)
993*b077aed3SPierre Pronchery                        {
994*b077aed3SPierre Pronchery                        $banner = $1 . "\n";
995*b077aed3SPierre Pronchery                        }
9961f13597dSJung-uk Kim                elsif (/^--cross-compile-prefix=(.*)$/)
9971f13597dSJung-uk Kim                        {
998e71b7053SJung-uk Kim                        $user{CROSS_COMPILE}=$1;
9991f13597dSJung-uk Kim                        }
1000e71b7053SJung-uk Kim                elsif (/^--config=(.*)$/)
100174664626SKris Kennaway                        {
1002e71b7053SJung-uk Kim                        read_config $1;
1003e71b7053SJung-uk Kim                        }
1004e71b7053SJung-uk Kim                elsif (/^-l(.*)$/)
1005e71b7053SJung-uk Kim                        {
1006e71b7053SJung-uk Kim                        push @{$useradd{LDLIBS}}, $_;
1007e71b7053SJung-uk Kim                        }
1008e71b7053SJung-uk Kim                elsif (/^-framework$/)
1009e71b7053SJung-uk Kim                        {
1010e71b7053SJung-uk Kim                        push @{$useradd{LDLIBS}}, $_, shift(@argvcopy);
1011e71b7053SJung-uk Kim                        }
1012e71b7053SJung-uk Kim                elsif (/^-L(.*)$/ or /^-Wl,/)
1013e71b7053SJung-uk Kim                        {
1014e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_;
1015e71b7053SJung-uk Kim                        }
1016e71b7053SJung-uk Kim                elsif (/^-rpath$/ or /^-R$/)
1017e71b7053SJung-uk Kim                        # -rpath is the OSF1 rpath flag
1018e71b7053SJung-uk Kim                        # -R is the old Solaris rpath flag
1019e71b7053SJung-uk Kim                        {
1020e71b7053SJung-uk Kim                        my $rpath = shift(@argvcopy) || "";
1021e71b7053SJung-uk Kim                        $rpath .= " " if $rpath ne "";
1022e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_, $rpath;
1023e71b7053SJung-uk Kim                        }
1024e71b7053SJung-uk Kim                elsif (/^-static$/)
1025e71b7053SJung-uk Kim                        {
1026e71b7053SJung-uk Kim                        push @{$useradd{LDFLAGS}}, $_;
1027e71b7053SJung-uk Kim                        }
102817f01e99SJung-uk Kim                elsif (m|^[-/]D(.*)$|)
1029e71b7053SJung-uk Kim                        {
1030e71b7053SJung-uk Kim                        push @{$useradd{CPPDEFINES}}, $1;
1031e71b7053SJung-uk Kim                        }
103217f01e99SJung-uk Kim                elsif (m|^[-/]I(.*)$|)
1033e71b7053SJung-uk Kim                        {
1034e71b7053SJung-uk Kim                        push @{$useradd{CPPINCLUDES}}, $1;
1035e71b7053SJung-uk Kim                        }
1036e71b7053SJung-uk Kim                elsif (/^-Wp,$/)
1037e71b7053SJung-uk Kim                        {
1038e71b7053SJung-uk Kim                        push @{$useradd{CPPFLAGS}}, $1;
10397bded2dbSJung-uk Kim                        }
10407bded2dbSJung-uk Kim                else    # common if (/^[-+]/), just pass down...
10417bded2dbSJung-uk Kim                        {
104217f01e99SJung-uk Kim                        # Treat %xx as an ASCII code (e.g. replace %20 by a space character).
104317f01e99SJung-uk Kim                        # This provides a simple way to pass options with arguments separated
104417f01e99SJung-uk Kim                        # by spaces without quoting (e.g. -opt%20arg translates to -opt arg).
10457bded2dbSJung-uk Kim                        $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
1046e71b7053SJung-uk Kim                        push @{$useradd{CFLAGS}}, $_;
1047e71b7053SJung-uk Kim                        push @{$useradd{CXXFLAGS}}, $_;
104874664626SKris Kennaway                        }
104974664626SKris Kennaway                }
105017f01e99SJung-uk Kim        elsif (m|^/|)
105117f01e99SJung-uk Kim                {
105217f01e99SJung-uk Kim                # Treat %xx as an ASCII code (e.g. replace %20 by a space character).
105317f01e99SJung-uk Kim                # This provides a simple way to pass options with arguments separated
105417f01e99SJung-uk Kim                # by spaces without quoting (e.g. /opt%20arg translates to /opt arg).
105517f01e99SJung-uk Kim                $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
105617f01e99SJung-uk Kim                push @{$useradd{CFLAGS}}, $_;
105717f01e99SJung-uk Kim                push @{$useradd{CXXFLAGS}}, $_;
105817f01e99SJung-uk Kim                }
105974664626SKris Kennaway        else
106074664626SKris Kennaway                {
10613b4e3dcbSSimon L. B. Nielsen                die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
106274664626SKris Kennaway                $target=$_;
106374664626SKris Kennaway                }
10643b4e3dcbSSimon L. B. Nielsen        unless ($_ eq $target || /^no-/ || /^disable-/)
10655c87c606SMark Murray                {
1066e71b7053SJung-uk Kim                # "no-..." follows later after implied deactivations
1067e71b7053SJung-uk Kim                # have been derived.  (Don't take this too seriously,
10683b4e3dcbSSimon L. B. Nielsen                # we really only write OPTIONS to the Makefile out of
10693b4e3dcbSSimon L. B. Nielsen                # nostalgia.)
10703b4e3dcbSSimon L. B. Nielsen
1071e71b7053SJung-uk Kim                if ($config{options} eq "")
1072e71b7053SJung-uk Kim                        { $config{options} = $_; }
10733b4e3dcbSSimon L. B. Nielsen                else
1074e71b7053SJung-uk Kim                        { $config{options} .= " ".$_; }
10753b4e3dcbSSimon L. B. Nielsen                }
10765c87c606SMark Murray        }
10775c87c606SMark Murray
1078e71b7053SJung-uk Kimif (keys %deprecated_options)
10795c87c606SMark Murray        {
1080e71b7053SJung-uk Kim        warn "***** Deprecated options: ",
1081e71b7053SJung-uk Kim                join(", ", keys %deprecated_options), "\n";
1082e71b7053SJung-uk Kim        }
1083e71b7053SJung-uk Kimif (keys %unsupported_options)
1084e71b7053SJung-uk Kim        {
1085e71b7053SJung-uk Kim        die "***** Unsupported options: ",
1086e71b7053SJung-uk Kim                join(", ", keys %unsupported_options), "\n";
10875c87c606SMark Murray        }
10885c87c606SMark Murray
1089e71b7053SJung-uk Kim# If any %useradd entry has been set, we must check that the "make
1090e71b7053SJung-uk Kim# variables" haven't been set.  We start by checking of any %useradd entry
1091e71b7053SJung-uk Kim# is set.
1092e71b7053SJung-uk Kimif (grep { scalar @$_ > 0 } values %useradd) {
1093e71b7053SJung-uk Kim    # Hash of env / make variables names.  The possible values are:
1094e71b7053SJung-uk Kim    # 1 - "make vars"
1095e71b7053SJung-uk Kim    # 2 - %useradd entry set
1096e71b7053SJung-uk Kim    # 3 - both set
1097e71b7053SJung-uk Kim    my %detected_vars =
1098e71b7053SJung-uk Kim        map { my $v = 0;
1099e71b7053SJung-uk Kim              $v += 1 if $cmdvars{$_};
1100e71b7053SJung-uk Kim              $v += 2 if @{$useradd{$_}};
1101e71b7053SJung-uk Kim              $_ => $v }
1102e71b7053SJung-uk Kim        keys %useradd;
1103e71b7053SJung-uk Kim
1104e71b7053SJung-uk Kim    # If any of the corresponding "make variables" is set, we error
1105e71b7053SJung-uk Kim    if (grep { $_ & 1 } values %detected_vars) {
1106e71b7053SJung-uk Kim        my $names = join(', ', grep { $detected_vars{$_} > 0 }
1107e71b7053SJung-uk Kim                               sort keys %detected_vars);
1108e71b7053SJung-uk Kim        die <<"_____";
1109e71b7053SJung-uk Kim***** Mixing make variables and additional compiler/linker flags as
1110e71b7053SJung-uk Kim***** configure command line option is not permitted.
1111e71b7053SJung-uk Kim***** Affected make variables: $names
1112e71b7053SJung-uk Kim_____
1113e71b7053SJung-uk Kim    }
11145c87c606SMark Murray}
11155c87c606SMark Murray
1116e71b7053SJung-uk Kim# Check through all supported command line variables to see if any of them
1117e71b7053SJung-uk Kim# were set, and canonicalise the values we got.  If no compiler or linker
1118e71b7053SJung-uk Kim# flag or anything else that affects %useradd was set, we also check the
1119e71b7053SJung-uk Kim# environment for values.
1120e71b7053SJung-uk Kimmy $anyuseradd =
1121e71b7053SJung-uk Kim    grep { defined $_ && (ref $_ ne 'ARRAY' || @$_) } values %useradd;
1122e71b7053SJung-uk Kimforeach (keys %user) {
1123e71b7053SJung-uk Kim    my $value = $cmdvars{$_};
1124e71b7053SJung-uk Kim    $value //= env($_) unless $anyuseradd;
1125e71b7053SJung-uk Kim    $value //=
1126e71b7053SJung-uk Kim        defined $user_synonyms{$_} ? $cmdvars{$user_synonyms{$_}} : undef;
1127e71b7053SJung-uk Kim    $value //= defined $user_synonyms{$_} ? env($user_synonyms{$_}) : undef
1128e71b7053SJung-uk Kim        unless $anyuseradd;
1129e71b7053SJung-uk Kim
1130e71b7053SJung-uk Kim    if (defined $value) {
1131e71b7053SJung-uk Kim        if (ref $user{$_} eq 'ARRAY') {
113217f01e99SJung-uk Kim            if ($_ eq 'CPPDEFINES' || $_ eq 'CPPINCLUDES') {
1133e71b7053SJung-uk Kim                $user{$_} = [ split /$list_separator_re/, $value ];
113417f01e99SJung-uk Kim            } else {
113517f01e99SJung-uk Kim                $user{$_} = [ $value ];
113617f01e99SJung-uk Kim            }
1137e71b7053SJung-uk Kim        } elsif (!defined $user{$_}) {
1138e71b7053SJung-uk Kim            $user{$_} = $value;
1139e71b7053SJung-uk Kim        }
1140e71b7053SJung-uk Kim    }
11413b4e3dcbSSimon L. B. Nielsen}
11423b4e3dcbSSimon L. B. Nielsen
1143e71b7053SJung-uk Kimif (grep { /-rpath\b/ } ($user{LDFLAGS} ? @{$user{LDFLAGS}} : ())
1144e71b7053SJung-uk Kim    && !$disabled{shared}
1145e71b7053SJung-uk Kim    && !($disabled{asan} && $disabled{msan} && $disabled{ubsan})) {
1146e71b7053SJung-uk Kim    die "***** Cannot simultaneously use -rpath, shared libraries, and\n",
1147e71b7053SJung-uk Kim        "***** any of asan, msan or ubsan\n";
11483b4e3dcbSSimon L. B. Nielsen}
11493b4e3dcbSSimon L. B. Nielsen
1150*b077aed3SPierre Pronchery# If no target was given, try guessing.
1151*b077aed3SPierre Proncheryunless ($target) {
1152*b077aed3SPierre Pronchery    my %system_config = OpenSSL::config::get_platform(%guess_opts, %user);
1153*b077aed3SPierre Pronchery
1154*b077aed3SPierre Pronchery    # The $system_config{disable} is used to populate %disabled with
1155*b077aed3SPierre Pronchery    # entries that aren't already there.
1156*b077aed3SPierre Pronchery    foreach ( @{$system_config{disable} // []} ) {
1157*b077aed3SPierre Pronchery        $disabled{$_} = 'system' unless defined $disabled{$_};
1158*b077aed3SPierre Pronchery    }
1159*b077aed3SPierre Pronchery    delete $system_config{disable};
1160*b077aed3SPierre Pronchery
1161*b077aed3SPierre Pronchery    # Override config entries with stuff from the guesser.
1162*b077aed3SPierre Pronchery    # It's assumed that this really is nothing new.
1163*b077aed3SPierre Pronchery    %config = ( %config, %system_config );
1164*b077aed3SPierre Pronchery    $target = $system_config{target};
1165*b077aed3SPierre Pronchery}
1166*b077aed3SPierre Pronchery
1167610a21fdSJung-uk Kimsub disable {
1168610a21fdSJung-uk Kim    my $disable_type = shift;
1169610a21fdSJung-uk Kim
1170610a21fdSJung-uk Kim    for (@_) {
1171610a21fdSJung-uk Kim        $disabled{$_} = $disable_type;
1172610a21fdSJung-uk Kim    }
1173610a21fdSJung-uk Kim
1174610a21fdSJung-uk Kim    my @tocheckfor = (@_ ? @_ : keys %disabled);
1175e71b7053SJung-uk Kim    while (@tocheckfor) {
1176e71b7053SJung-uk Kim        my %new_tocheckfor = ();
1177e71b7053SJung-uk Kim        my @cascade_copy = (@disable_cascades);
1178e71b7053SJung-uk Kim        while (@cascade_copy) {
1179610a21fdSJung-uk Kim            my ($test, $descendents) =
1180610a21fdSJung-uk Kim                (shift @cascade_copy, shift @cascade_copy);
1181e71b7053SJung-uk Kim            if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
1182e71b7053SJung-uk Kim                foreach (grep { !defined($disabled{$_}) } @$descendents) {
1183610a21fdSJung-uk Kim                    $new_tocheckfor{$_} = 1; $disabled{$_} = "cascade";
1184e71b7053SJung-uk Kim                }
1185e71b7053SJung-uk Kim            }
1186e71b7053SJung-uk Kim        }
1187e71b7053SJung-uk Kim        @tocheckfor = (keys %new_tocheckfor);
11883b4e3dcbSSimon L. B. Nielsen    }
1189610a21fdSJung-uk Kim}
1190610a21fdSJung-uk Kimdisable();                     # First cascade run
11913b4e3dcbSSimon L. B. Nielsen
1192e71b7053SJung-uk Kimour $die = sub { die @_; };
119374664626SKris Kennawayif ($target eq "TABLE") {
1194e71b7053SJung-uk Kim    local $die = sub { warn @_; };
1195e71b7053SJung-uk Kim    foreach (sort keys %table) {
1196e71b7053SJung-uk Kim        print_table_entry($_, "TABLE");
119774664626SKris Kennaway    }
119874664626SKris Kennaway    exit 0;
119974664626SKris Kennaway}
120074664626SKris Kennaway
1201f579bf8eSKris Kennawayif ($target eq "LIST") {
1202f579bf8eSKris Kennaway    foreach (sort keys %table) {
1203e71b7053SJung-uk Kim        print $_,"\n" unless $table{$_}->{template};
1204f579bf8eSKris Kennaway    }
1205f579bf8eSKris Kennaway    exit 0;
1206f579bf8eSKris Kennaway}
1207f579bf8eSKris Kennaway
1208e71b7053SJung-uk Kimif ($target eq "HASH") {
1209e71b7053SJung-uk Kim    local $die = sub { warn @_; };
1210e71b7053SJung-uk Kim    print "%table = (\n";
1211e71b7053SJung-uk Kim    foreach (sort keys %table) {
1212e71b7053SJung-uk Kim        print_table_entry($_, "HASH");
1213e71b7053SJung-uk Kim    }
1214e71b7053SJung-uk Kim    exit 0;
1215e71b7053SJung-uk Kim}
1216e71b7053SJung-uk Kim
1217*b077aed3SPierre Proncheryprint "Configuring OpenSSL version $config{full_version} ";
1218*b077aed3SPierre Proncheryprint "for target $target\n";
1219e71b7053SJung-uk Kim
1220e71b7053SJung-uk Kimif (scalar(@seed_sources) == 0) {
1221e71b7053SJung-uk Kim    print "Using os-specific seed configuration\n";
1222e71b7053SJung-uk Kim    push @seed_sources, 'os';
1223e71b7053SJung-uk Kim}
122458f35182SJung-uk Kimif (scalar(grep { $_ eq 'egd' } @seed_sources) > 0) {
122558f35182SJung-uk Kim    delete $disabled{'egd'};
122658f35182SJung-uk Kim}
1227e71b7053SJung-uk Kimif (scalar(grep { $_ eq 'none' } @seed_sources) > 0) {
1228e71b7053SJung-uk Kim    die "Cannot seed with none and anything else" if scalar(@seed_sources) > 1;
1229e71b7053SJung-uk Kim    warn <<_____ if scalar(@seed_sources) == 1;
1230e71b7053SJung-uk Kim
1231c9cf7b5cSJung-uk Kim============================== WARNING ===============================
1232c9cf7b5cSJung-uk KimYou have selected the --with-rand-seed=none option, which effectively
1233c9cf7b5cSJung-uk Kimdisables automatic reseeding of the OpenSSL random generator.
1234c9cf7b5cSJung-uk KimAll operations depending on the random generator such as creating keys
1235c9cf7b5cSJung-uk Kimwill not work unless the random generator is seeded manually by the
1236c9cf7b5cSJung-uk Kimapplication.
1237c9cf7b5cSJung-uk Kim
1238c9cf7b5cSJung-uk KimPlease read the 'Note on random number generation' section in the
1239*b077aed3SPierre ProncheryINSTALL.md instructions and the RAND_DRBG(7) manual page for more
1240*b077aed3SPierre Proncherydetails.
1241c9cf7b5cSJung-uk Kim============================== WARNING ===============================
1242c9cf7b5cSJung-uk Kim
1243e71b7053SJung-uk Kim_____
1244e71b7053SJung-uk Kim}
1245*b077aed3SPierre Proncherypush @{$config{openssl_feature_defines}},
1246e71b7053SJung-uk Kim     map { (my $x = $_) =~ tr|[\-a-z]|[_A-Z]|; "OPENSSL_RAND_SEED_$x" }
1247e71b7053SJung-uk Kim        @seed_sources;
1248e71b7053SJung-uk Kim
1249e71b7053SJung-uk Kim# Backward compatibility?
1250c1803d78SJacques Vidrineif ($target =~ m/^CygWin32(-.*)$/) {
1251c1803d78SJacques Vidrine    $target = "Cygwin".$1;
1252c1803d78SJacques Vidrine}
1253c1803d78SJacques Vidrine
1254e71b7053SJung-uk Kim# Support for legacy targets having a name starting with 'debug-'
1255e71b7053SJung-uk Kimmy ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
1256e71b7053SJung-uk Kimif ($d) {
1257e71b7053SJung-uk Kim    $config{build_type} = "debug";
1258ddd58736SKris Kennaway
1259e71b7053SJung-uk Kim    # If we do not find debug-foo in the table, the target is set to foo.
1260e71b7053SJung-uk Kim    if (!$table{$target}) {
1261e71b7053SJung-uk Kim        $target = $t;
12621f13597dSJung-uk Kim    }
1263e71b7053SJung-uk Kim}
12641f13597dSJung-uk Kim
1265*b077aed3SPierre Proncheryif ($target) {
1266*b077aed3SPierre Pronchery    # It's possible that we have different config targets for specific
1267*b077aed3SPierre Pronchery    # toolchains, so we try to detect them, and go for the plain config
1268*b077aed3SPierre Pronchery    # target if not.
1269*b077aed3SPierre Pronchery    my $found;
1270*b077aed3SPierre Pronchery    foreach ( ( "$target-$user{CC}", "$target", undef ) ) {
1271*b077aed3SPierre Pronchery        $found=$_ if $table{$_} && !$table{$_}->{template};
1272*b077aed3SPierre Pronchery        last if $found;
1273*b077aed3SPierre Pronchery    }
1274*b077aed3SPierre Pronchery    $target = $found;
1275*b077aed3SPierre Pronchery} else {
1276*b077aed3SPierre Pronchery    # If we don't have a config target now, we try the C compiler as we
1277*b077aed3SPierre Pronchery    # fallback
1278*b077aed3SPierre Pronchery    my $cc = $user{CC} // 'cc';
1279*b077aed3SPierre Pronchery    $target = $cc if $table{$cc} && !$table{$cc}->{template};
1280*b077aed3SPierre Pronchery}
1281*b077aed3SPierre Pronchery
1282*b077aed3SPierre Pronchery&usage unless $target;
1283*b077aed3SPierre Pronchery
1284*b077aed3SPierre Proncheryexit 0 if $dryrun;              # From older 'config'
1285e71b7053SJung-uk Kim
1286e71b7053SJung-uk Kim$config{target} = $target;
1287e71b7053SJung-uk Kimmy %target = resolve_config($target);
1288e71b7053SJung-uk Kim
1289e71b7053SJung-uk Kimforeach (keys %target_attr_translate) {
1290e71b7053SJung-uk Kim    $target{$target_attr_translate{$_}} = $target{$_}
1291e71b7053SJung-uk Kim        if $target{$_};
1292e71b7053SJung-uk Kim    delete $target{$_};
1293e71b7053SJung-uk Kim}
1294e71b7053SJung-uk Kim
1295e71b7053SJung-uk Kim%target = ( %{$table{DEFAULTS}}, %target );
1296e71b7053SJung-uk Kim
1297e71b7053SJung-uk Kimmy %conf_files = map { $_ => 1 } (@{$target{_conf_fname_int}});
1298e71b7053SJung-uk Kim$config{conf_files} = [ sort keys %conf_files ];
1299e71b7053SJung-uk Kim
1300610a21fdSJung-uk Kim# Using sub disable within these loops may prove fragile, so we run
1301610a21fdSJung-uk Kim# a cascade afterwards
1302e71b7053SJung-uk Kimforeach my $feature (@{$target{disable}}) {
1303e71b7053SJung-uk Kim    if (exists $deprecated_disablables{$feature}) {
1304e71b7053SJung-uk Kim        warn "***** config $target disables deprecated feature $feature\n";
1305e71b7053SJung-uk Kim    } elsif (!grep { $feature eq $_ } @disablables) {
1306e71b7053SJung-uk Kim        die "***** config $target disables unknown feature $feature\n";
1307e71b7053SJung-uk Kim    }
1308e71b7053SJung-uk Kim    $disabled{$feature} = 'config';
1309e71b7053SJung-uk Kim}
1310e71b7053SJung-uk Kimforeach my $feature (@{$target{enable}}) {
13116935a639SJung-uk Kim    if ("default" eq ($disabled{$feature} // "")) {
1312e71b7053SJung-uk Kim        if (exists $deprecated_disablables{$feature}) {
1313e71b7053SJung-uk Kim            warn "***** config $target enables deprecated feature $feature\n";
1314e71b7053SJung-uk Kim        } elsif (!grep { $feature eq $_ } @disablables) {
1315e71b7053SJung-uk Kim            die "***** config $target enables unknown feature $feature\n";
1316e71b7053SJung-uk Kim        }
13176935a639SJung-uk Kim        delete $disabled{$feature};
1318e71b7053SJung-uk Kim    }
1319e71b7053SJung-uk Kim}
1320*b077aed3SPierre Pronchery
1321*b077aed3SPierre Pronchery# If uplink_arch isn't defined, disable uplink
1322*b077aed3SPierre Pronchery$disabled{uplink} = 'no uplink_arch' unless (defined $target{uplink_arch});
1323*b077aed3SPierre Pronchery# If asm_arch isn't defined, disable asm
1324*b077aed3SPierre Pronchery$disabled{asm} = 'no asm_arch' unless (defined $target{asm_arch});
1325*b077aed3SPierre Pronchery
1326610a21fdSJung-uk Kimdisable();                      # Run a cascade now
1327e71b7053SJung-uk Kim
1328e71b7053SJung-uk Kim$target{CXXFLAGS}//=$target{CFLAGS} if $target{CXX};
1329e71b7053SJung-uk Kim$target{cxxflags}//=$target{cflags} if $target{CXX};
1330*b077aed3SPierre Pronchery$target{exe_extension}=".exe" if ($config{target} eq "DJGPP");
1331e71b7053SJung-uk Kim$target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
1332e71b7053SJung-uk Kim
1333e71b7053SJung-uk Kim# Fill %config with values from %user, and in case those are undefined or
1334e71b7053SJung-uk Kim# empty, use values from %target (acting as a default).
1335e71b7053SJung-uk Kimforeach (keys %user) {
1336e71b7053SJung-uk Kim    my $ref_type = ref $user{$_};
1337e71b7053SJung-uk Kim
1338e71b7053SJung-uk Kim    # Temporary function.  Takes an intended ref type (empty string or "ARRAY")
1339e71b7053SJung-uk Kim    # and a value that's to be coerced into that type.
1340e71b7053SJung-uk Kim    my $mkvalue = sub {
1341e71b7053SJung-uk Kim        my $type = shift;
1342e71b7053SJung-uk Kim        my $value = shift;
1343e71b7053SJung-uk Kim        my $undef_p = shift;
1344e71b7053SJung-uk Kim
1345e71b7053SJung-uk Kim        die "Too many arguments for \$mkvalue" if @_;
1346e71b7053SJung-uk Kim
1347e71b7053SJung-uk Kim        while (ref $value eq 'CODE') {
1348e71b7053SJung-uk Kim            $value = $value->();
1349e71b7053SJung-uk Kim        }
1350e71b7053SJung-uk Kim
1351e71b7053SJung-uk Kim        if ($type eq 'ARRAY') {
1352e71b7053SJung-uk Kim            return undef unless defined $value;
1353e71b7053SJung-uk Kim            return undef if ref $value ne 'ARRAY' && !$value;
1354e71b7053SJung-uk Kim            return undef if ref $value eq 'ARRAY' && !@$value;
1355e71b7053SJung-uk Kim            return [ $value ] unless ref $value eq 'ARRAY';
1356e71b7053SJung-uk Kim        }
1357e71b7053SJung-uk Kim        return undef unless $value;
1358e71b7053SJung-uk Kim        return $value;
1359e71b7053SJung-uk Kim    };
1360e71b7053SJung-uk Kim
1361e71b7053SJung-uk Kim    $config{$_} =
1362e71b7053SJung-uk Kim        $mkvalue->($ref_type, $user{$_})
1363e71b7053SJung-uk Kim        || $mkvalue->($ref_type, $target{$_});
1364e71b7053SJung-uk Kim    delete $config{$_} unless defined $config{$_};
1365e71b7053SJung-uk Kim}
1366e71b7053SJung-uk Kim
1367610a21fdSJung-uk Kim# Finish up %config by appending things the user gave us on the command line
1368610a21fdSJung-uk Kim# apart from "make variables"
1369610a21fdSJung-uk Kimforeach (keys %useradd) {
1370610a21fdSJung-uk Kim    # The must all be lists, so we assert that here
1371610a21fdSJung-uk Kim    die "internal error: \$useradd{$_} isn't an ARRAY\n"
1372610a21fdSJung-uk Kim        unless ref $useradd{$_} eq 'ARRAY';
1373610a21fdSJung-uk Kim
1374610a21fdSJung-uk Kim    if (defined $config{$_}) {
1375610a21fdSJung-uk Kim        push @{$config{$_}}, @{$useradd{$_}};
1376610a21fdSJung-uk Kim    } else {
1377610a21fdSJung-uk Kim        $config{$_} = [ @{$useradd{$_}} ];
1378610a21fdSJung-uk Kim    }
1379610a21fdSJung-uk Kim}
1380610a21fdSJung-uk Kim# At this point, we can forget everything about %user and %useradd,
1381610a21fdSJung-uk Kim# because it's now all been merged into the corresponding $config entry
1382610a21fdSJung-uk Kim
1383*b077aed3SPierre Proncheryif (grep { $_ =~ /(?:^|\s)-static(?:\s|$)/ } @{$config{LDFLAGS}}) {
138488e852c0SJung-uk Kim    disable('static', 'pic', 'threads');
138588e852c0SJung-uk Kim}
138688e852c0SJung-uk Kim
1387e71b7053SJung-uk Kim# Allow overriding the build file name
1388e71b7053SJung-uk Kim$config{build_file} = env('BUILDFILE') || $target{build_file} || "Makefile";
1389e71b7053SJung-uk Kim
1390e71b7053SJung-uk Kim# Make sure build_scheme is consistent.
1391e71b7053SJung-uk Kim$target{build_scheme} = [ $target{build_scheme} ]
1392e71b7053SJung-uk Kim    if ref($target{build_scheme}) ne "ARRAY";
1393e71b7053SJung-uk Kim
1394e71b7053SJung-uk Kimmy ($builder, $builder_platform, @builder_opts) =
1395e71b7053SJung-uk Kim    @{$target{build_scheme}};
1396e71b7053SJung-uk Kim
1397640242a5SJung-uk Kimforeach my $checker (($builder_platform."-".$config{build_file}."-checker.pm",
1398e71b7053SJung-uk Kim                      $builder_platform."-checker.pm")) {
1399e71b7053SJung-uk Kim    my $checker_path = catfile($srcdir, "Configurations", $checker);
1400e71b7053SJung-uk Kim    if (-f $checker_path) {
1401e71b7053SJung-uk Kim        my $fn = $ENV{CONFIGURE_CHECKER_WARN}
1402e71b7053SJung-uk Kim            ? sub { warn $@; } : sub { die $@; };
1403e71b7053SJung-uk Kim        if (! do $checker_path) {
1404e71b7053SJung-uk Kim            if ($@) {
1405e71b7053SJung-uk Kim                $fn->($@);
1406e71b7053SJung-uk Kim            } elsif ($!) {
1407e71b7053SJung-uk Kim                $fn->($!);
1408e71b7053SJung-uk Kim            } else {
1409e71b7053SJung-uk Kim                $fn->("The detected tools didn't match the platform\n");
1410e71b7053SJung-uk Kim            }
1411e71b7053SJung-uk Kim        }
1412e71b7053SJung-uk Kim        last;
1413e71b7053SJung-uk Kim    }
1414e71b7053SJung-uk Kim}
1415e71b7053SJung-uk Kim
1416e71b7053SJung-uk Kimpush @{$config{defines}}, "NDEBUG"    if $config{build_type} eq "release";
1417e71b7053SJung-uk Kim
1418e71b7053SJung-uk Kimif ($target =~ /^mingw/ && `$config{CC} --target-help 2>&1` =~ m/-mno-cygwin/m)
14191f13597dSJung-uk Kim        {
1420e71b7053SJung-uk Kim        push @{$config{cflags}}, "-mno-cygwin";
1421e71b7053SJung-uk Kim        push @{$config{cxxflags}}, "-mno-cygwin" if $config{CXX};
1422e71b7053SJung-uk Kim        push @{$config{shared_ldflag}}, "-mno-cygwin";
14231f13597dSJung-uk Kim        }
14241f13597dSJung-uk Kim
1425e71b7053SJung-uk Kimif ($target =~ /linux.*-mips/ && !$disabled{asm}
14268f1ef87aSJung-uk Kim        && !grep { $_ =~ /-m(ips|arch=)/ } (@{$config{CFLAGS}})) {
14277bded2dbSJung-uk Kim        # minimally required architecture flags for assembly modules
1428e71b7053SJung-uk Kim        my $value;
1429e71b7053SJung-uk Kim        $value = '-mips2' if ($target =~ /mips32/);
1430e71b7053SJung-uk Kim        $value = '-mips3' if ($target =~ /mips64/);
1431e71b7053SJung-uk Kim        unshift @{$config{cflags}}, $value;
1432e71b7053SJung-uk Kim        unshift @{$config{cxxflags}}, $value if $config{CXX};
1433e71b7053SJung-uk Kim}
1434e71b7053SJung-uk Kim
1435e71b7053SJung-uk Kim# If threads aren't disabled, check how possible they are
1436e71b7053SJung-uk Kimunless ($disabled{threads}) {
1437e71b7053SJung-uk Kim    if ($auto_threads) {
1438e71b7053SJung-uk Kim        # Enabled by default, disable it forcibly if unavailable
1439e71b7053SJung-uk Kim        if ($target{thread_scheme} eq "(unknown)") {
1440610a21fdSJung-uk Kim            disable("unavailable", 'threads');
1441e71b7053SJung-uk Kim        }
1442e71b7053SJung-uk Kim    } else {
1443e71b7053SJung-uk Kim        # The user chose to enable threads explicitly, let's see
1444e71b7053SJung-uk Kim        # if there's a chance that's possible
1445e71b7053SJung-uk Kim        if ($target{thread_scheme} eq "(unknown)") {
1446e71b7053SJung-uk Kim            # If the user asked for "threads" and we don't have internal
1447e71b7053SJung-uk Kim            # knowledge how to do it, [s]he is expected to provide any
1448e71b7053SJung-uk Kim            # system-dependent compiler options that are necessary.  We
1449e71b7053SJung-uk Kim            # can't truly check that the given options are correct, but
1450e71b7053SJung-uk Kim            # we expect the user to know what [s]He is doing.
1451610a21fdSJung-uk Kim            if (!@{$config{CFLAGS}} && !@{$config{CPPDEFINES}}) {
1452e71b7053SJung-uk Kim                die "You asked for multi-threading support, but didn't\n"
1453e71b7053SJung-uk Kim                    ,"provide any system-specific compiler options\n";
1454e71b7053SJung-uk Kim            }
1455e71b7053SJung-uk Kim        }
1456e71b7053SJung-uk Kim    }
1457e71b7053SJung-uk Kim}
1458e71b7053SJung-uk Kim
1459*b077aed3SPierre Pronchery# Find out if clang's sanitizers have been enabled with -fsanitize
1460*b077aed3SPierre Pronchery# flags and ensure that the corresponding %disabled elements area
1461*b077aed3SPierre Pronchery# removed to reflect that the sanitizers are indeed enabled.
1462*b077aed3SPierre Proncherymy %detected_sanitizers = ();
1463*b077aed3SPierre Proncheryforeach (grep /^-fsanitize=/, @{$config{CFLAGS} || []}) {
1464*b077aed3SPierre Pronchery    (my $checks = $_) =~ s/^-fsanitize=//;
1465*b077aed3SPierre Pronchery    foreach (split /,/, $checks) {
1466*b077aed3SPierre Pronchery        my $d = { address       => 'asan',
1467*b077aed3SPierre Pronchery                  undefined     => 'ubsan',
1468*b077aed3SPierre Pronchery                  memory        => 'msan' } -> {$_};
1469*b077aed3SPierre Pronchery        next unless defined $d;
1470*b077aed3SPierre Pronchery
1471*b077aed3SPierre Pronchery        $detected_sanitizers{$d} = 1;
1472*b077aed3SPierre Pronchery        if (defined $disabled{$d}) {
1473*b077aed3SPierre Pronchery            die "***** Conflict between disabling $d and enabling $_ sanitizer"
1474*b077aed3SPierre Pronchery                if $disabled{$d} ne "default";
1475*b077aed3SPierre Pronchery            delete $disabled{$d};
1476*b077aed3SPierre Pronchery        }
1477*b077aed3SPierre Pronchery    }
1478*b077aed3SPierre Pronchery}
1479*b077aed3SPierre Pronchery
1480e71b7053SJung-uk Kim# If threads still aren't disabled, add a C macro to ensure the source
1481e71b7053SJung-uk Kim# code knows about it.  Any other flag is taken care of by the configs.
1482e71b7053SJung-uk Kimunless($disabled{threads}) {
1483*b077aed3SPierre Pronchery    push @{$config{openssl_feature_defines}}, "OPENSSL_THREADS";
14847bded2dbSJung-uk Kim}
14857bded2dbSJung-uk Kim
1486fceca8a3SJacques Vidrinemy $no_shared_warn=0;
1487*b077aed3SPierre Proncheryif (($target{shared_target} // '') eq "")
14885c87c606SMark Murray        {
1489e71b7053SJung-uk Kim        $no_shared_warn = 1
1490e71b7053SJung-uk Kim            if (!$disabled{shared} || !$disabled{"dynamic-engine"});
1491610a21fdSJung-uk Kim        disable('no-shared-target', 'pic');
14925c87c606SMark Murray        }
14935c87c606SMark Murray
1494e71b7053SJung-uk Kimif ($disabled{"dynamic-engine"}) {
1495e71b7053SJung-uk Kim        $config{dynamic_engines} = 0;
1496e71b7053SJung-uk Kim} else {
1497e71b7053SJung-uk Kim        $config{dynamic_engines} = 1;
1498ddd58736SKris Kennaway}
1499ddd58736SKris Kennaway
1500*b077aed3SPierre Proncheryunless ($disabled{asan} || defined $detected_sanitizers{asan}) {
1501e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fsanitize=address";
150274664626SKris Kennaway}
150374664626SKris Kennaway
1504*b077aed3SPierre Proncheryunless ($disabled{ubsan} || defined $detected_sanitizers{ubsan}) {
1505*b077aed3SPierre Pronchery    push @{$config{cflags}}, "-fsanitize=undefined", "-fno-sanitize-recover=all", "-DPEDANTIC";
150674664626SKris Kennaway}
150774664626SKris Kennaway
1508*b077aed3SPierre Proncheryunless ($disabled{msan} || defined $detected_sanitizers{msan}) {
1509e71b7053SJung-uk Kim  push @{$config{cflags}}, "-fsanitize=memory";
15105c87c606SMark Murray}
15115c87c606SMark Murray
1512e71b7053SJung-uk Kimunless ($disabled{"fuzz-libfuzzer"} && $disabled{"fuzz-afl"}
1513e71b7053SJung-uk Kim        && $disabled{asan} && $disabled{ubsan} && $disabled{msan}) {
1514e71b7053SJung-uk Kim    push @{$config{cflags}}, "-fno-omit-frame-pointer", "-g";
1515e71b7053SJung-uk Kim    push @{$config{cxxflags}}, "-fno-omit-frame-pointer", "-g" if $config{CXX};
151674664626SKris Kennaway}
1517db522d3aSSimon L. B. Nielsen#
1518db522d3aSSimon L. B. Nielsen# Platform fix-ups
1519db522d3aSSimon L. B. Nielsen#
1520db522d3aSSimon L. B. Nielsen
1521e71b7053SJung-uk Kim# This saves the build files from having to check
1522e71b7053SJung-uk Kimif ($disabled{pic})
1523db522d3aSSimon L. B. Nielsen        {
1524e71b7053SJung-uk Kim        foreach (qw(shared_cflag shared_cxxflag shared_cppflag
1525e71b7053SJung-uk Kim                    shared_defines shared_includes shared_ldflag
1526e71b7053SJung-uk Kim                    module_cflags module_cxxflags module_cppflags
1527e71b7053SJung-uk Kim                    module_defines module_includes module_lflags))
1528e71b7053SJung-uk Kim                {
1529e71b7053SJung-uk Kim                delete $config{$_};
1530e71b7053SJung-uk Kim                $target{$_} = "";
1531ddd58736SKris Kennaway                }
1532aeb5019cSJung-uk Kim        }
1533aeb5019cSJung-uk Kimelse
1534aeb5019cSJung-uk Kim        {
1535e71b7053SJung-uk Kim        push @{$config{lib_defines}}, "OPENSSL_PIC";
1536aeb5019cSJung-uk Kim        }
1537e71b7053SJung-uk Kim
1538e71b7053SJung-uk Kimif ($target{sys_id} ne "")
153974664626SKris Kennaway        {
1540e71b7053SJung-uk Kim        push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
15413b4e3dcbSSimon L. B. Nielsen        }
1542e71b7053SJung-uk Kim
1543610a21fdSJung-uk Kimmy %predefined_C = compiler_predefined($config{CROSS_COMPILE}.$config{CC});
1544610a21fdSJung-uk Kimmy %predefined_CXX = $config{CXX}
1545610a21fdSJung-uk Kim    ? compiler_predefined($config{CROSS_COMPILE}.$config{CXX})
1546610a21fdSJung-uk Kim    : ();
15474f20a5a2SJacques Vidrine
1548*b077aed3SPierre Proncheryunless ($disabled{asm}) {
1549*b077aed3SPierre Pronchery    # big endian systems can use ELFv2 ABI
1550*b077aed3SPierre Pronchery    if ($target eq "linux-ppc64") {
1551*b077aed3SPierre Pronchery        $target{perlasm_scheme} = "linux64v2" if ($predefined_C{_CALL_ELF} == 2);
1552*b077aed3SPierre Pronchery    }
1553*b077aed3SPierre Pronchery}
1554*b077aed3SPierre Pronchery
1555e71b7053SJung-uk Kim# Check for makedepend capabilities.
1556e71b7053SJung-uk Kimif (!$disabled{makedepend}) {
1557*b077aed3SPierre Pronchery    # If the attribute makedep_scheme is defined, then we assume that the
1558*b077aed3SPierre Pronchery    # config target and its associated build file are programmed to deal
1559*b077aed3SPierre Pronchery    # with it.
1560*b077aed3SPierre Pronchery    # If makedep_scheme is undefined, we go looking for GCC compatible
1561*b077aed3SPierre Pronchery    # dependency making, and if that's not available, we try to fall back
1562*b077aed3SPierre Pronchery    # on 'makedepend'.
1563*b077aed3SPierre Pronchery    if ($target{makedep_scheme}) {
1564*b077aed3SPierre Pronchery        $config{makedep_scheme} = $target{makedep_scheme};
1565*b077aed3SPierre Pronchery        # If the makedepcmd attribute is defined, copy it.  If not, the
1566*b077aed3SPierre Pronchery        # build files will have to fend for themselves.
1567*b077aed3SPierre Pronchery        $config{makedepcmd} = $target{makedepcmd} if $target{makedepcmd};
1568610a21fdSJung-uk Kim    } elsif (($predefined_C{__GNUC__} // -1) >= 3
1569610a21fdSJung-uk Kim             && !($predefined_C{__APPLE_CC__} && !$predefined_C{__clang__})) {
1570e71b7053SJung-uk Kim        # We know that GNU C version 3 and up as well as all clang
1571e71b7053SJung-uk Kim        # versions support dependency generation, but Xcode did not
1572e71b7053SJung-uk Kim        # handle $cc -M before clang support (but claims __GNUC__ = 3)
1573*b077aed3SPierre Pronchery        $config{makedep_scheme} = 'gcc';
1574e71b7053SJung-uk Kim    } else {
1575*b077aed3SPierre Pronchery        # In all other cases, we look for 'makedepend', and set the
1576*b077aed3SPierre Pronchery        # makedep_scheme value if we found it.
1577*b077aed3SPierre Pronchery        $config{makedepcmd} = which('makedepend');
1578*b077aed3SPierre Pronchery        $config{makedep_scheme} = 'makedepend' if $config{makedepcmd};
157974664626SKris Kennaway    }
1580*b077aed3SPierre Pronchery
1581*b077aed3SPierre Pronchery    # If no depend scheme is set, we disable makedepend
1582*b077aed3SPierre Pronchery    disable('unavailable', 'makedepend') unless $config{makedep_scheme};
158374664626SKris Kennaway}
158474664626SKris Kennaway
1585610a21fdSJung-uk Kimif (!$disabled{asm} && !$predefined_C{__MACH__} && $^O ne 'VMS') {
1586e71b7053SJung-uk Kim    # probe for -Wa,--noexecstack option...
1587610a21fdSJung-uk Kim    if ($predefined_C{__clang__}) {
1588e71b7053SJung-uk Kim        # clang has builtin assembler, which doesn't recognize --help,
1589e71b7053SJung-uk Kim        # but it apparently recognizes the option in question on all
1590e71b7053SJung-uk Kim        # supported platforms even when it's meaningless. In other words
1591e71b7053SJung-uk Kim        # probe would fail, but probed option always accepted...
1592e71b7053SJung-uk Kim        push @{$config{cflags}}, "-Wa,--noexecstack", "-Qunused-arguments";
1593e71b7053SJung-uk Kim    } else {
1594e71b7053SJung-uk Kim        my $cc = $config{CROSS_COMPILE}.$config{CC};
1595e71b7053SJung-uk Kim        open(PIPE, "$cc -Wa,--help -c -o null.$$.o -x assembler /dev/null 2>&1 |");
1596dee36b4fSJung-uk Kim        while(<PIPE>) {
1597e71b7053SJung-uk Kim            if (m/--noexecstack/) {
1598e71b7053SJung-uk Kim                push @{$config{cflags}}, "-Wa,--noexecstack";
1599e71b7053SJung-uk Kim                last;
1600e71b7053SJung-uk Kim            }
1601dee36b4fSJung-uk Kim        }
1602dee36b4fSJung-uk Kim        close(PIPE);
1603e71b7053SJung-uk Kim        unlink("null.$$.o");
1604e71b7053SJung-uk Kim    }
1605e71b7053SJung-uk Kim}
160680815a77SJung-uk Kim
1607e71b7053SJung-uk Kim# Deal with bn_ops ###################################################
1608e71b7053SJung-uk Kim
1609e71b7053SJung-uk Kim$config{bn_ll}                  =0;
1610e71b7053SJung-uk Kimmy $def_int="unsigned int";
1611e71b7053SJung-uk Kim$config{rc4_int}                =$def_int;
1612e71b7053SJung-uk Kim($config{b64l},$config{b64},$config{b32})=(0,0,1);
1613e71b7053SJung-uk Kim
1614e71b7053SJung-uk Kimmy $count = 0;
1615e71b7053SJung-uk Kimforeach (sort split(/\s+/,$target{bn_ops})) {
1616e71b7053SJung-uk Kim    $count++ if /SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT/;
1617e71b7053SJung-uk Kim    $config{bn_ll}=1                            if $_ eq 'BN_LLONG';
1618e71b7053SJung-uk Kim    $config{rc4_int}="unsigned char"            if $_ eq 'RC4_CHAR';
1619e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1620e71b7053SJung-uk Kim        =(0,1,0)                                if $_ eq 'SIXTY_FOUR_BIT';
1621e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1622e71b7053SJung-uk Kim        =(1,0,0)                                if $_ eq 'SIXTY_FOUR_BIT_LONG';
1623e71b7053SJung-uk Kim    ($config{b64l},$config{b64},$config{b32})
1624e71b7053SJung-uk Kim        =(0,0,1)                                if $_ eq 'THIRTY_TWO_BIT';
1625e71b7053SJung-uk Kim}
1626e71b7053SJung-uk Kimdie "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
1627e71b7053SJung-uk Kim    if $count > 1;
1628e71b7053SJung-uk Kim
1629*b077aed3SPierre Pronchery$config{api} = $config{major} * 10000 + $config{minor} * 100
1630*b077aed3SPierre Pronchery    unless $config{api};
1631*b077aed3SPierre Proncheryforeach (keys %$apitable) {
1632*b077aed3SPierre Pronchery    $disabled{"deprecated-$_"} = "deprecation"
1633*b077aed3SPierre Pronchery        if $disabled{deprecated} && $config{api} >= $apitable->{$_};
1634*b077aed3SPierre Pronchery}
1635*b077aed3SPierre Pronchery
1636*b077aed3SPierre Proncherydisable();                      # Run a cascade now
1637e71b7053SJung-uk Kim
1638e71b7053SJung-uk Kim# Hack cflags for better warnings (dev option) #######################
1639e71b7053SJung-uk Kim
1640e71b7053SJung-uk Kim# "Stringify" the C and C++ flags string.  This permits it to be made part of
1641e71b7053SJung-uk Kim# a string and works as well on command lines.
1642e71b7053SJung-uk Kim$config{cflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1643e71b7053SJung-uk Kim                        @{$config{cflags}} ];
1644e71b7053SJung-uk Kim$config{cxxflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1645e71b7053SJung-uk Kim                          @{$config{cxxflags}} ] if $config{CXX};
1646e71b7053SJung-uk Kim
1647*b077aed3SPierre Pronchery$config{openssl_api_defines} = [
1648*b077aed3SPierre Pronchery    "OPENSSL_CONFIGURED_API=".$config{api},
1649*b077aed3SPierre Pronchery];
1650dea77ea6SJung-uk Kim
1651610a21fdSJung-uk Kimmy @strict_warnings_collection=();
165280815a77SJung-uk Kimif ($strict_warnings)
165380815a77SJung-uk Kim        {
16546a599222SSimon L. B. Nielsen        my $wopt;
1655610a21fdSJung-uk Kim        my $gccver = $predefined_C{__GNUC__} // -1;
1656e71b7053SJung-uk Kim
165717f01e99SJung-uk Kim        if ($gccver >= 4)
165817f01e99SJung-uk Kim                {
1659610a21fdSJung-uk Kim                push @strict_warnings_collection, @gcc_devteam_warn;
1660610a21fdSJung-uk Kim                push @strict_warnings_collection, @clang_devteam_warn
1661610a21fdSJung-uk Kim                    if (defined($predefined_C{__clang__}));
16626a599222SSimon L. B. Nielsen                }
166317f01e99SJung-uk Kim        elsif ($config{target} =~ /^VC-/)
166417f01e99SJung-uk Kim                {
166517f01e99SJung-uk Kim                push @strict_warnings_collection, @cl_devteam_warn;
166617f01e99SJung-uk Kim                }
166717f01e99SJung-uk Kim        else
166817f01e99SJung-uk Kim                {
166917f01e99SJung-uk Kim                warn "WARNING --strict-warnings requires gcc[>=4] or gcc-alike, or MSVC"
167017f01e99SJung-uk Kim                }
167117f01e99SJung-uk Kim        }
1672610a21fdSJung-uk Kim
1673610a21fdSJung-uk Kim$config{CFLAGS} = [ map { $_ eq '--ossl-strict-warnings'
1674610a21fdSJung-uk Kim                              ? @strict_warnings_collection
1675610a21fdSJung-uk Kim                              : ( $_ ) }
1676610a21fdSJung-uk Kim                    @{$config{CFLAGS}} ];
16776a599222SSimon L. B. Nielsen
1678e71b7053SJung-uk Kimunless ($disabled{afalgeng}) {
1679e71b7053SJung-uk Kim    $config{afalgeng}="";
1680e71b7053SJung-uk Kim    if (grep { $_ eq 'afalgeng' } @{$target{enable}}) {
1681e71b7053SJung-uk Kim        my $minver = 4*10000 + 1*100 + 0;
1682e71b7053SJung-uk Kim        if ($config{CROSS_COMPILE} eq "") {
1683e71b7053SJung-uk Kim            my $verstr = `uname -r`;
1684e71b7053SJung-uk Kim            my ($ma, $mi1, $mi2) = split("\\.", $verstr);
1685e71b7053SJung-uk Kim            ($mi2) = $mi2 =~ /(\d+)/;
1686e71b7053SJung-uk Kim            my $ver = $ma*10000 + $mi1*100 + $mi2;
1687e71b7053SJung-uk Kim            if ($ver < $minver) {
1688610a21fdSJung-uk Kim                disable('too-old-kernel', 'afalgeng');
1689e71b7053SJung-uk Kim            } else {
1690e71b7053SJung-uk Kim                push @{$config{engdirs}}, "afalg";
1691e71b7053SJung-uk Kim            }
1692e71b7053SJung-uk Kim        } else {
1693610a21fdSJung-uk Kim            disable('cross-compiling', 'afalgeng');
1694e71b7053SJung-uk Kim        }
1695e71b7053SJung-uk Kim    } else {
1696610a21fdSJung-uk Kim        disable('not-linux', 'afalgeng');
1697e71b7053SJung-uk Kim    }
1698e71b7053SJung-uk Kim}
1699f579bf8eSKris Kennaway
170017f01e99SJung-uk Kimunless ($disabled{devcryptoeng}) {
170117f01e99SJung-uk Kim    if ($target =~ m/^BSD/) {
170217f01e99SJung-uk Kim        my $maxver = 5*100 + 7;
170317f01e99SJung-uk Kim        my $sysstr = `uname -s`;
170417f01e99SJung-uk Kim        my $verstr = `uname -r`;
170517f01e99SJung-uk Kim        $sysstr =~ s|\R$||;
170617f01e99SJung-uk Kim        $verstr =~ s|\R$||;
170717f01e99SJung-uk Kim        my ($ma, $mi, @rest) = split m|\.|, $verstr;
170817f01e99SJung-uk Kim        my $ver = $ma*100 + $mi;
170917f01e99SJung-uk Kim        if ($sysstr eq 'OpenBSD' && $ver >= $maxver) {
171017f01e99SJung-uk Kim            disable('too-new-kernel', 'devcryptoeng');
171117f01e99SJung-uk Kim        }
171217f01e99SJung-uk Kim    }
171317f01e99SJung-uk Kim}
1714e71b7053SJung-uk Kim
1715aa906e2aSJohn Baldwinunless ($disabled{ktls}) {
1716aa906e2aSJohn Baldwin    $config{ktls}="";
1717*b077aed3SPierre Pronchery    my $cc = $config{CROSS_COMPILE}.$config{CC};
1718aa906e2aSJohn Baldwin    if ($target =~ m/^linux/) {
1719*b077aed3SPierre Pronchery        system("printf '#include <sys/types.h>\n#include <linux/tls.h>' | $cc -E - >/dev/null 2>&1");
1720*b077aed3SPierre Pronchery        if ($? != 0) {
1721aa906e2aSJohn Baldwin            disable('too-old-kernel', 'ktls');
1722aa906e2aSJohn Baldwin        }
1723aa906e2aSJohn Baldwin    } elsif ($target =~ m/^BSD/) {
1724aa906e2aSJohn Baldwin        system("printf '#include <sys/types.h>\n#include <sys/ktls.h>' | $cc -E - >/dev/null 2>&1");
1725aa906e2aSJohn Baldwin        if ($? != 0) {
1726aa906e2aSJohn Baldwin            disable('too-old-freebsd', 'ktls');
1727aa906e2aSJohn Baldwin        }
1728aa906e2aSJohn Baldwin    } else {
1729aa906e2aSJohn Baldwin        disable('not-linux-or-freebsd', 'ktls');
1730aa906e2aSJohn Baldwin    }
1731aa906e2aSJohn Baldwin}
1732aa906e2aSJohn Baldwin
1733aa906e2aSJohn Baldwinpush @{$config{openssl_other_defines}}, "OPENSSL_NO_KTLS" if ($disabled{ktls});
1734aa906e2aSJohn Baldwin
1735610a21fdSJung-uk Kim# Get the extra flags used when building shared libraries and modules.  We
1736610a21fdSJung-uk Kim# do this late because some of them depend on %disabled.
1737e71b7053SJung-uk Kim
1738610a21fdSJung-uk Kim# Make the flags to build DSOs the same as for shared libraries unless they
1739610a21fdSJung-uk Kim# are already defined
1740610a21fdSJung-uk Kim$target{module_cflags} = $target{shared_cflag} unless defined $target{module_cflags};
1741610a21fdSJung-uk Kim$target{module_cxxflags} = $target{shared_cxxflag} unless defined $target{module_cxxflags};
1742610a21fdSJung-uk Kim$target{module_ldflags} = $target{shared_ldflag} unless defined $target{module_ldflags};
1743610a21fdSJung-uk Kim{
1744610a21fdSJung-uk Kim    my $shared_info_pl =
1745610a21fdSJung-uk Kim        catfile(dirname($0), "Configurations", "shared-info.pl");
1746610a21fdSJung-uk Kim    my %shared_info = read_eval_file($shared_info_pl);
1747610a21fdSJung-uk Kim    push @{$target{_conf_fname_int}}, $shared_info_pl;
1748610a21fdSJung-uk Kim    my $si = $target{shared_target};
1749610a21fdSJung-uk Kim    while (ref $si ne "HASH") {
1750610a21fdSJung-uk Kim        last if ! defined $si;
1751610a21fdSJung-uk Kim        if (ref $si eq "CODE") {
1752610a21fdSJung-uk Kim            $si = $si->();
1753e71b7053SJung-uk Kim        } else {
1754610a21fdSJung-uk Kim            $si = $shared_info{$si};
1755e71b7053SJung-uk Kim        }
1756e71b7053SJung-uk Kim    }
1757e71b7053SJung-uk Kim
1758610a21fdSJung-uk Kim    # Some of the 'shared_target' values don't have any entries in
1759610a21fdSJung-uk Kim    # %shared_info.  That's perfectly fine, AS LONG AS the build file
1760610a21fdSJung-uk Kim    # template knows how to handle this.  That is currently the case for
1761610a21fdSJung-uk Kim    # Windows and VMS.
1762610a21fdSJung-uk Kim    if (defined $si) {
1763610a21fdSJung-uk Kim        # Just as above, copy certain shared_* attributes to the corresponding
1764610a21fdSJung-uk Kim        # module_ attribute unless the latter is already defined
1765610a21fdSJung-uk Kim        $si->{module_cflags} = $si->{shared_cflag} unless defined $si->{module_cflags};
1766610a21fdSJung-uk Kim        $si->{module_cxxflags} = $si->{shared_cxxflag} unless defined $si->{module_cxxflags};
1767610a21fdSJung-uk Kim        $si->{module_ldflags} = $si->{shared_ldflag} unless defined $si->{module_ldflags};
1768610a21fdSJung-uk Kim        foreach (sort keys %$si) {
1769610a21fdSJung-uk Kim            $target{$_} = defined $target{$_}
1770610a21fdSJung-uk Kim                ? add($si->{$_})->($target{$_})
1771610a21fdSJung-uk Kim                : $si->{$_};
1772610a21fdSJung-uk Kim        }
1773610a21fdSJung-uk Kim    }
1774610a21fdSJung-uk Kim}
1775610a21fdSJung-uk Kim
1776610a21fdSJung-uk Kim# ALL MODIFICATIONS TO %disabled, %config and %target MUST BE DONE FROM HERE ON
1777e71b7053SJung-uk Kim
1778*b077aed3SPierre Pronchery######################################################################
1779*b077aed3SPierre Pronchery# Build up information for skipping certain directories depending on disabled
1780*b077aed3SPierre Pronchery# features, as well as setting up macros for disabled features.
1781*b077aed3SPierre Pronchery
1782*b077aed3SPierre Pronchery# This is a tentative database of directories to skip.  Some entries may not
1783*b077aed3SPierre Pronchery# correspond to anything real, but that's ok, they will simply be ignored.
1784*b077aed3SPierre Pronchery# The actual processing of these entries is done in the build.info lookup
1785*b077aed3SPierre Pronchery# loop further down.
1786*b077aed3SPierre Pronchery#
1787*b077aed3SPierre Pronchery# The key is a Unix formatted path in the source tree, the value is an index
1788*b077aed3SPierre Pronchery# into %disabled_info, so any existing path gets added to a corresponding
1789*b077aed3SPierre Pronchery# 'skipped' entry in there with the list of skipped directories.
1790*b077aed3SPierre Proncherymy %skipdir = ();
179117f01e99SJung-uk Kimmy %disabled_info = ();         # For configdata.pm
179217f01e99SJung-uk Kimforeach my $what (sort keys %disabled) {
1793*b077aed3SPierre Pronchery    # There are deprecated disablables that translate to themselves.
1794*b077aed3SPierre Pronchery    # They cause disabling cascades, but should otherwise not regiter.
1795*b077aed3SPierre Pronchery    next if $deprecated_disablables{$what};
1796*b077aed3SPierre Pronchery    # The generated $disabled{"deprecated-x.y"} entries are special
1797*b077aed3SPierre Pronchery    # and treated properly elsewhere
1798*b077aed3SPierre Pronchery    next if $what =~ m|^deprecated-|;
1799*b077aed3SPierre Pronchery
180017f01e99SJung-uk Kim    $config{options} .= " no-$what";
180117f01e99SJung-uk Kim
1802*b077aed3SPierre Pronchery    if (!grep { $what eq $_ } ( 'buildtest-c++', 'fips', 'threads', 'shared',
1803*b077aed3SPierre Pronchery                                'module', 'pic', 'dynamic-engine', 'makedepend',
1804*b077aed3SPierre Pronchery                                'zlib-dynamic', 'zlib', 'sse2', 'legacy' )) {
180517f01e99SJung-uk Kim        (my $WHAT = uc $what) =~ s|-|_|g;
1806*b077aed3SPierre Pronchery        my $skipdir = $what;
180717f01e99SJung-uk Kim
180817f01e99SJung-uk Kim        # fix-up crypto/directory name(s)
1809*b077aed3SPierre Pronchery        $skipdir = "ripemd" if $what eq "rmd160";
1810*b077aed3SPierre Pronchery        $skipdir = "whrlpool" if $what eq "whirlpool";
181117f01e99SJung-uk Kim
181217f01e99SJung-uk Kim        my $macro = $disabled_info{$what}->{macro} = "OPENSSL_NO_$WHAT";
1813*b077aed3SPierre Pronchery        push @{$config{openssl_feature_defines}}, $macro;
181417f01e99SJung-uk Kim
1815*b077aed3SPierre Pronchery        $skipdir{engines} = $what if $what eq 'engine';
1816*b077aed3SPierre Pronchery        $skipdir{"crypto/$skipdir"} = $what
1817*b077aed3SPierre Pronchery            unless $what eq 'async' || $what eq 'err' || $what eq 'dso';
181817f01e99SJung-uk Kim    }
181917f01e99SJung-uk Kim}
182017f01e99SJung-uk Kim
182117f01e99SJung-uk Kimif ($disabled{"dynamic-engine"}) {
1822*b077aed3SPierre Pronchery    push @{$config{openssl_feature_defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
182317f01e99SJung-uk Kim} else {
1824*b077aed3SPierre Pronchery    push @{$config{openssl_feature_defines}}, "OPENSSL_NO_STATIC_ENGINE";
182517f01e99SJung-uk Kim}
182617f01e99SJung-uk Kim
1827e71b7053SJung-uk Kim# If we use the unified build, collect information from build.info files
1828e71b7053SJung-uk Kimmy %unified_info = ();
1829e71b7053SJung-uk Kim
1830e71b7053SJung-uk Kimmy $buildinfo_debug = defined($ENV{CONFIGURE_DEBUG_BUILDINFO});
1831e71b7053SJung-uk Kimif ($builder eq "unified") {
1832*b077aed3SPierre Pronchery    use Text::Template 1.46;
1833e71b7053SJung-uk Kim
1834e71b7053SJung-uk Kim    sub cleandir {
1835e71b7053SJung-uk Kim        my $base = shift;
1836e71b7053SJung-uk Kim        my $dir = shift;
1837e71b7053SJung-uk Kim        my $relativeto = shift || ".";
1838e71b7053SJung-uk Kim
1839e71b7053SJung-uk Kim        $dir = catdir($base,$dir) unless isabsolute($dir);
1840e71b7053SJung-uk Kim
1841e71b7053SJung-uk Kim        # Make sure the directories we're building in exists
1842e71b7053SJung-uk Kim        mkpath($dir);
1843e71b7053SJung-uk Kim
1844e71b7053SJung-uk Kim        my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
1845e71b7053SJung-uk Kim        #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
1846e71b7053SJung-uk Kim        return $res;
1847e71b7053SJung-uk Kim    }
1848e71b7053SJung-uk Kim
1849e71b7053SJung-uk Kim    sub cleanfile {
1850e71b7053SJung-uk Kim        my $base = shift;
1851e71b7053SJung-uk Kim        my $file = shift;
1852e71b7053SJung-uk Kim        my $relativeto = shift || ".";
1853e71b7053SJung-uk Kim
1854e71b7053SJung-uk Kim        $file = catfile($base,$file) unless isabsolute($file);
1855e71b7053SJung-uk Kim
1856e71b7053SJung-uk Kim        my $d = dirname($file);
1857e71b7053SJung-uk Kim        my $f = basename($file);
1858e71b7053SJung-uk Kim
1859e71b7053SJung-uk Kim        # Make sure the directories we're building in exists
1860e71b7053SJung-uk Kim        mkpath($d);
1861e71b7053SJung-uk Kim
1862e71b7053SJung-uk Kim        my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
1863e71b7053SJung-uk Kim        #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
1864e71b7053SJung-uk Kim        return $res;
1865e71b7053SJung-uk Kim    }
1866e71b7053SJung-uk Kim
1867e71b7053SJung-uk Kim    # Store the name of the template file we will build the build file from
1868e71b7053SJung-uk Kim    # in %config.  This may be useful for the build file itself.
1869e71b7053SJung-uk Kim    my @build_file_template_names =
1870640242a5SJung-uk Kim        ( $builder_platform."-".$config{build_file}.".tmpl",
1871640242a5SJung-uk Kim          $config{build_file}.".tmpl" );
1872e71b7053SJung-uk Kim    my @build_file_templates = ();
1873e71b7053SJung-uk Kim
1874e71b7053SJung-uk Kim    # First, look in the user provided directory, if given
1875e71b7053SJung-uk Kim    if (defined env($local_config_envname)) {
1876e71b7053SJung-uk Kim        @build_file_templates =
1877e71b7053SJung-uk Kim            map {
1878e71b7053SJung-uk Kim                if ($^O eq 'VMS') {
1879e71b7053SJung-uk Kim                    # VMS environment variables are logical names,
1880e71b7053SJung-uk Kim                    # which can be used as is
1881e71b7053SJung-uk Kim                    $local_config_envname . ':' . $_;
1882e71b7053SJung-uk Kim                } else {
1883e71b7053SJung-uk Kim                    catfile(env($local_config_envname), $_);
1884e71b7053SJung-uk Kim                }
1885e71b7053SJung-uk Kim            }
1886e71b7053SJung-uk Kim            @build_file_template_names;
1887e71b7053SJung-uk Kim    }
1888e71b7053SJung-uk Kim    # Then, look in our standard directory
1889e71b7053SJung-uk Kim    push @build_file_templates,
1890e71b7053SJung-uk Kim        ( map { cleanfile($srcdir, catfile("Configurations", $_), $blddir) }
1891e71b7053SJung-uk Kim          @build_file_template_names );
1892e71b7053SJung-uk Kim
1893e71b7053SJung-uk Kim    my $build_file_template;
1894e71b7053SJung-uk Kim    for $_ (@build_file_templates) {
1895e71b7053SJung-uk Kim        $build_file_template = $_;
1896e71b7053SJung-uk Kim        last if -f $build_file_template;
1897e71b7053SJung-uk Kim
1898e71b7053SJung-uk Kim        $build_file_template = undef;
1899e71b7053SJung-uk Kim    }
1900e71b7053SJung-uk Kim    if (!defined $build_file_template) {
1901e71b7053SJung-uk Kim        die "*** Couldn't find any of:\n", join("\n", @build_file_templates), "\n";
1902e71b7053SJung-uk Kim    }
1903e71b7053SJung-uk Kim    $config{build_file_templates}
1904e71b7053SJung-uk Kim      = [ cleanfile($srcdir, catfile("Configurations", "common0.tmpl"),
1905e71b7053SJung-uk Kim                    $blddir),
1906*b077aed3SPierre Pronchery           $build_file_template ];
1907e71b7053SJung-uk Kim
1908*b077aed3SPierre Pronchery    my @build_dirs = ( [ ] );   # current directory
1909e71b7053SJung-uk Kim
1910e71b7053SJung-uk Kim    $config{build_infos} = [ ];
1911e71b7053SJung-uk Kim
1912e71b7053SJung-uk Kim    # We want to detect configdata.pm in the source tree, so we
1913e71b7053SJung-uk Kim    # don't use it if the build tree is different.
1914e71b7053SJung-uk Kim    my $src_configdata = cleanfile($srcdir, "configdata.pm", $blddir);
1915e71b7053SJung-uk Kim
1916*b077aed3SPierre Pronchery    # Any source file that we recognise is placed in this hash table, with
1917*b077aed3SPierre Pronchery    # the list of its intended destinations as value.  When everything has
1918*b077aed3SPierre Pronchery    # been collected, there's a routine that checks that these source files
1919*b077aed3SPierre Pronchery    # exist, or if they are generated, that the generator exists.
1920*b077aed3SPierre Pronchery    my %check_exist = ();
1921*b077aed3SPierre Pronchery    my %check_generate = ();
1922*b077aed3SPierre Pronchery
1923*b077aed3SPierre Pronchery    my %ordinals = ();
1924*b077aed3SPierre Pronchery    while (@build_dirs) {
1925*b077aed3SPierre Pronchery        my @curd = @{shift @build_dirs};
1926*b077aed3SPierre Pronchery        my $sourced = catdir($srcdir, @curd);
1927*b077aed3SPierre Pronchery        my $buildd = catdir($blddir, @curd);
1928*b077aed3SPierre Pronchery
1929*b077aed3SPierre Pronchery        my $unixdir = join('/', @curd);
1930*b077aed3SPierre Pronchery        if (exists $skipdir{$unixdir}) {
1931*b077aed3SPierre Pronchery            my $what = $skipdir{$unixdir};
1932*b077aed3SPierre Pronchery            push @{$disabled_info{$what}->{skipped}}, catdir(@curd);
1933*b077aed3SPierre Pronchery            next;
1934*b077aed3SPierre Pronchery        }
1935*b077aed3SPierre Pronchery
1936*b077aed3SPierre Pronchery        mkpath($buildd);
1937*b077aed3SPierre Pronchery
1938*b077aed3SPierre Pronchery        my $f = 'build.info';
1939*b077aed3SPierre Pronchery        # The basic things we're trying to build
1940*b077aed3SPierre Pronchery        my @programs = ();
1941*b077aed3SPierre Pronchery        my @libraries = ();
1942*b077aed3SPierre Pronchery        my @modules = ();
1943*b077aed3SPierre Pronchery        my @scripts = ();
1944*b077aed3SPierre Pronchery
1945*b077aed3SPierre Pronchery        my %sources = ();
1946*b077aed3SPierre Pronchery        my %shared_sources = ();
1947*b077aed3SPierre Pronchery        my %includes = ();
1948*b077aed3SPierre Pronchery        my %defines = ();
1949*b077aed3SPierre Pronchery        my %depends = ();
1950*b077aed3SPierre Pronchery        my %generate = ();
1951*b077aed3SPierre Pronchery        my %imagedocs = ();
1952*b077aed3SPierre Pronchery        my %htmldocs = ();
1953*b077aed3SPierre Pronchery        my %mandocs = ();
1954*b077aed3SPierre Pronchery
1955*b077aed3SPierre Pronchery        # Support for $variablename in build.info files.
1956*b077aed3SPierre Pronchery        # Embedded perl code is the ultimate master, still.  If its output
1957*b077aed3SPierre Pronchery        # contains a dollar sign, it had better be escaped, or it will be
1958*b077aed3SPierre Pronchery        # taken for a variable name prefix.
1959*b077aed3SPierre Pronchery        my %variables = ();
1960*b077aed3SPierre Pronchery        # Variable name syntax
1961*b077aed3SPierre Pronchery        my $variable_name_re = qr/(?P<VARIABLE>[[:alpha:]][[:alnum:]_]*)/;
1962*b077aed3SPierre Pronchery        # Value modifier syntaxes
1963*b077aed3SPierre Pronchery        my $variable_subst_re = qr/\/(?P<RE>(?:\\\/|.)*?)\/(?P<SUBST>.*?)/;
1964*b077aed3SPierre Pronchery        # Variable reference
1965*b077aed3SPierre Pronchery        my $variable_simple_re = qr/(?<!\\)\$${variable_name_re}/;
1966*b077aed3SPierre Pronchery        my $variable_w_mod_re =
1967*b077aed3SPierre Pronchery            qr/(?<!\\)\$\{${variable_name_re}(?P<MOD>(?:\\\/|.)*?)\}/;
1968*b077aed3SPierre Pronchery        # Tie it all together
1969*b077aed3SPierre Pronchery        my $variable_re = qr/${variable_simple_re}|${variable_w_mod_re}/;
1970*b077aed3SPierre Pronchery
1971*b077aed3SPierre Pronchery        my $expand_variables = sub {
1972*b077aed3SPierre Pronchery            my $value = '';
1973*b077aed3SPierre Pronchery            my $value_rest = shift;
1974*b077aed3SPierre Pronchery
1975*b077aed3SPierre Pronchery            if ($ENV{CONFIGURE_DEBUG_VARIABLE_EXPAND}) {
1976*b077aed3SPierre Pronchery                print STDERR
1977*b077aed3SPierre Pronchery                    "DEBUG[\$expand_variables] Parsed '$value_rest' ...\n"
1978*b077aed3SPierre Pronchery            }
1979*b077aed3SPierre Pronchery
1980*b077aed3SPierre Pronchery            while ($value_rest =~ /${variable_re}/) {
1981*b077aed3SPierre Pronchery                # We must save important regexp values, because the next
1982*b077aed3SPierre Pronchery                # regexp clears them
1983*b077aed3SPierre Pronchery                my $mod = $+{MOD};
1984*b077aed3SPierre Pronchery                my $variable_value = $variables{$+{VARIABLE}};
1985*b077aed3SPierre Pronchery
1986*b077aed3SPierre Pronchery                $value_rest = $';
1987*b077aed3SPierre Pronchery                $value .= $`;
1988*b077aed3SPierre Pronchery
1989*b077aed3SPierre Pronchery                # Process modifier expressions, if present
1990*b077aed3SPierre Pronchery                if (defined $mod) {
1991*b077aed3SPierre Pronchery                    if ($mod =~ /^${variable_subst_re}$/) {
1992*b077aed3SPierre Pronchery                        my $re = $+{RE};
1993*b077aed3SPierre Pronchery                        my $subst = $+{SUBST};
1994*b077aed3SPierre Pronchery
1995*b077aed3SPierre Pronchery                        $variable_value =~ s/\Q$re\E/$subst/g;
1996*b077aed3SPierre Pronchery
1997*b077aed3SPierre Pronchery                        if ($ENV{CONFIGURE_DEBUG_VARIABLE_EXPAND}) {
1998*b077aed3SPierre Pronchery                            print STDERR
1999*b077aed3SPierre Pronchery                                "DEBUG[\$expand_variables] ... and substituted ",
2000*b077aed3SPierre Pronchery                                "'$re' with '$subst'\n";
2001*b077aed3SPierre Pronchery                        }
2002*b077aed3SPierre Pronchery                    }
2003*b077aed3SPierre Pronchery                }
2004*b077aed3SPierre Pronchery
2005*b077aed3SPierre Pronchery                $value .= $variable_value;
2006*b077aed3SPierre Pronchery            }
2007*b077aed3SPierre Pronchery            if ($ENV{CONFIGURE_DEBUG_VARIABLE_EXPAND}) {
2008*b077aed3SPierre Pronchery                print STDERR
2009*b077aed3SPierre Pronchery                    "DEBUG[\$expand_variables] ... into: '$value$value_rest'\n";
2010*b077aed3SPierre Pronchery            }
2011*b077aed3SPierre Pronchery            return $value . $value_rest;
2012*b077aed3SPierre Pronchery        };
2013*b077aed3SPierre Pronchery
2014*b077aed3SPierre Pronchery        # Support for attributes in build.info files
2015*b077aed3SPierre Pronchery        my %attributes = ();
2016*b077aed3SPierre Pronchery        my $handle_attributes = sub {
2017*b077aed3SPierre Pronchery            my $attr_str = shift;
2018*b077aed3SPierre Pronchery            my $ref = shift;
2019*b077aed3SPierre Pronchery            my @goals = @_;
2020*b077aed3SPierre Pronchery
2021*b077aed3SPierre Pronchery            return unless defined $attr_str;
2022*b077aed3SPierre Pronchery
2023*b077aed3SPierre Pronchery            my @a = tokenize($attr_str, qr|\s*,\s*|);
2024*b077aed3SPierre Pronchery            foreach my $a (@a) {
2025*b077aed3SPierre Pronchery                my $ac = 1;
2026*b077aed3SPierre Pronchery                my $ak = $a;
2027*b077aed3SPierre Pronchery                my $av = 1;
2028*b077aed3SPierre Pronchery                if ($a =~ m|^(!)?(.*?)\s* = \s*(.*?)$|x) {
2029*b077aed3SPierre Pronchery                    $ac = ! $1;
2030*b077aed3SPierre Pronchery                    $ak = $2;
2031*b077aed3SPierre Pronchery                    $av = $3;
2032*b077aed3SPierre Pronchery                }
2033*b077aed3SPierre Pronchery                foreach my $g (@goals) {
2034*b077aed3SPierre Pronchery                    if ($ac) {
2035*b077aed3SPierre Pronchery                        $$ref->{$g}->{$ak} = $av;
2036*b077aed3SPierre Pronchery                    } else {
2037*b077aed3SPierre Pronchery                        delete $$ref->{$g}->{$ak};
2038*b077aed3SPierre Pronchery                    }
2039*b077aed3SPierre Pronchery                }
2040*b077aed3SPierre Pronchery            }
2041*b077aed3SPierre Pronchery        };
2042*b077aed3SPierre Pronchery
2043*b077aed3SPierre Pronchery        # Support for pushing values on multiple indexes of a given hash
2044*b077aed3SPierre Pronchery        # array.
2045*b077aed3SPierre Pronchery        my $push_to = sub {
2046*b077aed3SPierre Pronchery            my $valueref = shift;
2047*b077aed3SPierre Pronchery            my $index_str = shift; # May be undef or empty
2048*b077aed3SPierre Pronchery            my $attrref = shift;   # May be undef
2049*b077aed3SPierre Pronchery            my $attr_str = shift;
2050*b077aed3SPierre Pronchery            my @values = @_;
2051*b077aed3SPierre Pronchery
2052*b077aed3SPierre Pronchery            if (defined $index_str) {
2053*b077aed3SPierre Pronchery                my @indexes = ( '' );
2054*b077aed3SPierre Pronchery                if ($index_str !~ m|^\s*$|) {
2055*b077aed3SPierre Pronchery                    @indexes = tokenize($index_str);
2056*b077aed3SPierre Pronchery                }
2057*b077aed3SPierre Pronchery                foreach (@indexes) {
2058*b077aed3SPierre Pronchery                    push @{$valueref->{$_}}, @values;
2059*b077aed3SPierre Pronchery                    if (defined $attrref) {
2060*b077aed3SPierre Pronchery                        $handle_attributes->($attr_str, \$$attrref->{$_},
2061*b077aed3SPierre Pronchery                                             @values);
2062*b077aed3SPierre Pronchery                    }
2063*b077aed3SPierre Pronchery                }
2064*b077aed3SPierre Pronchery            } else {
2065*b077aed3SPierre Pronchery                push @$valueref, @values;
2066*b077aed3SPierre Pronchery                $handle_attributes->($attr_str, $attrref, @values)
2067*b077aed3SPierre Pronchery                    if defined $attrref;
2068*b077aed3SPierre Pronchery            }
2069*b077aed3SPierre Pronchery        };
2070*b077aed3SPierre Pronchery
2071*b077aed3SPierre Pronchery        if ($buildinfo_debug) {
2072*b077aed3SPierre Pronchery            print STDERR "DEBUG: Reading ",catfile($sourced, $f),"\n";
2073*b077aed3SPierre Pronchery        }
2074e71b7053SJung-uk Kim        push @{$config{build_infos}}, catfile(abs2rel($sourced, $blddir), $f);
2075e71b7053SJung-uk Kim        my $template =
2076e71b7053SJung-uk Kim            Text::Template->new(TYPE => 'FILE',
2077e71b7053SJung-uk Kim                                SOURCE => catfile($sourced, $f),
2078e71b7053SJung-uk Kim                                PREPEND => qq{use lib "$FindBin::Bin/util/perl";});
2079e71b7053SJung-uk Kim        die "Something went wrong with $sourced/$f: $!\n" unless $template;
2080e71b7053SJung-uk Kim        my @text =
2081e71b7053SJung-uk Kim            split /^/m,
2082e71b7053SJung-uk Kim            $template->fill_in(HASH => { config => \%config,
2083e71b7053SJung-uk Kim                                         target => \%target,
2084e71b7053SJung-uk Kim                                         disabled => \%disabled,
2085e71b7053SJung-uk Kim                                         withargs => \%withargs,
2086e71b7053SJung-uk Kim                                         builddir => abs2rel($buildd, $blddir),
2087e71b7053SJung-uk Kim                                         sourcedir => abs2rel($sourced, $blddir),
2088e71b7053SJung-uk Kim                                         buildtop => abs2rel($blddir, $blddir),
2089e71b7053SJung-uk Kim                                         sourcetop => abs2rel($srcdir, $blddir) },
2090e71b7053SJung-uk Kim                               DELIMITERS => [ "{-", "-}" ]);
2091e71b7053SJung-uk Kim
2092e71b7053SJung-uk Kim        # The top item of this stack has the following values
2093e71b7053SJung-uk Kim        # -2 positive already run and we found ELSE (following ELSIF should fail)
2094e71b7053SJung-uk Kim        # -1 positive already run (skip until ENDIF)
2095e71b7053SJung-uk Kim        # 0 negatives so far (if we're at a condition, check it)
2096e71b7053SJung-uk Kim        # 1 last was positive (don't skip lines until next ELSE, ELSIF or ENDIF)
2097e71b7053SJung-uk Kim        # 2 positive ELSE (following ELSIF should fail)
2098e71b7053SJung-uk Kim        my @skip = ();
2099*b077aed3SPierre Pronchery
2100*b077aed3SPierre Pronchery        # A few useful generic regexps
2101*b077aed3SPierre Pronchery        my $index_re = qr/\[\s*(?P<INDEX>(?:\\.|.)*?)\s*\]/;
2102*b077aed3SPierre Pronchery        my $cond_re = qr/\[\s*(?P<COND>(?:\\.|.)*?)\s*\]/;
2103*b077aed3SPierre Pronchery        my $attribs_re = qr/(?:\{\s*(?P<ATTRIBS>(?:\\.|.)*?)\s*\})?/;
2104*b077aed3SPierre Pronchery        my $value_re = qr/(?P<VALUE>.*?)/;
2105e71b7053SJung-uk Kim        collect_information(
2106e71b7053SJung-uk Kim            collect_from_array([ @text ],
2107e71b7053SJung-uk Kim                               qr/\\$/ => sub { my $l1 = shift; my $l2 = shift;
2108e71b7053SJung-uk Kim                                                $l1 =~ s/\\$//; $l1.$l2 }),
2109e71b7053SJung-uk Kim            # Info we're looking for
2110*b077aed3SPierre Pronchery            qr/^\s* IF ${cond_re} \s*$/x
2111e71b7053SJung-uk Kim            => sub {
2112e71b7053SJung-uk Kim                if (! @skip || $skip[$#skip] > 0) {
2113*b077aed3SPierre Pronchery                    push @skip, !! $expand_variables->($+{COND});
2114e71b7053SJung-uk Kim                } else {
2115e71b7053SJung-uk Kim                    push @skip, -1;
2116e71b7053SJung-uk Kim                }
2117e71b7053SJung-uk Kim            },
2118*b077aed3SPierre Pronchery            qr/^\s* ELSIF ${cond_re} \s*$/x
2119e71b7053SJung-uk Kim            => sub { die "ELSIF out of scope" if ! @skip;
2120e71b7053SJung-uk Kim                     die "ELSIF following ELSE" if abs($skip[$#skip]) == 2;
2121e71b7053SJung-uk Kim                     $skip[$#skip] = -1 if $skip[$#skip] != 0;
2122*b077aed3SPierre Pronchery                     $skip[$#skip] = !! $expand_variables->($+{COND})
2123e71b7053SJung-uk Kim                         if $skip[$#skip] == 0; },
2124*b077aed3SPierre Pronchery            qr/^\s* ELSE \s*$/x
2125e71b7053SJung-uk Kim            => sub { die "ELSE out of scope" if ! @skip;
2126e71b7053SJung-uk Kim                     $skip[$#skip] = -2 if $skip[$#skip] != 0;
2127e71b7053SJung-uk Kim                     $skip[$#skip] = 2 if $skip[$#skip] == 0; },
2128*b077aed3SPierre Pronchery            qr/^\s* ENDIF \s*$/x
2129e71b7053SJung-uk Kim            => sub { die "ENDIF out of scope" if ! @skip;
2130e71b7053SJung-uk Kim                     pop @skip; },
2131*b077aed3SPierre Pronchery            qr/^\s* ${variable_re} \s* = \s* ${value_re} \s* $/x
2132e71b7053SJung-uk Kim            => sub {
2133e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
2134*b077aed3SPierre Pronchery                    $variables{$+{VARIABLE}} = $expand_variables->($+{VALUE});
2135e71b7053SJung-uk Kim                }
2136e71b7053SJung-uk Kim            },
2137*b077aed3SPierre Pronchery            qr/^\s* SUBDIRS \s* = \s* ${value_re} \s* $/x
2138e71b7053SJung-uk Kim            => sub {
2139e71b7053SJung-uk Kim                if (!@skip || $skip[$#skip] > 0) {
2140*b077aed3SPierre Pronchery                    foreach (tokenize($expand_variables->($+{VALUE}))) {
2141*b077aed3SPierre Pronchery                        push @build_dirs, [ @curd, splitdir($_, 1) ];
2142*b077aed3SPierre Pronchery                    }
2143e71b7053SJung-uk Kim                }
2144e71b7053SJung-uk Kim            },
2145*b077aed3SPierre Pronchery            qr/^\s* PROGRAMS ${attribs_re} \s* =  \s* ${value_re} \s* $/x
2146*b077aed3SPierre Pronchery            => sub { $push_to->(\@programs, undef,
2147*b077aed3SPierre Pronchery                                \$attributes{programs}, $+{ATTRIBS},
2148*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2149*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2150*b077aed3SPierre Pronchery            qr/^\s* LIBS ${attribs_re} \s* =  \s* ${value_re} \s* $/x
2151*b077aed3SPierre Pronchery            => sub { $push_to->(\@libraries, undef,
2152*b077aed3SPierre Pronchery                                \$attributes{libraries}, $+{ATTRIBS},
2153*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2154*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2155*b077aed3SPierre Pronchery            qr/^\s* MODULES ${attribs_re} \s* =  \s* ${value_re} \s* $/x
2156*b077aed3SPierre Pronchery            => sub { $push_to->(\@modules, undef,
2157*b077aed3SPierre Pronchery                                \$attributes{modules}, $+{ATTRIBS},
2158*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2159*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2160*b077aed3SPierre Pronchery            qr/^\s* SCRIPTS ${attribs_re} \s* = \s* ${value_re} \s* $/x
2161*b077aed3SPierre Pronchery            => sub { $push_to->(\@scripts, undef,
2162*b077aed3SPierre Pronchery                                \$attributes{scripts}, $+{ATTRIBS},
2163*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2164*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2165*b077aed3SPierre Pronchery            qr/^\s* IMAGEDOCS ${index_re} \s* = \s* ${value_re} \s* $/x
2166*b077aed3SPierre Pronchery            => sub { $push_to->(\%imagedocs, $expand_variables->($+{INDEX}),
2167*b077aed3SPierre Pronchery                                undef, undef,
2168*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2169*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2170*b077aed3SPierre Pronchery            qr/^\s* HTMLDOCS ${index_re} \s* = \s* ${value_re} \s* $/x
2171*b077aed3SPierre Pronchery            => sub { $push_to->(\%htmldocs, $expand_variables->($+{INDEX}),
2172*b077aed3SPierre Pronchery                                undef, undef,
2173*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2174*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2175*b077aed3SPierre Pronchery            qr/^\s* MANDOCS ${index_re} \s* = \s* ${value_re} \s* $/x
2176*b077aed3SPierre Pronchery            => sub { $push_to->(\%mandocs, $expand_variables->($+{INDEX}),
2177*b077aed3SPierre Pronchery                                undef, undef,
2178*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2179*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2180*b077aed3SPierre Pronchery            qr/^\s* SOURCE ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
2181*b077aed3SPierre Pronchery            => sub { $push_to->(\%sources, $expand_variables->($+{INDEX}),
2182*b077aed3SPierre Pronchery                                \$attributes{sources}, $+{ATTRIBS},
2183*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2184*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2185*b077aed3SPierre Pronchery            qr/^\s* SHARED_SOURCE ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
2186*b077aed3SPierre Pronchery            => sub { $push_to->(\%shared_sources, $expand_variables->($+{INDEX}),
2187*b077aed3SPierre Pronchery                                \$attributes{sources}, $+{ATTRIBS},
2188*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2189*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2190*b077aed3SPierre Pronchery            qr/^\s* INCLUDE ${index_re} \s* = \s* ${value_re} \s* $/x
2191*b077aed3SPierre Pronchery            => sub { $push_to->(\%includes, $expand_variables->($+{INDEX}),
2192*b077aed3SPierre Pronchery                                undef, undef,
2193*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2194*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2195*b077aed3SPierre Pronchery            qr/^\s* DEFINE ${index_re} \s* = \s* ${value_re} \s* $/x
2196*b077aed3SPierre Pronchery            => sub { $push_to->(\%defines, $expand_variables->($+{INDEX}),
2197*b077aed3SPierre Pronchery                                undef, undef,
2198*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2199*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2200*b077aed3SPierre Pronchery            qr/^\s* DEPEND ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
2201*b077aed3SPierre Pronchery            => sub { $push_to->(\%depends, $expand_variables->($+{INDEX}),
2202*b077aed3SPierre Pronchery                                \$attributes{depends}, $+{ATTRIBS},
2203*b077aed3SPierre Pronchery                                tokenize($expand_variables->($+{VALUE})))
2204*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2205*b077aed3SPierre Pronchery            qr/^\s* GENERATE ${index_re} ${attribs_re} \s* = \s* ${value_re} \s* $/x
2206*b077aed3SPierre Pronchery            => sub { $push_to->(\%generate, $expand_variables->($+{INDEX}),
2207*b077aed3SPierre Pronchery                                \$attributes{generate}, $+{ATTRIBS},
2208*b077aed3SPierre Pronchery                                $expand_variables->($+{VALUE}))
2209*b077aed3SPierre Pronchery                         if !@skip || $skip[$#skip] > 0; },
2210*b077aed3SPierre Pronchery            qr/^\s* (?:\#.*)? $/x => sub { },
2211e71b7053SJung-uk Kim            "OTHERWISE" => sub { die "Something wrong with this line:\n$_\nat $sourced/$f" },
2212e71b7053SJung-uk Kim            "BEFORE" => sub {
2213e71b7053SJung-uk Kim                if ($buildinfo_debug) {
2214e71b7053SJung-uk Kim                    print STDERR "DEBUG: Parsing ",join(" ", @_),"\n";
2215e71b7053SJung-uk Kim                    print STDERR "DEBUG: ... before parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
2216e71b7053SJung-uk Kim                }
2217e71b7053SJung-uk Kim            },
2218e71b7053SJung-uk Kim            "AFTER" => sub {
2219e71b7053SJung-uk Kim                if ($buildinfo_debug) {
2220e71b7053SJung-uk Kim                    print STDERR "DEBUG: .... after parsing, skip stack is ",join(" ", map { int($_) } @skip),"\n";
2221e71b7053SJung-uk Kim                }
2222e71b7053SJung-uk Kim            },
2223e71b7053SJung-uk Kim            );
2224e71b7053SJung-uk Kim        die "runaway IF?" if (@skip);
2225e71b7053SJung-uk Kim
2226*b077aed3SPierre Pronchery        if (grep { defined $attributes{modules}->{$_}->{engine} } keys %attributes
2227*b077aed3SPierre Pronchery                and !$config{dynamic_engines}) {
2228*b077aed3SPierre Pronchery            die <<"EOF"
2229e71b7053SJung-uk KimENGINES can only be used if configured with 'dynamic-engine'.
2230e71b7053SJung-uk KimThis is usually a fault in a build.info file.
2231e71b7053SJung-uk KimEOF
2232e71b7053SJung-uk Kim        }
2233e71b7053SJung-uk Kim
2234*b077aed3SPierre Pronchery        {
2235*b077aed3SPierre Pronchery            my %infos = ( programs  => [ @programs  ],
2236*b077aed3SPierre Pronchery                          libraries => [ @libraries ],
2237*b077aed3SPierre Pronchery                          modules   => [ @modules   ],
2238*b077aed3SPierre Pronchery                          scripts   => [ @scripts   ] );
2239*b077aed3SPierre Pronchery            foreach my $k (keys %infos) {
2240*b077aed3SPierre Pronchery                foreach (@{$infos{$k}}) {
2241*b077aed3SPierre Pronchery                    my $item = cleanfile($buildd, $_, $blddir);
2242*b077aed3SPierre Pronchery                    $unified_info{$k}->{$item} = 1;
2243e71b7053SJung-uk Kim
2244*b077aed3SPierre Pronchery                    # Fix up associated attributes
2245*b077aed3SPierre Pronchery                    $unified_info{attributes}->{$k}->{$item} =
2246*b077aed3SPierre Pronchery                        $attributes{$k}->{$_}
2247*b077aed3SPierre Pronchery                        if defined $attributes{$k}->{$_};
2248e71b7053SJung-uk Kim                }
2249e71b7053SJung-uk Kim            }
2250e71b7053SJung-uk Kim        }
2251e71b7053SJung-uk Kim
2252e71b7053SJung-uk Kim        # Check that we haven't defined any library as both shared and
2253e71b7053SJung-uk Kim        # explicitly static.  That is forbidden.
2254e71b7053SJung-uk Kim        my @doubles = ();
2255e71b7053SJung-uk Kim        foreach (grep /\.a$/, keys %{$unified_info{libraries}}) {
2256e71b7053SJung-uk Kim            (my $l = $_) =~ s/\.a$//;
2257*b077aed3SPierre Pronchery            push @doubles, $l if defined $unified_info{libraries}->{$l};
2258e71b7053SJung-uk Kim        }
2259e71b7053SJung-uk Kim        die "these libraries are both explicitly static and shared:\n  ",
2260e71b7053SJung-uk Kim            join(" ", @doubles), "\n"
2261e71b7053SJung-uk Kim            if @doubles;
2262e71b7053SJung-uk Kim
2263e71b7053SJung-uk Kim        foreach (keys %sources) {
2264e71b7053SJung-uk Kim            my $dest = $_;
2265e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2266e71b7053SJung-uk Kim            foreach (@{$sources{$dest}}) {
2267e71b7053SJung-uk Kim                my $s = cleanfile($sourced, $_, $blddir);
2268e71b7053SJung-uk Kim
2269*b077aed3SPierre Pronchery                # If it's generated or we simply don't find it in the source
2270*b077aed3SPierre Pronchery                # tree, we assume it's in the build tree.
2271*b077aed3SPierre Pronchery                if ($s eq $src_configdata || $generate{$_} || ! -f $s) {
2272e71b7053SJung-uk Kim                    $s = cleanfile($buildd, $_, $blddir);
2273e71b7053SJung-uk Kim                }
2274*b077aed3SPierre Pronchery                my $o = $_;
2275e71b7053SJung-uk Kim                # We recognise C++, C and asm files
2276e71b7053SJung-uk Kim                if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2277*b077aed3SPierre Pronchery                    push @{$check_exist{$s}}, $ddest;
2278e71b7053SJung-uk Kim                    $o =~ s/\.[csS]$/.o/; # C and assembler
2279e71b7053SJung-uk Kim                    $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2280e71b7053SJung-uk Kim                    $o = cleanfile($buildd, $o, $blddir);
2281*b077aed3SPierre Pronchery                    $unified_info{sources}->{$ddest}->{$o} = -1;
2282*b077aed3SPierre Pronchery                    $unified_info{sources}->{$o}->{$s} = -1;
2283e71b7053SJung-uk Kim                } elsif ($s =~ /\.rc$/) {
2284e71b7053SJung-uk Kim                    # We also recognise resource files
2285*b077aed3SPierre Pronchery                    push @{$check_exist{$s}}, $ddest;
2286e71b7053SJung-uk Kim                    $o =~ s/\.rc$/.res/; # Resource configuration
2287*b077aed3SPierre Pronchery                    $o = cleanfile($buildd, $o, $blddir);
2288*b077aed3SPierre Pronchery                    $unified_info{sources}->{$ddest}->{$o} = -1;
2289*b077aed3SPierre Pronchery                    $unified_info{sources}->{$o}->{$s} = -1;
2290e71b7053SJung-uk Kim                } else {
2291*b077aed3SPierre Pronchery                    push @{$check_exist{$s}}, $ddest;
2292e71b7053SJung-uk Kim                    $unified_info{sources}->{$ddest}->{$s} = 1;
2293e71b7053SJung-uk Kim                }
2294*b077aed3SPierre Pronchery                # Fix up associated attributes
2295*b077aed3SPierre Pronchery                if ($o ne $_) {
2296*b077aed3SPierre Pronchery                    $unified_info{attributes}->{sources}->{$ddest}->{$o} =
2297*b077aed3SPierre Pronchery                        $unified_info{attributes}->{sources}->{$o}->{$s} =
2298*b077aed3SPierre Pronchery                        $attributes{sources}->{$dest}->{$_}
2299*b077aed3SPierre Pronchery                        if defined $attributes{sources}->{$dest}->{$_};
2300*b077aed3SPierre Pronchery                } else {
2301*b077aed3SPierre Pronchery                    $unified_info{attributes}->{sources}->{$ddest}->{$s} =
2302*b077aed3SPierre Pronchery                        $attributes{sources}->{$dest}->{$_}
2303*b077aed3SPierre Pronchery                        if defined $attributes{sources}->{$dest}->{$_};
2304*b077aed3SPierre Pronchery                }
2305e71b7053SJung-uk Kim            }
2306e71b7053SJung-uk Kim        }
2307e71b7053SJung-uk Kim
2308e71b7053SJung-uk Kim        foreach (keys %shared_sources) {
2309e71b7053SJung-uk Kim            my $dest = $_;
2310e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2311e71b7053SJung-uk Kim            foreach (@{$shared_sources{$dest}}) {
2312e71b7053SJung-uk Kim                my $s = cleanfile($sourced, $_, $blddir);
2313e71b7053SJung-uk Kim
2314*b077aed3SPierre Pronchery                # If it's generated or we simply don't find it in the source
2315*b077aed3SPierre Pronchery                # tree, we assume it's in the build tree.
2316*b077aed3SPierre Pronchery                if ($s eq $src_configdata || $generate{$_} || ! -f $s) {
2317e71b7053SJung-uk Kim                    $s = cleanfile($buildd, $_, $blddir);
2318e71b7053SJung-uk Kim                }
2319e71b7053SJung-uk Kim
2320*b077aed3SPierre Pronchery                my $o = $_;
2321e71b7053SJung-uk Kim                if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2322e71b7053SJung-uk Kim                    # We recognise C++, C and asm files
2323*b077aed3SPierre Pronchery                    push @{$check_exist{$s}}, $ddest;
2324e71b7053SJung-uk Kim                    $o =~ s/\.[csS]$/.o/; # C and assembler
2325e71b7053SJung-uk Kim                    $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2326e71b7053SJung-uk Kim                    $o = cleanfile($buildd, $o, $blddir);
2327*b077aed3SPierre Pronchery                    $unified_info{shared_sources}->{$ddest}->{$o} = -1;
2328*b077aed3SPierre Pronchery                    $unified_info{sources}->{$o}->{$s} = -1;
2329e71b7053SJung-uk Kim                } elsif ($s =~ /\.rc$/) {
2330e71b7053SJung-uk Kim                    # We also recognise resource files
2331*b077aed3SPierre Pronchery                    push @{$check_exist{$s}}, $ddest;
2332e71b7053SJung-uk Kim                    $o =~ s/\.rc$/.res/; # Resource configuration
2333*b077aed3SPierre Pronchery                    $o = cleanfile($buildd, $o, $blddir);
2334*b077aed3SPierre Pronchery                    $unified_info{shared_sources}->{$ddest}->{$o} = -1;
2335*b077aed3SPierre Pronchery                    $unified_info{sources}->{$o}->{$s} = -1;
2336*b077aed3SPierre Pronchery                } elsif ($s =~ /\.ld$/) {
2337*b077aed3SPierre Pronchery                    # We also recognise linker scripts (or corresponding)
2338e71b7053SJung-uk Kim                    # We know they are generated files
2339*b077aed3SPierre Pronchery                    push @{$check_exist{$s}}, $ddest;
2340*b077aed3SPierre Pronchery                    $o = cleanfile($buildd, $_, $blddir);
2341*b077aed3SPierre Pronchery                    $unified_info{shared_sources}->{$ddest}->{$o} = 1;
2342e71b7053SJung-uk Kim                } else {
2343e71b7053SJung-uk Kim                    die "unrecognised source file type for shared library: $s\n";
2344e71b7053SJung-uk Kim                }
2345*b077aed3SPierre Pronchery                # Fix up associated attributes
2346*b077aed3SPierre Pronchery                if ($o ne $_) {
2347*b077aed3SPierre Pronchery                    $unified_info{attributes}->{shared_sources}->{$ddest}->{$o} =
2348*b077aed3SPierre Pronchery                        $unified_info{attributes}->{sources}->{$o}->{$s} =
2349*b077aed3SPierre Pronchery                        $attributes{sources}->{$dest}->{$_}
2350*b077aed3SPierre Pronchery                        if defined $attributes{sources}->{$dest}->{$_};
2351*b077aed3SPierre Pronchery                } else {
2352*b077aed3SPierre Pronchery                    $unified_info{attributes}->{shared_sources}->{$ddest}->{$o} =
2353*b077aed3SPierre Pronchery                        $attributes{sources}->{$dest}->{$_}
2354*b077aed3SPierre Pronchery                        if defined $attributes{sources}->{$dest}->{$_};
2355*b077aed3SPierre Pronchery                }
2356e71b7053SJung-uk Kim            }
2357e71b7053SJung-uk Kim        }
2358e71b7053SJung-uk Kim
2359e71b7053SJung-uk Kim        foreach (keys %generate) {
2360e71b7053SJung-uk Kim            my $dest = $_;
2361e71b7053SJung-uk Kim            my $ddest = cleanfile($buildd, $_, $blddir);
2362e71b7053SJung-uk Kim            die "more than one generator for $dest: "
2363e71b7053SJung-uk Kim                ,join(" ", @{$generate{$_}}),"\n"
2364e71b7053SJung-uk Kim                if scalar @{$generate{$_}} > 1;
2365e71b7053SJung-uk Kim            my @generator = split /\s+/, $generate{$dest}->[0];
2366*b077aed3SPierre Pronchery            my $gen = $generator[0];
2367*b077aed3SPierre Pronchery            $generator[0] = cleanfile($sourced, $gen, $blddir);
2368*b077aed3SPierre Pronchery
2369*b077aed3SPierre Pronchery            # If the generator is itself generated, it's in the build tree
2370*b077aed3SPierre Pronchery            if ($generate{$gen} || ! -f $generator[0]) {
2371*b077aed3SPierre Pronchery                $generator[0] = cleanfile($buildd, $gen, $blddir);
2372*b077aed3SPierre Pronchery            }
2373*b077aed3SPierre Pronchery            $check_generate{$ddest}->{$generator[0]}++;
2374*b077aed3SPierre Pronchery
2375e71b7053SJung-uk Kim            $unified_info{generate}->{$ddest} = [ @generator ];
2376*b077aed3SPierre Pronchery            # Fix up associated attributes
2377*b077aed3SPierre Pronchery            $unified_info{attributes}->{generate}->{$ddest} =
2378*b077aed3SPierre Pronchery                $attributes{generate}->{$dest}->{$gen}
2379*b077aed3SPierre Pronchery                if defined $attributes{generate}->{$dest}->{$gen};
2380e71b7053SJung-uk Kim        }
2381e71b7053SJung-uk Kim
2382e71b7053SJung-uk Kim        foreach (keys %depends) {
2383e71b7053SJung-uk Kim            my $dest = $_;
2384*b077aed3SPierre Pronchery            my $ddest = $dest;
2385*b077aed3SPierre Pronchery
2386*b077aed3SPierre Pronchery            if ($dest =~ /^\|(.*)\|$/) {
2387*b077aed3SPierre Pronchery                # Collect the raw target
2388*b077aed3SPierre Pronchery                $unified_info{targets}->{$1} = 1;
2389*b077aed3SPierre Pronchery                $ddest = $1;
2390*b077aed3SPierre Pronchery            } elsif ($dest eq '') {
2391*b077aed3SPierre Pronchery                $ddest = '';
2392*b077aed3SPierre Pronchery            } else {
2393*b077aed3SPierre Pronchery                $ddest = cleanfile($sourced, $_, $blddir);
2394e71b7053SJung-uk Kim
2395e71b7053SJung-uk Kim                # If the destination doesn't exist in source, it can only be
2396e71b7053SJung-uk Kim                # a generated file in the build tree.
2397*b077aed3SPierre Pronchery                if ($ddest eq $src_configdata || ! -f $ddest) {
2398e71b7053SJung-uk Kim                    $ddest = cleanfile($buildd, $_, $blddir);
2399e71b7053SJung-uk Kim                }
2400e71b7053SJung-uk Kim            }
2401e71b7053SJung-uk Kim            foreach (@{$depends{$dest}}) {
2402e71b7053SJung-uk Kim                my $d = cleanfile($sourced, $_, $blddir);
2403*b077aed3SPierre Pronchery                my $d2 = cleanfile($buildd, $_, $blddir);
2404e71b7053SJung-uk Kim
2405e71b7053SJung-uk Kim                # If we know it's generated, or assume it is because we can't
2406e71b7053SJung-uk Kim                # find it in the source tree, we set file we depend on to be
2407*b077aed3SPierre Pronchery                # in the build tree rather than the source tree.
2408e71b7053SJung-uk Kim                if ($d eq $src_configdata
2409*b077aed3SPierre Pronchery                    || (grep { $d2 eq $_ }
2410*b077aed3SPierre Pronchery                        keys %{$unified_info{generate}})
2411*b077aed3SPierre Pronchery                    || ! -f $d) {
2412*b077aed3SPierre Pronchery                    $d = $d2;
2413e71b7053SJung-uk Kim                }
2414e71b7053SJung-uk Kim                $unified_info{depends}->{$ddest}->{$d} = 1;
2415*b077aed3SPierre Pronchery
2416*b077aed3SPierre Pronchery                # Fix up associated attributes
2417*b077aed3SPierre Pronchery                $unified_info{attributes}->{depends}->{$ddest}->{$d} =
2418*b077aed3SPierre Pronchery                    $attributes{depends}->{$dest}->{$_}
2419*b077aed3SPierre Pronchery                    if defined $attributes{depends}->{$dest}->{$_};
2420e71b7053SJung-uk Kim            }
2421e71b7053SJung-uk Kim        }
2422e71b7053SJung-uk Kim
2423e71b7053SJung-uk Kim        foreach (keys %includes) {
2424e71b7053SJung-uk Kim            my $dest = $_;
2425e71b7053SJung-uk Kim            my $ddest = cleanfile($sourced, $_, $blddir);
2426e71b7053SJung-uk Kim
2427e71b7053SJung-uk Kim            # If the destination doesn't exist in source, it can only be
2428e71b7053SJung-uk Kim            # a generated file in the build tree.
2429e71b7053SJung-uk Kim            if ($ddest eq $src_configdata || ! -f $ddest) {
2430e71b7053SJung-uk Kim                $ddest = cleanfile($buildd, $_, $blddir);
2431e71b7053SJung-uk Kim            }
2432e71b7053SJung-uk Kim            foreach (@{$includes{$dest}}) {
2433e71b7053SJung-uk Kim                my $is = cleandir($sourced, $_, $blddir);
2434e71b7053SJung-uk Kim                my $ib = cleandir($buildd, $_, $blddir);
2435e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$ddest}->{source}}, $is
2436e71b7053SJung-uk Kim                    unless grep { $_ eq $is } @{$unified_info{includes}->{$ddest}->{source}};
2437e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$ddest}->{build}}, $ib
2438e71b7053SJung-uk Kim                    unless grep { $_ eq $ib } @{$unified_info{includes}->{$ddest}->{build}};
2439e71b7053SJung-uk Kim            }
2440e71b7053SJung-uk Kim        }
2441*b077aed3SPierre Pronchery
2442*b077aed3SPierre Pronchery        foreach my $dest (keys %defines) {
2443*b077aed3SPierre Pronchery            my $ddest;
2444*b077aed3SPierre Pronchery
2445*b077aed3SPierre Pronchery            if ($dest ne "") {
2446*b077aed3SPierre Pronchery                $ddest = cleanfile($sourced, $dest, $blddir);
2447*b077aed3SPierre Pronchery
2448*b077aed3SPierre Pronchery                # If the destination doesn't exist in source, it can only
2449*b077aed3SPierre Pronchery                # be a generated file in the build tree.
2450*b077aed3SPierre Pronchery                if (! -f $ddest) {
2451*b077aed3SPierre Pronchery                    $ddest = cleanfile($buildd, $dest, $blddir);
2452*b077aed3SPierre Pronchery                }
2453*b077aed3SPierre Pronchery            }
2454*b077aed3SPierre Pronchery            foreach my $v (@{$defines{$dest}}) {
2455*b077aed3SPierre Pronchery                $v =~ m|^([^=]*)(=.*)?$|;
2456*b077aed3SPierre Pronchery                die "0 length macro name not permitted\n" if $1 eq "";
2457*b077aed3SPierre Pronchery                if ($dest ne "") {
2458*b077aed3SPierre Pronchery                    die "$1 defined more than once\n"
2459*b077aed3SPierre Pronchery                        if defined $unified_info{defines}->{$ddest}->{$1};
2460*b077aed3SPierre Pronchery                    $unified_info{defines}->{$ddest}->{$1} = $2;
2461*b077aed3SPierre Pronchery                } else {
2462*b077aed3SPierre Pronchery                    die "$1 defined more than once\n"
2463*b077aed3SPierre Pronchery                        if grep { $v eq $_ } @{$config{defines}};
2464*b077aed3SPierre Pronchery                    push @{$config{defines}}, $v;
2465*b077aed3SPierre Pronchery                }
2466*b077aed3SPierre Pronchery            }
2467*b077aed3SPierre Pronchery        }
2468*b077aed3SPierre Pronchery
2469*b077aed3SPierre Pronchery        foreach my $section (keys %imagedocs) {
2470*b077aed3SPierre Pronchery            foreach (@{$imagedocs{$section}}) {
2471*b077aed3SPierre Pronchery                my $imagedocs = cleanfile($buildd, $_, $blddir);
2472*b077aed3SPierre Pronchery                $unified_info{imagedocs}->{$section}->{$imagedocs} = 1;
2473*b077aed3SPierre Pronchery            }
2474*b077aed3SPierre Pronchery        }
2475*b077aed3SPierre Pronchery
2476*b077aed3SPierre Pronchery        foreach my $section (keys %htmldocs) {
2477*b077aed3SPierre Pronchery            foreach (@{$htmldocs{$section}}) {
2478*b077aed3SPierre Pronchery                my $htmldocs = cleanfile($buildd, $_, $blddir);
2479*b077aed3SPierre Pronchery                $unified_info{htmldocs}->{$section}->{$htmldocs} = 1;
2480*b077aed3SPierre Pronchery            }
2481*b077aed3SPierre Pronchery        }
2482*b077aed3SPierre Pronchery
2483*b077aed3SPierre Pronchery        foreach my $section (keys %mandocs) {
2484*b077aed3SPierre Pronchery            foreach (@{$mandocs{$section}}) {
2485*b077aed3SPierre Pronchery                my $mandocs = cleanfile($buildd, $_, $blddir);
2486*b077aed3SPierre Pronchery                $unified_info{mandocs}->{$section}->{$mandocs} = 1;
2487*b077aed3SPierre Pronchery            }
2488*b077aed3SPierre Pronchery        }
2489e71b7053SJung-uk Kim    }
2490e71b7053SJung-uk Kim
2491e71b7053SJung-uk Kim    my $ordinals_text = join(', ', sort keys %ordinals);
2492e71b7053SJung-uk Kim    warn <<"EOF" if $ordinals_text;
2493e71b7053SJung-uk Kim
2494e71b7053SJung-uk KimWARNING: ORDINALS were specified for $ordinals_text
2495e71b7053SJung-uk KimThey are ignored and should be replaced with a combination of GENERATE,
2496e71b7053SJung-uk KimDEPEND and SHARED_SOURCE.
2497e71b7053SJung-uk KimEOF
2498e71b7053SJung-uk Kim
2499*b077aed3SPierre Pronchery    # Check that each generated file is only generated once
2500*b077aed3SPierre Pronchery    my $ambiguous_generation = 0;
2501*b077aed3SPierre Pronchery    foreach (sort keys %check_generate) {
2502*b077aed3SPierre Pronchery        my @generators = sort keys %{$check_generate{$_}};
2503*b077aed3SPierre Pronchery        my $generators_txt = join(', ', @generators);
2504*b077aed3SPierre Pronchery        if (scalar @generators > 1) {
2505*b077aed3SPierre Pronchery            warn "$_ is GENERATEd by more than one generator ($generators_txt)\n";
2506*b077aed3SPierre Pronchery            $ambiguous_generation++;
2507*b077aed3SPierre Pronchery        }
2508*b077aed3SPierre Pronchery        if ($check_generate{$_}->{$generators[0]} > 1) {
2509*b077aed3SPierre Pronchery            warn "INFO: $_ has more than one GENERATE declaration (same generator)\n"
2510*b077aed3SPierre Pronchery        }
2511*b077aed3SPierre Pronchery    }
2512*b077aed3SPierre Pronchery    die "There are ambiguous source file generations\n"
2513*b077aed3SPierre Pronchery        if $ambiguous_generation > 0;
2514e71b7053SJung-uk Kim
2515*b077aed3SPierre Pronchery    # All given source files should exist, or if generated, their
2516*b077aed3SPierre Pronchery    # generator should exist.  This loop ensures this is true.
2517*b077aed3SPierre Pronchery    my $missing = 0;
2518*b077aed3SPierre Pronchery    foreach my $orig (sort keys %check_exist) {
2519*b077aed3SPierre Pronchery        foreach my $dest (@{$check_exist{$orig}}) {
2520*b077aed3SPierre Pronchery            if ($orig ne $src_configdata) {
2521*b077aed3SPierre Pronchery                if ($orig =~ /\.a$/) {
2522*b077aed3SPierre Pronchery                    # Static library names may be used as sources, so we
2523*b077aed3SPierre Pronchery                    # need to detect those and give them special treatment.
2524*b077aed3SPierre Pronchery                    unless (grep { $_ eq $orig }
2525*b077aed3SPierre Pronchery                            keys %{$unified_info{libraries}}) {
2526*b077aed3SPierre Pronchery                        warn "$orig is given as source for $dest, but no such library is built\n";
2527*b077aed3SPierre Pronchery                        $missing++;
2528*b077aed3SPierre Pronchery                    }
2529*b077aed3SPierre Pronchery                } else {
2530*b077aed3SPierre Pronchery                    # A source may be generated, and its generator may be
2531*b077aed3SPierre Pronchery                    # generated as well.  We therefore loop to dig out the
2532*b077aed3SPierre Pronchery                    # first generator.
2533*b077aed3SPierre Pronchery                    my $gen = $orig;
2534*b077aed3SPierre Pronchery
2535*b077aed3SPierre Pronchery                    while (my @next = keys %{$check_generate{$gen}}) {
2536*b077aed3SPierre Pronchery                        $gen = $next[0];
2537*b077aed3SPierre Pronchery                    }
2538*b077aed3SPierre Pronchery
2539*b077aed3SPierre Pronchery                    if (! -f $gen) {
2540*b077aed3SPierre Pronchery                        if ($gen ne $orig) {
2541*b077aed3SPierre Pronchery                            $missing++;
2542*b077aed3SPierre Pronchery                            warn "$orig is given as source for $dest, but its generator (leading to $gen) is missing\n";
2543*b077aed3SPierre Pronchery                        } else {
2544*b077aed3SPierre Pronchery                            $missing++;
2545*b077aed3SPierre Pronchery                            warn "$orig is given as source for $dest, but is missing\n";
2546c9cf7b5cSJung-uk Kim                        }
2547c9cf7b5cSJung-uk Kim                    }
2548c9cf7b5cSJung-uk Kim                }
2549*b077aed3SPierre Pronchery            }
2550*b077aed3SPierre Pronchery        }
2551*b077aed3SPierre Pronchery    }
2552*b077aed3SPierre Pronchery    die "There are files missing\n" if $missing > 0;
2553*b077aed3SPierre Pronchery
2554*b077aed3SPierre Pronchery    # Go through the sources of all libraries and check that the same basename
2555*b077aed3SPierre Pronchery    # doesn't appear more than once.  Some static library archivers depend on
2556*b077aed3SPierre Pronchery    # them being unique.
2557*b077aed3SPierre Pronchery    {
2558*b077aed3SPierre Pronchery        my $err = 0;
2559*b077aed3SPierre Pronchery        foreach my $prod (keys %{$unified_info{libraries}}) {
2560*b077aed3SPierre Pronchery            my @prod_sources =
2561*b077aed3SPierre Pronchery                map { keys %{$unified_info{sources}->{$_}} }
2562*b077aed3SPierre Pronchery                keys %{$unified_info{sources}->{$prod}};
2563*b077aed3SPierre Pronchery            my %srccnt = ();
2564*b077aed3SPierre Pronchery
2565*b077aed3SPierre Pronchery            # Count how many times a given each source basename
2566*b077aed3SPierre Pronchery            # appears for each product.
2567*b077aed3SPierre Pronchery            foreach my $src (@prod_sources) {
2568*b077aed3SPierre Pronchery                $srccnt{basename $src}++;
2569*b077aed3SPierre Pronchery            }
2570*b077aed3SPierre Pronchery
2571*b077aed3SPierre Pronchery            foreach my $src (keys %srccnt) {
2572*b077aed3SPierre Pronchery                if ((my $cnt = $srccnt{$src}) > 1) {
2573*b077aed3SPierre Pronchery                    print STDERR "$src appears $cnt times for the product $prod\n";
2574*b077aed3SPierre Pronchery                    $err++
2575*b077aed3SPierre Pronchery                }
2576*b077aed3SPierre Pronchery            }
2577*b077aed3SPierre Pronchery        }
2578*b077aed3SPierre Pronchery        die if $err > 0;
2579*b077aed3SPierre Pronchery    }
2580*b077aed3SPierre Pronchery
2581*b077aed3SPierre Pronchery    # Massage the result
2582c9cf7b5cSJung-uk Kim
2583e71b7053SJung-uk Kim    # If we depend on a header file or a perl module, add an inclusion of
2584e71b7053SJung-uk Kim    # its directory to allow smoothe inclusion
2585e71b7053SJung-uk Kim    foreach my $dest (keys %{$unified_info{depends}}) {
2586e71b7053SJung-uk Kim        next if $dest eq "";
2587e71b7053SJung-uk Kim        foreach my $d (keys %{$unified_info{depends}->{$dest}}) {
2588e71b7053SJung-uk Kim            next unless $d =~ /\.(h|pm)$/;
2589e71b7053SJung-uk Kim            my $i = dirname($d);
2590e71b7053SJung-uk Kim            my $spot =
2591e71b7053SJung-uk Kim                $d eq "configdata.pm" || defined($unified_info{generate}->{$d})
2592e71b7053SJung-uk Kim                ? 'build' : 'source';
2593e71b7053SJung-uk Kim            push @{$unified_info{includes}->{$dest}->{$spot}}, $i
2594e71b7053SJung-uk Kim                unless grep { $_ eq $i } @{$unified_info{includes}->{$dest}->{$spot}};
2595e71b7053SJung-uk Kim        }
2596e71b7053SJung-uk Kim    }
2597e71b7053SJung-uk Kim
2598*b077aed3SPierre Pronchery    # Go through all intermediary files and change their names to something that
2599*b077aed3SPierre Pronchery    # reflects what they will be built for.  Note that for some source files,
2600*b077aed3SPierre Pronchery    # this leads to duplicate object files because they are used multiple times.
2601*b077aed3SPierre Pronchery    # the goal is to rename all object files according to this scheme:
2602*b077aed3SPierre Pronchery    #    {productname}-{midfix}-{origobjname}.[o|res]
2603*b077aed3SPierre Pronchery    # the {midfix} is a keyword indicating the type of product, which is mostly
2604*b077aed3SPierre Pronchery    # valuable for libraries since they come in two forms.
2605*b077aed3SPierre Pronchery    #
2606*b077aed3SPierre Pronchery    # This also reorganises the {sources} and {shared_sources} so that the
2607*b077aed3SPierre Pronchery    # former only contains ALL object files that are supposed to end up in
2608*b077aed3SPierre Pronchery    # static libraries and programs, while the latter contains ALL object files
2609*b077aed3SPierre Pronchery    # that are supposed to end up in shared libraries and DSOs.
2610*b077aed3SPierre Pronchery    # The main reason for having two different source structures is to allow
2611*b077aed3SPierre Pronchery    # the same name to be used for the static and the shared variants of a
2612*b077aed3SPierre Pronchery    # library.
2613*b077aed3SPierre Pronchery    {
2614*b077aed3SPierre Pronchery        # Take copies so we don't get interference from added stuff
2615*b077aed3SPierre Pronchery        my %unified_copy = ();
2616*b077aed3SPierre Pronchery        foreach (('sources', 'shared_sources')) {
2617*b077aed3SPierre Pronchery            $unified_copy{$_} = { %{$unified_info{$_}} }
2618*b077aed3SPierre Pronchery                if defined($unified_info{$_});
2619*b077aed3SPierre Pronchery            delete $unified_info{$_};
2620*b077aed3SPierre Pronchery        }
2621*b077aed3SPierre Pronchery        foreach my $prodtype (('programs', 'libraries', 'modules', 'scripts')) {
2622*b077aed3SPierre Pronchery            # $intent serves multi purposes:
2623*b077aed3SPierre Pronchery            # - give a prefix for the new object files names
2624*b077aed3SPierre Pronchery            # - in the case of libraries, rearrange the object files so static
2625*b077aed3SPierre Pronchery            #   libraries use the 'sources' structure exclusively, while shared
2626*b077aed3SPierre Pronchery            #   libraries use the 'shared_sources' structure exclusively.
2627*b077aed3SPierre Pronchery            my $intent = {
2628*b077aed3SPierre Pronchery                programs  => { bin    => { src => [ 'sources' ],
2629*b077aed3SPierre Pronchery                                           dst => 'sources' } },
2630*b077aed3SPierre Pronchery                libraries => { lib    => { src => [ 'sources' ],
2631*b077aed3SPierre Pronchery                                           dst => 'sources' },
2632*b077aed3SPierre Pronchery                               shlib  => { prodselect =>
2633*b077aed3SPierre Pronchery                                               sub { grep !/\.a$/, @_ },
2634*b077aed3SPierre Pronchery                                           src => [ 'sources',
2635*b077aed3SPierre Pronchery                                                    'shared_sources' ],
2636*b077aed3SPierre Pronchery                                           dst => 'shared_sources' } },
2637*b077aed3SPierre Pronchery                modules   => { dso    => { src => [ 'sources' ],
2638*b077aed3SPierre Pronchery                                           dst => 'sources' } },
2639*b077aed3SPierre Pronchery                scripts   => { script => { src => [ 'sources' ],
2640*b077aed3SPierre Pronchery                                           dst => 'sources' } }
2641*b077aed3SPierre Pronchery               } -> {$prodtype};
2642*b077aed3SPierre Pronchery            foreach my $kind (keys %$intent) {
2643*b077aed3SPierre Pronchery                next if ($intent->{$kind}->{dst} eq 'shared_sources'
2644*b077aed3SPierre Pronchery                             && $disabled{shared});
2645*b077aed3SPierre Pronchery
2646*b077aed3SPierre Pronchery                my @src = @{$intent->{$kind}->{src}};
2647*b077aed3SPierre Pronchery                my $dst = $intent->{$kind}->{dst};
2648*b077aed3SPierre Pronchery                my $prodselect = $intent->{$kind}->{prodselect} // sub { @_ };
2649*b077aed3SPierre Pronchery                foreach my $prod ($prodselect->(keys %{$unified_info{$prodtype}})) {
2650*b077aed3SPierre Pronchery                    # %prod_sources has all applicable objects as keys, and
2651*b077aed3SPierre Pronchery                    # their corresponding sources as values
2652*b077aed3SPierre Pronchery                    my %prod_sources =
2653*b077aed3SPierre Pronchery                        map { $_ => [ keys %{$unified_copy{sources}->{$_}} ] }
2654*b077aed3SPierre Pronchery                        map { keys %{$unified_copy{$_}->{$prod}} }
2655*b077aed3SPierre Pronchery                        @src;
2656*b077aed3SPierre Pronchery                    foreach (keys %prod_sources) {
2657*b077aed3SPierre Pronchery                        # Only affect object files and resource files,
2658*b077aed3SPierre Pronchery                        # the others simply get a new value
2659*b077aed3SPierre Pronchery                        # (+1 instead of -1)
2660*b077aed3SPierre Pronchery                        if ($_ =~ /\.(o|res)$/) {
2661*b077aed3SPierre Pronchery                            (my $prodname = $prod) =~ s|\.a$||;
2662*b077aed3SPierre Pronchery                            my $newobj =
2663*b077aed3SPierre Pronchery                                catfile(dirname($_),
2664*b077aed3SPierre Pronchery                                        basename($prodname)
2665*b077aed3SPierre Pronchery                                            . '-' . $kind
2666*b077aed3SPierre Pronchery                                            . '-' . basename($_));
2667*b077aed3SPierre Pronchery                            $unified_info{$dst}->{$prod}->{$newobj} = 1;
2668*b077aed3SPierre Pronchery                            foreach my $src (@{$prod_sources{$_}}) {
2669*b077aed3SPierre Pronchery                                $unified_info{sources}->{$newobj}->{$src} = 1;
2670*b077aed3SPierre Pronchery                                # Adjust source attributes
2671*b077aed3SPierre Pronchery                                my $attrs = $unified_info{attributes}->{sources};
2672*b077aed3SPierre Pronchery                                if (defined $attrs->{$prod}
2673*b077aed3SPierre Pronchery                                    && defined $attrs->{$prod}->{$_}) {
2674*b077aed3SPierre Pronchery                                    $attrs->{$prod}->{$newobj} =
2675*b077aed3SPierre Pronchery                                        $attrs->{$prod}->{$_};
2676*b077aed3SPierre Pronchery                                    delete $attrs->{$prod}->{$_};
2677*b077aed3SPierre Pronchery                                }
2678*b077aed3SPierre Pronchery                                foreach my $objsrc (keys %{$attrs->{$_} // {}}) {
2679*b077aed3SPierre Pronchery                                    $attrs->{$newobj}->{$objsrc} =
2680*b077aed3SPierre Pronchery                                        $attrs->{$_}->{$objsrc};
2681*b077aed3SPierre Pronchery                                    delete $attrs->{$_}->{$objsrc};
2682*b077aed3SPierre Pronchery                                }
2683*b077aed3SPierre Pronchery                            }
2684*b077aed3SPierre Pronchery                            # Adjust dependencies
2685*b077aed3SPierre Pronchery                            foreach my $deps (keys %{$unified_info{depends}->{$_}}) {
2686*b077aed3SPierre Pronchery                                $unified_info{depends}->{$_}->{$deps} = -1;
2687*b077aed3SPierre Pronchery                                $unified_info{depends}->{$newobj}->{$deps} = 1;
2688*b077aed3SPierre Pronchery                            }
2689*b077aed3SPierre Pronchery                            # Adjust includes
2690*b077aed3SPierre Pronchery                            foreach my $k (('source', 'build')) {
2691*b077aed3SPierre Pronchery                                next unless
2692*b077aed3SPierre Pronchery                                    defined($unified_info{includes}->{$_}->{$k});
2693*b077aed3SPierre Pronchery                                my @incs = @{$unified_info{includes}->{$_}->{$k}};
2694*b077aed3SPierre Pronchery                                $unified_info{includes}->{$newobj}->{$k} = [ @incs ];
2695*b077aed3SPierre Pronchery                            }
2696*b077aed3SPierre Pronchery                        } else {
2697*b077aed3SPierre Pronchery                            $unified_info{$dst}->{$prod}->{$_} = 1;
2698e71b7053SJung-uk Kim                        }
2699e71b7053SJung-uk Kim                    }
2700e71b7053SJung-uk Kim                }
2701e71b7053SJung-uk Kim            }
2702*b077aed3SPierre Pronchery        }
2703*b077aed3SPierre Pronchery    }
2704*b077aed3SPierre Pronchery
2705*b077aed3SPierre Pronchery    # At this point, we have a number of sources with the value -1.  They
2706*b077aed3SPierre Pronchery    # aren't part of the local build and are probably meant for a different
2707*b077aed3SPierre Pronchery    # platform, and can therefore be cleaned away.  That happens when making
2708*b077aed3SPierre Pronchery    # %unified_info more efficient below.
2709e71b7053SJung-uk Kim
2710e71b7053SJung-uk Kim    ### Make unified_info a bit more efficient
2711e71b7053SJung-uk Kim    # One level structures
2712*b077aed3SPierre Pronchery    foreach (("programs", "libraries", "modules", "scripts", "targets")) {
2713e71b7053SJung-uk Kim        $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
2714e71b7053SJung-uk Kim    }
2715e71b7053SJung-uk Kim    # Two level structures
2716*b077aed3SPierre Pronchery    foreach my $l1 (("sources", "shared_sources", "ldadd", "depends",
2717*b077aed3SPierre Pronchery                     "imagedocs", "htmldocs", "mandocs")) {
2718e71b7053SJung-uk Kim        foreach my $l2 (sort keys %{$unified_info{$l1}}) {
2719*b077aed3SPierre Pronchery            my @items =
2720*b077aed3SPierre Pronchery                sort
2721*b077aed3SPierre Pronchery                grep { $unified_info{$l1}->{$l2}->{$_} > 0 }
2722*b077aed3SPierre Pronchery                keys %{$unified_info{$l1}->{$l2}};
2723*b077aed3SPierre Pronchery            if (@items) {
2724*b077aed3SPierre Pronchery                $unified_info{$l1}->{$l2} = [ @items ];
2725*b077aed3SPierre Pronchery            } else {
2726*b077aed3SPierre Pronchery                delete $unified_info{$l1}->{$l2};
2727e71b7053SJung-uk Kim            }
2728e71b7053SJung-uk Kim        }
2729*b077aed3SPierre Pronchery    }
2730*b077aed3SPierre Pronchery    # Defines
2731*b077aed3SPierre Pronchery    foreach my $dest (sort keys %{$unified_info{defines}}) {
2732*b077aed3SPierre Pronchery        $unified_info{defines}->{$dest}
2733*b077aed3SPierre Pronchery            = [ map { $_.$unified_info{defines}->{$dest}->{$_} }
2734*b077aed3SPierre Pronchery                sort keys %{$unified_info{defines}->{$dest}} ];
2735*b077aed3SPierre Pronchery    }
2736e71b7053SJung-uk Kim    # Includes
2737e71b7053SJung-uk Kim    foreach my $dest (sort keys %{$unified_info{includes}}) {
2738e71b7053SJung-uk Kim        if (defined($unified_info{includes}->{$dest}->{build})) {
2739e71b7053SJung-uk Kim            my @source_includes = ();
2740e71b7053SJung-uk Kim            @source_includes = ( @{$unified_info{includes}->{$dest}->{source}} )
2741e71b7053SJung-uk Kim                if defined($unified_info{includes}->{$dest}->{source});
2742e71b7053SJung-uk Kim            $unified_info{includes}->{$dest} =
2743e71b7053SJung-uk Kim                [ @{$unified_info{includes}->{$dest}->{build}} ];
2744e71b7053SJung-uk Kim            foreach my $inc (@source_includes) {
2745e71b7053SJung-uk Kim                push @{$unified_info{includes}->{$dest}}, $inc
2746e71b7053SJung-uk Kim                    unless grep { $_ eq $inc } @{$unified_info{includes}->{$dest}};
2747e71b7053SJung-uk Kim            }
2748*b077aed3SPierre Pronchery        } elsif (defined($unified_info{includes}->{$dest}->{source})) {
2749e71b7053SJung-uk Kim            $unified_info{includes}->{$dest} =
2750e71b7053SJung-uk Kim                [ @{$unified_info{includes}->{$dest}->{source}} ];
2751*b077aed3SPierre Pronchery        } else {
2752*b077aed3SPierre Pronchery            delete $unified_info{includes}->{$dest};
2753e71b7053SJung-uk Kim        }
2754e71b7053SJung-uk Kim    }
2755c9cf7b5cSJung-uk Kim
2756c9cf7b5cSJung-uk Kim    # For convenience collect information regarding directories where
2757c9cf7b5cSJung-uk Kim    # files are generated, those generated files and the end product
2758c9cf7b5cSJung-uk Kim    # they end up in where applicable.  Then, add build rules for those
2759c9cf7b5cSJung-uk Kim    # directories
2760c9cf7b5cSJung-uk Kim    my %loopinfo = ( "lib" => [ @{$unified_info{libraries}} ],
2761*b077aed3SPierre Pronchery                     "dso" => [ @{$unified_info{modules}} ],
2762c9cf7b5cSJung-uk Kim                     "bin" => [ @{$unified_info{programs}} ],
2763*b077aed3SPierre Pronchery                     "script" => [ @{$unified_info{scripts}} ],
2764*b077aed3SPierre Pronchery                     "docs" => [ (map { @{$unified_info{imagedocs}->{$_} // []} }
2765*b077aed3SPierre Pronchery                                  keys %{$unified_info{imagedocs} // {}}),
2766*b077aed3SPierre Pronchery                                 (map { @{$unified_info{htmldocs}->{$_} // []} }
2767*b077aed3SPierre Pronchery                                  keys %{$unified_info{htmldocs} // {}}),
2768*b077aed3SPierre Pronchery                                 (map { @{$unified_info{mandocs}->{$_} // []} }
2769*b077aed3SPierre Pronchery                                  keys %{$unified_info{mandocs} // {}}) ] );
2770*b077aed3SPierre Pronchery    foreach my $type (sort keys %loopinfo) {
2771c9cf7b5cSJung-uk Kim        foreach my $product (@{$loopinfo{$type}}) {
2772c9cf7b5cSJung-uk Kim            my %dirs = ();
2773c9cf7b5cSJung-uk Kim            my $pd = dirname($product);
2774c9cf7b5cSJung-uk Kim
2775c9cf7b5cSJung-uk Kim            foreach (@{$unified_info{sources}->{$product} // []},
2776c9cf7b5cSJung-uk Kim                     @{$unified_info{shared_sources}->{$product} // []}) {
2777c9cf7b5cSJung-uk Kim                my $d = dirname($_);
2778c9cf7b5cSJung-uk Kim
2779c9cf7b5cSJung-uk Kim                # We don't want to create targets for source directories
2780c9cf7b5cSJung-uk Kim                # when building out of source
2781c9cf7b5cSJung-uk Kim                next if ($config{sourcedir} ne $config{builddir}
2782c9cf7b5cSJung-uk Kim                             && $d =~ m|^\Q$config{sourcedir}\E|);
2783c9cf7b5cSJung-uk Kim                # We already have a "test" target, and the current directory
2784c9cf7b5cSJung-uk Kim                # is just silly to make a target for
2785c9cf7b5cSJung-uk Kim                next if $d eq "test" || $d eq ".";
2786c9cf7b5cSJung-uk Kim
2787c9cf7b5cSJung-uk Kim                $dirs{$d} = 1;
2788c9cf7b5cSJung-uk Kim                push @{$unified_info{dirinfo}->{$d}->{deps}}, $_
2789c9cf7b5cSJung-uk Kim                    if $d ne $pd;
2790c9cf7b5cSJung-uk Kim            }
2791*b077aed3SPierre Pronchery            foreach (sort keys %dirs) {
2792c9cf7b5cSJung-uk Kim                push @{$unified_info{dirinfo}->{$_}->{products}->{$type}},
2793c9cf7b5cSJung-uk Kim                    $product;
2794c9cf7b5cSJung-uk Kim            }
2795c9cf7b5cSJung-uk Kim        }
2796c9cf7b5cSJung-uk Kim    }
2797e71b7053SJung-uk Kim}
2798e71b7053SJung-uk Kim
2799e71b7053SJung-uk Kim# For the schemes that need it, we provide the old *_obj configs
2800e71b7053SJung-uk Kim# from the *_asm_obj ones
2801e71b7053SJung-uk Kimforeach (grep /_(asm|aux)_src$/, keys %target) {
2802e71b7053SJung-uk Kim    my $src = $_;
2803e71b7053SJung-uk Kim    (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
2804e71b7053SJung-uk Kim    $target{$obj} = $target{$src};
2805e71b7053SJung-uk Kim    $target{$obj} =~ s/\.[csS]\b/.o/g; # C and assembler
2806e71b7053SJung-uk Kim    $target{$obj} =~ s/\.(cc|cpp)\b/_cc.o/g; # C++
2807e71b7053SJung-uk Kim}
2808e71b7053SJung-uk Kim
2809e71b7053SJung-uk Kim# Write down our configuration where it fits #########################
2810e71b7053SJung-uk Kim
2811*b077aed3SPierre Proncherymy %template_vars = (
2812*b077aed3SPierre Pronchery    config => \%config,
2813*b077aed3SPierre Pronchery    target => \%target,
2814*b077aed3SPierre Pronchery    disablables => \@disablables,
2815*b077aed3SPierre Pronchery    disablables_int => \@disablables_int,
2816*b077aed3SPierre Pronchery    disabled => \%disabled,
2817*b077aed3SPierre Pronchery    withargs => \%withargs,
2818*b077aed3SPierre Pronchery    unified_info => \%unified_info,
2819*b077aed3SPierre Pronchery    tls => \@tls,
2820*b077aed3SPierre Pronchery    dtls => \@dtls,
2821*b077aed3SPierre Pronchery    makevars => [ sort keys %user ],
2822*b077aed3SPierre Pronchery    disabled_info => \%disabled_info,
2823*b077aed3SPierre Pronchery    user_crossable => \@user_crossable,
2824e71b7053SJung-uk Kim);
2825*b077aed3SPierre Proncherymy $configdata_outname = 'configdata.pm';
2826*b077aed3SPierre Proncheryopen CONFIGDATA, ">$configdata_outname.new"
2827*b077aed3SPierre Pronchery    or die "Trying to create $configdata_outname.new: $!";
2828*b077aed3SPierre Proncherymy $configdata_tmplname = cleanfile($srcdir, "configdata.pm.in", $blddir);
2829*b077aed3SPierre Proncherymy $configdata_tmpl =
2830*b077aed3SPierre Pronchery    OpenSSL::Template->new(TYPE => 'FILE', SOURCE => $configdata_tmplname);
2831*b077aed3SPierre Pronchery$configdata_tmpl->fill_in(
2832*b077aed3SPierre Pronchery    FILENAME => $configdata_tmplname,
2833*b077aed3SPierre Pronchery    OUTPUT => \*CONFIGDATA,
2834*b077aed3SPierre Pronchery    HASH => { %template_vars,
2835*b077aed3SPierre Pronchery              autowarntext => [
2836*b077aed3SPierre Pronchery                  'WARNING: do not edit!',
2837*b077aed3SPierre Pronchery                  "Generated by Configure from $configdata_tmplname",
2838*b077aed3SPierre Pronchery              ] }
2839*b077aed3SPierre Pronchery) or die $Text::Template::ERROR;
2840*b077aed3SPierre Proncheryclose CONFIGDATA;
2841e71b7053SJung-uk Kim
2842*b077aed3SPierre Proncheryrename "$configdata_outname.new", $configdata_outname;
2843e71b7053SJung-uk Kimif ($builder_platform eq 'unix') {
2844e71b7053SJung-uk Kim    my $mode = (0755 & ~umask);
2845e71b7053SJung-uk Kim    chmod $mode, 'configdata.pm'
2846e71b7053SJung-uk Kim        or warn sprintf("WARNING: Couldn't change mode for 'configdata.pm' to 0%03o: %s\n",$mode,$!);
28473b4e3dcbSSimon L. B. Nielsen}
2848*b077aed3SPierre Proncheryprint "Created $configdata_outname\n";
28493b4e3dcbSSimon L. B. Nielsen
2850*b077aed3SPierre Proncheryprint "Running $configdata_outname\n";
2851*b077aed3SPierre Proncherymy $perlcmd = (quotify("maybeshell", $config{PERL}))[0];
2852*b077aed3SPierre Proncherymy $cmd = "$perlcmd $configdata_outname";
2853*b077aed3SPierre Pronchery#print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
2854*b077aed3SPierre Proncherysystem($cmd);
2855*b077aed3SPierre Proncheryexit 1 if $? != 0;
285674664626SKris Kennaway
2857e71b7053SJung-uk Kim$SIG{__DIE__} = $orig_death_handler;
2858e71b7053SJung-uk Kim
2859e71b7053SJung-uk Kimprint <<"EOF" if ($disabled{threads} eq "unavailable");
286074664626SKris Kennaway
286174664626SKris KennawayThe library could not be configured for supporting multi-threaded
286274664626SKris Kennawayapplications as the compiler options required on this system are not known.
2863*b077aed3SPierre ProncherySee file INSTALL.md for details if you need multi-threading.
286474664626SKris KennawayEOF
286574664626SKris Kennaway
2866e71b7053SJung-uk Kimprint <<"EOF" if ($no_shared_warn);
2867fceca8a3SJacques Vidrine
2868e71b7053SJung-uk KimThe options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
2869e71b7053SJung-uk Kimplatform, so we will pretend you gave the option 'no-pic', which also disables
2870e71b7053SJung-uk Kim'shared' and 'dynamic-engine'.  If you know how to implement shared libraries
2871e71b7053SJung-uk Kimor position independent code, please let us know (but please first make sure
2872e71b7053SJung-uk Kimyou have tried with a current version of OpenSSL).
28738180e704SJung-uk KimEOF
28748180e704SJung-uk Kim
2875*b077aed3SPierre Proncheryprint $banner;
2876fceca8a3SJacques Vidrine
287774664626SKris Kennawayexit(0);
287874664626SKris Kennaway
2879e71b7053SJung-uk Kim######################################################################
2880e71b7053SJung-uk Kim#
2881e71b7053SJung-uk Kim# Helpers and utility functions
2882e71b7053SJung-uk Kim#
2883e71b7053SJung-uk Kim
2884e71b7053SJung-uk Kim# Death handler, to print a helpful message in case of failure #######
2885e71b7053SJung-uk Kim#
2886e71b7053SJung-uk Kimsub death_handler {
2887e71b7053SJung-uk Kim    die @_ if $^S;              # To prevent the added message in eval blocks
2888640242a5SJung-uk Kim    my $build_file = $config{build_file} // "build file";
2889e71b7053SJung-uk Kim    my @message = ( <<"_____", @_ );
2890e71b7053SJung-uk Kim
2891e71b7053SJung-uk KimFailure!  $build_file wasn't produced.
2892*b077aed3SPierre ProncheryPlease read INSTALL.md and associated NOTES-* files.  You may also have to
2893*b077aed3SPierre Proncherylook over your available compiler tool chain or change your configuration.
2894e71b7053SJung-uk Kim
2895e71b7053SJung-uk Kim_____
2896e71b7053SJung-uk Kim
2897e71b7053SJung-uk Kim    # Dying is terminal, so it's ok to reset the signal handler here.
2898e71b7053SJung-uk Kim    $SIG{__DIE__} = $orig_death_handler;
2899e71b7053SJung-uk Kim    die @message;
2900e71b7053SJung-uk Kim}
2901e71b7053SJung-uk Kim
2902e71b7053SJung-uk Kim# Configuration file reading #########################################
2903e71b7053SJung-uk Kim
2904e71b7053SJung-uk Kim# Note: All of the helper functions are for lazy evaluation.  They all
2905e71b7053SJung-uk Kim# return a CODE ref, which will return the intended value when evaluated.
2906e71b7053SJung-uk Kim# Thus, whenever there's mention of a returned value, it's about that
2907e71b7053SJung-uk Kim# intended value.
2908e71b7053SJung-uk Kim
2909e71b7053SJung-uk Kim# Helper function to implement conditional value variants, with a default
2910e71b7053SJung-uk Kim# plus additional values based on the value of $config{build_type}.
2911e71b7053SJung-uk Kim# Arguments are given in hash table form:
2912e71b7053SJung-uk Kim#
2913e71b7053SJung-uk Kim#       picker(default => "Basic string: ",
2914e71b7053SJung-uk Kim#              debug   => "debug",
2915e71b7053SJung-uk Kim#              release => "release")
2916e71b7053SJung-uk Kim#
2917e71b7053SJung-uk Kim# When configuring with --debug, the resulting string will be
2918e71b7053SJung-uk Kim# "Basic string: debug", and when not, it will be "Basic string: release"
2919e71b7053SJung-uk Kim#
2920e71b7053SJung-uk Kim# This can be used to create variants of sets of flags according to the
2921e71b7053SJung-uk Kim# build type:
2922e71b7053SJung-uk Kim#
2923e71b7053SJung-uk Kim#       cflags => picker(default => "-Wall",
2924e71b7053SJung-uk Kim#                        debug   => "-g -O0",
2925e71b7053SJung-uk Kim#                        release => "-O3")
2926e71b7053SJung-uk Kim#
2927e71b7053SJung-uk Kimsub picker {
2928e71b7053SJung-uk Kim    my %opts = @_;
2929e71b7053SJung-uk Kim    return sub { add($opts{default} || (),
2930e71b7053SJung-uk Kim                     $opts{$config{build_type}} || ())->(); }
2931e71b7053SJung-uk Kim}
2932e71b7053SJung-uk Kim
2933e71b7053SJung-uk Kim# Helper function to combine several values of different types into one.
2934e71b7053SJung-uk Kim# This is useful if you want to combine a string with the result of a
2935e71b7053SJung-uk Kim# lazy function, such as:
2936e71b7053SJung-uk Kim#
2937e71b7053SJung-uk Kim#       cflags => combine("-Wall", sub { $disabled{zlib} ? () : "-DZLIB" })
2938e71b7053SJung-uk Kim#
2939e71b7053SJung-uk Kimsub combine {
2940e71b7053SJung-uk Kim    my @stuff = @_;
2941e71b7053SJung-uk Kim    return sub { add(@stuff)->(); }
2942e71b7053SJung-uk Kim}
2943e71b7053SJung-uk Kim
2944e71b7053SJung-uk Kim# Helper function to implement conditional values depending on the value
2945e71b7053SJung-uk Kim# of $disabled{threads}.  Can be used as follows:
2946e71b7053SJung-uk Kim#
2947e71b7053SJung-uk Kim#       cflags => combine("-Wall", threads("-pthread"))
2948e71b7053SJung-uk Kim#
2949e71b7053SJung-uk Kimsub threads {
2950e71b7053SJung-uk Kim    my @flags = @_;
2951e71b7053SJung-uk Kim    return sub { add($disabled{threads} ? () : @flags)->(); }
2952e71b7053SJung-uk Kim}
2953e71b7053SJung-uk Kim
2954e71b7053SJung-uk Kimsub shared {
2955e71b7053SJung-uk Kim    my @flags = @_;
2956e71b7053SJung-uk Kim    return sub { add($disabled{shared} ? () : @flags)->(); }
2957e71b7053SJung-uk Kim}
2958e71b7053SJung-uk Kim
2959e71b7053SJung-uk Kimour $add_called = 0;
2960e71b7053SJung-uk Kim# Helper function to implement adding values to already existing configuration
2961e71b7053SJung-uk Kim# values.  It handles elements that are ARRAYs, CODEs and scalars
2962e71b7053SJung-uk Kimsub _add {
2963e71b7053SJung-uk Kim    my $separator = shift;
2964e71b7053SJung-uk Kim
2965e71b7053SJung-uk Kim    # If there's any ARRAY in the collection of values OR the separator
2966e71b7053SJung-uk Kim    # is undef, we will return an ARRAY of combined values, otherwise a
2967e71b7053SJung-uk Kim    # string of joined values with $separator as the separator.
2968e71b7053SJung-uk Kim    my $found_array = !defined($separator);
2969e71b7053SJung-uk Kim
2970e71b7053SJung-uk Kim    my @values =
2971e71b7053SJung-uk Kim        map {
2972e71b7053SJung-uk Kim            my $res = $_;
2973e71b7053SJung-uk Kim            while (ref($res) eq "CODE") {
2974e71b7053SJung-uk Kim                $res = $res->();
2975e71b7053SJung-uk Kim            }
2976e71b7053SJung-uk Kim            if (defined($res)) {
2977e71b7053SJung-uk Kim                if (ref($res) eq "ARRAY") {
2978e71b7053SJung-uk Kim                    $found_array = 1;
2979e71b7053SJung-uk Kim                    @$res;
2980e71b7053SJung-uk Kim                } else {
2981e71b7053SJung-uk Kim                    $res;
2982e71b7053SJung-uk Kim                }
2983e71b7053SJung-uk Kim            } else {
2984e71b7053SJung-uk Kim                ();
2985e71b7053SJung-uk Kim            }
2986e71b7053SJung-uk Kim    } (@_);
2987e71b7053SJung-uk Kim
2988e71b7053SJung-uk Kim    $add_called = 1;
2989e71b7053SJung-uk Kim
2990e71b7053SJung-uk Kim    if ($found_array) {
2991e71b7053SJung-uk Kim        [ @values ];
2992e71b7053SJung-uk Kim    } else {
2993e71b7053SJung-uk Kim        join($separator, grep { defined($_) && $_ ne "" } @values);
2994e71b7053SJung-uk Kim    }
2995e71b7053SJung-uk Kim}
2996e71b7053SJung-uk Kimsub add_before {
2997e71b7053SJung-uk Kim    my $separator = " ";
2998e71b7053SJung-uk Kim    if (ref($_[$#_]) eq "HASH") {
2999e71b7053SJung-uk Kim        my $opts = pop;
3000e71b7053SJung-uk Kim        $separator = $opts->{separator};
3001e71b7053SJung-uk Kim    }
3002e71b7053SJung-uk Kim    my @x = @_;
3003e71b7053SJung-uk Kim    sub { _add($separator, @x, @_) };
3004e71b7053SJung-uk Kim}
3005e71b7053SJung-uk Kimsub add {
3006e71b7053SJung-uk Kim    my $separator = " ";
3007e71b7053SJung-uk Kim    if (ref($_[$#_]) eq "HASH") {
3008e71b7053SJung-uk Kim        my $opts = pop;
3009e71b7053SJung-uk Kim        $separator = $opts->{separator};
3010e71b7053SJung-uk Kim    }
3011e71b7053SJung-uk Kim    my @x = @_;
3012e71b7053SJung-uk Kim    sub { _add($separator, @_, @x) };
3013e71b7053SJung-uk Kim}
3014e71b7053SJung-uk Kim
3015e71b7053SJung-uk Kimsub read_eval_file {
3016e71b7053SJung-uk Kim    my $fname = shift;
3017e71b7053SJung-uk Kim    my $content;
3018e71b7053SJung-uk Kim    my @result;
3019e71b7053SJung-uk Kim
3020e71b7053SJung-uk Kim    open F, "< $fname" or die "Can't open '$fname': $!\n";
3021e71b7053SJung-uk Kim    {
3022e71b7053SJung-uk Kim        undef local $/;
3023e71b7053SJung-uk Kim        $content = <F>;
3024e71b7053SJung-uk Kim    }
3025e71b7053SJung-uk Kim    close F;
3026e71b7053SJung-uk Kim    {
3027e71b7053SJung-uk Kim        local $@;
3028e71b7053SJung-uk Kim
3029e71b7053SJung-uk Kim        @result = ( eval $content );
3030e71b7053SJung-uk Kim        warn $@ if $@;
3031e71b7053SJung-uk Kim    }
3032e71b7053SJung-uk Kim    return wantarray ? @result : $result[0];
3033e71b7053SJung-uk Kim}
3034e71b7053SJung-uk Kim
3035e71b7053SJung-uk Kim# configuration reader, evaluates the input file as a perl script and expects
3036e71b7053SJung-uk Kim# it to fill %targets with target configurations.  Those are then added to
3037e71b7053SJung-uk Kim# %table.
3038e71b7053SJung-uk Kimsub read_config {
3039e71b7053SJung-uk Kim    my $fname = shift;
3040e71b7053SJung-uk Kim    my %targets;
3041e71b7053SJung-uk Kim
3042e71b7053SJung-uk Kim    {
3043e71b7053SJung-uk Kim        # Protect certain tables from tampering
3044e71b7053SJung-uk Kim        local %table = ();
3045e71b7053SJung-uk Kim
3046e71b7053SJung-uk Kim        %targets = read_eval_file($fname);
3047e71b7053SJung-uk Kim    }
3048e71b7053SJung-uk Kim    my %preexisting = ();
3049e71b7053SJung-uk Kim    foreach (sort keys %targets) {
3050e71b7053SJung-uk Kim        $preexisting{$_} = 1 if $table{$_};
3051e71b7053SJung-uk Kim    }
3052e71b7053SJung-uk Kim    die <<"EOF",
3053e71b7053SJung-uk KimThe following config targets from $fname
3054e71b7053SJung-uk Kimshadow pre-existing config targets with the same name:
3055e71b7053SJung-uk KimEOF
3056e71b7053SJung-uk Kim        map { "  $_\n" } sort keys %preexisting
3057e71b7053SJung-uk Kim        if %preexisting;
3058e71b7053SJung-uk Kim
3059e71b7053SJung-uk Kim
3060e71b7053SJung-uk Kim    # For each target, check that it's configured with a hash table.
3061e71b7053SJung-uk Kim    foreach (keys %targets) {
3062e71b7053SJung-uk Kim        if (ref($targets{$_}) ne "HASH") {
3063e71b7053SJung-uk Kim            if (ref($targets{$_}) eq "") {
3064e71b7053SJung-uk Kim                warn "Deprecated target configuration for $_, ignoring...\n";
3065e71b7053SJung-uk Kim            } else {
3066e71b7053SJung-uk Kim                warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
3067e71b7053SJung-uk Kim            }
3068e71b7053SJung-uk Kim            delete $targets{$_};
3069e71b7053SJung-uk Kim        } else {
3070e71b7053SJung-uk Kim            $targets{$_}->{_conf_fname_int} = add([ $fname ]);
3071e71b7053SJung-uk Kim        }
3072e71b7053SJung-uk Kim    }
3073e71b7053SJung-uk Kim
3074e71b7053SJung-uk Kim    %table = (%table, %targets);
3075e71b7053SJung-uk Kim
3076e71b7053SJung-uk Kim}
3077e71b7053SJung-uk Kim
3078e71b7053SJung-uk Kim# configuration resolver.  Will only resolve all the lazy evaluation
3079e71b7053SJung-uk Kim# codeblocks for the chosen target and all those it inherits from,
3080e71b7053SJung-uk Kim# recursively
3081e71b7053SJung-uk Kimsub resolve_config {
3082e71b7053SJung-uk Kim    my $target = shift;
3083e71b7053SJung-uk Kim    my @breadcrumbs = @_;
3084e71b7053SJung-uk Kim
3085e71b7053SJung-uk Kim#    my $extra_checks = defined($ENV{CONFIGURE_EXTRA_CHECKS});
3086e71b7053SJung-uk Kim
3087e71b7053SJung-uk Kim    if (grep { $_ eq $target } @breadcrumbs) {
3088e71b7053SJung-uk Kim        die "inherit_from loop!  target backtrace:\n  "
3089e71b7053SJung-uk Kim            ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
3090e71b7053SJung-uk Kim    }
3091e71b7053SJung-uk Kim
3092e71b7053SJung-uk Kim    if (!defined($table{$target})) {
3093e71b7053SJung-uk Kim        warn "Warning! target $target doesn't exist!\n";
3094e71b7053SJung-uk Kim        return ();
3095e71b7053SJung-uk Kim    }
3096e71b7053SJung-uk Kim    # Recurse through all inheritances.  They will be resolved on the
3097e71b7053SJung-uk Kim    # fly, so when this operation is done, they will all just be a
3098e71b7053SJung-uk Kim    # bunch of attributes with string values.
3099e71b7053SJung-uk Kim    # What we get here, though, are keys with references to lists of
3100e71b7053SJung-uk Kim    # the combined values of them all.  We will deal with lists after
3101e71b7053SJung-uk Kim    # this stage is done.
3102e71b7053SJung-uk Kim    my %combined_inheritance = ();
3103e71b7053SJung-uk Kim    if ($table{$target}->{inherit_from}) {
3104e71b7053SJung-uk Kim        my @inherit_from =
3105e71b7053SJung-uk Kim            map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
3106e71b7053SJung-uk Kim        foreach (@inherit_from) {
3107e71b7053SJung-uk Kim            my %inherited_config = resolve_config($_, $target, @breadcrumbs);
3108e71b7053SJung-uk Kim
3109e71b7053SJung-uk Kim            # 'template' is a marker that's considered private to
3110e71b7053SJung-uk Kim            # the config that had it.
3111e71b7053SJung-uk Kim            delete $inherited_config{template};
3112e71b7053SJung-uk Kim
3113e71b7053SJung-uk Kim            foreach (keys %inherited_config) {
3114e71b7053SJung-uk Kim                if (!$combined_inheritance{$_}) {
3115e71b7053SJung-uk Kim                    $combined_inheritance{$_} = [];
3116e71b7053SJung-uk Kim                }
3117e71b7053SJung-uk Kim                push @{$combined_inheritance{$_}}, $inherited_config{$_};
3118e71b7053SJung-uk Kim            }
3119e71b7053SJung-uk Kim        }
3120e71b7053SJung-uk Kim    }
3121e71b7053SJung-uk Kim
3122e71b7053SJung-uk Kim    # We won't need inherit_from in this target any more, since we've
3123e71b7053SJung-uk Kim    # resolved all the inheritances that lead to this
3124e71b7053SJung-uk Kim    delete $table{$target}->{inherit_from};
3125e71b7053SJung-uk Kim
3126e71b7053SJung-uk Kim    # Now is the time to deal with those lists.  Here's the place to
3127e71b7053SJung-uk Kim    # decide what shall be done with those lists, all based on the
3128e71b7053SJung-uk Kim    # values of the target we're currently dealing with.
3129e71b7053SJung-uk Kim    # - If a value is a coderef, it will be executed with the list of
3130e71b7053SJung-uk Kim    #   inherited values as arguments.
3131e71b7053SJung-uk Kim    # - If the corresponding key doesn't have a value at all or is the
3132e71b7053SJung-uk Kim    #   empty string, the inherited value list will be run through the
3133e71b7053SJung-uk Kim    #   default combiner (below), and the result becomes this target's
3134e71b7053SJung-uk Kim    #   value.
3135e71b7053SJung-uk Kim    # - Otherwise, this target's value is assumed to be a string that
3136e71b7053SJung-uk Kim    #   will simply override the inherited list of values.
3137e71b7053SJung-uk Kim    my $default_combiner = add();
3138e71b7053SJung-uk Kim
3139e71b7053SJung-uk Kim    my %all_keys =
3140e71b7053SJung-uk Kim        map { $_ => 1 } (keys %combined_inheritance,
3141e71b7053SJung-uk Kim                         keys %{$table{$target}});
3142e71b7053SJung-uk Kim
3143e71b7053SJung-uk Kim    sub process_values {
3144e71b7053SJung-uk Kim        my $object    = shift;
3145e71b7053SJung-uk Kim        my $inherited = shift;  # Always a [ list ]
3146e71b7053SJung-uk Kim        my $target    = shift;
3147e71b7053SJung-uk Kim        my $entry     = shift;
3148e71b7053SJung-uk Kim
3149e71b7053SJung-uk Kim        $add_called = 0;
3150e71b7053SJung-uk Kim
3151e71b7053SJung-uk Kim        while(ref($object) eq "CODE") {
3152e71b7053SJung-uk Kim            $object = $object->(@$inherited);
3153e71b7053SJung-uk Kim        }
3154e71b7053SJung-uk Kim        if (!defined($object)) {
3155e71b7053SJung-uk Kim            return ();
3156e71b7053SJung-uk Kim        }
3157e71b7053SJung-uk Kim        elsif (ref($object) eq "ARRAY") {
3158e71b7053SJung-uk Kim            local $add_called;  # To make sure recursive calls don't affect it
3159e71b7053SJung-uk Kim            return [ map { process_values($_, $inherited, $target, $entry) }
3160e71b7053SJung-uk Kim                     @$object ];
3161e71b7053SJung-uk Kim        } elsif (ref($object) eq "") {
3162e71b7053SJung-uk Kim            return $object;
3163e71b7053SJung-uk Kim        } else {
3164e71b7053SJung-uk Kim            die "cannot handle reference type ",ref($object)
3165e71b7053SJung-uk Kim                ," found in target ",$target," -> ",$entry,"\n";
3166e71b7053SJung-uk Kim        }
3167e71b7053SJung-uk Kim    }
3168e71b7053SJung-uk Kim
31695ac766abSJung-uk Kim    foreach my $key (sort keys %all_keys) {
31705ac766abSJung-uk Kim        my $previous = $combined_inheritance{$key};
3171e71b7053SJung-uk Kim
3172e71b7053SJung-uk Kim        # Current target doesn't have a value for the current key?
3173e71b7053SJung-uk Kim        # Assign it the default combiner, the rest of this loop body
3174e71b7053SJung-uk Kim        # will handle it just like any other coderef.
31755ac766abSJung-uk Kim        if (!exists $table{$target}->{$key}) {
31765ac766abSJung-uk Kim            $table{$target}->{$key} = $default_combiner;
3177e71b7053SJung-uk Kim        }
3178e71b7053SJung-uk Kim
31795ac766abSJung-uk Kim        $table{$target}->{$key} = process_values($table{$target}->{$key},
31805ac766abSJung-uk Kim                                               $combined_inheritance{$key},
31815ac766abSJung-uk Kim                                               $target, $key);
31825ac766abSJung-uk Kim        unless(defined($table{$target}->{$key})) {
31835ac766abSJung-uk Kim            delete $table{$target}->{$key};
3184e71b7053SJung-uk Kim        }
3185e71b7053SJung-uk Kim#        if ($extra_checks &&
31865ac766abSJung-uk Kim#            $previous && !($add_called ||  $previous ~~ $table{$target}->{$key})) {
31875ac766abSJung-uk Kim#            warn "$key got replaced in $target\n";
3188e71b7053SJung-uk Kim#        }
3189e71b7053SJung-uk Kim    }
3190e71b7053SJung-uk Kim
3191e71b7053SJung-uk Kim    # Finally done, return the result.
3192e71b7053SJung-uk Kim    return %{$table{$target}};
3193e71b7053SJung-uk Kim}
3194e71b7053SJung-uk Kim
319574664626SKris Kennawaysub usage
319674664626SKris Kennaway        {
319774664626SKris Kennaway        print STDERR $usage;
3198f579bf8eSKris Kennaway        print STDERR "\npick os/compiler from:\n";
319974664626SKris Kennaway        my $j=0;
320074664626SKris Kennaway        my $i;
3201f579bf8eSKris Kennaway        my $k=0;
320274664626SKris Kennaway        foreach $i (sort keys %table)
320374664626SKris Kennaway                {
3204e71b7053SJung-uk Kim                next if $table{$i}->{template};
320574664626SKris Kennaway                next if $i =~ /^debug/;
3206f579bf8eSKris Kennaway                $k += length($i) + 1;
3207f579bf8eSKris Kennaway                if ($k > 78)
3208f579bf8eSKris Kennaway                        {
3209f579bf8eSKris Kennaway                        print STDERR "\n";
3210f579bf8eSKris Kennaway                        $k=length($i);
3211f579bf8eSKris Kennaway                        }
3212f579bf8eSKris Kennaway                print STDERR $i . " ";
321374664626SKris Kennaway                }
321474664626SKris Kennaway        foreach $i (sort keys %table)
321574664626SKris Kennaway                {
3216e71b7053SJung-uk Kim                next if $table{$i}->{template};
321774664626SKris Kennaway                next if $i !~ /^debug/;
3218f579bf8eSKris Kennaway                $k += length($i) + 1;
3219f579bf8eSKris Kennaway                if ($k > 78)
3220f579bf8eSKris Kennaway                        {
322174664626SKris Kennaway                        print STDERR "\n";
3222f579bf8eSKris Kennaway                        $k=length($i);
3223f579bf8eSKris Kennaway                        }
3224f579bf8eSKris Kennaway                print STDERR $i . " ";
3225f579bf8eSKris Kennaway                }
322674664626SKris Kennaway        exit(1);
322774664626SKris Kennaway        }
322874664626SKris Kennaway
3229e71b7053SJung-uk Kimsub compiler_predefined {
3230e71b7053SJung-uk Kim    state %predefined;
3231e71b7053SJung-uk Kim    my $cc = shift;
3232e71b7053SJung-uk Kim
3233e71b7053SJung-uk Kim    return () if $^O eq 'VMS';
3234e71b7053SJung-uk Kim
3235e71b7053SJung-uk Kim    die 'compiler_predefined called without a compiler command'
3236e71b7053SJung-uk Kim        unless $cc;
3237e71b7053SJung-uk Kim
3238e71b7053SJung-uk Kim    if (! $predefined{$cc}) {
3239e71b7053SJung-uk Kim
3240e71b7053SJung-uk Kim        $predefined{$cc} = {};
3241e71b7053SJung-uk Kim
3242e71b7053SJung-uk Kim        # collect compiler pre-defines from gcc or gcc-alike...
3243e71b7053SJung-uk Kim        open(PIPE, "$cc -dM -E -x c /dev/null 2>&1 |");
3244e71b7053SJung-uk Kim        while (my $l = <PIPE>) {
3245e71b7053SJung-uk Kim            $l =~ m/^#define\s+(\w+(?:\(\w+\))?)(?:\s+(.+))?/ or last;
3246e71b7053SJung-uk Kim            $predefined{$cc}->{$1} = $2 // '';
3247e71b7053SJung-uk Kim        }
3248e71b7053SJung-uk Kim        close(PIPE);
3249e71b7053SJung-uk Kim    }
3250e71b7053SJung-uk Kim
3251e71b7053SJung-uk Kim    return %{$predefined{$cc}};
3252e71b7053SJung-uk Kim}
3253e71b7053SJung-uk Kim
325474664626SKris Kennawaysub which
325574664626SKris Kennaway{
325674664626SKris Kennaway    my ($name)=@_;
3257e71b7053SJung-uk Kim
3258e71b7053SJung-uk Kim    if (eval { require IPC::Cmd; 1; }) {
3259e71b7053SJung-uk Kim        IPC::Cmd->import();
3260e71b7053SJung-uk Kim        return scalar IPC::Cmd::can_run($name);
3261e71b7053SJung-uk Kim    } else {
3262e71b7053SJung-uk Kim        # if there is $directories component in splitpath,
3263e71b7053SJung-uk Kim        # then it's not something to test with $PATH...
3264e71b7053SJung-uk Kim        return $name if (File::Spec->splitpath($name))[1];
3265e71b7053SJung-uk Kim
3266e71b7053SJung-uk Kim        foreach (File::Spec->path()) {
3267e71b7053SJung-uk Kim            my $fullpath = catfile($_, "$name$target{exe_extension}");
3268e71b7053SJung-uk Kim            if (-f $fullpath and -x $fullpath) {
3269e71b7053SJung-uk Kim                return $fullpath;
3270e71b7053SJung-uk Kim            }
327174664626SKris Kennaway        }
327274664626SKris Kennaway    }
327374664626SKris Kennaway}
327474664626SKris Kennaway
3275e71b7053SJung-uk Kimsub env
327674664626SKris Kennaway{
3277e71b7053SJung-uk Kim    my $name = shift;
3278e71b7053SJung-uk Kim    my %opts = @_;
327974664626SKris Kennaway
3280e71b7053SJung-uk Kim    unless ($opts{cacheonly}) {
3281e71b7053SJung-uk Kim        # Note that if $ENV{$name} doesn't exist or is undefined,
3282e71b7053SJung-uk Kim        # $config{perlenv}->{$name} will be created with the value
3283e71b7053SJung-uk Kim        # undef.  This is intentional.
3284e71b7053SJung-uk Kim
3285e71b7053SJung-uk Kim        $config{perlenv}->{$name} = $ENV{$name}
3286e71b7053SJung-uk Kim            if ! exists $config{perlenv}->{$name};
328774664626SKris Kennaway    }
3288e71b7053SJung-uk Kim    return $config{perlenv}->{$name};
328974664626SKris Kennaway}
329074664626SKris Kennaway
3291e71b7053SJung-uk Kim# Configuration printer ##############################################
3292e71b7053SJung-uk Kim
329374664626SKris Kennawaysub print_table_entry
329474664626SKris Kennaway{
3295e71b7053SJung-uk Kim    local $now_printing = shift;
3296e71b7053SJung-uk Kim    my %target = resolve_config($now_printing);
3297e71b7053SJung-uk Kim    my $type = shift;
329874664626SKris Kennaway
3299e71b7053SJung-uk Kim    # Don't print the templates
3300e71b7053SJung-uk Kim    return if $target{template};
330174664626SKris Kennaway
3302e71b7053SJung-uk Kim    my @sequence = (
3303e71b7053SJung-uk Kim        "sys_id",
3304e71b7053SJung-uk Kim        "cpp",
3305e71b7053SJung-uk Kim        "cppflags",
3306e71b7053SJung-uk Kim        "defines",
3307e71b7053SJung-uk Kim        "includes",
3308e71b7053SJung-uk Kim        "cc",
3309e71b7053SJung-uk Kim        "cflags",
3310e71b7053SJung-uk Kim        "ld",
3311e71b7053SJung-uk Kim        "lflags",
3312e71b7053SJung-uk Kim        "loutflag",
3313e71b7053SJung-uk Kim        "ex_libs",
3314e71b7053SJung-uk Kim        "bn_ops",
3315*b077aed3SPierre Pronchery        "enable",
3316*b077aed3SPierre Pronchery        "disable",
3317e71b7053SJung-uk Kim        "poly1035_asm_src",
3318e71b7053SJung-uk Kim        "thread_scheme",
3319e71b7053SJung-uk Kim        "perlasm_scheme",
3320e71b7053SJung-uk Kim        "dso_scheme",
3321e71b7053SJung-uk Kim        "shared_target",
3322e71b7053SJung-uk Kim        "shared_cflag",
3323e71b7053SJung-uk Kim        "shared_defines",
3324e71b7053SJung-uk Kim        "shared_ldflag",
3325e71b7053SJung-uk Kim        "shared_rcflag",
3326e71b7053SJung-uk Kim        "shared_extension",
3327e71b7053SJung-uk Kim        "dso_extension",
3328e71b7053SJung-uk Kim        "obj_extension",
3329e71b7053SJung-uk Kim        "exe_extension",
3330e71b7053SJung-uk Kim        "ranlib",
3331e71b7053SJung-uk Kim        "ar",
3332e71b7053SJung-uk Kim        "arflags",
3333e71b7053SJung-uk Kim        "aroutflag",
3334e71b7053SJung-uk Kim        "rc",
3335e71b7053SJung-uk Kim        "rcflags",
3336e71b7053SJung-uk Kim        "rcoutflag",
3337e71b7053SJung-uk Kim        "mt",
3338e71b7053SJung-uk Kim        "mtflags",
3339e71b7053SJung-uk Kim        "mtinflag",
3340e71b7053SJung-uk Kim        "mtoutflag",
3341e71b7053SJung-uk Kim        "multilib",
3342e71b7053SJung-uk Kim        "build_scheme",
3343e71b7053SJung-uk Kim        );
334474664626SKris Kennaway
3345e71b7053SJung-uk Kim    if ($type eq "TABLE") {
3346e71b7053SJung-uk Kim        print "\n";
3347e71b7053SJung-uk Kim        print "*** $now_printing\n";
3348e71b7053SJung-uk Kim        foreach (@sequence) {
3349e71b7053SJung-uk Kim            if (ref($target{$_}) eq "ARRAY") {
3350e71b7053SJung-uk Kim                printf "\$%-12s = %s\n", $_, join(" ", @{$target{$_}});
3351e71b7053SJung-uk Kim            } else {
3352e71b7053SJung-uk Kim                printf "\$%-12s = %s\n", $_, $target{$_};
3353c1803d78SJacques Vidrine            }
3354c1803d78SJacques Vidrine        }
3355e71b7053SJung-uk Kim    } elsif ($type eq "HASH") {
3356e71b7053SJung-uk Kim        my $largest =
3357e71b7053SJung-uk Kim            length((sort { length($a) <=> length($b) } @sequence)[-1]);
3358e71b7053SJung-uk Kim        print "    '$now_printing' => {\n";
3359e71b7053SJung-uk Kim        foreach (@sequence) {
3360e71b7053SJung-uk Kim            if ($target{$_}) {
3361e71b7053SJung-uk Kim                if (ref($target{$_}) eq "ARRAY") {
3362e71b7053SJung-uk Kim                    print "      '",$_,"'"," " x ($largest - length($_))," => [ ",join(", ", map { "'$_'" } @{$target{$_}})," ],\n";
3363e71b7053SJung-uk Kim                } else {
3364e71b7053SJung-uk Kim                    print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
3365e71b7053SJung-uk Kim                }
3366e71b7053SJung-uk Kim            }
3367e71b7053SJung-uk Kim        }
3368e71b7053SJung-uk Kim        print "    },\n";
3369e71b7053SJung-uk Kim    }
3370c1803d78SJacques Vidrine}
33716cf8931aSJung-uk Kim
3372e71b7053SJung-uk Kim# Utility routines ###################################################
3373e71b7053SJung-uk Kim
3374e71b7053SJung-uk Kim# On VMS, if the given file is a logical name, File::Spec::Functions
3375e71b7053SJung-uk Kim# will consider it an absolute path.  There are cases when we want a
3376e71b7053SJung-uk Kim# purely syntactic check without checking the environment.
3377e71b7053SJung-uk Kimsub isabsolute {
3378e71b7053SJung-uk Kim    my $file = shift;
3379e71b7053SJung-uk Kim
3380e71b7053SJung-uk Kim    # On non-platforms, we just use file_name_is_absolute().
3381e71b7053SJung-uk Kim    return file_name_is_absolute($file) unless $^O eq "VMS";
3382e71b7053SJung-uk Kim
3383e71b7053SJung-uk Kim    # If the file spec includes a device or a directory spec,
3384e71b7053SJung-uk Kim    # file_name_is_absolute() is perfectly safe.
3385e71b7053SJung-uk Kim    return file_name_is_absolute($file) if $file =~ m|[:\[]|;
3386e71b7053SJung-uk Kim
3387e71b7053SJung-uk Kim    # Here, we know the given file spec isn't absolute
3388e71b7053SJung-uk Kim    return 0;
3389e71b7053SJung-uk Kim}
3390e71b7053SJung-uk Kim
3391e71b7053SJung-uk Kim# Makes a directory absolute and cleans out /../ in paths like foo/../bar
3392e71b7053SJung-uk Kim# On some platforms, this uses rel2abs(), while on others, realpath() is used.
3393e71b7053SJung-uk Kim# realpath() requires that at least all path components except the last is an
3394e71b7053SJung-uk Kim# existing directory.  On VMS, the last component of the directory spec must
3395e71b7053SJung-uk Kim# exist.
3396e71b7053SJung-uk Kimsub absolutedir {
3397e71b7053SJung-uk Kim    my $dir = shift;
3398e71b7053SJung-uk Kim
3399e71b7053SJung-uk Kim    # realpath() is quite buggy on VMS.  It uses LIB$FID_TO_NAME, which
3400e71b7053SJung-uk Kim    # will return the volume name for the device, no matter what.  Also,
3401e71b7053SJung-uk Kim    # it will return an incorrect directory spec if the argument is a
3402e71b7053SJung-uk Kim    # directory that doesn't exist.
3403e71b7053SJung-uk Kim    if ($^O eq "VMS") {
3404e71b7053SJung-uk Kim        return rel2abs($dir);
3405e71b7053SJung-uk Kim    }
3406e71b7053SJung-uk Kim
3407e71b7053SJung-uk Kim    # We use realpath() on Unix, since no other will properly clean out
3408e71b7053SJung-uk Kim    # a directory spec.
3409e71b7053SJung-uk Kim    use Cwd qw/realpath/;
3410e71b7053SJung-uk Kim
3411e71b7053SJung-uk Kim    return realpath($dir);
3412e71b7053SJung-uk Kim}
3413e71b7053SJung-uk Kim
341458f35182SJung-uk Kim# Check if all paths are one and the same, using stat.  They must both exist
341558f35182SJung-uk Kim# We need this for the cases when File::Spec doesn't detect case insensitivity
341658f35182SJung-uk Kim# (File::Spec::Unix assumes case sensitivity)
341758f35182SJung-uk Kimsub samedir {
341858f35182SJung-uk Kim    die "samedir expects two arguments\n" unless scalar @_ == 2;
341958f35182SJung-uk Kim
342058f35182SJung-uk Kim    my @stat0 = stat($_[0]);    # First argument
342158f35182SJung-uk Kim    my @stat1 = stat($_[1]);    # Second argument
342258f35182SJung-uk Kim
342358f35182SJung-uk Kim    die "Couldn't stat $_[0]" unless @stat0;
342458f35182SJung-uk Kim    die "Couldn't stat $_[1]" unless @stat1;
342558f35182SJung-uk Kim
342658f35182SJung-uk Kim    # Compare device number
342758f35182SJung-uk Kim    return 0 unless ($stat0[0] == $stat1[0]);
342858f35182SJung-uk Kim    # Compare "inode".  The perl manual recommends comparing as
342958f35182SJung-uk Kim    # string rather than as number.
343058f35182SJung-uk Kim    return 0 unless ($stat0[1] eq $stat1[1]);
343158f35182SJung-uk Kim
343258f35182SJung-uk Kim    return 1;                   # All the same
343358f35182SJung-uk Kim}
343458f35182SJung-uk Kim
3435e71b7053SJung-uk Kimsub quotify {
3436e71b7053SJung-uk Kim    my %processors = (
3437e71b7053SJung-uk Kim        perl    => sub { my $x = shift;
3438e71b7053SJung-uk Kim                         $x =~ s/([\\\$\@"])/\\$1/g;
3439e71b7053SJung-uk Kim                         return '"'.$x.'"'; },
3440e71b7053SJung-uk Kim        maybeshell => sub { my $x = shift;
3441e71b7053SJung-uk Kim                            (my $y = $x) =~ s/([\\\"])/\\$1/g;
3442e71b7053SJung-uk Kim                            if ($x ne $y || $x =~ m|\s|) {
3443e71b7053SJung-uk Kim                                return '"'.$y.'"';
3444e71b7053SJung-uk Kim                            } else {
3445e71b7053SJung-uk Kim                                return $x;
3446e71b7053SJung-uk Kim                            }
3447e71b7053SJung-uk Kim                        },
3448e71b7053SJung-uk Kim        );
3449e71b7053SJung-uk Kim    my $for = shift;
3450e71b7053SJung-uk Kim    my $processor =
3451e71b7053SJung-uk Kim        defined($processors{$for}) ? $processors{$for} : sub { shift; };
3452e71b7053SJung-uk Kim
3453e71b7053SJung-uk Kim    return map { $processor->($_); } @_;
3454e71b7053SJung-uk Kim}
3455e71b7053SJung-uk Kim
3456e71b7053SJung-uk Kim# collect_from_file($filename, $line_concat_cond_re, $line_concat)
3457e71b7053SJung-uk Kim# $filename is a file name to read from
3458e71b7053SJung-uk Kim# $line_concat_cond_re is a regexp detecting a line continuation ending
3459e71b7053SJung-uk Kim# $line_concat is a CODEref that takes care of concatenating two lines
3460e71b7053SJung-uk Kimsub collect_from_file {
3461e71b7053SJung-uk Kim    my $filename = shift;
3462e71b7053SJung-uk Kim    my $line_concat_cond_re = shift;
3463e71b7053SJung-uk Kim    my $line_concat = shift;
3464e71b7053SJung-uk Kim
3465e71b7053SJung-uk Kim    open my $fh, $filename || die "unable to read $filename: $!\n";
3466e71b7053SJung-uk Kim    return sub {
3467e71b7053SJung-uk Kim        my $saved_line = "";
3468e71b7053SJung-uk Kim        $_ = "";
3469e71b7053SJung-uk Kim        while (<$fh>) {
3470e71b7053SJung-uk Kim            s|\R$||;
3471e71b7053SJung-uk Kim            if (defined $line_concat) {
3472e71b7053SJung-uk Kim                $_ = $line_concat->($saved_line, $_);
3473e71b7053SJung-uk Kim                $saved_line = "";
3474e71b7053SJung-uk Kim            }
3475e71b7053SJung-uk Kim            if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
3476e71b7053SJung-uk Kim                $saved_line = $_;
3477e71b7053SJung-uk Kim                next;
3478e71b7053SJung-uk Kim            }
3479e71b7053SJung-uk Kim            return $_;
3480e71b7053SJung-uk Kim        }
3481e71b7053SJung-uk Kim        die "$filename ending with continuation line\n" if $_;
3482e71b7053SJung-uk Kim        close $fh;
3483e71b7053SJung-uk Kim        return undef;
3484e71b7053SJung-uk Kim    }
3485e71b7053SJung-uk Kim}
3486e71b7053SJung-uk Kim
3487e71b7053SJung-uk Kim# collect_from_array($array, $line_concat_cond_re, $line_concat)
3488e71b7053SJung-uk Kim# $array is an ARRAYref of lines
3489e71b7053SJung-uk Kim# $line_concat_cond_re is a regexp detecting a line continuation ending
3490e71b7053SJung-uk Kim# $line_concat is a CODEref that takes care of concatenating two lines
3491e71b7053SJung-uk Kimsub collect_from_array {
3492e71b7053SJung-uk Kim    my $array = shift;
3493e71b7053SJung-uk Kim    my $line_concat_cond_re = shift;
3494e71b7053SJung-uk Kim    my $line_concat = shift;
3495e71b7053SJung-uk Kim    my @array = (@$array);
3496e71b7053SJung-uk Kim
3497e71b7053SJung-uk Kim    return sub {
3498e71b7053SJung-uk Kim        my $saved_line = "";
3499e71b7053SJung-uk Kim        $_ = "";
3500e71b7053SJung-uk Kim        while (defined($_ = shift @array)) {
3501e71b7053SJung-uk Kim            s|\R$||;
3502e71b7053SJung-uk Kim            if (defined $line_concat) {
3503e71b7053SJung-uk Kim                $_ = $line_concat->($saved_line, $_);
3504e71b7053SJung-uk Kim                $saved_line = "";
3505e71b7053SJung-uk Kim            }
3506e71b7053SJung-uk Kim            if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
3507e71b7053SJung-uk Kim                $saved_line = $_;
3508e71b7053SJung-uk Kim                next;
3509e71b7053SJung-uk Kim            }
3510e71b7053SJung-uk Kim            return $_;
3511e71b7053SJung-uk Kim        }
3512e71b7053SJung-uk Kim        die "input text ending with continuation line\n" if $_;
3513e71b7053SJung-uk Kim        return undef;
3514e71b7053SJung-uk Kim    }
3515e71b7053SJung-uk Kim}
3516e71b7053SJung-uk Kim
3517e71b7053SJung-uk Kim# collect_information($lineiterator, $line_continue, $regexp => $CODEref, ...)
3518e71b7053SJung-uk Kim# $lineiterator is a CODEref that delivers one line at a time.
3519e71b7053SJung-uk Kim# All following arguments are regex/CODEref pairs, where the regexp detects a
3520e71b7053SJung-uk Kim# line and the CODEref does something with the result of the regexp.
3521e71b7053SJung-uk Kimsub collect_information {
3522e71b7053SJung-uk Kim    my $lineiterator = shift;
3523e71b7053SJung-uk Kim    my %collectors = @_;
3524e71b7053SJung-uk Kim
3525e71b7053SJung-uk Kim    while(defined($_ = $lineiterator->())) {
3526e71b7053SJung-uk Kim        s|\R$||;
3527e71b7053SJung-uk Kim        my $found = 0;
3528e71b7053SJung-uk Kim        if ($collectors{"BEFORE"}) {
3529e71b7053SJung-uk Kim            $collectors{"BEFORE"}->($_);
3530e71b7053SJung-uk Kim        }
3531e71b7053SJung-uk Kim        foreach my $re (keys %collectors) {
3532e71b7053SJung-uk Kim            if ($re !~ /^OTHERWISE|BEFORE|AFTER$/ && /$re/) {
3533e71b7053SJung-uk Kim                $collectors{$re}->($lineiterator);
3534e71b7053SJung-uk Kim                $found = 1;
3535e71b7053SJung-uk Kim            };
3536e71b7053SJung-uk Kim        }
3537e71b7053SJung-uk Kim        if ($collectors{"OTHERWISE"}) {
3538e71b7053SJung-uk Kim            $collectors{"OTHERWISE"}->($lineiterator, $_)
3539e71b7053SJung-uk Kim                unless $found || !defined $collectors{"OTHERWISE"};
3540e71b7053SJung-uk Kim        }
3541e71b7053SJung-uk Kim        if ($collectors{"AFTER"}) {
3542e71b7053SJung-uk Kim            $collectors{"AFTER"}->($_);
3543e71b7053SJung-uk Kim        }
3544e71b7053SJung-uk Kim    }
3545e71b7053SJung-uk Kim}
3546e71b7053SJung-uk Kim
3547e71b7053SJung-uk Kim# tokenize($line)
3548*b077aed3SPierre Pronchery# tokenize($line,$separator)
3549e71b7053SJung-uk Kim# $line is a line of text to split up into tokens
3550*b077aed3SPierre Pronchery# $separator [optional] is a regular expression that separates the tokens,
3551*b077aed3SPierre Pronchery# the default being spaces.  Do not use quotes of any kind as separators,
3552*b077aed3SPierre Pronchery# that will give undefined results.
3553*b077aed3SPierre Pronchery# Returns a list of tokens.
3554e71b7053SJung-uk Kim#
3555*b077aed3SPierre Pronchery# Tokens are divided by separator (spaces by default).  If the tokens include
3556*b077aed3SPierre Pronchery# the separators, they have to be quoted with single or double quotes.
3557*b077aed3SPierre Pronchery# Double quotes inside a double quoted token must be escaped.  Escaping is done
3558e71b7053SJung-uk Kim# with backslash.
3559e71b7053SJung-uk Kim# Basically, the same quoting rules apply for " and ' as in any
3560e71b7053SJung-uk Kim# Unix shell.
3561e71b7053SJung-uk Kimsub tokenize {
3562e71b7053SJung-uk Kim    my $line = my $debug_line = shift;
3563*b077aed3SPierre Pronchery    my $separator = shift // qr|\s+|;
3564e71b7053SJung-uk Kim    my @result = ();
3565e71b7053SJung-uk Kim
3566*b077aed3SPierre Pronchery    if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
3567*b077aed3SPierre Pronchery        print STDERR "DEBUG[tokenize]: \$separator = $separator\n";
3568*b077aed3SPierre Pronchery    }
3569*b077aed3SPierre Pronchery
3570*b077aed3SPierre Pronchery    while ($line =~ s|^${separator}||, $line ne "") {
3571e71b7053SJung-uk Kim        my $token = "";
3572*b077aed3SPierre Pronchery    again:
3573*b077aed3SPierre Pronchery        $line =~ m/^(.*?)(${separator}|"|'|$)/;
3574*b077aed3SPierre Pronchery        $token .= $1;
3575*b077aed3SPierre Pronchery        $line = $2.$';
3576*b077aed3SPierre Pronchery
3577e71b7053SJung-uk Kim        if ($line =~ m/^"((?:[^"\\]+|\\.)*)"/) {
3578e71b7053SJung-uk Kim            $token .= $1;
3579e71b7053SJung-uk Kim            $line = $';
3580*b077aed3SPierre Pronchery            goto again;
3581e71b7053SJung-uk Kim        } elsif ($line =~ m/^'([^']*)'/) {
3582e71b7053SJung-uk Kim            $token .= $1;
3583e71b7053SJung-uk Kim            $line = $';
3584*b077aed3SPierre Pronchery            goto again;
3585e71b7053SJung-uk Kim        }
3586e71b7053SJung-uk Kim        push @result, $token;
3587e71b7053SJung-uk Kim    }
3588e71b7053SJung-uk Kim
3589e71b7053SJung-uk Kim    if ($ENV{CONFIGURE_DEBUG_TOKENIZE}) {
3590e71b7053SJung-uk Kim        print STDERR "DEBUG[tokenize]: Parsed '$debug_line' into:\n";
3591e71b7053SJung-uk Kim        print STDERR "DEBUG[tokenize]: ('", join("', '", @result), "')\n";
3592e71b7053SJung-uk Kim    }
3593e71b7053SJung-uk Kim    return @result;
35946cf8931aSJung-uk Kim}
3595