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