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