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