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