s3-waf: only compile common.c in netapi examples code once.
[Samba/id10ts.git] / script / mkparamdefs.pl
blobb489cc9277a40d1c7901563bb91ae55e3ee58a2a
1 #!/usr/bin/perl
2 # Generate loadparm tables for loadparm.c
3 # by Andrew Bartlett
4 # based on mkproto.pl Written by Jelmer Vernooij
5 # based on the original mkproto.sh by Andrew Tridgell
7 use strict;
9 # don't use warnings module as it is not portable enough
10 # use warnings;
12 use Getopt::Long;
13 use File::Basename;
14 use File::Path;
16 #####################################################################
17 # read a file into a string
19 my $file = undef;
20 my $public_define = undef;
21 my $_public = "";
22 my $_private = "";
23 my $public_data = \$_public;
24 my $builddir = ".";
25 my $srcdir = ".";
26 my $generate_scope = undef;
28 sub public($)
30 my ($d) = @_;
31 $$public_data .= $d;
34 sub usage()
36 print "Usage: mkparamdefs.pl [options] [c files]\n";
37 print "OPTIONS:\n";
38 print " --srcdir=path Read files relative to this directory\n";
39 print " --builddir=path Write file relative to this directory\n";
40 print " --generate-scope=[GLOBAL|LOCAL] Filter which definitions to generate\n";
41 print " --help Print this help message\n\n";
42 exit 0;
45 GetOptions(
46 'file=s' => sub { my ($f,$v) = @_; $file = $v; },
47 'srcdir=s' => sub { my ($f,$v) = @_; $srcdir = $v; },
48 'builddir=s' => sub { my ($f,$v) = @_; $builddir = $v; },
49 'generate-scope=s' => sub { my ($f,$v) = @_; $generate_scope = $v; },
50 'help' => \&usage
51 ) or exit(1);
53 sub normalize_define($$)
55 my ($define, $file) = @_;
57 if (not defined($define) and defined($file)) {
58 $define = "__" . uc($file) . "__";
59 $define =~ tr{./}{__};
60 $define =~ tr{\-}{_};
61 } elsif (not defined($define)) {
62 $define = '_S3_PARAM_H_';
65 return $define;
68 $public_define = normalize_define($public_define, $file);
70 sub file_load($)
72 my($filename) = @_;
73 local(*INPUTFILE);
74 open(INPUTFILE, $filename) or return undef;
75 my($saved_delim) = $/;
76 undef $/;
77 my($data) = <INPUTFILE>;
78 close(INPUTFILE);
79 $/ = $saved_delim;
80 return $data;
83 sub print_header($$$)
85 my ($file, $header_name,$generate_scope) = @_;
86 $file->("#ifndef $header_name\n");
87 $file->("#define $header_name\n\n");
88 $file->("/* This file was automatically generated by mkparamdefs.pl. DO NOT EDIT */\n\n");
89 $file->("/**\n");
90 if ($generate_scope eq "GLOBAL") {
91 $file->(" * This structure describes global (ie., server-wide) parameters.\n");
92 $file->(" */\n");
93 $file->("struct loadparm_global \n");
94 } elsif ($generate_scope eq "LOCAL") {
95 $file->(" * This structure describes a single service.\n");
96 $file->(" */\n");
97 $file->("struct loadparm_service \n");
99 $file->("{\n");
102 sub print_footer($$$)
104 my ($file, $header_name, $generate_scope) = @_;
105 $file->("LOADPARM_EXTRA_" . $generate_scope . "S\n");
106 $file->("};\n");
107 $file->("\n#endif /* $header_name */\n\n");
110 sub handle_loadparm($$$)
112 my ($file,$line,$generate_scope) = @_;
114 my $scope;
115 my $type;
116 my $name;
117 my $var;
119 if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),(.*)\)/o) {
120 $scope = $1;
121 $type = $2;
122 $name = $3;
123 $var = $4;
124 } elsif ($line =~ /^FN_(GLOBAL|LOCAL)_PARM_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),(.*)\)/o) {
125 $scope = $1;
126 $type = $2;
127 $name = $3;
128 $var = $4;
129 } else {
130 return;
132 my %tmap = (
133 "BOOL" => "bool ",
134 "CONST_STRING" => "char *",
135 "STRING" => "char *",
136 "INTEGER" => "int ",
137 "CHAR" => "char ",
138 "LIST" => "const char **",
141 if ($scope eq $generate_scope) {
142 $file->("\t$tmap{$type} $var;\n");
146 sub process_file($$)
148 my ($file, $filename) = @_;
150 $filename =~ s/\.o$/\.c/g;
152 if ($filename =~ /^\//) {
153 open(FH, "<$filename") or die("Failed to open $filename");
154 } elsif (!open(FH, "< $builddir/$filename")) {
155 open(FH, "< $srcdir/$filename") || die "Failed to open $filename";
158 my $comment = undef;
159 my $incomment = 0;
160 while (my $line = <FH>) {
161 if ($line =~ /^\/\*\*/) {
162 $comment = "";
163 $incomment = 1;
166 if ($incomment) {
167 $comment .= $line;
168 if ($line =~ /\*\//) {
169 $incomment = 0;
173 # these are ordered for maximum speed
174 next if ($line =~ /^\s/);
176 next unless ($line =~ /\(/);
178 next if ($line =~ /^\/|[;]/);
180 if ($line =~ /^static (FN_.*)/) {
181 handle_loadparm($file, $1, $generate_scope);
182 } elsif ($line =~ /^FN_/) {
183 handle_loadparm($file, $line, $generate_scope);
185 next;
188 close(FH);
192 print_header(\&public, $public_define, $generate_scope);
194 process_file(\&public, $_) foreach (@ARGV);
195 print_footer(\&public, $public_define, $generate_scope);
197 if (not defined($file)) {
198 print STDOUT $$public_data;
201 mkpath(dirname($file), 0, 0755);
202 open(PUBLIC, ">$file") or die("Can't open `$file': $!");
203 print PUBLIC "$$public_data";
204 close(PUBLIC);