makefiles: Automatically add missing source variables in make_makefiles.
[wine] / tools / make_makefiles
1 #!/usr/bin/perl -w
2 #
3 # Build the auto-generated parts of the Wine makefiles.
4 #
5 # Copyright 2006 Alexandre Julliard
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #
21
22 use strict;
23
24 # Make rules files
25 my %makerules =
26 (
27  "MAKE_RULES" => "Make.rules",
28  "MAKE_DLL_RULES" => "dlls/Makedll.rules",
29  "MAKE_IMPLIB_RULES" => "dlls/Makeimplib.rules",
30  "MAKE_TEST_RULES" => "Maketest.rules",
31  "MAKE_PROG_RULES" => "programs/Makeprog.rules",
32 );
33
34 # Programs that we want to install in the bin directory too
35 my %bin_install =
36 (
37   "msiexec" => 1,
38   "notepad" => 1,
39   "regedit" => 1,
40   "regsvr32" => 1,
41   "wineboot" => 1,
42   "winecfg" => 1,
43   "wineconsole" => 1,
44   "winedbg" => 1,
45   "winefile" => 1,
46   "winemine" => 1,
47   "winepath" => 1,
48 );
49
50 # Programs that we don't want to install at all
51 my %dont_install =
52 (
53   "cmdlgtst" => 1,
54   "view" => 1,
55   "winetest" => 1,
56 );
57
58 # Dlls and programs that are 16-bit specific
59 my %modules16 =
60 (
61   "ifsmgr.vxd" => 1,
62   "mmdevldr.vxd" => 1,
63   "monodebg.vxd" => 1,
64   "vdhcp.vxd" => 1,
65   "vmm.vxd" => 1,
66   "vnbt.vxd" => 1,
67   "vnetbios.vxd" => 1,
68   "vtdapi.vxd" => 1,
69   "vwin32.vxd" => 1,
70   "w32skrnl.dll" => 1,
71   "winevdm.exe" => 1,
72   "wow32.dll" => 1,
73 );
74
75 # Default patterns for top-level .gitignore
76 my @ignores = (
77     "*.[oa]",
78     "*.fake",
79     "*.man",
80     "*.ok",
81     "*.res",
82     "*.so",
83     "/autom4te.cache",
84     "/config.cache",
85     "/config.log",
86     "/config.status",
87     "/configure.lineno",
88     "/TAGS",
89     "/tags",
90     "/wine",
91     "Makefile",
92     "dlldata.c",
93     "dlls/*/*.def",
94     "dlls/shell32/AUTHORS",
95     "*/*/tests/*crosstest.exe",
96     "*/*/tests/testlist.c",
97     "include/config.h",
98     "include/stamp-h",
99     "programs/winetest/*_test.exe",
100     "programs/winetest/*_test.rc",
101     "programs/winetest/build.nfo",
102     "programs/winetest/build.rc",
103     "tools/makedep",
104 );
105
106 # Source files and their resulting target to ignore
107 my @ignore_srcs = (
108     [ 'BISON_SRCS',   '\.y',   '.tab.c' ],
109     [ 'BISON_SRCS',   '\.y',   '.tab.h' ],
110     [ 'LEX_SRCS',     '\.l',   '.yy.c' ],
111     [ 'MC_SRCS',      '\.mc',  '.mc.rc' ],
112     [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
113     [ 'IDL_H_SRCS',   '\.idl', '.h' ],
114     [ 'IDL_C_SRCS',   '\.idl', '.h' ],
115     [ 'IDL_I_SRCS',   '\.idl', '.h' ],
116     [ 'IDL_P_SRCS',   '\.idl', '.h' ],
117     [ 'IDL_S_SRCS',   '\.idl', '.h' ],
118     [ 'IDL_C_SRCS',   '\.idl', '_c.c' ],
119     [ 'IDL_I_SRCS',   '\.idl', '_i.c' ],
120     [ 'IDL_P_SRCS',   '\.idl', '_p.c' ],
121     [ 'IDL_S_SRCS',   '\.idl', '_s.c' ],
122 );
123
124 my %exported_wine_headers = (
125     "wine/debug.h" => 1,
126     "wine/exception.h" => 1,
127     "wine/library.h" => 1,
128     "wine/unicode.h" => 1,
129     "wine/itss.idl" => 1,
130     "wine/svcctl.idl" => 1,
131 );
132
133 my %private_idl_headers = (
134     "access.idl" => 1,
135     "asynot.idl" => 1,
136     "asysta.idl" => 1,
137     "axcore.idl" => 1,
138     "axextend.idl" => 1,
139     "binres.idl" => 1,
140     "cmdbas.idl" => 1,
141     "cmdtxt.idl" => 1,
142     "crtrow.idl" => 1,
143     "dbccmd.idl" => 1,
144     "dbcses.idl" => 1,
145     "dbdsad.idl" => 1,
146     "dbinit.idl" => 1,
147     "dbprop.idl" => 1,
148     "dbs.idl" => 1,
149     "devenum.idl" => 1,
150     "dyngraph.idl" => 1,
151     "opnrst.idl" => 1,
152     "row.idl" => 1,
153     "rowchg.idl" => 1,
154     "rstbas.idl" => 1,
155     "rstinf.idl" => 1,
156     "rstloc.idl" => 1,
157     "sesprp.idl" => 1,
158     "vmrender.idl" => 1,
159     "xmldom.idl" => 1,
160     "xmldso.idl" => 1,
161     "wine/wined3d.idl" => 1,
162     "wine/winedxgi.idl" => 1,
163 );
164
165 my %ignored_source_files = (
166     "dlls/wineps.drv/afm2c.c" => 1,
167     "dlls/wineps.drv/mkagl.c" => 1,
168     "programs/winetest/dist.rc" => 1,
169 );
170
171 my (@all_files, @makefiles, %makefiles);
172
173 sub dirname($)
174 {
175     my $ret = shift;
176     return "" unless $ret =~ /\//;
177     $ret =~ s!/[^/]*$!!;
178     return $ret;
179 }
180
181 # update a file if changed
182 sub update_file($)
183 {
184     my $file = shift;
185     my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
186     if (!$ret)
187     {
188         unlink "$file.new";
189     }
190     else
191     {
192         rename "$file.new", "$file";
193         print "$file updated\n";
194         if ($file eq "configure.ac")
195         {
196             system "autoconf";
197             print "configure updated\n";
198         }
199     }
200     return $ret;
201 }
202
203 # replace some lines in a file between two markers
204 sub replace_in_file($$$@)
205 {
206     my $file = shift;
207     my $start = shift;
208     my $end = shift;
209
210     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
211
212     if (defined($start))
213     {
214         open OLD_FILE, "$file" or die "cannot open $file";
215         while (<OLD_FILE>)
216         {
217             last if /$start/;
218             print NEW_FILE $_;
219         }
220     }
221
222     print NEW_FILE @_;
223
224     if (defined($end))
225     {
226         my $skip=1;
227         while (<OLD_FILE>)
228         {
229             print NEW_FILE $_ unless $skip;
230             $skip = 0 if /$end/;
231         }
232     }
233
234     close OLD_FILE if defined($start);
235     close NEW_FILE;
236     return update_file($file);
237 }
238
239 # replace a variable in a makefile
240 sub replace_makefile_variable($$)
241 {
242     my ($file, $var) = @_;
243     my $make = $makefiles{$file};
244     my $replaced = 0;
245
246     return unless defined ${$make}{"=$var"};
247
248     my @values = @{${$make}{"=$var"}};
249     ${$make}{$var} = \@values;
250
251     open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
252
253     open OLD_FILE, "$file.in" or die "cannot open $file.in";
254     while (<OLD_FILE>)
255     {
256         if (/^\s*($var\s*)=/)
257         {
258             # try to preserve formatting
259             my $prefix = $1;
260             my $multiline = /\\$/ || (@values > 1);
261             while (/\\$/)
262             {
263                 $_ = <OLD_FILE>;
264                 last unless $_;
265             }
266             if ($multiline)
267             {
268                 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
269             }
270             else
271             {
272                 print NEW_FILE "$prefix= @values\n";
273             }
274             $replaced = 1;
275             next;
276         }
277         if (/^\@MAKE/ && !$replaced)
278         {
279             print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
280         }
281         print NEW_FILE $_;
282     }
283     close OLD_FILE;
284     close NEW_FILE;
285     return update_file("$file.in");
286 }
287
288 # parse the specified makefile to identify the rules file
289 sub parse_makefile($)
290 {
291     my $file = shift;
292     my %make;
293
294     ($make{"=dir"} = $file) =~ s/[^\/]+$//;
295
296     open MAKE, "$file.in" or die "cannot open $file.in\n";
297
298     while (<MAKE>)
299     {
300         chomp;
301         next if (/^\s*#/);
302         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
303         next if (/^\s*$/);
304
305         if (/^\@(MAKE.*RULES)\@/)
306         {
307             my $var = $1;
308             $make{"=rules"} = $makerules{$var};
309             next;
310         }
311         if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
312         {
313             $make{$1} = $2;
314             next;
315         }
316         if (/^\s*(BISON_SRCS|LEX_SRCS|IDL_[CHIPRS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|C_SRCS|MC_SRCS|RC_SRCS|SVG_SRCS|PROGRAMS)\s*=\s*(.*)/)
317         {
318             my @list = split(/\s+/, $2);
319             $make{$1} = \@list;
320             next;
321         }
322         if (/^\s*(TOPSRCDIR|TOPOBJDIR|SRCDIR|VPATH)\s*=\s*(.*)/)
323         {
324             die "Variable $1 in $file.in is obsolete";
325         }
326     }
327     return %make;
328 }
329
330 # assign source files to their respective makefile
331 sub assign_sources_to_makefiles()
332 {
333     foreach my $file (@all_files)
334     {
335         next if defined $ignored_source_files{$file};
336         my $dir = dirname( $file );
337
338         while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
339         next unless $dir;
340
341         die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
342
343         my $make = $makefiles{"$dir/Makefile"};
344         my $basename = substr( $file, length($dir) + 1 );
345
346         if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
347         elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
348         elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
349         elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
350         elsif ($basename =~ /\.mc$/) { push @{${$make}{"=MC_SRCS"}}, $basename; }
351         elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
352     }
353 }
354
355 ################################################################
356 # update the makefile list in configure.ac
357
358 sub update_makefiles(@)
359 {
360     my (@lines);
361
362     foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
363     {
364         my $file = $makerules{$var};
365         my %make = %{$makefiles{$file}};
366         my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
367         push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
368     }
369     push @lines, "\n";
370
371     foreach my $file (sort @_)
372     {
373         my %make = %{$makefiles{$file}};
374         my $rules = $make{"=rules"};
375         my $args = "";
376         my @flags;
377         my $is_win16 = $make{"MODULE"} && ($make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}});
378         if ($rules eq $makerules{"MAKE_DLL_RULES"})
379         {
380             (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
381             if ($name =~ /\./)
382             {
383                 die "Invalid MODULE in $file" unless $make{"MODULE"} eq $name;
384             }
385             else
386             {
387                 die "Invalid MODULE in $file" unless $make{"MODULE"} eq "$name.dll";
388             }
389             my $implib = $make{"IMPORTLIB"} || "";
390             push @flags, "implib" if $implib;
391             push @flags, "staticimplib" if defined($make{"IMPLIB_SRCS"});
392             $args .= "," if $is_win16 || @flags;
393             $args .= "enable_win16" if $is_win16;
394             $args .= ",[" . join(",",@flags) ."]" if @flags;
395             $args .= ",[$implib]" if $implib && $implib ne $name;
396             push @lines, "WINE_CONFIG_DLL($name$args)\n";
397         }
398         elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
399         {
400             (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
401             if ($name =~ /\./)
402             {
403                 die "Invalid MODULE in $file" unless $make{"MODULE"} eq $name;
404             }
405             else
406             {
407                 die "Invalid MODULE in $file" unless $make{"MODULE"} eq "$name.exe";
408             }
409             push @flags, "install" unless $dont_install{$name};
410             push @flags, "installbin" if $bin_install{$name};
411             $args .= "," if $is_win16 || @flags;
412             $args .= "enable_win16" if $is_win16;
413             $args .= ",[" . join(",",@flags) ."]" if @flags;
414             push @lines, "WINE_CONFIG_PROGRAM($name$args)\n";
415         }
416         elsif ($rules eq $makerules{"MAKE_TEST_RULES"})
417         {
418             (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
419             push @lines, "WINE_CONFIG_TEST($dir)\n";
420         }
421         elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"})
422         {
423             (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
424             push @lines, "WINE_CONFIG_LIB($name)\n";
425         }
426         elsif ($file =~ /^tools.*\/Makefile$/)
427         {
428             (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
429             push @lines, "WINE_CONFIG_TOOL($name)\n";
430         }
431         elsif ($file =~ /\/Makefile$/)
432         {
433             (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
434             push @lines, "WINE_CONFIG_MAKEFILE([$name])\n";
435         }
436     }
437
438     # update the source variables in all the makefiles
439
440     foreach my $file (sort @_)
441     {
442         my %make = %{$makefiles{$file}};
443
444         replace_makefile_variable( $file, "LEX_SRCS" );
445         replace_makefile_variable( $file, "BISON_SRCS" );
446         replace_makefile_variable( $file, "MC_SRCS" );
447         replace_makefile_variable( $file, "SVG_SRCS" );
448         replace_makefile_variable( $file, "C_SRCS" );
449         replace_makefile_variable( $file, "RC_SRCS" );
450     }
451
452     push @lines, "dnl End of auto-generated output commands\n";
453     replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl End of auto-generated output commands\n$', @lines);
454 }
455
456
457 ################################################################
458 # process ignore targets for generic source files
459
460 sub update_ignores(@)
461 {
462     my @ignores;
463
464     foreach my $file (sort @_)
465     {
466         my %makefile = %{$makefiles{$file}};
467         my @list;
468
469         foreach my $src (@ignore_srcs)
470         {
471             my @pattern = @{$src};
472             next unless defined $makefile{$pattern[0]};
473             push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
474         }
475         foreach my $f (@list)
476         {
477             push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
478         }
479
480         if (defined $makefile{"IMPORTLIB"})
481         {
482             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
483             {
484                 if ("dlls/$1/" ne $makefile{"=dir"}) { push @ignores, "dlls/lib$1.def"; }
485             }
486             else
487             {
488                 die "invalid importlib name $makefile{IMPORTLIB} in $file";
489             }
490         }
491     }
492     return @ignores;
493 }
494
495
496 ################################################################
497 # update include/Makefile.in
498
499 sub update_includes()
500 {
501     my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
502     my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
503     foreach my $incl (@includes)
504     {
505         if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
506         next if ($incl =~ /\.in$/);
507         if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
508         {
509             if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
510             next;
511         }
512         if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
513         elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
514         elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
515         elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
516         elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
517         elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
518         else { die "unknown file $incl in include dir"; }
519     }
520     replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
521                      "PRIVATE_IDL_H_SRCS = \\\n\t",
522                      join( " \\\n\t", sort @private_idl_srcs ),
523                      "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
524                      join( " \\\n\t", sort @public_idl_srcs ),
525                      "\n\nIDL_TLB_SRCS = \\\n\t",
526                      join( " \\\n\t", sort @tlb_srcs ),
527                      "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
528                      join( " \\\n\t", sort @h_srcs ),
529                      "\n\nEXTRASUBDIRS = ",
530                      join( " ", sort keys %subdirs ),
531                      "\n\nINSTALLDIRS = \\\n" );
532     return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
533 }
534
535
536 ################################################################
537 # update the main .gitignore
538
539 sub update_gitignore(@)
540 {
541     my @ignores = values %makerules;
542
543     foreach my $make (@makefiles)
544     {
545         my %makefile = %{$makefiles{$make}};
546         my $dir = $makefile{"=dir"};
547         if (defined $makefile{"PROGRAMS"})
548         {
549             push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
550         }
551     }
552
553     # prepend a slash to paths that don't have one
554     @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
555
556     # get rid of duplicates
557     my %ignores = ();
558     foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
559
560     replace_in_file( ".gitignore", undef, undef,
561                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
562                      join("\n", sort keys %ignores), "\n" );
563 }
564
565
566 die "needs to be run from a git checkout" unless -d ".git";
567
568 @all_files = split /\0/, `git ls-files -c -z`;
569 @makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
570
571 foreach my $file (sort values %makerules, @makefiles)
572 {
573     my %make = parse_makefile( $file );
574     $makefiles{$file} = \%make;
575 }
576
577 assign_sources_to_makefiles();
578 update_makefiles( @makefiles );
579 push @ignores, update_includes();
580 push @ignores, update_ignores( @makefiles );
581 update_gitignore( @ignores );