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