070c7aa79c59917e439d7ebe92da6b6a3c0f5412
[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=   "libssl32";
94 #$crypto="libcrypto32";
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    # mv command
130    $mv='move /y';
131 }
132
133 # assembler
134 if ($nw_nasm)
135 {
136    $asm=(`nasm -v 2>NUL` gt `nasmw -v 2>NUL`?"nasm":"nasmw");
137    if ($gnuc)
138    {
139       $asm.=" -s -f elf";
140    }
141    else
142    {
143       $asm.=" -s -f coff -d __coff__";
144    }
145    $afile="-o ";
146    $asm.=" -g" if $debug;
147 }
148 elsif ($nw_mwasm)
149 {
150    $asm="mwasmnlm -maxerrors 20";
151    $afile="-o ";
152    $asm.=" -g" if $debug;
153 }
154 elsif ($nw_masm)
155 {
156 # masm assembly settings - it should be possible to use masm but haven't
157 # got it working.
158 # $asm='ml /Cp /coff /c /Cx';
159 # $asm.=" /Zi" if $debug;
160 # $afile='/Fo';
161    die("Support for masm assembler not yet functional\n");
162 }
163 else
164 {
165    $asm="";
166    $afile="";
167 }
168
169
170
171 if ($gnuc)
172 {
173    # compile flags for GNUC
174    # additional flags based upon debug | non-debug
175    if ($debug)
176    {
177       $cflags="-g -DDEBUG";
178    }
179    else
180    {
181       $cflags="-O2";
182    }
183    $cflags.=" -nostdinc -I$include_path \\
184          -fno-builtin -fpcc-struct-return -fno-strict-aliasing \\
185          -funsigned-char -Wall -Wno-unused -Wno-uninitialized";
186
187    # link flags
188    $lflags="-T";
189 }
190 else
191 {
192    # compile flags for CodeWarrior
193    # additional flags based upon debug | non-debug
194    if ($debug)
195    {
196       $cflags="-opt off -g -sym internal -DDEBUG";
197    }
198    else
199    {
200    # CodeWarrior compiler has a problem with optimizations for floating
201    # points - no optimizations until further investigation
202    #      $cflags="-opt all";
203    }
204
205    # NOTES: Several c files in the crypto subdirectory include headers from
206    #        their local directories.  Metrowerks wouldn't find these h files
207    #        without adding individual include directives as compile flags
208    #        or modifying the c files.  Instead of adding individual include
209    #        paths for each subdirectory a recursive include directive
210    #        is used ( -ir crypto ).
211    #
212    #        A similar issue exists for the engines and apps subdirectories.
213    #
214    #        Turned off the "possible" warnings ( -w nopossible ).  Metrowerks
215    #        complained a lot about various stuff.  May want to turn back
216    #        on for further development.
217    $cflags.=" -nostdinc -ir crypto -ir ssl -ir engines -ir apps -I$include_path \\
218          -msgstyle gcc -align 4 -processor pentium -char unsigned \\
219          -w on -w nolargeargs -w nopossible -w nounusedarg -w nounusedexpr \\
220          -w noimplicitconv -relax_pointers -nosyspath -maxerrors 20";
221
222    # link flags
223    $lflags="-msgstyle gcc -zerobss -nostdlib -sym internal -commandfile";
224 }
225
226 # common defines
227 $cflags.=" -DL_ENDIAN -DOPENSSL_SYS_NETWARE -U_WIN32";
228
229 # If LibC build add in NKS_LIBC define and set the entry/exit
230 # routines - The default entry/exit routines are for CLib and don't exist
231 # in LibC
232 if ($LIBC)
233 {
234    $cflags.=" -DNETWARE_LIBC";
235    $nlmstart = "_LibCPrelude";
236    $nlmexit = "_LibCPostlude";
237    @nlm_flags = ("pseudopreemption", "flag_on 64");
238 }
239 else
240 {
241    $cflags.=" -DNETWARE_CLIB";
242    $nlmstart = "_Prelude";
243    $nlmexit = "_Stop";
244 }
245
246 # If BSD Socket support is requested, set a define for the compiler
247 if ($BSDSOCK)
248 {
249    $cflags.=" -DNETWARE_BSDSOCK";
250    if (!$LIBC)
251    {
252       $cflags.=" -DNETDB_USE_INTERNET";
253    }
254 }
255
256
257 # linking stuff
258 # for the output directories use the mk1mf.pl values with "_nw" appended
259 if ($shlib)
260 {
261    if ($LIBC)
262    {
263       $out_def.="_nw_libc_nlm";
264       $tmp_def.="_nw_libc_nlm";
265       $inc_def.="_nw_libc_nlm";
266    }
267    else  # NETWARE_CLIB
268    {
269       $out_def.="_nw_clib_nlm";
270       $tmp_def.="_nw_clib_nlm";
271       $inc_def.="_nw_clib_nlm";
272    }
273 }
274 else
275 {
276    if ($gnuc) # GNUC Tools
277    {
278       $libp=".a";
279       $shlibp=".a";
280       $lib_flags="-cr";
281    }
282    else       # CodeWarrior
283    {
284       $libp=".lib";
285       $shlibp=".lib";
286       $lib_flags="-nodefaults -type library -o";
287    }
288    if ($LIBC)
289    {
290       $out_def.="_nw_libc";
291       $tmp_def.="_nw_libc";
292       $inc_def.="_nw_libc";
293    }
294    else  # NETWARE_CLIB
295    {
296       $out_def.="_nw_clib";
297       $tmp_def.="_nw_clib";
298       $inc_def.="_nw_clib";
299    }
300 }
301
302 # used by mk1mf.pl
303 $obj='.o';
304 $ofile='-o ';
305 $efile='';
306 $exep='.nlm';
307 $ex_libs='';
308
309 if (!$no_asm)
310 {
311    $bn_asm_obj="\$(OBJ_D)${o}bn-nw${obj}";
312    $bn_asm_src="crypto${o}bn${o}asm${o}bn-nw.asm";
313    $bnco_asm_obj="\$(OBJ_D)${o}co-nw${obj}";
314    $bnco_asm_src="crypto${o}bn${o}asm${o}co-nw.asm";
315    $aes_asm_obj="\$(OBJ_D)${o}a-nw${obj}";
316    $aes_asm_src="crypto${o}aes${o}asm${o}a-nw.asm";
317    $des_enc_obj="\$(OBJ_D)${o}d-nw${obj} \$(OBJ_D)${o}y-nw${obj}";
318    $des_enc_src="crypto${o}des${o}asm${o}d-nw.asm crypto${o}des${o}asm${o}y-nw.asm";
319    $bf_enc_obj="\$(OBJ_D)${o}b-nw${obj}";
320    $bf_enc_src="crypto${o}bf${o}asm${o}b-nw.asm";
321    $cast_enc_obj="\$(OBJ_D)${o}c-nw${obj}";
322    $cast_enc_src="crypto${o}cast${o}asm${o}c-nw.asm";
323    $rc4_enc_obj="\$(OBJ_D)${o}r4-nw${obj}";
324    $rc4_enc_src="crypto${o}rc4${o}asm${o}r4-nw.asm";
325    $rc5_enc_obj="\$(OBJ_D)${o}r5-nw${obj}";
326    $rc5_enc_src="crypto${o}rc5${o}asm${o}r5-nw.asm";
327    $md5_asm_obj="\$(OBJ_D)${o}m5-nw${obj}";
328    $md5_asm_src="crypto${o}md5${o}asm${o}m5-nw.asm";
329    $sha1_asm_obj="\$(OBJ_D)${o}s1-nw${obj} \$(OBJ_D)${o}sha256-nw${obj} \$(OBJ_D)${o}sha512-nw${obj}";
330    $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";
331    $rmd160_asm_obj="\$(OBJ_D)${o}rm-nw${obj}";
332    $rmd160_asm_src="crypto${o}ripemd${o}asm${o}rm-nw.asm";
333    $whirlpool_asm_obj="\$(OBJ_D)${o}wp-nw${obj}";
334    $whirlpool_asm_src="crypto${o}whrlpool${o}asm${o}wp-nw.asm";
335    $cpuid_asm_obj="\$(OBJ_D)${o}x86cpuid-nw${obj}";
336    $cpuid_asm_src="crypto${o}x86cpuid-nw.asm";
337    $cflags.=" -DOPENSSL_CPUID_OBJ -DBN_ASM -DOPENSSL_BN_ASM_PART_WORDS -DMD5_ASM -DWHIRLPOOL_ASM";
338    $cflags.=" -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM";
339    $cflags.=" -DAES_ASM -DRMD160_ASM";
340 }
341 else
342 {
343    $bn_asm_obj='';
344    $bn_asm_src='';
345    $bnco_asm_obj='';
346    $bnco_asm_src='';
347    $aes_asm_obj='';
348    $aes_asm_src='';
349    $des_enc_obj='';
350    $des_enc_src='';
351    $bf_enc_obj='';
352    $bf_enc_src='';
353    $cast_enc_obj='';
354    $cast_enc_src='';
355    $rc4_enc_obj='';
356    $rc4_enc_src='';
357    $rc5_enc_obj='';
358    $rc5_enc_src='';
359    $md5_asm_obj='';
360    $md5_asm_src='';
361    $sha1_asm_obj='';
362    $sha1_asm_src='';
363    $rmd160_asm_obj='';
364    $rmd160_asm_src='';
365    $whirlpool_asm_obj='';
366    $whirlpool_asm_src='';
367    $cpuid_asm_obj='';
368    $cpuid_asm_src='';
369 }
370
371 # create the *.def linker command files in \openssl\netware\ directory
372 sub do_def_file
373 {
374    # strip off the leading path
375    my($target) = bname(shift);
376    my($i);
377
378    if ($target =~ /(.*).nlm/)
379    {
380       $target = $1;
381    }
382
383    # special case for openssl - the mk1mf.pl defines E_EXE = openssl
384    if ($target =~ /E_EXE/)
385    {
386       $target =~ s/\$\(E_EXE\)/openssl/;
387    }
388
389    # Note: originally tried to use full path ( \openssl\netware\$target.def )
390    # Metrowerks linker choked on this with an assertion failure. bug???
391    #
392    my($def_file) = "netware${o}$target.def";
393
394    open(DEF_OUT, ">$def_file") || die("unable to open file $def_file\n");
395
396    print( DEF_OUT "# command file generated by netware.pl for NLM target.\n" );
397    print( DEF_OUT "# do not edit this file - all your changes will be lost!!\n" );
398    print( DEF_OUT "#\n");
399    print( DEF_OUT "DESCRIPTION \"$target ($libarch) - OpenSSL $nlmverstr\"\n");
400    print( DEF_OUT "COPYRIGHT \"$nlmcpystr\"\n");
401    print( DEF_OUT "VERSION $nlmvernum\n");
402    print( DEF_OUT "STACK $nlmstack\n");
403    print( DEF_OUT "START $nlmstart\n");
404    print( DEF_OUT "EXIT $nlmexit\n");
405
406    # special case for openssl
407    if ($target eq "openssl")
408    {
409       print( DEF_OUT "SCREENNAME \"OpenSSL $nlmverstr\"\n");
410    }
411    else
412    {
413       print( DEF_OUT "SCREENNAME \"DEFAULT\"\n");
414    }
415
416    foreach $i (@misc_imports)
417    {
418       print( DEF_OUT "IMPORT $i\n");
419    }
420
421    foreach $i (@import_files)
422    {
423       print( DEF_OUT "IMPORT \@$import_path${o}$i\n");
424    }
425
426    foreach $i (@module_files)
427    {
428       print( DEF_OUT "MODULE $i\n");
429    }
430
431    foreach $i (@nlm_flags)
432    {
433       print( DEF_OUT "$i\n");
434    }
435
436    if ($gnuc)
437    {
438       if ($target =~ /openssl/)
439       {
440          print( DEF_OUT "INPUT ${tmp_def}${o}openssl${obj}\n");
441          print( DEF_OUT "INPUT ${tmp_def}${o}openssl${libp}\n");
442       }
443       else
444       {
445          print( DEF_OUT "INPUT ${tmp_def}${o}${target}${obj}\n");
446       }
447       print( DEF_OUT "INPUT $prelude\n");
448       print( DEF_OUT "INPUT ${out_def}${o}${ssl}${libp} ${out_def}${o}${crypto}${libp}\n");
449       print( DEF_OUT "OUTPUT $target.nlm\n");
450    }
451
452    close(DEF_OUT);
453    return($def_file);
454 }
455
456 sub do_lib_rule
457 {
458    my($objs,$target,$name,$shlib)=@_;
459    my($ret);
460
461    $ret.="$target: $objs\n";
462    if (!$shlib)
463    {
464       $ret.="\t\@echo Building Lib: $name\n";
465       $ret.="\t\$(MKLIB) $lib_flags $target $objs\n";
466       $ret.="\t\@echo .\n"
467    }
468    else
469    {
470       die( "Building as NLM not currently supported!" );
471    }
472
473    $ret.="\n";
474    return($ret);
475 }
476
477 sub do_link_rule
478 {
479    my($target,$files,$dep_libs,$libs)=@_;
480    my($ret);
481    my($def_file) = do_def_file($target);
482
483    $ret.="$target: $files $dep_libs\n";
484
485    # NOTE:  When building the test nlms no screen name is given
486    #  which causes the console screen to be used.  By using the console
487    #  screen there is no "<press any key to continue>" message which
488    #  requires user interaction.  The test script ( do_tests.pl ) needs
489    #  to be able to run the tests without requiring user interaction.
490    #
491    #  However, the sample program "openssl.nlm" is used by the tests and is
492    #  a interactive sample so a screen is desired when not be run by the
493    #  tests.  To solve the problem, two versions of the program are built:
494    #    openssl2 - no screen used by tests
495    #    openssl - default screen - use for normal interactive modes
496    #
497
498    # special case for openssl - the mk1mf.pl defines E_EXE = openssl
499    if ($target =~ /E_EXE/)
500    {
501       my($target2) = $target;
502
503       $target2 =~ s/\(E_EXE\)/\(E_EXE\)2/;
504
505       # openssl2
506       my($def_file2) = do_def_file($target2);
507
508       if ($gnuc)
509       {
510          $ret.="\t\$(MKLIB) $lib_flags \$(TMP_D)${o}\$(E_EXE).a \$(filter-out \$(TMP_D)${o}\$(E_EXE)${obj},$files)\n";
511          $ret.="\t\$(LINK_CMD) \$(LFLAGS) $def_file2\n";
512          $ret.="\t\@$mv \$(E_EXE)2.nlm \$(TEST_D)\n";
513       }
514       else
515       {
516          $ret.="\t\$(LINK_CMD) \$(LFLAGS) $def_file2 $files \"$prelude\" $libs -o $target2\n";
517       }
518    }
519    if ($gnuc)
520    {
521       $ret.="\t\$(LINK_CMD) \$(LFLAGS) $def_file\n";
522       $ret.="\t\@$mv \$(\@F) \$(TEST_D)\n";
523    }
524    else
525    {
526       $ret.="\t\$(LINK_CMD) \$(LFLAGS) $def_file $files \"$prelude\" $libs -o $target\n";
527    }
528
529    $ret.="\n";
530    return($ret);
531
532 }
533
534 1;