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