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