Clean away $no_threads since we have $disabled{threads}
[openssl.git] / Configure
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 ##
5 ##  Configure -- OpenSSL source tree configuration script
6 ##  If editing this file, run this command before committing
7 ##      make -f Makefile.in TABLE
8 ##
9
10 require 5.000;
11 use strict;
12 use File::Basename;
13 use File::Spec::Functions qw/:DEFAULT abs2rel rel2abs/;
14 use File::Path qw/mkpath/;
15
16 # see INSTALL for instructions.
17
18 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";
19
20 # Options:
21 #
22 # --config      add the given configuration file, which will be read after
23 #               any "Configurations*" files that are found in the same
24 #               directory as this script.
25 # --prefix      prefix for the OpenSSL installation, which includes the
26 #               directories bin, lib, include, share/man, share/doc/openssl
27 #               This becomes the value of INSTALLTOP in Makefile
28 #               (Default: /usr/local)
29 # --openssldir  OpenSSL data area, such as openssl.cnf, certificates and keys.
30 #               If it's a relative directory, it will be added on the directory
31 #               given with --prefix.
32 #               This becomes the value of OPENSSLDIR in Makefile and in C.
33 #               (Default: PREFIX/ssl)
34 #
35 # --cross-compile-prefix Add specified prefix to binutils components.
36 #
37 # --api         One of 0.9.8, 1.0.0 or 1.1.0.  Do not compile support for
38 #               interfaces deprecated as of the specified OpenSSL version.
39 #
40 # no-hw-xxx     do not compile support for specific crypto hardware.
41 #               Generic OpenSSL-style methods relating to this support
42 #               are always compiled but return NULL if the hardware
43 #               support isn't compiled.
44 # no-hw         do not compile support for any crypto hardware.
45 # [no-]threads  [don't] try to create a library that is suitable for
46 #               multithreaded applications (default is "threads" if we
47 #               know how to do it)
48 # [no-]shared   [don't] try to create shared libraries when supported.
49 # [no-]pic      [don't] try to build position independent code when supported.
50 #               If disabled, it also disables shared and dynamic-engine.
51 # no-asm        do not use assembler
52 # no-dso        do not compile in any native shared-library methods. This
53 #               will ensure that all methods just return NULL.
54 # no-egd        do not compile support for the entropy-gathering daemon APIs
55 # [no-]zlib     [don't] compile support for zlib compression.
56 # zlib-dynamic  Like "zlib", but the zlib library is expected to be a shared
57 #               library and will be loaded in run-time by the OpenSSL library.
58 # sctp          include SCTP support
59 # 386           generate 80386 code
60 # no-sse2       disables IA-32 SSE2 code, above option implies no-sse2
61 # no-<cipher>   build without specified algorithm (rsa, idea, rc5, ...)
62 # -<xxx> +<xxx> compiler options are passed through
63 #
64 # DEBUG_SAFESTACK use type-safe stacks to enforce type-safety on stack items
65 #               provided to stack calls. Generates unique stack functions for
66 #               each possible stack type.
67 # BN_LLONG      use the type 'long long' in crypto/bn/bn.h
68 # RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
69 # Following are set automatically by this script
70 #
71 # MD5_ASM       use some extra md5 assember,
72 # SHA1_ASM      use some extra sha1 assember, must define L_ENDIAN for x86
73 # RMD160_ASM    use some extra ripemd160 assember,
74 # SHA256_ASM    sha256_block is implemented in assembler
75 # SHA512_ASM    sha512_block is implemented in assembler
76 # AES_ASM       ASE_[en|de]crypt is implemented in assembler
77
78 # Minimum warning options... any contributions to OpenSSL should at least get
79 # past these.
80
81 my $gcc_devteam_warn = "-DPEDANTIC -DREF_DEBUG -DDEBUG_UNUSED -DBIO_DEBUG"
82         . " -pedantic"
83         . " -Wall"
84         . " -Wno-long-long"
85         . " -Wsign-compare"
86         . " -Wmissing-prototypes"
87         . " -Wshadow"
88         . " -Wformat"
89         . " -Wtype-limits"
90         . " -Werror"
91         ;
92
93 # These are used in addition to $gcc_devteam_warn when the compiler is clang.
94 # TODO(openssl-team): fix problems and investigate if (at least) the
95 # following warnings can also be enabled:
96 #       -Wswitch-enum
97 #       -Wunused-macros
98 #       -Wcast-align
99 #       -Wunreachable-code
100 #       -Wlanguage-extension-token
101 #       -Wextended-offsetof
102 my $clang_devteam_warn = ""
103         . " -Qunused-arguments"
104         . " -Wextra"
105         . " -Wno-unused-parameter"
106         . " -Wno-missing-field-initializers"
107         . " -Wno-language-extension-token"
108         . " -Wno-extended-offsetof"
109         . " -Wconditional-uninitialized"
110         . " -Wincompatible-pointer-types-discards-qualifiers"
111         . " -Wmissing-variable-declarations"
112         ;
113
114 # This adds backtrace information to the memory leak info.  Is only used
115 # when crypto-mdebug-backtrace is enabled.
116 my $memleak_devteam_backtrace = "-rdynamic";
117
118 my $strict_warnings = 0;
119
120 # As for $BSDthreads. Idea is to maintain "collective" set of flags,
121 # which would cover all BSD flavors. -pthread applies to them all,
122 # but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
123 # -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
124 # which has to be accompanied by explicit -D_THREAD_SAFE and
125 # sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
126 # seems to be sufficient?
127 my $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
128
129 #
130 # API compability name to version number mapping.
131 #
132 my $maxapi = "1.1.0";           # API for "no-deprecated" builds
133 my $apitable = {
134     "1.1.0" => "0x10100000L",
135     "1.0.0" => "0x10000000L",
136     "0.9.8" => "0x00908000L",
137 };
138
139 my $base_target = "BASE";   # The template that all other inherit from
140 our %table = ();
141 our %config = ();
142
143 # Forward declarations ###############################################
144
145 # read_config(filename)
146 #
147 # Reads a configuration file and populates %table with the contents
148 # (which the configuration file places in %targets).
149 sub read_config;
150
151 # resolve_config(target)
152 #
153 # Resolves all the late evalutations, inheritances and so on for the
154 # chosen target and any target it inherits from.
155 sub resolve_config;
156
157
158 # Information collection #############################################
159
160 # Unified build supports separate build dir
161 my $srcdir = catdir(absolutedir(dirname($0))); # catdir ensures local syntax
162 my $blddir = catdir(absolutedir("."));         # catdir ensures local syntax
163 my $dofile = abs2rel(catfile($srcdir, "util/dofile.pl"));
164
165 $config{sourcedir} = abs2rel($srcdir);
166 $config{builddir} = abs2rel($blddir);
167
168 # Collect version numbers
169 $config{version} = "unknown";
170 $config{version_num} = "unknown";
171 $config{shlib_version_number} = "unknown";
172 $config{shlib_version_history} = "unknown";
173
174 collect_information(
175     collect_from_file(catfile($srcdir,'include/openssl/opensslv.h')),
176     qr/OPENSSL.VERSION.TEXT.*OpenSSL (\S+) / => sub { $config{version} = $1; },
177     qr/OPENSSL.VERSION.NUMBER.*(0x\S+)/      => sub { $config{version_num}=$1 },
178     qr/SHLIB_VERSION_NUMBER *"([^"]+)"/      => sub { $config{shlib_version_number}=$1 },
179     qr/SHLIB_VERSION_HISTORY *"([^"]*)"/     => sub { $config{shlib_version_history}=$1 }
180     );
181 if ($config{shlib_version_history} ne "") { $config{shlib_version_history} .= ":"; }
182
183 ($config{major}, $config{minor})
184     = ($config{version} =~ /^([0-9]+)\.([0-9\.]+)/);
185 ($config{shlib_major}, $config{shlib_minor})
186     = ($config{shlib_version_number} =~ /^([0-9]+)\.([0-9\.]+)/);
187 die "erroneous version information in opensslv.h: ",
188     "$config{major}, $config{minor}, $config{shlib_major}, $config{shlib_minor}\n"
189     if ($config{major} eq "" || $config{minor} eq ""
190         || $config{shlib_major} eq "" ||  $config{shlib_minor} eq "");
191
192 # Collect target configurations
193
194 my $pattern = catfile(dirname($0), "Configurations", "*.conf");
195 foreach (sort glob($pattern) ) {
196     &read_config($_);
197 }
198
199
200 print "Configuring OpenSSL version $config{version} (0x$config{version_num})\n";
201
202 $config{perl};
203 $config{prefix}="";
204 $config{openssldir}="";
205 $config{processor}="";
206 $config{libdir}="";
207 $config{cross_compile_prefix}="";
208 $config{fipslibdir}="/usr/local/ssl/fips-2.0/lib/";
209 my $nofipscanistercheck=0;
210 $config{baseaddr}="0xFB00000";
211 my $threads=0;
212 my $zlib=1;      # but "no-zlib" is default
213 my $no_rfc3779=0;
214 my $no_asm=0;
215 my $no_dso=0;
216 my $default_ranlib;
217 $config{fips}=0;
218
219 # Top level directories to build
220 $config{dirs} = [ "crypto", "ssl", "engines", "apps", "test", "tools" ];
221 # crypto/ subdirectories to build
222 $config{sdirs} = [
223     "objects",
224     "md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305",
225     "des", "aes", "rc2", "rc4", "rc5", "idea", "bf", "cast", "camellia", "seed", "chacha", "modes",
226     "bn", "ec", "rsa", "dsa", "dh", "dso", "engine",
227     "buffer", "bio", "stack", "lhash", "rand", "err",
228     "evp", "asn1", "pem", "x509", "x509v3", "conf", "txt_db", "pkcs7", "pkcs12", "comp", "ocsp", "ui",
229     "cms", "ts", "srp", "cmac", "ct", "async", "kdf"
230     ];
231
232 # Known TLS and DTLS protocols
233 my @tls = qw(ssl3 tls1 tls1_1 tls1_2);
234 my @dtls = qw(dtls1 dtls1_2);
235
236 # Explicitelly known options that are possible to disable.  They can
237 # be regexps, and will be used like this: /^no-${option}$/
238 # For developers: keep it sorted alphabetically
239
240 my @disablables = (
241     "aes",
242     "asm",
243     "async",
244     "autoalginit",
245     "autoerrinit",
246     "bf",
247     "camellia",
248     "capieng",
249     "cast",
250     "chacha",
251     "cmac",
252     "cms",
253     "comp",
254     "crypto-mdebug",
255     "crypto-mdebug-backtrace",
256     "ct",
257     "deprecated",
258     "des",
259     "dgram",
260     "dh",
261     "dsa",
262     "dso",
263     "dtls",
264     "dynamic-engine",
265     "ec",
266     "ec2m",
267     "ecdh",
268     "ecdsa",
269     "ec_nistp_64_gcc_128",
270     "egd",
271     "engine",
272     "err",
273     "filenames",
274     "heartbeats",
275     "hmac",
276     "hw(-.+)?",
277     "idea",
278     "locking",
279     "md2",
280     "md4",
281     "md5",
282     "mdc2",
283     "md[-_]ghost94",
284     "nextprotoneg",
285     "ocb",
286     "ocsp",
287     "pic",
288     "poly1305",
289     "posix-io",
290     "psk",
291     "rc2",
292     "rc4",
293     "rc5",
294     "rdrand",
295     "rfc3779",
296     "rijndael",                 # Old AES name
297     "ripemd",
298     "rmd160",
299     "rsa",
300     "scrypt",
301     "sct",
302     "sctp",
303     "seed",
304     "sha",
305     "shared",
306     "sock",
307     "srp",
308     "srtp",
309     "sse2",
310     "ssl",
311     "ssl-trace",
312     "static-engine",
313     "stdio",
314     "threads",
315     "tls",
316     "ts",
317     "ui",
318     "unit-test",
319     "whirlpool",
320     "zlib",
321     "zlib-dynamic",
322     );
323 foreach my $proto ((@tls, @dtls))
324         {
325         push(@disablables, $proto);
326         push(@disablables, "$proto-method");
327         }
328
329 my @deprecated_disablables = (
330     "ssl2",
331     );
332
333 # All of the following is disabled by default (RC5 was enabled before 0.9.8):
334
335 our %disabled = ( # "what"         => "comment"
336                   "ec_nistp_64_gcc_128" => "default",
337                   "egd"            => "default",
338                   "md2"            => "default",
339                   "rc5"            => "default",
340                   "sctp"           => "default",
341                   "shared"         => "default",
342                   "ssl-trace"      => "default",
343                   "static-engine"  => "default",
344                   "unit-test"      => "default",
345                   "zlib"           => "default",
346                   "crypto-mdebug"  => "default",
347                   "heartbeats"     => "default",
348                 );
349
350 # Note: => pair form used for aesthetics, not to truly make a hash table
351 my @disable_cascades = (
352     # "what"            => [ "cascade", ... ]
353     sub { $config{processor} eq "386" }
354                         => [ "sse2" ],
355     "ssl"               => [ "ssl3" ],
356     "ssl3-method"       => [ "ssl3" ],
357     "zlib"              => [ "zlib-dynamic" ],
358     "rijndael"          => [ "aes" ],
359     "des"               => [ "mdc2" ],
360     "ec"                => [ "ecdsa", "ecdh" ],
361
362     "dgram"             => [ "dtls" ],
363     "dtls"              => [ @dtls ],
364
365     # SSL 3.0, (D)TLS 1.0 and TLS 1.1 require MD5 and SHA
366     "md5"               => [ "ssl", "tls1", "tls1_1", "dtls1" ],
367     "sha"               => [ "ssl", "tls1", "tls1_1", "dtls1" ],
368
369     # Additionally, SSL 3.0 requires either RSA or DSA+DH
370     sub { $disabled{rsa}
371           && ($disabled{dsa} || $disabled{dh}); }
372                         => [ "ssl" ],
373
374     # (D)TLS 1.0 and TLS 1.1 also require either RSA or DSA+DH
375     # or ECDSA + ECDH.  (D)TLS 1.2 has this requirement as well.
376     # (XXX: We don't support PSK-only builds).
377     sub { $disabled{rsa}
378           && ($disabled{dsa} || $disabled{dh})
379           && ($disabled{ecdsa} || $disabled{ecdh}); }
380                         => [ "tls1", "tls1_1", "tls1_2",
381                              "dtls1", "dtls1_2" ],
382
383     "tls"               => [ @tls ],
384
385     # SRP and HEARTBEATS require TLSEXT
386     "tlsext"            => [ "srp", "heartbeats" ],
387
388     "crypto-mdebug"     => [ "crypto-mdebug-backtrace" ],
389
390     # Without DSO, we can't load dynamic engines, so don't build them dynamic
391     "dso"               => [ "dynamic-engine" ],
392
393     # Without position independent code, there can be no shared libraries or DSOs
394     "pic"               => [ "shared", "dynamic-engine" ],
395     );
396
397 # Avoid protocol support holes.  Also disable all versions below N, if version
398 # N is disabled while N+1 is enabled.
399 #
400 my @list = (reverse @tls);
401 while ((my $first, my $second) = (shift @list, shift @list)) {
402     last unless @list;
403     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
404                               => [ @list ] );
405     unshift @list, $second;
406 }
407 my @list = (reverse @dtls);
408 while ((my $first, my $second) = (shift @list, shift @list)) {
409     last unless @list;
410     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
411                               => [ @list ] );
412     unshift @list, $second;
413 }
414
415 # Explicit "no-..." options will be collected in %disabled along with the defaults.
416 # To remove something from %disabled, use "enable-foo".
417 # For symmetry, "disable-foo" is a synonym for "no-foo".
418
419 my @generated_headers = (
420     "include/openssl/opensslconf.h",
421     "crypto/include/internal/bn_conf.h",
422     "crypto/include/internal/dso_conf.h"
423     );
424
425 my @generated_by_make_headers = (
426     "crypto/buildinf.h"
427     );
428
429
430 my $no_sse2=0;
431
432 &usage if ($#ARGV < 0);
433
434 my $user_cflags="";
435 my @user_defines=();
436 my $unified = 0;
437 $config{openssl_api_defines}=[];
438 $config{openssl_algorithm_defines}=[];
439 $config{openssl_thread_defines}=[];
440 $config{openssl_sys_defines}=[];
441 $config{openssl_other_defines}=[];
442 my $libs="";
443 my $target="";
444 $config{options}="";
445 my %withargs=();
446 my $build_prefix = "release_";
447
448 my @argvcopy=@ARGV;
449
450 if (grep /^reconf(igure)?$/, @argvcopy) {
451     if (-f "./configdata.pm") {
452         my $file = "./configdata.pm";
453         unless (my $return = do $file) {
454             die "couldn't parse $file: $@" if $@;
455             die "couldn't do $file: $!"    unless defined $return;
456             die "couldn't run $file"       unless $return;
457         }
458
459         @argvcopy = defined($configdata::config{perlargv}) ?
460             @{$configdata::config{perlargv}} : ();
461         die "Incorrect data to reconfigure, please do a normal configuration\n"
462             if (grep(/^reconf/,@argvcopy));
463         $ENV{CROSS_COMPILE} = $configdata::config{cross_compile_prefix}
464             if defined($configdata::config{cross_compile_prefix});
465         $ENV{CROSS_COMPILE} = $configdata::config{cc}
466             if defined($configdata::config{cc});
467
468         print "Reconfiguring with: ", join(" ",@argvcopy), "\n";
469         print "    CROSS_COMPILE = ",$ENV{CROSS_COMPILE},"\n"
470             if $ENV{CROSS_COMPILE};
471         print "    CC = ",$ENV{CC},"\n" if $ENV{CC};
472     } elsif (open IN, "<Makefile") {
473         #
474         # THIS SECTION IS TEMPORARY, it helps transitioning from Makefile
475         # centered information gathering the reading configdata.pm
476         #
477         while (<IN>) {
478             s|\R$||;
479             if (/^CONFIGURE_ARGS=\s*(.*)\s*/) {
480                 # Older form, we split the string and hope for the best
481                 @argvcopy = split /\s+/, $_;
482                 die "Incorrect data to reconfigure, please do a normal configuration\n"
483                     if (grep(/^reconf/,@argvcopy));
484             } elsif (/^CROSS_COMPILE=\s*(.*)/) {
485                 $ENV{CROSS_COMPILE}=$1;
486             } elsif (/^CC=\s*(?:\$\(CROSS_COMPILE\))?(.*?)$/) {
487                 $ENV{CC}=$1;
488             }
489         }
490         #
491         # END OF TEMPORARY SECTION
492         #
493     } else {
494         die "Insufficient data to reconfigure, please do a normal configuration\n";
495     }
496 }
497
498 $config{perlargv} = [ @argvcopy ];
499
500 my %unsupported_options = ();
501 my %deprecated_options = ();
502 foreach (@argvcopy)
503         {
504         # VMS is a case insensitive environment, and depending on settings
505         # out of our control, we may receive options uppercased.  Let's
506         # downcase at least the part before any equal sign.
507         if ($^O eq "VMS")
508                 {
509                 s/^([^=]*)/lc($1)/e;
510                 }
511         s /^-no-/no-/; # some people just can't read the instructions
512
513         # rewrite some options in "enable-..." form
514         s /^-?-?shared$/enable-shared/;
515         s /^sctp$/enable-sctp/;
516         s /^threads$/enable-threads/;
517         s /^zlib$/enable-zlib/;
518         s /^zlib-dynamic$/enable-zlib-dynamic/;
519
520         if (/^(no|disable|enable)-(.+)$/)
521                 {
522                 my $word = $2;
523                 if (grep { $word =~ /^${_}$/ } @deprecated_disablables)
524                         {
525                         $deprecated_options{$_} = 1;
526                         next;
527                         }
528                 elsif (!grep { $word =~ /^${_}$/ } @disablables)
529                         {
530                         $unsupported_options{$_} = 1;
531                         next;
532                         }
533                 }
534         if (/^no-(.+)$/ || /^disable-(.+)$/)
535                 {
536                 foreach my $proto ((@tls, @dtls))
537                         {
538                         if ($1 eq "$proto-method")
539                                 {
540                                 $disabled{"$proto"} = "option($proto-method)";
541                                 last;
542                                 }
543                         }
544                 if ($1 eq "dtls")
545                         {
546                         foreach my $proto (@dtls)
547                                 {
548                                 $disabled{$proto} = "option(dtls)";
549                                 }
550                         }
551                 elsif ($1 eq "ssl")
552                         {
553                         # Last one of its kind
554                         $disabled{"ssl3"} = "option(ssl)";
555                         }
556                 elsif ($1 eq "tls")
557                         {
558                         # XXX: Tests will fail if all SSL/TLS
559                         # protocols are disabled.
560                         foreach my $proto (@tls)
561                                 {
562                                 $disabled{$proto} = "option(tls)";
563                                 }
564                         }
565                 elsif ($1 eq "static-engine")
566                         {
567                         delete $disabled{"dynamic-engine"};
568                         }
569                 elsif ($1 eq "dynamic-engine")
570                         {
571                         $disabled{"dynamic-engine"} = "option";
572                         }
573                 else
574                         {
575                         $disabled{$1} = "option";
576                         }
577                 }
578         elsif (/^enable-(.+)$/)
579                 {
580                 if ($1 eq "static-engine")
581                         {
582                         $disabled{"dynamic-engine"} = "option";
583                         }
584                 elsif ($1 eq "dynamic-engine")
585                         {
586                         delete $disabled{"dynamic-engine"};
587                         }
588                 my $algo = $1;
589                 delete $disabled{$algo};
590
591                 $threads = 1 if ($algo eq "threads");
592                 }
593         elsif (/^--strict-warnings$/)
594                 {
595                 $strict_warnings = 1;
596                 }
597         elsif (/^--debug$/)
598                 {
599                 $build_prefix = "debug_";
600                 }
601         elsif (/^--release$/)
602                 {
603                 $build_prefix = "release_";
604                 }
605         elsif (/^386$/)
606                 { $config{processor}=386; }
607         elsif (/^fips$/)
608                 {
609                 $config{fips}=1;
610                 }
611         elsif (/^rsaref$/)
612                 {
613                 # No RSAref support any more since it's not needed.
614                 # The check for the option is there so scripts aren't
615                 # broken
616                 }
617         elsif (/^nofipscanistercheck$/)
618                 {
619                 $config{fips} = 1;
620                 $nofipscanistercheck = 1;
621                 }
622         elsif (/^[-+]/)
623                 {
624                 if (/^--unified$/)
625                         {
626                         $unified=1;
627                         }
628                 elsif (/^--prefix=(.*)$/)
629                         {
630                         $config{prefix}=$1;
631                         die "Directory given with --prefix MUST be absolute\n"
632                                 unless file_name_is_absolute($config{prefix});
633                         }
634                 elsif (/^--api=(.*)$/)
635                         {
636                         $config{api}=$1;
637                         }
638                 elsif (/^--libdir=(.*)$/)
639                         {
640                         $config{libdir}=$1;
641                         }
642                 elsif (/^--openssldir=(.*)$/)
643                         {
644                         $config{openssldir}=$1;
645                         }
646                 elsif (/^--with-zlib-lib=(.*)$/)
647                         {
648                         $withargs{zlib_lib}=$1;
649                         }
650                 elsif (/^--with-zlib-include=(.*)$/)
651                         {
652                         $withargs{zlib_include}="-I$1";
653                         }
654                 elsif (/^--with-fipslibdir=(.*)$/)
655                         {
656                         $config{fipslibdir}="$1/";
657                         }
658                 elsif (/^--with-baseaddr=(.*)$/)
659                         {
660                         $config{baseaddr}="$1";
661                         }
662                 elsif (/^--cross-compile-prefix=(.*)$/)
663                         {
664                         $config{cross_compile_prefix}=$1;
665                         }
666                 elsif (/^--config=(.*)$/)
667                         {
668                         read_config $1;
669                         }
670                 elsif (/^-[lL](.*)$/ or /^-Wl,/)
671                         {
672                         $libs.=$_." ";
673                         }
674                 elsif (/^-D(.*)$/)
675                         {
676                         push @user_defines, $1;
677                         }
678                 else    # common if (/^[-+]/), just pass down...
679                         {
680                         $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
681                         $user_cflags.=" ".$_;
682                         }
683                 }
684         elsif ($_ =~ /^([^:]+):(.+)$/)
685                 {
686                 eval "\$table{\$1} = \"$2\""; # allow $xxx constructs in the string
687                 $target=$1;
688                 }
689         else
690                 {
691                 die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
692                 $target=$_;
693                 }
694         unless ($_ eq $target || /^no-/ || /^disable-/)
695                 {
696                 # "no-..." follows later after implied disactivations
697                 # have been derived.  (Don't take this too seroiusly,
698                 # we really only write OPTIONS to the Makefile out of
699                 # nostalgia.)
700
701                 if ($config{options} eq "")
702                         { $config{options} = $_; }
703                 else
704                         { $config{options} .= " ".$_; }
705                 }
706
707         if (defined($config{api}) && !exists $apitable->{$config{api}}) {
708                 die "***** Unsupported api compatibility level: $config{api}\n",
709         }
710
711         if (keys %deprecated_options)
712                 {
713                 warn "***** Deprecated options: ",
714                         join(", ", keys %deprecated_options), "\n";
715                 }
716         if (keys %unsupported_options)
717                 {
718                 die "***** Unsupported options: ",
719                         join(", ", keys %unsupported_options), "\n";
720                 }
721         }
722
723 if ($config{fips})
724         {
725         delete $disabled{"shared"} if ($disabled{"shared"} =~ /^default/);
726         }
727 else
728         {
729         @{$config{dirs}} = grep !/^fips$/, @{$config{dirs}};
730         }
731
732 my @tocheckfor = (keys %disabled);
733 while (@tocheckfor) {
734     my %new_tocheckfor = ();
735     my @cascade_copy = (@disable_cascades);
736     while (@cascade_copy) {
737         my ($test, $descendents) = (shift @cascade_copy, shift @cascade_copy);
738         if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
739             map {
740                 $new_tocheckfor{$_} => 1; $disabled{$_} = "forced";
741             } grep { !defined($disabled{$_}) } @$descendents;
742         }
743     }
744     @tocheckfor = (keys %new_tocheckfor);
745 }
746
747 if ($target eq "TABLE") {
748     foreach (sort keys %table) {
749         print_table_entry($_, "TABLE");
750     }
751     exit 0;
752 }
753
754 if ($target eq "LIST") {
755     foreach (sort keys %table) {
756         print $_,"\n" unless $table{$_}->{template};
757     }
758     exit 0;
759 }
760
761 if ($target eq "HASH") {
762     print "%table = (\n";
763     foreach (sort keys %table) {
764         print_table_entry($_, "HASH");
765     }
766     exit 0;
767 }
768
769 # Backward compatibility?
770 if ($target =~ m/^CygWin32(-.*)$/) {
771     $target = "Cygwin".$1;
772 }
773
774 foreach (sort (keys %disabled))
775         {
776         $config{options} .= " no-$_";
777
778         printf "    no-%-12s %-10s", $_, "[$disabled{$_}]";
779
780         if (/^dso$/)
781                 { $no_dso = 1; }
782         elsif (/^threads$/)
783                 { }
784         elsif (/^shared$/)
785                 { }
786         elsif (/^pic$/)
787                 { }
788         elsif (/^zlib$/)
789                 { $zlib = 0; }
790         elsif (/^dynamic-engine$/)
791                 { }
792         elsif (/^zlib-dynamic$/)
793                 { }
794         elsif (/^sse2$/)
795                 { $no_sse2 = 1; }
796         elsif (/^engine$/)
797                 {
798                 @{$config{dirs}} = grep !/^engines$/, @{$config{dirs}};
799                 @{$config{sdirs}} = grep !/^engine$/, @{$config{sdirs}};
800                 push @{$config{openssl_other_defines}}, "OPENSSL_NO_ENGINE";
801                 }
802         else
803                 {
804                 my ($ALGO, $algo);
805                 ($ALGO = $algo = $_) =~ tr/[\-a-z]/[_A-Z]/;
806
807                 if (/^asm$/ || /^err$/ || /^hw$/ || /^hw-/
808                                 || /^autoalginit/ || /^autoerrinit/)
809                         {
810                         push @{$config{openssl_other_defines}}, "OPENSSL_NO_$ALGO";
811                         print " OPENSSL_NO_$ALGO";
812
813                         if (/^err$/)    { push @user_defines, "OPENSSL_NO_ERR"; }
814                         elsif (/^asm$/) { $no_asm = 1; }
815                         }
816                 else
817                         {
818                         ($ALGO,$algo) = ("RMD160","rmd160") if ($algo eq "ripemd");
819
820                         push @{$config{openssl_algorithm_defines}}, "OPENSSL_NO_$ALGO";
821                         print " OPENSSL_NO_$ALGO";
822
823                         # fix-up crypto/directory name(s)
824                         $algo="whrlpool" if $algo eq "whirlpool";
825                         $algo="ripemd" if $algo eq "rmd160";
826                         @{$config{sdirs}} = grep { $_ ne $algo} @{$config{sdirs}};
827
828                         print " (skip dir)";
829                         }
830                 }
831
832         print "\n";
833         }
834
835 print "Configuring for $target\n";
836
837 # Support for legacy targets having a name starting with 'debug-'
838 my ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
839 if ($d) {
840     $build_prefix = "debug_";
841
842     # If we do not find debug-foo in the table, the target is set to foo.
843     if (!$table{$target}) {
844         $target = $t;
845     }
846 }
847 $config{target} = $target;
848 delete $table{$base_target}->{template}; # or the next test will fail.
849 my %target = ( %{$table{$base_target}}, resolve_config($target) );
850
851 &usage if (!%target || $target{template});
852
853 $target{exe_extension}="";
854 $target{exe_extension}=".exe" if ($config{target} eq "DJGPP"
855                                   || $config{target} =~ /^(?:Cygwin|mingw)/);
856 $target{exe_extension}=".nlm" if ($config{target} =~ /netware/);
857 $target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
858
859 ($target{shared_extension_simple}=$target{shared_extension})
860     =~ s|\.\$\(SHLIB_MAJOR\)\.\$\(SHLIB_MINOR\)||;
861 $target{dso_extension}=$target{shared_extension_simple};
862 ($target{shared_import_extension}=$target{shared_extension_simple}.".a")
863     if ($config{target} =~ /^(?:Cygwin|mingw)/);
864
865
866 $default_ranlib = which("ranlib") || "true";
867 $config{perl}   = $ENV{'PERL'} || which("perl5") || which("perl") || "perl";
868 my $make        = $ENV{'MAKE'} || "make";
869
870 $config{cross_compile_prefix} = $ENV{'CROSS_COMPILE'}
871     if $config{cross_compile_prefix} eq "";
872
873 # Allow environment CC to override compiler...
874 $target{cc} = $ENV{CC} || $target{cc};
875
876 # For cflags, lflags, plib_lflags, ex_libs and defines, add the debug_
877 # or release_ attributes.
878 # Do it in such a way that no spurious space is appended (hence the grep).
879 $config{defines} = [ @{$target{defines}},
880                      @{$target{$build_prefix."defines"}} ];
881 $config{cflags} = join(" ",
882                        grep { $_ ne "" } ($target{cflags},
883                                           $target{$build_prefix."cflags"}));
884 $config{lflags} = join(" ",
885                        grep { $_ ne "" } ($target{lflags},
886                                           $target{$build_prefix."lflags"}));
887 $config{plib_lflags} = join(" ",
888                             grep { $_  ne "" } ($target{plib_lflags},
889                                                 $target{$build_prefix."plib_lflags"}));
890 $config{ex_libs} = join(" ",
891                         grep { $_  ne "" } ($target{ex_libs},
892                                             $target{$build_prefix."ex_libs"}));
893
894 $target{ranlib} = $ENV{'RANLIB'} || $target{ranlib} || $default_ranlib;
895 $target{ar} = $ENV{'AR'} || "ar";
896 $target{arflags} = "" if !defined($target{arflags});
897 $target{nm} = "nm";
898 # Make sure build_scheme is consistent.
899 $target{build_scheme} = [ $target{build_scheme} ]
900     if ref($target{build_scheme}) ne "ARRAY";
901
902 ###### TO BE REMOVED BEFORE FINAL RELEASE
903 ######
904 ###### If the user has chosen --unified, we give it to them.
905 ###### The same happens if we detect that they try to build out-of-source.
906 if ($target{build_file} eq "Makefile"
907     && $target{build_scheme}->[0] eq "unixmake"
908     && ($unified || $srcdir ne $blddir)) {
909     $target{build_scheme} = [ "unified", "unix" ];
910 }
911
912 my ($builder, $builder_platform, @builder_opts) =
913     @{$target{build_scheme}};
914
915 if ($target =~ /^mingw/ && `$target{cc} --target-help 2>&1` =~ m/-mno-cygwin/m)
916         {
917         $config{cflags} .= " -mno-cygwin";
918         $target{shared_ldflag} .= " -mno-cygwin";
919         }
920
921 if ($target =~ /linux.*-mips/ && !$no_asm && $user_cflags !~ /-m(ips|arch=)/) {
922         # minimally required architecture flags for assembly modules
923         $config{cflags}="-mips2 $config{cflags}" if ($target =~ /mips32/);
924         $config{cflags}="-mips3 $config{cflags}" if ($target =~ /mips64/);
925 }
926
927 my $no_shared_warn=0;
928 my $no_user_cflags=0;
929 my $no_user_defines=0;
930
931 # The DSO code currently always implements all functions so that no
932 # applications will have to worry about that from a compilation point
933 # of view. However, the "method"s may return zero unless that platform
934 # has support compiled in for them. Currently each method is enabled
935 # by a define "DSO_<name>" ... we translate the "dso_scheme" config
936 # string entry into using the following logic;
937 if (!$no_dso && $target{dso_scheme} ne "")
938         {
939         $target{dso_scheme} =~ tr/[a-z]/[A-Z]/;
940         if ($target{dso_scheme} eq "DLFCN")
941                 {
942                 $config{defines} = [ "DSO_DLFCN", "HAVE_DLFCN_H",
943                                      @{$config{defines}} ]
944                 }
945         elsif ($target{dso_scheme} eq "DLFCN_NO_H")
946                 {
947                 $config{defines} = [ "DSO_DLFCN", @{$config{defines}} ]
948                 }
949         else
950                 {
951                 $config{defines} = [ "DSO_$target{dso_scheme}",
952                                      @{$config{defines}} ]
953                 }
954         }
955
956 my $thread_cflags = "";
957 my @thread_defines;
958 if ($target{thread_cflag} ne "(unknown)" && !$disabled{threads})
959         {
960         # If we know how to do it, support threads by default.
961         $threads = 1;
962         }
963 if ($target{thread_cflag} eq "(unknown)" && $threads)
964         {
965         # If the user asked for "threads", [s]he is also expected to
966         # provide any system-dependent compiler options that are
967         # necessary.
968         if ($no_user_cflags && $no_user_defines)
969                 {
970                 print "You asked for multi-threading support, but didn't\n";
971                 print "provide any system-specific compiler options\n";
972                 exit(1);
973                 }
974         push @thread_defines, "OPENSSL_THREADS";
975         }
976 else
977         {
978         $thread_cflags=" $target{thread_cflag}";
979         push @thread_defines, @{$target{thread_defines}}, "OPENSSL_THREADS";
980         }
981
982 $config{ex_libs}="$libs$config{ex_libs}" if ($libs ne "");
983
984 if ($no_asm)
985         {
986         @{$config{defines}} = grep !/^[BL]_ENDIAN$/, @{$config{defines}}
987             if ($config{fips});
988         }
989
990 if ($threads)
991         {
992         $config{cflags} = "$thread_cflags $config{cflags}" if $thread_cflags;
993         push @{$config{defines}}, @thread_defines;
994         push @{$config{openssl_thread_defines}}, @thread_defines;
995         }
996
997 if ($zlib)
998         {
999         push @{$config{defines}}, "ZLIB";
1000         if (defined($disabled{"zlib-dynamic"}))
1001                 {
1002                 if (defined($withargs{zlib_lib}))
1003                         {
1004                         $config{ex_libs} .= " -L" . $withargs{zlib_lib} . " -lz";
1005                         }
1006                 else
1007                         {
1008                         $config{ex_libs} .= " -lz";
1009                         }
1010                 }
1011         else
1012                 {
1013                 push @{$config{defines}}, "ZLIB_SHARED";
1014                 }
1015         }
1016
1017 # With "deprecated" disable all deprecated features.
1018 if (defined($disabled{"deprecated"})) {
1019         $config{api} = $maxapi;
1020 }
1021
1022 if ($target{shared_target} eq "")
1023         {
1024         $no_shared_warn = 1
1025             if ((!$disabled{shared} || !$disabled{"dynamic-engine"})
1026                 && !$config{fips});
1027         $disabled{shared} = "no-shared-target";
1028         $disabled{pic} = $disabled{shared} = $disabled{"dynamic-engine"} =
1029             "no-shared-target";
1030         }
1031
1032 if ($disabled{"dynamic-engine"}) {
1033         push @{$config{defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
1034         $config{dynamic_engines} = 0;
1035 } else {
1036         push @{$config{defines}}, "OPENSSL_NO_STATIC_ENGINE";
1037         $config{dynamic_engines} = 1;
1038 }
1039
1040 #
1041 # Platform fix-ups
1042 #
1043
1044 # This saves the build files from having to check
1045 if ($disabled{pic})
1046         {
1047         $target{shared_cflag} = $target{shared_ldflag} =
1048                 $target{shared_rcflag} = "";
1049         }
1050
1051 if ($target{sys_id} ne "")
1052         {
1053         push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
1054         }
1055
1056 if ($target{ranlib} eq "")
1057         {
1058         $target{ranlib} = $default_ranlib;
1059         }
1060
1061 if (!$no_asm) {
1062     $target{cpuid_asm_src}=$table{BASE}->{cpuid_asm_src} if ($config{processor} eq "386");
1063     $target{bn_asm_src} =~ s/\w+-gf2m.c// if (defined($disabled{ec2m}));
1064
1065     # bn-586 is the only one implementing bn_*_part_words
1066     push @{$config{defines}}, "OPENSSL_BN_ASM_PART_WORDS" if ($target{bn_asm_src} =~ /bn-586/);
1067     push @{$config{defines}}, "OPENSSL_IA32_SSE2" if (!$no_sse2 && $target{bn_asm_src} =~ /86/);
1068
1069     push @{$config{defines}}, "OPENSSL_BN_ASM_MONT" if ($target{bn_asm_src} =~ /-mont/);
1070     push @{$config{defines}}, "OPENSSL_BN_ASM_MONT5" if ($target{bn_asm_src} =~ /-mont5/);
1071     push @{$config{defines}}, "OPENSSL_BN_ASM_GF2m" if ($target{bn_asm_src} =~ /-gf2m/);
1072
1073     if ($config{fips}) {
1074         push @{$config{openssl_other_defines}}, "OPENSSL_FIPS";
1075     }
1076
1077     if ($target{sha1_asm_src}) {
1078         push @{$config{defines}}, "SHA1_ASM"   if ($target{sha1_asm_src} =~ /sx86/ || $target{sha1_asm_src} =~ /sha1/);
1079         push @{$config{defines}}, "SHA256_ASM" if ($target{sha1_asm_src} =~ /sha256/);
1080         push @{$config{defines}}, "SHA512_ASM" if ($target{sha1_asm_src} =~ /sha512/);
1081     }
1082     if ($target{md5_asm_src}) {
1083         push @{$config{defines}}, "MD5_ASM";
1084     }
1085     $target{cast_asm_src}=$table{BASE}->{cast_asm_src} unless $disabled{pic}; # CAST assembler is not PIC
1086     if ($target{rmd160_asm_src}) {
1087         push @{$config{defines}}, "RMD160_ASM";
1088     }
1089     if ($target{aes_asm_src}) {
1090         push @{$config{defines}}, "AES_ASM" if ($target{aes_asm_src} =~ m/\baes-/);;
1091         # aes-ctr.fake is not a real file, only indication that assembler
1092         # module implements AES_ctr32_encrypt...
1093         push @{$config{defines}}, "AES_CTR_ASM" if ($target{aes_asm_src} =~ s/\s*aes-ctr\.fake//);
1094         # aes-xts.fake indicates presence of AES_xts_[en|de]crypt...
1095         push @{$config{defines}}, "AES_XTS_ASM" if ($target{aes_asm_src} =~ s/\s*aes-xts\.fake//);
1096         $target{aes_asm_src} =~ s/\s*(vpaes|aesni)-x86\.s//g if ($no_sse2);
1097         push @{$config{defines}}, "VPAES_ASM" if ($target{aes_asm_src} =~ m/vpaes/);
1098         push @{$config{defines}}, "BSAES_ASM" if ($target{aes_asm_src} =~ m/bsaes/);
1099     }
1100     if ($target{wp_asm_src} =~ /mmx/) {
1101         if ($config{processor} eq "386") {
1102             $target{wp_asm_src}=$table{BASE}->{wp_asm_src};
1103         } elsif (!$disabled{"whirlpool"}) {
1104             $config{cflags}.=" -DWHIRLPOOL_ASM";
1105         }
1106     }
1107     if ($target{modes_asm_src} =~ /ghash-/) {
1108         push @{$config{defines}}, "GHASH_ASM";
1109     }
1110     if ($target{ec_asm_src} =~ /ecp_nistz256/) {
1111         push @{$config{defines}}, "ECP_NISTZ256_ASM";
1112     }
1113     if ($target{poly1305_asm_src} ne "") {
1114         push @{$config{defines}}, "POLY1305_ASM";
1115     }
1116 }
1117
1118 my $ecc = $target{cc};
1119 if ($^O ne "VMS") {
1120     # Is the compiler gcc or clang?  $ecc is used below to see if
1121     # error-checking can be turned on.
1122     my $ccpcc = "$config{cross_compile_prefix}$target{cc}";
1123     $config{makedepprog} = 'makedepend';
1124     open(PIPE, "$ccpcc --version 2>&1 | head -2 |");
1125     while ( <PIPE> ) {
1126         $config{makedepprog} = $ccpcc if /clang|gcc/;
1127         $ecc = "clang" if /clang/;
1128         $ecc = "gcc" if /gcc/;
1129     }
1130     close(PIPE);
1131 }
1132
1133
1134 # Deal with bn_ops ###################################################
1135
1136 $config{bn_ll}                  =0;
1137 $config{export_var_as_fn}       =0;
1138 my $def_int="unsigned int";
1139 $config{rc4_int}                =$def_int;
1140 ($config{b64l},$config{b64},$config{b32})=(0,0,1);
1141
1142 my $count = 0;
1143 foreach (sort split(/\s+/,$target{bn_ops})) {
1144     $count++ if /SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT/;
1145     $config{export_var_as_fn}=1                 if $_ eq 'EXPORT_VAR_AS_FN';
1146     $config{bn_ll}=1                            if $_ eq 'BN_LLONG';
1147     $config{rc4_int}="unsigned char"            if $_ eq 'RC4_CHAR';
1148     ($config{b64l},$config{b64},$config{b32})
1149         =(0,1,0)                                if $_ eq 'SIXTY_FOUR_BIT';
1150     ($config{b64l},$config{b64},$config{b32})
1151         =(1,0,0)                                if $_ eq 'SIXTY_FOUR_BIT_LONG';
1152     ($config{b64l},$config{b64},$config{b32})
1153         =(0,0,1)                                if $_ eq 'THIRTY_TWO_BIT';
1154 }
1155 die "Exactly one of SIXTY_FOUR_BIT|SIXTY_FOUR_BIT_LONG|THIRTY_TWO_BIT can be set in bn_ops\n"
1156     if $count > 1;
1157
1158
1159 # Hack cflags for better warnings (dev option) #######################
1160
1161 # "Stringify" the C flags string.  This permits it to be made part of a string
1162 # and works as well on command lines.
1163 $config{cflags} =~ s/([\\\"])/\\\1/g;
1164
1165 if (defined($config{api})) {
1166     $config{openssl_api_defines} = [ "OPENSSL_MIN_API=".$apitable->{$config{api}} ];
1167     my $apiflag = sprintf("OPENSSL_API_COMPAT=%s", $apitable->{$config{api}});
1168     push @{$config{defines}}, $apiflag;
1169 }
1170
1171 if ($strict_warnings)
1172         {
1173         my $wopt;
1174         die "ERROR --strict-warnings requires gcc or clang"
1175             unless $ecc eq 'gcc' || $ecc eq 'clang';
1176         foreach $wopt (split /\s+/, $gcc_devteam_warn)
1177                 {
1178                 $config{cflags} .= " $wopt" unless ($config{cflags} =~ /(?:^|\s)$wopt(?:\s|$)/)
1179                 }
1180         if ($ecc eq "clang")
1181                 {
1182                 foreach $wopt (split /\s+/, $clang_devteam_warn)
1183                         {
1184                         $config{cflags} .= " $wopt" unless ($config{cflags} =~ /(?:^|\s)$wopt(?:\s|$)/)
1185                         }
1186                 }
1187         }
1188
1189 unless ($disabled{"crypto-mdebug-backtrace"})
1190         {
1191         foreach my $wopt (split /\s+/, $memleak_devteam_backtrace)
1192                 {
1193                 $config{cflags} .= " $wopt" unless ($config{cflags} =~ /(?:^|\s)$wopt(?:\s|$)/)
1194                 }
1195         if ($target =~ /^BSD-/)
1196                 {
1197                 $config{ex_libs} .= " -lexecinfo";
1198                 }
1199         }
1200
1201 if ($user_cflags ne "") { $config{cflags}="$config{cflags}$user_cflags"; }
1202 else                    { $no_user_cflags=1;  }
1203 if (@user_defines) { $config{defines}=[ @{$config{defines}}, @user_defines ]; }
1204 else               { $no_user_defines=1;    }
1205
1206 # ALL MODIFICATIONS TO %config and %target MUST BE DONE FROM HERE ON
1207
1208 # If we use the unified build, collect information from build.info files
1209 my %unified_info = ();
1210
1211 if ($builder eq "unified") {
1212     # Store the name of the template file we will build the build file from
1213     # in %config.  This may be useful for the build file itself.
1214     my $build_file_template =
1215         catfile($srcdir, "Configurations",
1216                 $builder_platform."-".$target{build_file}.".tmpl");
1217     $build_file_template =
1218         catfile($srcdir, "Configurations", $target{build_file}.".tmpl")
1219         if (! -f $build_file_template);
1220     $config{build_file_template} = $build_file_template;
1221
1222     use lib catdir(dirname(__FILE__),"util");
1223     use with_fallback qw(Text::Template);
1224
1225     sub cleandir {
1226         my $base = shift;
1227         my $dir = shift;
1228         my $relativeto = shift || ".";
1229
1230         $dir = catdir($base,$dir) unless isabsolute($dir);
1231
1232         # Make sure the directories we're building in exists
1233         mkpath($dir);
1234
1235         my $res = abs2rel(absolutedir($dir), rel2abs($relativeto));
1236         #print STDERR "DEBUG[cleandir]: $dir , $base => $res\n";
1237         return $res;
1238     }
1239
1240     sub cleanfile {
1241         my $base = shift;
1242         my $file = shift;
1243         my $relativeto = shift || ".";
1244
1245         $file = catfile($base,$file) unless isabsolute($file);
1246
1247         my $d = dirname($file);
1248         my $f = basename($file);
1249
1250         # Make sure the directories we're building in exists
1251         mkpath($d);
1252
1253         my $res = abs2rel(catfile(absolutedir($d), $f), rel2abs($relativeto));
1254         #print STDERR "DEBUG[cleanfile]: $d , $f => $res\n";
1255         return $res;
1256     }
1257
1258     my @build_infos = ( [ ".", "build.info" ] );
1259     foreach (@{$config{dirs}}) {
1260         push @build_infos, [ $_, "build.info" ]
1261             if (-f catfile($srcdir, $_, "build.info"));
1262     }
1263     foreach (@{$config{sdirs}}) {
1264         push @build_infos, [ catdir("crypto", $_), "build.info" ]
1265             if (-f catfile($srcdir, "crypto", $_, "build.info"));
1266     }
1267     foreach (@{$config{engdirs}}) {
1268         push @build_infos, [ catdir("engines", $_), "build.info" ]
1269             if (-f catfile($srcdir, "engines", $_, "build.info"));
1270     }
1271
1272     foreach (@build_infos) {
1273         my $sourced = catdir($srcdir, $_->[0]);
1274         my $buildd = catdir($blddir, $_->[0]);
1275
1276         mkpath($buildd);
1277
1278         my $f = $_->[1];
1279         # The basic things we're trying to build
1280         my @programs = ();
1281         my @libraries = ();
1282         my @engines = ();
1283         my @scripts = ();
1284         my @extra = ();
1285         my @intermediates = ();
1286         my @rawlines = ();
1287
1288         my %ordinals = ();
1289         my %sources = ();
1290         my %includes = ();
1291         my %depends = ();
1292         my %renames = ();
1293         my %sharednames = ();
1294
1295         my $template = Text::Template->new(TYPE => 'FILE',
1296                                            SOURCE => catfile($sourced, $f));
1297         die "Something went wrong with $sourced/$f: $!\n" unless $template;
1298         my @text =
1299             split /^/m,
1300             $template->fill_in(HASH => { config => \%config,
1301                                          target => \%target,
1302                                          disabled => \%disabled,
1303                                          builddir => abs2rel($buildd, $blddir),
1304                                          sourcedir => abs2rel($sourced, $blddir),
1305                                          buildtop => abs2rel($blddir, $blddir),
1306                                          sourcetop => abs2rel($srcdir, $blddir) },
1307                                DELIMITERS => [ "{-", "-}" ]);
1308
1309         # The top item of this stack has the following values
1310         # -2 positive already run and we found ELSE (following ELSIF should fail)
1311         # -1 positive already run (skip until ENDIF)
1312         # 0 negatives so far (if we're at a condition, check it)
1313         # 1 last was positive (don't skip lines until next ELSE, ELSIF or ENDIF)
1314         # 2 positive ELSE (following ELSIF should fail)
1315         my @skip = ();
1316         collect_information(
1317             collect_from_array([ @text ],
1318                                qr/\\$/ => sub { my $l1 = shift; my $l2 = shift;
1319                                                 $l1 =~ s/\\$//; $l1.$l2 }),
1320             # Info we're looking for
1321             qr/^\s*IF\[((?:\\.|[^\\\]])*)\]\s*$/
1322             => sub { push @skip, !! $1; },
1323             qr/^\s*ELSIF\[((?:\\.|[^\\\]])*)\]\s*$/
1324             => sub { die "ELSIF out of scope" if ! @skip;
1325                      die "ELSIF following ELSE" if abs($skip[$#skip]) == 2;
1326                      $skip[$#skip] = -1 if $skip[$#skip] != 0;
1327                      $skip[$#skip] = !! $1
1328                          if $skip[$#skip] == 0; },
1329             qr/^\s*ELSE\s*$/
1330             => sub { die "ELSE out of scope" if ! @skip;
1331                      $skip[$#skip] = -2 if $skip[$#skip] != 0;
1332                      $skip[$#skip] = 2 if $skip[$#skip] == 0; },
1333             qr/^\s*ENDIF\s*$/
1334             => sub { die "ENDIF out of scope" if ! @skip;
1335                      pop @skip; },
1336             qr/^\s*PROGRAMS\s*=\s*(.*)\s*$/
1337             => sub { push @programs, split(/\s+/, $1)
1338                          if !@skip || $skip[$#skip] > 0 },
1339             qr/^\s*LIBS\s*=\s*(.*)\s*$/
1340             => sub { push @libraries, split(/\s+/, $1)
1341                          if !@skip || $skip[$#skip] > 0 },
1342             qr/^\s*ENGINES\s*=\s*(.*)\s*$/
1343             => sub { push @engines, split(/\s+/, $1)
1344                          if !@skip || $skip[$#skip] > 0 },
1345             qr/^\s*SCRIPTS\s*=\s*(.*)\s*$/
1346             => sub { push @scripts, split(/\s+/, $1)
1347                          if !@skip || $skip[$#skip] > 0 },
1348             qr/^\s*EXTRA\s*=\s*(.*)\s*$/
1349             => sub { push @extra, split(/\s+/, $1)
1350                          if !@skip || $skip[$#skip] > 0 },
1351
1352             qr/^\s*ORDINALS\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/,
1353             => sub { push @{$ordinals{$1}}, split(/\s+/, $2)
1354                          if !@skip || $skip[$#skip] > 0 },
1355             qr/^\s*SOURCE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1356             => sub { push @{$sources{$1}}, split(/\s+/, $2)
1357                          if !@skip || $skip[$#skip] > 0 },
1358             qr/^\s*INCLUDE\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1359             => sub { push @{$includes{$1}}, split(/\s+/, $2)
1360                          if !@skip || $skip[$#skip] > 0 },
1361             qr/^\s*DEPEND\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1362             => sub { push @{$depends{$1}}, split(/\s+/, $2)
1363                          if !@skip || $skip[$#skip] > 0 },
1364             qr/^\s*RENAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1365             => sub { push @{$renames{$1}}, split(/\s+/, $2)
1366                          if !@skip || $skip[$#skip] > 0 },
1367             qr/^\s*SHARED_NAME\[((?:\\.|[^\\\]])+)\]\s*=\s*(.*)\s*$/
1368             => sub { push @{$sharednames{$1}}, split(/\s+/, $2)
1369                          if !@skip || $skip[$#skip] > 0 },
1370             qr/^\s*BEGINRAW\[((?:\\.|[^\\\]])+)\]\s*$/
1371             => sub {
1372                 my $lineiterator = shift;
1373                 my $target_kind = $1;
1374                 while (defined $lineiterator->()) {
1375                     s|\R$||;
1376                     if (/^\s*ENDRAW\[((?:\\.|[^\\\]])+)\]\s*$/) {
1377                         die "ENDRAW doesn't match BEGINRAW"
1378                             if $1 ne $target_kind;
1379                         last;
1380                     }
1381                     next if @skip && $skip[$#skip] <= 0;
1382                     push @rawlines,  $_
1383                         if ($target_kind eq $target{build_file}
1384                             || $target_kind eq $target{build_file}."(".$builder_platform.")");
1385                 }
1386             },
1387             qr/^(?:#.*|\s*)$/ => sub { },
1388             "OTHERWISE" => sub { die "Something wrong with this line:\n$_\nat $sourced/$f" }
1389             );
1390         die "runaway IF?" if (@skip);
1391
1392         foreach (keys %renames) {
1393             die "$_ renamed to more than one thing: "
1394                 ,join(" ", @{$renames{$_}}),"\n"
1395                 if scalar @{$renames{$_}} > 1;
1396             my $dest = cleanfile($buildd, $_, $blddir);
1397             my $to = cleanfile($buildd, $renames{$_}->[0], $blddir);
1398             die "$dest renamed to more than one thing: "
1399                 ,$unified_info{rename}->{$dest}, $to
1400                 unless !defined($unified_info{rename}->{$dest})
1401                 or $unified_info{rename}->{$dest} eq $to;
1402             $unified_info{rename}->{$dest} = $to;
1403         }
1404
1405         foreach (@programs) {
1406             my $program = cleanfile($buildd, $_, $blddir);
1407             if ($unified_info{rename}->{$program}) {
1408                 $program = $unified_info{rename}->{$program};
1409             }
1410             $unified_info{programs}->{$program} = 1;
1411         }
1412
1413         foreach (@libraries) {
1414             my $library = cleanfile($buildd, $_, $blddir);
1415             if ($unified_info{rename}->{$library}) {
1416                 $library = $unified_info{rename}->{$library};
1417             }
1418             $unified_info{libraries}->{$library} = 1;
1419         }
1420
1421         die <<"EOF" if scalar @engines and !$config{dynamic_engines};
1422 ENGINES can only be used if configured with 'dynamic-engine'.
1423 This is usually a fault in a build.info file.
1424 EOF
1425         foreach (@engines) {
1426             my $library = cleanfile($buildd, $_, $blddir);
1427             if ($unified_info{rename}->{$library}) {
1428                 $library = $unified_info{rename}->{$library};
1429             }
1430             $unified_info{engines}->{$library} = 1;
1431         }
1432
1433         foreach (@scripts) {
1434             my $script = cleanfile($buildd, $_, $blddir);
1435             if ($unified_info{rename}->{$script}) {
1436                 $script = $unified_info{rename}->{$script};
1437             }
1438             $unified_info{scripts}->{$script} = 1;
1439         }
1440
1441         foreach (@extra) {
1442             my $extra = cleanfile($buildd, $_, $blddir);
1443             $unified_info{extra}->{$extra} = 1;
1444         }
1445
1446         push @{$unified_info{rawlines}}, @rawlines;
1447
1448         unless ($disabled{shared}) {
1449             # Check sharednames.
1450             foreach (keys %sharednames) {
1451                 my $dest = cleanfile($buildd, $_, $blddir);
1452                 if ($unified_info{rename}->{$dest}) {
1453                     $dest = $unified_info{rename}->{$dest};
1454                 }
1455                 die "shared_name for $dest with multiple values: "
1456                     ,join(" ", @{$sharednames{$_}}),"\n"
1457                     if scalar @{$sharednames{$_}} > 1;
1458                 my $to = cleanfile($buildd, $sharednames{$_}->[0], $blddir);
1459                 die "shared_name found for a library $dest that isn't defined\n"
1460                     unless $unified_info{libraries}->{$dest};
1461                 die "shared_name for $dest with multiple values: "
1462                     ,$unified_info{sharednames}->{$dest}, ", ", $to
1463                     unless !defined($unified_info{sharednames}->{$dest})
1464                     or $unified_info{sharednames}->{$dest} eq $to;
1465                 $unified_info{sharednames}->{$dest} = $to;
1466             }
1467
1468             # Additionally, we set up sharednames for libraries that don't
1469             # have any, as themselves.
1470             foreach (keys %{$unified_info{libraries}}) {
1471                 if (!defined $unified_info{sharednames}->{$_}) {
1472                     $unified_info{sharednames}->{$_} = $_
1473                 }
1474             }
1475         }
1476
1477         foreach (keys %ordinals) {
1478             my $dest = $_;
1479             my $ddest = cleanfile($buildd, $_, $blddir);
1480             if ($unified_info{rename}->{$ddest}) {
1481                 $ddest = $unified_info{rename}->{$ddest};
1482             }
1483             foreach (@{$ordinals{$dest}}) {
1484                 my %known_ordinals =
1485                     (
1486                      crypto =>
1487                      cleanfile($sourced, catfile("util", "libeay.num"), $blddir),
1488                      ssl =>
1489                      cleanfile($sourced, catfile("util", "ssleay.num"), $blddir)
1490                     );
1491                 my $o = $known_ordinals{$_};
1492                 die "Ordinals for $ddest defined more than once\n"
1493                     if $unified_info{ordinals}->{$ddest};
1494                 $unified_info{ordinals}->{$ddest} = [ $_, $o ];
1495             }
1496         }
1497
1498         foreach (keys %sources) {
1499             my $dest = $_;
1500             my $ddest = cleanfile($buildd, $_, $blddir);
1501             if ($unified_info{rename}->{$ddest}) {
1502                 $ddest = $unified_info{rename}->{$ddest};
1503             }
1504             foreach (@{$sources{$dest}}) {
1505                 my $s = cleanfile($sourced, $_, $blddir);
1506
1507                 # If it isn't in the source tree, we assume it's generated
1508                 # in the build tree
1509                 if (! -f $s) {
1510                     $s = cleanfile($buildd, $_, $blddir);
1511                 }
1512                 # We recognise C and asm files
1513                 if ($s =~ /\.[csS]\b$/) {
1514                     (my $o = $_) =~ s/\.[csS]\b$/.o/;
1515                     $o = cleanfile($buildd, $o, $blddir);
1516                     $unified_info{sources}->{$ddest}->{$o} = 1;
1517                     $unified_info{sources}->{$o}->{$s} = 1;
1518                 } else {
1519                     $unified_info{sources}->{$ddest}->{$s} = 1;
1520                 }
1521             }
1522         }
1523
1524         foreach (keys %depends) {
1525             my $dest = $_;
1526             my $ddest = cleanfile($buildd, $_, $blddir);
1527             if ($unified_info{rename}->{$ddest}) {
1528                 $ddest = $unified_info{rename}->{$ddest};
1529             }
1530             foreach (@{$depends{$dest}}) {
1531                 my $d = cleanfile($sourced, $_, $blddir);
1532
1533                 # If we know it's generated, or assume it is because we can't
1534                 # find it in the source tree, we set file we depend on to be
1535                 # in the build tree rather than the source tree, and assume
1536                 # and that there are lines to build it in a BEGINRAW..ENDRAW
1537                 # section or in the Makefile template.
1538                 if (! -f $d
1539                     || !(grep { $d eq $_ }
1540                          map { cleanfile($srcdir, $_, $blddir) }
1541                          (@generated_headers, @generated_by_make_headers))) {
1542                     $d = cleanfile($buildd, $_, $blddir);
1543                 }
1544                 # Take note if the file to depend on is being renamed
1545                 if ($unified_info{rename}->{$d}) {
1546                     $d = $unified_info{rename}->{$d};
1547                 }
1548                 $unified_info{depends}->{$ddest}->{$d} = 1;
1549                 # If we depend on a header file, let's make sure it
1550                 # can get included
1551                 if ($d =~ /\.h$/) {
1552                     my $i = dirname($d);
1553                     push @{$unified_info{includes}->{$ddest}}, $i
1554                         unless grep { $_ eq $i } @{$unified_info{includes}->{$ddest}};
1555                 }
1556             }
1557         }
1558
1559         foreach (keys %includes) {
1560             my $dest = $_;
1561             my $ddest = cleanfile($buildd, $_, $blddir);
1562             if ($unified_info{rename}->{$ddest}) {
1563                 $ddest = $unified_info{rename}->{$ddest};
1564             }
1565             foreach (@{$includes{$dest}}) {
1566                 my $i = cleandir($sourced, $_, $blddir);
1567                 push @{$unified_info{includes}->{$ddest}}, $i
1568                     unless grep { $_ eq $i } @{$unified_info{includes}->{$ddest}};
1569             }
1570         }
1571     }
1572
1573     ### Make unified_info a bit more efficient
1574     # One level structures
1575     foreach (("programs", "libraries", "engines", "scripts", "extra")) {
1576         $unified_info{$_} = [ sort keys %{$unified_info{$_}} ];
1577     }
1578     # Two level structures
1579     foreach my $l1 (("sources", "ldadd", "depends")) {
1580         foreach my $l2 (sort keys %{$unified_info{$l1}}) {
1581             $unified_info{$l1}->{$l2} =
1582                 [ sort keys %{$unified_info{$l1}->{$l2}} ];
1583         }
1584     }
1585 }
1586
1587 # For the schemes that need it, we provide the old *_obj configs
1588 # from the *_asm_obj ones
1589 foreach (grep /_(asm|aux)_src$/, keys %target) {
1590     my $src = $_;
1591     (my $obj = $_) =~ s/_(asm|aux)_src$/_obj/;
1592     ($target{$obj} = $target{$src}) =~ s/\.[csS]\b/.o/g;
1593 }
1594
1595 # Write down our configuration where it fits #########################
1596
1597 open(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
1598 print OUT <<"EOF";
1599 package configdata;
1600
1601 use strict;
1602 use warnings;
1603
1604 use Exporter;
1605 #use vars qw(\@ISA \@EXPORT);
1606 our \@ISA = qw(Exporter);
1607 our \@EXPORT = qw(\%config \%target %withargs %unified_info);
1608
1609 EOF
1610 print OUT "our %config = (\n";
1611 foreach (sort keys %config) {
1612     if (ref($config{$_}) eq "ARRAY") {
1613         print OUT "  ", $_, " => [ ", join(", ",
1614                                            map { quotify("perl", $_) }
1615                                            @{$config{$_}}), " ],\n";
1616     } else {
1617         print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
1618     }
1619 }
1620 print OUT <<"EOF";
1621 );
1622
1623 EOF
1624 print OUT "our %target = (\n";
1625 foreach (sort keys %target) {
1626     if (ref($target{$_}) eq "ARRAY") {
1627         print OUT "  ", $_, " => [ ", join(", ",
1628                                            map { quotify("perl", $_) }
1629                                            @{$target{$_}}), " ],\n";
1630     } else {
1631         print OUT "  ", $_, " => ", quotify("perl", $target{$_}), ",\n"
1632     }
1633 }
1634 print OUT <<"EOF";
1635 );
1636
1637 EOF
1638 print OUT "our \%available_protocols = (\n";
1639 print OUT "  tls => [ ", join(", ", map { quotify("perl", $_) } @tls), " ],\n";
1640 print OUT "  dtls => [ ", join(", ", map { quotify("perl", $_) } @dtls), " ],\n";
1641 print OUT <<"EOF";
1642 );
1643
1644 EOF
1645 print OUT "our \%disabled = (\n";
1646 foreach (sort keys %disabled) {
1647     print OUT "  ", quotify("perl", $_), " => ", quotify("perl", $disabled{$_}), ",\n";
1648 }
1649 print OUT <<"EOF";
1650 );
1651
1652 EOF
1653 print OUT "our %withargs = (\n";
1654 foreach (sort keys %withargs) {
1655     if (ref($withargs{$_}) eq "ARRAY") {
1656         print OUT "  ", $_, " => [ ", join(", ",
1657                                            map { quotify("perl", $_) }
1658                                            @{$withargs{$_}}), " ],\n";
1659     } else {
1660         print OUT "  ", $_, " => ", quotify("perl", $withargs{$_}), ",\n"
1661     }
1662 }
1663 print OUT <<"EOF";
1664 );
1665
1666 EOF
1667 if ($builder eq "unified") {
1668     my $recurse;
1669     $recurse = sub {
1670         my $indent = shift;
1671         foreach (@_) {
1672             if (ref $_ eq "ARRAY") {
1673                 print OUT " "x$indent, "[\n";
1674                 foreach (@$_) {
1675                     $recurse->($indent + 4, $_);
1676                 }
1677                 print OUT " "x$indent, "],\n";
1678             } elsif (ref $_ eq "HASH") {
1679                 my %h = %$_;
1680                 print OUT " "x$indent, "{\n";
1681                 foreach (sort keys %h) {
1682                     if (ref $h{$_} eq "") {
1683                         print OUT " "x($indent + 4), quotify("perl", $_), " => ", quotify("perl", $h{$_}), ",\n";
1684                     } else {
1685                         print OUT " "x($indent + 4), quotify("perl", $_), " =>\n";
1686                         $recurse->($indent + 8, $h{$_});
1687                     }
1688                 }
1689                 print OUT " "x$indent, "},\n";
1690             } else {
1691                 print OUT " "x$indent, quotify("perl", $_), ",\n";
1692             }
1693         }
1694     };
1695     print OUT "our %unified_info = (\n";
1696     foreach (sort keys %unified_info) {
1697         if (ref $unified_info{$_} eq "") {
1698             print OUT " "x4, quotify("perl", $_), " => ", quotify("perl", $unified_info{$_}), ",\n";
1699         } else {
1700             print OUT " "x4, quotify("perl", $_), " =>\n";
1701             $recurse->(8, $unified_info{$_});
1702         }
1703     }
1704     print OUT <<"EOF";
1705 );
1706
1707 EOF
1708 }
1709 print OUT "1;\n";
1710 close(OUT);
1711
1712
1713 print "IsMK1MF       =", ($builder eq "mk1mf" ? "yes" : "no"), "\n";
1714 print "CC            =$target{cc}\n";
1715 print "CFLAG         =$config{cflags}\n";
1716 print "DEFINES       =",join(" ", @{$config{defines}}),"\n";
1717 print "LFLAG         =$config{lflags}\n";
1718 print "PLIB_LFLAG    =$config{plib_lflags}\n";
1719 print "EX_LIBS       =$config{ex_libs}\n";
1720 print "CPUID_OBJ     =$target{cpuid_obj}\n";
1721 print "BN_ASM        =$target{bn_obj}\n";
1722 print "EC_ASM        =$target{ec_obj}\n";
1723 print "DES_ENC       =$target{des_obj}\n";
1724 print "AES_ENC       =$target{aes_obj}\n";
1725 print "BF_ENC        =$target{bf_obj}\n";
1726 print "CAST_ENC      =$target{cast_obj}\n";
1727 print "RC4_ENC       =$target{rc4_obj}\n";
1728 print "RC5_ENC       =$target{rc5_obj}\n";
1729 print "MD5_OBJ_ASM   =$target{md5_obj}\n";
1730 print "SHA1_OBJ_ASM  =$target{sha1_obj}\n";
1731 print "RMD160_OBJ_ASM=$target{rmd160_obj}\n";
1732 print "CMLL_ENC      =$target{cmll_obj}\n";
1733 print "MODES_OBJ     =$target{modes_obj}\n";
1734 print "PADLOCK_OBJ   =$target{padlock_obj}\n";
1735 print "CHACHA_ENC    =$target{chacha_obj}\n";
1736 print "POLY1305_OBJ  =$target{poly1305_obj}\n";
1737 print "PROCESSOR     =$config{processor}\n";
1738 print "RANLIB        =$target{ranlib}\n";
1739 print "ARFLAGS       =$target{arflags}\n";
1740 print "PERL          =$config{perl}\n";
1741 print "\n";
1742 print "SIXTY_FOUR_BIT_LONG mode\n" if $config{b64l};
1743 print "SIXTY_FOUR_BIT mode\n" if $config{b64};
1744 print "THIRTY_TWO_BIT mode\n" if $config{b32};
1745 print "BN_LLONG mode\n" if $config{bn_ll};
1746 print "RC4 uses $config{rc4_int}\n" if $config{rc4_int} != $def_int;
1747
1748 for (@generated_headers) {
1749     mkpath(catdir($blddir, dirname($_)));
1750     run_dofile(catfile($blddir, $_),
1751                catfile($srcdir, $_.".in"));
1752 }
1753
1754 ###
1755 ### When the old "unixmake" scheme goes away, so does this function
1756 ###
1757 sub build_Makefile {
1758     run_dofile("Makefile","Makefile.in");
1759
1760     # Copy all Makefile.in to Makefile (except top-level)
1761     use File::Find;
1762     use IO::File;
1763     find(
1764         {
1765             preprocess => sub {
1766                 grep(!/^\./, @_);
1767             },
1768             wanted => sub {
1769                 return if ($_ ne "Makefile.in" || $File::Find::dir eq ".");
1770                 my $in = IO::File->new($_, "r") or
1771                     die sprintf "Error reading Makefile.in in %s: !$\n",
1772                     $File::Find::dir;
1773                 my $out = IO::File->new("Makefile", "w") or
1774                     die sprintf "Error writing Makefile in %s: !$\n",
1775                     $File::Find::dir;
1776                 print $out "# Generated from $_, do not edit\n";
1777                 while (my $line = <$in>) { print $out $line }
1778                 $in->close() or
1779                     die sprintf "Error reading Makefile.in in %s: !$\n",
1780                     $File::Find::dir;
1781                 $out->close() or
1782                     die sprintf "Error writing Makefile in %s: !$\n",
1783                     $File::Find::dir;
1784             },
1785         },
1786         ".");
1787 }
1788
1789 my %builders = (
1790     unified => sub {
1791         run_dofile(catfile($blddir, $target{build_file}),
1792                    $config{build_file_template},
1793                    catfile($srcdir, "Configurations", "common.tmpl"));
1794     },
1795     unixmake => sub {
1796         build_Makefile();
1797
1798         run_dofile("util/domd", "util/domd.in");
1799         chmod 0755, "util/domd";
1800     },
1801     mk1mf => sub {
1802         my $platform = shift;
1803         # The only reason we do this is to have something to build MINFO from
1804         build_Makefile();
1805
1806         # create the ms/version32.rc file if needed
1807         if ($platform ne "netware") {
1808             my ($v1, $v2, $v3, $v4);
1809             if ($config{version_num} =~ /^0x([0-9a-f]{1})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{1})L$/i) {
1810                 $v1=hex $1;
1811                 $v2=hex $2;
1812                 $v3=hex $3;
1813                 $v4=hex $4;
1814             }
1815             open (OUT,">ms/version32.rc") || die "Can't open ms/version32.rc";
1816             print OUT <<"EOF";
1817 #include <winver.h>
1818
1819 LANGUAGE 0x09,0x01
1820
1821 1 VERSIONINFO
1822   FILEVERSION $v1,$v2,$v3,$v4
1823   PRODUCTVERSION $v1,$v2,$v3,$v4
1824   FILEFLAGSMASK 0x3fL
1825 #ifdef _DEBUG
1826   FILEFLAGS 0x01L
1827 #else
1828   FILEFLAGS 0x00L
1829 #endif
1830   FILEOS VOS__WINDOWS32
1831   FILETYPE VFT_DLL
1832   FILESUBTYPE 0x0L
1833 BEGIN
1834     BLOCK "StringFileInfo"
1835     BEGIN
1836         BLOCK "040904b0"
1837         BEGIN
1838             // Required:
1839             VALUE "CompanyName", "The OpenSSL Project, http://www.openssl.org/\\0"
1840             VALUE "FileDescription", "OpenSSL Shared Library\\0"
1841             VALUE "FileVersion", "$config{version}\\0"
1842 #if defined(CRYPTO)
1843             VALUE "InternalName", "libeay32\\0"
1844             VALUE "OriginalFilename", "libeay32.dll\\0"
1845 #elif defined(SSL)
1846             VALUE "InternalName", "ssleay32\\0"
1847             VALUE "OriginalFilename", "ssleay32.dll\\0"
1848 #endif
1849             VALUE "ProductName", "The OpenSSL Toolkit\\0"
1850             VALUE "ProductVersion", "$config{version}\\0"
1851             // Optional:
1852             //VALUE "Comments", "\\0"
1853             VALUE "LegalCopyright", "Copyright Â© 1998-2015 The OpenSSL Project. Copyright Â© 1995-1998 Eric A. Young, Tim J. Hudson. All rights reserved.\\0"
1854             //VALUE "LegalTrademarks", "\\0"
1855             //VALUE "PrivateBuild", "\\0"
1856             //VALUE "SpecialBuild", "\\0"
1857         END
1858     END
1859     BLOCK "VarFileInfo"
1860     BEGIN
1861         VALUE "Translation", 0x409, 0x4b0
1862     END
1863 END
1864 EOF
1865             close(OUT);
1866         }
1867     },
1868     );
1869
1870 $builders{$builder}->($builder_platform, @builder_opts);
1871
1872 print <<"EOF";
1873
1874 Configured for $target.
1875 EOF
1876
1877 print <<"EOF" if (!$disabled{threads} && !$threads);
1878
1879 The library could not be configured for supporting multi-threaded
1880 applications as the compiler options required on this system are not known.
1881 See file INSTALL for details if you need multi-threading.
1882 EOF
1883
1884 print <<"EOF" if ($no_shared_warn);
1885
1886 The options 'shared', 'pic' and 'dynamic-engine' aren't supported on this
1887 platform, so we will pretend you gave the option 'no-pic', which also disables
1888 'shared' and 'dynamic-engine'.  If you know how to implement shared libraries
1889 or position independent code, please let us know (but please first make sure
1890 you have tried with a current version of OpenSSL).
1891 EOF
1892
1893 ###### TO BE REMOVED BEFORE FINAL RELEASE
1894 ######
1895 ###### If the user hasn't chosen --unified, try to nudge them.
1896 if ($target{build_file} eq "Makefile"
1897     && $target{build_scheme}->[0] eq "unixmake"
1898     && !$unified) {
1899
1900     my $plausible_builddir =
1901         abs2rel(rel2abs("../_openssl-build_$target"),rel2abs("."));
1902     my $plausible_to_sourcedir =
1903         abs2rel(rel2abs("."),rel2abs("../_openssl-build_$target"));
1904     print <<"EOF";
1905
1906 ----------------------------------------------------------------------
1907 Please consider configuring with the flag --unified .
1908 It's to test out a new "unified" building system.
1909
1910 One cool feature is that you can have your build directory elsewhere,
1911 for example:
1912
1913     make clean          # Clean the current configuration away
1914     mkdir $plausible_builddir
1915     cd $plausible_builddir
1916     $plausible_to_sourcedir/config --unified
1917     make
1918     make test
1919
1920 Please report any problem you have.
1921 ----------------------------------------------------------------------
1922
1923 EOF
1924 }
1925
1926 exit(0);
1927
1928 ######################################################################
1929 #
1930 # Helpers and utility functions
1931 #
1932
1933 # Configuration file reading #########################################
1934
1935 # Helper function to implement conditional inheritance depending on the
1936 # value of $no_asm.  Used in inherit_from values as follows:
1937 #
1938 #      inherit_from => [ "template", asm("asm_tmpl") ]
1939 #
1940 sub asm {
1941     my @x = @_;
1942     sub {
1943         $no_asm ? () : @x;
1944     }
1945 }
1946
1947 # Helper function to implement adding values to already existing configuration
1948 # values.  It handles elements that are ARRAYs, CODEs and scalars
1949 sub _add {
1950     my $separator = shift;
1951
1952     # If there's any ARRAY in the collection of values OR the separator
1953     # is undef, we will return an ARRAY of combined values, otherwise a
1954     # string of joined values with $separator as the separator.
1955     my $found_array = !defined($separator);
1956
1957     my @values =
1958         map {
1959             if (ref($_) eq "ARRAY") {
1960                 $found_array = 1;
1961                 @$_;
1962             } else {
1963                 $_;
1964             }
1965     } (@_);
1966
1967     if ($found_array) {
1968         [ @values ];
1969     } else {
1970         join($separator, @values);
1971     }
1972 }
1973 sub add_before {
1974     my $separator = shift;
1975     my @x = @_;
1976     sub { _add($separator, @x, @_) };
1977 }
1978 sub add {
1979     my $separator = shift;
1980     my @x = @_;
1981     sub { _add($separator, @_, @x) };
1982 }
1983
1984 # configuration reader, evaluates the input file as a perl script and expects
1985 # it to fill %targets with target configurations.  Those are then added to
1986 # %table.
1987 sub read_config {
1988     my $fname = shift;
1989     open(CONFFILE, "< $fname")
1990         or die "Can't open configuration file '$fname'!\n";
1991     my $x = $/;
1992     undef $/;
1993     my $content = <CONFFILE>;
1994     $/ = $x;
1995     close(CONFFILE);
1996     my %targets = ();
1997     {
1998         local %table = %::table;    # Protect %table from tampering
1999
2000         eval $content;
2001         warn $@ if $@;
2002     }
2003
2004     # For each target, check that it's configured with a hash table.
2005     foreach (keys %targets) {
2006         if (ref($targets{$_}) ne "HASH") {
2007             if (ref($targets{$_}) eq "") {
2008                 warn "Deprecated target configuration for $_, ignoring...\n";
2009             } else {
2010                 warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
2011             }
2012             delete $targets{$_};
2013         }
2014     }
2015
2016     %table = (%table, %targets);
2017
2018 }
2019
2020 # configuration resolver.  Will only resolve all the lazy evalutation
2021 # codeblocks for the chozen target and all those it inherits from,
2022 # recursively
2023 sub resolve_config {
2024     my $target = shift;
2025     my @breadcrumbs = @_;
2026
2027     if (grep { $_ eq $target } @breadcrumbs) {
2028         die "inherit_from loop!  target backtrace:\n  "
2029             ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
2030     }
2031
2032     if (!defined($table{$target})) {
2033         warn "Warning! target $target doesn't exist!\n";
2034         return ();
2035     }
2036     # Recurse through all inheritances.  They will be resolved on the
2037     # fly, so when this operation is done, they will all just be a
2038     # bunch of attributes with string values.
2039     # What we get here, though, are keys with references to lists of
2040     # the combined values of them all.  We will deal with lists after
2041     # this stage is done.
2042     my %combined_inheritance = ();
2043     if ($table{$target}->{inherit_from}) {
2044         my @inherit_from =
2045             map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
2046         foreach (@inherit_from) {
2047             my %inherited_config = resolve_config($_, $target, @breadcrumbs);
2048
2049             # 'template' is a marker that's considered private to
2050             # the config that had it.
2051             delete $inherited_config{template};
2052
2053             map {
2054                 if (!$combined_inheritance{$_}) {
2055                     $combined_inheritance{$_} = [];
2056                 }
2057                 push @{$combined_inheritance{$_}}, $inherited_config{$_};
2058             } keys %inherited_config;
2059         }
2060     }
2061
2062     # We won't need inherit_from in this target any more, since we've
2063     # resolved all the inheritances that lead to this
2064     delete $table{$target}->{inherit_from};
2065
2066     # Now is the time to deal with those lists.  Here's the place to
2067     # decide what shall be done with those lists, all based on the
2068     # values of the target we're currently dealing with.
2069     # - If a value is a coderef, it will be executed with the list of
2070     #   inherited values as arguments.
2071     # - If the corresponding key doesn't have a value at all or is the
2072     #   emoty string, the inherited value list will be run through the
2073     #   default combiner (below), and the result becomes this target's
2074     #   value.
2075     # - Otherwise, this target's value is assumed to be a string that
2076     #   will simply override the inherited list of values.
2077     my $default_combiner = add(" ");
2078
2079     my %all_keys =
2080         map { $_ => 1 } (keys %combined_inheritance,
2081                          keys %{$table{$target}});
2082     foreach (sort keys %all_keys) {
2083
2084         # Current target doesn't have a value for the current key?
2085         # Assign it the default combiner, the rest of this loop body
2086         # will handle it just like any other coderef.
2087         if (!exists $table{$target}->{$_}) {
2088             $table{$target}->{$_} = $default_combiner;
2089         }
2090
2091         my $valuetype = ref($table{$target}->{$_});
2092         if ($valuetype eq "CODE") {
2093             # CODE reference, execute it with the inherited values as
2094             # arguments.
2095             $table{$target}->{$_} =
2096                 $table{$target}->{$_}->(@{$combined_inheritance{$_}});
2097         } elsif ($valuetype eq "ARRAY" || $valuetype eq "") {
2098             # ARRAY or Scalar, just leave it as is.
2099         } else {
2100             # Some other type of reference that we don't handle.
2101             # Better to abort at this point.
2102             die "cannot handle reference type $valuetype,"
2103                 ," found in target $target -> $_\n";
2104         }
2105     }
2106
2107     # Finally done, return the result.
2108     return %{$table{$target}};
2109 }
2110
2111 sub usage
2112         {
2113         print STDERR $usage;
2114         print STDERR "\npick os/compiler from:\n";
2115         my $j=0;
2116         my $i;
2117         my $k=0;
2118         foreach $i (sort keys %table)
2119                 {
2120                 next if $table{$i}->{template};
2121                 next if $i =~ /^debug/;
2122                 $k += length($i) + 1;
2123                 if ($k > 78)
2124                         {
2125                         print STDERR "\n";
2126                         $k=length($i);
2127                         }
2128                 print STDERR $i . " ";
2129                 }
2130         foreach $i (sort keys %table)
2131                 {
2132                 next if $table{$i}->{template};
2133                 next if $i !~ /^debug/;
2134                 $k += length($i) + 1;
2135                 if ($k > 78)
2136                         {
2137                         print STDERR "\n";
2138                         $k=length($i);
2139                         }
2140                 print STDERR $i . " ";
2141                 }
2142         print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n";
2143         exit(1);
2144         }
2145
2146 sub run_dofile()
2147 {
2148     my $out = shift;
2149     my @templates = @_;
2150
2151     unlink $out || warn "Can't remove $out, $!"
2152         if -f $out;
2153     foreach (@templates) {
2154         die "Can't open $_, $!" unless -f $_;
2155     }
2156     my $cmd = "$config{perl} \"-I.\" \"-Mconfigdata\" $dofile -o\"Configure\" \"".join("\" \"",@templates)."\" > \"$out.new\"";
2157     #print STDERR "DEBUG[run_dofile]: \$cmd = $cmd\n";
2158     system($cmd);
2159     exit 1 if $? != 0;
2160     rename("$out.new", $out) || die "Can't rename $out.new, $!";
2161 }
2162
2163 # Configuration printer ##############################################
2164
2165 sub print_table_entry
2166 {
2167     my $target = shift;
2168     my %target = resolve_config($target);
2169     my $type = shift;
2170
2171     # Don't print the templates
2172     return if $target{template};
2173
2174     my @sequence = (
2175         "sys_id",
2176         "cc",
2177         "cflags",
2178         "defines",
2179         "debug_cflags",
2180         "debug_defines",
2181         "release_cflags",
2182         "release_defines",
2183         "thread_cflag",
2184         "unistd",
2185         "ld",
2186         "lflags",
2187         "plib_lflags",
2188         "ex_libs",
2189         "debug_lflags",
2190         "debug_plib_lflags",
2191         "debug_ex_libs",
2192         "release_lflags",
2193         "release_plib_lflags",
2194         "release_ex_libs",
2195         "bn_ops",
2196         "cpuid_obj",
2197         "bn_obj",
2198         "ec_obj",
2199         "des_obj",
2200         "aes_obj",
2201         "bf_obj",
2202         "md5_obj",
2203         "sha1_obj",
2204         "cast_obj",
2205         "rc4_obj",
2206         "rmd160_obj",
2207         "rc5_obj",
2208         "wp_obj",
2209         "cmll_obj",
2210         "modes_obj",
2211         "padlock_obj",
2212         "perlasm_scheme",
2213         "dso_scheme",
2214         "shared_target",
2215         "shared_cflag",
2216         "shared_ldflag",
2217         "shared_rcflag",
2218         "shared_extension",
2219         "shared_extension_simple",
2220         "shared_import_extension",
2221         "dso_extension",
2222         "obj_extension",
2223         "exe_extension",
2224         "ranlib",
2225         "ar",
2226         "arflags",
2227         "multilib",
2228         "build_scheme",
2229         );
2230
2231     if ($type eq "TABLE") {
2232         print "\n";
2233         print "*** $target\n";
2234         printf "\$%-12s = %s\n", $_, $target{$_} foreach (@sequence);
2235     } elsif ($type eq "HASH") {
2236         my $largest =
2237             length((sort { length($a) <=> length($b) } @sequence)[-1]);
2238         print "    '$target' => {\n";
2239         foreach (@sequence) {
2240             if ($target{$_}) {
2241                 print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
2242             }
2243         }
2244         print "    },\n";
2245     }
2246 }
2247
2248 # Utility routines ###################################################
2249
2250 # On VMS, if the given file is a logical name, File::Spec::Functions
2251 # will consider it an absolute path.  There are cases when we want a
2252 # purely syntactic check without checking the environment.
2253 sub isabsolute {
2254     my $file = shift;
2255
2256     # On non-platforms, we just use file_name_is_absolute().
2257     return file_name_is_absolute($file) unless $^O eq "VMS";
2258
2259     # If the file spec includes a device or a directpry spec,
2260     # file_name_is_absolute() is perfectly safe.
2261     return file_name_is_absolute($file) if $file =~ m|[:\[]|;
2262
2263     # Here, we know the given file spec isn't absolute
2264     return 0;
2265 }
2266
2267 # Makes a directory absolute and cleans out /../ in paths like foo/../bar
2268 # On some platforms, this uses rel2abs(), while on others, realpath() is used.
2269 # realpath() requires that at least all path components except the last is an
2270 # existing directory.  On VMS, the last component of the directory spec must
2271 # exist.
2272 sub absolutedir {
2273     my $dir = shift;
2274
2275     # realpath() is quite buggy on VMS.  It uses LIB$FID_TO_NAME, which
2276     # will return the volume name for the device, no matter what.  Also,
2277     # it will return an incorrect directory spec if the argument is a
2278     # directory that doesn't exist.
2279     if ($^O eq "VMS") {
2280         return rel2abs($dir);
2281     }
2282
2283     # We use realpath() on Unix, since no other will properly clean out
2284     # a directory spec.
2285     use Cwd qw/realpath/;
2286
2287     return realpath($dir);
2288 }
2289
2290 sub which
2291         {
2292         my($name)=@_;
2293         my $path;
2294         foreach $path (split /:/, $ENV{PATH})
2295                 {
2296                 if (-f "$path/$name$target{exe_extension}" and -x _)
2297                         {
2298                         return "$path/$name$target{exe_extension}" unless ($name eq "perl" and
2299                          system("$path/$name$target{exe_extension} -e " . '\'exit($]<5.0);\''));
2300                         }
2301                 }
2302         }
2303
2304 sub quotify {
2305     my %processors = (
2306         perl    => sub { my $x = shift;
2307                          $x =~ s/([\\\$\@"])/\\$1/g;
2308                          return '"'.$x.'"'; },
2309         );
2310     my $for = shift;
2311     my $processor =
2312         defined($processors{$for}) ? $processors{$for} : sub { shift; };
2313
2314     map { $processor->($_); } @_;
2315 }
2316
2317 # collect_from_file($filename, $line_concat_cond_re, $line_concat)
2318 # $filename is a file name to read from
2319 # $line_concat_cond_re is a regexp detecting a line continuation ending
2320 # $line_concat is a CODEref that takes care of concatenating two lines
2321 sub collect_from_file {
2322     my $filename = shift;
2323     my $line_concat_cond_re = shift;
2324     my $line_concat = shift;
2325
2326     open my $fh, $filename || die "unable to read $filename: $!\n";
2327     return sub {
2328         my $saved_line = "";
2329         $_ = "";
2330         while (<$fh>) {
2331             s|\R$||;
2332             if (defined $line_concat) {
2333                 $_ = $line_concat->($saved_line, $_);
2334                 $saved_line = "";
2335             }
2336             if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
2337                 $saved_line = $_;
2338                 next;
2339             }
2340             return $_;
2341         }
2342         die "$filename ending with continuation line\n" if $_;
2343         close $fh;
2344         return undef;
2345     }
2346 }
2347
2348 # collect_from_array($array, $line_concat_cond_re, $line_concat)
2349 # $array is an ARRAYref of lines
2350 # $line_concat_cond_re is a regexp detecting a line continuation ending
2351 # $line_concat is a CODEref that takes care of concatenating two lines
2352 sub collect_from_array {
2353     my $array = shift;
2354     my $line_concat_cond_re = shift;
2355     my $line_concat = shift;
2356     my @array = (@$array);
2357
2358     return sub {
2359         my $saved_line = "";
2360         $_ = "";
2361         while (defined($_ = shift @array)) {
2362             s|\R$||;
2363             if (defined $line_concat) {
2364                 $_ = $line_concat->($saved_line, $_);
2365                 $saved_line = "";
2366             }
2367             if (defined $line_concat_cond_re && /$line_concat_cond_re/) {
2368                 $saved_line = $_;
2369                 next;
2370             }
2371             return $_;
2372         }
2373         die "input text ending with continuation line\n" if $_;
2374         return undef;
2375     }
2376 }
2377
2378 # collect_information($lineiterator, $line_continue, $regexp => $CODEref, ...)
2379 # $lineiterator is a CODEref that delivers one line at a time.
2380 # All following arguments are regex/CODEref pairs, where the regexp detects a
2381 # line and the CODEref does something with the result of the regexp.
2382 sub collect_information {
2383     my $lineiterator = shift;
2384     my %collectors = @_;
2385
2386     while(defined($_ = $lineiterator->())) {
2387         s|\R$||;
2388         my $found = 0;
2389         foreach my $re (keys %collectors) {
2390             if ($re ne "OTHERWISE" && /$re/) {
2391                 $collectors{$re}->($lineiterator);
2392                 $found = 1;
2393             };
2394         }
2395         if ($collectors{"OTHERWISE"}) {
2396             $collectors{"OTHERWISE"}->($lineiterator, $_)
2397                 unless $found || !defined $collectors{"OTHERWISE"};
2398         }
2399     }
2400 }