CHANGES and NEWS updates for release
[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;
14
15 # see INSTALL for instructions.
16
17 my $usage="Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [experimental-<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";
18
19 # Options:
20 #
21 # --config      add the given configuration file, which will be read after
22 #               any "Configurations*" files that are found in the same
23 #               directory as this script.
24 # --prefix      prefix for the OpenSSL installation, which includes the
25 #               directories bin, lib, include, share/man, share/doc/openssl
26 #               This becomes the value of INSTALLTOP in Makefile
27 #               (Default: /usr/local)
28 # --openssldir  OpenSSL data area, such as openssl.cnf, certificates and keys.
29 #               If it's a relative directory, it will be added on the directory
30 #               given with --prefix.
31 #               This becomes the value of OPENSSLDIR in Makefile and in C.
32 #               (Default: PREFIX/ssl)
33 #
34 # --install_prefix  Additional prefix for package builders (empty by
35 #               default).  This needn't be set in advance, you can
36 #               just as well use "make INSTALL_PREFIX=/whatever install".
37 #
38 # --cross-compile-prefix Add specified prefix to binutils components.
39 #
40 # --api         One of 0.9.8, 1.0.0 or 1.1.0.  Do not compile support for
41 #               interfaces deprecated as of the specified OpenSSL version.
42 #
43 # no-hw-xxx     do not compile support for specific crypto hardware.
44 #               Generic OpenSSL-style methods relating to this support
45 #               are always compiled but return NULL if the hardware
46 #               support isn't compiled.
47 # no-hw         do not compile support for any crypto hardware.
48 # [no-]threads  [don't] try to create a library that is suitable for
49 #               multithreaded applications (default is "threads" if we
50 #               know how to do it)
51 # [no-]shared   [don't] try to create shared libraries when supported.
52 # no-asm        do not use assembler
53 # no-dso        do not compile in any native shared-library methods. This
54 #               will ensure that all methods just return NULL.
55 # no-egd        do not compile support for the entropy-gathering daemon APIs
56 # [no-]zlib     [don't] compile support for zlib compression.
57 # zlib-dynamic  Like "zlib", but the zlib library is expected to be a shared
58 #               library and will be loaded in run-time by the OpenSSL library.
59 # sctp          include SCTP support
60 # 386           generate 80386 code
61 # no-sse2       disables IA-32 SSE2 code, above option implies no-sse2
62 # no-<cipher>   build without specified algorithm (rsa, idea, rc5, ...)
63 # -<xxx> +<xxx> compiler options are passed through
64 #
65 # DEBUG_SAFESTACK use type-safe stacks to enforce type-safety on stack items
66 #               provided to stack calls. Generates unique stack functions for
67 #               each possible stack type.
68 # BN_LLONG      use the type 'long long' in crypto/bn/bn.h
69 # RC4_CHAR      use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h
70 # Following are set automatically by this script
71 #
72 # MD5_ASM       use some extra md5 assember,
73 # SHA1_ASM      use some extra sha1 assember, must define L_ENDIAN for x86
74 # RMD160_ASM    use some extra ripemd160 assember,
75 # SHA256_ASM    sha256_block is implemented in assembler
76 # SHA512_ASM    sha512_block is implemented in assembler
77 # AES_ASM       ASE_[en|de]crypt is implemented in assembler
78
79 # Minimum warning options... any contributions to OpenSSL should at least get
80 # past these.
81
82 my $gcc_devteam_warn = "-Wall -pedantic -DPEDANTIC -Wno-long-long -Wsign-compare -Wmissing-prototypes -Wshadow -Wformat -Wtype-limits -Werror -DREF_CHECK -DDEBUG_UNUSED";
83
84 # These are used in addition to $gcc_devteam_warn when the compiler is clang.
85 # TODO(openssl-team): fix problems and investigate if (at least) the
86 # following warnings can also be enabled:
87 # -Wswitch-enum, -Wunused-macros, -Wmissing-field-initializers,
88 # -Wcast-align,
89 # -Wunreachable-code -Wunused-parameter -Wlanguage-extension-token
90 # -Wextended-offsetof
91 my $clang_devteam_warn = "-Wno-unused-parameter -Wno-missing-field-initializers -Wno-language-extension-token -Wno-extended-offsetof -Wconditional-uninitialized -Qunused-arguments -Wincompatible-pointer-types-discards-qualifiers -Wmissing-variable-declarations";
92
93 # Warn that "make depend" should be run?
94 my $warn_make_depend = 0;
95
96 # These are used in addition to $gcc_devteam_warn unless this is a mingw build.
97 # This adds backtrace information to the memory leak info.
98 my $memleak_devteam_backtrace = "-rdynamic -DCRYPTO_MDEBUG_BACKTRACE";
99
100
101 my $strict_warnings = 0;
102
103 my $x86_gcc_des="";
104 my $x86_gcc_opts="";
105
106 # As for $BSDthreads. Idea is to maintain "collective" set of flags,
107 # which would cover all BSD flavors. -pthread applies to them all,
108 # but is treated differently. OpenBSD expands is as -D_POSIX_THREAD
109 # -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r,
110 # which has to be accompanied by explicit -D_THREAD_SAFE and
111 # sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which
112 # seems to be sufficient?
113 my $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT";
114
115 #
116 # API compability name to version number mapping.
117 #
118 my $maxapi = "1.1.0";           # API for "no-deprecated" builds
119 my $apitable = {
120     "1.1.0" => "0x10100000L",
121     "1.0.0" => "0x10000000L",
122     "0.9.8" => "0x00908000L",
123 };
124
125 my $base_target = "BASE";   # The template that all other inherit from
126 our %table = ();
127 our %config = ();
128
129 # Forward declarations ###############################################
130
131 # read_config(filename)
132 #
133 # Reads a configuration file and populates %table with the contents
134 # (which the configuration file places in %targets).
135 sub read_config;
136
137 # resolve_config(target)
138 #
139 # Resolves all the late evalutations, inheritances and so on for the
140 # chosen target and any target it inherits from.
141 sub resolve_config;
142
143
144 # Information collection #############################################
145
146 # Collect version numbers
147 $config{version} = "unknown";
148 $config{version_num} = "unknown";
149 $config{shlib_version_number} = "unknown";
150 $config{shlib_version_history} = "unknown";
151
152 collect_information(
153     '<include/openssl/opensslv.h',
154     undef,
155     qr/OPENSSL.VERSION.TEXT.*OpenSSL (\S+) / => sub { $config{version} = $1; },
156     qr/OPENSSL.VERSION.NUMBER.*(0x\S+)/      => sub { $config{version_num}=$1 },
157     qr/SHLIB_VERSION_NUMBER *"([^"]+)"/      => sub { $config{shlib_version_number}=$1 },
158     qr/SHLIB_VERSION_HISTORY *"([^"]*)"/     => sub { $config{shlib_version_history}=$1 }
159     );
160 if ($config{shlib_version_history} ne "") { $config{shlib_version_history} .= ":"; }
161
162 ($config{major}, $config{minor})
163     = ($config{version} =~ /^([0-9]+)\.([0-9\.]+)/);
164 ($config{shlib_major}, $config{shlib_minor})
165     = ($config{shlib_version_number} =~ /^([0-9]+)\.([0-9\.]+)/);
166 die "erroneous version information in opensslv.h: ",
167     "$config{major}, $config{minor}, $config{shlib_major}, $config{shlib_minor}\n"
168     if ($config{major} eq "" || $config{minor} eq ""
169         || $config{shlib_major} eq "" ||  $config{shlib_minor} eq "");
170
171 # Collect target configurations
172
173 my ($vol, $dir, $dummy) = File::Spec->splitpath($0);
174 my $pattern = File::Spec->catpath($vol, $dir, "Configurations/*.conf");
175 foreach (sort glob($pattern) ) {
176     &read_config($_);
177 }
178
179
180 print "Configuring OpenSSL version $config{version} (0x$config{version_num})\n";
181
182 $config{perl};
183 $config{prefix}="";
184 $config{openssldir}="";
185 $config{processor}="";
186 $config{libdir}="";
187 $config{install_prefix}= "$ENV{'INSTALL_PREFIX'}";
188 $config{cross_compile_prefix}="";
189 $config{fipslibdir}="/usr/local/ssl/fips-2.0/lib/";
190 my $nofipscanistercheck=0;
191 $config{baseaddr}="0xFB00000";
192 my $no_threads=0;
193 my $threads=0;
194 $config{no_shared}=0; # but "no-shared" is default
195 my $zlib=1;      # but "no-zlib" is default
196 my $no_rfc3779=0;
197 my $no_asm=0;
198 my $no_dso=0;
199 my $Makefile="Makefile";
200 my $default_ranlib;
201 $config{fips}=0;
202
203 # Top level directories to build
204 $config{dirs} = [ "crypto", "ssl", "engines", "apps", "test", "tools" ];
205 # crypto/ subdirectories to build
206 $config{sdirs} = [
207     "objects",
208     "md2", "md4", "md5", "sha", "mdc2", "hmac", "ripemd", "whrlpool", "poly1305",
209     "des", "aes", "rc2", "rc4", "rc5", "idea", "bf", "cast", "camellia", "seed", "chacha", "modes",
210     "bn", "ec", "rsa", "dsa", "dh", "dso", "engine",
211     "buffer", "bio", "stack", "lhash", "rand", "err",
212     "evp", "asn1", "pem", "x509", "x509v3", "conf", "txt_db", "pkcs7", "pkcs12", "comp", "ocsp", "ui",
213     "cms", "ts", "jpake", "srp", "store", "cmac", "ct", "async", "kdf"
214     ];
215
216 # Known TLS and DTLS protocols
217 my @tls = qw(ssl3 tls1 tls1_1 tls1_2);
218 my @dtls = qw(dtls1 dtls1_2);
219
220 # Explicitelly known options that are possible to disable.  They can
221 # be regexps, and will be used like this: /^no-${option}$/
222 # For developers: keep it sorted alphabetically
223
224 my @disablables = (
225     "aes",
226     "asm",
227     "bf",
228     "camellia",
229     "capieng",
230     "cast",
231     "chacha",
232     "cmac",
233     "cms",
234     "comp",
235     "crypto-mdebug",
236     "ct",
237     "deprecated",
238     "des",
239     "dgram",
240     "dh",
241     "dsa",
242     "dso",
243     "dtls",
244     "dynamic[-_]engine",
245     "ec",
246     "ec2m",
247     "ecdh",
248     "ecdsa",
249     "ec_nistp_64_gcc_128",
250     "engine",
251     "err",                      # Really???
252     "heartbeats",
253     "hmac",
254     "hw(-.+)?",
255     "idea",
256     "jpake",
257     "locking",                  # Really???
258     "md2",
259     "md4",
260     "md5",
261     "mdc2",
262     "md[-_]ghost94",
263     "nextprotoneg",
264     "ocb",
265     "ocsp",
266     "poly1305",
267     "posix-io",
268     "psk",
269     "rc2",
270     "rc4",
271     "rc5",
272     "rdrand",
273     "rfc3779",
274     "rijndael",                 # Old AES name
275     "rmd160",
276     "rsa",
277     "scrypt",
278     "sct",
279     "sctp",
280     "seed",
281     "sha",
282     "shared",
283     "sock",
284     "srp",
285     "srtp",
286     "sse2",
287     "ssl",
288     "ssl-trace",
289     "static-engine",
290     "stdio",
291     "store",
292     "threads",
293     "tls",
294     "unit-test",
295     "whirlpool",
296     "zlib",
297     "zlib-dynamic",
298     );
299 foreach my $proto ((@tls, @dtls))
300         {
301         push(@disablables, $proto);
302         push(@disablables, "$proto-method");
303         }
304
305 # All of the following is disabled by default (RC5 was enabled before 0.9.8):
306
307 my %disabled = ( # "what"         => "comment" [or special keyword "experimental"]
308                  "ec_nistp_64_gcc_128" => "default",
309                  "egd"            => "default",
310                  "jpake"          => "experimental",
311                  "md2"            => "default",
312                  "rc5"            => "default",
313                  "sctp"           => "default",
314                  "shared"         => "default",
315                  "ssl-trace"      => "default",
316                  "store"          => "experimental",
317                  "unit-test"      => "default",
318                  "zlib"           => "default",
319                  "zlib-dynamic"   => "default",
320                  "crypto-mdebug"  => "default",
321                );
322 my @experimental = ();
323
324 # Note: => pair form used for aesthetics, not to truly make a hash table
325 my @disable_cascades = (
326     # "what"            => [ "cascade", ... ]
327     sub { $config{processor} eq "386" }
328                         => [ "sse2" ],
329     "ssl"               => [ "ssl3" ],
330     "ssl3-method"       => [ "ssl3" ],
331     "zlib"              => [ "zlib-dynamic" ],
332     "rijndael"          => [ "aes" ],
333     "des"               => [ "mdc2" ],
334     "ec"                => [ "ecdsa", "ecdh" ],
335     "psk"               => [ "jpake" ],
336
337     "dgram"             => [ "dtls" ],
338     "dtls"              => [ @dtls ],
339
340     # SSL 3.0, (D)TLS 1.0 and TLS 1.1 require MD5 and SHA
341     "md5"               => [ "ssl", "tls1", "tls1_1", "dtls1" ],
342     "sha"               => [ "ssl", "tls1", "tls1_1", "dtls1" ],
343
344     # Additionally, SSL 3.0 requires either RSA or DSA+DH
345     sub { $disabled{rsa}
346           && ($disabled{dsa} || $disabled{dh}); }
347                         => [ "ssl" ],
348
349     # (D)TLS 1.0 and TLS 1.1 also require either RSA or DSA+DH
350     # or ECDSA + ECDH.  (D)TLS 1.2 has this requirement as well.
351     # (XXX: We don't support PSK-only builds).
352     sub { $disabled{rsa}
353           && ($disabled{dsa} || $disabled{dh})
354           && ($disabled{ecdsa} || $disabled{ecdh}); }
355                         => [ "tls1", "tls1_1", "tls1_2",
356                              "dtls1", "dtls1_2" ],
357
358     "tls"               => [ @tls ],
359
360     # SRP and HEARTBEATS require TLSEXT
361     "tlsext"            => [ "srp", "heartbeats" ],
362     );
363
364 # Avoid protocol support holes.  Also disable all versions below N, if version
365 # N is disabled while N+1 is enabled.
366 #
367 my @list = (reverse @tls);
368 while ((my $first, my $second) = (shift @list, shift @list)) {
369     last unless @list;
370     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
371                               => [ @list ] );
372     unshift @list, $second;
373 }
374 my @list = (reverse @dtls);
375 while ((my $first, my $second) = (shift @list, shift @list)) {
376     last unless @list;
377     push @disable_cascades, ( sub { !$disabled{$first} && $disabled{$second} }
378                               => [ @list ] );
379     unshift @list, $second;
380 }
381
382 # Construct the string of what $config{depflags} should look like with the defaults
383 # from %disabled above.  (we need this to see if we should advise the user
384 # to run "make depend"):
385 my $default_depflags = join(" ",
386     map { my $x = $_; $x =~ tr{[a-z]-}{[A-Z]_}; "-DOPENSSL_NO_$x"; }
387     grep { $disabled{$_} !~ /\(no-depflags\)$/ }
388     sort keys %disabled);
389
390 # Explicit "no-..." options will be collected in %disabled along with the defaults.
391 # To remove something from %disabled, use "enable-foo" (unless it's experimental).
392 # For symmetry, "disable-foo" is a synonym for "no-foo".
393
394 # For features called "experimental" here, a more explicit "experimental-foo" is needed to enable.
395 # We will collect such requests in @experimental.
396 # To avoid accidental use of experimental features, applications will have to use -DOPENSSL_EXPERIMENTAL_FOO.
397
398
399 my $no_sse2=0;
400
401 &usage if ($#ARGV < 0);
402
403 my $flags="";
404 $config{depflags}="";
405 $config{openssl_experimental_defines}=[];
406 $config{openssl_api_defines}=[];
407 $config{openssl_algorithm_defines}=[];
408 $config{openssl_thread_defines}=[];
409 $config{openssl_sys_defines}=[];
410 $config{openssl_other_defines}=[];
411 my $libs="";
412 my $target="";
413 $config{options}="";
414 my $make_depend=0;
415 my %withargs=();
416 my $build_prefix = "release_";
417
418 my @argvcopy=@ARGV;
419
420 if (grep /^reconf(igure)?$/, @argvcopy) {
421     if (-f "./configdata.pm") {
422         my $file = "./configdata.pm";
423         unless (my $return = do $file) {
424             die "couldn't parse $file: $@" if $@;
425             die "couldn't do $file: $!"    unless defined $return;
426             die "couldn't run $file"       unless $return;
427         }
428
429         @argvcopy = defined($configdata::config{perlargv}) ?
430             @{$configdata::config{perlargv}} : ();
431         die "Incorrect data to reconfigure, please do a normal configuration\n"
432             if (grep(/^reconf/,@argvcopy));
433         $ENV{CROSS_COMPILE} = $configdata::config{cross_compile_prefix}
434             if defined($configdata::config{cross_compile_prefix});
435         $ENV{CROSS_COMPILE} = $configdata::config{cc}
436             if defined($configdata::config{cc});
437
438         print "Reconfiguring with: ", join(" ",@argvcopy), "\n";
439         print "    CROSS_COMPILE = ",$ENV{CROSS_COMPILE},"\n"
440             if $ENV{CROSS_COMPILE};
441         print "    CC = ",$ENV{CC},"\n" if $ENV{CC};
442     } elsif (open IN, "<Makefile") {
443         #
444         # THIS SECTION IS TEMPORARY, it helps transitioning from Makefile
445         # centered information gathering the reading configdata.pm
446         #
447         while (<IN>) {
448             chomp;
449             if (/^CONFIGURE_ARGS=\s*(.*)\s*/) {
450                 # Older form, we split the string and hope for the best
451                 @argvcopy = split /\s+/, $_;
452                 die "Incorrect data to reconfigure, please do a normal configuration\n"
453                     if (grep(/^reconf/,@argvcopy));
454             } elsif (/^CROSS_COMPILE=\s*(.*)/) {
455                 $ENV{CROSS_COMPILE}=$1;
456             } elsif (/^CC=\s*(?:\$\(CROSS_COMPILE\))?(.*?)$/) {
457                 $ENV{CC}=$1;
458             }
459         }
460         #
461         # END OF TEMPORARY SECTION
462         #
463     } else {
464         die "Insufficient data to reconfigure, please do a normal configuration\n";
465     }
466 }
467
468 $config{perlargv} = [ @argvcopy ];
469
470 my %unsupported_options = ();
471 foreach (@argvcopy)
472         {
473         s /^-no-/no-/; # some people just can't read the instructions
474
475         # rewrite some options in "enable-..." form
476         s /^-?-?shared$/enable-shared/;
477         s /^sctp$/enable-sctp/;
478         s /^threads$/enable-threads/;
479         s /^zlib$/enable-zlib/;
480         s /^zlib-dynamic$/enable-zlib-dynamic/;
481
482         if (/^(no|disable|enable|experimental)-(.+)$/)
483                 {
484                 my $word = $2;
485                 if (!grep { $word =~ /^${_}$/ } @disablables)
486                         {
487                         $unsupported_options{$_} = 1;
488                         next;
489                         }
490                 }
491         if (/^no-(.+)$/ || /^disable-(.+)$/)
492                 {
493                 if (!($disabled{$1} eq "experimental"))
494                         {
495                         foreach my $proto ((@tls, @dtls))
496                                 {
497                                 if ($1 eq "$proto-method")
498                                         {
499                                         $disabled{"$proto"} = "option($proto-method)";
500                                         last;
501                                         }
502                                 }
503                         if ($1 eq "dtls")
504                                 {
505                                 foreach my $proto (@dtls)
506                                         {
507                                         $disabled{$proto} = "option(dtls)";
508                                         }
509                                 }
510                         elsif ($1 eq "ssl")
511                                 {
512                                 # Last one of its kind
513                                 $disabled{"ssl3"} = "option(ssl)";
514                                 }
515                         elsif ($1 eq "tls")
516                                 {
517                                 # XXX: Tests will fail if all SSL/TLS
518                                 # protocols are disabled.
519                                 foreach my $proto (@tls)
520                                         {
521                                         $disabled{$proto} = "option(tls)";
522                                         }
523                                 }
524                         else
525                                 {
526                                 $disabled{$1} = "option";
527                                 }
528                         }
529                 }
530         elsif (/^enable-(.+)$/ || /^experimental-(.+)$/)
531                 {
532                 my $algo = $1;
533                 if ($disabled{$algo} eq "experimental")
534                         {
535                         die "You are requesting an experimental feature; please say 'experimental-$algo' if you are sure\n"
536                                 unless (/^experimental-/);
537                         push @experimental, $algo;
538                         }
539                 delete $disabled{$algo};
540
541                 $threads = 1 if ($algo eq "threads");
542                 }
543         elsif (/^--strict-warnings$/)
544                 {
545                 $strict_warnings = 1;
546                 }
547         elsif (/^--debug$/)
548                 {
549                 $build_prefix = "debug_";
550                 }
551         elsif (/^--release$/)
552                 {
553                 $build_prefix = "release_";
554                 }
555         elsif (/^386$/)
556                 { $config{processor}=386; }
557         elsif (/^fips$/)
558                 {
559                 $config{fips}=1;
560                 }
561         elsif (/^rsaref$/)
562                 {
563                 # No RSAref support any more since it's not needed.
564                 # The check for the option is there so scripts aren't
565                 # broken
566                 }
567         elsif (/^nofipscanistercheck$/)
568                 {
569                 $config{fips} = 1;
570                 $nofipscanistercheck = 1;
571                 }
572         elsif (/^[-+]/)
573                 {
574                 if (/^--prefix=(.*)$/)
575                         {
576                         $config{prefix}=$1;
577                         }
578                 elsif (/^--api=(.*)$/)
579                         {
580                         $config{api}=$1;
581                         }
582                 elsif (/^--libdir=(.*)$/)
583                         {
584                         $config{libdir}=$1;
585                         }
586                 elsif (/^--openssldir=(.*)$/)
587                         {
588                         $config{openssldir}=$1;
589                         }
590                 elsif (/^--install.prefix=(.*)$/)
591                         {
592                         $config{install_prefix}=$1;
593                         }
594                 elsif (/^--with-zlib-lib=(.*)$/)
595                         {
596                         $withargs{"zlib-lib"}=$1;
597                         }
598                 elsif (/^--with-zlib-include=(.*)$/)
599                         {
600                         $withargs{"zlib-include"}="-I$1";
601                         }
602                 elsif (/^--with-fipslibdir=(.*)$/)
603                         {
604                         $config{fipslibdir}="$1/";
605                         }
606                 elsif (/^--with-baseaddr=(.*)$/)
607                         {
608                         $config{baseaddr}="$1";
609                         }
610                 elsif (/^--cross-compile-prefix=(.*)$/)
611                         {
612                         $config{cross_compile_prefix}=$1;
613                         }
614                 elsif (/^--config=(.*)$/)
615                         {
616                         read_config $1;
617                         }
618                 elsif (/^-[lL](.*)$/ or /^-Wl,/)
619                         {
620                         $libs.=$_." ";
621                         }
622                 else    # common if (/^[-+]/), just pass down...
623                         {
624                         $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei;
625                         $flags.=$_." ";
626                         }
627                 }
628         elsif ($_ =~ /^([^:]+):(.+)$/)
629                 {
630                 eval "\$table{\$1} = \"$2\""; # allow $xxx constructs in the string
631                 $target=$1;
632                 }
633         else
634                 {
635                 die "target already defined - $target (offending arg: $_)\n" if ($target ne "");
636                 $target=$_;
637                 }
638         unless ($_ eq $target || /^no-/ || /^disable-/)
639                 {
640                 # "no-..." follows later after implied disactivations
641                 # have been derived.  (Don't take this too seroiusly,
642                 # we really only write OPTIONS to the Makefile out of
643                 # nostalgia.)
644
645                 if ($config{options} eq "")
646                         { $config{options} = $_; }
647                 else
648                         { $config{options} .= " ".$_; }
649                 }
650
651         if (defined($config{api}) && !exists $apitable->{$config{api}}) {
652                 die "***** Unsupported api compatibility level: $config{api}\n",
653         }
654
655         if (keys %unsupported_options)
656                 {
657                 die "***** Unsupported options: ",
658                         join(", ", keys %unsupported_options), "\n";
659                 }
660         }
661
662 if ($config{fips})
663         {
664         delete $disabled{"shared"} if ($disabled{"shared"} =~ /^default/);
665         }
666 else
667         {
668         @{$config{dirs}} = grep !/^fips$/, @{$config{dirs}};
669         }
670
671 my @tocheckfor = (keys %disabled);
672 while (@tocheckfor) {
673     my %new_tocheckfor = ();
674     my @cascade_copy = (@disable_cascades);
675     while (@cascade_copy) {
676         my ($test, $descendents) = (shift @cascade_copy, shift @cascade_copy);
677         if (ref($test) eq "CODE" ? $test->() : defined($disabled{$test})) {
678             map {
679                 $new_tocheckfor{$_} => 1; $disabled{$_} = "forced";
680             } grep { !defined($disabled{$_}) } @$descendents;
681         }
682     }
683     @tocheckfor = (keys %new_tocheckfor);
684 }
685
686 if ($target eq "TABLE") {
687     foreach (sort keys %table) {
688         print_table_entry($_, "TABLE");
689     }
690     exit 0;
691 }
692
693 if ($target eq "LIST") {
694     foreach (sort keys %table) {
695         print $_,"\n" unless $table{$_}->{template};
696     }
697     exit 0;
698 }
699
700 if ($target eq "HASH") {
701     print "%table = (\n";
702     foreach (sort keys %table) {
703         print_table_entry($_, "HASH");
704     }
705     exit 0;
706 }
707
708 # Backward compatibility?
709 if ($target =~ m/^CygWin32(-.*)$/) {
710     $target = "Cygwin".$1;
711 }
712
713 foreach (sort (keys %disabled))
714         {
715         $config{options} .= " no-$_";
716
717         printf "    no-%-12s %-10s", $_, "[$disabled{$_}]";
718
719         if (/^dso$/)
720                 { $no_dso = 1; }
721         elsif (/^threads$/)
722                 { $no_threads = 1; }
723         elsif (/^shared$/)
724                 { $config{no_shared} = 1; }
725         elsif (/^zlib$/)
726                 { $zlib = 0; }
727         elsif (/^static-engine$/)
728                 { }
729         elsif (/^zlib-dynamic$/)
730                 { }
731         elsif (/^sse2$/)
732                 { $no_sse2 = 1; }
733         elsif (/^engine$/)
734                 { @{$config{dirs}} = grep !/^engine$/, @{$config{dirs}}; }
735         else
736                 {
737                 my ($ALGO, $algo);
738                 ($ALGO = $algo = $_) =~ tr/[\-a-z]/[_A-Z]/;
739
740                 if (/^asm$/ || /^err$/ || /^hw$/ || /^hw-/)
741                         {
742                         push @{$config{openssl_other_defines}}, "OPENSSL_NO_$ALGO";
743                         print " OPENSSL_NO_$ALGO";
744
745                         if (/^err$/)    { $flags .= "-DOPENSSL_NO_ERR "; }
746                         elsif (/^asm$/) { $no_asm = 1; }
747                         }
748                 else
749                         {
750                         ($ALGO,$algo) = ("RMD160","rmd160") if ($algo eq "ripemd");
751
752                         push @{$config{openssl_algorithm_defines}}, "OPENSSL_NO_$ALGO";
753                         $config{depflags} .= " -DOPENSSL_NO_$ALGO";
754                         print " OPENSSL_NO_$ALGO";
755
756                         # fix-up crypto/directory name(s)
757                         $algo="whrlpool" if $algo eq "whirlpool";
758                         $algo="ripemd" if $algo eq "rmd160";
759                         @{$config{sdirs}} = grep { $_ ne $algo} @{$config{sdirs}};
760
761                         print " (skip dir)";
762                         }
763                 }
764
765         print "\n";
766         }
767
768 my $exp_cflags = "";
769
770 foreach (sort @experimental)
771         {
772         my $ALGO;
773         ($ALGO = $_) =~ tr/[a-z]/[A-Z]/;
774
775         # opensslconf.h will set OPENSSL_NO_... unless OPENSSL_EXPERIMENTAL_... is defined
776         push @{$config{openssl_experimental_defines}}, "OPENSSL_NO_$ALGO";
777         $exp_cflags .= " -DOPENSSL_EXPERIMENTAL_$ALGO";
778         }
779
780 print "Configuring for $target\n";
781
782 # Support for legacy targets having a name starting with 'debug-'
783 my ($d, $t) = $target =~ m/^(debug-)?(.*)$/;
784 if ($d) {
785     $build_prefix = "debug_";
786
787     # If we do not find debug-foo in the table, the target is set to foo.
788     if (!$table{$target}) {
789         $target = $t;
790     }
791 }
792 $config{target} = $target;
793 delete $table{$base_target}->{template}; # or the next test will fail.
794 my %target = ( %{$table{$base_target}}, resolve_config($target) );
795
796 &usage if (!%target || $target{template});
797
798 $target{exe_extension}="";
799 $target{exe_extension}=".exe" if ($config{target} eq "Cygwin" || $config{target} eq "DJGPP" || $config{target} =~ /^mingw/);
800 $target{exe_extension}=".nlm" if ($config{target} =~ /netware/);
801 $target{exe_extension}=".pm"  if ($config{target} =~ /vos/);
802
803 $default_ranlib = which("ranlib") || "true";
804 $config{perl}   = $ENV{'PERL'} || which("perl5") || which("perl") || "perl";
805 my $make        = $ENV{'MAKE'} || "make";
806
807 $config{cross_compile_prefix} = $ENV{'CROSS_COMPILE'}
808     if $config{cross_compile_prefix} eq "";
809
810 $config{prefix} = "/usr/local" if !$config{prefix};
811 $config{openssldir} = "ssl" if !$config{openssldir};
812 $config{openssldir} = catdir($config{prefix}, $config{openssldir})
813     unless file_name_is_absolute($config{openssldir});
814
815 # Allow environment CC to override compiler...
816 $target{cc} = $ENV{CC} || $target{cc};
817
818 # For cflags and lflags, add the debug_ or release_ attributes
819 # Do it in such a way that no spurious space is appended (hence the grep).
820 $config{cflags} = join(" ",
821                        grep { $_ ne "" } ($target{cflags},
822                                           $target{$build_prefix."cflags"}));
823 $config{lflags} = join(" ",
824                        grep { $_ ne "" } ($target{lflags},
825                                           $target{$build_prefix."lflags"}));
826
827 $target{ranlib} = $ENV{'RANLIB'} || $target{ranlib} || $default_ranlib;
828 $target{ar} = $ENV{'AR'} || "ar";
829 $target{arflags} = "" if !defined($target{arflags});
830 $target{nm} = "nm";
831 # Make sure build_scheme is consistent.
832 $target{build_scheme} = [ $target{build_scheme} ]
833     if ref($target{build_scheme}) ne "ARRAY";
834
835 # if $config{prefix}/lib$target{multilib} is not an existing directory, then
836 # assume that it's not searched by linker automatically, in
837 # which case adding $target{multilib} suffix causes more grief than
838 # we're ready to tolerate, so don't...
839 $target{multilib}="" if !-d "$config{prefix}/lib$target{multilib}";
840
841 $config{libdir}="lib$target{multilib}" if $config{libdir} eq "";
842 $config{enginesdir}=$config{prefix} . "/" . $config{libdir}  . "/engines";
843
844 $config{cflags} .= "$exp_cflags";
845
846 # '%' in $config{lflags} is used to split flags to "pre-" and post-flags
847 my ($pre,$post)=split('%',$config{lflags});
848 if (defined($post))     { $config{prelflags}=$pre; $config{lflags}=$post;       }
849 else                    { $config{prelflags}="";   $config{lflags}=$pre;        }
850
851 if ($target =~ /^mingw/ && `$target{cc} --target-help 2>&1` !~ m/-mno-cygwin/m)
852         {
853         $config{cflags} =~ s/-mno-cygwin\s*//;
854         $target{shared_ldflag} =~ s/-mno-cygwin\s*//;
855         }
856
857 if ($target =~ /linux.*-mips/ && !$no_asm && $flags !~ /-m(ips|arch=)/) {
858         # minimally required architecture flags for assembly modules
859         $config{cflags}="-mips2 $config{cflags}" if ($target =~ /mips32/);
860         $config{cflags}="-mips3 $config{cflags}" if ($target =~ /mips64/);
861 }
862
863 my $no_shared_warn=0;
864 my $no_user_cflags=0;
865
866 if ($flags ne "")       { $config{cflags}="$flags$config{cflags}"; }
867 else                    { $no_user_cflags=1;       }
868
869 # The DSO code currently always implements all functions so that no
870 # applications will have to worry about that from a compilation point
871 # of view. However, the "method"s may return zero unless that platform
872 # has support compiled in for them. Currently each method is enabled
873 # by a define "DSO_<name>" ... we translate the "dso_scheme" config
874 # string entry into using the following logic;
875 my $dso_cflags;
876 if (!$no_dso && $target{dso_scheme} ne "")
877         {
878         $target{dso_scheme} =~ tr/[a-z]/[A-Z]/;
879         if ($target{dso_scheme} eq "DLFCN")
880                 {
881                 $dso_cflags = "-DDSO_DLFCN -DHAVE_DLFCN_H";
882                 }
883         elsif ($target{dso_scheme} eq "DLFCN_NO_H")
884                 {
885                 $dso_cflags = "-DDSO_DLFCN";
886                 }
887         else
888                 {
889                 $dso_cflags = "-DDSO_$target{dso_scheme}";
890                 }
891         $config{cflags} = "$dso_cflags $config{cflags}";
892         }
893
894 my $thread_cflags;
895 my @thread_defines;
896 if ($target{thread_cflag} ne "(unknown)" && !$no_threads)
897         {
898         # If we know how to do it, support threads by default.
899         $threads = 1;
900         }
901 if ($target{thread_cflag} eq "(unknown)" && $threads)
902         {
903         # If the user asked for "threads", [s]he is also expected to
904         # provide any system-dependent compiler options that are
905         # necessary.
906         if ($no_user_cflags)
907                 {
908                 print "You asked for multi-threading support, but didn't\n";
909                 print "provide any system-specific compiler options\n";
910                 exit(1);
911                 }
912         $thread_cflags="-DOPENSSL_THREADS $config{cflags}" ;
913         push @thread_defines, "OPENSSL_THREADS";
914         }
915 else
916         {
917         $thread_cflags="-DOPENSSL_THREADS $target{thread_cflag} $config{cflags}";
918         push @thread_defines, "OPENSSL_THREADS";
919 #       my $def;
920 #       foreach $def (split ' ',$target{thread_cflag})
921 #               {
922 #               if ($def =~ s/^-D// && $def !~ /^_/)
923 #                       {
924 #                       push @thread_defines, "$def";
925 #                       }
926 #               }
927         }
928
929 $config{lflags}="$libs$config{lflags}" if ($libs ne "");
930
931 if ($no_asm)
932         {
933         $config{cflags}=~s/-D[BL]_ENDIAN//              if ($config{fips});
934         $thread_cflags=~s/-D[BL]_ENDIAN//       if ($config{fips});
935         }
936
937 if ($threads)
938         {
939         $config{cflags}=$thread_cflags;
940         push @{$config{openssl_thread_defines}}, @thread_defines;
941         }
942
943 if ($zlib)
944         {
945         $config{cflags} = "-DZLIB $config{cflags}";
946         if (defined($disabled{"zlib-dynamic"}))
947                 {
948                 if (defined($withargs{"zlib-lib"}))
949                         {
950                         $config{lflags} .= " -L" . $withargs{"zlib-lib"} . " -lz";
951                         }
952                 else
953                         {
954                         $config{lflags} .= " -lz";
955                         }
956                 }
957         else
958                 {
959                 $config{cflags} = "-DZLIB_SHARED $config{cflags}";
960                 }
961         }
962
963 # With "deprecated" disable all deprecated features.
964 if (defined($disabled{"deprecated"})) {
965         $config{api} = $maxapi;
966 }
967
968 if ($target{shared_target} eq "")
969         {
970         $no_shared_warn = 1 if !$config{no_shared} && !$config{fips};
971         $config{no_shared} = 1;
972         }
973 if (!$config{no_shared})
974         {
975         if ($target{shared_cflag} ne "")
976                 {
977                 $config{cflags} = "$target{shared_cflag} -DOPENSSL_PIC $config{cflags}";
978                 }
979         }
980
981 if ($target{build_scheme}->[0] ne "mk1mf")
982         {
983         # add {no-}static-engine to options to allow mkdef.pl to work without extra arguments
984         if ($config{no_shared})
985                 {
986                 push @{$config{openssl_other_defines}}, "OPENSSL_NO_DYNAMIC_ENGINE";
987                 $config{options}.=" static-engine";
988                 }
989         else
990                 {
991                 push @{$config{openssl_other_defines}}, "OPENSSL_NO_STATIC_ENGINE";
992                 $config{options}.=" no-static-engine";
993                 }
994         }
995
996 #
997 # Platform fix-ups
998 #
999 if ($target =~ /-icc$/) # Intel C compiler
1000         {
1001         my $iccver=0;
1002         if (open(FD,"$target{cc} -V 2>&1 |"))
1003                 {
1004                 while(<FD>) { $iccver=$1 if (/Version ([0-9]+)\./); }
1005                 close(FD);
1006                 }
1007         if ($iccver>=8)
1008                 {
1009                 $config{cflags}=~s/-KPIC/-fPIC/;
1010                 # Eliminate unnecessary dependency from libirc.a. This is
1011                 # essential for shared library support, as otherwise
1012                 # apps/openssl can end up in endless loop upon startup...
1013                 $config{cflags}.=" -Dmemcpy=__builtin_memcpy -Dmemset=__builtin_memset";
1014                 }
1015         if ($iccver>=9)
1016                 {
1017                 $config{lflags}.=" -i-static";
1018                 $config{lflags}=~s/-no_cpprt/-no-cpprt/;
1019                 }
1020         if ($iccver>=10)
1021                 {
1022                 $config{lflags}=~s/-i-static/-static-intel/;
1023                 }
1024         if ($iccver>=11)
1025                 {
1026                 $config{cflags}.=" -no-intel-extensions";       # disable Cilk
1027                 $config{lflags}=~s/-no-cpprt/-no-cxxlib/;
1028                 }
1029         }
1030
1031 # Unlike other OSes (like Solaris, Linux, Tru64, IRIX) BSD run-time
1032 # linkers (tested OpenBSD, NetBSD and FreeBSD) "demand" RPATH set on
1033 # .so objects. Apparently application RPATH is not global and does
1034 # not apply to .so linked with other .so. Problem manifests itself
1035 # when libssl.so fails to load libcrypto.so. One can argue that we
1036 # should engrave this into Makefile.shared rules or into BSD-* config
1037 # lines above. Meanwhile let's try to be cautious and pass -rpath to
1038 # linker only when --prefix is not /usr.
1039 if ($target =~ /^BSD-/)
1040         {
1041         $target{shared_ldflag}.=" -Wl,-rpath,\$\$(LIBRPATH)" if ($config{prefix} !~ m|^/usr[/]*$|);
1042         }
1043
1044 if ($target{sys_id} ne "")
1045         {
1046         #$config{cflags}="-DOPENSSL_SYS_$target{sys_id} $config{cflags}";
1047         push @{$config{openssl_sys_defines}}, "OPENSSL_SYS_$target{sys_id}";
1048         }
1049
1050 if ($target{ranlib} eq "")
1051         {
1052         $target{ranlib} = $default_ranlib;
1053         }
1054
1055 if (!$no_asm) {
1056     $target{cpuid_obj}=$table{BASE}->{cpuid_obj} if ($config{processor} eq "386");
1057     $target{cpuid_obj}.=" uplink.o uplink-x86.o" if ($config{cflags} =~ /-DOPENSSL_USE_APPLINK/);
1058
1059     $target{bn_obj} =~ s/\w+-gf2m.o// if (defined($disabled{ec2m}));
1060
1061     # bn-586 is the only one implementing bn_*_part_words
1062     $config{cflags}.=" -DOPENSSL_BN_ASM_PART_WORDS" if ($target{bn_obj} =~ /bn-586/);
1063     $config{cflags}.=" -DOPENSSL_IA32_SSE2" if (!$no_sse2 && $target{bn_obj} =~ /86/);
1064
1065     $config{cflags}.=" -DOPENSSL_BN_ASM_MONT" if ($target{bn_obj} =~ /-mont/);
1066     $config{cflags}.=" -DOPENSSL_BN_ASM_MONT5" if ($target{bn_obj} =~ /-mont5/);
1067     $config{cflags}.=" -DOPENSSL_BN_ASM_GF2m" if ($target{bn_obj} =~ /-gf2m/);
1068
1069     if ($config{fips}) {
1070         push @{$config{openssl_other_defines}}, "OPENSSL_FIPS";
1071     }
1072
1073     if ($target{sha1_obj} =~ /\.o$/) {
1074         $config{cflags}.=" -DSHA1_ASM"   if ($target{sha1_obj} =~ /sx86/ || $target{sha1_obj} =~ /sha1/);
1075         $config{cflags}.=" -DSHA256_ASM" if ($target{sha1_obj} =~ /sha256/);
1076         $config{cflags}.=" -DSHA512_ASM" if ($target{sha1_obj} =~ /sha512/);
1077         if ($target{sha1_obj} =~ /sse2/) {
1078             if ($no_sse2) {
1079                 $target{sha1_obj} =~ s/\S*sse2\S+//;
1080             } elsif ($config{cflags} !~ /OPENSSL_IA32_SSE2/) {
1081                 $config{cflags}.=" -DOPENSSL_IA32_SSE2";
1082             }
1083         }
1084     }
1085     if ($target{md5_obj} =~ /\.o$/) {
1086         $config{cflags}.=" -DMD5_ASM";
1087     }
1088     $target{cast_obj}=$table{BASE}->{cast_obj} if (!$config{no_shared}); # CAST assembler is not PIC
1089     if ($target{rmd160_obj} =~ /\.o$/) {
1090         $config{cflags}.=" -DRMD160_ASM";
1091     }
1092     if ($target{aes_obj} =~ /\.o$/) {
1093         $config{cflags}.=" -DAES_ASM" if ($target{aes_obj} =~ m/\baes-/);;
1094         # aes-ctr.o is not a real file, only indication that assembler
1095         # module implements AES_ctr32_encrypt...
1096         $config{cflags}.=" -DAES_CTR_ASM" if ($target{aes_obj} =~ s/\s*aes-ctr\.o//);
1097         # aes-xts.o indicates presence of AES_xts_[en|de]crypt...
1098         $config{cflags}.=" -DAES_XTS_ASM" if ($target{aes_obj} =~ s/\s*aes-xts\.o//);
1099         $target{aes_obj} =~ s/\s*(vpaes|aesni)-x86\.o//g if ($no_sse2);
1100         $config{cflags}.=" -DVPAES_ASM" if ($target{aes_obj} =~ m/vpaes/);
1101         $config{cflags}.=" -DBSAES_ASM" if ($target{aes_obj} =~ m/bsaes/);
1102     }
1103     if ($target{wp_obj} =~ /mmx/ && $config{processor} eq "386") {
1104         $target{wp_obj}=$table{BASE}->{wp_obj};
1105     } elsif (!$disabled{"whirlpool"}) {
1106         $config{cflags}.=" -DWHIRLPOOL_ASM";
1107     }
1108     if ($target{modes_obj} =~ /ghash-/) {
1109         $config{cflags}.=" -DGHASH_ASM";
1110     }
1111     if ($target{ec_obj} =~ /ecp_nistz256/) {
1112         $config{cflags}.=" -DECP_NISTZ256_ASM";
1113     }
1114     if ($target{poly1305_obj} =~ /\.o$/) {
1115         $config{cflags}.=" -DPOLY1305_ASM";
1116     }
1117 }
1118
1119 my $ecc = $target{cc};
1120 $ecc = "clang" if `$target{cc} --version 2>&1` =~ /clang/;
1121
1122 $config{makedepprog} =
1123     $ecc eq "gcc" || $ecc eq "clang" ? $target{cc} : "makedepend";
1124 $config{depflags} =~ s/^\s*//;
1125
1126
1127 # Deal with bn_ops ###################################################
1128
1129 $config{bn_ll}                  =0;
1130 $config{export_var_as_fn}       =0;
1131 my $def_int="unsigned int";
1132 $config{rc4_int}                =$def_int;
1133 $config{rc2_int}                =$def_int;
1134 ($config{b64l},$config{b64},$config{b32})=(0,0,1);
1135
1136 foreach (sort split(/\s+/,$target{bn_ops})) {
1137     $config{bn_ll}=1                            if /BN_LLONG/;
1138     $config{rc4_int}="unsigned char"            if /RC4_CHAR/;
1139     ($config{b64l},$config{b64},$config{b32},$config{b16},$config{b8})
1140         =(0,1,0,0,0)                            if /SIXTY_FOUR_BIT/;
1141     ($config{b64l},$config{b64},$config{b32},$config{b16},$config{b8})
1142         =(1,0,0,0,0)                            if /SIXTY_FOUR_BIT_LONG/;
1143     ($config{b64l},$config{b64},$config{b32},$config{b16},$config{b8})
1144         =(0,0,1,0,0)                            if /THIRTY_TWO_BIT/;
1145     ($config{b64l},$config{b64},$config{b32},$config{b16},$config{b8})
1146         =(0,0,0,1,0)                            if /SIXTEEN_BIT/;
1147     ($config{b64l},$config{b64},$config{b32},$config{b16},$config{b8})
1148         =(0,0,0,0,1)                            if /EIGHT_BIT/;
1149     $config{export_var_as_fn}=1                 if /EXPORT_VAR_AS_FN/;
1150 }
1151
1152
1153 # Hack cflags for better warnings (dev option) #######################
1154
1155 # "Stringify" the C flags string.  This permits it to be made part of a string
1156 # and works as well on command lines.
1157 $config{cflags} =~ s/([\\\"])/\\\1/g;
1158
1159 if (defined($config{api})) {
1160     $config{openssl_api_defines} = [ "OPENSSL_MIN_API=".$apitable->{$config{api}} ];
1161     my $apiflag = sprintf("-DOPENSSL_API_COMPAT=%s", $apitable->{$config{api}});
1162     $default_depflags .= " $apiflag";
1163     $config{cflags} .= " $apiflag";
1164 }
1165
1166 if ($strict_warnings)
1167         {
1168         my $wopt;
1169         die "ERROR --strict-warnings requires gcc or clang" unless ($ecc =~ /gcc(-\d(\.\d)*)?$/ or $ecc =~ /clang$/);
1170         foreach $wopt (split /\s+/, $gcc_devteam_warn)
1171                 {
1172                 $config{cflags} .= " $wopt" unless ($config{cflags} =~ /(^|\s)$wopt(\s|$)/)
1173                 }
1174         if ($ecc eq "clang")
1175                 {
1176                 foreach $wopt (split /\s+/, $clang_devteam_warn)
1177                         {
1178                         $config{cflags} .= " $wopt" unless ($config{cflags} =~ /(^|\s)$wopt(\s|$)/)
1179                         }
1180                 }
1181         if ($target !~ /^mingw/)
1182                 {
1183                 foreach $wopt (split /\s+/, $memleak_devteam_backtrace)
1184                         {
1185                         $config{cflags} .= " $wopt" unless ($config{cflags} =~ /(^|\s)$wopt(\s|$)/)
1186                         }
1187                 if ($target =~ /^BSD-/)
1188                         {
1189                         $config{lflags} .= " -lexecinfo";
1190                         }
1191                 }
1192         }
1193
1194 # Write down our configuration where it fits #########################
1195
1196 open(OUT,">configdata.pm") || die "unable to create configdata.pm: $!\n";
1197 print OUT <<"EOF";
1198 package configdata;
1199
1200 use strict;
1201 use warnings;
1202
1203 use Exporter;
1204 #use vars qw(\@ISA \@EXPORT);
1205 our \@ISA = qw(Exporter);
1206 our \@EXPORT = qw(\%config \%target %withargs);
1207
1208 EOF
1209 print OUT "our %config = (\n";
1210 foreach (sort keys %config) {
1211     if (ref($config{$_}) eq "ARRAY") {
1212         print OUT "  ", $_, " => [ ", join(", ",
1213                                            map { quotify("perl", $_) }
1214                                            @{$config{$_}}), " ],\n";
1215     } else {
1216         print OUT "  ", $_, " => ", quotify("perl", $config{$_}), ",\n"
1217     }
1218 }
1219 print OUT <<"EOF";
1220 );
1221
1222 EOF
1223 print OUT "our %target = (\n";
1224 foreach (sort keys %target) {
1225     if (ref($target{$_}) eq "ARRAY") {
1226         print OUT "  ", $_, " => [ ", join(", ",
1227                                            map { quotify("perl", $_) }
1228                                            @{$target{$_}}), " ],\n";
1229     } else {
1230         print OUT "  ", $_, " => ", quotify("perl", $target{$_}), ",\n"
1231     }
1232 }
1233 print OUT <<"EOF";
1234 );
1235
1236 EOF
1237 print OUT "our \%available_protocols = (\n";
1238 print OUT "  tls => [ ", join(", ", map { quotify("perl", $_) } @tls), " ],\n";
1239 print OUT "  dtls => [ ", join(", ", map { quotify("perl", $_) } @dtls), " ],\n";
1240 print OUT <<"EOF";
1241 );
1242
1243 EOF
1244 print OUT "our \%disabled = (\n";
1245 foreach (sort keys %disabled) {
1246     print OUT "  ", quotify("perl", $_), " => ", quotify("perl", $disabled{$_}), ",\n";
1247 }
1248 print OUT <<"EOF";
1249 );
1250
1251 EOF
1252 print OUT "our %withargs = (\n";
1253 foreach (sort keys %withargs) {
1254     if (ref($withargs{$_}) eq "ARRAY") {
1255         print OUT "  ", $_, " => [ ", join(", ",
1256                                            map { quotify("perl", $_) }
1257                                            @{$withargs{$_}}), " ],\n";
1258     } else {
1259         print OUT "  ", $_, " => ", quotify("perl", $withargs{$_}), ",\n"
1260     }
1261 }
1262 print OUT <<"EOF";
1263 );
1264
1265 1;
1266 EOF
1267 close(OUT);
1268
1269 print "IsMK1MF       =", ($target{build_scheme}->[0] eq "mk1mf" ? "yes" : "no"), "\n";
1270 print "CC            =$target{cc}\n";
1271 print "CFLAG         =$config{cflags}\n";
1272 print "EX_LIBS       =$config{lflags}\n";
1273 print "CPUID_OBJ     =$target{cpuid_obj}\n";
1274 print "BN_ASM        =$target{bn_obj}\n";
1275 print "EC_ASM        =$target{ec_obj}\n";
1276 print "DES_ENC       =$target{des_obj}\n";
1277 print "AES_ENC       =$target{aes_obj}\n";
1278 print "BF_ENC        =$target{bf_obj}\n";
1279 print "CAST_ENC      =$target{cast_obj}\n";
1280 print "RC4_ENC       =$target{rc4_obj}\n";
1281 print "RC5_ENC       =$target{rc5_obj}\n";
1282 print "MD5_OBJ_ASM   =$target{md5_obj}\n";
1283 print "SHA1_OBJ_ASM  =$target{sha1_obj}\n";
1284 print "RMD160_OBJ_ASM=$target{rmd160_obj}\n";
1285 print "CMLL_ENC      =$target{cmll_obj}\n";
1286 print "MODES_OBJ     =$target{modes_obj}\n";
1287 print "PADLOCK_OBJ   =$target{padlock_obj}\n";
1288 print "CHACHA_ENC    =$target{chacha_obj}\n";
1289 print "POLY1305_OBJ  =$target{poly1305_obj}\n";
1290 print "PROCESSOR     =$config{processor}\n";
1291 print "RANLIB        =$target{ranlib}\n";
1292 print "ARFLAGS       =$target{arflags}\n";
1293 print "PERL          =$config{perl}\n";
1294 print "\n";
1295 print "SIXTY_FOUR_BIT_LONG mode\n" if $config{b64l};
1296 print "SIXTY_FOUR_BIT mode\n" if $config{b64};
1297 print "THIRTY_TWO_BIT mode\n" if $config{b32};
1298 print "BN_LLONG mode\n" if $config{bn_ll};
1299 print "RC4 uses $config{rc4_int}\n" if $config{rc4_int} != $def_int;
1300 print "RC2 uses $config{rc2_int}\n" if $config{rc2_int} != $def_int;
1301
1302 run_dofile("$Makefile.in","$Makefile");
1303
1304 run_dofile("include/openssl/opensslconf.h.in", "include/openssl/opensslconf.h");
1305
1306 foreach my $alg ( 'bn' ) {
1307     run_dofile("crypto/include/internal/${alg}_conf.h.in",
1308                "crypto/include/internal/${alg}_conf.h");
1309 }
1310
1311 # Copy all Makefile.in to Makefile (except top-level)
1312 use File::Find;
1313 use IO::File;
1314 find(sub {
1315         return if ($_ ne "Makefile.in" || $File::Find::dir eq ".");
1316         my $in = IO::File->new($_, "r") or
1317             die sprintf "Error reading Makefile.in in %s: !$\n",
1318                 $File::Find::dir;
1319         my $out = IO::File->new("Makefile", "w") or
1320             die sprintf "Error writing Makefile in %s: !$\n",
1321                 $File::Find::dir;
1322         print $out "# Generated from $_, do not edit\n";
1323         while (my $line = <$in>) { print $out $line }
1324         $in->close() or
1325             die sprintf "Error reading Makefile.in in %s: !$\n",
1326                 $File::Find::dir;
1327         $out->close() or
1328             die sprintf "Error writing Makefile in %s: !$\n",
1329                 $File::Find::dir;
1330     }, ".");
1331
1332 my %builders = (
1333     unixmake => sub {
1334         my $make_command = "$make PERL=\'$config{perl}\'";
1335         my $make_targets = "";
1336         $make_targets .= " depend" if $config{depflags} ne $default_depflags && $make_depend;
1337         (system $make_command.$make_targets) == 0 or die "make $make_targets failed"
1338             if $make_targets ne "";
1339         if ($config{depflags} ne $default_depflags && !$make_depend) {
1340             $warn_make_depend++;
1341         }
1342     },
1343     mk1mf => sub {
1344         open (OUT,">crypto/buildinf.h") || die "Can't open buildinf.h";
1345         printf OUT <<"EOF";
1346 #ifndef MK1MF_BUILD
1347   /* auto-generated by Configure for crypto/cversion.c:
1348    * for Unix builds, crypto/Makefile.ssl generates functional definitions;
1349    * Windows builds (and other mk1mf builds) compile cversion.c with
1350    * -DMK1MF_BUILD and use definitions added to this file by util/mk1mf.pl. */
1351   #error "Windows builds (PLATFORM=$target) use mk1mf.pl-created Makefiles"
1352 #endif
1353 EOF
1354         close(OUT);
1355
1356         # create the ms/version32.rc file if needed
1357         if (! grep /^netware/, @{$target{build_scheme}}) {
1358             my ($v1, $v2, $v3, $v4);
1359             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) {
1360                 $v1=hex $1;
1361                 $v2=hex $2;
1362                 $v3=hex $3;
1363                 $v4=hex $4;
1364             }
1365             open (OUT,">ms/version32.rc") || die "Can't open ms/version32.rc";
1366             print OUT <<"EOF";
1367 #include <winver.h>
1368
1369 LANGUAGE 0x09,0x01
1370
1371 1 VERSIONINFO
1372   FILEVERSION $v1,$v2,$v3,$v4
1373   PRODUCTVERSION $v1,$v2,$v3,$v4
1374   FILEFLAGSMASK 0x3fL
1375 #ifdef _DEBUG
1376   FILEFLAGS 0x01L
1377 #else
1378   FILEFLAGS 0x00L
1379 #endif
1380   FILEOS VOS__WINDOWS32
1381   FILETYPE VFT_DLL
1382   FILESUBTYPE 0x0L
1383 BEGIN
1384     BLOCK "StringFileInfo"
1385     BEGIN
1386         BLOCK "040904b0"
1387         BEGIN
1388             // Required:
1389             VALUE "CompanyName", "The OpenSSL Project, http://www.openssl.org/\\0"
1390             VALUE "FileDescription", "OpenSSL Shared Library\\0"
1391             VALUE "FileVersion", "$config{version}\\0"
1392 #if defined(CRYPTO)
1393             VALUE "InternalName", "libeay32\\0"
1394             VALUE "OriginalFilename", "libeay32.dll\\0"
1395 #elif defined(SSL)
1396             VALUE "InternalName", "ssleay32\\0"
1397             VALUE "OriginalFilename", "ssleay32.dll\\0"
1398 #endif
1399             VALUE "ProductName", "The OpenSSL Toolkit\\0"
1400             VALUE "ProductVersion", "$config{version}\\0"
1401             // Optional:
1402             //VALUE "Comments", "\\0"
1403             VALUE "LegalCopyright", "Copyright Â© 1998-2015 The OpenSSL Project. Copyright Â© 1995-1998 Eric A. Young, Tim J. Hudson. All rights reserved.\\0"
1404             //VALUE "LegalTrademarks", "\\0"
1405             //VALUE "PrivateBuild", "\\0"
1406             //VALUE "SpecialBuild", "\\0"
1407         END
1408     END
1409     BLOCK "VarFileInfo"
1410     BEGIN
1411         VALUE "Translation", 0x409, 0x4b0
1412     END
1413 END
1414 EOF
1415             close(OUT);
1416         }
1417     },
1418     );
1419
1420 my ($builder, @builder_opts) = @{$target{build_scheme}};
1421 $builders{$builder}->(@builder_opts);
1422
1423 print <<"EOF";
1424
1425 Configured for $target.
1426 EOF
1427
1428 print <<"EOF" if (!$no_threads && !$threads);
1429
1430 The library could not be configured for supporting multi-threaded
1431 applications as the compiler options required on this system are not known.
1432 See file INSTALL for details if you need multi-threading.
1433 EOF
1434
1435 print <<"EOF" if ($no_shared_warn);
1436
1437 You gave the option 'shared', which is not supported on this platform, so
1438 we will pretend you gave the option 'no-shared'.  If you know how to implement
1439 shared libraries, please let us know (but please first make sure you have
1440 tried with a current version of OpenSSL).
1441 EOF
1442
1443 print <<"EOF" if ($warn_make_depend);
1444
1445 *** Because of configuration changes, you MUST do the following before
1446 *** building:
1447
1448         make depend
1449 EOF
1450
1451 exit(0);
1452
1453 ######################################################################
1454 #
1455 # Helpers and utility functions
1456 #
1457
1458 # Configuration file reading #########################################
1459
1460 # Helper function to implement conditional inheritance depending on the
1461 # value of $no_asm.  Used in inherit_from values as follows:
1462 #
1463 #      inherit_from => [ "template", asm("asm_tmpl") ]
1464 #
1465 sub asm {
1466     my @x = @_;
1467     sub {
1468         $no_asm ? () : @x;
1469     }
1470 }
1471
1472 # Helper function to implement adding values to already existing configuration
1473 # values.  It handles elements that are ARRAYs, CODEs and scalars
1474 sub _add {
1475     my $separator = shift;
1476
1477     # If there's any ARRAY in the collection of values, we will return
1478     # an ARRAY of combined values, otherwise a string of joined values
1479     # with $separator as the separator.
1480     my $found_array = 0;
1481
1482     my @values =
1483         map {
1484             if (ref($_) eq "ARRAY") {
1485                 $found_array = 1;
1486                 @$_;
1487             } else {
1488                 $_;
1489             }
1490     } (@_);
1491
1492     if ($found_array) {
1493         [ @values ];
1494     } else {
1495         join($separator, @values);
1496     }
1497 }
1498 sub add_before {
1499     my $separator = shift;
1500     my @x = @_;
1501     sub { _add($separator, @x, @_) };
1502 }
1503 sub add {
1504     my $separator = shift;
1505     my @x = @_;
1506     sub { _add($separator, @_, @x) };
1507 }
1508
1509 # configuration reader, evaluates the input file as a perl script and expects
1510 # it to fill %targets with target configurations.  Those are then added to
1511 # %table.
1512 sub read_config {
1513     my $fname = shift;
1514     open(CONFFILE, "< $fname")
1515         or die "Can't open configuration file '$fname'!\n";
1516     my $x = $/;
1517     undef $/;
1518     my $content = <CONFFILE>;
1519     $/ = $x;
1520     close(CONFFILE);
1521     my %targets = ();
1522     {
1523         local %table = %::table;    # Protect %table from tampering
1524
1525         eval $content;
1526         warn $@ if $@;
1527     }
1528
1529     # For each target, check that it's configured with a hash table.
1530     foreach (keys %targets) {
1531         if (ref($targets{$_}) ne "HASH") {
1532             if (ref($targets{$_}) eq "") {
1533                 warn "Deprecated target configuration for $_, ignoring...\n";
1534             } else {
1535                 warn "Misconfigured target configuration for $_ (should be a hash table), ignoring...\n";
1536             }
1537             delete $targets{$_};
1538         }
1539     }
1540
1541     %table = (%table, %targets);
1542
1543 }
1544
1545 # configuration resolver.  Will only resolve all the lazy evalutation
1546 # codeblocks for the chozen target and all those it inherits from,
1547 # recursively
1548 sub resolve_config {
1549     my $target = shift;
1550     my @breadcrumbs = @_;
1551
1552     if (grep { $_ eq $target } @breadcrumbs) {
1553         die "inherit_from loop!  target backtrace:\n  "
1554             ,$target,"\n  ",join("\n  ", @breadcrumbs),"\n";
1555     }
1556
1557     if (!defined($table{$target})) {
1558         warn "Warning! target $target doesn't exist!\n";
1559         return ();
1560     }
1561     # Recurse through all inheritances.  They will be resolved on the
1562     # fly, so when this operation is done, they will all just be a
1563     # bunch of attributes with string values.
1564     # What we get here, though, are keys with references to lists of
1565     # the combined values of them all.  We will deal with lists after
1566     # this stage is done.
1567     my %combined_inheritance = ();
1568     if ($table{$target}->{inherit_from}) {
1569         my @inherit_from =
1570             map { ref($_) eq "CODE" ? $_->() : $_ } @{$table{$target}->{inherit_from}};
1571         foreach (@inherit_from) {
1572             my %inherited_config = resolve_config($_, $target, @breadcrumbs);
1573
1574             # 'template' is a marker that's considered private to
1575             # the config that had it.
1576             delete $inherited_config{template};
1577
1578             map {
1579                 if (!$combined_inheritance{$_}) {
1580                     $combined_inheritance{$_} = [];
1581                 }
1582                 push @{$combined_inheritance{$_}}, $inherited_config{$_};
1583             } keys %inherited_config;
1584         }
1585     }
1586
1587     # We won't need inherit_from in this target any more, since we've
1588     # resolved all the inheritances that lead to this
1589     delete $table{$target}->{inherit_from};
1590
1591     # Now is the time to deal with those lists.  Here's the place to
1592     # decide what shall be done with those lists, all based on the
1593     # values of the target we're currently dealing with.
1594     # - If a value is a coderef, it will be executed with the list of
1595     #   inherited values as arguments.
1596     # - If the corresponding key doesn't have a value at all or is the
1597     #   emoty string, the inherited value list will be run through the
1598     #   default combiner (below), and the result becomes this target's
1599     #   value.
1600     # - Otherwise, this target's value is assumed to be a string that
1601     #   will simply override the inherited list of values.
1602     my $default_combiner = add(" ");
1603
1604     my %all_keys =
1605         map { $_ => 1 } (keys %combined_inheritance,
1606                          keys %{$table{$target}});
1607     foreach (sort keys %all_keys) {
1608
1609         # Current target doesn't have a value for the current key?
1610         # Assign it the default combiner, the rest of this loop body
1611         # will handle it just like any other coderef.
1612         if (!exists $table{$target}->{$_}) {
1613             $table{$target}->{$_} = $default_combiner;
1614         }
1615
1616         my $valuetype = ref($table{$target}->{$_});
1617         if ($valuetype eq "CODE") {
1618             # CODE reference, execute it with the inherited values as
1619             # arguments.
1620             $table{$target}->{$_} =
1621                 $table{$target}->{$_}->(@{$combined_inheritance{$_}});
1622         } elsif ($valuetype eq "ARRAY" || $valuetype eq "") {
1623             # ARRAY or Scalar, just leave it as is.
1624         } else {
1625             # Some other type of reference that we don't handle.
1626             # Better to abort at this point.
1627             die "cannot handle reference type $valuetype,"
1628                 ," found in target $target -> $_\n";
1629         }
1630     }
1631
1632     # Finally done, return the result.
1633     return %{$table{$target}};
1634 }
1635
1636 sub usage
1637         {
1638         print STDERR $usage;
1639         print STDERR "\npick os/compiler from:\n";
1640         my $j=0;
1641         my $i;
1642         my $k=0;
1643         foreach $i (sort keys %table)
1644                 {
1645                 next if $table{$i}->{template};
1646                 next if $i =~ /^debug/;
1647                 $k += length($i) + 1;
1648                 if ($k > 78)
1649                         {
1650                         print STDERR "\n";
1651                         $k=length($i);
1652                         }
1653                 print STDERR $i . " ";
1654                 }
1655         foreach $i (sort keys %table)
1656                 {
1657                 next if $table{$i}->{template};
1658                 next if $i !~ /^debug/;
1659                 $k += length($i) + 1;
1660                 if ($k > 78)
1661                         {
1662                         print STDERR "\n";
1663                         $k=length($i);
1664                         }
1665                 print STDERR $i . " ";
1666                 }
1667         print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n";
1668         exit(1);
1669         }
1670
1671 sub run_dofile()
1672 {
1673     my $in = shift;
1674     my $out = shift;
1675
1676     # should we remove $out ?
1677     system("$config{perl} -I. -Mconfigdata util/dofile.pl -o\"Configure\" $in > $out.new");
1678     exit 1 if $? != 0;
1679     rename("$out.new", $out) || die "Can't rename $out.new, $!";
1680 }
1681
1682 # Configuration printer ##############################################
1683
1684 sub print_table_entry
1685 {
1686     my $target = shift;
1687     my %target = resolve_config($target);
1688     my $type = shift;
1689
1690     # Don't print the templates
1691     return if $target{template};
1692
1693     my @sequence = (
1694         "sys_id",
1695         "cc",
1696         "cflags",
1697         "debug_cflags",
1698         "release_cflags",
1699         "thread_cflag",
1700         "unistd",
1701         "ld",
1702         "lflags",
1703         "debug_lflags",
1704         "release_lflags",
1705         "bn_ops",
1706         "cpuid_obj",
1707         "bn_obj",
1708         "ec_obj",
1709         "des_obj",
1710         "aes_obj",
1711         "bf_obj",
1712         "md5_obj",
1713         "sha1_obj",
1714         "cast_obj",
1715         "rc4_obj",
1716         "rmd160_obj",
1717         "rc5_obj",
1718         "wp_obj",
1719         "cmll_obj",
1720         "modes_obj",
1721         "padlock_obj",
1722         "perlasm_scheme",
1723         "dso_scheme",
1724         "shared_target",
1725         "shared_cflag",
1726         "shared_ldflag",
1727         "shared_extension",
1728         "obj_extension",
1729         "exe_extension",
1730         "ranlib",
1731         "ar",
1732         "arflags",
1733         "multilib",
1734         "build_scheme",
1735         );
1736
1737     if ($type eq "TABLE") {
1738         print "\n";
1739         print "*** $target\n";
1740         printf "\$%-12s = %s\n", $_, $target{$_} foreach (@sequence);
1741     } elsif ($type eq "HASH") {
1742         my $largest =
1743             length((sort { length($a) <=> length($b) } @sequence)[-1]);
1744         print "    '$target' => {\n";
1745         foreach (@sequence) {
1746             if ($target{$_}) {
1747                 print "      '",$_,"'"," " x ($largest - length($_))," => '",$target{$_},"',\n";
1748             }
1749         }
1750         print "    },\n";
1751     }
1752 }
1753
1754 # Utility routines ###################################################
1755
1756 sub which
1757         {
1758         my($name)=@_;
1759         my $path;
1760         foreach $path (split /:/, $ENV{PATH})
1761                 {
1762                 if (-f "$path/$name$target{exe_extension}" and -x _)
1763                         {
1764                         return "$path/$name$target{exe_extension}" unless ($name eq "perl" and
1765                          system("$path/$name$target{exe_extension} -e " . '\'exit($]<5.0);\''));
1766                         }
1767                 }
1768         }
1769
1770 sub quotify {
1771     my %processors = (
1772         perl    => sub { my $x = shift;
1773                          $x =~ s/([\\\$\@"])/\\$1/g;
1774                          return '"'.$x.'"'; },
1775         );
1776     my $for = shift;
1777     my $processor =
1778         defined($processors{$for}) ? $processors{$for} : sub { shift; };
1779
1780     map { $processor->($_); } @_;
1781 }
1782
1783 # collect_information($filename, $line_continue, $regexp => $CODEref, ...)
1784 # $filename is the file to read.
1785 # $line_continue is either undef (which is a noop), or two arguments, where
1786 # the first is a regexp detecting a line continuation ending, and the
1787 # following argument is a CODEref that takes care of concatenating two
1788 # lines.
1789 # All following arguments are regex/CODEref pairs, where the regexp detects a
1790 # line and the CODEref does something with the result of the regexp.
1791 sub collect_information {
1792     my $filename = shift;
1793     my $line_continue_re = shift;
1794     my $line_concat = defined($line_continue_re) ? shift : undef;
1795     my %collectors = @_;
1796
1797     my $saved_line = "";
1798     open IN, $filename || die "unable to read $filename: $!\n";
1799     while(<IN>) {
1800         chomp;
1801         if (defined $line_concat) {
1802             $_ = $line_concat->($saved_line, $_);
1803         }
1804         if (defined $line_continue_re && /$line_continue_re/) {
1805             $saved_line = $_;
1806             next;
1807         }
1808         foreach my $re (keys %collectors) {
1809             if (/$re/) { $collectors{$re}->() };
1810         }
1811     }
1812     close IN;
1813 }