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