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