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