Switch to MAJOR.MINOR.PATCH versioning and version 3.0.0-dev
[openssl.git] / Configure
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3 # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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
9
10 ##  Configure -- OpenSSL source tree configuration script
11
12 use 5.10.0;
13 use strict;
14 use Config;
15 use FindBin;
16 use lib "$FindBin::Bin/util/perl";
17 use File::Basename;
18 use File::Spec::Functions qw/:DEFAULT abs2rel rel2abs splitdir/;
19 use File::Path qw/mkpath/;
20 use OpenSSL::Glob;
21
22 # see INSTALL for instructions.
23
24 my $orig_death_handler = $SIG{__DIE__};
25 $SIG{__DIE__} = \&death_handler;
26
27 my $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";
28
29 # Options:
30 #
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.
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)
43 #
44 # --cross-compile-prefix Add specified prefix to binutils components.
45 #
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 #
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.
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)
57 # [no-]shared   [don't] try to create shared libraries when supported.
58 # [no-]pic      [don't] try to build position independent code when supported.
59 #               If disabled, it also disables shared and dynamic-engine.
60 # no-asm        do not use assembler
61 # no-dso        do not compile in any native shared-library methods. This
62 #               will ensure that all methods just return NULL.
63 # no-egd        do not compile support for the entropy-gathering daemon APIs
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.
67 # sctp          include SCTP support
68 # enable-weak-ssl-ciphers
69 #               Enable weak ciphers that are disabled by default.
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
73 # no-<cipher>   build without specified algorithm (rsa, idea, rc5, ...)
74 # -<xxx> +<xxx> compiler options are passed through
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.
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.
95 # BN_LLONG      use the type 'long long' in crypto/bn/bn.h
96 # RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
97 # Following are set automatically by this script
98 #
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,
102 # SHA256_ASM    sha256_block is implemented in assembler
103 # SHA512_ASM    sha512_block is implemented in assembler
104 # AES_ASM       AES_[en|de]crypt is implemented in assembler
105
106 # Minimum warning options... any contributions to OpenSSL should at least get
107 # past these.
108
109 # DEBUG_UNUSED enables __owur (warn unused result) checks.
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
122 my $gcc_devteam_warn = "-DDEBUG_UNUSED"
123         . " -DPEDANTIC -pedantic -Wno-long-long"
124         . " -Wall"
125         . " -Wextra"
126         . " -Wno-unused-parameter"
127         . " -Wno-missing-field-initializers"
128         . " -Wswitch"
129         . " -Wsign-compare"
130         . " -Wmissing-prototypes"
131         . " -Wstrict-prototypes"
132         . " -Wshadow"
133         . " -Wformat"
134         . " -Wtype-limits"
135         . " -Wundef"
136         . " -Werror"
137         ;
138
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
141 # following warnings can also be enabled:
142 #       -Wcast-align
143 #       -Wunreachable-code -- no, too ugly/compiler-specific
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
147 my $clang_devteam_warn = ""
148         . " -Wswitch-default"
149         . " -Wno-parentheses-equality"
150         . " -Wno-language-extension-token"
151         . " -Wno-extended-offsetof"
152         . " -Wconditional-uninitialized"
153         . " -Wincompatible-pointer-types-discards-qualifiers"
154         . " -Wmissing-variable-declarations"
155         . " -Wno-unknown-warning-option"
156         ;
157
158 # This adds backtrace information to the memory leak info.  Is only used
159 # when crypto-mdebug-backtrace is enabled.
160 my $memleak_devteam_backtrace = "-rdynamic";
161
162 my $strict_warnings = 0;
163
164 # As for $BSDthreads. Idea is to maintain "collective" set of flags,
165 # which would cover all BSD flavors. -pthread applies to them all,
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?
171 our $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
172
173 #
174 # API compatibility name to version number mapping.
175 #
176 my $maxapi = "1.1.0";           # API for "no-deprecated" builds
177 my $apitable = {
178     "1.1.0" => "0x10100000L",
179     "1.0.0" => "0x10000000L",
180     "0.9.8" => "0x00908000L",
181 };
182
183 our %table = ();
184 our %config = ();
185 our %withargs = ();
186 our $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)
190
191 # Forward declarations ###############################################
192
193 # read_config(filename)
194 #
195 # Reads a configuration file and populates %table with the contents
196 # (which the configuration file places in %targets).
197 sub read_config;
198
199 # resolve_config(target)
200 #
201 # Resolves all the late evaluations, inheritances and so on for the
202 # chosen target and any target it inherits from.
203 sub resolve_config;
204
205
206 # Information collection #############################################
207
208 # Unified build supports separate build dir
209 my $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
210 my $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
211 my $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
212
213 my $local_config_envname = 'OPENSSL_LOCAL_CONFIG_DIR';
214
215 $config{sourcedir} = abs2rel($srcdir);
216 $config{builddir} = abs2rel($blddir);
217
218 # Collect reconfiguration information if needed
219 my @argvcopy=@ARGV;
220
221 if (grep /^reconf(igure)?$/, @argvcopy) {
222     die "reconfiguring with other arguments present isn't supported"
223         if scalar @argvcopy > 1;
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));
236         $config{perlenv} = $configdata::config{perlenv} // {};
237     } else {
238         die "Insufficient data to reconfigure, please do a normal configuration\n";
239     }
240 }
241
242 $config{perlargv} = [ @argvcopy ];
243
244 # Collect version numbers
245 $config{major} = "unknown";
246 $config{minor} = "unknown";
247 $config{patch} = "unknown";
248 $config{prerelease} = "";
249 $config{build_metadata} = "";
250 $config{shlib_version} = "unknown";
251
252 collect_information(
253     collect_from_file(catfile($srcdir,'include/openssl/opensslv.h')),
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; },
266     );
267 die "erroneous version information in opensslv.h: ",
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");
273
274 # Collect target configurations
275
276 my $pattern = catfile(dirname($0), "Configurations", "*.conf");
277 foreach (sort glob($pattern)) {
278     &read_config($_);
279 }
280
281 if (defined env($local_config_envname)) {
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 {
287         $pattern = catfile(env($local_config_envname), '*.conf');
288     }
289
290     foreach (sort glob($pattern)) {
291         &read_config($_);
292     }
293 }
294
295 # Save away perl command information
296 $config{perl_cmd} = $^X;
297 $config{perl_version} = $Config{version};
298 $config{perl_archname} = $Config{archname};
299
300 $config{prefix}="";
301 $config{openssldir}="";
302 $config{processor}="";
303 $config{libdir}="";
304 my $auto_threads=1;    # enable threads automatically? true by default
305 my $default_ranlib;
306
307 # Known TLS and DTLS protocols
308 my @tls = qw(ssl3 tls1 tls1_1 tls1_2 tls1_3);
309 my @dtls = qw(dtls1 dtls1_2);
310
311 # Explicitly known options that are possible to disable.  They can
312 # be regexps, and will be used like this: /^no-${option}$/
313 # For developers: keep it sorted alphabetically
314
315 my @disablables = (
316     "afalgeng",
317     "aria",
318     "asan",
319     "asm",
320     "async",
321     "autoalginit",
322     "autoerrinit",
323     "autoload-config",
324     "bf",
325     "blake2",
326     "camellia",
327     "capieng",
328     "cast",
329     "chacha",
330     "cmac",
331     "cms",
332     "comp",
333     "crypto-mdebug",
334     "crypto-mdebug-backtrace",
335     "ct",
336     "deprecated",
337     "des",
338     "devcryptoeng",
339     "dgram",
340     "dh",
341     "dsa",
342     "dso",
343     "dtls",
344     "dynamic-engine",
345     "ec",
346     "ec2m",
347     "ecdh",
348     "ecdsa",
349     "ec_nistp_64_gcc_128",
350     "egd",
351     "engine",
352     "err",
353     "external-tests",
354     "filenames",
355     "fuzz-libfuzzer",
356     "fuzz-afl",
357     "gost",
358     "heartbeats",
359     "hw(-.+)?",
360     "idea",
361     "makedepend",
362     "md2",
363     "md4",
364     "mdc2",
365     "msan",
366     "multiblock",
367     "nextprotoneg",
368     "ocb",
369     "ocsp",
370     "pic",
371     "poly1305",
372     "posix-io",
373     "psk",
374     "rc2",
375     "rc4",
376     "rc5",
377     "rdrand",
378     "rfc3779",
379     "rmd160",
380     "scrypt",
381     "sctp",
382     "seed",
383     "shared",
384     "siphash",
385     "sm2",
386     "sm3",
387     "sm4",
388     "sock",
389     "srp",
390     "srtp",
391     "sse2",
392     "ssl",
393     "ssl-trace",
394     "static-engine",
395     "stdio",
396     "tests",
397     "threads",
398     "tls",
399     "ts",
400     "ubsan",
401     "ui-console",
402     "unit-test",
403     "whirlpool",
404     "weak-ssl-ciphers",
405     "zlib",
406     "zlib-dynamic",
407     );
408 foreach my $proto ((@tls, @dtls))
409         {
410         push(@disablables, $proto);
411         push(@disablables, "$proto-method") unless $proto eq "tls1_3";
412         }
413
414 my %deprecated_disablables = (
415     "ssl2" => undef,
416     "buf-freelists" => undef,
417     "ripemd" => "rmd160",
418     "ui" => "ui-console",
419     );
420
421 # All of the following are disabled by default:
422
423 our %disabled = ( # "what"         => "comment"
424                   "asan"                => "default",
425                   "crypto-mdebug"       => "default",
426                   "crypto-mdebug-backtrace" => "default",
427                   "devcryptoeng"        => "default",
428                   "ec_nistp_64_gcc_128" => "default",
429                   "egd"                 => "default",
430                   "external-tests"      => "default",
431                   "fuzz-libfuzzer"      => "default",
432                   "fuzz-afl"            => "default",
433                   "heartbeats"          => "default",
434                   "md2"                 => "default",
435                   "msan"                => "default",
436                   "rc5"                 => "default",
437                   "sctp"                => "default",
438                   "ssl-trace"           => "default",
439                   "ssl3"                => "default",
440                   "ssl3-method"         => "default",
441                   "ubsan"               => "default",
442                   "unit-test"           => "default",
443                   "weak-ssl-ciphers"    => "default",
444                   "zlib"                => "default",
445                   "zlib-dynamic"        => "default",
446                 );
447
448 # Note: => pair form used for aesthetics, not to truly make a hash table
449 my @disable_cascades = (
450     # "what"            => [ "cascade", ... ]
451     sub { $config{processor} eq "386" }
452                         => [ "sse2" ],
453     "ssl"               => [ "ssl3" ],
454     "ssl3-method"       => [ "ssl3" ],
455     "zlib"              => [ "zlib-dynamic" ],
456     "des"               => [ "mdc2" ],
457     "ec"                => [ "ecdsa", "ecdh" ],
458
459     "dgram"             => [ "dtls", "sctp" ],
460     "sock"              => [ "dgram" ],
461     "dtls"              => [ @dtls ],
462     sub { 0 == scalar grep { !$disabled{$_} } @dtls }
463                         => [ "dtls" ],
464
465     "tls"               => [ @tls ],
466     sub { 0 == scalar grep { !$disabled{$_} } @tls }
467                         => [ "tls" ],
468
469     "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
470
471     # Without DSO, we can't load dynamic engines, so don't build them dynamic
472     "dso"               => [ "dynamic-engine" ],
473
474     # Without position independent code, there can be no shared libraries or DSOs
475     "pic"               => [ "shared" ],
476     "shared"            => [ "dynamic-engine" ],
477     "engine"            => [ "afalgeng", "devcryptoeng" ],
478
479     # no-autoalginit is only useful when building non-shared
480     "autoalginit"       => [ "shared", "apps" ],
481
482     "stdio"             => [ "apps", "capieng", "egd" ],
483     "apps"              => [ "tests" ],
484     "tests"             => [ "external-tests" ],
485     "comp"              => [ "zlib" ],
486     "ec"                => [ "tls1_3", "sm2" ],
487     "sm3"               => [ "sm2" ],
488     sub { !$disabled{"unit-test"} } => [ "heartbeats" ],
489
490     sub { !$disabled{"msan"} } => [ "asm" ],
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 #
496 my @list = (reverse @tls);
497 while ((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 }
503 my @list = (reverse @dtls);
504 while ((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
511 # Explicit "no-..." options will be collected in %disabled along with the defaults.
512 # To remove something from %disabled, use "enable-foo".
513 # For symmetry, "disable-foo" is a synonym for "no-foo".
514
515 &usage if ($#ARGV < 0);
516
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.
522 my $list_separator_re =
523     { VMS           => qr/(?<!\^),/,
524       MSWin32       => qr/(?<!\\);/ } -> {$^O} // qr/(?<!\\)[:\s]/;
525 # All the "make variables" we support
526 # Some get pre-populated for the sake of backward compatibility
527 # (we supported those before the change to "make variable" support.
528 my %user = (
529     AR          => env('AR'),
530     ARFLAGS     => [],
531     AS          => undef,
532     ASFLAGS     => [],
533     CC          => env('CC'),
534     CFLAGS      => [],
535     CXX         => env('CXX'),
536     CXXFLAGS    => [],
537     CPP         => undef,
538     CPPFLAGS    => [],  # -D, -I, -Wp,
539     CPPDEFINES  => [],  # Alternative for -D
540     CPPINCLUDES => [],  # Alternative for -I
541     CROSS_COMPILE => env('CROSS_COMPILE'),
542     HASHBANGPERL=> env('HASHBANGPERL') || env('PERL'),
543     LD          => undef,
544     LDFLAGS     => [],  # -L, -Wl,
545     LDLIBS      => [],  # -l
546     MT          => undef,
547     MTFLAGS     => [],
548     PERL        => env('PERL') || ($^O ne "VMS" ? $^X : "perl"),
549     RANLIB      => env('RANLIB'),
550     RC          => env('RC') || env('WINDRES'),
551     RCFLAGS     => [],
552     RM          => undef,
553    );
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.
556 my @user_crossable = qw ( AR AS CC CXX CPP LD MT RANLIB RC );
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
560 my %useradd = (
561     CPPDEFINES  => [],
562     CPPINCLUDES => [],
563     CPPFLAGS    => [],
564     CFLAGS      => [],
565     CXXFLAGS    => [],
566     LDFLAGS     => [],
567     LDLIBS      => [],
568    );
569
570 my %user_synonyms = (
571     HASHBANGPERL=> 'PERL',
572     RC          => 'WINDRES',
573    );
574
575 # Some target attributes have been renamed, this is the translation table
576 my %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',
588    );
589
590 # Initialisers coming from 'config' scripts
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') || () ];
598
599 $config{openssl_api_defines}=[];
600 $config{openssl_sys_defines}=[];
601 $config{openssl_feature_defines}=[];
602 $config{options}="";
603 $config{build_type} = "release";
604 my $target="";
605
606 my %cmdvars = ();               # Stores FOO='blah' type arguments
607 my %unsupported_options = ();
608 my %deprecated_options = ();
609 # If you change this, update apps/version.c
610 my @known_seed_sources = qw(getrandom devrandom os egd none rdcpu librandom);
611 my @seed_sources = ();
612 while (@argvcopy)
613         {
614         $_ = shift @argvcopy;
615
616         # Support env variable assignments among the options
617         if (m|^(\w+)=(.+)?$|)
618                 {
619                 $cmdvars{$1} = $2;
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                         }
626                 #if (exists $useradd{$1})
627                 #       {
628                 #       $useradd{$1} = [];
629                 #       }
630                 next;
631                 }
632
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                 }
640
641         # some people just can't read the instructions, clang people have to...
642         s/^-no-(?!integrated-as)/no-/;
643
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/;
650
651         if (/^(no|disable|enable)-(.+)$/)
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                 {
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                                 }
677                         $disabled{"dtls"} = "option(dtls)";
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                         }
693                 elsif ($1 eq "static-engine")
694                         {
695                         delete $disabled{"dynamic-engine"};
696                         }
697                 elsif ($1 eq "dynamic-engine")
698                         {
699                         $disabled{"dynamic-engine"} = "option";
700                         }
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                         }
709                 else
710                         {
711                         $disabled{$1} = "option";
712                         }
713                 # No longer an automatic choice
714                 $auto_threads = 0 if ($1 eq "threads");
715                 }
716         elsif (/^enable-(.+)$/)
717                 {
718                 if ($1 eq "static-engine")
719                         {
720                         $disabled{"dynamic-engine"} = "option";
721                         }
722                 elsif ($1 eq "dynamic-engine")
723                         {
724                         delete $disabled{"dynamic-engine"};
725                         }
726                 elsif ($1 eq "zlib-dynamic")
727                         {
728                         delete $disabled{"zlib"};
729                         }
730                 my $algo = $1;
731                 delete $disabled{$algo};
732
733                 # No longer an automatic choice
734                 $auto_threads = 0 if ($1 eq "threads");
735                 }
736         elsif (/^--strict-warnings$/)
737                 {
738                 $strict_warnings = 1;
739                 }
740         elsif (/^--debug$/)
741                 {
742                 $config{build_type} = "debug";
743                 }
744         elsif (/^--release$/)
745                 {
746                 $config{build_type} = "release";
747                 }
748         elsif (/^386$/)
749                 { $config{processor}=386; }
750         elsif (/^fips$/)
751                 {
752                 die "FIPS mode not supported\n";
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                 {
762                 die "FIPS mode not supported\n";
763                 }
764         elsif (/^[-+]/)
765                 {
766                 if (/^--prefix=(.*)$/)
767                         {
768                         $config{prefix}=$1;
769                         die "Directory given with --prefix MUST be absolute\n"
770                                 unless file_name_is_absolute($config{prefix});
771                         }
772                 elsif (/^--api=(.*)$/)
773                         {
774                         $config{api}=$1;
775                         }
776                 elsif (/^--libdir=(.*)$/)
777                         {
778                         $config{libdir}=$1;
779                         }
780                 elsif (/^--openssldir=(.*)$/)
781                         {
782                         $config{openssldir}=$1;
783                         }
784                 elsif (/^--with-zlib-lib=(.*)$/)
785                         {
786                         $withargs{zlib_lib}=$1;
787                         }
788                 elsif (/^--with-zlib-include=(.*)$/)
789                         {
790                         $withargs{zlib_include}=$1;
791                         }
792                 elsif (/^--with-fuzzer-lib=(.*)$/)
793                         {
794                         $withargs{fuzzer_lib}=$1;
795                         }
796                 elsif (/^--with-fuzzer-include=(.*)$/)
797                         {
798                         $withargs{fuzzer_include}=$1;
799                         }
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                         }
809                 elsif (/^--cross-compile-prefix=(.*)$/)
810                         {
811                         $user{CROSS_COMPILE}=$1;
812                         }
813                 elsif (/^--config=(.*)$/)
814                         {
815                         read_config $1;
816                         }
817                 elsif (/^-l(.*)$/)
818                         {
819                         push @{$useradd{LDLIBS}}, $_;
820                         }
821                 elsif (/^-framework$/)
822                         {
823                         push @{$useradd{LDLIBS}}, $_, shift(@argvcopy);
824                         }
825                 elsif (/^-L(.*)$/ or /^-Wl,/)
826                         {
827                         push @{$useradd{LDFLAGS}}, $_;
828                         }
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 "";
835                         push @{$useradd{LDFLAGS}}, $_, $rpath;
836                         }
837                 elsif (/^-static$/)
838                         {
839                         push @{$useradd{LDFLAGS}}, $_;
840                         $disabled{"dso"} = "forced";
841                         $disabled{"pic"} = "forced";
842                         $disabled{"shared"} = "forced";
843                         $disabled{"threads"} = "forced";
844                         }
845                 elsif (/^-D(.*)$/)
846                         {
847                         push @{$useradd{CPPDEFINES}}, $1;
848                         }
849                 elsif (/^-I(.*)$/)
850                         {
851                         push @{$useradd{CPPINCLUDES}}, $1;
852                         }
853                 elsif (/^-Wp,$/)
854                         {
855                         push @{$useradd{CPPFLAGS}}, $1;
856                         }
857                 else    # common if (/^[-+]/), just pass down...
858                         {
859                         $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
860                         push @{$useradd{CFLAGS}}, $_;
861                         push @{$useradd{CXXFLAGS}}, $_;
862                         }
863                 }
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                 {
871                 # "no-..." follows later after implied deactivations
872                 # have been derived.  (Don't take this too seriously,
873                 # we really only write OPTIONS to the Makefile out of
874                 # nostalgia.)
875
876                 if ($config{options} eq "")
877                         { $config{options} = $_; }
878                 else
879                         { $config{options} .= " ".$_; }
880                 }
881         }
882
883 if (defined($config{api}) && !exists $apitable->{$config{api}}) {
884         die "***** Unsupported api compatibility level: $config{api}\n",
885 }
886
887 if (keys %deprecated_options)
888         {
889         warn "***** Deprecated options: ",
890                 join(", ", keys %deprecated_options), "\n";
891         }
892 if (keys %unsupported_options)
893         {
894         die "***** Unsupported options: ",
895                 join(", ", keys %unsupported_options), "\n";
896         }
897
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
900 # is set.
901 if (grep { scalar @$_ > 0 } values %useradd) {
902     # Hash of env / make variables names.  The possible values are:
903     # 1 - "make vars"
904     # 2 - %useradd entry set
905     # 3 - both set
906     my %detected_vars =
907         map { my $v = 0;
908               $v += 1 if $cmdvars{$_};
909               $v += 2 if @{$useradd{$_}};
910               $_ => $v }
911         keys %useradd;
912
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);
917         die <<"_____";
918 ***** Mixing make variables and additional compiler/linker flags as
919 ***** configure command line option is not permitted.
920 ***** Affected make variables: $names
921 _____
922     }
923 }
924
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.
929 my $anyuseradd =
930     grep { defined $_ && (ref $_ ne 'ARRAY' || @$_) } values %useradd;
931 foreach (keys %user) {
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;
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
948 if (grep { /-rpath\b/ } ($user{LDFLAGS} ? @{$user{LDFLAGS}} : ())
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
955 my @tocheckfor = (keys %disabled);
956 while (@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})) {
962             foreach(grep { !defined($disabled{$_}) } @$descendents) {
963                 $new_tocheckfor{$_} = 1; $disabled{$_} = "forced";
964             }
965         }
966     }
967     @tocheckfor = (keys %new_tocheckfor);
968 }
969
970 our $die = sub { die @_; };
971 if ($target eq "TABLE") {
972     local $die = sub { warn @_; };
973     foreach (sort keys %table) {
974         print_table_entry($_, "TABLE");
975     }
976     exit 0;
977 }
978
979 if ($target eq "LIST") {
980     foreach (sort keys %table) {
981         print $_,"\n" unless $table{$_}->{template};
982     }
983     exit 0;
984 }
985
986 if ($target eq "HASH") {
987     local $die = sub { warn @_; };
988     print "%table = (\n";
989     foreach (sort keys %table) {
990         print_table_entry($_, "HASH");
991     }
992     exit 0;
993 }
994
995 print "Configuring OpenSSL version $config{version} ($config{version_num}) ";
996 print "for $target\n";
997
998 if (scalar(@seed_sources) == 0) {
999     print "Using os-specific seed configuration\n";
1000     push @seed_sources, 'os';
1001 }
1002 if (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;
1005
1006 ============================== WARNING ===============================
1007 You have selected the --with-rand-seed=none option, which effectively
1008 disables automatic reseeding of the OpenSSL random generator.
1009 All operations depending on the random generator such as creating keys
1010 will not work unless the random generator is seeded manually by the
1011 application.
1012
1013 Please read the 'Note on random number generation' section in the
1014 INSTALL instructions and the RAND_DRBG(7) manual page for more details.
1015 ============================== WARNING ===============================
1016
1017 _____
1018 }
1019 push @{$config{openssl_feature_defines}},
1020      map { (my $x = $_) =~ tr|[\-a-z]|[_A-Z]|; "OPENSSL_RAND_SEED_$x" }
1021         @seed_sources;
1022
1023 # Backward compatibility?
1024 if ($target =~ m/^CygWin32(-.*)$/) {
1025     $target = "Cygwin".$1;
1026 }
1027
1028 # Support for legacy targets having a name starting with 'debug-'
1029 my ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
1030 if ($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 }
1038
1039 &usage if !$table{$target} || $table{$target}->{template};
1040
1041 $config{target} = $target;
1042 my %target = resolve_config($target);
1043
1044 foreach (keys %target_attr_translate) {
1045     $target{$target_attr_translate{$_}} = $target{$_}
1046         if $target{$_};
1047     delete $target{$_};
1048 }
1049
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
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};
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
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};
1082         foreach (sort keys %$si) {
1083             $target{$_} = defined $target{$_}
1084                 ? add($si->{$_})->($target{$_})
1085                 : $si->{$_};
1086         }
1087     }
1088 }
1089
1090 my %conf_files = map { $_ => 1 } (@{$target{_conf_fname_int}});
1091 $config{conf_files} = [ sort keys %conf_files ];
1092
1093 foreach 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 }
1101 foreach 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
1112 $target{CXXFLAGS}//=$target{CFLAGS} if $target{CXX};
1113 $target{cxxflags}//=$target{cflags} if $target{CXX};
1114 $target{exe_extension}="";
1115 $target{exe_extension}=".exe" if ($config{target} eq "DJGPP"
1116                                   || $config{target} =~ /^(?:Cygwin|mingw)/);
1117 $target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
1118 $target{def_extension}=".ld";
1119 $target{def_extension}=".def" if $config{target} =~ /^mingw|VC-/;
1120 $target{def_extension}=".opt" if $config{target} =~ /^vms/;
1121 ($target{shared_extension_simple}=$target{shared_extension})
1122     =~ s|\.\$\(SHLIB_VERSION_NUMBER\)||
1123     unless defined($target{shared_extension_simple});
1124 $target{dso_extension}//=$target{shared_extension_simple};
1125 ($target{shared_import_extension}=$target{shared_extension_simple}.".a")
1126     if ($config{target} =~ /^(?:Cygwin|mingw)/);
1127
1128 # Fill %config with values from %user, and in case those are undefined or
1129 # empty, use values from %target (acting as a default).
1130 foreach (keys %user) {
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
1156     $config{$_} =
1157         $mkvalue->($ref_type, $user{$_})
1158         || $mkvalue->($ref_type, $target{$_});
1159     delete $config{$_} unless defined $config{$_};
1160 }
1161
1162 # Allow overriding the build file name
1163 $config{build_file} = env('BUILDFILE') || $target{build_file} || "Makefile";
1164
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.
1177 my %skipdir = ();
1178 my %disabled_info = ();         # For configdata.pm
1179 foreach 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;
1186         my $skipdir = $what;
1187
1188         # fix-up crypto/directory name(s)
1189         $skipdir = "ripemd" if $what eq "rmd160";
1190         $skipdir = "whrlpool" if $what eq "whirlpool";
1191
1192         my $macro = $disabled_info{$what}->{macro} = "OPENSSL_NO_$WHAT";
1193         push @{$config{openssl_feature_defines}}, $macro;
1194
1195         $skipdir{engines} = $what if $what eq 'engine';
1196         $skipdir{"crypto/$skipdir"} = $what
1197             unless $what eq 'async' || $what eq 'err';
1198     }
1199 }
1200
1201 # Make sure build_scheme is consistent.
1202 $target{build_scheme} = [ $target{build_scheme} ]
1203     if ref($target{build_scheme}) ne "ARRAY";
1204
1205 my ($builder, $builder_platform, @builder_opts) =
1206     @{$target{build_scheme}};
1207
1208 foreach 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
1227 push @{$config{defines}}, "NDEBUG"    if $config{build_type} eq "release";
1228
1229 if ($target =~ /^mingw/ && `$config{CC} --target-help 2>&1` =~ m/-mno-cygwin/m)
1230         {
1231         push @{$config{cflags}}, "-mno-cygwin";
1232         push @{$config{cxxflags}}, "-mno-cygwin" if $config{CXX};
1233         push @{$config{shared_ldflag}}, "-mno-cygwin";
1234         }
1235
1236 if ($target =~ /linux.*-mips/ && !$disabled{asm}
1237         && !grep { $_ !~ /-m(ips|arch=)/ } (@{$user{CFLAGS}},
1238                                             @{$useradd{CFLAGS}})) {
1239         # minimally required architecture flags for assembly modules
1240         my $value;
1241         $value = '-mips2' if ($target =~ /mips32/);
1242         $value = '-mips3' if ($target =~ /mips64/);
1243         unshift @{$config{cflags}}, $value;
1244         unshift @{$config{cxxflags}}, $value if $config{CXX};
1245 }
1246
1247 # If threads aren't disabled, check how possible they are
1248 unless ($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 {
1255         # The user chose to enable threads explicitly, let's see
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.
1263             if (!@{$user{CFLAGS}} && !@{$useradd{CFLAGS}}
1264                     && !@{$user{CPPDEFINES}} && !@{$useradd{CPPDEFINES}}) {
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.
1274 unless($disabled{threads}) {
1275     push @{$config{openssl_feature_defines}}, "OPENSSL_THREADS";
1276 }
1277
1278 # With "deprecated" disable all deprecated features.
1279 if (defined($disabled{"deprecated"})) {
1280         $config{api} = $maxapi;
1281 }
1282
1283 my $no_shared_warn=0;
1284 if ($target{shared_target} eq "")
1285         {
1286         $no_shared_warn = 1
1287             if (!$disabled{shared} || !$disabled{"dynamic-engine"});
1288         $disabled{shared} = "no-shared-target";
1289         $disabled{pic} = $disabled{shared} = $disabled{"dynamic-engine"} =
1290             "no-shared-target";
1291         }
1292
1293 if ($disabled{"dynamic-engine"}) {
1294         push @{$config{openssl_feature_defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
1295         $config{dynamic_engines} = 0;
1296 } else {
1297         push @{$config{openssl_feature_defines}}, "OPENSSL_NO_STATIC_ENGINE";
1298         $config{dynamic_engines} = 1;
1299 }
1300
1301 unless ($disabled{asan}) {
1302     push @{$config{cflags}}, "-fsanitize=address";
1303     push @{$config{cxxflags}}, "-fsanitize=address" if $config{CXX};
1304 }
1305
1306 unless ($disabled{ubsan}) {
1307     # -DPEDANTIC or -fnosanitize=alignment may also be required on some
1308     # platforms.
1309     push @{$config{cflags}}, "-fsanitize=undefined", "-fno-sanitize-recover=all";
1310     push @{$config{cxxflags}}, "-fsanitize=undefined", "-fno-sanitize-recover=all"
1311         if $config{CXX};
1312 }
1313
1314 unless ($disabled{msan}) {
1315   push @{$config{cflags}}, "-fsanitize=memory";
1316   push @{$config{cxxflags}}, "-fsanitize=memory" if $config{CXX};
1317 }
1318
1319 unless ($disabled{"fuzz-libfuzzer"} && $disabled{"fuzz-afl"}
1320         && $disabled{asan} && $disabled{ubsan} && $disabled{msan}) {
1321     push @{$config{cflags}}, "-fno-omit-frame-pointer", "-g";
1322     push @{$config{cxxflags}}, "-fno-omit-frame-pointer", "-g" if $config{CXX};
1323 }
1324 #
1325 # Platform fix-ups
1326 #
1327
1328 # This saves the build files from having to check
1329 if ($disabled{pic})
1330         {
1331         foreach (qw(shared_cflag shared_cxxflag shared_cppflag
1332                     shared_defines shared_includes shared_ldflag
1333                     module_cflags module_cxxflags module_cppflags
1334                     module_defines module_includes module_lflags))
1335                 {
1336                 delete $config{$_};
1337                 $target{$_} = "";
1338                 }
1339         }
1340 else
1341         {
1342         push @{$config{lib_defines}}, "OPENSSL_PIC";
1343         }
1344
1345 if ($target{sys_id} ne "")
1346         {
1347         push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
1348         }
1349
1350 unless ($disabled{asm}) {
1351     $target{cpuid_asm_src}=$table{DEFAULTS}->{cpuid_asm_src} if ($config{processor} eq "386");
1352     push @{$config{lib_defines}}, "OPENSSL_CPUID_OBJ" if ($target{cpuid_asm_src} ne "mem_clr.c");
1353
1354     $target{bn_asm_src} =~ s/\w+-gf2m.c// if (defined($disabled{ec2m}));
1355
1356     # bn-586 is the only one implementing bn_*_part_words
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/);
1359
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/);
1363     push @{$config{lib_defines}}, "BN_DIV3W" if ($target{bn_asm_src} =~ /-div3w/);
1364
1365     if ($target{sha1_asm_src}) {
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/);
1369     }
1370     if ($target{keccak1600_asm_src} ne $table{DEFAULTS}->{keccak1600_asm_src}) {
1371         push @{$config{lib_defines}}, "KECCAK1600_ASM";
1372     }
1373     if ($target{rc4_asm_src} ne $table{DEFAULTS}->{rc4_asm_src}) {
1374         push @{$config{lib_defines}}, "RC4_ASM";
1375     }
1376     if ($target{md5_asm_src}) {
1377         push @{$config{lib_defines}}, "MD5_ASM";
1378     }
1379     $target{cast_asm_src}=$table{DEFAULTS}->{cast_asm_src} unless $disabled{pic}; # CAST assembler is not PIC
1380     if ($target{rmd160_asm_src}) {
1381         push @{$config{lib_defines}}, "RMD160_ASM";
1382     }
1383     if ($target{aes_asm_src}) {
1384         push @{$config{lib_defines}}, "AES_ASM" if ($target{aes_asm_src} =~ m/\baes-/);;
1385         # aes-ctr.fake is not a real file, only indication that assembler
1386         # module implements AES_ctr32_encrypt...
1387         push @{$config{lib_defines}}, "AES_CTR_ASM" if ($target{aes_asm_src} =~ s/\s*aes-ctr\.fake//);
1388         # aes-xts.fake indicates presence of AES_xts_[en|de]crypt...
1389         push @{$config{lib_defines}}, "AES_XTS_ASM" if ($target{aes_asm_src} =~ s/\s*aes-xts\.fake//);
1390         $target{aes_asm_src} =~ s/\s*(vpaes|aesni)-x86\.s//g if ($disabled{sse2});
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/);
1393     }
1394     if ($target{wp_asm_src} =~ /mmx/) {
1395         if ($config{processor} eq "386") {
1396             $target{wp_asm_src}=$table{DEFAULTS}->{wp_asm_src};
1397         } elsif (!$disabled{"whirlpool"}) {
1398             push @{$config{lib_defines}}, "WHIRLPOOL_ASM";
1399         }
1400     }
1401     if ($target{modes_asm_src} =~ /ghash-/) {
1402         push @{$config{lib_defines}}, "GHASH_ASM";
1403     }
1404     if ($target{ec_asm_src} =~ /ecp_nistz256/) {
1405         push @{$config{lib_defines}}, "ECP_NISTZ256_ASM";
1406     }
1407     if ($target{ec_asm_src} =~ /x25519/) {
1408         push @{$config{lib_defines}}, "X25519_ASM";
1409     }
1410     if ($target{padlock_asm_src} ne $table{DEFAULTS}->{padlock_asm_src}) {
1411         push @{$config{lib_defines}}, "PADLOCK_ASM";
1412     }
1413     if ($target{poly1305_asm_src} ne "") {
1414         push @{$config{lib_defines}}, "POLY1305_ASM";
1415     }
1416 }
1417
1418 my %predefined = compiler_predefined($config{CROSS_COMPILE}.$config{CC});
1419
1420 # Check for makedepend capabilities.
1421 if (!$disabled{makedepend}) {
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).
1426     } elsif (($predefined{__GNUC__} // -1) >= 3
1427              && !($predefined{__APPLE_CC__} && !$predefined{__clang__})) {
1428         # We know that GNU C version 3 and up as well as all clang
1429         # versions support dependency generation, but Xcode did not
1430         # handle $cc -M before clang support (but claims __GNUC__ = 3)
1431         $config{makedepprog} = "\$(CROSS_COMPILE)$config{CC}";
1432     } else {
1433         # In all other cases, we look for 'makedepend', and disable the
1434         # capability if not found.
1435         $config{makedepprog} = which('makedepend');
1436         $disabled{makedepend} = "unavailable" unless $config{makedepprog};
1437     }
1438 }
1439
1440 if (!$disabled{asm} && !$predefined{__MACH__} && $^O ne 'VMS') {
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";
1448     } else {
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 }
1461
1462 # Deal with bn_ops ###################################################
1463
1464 $config{bn_ll}                  =0;
1465 $config{export_var_as_fn}       =0;
1466 my $def_int="unsigned int";
1467 $config{rc4_int}                =$def_int;
1468 ($config{b64l},$config{b64},$config{b32})=(0,0,1);
1469
1470 my $count = 0;
1471 foreach (sort split(/\s+/,$target{bn_ops})) {
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';
1482 }
1483 die "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
1484     if $count > 1;
1485
1486
1487 # Hack cflags for better warnings (dev option) #######################
1488
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.
1491 $config{cflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1492                         @{$config{cflags}} ];
1493 $config{cxxflags} = [ map { (my $x = $_) =~ s/([\\\"])/\\$1/g; $x }
1494                           @{$config{cxxflags}} ] if $config{CXX};
1495
1496 if (defined($config{api})) {
1497     $config{openssl_api_defines} = [ "OPENSSL_MIN_API=".$apitable->{$config{api}} ];
1498     my $apiflag = sprintf("OPENSSL_API_COMPAT=%s", $apitable->{$config{api}});
1499     push @{$config{defines}}, $apiflag;
1500 }
1501
1502 if ($strict_warnings)
1503         {
1504         my $wopt;
1505         my $gccver = $predefined{__GNUC__} // -1;
1506
1507         die "ERROR --strict-warnings requires gcc[>=4] or gcc-alike"
1508             unless $gccver >= 4;
1509         foreach $wopt (split /\s+/, $gcc_devteam_warn)
1510                 {
1511                 push @{$config{cflags}}, $wopt
1512                         unless grep { $_ eq $wopt } @{$config{cflags}};
1513                 push @{$config{cxxflags}}, $wopt
1514                         if ($config{CXX}
1515                             && !grep { $_ eq $wopt } @{$config{cxxflags}});
1516                 }
1517         if (defined($predefined{__clang__}))
1518                 {
1519                 foreach $wopt (split /\s+/, $clang_devteam_warn)
1520                         {
1521                         push @{$config{cflags}}, $wopt
1522                                 unless grep { $_ eq $wopt } @{$config{cflags}};
1523                         push @{$config{cxxflags}}, $wopt
1524                                 if ($config{CXX}
1525                                     && !grep { $_ eq $wopt } @{$config{cxxflags}});
1526                         }
1527                 }
1528         }
1529
1530 unless ($disabled{"crypto-mdebug-backtrace"})
1531         {
1532         foreach my $wopt (split /\s+/, $memleak_devteam_backtrace)
1533                 {
1534                 push @{$config{cflags}}, $wopt
1535                         unless grep { $_ eq $wopt } @{$config{cflags}};
1536                 push @{$config{cxxflags}}, $wopt
1537                         if ($config{CXX}
1538                             && !grep { $_ eq $wopt } @{$config{cxxflags}});
1539                 }
1540         if ($target =~ /^BSD-/)
1541                 {
1542                 push @{$config{ex_libs}}, "-lexecinfo";
1543                 }
1544         }
1545
1546 unless ($disabled{afalgeng}) {
1547     $config{afalgeng}="";
1548     if (grep { $_ eq 'afalgeng' } @{$target{enable}}) {
1549         my $minver = 4*10000 + 1*100 + 0;
1550         if ($config{CROSS_COMPILE} eq "") {
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) {
1556                 $disabled{afalgeng} = "too-old-kernel";
1557             } else {
1558                 push @{$config{engdirs}}, "afalg";
1559             }
1560         } else {
1561             $disabled{afalgeng} = "cross-compiling";
1562         }
1563     } else {
1564         $disabled{afalgeng}  = "not-linux";
1565     }
1566 }
1567
1568 push @{$config{openssl_feature_defines}}, "OPENSSL_NO_AFALGENG" if ($disabled{afalgeng});
1569
1570 # Finish up %config by appending things the user gave us on the command line
1571 # apart from "make variables"
1572 foreach (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
1577     if (defined $config{$_}) {
1578         push @{$config{$_}}, @{$useradd{$_}};
1579     } else {
1580         $config{$_} = [ @{$useradd{$_}} ];
1581     }
1582 }
1583
1584 # ALL MODIFICATIONS TO %config and %target MUST BE DONE FROM HERE ON
1585
1586 # If we use the unified build, collect information from build.info files
1587 my %unified_info = ();
1588
1589 my $buildinfo_debug = defined($ENV{CONFIGURE_DEBUG_BUILDINFO});
1590 if ($builder eq "unified") {
1591     use with_fallback qw(Text::Template);
1592
1593     sub cleandir {
1594         my $base = shift;
1595         my $dir = shift;
1596         my $relativeto = shift || ".";
1597
1598         $dir = catdir($base,$dir) unless isabsolute($dir);
1599
1600         # Make sure the directories we're building in exists
1601         mkpath($dir);
1602
1603         my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
1604         #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
1605         return $res;
1606     }
1607
1608     sub cleanfile {
1609         my $base = shift;
1610         my $file = shift;
1611         my $relativeto = shift || ".";
1612
1613         $file = catfile($base,$file) unless isabsolute($file);
1614
1615         my $d = dirname($file);
1616         my $f = basename($file);
1617
1618         # Make sure the directories we're building in exists
1619         mkpath($d);
1620
1621         my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
1622         #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
1623         return $res;
1624     }
1625
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
1634     if (defined env($local_config_envname)) {
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 {
1642                     catfile(env($local_config_envname), $_);
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}
1663       = [ cleanfile($srcdir, catfile("Configurations", "common0.tmpl"),
1664                     $blddir),
1665           $build_file_template,
1666           cleanfile($srcdir, catfile("Configurations", "common.tmpl"),
1667                     $blddir) ];
1668
1669     my @build_dirs = ( [ ] );   # current directory
1670
1671     $config{build_infos} = [ ];
1672
1673     my %ordinals = ();
1674     while (@build_dirs) {
1675         my @curd = @{shift @build_dirs};
1676         my $sourced = catdir($srcdir, @curd);
1677         my $buildd = catdir($blddir, @curd);
1678
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
1686         mkpath($buildd);
1687
1688         my $f = 'build.info';
1689         # The basic things we're trying to build
1690         my @programs = ();
1691         my @programs_install = ();
1692         my @libraries = ();
1693         my @libraries_install = ();
1694         my @engines = ();
1695         my @engines_install = ();
1696         my @scripts = ();
1697         my @scripts_install = ();
1698         my @extra = ();
1699         my @overrides = ();
1700         my @intermediates = ();
1701         my @rawlines = ();
1702
1703         my %sources = ();
1704         my %shared_sources = ();
1705         my %includes = ();
1706         my %defines = ();
1707         my %depends = ();
1708         my %renames = ();
1709         my %sharednames = ();
1710         my %generate = ();
1711
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
1716         push @{$config{build_infos}}, catfile(abs2rel($sourced, $blddir), $f);
1717         my $template =
1718             Text::Template->new(TYPE => 'FILE',
1719                                 SOURCE => catfile($sourced, $f),
1720                                 PREPEND => qq{use lib "$FindBin::Bin/util/perl";});
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,
1726                                          disabled => \%disabled,
1727                                          withargs => \%withargs,
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*$/
1747             => sub {
1748                 if (! @skip || $skip[$#skip] > 0) {
1749                     push @skip, !! $1;
1750                 } else {
1751                     push @skip, -1;
1752                 }
1753             },
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; },
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             },
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             },
1811             qr/^\s*EXTRA\s*=\s*(.*)\s*$/
1812             => sub { push @extra, tokenize($1)
1813                          if !@skip || $skip[$#skip] > 0 },
1814             qr/^\s*OVERRIDES\s*=\s*(.*)\s*$/
1815             => sub { push @overrides, tokenize($1)
1816                          if !@skip || $skip[$#skip] > 0 },
1817
1818             qr/^\s*ORDINALS\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/,
1819             => sub { push @{$ordinals{$1}}, tokenize($2)
1820                          if !@skip || $skip[$#skip] > 0 },
1821             qr/^\s*SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1822             => sub { push @{$sources{$1}}, tokenize($2)
1823                          if !@skip || $skip[$#skip] > 0 },
1824             qr/^\s*SHARED_SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1825             => sub { push @{$shared_sources{$1}}, tokenize($2)
1826                          if !@skip || $skip[$#skip] > 0 },
1827             qr/^\s*INCLUDE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1828             => sub { push @{$includes{$1}}, tokenize($2)
1829                          if !@skip || $skip[$#skip] > 0 },
1830             qr/^\s*DEFINE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1831             => sub { push @{$defines{$1}}, tokenize($2)
1832                          if !@skip || $skip[$#skip] > 0 },
1833             qr/^\s*DEPEND\[((?:\\.|[^\\\]])*)\]\s*=\s*(.*)\s*$/
1834             => sub { push @{$depends{$1}}, tokenize($2)
1835                          if !@skip || $skip[$#skip] > 0 },
1836             qr/^\s*GENERATE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1837             => sub { push @{$generate{$1}}, $2
1838                          if !@skip || $skip[$#skip] > 0 },
1839             qr/^\s*RENAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1840             => sub { push @{$renames{$1}}, tokenize($2)
1841                          if !@skip || $skip[$#skip] > 0 },
1842             qr/^\s*SHARED_NAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1843             => sub { push @{$sharednames{$1}}, tokenize($2)
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->()) {
1850                     s|\R$||;
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}
1859                             || $target_kind eq $target{build_file}."(".$builder_platform.")");
1860                 }
1861             },
1862             qr/^\s*(?:#.*)?$/ => sub { },
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             },
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;
1882             my $dest = cleanfile($buildd, $_, $blddir);
1883             my $to = cleanfile($buildd, $renames{$_}->[0], $blddir);
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) {
1892             my $program = cleanfile($buildd, $_, $blddir);
1893             if ($unified_info{rename}->{$program}) {
1894                 $program = $unified_info{rename}->{$program};
1895             }
1896             $unified_info{programs}->{$program} = 1;
1897         }
1898
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
1907         foreach (@libraries) {
1908             my $library = cleanfile($buildd, $_, $blddir);
1909             if ($unified_info{rename}->{$library}) {
1910                 $library = $unified_info{rename}->{$library};
1911             }
1912             $unified_info{libraries}->{$library} = 1;
1913         }
1914
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
1923         die <<"EOF" if scalar @engines and !$config{dynamic_engines};
1924 ENGINES can only be used if configured with 'dynamic-engine'.
1925 This is usually a fault in a build.info file.
1926 EOF
1927         foreach (@engines) {
1928             my $library = cleanfile($buildd, $_, $blddir);
1929             if ($unified_info{rename}->{$library}) {
1930                 $library = $unified_info{rename}->{$library};
1931             }
1932             $unified_info{engines}->{$library} = 1;
1933         }
1934
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
1943         foreach (@scripts) {
1944             my $script = cleanfile($buildd, $_, $blddir);
1945             if ($unified_info{rename}->{$script}) {
1946                 $script = $unified_info{rename}->{$script};
1947             }
1948             $unified_info{scripts}->{$script} = 1;
1949         }
1950
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
1959         foreach (@extra) {
1960             my $extra = cleanfile($buildd, $_, $blddir);
1961             $unified_info{extra}->{$extra} = 1;
1962         }
1963
1964         foreach (@overrides) {
1965             my $override = cleanfile($buildd, $_, $blddir);
1966             $unified_info{overrides}->{$override} = 1;
1967         }
1968
1969         push @{$unified_info{rawlines}}, @rawlines;
1970
1971         unless ($disabled{shared}) {
1972             # Check sharednames.
1973             foreach (keys %sharednames) {
1974                 my $dest = cleanfile($buildd, $_, $blddir);
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;
1981                 my $to = cleanfile($buildd, $sharednames{$_}->[0], $blddir);
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
1992             # have any, as themselves.  Only for libraries that aren't
1993             # explicitly static.
1994             foreach (grep !/\.a$/, keys %{$unified_info{libraries}}) {
1995                 if (!defined $unified_info{sharednames}->{$_}) {
1996                     $unified_info{sharednames}->{$_} = $_
1997                 }
1998             }
1999
2000             # Check that we haven't defined any library as both shared and
2001             # explicitly static.  That is forbidden.
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             }
2007             die "these libraries are both explicitly static and shared:\n  ",
2008                 join(" ", @doubles), "\n"
2009                 if @doubles;
2010         }
2011
2012         foreach (keys %sources) {
2013             my $dest = $_;
2014             my $ddest = cleanfile($buildd, $_, $blddir);
2015             if ($unified_info{rename}->{$ddest}) {
2016                 $ddest = $unified_info{rename}->{$ddest};
2017             }
2018             foreach (@{$sources{$dest}}) {
2019                 my $s = cleanfile($sourced, $_, $blddir);
2020
2021                 # If it isn't in the source tree, we assume it's generated
2022                 # in the build tree
2023                 if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
2024                     $s = cleanfile($buildd, $_, $blddir);
2025                 }
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++
2031                     $o = cleanfile($buildd, $o, $blddir);
2032                     $unified_info{sources}->{$ddest}->{$o} = -1;
2033                     $unified_info{sources}->{$o}->{$s} = -1;
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);
2039                     $unified_info{sources}->{$ddest}->{$o} = -1;
2040                     $unified_info{sources}->{$o}->{$s} = -1;
2041                 } else {
2042                     $unified_info{sources}->{$ddest}->{$s} = 1;
2043                 }
2044             }
2045         }
2046
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
2058                 if ($s eq $src_configdata || ! -f $s || $generate{$_}) {
2059                     $s = cleanfile($buildd, $_, $blddir);
2060                 }
2061
2062                 if ($s =~ /\.(cc|cpp|c|s|S)$/) {
2063                     # We recognise C++, C and asm files
2064                     my $o = $_;
2065                     $o =~ s/\.[csS]$/.o/; # C and assembler
2066                     $o =~ s/\.(cc|cpp)$/_cc.o/; # C++
2067                     $o = cleanfile($buildd, $o, $blddir);
2068                     $unified_info{shared_sources}->{$ddest}->{$o} = -1;
2069                     $unified_info{sources}->{$o}->{$s} = -1;
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);
2075                     $unified_info{shared_sources}->{$ddest}->{$o} = -1;
2076                     $unified_info{sources}->{$o}->{$s} = -1;
2077                 } elsif ($s =~ /\.ld$/) {
2078                     # We also recognise linker scripts (or corresponding)
2079                     # We know they are generated files
2080                     my $ld = cleanfile($buildd, $_, $blddir);
2081                     $unified_info{shared_sources}->{$ddest}->{$ld} = 1;
2082                 } else {
2083                     die "unrecognised source file type for shared library: $s\n";
2084                 }
2085             }
2086         }
2087
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
2102         foreach (keys %depends) {
2103             my $dest = $_;
2104             my $ddest = $dest eq "" ? "" : cleanfile($sourced, $_, $blddir);
2105
2106             # If the destination doesn't exist in source, it can only be
2107             # a generated file in the build tree.
2108             if ($ddest ne "" && ($ddest eq $src_configdata || ! -f $ddest)) {
2109                 $ddest = cleanfile($buildd, $_, $blddir);
2110                 if ($unified_info{rename}->{$ddest}) {
2111                     $ddest = $unified_info{rename}->{$ddest};
2112                 }
2113             }
2114             foreach (@{$depends{$dest}}) {
2115                 my $d = cleanfile($sourced, $_, $blddir);
2116
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.
2122                 if ($d eq $src_configdata
2123                     || ! -f $d
2124                     || (grep { $d eq $_ }
2125                         map { cleanfile($srcdir, $_, $blddir) }
2126                         grep { /\.h$/ } keys %{$unified_info{generate}})) {
2127                     $d = cleanfile($buildd, $_, $blddir);
2128                 }
2129                 # Take note if the file to depend on is being renamed
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 = $`;
2136                 if ($unified_info{rename}->{$d}) {
2137                     $d = $unified_info{rename}->{$d};
2138                 }
2139                 $d .= $e;
2140                 $unified_info{depends}->{$ddest}->{$d} = 1;
2141             }
2142         }
2143
2144         foreach (keys %includes) {
2145             my $dest = $_;
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.
2150             if ($ddest eq $src_configdata || ! -f $ddest) {
2151                 $ddest = cleanfile($buildd, $_, $blddir);
2152                 if ($unified_info{rename}->{$ddest}) {
2153                     $ddest = $unified_info{rename}->{$ddest};
2154                 }
2155             }
2156             foreach (@{$includes{$dest}}) {
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}};
2163             }
2164         }
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         }
2186     }
2187
2188     my $ordinals_text = join(', ', sort keys %ordinals);
2189     warn <<"EOF" if $ordinals_text;
2190
2191 WARNING: ORDINALS were specified for $ordinals_text
2192 They are ignored and should be replaced with a combination of GENERATE,
2193 DEPEND and SHARED_SOURCE.
2194 EOF
2195
2196     # Massage the result
2197
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)$/;
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}};
2210         }
2211     }
2212
2213     # Go through all intermediary files and change their names to something that
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) {
2259                 next if ($intent->{$kind}->{dst} eq 'shared_sources'
2260                              && $disabled{shared});
2261
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) {
2273                         # Only affect object files and resource files,
2274                         # the others simply get a new value
2275                         # (+1 instead of -1)
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
2312     ### Make unified_info a bit more efficient
2313     # One level structures
2314     foreach (("programs", "libraries", "engines", "scripts", "extra", "overrides")) {
2315         $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
2316     }
2317     # Two level structures
2318     foreach my $l1 (("install", "sources", "shared_sources", "ldadd", "depends")) {
2319         foreach my $l2 (sort keys %{$unified_info{$l1}}) {
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             }
2329         }
2330     }
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     }
2337     # Includes
2338     foreach my $dest (sort keys %{$unified_info{includes}}) {
2339         if (defined($unified_info{includes}->{$dest}->{build})) {
2340             my @source_includes = ();
2341             @source_includes = ( @{$unified_info{includes}->{$dest}->{source}} )
2342                 if defined($unified_info{includes}->{$dest}->{source});
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             }
2349         } elsif (defined($unified_info{includes}->{$dest}->{source})) {
2350             $unified_info{includes}->{$dest} =
2351                 [ @{$unified_info{includes}->{$dest}->{source}} ];
2352         } else {
2353             delete $unified_info{includes}->{$dest};
2354         }
2355     }
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
2370             foreach (@{$unified_info{sources}->{$product} // []},
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     }
2392 }
2393
2394 # For the schemes that need it, we provide the old *_obj configs
2395 # from the *_asm_obj ones
2396 foreach (grep /_(asm|aux)_src$/, keys %target) {
2397     my $src = $_;
2398     (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
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++
2402 }
2403
2404 # Write down our configuration where it fits #########################
2405
2406 print "Creating configdata.pm\n";
2407 open(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
2408 print OUT <<"EOF";
2409 #! $config{HASHBANGPERL}
2410
2411 package configdata;
2412
2413 use strict;
2414 use warnings;
2415
2416 use Exporter;
2417 #use vars qw(\@ISA \@EXPORT);
2418 our \@ISA = qw(Exporter);
2419 our \@EXPORT = qw(\%config \%target \%disabled \%withargs \%unified_info \@disablables);
2420
2421 EOF
2422 print OUT "our %config = (\n";
2423 foreach (sort keys %config) {
2424     if (ref($config{$_}) eq "ARRAY") {
2425         print OUT "  ", $_, " => [ ", join(", ",
2426                                            map { quotify("perl", $_) }
2427                                            @{$config{$_}}), " ],\n";
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";
2444     } else {
2445         print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
2446     }
2447 }
2448 print OUT <<"EOF";
2449 );
2450
2451 EOF
2452 print OUT "our %target = (\n";
2453 foreach (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 }
2462 print OUT <<"EOF";
2463 );
2464
2465 EOF
2466 print OUT "our \%available_protocols = (\n";
2467 print OUT "  tls => [ ", join(", ", map { quotify("perl", $_) } @tls), " ],\n";
2468 print OUT "  dtls => [ ", join(", ", map { quotify("perl", $_) } @dtls), " ],\n";
2469 print OUT <<"EOF";
2470 );
2471
2472 EOF
2473 print OUT "our \@disablables = (\n";
2474 foreach (@disablables) {
2475     print OUT "  ", quotify("perl", $_), ",\n";
2476 }
2477 print OUT <<"EOF";
2478 );
2479
2480 EOF
2481 print OUT "our \%disabled = (\n";
2482 foreach (sort keys %disabled) {
2483     print OUT "  ", quotify("perl", $_), " => ", quotify("perl", $disabled{$_}), ",\n";
2484 }
2485 print OUT <<"EOF";
2486 );
2487
2488 EOF
2489 print OUT "our %withargs = (\n";
2490 foreach (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 }
2499 print OUT <<"EOF";
2500 );
2501
2502 EOF
2503 if ($builder eq "unified") {
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
2543 EOF
2544 }
2545 print OUT
2546     "# The following data is only used when this files is use as a script\n";
2547 print OUT "my \@makevars = (\n";
2548 foreach (sort keys %user) {
2549     print OUT "    '",$_,"',\n";
2550 }
2551 print OUT ");\n";
2552 print OUT "my \%disabled_info = (\n";
2553 foreach 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 }
2567 print OUT ");\n";
2568 print OUT 'my @user_crossable = qw( ', join (' ', @user_crossable), " );\n";
2569 print OUT << 'EOF';
2570 # If run directly, we can give some answers, and even reconfigure
2571 unless (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;
2581     my $options = undef;
2582     my $target = undef;
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,
2592                'options|o'              => \$options,
2593                'target|t'               => \$target,
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
2603     unless ($dump || $cmdline || $options || $target || $envvars || $makevars
2604             || $buildparams || $reconf || $verbose || $help || $man) {
2605         print STDERR <<"_____";
2606 You must give at least one option.
2607 For 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) {
2621         print "\nCommand line (with current working directory = $here):\n\n";
2622         print '    ',join(' ',
2623                           $config{PERL},
2624                           catfile($config{sourcedir}, 'Configure'),
2625                           @{$config{perlargv}}), "\n";
2626         print "\nPerl information:\n\n";
2627         print '    ',$config{perl_cmd},"\n";
2628         print '    ',$config{perl_version},' for ',$config{perl_archname},"\n";
2629     }
2630     if ($dump || $options) {
2631         my $longest = 0;
2632         my $longest2 = 0;
2633         foreach my $what (@disablables) {
2634             $longest = length($what) if $longest < length($what);
2635             $longest2 = length($disabled{$what})
2636                 if $disabled{$what} && $longest2 < length($disabled{$what});
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),
2646                     "[$disabled{$what}]", ' ' x ($longest2 - length($disabled{$what}) + 1);
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     }
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     }
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";
2680         foreach my $var (@makevars) {
2681             my $prefix = '';
2682             $prefix = $config{CROSS_COMPILE}
2683                 if grep { $var eq $_ } @user_crossable;
2684             $prefix //= '';
2685             print '    ',$var,' ' x (16 - length $var),'= ',
2686                 (ref $config{$var} eq 'ARRAY'
2687                  ? join(' ', @{$config{$var}})
2688                  : $prefix.$config{$var}),
2689                 "\n"
2690                 if defined $config{$var};
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
2699 NOTE: These variables only represent the configuration view.  The build file
2700 template may have processed these variables further, please have a look at the
2701 build 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
2733 1;
2734
2735 __END__
2736
2737 =head1 NAME
2738
2739 configdata.pm - configuration data for OpenSSL builds
2740
2741 =head1 SYNOPSIS
2742
2743 Interactive:
2744
2745   perl configdata.pm [options]
2746
2747 As data bank module:
2748
2749   use configdata;
2750
2751 =head1 DESCRIPTION
2752
2753 This module can be used in two modes, interactively and as a module containing
2754 all the data recorded by OpenSSL's Configure script.
2755
2756 When used interactively, simply run it as any perl script, with at least one
2757 option, and you will get the information you ask for.  See L</OPTIONS> below.
2758
2759 When loaded as a module, you get a few databanks with useful information to
2760 perform 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
2777 Print a brief help message and exit.
2778
2779 =item B<--man>
2780
2781 Print the manual page and exit.
2782
2783 =item B<--dump> | B<-d>
2784
2785 Print all relevant configuration data.  This is equivalent to B<--command-line>
2786 B<--options> B<--target> B<--environment> B<--make-variables>
2787 B<--build-parameters>.
2788
2789 =item B<--command-line> | B<-c>
2790
2791 Print the current configuration command line.
2792
2793 =item B<--options> | B<-o>
2794
2795 Print the features, both enabled and disabled, and display defined macro and
2796 skipped directories where applicable.
2797
2798 =item B<--target> | B<-t>
2799
2800 Print the config attributes for this config target.
2801
2802 =item B<--environment> | B<-e>
2803
2804 Print the environment variables and their values at the time of configuration.
2805
2806 =item B<--make-variables> | B<-m>
2807
2808 Print the main make variables generated in the current configuration
2809
2810 =item B<--build-parameters> | B<-b>
2811
2812 Print the build parameters, i.e. build file and build file templates.
2813
2814 =item B<--reconfigure> | B<--reconf> | B<-r>
2815
2816 Redo the configuration.
2817
2818 =item B<--verbose> | B<-v>
2819
2820 Verbose output.
2821
2822 =back
2823
2824 =cut
2825
2826 EOF
2827 close(OUT);
2828 if ($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 }
2833
2834 my %builders = (
2835     unified => sub {
2836         print 'Creating ',$target{build_file},"\n";
2837         run_dofile(catfile($blddir, $target{build_file}),
2838                    @{$config{build_file_templates}});
2839     },
2840     );
2841
2842 $builders{$builder}->($builder_platform, @builder_opts);
2843
2844 $SIG{__DIE__} = $orig_death_handler;
2845
2846 print <<"EOF" if ($disabled{threads} eq "unavailable");
2847
2848 The library could not be configured for supporting multi-threaded
2849 applications as the compiler options required on this system are not known.
2850 See file INSTALL for details if you need multi-threading.
2851 EOF
2852
2853 print <<"EOF" if ($no_shared_warn);
2854
2855 The options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
2856 platform, 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
2858 or position independent code, please let us know (but please first make sure
2859 you have tried with a current version of OpenSSL).
2860 EOF
2861
2862 print <<"EOF";
2863
2864 **********************************************************************
2865 ***                                                                ***
2866 ***   OpenSSL has been successfully configured                     ***
2867 ***                                                                ***
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)         ***
2876 ***                                                                ***
2877 **********************************************************************
2878 EOF
2879
2880 exit(0);
2881
2882 ######################################################################
2883 #
2884 # Helpers and utility functions
2885 #
2886
2887 # Death handler, to print a helpful message in case of failure #######
2888 #
2889 sub death_handler {
2890     die @_ if $^S;              # To prevent the added message in eval blocks
2891     my $build_file = $target{build_file} // "build file";
2892     my @message = ( <<"_____", @_ );
2893
2894 Failure!  $build_file wasn't produced.
2895 Please read INSTALL and associated NOTES files.  You may also have to look over
2896 your available compiler tool chain or change your configuration.
2897
2898 _____
2899
2900     # Dying is terminal, so it's ok to reset the signal handler here.
2901     $SIG{__DIE__} = $orig_death_handler;
2902     die @message;
2903 }
2904
2905 # Configuration file reading #########################################
2906
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
2912 # Helper function to implement conditional inheritance depending on the
2913 # value of $disabled{asm}.  Used in inherit_from values as follows:
2914 #
2915 #      inherit_from => [ "template", asm("asm_tmpl") ]
2916 #
2917 sub asm {
2918     my @x = @_;
2919     sub {
2920         $disabled{asm} ? () : @x;
2921     }
2922 }
2923
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 #
2942 sub 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 #
2954 sub 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 #
2964 sub threads {
2965     my @flags = @_;
2966     return sub { add($disabled{threads} ? () : @flags)->(); }
2967 }
2968
2969 sub shared {
2970     my @flags = @_;
2971     return sub { add($disabled{shared} ? () : @flags)->(); }
2972 }
2973
2974 our $add_called = 0;
2975 # Helper function to implement adding values to already existing configuration
2976 # values.  It handles elements that are ARRAYs, CODEs and scalars
2977 sub _add {
2978     my $separator = shift;
2979
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);
2984
2985     my @values =
2986         map {
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                 }
2998             } else {
2999                 ();
3000             }
3001     } (@_);
3002
3003     $add_called = 1;
3004
3005     if ($found_array) {
3006         [ @values ];
3007     } else {
3008         join($separator, grep { defined($_) && $_ ne "" } @values);
3009     }
3010 }
3011 sub add_before {
3012     my $separator = " ";
3013     if (ref($_[$#_]) eq "HASH") {
3014         my $opts = pop;
3015         $separator = $opts->{separator};
3016     }
3017     my @x = @_;
3018     sub { _add($separator, @x, @_) };
3019 }
3020 sub add {
3021     my $separator = " ";
3022     if (ref($_[$#_]) eq "HASH") {
3023         my $opts = pop;
3024         $separator = $opts->{separator};
3025     }
3026     my @x = @_;
3027     sub { _add($separator, @_, @x) };
3028 }
3029
3030 sub 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
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.
3053 sub read_config {
3054     my $fname = shift;
3055     my %targets;
3056
3057     {
3058         # Protect certain tables from tampering
3059         local %table = ();
3060
3061         %targets = read_eval_file($fname);
3062     }
3063     my %preexisting = ();
3064     foreach (sort keys %targets) {
3065         $preexisting{$_} = 1 if $table{$_};
3066     }
3067     die <<"EOF",
3068 The following config targets from $fname
3069 shadow pre-existing config targets with the same name:
3070 EOF
3071         map { "  $_\n" } sort keys %preexisting
3072         if %preexisting;
3073
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{$_};
3084         } else {
3085             $targets{$_}->{_conf_fname_int} = add([ $fname ]);
3086         }
3087     }
3088
3089     %table = (%table, %targets);
3090
3091 }
3092
3093 # configuration resolver.  Will only resolve all the lazy evaluation
3094 # codeblocks for the chosen target and all those it inherits from,
3095 # recursively
3096 sub resolve_config {
3097     my $target = shift;
3098     my @breadcrumbs = @_;
3099
3100 #    my $extra_checks = defined($ENV{CONFIGURE_EXTRA_CHECKS});
3101
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
3128             foreach (keys %inherited_config) {
3129                 if (!$combined_inheritance{$_}) {
3130                     $combined_inheritance{$_} = [];
3131                 }
3132                 push @{$combined_inheritance{$_}}, $inherited_config{$_};
3133             }
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
3147     #   empty string, the inherited value list will be run through the
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.
3152     my $default_combiner = add();
3153
3154     my %all_keys =
3155         map { $_ => 1 } (keys %combined_inheritance,
3156                          keys %{$table{$target}});
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
3164         $add_called = 0;
3165
3166         while(ref($object) eq "CODE") {
3167             $object = $object->(@$inherited);
3168         }
3169         if (!defined($object)) {
3170             return ();
3171         }
3172         elsif (ref($object) eq "ARRAY") {
3173             local $add_called;  # To make sure recursive calls don't affect it
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
3184     foreach (sort keys %all_keys) {
3185         my $previous = $combined_inheritance{$_};
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
3194         $table{$target}->{$_} = process_values($table{$target}->{$_},
3195                                                $combined_inheritance{$_},
3196                                                $target, $_);
3197         unless(defined($table{$target}->{$_})) {
3198             delete $table{$target}->{$_};
3199         }
3200 #        if ($extra_checks &&
3201 #            $previous && !($add_called ||  $previous ~~ $table{$target}->{$_})) {
3202 #            warn "$_ got replaced in $target\n";
3203 #        }
3204     }
3205
3206     # Finally done, return the result.
3207     return %{$table{$target}};
3208 }
3209
3210 sub usage
3211         {
3212         print STDERR $usage;
3213         print STDERR "\npick os/compiler from:\n";
3214         my $j=0;
3215         my $i;
3216         my $k=0;
3217         foreach $i (sort keys %table)
3218                 {
3219                 next if $table{$i}->{template};
3220                 next if $i =~ /^debug/;
3221                 $k += length($i) + 1;
3222                 if ($k > 78)
3223                         {
3224                         print STDERR "\n";
3225                         $k=length($i);
3226                         }
3227                 print STDERR $i . " ";
3228                 }
3229         foreach $i (sort keys %table)
3230                 {
3231                 next if $table{$i}->{template};
3232                 next if $i !~ /^debug/;
3233                 $k += length($i) + 1;
3234                 if ($k > 78)
3235                         {
3236                         print STDERR "\n";
3237                         $k=length($i);
3238                         }
3239                 print STDERR $i . " ";
3240                 }
3241         print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n";
3242         exit(1);
3243         }
3244
3245 sub run_dofile
3246 {
3247     my $out = shift;
3248     my @templates = @_;
3249
3250     unlink $out || warn "Can't remove $out, $!"
3251         if -f $out;
3252     foreach (@templates) {
3253         die "Can't open $_, $!" unless -f $_;
3254     }
3255     my $perlcmd = (quotify("maybeshell", $config{PERL}))[0];
3256     my $cmd = "$perlcmd \"-I.\" \"-Mconfigdata\" \"$dofile\" -o\"Configure\" \"".join("\" \"",@templates)."\" > \"$out.new\"";
3257     #print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
3258     system($cmd);
3259     exit 1 if $? != 0;
3260     rename("$out.new", $out) || die "Can't rename $out.new, $!";
3261 }
3262
3263 sub compiler_predefined {
3264     state %predefined;
3265     my $cc = shift;
3266
3267     return () if $^O eq 'VMS';
3268
3269     die 'compiler_predefined called without a compiler command'
3270         unless $cc;
3271
3272     if (! $predefined{$cc}) {
3273
3274         $predefined{$cc} = {};
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;
3280             $predefined{$cc}->{$1} = $2 // '';
3281         }
3282         close(PIPE);
3283     }
3284
3285     return %{$predefined{$cc}};
3286 }
3287
3288 sub 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
3309 sub env
3310 {
3311     my $name = shift;
3312     my %opts = @_;
3313
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.
3318
3319         $config{perlenv}->{$name} = $ENV{$name}
3320             if ! exists $config{perlenv}->{$name};
3321     }
3322     return $config{perlenv}->{$name};
3323 }
3324
3325 # Configuration printer ##############################################
3326
3327 sub print_table_entry
3328 {
3329     local $now_printing = shift;
3330     my %target = resolve_config($now_printing);
3331     my $type = shift;
3332
3333     # Don't print the templates
3334     return if $target{template};
3335
3336     my @sequence = (
3337         "sys_id",
3338         "cpp",
3339         "cppflags",
3340         "defines",
3341         "includes",
3342         "cc",
3343         "cflags",
3344         "unistd",
3345         "ld",
3346         "lflags",
3347         "loutflag",
3348         "ex_libs",
3349         "bn_ops",
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",
3370         "thread_scheme",
3371         "perlasm_scheme",
3372         "dso_scheme",
3373         "shared_target",
3374         "shared_cflag",
3375         "shared_defines",
3376         "shared_ldflag",
3377         "shared_rcflag",
3378         "shared_extension",
3379         "dso_extension",
3380         "obj_extension",
3381         "exe_extension",
3382         "ranlib",
3383         "ar",
3384         "arflags",
3385         "aroutflag",
3386         "rc",
3387         "rcflags",
3388         "rcoutflag",
3389         "mt",
3390         "mtflags",
3391         "mtinflag",
3392         "mtoutflag",
3393         "multilib",
3394         "build_scheme",
3395         );
3396
3397     if ($type eq "TABLE") {
3398         print "\n";
3399         print "*** $now_printing\n";
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         }
3407     } elsif ($type eq "HASH") {
3408         my $largest =
3409             length((sort { length($a) <=> length($b) } @sequence)[-1]);
3410         print "    '$now_printing' => {\n";
3411         foreach (@sequence) {
3412             if ($target{$_}) {
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                 }
3418             }
3419         }
3420         print "    },\n";
3421     }
3422 }
3423
3424 # Utility routines ###################################################
3425
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.
3429 sub 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
3435     # If the file spec includes a device or a directory spec,
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
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.
3448 sub 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
3466 sub quotify {
3467     my %processors = (
3468         perl    => sub { my $x = shift;
3469                          $x =~ s/([\\\$\@"])/\\$1/g;
3470                          return '"'.$x.'"'; },
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                         },
3479         );
3480     my $for = shift;
3481     my $processor =
3482         defined($processors{$for}) ? $processors{$for} : sub { shift; };
3483
3484     return map { $processor->($_); } @_;
3485 }
3486
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
3491 sub 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>) {
3501             s|\R$||;
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
3522 sub 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)) {
3532             s|\R$||;
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.
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.
3552 sub collect_information {
3553     my $lineiterator = shift;
3554     my %collectors = @_;
3555
3556     while(defined($_ = $lineiterator->())) {
3557         s|\R$||;
3558         my $found = 0;
3559         if ($collectors{"BEFORE"}) {
3560             $collectors{"BEFORE"}->($_);
3561         }
3562         foreach my $re (keys %collectors) {
3563             if ($re !~ /^OTHERWISE|BEFORE|AFTER$/ && /$re/) {
3564                 $collectors{$re}->($lineiterator);
3565                 $found = 1;
3566             };
3567         }
3568         if ($collectors{"OTHERWISE"}) {
3569             $collectors{"OTHERWISE"}->($lineiterator, $_)
3570                 unless $found || !defined $collectors{"OTHERWISE"};
3571         }
3572         if ($collectors{"AFTER"}) {
3573             $collectors{"AFTER"}->($_);
3574         }
3575     }
3576 }
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.
3588 sub 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 }