Always build library object files with shared library cflags
[openssl.git] / util / pl / VC-32.pl
1 #!/usr/local/bin/perl
2 # VC-32.pl - unified script for Microsoft Visual C++, covering Win32,
3 # Win64 and WinCE [follow $FLAVOR variable to trace the differences].
4 #
5
6 $ssl=   "ssleay32";
7 $crypto="libeay32";
8
9 if ($fips && !$shlib)
10         {
11         $crypto="libeayfips32";
12         $crypto_compat = "libeaycompat32.lib";
13         }
14 else
15         {
16         $crypto="libeay32";
17         }
18
19 if ($fipscanisterbuild)
20         {
21         $fips_canister_path = "\$(LIB_D)\\fipscanister.lib";
22         }
23
24 $o='\\';
25 $cp='$(PERL) util/copy.pl';
26 $cp2='$(PERL) util/copy.pl -stripcr';
27 $mkdir='$(PERL) util/mkdir-p.pl';
28 $rm='del /Q';
29 $mv='move /Y';
30
31 $zlib_lib="zlib1.lib";
32
33 # Santize -L options for ms link
34 $l_flags =~ s/-L("\[^"]+")/\/libpath:$1/g;
35 $l_flags =~ s/-L(\S+)/\/libpath:$1/g;
36
37 # C compiler stuff
38 $cc='cl';
39 if ($FLAVOR =~ /WIN64/)
40     {
41     # Note that we currently don't have /WX on Win64! There is a lot of
42     # warnings, but only of two types:
43     #
44     # C4344: conversion from '__int64' to 'int/long', possible loss of data
45     # C4267: conversion from 'size_t' to 'int/long', possible loss of data
46     #
47     # Amount of latter type is minimized by aliasing strlen to function of
48     # own desing and limiting its return value to 2GB-1 (see e_os.h). As
49     # per 0.9.8 release remaining warnings were explicitly examined and
50     # considered safe to ignore.
51     # 
52     $base_cflags= " $mf_cflag" . ($mf_shared_cflag ? " $mf_shared_cflag" : "");
53     my $f = ($shlib and !$fipscanisterbuild)?' /MD':' /MT';
54     $opt_cflags=$f.' /Ox';
55     $dbg_cflags=$f.'d /Od -DDEBUG -D_DEBUG';
56     $lflags="/nologo /subsystem:console /opt:ref";
57
58     *::perlasm_compile_target = sub {
59         my ($target,$source,$bname)=@_;
60         my $ret;
61
62         $bname =~ s/(.*)\.[^\.]$/$1/;
63         $ret=<<___;
64 \$(TMP_D)$o$bname.asm: $source
65         set ASM=\$(ASM)
66         \$(PERL) $source \$\@
67 ___
68         $ret .= "\t\$(PERL) util\\fipsas.pl . \$@ norunasm \$(CFLAG)\n" if $fipscanisterbuild;
69
70         $ret.=<<___;
71
72 $target: \$(TMP_D)$o$bname.asm
73         \$(ASM) $afile\$\@ \$(TMP_D)$o$bname.asm
74
75 ___
76         }
77     }
78 elsif ($FLAVOR =~ /CE/)
79     {
80     # sanity check
81     die '%OSVERSION% is not defined'    if (!defined($ENV{'OSVERSION'}));
82     die '%PLATFORM% is not defined'     if (!defined($ENV{'PLATFORM'}));
83     die '%TARGETCPU% is not defined'    if (!defined($ENV{'TARGETCPU'}));
84
85     #
86     # Idea behind this is to mimic flags set by eVC++ IDE...
87     #
88     $wcevers = $ENV{'OSVERSION'};                       # WCENNN
89     die '%OSVERSION% value is insane'   if ($wcevers !~ /^WCE([1-9])([0-9]{2})$/);
90     $wcecdefs = "-D_WIN32_WCE=$1$2 -DUNDER_CE=$1$2";    # -D_WIN32_WCE=NNN
91     $wcelflag = "/subsystem:windowsce,$1.$2";           # ...,N.NN
92
93     $wceplatf =  $ENV{'PLATFORM'};
94     $wceplatf =~ tr/a-z0-9 /A-Z0-9_/d;
95     $wcecdefs .= " -DWCE_PLATFORM_$wceplatf";
96
97     $wcetgt = $ENV{'TARGETCPU'};        # just shorter name...
98     SWITCH: for($wcetgt) {
99         /^X86/          && do { $wcecdefs.=" -Dx86 -D_X86_ -D_i386_ -Di_386_";
100                                 $wcelflag.=" /machine:X86";     last; };
101         /^ARMV4[IT]/    && do { $wcecdefs.=" -DARM -D_ARM_ -D$wcetgt";
102                                 $wcecdefs.=" -DTHUMB -D_THUMB_" if($wcetgt=~/T$/);
103                                 $wcecdefs.=" -QRarch4T -QRinterwork-return";
104                                 $wcelflag.=" /machine:THUMB";   last; };
105         /^ARM/          && do { $wcecdefs.=" -DARM -D_ARM_ -D$wcetgt";
106                                 $wcelflag.=" /machine:ARM";     last; };
107         /^MIPSIV/       && do { $wcecdefs.=" -DMIPS -D_MIPS_ -DR4000 -D$wcetgt";
108                                 $wcecdefs.=" -D_MIPS64 -QMmips4 -QMn32";
109                                 $wcelflag.=" /machine:MIPSFPU"; last; };
110         /^MIPS16/       && do { $wcecdefs.=" -DMIPS -D_MIPS_ -DR4000 -D$wcetgt";
111                                 $wcecdefs.=" -DMIPSII -QMmips16";
112                                 $wcelflag.=" /machine:MIPS16";  last; };
113         /^MIPSII/       && do { $wcecdefs.=" -DMIPS -D_MIPS_ -DR4000 -D$wcetgt";
114                                 $wcecdefs.=" -QMmips2";
115                                 $wcelflag.=" /machine:MIPS";    last; };
116         /^R4[0-9]{3}/   && do { $wcecdefs.=" -DMIPS -D_MIPS_ -DR4000";
117                                 $wcelflag.=" /machine:MIPS";    last; };
118         /^SH[0-9]/      && do { $wcecdefs.=" -D$wcetgt -D_$wcetgt_ -DSHx";
119                                 $wcecdefs.=" -Qsh4" if ($wcetgt =~ /^SH4/);
120                                 $wcelflag.=" /machine:$wcetgt"; last; };
121         { $wcecdefs.=" -D$wcetgt -D_$wcetgt_";
122           $wcelflag.=" /machine:$wcetgt";                       last; };
123     }
124
125     $cc=($ENV{CC} or "cl");
126     $base_cflags=' /W3 /WX /GF /Gy /nologo -DUNICODE -D_UNICODE -DOPENSSL_SYS_WINCE -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DDSO_WIN32 -DNO_CHMOD -DOPENSSL_SMALL_FOOTPRINT';
127     $base_cflags.=" $wcecdefs";
128     $base_cflags.=' -I$(WCECOMPAT)/include'             if (defined($ENV{'WCECOMPAT'}));
129     $base_cflags.=' -I$(PORTSDK_LIBPATH)/../../include' if (defined($ENV{'PORTSDK_LIBPATH'}));
130     if (`$cc 2>&1` =~ /Version ([0-9]+)\./ && $1>=14) {
131         $base_cflags.=($shlib and !$fipscanisterbuild)?' /MD':' /MT';
132     } else {
133         $base_cflags.=' /MC';
134     }
135     $opt_cflags=' /O1i';        # optimize for space, but with intrinsics...
136     $dbg_cflags=' /Od -DDEBUG -D_DEBUG';
137     $lflags="/nologo /opt:ref $wcelflag";
138     }
139 else    # Win32
140     {
141     $base_cflags= " $mf_cflag" . ($mf_shared_cflag ? " $mf_shared_cflag" : "");
142     my $f = ($shlib and !$fipscanisterbuild)?' /MD':' /MT';
143     $opt_cflags=$f.' /Ox /O2 /Ob2';
144     $dbg_cflags=$f.'d /Od -DDEBUG -D_DEBUG';
145     $lflags="/nologo /subsystem:console /opt:ref";
146     }
147 $lib_cflag='/Zl' if (!$shlib or $fipscanisterbuild);    # remove /DEFAULTLIBs
148 $mlflags='';
149
150 $out_def ="out32";      $out_def.="dll"                 if ($shlib);
151                         $out_def.='_$(TARGETCPU)'       if ($FLAVOR =~ /CE/);
152 $tmp_def ="tmp32";      $tmp_def.="dll"                 if ($shlib);
153                         $tmp_def.='_$(TARGETCPU)'       if ($FLAVOR =~ /CE/);
154 $inc_def="inc32";
155
156 if ($debug)
157         {
158         $cflags=$dbg_cflags.$base_cflags;
159         }
160 else
161         {
162         $cflags=$opt_cflags.$base_cflags;
163         }
164
165 # generate symbols.pdb unconditionally
166 $app_cflag.=" /Zi /Fd\$(TMP_D)/app";
167 $lib_cflag.=" /Zi /Fd\$(TMP_D)/lib";
168 $lflags.=" /debug";
169
170 $lflags.=" /fixed" if ($fips && $FLAVOR !~ /WIN64/);
171
172 $obj='.obj';
173 $asm_suffix='.asm';
174 $ofile="/Fo";
175
176 # EXE linking stuff
177 $link="link";
178 $rsc="rc";
179 $efile="/out:";
180 $exep='.exe';
181 if ($no_sock)           { $ex_libs=''; }
182 elsif ($FLAVOR =~ /CE/) { $ex_libs='ws2.lib'; }
183 else                    { $ex_libs='ws2_32.lib'; }
184
185 if ($FLAVOR =~ /CE/)
186         {
187         $ex_libs.=' crypt32.lib';       # for e_capi.c
188         if (defined($ENV{WCECOMPAT}))
189                 {
190                 $ex_libs.= ' $(WCECOMPAT)/lib';
191                 if (-f "$ENV{WCECOMPAT}/lib/$ENV{TARGETCPU}/wcecompatex.lib")
192                         {
193                         $ex_libs.='/$(TARGETCPU)/wcecompatex.lib';
194                         }
195                 else
196                         {
197                         $ex_libs.='/wcecompatex.lib';
198                         }
199                 }
200         $ex_libs.=' $(PORTSDK_LIBPATH)/portlib.lib'     if (defined($ENV{'PORTSDK_LIBPATH'}));
201         $ex_libs.=' /nodefaultlib coredll.lib corelibc.lib' if ($ENV{'TARGETCPU'} eq "X86");
202         }
203 else
204         {
205         $ex_libs.=' gdi32.lib advapi32.lib crypt32.lib user32.lib';
206         $ex_libs.=' bufferoverflowu.lib' if ($FLAVOR =~ /WIN64/ and `cl 2>&1` =~ /14\.00\.4[0-9]{4}\./);
207         # WIN32 UNICODE build gets linked with unicows.lib for
208         # backward compatibility with Win9x.
209         $ex_libs="unicows.lib $ex_libs" if ($FLAVOR =~ /WIN32/ and $cflags =~ /\-DUNICODE/);
210         }
211
212 # static library stuff
213 $mklib='lib /nologo';
214 $ranlib='';
215 $plib="";
216 $libp=".lib";
217 $shlibp=($shlib)?".dll":".lib";
218 $lfile='/out:';
219
220 $shlib_ex_obj="";
221 $app_ex_obj="setargv.obj" if ($FLAVOR !~ /CE/);
222 if ($FLAVOR =~ /WIN64A/) {
223         if (`nasm -v 2>NUL` =~ /NASM version ([0-9]+\.[0-9]+)/ && $1 >= 2.0) {
224                 $asm='nasm -f win64 -DNEAR -Ox -g';
225                 $afile='-o ';
226         } else {
227                 $asm='ml64 /c /Cp /Cx /Zi';
228                 $afile='/Fo';
229         }
230 } elsif ($FLAVOR =~ /WIN64I/) {
231         $asm='ias -d debug';
232         $afile="-o ";
233 } elsif ($nasm) {
234         my $ver=`nasm -v 2>NUL`;
235         my $vew=`nasmw -v 2>NUL`;
236         # pick newest version
237         $asm=($ver ge $vew?"nasm":"nasmw")." -f win32";
238         $asmtype="win32n";
239         $afile='-o ';
240 } else {
241         $asm='ml /nologo /Cp /coff /c /Cx /Zi';
242         $afile='/Fo';
243         $asmtype="win32";
244 }
245
246 $bn_asm_obj='';
247 $bn_asm_src='';
248 $des_enc_obj='';
249 $des_enc_src='';
250 $bf_enc_obj='';
251 $bf_enc_src='';
252
253 if (!$no_asm)
254         {
255         win32_import_asm($mf_bn_asm, "bn", \$bn_asm_obj, \$bn_asm_src);
256         win32_import_asm($mf_aes_asm, "aes", \$aes_asm_obj, \$aes_asm_src);
257         win32_import_asm($mf_des_asm, "des", \$des_enc_obj, \$des_enc_src);
258         win32_import_asm($mf_bf_asm, "bf", \$bf_enc_obj, \$bf_enc_src);
259         win32_import_asm($mf_cast_asm, "cast", \$cast_enc_obj, \$cast_enc_src);
260         win32_import_asm($mf_rc4_asm, "rc4", \$rc4_enc_obj, \$rc4_enc_src);
261         win32_import_asm($mf_rc5_asm, "rc5", \$rc5_enc_obj, \$rc5_enc_src);
262         win32_import_asm($mf_md5_asm, "md5", \$md5_asm_obj, \$md5_asm_src);
263         win32_import_asm($mf_sha_asm, "sha", \$sha1_asm_obj, \$sha1_asm_src);
264         win32_import_asm($mf_rmd_asm, "ripemd", \$rmd160_asm_obj, \$rmd160_asm_src);
265         win32_import_asm($mf_wp_asm, "whrlpool", \$whirlpool_asm_obj, \$whirlpool_asm_src);
266         win32_import_asm($mf_modes_asm, "modes", \$modes_asm_obj, \$modes_asm_src);
267         win32_import_asm($mf_cpuid_asm, "", \$cpuid_asm_obj, \$cpuid_asm_src);
268         $perl_asm = 1;
269         }
270
271 if ($shlib && $FLAVOR !~ /CE/)
272         {
273         $mlflags.=" $lflags /dll";
274         $lib_cflag.=" -D_WINDLL";
275         #
276         # Engage Applink...
277         #
278         $app_ex_obj.=" \$(OBJ_D)\\applink.obj /implib:\$(TMP_D)\\junk.lib";
279         $cflags.=" -DOPENSSL_USE_APPLINK -I.";
280         # I'm open for better suggestions than overriding $banner...
281         $banner=<<'___';
282         @echo Building OpenSSL
283
284 $(OBJ_D)\applink.obj:   ms\applink.c
285         $(CC) /Fo$(OBJ_D)\applink.obj $(APP_CFLAGS) -c ms\applink.c
286 $(OBJ_D)\uplink.obj:    ms\uplink.c ms\applink.c
287         $(CC) /Fo$(OBJ_D)\uplink.obj $(SHLIB_CFLAGS) -c ms\uplink.c
288
289 LIBS_DEP=$(LIBS_DEP) $(OBJ_D)\applink.obj
290 CRYPTOOBJ=$(OBJ_D)\uplink.obj $(CRYPTOOBJ)
291 ___
292         $banner.=<<'___' if ($FLAVOR =~ /WIN64/);
293 CRYPTOOBJ=ms\uptable.obj $(CRYPTOOBJ)
294 ___
295         }
296 elsif ($shlib && $FLAVOR =~ /CE/)
297         {
298         $mlflags.=" $lflags /dll";
299         $lflags.=' /entry:mainCRTstartup' if(defined($ENV{'PORTSDK_LIBPATH'}));
300         $lib_cflag.=" -D_WINDLL";
301         $lib_cflag.=" -D_DLL" if (!$fipscanisterbuild);
302         }
303
304 sub do_rehash_rule {
305     my ($target, $deps) = @_;
306     my $ret = <<"EOF";
307 $target: $deps
308         set OPENSSL=\$(BIN_D)${o}openssl.exe
309         set OPENSSL_DEBUG_MEMORY=on
310         \$(PERL) \$(BIN_D)${o}c_rehash certs/demo
311         echo off > $target
312 EOF
313     return $ret
314 }
315 sub do_test_rule {
316     my ($target, $deps, $test_cmd) = @_;
317     my $ret = <<"EOF";
318 $target: $deps force.$target
319         set TOP=.
320         set BIN_D=\$(BIN_D)
321         set TEST_D=\$(TEST_D)
322         set PERL=\$(PERL)
323         \$(PERL) test\\$test_cmd \$(TESTS)
324 force.$target:
325 EOF
326 }
327
328 sub do_lib_rule
329         {
330         my($objs,$target,$name,$shlib,$ign,$base_addr) = @_;
331         local($ret);
332
333         $taget =~ s/\//$o/g if $o ne '/';
334         my $base_arg;
335         if ($base_addr ne "")
336                 {
337                 $base_arg= " /base:$base_addr";
338                 }
339         else
340                 {
341                 $base_arg = "";
342                 }
343         if ($name ne "")
344                 {
345                 $name =~ tr/a-z/A-Z/;
346                 $name = "/def:ms/${name}.def";
347                 }
348
349 #       $target="\$(LIB_D)$o$target";
350 #       $ret.="$target: $objs\n";
351         if (!$shlib)
352                 {
353 #               $ret.="\t\$(RM) \$(O_$Name)\n";
354                 $ret.="$target: $objs\n";
355                 $ret.="\t\$(MKLIB) $lfile$target @<<\n  $objs\n<<\n";
356                 }
357         else
358                 {
359                 local($ex)=($target =~ /O_CRYPTO/)?'':' $(L_CRYPTO)';
360                 $ex.=" $zlib_lib" if $zlib_opt == 1 && $target =~ /O_CRYPTO/;
361
362                 if ($fips && $target =~ /O_CRYPTO/)
363                         {
364                         $ret.="$target: $objs \$(PREMAIN_DSO_EXE)";
365                         $ret.="\n\tSET FIPS_LINK=\$(LINK_CMD)\n";
366                         $ret.="\tSET FIPS_CC=\$(CC)\n";
367                         $ret.="\tSET FIPS_CC_ARGS=/Fo\$(OBJ_D)${o}fips_premain.obj \$(SHLIB_CFLAGS) -c\n";
368                         $ret.="\tSET PREMAIN_DSO_EXE=\$(PREMAIN_DSO_EXE)\n";
369                         $ret.="\tSET FIPS_SHA1_EXE=\$(FIPS_SHA1_EXE)\n";
370                         $ret.="\tSET FIPS_TARGET=$target\n";
371                         $ret.="\tSET FIPSLIB_D=\$(FIPSLIB_D)\n";
372                         $ret.="\t\$(FIPSLINK) \$(MLFLAGS) /map $base_arg $efile$target ";
373                         $ret.="$name @<<\n  \$(SHLIB_EX_OBJ) $objs \$(EX_LIBS) ";
374                         $ret.="\$(OBJ_D)${o}fips_premain.obj $ex\n<<\n";
375                         }
376                 else
377                         {
378                         $ret.="$target: $objs";
379                         $ret.="\n\t\$(LINK_CMD) \$(MLFLAGS) $efile$target $name @<<\n  \$(SHLIB_EX_OBJ) $objs $ex \$(EX_LIBS)\n<<\n";
380                         }
381
382                 $ret.="\tIF EXIST \$@.manifest mt -nologo -manifest \$@.manifest -outputresource:\$@;2\n\n";
383                 }
384         $ret.="\n";
385         return($ret);
386         }
387
388 sub do_link_rule
389         {
390         my($target,$files,$dep_libs,$libs,$standalone)=@_;
391         local($ret,$_);
392         $file =~ s/\//$o/g if $o ne '/';
393         $n=&bname($target);
394         $ret.="$target: $files $dep_libs\n";
395         if ($standalone == 1)
396                 {
397                 $ret.="  \$(LINK_CMD) \$(LFLAGS) $efile$target @<<\n\t";
398                 $ret.= "\$(EX_LIBS) " if ($files =~ /O_FIPSCANISTER/ && !$fipscanisterbuild);
399                 $ret.="$files $libs\n<<\n";
400                 }
401         elsif ($standalone == 2)
402                 {
403                 $ret.="\tSET FIPS_LINK=\$(LINK_CMD)\n";
404                 $ret.="\tSET FIPS_CC=\$(CC)\n";
405                 $ret.="\tSET FIPS_CC_ARGS=/Fo\$(OBJ_D)${o}fips_premain.obj \$(SHLIB_CFLAGS) -c\n";
406                 $ret.="\tSET PREMAIN_DSO_EXE=\n";
407                 $ret.="\tSET FIPS_TARGET=$target\n";
408                 $ret.="\tSET FIPS_SHA1_EXE=\$(FIPS_SHA1_EXE)\n";
409                 $ret.="\tSET FIPSLIB_D=\$(FIPSLIB_D)\n";
410                 $ret.="\t\$(FIPSLINK) \$(LFLAGS) /map $efile$target @<<\n";
411                 $ret.="\t\$(APP_EX_OBJ) $files \$(OBJ_D)${o}fips_premain.obj $libs\n<<\n";
412                 }
413         else
414                 {
415                 $ret.="\t\$(LINK_CMD) \$(LFLAGS) $efile$target @<<\n";
416                 $ret.="\t\$(APP_EX_OBJ) $files $libs\n<<\n";
417                 }
418         $ret.="\tIF EXIST \$@.manifest mt -nologo -manifest \$@.manifest -outputresource:\$@;1\n\n";
419         return($ret);
420         }
421
422 sub do_rlink_rule
423         {
424         local($target,$rl_start, $rl_mid, $rl_end,$dep_libs,$libs)=@_;
425         local($ret,$_);
426         my $files = "$rl_start $rl_mid $rl_end";
427
428         $file =~ s/\//$o/g if $o ne '/';
429         $n=&bname($target);
430         $ret.="$target: $files $dep_libs \$(FIPS_SHA1_EXE)\n";
431         $ret.="\t\$(PERL) ms\\segrenam.pl \$\$a $rl_start\n";
432         $ret.="\t\$(PERL) ms\\segrenam.pl \$\$b $rl_mid\n";
433         $ret.="\t\$(PERL) ms\\segrenam.pl \$\$c $rl_end\n";
434         $ret.="\t\$(MKLIB) $lfile$target @<<\n\t$files\n<<\n";
435         $ret.="\t\$(FIPS_SHA1_EXE) $target > ${target}.sha1\n";
436         $ret.="\t\$(PERL) util${o}copy.pl -stripcr fips${o}fips_premain.c \$(LIB_D)${o}fips_premain.c\n";
437         $ret.="\t\$(CP) fips${o}fips_premain.c.sha1 \$(LIB_D)${o}fips_premain.c.sha1\n";
438         $ret.="\n";
439         return($ret);
440         }
441
442 sub win32_import_asm
443         {
444         my ($mf_var, $asm_name, $oref, $sref) = @_;
445         my $asm_dir;
446         if ($asm_name eq "")
447                 {
448                 $asm_dir = "crypto\\";
449                 }
450         else
451                 {
452                 $asm_dir = "crypto\\$asm_name\\asm\\";
453                 }
454
455         $$oref = "";
456         $$sref = "";
457         $mf_var =~ s/\.o//g;
458
459         foreach (split(/ /, $mf_var))
460                 {
461                 $$sref .= $asm_dir . $_ . ".asm ";
462                 }
463         foreach (split(/ /, $mf_var))
464                 {
465                 $$oref .= "\$(TMP_D)\\" . $_ . ".obj ";
466                 }
467         $$oref =~ s/ $//;
468         $$sref =~ s/ $//;
469
470         }
471
472
473 1;