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