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