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