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