make fast work with mit kerberos
[heimdal.git] / cf / make-proto.pl
blob4ac88de6fe4e71debd871fb4386363116b7031a7
1 # Make prototypes from .c files
2 # $Id$
4 use Getopt::Std;
5 use File::Compare;
7 use JSON;
9 my $comment = 0;
10 my $doxygen = 0;
11 my $funcdoc = 0;
12 my $if_0 = 0;
13 my $brace = 0;
14 my $line = "";
15 my $debug = 0;
16 my $oproto = 1;
17 my $private_func_re = "^_";
18 my %depfunction;
19 my %exported;
20 my %deprecated;
21 my $apple = 0;
22 my %documentation;
24 getopts('x:m:o:p:dqE:R:P:') || die "foo";
25 if($opt_a) {
26 $apple = 1;
29 if($opt_a) {
30 $apple = 1;
33 if($opt_d) {
34 $debug = 1;
37 if($opt_q) {
38 $oproto = 0;
41 if($opt_R) {
42 $private_func_re = $opt_R;
44 my %flags = (
45 'multiline-proto' => 1,
46 'header' => 1,
47 'function-blocking' => 0,
48 'gnuc-attribute' => 1,
49 'cxx' => 1
51 if($opt_m) {
52 foreach $i (split(/,/, $opt_m)) {
53 if($i eq "roken") {
54 $flags{"multiline-proto"} = 0;
55 $flags{"header"} = 0;
56 $flags{"function-blocking"} = 0;
57 $flags{"gnuc-attribute"} = 0;
58 $flags{"cxx"} = 0;
59 } else {
60 if(substr($i, 0, 3) eq "no-") {
61 $flags{substr($i, 3)} = 0;
62 } else {
63 $flags{$i} = 1;
69 if($opt_x) {
70 my $EXP;
71 local $/;
72 open(EXP, '<', $opt_x) || die "open ${opt_x}";
73 my $obj = JSON->new->utf8->decode(<EXP>);
74 close $EXP;
76 foreach my $x (keys %$obj) {
77 if (defined $obj->{$x}->{"export"}) {
78 $exported{$x} = $obj->{$x};
80 if (defined $obj->{$x}->{"deprecated"}) {
81 $deprecated{$x} = $obj->{$x}->{"deprecated"};
86 while(<>) {
87 print $brace, " ", $_ if($debug);
89 # Handle C comments
90 s@/\*.*\*/@@;
91 s@//.*/@@;
92 if ( s@/\*\*(.*)@@) { $comment = 1; $doxygen = 1; $funcdoc = $1;
93 } elsif ( s@/\*.*@@) { $comment = 1;
94 } elsif ($comment && s@.*\*/@@) { $comment = 0; $doxygen = 0;
95 } elsif ($doxygen) { $funcdoc .= $_; next;
96 } elsif ($comment) { next; }
98 if(/^\#if 0/) {
99 $if_0 = 1;
101 if($if_0 && /^\#endif/) {
102 $if_0 = 0;
104 if($if_0) { next }
105 if(/^\s*\#/) {
106 next;
108 if(/^\s*$/) {
109 $line = "";
110 next;
112 if(/\{/){
113 if (!/\}/) {
114 $brace++;
116 $_ = $line;
117 while(s/\*\//\ca/){
118 s/\/\*(.|\n)*\ca//;
120 s/^\s*//;
121 s/\s*$//;
122 s/\s+/ /g;
123 if($_ =~ /\)$/){
124 if(!/^static/ && !/^PRIVATE/){
125 $attr = "";
126 if(m/(.*)(__attribute__\s?\(.*\))/) {
127 $attr .= " $2";
128 $_ = $1;
130 if(m/(.*)\s(\w+DEPRECATED_FUNCTION)\s?(\(.*\))(.*)/) {
131 $depfunction{$2} = 1;
132 $attr .= " $2$3";
133 $_ = "$1 $4";
135 if(m/(.*)\s(\w+DEPRECATED)(.*)/) {
136 $attr .= " $2";
137 $_ = "$1 $3";
139 # remove outer ()
140 s/\s*\(/</;
141 s/\)\s?$/>/;
142 # remove , within ()
143 while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
144 s/\<\s*void\s*\>/<>/;
145 # remove parameter names
146 if($opt_P eq "remove") {
147 s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
148 s/\s+\*/*/g;
149 s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
150 } elsif($opt_P eq "comment") {
151 s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
152 s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
154 s/\<\>/<void>/;
155 # add newlines before parameters
156 if($flags{"multiline-proto"}) {
157 s/,\s*/,\n\t/g;
158 } else {
159 s/,\s*/, /g;
161 # fix removed ,
162 s/\$/,/g;
163 # match function name
164 /([a-zA-Z0-9_]+)\s*\</;
165 $f = $1;
166 if($oproto) {
167 $LP = "__P((";
168 $RP = "))";
169 } else {
170 $LP = "(";
171 $RP = ")";
173 # only add newline if more than one parameter
174 if($flags{"multiline-proto"} && /,/){
175 s/\</ $LP\n\t/;
176 }else{
177 s/\</ $LP/;
179 s/\>/$RP/;
180 # insert newline before function name
181 if($flags{"multiline-proto"}) {
182 s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
184 if($attr ne "") {
185 $_ .= "\n $attr";
187 if ($funcdoc) {
188 $documentation{$f} = $funcdoc;
190 $funcdoc = undef;
191 if ($apple && exists $exported{$f}) {
192 $ios = $exported{$f}{ios};
193 $ios = "NA" if (!defined $ios);
194 $mac = $exported{$f}{macos};
195 $mac = "NA" if (!defined $mac);
196 die "$f neither" if ($mac eq "NA" and $ios eq "NA");
197 $_ = $_ . " __OSX_AVAILABLE_STARTING(__MAC_${mac}, __IPHONE_${ios})";
199 if (exists $deprecated{$f}) {
200 $_ = $_ . " GSSAPI_DEPRECATED_FUNCTION(\"$deprecated{$f}\")";
201 $depfunction{GSSAPI_DEPRECATED_FUNCTION} = 1;
203 $_ = $_ . ";";
204 $funcs{$f} = $_;
207 $line = "";
209 if(/\}/){
210 $brace--;
212 if(/^\}/){
213 $brace = 0;
215 if($brace == 0) {
216 $line = $line . " " . $_;
220 die "reached end of code and still in doxygen comment" if ($doxygen);
221 die "reached end of code and still in comment" if ($comment);
223 sub foo {
224 local ($arg) = @_;
225 $_ = $arg;
226 s/.*\/([^\/]*)/$1/;
227 s/.*\\([^\\]*)/$1/;
228 s/[^a-zA-Z0-9]/_/g;
229 "__" . $_ . "__";
232 if($opt_o) {
233 open(OUT, ">${opt_o}.new");
234 $block = &foo($opt_o);
235 } else {
236 $block = "__public_h__";
239 if($opt_p) {
240 open(PRIV, ">${opt_p}.new");
241 $private = &foo($opt_p);
242 } else {
243 $private = "__private_h__";
246 $public_h = "";
247 $private_h = "";
249 $public_h_header .= "/* This is a generated file */
250 #ifndef $block
251 #define $block
254 if ($oproto) {
255 $public_h_header .= "#ifdef __STDC__
256 #include <stdarg.h>
257 #ifndef __P
258 #define __P(x) x
259 #endif
260 #else
261 #ifndef __P
262 #define __P(x) ()
263 #endif
264 #endif
267 } else {
268 $public_h_header .= "#include <stdarg.h>
272 $public_h_trailer = "";
274 $private_h_header = "/* This is a generated file */
275 #ifndef $private
276 #define $private
279 if($oproto) {
280 $private_h_header .= "#ifdef __STDC__
281 #include <stdarg.h>
282 #ifndef __P
283 #define __P(x) x
284 #endif
285 #else
286 #ifndef __P
287 #define __P(x) ()
288 #endif
289 #endif
292 } else {
293 $private_h_header .= "#include <stdarg.h>
297 $private_h_trailer = "";
299 foreach(sort keys %funcs){
300 if(/^(main)$/) { next }
301 if ($funcs{$_} =~ /\^/) {
302 $beginblock = "#ifdef __BLOCKS__\n";
303 $endblock = "#endif /* __BLOCKS__ */\n";
304 } else {
305 $beginblock = $endblock = "";
307 # if we have an export table and doesn't have content, or matches private RE
308 if((scalar(keys(%exported)) ne 0 && !exists $exported{$_} ) || /$private_func_re/) {
309 $private_h .= $beginblock;
310 $private_h .= $funcs{$_} . "\n" ;
311 $private_h .= $endblock . "\n";
312 if($funcs{$_} =~ /__attribute__/) {
313 $private_attribute_seen = 1;
315 } else {
316 if($documentation{$_}) {
317 $public_h .= "/**\n";
318 $public_h .= "$documentation{$_}";
319 $public_h .= " */\n";
321 if($flags{"function-blocking"}) {
322 $fupper = uc $_;
323 if($exported{$_} =~ /proto/) {
324 $public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
325 } else {
326 $public_h .= "#ifndef HAVE_$fupper\n";
329 $public_h .= $beginblock . $funcs{$_} . "\n" . $endblock;
330 if($funcs{$_} =~ /__attribute__/) {
331 $public_attribute_seen = 1;
333 if($flags{"function-blocking"}) {
334 $public_h .= "#endif\n";
336 $public_h .= "\n";
340 if($flags{"gnuc-attribute"}) {
341 if ($public_attribute_seen) {
342 $public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
343 #define __attribute__(x)
344 #endif
349 if ($private_attribute_seen) {
350 $private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
351 #define __attribute__(x)
352 #endif
358 my $depstr = "";
359 my $undepstr = "";
360 foreach (keys %depfunction) {
361 $depstr .= "#ifndef $_
362 #ifndef __has_extension
363 #define __has_extension(x) 0
364 #define ${_}has_extension 1
365 #endif
366 #if __has_extension(attribute_deprecated_with_message)
367 #define $_(x) __attribute__((__deprecated__(x)))
368 #elif defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
369 #define $_(X) __attribute__((__deprecated__))
370 #else
371 #define $_(X)
372 #endif
373 #ifdef ${_}has_extension
374 #undef __has_extension
375 #undef ${_}has_extension
376 #endif
377 #endif /* $_ */
381 $public_h_trailer .= "#undef $_
384 $private_h_trailer .= "#undef $_
385 #define $_(X)
390 $public_h_header .= $depstr;
391 $private_h_header .= $depstr;
394 if($flags{"cxx"}) {
395 $public_h_header .= "#ifdef __cplusplus
396 extern \"C\" {
397 #endif
400 $public_h_trailer = "#ifdef __cplusplus
402 #endif
404 " . $public_h_trailer;
407 if ($opt_E) {
408 $public_h_header .= "#ifndef $opt_E
409 #ifndef ${opt_E}_FUNCTION
410 #if defined(_WIN32)
411 #define ${opt_E}_FUNCTION __declspec(dllimport)
412 #define ${opt_E}_CALL __stdcall
413 #define ${opt_E}_VARIABLE __declspec(dllimport)
414 #else
415 #define ${opt_E}_FUNCTION
416 #define ${opt_E}_CALL
417 #define ${opt_E}_VARIABLE
418 #endif
419 #endif
420 #endif
423 $private_h_header .= "#ifndef $opt_E
424 #ifndef ${opt_E}_FUNCTION
425 #if defined(_WIN32)
426 #define ${opt_E}_FUNCTION __declspec(dllimport)
427 #define ${opt_E}_CALL __stdcall
428 #define ${opt_E}_VARIABLE __declspec(dllimport)
429 #else
430 #define ${opt_E}_FUNCTION
431 #define ${opt_E}_CALL
432 #define ${opt_E}_VARIABLE
433 #endif
434 #endif
435 #endif
440 $public_h_trailer .= $undepstr;
441 $private_h_trailer .= $undepstr;
443 if ($public_h ne "" && $flags{"header"}) {
444 $public_h = $public_h_header . $public_h .
445 $public_h_trailer . "#endif /* $block */\n";
447 if ($private_h ne "" && $flags{"header"}) {
448 $private_h = $private_h_header . $private_h .
449 $private_h_trailer . "#endif /* $private */\n";
452 if($opt_o) {
453 print OUT $public_h;
455 if($opt_p) {
456 print PRIV $private_h;
459 close OUT;
460 close PRIV;
462 if ($opt_o) {
464 if (compare("${opt_o}.new", ${opt_o}) != 0) {
465 printf("updating ${opt_o}\n");
466 rename("${opt_o}.new", ${opt_o});
467 } else {
468 unlink("${opt_o}.new");
472 if ($opt_p) {
473 if (compare("${opt_p}.new", ${opt_p}) != 0) {
474 printf("updating ${opt_p}\n");
475 rename("${opt_p}.new", ${opt_p});
476 } else {
477 unlink("${opt_p}.new");