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