This commit was generated by cvs2svn to track changes on a CVS vendor
[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/rc2/rc2.h";
37 $crypto.=" crypto/bf/blowfish.h";
38 $crypto.=" crypto/md/md2.h";
39 $crypto.=" crypto/md/md5.h";
40 $crypto.=" crypto/mdc2/mdc2.h";
41 $crypto.=" crypto/sha/sha.h";
42
43 $crypto.=" crypto/bn/bn.h";
44 $crypto.=" crypto/rsa/rsa.h";
45 $crypto.=" crypto/dsa/dsa.h";
46 $crypto.=" crypto/dh/dh.h";
47
48 $crypto.=" crypto/stack/stack.h";
49 $crypto.=" crypto/buffer/buffer.h";
50 $crypto.=" crypto/bio/bio.h";
51 $crypto.=" crypto/lhash/lhash.h";
52 $crypto.=" crypto/conf/conf.h";
53 $crypto.=" crypto/txt_db/txt_db.h";
54
55 $crypto.=" crypto/evp/evp.h";
56 $crypto.=" crypto/objects/objects.h";
57 $crypto.=" crypto/pem/pem.h";
58 #$crypto.=" crypto/meth/meth.h";
59 $crypto.=" crypto/asn1/asn1.h";
60 $crypto.=" crypto/asn1/asn1_mac.h";
61 $crypto.=" crypto/err/err.h";
62 $crypto.=" crypto/pkcs7/pkcs7.h";
63 $crypto.=" crypto/x509/x509.h";
64 $crypto.=" crypto/x509/x509_vfy.h";
65 $crypto.=" crypto/rand/rand.h";
66
67 $match{'NOPROTO'}=1;
68 $match2{'PERL5'}=1;
69
70 &print_def_file(*STDOUT,"SSLEAY",*ssl_list,&do_defs("SSLEAY",$ssl))
71         if $do_ssl == 1;
72
73 &print_def_file(*STDOUT,"LIBEAY",*crypto_list,&do_defs("LIBEAY",$crypto))
74         if $do_crypto == 1;
75
76 sub do_defs
77         {
78         local($name,$files)=@_;
79         local(@ret);
80
81         $off=-1;
82         foreach $file (split(/\s+/,$files))
83                 {
84 #               print STDERR "reading $file\n";
85                 open(IN,"<$file") || die "unable to open $file:$!\n";
86                 $depth=0;
87                 $pr=-1;
88                 @np="";
89                 $/=undef;
90                 $a=<IN>;
91                 while (($i=index($a,"/*")) >= 0)
92                         {
93                         $j=index($a,"*/");
94                         break unless ($j >= 0);
95                         $a=substr($a,0,$i).substr($a,$j+2);
96                 #       print "$i $j\n";
97                         }
98                 foreach (split("\n",$a))
99                         {
100                         if (/^\#ifndef (.*)/)
101                                 {
102                                 push(@tag,$1);
103                                 $tag{$1}=-1;
104                                 next;
105                                 }
106                         elsif (/^\#if !defined\(([^\)]+)\)/)
107                                 {
108                                 push(@tag,$1);
109                                 $tag{$1}=-1;
110                                 next;
111                                 }
112                         elsif (/^\#ifdef (.*)/)
113                                 {
114                                 push(@tag,$1);
115                                 $tag{$1}=1;
116                                 next;
117                                 }
118                         elsif (/^\#if (.*)/)
119                                 {
120                                 push(@tag,$1);
121                                 $tag{$1}=1;
122                                 next;
123                                 }
124                         elsif (/^\#endif/)
125                                 {
126                                 $tag{$tag[$#tag]}=0;
127                                 pop(@tag);
128                                 next;
129                                 }
130                         elsif (/^\#else/)
131                                 {
132                                 $t=$tag[$#tag];
133                                 $tag{$t}= -$tag{$t};
134                                 next;
135                                 }
136                         $t=undef;
137                         if (/^extern .*;$/)
138                                 { $t=&do_extern($name,$_); }
139                         elsif ( ($tag{'NOPROTO'} == 1) &&
140                                 ($tag{'FreeBSD'} != 1) &&
141                                 (($NT && ($tag{'WIN16'} != 1)) ||
142                                  (!$NT && ($tag{'WIN16'} != -1))) &&
143                                 ($tag{'PERL5'} != 1))
144                                 { $t=&do_line($name,$_); }
145                         if (($t ne undef) && (!$done{$name,$t}))
146                                 {
147                                 $done{$name,$t}++;
148                                 push(@ret,$t);
149                                 }
150                         }
151                 close(IN);
152                 }
153         return(@ret);
154         }
155
156 sub do_line
157         {
158         local($file,$_)=@_;
159         local($n);
160
161         return(undef) if /^$/;
162         return(undef) if /^\s/;
163         if (/(CRYPTO_get_locking_callback)/)
164                 { return($1); }
165         elsif (/(CRYPTO_get_id_callback)/)
166                 { return($1); }
167         elsif (/(CRYPTO_get_add_lock_callback)/)
168                 { return($1); }
169         elsif (/(SSL_CTX_get_verify_callback)/)
170                 { return($1); }
171         else
172                 {
173                 /\s\**(\S+)\s*\(/;
174                 return($1);
175                 }
176         }
177
178 sub do_extern
179         {
180         local($file,$_)=@_;
181         local($n);
182
183         /\s\**(\S+);$/;
184         return($1);
185         }
186
187 sub print_def_file
188         {
189         local(*OUT,$name,*nums,@functions)=@_;
190         local($n)=1;
191
192         if ($NT)
193                 { $name.="32"; }
194         else
195                 { $name.="16"; }
196
197         print OUT <<"EOF";
198 ;
199 ; Definition file for the DDL version of the $name library from SSLeay
200 ;
201
202 LIBRARY         $name
203
204 DESCRIPTION     'SSLeay $name - eay\@cryptsoft.com'
205
206 EOF
207
208         if (!$NT)
209                 {
210                 print <<"EOF";
211 CODE            PRELOAD MOVEABLE
212 DATA            PRELOAD MOVEABLE SINGLE
213
214 EXETYPE         WINDOWS
215
216 HEAPSIZE        4096
217 STACKSIZE       8192
218
219 EOF
220                 }
221
222         print "EXPORTS\n";
223
224
225         (@e)=grep(/^SSLeay/,@functions);
226         (@r)=grep(!/^SSLeay/,@functions);
227         @functions=((sort @e),(sort @r));
228
229         foreach $func (@functions)
230                 {
231                 if (!defined($nums{$func}))
232                         {
233                         printf STDERR "$func does not have a number assigned\n";
234                         }
235                 else
236                         {
237                         $n=$nums{$func};
238                         printf OUT "    %s%-35s@%d\n",($NT)?"":"_",$func,$n;
239                         }
240                 }
241         printf OUT "\n";
242         }
243
244 sub load_numbers
245         {
246         local($name)=@_;
247         local($j,@a,%ret);
248
249         open(IN,"<$name") || die "unable to open $name:$!\n";
250         while (<IN>)
251                 {
252                 chop;
253                 s/#.*$//;
254                 next if /^\s*$/;
255                 @a=split;
256                 $ret{$a[0]}=$a[1];
257                 }
258         close(IN);
259         return(%ret);
260         }