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