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