Remove support for Borland C++
[openssl.git] / util / mk1mf.pl
1 #!/usr/bin/env perl
2 # A bit of an evil hack but it post processes the file ../MINFO which
3 # is generated by `make files` in the top directory.
4 # This script outputs one mega makefile that has no shell stuff or any
5 # funny stuff (if the target is not "copy").
6 # If the target is "copy", then it tries to create a makefile that can be
7 # safely used with the -j flag and that is compatible with the top-level
8 # Makefile, in the sense that it uses the same options and assembler files etc.
9
10 use Cwd;
11
12 $INSTALLTOP="/usr/local";
13 $OPENSSLDIR="/usr/local/ssl";
14 $ENGINESDIR="/usr/local/lib/engines";
15 $OPTIONS="";
16 $ssl_version="";
17 $banner="\t\@echo Building OpenSSL";
18
19 my $no_static_engine = 1;
20 my $engines = "";
21 my @engines_obj = "";
22 my $otherlibs = "";
23 local $zlib_opt = 0;    # 0 = no zlib, 1 = static, 2 = dynamic
24 local $zlib_lib = "";
25 local $perl_asm = 0;    # 1 to autobuild asm files from perl scripts
26
27 local $fips_canister_path = "";
28 my $fips_premain_dso_exe_path = "";
29 my $fips_premain_c_path = "";
30 my $fips_sha1_exe_path = "";
31
32 local $fipscanisterbuild = 0;
33
34 my $fipscanisteronly = 0;
35
36 my $fipslibdir = "";
37 my $baseaddr = "";
38
39 my $ex_l_libs = "";
40
41 my $build_targets = "lib exe";
42 my $libs_dep = "\$(O_CRYPTO) \$(O_SSL)";
43
44 # Options to import from top level Makefile
45
46 my %mf_import = (
47         VERSION        => \$ssl_version,
48         OPTIONS        => \$OPTIONS,
49         INSTALLTOP     => \$INSTALLTOP,
50         OPENSSLDIR     => \$OPENSSLDIR,
51         ENGINESDIR     => \$ENGINESDIR,
52         PLATFORM       => \$mf_platform,
53         CC             => \$mf_cc,
54         CFLAG          => \$mf_cflag,
55         CFLAG_Q        => \$mf_cflag_q,
56         SHARED_CFLAG   => \$mf_shared_cflag,
57         DEPFLAG        => \$mf_depflag,
58         CPUID_OBJ      => \$mf_cpuid_asm,
59         BN_ASM         => \$mf_bn_asm,
60         DES_ENC        => \$mf_des_asm,
61         AES_ENC        => \$mf_aes_asm,
62         BF_ENC         => \$mf_bf_asm,
63         CAST_ENC       => \$mf_cast_asm,
64         RC4_ENC        => \$mf_rc4_asm,
65         RC5_ENC        => \$mf_rc5_asm,
66         MD5_ASM_OBJ    => \$mf_md5_asm,
67         SHA1_ASM_OBJ   => \$mf_sha_asm,
68         RMD160_ASM_OBJ => \$mf_rmd_asm,
69         WP_ASM_OBJ     => \$mf_wp_asm,
70         CMLL_ENC       => \$mf_cm_asm,
71         MODES_ASM_OBJ  => \$mf_modes_asm,
72         ENGINES_ASM_OBJ=> \$mf_engines_asm,
73         PERLASM_SCHEME => \$mf_perlasm_scheme,
74         FIPSCANISTERONLY  => \$mf_fipscanisteronly,
75         FIPSCANISTERINTERNAL  => \$mf_fipscanisterinternal,
76         EC_ASM         => \$mf_ec_asm,
77 );
78
79 open(IN,"<Makefile") || die "unable to open Makefile!\n";
80 while(<IN>) {
81     my ($mf_opt, $mf_ref);
82     while (($mf_opt, $mf_ref) = each %mf_import) {
83         if (/^$mf_opt\s*=\s*(.*)$/ && !defined($$mfref)) {
84            $$mf_ref = $1;
85         }
86     }
87 }
88 close(IN);
89
90 if ($mf_fipscanisterinternal eq "y") {
91         $fips = 1;
92         $fipscanisterbuild = 1;
93         $fipscanisteronly = 1;
94 }
95
96
97 die "Makefile is not the toplevel Makefile!\n" if $ssl_version eq "";
98
99 $infile="MINFO";
100
101 %ops=(
102         "VC-WIN32",   "Microsoft Visual C++ [4-6] - Windows NT or 9X",
103         "VC-WIN64I",  "Microsoft C/C++ - Win64/IA-64",
104         "VC-WIN64A",  "Microsoft C/C++ - Win64/x64",
105         "VC-CE",   "Microsoft eMbedded Visual C++ 3.0 - Windows CE ONLY",
106         "VC-NT",   "Microsoft Visual C++ [4-6] - Windows NT ONLY",
107         "Mingw32", "GNU C++ - Windows NT or 9x",
108         "Mingw32-files", "Create files with DOS copy ...",
109         "linux-elf","Linux elf",
110         "ultrix-mips","DEC mips ultrix",
111         "FreeBSD","FreeBSD distribution",
112         "OS2-EMX", "EMX GCC OS/2",
113         "netware-clib", "CodeWarrior for NetWare - CLib - with WinSock Sockets",
114         "netware-clib-bsdsock", "CodeWarrior for NetWare - CLib - with BSD Sockets",
115         "netware-libc", "CodeWarrior for NetWare - LibC - with WinSock Sockets",
116         "netware-libc-bsdsock", "CodeWarrior for NetWare - LibC - with BSD Sockets",
117         "default","cc under unix",
118         "auto", "auto detect from top level Makefile",
119         "copy", "copy from top level Makefile"
120         );
121
122 $platform="";
123 my $xcflags="";
124 foreach (@ARGV)
125         {
126         if (!&read_options && !defined($ops{$_}))
127                 {
128                 print STDERR "unknown option - $_\n";
129                 print STDERR "usage: perl mk1mf.pl [options] [system]\n";
130                 print STDERR "\nwhere [system] can be one of the following\n";
131                 foreach $i (sort keys %ops)
132                 { printf STDERR "\t%-10s\t%s\n",$i,$ops{$i}; }
133                 print STDERR <<"EOF";
134 and [options] can be one of
135         no-md2 no-md4 no-md5 no-sha no-mdc2     - Skip this digest
136         no-rmd160
137         no-blake2                               - No blake2
138         no-rc2 no-rc4 no-rc5 no-idea no-des     - Skip this symetric cipher
139         no-bf no-cast no-aes no-camellia no-seed
140         no-rsa no-dsa no-dh                     - Skip this public key cipher
141         no-ssl3                                 - Skip this version of SSL
142         just-ssl                                - remove all non-ssl keys/digest
143         no-asm                                  - No x86 asm
144         no-srp                                  - No SRP
145         no-ec                                   - No EC
146         no-engine                               - No engine
147         no-egd                                  - No EGD
148         no-hw                                   - No hw
149         no-async                                - No Async (use NULL)
150         no-autoalginit                          - Don't auto load algorithms in libcrypto
151         no-autoerrinit                          - Don't auto load error strings for libcrypto or libssl
152         nasm                                    - Use NASM for x86 asm
153         nw-nasm                                 - Use NASM x86 asm for NetWare
154         nw-mwasm                                - Use Metrowerks x86 asm for NetWare
155         gaswin                                  - Use GNU as with Mingw32
156         no-socks                                - No socket code
157         no-err                                  - No error strings
158         dll/shlib                               - Build shared libraries (MS)
159         debug                                   - Debug build
160         profile                                 - Profiling build
161         gcc                                     - Use Gcc (unix)
162
163 Values that can be set
164 TMP=tmpdir OUT=outdir SRC=srcdir BIN=binpath INC=header-outdir CC=C-compiler
165
166 -L<ex_lib_path> -l<ex_lib>                      - extra library flags (unix)
167 -<ex_cc_flags>                                  - extra 'cc' flags,
168                                                   added (MS), or replace (unix)
169 EOF
170                 exit(1);
171                 }
172         $platform=$_;
173         }
174 foreach (grep(!/^$/, split(/ /, $OPTIONS)))
175         {
176         print STDERR "unknown option - $_\n" if !&read_options;
177         }
178
179 $no_mdc2=1 if ($no_des);
180
181 $no_ssl3=1 if ($no_md5);
182 $no_ssl3=1 if ($no_rsa && $no_dh);
183
184 $out_def="out";
185 $inc_def="outinc";
186 $tmp_def="tmp";
187
188 $perl="perl" unless defined $perl;
189 $mkdir="-mkdir" unless defined $mkdir;
190 $mv="mv" unless defined $mv;
191
192 ($ssl,$crypto)=("ssl","crypto");
193 $ranlib="echo ranlib";
194
195 $cc=(defined($VARS{'CC'}))?$VARS{'CC'}:'cc';
196 $src_dir=(defined($VARS{'SRC'}))?$VARS{'SRC'}: $platform eq 'copy' ? getcwd() : '.';
197 $bin_dir=(defined($VARS{'BIN'}))?$VARS{'BIN'}:'';
198
199 # $bin_dir.=$o causes a core dump on my sparc :-(
200
201
202 $NT=0;
203
204 push(@INC,"util/pl","pl");
205
206 if ($platform eq "auto" || $platform eq 'copy') {
207         $orig_platform = $platform;
208         $platform = $mf_platform;
209         print STDERR "Imported platform $mf_platform\n";
210 }
211
212 if (($platform =~ /VC-(.+)/))
213         {
214         $FLAVOR=$1;
215         $NT = 1 if $1 eq "NT";
216         require 'VC-32.pl';
217         }
218 elsif ($platform eq "Mingw32")
219         {
220         require 'Mingw32.pl';
221         }
222 elsif ($platform eq "Mingw32-files")
223         {
224         require 'Mingw32f.pl';
225         }
226 elsif ($platform eq "FreeBSD")
227         {
228         require 'unix.pl';
229         $cflags='-DTERMIO -D_ANSI_SOURCE -O2 -fomit-frame-pointer';
230         }
231 elsif ($platform eq "linux-elf")
232         {
233         require "unix.pl";
234         require "linux.pl";
235         $unix=1;
236         }
237 elsif ($platform eq "ultrix-mips")
238         {
239         require "unix.pl";
240         require "ultrix.pl";
241         $unix=1;
242         }
243 elsif ($platform eq "OS2-EMX")
244         {
245         $wc=1;
246         require 'OS2-EMX.pl';
247         }
248 elsif (($platform eq "netware-clib") || ($platform eq "netware-libc") ||
249        ($platform eq "netware-clib-bsdsock") || ($platform eq "netware-libc-bsdsock"))
250         {
251         $LIBC=1 if $platform eq "netware-libc" || $platform eq "netware-libc-bsdsock";
252         $BSDSOCK=1 if ($platform eq "netware-libc-bsdsock") || ($platform eq "netware-clib-bsdsock");
253         require 'netware.pl';
254         }
255 else
256         {
257         require "unix.pl";
258
259         $unix=1;
260         $cflags.=' -DTERMIO';
261         }
262
263 $out_dir=(defined($VARS{'OUT'}))?$VARS{'OUT'}:$out_def.($debug?".dbg":"");
264 $tmp_dir=(defined($VARS{'TMP'}))?$VARS{'TMP'}:$tmp_def.($debug?".dbg":"");
265 $inc_dir=(defined($VARS{'INC'}))?$VARS{'INC'}:$inc_def;
266
267 $bin_dir=$bin_dir.$o unless ((substr($bin_dir,-1,1) eq $o) || ($bin_dir eq ''));
268
269 $cflags= "$xcflags$cflags" if $xcflags ne "";
270
271 $cflags.=" -DOPENSSL_NO_IDEA" if $no_idea;
272 $cflags.=" -DOPENSSL_NO_AES"  if $no_aes;
273 $cflags.=" -DOPENSSL_NO_CAMELLIA"  if $no_camellia;
274 $cflags.=" -DOPENSSL_NO_SEED" if $no_seed;
275 $cflags.=" -DOPENSSL_NO_RC2"  if $no_rc2;
276 $cflags.=" -DOPENSSL_NO_RC4"  if $no_rc4;
277 $cflags.=" -DOPENSSL_NO_RC5"  if $no_rc5;
278 $cflags.=" -DOPENSSL_NO_MD2"  if $no_md2;
279 $cflags.=" -DOPENSSL_NO_MD4"  if $no_md4;
280 $cflags.=" -DOPENSSL_NO_MD5"  if $no_md5;
281 $cflags.=" -DOPENSSL_NO_RMD160" if $no_ripemd;
282 $cflags.=" -DOPENSSL_NO_MDC2" if $no_mdc2;
283 $cflags.=" -DOPENSSL_NO_BF"  if $no_bf;
284 $cflags.=" -DOPENSSL_NO_CAST" if $no_cast;
285 $cflags.=" -DOPENSSL_NO_DES"  if $no_des;
286 $cflags.=" -DOPENSSL_NO_RSA"  if $no_rsa;
287 $cflags.=" -DOPENSSL_NO_DSA"  if $no_dsa;
288 $cflags.=" -DOPENSSL_NO_DH"   if $no_dh;
289 $cflags.=" -DOPENSSL_NO_WHIRLPOOL"   if $no_whirlpool;
290 $cflags.=" -DOPENSSL_NO_SOCK" if $no_sock;
291 $cflags.=" -DOPENSSL_NO_SSL3" if $no_ssl3;
292 $cflags.=" -DOPENSSL_NO_SRP" if $no_srp;
293 $cflags.=" -DOPENSSL_NO_CMS" if $no_cms;
294 $cflags.=" -DOPENSSL_NO_ERR"  if $no_err;
295 $cflags.=" -DOPENSSL_NO_EC"   if $no_ec;
296 $cflags.=" -DOPENSSL_NO_GOST" if $no_gost;
297 $cflags.=" -DOPENSSL_NO_ENGINE"   if $no_engine;
298 $cflags.=" -DOPENSSL_NO_HW"   if $no_hw;
299 $cflags.=" -DOPENSSL_NO_ASYNC" if $no_async;
300 $cflags.=" -DOPENSSL_NO_AUTOALGINIT" if $no_autoalginit;
301 $cflags.=" -DOPENSSL_NO_AUTOERRINIT" if $no_autoerrinit;
302 $cflags.=" -DOPENSSL_FIPS"    if $fips;
303 $cflags.=" -DOPENSSL_NO_EC2M"    if $no_ec2m;
304 $cflags.= " -DZLIB" if $zlib_opt;
305 $cflags.= " -DZLIB_SHARED" if $zlib_opt == 2;
306 $cflags.=" -DOPENSSL_PIC";
307
308 if ($no_static_engine)
309         {
310         $cflags .= " -DOPENSSL_NO_STATIC_ENGINE";
311         }
312 else
313         {
314         $cflags .= " -DOPENSSL_NO_DYNAMIC_ENGINE";
315         }
316
317 #$cflags.=" -DRSAref"  if $rsaref ne "";
318
319 ## if ($unix)
320 ##      { $cflags="$c_flags" if ($c_flags ne ""); }
321 ##else
322         { $cflags="$c_flags$cflags" if ($c_flags ne ""); }
323
324 if ($orig_platform eq 'copy') {
325     $cflags = "$mf_cflag $mf_shared_cflag";
326     $cc = $mf_cc;
327 }
328
329 $ex_libs="$l_flags$ex_libs" if ($l_flags ne "");
330
331
332 %shlib_ex_cflags=("SSL" => " -DOPENSSL_BUILD_SHLIBSSL",
333                   "CRYPTO" => " -DOPENSSL_BUILD_SHLIBCRYPTO");
334
335 if ($msdos)
336         {
337         $banner ="\t\@echo Make sure you have run 'perl Configure $platform' in the\n";
338         $banner.="\t\@echo top level directory, if you don't have perl, you will\n";
339         $banner.="\t\@echo need to probably edit crypto/bn/bn.h, check the\n";
340         $banner.="\t\@echo documentation for details.\n";
341         }
342
343 # have to do this to allow $(CC) under unix
344 $link="$bin_dir$link" if ($link !~ /^\$/);
345
346 $INSTALLTOP =~ s|/|$o|g;
347 $OPENSSLDIR =~ s|/|$o|g;
348
349 #############################################
350 # We parse in input file and 'store' info for later printing.
351 open(IN,"<$infile") || die "unable to open $infile:$!\n";
352 $_=<IN>;
353 for (;;)
354         {
355         s/\s*$//; # was chop, didn't work in mixture of perls for Windows...
356
357         ($key,$val)=/^([^=]+)=(.*)/;
358         if ($key eq "RELATIVE_DIRECTORY")
359                 {
360                 if ($lib ne "")
361                         {
362                         if ($fips && $dir =~ /^fips/)
363                                 {
364                                 $uc = "FIPS";
365                                 }
366                         else
367                                 {
368                                 $uc=$lib;
369                                 $uc =~ s/^lib(.*)\.a/$1/;
370                                 $uc =~ tr/a-z/A-Z/;
371                                 }
372                         if (($uc ne "FIPS") || $fipscanisterbuild)
373                                 {
374                                 $lib_nam{$uc}=$uc;
375                                 $lib_obj{$uc}.=$libobj." ";
376                                 }
377                         }
378                 last if ($val eq "FINISHED");
379                 $lib="";
380                 $libobj="";
381                 $dir=$val;
382                 }
383
384         if ($key eq "ZLIB_INCLUDE")
385                 { $cflags .= " $val" if $val ne "";}
386
387         if ($key eq "LIBZLIB")
388                 { $zlib_lib = "$val" if $val ne "";}
389
390         if ($key eq "EX_LIBS")
391                 { $ex_libs .= " $val" if $val ne "";}
392
393         # There was a condition here before:
394         #       !$fipscanisteronly || $dir =~ /^fips/
395         # It currently fills no function and needs to be rewritten anyway, so
396         # removed for now.
397         if ($dir eq "test" && $key eq "EXE")
398                 {
399                 foreach my $t (split /\s+/, $val) {
400                         $test.=&var_add($dir,$t, 0) if $t; }
401                 }
402
403         if ($key eq "EXE_OBJ")
404                 { $e_exe.=&var_add($dir,$val, 0); }
405
406         if ($key eq "LIB")
407                 {
408                 $lib=$val;
409                 $lib =~ s/^.*\/([^\/]+)$/$1/;
410                 }
411         if ($key eq "LIBNAME" && $no_static_engine)
412                 {
413                 $lib=$val;
414                 $lib =~ s/^.*\/([^\/]+)$/$1/;
415                 $otherlibs .= " $lib";
416                 }
417
418         if ($key eq "HEADER")
419                 { $header.=&var_add($dir,$val, 1); }
420
421         if ($key eq "LIBOBJ")
422             {
423             if ($dir ne "engines" || !$no_static_engine)
424                 { $libobj=&var_add($dir,$val, 0); }
425             else
426                 { push(@engines_obj,split(/\s+/,&var_add($dir,$val,0))); }
427             }
428         if ($key eq "LIBNAMES" && $dir eq "engines" && $no_static_engine)
429                 { $engines.=$val }
430
431         if ($key eq "FIPS_EX_OBJ")
432                 { 
433                 $fips_ex_obj=&var_add("crypto",$val,0);
434                 }
435
436         if ($key eq "FIPSLIBDIR")
437                 {
438                 $fipslibdir=$val;
439                 $fipslibdir =~ s/\/$//;
440                 $fipslibdir =~ s/\//$o/g;
441                 }
442
443         if ($key eq "BASEADDR")
444                 { $baseaddr=$val;}
445
446         if (!($_=<IN>))
447                 { $_="RELATIVE_DIRECTORY=FINISHED\n"; }
448         }
449 close(IN);
450
451 if ($orig_platform eq 'copy')
452         {
453         # Remove opensslconf.h so it doesn't get updated if we configure a
454         # different branch.
455         $header =~ s/[^ ]+\/opensslconf.h//;
456         }
457
458 if ($fips)
459         {
460
461         foreach (split " ", $fips_ex_obj)
462                 {
463                 $fips_exclude_obj{$1} = 1 if (/\/([^\/]*)$/);
464                 }
465         foreach (split " ",
466                 "$mf_cpuid_asm $mf_aes_asm $mf_sha_asm $mf_bn_asm " .
467                 "$mf_des_asm $mf_modes_asm")
468                 {
469                 s/\.o//;
470                 $fips_exclude_obj{$_} = 1;
471                 }
472         my @ltmp = split " ", $lib_obj{"CRYPTO"};
473
474
475         $lib_obj{"CRYPTO"} = "";
476
477         foreach(@ltmp)
478                 {
479                 if (/\/([^\/]*)$/ && exists $fips_exclude_obj{$1})
480                         {
481                         if ($fipscanisterbuild)
482                                 {
483                                 $lib_obj{"FIPS"} .= "$_ ";
484                                 }
485                         }
486                 elsif (!$fipscanisteronly)
487                         {
488                         $lib_obj{"CRYPTO"} .= "$_ ";
489                         }
490                 }
491
492         }
493
494 if ($fipscanisterbuild)
495         {
496         $fips_canister_path = "\$(LIB_D)${o}fipscanister.lib" if $fips_canister_path eq "";
497         $fips_premain_c_path = "\$(LIB_D)${o}fips_premain.c";
498         }
499 else
500         {
501         if ($fips_canister_path eq "")
502                 {
503                 $fips_canister_path = "\$(FIPSLIB_D)${o}fipscanister.lib";
504                 }
505
506         if ($fips_premain_c_path eq "")
507                 {
508                 $fips_premain_c_path = "\$(FIPSLIB_D)${o}fips_premain.c";
509                 }
510         }
511
512 if ($fips)
513         {
514         if ($fips_sha1_exe_path eq "")
515                 {
516                 $fips_sha1_exe_path =
517                         "\$(BIN_D)${o}fips_standalone_sha1$exep";
518                 }
519         }
520         else
521         {
522         $fips_sha1_exe_path = "";
523         }
524
525 if ($fips_premain_dso_exe_path eq "")
526         {
527         $fips_premain_dso_exe_path = "\$(BIN_D)${o}fips_premain_dso$exep";
528         }
529
530 #       $ex_build_targets .= "\$(BIN_D)${o}\$(E_PREMAIN_DSO)$exep" if ($fips);
531
532 if ($fips)
533         {
534         if (!$shlib)
535                 {
536                 $build_targets .= " \$(LIB_D)$o$crypto_compat \$(PREMAIN_DSO_EXE)";
537                 $ex_l_libs .= " \$(O_FIPSCANISTER)";
538                 $ex_libs_dep .= " \$(O_FIPSCANISTER)" if $fipscanisterbuild;
539                 }
540         if ($fipscanisterbuild)
541                 {
542                 $fipslibdir = "\$(LIB_D)";
543                 }
544         else
545                 {
546                 if ($fipslibdir eq "")
547                         {
548                         open (IN, "util/fipslib_path.txt") || fipslib_error();
549                         $fipslibdir = <IN>;
550                         close IN;
551                         $fipslibdir = "" unless defined($fipslibdir);
552                         $fipslibdir =~ s{\R$}{};
553                         fipslib_error() if ($fipslibdir eq "");
554                         }
555                 fips_check_files($fipslibdir,
556                                 "fipscanister.lib", "fipscanister.lib.sha1",
557                                 "fips_premain.c", "fips_premain.c.sha1");
558                 }
559         }
560
561 if ($fipscanisteronly)
562         {
563         $build_targets = "\$(O_FIPSCANISTER) \$(T_EXE)";
564         $libs_dep = "";
565         }
566
567 $cp2 = $cp unless defined $cp2;
568
569 $extra_install= <<"EOF";
570         \$(CP) \"include${o}openssl${o}*.\[ch\]\" \"\$(INSTALLTOP)${o}include${o}openssl\"
571         \$(CP) \"\$(BIN_D)$o\$(E_EXE)$exep \$(INSTALLTOP)${o}bin\"
572         \$(MKDIR) \"\$(OPENSSLDIR)\"
573         \$(CP) apps${o}openssl.cnf \"\$(OPENSSLDIR)\"
574 EOF
575
576 if ($fipscanisteronly)
577         {
578         $extra_install = <<"EOF";
579         \$(CP) \"\$(O_FIPSCANISTER)\" \"\$(INSTALLTOP)${o}lib\"
580         \$(CP) \"\$(O_FIPSCANISTER).sha1\" \"\$(INSTALLTOP)${o}lib\"
581         \$(CP2) \"fips${o}fips_premain.c\" \"\$(INSTALLTOP)${o}lib\"
582         \$(CP) \"fips${o}fips_premain.c.sha1\" \"\$(INSTALLTOP)${o}lib\"
583         \$(CP) \"include${o}openssl${o}fips.h\" \"\$(INSTALLTOP)${o}include${o}openssl\"
584         \$(CP) \"include${o}openssl${o}fips_rand.h\" \"\$(INSTALLTOP)${o}include${o}openssl\"
585         \$(CP) "\$(BIN_D)${o}fips_standalone_sha1$exep" \"\$(INSTALLTOP)${o}bin\"
586         \$(CP) \"util${o}fipslink.pl\" \"\$(INSTALLTOP)${o}bin\"
587 EOF
588         }
589 elsif ($shlib)
590         {
591         $extra_install .= <<"EOF";
592         \$(CP) \"\$(O_SSL)\" \"\$(INSTALLTOP)${o}bin\"
593         \$(CP) \"\$(O_CRYPTO)\" \"\$(INSTALLTOP)${o}bin\"
594         \$(CP) \"\$(L_SSL)\" \"\$(INSTALLTOP)${o}lib\"
595         \$(CP) \"\$(L_CRYPTO)\" \"\$(INSTALLTOP)${o}lib\"
596 EOF
597         if ($no_static_engine)
598                 {
599                 $extra_install .= <<"EOF"
600         \$(MKDIR) \"\$(INSTALLTOP)${o}lib${o}engines\"
601         \$(CP) \"\$(E_SHLIB)\" \"\$(INSTALLTOP)${o}lib${o}engines\"
602 EOF
603                 }
604         }
605 else
606         {
607         $extra_install .= <<"EOF";
608         \$(CP) \"\$(O_SSL)\" \"\$(INSTALLTOP)${o}lib\"
609         \$(CP) \"\$(O_CRYPTO)\" \"\$(INSTALLTOP)${o}lib\"
610 EOF
611         $ex_libs .= " $zlib_lib" if $zlib_opt == 1;
612         }
613
614 my $asm_def = $orig_platform eq 'copy' ? "" : "ASM=$bin_dir$asm";
615
616 $cflags =~ s/\((ENGINESDIR|OPENSSLDIR)\)/\(${1}_QQ\)/g;
617 (my $cflags_q = $cflags) =~ s/([\\"])/\\$1/g;
618 (my $INSTALLTOP_Q = $INSTALLTOP) =~ s/([\\"])/\\$1/g;
619 (my $INSTALLTOP_QQ = $INSTALLTOP_Q) =~ s/\\/\\\\/g;
620 (my $OPENSSLDIR_Q = $OPENSSLDIR) =~ s/([\\"])/\\$1/g;
621 (my $OPENSSLDIR_QQ = $OPENSSLDIR_Q) =~ s/\\/\\\\/g;
622 (my $ENGINESDIR_Q = $ENGINESDIR) =~ s/([\\"])/\\$1/g;
623 (my $ENGINESDIR_QQ = $ENGINESDIR_Q) =~ s/\\/\\\\/g;
624
625 $defs= <<"EOF";
626 # N.B. You MUST use -j on FreeBSD.
627 # This makefile has been automatically generated from the OpenSSL distribution.
628 # This single makefile will build the complete OpenSSL distribution and
629 # by default leave the 'interesting' output files in .${o}out and the stuff
630 # that needs deleting in .${o}tmp.
631 # The file was generated by running 'make makefile.one', which
632 # does a 'make files', which writes all the environment variables from all
633 # the makefiles to the file call MINFO.  This file is used by
634 # util${o}mk1mf.pl to generate makefile.one.
635 # The 'makefile per directory' system suites me when developing this
636 # library and also so I can 'distribute' indervidual library sections.
637 # The one monster makefile better suits building in non-unix
638 # environments.
639
640 EOF
641
642 $defs .= $preamble if defined $preamble;
643
644 $defs.= <<"EOF";
645 INSTALLTOP=$INSTALLTOP
646 INSTALLTOP_QQ=$INSTALLTOP_QQ
647 OPENSSLDIR=$OPENSSLDIR
648 OPENSSLDIR_QQ=$OPENSSLDIR_QQ
649 ENGINESDIR=$ENGINESDIR
650 ENGINESDIR_QQ=$ENGINESDIR_QQ
651
652 # Set your compiler options
653 PLATFORM=$platform
654 CC=$bin_dir${cc}
655 CFLAG=$cflags
656 CFLAG_Q=$cflags_q
657 APP_CFLAG=$app_cflag
658 LIB_CFLAG=$lib_cflag
659 SHLIB_CFLAG=$shl_cflag
660 APP_EX_OBJ=$app_ex_obj
661 SHLIB_EX_OBJ=$shlib_ex_obj
662 # add extra libraries to this define, for solaris -lsocket -lnsl would
663 # be added
664 EX_LIBS=$ex_libs
665
666 # The OpenSSL directory
667 SRC_D=$src_dir
668
669 LINK_CMD=$link
670 LFLAGS=$lflags
671 RSC=$rsc
672 FIPSLINK=\$(PERL) util${o}fipslink.pl
673
674 # The output directory for everything interesting
675 OUT_D=$out_dir
676 # The output directory for all the temporary muck
677 TMP_D=$tmp_dir
678
679 PERL=$perl
680 PERLASM_SCHEME=$mf_perlasm_scheme
681 CP=$cp
682 CP2=$cp2
683 RM=$rm
684 MV=$mv
685 RANLIB=$ranlib
686 MKDIR=$mkdir
687 MKLIB=$bin_dir$mklib
688 MLFLAGS=$mlflags
689 $asm_def
690
691 # FIPS validated module and support file locations
692
693 E_PREMAIN_DSO=fips_premain_dso
694
695 FIPSLIB_D=$fipslibdir
696 BASEADDR=$baseaddr
697 FIPS_PREMAIN_SRC=$fips_premain_c_path
698 O_FIPSCANISTER=$fips_canister_path
699 FIPS_SHA1_EXE=$fips_sha1_exe_path
700 PREMAIN_DSO_EXE=$fips_premain_dso_exe_path
701
702 ######################################################
703 # You should not need to touch anything below this point
704 ######################################################
705
706 E_EXE=openssl
707 SSL=$ssl
708 CRYPTO=$crypto
709
710 # BIN_D  - Binary output directory
711 # TEST_D - Binary test file output directory
712 # LIB_D  - library output directory
713 # ENG_D  - dynamic engine output directory
714 # Note: if you change these point to different directories then uncomment out
715 # the lines around the 'NB' comment below.
716
717 BIN_D=\$(OUT_D)
718 TEST_D=\$(OUT_D)
719 LIB_D=\$(OUT_D)
720 ENG_D=\$(OUT_D)
721
722 # INCL_D - local library directory
723 # OBJ_D  - temp object file directory
724 OBJ_D=\$(TMP_D)
725 INCL_D=\$(TMP_D)
726
727 O_SSL=     \$(LIB_D)$o$plib\$(SSL)$shlibp
728 O_CRYPTO=  \$(LIB_D)$o$plib\$(CRYPTO)$shlibp
729 SO_SSL=    $plib\$(SSL)$so_shlibp
730 SO_CRYPTO= $plib\$(CRYPTO)$so_shlibp
731 L_SSL=     \$(LIB_D)$o$plib\$(SSL)$libp
732 L_CRYPTO=  \$(LIB_D)$o$plib\$(CRYPTO)$libp
733
734 L_LIBS= \$(L_SSL) \$(L_CRYPTO) $ex_l_libs
735
736 ######################################################
737 # Don't touch anything below this point
738 ######################################################
739
740 INC=-I\$(SRC_D)${o}include -I\$(INCL_D) -I\$(SRC_D)${o}crypto${o}include
741 APP_CFLAGS=\$(INC) \$(CFLAG) \$(APP_CFLAG)
742 LIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG)
743 SHLIB_CFLAGS=\$(INC) \$(CFLAG) \$(LIB_CFLAG) \$(SHLIB_CFLAG)
744 LIBS_DEP=$libs_dep
745
746 #############################################
747 EOF
748
749 $rules=<<"EOF";
750 all: banner \$(TMP_D) \$(BIN_D) \$(TEST_D) \$(LIB_D) headers \$(FIPS_SHA1_EXE) $build_targets
751
752 banner:
753 $banner
754
755 \$(TMP_D):
756         \$(MKDIR) \"\$(TMP_D)\"
757 # NB: uncomment out these lines if BIN_D, TEST_D and LIB_D are different
758 #\$(BIN_D):
759 #       \$(MKDIR) \$(BIN_D)
760 #
761 #\$(TEST_D):
762 #       \$(MKDIR) \$(TEST_D)
763
764 \$(LIB_D):
765         \$(MKDIR) \"\$(LIB_D)\"
766
767 # This needs to be invoked once, when the makefile is first constructed, or
768 # after cleaning.
769 init: \$(TMP_D) \$(LIB_D) \$(BIN_D) \$(TEST_D) headers
770
771 headers: \$(HEADER)
772
773 lib: \$(LIBS_DEP) \$(E_SHLIB)
774
775 exe: apps tools testapps
776 apps: \$(BIN_D)$o\$(E_EXE)$exep \$(BIN_D)${o}CA.pl
777 testapps: \$(T_EXE)
778 tools: \$(BIN_D)${o}c_rehash
779
780 install: all
781         \$(MKDIR) \"\$(INSTALLTOP)\"
782         \$(MKDIR) \"\$(INSTALLTOP)${o}bin\"
783         \$(MKDIR) \"\$(INSTALLTOP)${o}include\"
784         \$(MKDIR) \"\$(INSTALLTOP)${o}include${o}openssl\"
785         \$(MKDIR) \"\$(INSTALLTOP)${o}lib\"
786         \$(MKDIR) \"\$(INSTALLTOP)${o}lib${o}engines\"
787 $extra_install
788
789 clean:
790         \$(RM) \$(TMP_D)$o*.*
791
792 vclean:
793         \$(RM) \$(TMP_D)$o*.*
794         \$(RM) \$(OUT_D)$o*.*
795
796 reallyclean:
797         \$(RM) -rf \$(TMP_D)
798         \$(RM) -rf \$(BIN_D)
799         \$(RM) -rf \$(TEST_D)
800         \$(RM) -rf \$(LIB_D)
801
802 EOF
803
804 $rules .= &do_rehash_rule("rehash.time", "apps tools");
805 $rules .= &do_test_rule("test", "rehash.time", "run_tests.pl");
806
807 $rules .= <<"EOF";
808 crypto${o}buildinf.h : MINFO
809         \$(PERL) util${o}mkbuildinf.pl "\$(CC) \$(CFLAG_Q)" "\$(PLATFORM)" > crypto${o}buildinf.h
810 $(OBJ_D)${o}cversion${obj} : crypto${o}buildinf.h
811 EOF
812
813 # Strip off trailing ' '
814 foreach (keys %lib_obj) { $lib_obj{$_}=&clean_up_ws($lib_obj{$_}); }
815 $test=&clean_up_ws($test);
816 $e_exe=&clean_up_ws($e_exe);
817 $header=&clean_up_ws($header);
818
819 $defs.=&do_defs("HEADER",$header,"\$(INCL_D)","");
820 $rules.=&do_copy_rule("\$(INCL_D)",$header,"");
821
822 $defs.=&do_defs("T_OBJ",$test,"\$(OBJ_D)",$obj);
823 $rules.=&do_compile_rule("\$(OBJ_D)",$test,"\$(APP_CFLAGS)");
824
825 $defs.=&do_defs("E_OBJ",$e_exe,"\$(OBJ_D)",$obj);
826 $rules.=&do_compile_rule("\$(OBJ_D)",$e_exe,'-DMONOLITH $(APP_CFLAGS)');
827
828 # Special case rules for fips_start and fips_end fips_premain_dso
829
830 if ($fips)
831         {
832         if ($fipscanisterbuild)
833                 {
834                 $rules.=&cc_compile_target("\$(OBJ_D)${o}fips_start$obj",
835                         "fips${o}fips_canister.c",
836                         "-DFIPS_START \$(SHLIB_CFLAGS)");
837                 $rules.=&cc_compile_target("\$(OBJ_D)${o}fips_end$obj",
838                         "fips${o}fips_canister.c", "\$(SHLIB_CFLAGS)");
839                 }
840         $rules.=&cc_compile_target("\$(OBJ_D)${o}fips_standalone_sha1$obj",
841                 "fips${o}sha${o}fips_standalone_sha1.c",
842                 "\$(APP_CFLAGS)");
843         $rules.=&cc_compile_target("\$(OBJ_D)${o}\$(E_PREMAIN_DSO)$obj",
844                 "fips${o}fips_premain.c",
845                 "-DFINGERPRINT_PREMAIN_DSO_LOAD \$(APP_CFLAGS)");
846         }
847
848 sub fix_asm
849         {
850         my($asm, $dir) = @_;
851
852         $asm = " $asm";
853         $asm =~ s/\s+/ $dir\//g;
854         $asm =~ s/\.o//g;
855         $asm =~ s/^ //;
856
857         return $asm . ' ';
858         }
859
860 if ($orig_platform eq 'copy') {
861         $lib_obj{CRYPTO} .= fix_asm($mf_md5_asm, 'crypto/md5');
862         $lib_obj{CRYPTO} .= fix_asm($mf_bn_asm, 'crypto/bn');
863         # cpuid is included by the crypto dir
864         #$lib_obj{CRYPTO} .= fix_asm($mf_cpuid_asm, 'crypto');
865         # AES asm files end up included by the aes dir itself
866         #$lib_obj{CRYPTO} .= fix_asm($mf_aes_asm, 'crypto/aes');
867         $lib_obj{CRYPTO} .= fix_asm($mf_sha_asm, 'crypto/sha');
868         $lib_obj{CRYPTO} .= fix_asm($mf_engines_asm, 'engines');
869         $lib_obj{CRYPTO} .= fix_asm($mf_rc4_asm, 'crypto/rc4');
870         $lib_obj{CRYPTO} .= fix_asm($mf_modes_asm, 'crypto/modes');
871         $lib_obj{CRYPTO} .= fix_asm($mf_ec_asm, 'crypto/ec');
872 }
873
874 foreach (values %lib_nam)
875         {
876         $lib_obj=$lib_obj{$_};
877         local($slib)=$shlib;
878
879         $defs.=&do_defs(${_}."OBJ",$lib_obj,"\$(OBJ_D)",$obj);
880         $lib=($slib)?" \$(SHLIB_CFLAGS)".$shlib_ex_cflags{$_}:" \$(LIB_CFLAGS)";
881         $rules.=&do_compile_rule("\$(OBJ_D)",$lib_obj{$_},$lib);
882         }
883
884 # hack to add version info on MSVC
885 if (($platform eq "VC-WIN32") || ($platform eq "VC-WIN64A")
886         || ($platform eq "VC-WIN64I") || ($platform eq "VC-NT")) {
887     $rules.= <<"EOF";
888 \$(OBJ_D)\\\$(CRYPTO).res: ms\\version32.rc
889         \$(RSC) /fo"\$(OBJ_D)\\\$(CRYPTO).res" /d CRYPTO ms\\version32.rc
890
891 \$(OBJ_D)\\\$(SSL).res: ms\\version32.rc
892         \$(RSC) /fo"\$(OBJ_D)\\\$(SSL).res" /d SSL ms\\version32.rc
893
894 EOF
895 }
896
897 $defs.=&do_defs("T_EXE",$test,"\$(TEST_D)",$exep);
898 foreach (split(/\s+/,$test))
899         {
900         my $t_libs;
901         $t=&bname($_);
902         my $ltype;
903         # Check to see if test program is FIPS
904         if ($fips && /fips/)
905                 {
906                 # If fips perform static link to 
907                 # $(O_FIPSCANISTER)
908                 $t_libs = "\$(O_FIPSCANISTER)";
909                 $ltype = 2;
910                 }
911         else
912                 {
913                 $t_libs = "\$(L_LIBS)";
914                 $ltype = 0;
915                 }
916
917         $tt="\$(OBJ_D)${o}$t${obj}";
918         $rules.=&do_link_rule("\$(TEST_D)$o$t$exep",$tt,"\$(LIBS_DEP)","$t_libs \$(EX_LIBS)", $ltype);
919         }
920
921 $defs.=&do_defs("E_SHLIB",$engines . $otherlibs,"\$(ENG_D)",$shlibp);
922
923 foreach (split(/\s+/,$engines))
924         {
925         my $engine = $_;
926         my @objs   = grep {/e_$engine/} @engines_obj;
927         $rules.=&do_compile_rule("\$(OBJ_D)",join(" ",@objs),$lib);
928         map {$_=~s/.*\/([^\/]+)$/\$(OBJ_D)${o}$1$obj/} @objs;
929         $rules.= &do_lib_rule(join(" ",@objs),"\$(ENG_D)$o$engine$shlibp","",$shlib,"");
930         }
931
932
933
934 $rules.= &do_lib_rule("\$(SSLOBJ)","\$(O_SSL)",$ssl,$shlib,"\$(SO_SSL)");
935 #$rules.= &do_lib_rule("\$(CRYPTOOBJ)","\$(O_CRYPTO)",$crypto,$shlib,"\$(SO_CRYPTO)");
936
937 foreach (split(" ",$otherlibs))
938         {
939         my $uc = $_;
940         $uc =~ tr /a-z/A-Z/;    
941         $rules.= &do_lib_rule("\$(${uc}OBJ)","\$(ENG_D)$o$_$shlibp", "", $shlib, "");
942
943         }
944
945 if ($fips)
946         {
947         if ($shlib)
948                 {
949                 $rules.= &do_lib_rule("\$(CRYPTOOBJ) \$(O_FIPSCANISTER)",
950                                 "\$(O_CRYPTO)", "$crypto",
951                                 $shlib, "\$(SO_CRYPTO)", "\$(BASEADDR)");
952                 }
953         else
954                 {
955                 $rules.= &do_lib_rule("\$(CRYPTOOBJ)",
956                         "\$(O_CRYPTO)",$crypto,$shlib,"\$(SO_CRYPTO)", "");
957                 $rules.= &do_lib_rule("\$(CRYPTOOBJ) \$(O_FIPSCANISTER)",
958                         "\$(LIB_D)$o$crypto_compat",$crypto,$shlib,"\$(SO_CRYPTO)", "");
959                 }
960         }
961         else
962         {
963         $rules.= &do_lib_rule("\$(CRYPTOOBJ)","\$(O_CRYPTO)",$crypto,$shlib,
964                                                         "\$(SO_CRYPTO)");
965         }
966
967 if ($fips)
968         {
969         if ($fipscanisterbuild)
970                 {
971                 $rules.= &do_rlink_rule("\$(O_FIPSCANISTER)",
972                                         "\$(OBJ_D)${o}fips_start$obj",
973                                         "\$(FIPSOBJ)",
974                                         "\$(OBJ_D)${o}fips_end$obj",
975                                         "\$(FIPS_SHA1_EXE)", "");
976                 # FIXME
977                 $rules.=&do_link_rule("\$(FIPS_SHA1_EXE)",
978                                         "\$(OBJ_D)${o}fips_standalone_sha1$obj \$(OBJ_D)${o}sha1dgst$obj $sha1_asm_obj",
979                                         "","\$(EX_LIBS)", 1);
980                 }
981         else
982                 {
983                 $rules.=&do_link_rule("\$(FIPS_SHA1_EXE)",
984                                         "\$(OBJ_D)${o}fips_standalone_sha1$obj \$(O_FIPSCANISTER)",
985                                         "","", 1);
986
987                 }
988         $rules.=&do_link_rule("\$(PREMAIN_DSO_EXE)","\$(OBJ_D)${o}\$(E_PREMAIN_DSO)$obj \$(CRYPTOOBJ) \$(O_FIPSCANISTER)","","\$(EX_LIBS)", 1);
989         
990         }
991
992 $rules.=&do_link_rule("\$(BIN_D)$o\$(E_EXE)$exep","\$(E_OBJ)","\$(LIBS_DEP)","\$(L_LIBS) \$(EX_LIBS)", ($fips && !$shlib) ? 2 : 0);
993
994 $rules.=&do_dofile_rule("\$(BIN_D)","c_rehash","tools/c_rehash.in");
995 $rules.=&do_dofile_rule("\$(BIN_D)","CA.pl","apps/CA.pl.in");
996
997 print $defs;
998
999 if ($platform eq "linux-elf") {
1000     print <<"EOF";
1001 # Generate perlasm output files
1002 %.cpp:
1003         (cd \$(\@D)/..; PERL=perl make -f Makefile asm/\$(\@F))
1004 EOF
1005 }
1006 print "###################################################################\n";
1007 print $rules;
1008
1009 ###############################################
1010 # strip off any trailing .[och] and append the relative directory
1011 # also remembering to do nothing if we are in one of the dropped
1012 # directories
1013 sub var_add
1014         {
1015         local($dir,$val,$keepext)=@_;
1016         local(@a,$_,$ret);
1017
1018         return("") if $no_engine && $dir =~ /\/engine/;
1019         return("") if $no_hw   && $dir =~ /\/hw/;
1020         return("") if $no_idea && $dir =~ /\/idea/;
1021         return("") if $no_aes  && $dir =~ /\/aes/;
1022         return("") if $no_camellia  && $dir =~ /\/camellia/;
1023         return("") if $no_seed && $dir =~ /\/seed/;
1024         return("") if $no_rc2  && $dir =~ /\/rc2/;
1025         return("") if $no_rc4  && $dir =~ /\/rc4/;
1026         return("") if $no_rc5  && $dir =~ /\/rc5/;
1027         return("") if $no_rsa  && $dir =~ /\/rsa/;
1028         return("") if $no_rsa  && $dir =~ /^rsaref/;
1029         return("") if $no_dsa  && $dir =~ /\/dsa/;
1030         return("") if $no_dh   && $dir =~ /\/dh/;
1031         return("") if $no_ec   && $dir =~ /\/ec/;
1032         return("") if $no_cms  && $dir =~ /\/cms/;
1033         return("") if !$fips   && $dir =~ /^fips/;
1034         if ($no_des && $dir =~ /\/des/)
1035                 {
1036                 if ($val =~ /read_pwd/)
1037                         { return("$dir/read_pwd "); }
1038                 else
1039                         { return(""); }
1040                 }
1041         return("") if $no_mdc2 && $dir =~ /\/mdc2/;
1042         return("") if $no_sock && $dir =~ /\/proxy/;
1043         return("") if $no_bf   && $dir =~ /\/bf/;
1044         return("") if $no_cast && $dir =~ /\/cast/;
1045         return("") if $no_whirlpool && $dir =~ /\/whrlpool/;
1046
1047         $val =~ s/^\s*(.*)\s*$/$1/;
1048         @a=split(/\s+/,$val);
1049         grep(s/\.[och]$//,@a) unless $keepext;
1050
1051         @a=grep(!/^e_.*_3d$/,@a) if $no_des;
1052         @a=grep(!/^e_.*_d$/,@a) if $no_des;
1053         @a=grep(!/^e_.*_ae$/,@a) if $no_idea;
1054         @a=grep(!/^e_.*_i$/,@a) if $no_aes;
1055         @a=grep(!/^e_.*_r2$/,@a) if $no_rc2;
1056         @a=grep(!/^e_.*_r5$/,@a) if $no_rc5;
1057         @a=grep(!/^e_.*_bf$/,@a) if $no_bf;
1058         @a=grep(!/^e_.*_c$/,@a) if $no_cast;
1059         @a=grep(!/^e_rc4$/,@a) if $no_rc4;
1060         @a=grep(!/^e_camellia$/,@a) if $no_camellia;
1061         @a=grep(!/^e_seed$/,@a) if $no_seed;
1062
1063         #@a=grep(!/(^s3_)|(^s23_)/,@a) if $no_ssl3;
1064
1065         @a=grep(!/(_sock$)|(_acpt$)|(_conn$)|(^pxy_)/,@a) if $no_sock;
1066
1067         @a=grep(!/(^md2)|(_md2$)/,@a) if $no_md2;
1068         @a=grep(!/(^md4)|(_md4$)/,@a) if $no_md4;
1069         @a=grep(!/(^md5)|(_md5$)/,@a) if $no_md5;
1070         @a=grep(!/(rmd)|(ripemd)/,@a) if $no_ripemd;
1071         @a=grep(!/(^blake)/,@a) if $no_blake2;
1072
1073         @a=grep(!/(^d2i_r_)|(^i2d_r_)/,@a) if $no_rsa;
1074         @a=grep(!/(^p_open$)/,@a) if $no_rsa;
1075
1076         @a=grep(!/(m_dss$)|(m_dss1$)/,@a) if $no_dsa;
1077         @a=grep(!/(^d2i_s_)|(^i2d_s_)|(_dsap$)/,@a) if $no_dsa;
1078
1079         @a=grep(!/^n_pkey$/,@a) if $no_rsa || $no_rc4;
1080
1081         @a=grep(!/_dhp$/,@a) if $no_dh;
1082
1083         @a=grep(!/_mdc2$/,@a) if $no_mdc2;
1084
1085         @a=grep(!/(srp)/,@a) if $no_srp;
1086
1087         @a=grep(!/^engine$/,@a) if $no_engine;
1088         @a=grep(!/^hw$/,@a) if $no_hw;
1089         @a=grep(!/(^rsa$)|(^genrsa$)/,@a) if $no_rsa;
1090         @a=grep(!/(^dsa$)|(^gendsa$)|(^dsaparam$)/,@a) if $no_dsa;
1091         @a=grep(!/(^dh$)|(^gendh$)/,@a) if $no_dh;
1092
1093         grep($_="$dir/$_",@a);
1094         @a=grep(!/(^|\/)s_/,@a) if $no_sock;
1095         @a=grep(!/(^|\/)bio_sock/,@a) if $no_sock;
1096         $ret=join(' ',@a)." ";
1097         return($ret);
1098         }
1099
1100 # change things so that each 'token' is only separated by one space
1101 sub clean_up_ws
1102         {
1103         local($w)=@_;
1104
1105         $w =~ s/^\s*(.*)\s*$/$1/;
1106         $w =~ s/\s+/ /g;
1107         return($w);
1108         }
1109
1110 sub do_defs
1111         {
1112         local($var,$files,$location,$postfix)=@_;
1113         local($_,$ret,$pf);
1114         local(*OUT,$tmp,$t);
1115
1116         $files =~ s/\//$o/g if $o ne '/';
1117         $ret="$var="; 
1118         $n=1;
1119         $Vars{$var}.="";
1120         foreach (split(/ /,$files))
1121                 {
1122                 $orig=$_;
1123                 $_=&bname($_) unless /^\$/;
1124                 if ($n++ == 2)
1125                         {
1126                         $n=0;
1127                         $ret.="\\\n\t";
1128                         }
1129                 if (($_ =~ /bss_file/) && ($postfix eq ".h"))
1130                         { $pf=".c"; }
1131                 else    { $pf=$postfix; }
1132                 if ($_ =~ /BN_ASM/)     { $t="$_ "; }
1133                 elsif ($_ =~ /BNCO_ASM/){ $t="$_ "; }
1134                 elsif ($_ =~ /AES_ASM/){ $t="$_ "; }
1135                 elsif ($_ =~ /DES_ENC/) { $t="$_ "; }
1136                 elsif ($_ =~ /BF_ENC/)  { $t="$_ "; }
1137                 elsif ($_ =~ /CAST_ENC/){ $t="$_ "; }
1138                 elsif ($_ =~ /RC4_ENC/) { $t="$_ "; }
1139                 elsif ($_ =~ /RC5_ENC/) { $t="$_ "; }
1140                 elsif ($_ =~ /MD5_ASM/) { $t="$_ "; }
1141                 elsif ($_ =~ /SHA1_ASM/){ $t="$_ "; }
1142                 elsif ($_ =~ /RMD160_ASM/){ $t="$_ "; }
1143                 elsif ($_ =~ /WHIRLPOOL_ASM/){ $t="$_ "; }
1144                 elsif ($_ =~ /CPUID_ASM/){ $t="$_ "; }
1145                 else    { $t="$location${o}$_$pf "; }
1146
1147                 $Vars{$var}.="$t ";
1148                 $ret.=$t;
1149                 }
1150         # hack to add version info on MSVC
1151         if ($shlib && (($platform eq "VC-WIN32") || ($platfrom eq "VC-WIN64I") || ($platform eq "VC-WIN64A") || ($platform eq "VC-NT")))
1152                 {
1153                 if ($var eq "CRYPTOOBJ")
1154                         { $ret.="\$(OBJ_D)\\\$(CRYPTO).res "; }
1155                 elsif ($var eq "SSLOBJ")
1156                         { $ret.="\$(OBJ_D)\\\$(SSL).res "; }
1157                 }
1158         chomp($ret);            # Does this actually do something? /RL
1159         $ret.="\n\n";
1160         return($ret);
1161         }
1162
1163 # return the name with the leading path removed
1164 sub bname
1165         {
1166         local($ret)=@_;
1167         $ret =~ s/^.*[\\\/]([^\\\/]+)$/$1/;
1168         return($ret);
1169         }
1170
1171 # return the leading path
1172 sub dname
1173         {
1174         my $ret=shift;
1175         $ret =~ s/(^.*)[\\\/][^\\\/]+$/$1/;
1176         return($ret);
1177         }
1178
1179 ##############################################################
1180 # do a rule for each file that says 'compile' to new direcory
1181 # compile the files in '$files' into $to
1182 sub do_compile_rule
1183         {
1184         local($to,$files,$ex)=@_;
1185         local($ret,$_,$n,$d,$s);
1186
1187         $files =~ s/\//$o/g if $o ne '/';
1188         foreach (split(/\s+/,$files))
1189                 {
1190                 $n=&bname($_);
1191                 $d=&dname($_);
1192                 if (-f "${_}.c")
1193                         {
1194                         $ret.=&cc_compile_target("$to${o}$n$obj","${_}.c",$ex)
1195                         }
1196                 elsif (-f ($s="${d}${o}asm${o}${n}.pl") or
1197                        ($s=~s/sha256/sha512/ and -f $s) or
1198                        -f ($s="${d}${o}${n}.pl"))
1199                         {
1200                         $ret.=&perlasm_compile_target("$to${o}$n$obj",$s,$n);
1201                         }
1202                 elsif (-f ($s="${d}${o}asm${o}${n}.S") or
1203                        -f ($s="${d}${o}${n}.S"))
1204                         {
1205                         $ret.=&Sasm_compile_target("$to${o}$n$obj",$s,$n);
1206                         }
1207                 elsif (defined &special_compile_target and
1208                        ($s=special_compile_target($_)))
1209                         {
1210                         $ret.=$s;
1211                         }
1212                 else    { die "no rule for $_"; }
1213                 }
1214         return($ret);
1215         }
1216
1217 ##############################################################
1218 # do a rule for each file that says 'compile' to new direcory
1219 sub perlasm_compile_target
1220         {
1221         my($target,$source,$bname)=@_;
1222
1223         return platform_perlasm_compile_target($target, $source, $bname)
1224             if defined &platform_perlasm_compile_target;
1225
1226         my($ret);
1227         $bname =~ s/(.*)\.[^\.]$/$1/;
1228         $ret ="\$(TMP_D)$o$bname$asm_suffix: $source\n";
1229         $ret.="\t\$(PERL) $source $asmtype \$(CFLAG) \$\@\n";
1230         if ($fipscanisteronly)
1231                 {
1232                 $ret .= "\t\$(PERL) util$o.pl . \$@ norunasm \$(CFLAG)\n";
1233                 }
1234         $ret .= "\n";
1235         $ret.="$target: \$(TMP_D)$o$bname$asm_suffix\n";
1236         $ret.="\t\$(ASM) $afile\$\@ \$(TMP_D)$o$bname$asm_suffix\n\n";
1237         return($ret);
1238         }
1239
1240 sub Sasm_compile_target
1241         {
1242         my($target,$source,$bname)=@_;
1243         my($ret);
1244
1245         $bname =~ s/(.*)\.[^\.]$/$1/;
1246         $ret ="\$(TMP_D)$o$bname.asm: $source\n";
1247         $ret.="\t\$(CC) -E \$(CFLAG) $source >\$\@\n";
1248         $ret.="\t\$(PERL) util\\fipsas.pl . \$@ norunasm \$(CFLAG)\n" if $fipscanisteronly;
1249         $ret.="\n";
1250         $ret.="$target: \$(TMP_D)$o$bname.asm\n";
1251         $ret.="\t\$(ASM) $afile\$\@ \$(TMP_D)$o$bname.asm\n\n";
1252         return($ret);
1253         }
1254
1255 sub cc_compile_target
1256         {
1257         local($target,$source,$ex_flags)=@_;
1258         local($ret);
1259         
1260         $target =~ s/\//$o/g if $o ne "/";
1261         $source =~ s/\//$o/g if $o ne "/";
1262         $ret ="$target: \$(SRC_D)$o$source\n\t";
1263         $ret.="\$(CC)";
1264         $ret.= " -MMD" if $orig_platform eq "copy";
1265         $ret.= " ${ofile}$target $ex_flags -c \$(SRC_D)$o$source\n\n";
1266         $target =~ s/\.o$/.d/;
1267         $ret.=".sinclude \"$target\"\n\n" if $orig_platform eq "copy";
1268         return($ret);
1269         }
1270
1271 ##############################################################
1272 sub do_asm_rule
1273         {
1274         local($target,$src)=@_;
1275         local($ret,@s,@t,$i);
1276
1277         $target =~ s/\//$o/g if $o ne "/";
1278         $src =~ s/\//$o/g if $o ne "/";
1279
1280         @t=split(/\s+/,$target);
1281         @s=split(/\s+/,$src);
1282
1283
1284         for ($i=0; $i<=$#s; $i++)
1285                 {
1286                 my $objfile = $t[$i];
1287                 my $srcfile = $s[$i];
1288
1289                 if ($perl_asm == 1)
1290                         {
1291                         my $plasm = $objfile;
1292                         $plasm =~ s/${obj}/.pl/;
1293                         $ret.="$srcfile: $plasm\n";
1294                         $ret.="\t\$(PERL) $plasm $asmtype \$(CFLAG) $srcfile\n\n";
1295                         }
1296
1297                 $ret.="$objfile: $srcfile\n";
1298                 $ret.="\t\$(ASM) $afile$objfile \$(SRC_D)$o$srcfile\n\n";
1299                 }
1300         return($ret);
1301         }
1302
1303 sub do_shlib_rule
1304         {
1305         local($n,$def)=@_;
1306         local($ret,$nn);
1307         local($t);
1308
1309         ($nn=$n) =~ tr/a-z/A-Z/;
1310         $ret.="$n.dll: \$(${nn}OBJ)\n";
1311         if ($vc && $w32)
1312                 {
1313                 $ret.="\t\$(MKSHLIB) $efile$n.dll $def @<<\n  \$(${nn}OBJ_F)\n<<\n";
1314                 }
1315         $ret.="\n";
1316         return($ret);
1317         }
1318
1319 # do a rule for each file that says 'copy' to new direcory on change
1320 sub do_copy_rule
1321         {
1322         local($to,$files,$p)=@_;
1323         local($ret,$_,$n,$pp);
1324         
1325
1326         $files =~ s/\//$o/g if $o ne '/';
1327         foreach (split(/\s+/,$files))
1328                 {
1329                 $n=&bname($_);
1330                 if ($n =~ /bss_file/)
1331                         { $pp=".c"; }
1332                 else    { $pp=$p; }
1333                 $ret.="$to${o}$n$pp: \$(SRC_D)$o$_$pp\n\t\$(PERL) \$(SRC_D)${o}util${o}copy-if-different.pl \"\$(SRC_D)$o$_$pp\" \"$to${o}$n$pp\"\n\n";
1334                 }
1335         return($ret);
1336         }
1337
1338 sub do_dofile_rule
1339         {
1340         (my $to, my $file, my $tmpl) = @_;
1341
1342         $file =~ s|/|$o|g if $o ne '/';
1343         return <<"EOF";
1344 $to${o}$file: $tmpl
1345         \$(PERL) "-I." "-Mconfigdata" util/dofile.pl "$tmpl" > "$to${o}$file.new"
1346         \$(MV) "$to${o}$file.new" "$to${o}$file"
1347 EOF
1348         }
1349
1350 # Options picked up from the OPTIONS line in the top level Makefile
1351 # generated by Configure.
1352
1353 sub read_options
1354         {
1355         # Many options are handled in a similar way. In particular
1356         # no-xxx sets zero or more scalars to 1.
1357         # Process these using the %valid_options hash containing the option
1358         # name and reference to the scalars to set. In some cases the option
1359         # needs no special handling and can be ignored: this is done by
1360         # setting the value to 0.
1361
1362         my %valid_options = (
1363                 "no-rc2" => \$no_rc2,
1364                 "no-rc4" => \$no_rc4,
1365                 "no-rc5" => \$no_rc5,
1366                 "no-idea" => \$no_idea,
1367                 "no-aes" => \$no_aes,
1368                 "no-camellia" => \$no_camellia,
1369                 "no-seed" => \$no_seed,
1370                 "no-des" => \$no_des,
1371                 "no-bf" => \$no_bf,
1372                 "no-cast" => \$no_cast,
1373                 "no-md2" => \$no_md2,
1374                 "no-md4" => \$no_md4,
1375                 "no-md5" => \$no_md5,
1376                 "no-rmd160" => \$no_ripemd,
1377                 "no-blake2" => \$no_blake2,             
1378                 "no-mdc2" => \$no_mdc2,
1379                 "no-whirlpool" => \$no_whirlpool,
1380                 "no-patents" => 
1381                         [\$no_rc2, \$no_rc4, \$no_rc5, \$no_idea, \$no_rsa],
1382                 "no-rsa" => \$no_rsa,
1383                 "no-dsa" => \$no_dsa,
1384                 "no-dh" => \$no_dh,
1385                 "no-asm" => \$no_asm,
1386                 "nasm" => \$nasm,
1387                 "nw-nasm" => \$nw_nasm,
1388                 "nw-mwasm" => \$nw_mwasm,
1389                 "gaswin" => \$gaswin,
1390                 "no-ssl3" => \$no_ssl3,
1391                 "no-ssl3-method" => 0,
1392                 "no-weak-ssl-ciphers" => 0,
1393                 "no-srp" => \$no_srp,
1394                 "no-cms" => \$no_cms,
1395                 "no-ec2m" => \$no_ec2m,
1396                 "no-ec_nistp_64_gcc_128" => 0,
1397                 "no-err" => \$no_err,
1398                 "no-sock" => \$no_sock,
1399                 "no-ec" => \$no_ec,
1400                 "no-gost" => \$no_gost,
1401                 "no-engine" => \$no_engine,
1402                 "no-egd" => 0,
1403                 "no-heartbeats" => 0,
1404                 "no-hw" => \$no_hw,
1405                 "no-async" => \$no_async,
1406                 "no-autoalginit" => \$no_autoalginit,
1407                 "no-autoerrinit" => \$no_autoerrinit,
1408                 "just-ssl" =>
1409                         [\$no_rc2, \$no_idea, \$no_des, \$no_bf, \$no_cast,
1410                           \$no_md2, \$no_mdc2, \$no_dsa, \$no_dh,
1411                           \$no_err, \$no_ripemd, \$no_rc5,
1412                           \$no_aes, \$no_camellia, \$no_seed, \$no_srp],
1413                 "rsaref" => 0,
1414                 "gcc" => \$gcc,
1415                 "debug" => \$debug,
1416                 "--debug" => \$debug,
1417                 "--classic" => 0,
1418                 "profile" => \$profile,
1419                 "shlib" => \$shlib,
1420                 "dll" => \$shlib,
1421                 "shared" => 0,
1422                 "no-sctp" => 0,
1423                 "no-srtp" => 0,
1424                 "no-gmp" => 0,
1425                 "no-rfc3779" => 0,
1426                 "no-montasm" => 0,
1427                 "no-shared" => 0,
1428                 "no-store" => 0,
1429                 "no-zlib" => 0,
1430                 "no-zlib-dynamic" => 0,
1431                 "no-ssl-trace" => 0,
1432                 "no-unit-test" => 0,
1433                 "no-deprecated" => 0,
1434                 "no-ocb" => 0,
1435                 "no-crypto-mdebug" => 0,
1436                 "no-crypto-mdebug-backtrace" => 0,
1437                 "fips" => \$fips,
1438                 "fipscanisterbuild" => [\$fips, \$fipscanisterbuild],
1439                 "fipscanisteronly" => [\$fips, \$fipscanisterbuild, \$fipscanisteronly],
1440                 "fipscheck" => [\$fips, \$fipscanisterbuild, \$fipscanisteronly],
1441                 );
1442
1443         if (exists $valid_options{$_})
1444                 {
1445                 my $r = $valid_options{$_};
1446                 if ( ref $r eq "SCALAR")
1447                         { $$r = 1;}
1448                 elsif ( ref $r eq "ARRAY")
1449                         {
1450                         my $r2;
1451                         foreach $r2 (@$r)
1452                                 {
1453                                 $$r2 = 1;
1454                                 }
1455                         }
1456                 }
1457         elsif (/^no-comp$/) { $xcflags = "-DOPENSSL_NO_COMP $xcflags"; }
1458         elsif (/^enable-zlib$/) { $zlib_opt = 1 if $zlib_opt == 0 }
1459         elsif (/^enable-zlib-dynamic$/)
1460                 {
1461                 $zlib_opt = 2;
1462                 }
1463         elsif (/^no-static-engine/ or /^enable-dynamic-engine/)
1464                 {
1465                 $no_static_engine = 1;
1466                 }
1467         elsif (/^no-dynamic-engine/ or /^enable-static-engine/)
1468                 {
1469                 $no_static_engine = 0;
1470                 }
1471         # There are also enable-xxx options which correspond to
1472         # the no-xxx. Since the scalars are enabled by default
1473         # these can be ignored.
1474         elsif (/^enable-/)
1475                 {
1476                 my $t = $_;
1477                 $t =~ s/^enable/no/;
1478                 if (exists $valid_options{$t})
1479                         {return 1;}
1480                 return 0;
1481                 }
1482         elsif (/^([^=]*)=(.*)$/){ $VARS{$1}=$2; }
1483         elsif (/^-[lL].*$/)     { $l_flags.="$_ "; }
1484         elsif ((!/^-help/) && (!/^-h/) && (!/^-\?/) && /^-.*$/)
1485                 { $c_flags.="$_ "; }
1486         else { return(0); }
1487         return(1);
1488         }
1489
1490 sub fipslib_error
1491         {
1492         print STDERR "***FIPS module directory sanity check failed***\n";
1493         print STDERR "FIPS module build failed, or was deleted\n";
1494         print STDERR "Please rebuild FIPS module.\n"; 
1495         exit 1;
1496         }
1497
1498 sub fips_check_files
1499         {
1500         my $dir = shift @_;
1501         my $ret = 1;
1502         if (!-d $dir)
1503                 {
1504                 print STDERR "FIPS module directory $dir does not exist\n";
1505                 fipslib_error();
1506                 }
1507         foreach (@_)
1508                 {
1509                 if (!-f "$dir${o}$_")
1510                         {
1511                         print STDERR "FIPS module file $_ does not exist!\n";
1512                         $ret = 0;
1513                         }
1514                 }
1515         fipslib_error() if ($ret == 0);
1516         }