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