s3: Lift the server_messaging_context from send_spoolss_notify2_msg
[Samba/gbeck.git] / buildtools / mktowscript / mktowscript.pl
bloba05506bcbdfac7ff795c14b73e53e6374fe3ebe5
1 #!/usr/bin/perl -w
3 use strict;
4 use Data::Dumper;
5 use File::Basename;
6 use List::MoreUtils qw(uniq);
8 my $globals;
9 my $dname;
11 sub read_file($)
13 my $filename = shift;
14 open(CONFIG_MK, "$filename");
15 my @lines = <CONFIG_MK>;
16 close(CONFIG_MK);
17 return @lines;
20 sub trim($)
22 my $string = shift;
23 $string =~ s/^\s+//;
24 $string =~ s/\s+$//;
25 return $string;
28 sub strlist($)
30 my $s = shift;
31 $s =~ s/\$\(SHLIBEXT\)/so/g;
32 $s =~ s/\$\(heimdalsrcdir\)/..\/heimdal/g;
33 $s =~ s/\$\(heimdalbuildsrcdir\)/..\/heimdal_build/g;
34 $s =~ s/\$\(nsswitchsrcdir\)/..\/nsswitch/g;
35 $s =~ s/\$\(param_OBJ_FILES\)/..\/pyparam.c/g;
36 $s =~ s/\$\(libclisrcdir\)\///g;
37 $s =~ s/\$\(socketwrappersrcdir\)\///g;
38 $s =~ s/\$\(libcompressionsrcdir\)\///g;
39 $s =~ s/\$\(\w*srcdir\)\///g;
40 $s =~ s/\$\(libgpodir\)\///g;
41 $s =~ s/\:\.o=\.ho//g;
42 $s =~ s/\:\.o=\.d//g;
44 # this one doesn't exist?
45 $s =~ s/\bLDAP_ENCODE\b//g;
47 # these need to use the library names
48 $s =~ s/\bLIBLDB\b/ldb/g;
49 $s =~ s/\bLDB\b/ldb/g;
50 $s =~ s/\bLIBTALLOC\b/talloc/g;
51 $s =~ s/\bTALLOC\b/talloc/g;
52 $s =~ s/\bLIBTEVENT\b/tevent/g;
53 $s =~ s/\bTEVENT\b/tevent/g;
54 $s =~ s/\bTSOCKET\b/LIBTSOCKET/g;
55 $s =~ s/\bGENSEC\b/gensec/g;
56 $s =~ s/\bLIBTDB\b/tdb/g;
57 $s =~ s/\bRESOLV\b/resolv/g;
59 return trim(join(' ', split(/\s+/, $s)));
62 sub expand_vars($$)
64 my $vars = shift;
65 my $s = shift;
66 foreach my $v (keys %{$vars}) {
67 if ($s =~ /\$\($v\)/) {
68 $s =~ s/\$\($v\)/$vars->{$v}/g;
69 delete($vars->{$v});
72 foreach my $v (keys %{$globals}) {
73 if ($s =~ /\$\($v\)/) {
74 $s =~ s/\$\($v\)/$globals->{$v}/g;
77 return $s;
80 sub find_file($)
82 my $f = shift;
83 my $orig = $f;
85 if ($f =~ /\$/) {
86 printf(STDERR "bad variable expansion for file $orig in $dname\n");
87 exit(1);
90 my $b = basename($f);
91 return $b if (-e $b);
93 return $f if (-e $f);
95 while ($f =~ /\//) {
96 $f =~ s/^[^\/]+\///g;
97 return $f if (-e $f);
99 my $f2;
100 $f2 = `find . -name "$f" -type f`;
101 return $f2 unless ($f2 eq "");
102 $f2 = `find .. -name "$f" -type f`;
103 return $f2 unless ($f2 eq "");
104 $f2 = `find ../.. -name "$f" -type f`;
105 return $f2 unless ($f2 eq "");
106 $f2 = `find ../../.. -name "$f" -type f`;
107 return $f2 unless ($f2 eq "");
108 printf(STDERR "Failed to find $orig in $dname\n");
109 exit(1);
110 return '';
113 sub find_files($)
115 my $list = shift;
116 my $ret = '';
117 foreach my $f (split(/\s+/, $list)) {
118 if ($f =~ /\.[0-9]$/) {
119 # a man page
120 my $m = find_file($f . ".xml");
121 die("Unable to find man page $f\n") if ($m eq "");
122 $m =~ s/\.xml$//;
123 return $m;
125 $f = find_file($f);
126 $f =~ s/^[.]\///;
127 $ret .= ' ' . $f;
129 $ret = strlist($ret);
130 my @list = split(/\s+/, $ret);
131 @list = uniq(@list);
132 $ret = trim(join(' ', @list));
133 return $ret;
136 sub read_config_mk($)
138 my $filename = shift;
139 my @lines = read_file($filename);
140 my $prev = "";
141 my $linenum = 1;
142 my $section = "GLOBAL";
143 my $infragment;
144 my $result;
145 my $line = "";
146 my $secnumber = 1;
148 $result->{"GLOBAL"}->{SECNUMBER} = $secnumber++;
150 foreach (@lines) {
151 $linenum++;
153 # lines beginning with '#' are ignored
154 next if (/^\#.*$/);
156 if (/^(.*)\\$/) {
157 $prev .= $1;
158 next;
159 } else {
160 $line = "$prev$_";
161 $prev = "";
164 if ($line =~ /^mkinclude.*asn1_deps.pl\s+([^\s]+)\s+([^\s]+)\s+\\\$\\\(\w+\\\)\/([^\s|]+)\s*([^|]*)\|$/) {
165 my $src = $1;
166 $section = $2;
167 my $dir = $3;
168 my $options = $4;
169 $section = "HEIMDAL_" . uc($section);
170 $result->{$section}->{TYPE} = 'ASN1';
171 $result->{$section}->{SECNUMBER} = $secnumber++;
172 if ($options ne '') {
173 $result->{$section}->{OPTIONS} = $options;
175 $result->{$section}->{DIRECTORY} = $dir;
176 $result->{$section}->{$section . '_OBJ_FILES'} = $src;
177 next;
180 if ($line =~ /^mkinclude.*et_deps.pl\s+([^\s]+)\s+\\\$\\\(\w+\\\)\/([^\s|]+)\|$/) {
181 my $src = $1;
182 my $dir = $2;
183 $section = basename($src);
184 $section =~ s/\./_/g;
185 $section = "HEIMDAL_" . uc($section);
186 $result->{$section}->{TYPE} = 'ERRTABLE';
187 $result->{$section}->{SECNUMBER} = $secnumber++;
188 $result->{$section}->{DIRECTORY} = "$dir";
189 $result->{$section}->{$section . '_OBJ_FILES'} = $src;
190 next;
193 if ($line =~ /^\[(\w+)::([\w-]+)\]/)
195 my $type = $1;
196 $section = $2;
197 $infragment = 0;
199 $result->{$section}->{TYPE} = $type;
200 $result->{$section}->{SECNUMBER} = $secnumber++;
201 next;
204 # include
205 if ($line =~ /^mkinclude (.*)$/) {
206 my $subfile = $1;
207 $result->{$subfile}->{TYPE} = 'SUBCONFIG';
208 $result->{$subfile}->{SECNUMBER} = $secnumber++;
209 next;
212 # empty line
213 if ($line =~ /^[ \t]*$/) {
214 next;
217 # global stuff is considered part of the makefile
218 if ($section eq "GLOBAL") {
219 $infragment = 1;
220 next;
223 # Assignment
224 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*=(.*)$/) {
225 $result->{$section}->{$1} = expand_vars($result->{$section}, strlist($2));
226 $globals->{$1} = $result->{$section}->{$1};
227 next;
230 # +=
231 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*\+=(.*)$/) {
232 if (!$result->{$section}->{$1}) {
233 $result->{$section}->{$1}="";
235 $result->{$section}->{$1} .= " " . expand_vars($result->{$section}, strlist($2));
236 $globals->{$1} = $result->{$section}->{$1};
237 next;
240 if ($line =~ /\$\(eval.\$\(call.proto_header_template.*,(.*),.*/) {
241 $result->{$section}->{AUTOPROTO} = $1;
243 if ($line =~ /^\$\(eval/) {
244 # skip eval lines for now
245 next;
248 printf(STDERR "$linenum: Bad line: $line");
251 return $result;
255 sub process_results($)
257 my $result = shift;
259 foreach my $s (sort {$result->{$a}->{SECNUMBER} <=> $result->{$b}->{SECNUMBER}} keys %{$result}) {
260 next if ($s eq "GLOBAL");
261 my $sec = $result->{$s};
262 if ($sec->{TYPE} eq "SUBCONFIG") {
263 my $d = dirname($s);
264 next if ($d eq ".");
265 printf "bld.BUILD_SUBDIR('%s')\n", dirname($s);
266 } else {
267 printf "\nbld.SAMBA_%s('%s'", $sec->{TYPE}, $s;
268 my $trailer="";
269 my $got_src = 0;
270 my $got_private_deps = 0;
272 foreach my $k (keys %{$sec}) {
273 #print "key=$k\n";
275 next if ($k eq "SECNUMBER");
276 next if ($k eq "TYPE");
277 if ($k eq "INIT_FUNCTION") {
278 $trailer .= sprintf(",\n\tinit_function='%s'", trim($sec->{$k}));
279 next;
281 if ($k eq "INIT_FUNCTION_SENTINEL") {
282 $trailer .= sprintf(",\n\tinit_function_sentinal='%s'", trim($sec->{$k}));
283 next;
285 if ($k eq "_PY_FILES" ||
286 $k eq "EPYDOC_OPTIONS" ||
287 $k eq "COV_TARGET" ||
288 $k eq "GCOV" ||
289 $k eq "PC_FILES" ||
290 $k eq "CONFIG4FILE" ||
291 $k eq "LMHOSTSFILE4") {
292 $trailer .= sprintf(",\n\t# %s='%s'", $k, trim($sec->{$k}));
293 next;
295 if ($k eq "SUBSYSTEM") {
296 $trailer .= sprintf(",\n\tsubsystem='%s'", trim($sec->{$k}));
297 next;
299 if ($k eq "PRIVATE_DEPENDENCIES") {
300 $trailer .= sprintf(",\n\tdeps='%s'", strlist($sec->{$k}));
301 $got_private_deps = 1;
302 next;
304 if ($k eq "PUBLIC_DEPENDENCIES") {
305 $trailer .= sprintf(",\n\tpublic_deps='%s'", strlist($sec->{$k}));
306 next;
308 if ($k eq "ALIASES") {
309 $trailer .= sprintf(",\n\taliases='%s'", strlist($sec->{$k}));
310 next;
312 if ($k eq "CFLAGS") {
313 $trailer .= sprintf(",\n\tcflags='%s'", strlist($sec->{$k}));
314 next;
316 if ($k eq "OPTIONS") {
317 $trailer .= sprintf(",\n\toptions='%s'", strlist($sec->{$k}));
318 next;
320 if ($k eq "DIRECTORY") {
321 $trailer .= sprintf(",\n\tdirectory='%s'", strlist($sec->{$k}));
322 next;
324 if ($k eq "LDFLAGS") {
325 $trailer .= sprintf(",\n\tldflags='%s'", strlist($sec->{$k}));
326 next;
328 if ($k eq "INSTALLDIR") {
329 $trailer .= sprintf(",\n\tinstalldir='%s'", strlist($sec->{$k}));
330 next;
332 if ($k eq "ASN1C") {
333 $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
334 next;
336 if ($k eq "ET_COMPILER") {
337 $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
338 next;
340 if ($k eq "ENABLE") {
341 my $v = strlist($sec->{$k});
342 if ($v eq "NO") {
343 $trailer .= sprintf(",\n\tenabled=False");
344 next;
346 next if ($v eq "YES");
347 die("Unknown ENABLE value $v in $s\n");
349 if ($k eq "USE_HOSTCC") {
350 my $v = strlist($sec->{$k});
351 if ($v eq "YES") {
352 $trailer .= sprintf(",\n\tuse_hostcc=True");
353 next;
355 next if ($v eq "NO");
356 die("Unknown HOST_CC value $v in $s\n");
358 if ($k eq "$s" . "_VERSION") {
359 $trailer .= sprintf(",\n\tvnum='%s'", strlist($sec->{$k}));
360 next;
362 if ($k eq "$s" . "_SOVERSION") {
363 next;
365 if ($k eq "LIBRARY_REALNAME") {
366 $trailer .= sprintf(",\n\trealname='%s'", strlist($sec->{$k}));
367 next;
369 if ($k eq "OUTPUT_TYPE") {
370 $trailer .= sprintf(",\n\toutput_type='%s'", strlist($sec->{$k}));
371 next;
373 if ($k eq "AUTOPROTO") {
374 my $list = trim(find_files(strlist($sec->{$k})));
375 $trailer .= sprintf(",\n\tautoproto='%s'", $list);
376 next;
378 if ($k eq "PUBLIC_HEADERS") {
379 my $list = trim(strlist($sec->{$k}));
380 if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
381 $list = trim("$1 $2");
382 $list = find_files($list);
383 } else {
384 $list = trim(find_files(strlist($sec->{$k})));
386 $trailer .= sprintf(",\n\tpublic_headers='%s'", $list);
387 next;
389 if ($k eq "MANPAGES") {
390 my $list = trim(find_files(strlist($sec->{$k})));
391 $trailer .= sprintf(",\n\tmanpages='%s'", $list);
392 next;
394 if ($k eq "$s" . "_OBJ_FILES") {
395 my $list = trim(strlist($sec->{$k}));
396 $list =~ s/\.o/.c/g;
397 $list =~ s/\.ho/.c/g;
398 if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
399 $list = trim("$1 $2");
400 $list = find_files($list);
401 $list = "'$list'";
402 } elsif ($list =~ /\$\(addprefix \$\((\w+)\)(.*),(.*)\)(.*)$/) {
403 my $src = trim($3);
404 my $dir = "$1$2";
405 $dir =~ s/\/$//;
406 my $res = "bld.SUBDIR('$dir', '$src')";
407 if ($4) {
408 $res = "$res + '$4'";
410 $list = $res;
411 } else {
412 $list = find_files($list);
413 $list="'$list'";
415 $list =~ s/\$\(\w+srcdir\)\///g;
416 printf(",\n\tsource=%s", $list);
417 $got_src = 1;
418 next;
420 if ($k eq "HEIMDAL_GSSAPI_KRB5_OBJ_FILES" ||
421 $k eq "HEIMDAL_GSSAPI_SPNEGO_OBJ_FILES" ||
422 $k eq "HEIMDAL_HEIM_ASN1_DER_OBJ_FILES" ||
423 $k eq "HEIMDAL_HX509_OBJH_FILES" ||
424 $k eq "HEIMDAL_HX509_OBJG_FILES" ||
425 $k eq "HEIMDAL_ROKEN_OBJ_FILES"
427 next;
429 die("Unknown keyword $k in $s\n");
431 die("No source list in $s\n") unless $got_src or $got_private_deps;
432 if (! $got_src) {
433 printf(",source=''\n\t");
435 printf("%s\n\t)\n\n", $trailer);
440 for (my $i=0; $i <= $#ARGV; $i++) {
441 my $filename=$ARGV[$i];
442 $dname=dirname($filename);
443 my $result = read_config_mk($filename);
444 if ($i != 0) {
445 print "\n\n\n";
447 print "# AUTOGENERATED by mktowscript.pl from $filename\n# Please remove this notice if hand editing\n\n";
448 die("Unable to chdir to $dname\n") unless chdir($dname);
449 process_results($result);