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