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