VC-32.pl 12.5 KB
Newer Older
1
#!/usr/local/bin/perl
2 3
# VC-32.pl - unified script for Microsoft Visual C++, covering Win32,
# Win64 and WinCE [follow $FLAVOR variable to trace the differences].
4 5 6 7 8
#

$ssl=	"ssleay32";
$crypto="libeay32";

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
if ($fips && !$shlib)
	{
	$crypto="libeayfips32";
	$crypto_compat = "libeaycompat32.lib";
	}
else
	{
	$crypto="libeay32";
	}

if ($fipscanisterbuild)
	{
	$fips_canister_path = "\$(LIB_D)\\fipscanister.lib";
	}

24
$o='\\';
D
Dr. Stephen Henson 已提交
25
$cp='$(PERL) util/copy.pl';
26
$cp2='$(PERL) util/copy.pl -stripcr';
D
Dr. Stephen Henson 已提交
27
$mkdir='$(PERL) util/mkdir-p.pl';
28
$rm='del /Q';
29

D
Dr. Stephen Henson 已提交
30 31
$zlib_lib="zlib1.lib";

32 33 34 35
# Santize -L options for ms link
$l_flags =~ s/-L("\[^"]+")/\/libpath:$1/g;
$l_flags =~ s/-L(\S+)/\/libpath:$1/g;

36 37
# C compiler stuff
$cc='cl';
38 39 40 41 42 43 44 45 46 47
if ($FLAVOR =~ /WIN64/)
    {
    # Note that we currently don't have /WX on Win64! There is a lot of
    # warnings, but only of two types:
    #
    # C4344: conversion from '__int64' to 'int/long', possible loss of data
    # C4267: conversion from 'size_t' to 'int/long', possible loss of data
    #
    # Amount of latter type is minimized by aliasing strlen to function of
    # own desing and limiting its return value to 2GB-1 (see e_os.h). As
48
    # per 0.9.8 release remaining warnings were explicitly examined and
49 50
    # considered safe to ignore.
    # 
51
    $base_cflags= " $mf_cflag";
A
Andy Polyakov 已提交
52 53
    my $f = $shlib?' /MD':' /MT';
    $lib_cflag='/Zl' if (!$shlib);	# remove /DEFAULTLIBs from static lib
54 55
    $opt_cflags=$f.' /Ox';
    $dbg_cflags=$f.'d /Od -DDEBUG -D_DEBUG';
56
    $lflags="/nologo /subsystem:console /opt:ref";
57 58 59 60 61 62 63 64

    *::perlasm_compile_target = sub {
	my ($target,$source,$bname)=@_;
	my $ret;

	$bname =~ s/(.*)\.[^\.]$/$1/;
	$ret=<<___;
\$(TMP_D)$o$bname.asm: $source
65
	set ASM=\$(ASM)
66
	\$(PERL) $source \$\@
67 68 69 70
___
	$ret .= "\t\$(PERL) util\\fipsas.pl . \$@ norunasm \$(CFLAG)\n" if $fipscanisterbuild;

	$ret.=<<___;
71 72 73 74 75 76

$target: \$(TMP_D)$o$bname.asm
	\$(ASM) $afile\$\@ \$(TMP_D)$o$bname.asm

___
	}
77
    }
78 79
elsif ($FLAVOR =~ /CE/)
    {
80 81 82 83 84
    # sanity check
    die '%OSVERSION% is not defined'	if (!defined($ENV{'OSVERSION'}));
    die '%PLATFORM% is not defined'	if (!defined($ENV{'PLATFORM'}));
    die '%TARGETCPU% is not defined'	if (!defined($ENV{'TARGETCPU'}));

85 86 87
    #
    # Idea behind this is to mimic flags set by eVC++ IDE...
    #
88 89
    $wcevers = $ENV{'OSVERSION'};			# WCENNN
    die '%OSVERSION% value is insane'	if ($wcevers !~ /^WCE([1-9])([0-9]{2})$/);
90 91
    $wcecdefs = "-D_WIN32_WCE=$1$2 -DUNDER_CE=$1$2";	# -D_WIN32_WCE=NNN
    $wcelflag = "/subsystem:windowsce,$1.$2";		# ...,N.NN
92

93 94
    $wceplatf =  $ENV{'PLATFORM'};
    $wceplatf =~ tr/a-z0-9 /A-Z0-9_/d;
95
    $wcecdefs .= " -DWCE_PLATFORM_$wceplatf";
96

97 98
    $wcetgt = $ENV{'TARGETCPU'};	# just shorter name...
    SWITCH: for($wcetgt) {
99 100
	/^X86/		&& do {	$wcecdefs.=" -Dx86 -D_X86_ -D_i386_ -Di_386_";
				$wcelflag.=" /machine:IX86";	last; };
101
	/^ARMV4[IT]/	&& do { $wcecdefs.=" -DARM -D_ARM_ -D$wcetgt";
102 103 104 105
				$wcecdefs.=" -DTHUMB -D_THUMB_" if($wcetgt=~/T$/);
				$wcecdefs.=" -QRarch4T -QRinterwork-return";
				$wcelflag.=" /machine:THUMB";	last; };
	/^ARM/		&& do {	$wcecdefs.=" -DARM -D_ARM_ -D$wcetgt";
A
Andy Polyakov 已提交
106
				$wcelflag.=" /machine:ARM";	last; };
107 108 109 110 111 112 113 114 115 116
	/^MIPSIV/	&& do {	$wcecdefs.=" -DMIPS -D_MIPS_ -DR4000 -D$wcetgt";
				$wcecdefs.=" -D_MIPS64 -QMmips4 -QMn32";
				$wcelflag.=" /machine:MIPSFPU";	last; };
	/^MIPS16/	&& do {	$wcecdefs.=" -DMIPS -D_MIPS_ -DR4000 -D$wcetgt";
				$wcecdefs.=" -DMIPSII -QMmips16";
				$wcelflag.=" /machine:MIPS16";	last; };
	/^MIPSII/	&& do {	$wcecdefs.=" -DMIPS -D_MIPS_ -DR4000 -D$wcetgt";
				$wcecdefs.=" -QMmips2";
				$wcelflag.=" /machine:MIPS";	last; };
	/^R4[0-9]{3}/	&& do {	$wcecdefs.=" -DMIPS -D_MIPS_ -DR4000";
A
Andy Polyakov 已提交
117
				$wcelflag.=" /machine:MIPS";	last; };
118
	/^SH[0-9]/	&& do {	$wcecdefs.=" -D$wcetgt -D_$wcetgt_ -DSHx";
119
				$wcecdefs.=" -Qsh4" if ($wcetgt =~ /^SH4/);
A
Andy Polyakov 已提交
120
				$wcelflag.=" /machine:$wcetgt";	last; };
121
	{ $wcecdefs.=" -D$wcetgt -D_$wcetgt_";
A
Andy Polyakov 已提交
122
	  $wcelflag.=" /machine:$wcetgt";			last; };
123
    }
124 125

    $cc='$(CC)';
126
    $base_cflags=' /W3 /WX /GF /Gy /nologo -DUNICODE -D_UNICODE -DOPENSSL_SYSNAME_WINCE -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DDSO_WIN32 -DNO_CHMOD -DOPENSSL_SMALL_FOOTPRINT';
127
    $base_cflags.=" $wcecdefs";
A
Andy Polyakov 已提交
128 129
    $base_cflags.=' -I$(WCECOMPAT)/include'		if (defined($ENV{'WCECOMPAT'}));
    $base_cflags.=' -I$(PORTSDK_LIBPATH)/../../include'	if (defined($ENV{'PORTSDK_LIBPATH'}));
130 131
    $opt_cflags=' /MC /O1i';	# optimize for space, but with intrinsics...
    $dbg_clfags=' /MC /Od -DDEBUG -D_DEBUG';
132
    $lflags="/nologo /opt:ref $wcelflag";
133 134
    }
else	# Win32
135
    {
136
    $base_cflags= " $mf_cflag";
A
Andy Polyakov 已提交
137 138
    my $f = $shlib?' /MD':' /MT';
    $lib_cflag='/Zl' if (!$shlib);	# remove /DEFAULTLIBs from static lib
139 140
    $opt_cflags=$f.' /Ox /O2 /Ob2';
    $dbg_cflags=$f.'d /Od -DDEBUG -D_DEBUG';
A
Andy Polyakov 已提交
141
    $lflags="/nologo /subsystem:console /opt:ref";
142
    }
143
$mlflags='';
144

145 146 147 148
$out_def ="out32";	$out_def.="dll"			if ($shlib);
			$out_def.='_$(TARGETCPU)'	if ($FLAVOR =~ /CE/);
$tmp_def ="tmp32";	$tmp_def.="dll"			if ($shlib);
			$tmp_def.='_$(TARGETCPU)'	if ($FLAVOR =~ /CE/);
149 150
$inc_def="inc32";

151 152
if ($debug)
	{
153
	$cflags=$dbg_cflags.$base_cflags;
154
	}
155 156 157 158
else
	{
	$cflags=$opt_cflags.$base_cflags;
	}
159

160
# generate symbols.pdb unconditionally
161 162
$app_cflag.=" /Zi /Fd\$(TMP_D)/app";
$lib_cflag.=" /Zi /Fd\$(TMP_D)/lib";
163 164
$lflags.=" /debug";

165 166
$lflags.=" /fixed" if $fips;

167
$obj='.obj';
168
$asm_suffix='.asm';
169 170 171 172
$ofile="/Fo";

# EXE linking stuff
$link="link";
D
 
Dr. Stephen Henson 已提交
173
$rsc="rc";
174 175
$efile="/out:";
$exep='.exe';
176 177
if ($no_sock)		{ $ex_libs=''; }
elsif ($FLAVOR =~ /CE/)	{ $ex_libs='winsock.lib'; }
178
else			{ $ex_libs='ws2_32.lib'; }
179 180

if ($FLAVOR =~ /CE/)
181
	{
182 183
	$ex_libs.=' $(WCECOMPAT)/lib/wcecompatex.lib'	if (defined($ENV{'WCECOMPAT'}));
	$ex_libs.=' $(PORTSDK_LIBPATH)/portlib.lib'	if (defined($ENV{'PORTSDK_LIBPATH'}));
184 185
	$ex_libs.=' /nodefaultlib:oldnames.lib coredll.lib corelibc.lib' if ($ENV{'TARGETCPU'} eq "X86");
	}
186 187
else
	{
188
	$ex_libs.=' gdi32.lib advapi32.lib crypt32.lib user32.lib';
189
	$ex_libs.=' bufferoverflowu.lib' if ($FLAVOR =~ /WIN64/ and `cl 2>&1` =~ /14\.00\.4[0-9]{4}\./);
190 191 192
	# WIN32 UNICODE build gets linked with unicows.lib for
	# backward compatibility with Win9x.
	$ex_libs="unicows.lib $ex_libs" if ($FLAVOR =~ /WIN32/ and $cflags =~ /\-DUNICODE/);
193 194
	}

195
# static library stuff
D
Dr. Stephen Henson 已提交
196
$mklib='lib /nologo';
197 198 199 200 201 202 203
$ranlib='';
$plib="";
$libp=".lib";
$shlibp=($shlib)?".dll":".lib";
$lfile='/out:';

$shlib_ex_obj="";
204
$app_ex_obj="setargv.obj" if ($FLAVOR !~ /CE/);
205
if ($FLAVOR =~ /WIN64A/) {
206 207
	if (`nasm -v 2>NUL` =~ /NASM version ([0-9]+\.[0-9]+)/ && $1 >= 2.0) {
		$asm='nasm -f win64 -DNEAR -Ox -g';
208 209
		$afile='-o ';
	} else {
210
		$asm='ml64 /c /Cp /Cx /Zi';
211 212
		$afile='/Fo';
	}
213
} elsif ($FLAVOR =~ /WIN64I/) {
214
	$asm='ias -d debug';
215 216
	$afile="-o ";
} elsif ($nasm) {
217 218
	my $ver=`nasm -v 2>NUL`;
	my $vew=`nasmw -v 2>NUL`;
219
	# pick newest version
220
	$asm=($ver ge $vew?"nasm":"nasmw")." -f win32";
221
	$asmtype="win32n";
222 223
	$afile='-o ';
} else {
224
	$asm='ml /nologo /Cp /coff /c /Cx /Zi';
225
	$afile='/Fo';
226
	$asmtype="win32";
227
}
228

229 230
$bn_asm_obj='';
$bn_asm_src='';
231 232 233 234 235 236 237
$des_enc_obj='';
$des_enc_src='';
$bf_enc_obj='';
$bf_enc_src='';

if (!$no_asm)
	{
238 239 240 241 242 243 244 245 246 247 248
	win32_import_asm($mf_bn_asm, "bn", \$bn_asm_obj, \$bn_asm_src);
	win32_import_asm($mf_aes_asm, "aes", \$aes_asm_obj, \$aes_asm_src);
	win32_import_asm($mf_des_asm, "des", \$des_enc_obj, \$des_enc_src);
	win32_import_asm($mf_bf_asm, "bf", \$bf_enc_obj, \$bf_enc_src);
	win32_import_asm($mf_cast_asm, "cast", \$cast_enc_obj, \$cast_enc_src);
	win32_import_asm($mf_rc4_asm, "rc4", \$rc4_enc_obj, \$rc4_enc_src);
	win32_import_asm($mf_rc5_asm, "rc5", \$rc5_enc_obj, \$rc5_enc_src);
	win32_import_asm($mf_md5_asm, "md5", \$md5_asm_obj, \$md5_asm_src);
	win32_import_asm($mf_sha_asm, "sha", \$sha1_asm_obj, \$sha1_asm_src);
	win32_import_asm($mf_rmd_asm, "ripemd", \$rmd160_asm_obj, \$rmd160_asm_src);
	win32_import_asm($mf_wp_asm, "whrlpool", \$whirlpool_asm_obj, \$whirlpool_asm_src);
249
	win32_import_asm($mf_modes_asm, "modes", \$modes_asm_obj, \$modes_asm_src);
250 251
	win32_import_asm($mf_cpuid_asm, "", \$cpuid_asm_obj, \$cpuid_asm_src);
	$perl_asm = 1;
252 253
	}

254
if ($shlib && $FLAVOR !~ /CE/)
255 256
	{
	$mlflags.=" $lflags /dll";
257
	$lib_cflag.=" -D_WINDLL";
A
Andy Polyakov 已提交
258 259 260 261
	#
	# Engage Applink...
	#
	$app_ex_obj.=" \$(OBJ_D)\\applink.obj /implib:\$(TMP_D)\\junk.lib";
262
	$cflags.=" -DOPENSSL_USE_APPLINK -I.";
A
Andy Polyakov 已提交
263 264 265 266 267 268
	# I'm open for better suggestions than overriding $banner...
	$banner=<<'___';
	@echo Building OpenSSL

$(OBJ_D)\applink.obj:	ms\applink.c
	$(CC) /Fo$(OBJ_D)\applink.obj $(APP_CFLAGS) -c ms\applink.c
269
$(OBJ_D)\uplink.obj:	ms\uplink.c ms\applink.c
A
Andy Polyakov 已提交
270 271 272 273 274 275 276 277
	$(CC) /Fo$(OBJ_D)\uplink.obj $(SHLIB_CFLAGS) -c ms\uplink.c
$(INCO_D)\applink.c:	ms\applink.c
	$(CP) ms\applink.c $(INCO_D)\applink.c

EXHEADER= $(EXHEADER) $(INCO_D)\applink.c

LIBS_DEP=$(LIBS_DEP) $(OBJ_D)\applink.obj
CRYPTOOBJ=$(OBJ_D)\uplink.obj $(CRYPTOOBJ)
278 279 280
___
	$banner.=<<'___' if ($FLAVOR =~ /WIN64/);
CRYPTOOBJ=ms\uptable.obj $(CRYPTOOBJ)
A
Andy Polyakov 已提交
281
___
282
	}
283 284 285
elsif ($shlib && $FLAVOR =~ /CE/)
	{
	$mlflags.=" $lflags /dll";
286
	$lflags.=' /entry:mainCRTstartup' if(defined($ENV{'PORTSDK_LIBPATH'}));
287
	$lib_cflag.=" -D_WINDLL -D_DLL";
288
	}
289 290 291

sub do_lib_rule
	{
292
	my($objs,$target,$name,$shlib,$ign,$base_addr) = @_;
D
Dr. Stephen Henson 已提交
293
	local($ret);
294 295

	$taget =~ s/\//$o/g if $o ne '/';
296 297 298 299 300 301 302 303 304
	my $base_arg;
	if ($base_addr ne "")
		{
		$base_arg= " /base:$base_addr";
		}
	else
		{
		$base_arg = "";
		}
D
Dr. Stephen Henson 已提交
305 306 307 308 309
	if ($name ne "")
		{
		$name =~ tr/a-z/A-Z/;
		$name = "/def:ms/${name}.def";
		}
310

311
#	$target="\$(LIB_D)$o$target";
312
#	$ret.="$target: $objs\n";
313 314 315
	if (!$shlib)
		{
#		$ret.="\t\$(RM) \$(O_$Name)\n";
316
		$ret.="$target: $objs\n";
317
		$ret.="\t\$(MKLIB) $lfile$target @<<\n  $objs\n<<\n";
318 319 320
		}
	else
		{
D
Dr. Stephen Henson 已提交
321
		local($ex)=($target =~ /O_CRYPTO/)?'':' $(L_CRYPTO)';
D
Dr. Stephen Henson 已提交
322
		$ex.=" $zlib_lib" if $zlib_opt == 1 && $target =~ /O_CRYPTO/;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

 		if ($fips && $target =~ /O_CRYPTO/)
			{
			$ret.="$target: $objs \$(PREMAIN_DSO_EXE)";
			$ret.="\n\tSET FIPS_LINK=\$(LINK)\n";
			$ret.="\tSET FIPS_CC=\$(CC)\n";
			$ret.="\tSET FIPS_CC_ARGS=/Fo\$(OBJ_D)${o}fips_premain.obj \$(SHLIB_CFLAGS) -c\n";
			$ret.="\tSET PREMAIN_DSO_EXE=\$(PREMAIN_DSO_EXE)\n";
			$ret.="\tSET FIPS_SHA1_EXE=\$(FIPS_SHA1_EXE)\n";
			$ret.="\tSET FIPS_TARGET=$target\n";
			$ret.="\tSET FIPSLIB_D=\$(FIPSLIB_D)\n";
			$ret.="\t\$(FIPSLINK) \$(MLFLAGS) /map $base_arg $efile$target ";
			$ret.="$name @<<\n  \$(SHLIB_EX_OBJ) $objs \$(EX_LIBS) ";
			$ret.="\$(OBJ_D)${o}fips_premain.obj $ex\n<<\n";
			}
		else
			{
			$ret.="$target: $objs";
			$ret.="\n\t\$(LINK) \$(MLFLAGS) $efile$target $name @<<\n  \$(SHLIB_EX_OBJ) $objs $ex \$(EX_LIBS)\n<<\n";
			}

344
		$ret.="\tIF EXIST \$@.manifest mt -nologo -manifest \$@.manifest -outputresource:\$@;2\n\n";
345 346 347 348 349 350 351
		}
	$ret.="\n";
	return($ret);
	}

sub do_link_rule
	{
352
	my($target,$files,$dep_libs,$libs,$standalone)=@_;
353 354 355 356
	local($ret,$_);
	$file =~ s/\//$o/g if $o ne '/';
	$n=&bname($targer);
	$ret.="$target: $files $dep_libs\n";
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	if ($standalone == 1)
		{
		$ret.="  \$(LINK) \$(LFLAGS) $efile$target @<<\n\t";
		$ret.= "\$(EX_LIBS) " if ($files =~ /O_FIPSCANISTER/ && !$fipscanisterbuild);
		$ret.="$files $libs\n<<\n";
		}
	elsif ($standalone == 2)
		{
		$ret.="\tSET FIPS_LINK=\$(LINK)\n";
		$ret.="\tSET FIPS_CC=\$(CC)\n";
		$ret.="\tSET FIPS_CC_ARGS=/Fo\$(OBJ_D)${o}fips_premain.obj \$(SHLIB_CFLAGS) -c\n";
		$ret.="\tSET PREMAIN_DSO_EXE=\n";
		$ret.="\tSET FIPS_TARGET=$target\n";
		$ret.="\tSET FIPS_SHA1_EXE=\$(FIPS_SHA1_EXE)\n";
		$ret.="\tSET FIPSLIB_D=\$(FIPSLIB_D)\n";
		$ret.="\t\$(FIPSLINK) \$(LFLAGS) /map $efile$target @<<\n";
		$ret.="\t\$(APP_EX_OBJ) $files \$(OBJ_D)${o}fips_premain.obj $libs\n<<\n";
		}
	else
		{
		$ret.="\t\$(LINK) \$(LFLAGS) $efile$target @<<\n";
		$ret.="\t\$(APP_EX_OBJ) $files $libs\n<<\n";
		}
    	$ret.="\tIF EXIST \$@.manifest mt -nologo -manifest \$@.manifest -outputresource:\$@;1\n\n";
	return($ret);
	}

sub do_rlink_rule
	{
	local($target,$rl_start, $rl_mid, $rl_end,$dep_libs,$libs)=@_;
	local($ret,$_);
	my $files = "$rl_start $rl_mid $rl_end";

	$file =~ s/\//$o/g if $o ne '/';
	$n=&bname($targer);
	$ret.="$target: $files $dep_libs \$(FIPS_SHA1_EXE)\n";
	$ret.="\t\$(PERL) ms\\segrenam.pl \$\$a $rl_start\n";
	$ret.="\t\$(PERL) ms\\segrenam.pl \$\$b $rl_mid\n";
	$ret.="\t\$(PERL) ms\\segrenam.pl \$\$c $rl_end\n";
	$ret.="\t\$(MKLIB) $lfile$target @<<\n\t$files\n<<\n";
	$ret.="\t\$(FIPS_SHA1_EXE) $target > ${target}.sha1\n";
	$ret.="\t\$(PERL) util${o}copy.pl -stripcr fips${o}fips_premain.c \$(LIB_D)${o}fips_premain.c\n";
	$ret.="\t\$(CP) fips${o}fips_premain.c.sha1 \$(LIB_D)${o}fips_premain.c.sha1\n";
	$ret.="\n";
401 402 403
	return($ret);
	}

404 405 406 407 408 409 410 411 412 413 414 415 416 417
sub win32_import_asm
	{
	my ($mf_var, $asm_name, $oref, $sref) = @_;
	my $asm_dir;
	if ($asm_name eq "")
		{
		$asm_dir = "crypto\\";
		}
	else
		{
		$asm_dir = "crypto\\$asm_name\\asm\\";
		}

	$$oref = "";
418 419
	$$sref = "";
	$mf_var =~ s/\.o//g;
420 421 422

	foreach (split(/ /, $mf_var))
		{
423 424 425 426 427
		$$sref .= $asm_dir . $_ . ".asm ";
		}
	foreach (split(/ /, $mf_var))
		{
		$$oref .= "\$(TMP_D)\\" . $_ . ".obj ";
428 429
		}
	$$oref =~ s/ $//;
430
	$$sref =~ s/ $//;
431 432 433 434

	}


435
1;