Recognise fipscheck option and call fipsas for WIN64 builds.
[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
30 $zlib_lib="zlib1.lib";
31
32 # Santize -L options for ms link
33 $l_flags =~ s/-L("\[^"]+")/\/libpath:$1/g;
34 $l_flags =~ s/-L(\S+)/\/libpath:$1/g;
35
36 # C compiler stuff
37 $cc='cl';
38 if ($FLAVOR =~ /WIN64/)
39     {
40     # Note that we currently don't have /WX on Win64! There is a lot of
41     # warnings, but only of two types:
42     #
43     # C4344: conversion from '__int64' to 'int/long', possible loss of data
44     # C4267: conversion from 'size_t' to 'int/long', possible loss of data
45     #
46     # Amount of latter type is minimized by aliasing strlen to function of
47     # 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     # considered safe to ignore.
50     # 
51     $base_cflags= " $mf_cflag";
52     my $f = $shlib?' /MD':' /MT';
53     $lib_cflag='/Zl' if (!$shlib);      # remove /DEFAULTLIBs from static lib
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:IX86";    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='$(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";
128     $base_cflags.=' -I$(WCECOMPAT)/include'             if (defined($ENV{'WCECOMPAT'}));
129     $base_cflags.=' -I$(PORTSDK_LIBPATH)/../../include' if (defined($ENV{'PORTSDK_LIBPATH'}));
130     $opt_cflags=' /MC /O1i';    # optimize for space, but with intrinsics...
131     $dbg_clfags=' /MC /Od -DDEBUG -D_DEBUG';
132     $lflags="/nologo /opt:ref $wcelflag";
133     }
134 else    # Win32
135     {
136     $base_cflags= " $mf_cflag";
137     my $f = $shlib?' /MD':' /MT';
138     $lib_cflag='/Zl' if (!$shlib);      # remove /DEFAULTLIBs from static lib
139     $opt_cflags=$f.' /Ox /O2 /Ob2';
140     $dbg_cflags=$f.'d /Od -DDEBUG -D_DEBUG';
141     $lflags="/nologo /subsystem:console /opt:ref";
142     }
143 $mlflags='';
144
145 $out_def ="out32";      $out_def.="dll"                 if ($shlib);
146                         $out_def.='_$(TARGETCPU)'       if ($FLAVOR =~ /CE/);
147 $tmp_def ="tmp32";      $tmp_def.="dll"                 if ($shlib);
148                         $tmp_def.='_$(TARGETCPU)'       if ($FLAVOR =~ /CE/);
149 $inc_def="inc32";
150
151 if ($debug)
152         {
153         $cflags=$dbg_cflags.$base_cflags;
154         }
155 else
156         {
157         $cflags=$opt_cflags.$base_cflags;
158         }
159
160 # generate symbols.pdb unconditionally
161 $app_cflag.=" /Zi /Fd\$(TMP_D)/app";
162 $lib_cflag.=" /Zi /Fd\$(TMP_D)/lib";
163 $lflags.=" /debug";
164
165 $obj='.obj';
166 $asm_suffix='.asm';
167 $ofile="/Fo";
168
169 # EXE linking stuff
170 $link="link";
171 $rsc="rc";
172 $efile="/out:";
173 $exep='.exe';
174 if ($no_sock)           { $ex_libs=''; }
175 elsif ($FLAVOR =~ /CE/) { $ex_libs='winsock.lib'; }
176 else                    { $ex_libs='ws2_32.lib'; }
177
178 if ($FLAVOR =~ /CE/)
179         {
180         $ex_libs.=' $(WCECOMPAT)/lib/wcecompatex.lib'   if (defined($ENV{'WCECOMPAT'}));
181         $ex_libs.=' $(PORTSDK_LIBPATH)/portlib.lib'     if (defined($ENV{'PORTSDK_LIBPATH'}));
182         $ex_libs.=' /nodefaultlib:oldnames.lib coredll.lib corelibc.lib' if ($ENV{'TARGETCPU'} eq "X86");
183         }
184 else
185         {
186         $ex_libs.=' gdi32.lib advapi32.lib crypt32.lib user32.lib';
187         $ex_libs.=' bufferoverflowu.lib' if ($FLAVOR =~ /WIN64/ and `cl 2>&1` =~ /14\.00\.4[0-9]{4}\./);
188         # WIN32 UNICODE build gets linked with unicows.lib for
189         # backward compatibility with Win9x.
190         $ex_libs="unicows.lib $ex_libs" if ($FLAVOR =~ /WIN32/ and $cflags =~ /\-DUNICODE/);
191         }
192
193 # static library stuff
194 $mklib='lib /nologo';
195 $ranlib='';
196 $plib="";
197 $libp=".lib";
198 $shlibp=($shlib)?".dll":".lib";
199 $lfile='/out:';
200
201 $shlib_ex_obj="";
202 $app_ex_obj="setargv.obj" if ($FLAVOR !~ /CE/);
203 if ($FLAVOR =~ /WIN64A/) {
204         if (`nasm -v 2>NUL` =~ /NASM version ([0-9]+\.[0-9]+)/ && $1 >= 2.0) {
205                 $asm='nasm -f win64 -DNEAR -Ox -g';
206                 $afile='-o ';
207         } else {
208                 $asm='ml64 /c /Cp /Cx /Zi';
209                 $afile='/Fo';
210         }
211 } elsif ($FLAVOR =~ /WIN64I/) {
212         $asm='ias -d debug';
213         $afile="-o ";
214 } elsif ($nasm) {
215         my $ver=`nasm -v 2>NUL`;
216         my $vew=`nasmw -v 2>NUL`;
217         # pick newest version
218         $asm=($ver ge $vew?"nasm":"nasmw")." -f win32";
219         $asmtype="win32n";
220         $afile='-o ';
221 } else {
222         $asm='ml /nologo /Cp /coff /c /Cx /Zi';
223         $afile='/Fo';
224         $asmtype="win32";
225 }
226
227 $bn_asm_obj='';
228 $bn_asm_src='';
229 $des_enc_obj='';
230 $des_enc_src='';
231 $bf_enc_obj='';
232 $bf_enc_src='';
233
234 if (!$no_asm)
235         {
236         win32_import_asm($mf_bn_asm, "bn", \$bn_asm_obj, \$bn_asm_src);
237         win32_import_asm($mf_aes_asm, "aes", \$aes_asm_obj, \$aes_asm_src);
238         win32_import_asm($mf_des_asm, "des", \$des_enc_obj, \$des_enc_src);
239         win32_import_asm($mf_bf_asm, "bf", \$bf_enc_obj, \$bf_enc_src);
240         win32_import_asm($mf_cast_asm, "cast", \$cast_enc_obj, \$cast_enc_src);
241         win32_import_asm($mf_rc4_asm, "rc4", \$rc4_enc_obj, \$rc4_enc_src);
242         win32_import_asm($mf_rc5_asm, "rc5", \$rc5_enc_obj, \$rc5_enc_src);
243         win32_import_asm($mf_md5_asm, "md5", \$md5_asm_obj, \$md5_asm_src);
244         win32_import_asm($mf_sha_asm, "sha", \$sha1_asm_obj, \$sha1_asm_src);
245         win32_import_asm($mf_rmd_asm, "ripemd", \$rmd160_asm_obj, \$rmd160_asm_src);
246         win32_import_asm($mf_wp_asm, "whrlpool", \$whirlpool_asm_obj, \$whirlpool_asm_src);
247         win32_import_asm($mf_modes_asm, "modes", \$modes_asm_obj, \$modes_asm_src);
248         win32_import_asm($mf_cpuid_asm, "", \$cpuid_asm_obj, \$cpuid_asm_src);
249         $perl_asm = 1;
250         }
251
252 if ($shlib && $FLAVOR !~ /CE/)
253         {
254         $mlflags.=" $lflags /dll";
255         $lib_cflag.=" -D_WINDLL";
256         #
257         # Engage Applink...
258         #
259         $app_ex_obj.=" \$(OBJ_D)\\applink.obj /implib:\$(TMP_D)\\junk.lib";
260         $cflags.=" -DOPENSSL_USE_APPLINK -I.";
261         # I'm open for better suggestions than overriding $banner...
262         $banner=<<'___';
263         @echo Building OpenSSL
264
265 $(OBJ_D)\applink.obj:   ms\applink.c
266         $(CC) /Fo$(OBJ_D)\applink.obj $(APP_CFLAGS) -c ms\applink.c
267 $(OBJ_D)\uplink.obj:    ms\uplink.c ms\applink.c
268         $(CC) /Fo$(OBJ_D)\uplink.obj $(SHLIB_CFLAGS) -c ms\uplink.c
269 $(INCO_D)\applink.c:    ms\applink.c
270         $(CP) ms\applink.c $(INCO_D)\applink.c
271
272 EXHEADER= $(EXHEADER) $(INCO_D)\applink.c
273
274 LIBS_DEP=$(LIBS_DEP) $(OBJ_D)\applink.obj
275 CRYPTOOBJ=$(OBJ_D)\uplink.obj $(CRYPTOOBJ)
276 ___
277         $banner.=<<'___' if ($FLAVOR =~ /WIN64/);
278 CRYPTOOBJ=ms\uptable.obj $(CRYPTOOBJ)
279 ___
280         }
281 elsif ($shlib && $FLAVOR =~ /CE/)
282         {
283         $mlflags.=" $lflags /dll";
284         $lflags.=' /entry:mainCRTstartup' if(defined($ENV{'PORTSDK_LIBPATH'}));
285         $lib_cflag.=" -D_WINDLL -D_DLL";
286         }
287
288 sub do_lib_rule
289         {
290         my($objs,$target,$name,$shlib,$ign,$base_addr) = @_;
291         local($ret);
292
293         $taget =~ s/\//$o/g if $o ne '/';
294         my $base_arg;
295         if ($base_addr ne "")
296                 {
297                 $base_arg= " /base:$base_addr";
298                 }
299         else
300                 {
301                 $base_arg = "";
302                 }
303         if ($name ne "")
304                 {
305                 $name =~ tr/a-z/A-Z/;
306                 $name = "/def:ms/${name}.def";
307                 }
308
309 #       $target="\$(LIB_D)$o$target";
310 #       $ret.="$target: $objs\n";
311         if (!$shlib)
312                 {
313 #               $ret.="\t\$(RM) \$(O_$Name)\n";
314                 $ret.="$target: $objs\n";
315                 $ret.="\t\$(MKLIB) $lfile$target @<<\n  $objs\n<<\n";
316                 }
317         else
318                 {
319                 local($ex)=($target =~ /O_CRYPTO/)?'':' $(L_CRYPTO)';
320                 $ex.=" $zlib_lib" if $zlib_opt == 1 && $target =~ /O_CRYPTO/;
321
322                 if ($fips && $target =~ /O_CRYPTO/)
323                         {
324                         $ret.="$target: $objs \$(PREMAIN_DSO_EXE)";
325                         $ret.="\n\tSET FIPS_LINK=\$(LINK)\n";
326                         $ret.="\tSET FIPS_CC=\$(CC)\n";
327                         $ret.="\tSET FIPS_CC_ARGS=/Fo\$(OBJ_D)${o}fips_premain.obj \$(SHLIB_CFLAGS) -c\n";
328                         $ret.="\tSET PREMAIN_DSO_EXE=\$(PREMAIN_DSO_EXE)\n";
329                         $ret.="\tSET FIPS_SHA1_EXE=\$(FIPS_SHA1_EXE)\n";
330                         $ret.="\tSET FIPS_TARGET=$target\n";
331                         $ret.="\tSET FIPSLIB_D=\$(FIPSLIB_D)\n";
332                         $ret.="\t\$(FIPSLINK) \$(MLFLAGS) /map $base_arg $efile$target ";
333                         $ret.="$name @<<\n  \$(SHLIB_EX_OBJ) $objs \$(EX_LIBS) ";
334                         $ret.="\$(OBJ_D)${o}fips_premain.obj $ex\n<<\n";
335                         }
336                 else
337                         {
338                         $ret.="$target: $objs";
339                         $ret.="\n\t\$(LINK) \$(MLFLAGS) $efile$target $name @<<\n  \$(SHLIB_EX_OBJ) $objs $ex \$(EX_LIBS)\n<<\n";
340                         }
341
342                 $ret.="\tIF EXIST \$@.manifest mt -nologo -manifest \$@.manifest -outputresource:\$@;2\n\n";
343                 }
344         $ret.="\n";
345         return($ret);
346         }
347
348 sub do_link_rule
349         {
350         my($target,$files,$dep_libs,$libs,$standalone)=@_;
351         local($ret,$_);
352         $file =~ s/\//$o/g if $o ne '/';
353         $n=&bname($targer);
354         $ret.="$target: $files $dep_libs\n";
355         if ($standalone == 1)
356                 {
357                 $ret.="  \$(LINK) \$(LFLAGS) $efile$target @<<\n\t";
358                 $ret.= "\$(EX_LIBS) " if ($files =~ /O_FIPSCANISTER/ && !$fipscanisterbuild);
359                 $ret.="$files $libs\n<<\n";
360                 }
361         elsif ($standalone == 2)
362                 {
363                 $ret.="\tSET FIPS_LINK=\$(LINK)\n";
364                 $ret.="\tSET FIPS_CC=\$(CC)\n";
365                 $ret.="\tSET FIPS_CC_ARGS=/Fo\$(OBJ_D)${o}fips_premain.obj \$(SHLIB_CFLAGS) -c\n";
366                 $ret.="\tSET PREMAIN_DSO_EXE=\n";
367                 $ret.="\tSET FIPS_TARGET=$target\n";
368                 $ret.="\tSET FIPS_SHA1_EXE=\$(FIPS_SHA1_EXE)\n";
369                 $ret.="\tSET FIPSLIB_D=\$(FIPSLIB_D)\n";
370                 $ret.="\t\$(FIPSLINK) \$(LFLAGS) /map $efile$target @<<\n";
371                 $ret.="\t\$(APP_EX_OBJ) $files \$(OBJ_D)${o}fips_premain.obj $libs\n<<\n";
372                 }
373         else
374                 {
375                 $ret.="\t\$(LINK) \$(LFLAGS) $efile$target @<<\n";
376                 $ret.="\t\$(APP_EX_OBJ) $files $libs\n<<\n";
377                 }
378         $ret.="\tIF EXIST \$@.manifest mt -nologo -manifest \$@.manifest -outputresource:\$@;1\n\n";
379         return($ret);
380         }
381
382 sub do_rlink_rule
383         {
384         local($target,$rl_start, $rl_mid, $rl_end,$dep_libs,$libs)=@_;
385         local($ret,$_);
386         my $files = "$rl_start $rl_mid $rl_end";
387
388         $file =~ s/\//$o/g if $o ne '/';
389         $n=&bname($targer);
390         $ret.="$target: $files $dep_libs \$(FIPS_SHA1_EXE)\n";
391         $ret.="\t\$(PERL) ms\\segrenam.pl \$\$a $rl_start\n";
392         $ret.="\t\$(PERL) ms\\segrenam.pl \$\$b $rl_mid\n";
393         $ret.="\t\$(PERL) ms\\segrenam.pl \$\$c $rl_end\n";
394         $ret.="\t\$(MKLIB) $lfile$target @<<\n\t$files\n<<\n";
395         $ret.="\t\$(FIPS_SHA1_EXE) $target > ${target}.sha1\n";
396         $ret.="\t\$(PERL) util${o}copy.pl -stripcr fips${o}fips_premain.c \$(LIB_D)${o}fips_premain.c\n";
397         $ret.="\t\$(CP) fips${o}fips_premain.c.sha1 \$(LIB_D)${o}fips_premain.c.sha1\n";
398         $ret.="\n";
399         return($ret);
400         }
401
402 sub win32_import_asm
403         {
404         my ($mf_var, $asm_name, $oref, $sref) = @_;
405         my $asm_dir;
406         if ($asm_name eq "")
407                 {
408                 $asm_dir = "crypto\\";
409                 }
410         else
411                 {
412                 $asm_dir = "crypto\\$asm_name\\asm\\";
413                 }
414
415         $$oref = "";
416         $$sref = "";
417         $mf_var =~ s/\.o//g;
418
419         foreach (split(/ /, $mf_var))
420                 {
421                 $$sref .= $asm_dir . $_ . ".asm ";
422                 }
423         foreach (split(/ /, $mf_var))
424                 {
425                 $$oref .= "\$(TMP_D)\\" . $_ . ".obj ";
426                 }
427         $$oref =~ s/ $//;
428         $$sref =~ s/ $//;
429
430         }
431
432
433 1;