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