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