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