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