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