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