source: trunk/server/buildtools/mktowscript/mktowscript.pl@ 751

Last change on this file since 751 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 11.2 KB
Line 
1#!/usr/bin/perl -w
2
3use strict;
4use Data::Dumper;
5use File::Basename;
6use List::MoreUtils qw(uniq);
7
8my $globals;
9my $dname;
10
11sub read_file($)
12{
13 my $filename = shift;
14 open(CONFIG_MK, "$filename");
15 my @lines = <CONFIG_MK>;
16 close(CONFIG_MK);
17 return @lines;
18}
19
20sub trim($)
21{
22 my $string = shift;
23 $string =~ s/^\s+//;
24 $string =~ s/\s+$//;
25 return $string;
26}
27
28sub strlist($)
29{
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;
43
44 # this one doesn't exist?
45 $s =~ s/\bLDAP_ENCODE\b//g;
46
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;
58
59 return trim(join(' ', split(/\s+/, $s)));
60}
61
62sub expand_vars($$)
63{
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});
70 }
71 }
72 foreach my $v (keys %{$globals}) {
73 if ($s =~ /\$\($v\)/) {
74 $s =~ s/\$\($v\)/$globals->{$v}/g;
75 }
76 }
77 return $s;
78}
79
80sub find_file($)
81{
82 my $f = shift;
83 my $orig = $f;
84
85 if ($f =~ /\$/) {
86 printf(STDERR "bad variable expansion for file $orig in $dname\n");
87 exit(1);
88 }
89
90 my $b = basename($f);
91 return $b if (-e $b);
92
93 return $f if (-e $f);
94
95 while ($f =~ /\//) {
96 $f =~ s/^[^\/]+\///g;
97 return $f if (-e $f);
98 }
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 '';
111}
112
113sub find_files($)
114{
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;
124 }
125 $f = find_file($f);
126 $f =~ s/^[.]\///;
127 $ret .= ' ' . $f;
128 }
129 $ret = strlist($ret);
130 my @list = split(/\s+/, $ret);
131 @list = uniq(@list);
132 $ret = trim(join(' ', @list));
133 return $ret;
134}
135
136sub read_config_mk($)
137{
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;
147
148 $result->{"GLOBAL"}->{SECNUMBER} = $secnumber++;
149
150 foreach (@lines) {
151 $linenum++;
152
153 # lines beginning with '#' are ignored
154 next if (/^\#.*$/);
155
156 if (/^(.*)\\$/) {
157 $prev .= $1;
158 next;
159 } else {
160 $line = "$prev$_";
161 $prev = "";
162 }
163
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;
174 }
175 $result->{$section}->{DIRECTORY} = $dir;
176 $result->{$section}->{$section . '_OBJ_FILES'} = $src;
177 next;
178 }
179
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;
191 }
192
193 if ($line =~ /^\[(\w+)::([\w-]+)\]/)
194 {
195 my $type = $1;
196 $section = $2;
197 $infragment = 0;
198
199 $result->{$section}->{TYPE} = $type;
200 $result->{$section}->{SECNUMBER} = $secnumber++;
201 next;
202 }
203
204 # include
205 if ($line =~ /^mkinclude (.*)$/) {
206 my $subfile = $1;
207 $result->{$subfile}->{TYPE} = 'SUBCONFIG';
208 $result->{$subfile}->{SECNUMBER} = $secnumber++;
209 next;
210 }
211
212 # empty line
213 if ($line =~ /^[ \t]*$/) {
214 next;
215 }
216
217 # global stuff is considered part of the makefile
218 if ($section eq "GLOBAL") {
219 $infragment = 1;
220 next;
221 }
222
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;
228 }
229
230 # +=
231 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*\+=(.*)$/) {
232 if (!$result->{$section}->{$1}) {
233 $result->{$section}->{$1}="";
234 }
235 $result->{$section}->{$1} .= " " . expand_vars($result->{$section}, strlist($2));
236 $globals->{$1} = $result->{$section}->{$1};
237 next;
238 }
239
240 if ($line =~ /\$\(eval.\$\(call.proto_header_template.*,(.*),.*/) {
241 $result->{$section}->{AUTOPROTO} = $1;
242 }
243 if ($line =~ /^\$\(eval/) {
244 # skip eval lines for now
245 next;
246 }
247
248 printf(STDERR "$linenum: Bad line: $line");
249 }
250
251 return $result;
252}
253
254
255sub process_results($)
256{
257 my $result = shift;
258
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;
271
272 foreach my $k (keys %{$sec}) {
273 #print "key=$k\n";
274
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;
280 }
281 if ($k eq "INIT_FUNCTION_SENTINEL") {
282 $trailer .= sprintf(",\n\tinit_function_sentinal='%s'", trim($sec->{$k}));
283 next;
284 }
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;
294 }
295 if ($k eq "SUBSYSTEM") {
296 $trailer .= sprintf(",\n\tsubsystem='%s'", trim($sec->{$k}));
297 next;
298 }
299 if ($k eq "PRIVATE_DEPENDENCIES") {
300 $trailer .= sprintf(",\n\tdeps='%s'", strlist($sec->{$k}));
301 $got_private_deps = 1;
302 next;
303 }
304 if ($k eq "PUBLIC_DEPENDENCIES") {
305 $trailer .= sprintf(",\n\tpublic_deps='%s'", strlist($sec->{$k}));
306 next;
307 }
308 if ($k eq "ALIASES") {
309 $trailer .= sprintf(",\n\taliases='%s'", strlist($sec->{$k}));
310 next;
311 }
312 if ($k eq "CFLAGS") {
313 $trailer .= sprintf(",\n\tcflags='%s'", strlist($sec->{$k}));
314 next;
315 }
316 if ($k eq "OPTIONS") {
317 $trailer .= sprintf(",\n\toptions='%s'", strlist($sec->{$k}));
318 next;
319 }
320 if ($k eq "DIRECTORY") {
321 $trailer .= sprintf(",\n\tdirectory='%s'", strlist($sec->{$k}));
322 next;
323 }
324 if ($k eq "LDFLAGS") {
325 $trailer .= sprintf(",\n\tldflags='%s'", strlist($sec->{$k}));
326 next;
327 }
328 if ($k eq "INSTALLDIR") {
329 $trailer .= sprintf(",\n\tinstalldir='%s'", strlist($sec->{$k}));
330 next;
331 }
332 if ($k eq "ASN1C") {
333 $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
334 next;
335 }
336 if ($k eq "ET_COMPILER") {
337 $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
338 next;
339 }
340 if ($k eq "ENABLE") {
341 my $v = strlist($sec->{$k});
342 if ($v eq "NO") {
343 $trailer .= sprintf(",\n\tenabled=False");
344 next;
345 }
346 next if ($v eq "YES");
347 die("Unknown ENABLE value $v in $s\n");
348 }
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;
354 }
355 next if ($v eq "NO");
356 die("Unknown HOST_CC value $v in $s\n");
357 }
358 if ($k eq "$s" . "_VERSION") {
359 $trailer .= sprintf(",\n\tvnum='%s'", strlist($sec->{$k}));
360 next;
361 }
362 if ($k eq "$s" . "_SOVERSION") {
363 next;
364 }
365 if ($k eq "LIBRARY_REALNAME") {
366 $trailer .= sprintf(",\n\trealname='%s'", strlist($sec->{$k}));
367 next;
368 }
369 if ($k eq "OUTPUT_TYPE") {
370 $trailer .= sprintf(",\n\toutput_type='%s'", strlist($sec->{$k}));
371 next;
372 }
373 if ($k eq "AUTOPROTO") {
374 my $list = trim(find_files(strlist($sec->{$k})));
375 $trailer .= sprintf(",\n\tautoproto='%s'", $list);
376 next;
377 }
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})));
385 }
386 $trailer .= sprintf(",\n\tpublic_headers='%s'", $list);
387 next;
388 }
389 if ($k eq "MANPAGES") {
390 my $list = trim(find_files(strlist($sec->{$k})));
391 $trailer .= sprintf(",\n\tmanpages='%s'", $list);
392 next;
393 }
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'";
409 }
410 $list = $res;
411 } else {
412 $list = find_files($list);
413 $list="'$list'";
414 }
415 $list =~ s/\$\(\w+srcdir\)\///g;
416 printf(",\n\tsource=%s", $list);
417 $got_src = 1;
418 next;
419 }
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"
426 ) {
427 next;
428 }
429 die("Unknown keyword $k in $s\n");
430 }
431 die("No source list in $s\n") unless $got_src or $got_private_deps;
432 if (! $got_src) {
433 printf(",source=''\n\t");
434 }
435 printf("%s\n\t)\n\n", $trailer);
436 }
437 }
438}
439
440for (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";
446 }
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);
450}
451
Note: See TracBrowser for help on using the repository browser.