build: heimdal_build waf support
[nivanova/samba-autobuild/.git] / buildtools / mktowscript / mktowscript.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Data::Dumper;
5 use File::Basename;
6 use List::MoreUtils qw(uniq);
7
8 my $globals;
9 my $dname;
10
11 sub 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
20 sub trim($)
21 {
22         my $string = shift;
23         $string =~ s/^\s+//;
24         $string =~ s/\s+$//;
25         return $string;
26 }
27
28 sub 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/\bRESOLV\b/resolv/g;
54
55         return trim(join(' ', split(/\s+/, $s)));
56 }
57
58 sub expand_vars($$)
59 {
60         my $vars = shift;
61         my $s = shift;
62         foreach my $v (keys %{$vars}) {
63                 if ($s =~ /\$\($v\)/) {
64                         $s =~ s/\$\($v\)/$vars->{$v}/g;
65                         delete($vars->{$v});
66                 }
67         }
68         foreach my $v (keys %{$globals}) {
69                 if ($s =~ /\$\($v\)/) {
70                         $s =~ s/\$\($v\)/$globals->{$v}/g;
71                 }
72         }
73         return $s;
74 }
75
76 sub find_file($)
77 {
78         my $f = shift;
79         my $orig = $f;
80
81         if ($f =~ /\$/) {
82                 printf(STDERR "bad variable expansion for file $orig in $dname\n");
83                 exit(1);
84         }
85
86         my $b = basename($f);
87         return $b if (-e $b);
88
89         return $f if (-e $f);
90
91         while ($f =~ /\//) {
92                 $f =~ s/^[^\/]+\///g;
93                 return $f if (-e $f);
94         }
95         my $f2;
96         $f2 = `find . -name "$f" -type f`;
97         return $f2 unless ($f2 eq "");
98         $f2 = `find .. -name "$f" -type f`;
99         return $f2 unless ($f2 eq "");
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         printf(STDERR "Failed to find $orig in $dname\n");
105         exit(1);
106         return '';
107 }
108
109 sub find_files($)
110 {
111         my $list = shift;
112         my $ret = '';
113         foreach my $f (split(/\s+/, $list)) {
114                 if ($f =~ /\.[0-9]$/) {
115                         # a man page
116                         my $m = find_file($f . ".xml");
117                         die("Unable to find man page $f\n") if ($m eq "");
118                         $m =~ s/\.xml$//;
119                         return $m;
120                 }
121                 $f = find_file($f);
122                 $f =~ s/^[.]\///;
123                 $ret .= ' ' . $f;
124         }
125         $ret = strlist($ret);
126         my @list = split(/\s+/, $ret);
127         @list = uniq(@list);
128         $ret = trim(join(' ', @list));
129         return $ret;
130 }
131
132 sub read_config_mk($)
133 {
134         my $filename = shift;
135         my @lines = read_file($filename);
136         my $prev = "";
137         my $linenum = 1;
138         my $section = "GLOBAL";
139         my $infragment;
140         my $result;
141         my $line = "";
142         my $secnumber = 1;
143
144         $result->{"GLOBAL"}->{SECNUMBER} = $secnumber++;
145
146         foreach (@lines) {
147                 $linenum++;
148
149                 # lines beginning with '#' are ignored
150                 next if (/^\#.*$/);
151
152                 if (/^(.*)\\$/) {
153                         $prev .= $1;
154                         next;
155                 } else {
156                         $line = "$prev$_";
157                         $prev = "";
158                 }
159
160                 if ($line =~ /^mkinclude.*asn1_deps.pl\s+([^\s]+)\s+([^\s]+)\s+\\\$\\\(\w+\\\)\/([^\s|]+)\s*([^|]*)\|$/) {
161                         my $src = $1;
162                         $section = $2;
163                         my $dir = $3;
164                         my $options = $4;
165                         $section = "HEIMDAL_" . uc($section);
166                         $result->{$section}->{TYPE} = 'ASN1';
167                         $result->{$section}->{SECNUMBER} = $secnumber++;
168                         if ($options ne '') {
169                                 $result->{$section}->{OPTIONS} = $options;
170                         }
171                         $result->{$section}->{DIRECTORY} = $dir;
172                         $result->{$section}->{$section . '_OBJ_FILES'} = $src;
173                         next;
174                 }
175
176                 if ($line =~ /^mkinclude.*et_deps.pl\s+([^\s]+)\s+\\\$\\\(\w+\\\)\/([^\s|]+)\|$/) {
177                         my $src = $1;
178                         my $dir = $2;
179                         $section = basename($src);
180                         $section =~ s/\./_/g;
181                         $section = "HEIMDAL_" . uc($section);
182                         $result->{$section}->{TYPE} = 'ERRTABLE';
183                         $result->{$section}->{SECNUMBER} = $secnumber++;
184                         $result->{$section}->{DIRECTORY} = "$dir";
185                         $result->{$section}->{$section . '_OBJ_FILES'} = $src;
186                         next;
187                 }
188
189                 if ($line =~ /^\[(\w+)::([\w-]+)\]/)
190                 {
191                         my $type = $1;
192                         $section = $2;
193                         $infragment = 0;
194
195                         $result->{$section}->{TYPE} = $type;
196                         $result->{$section}->{SECNUMBER} = $secnumber++;
197                         next;
198                 }
199
200                 # include
201                 if ($line =~ /^mkinclude (.*)$/) {
202                         my $subfile = $1;
203                         $result->{$subfile}->{TYPE} = 'SUBCONFIG';
204                         $result->{$subfile}->{SECNUMBER} = $secnumber++;
205                         next;
206                 }
207
208                 # empty line
209                 if ($line =~ /^[ \t]*$/) {
210                         next;
211                 }
212
213                 # global stuff is considered part of the makefile
214                 if ($section eq "GLOBAL") {
215                         $infragment = 1;
216                         next;
217                 }
218
219                 # Assignment
220                 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*=(.*)$/) {
221                         $result->{$section}->{$1} = expand_vars($result->{$section}, strlist($2));
222                         $globals->{$1} = $result->{$section}->{$1};
223                         next;
224                 }
225
226                 # +=
227                 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*\+=(.*)$/) {
228                         if (!$result->{$section}->{$1}) {
229                                 $result->{$section}->{$1}="";
230                         }
231                         $result->{$section}->{$1} .= " " . expand_vars($result->{$section}, strlist($2));
232                         $globals->{$1} = $result->{$section}->{$1};
233                         next;
234                 }
235
236                 if ($line =~ /\$\(eval.\$\(call.proto_header_template.*,(.*),.*/) {
237                         $result->{$section}->{AUTOPROTO} = $1;
238                 }
239                 if ($line =~ /^\$\(eval/) {
240                         # skip eval lines for now
241                         next;
242                 }
243
244                 printf(STDERR "$linenum: Bad line: $line");
245         }
246
247         return $result;
248 }
249
250
251 sub process_results($)
252 {
253         my $result = shift;
254
255         foreach my $s (sort {$result->{$a}->{SECNUMBER} <=> $result->{$b}->{SECNUMBER}} keys %{$result}) {
256                 next if ($s eq "GLOBAL");
257                 my $sec = $result->{$s};
258                 if ($sec->{TYPE} eq "SUBCONFIG") {
259                         my $d = dirname($s);
260                         next if ($d eq ".");
261                         printf "bld.BUILD_SUBDIR('%s')\n", dirname($s);
262                 } else {
263                         printf "\nbld.SAMBA_%s('%s'", $sec->{TYPE}, $s;
264                         my $trailer="";
265                         my $got_src = 0;
266
267                         foreach my $k (keys %{$sec}) {
268                                 #print "key=$k\n";
269
270                                 next if ($k eq "SECNUMBER");
271                                 next if ($k eq "TYPE");
272                                 if ($k eq "INIT_FUNCTION") {
273                                         $trailer .= sprintf(",\n\tinit_function='%s'", trim($sec->{$k}));
274                                         next;
275                                 }
276                                 if ($k eq "INIT_FUNCTION_SENTINEL") {
277                                         $trailer .= sprintf(",\n\tinit_function_sentinal='%s'", trim($sec->{$k}));
278                                         next;
279                                 }
280                                 if ($k eq "_PY_FILES" ||
281                                     $k eq "EPYDOC_OPTIONS" ||
282                                     $k eq "COV_TARGET" ||
283                                     $k eq "GCOV" ||
284                                     $k eq "PC_FILES" ||
285                                     $k eq "CONFIG4FILE" ||
286                                     $k eq "LMHOSTSFILE4") {
287                                         $trailer .= sprintf(",\n\t# %s='%s'", $k, trim($sec->{$k}));
288                                         next;
289                                 }
290                                 if ($k eq "SUBSYSTEM") {
291                                         $trailer .= sprintf(",\n\tsubsystem='%s'", trim($sec->{$k}));
292                                         next;
293                                 }
294                                 if ($k eq "PRIVATE_DEPENDENCIES") {
295                                         $trailer .= sprintf(",\n\tdeps='%s'", strlist($sec->{$k}));
296                                         next;
297                                 }
298                                 if ($k eq "PUBLIC_DEPENDENCIES") {
299                                         $trailer .= sprintf(",\n\tpublic_deps='%s'", strlist($sec->{$k}));
300                                         next;
301                                 }
302                                 if ($k eq "ALIASES") {
303                                         $trailer .= sprintf(",\n\taliases='%s'", strlist($sec->{$k}));
304                                         next;
305                                 }
306                                 if ($k eq "CFLAGS") {
307                                         $trailer .= sprintf(",\n\tcflags='%s'", strlist($sec->{$k}));
308                                         next;
309                                 }
310                                 if ($k eq "OPTIONS") {
311                                         $trailer .= sprintf(",\n\toptions='%s'", strlist($sec->{$k}));
312                                         next;
313                                 }
314                                 if ($k eq "DIRECTORY") {
315                                         $trailer .= sprintf(",\n\tdirectory='%s'", strlist($sec->{$k}));
316                                         next;
317                                 }
318                                 if ($k eq "LDFLAGS") {
319                                         $trailer .= sprintf(",\n\tldflags='%s'", strlist($sec->{$k}));
320                                         next;
321                                 }
322                                 if ($k eq "INSTALLDIR") {
323                                         $trailer .= sprintf(",\n\tinstalldir='%s'", strlist($sec->{$k}));
324                                         next;
325                                 }
326                                 if ($k eq "ASN1C") {
327                                         $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
328                                         next;
329                                 }
330                                 if ($k eq "ET_COMPILER") {
331                                         $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
332                                         next;
333                                 }
334                                 if ($k eq "ENABLE") {
335                                         my $v = strlist($sec->{$k});
336                                         if ($v eq "NO") {
337                                                 $trailer .= sprintf(",\n\tenabled=False");
338                                                 next;
339                                         }
340                                         next if ($v eq "YES");
341                                         die("Unknown ENABLE value $v in $s\n");
342                                 }
343                                 if ($k eq "USE_HOSTCC") {
344                                         my $v = strlist($sec->{$k});
345                                         if ($v eq "YES") {
346                                                 $trailer .= sprintf(",\n\tuse_hostcc=True");
347                                                 next;
348                                         }
349                                         next if ($v eq "NO");
350                                         die("Unknown HOST_CC value $v in $s\n");
351                                 }
352                                 if ($k eq "$s" . "_VERSION") {
353                                         $trailer .= sprintf(",\n\tvnum='%s'", strlist($sec->{$k}));
354                                         next;
355                                 }
356                                 if ($k eq "$s" . "_SOVERSION") {
357                                         next;
358                                 }
359                                 if ($k eq "LIBRARY_REALNAME") {
360                                         $trailer .= sprintf(",\n\trealname='%s'", strlist($sec->{$k}));
361                                         next;
362                                 }
363                                 if ($k eq "OUTPUT_TYPE") {
364                                         $trailer .= sprintf(",\n\toutput_type='%s'", strlist($sec->{$k}));
365                                         next;
366                                 }
367                                 if ($k eq "AUTOPROTO") {
368                                         my $list = trim(find_files(strlist($sec->{$k})));
369                                         $trailer .= sprintf(",\n\tautoproto='%s'", $list);
370                                         next;
371                                 }
372                                 if ($k eq "PUBLIC_HEADERS") {
373                                         my $list = trim(strlist($sec->{$k}));
374                                         if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
375                                                 $list = trim("$1 $2");
376                                                 $list = find_files($list);
377                                         } else {
378                                                 $list = trim(find_files(strlist($sec->{$k})));
379                                         }
380                                         $trailer .= sprintf(",\n\tpublic_headers='%s'", $list);
381                                         next;
382                                 }
383                                 if ($k eq "MANPAGES") {
384                                         my $list = trim(find_files(strlist($sec->{$k})));
385                                         $trailer .= sprintf(",\n\tmanpages='%s'", $list);
386                                         next;
387                                 }
388                                 if ($k eq "$s" . "_OBJ_FILES") {
389                                         my $list = trim(strlist($sec->{$k}));
390                                         $list =~ s/\.o/.c/g;
391                                         $list =~ s/\.ho/.c/g;
392                                         if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
393                                                 $list = trim("$1 $2");
394                                                 $list = find_files($list);
395                                                 $list = "'$list'";
396                                         } elsif ($list =~ /\$\(addprefix \$\((\w+)\)(.*),(.*)\)(.*)$/) {
397                                                 my $src = trim($3);
398                                                 my $dir = "$1$2";
399                                                 $dir =~ s/\/$//;
400                                                 my $res = "bld.SUBDIR('$dir', '$src')";
401                                                 if ($4) {
402                                                         $res = "$res + '$4'";
403                                                 }
404                                                 $list = $res;
405                                         } else {
406                                                 $list = find_files($list);
407                                                 $list="'$list'";
408                                         }
409                                         $list =~ s/\$\(\w+srcdir\)\///g;
410                                         printf(",\n\t%s", $list);
411                                         $got_src = 1;
412                                         next;
413                                 }
414                                 if ($k eq "HEIMDAL_GSSAPI_KRB5_OBJ_FILES" ||
415                                     $k eq "HEIMDAL_GSSAPI_SPNEGO_OBJ_FILES" ||
416                                     $k eq "HEIMDAL_HEIM_ASN1_DER_OBJ_FILES" ||
417                                     $k eq "HEIMDAL_HX509_OBJH_FILES" ||
418                                     $k eq "HEIMDAL_HX509_OBJG_FILES" ||
419                                     $k eq "HEIMDAL_ROKEN_OBJ_FILES"
420                                     ) {
421                                         next;
422                                 }
423                                 die("Unknown keyword $k in $s\n");
424                         }
425                         die("No source list in $s\n") unless $got_src;
426                         printf("%s\n\t)\n\n", $trailer);
427                 }
428         }
429 }
430
431 for (my $i=0; $i <= $#ARGV; $i++) {
432         my $filename=$ARGV[$i];
433         $dname=dirname($filename);
434         my $result = read_config_mk($filename);
435         if ($i != 0) {
436                 print "\n\n\n";
437         }
438         print "# AUTOGENERATED by mktowscript.pl from $filename\n# Please remove this notice if hand editing\n\n";
439         die("Unable to chdir to $dname\n") unless chdir($dname);
440         process_results($result);
441 }
442