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