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