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