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