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