build: first attempt at a script to auto-convert config.mk files to wscript
[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
10 sub read_file($)
11 {
12         my $filename = shift;
13         open(CONFIG_MK, "$filename");
14         my @lines = <CONFIG_MK>;
15         close(CONFIG_MK);
16         return @lines;
17 }
18
19 sub trim($)
20 {
21         my $string = shift;
22         $string =~ s/^\s+//;
23         $string =~ s/\s+$//;
24         return $string;
25 }
26
27 sub strlist($)
28 {
29         my $s = shift;
30         $s =~ s/\$\(SHLIBEXT\)/so/g;
31         $s =~ s/\$\(heimdalsrcdir\)/..\/heimdal/g;
32         $s =~ s/\$\(heimdalbuildsrcdir\)/..\/heimdal_build/g;
33         $s =~ s/\$\(nsswitchsrcdir\)/..\/nsswitch/g;
34         $s =~ s/\$\(param_OBJ_FILES\)/..\/pyparam.c/g;
35         $s =~ s/\$\(libclisrcdir\)\///g;
36
37         return trim(join(' ', split(/\s+/, $s)));
38 }
39
40 sub find_file($)
41 {
42         my $f = shift;
43         my $orig = $f;
44
45         my $b = basename($f);
46         return $b if (-e $b);
47
48         return $f if (-e $f);
49         while ($f =~ /\//) {
50                 $f =~ s/^[^\/]+\///g;
51                 #printf(STDERR "Trying $f in $dname\n");
52                 return $f if (-e $f);
53         }
54         my $f2;
55         $f2 = `find . -name $f -type f`;
56         return $f2 unless ($f2 eq "");
57         $f2 = `find .. -name $f -type f`;
58         return $f2 unless ($f2 eq "");
59         $f2 = `find ../.. -name $f -type f`;
60         return $f2 unless ($f2 eq "");
61         $f2 = `find ../../.. -name $f -type f`;
62         return $f2 unless ($f2 eq "");
63         printf(STDERR "Failed to find $orig in $dname\n");
64         exit(1);
65         return '';
66 }
67
68 sub find_files($)
69 {
70         my $list = shift;
71         my $ret = '';
72         foreach my $f (split(/\s+/, $list)) {
73                 $f = find_file($f);
74                 $f =~ s/^[.]\///;
75                 $ret .= ' ' . $f;
76         }
77         return strlist($ret);
78 }
79
80 sub read_config_mk($)
81 {
82         my $filename = shift;
83         my @lines = read_file($filename);
84         my $prev = "";
85         my $linenum = 1;
86         my $section = "GLOBAL";
87         my $infragment;
88         my $result;
89         my $line = "";
90         my $secnumber = 1;
91
92         $result->{"GLOBAL"}->{SECNUMBER} = $secnumber++;
93
94         foreach (@lines) {
95                 $linenum++;
96
97                 # lines beginning with '#' are ignored
98                 next if (/^\#.*$/);
99
100                 if (/^(.*)\\$/) {
101                         $prev .= $1;
102                         next;
103                 } else {
104                         $line = "$prev$_";
105                         $prev = "";
106                 }
107
108                 if ($line =~ /^\[(\w+)::([\w-]+)\]/)
109                 {
110                         my $type = $1;
111                         $section = $2;
112                         $infragment = 0;
113
114                         $result->{$section}->{TYPE} = $type;
115                         $result->{$section}->{SECNUMBER} = $secnumber++;
116                         next;
117                 }
118
119                 # include
120                 if ($line =~ /^mkinclude (.*)$/) {
121                         my $subfile = $1;
122                         $result->{$subfile}->{TYPE} = 'SUBCONFIG';
123                         $result->{$subfile}->{SECNUMBER} = $secnumber++;
124                         next;
125                 }
126
127                 # empty line
128                 if ($line =~ /^[ \t]*$/) {
129                         next;
130                 }
131
132                 # global stuff is considered part of the makefile
133                 if ($section eq "GLOBAL") {
134                         $infragment = 1;
135                         next;
136                 }
137
138                 # Assignment
139                 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*=(.*)$/) {
140                         $result->{$section}->{$1} = $2;
141                         next;
142                 }
143
144                 # +=
145                 if ($line =~ /^([a-zA-Z0-9_-]+)[\t ]*\+=(.*)$/) {
146                         if (!$result->{$section}->{$1}) {
147                                 $result->{$section}->{$1}="";
148                         }
149                         $result->{$section}->{$1} .= " " . $2;
150                         next;
151                 }
152
153                 if ($line =~ /^\$\(eval/) {
154                         # skip eval lines for now
155                         next;
156                 }
157
158                 printf(STDERR "$linenum: Bad line: $line\n");
159         }
160
161         return $result;
162 }
163
164
165 my $result = read_config_mk($filename);
166
167 #print Dumper $result;
168
169 print "# AUTOGENERATED by mktowscript.pl\n# Please remove this notice if hand editing\n\n";
170
171 chdir($dname);
172
173 foreach my $s (sort {$result->{$a}->{SECNUMBER} <=> $result->{$b}->{SECNUMBER}} keys %{$result}) {
174     next if ($s eq "GLOBAL");
175     my $sec = $result->{$s};
176     if ($sec->{TYPE} eq "SUBCONFIG") {
177             my $d = dirname($s);
178             next if ($d eq ".");
179             printf "bld.add_subdirs('%s')\n", dirname($s);
180     } else {
181             printf "\nbld.SAMBA_%s('%s'", $sec->{TYPE}, $s;
182             my $trailer="";
183             my $got_src = 0;
184
185             foreach my $k (keys %{$sec}) {
186                     #print "key=$k\n";
187
188                     next if ($k eq "SECNUMBER");
189                     next if ($k eq "TYPE");
190                     if ($k eq "INIT_FUNCTION") {
191                             $trailer .= sprintf(",\n\tinit_function='%s'", trim($sec->{$k}));
192                             next;
193                     }
194                     if ($k eq "INIT_FUNCTION_SENTINEL") {
195                             $trailer .= sprintf(",\n\tinit_function_sentinal='%s'", trim($sec->{$k}));
196                             next;
197                     }
198                     if ($k eq "_PY_FILES" ||
199                         $k eq "EPYDOC_OPTIONS" ||
200                         $k eq "COV_TARGET" ||
201                         $k eq "GCOV" ||
202                         $k eq "PC_FILES" ||
203                         $k eq "PUBLIC_HEADERS" ||
204                         $k eq "MANPAGES" ||
205                         $k eq "CONFIG4FILE" ||
206                         $k eq "LMHOSTSFILE4") {
207                             $trailer .= sprintf(",\n\t# %s='%s'", $k, trim($sec->{$k}));
208                             next;
209                     }
210                     if ($k eq "SUBSYSTEM") {
211                             $trailer .= sprintf(",\n\tsubsystem='%s'", trim($sec->{$k}));
212                             next;
213                     }
214                     if ($k eq "PRIVATE_DEPENDENCIES") {
215                             $trailer .= sprintf(",\n\tdeps='%s'", strlist($sec->{$k}));
216                             next;
217                     }
218                     if ($k eq "PUBLIC_DEPENDENCIES") {
219                             $trailer .= sprintf(",\n\tpublic_deps='%s'", strlist($sec->{$k}));
220                             next;
221                     }
222                     if ($k eq "ALIASES") {
223                             $trailer .= sprintf(",\n\taliases='%s'", strlist($sec->{$k}));
224                             next;
225                     }
226                     if ($k eq "CFLAGS") {
227                             $trailer .= sprintf(",\n\tcflags='%s'", strlist($sec->{$k}));
228                             next;
229                     }
230                     if ($k eq "LDFLAGS") {
231                             $trailer .= sprintf(",\n\tldflags='%s'", strlist($sec->{$k}));
232                             next;
233                     }
234                     if ($k eq "INSTALLDIR") {
235                             $trailer .= sprintf(",\n\tinstalldir='%s'", strlist($sec->{$k}));
236                             next;
237                     }
238                     if ($k eq "ENABLE") {
239                             my $v = strlist($sec->{$k});
240                             if ($v eq "NO") {
241                                     $trailer .= sprintf(",\n\tenabled=False");
242                                     next;
243                             }
244                             next if ($v eq "YES");
245                             die("Unknown ENABLE value $v in $s\n");
246                     }
247                     if ($k eq "$s" . "_VERSION") {
248                             $trailer .= sprintf(",\n\tvnum='%s'", strlist($sec->{$k}));
249                             next;
250                     }
251                     if ($k eq "$s" . "_SOVERSION") {
252                             next;
253                     }
254                     if ($k eq "LIBRARY_REALNAME") {
255                             $trailer .= sprintf(",\n\trealname='%s'", strlist($sec->{$k}));
256                             next;
257                     }
258                     if ($k eq "OUTPUT_TYPE") {
259                             $trailer .= sprintf(",\n\toutput_type='%s'", strlist($sec->{$k}));
260                             next;
261                     }
262                     if ($k eq "$s" . "_OBJ_FILES") {
263                             my $list = trim(strlist($sec->{$k}));
264                             $list =~ s/\.o/.c/g;
265                             if ($list =~ /\$\(addprefix .*,(.*)\)(.*)$/) {
266                                     $list = trim("$1 $2");
267                                     $list = find_files($list);
268                                     $list = "'$list'";
269                             } elsif ($list =~ /\$\(addprefix \$\((\w+)\)(.*),(.*)\)(.*)$/) {
270                                     my $src = trim($3);
271                                     my $dir = "$1$2";
272                                     $dir =~ s/\/$//;
273                                     my $res = "bld.SUBDIR('$dir', '$src')";
274                                     if ($4) {
275                                             $res = "$res + '$4'";
276                                     }
277                                     $list = $res;
278                             } else {
279                                     $list = find_files($list);
280                                     $list="'$list'";
281                             }
282                             $list =~ s/\$\(\w+srcdir\)\///g;
283                             printf(",\n\t%s", $list);
284                             $got_src = 1;
285                             next;
286                     }
287                     die("Unknown keyword $k in $s\n");
288             }
289             die("No source list in $s\n") unless $got_src;
290             printf("%s\n\t)\n\n", $trailer);
291     }
292 }
293
294 #print Dumper $result;