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