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