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