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