Fix various stuff: that VC++ 5.0 chokes on:
[openssl.git] / util / mkdef.pl
1 #!/usr/local/bin/perl
2 #
3 # generate a .def file
4 #
5 # It does this by parsing the header files and looking for the
6 # non-prototyped functions.
7 #
8
9 $crypto_num="util/libeay.num";
10 $ssl_num=   "util/ssleay.num";
11
12 $NT=1;
13 foreach (@ARGV)
14         {
15         $NT=1 if $_ eq "32";
16         $NT=0 if $_ eq "16";
17         $do_ssl=1 if $_ eq "ssleay";
18         $do_crypto=1 if $_ eq "libeay";
19         }
20
21 if (!$do_ssl && !$do_crypto)
22         {
23         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 ]\n";
24         exit(1);
25         }
26
27 %ssl_list=&load_numbers($ssl_num);
28 %crypto_list=&load_numbers($crypto_num);
29
30 $ssl="ssl/ssl.h";
31
32 $crypto ="crypto/crypto.h";
33 $crypto.=" crypto/des/des.h";
34 $crypto.=" crypto/idea/idea.h";
35 $crypto.=" crypto/rc4/rc4.h";
36 $crypto.=" crypto/rc5/rc5.h";
37 $crypto.=" crypto/rc2/rc2.h";
38 $crypto.=" crypto/bf/blowfish.h";
39 $crypto.=" crypto/cast/cast.h";
40 $crypto.=" crypto/md2/md2.h";
41 $crypto.=" crypto/md5/md5.h";
42 $crypto.=" crypto/mdc2/mdc2.h";
43 $crypto.=" crypto/sha/sha.h";
44 $crypto.=" crypto/ripemd/ripemd.h";
45
46 $crypto.=" crypto/bn/bn.h";
47 $crypto.=" crypto/rsa/rsa.h";
48 $crypto.=" crypto/dsa/dsa.h";
49 $crypto.=" crypto/dh/dh.h";
50
51 $crypto.=" crypto/stack/stack.h";
52 $crypto.=" crypto/buffer/buffer.h";
53 $crypto.=" crypto/bio/bio.h";
54 $crypto.=" crypto/lhash/lhash.h";
55 $crypto.=" crypto/conf/conf.h";
56 $crypto.=" crypto/txt_db/txt_db.h";
57
58 $crypto.=" crypto/evp/evp.h";
59 $crypto.=" crypto/objects/objects.h";
60 $crypto.=" crypto/pem/pem.h";
61 #$crypto.=" crypto/meth/meth.h";
62 $crypto.=" crypto/asn1/asn1.h";
63 $crypto.=" crypto/asn1/asn1_mac.h";
64 $crypto.=" crypto/err/err.h";
65 $crypto.=" crypto/pkcs7/pkcs7.h";
66 $crypto.=" crypto/x509/x509.h";
67 $crypto.=" crypto/x509/x509_vfy.h";
68 $crypto.=" crypto/x509v3/x509v3.h";
69 $crypto.=" crypto/rand/rand.h";
70 $crypto.=" crypto/hmac/hmac.h";
71 $crypto.=" crypto/comp/comp.h";
72 $crypto.=" crypto/tmdiff.h";
73
74 $match{'NOPROTO'}=1;
75 $match2{'PERL5'}=1;
76
77 &print_def_file(*STDOUT,"SSLEAY",*ssl_list,&do_defs("SSLEAY",$ssl))
78         if $do_ssl == 1;
79
80 &print_def_file(*STDOUT,"LIBEAY",*crypto_list,&do_defs("LIBEAY",$crypto))
81         if $do_crypto == 1;
82
83 sub do_defs
84         {
85         local($name,$files)=@_;
86         local(@ret);
87
88         $off=-1;
89         foreach $file (split(/\s+/,$files))
90                 {
91 #               print STDERR "reading $file\n";
92                 open(IN,"<$file") || die "unable to open $file:$!\n";
93                 $depth=0;
94                 $pr=-1;
95                 @np="";
96                 $/=undef;
97                 $a=<IN>;
98                 while (($i=index($a,"/*")) >= 0)
99                         {
100                         $j=index($a,"*/");
101                         break unless ($j >= 0);
102                         $a=substr($a,0,$i).substr($a,$j+2);
103                 #       print "$i $j\n";
104                         }
105                 foreach (split("\n",$a))
106                         {
107                         if (/^\#\s*ifndef (.*)/)
108                                 {
109                                 push(@tag,$1);
110                                 $tag{$1}=-1;
111                                 next;
112                                 }
113                         elsif (/^\#\s*if !defined\(([^\)]+)\)/)
114                                 {
115                                 push(@tag,$1);
116                                 $tag{$1}=-1;
117                                 next;
118                                 }
119                         elsif (/^\#\s*ifdef (.*)/)
120                                 {
121                                 push(@tag,$1);
122                                 $tag{$1}=1;
123                                 next;
124                                 }
125                         elsif (/^\#\s*if defined(.*)/)
126                                 {
127                                 push(@tag,$1);
128                                 $tag{$1}=1;
129                                 next;
130                                 }
131                         elsif (/^\#\s*endif/)
132                                 {
133                                 $tag{$tag[$#tag]}=0;
134                                 pop(@tag);
135                                 next;
136                                 }
137                         elsif (/^\#\s*else/)
138                                 {
139                                 $t=$tag[$#tag];
140                                 $tag{$t}= -$tag{$t};
141                                 next;
142                                 }
143 #printf STDERR "$_\n%2d %2d %2d %2d %2d $NT\n",
144 #$tag{'NOPROTO'},$tag{'FreeBSD'},$tag{'WIN16'},$tag{'PERL5'},$tag{'NO_FP_API'};
145
146                         $t=undef;
147                         if (/^extern .*;$/)
148                                 { $t=&do_extern($name,$_); }
149                         elsif ( ($tag{'NOPROTO'} == 1) &&
150                                 ($tag{'FreeBSD'} != 1) &&
151                                 (($NT && ($tag{'WIN16'} != 1)) ||
152                                  (!$NT && ($tag{'WIN16'} != -1))) &&
153                                 ($tag{'PERL5'} != 1) &&
154 #                               ($tag{'_WINDLL'} != -1) &&
155                                 ((!$NT && $tag{'_WINDLL'} != -1) ||
156                                  ($NT && $tag{'_WINDLL'} != 1)) &&
157                                 ((($tag{'NO_FP_API'} != 1) && $NT) ||
158                                  (($tag{'NO_FP_API'} != -1) && !$NT)))
159                                 { $t=&do_line($name,$_); }
160                         else
161                                 { $t=undef; }
162                         if (($t ne undef) && (!$done{$name,$t}))
163                                 {
164                                 $done{$name,$t}++;
165                                 push(@ret,$t);
166 #printf STDERR "one:$t\n" if $t =~ /BIO_/;
167                                 }
168                         }
169                 close(IN);
170                 }
171         return(@ret);
172         }
173
174 sub do_line
175         {
176         local($file,$_)=@_;
177         local($n);
178
179         return(undef) if /^$/;
180         return(undef) if /^\s/;
181 #printf STDERR "two:$_\n" if $_ =~ /BIO_/;
182         if (/(CRYPTO_get_locking_callback)/)
183                 { return($1); }
184         elsif (/(CRYPTO_get_id_callback)/)
185                 { return($1); }
186         elsif (/(CRYPTO_get_add_lock_callback)/)
187                 { return($1); }
188         elsif (/(SSL_CTX_get_verify_callback)/)
189                 { return($1); }
190         elsif (/(SSL_get_info_callback)/)
191                 { return($1); }
192         elsif ((!$NT) && /(ERR_load_CRYPTO_strings)/)
193                 { return("ERR_load_CRYPTOlib_strings"); }
194         elsif (!$NT && /BIO_s_file/)
195                 { return(undef); }
196         elsif (!$NT && /BIO_new_file/)
197                 { return(undef); }
198         elsif (!$NT && /BIO_new_fp/)
199                 { return(undef); }
200         elsif ($NT && /BIO_s_file_internal/)
201                 { return(undef); }
202         elsif ($NT && /BIO_new_file_internal/)
203                 { return(undef); }
204         elsif ($NT && /BIO_new_fp_internal/)
205                 { return(undef); }
206         else
207                 {
208                 /\s\**(\S+)\s*\(/;
209                 return($1);
210                 }
211         }
212
213 sub do_extern
214         {
215         local($file,$_)=@_;
216         local($n);
217
218         /\s\**(\S+);$/;
219         return($1);
220         }
221
222 sub print_def_file
223         {
224         local(*OUT,$name,*nums,@functions)=@_;
225         local($n)=1;
226
227         if ($NT)
228                 { $name.="32"; }
229         else
230                 { $name.="16"; }
231
232         print OUT <<"EOF";
233 ;
234 ; Definition file for the DLL version of the $name library from OpenSSL
235 ;
236
237 LIBRARY         $name
238
239 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
240
241 EOF
242
243         if (!$NT)
244                 {
245                 print <<"EOF";
246 CODE            PRELOAD MOVEABLE
247 DATA            PRELOAD MOVEABLE SINGLE
248
249 EXETYPE         WINDOWS
250
251 HEAPSIZE        4096
252 STACKSIZE       8192
253
254 EOF
255                 }
256
257         print "EXPORTS\n";
258
259
260         (@e)=grep(/^SSLeay/,@functions);
261         (@r)=grep(!/^SSLeay/,@functions);
262         @functions=((sort @e),(sort @r));
263
264         foreach $func (@functions)
265                 {
266                 if (!defined($nums{$func}))
267                         {
268                         printf STDERR "$func does not have a number assigned\n";
269                         }
270                 else
271                         {
272                         $n=$nums{$func};
273                         printf OUT "    %s%-40s@%d\n",($NT)?"":"_",$func,$n;
274                         }
275                 }
276         printf OUT "\n";
277         }
278
279 sub load_numbers
280         {
281         local($name)=@_;
282         local($j,@a,%ret);
283
284         open(IN,"<$name") || die "unable to open $name:$!\n";
285         while (<IN>)
286                 {
287                 chop;
288                 s/#.*$//;
289                 next if /^\s*$/;
290                 @a=split;
291                 $ret{$a[0]}=$a[1];
292                 }
293         close(IN);
294         return(%ret);
295         }