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