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