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