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