mk1mf.pl and mkdef.pl read OPTIONS from toplevel Makefile.
[openssl.git] / util / mkdef.pl
1 #!/usr/my/bin/perl -w
2 #
3 # generate a .def file
4 #
5 # It does this by parsing the header files and looking for the
6 # prototyped functions: it then prunes the output.
7 #
8
9 $crypto_num="util/libeay.num";
10 $ssl_num=   "util/ssleay.num";
11
12 my $do_update = 0;
13 my $do_crypto = 0;
14 my $do_ssl = 0;
15 $rsaref = 0;
16
17 $W32=1;
18 $NT=0;
19 # Set this to make typesafe STACK definitions appear in DEF
20 $safe_stack_def = 1;
21
22 $options="";
23 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
24 while(<IN>) {
25     $options=$1 if (/^OPTIONS=(.*)$/);
26 }
27
28 foreach (@ARGV, split(/ /, $options))
29         {
30         $W32=1 if $_ eq "32";
31         $W32=0 if $_ eq "16";
32         if($_ eq "NT") {
33                 $W32 = 1;
34                 $NT = 1;
35         }
36         $do_ssl=1 if $_ eq "ssleay";
37         $do_ssl=1 if $_ eq "ssl";
38         $do_crypto=1 if $_ eq "libeay";
39         $do_crypto=1 if $_ eq "crypto";
40         $do_update=1 if $_ eq "update";
41         $rsaref=1 if $_ eq "rsaref";
42
43         if    (/^no-rc2$/)      { $no_rc2=1; }
44         elsif (/^no-rc4$/)      { $no_rc4=1; }
45         elsif (/^no-rc5$/)      { $no_rc5=1; }
46         elsif (/^no-idea$/)     { $no_idea=1; }
47         elsif (/^no-des$/)      { $no_des=1; }
48         elsif (/^no-bf$/)       { $no_bf=1; }
49         elsif (/^no-cast$/)     { $no_cast=1; }
50         elsif (/^no-md2$/)      { $no_md2=1; }
51         elsif (/^no-md5$/)      { $no_md5=1; }
52         elsif (/^no-sha$/)      { $no_sha=1; }
53         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
54         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
55         elsif (/^no-rsa$/)      { $no_rsa=1; }
56         elsif (/^no-dsa$/)      { $no_dsa=1; }
57         elsif (/^no-dh$/)       { $no_dh=1; }
58         elsif (/^no-hmac$/)     { $no_hmac=1; }
59         }
60
61 if (!$do_ssl && !$do_crypto)
62         {
63         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ] [rsaref]\n";
64         exit(1);
65         }
66
67 %ssl_list=&load_numbers($ssl_num);
68 $max_ssl = $max_num;
69 %crypto_list=&load_numbers($crypto_num);
70 $max_crypto = $max_num;
71
72 $ssl="ssl/ssl.h";
73
74 $crypto ="crypto/crypto.h";
75 $crypto.=" crypto/des/des.h" unless $no_des;
76 $crypto.=" crypto/idea/idea.h" unless $no_idea;
77 $crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
78 $crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
79 $crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
80 $crypto.=" crypto/bf/blowfish.h" unless $no_bf;
81 $crypto.=" crypto/cast/cast.h" unless $no_cast;
82 $crypto.=" crypto/md2/md2.h" unless $no_md2;
83 $crypto.=" crypto/md5/md5.h" unless $no_md5;
84 $crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
85 $crypto.=" crypto/sha/sha.h" unless $no_sha;
86 $crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
87
88 $crypto.=" crypto/bn/bn.h";
89 $crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
90 $crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
91 $crypto.=" crypto/dh/dh.h" unless $no_dh;
92 $crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
93
94 $crypto.=" crypto/stack/stack.h";
95 $crypto.=" crypto/buffer/buffer.h";
96 $crypto.=" crypto/bio/bio.h";
97 $crypto.=" crypto/lhash/lhash.h";
98 $crypto.=" crypto/conf/conf.h";
99 $crypto.=" crypto/txt_db/txt_db.h";
100
101 $crypto.=" crypto/evp/evp.h";
102 $crypto.=" crypto/objects/objects.h";
103 $crypto.=" crypto/pem/pem.h";
104 #$crypto.=" crypto/meth/meth.h";
105 $crypto.=" crypto/asn1/asn1.h";
106 $crypto.=" crypto/asn1/asn1_mac.h";
107 $crypto.=" crypto/err/err.h";
108 $crypto.=" crypto/pkcs7/pkcs7.h";
109 $crypto.=" crypto/pkcs12/pkcs12.h";
110 $crypto.=" crypto/x509/x509.h";
111 $crypto.=" crypto/x509/x509_vfy.h";
112 $crypto.=" crypto/x509v3/x509v3.h";
113 $crypto.=" crypto/rand/rand.h";
114 $crypto.=" crypto/comp/comp.h";
115 $crypto.=" crypto/tmdiff.h";
116
117 @ssl_func = &do_defs("SSLEAY", $ssl);
118 @crypto_func = &do_defs("LIBEAY", $crypto);
119
120
121 if ($do_update) {
122
123 if ($do_ssl == 1) {
124         open(OUT, ">>$ssl_num");
125         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl, @ssl_func);
126         close OUT;
127 }
128
129 if($do_crypto == 1) {
130         open(OUT, ">>$crypto_num");
131         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto, @crypto_func);
132         close OUT;
133 }
134
135 } else {
136
137         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
138                 if $do_ssl == 1;
139
140         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
141                 if $do_crypto == 1;
142
143 }
144
145
146 sub do_defs
147 {
148         my($name,$files)=@_;
149         my @ret;
150         my %funcs;
151
152         foreach $file (split(/\s+/,$files))
153                 {
154                 open(IN,"<$file") || die "unable to open $file:$!\n";
155
156                 my $line = "", $def= "";
157                 my %tag = (
158                         FreeBSD         => 0,
159                         NOPROTO         => 0,
160                         WIN16           => 0,
161                         PERL5           => 0,
162                         _WINDLL         => 0,
163                         NO_FP_API       => 0,
164                         CONST_STRICT    => 0,
165                         TRUE            => 1,
166                 );
167                 while(<IN>) {
168                         last if (/BEGIN ERROR CODES/);
169                         if ($line ne '') {
170                                 $_ = $line . $_;
171                                 $line = '';
172                         }
173
174                         if (/\\$/) {
175                                 $line = $_;
176                                 next;
177                         }
178
179                         $cpp = 1 if /^#.*ifdef.*cplusplus/;
180                         if ($cpp) {
181                                 $cpp = 0 if /^#.*endif/;
182                                 next;
183                         }
184
185                         s/\/\*.*?\*\///gs;                   # ignore comments
186                         s/{[^{}]*}//gs;                      # ignore {} blocks
187                         if (/^\#\s*ifndef (.*)/) {
188                                 push(@tag,$1);
189                                 $tag{$1}=-1;
190                                 next;
191                         } elsif (/^\#\s*if !defined\(([^\)]+)\)/) {
192                                 push(@tag,$1);
193                                 $tag{$1}=-1;
194                                 next;
195                         } elsif (/^\#\s*ifdef (.*)/) {
196                                 push(@tag,$1);
197                                 $tag{$1}=1;
198                                 next;
199                         } elsif (/^\#\s*if defined(.*)/) {
200                                 push(@tag,$1);
201                                 $tag{$1}=1;
202                                 next;
203                         } elsif (/^\#\s*endif/) {
204                                 $tag{$tag[$#tag]}=0;
205                                 pop(@tag);
206                                 next;
207                         } elsif (/^\#\s*else/) {
208                                 my $t=$tag[$#tag];
209                                 $tag{$t}= -$tag{$t};
210                                 next;
211                         } elsif (/^\#\s*if\s+1/) {
212                                 # Dummy tag
213                                 push(@tag,"TRUE");
214                                 $tag{"TRUE"}=1;
215                                 next;
216                         } elsif (/^\#/) {
217                                 next;
218                         }
219                         if ($safe_stack_def &&
220                                 /^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
221                                 $funcs{"sk_${1}_new"} = 1;
222                                 $funcs{"sk_${1}_new_null"} = 1;
223                                 $funcs{"sk_${1}_free"} = 1;
224                                 $funcs{"sk_${1}_num"} = 1;
225                                 $funcs{"sk_${1}_value"} = 1;
226                                 $funcs{"sk_${1}_set"} = 1;
227                                 $funcs{"sk_${1}_zero"} = 1;
228                                 $funcs{"sk_${1}_push"} = 1;
229                                 $funcs{"sk_${1}_unshift"} = 1;
230                                 $funcs{"sk_${1}_find"} = 1;
231                                 $funcs{"sk_${1}_delete"} = 1;
232                                 $funcs{"sk_${1}_delete_ptr"} = 1;
233                                 $funcs{"sk_${1}_insert"} = 1;
234                                 $funcs{"sk_${1}_set_cmp_func"} = 1;
235                                 $funcs{"sk_${1}_dup"} = 1;
236                                 $funcs{"sk_${1}_pop_free"} = 1;
237                                 $funcs{"sk_${1}_shift"} = 1;
238                                 $funcs{"sk_${1}_pop"} = 1;
239                         } elsif ($safe_stack_def &&
240                                 /^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
241                                 $funcs{"d2i_ASN1_SET_OF_${1}"} = 1;
242                                 $funcs{"i2d_ASN1_SET_OF_${1}"} = 1;
243                         } elsif ( 
244                                 ($tag{'FreeBSD'} != 1) &&
245                                 ($tag{'CONST_STRICT'} != 1) &&
246                                 (($W32 && ($tag{'WIN16'} != 1)) ||
247                                  (!$W32 && ($tag{'WIN16'} != -1))) &&
248                                 ($tag{'PERL5'} != 1) &&
249 #                               ($tag{'_WINDLL'} != -1) &&
250                                 ((!$W32 && $tag{'_WINDLL'} != -1) ||
251                                  ($W32 && $tag{'_WINDLL'} != 1)) &&
252                                 ((($tag{'NO_FP_API'} != 1) && $W32) ||
253                                  (($tag{'NO_FP_API'} != -1) && !$W32)))
254                                 {
255                                         if (/{|\/\*/) { # }
256                                                 $line = $_;
257                                         } else {
258                                                 $def .= $_;
259                                         }
260                                 }
261                         }
262                 close(IN);
263
264                 foreach (split /;/, $def) {
265                         s/^[\n\s]*//g;
266                         s/[\n\s]*$//g;
267                         next if(/typedef\W/);
268                         next if(/EVP_bf/ and $no_bf);
269                         next if(/EVP_cast/ and $no_cast);
270                         next if(/EVP_des/ and $no_des);
271                         next if(/EVP_dss/ and $no_dsa);
272                         next if(/EVP_idea/ and $no_idea);
273                         next if(/EVP_md2/ and $no_md2);
274                         next if(/EVP_md5/ and $no_md5);
275                         next if(/EVP_rc2/ and $no_rc2);
276                         next if(/EVP_rc4/ and $no_rc4);
277                         next if(/EVP_rc5/ and $no_rc5);
278                         next if(/EVP_ripemd/ and $no_ripemd);
279                         next if(/EVP_sha/ and $no_sha);
280                         if (/\(\*(\w*)\([^\)]+/) {
281                                 $funcs{$1} = 1;
282                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
283                                 # K&R C
284                                 next;
285                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
286                                 while (not /\(\)$/s) {
287                                         s/[^\(\)]*\)$/\)/s;
288                                         s/\([^\(\)]*\)\)$/\)/s;
289                                 }
290                                 s/\(void\)//;
291                                 /(\w+)\W*\(\)/s;
292                                 $funcs{$1} = 1;
293                         } elsif (/\(/ and not (/=/)) {
294                                 print STDERR "File $file: cannot parse: $_;\n";
295                         }
296                 }
297         }
298
299         # Prune the returned functions
300
301         delete $funcs{"SSL_add_dir_cert_subjects_to_stack"};
302         delete $funcs{"des_crypt"};
303         delete $funcs{"RSA_PKCS1_RSAref"} unless $rsaref;
304
305         if($W32) {
306                 delete $funcs{"BIO_s_file_internal"};
307                 delete $funcs{"BIO_new_file_internal"};
308                 delete $funcs{"BIO_new_fp_internal"};
309         } else {
310                 if(exists $funcs{"ERR_load_CRYPTO_strings"}) {
311                         delete $funcs{"ERR_load_CRYPTO_strings"};
312                         $funcs{"ERR_load_CRYPTOlib_strings"} = 1;
313                 }
314                 delete $funcs{"BIO_s_file"};
315                 delete $funcs{"BIO_new_file"};
316                 delete $funcs{"BIO_new_fp"};
317         }
318         if (!$NT) {
319                 delete $funcs{"BIO_s_log"};
320         }
321
322         push @ret, keys %funcs;
323
324         return(@ret);
325 }
326
327 sub print_def_file
328 {
329         (*OUT,my $name,*nums,@functions)=@_;
330         my $n =1;
331
332         if ($W32)
333                 { $name.="32"; }
334         else
335                 { $name.="16"; }
336
337         print OUT <<"EOF";
338 ;
339 ; Definition file for the DLL version of the $name library from OpenSSL
340 ;
341
342 LIBRARY         $name
343
344 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
345
346 EOF
347
348         if (!$W32) {
349                 print <<"EOF";
350 CODE            PRELOAD MOVEABLE
351 DATA            PRELOAD MOVEABLE SINGLE
352
353 EXETYPE         WINDOWS
354
355 HEAPSIZE        4096
356 STACKSIZE       8192
357
358 EOF
359         }
360
361         print "EXPORTS\n";
362
363
364         (@e)=grep(/^SSLeay/,@functions);
365         (@r)=grep(!/^SSLeay/,@functions);
366         @functions=((sort @e),(sort @r));
367
368         foreach $func (@functions) {
369                 if (!defined($nums{$func})) {
370                         printf STDERR "$func does not have a number assigned\n"
371                                         if(!$do_update);
372                 } else {
373                         $n=$nums{$func};
374                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$func,$n;
375                 }
376         }
377         printf OUT "\n";
378 }
379
380 sub load_numbers
381 {
382         my($name)=@_;
383         my(@a,%ret);
384
385         $max_num = 0;
386
387         open(IN,"<$name") || die "unable to open $name:$!\n";
388         while (<IN>) {
389                 chop;
390                 s/#.*$//;
391                 next if /^\s*$/;
392                 @a=split;
393                 $ret{$a[0]}=$a[1];
394                 $max_num = $a[1] if $a[1] > $max_num;
395         }
396         close(IN);
397         return(%ret);
398 }
399
400 sub update_numbers
401 {
402         (*OUT,$name,*nums,my $start_num, my @functions)=@_;
403         my $new_funcs = 0;
404         print STDERR "Updating $name\n";
405         foreach $func (@functions) {
406                 if (!exists $nums{$func}) {
407                         $new_funcs++;
408                         printf OUT "%s%-40s%d\n","",$func, ++$start_num;
409                 }
410         }
411         if($new_funcs) {
412                 print STDERR "$new_funcs New Functions added\n";
413         } else {
414                 print STDERR "No New Functions Added\n";
415         }
416 }