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