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