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