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