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