98c1b7ed32bcac666cbe380ac5fc94dcb0fd7ed3
[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                         my $got_private_deps = 0;
267
268                         foreach my $k (keys %{$sec}) {
269                                 #print "key=$k\n";
270
271                                 next if ($k eq "SECNUMBER");
272                                 next if ($k eq "TYPE");
273                                 if ($k eq "INIT_FUNCTION") {
274                                         $trailer .= sprintf(",\n\tinit_function='%s'", trim($sec->{$k}));
275                                         next;
276                                 }
277                                 if ($k eq "INIT_FUNCTION_SENTINEL") {
278                                         $trailer .= sprintf(",\n\tinit_function_sentinal='%s'", trim($sec->{$k}));
279                                         next;
280                                 }
281                                 if ($k eq "_PY_FILES" ||
282                                     $k eq "EPYDOC_OPTIONS" ||
283                                     $k eq "COV_TARGET" ||
284                                     $k eq "GCOV" ||
285                                     $k eq "PC_FILES" ||
286                                     $k eq "CONFIG4FILE" ||
287                                     $k eq "LMHOSTSFILE4") {
288                                         $trailer .= sprintf(",\n\t# %s='%s'", $k, trim($sec->{$k}));
289                                         next;
290                                 }
291                                 if ($k eq "SUBSYSTEM") {
292                                         $trailer .= sprintf(",\n\tsubsystem='%s'", trim($sec->{$k}));
293                                         next;
294                                 }
295                                 if ($k eq "PRIVATE_DEPENDENCIES") {
296                                         $trailer .= sprintf(",\n\tdeps='%s'", strlist($sec->{$k}));
297                                         $got_private_deps = 1;
298                                         next;
299                                 }
300                                 if ($k eq "PUBLIC_DEPENDENCIES") {
301                                         $trailer .= sprintf(",\n\tpublic_deps='%s'", strlist($sec->{$k}));
302                                         next;
303                                 }
304                                 if ($k eq "ALIASES") {
305                                         $trailer .= sprintf(",\n\taliases='%s'", strlist($sec->{$k}));
306                                         next;
307                                 }
308                                 if ($k eq "CFLAGS") {
309                                         $trailer .= sprintf(",\n\tcflags='%s'", strlist($sec->{$k}));
310                                         next;
311                                 }
312                                 if ($k eq "OPTIONS") {
313                                         $trailer .= sprintf(",\n\toptions='%s'", strlist($sec->{$k}));
314                                         next;
315                                 }
316                                 if ($k eq "DIRECTORY") {
317                                         $trailer .= sprintf(",\n\tdirectory='%s'", strlist($sec->{$k}));
318                                         next;
319                                 }
320                                 if ($k eq "LDFLAGS") {
321                                         $trailer .= sprintf(",\n\tldflags='%s'", strlist($sec->{$k}));
322                                         next;
323                                 }
324                                 if ($k eq "INSTALLDIR") {
325                                         $trailer .= sprintf(",\n\tinstalldir='%s'", strlist($sec->{$k}));
326                                         next;
327                                 }
328                                 if ($k eq "ASN1C") {
329                                         $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
330                                         next;
331                                 }
332                                 if ($k eq "ET_COMPILER") {
333                                         $trailer .= sprintf(",\n\tcompiler='%s'", strlist($sec->{$k}));
334                                         next;
335                                 }
336                                 if ($k eq "ENABLE") {
337                                         my $v = strlist($sec->{$k});
338                                         if ($v eq "NO") {
339                                                 $trailer .= sprintf(",\n\tenabled=False");
340                                                 next;
341                                         }
342                                         next if ($v eq "YES");
343                                         die("Unknown ENABLE value $v in $s\n");
344                                 }
345                                 if ($k eq "USE_HOSTCC") {
346                                         my $v = strlist($sec->{$k});
347                                         if ($v eq "YES") {
348                                                 $trailer .= sprintf(",\n\tuse_hostcc=True");
349                                                 next;
350                                         }
351                                         next if ($v eq "NO");
352                                         die("Unknown HOST_CC value $v in $s\n");
353                                 }
354                                 if ($k eq "$s" . "_VERSION") {
355                                         $trailer .= sprintf(",\n\tvnum='%s'", strlist($sec->{$k}));
356                                         next;
357                                 }
358                                 if ($k eq "$s" . "_SOVERSION") {
359                                         next;
360                                 }
361                                 if ($k eq "LIBRARY_REALNAME") {
362                                         $trailer .= sprintf(",\n\trealname='%s'", strlist($sec->{$k}));
363                                         next;
364                                 }
365                                 if ($k eq "OUTPUT_TYPE") {
366                                         $trailer .= sprintf(",\n\toutput_type='%s'", strlist($sec->{$k}));
367                                         next;
368                                 }
369                                 if ($k eq "AUTOPROTO") {
370                                         my $list = trim(find_files(strlist($sec->{$k})));
371                                         $trailer .= sprintf(",\n\tautoproto='%s'", $list);
372                                         next;
373                                 }
374                                 if ($k eq "PUBLIC_HEADERS") {
375                                         my $list = trim(strlist($sec->{$k}));
376                                         if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
377                                                 $list = trim("$1 $2");
378                                                 $list = find_files($list);
379                                         } else {
380                                                 $list = trim(find_files(strlist($sec->{$k})));
381                                         }
382                                         $trailer .= sprintf(",\n\tpublic_headers='%s'", $list);
383                                         next;
384                                 }
385                                 if ($k eq "MANPAGES") {
386                                         my $list = trim(find_files(strlist($sec->{$k})));
387                                         $trailer .= sprintf(",\n\tmanpages='%s'", $list);
388                                         next;
389                                 }
390                                 if ($k eq "$s" . "_OBJ_FILES") {
391                                         my $list = trim(strlist($sec->{$k}));
392                                         $list =~ s/\.o/.c/g;
393                                         $list =~ s/\.ho/.c/g;
394                                         if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
395                                                 $list = trim("$1 $2");
396                                                 $list = find_files($list);
397                                                 $list = "'$list'";
398                                         } elsif ($list =~ /\$\(addprefix \$\((\w+)\)(.*),(.*)\)(.*)$/) {
399                                                 my $src = trim($3);
400                                                 my $dir = "$1$2";
401                                                 $dir =~ s/\/$//;
402                                                 my $res = "bld.SUBDIR('$dir', '$src')";
403                                                 if ($4) {
404                                                         $res = "$res + '$4'";
405                                                 }
406                                                 $list = $res;
407                                         } else {
408                                                 $list = find_files($list);
409                                                 $list="'$list'";
410                                         }
411                                         $list =~ s/\$\(\w+srcdir\)\///g;
412                                         printf(",\n\tsource=%s", $list);
413                                         $got_src = 1;
414                                         next;
415                                 }
416                                 if ($k eq "HEIMDAL_GSSAPI_KRB5_OBJ_FILES" ||
417                                     $k eq "HEIMDAL_GSSAPI_SPNEGO_OBJ_FILES" ||
418                                     $k eq "HEIMDAL_HEIM_ASN1_DER_OBJ_FILES" ||
419                                     $k eq "HEIMDAL_HX509_OBJH_FILES" ||
420                                     $k eq "HEIMDAL_HX509_OBJG_FILES" ||
421                                     $k eq "HEIMDAL_ROKEN_OBJ_FILES"
422                                     ) {
423                                         next;
424                                 }
425                                 die("Unknown keyword $k in $s\n");
426                         }
427                         die("No source list in $s\n") unless $got_src or $got_private_deps;
428                         if (! $got_src) {
429                                 printf(",source=''\n\t");
430                         }
431                         printf("%s\n\t)\n\n", $trailer);
432                 }
433         }
434 }
435
436 for (my $i=0; $i <= $#ARGV; $i++) {
437         my $filename=$ARGV[$i];
438         $dname=dirname($filename);
439         my $result = read_config_mk($filename);
440         if ($i != 0) {
441                 print "\n\n\n";
442         }
443         print "# AUTOGENERATED by mktowscript.pl from $filename\n# Please remove this notice if hand editing\n\n";
444         die("Unable to chdir to $dname\n") unless chdir($dname);
445         process_results($result);
446 }
447