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