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