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