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