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