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