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