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