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