Update netware to use new SHA2 assembly language modules.
[openssl.git] / util / pl / netware.pl
1 # Metrowerks Codewarrior or gcc / nlmconv for NetWare
2 #
3
4 $version_header = "crypto/opensslv.h";
5 open(IN, "$version_header") or die "Couldn't open $version_header: $!";
6 while (<IN>) {
7   if (/^#define[\s\t]+OPENSSL_VERSION_NUMBER[\s\t]+0x(\d)(\d{2})(\d{2})(\d{2})/)
8   {
9     # die "OpenSSL version detected: $1.$2.$3.$4\n";
10     #$nlmvernum = "$1,$2,$3";
11     $nlmvernum = "$1,".($2*10+$3).",".($4*1);
12     #$nlmverstr = "$1.".($2*1).".".($3*1).($4?(chr(96+$4)):"");
13     break;
14   }
15 }
16 close(IN) or die "Couldn't close $version_header: $!";
17
18 $readme_file = "README";
19 open(IN, $readme_file) or die "Couldn't open $readme_file: $!";
20 while (<IN>) {
21   if (/^[\s\t]+OpenSSL[\s\t]+(\d)\.(\d{1,2})\.(\d{1,2})([a-z])(.*)/)
22   {
23     #$nlmvernum = "$1,$2,$3";
24     #$nlmvernum = "$1,".($2*10+$3).",".($4*1);
25     $nlmverstr = "$1.$2.$3$4$5";
26   }
27   elsif (/^[\s\t]+(Copyright \(c\) \d{4}\-\d{4} The OpenSSL Project)$/)
28   {
29     $nlmcpystr = $1;
30   }
31   break if ($nlmvernum && $nlmcpystr);
32 }
33 close(IN) or die "Couldn't close $readme_file: $!";
34
35 # Define stacksize here
36 $nlmstack = "32768";
37
38 # some default settings here in case we failed to find them in README
39 $nlmvernum = "1,0,0" if (!$nlmvernum);
40 $nlmverstr = "OpenSSL" if (!$nlmverstr);
41 $nlmcpystr = "Copyright (c) 1998-now The OpenSSL Project" if (!$nlmcpystr);
42
43 # die "OpenSSL copyright: $nlmcpystr\nOpenSSL verstring: $nlmverstr\nOpenSSL vernumber: $nlmvernum\n";
44
45 # The import files and other misc imports needed to link
46 @misc_imports = ("GetProcessSwitchCount", "RunningProcess",
47                  "GetSuperHighResolutionTimer");
48 if ($LIBC)
49 {
50    @import_files = ("libc.imp");
51    @module_files = ("libc");
52    $libarch = "LIBC";
53 }
54 else
55 {
56    # clib build
57    @import_files = ("clib.imp");
58    push(@import_files, "socklib.imp") if ($BSDSOCK);
59    @module_files = ("clib");
60    # push(@misc_imports, "_rt_modu64%16", "_rt_divu64%16");
61    $libarch = "CLIB";
62 }
63 if ($BSDSOCK)
64 {
65    $libarch .= "-BSD";
66 }
67 else
68 {
69    $libarch .= "-WS2";
70    push(@import_files, "ws2nlm.imp");
71 }
72
73 # The "IMPORTS" environment variable must be set and point to the location
74 # where import files (*.imp) can be found.
75 # Example:  set IMPORTS=c:\ndk\nwsdk\imports
76 $import_path = $ENV{"IMPORTS"} || die ("IMPORTS environment variable not set\n");
77
78
79 # The "PRELUDE" environment variable must be set and point to the location
80 # and name of the prelude source to link with ( nwpre.obj is recommended ).
81 # Example: set PRELUDE=c:\codewar\novell support\metrowerks support\libraries\runtime\nwpre.obj
82 $prelude = $ENV{"PRELUDE"} || die ("PRELUDE environment variable not set\n");
83
84 # The "INCLUDES" environment variable must be set and point to the location
85 # where import files (*.imp) can be found.
86 $include_path = $ENV{"INCLUDE"} || die ("INCLUDES environment variable not set\n");
87 $include_path =~ s/\\/\//g;
88 $include_path = join(" -I", split(/;/, $include_path));
89
90 # check for gcc compiler
91 $gnuc = $ENV{"GNUC"};
92
93 #$ssl=   "ssleay32";
94 #$crypto="libeay32";
95
96 if ($gnuc)
97 {
98    # C compiler
99    $cc='gcc';
100    # Linker
101    $link='nlmconv';
102    # librarian
103    $mklib='ar';
104    $o='/';
105    # cp command
106    $cp='cp -af';
107    # rm command
108    $rm='rm -f';
109    # mv command
110    $mv='mv -f';
111    # mkdir command
112    $mkdir='gmkdir';
113    #$ranlib='ranlib';
114 }
115 else
116 {
117    # C compiler
118    $cc='mwccnlm';
119    # Linker
120    $link='mwldnlm';
121    # librarian
122    $mklib='mwldnlm';
123    # Path separator
124    $o='\\';
125    # cp command
126    $cp='copy >nul:';
127    # rm command
128    $rm='del /f /q';
129 }
130
131 # assembler
132 if ($nw_nasm)
133 {
134    $asm=(`nasm -v 2>NUL` gt `nasmw -v 2>NUL`?"nasm":"nasmw");
135    if ($gnuc)
136    {
137       $asm.=" -s -f elf";
138    }
139    else
140    {
141       $asm.=" -s -f coff -d __coff__";
142    }
143    $afile="-o ";
144    $asm.=" -g" if $debug;
145 }
146 elsif ($nw_mwasm)
147 {
148    $asm="mwasmnlm -maxerrors 20";
149    $afile="-o ";
150    $asm.=" -g" if $debug;
151 }
152 elsif ($nw_masm)
153 {
154 # masm assembly settings - it should be possible to use masm but haven't
155 # got it working.
156 # $asm='ml /Cp /coff /c /Cx';
157 # $asm.=" /Zi" if $debug;
158 # $afile='/Fo';
159    die("Support for masm assembler not yet functional\n");
160 }
161 else
162 {
163    $asm="";
164    $afile="";
165 }
166
167
168
169 if ($gnuc)
170 {
171    # compile flags for GNUC
172    # additional flags based upon debug | non-debug
173    if ($debug)
174    {
175       $cflags="-g -DDEBUG";
176    }
177    else
178    {
179       $cflags="-O2";
180    }
181    $cflags.=" -nostdinc -I$include_path \\
182          -fno-builtin -fpcc-struct-return -fno-strict-aliasing \\
183          -funsigned-char -Wall -Wno-unused -Wno-uninitialized";
184
185    # link flags
186    $lflags="-T";
187 }
188 else
189 {
190    # compile flags for CodeWarrior
191    # additional flags based upon debug | non-debug
192    if ($debug)
193    {
194       $cflags="-opt off -g -sym internal -DDEBUG";
195    }
196    else
197    {
198    # CodeWarrior compiler has a problem with optimizations for floating
199    # points - no optimizations until further investigation
200    #      $cflags="-opt all";
201    }
202
203    # NOTES: Several c files in the crypto subdirectory include headers from
204    #        their local directories.  Metrowerks wouldn't find these h files
205    #        without adding individual include directives as compile flags
206    #        or modifying the c files.  Instead of adding individual include
207    #        paths for each subdirectory a recursive include directive
208    #        is used ( -ir crypto ).
209    #
210    #        A similar issue exists for the engines and apps subdirectories.
211    #
212    #        Turned off the "possible" warnings ( -w nopossible ).  Metrowerks
213    #        complained a lot about various stuff.  May want to turn back
214    #        on for further development.
215    $cflags.=" -nostdinc -ir crypto -ir engines -ir apps -I$include_path \\
216          -msgstyle gcc -align 4 -processor pentium -char unsigned \\
217          -w on -w nolargeargs -w nopossible -w nounusedarg -w nounusedexpr \\
218          -w noimplicitconv -relax_pointers -nosyspath -maxerrors 20";
219
220    # link flags
221    $lflags="-msgstyle gcc -zerobss -nostdlib -sym internal -commandfile";
222 }
223
224 # common defines
225 $cflags.=" -DL_ENDIAN -DOPENSSL_SYSNAME_NETWARE -U_WIN32";
226
227 # If LibC build add in NKS_LIBC define and set the entry/exit
228 # routines - The default entry/exit routines are for CLib and don't exist
229 # in LibC
230 if ($LIBC)
231 {
232    $cflags.=" -DNETWARE_LIBC";
233    $nlmstart = "_LibCPrelude";
234    $nlmexit = "_LibCPostlude";
235    @nlm_flags = ("pseudopreemption", "flag_on 64");
236 }
237 else
238 {
239    $cflags.=" -DNETWARE_CLIB";
240    $nlmstart = "_Prelude";
241    $nlmexit = "_Stop";
242 }
243
244 # If BSD Socket support is requested, set a define for the compiler
245 if ($BSDSOCK)
246 {
247    $cflags.=" -DNETWARE_BSDSOCK";
248    if (!$LIBC)
249    {
250       $cflags.=" -DNETDB_USE_INTERNET";
251    }
252 }
253
254
255 # linking stuff
256 # for the output directories use the mk1mf.pl values with "_nw" appended
257 if ($shlib)
258 {
259    if ($LIBC)
260    {
261       $out_def.="_nw_libc_nlm";
262       $tmp_def.="_nw_libc_nlm";
263       $inc_def.="_nw_libc_nlm";
264    }
265    else  # NETWARE_CLIB
266    {
267       $out_def.="_nw_clib_nlm";
268       $tmp_def.="_nw_clib_nlm";
269       $inc_def.="_nw_clib_nlm";
270    }
271 }
272 else
273 {
274    if ($gnuc) # GNUC Tools
275    {
276       $libp=".a";
277       $shlibp=".a";
278       $lib_flags="-cr";
279    }
280    else       # CodeWarrior
281    {
282       $libp=".lib";
283       $shlibp=".lib";
284       $lib_flags="-nodefaults -type library -o";
285    }
286    if ($LIBC)
287    {
288       $out_def.="_nw_libc";
289       $tmp_def.="_nw_libc";
290       $inc_def.="_nw_libc";
291    }
292    else  # NETWARE_CLIB
293    {
294       $out_def.="_nw_clib";
295       $tmp_def.="_nw_clib";
296       $inc_def.="_nw_clib";
297    }
298 }
299
300 # used by mk1mf.pl
301 $obj='.o';
302 $ofile='-o ';
303 $efile='';
304 $exep='.nlm';
305 $ex_libs='';
306
307 if (!$no_asm)
308 {
309    $bn_asm_obj="\$(OBJ_D)${o}bn-nw${obj}";
310    $bn_asm_src="crypto${o}bn${o}asm${o}bn-nw.asm";
311    $bnco_asm_obj="\$(OBJ_D)${o}co-nw${obj}";
312    $bnco_asm_src="crypto${o}bn${o}asm${o}co-nw.asm";
313    $aes_asm_obj="\$(OBJ_D)${o}a-nw${obj}";
314    $aes_asm_src="crypto${o}aes${o}asm${o}a-nw.asm";
315    $des_enc_obj="\$(OBJ_D)${o}d-nw${obj} \$(OBJ_D)${o}y-nw${obj}";
316    $des_enc_src="crypto${o}des${o}asm${o}d-nw.asm crypto${o}des${o}asm${o}y-nw.asm";
317    $bf_enc_obj="\$(OBJ_D)${o}b-nw${obj}";
318    $bf_enc_src="crypto${o}bf${o}asm${o}b-nw.asm";
319    $cast_enc_obj="\$(OBJ_D)${o}c-nw${obj}";
320    $cast_enc_src="crypto${o}cast${o}asm${o}c-nw.asm";
321    $rc4_enc_obj="\$(OBJ_D)${o}r4-nw${obj}";
322    $rc4_enc_src="crypto${o}rc4${o}asm${o}r4-nw.asm";
323    $rc5_enc_obj="\$(OBJ_D)${o}r5-nw${obj}";
324    $rc5_enc_src="crypto${o}rc5${o}asm${o}r5-nw.asm";
325    $md5_asm_obj="\$(OBJ_D)${o}m5-nw${obj}";
326    $md5_asm_src="crypto${o}md5${o}asm${o}m5-nw.asm";
327    $sha1_asm_obj="\$(OBJ_D)${o}s1-nw${obj} \$(OBJ_D)${o}sha256-nw${obj} \$(OBJ_D)${o}sha512-nw${obj}";
328    $sha1_asm_src="crypto${o}sha${o}asm${o}s1-nw.asm crypto${o}sha${o}asm${o}sha256-nw.asm crypto${o}sha${o}asm${o}sha512-nw.asm";
329    $rmd160_asm_obj="\$(OBJ_D)${o}rm-nw${obj}";
330    $rmd160_asm_src="crypto${o}ripemd${o}asm${o}rm-nw.asm";
331    $whirlpool_asm_obj="\$(OBJ_D)${o}wp-nw${obj}";
332    $whirlpool_asm_src="crypto${o}whrlpool${o}asm${o}wp-nw.asm";
333    $cpuid_asm_obj="\$(OBJ_D)${o}x86cpuid-nw${obj}";
334    $cpuid_asm_src="crypto${o}x86cpuid-nw.asm";
335    $cflags.=" -DOPENSSL_CPUID_OBJ -DBN_ASM -DOPENSSL_BN_ASM_PART_WORDS -DMD5_ASM -DSHA1_ASM -DWHIRLPOOL_ASM";
336    $cflags.=" -DAES_ASM -DRMD160_ASM";
337 }
338 else
339 {
340    $bn_asm_obj='';
341    $bn_asm_src='';
342    $bnco_asm_obj='';
343    $bnco_asm_src='';
344    $aes_asm_obj='';
345    $aes_asm_src='';
346    $des_enc_obj='';
347    $des_enc_src='';
348    $bf_enc_obj='';
349    $bf_enc_src='';
350    $cast_enc_obj='';
351    $cast_enc_src='';
352    $rc4_enc_obj='';
353    $rc4_enc_src='';
354    $rc5_enc_obj='';
355    $rc5_enc_src='';
356    $md5_asm_obj='';
357    $md5_asm_src='';
358    $sha1_asm_obj='';
359    $sha1_asm_src='';
360    $rmd160_asm_obj='';
361    $rmd160_asm_src='';
362    $whirlpool_asm_obj='';
363    $whirlpool_asm_src='';
364    $cpuid_asm_obj='';
365    $cpuid_asm_src='';
366 }
367
368 # create the *.def linker command files in \openssl\netware\ directory
369 sub do_def_file
370 {
371    # strip off the leading path
372    my($target) = bname(shift);
373    my($i);
374
375    if ($target =~ /(.*).nlm/)
376    {
377       $target = $1;
378    }
379
380    # special case for openssl - the mk1mf.pl defines E_EXE = openssl
381    if ($target =~ /E_EXE/)
382    {
383       $target =~ s/\$\(E_EXE\)/openssl/;
384    }
385
386    # Note: originally tried to use full path ( \openssl\netware\$target.def )
387    # Metrowerks linker choked on this with an assertion failure. bug???
388    #
389    my($def_file) = "netware${o}$target.def";
390
391    open(DEF_OUT, ">$def_file") || die("unable to open file $def_file\n");
392
393    print( DEF_OUT "# command file generated by netware.pl for NLM target.\n" );
394    print( DEF_OUT "# do not edit this file - all your changes will be lost!!\n" );
395    print( DEF_OUT "#\n");
396    print( DEF_OUT "DESCRIPTION \"$target ($libarch) - OpenSSL $nlmverstr\"\n");
397    print( DEF_OUT "COPYRIGHT \"$nlmcpystr\"\n");
398    print( DEF_OUT "VERSION $nlmvernum\n");
399    print( DEF_OUT "STACK $nlmstack\n");
400    print( DEF_OUT "START $nlmstart\n");
401    print( DEF_OUT "EXIT $nlmexit\n");
402
403    # special case for openssl
404    if ($target eq "openssl")
405    {
406       print( DEF_OUT "SCREENNAME \"OpenSSL $nlmverstr\"\n");
407    }
408    else
409    {
410       print( DEF_OUT "SCREENNAME \"DEFAULT\"\n");
411    }
412
413    foreach $i (@misc_imports)
414    {
415       print( DEF_OUT "IMPORT $i\n");
416    }
417
418    foreach $i (@import_files)
419    {
420       print( DEF_OUT "IMPORT \@$import_path${o}$i\n");
421    }
422
423    foreach $i (@module_files)
424    {
425       print( DEF_OUT "MODULE $i\n");
426    }
427
428    foreach $i (@nlm_flags)
429    {
430       print( DEF_OUT "$i\n");
431    }
432
433    if ($gnuc)
434    {
435       if ($target =~ /openssl/)
436       {
437          print( DEF_OUT "INPUT ${tmp_def}${o}openssl${obj}\n");
438          print( DEF_OUT "INPUT ${tmp_def}${o}openssl${libp}\n");
439       }
440       else
441       {
442          print( DEF_OUT "INPUT ${tmp_def}${o}${target}${obj}\n");
443       }
444       print( DEF_OUT "INPUT $prelude\n");
445       print( DEF_OUT "INPUT ${out_def}${o}${ssl}${libp} ${out_def}${o}${crypto}${libp}\n");
446       print( DEF_OUT "OUTPUT $target.nlm\n");
447    }
448
449    close(DEF_OUT);
450    return($def_file);
451 }
452
453 sub do_lib_rule
454 {
455    my($objs,$target,$name,$shlib)=@_;
456    my($ret);
457
458    $ret.="$target: $objs\n";
459    if (!$shlib)
460    {
461       $ret.="\t\@echo Building Lib: $name\n";
462       $ret.="\t\$(MKLIB) $lib_flags $target $objs\n";
463       $ret.="\t\@echo .\n"
464    }
465    else
466    {
467       die( "Building as NLM not currently supported!" );
468    }
469
470    $ret.="\n";
471    return($ret);
472 }
473
474 sub do_link_rule
475 {
476    my($target,$files,$dep_libs,$libs)=@_;
477    my($ret);
478    my($def_file) = do_def_file($target);
479
480    $ret.="$target: $files $dep_libs\n";
481
482    # NOTE:  When building the test nlms no screen name is given
483    #  which causes the console screen to be used.  By using the console
484    #  screen there is no "<press any key to continue>" message which
485    #  requires user interaction.  The test script ( do_tests.pl ) needs
486    #  to be able to run the tests without requiring user interaction.
487    #
488    #  However, the sample program "openssl.nlm" is used by the tests and is
489    #  a interactive sample so a screen is desired when not be run by the
490    #  tests.  To solve the problem, two versions of the program are built:
491    #    openssl2 - no screen used by tests
492    #    openssl - default screen - use for normal interactive modes
493    #
494
495    # special case for openssl - the mk1mf.pl defines E_EXE = openssl
496    if ($target =~ /E_EXE/)
497    {
498       my($target2) = $target;
499
500       $target2 =~ s/\(E_EXE\)/\(E_EXE\)2/;
501
502       # openssl2
503       my($def_file2) = do_def_file($target2);
504
505       if ($gnuc)
506       {
507          $ret.="\t\$(MKLIB) $lib_flags \$(TMP_D)${o}\$(E_EXE).a \$(filter-out \$(TMP_D)${o}\$(E_EXE)${obj},$files)\n";
508          $ret.="\t\$(LINK) \$(LFLAGS) $def_file2\n";
509          $ret.="\t\@$mv \$(E_EXE)2.nlm \$(TEST_D)\n";
510       }
511       else
512       {
513          $ret.="\t\$(LINK) \$(LFLAGS) $def_file2 $files \"$prelude\" $libs -o $target2\n";
514       }
515    }
516    if ($gnuc)
517    {
518       $ret.="\t\$(LINK) \$(LFLAGS) $def_file\n";
519       $ret.="\t\@$mv \$(\@F) \$(TEST_D)\n";
520    }
521    else
522    {
523       $ret.="\t\$(LINK) \$(LFLAGS) $def_file $files \"$prelude\" $libs -o $target\n";
524    }
525
526    $ret.="\n";
527    return($ret);
528
529 }
530
531 1;