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