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