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