makefiles: Generate all the top-level make rules inside the corresponding shell function.
[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     "*.ok",
80     "*.res",
81     "*.so",
82     "/autom4te.cache",
83     "/config.cache",
84     "/config.log",
85     "/config.status",
86     "/configure.lineno",
87     "/TAGS",
88     "/tags",
89     "/wine",
90     "Makefile",
91     "dlldata.c",
92     "dlls/*/*.def",
93     "*/*/tests/*crosstest.exe",
94     "*/*/tests/testlist.c",
95     "include/config.h",
96     "include/stamp-h",
97     "programs/winetest/*_test.exe",
98     "programs/winetest/*_test.rc",
99     "programs/winetest/build.rc",
100     "tools/makedep",
101 );
102
103 # Source files and their resulting target to ignore
104 my @ignore_srcs = (
105     [ 'BISON_SRCS',   '\.y',   '.tab.c' ],
106     [ 'BISON_SRCS',   '\.y',   '.tab.h' ],
107     [ 'LEX_SRCS',     '\.l',   '.yy.c' ],
108     [ 'MC_SRCS',      '\.mc',  '.mc.rc' ],
109     [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
110     [ 'IDL_H_SRCS',   '\.idl', '.h' ],
111     [ 'IDL_C_SRCS',   '\.idl', '.h' ],
112     [ 'IDL_I_SRCS',   '\.idl', '.h' ],
113     [ 'IDL_P_SRCS',   '\.idl', '.h' ],
114     [ 'IDL_S_SRCS',   '\.idl', '.h' ],
115     [ 'IDL_C_SRCS',   '\.idl', '_c.c' ],
116     [ 'IDL_I_SRCS',   '\.idl', '_i.c' ],
117     [ 'IDL_P_SRCS',   '\.idl', '_p.c' ],
118     [ 'IDL_S_SRCS',   '\.idl', '_s.c' ],
119 );
120
121 my %exported_wine_headers = (
122     "wine/debug.h" => 1,
123     "wine/exception.h" => 1,
124     "wine/library.h" => 1,
125     "wine/unicode.h" => 1,
126     "wine/itss.idl" => 1,
127     "wine/svcctl.idl" => 1,
128 );
129
130 my %private_idl_headers = (
131     "access.idl" => 1,
132     "asynot.idl" => 1,
133     "asysta.idl" => 1,
134     "axcore.idl" => 1,
135     "axextend.idl" => 1,
136     "binres.idl" => 1,
137     "cmdbas.idl" => 1,
138     "cmdtxt.idl" => 1,
139     "crtrow.idl" => 1,
140     "dbccmd.idl" => 1,
141     "dbcses.idl" => 1,
142     "dbdsad.idl" => 1,
143     "dbinit.idl" => 1,
144     "dbprop.idl" => 1,
145     "dbs.idl" => 1,
146     "devenum.idl" => 1,
147     "dyngraph.idl" => 1,
148     "opnrst.idl" => 1,
149     "row.idl" => 1,
150     "rowchg.idl" => 1,
151     "rstbas.idl" => 1,
152     "rstinf.idl" => 1,
153     "rstloc.idl" => 1,
154     "sesprp.idl" => 1,
155     "vmrender.idl" => 1,
156     "wine/wined3d.idl" => 1,
157     "wine/winedxgi.idl" => 1,
158 );
159
160 my %ignored_source_files = (
161     "dlls/wineps.drv/afm2c.c" => 1,
162     "dlls/wineps.drv/mkagl.c" => 1,
163     "programs/winetest/dist.rc" => 1,
164 );
165
166 my (@all_files, @makefiles, %makefiles);
167
168 sub dirname($)
169 {
170     my $ret = shift;
171     return "" unless $ret =~ /\//;
172     $ret =~ s!/[^/]*$!!;
173     return $ret;
174 }
175
176 # update a file if changed
177 sub update_file($)
178 {
179     my $file = shift;
180     my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
181     if (!$ret)
182     {
183         unlink "$file.new";
184     }
185     else
186     {
187         rename "$file.new", "$file";
188         print "$file updated\n";
189         if ($file eq "configure.ac")
190         {
191             system "autoconf";
192             print "configure updated\n";
193         }
194     }
195     return $ret;
196 }
197
198 # replace some lines in a file between two markers
199 sub replace_in_file($$$@)
200 {
201     my $file = shift;
202     my $start = shift;
203     my $end = shift;
204
205     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
206
207     if (defined($start))
208     {
209         open OLD_FILE, "$file" or die "cannot open $file";
210         while (<OLD_FILE>)
211         {
212             last if /$start/;
213             print NEW_FILE $_;
214         }
215     }
216
217     print NEW_FILE @_;
218
219     if (defined($end))
220     {
221         my $skip=1;
222         while (<OLD_FILE>)
223         {
224             print NEW_FILE $_ unless $skip;
225             $skip = 0 if /$end/;
226         }
227     }
228
229     close OLD_FILE if defined($start);
230     close NEW_FILE;
231     return update_file($file);
232 }
233
234 # replace a variable in a makefile
235 sub replace_makefile_variable($$)
236 {
237     my ($file, $var) = @_;
238     my $make = $makefiles{$file};
239
240     return unless defined ${$make}{"=$var"};
241
242     my @values = @{${$make}{"=$var"}};
243     ${$make}{$var} = \@values;
244
245     open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
246
247     open OLD_FILE, "$file.in" or die "cannot open $file.in";
248     while (<OLD_FILE>)
249     {
250         if (/^\s*($var\s+)=/)
251         {
252             # try to preserve formatting
253             my $prefix = $1;
254             my $multiline = /\\$/ || (@values > 1);
255             while (/\\$/)
256             {
257                 $_ = <OLD_FILE>;
258                 last unless $_;
259             }
260             if ($multiline)
261             {
262                 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
263             }
264             else
265             {
266                 print NEW_FILE "$prefix= @values\n";
267             }
268             next;
269         }
270         print NEW_FILE $_;
271     }
272     close OLD_FILE;
273     close NEW_FILE;
274     return update_file("$file.in");
275 }
276
277 # parse the specified makefile to identify the rules file
278 sub parse_makefile($)
279 {
280     my $file = shift;
281     my %make;
282
283     ($make{"=dir"} = $file) =~ s/[^\/]+$//;
284
285     open MAKE, "$file.in" or die "cannot open $file.in\n";
286
287     while (<MAKE>)
288     {
289         chomp;
290         next if (/^\s*#/);
291         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
292         next if (/^\s*$/);
293
294         if (/^\@(MAKE.*RULES)\@/)
295         {
296             my $var = $1;
297             $make{"=rules"} = $makerules{$var};
298             next;
299         }
300         if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
301         {
302             $make{$1} = $2;
303             next;
304         }
305         if (/^\s*(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|C_SRCS|MC_SRCS|RC_SRCS|SVG_SRCS|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
306         {
307             my @list = split(/\s+/, $2);
308             $make{$1} = \@list;
309             next;
310         }
311     }
312     return %make;
313 }
314
315 # assign source files to their respective makefile
316 sub assign_sources_to_makefiles()
317 {
318     foreach my $file (@all_files)
319     {
320         next if defined $ignored_source_files{$file};
321         my $dir = dirname( $file );
322
323         while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
324         next unless $dir;
325
326         die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
327
328         my $make = $makefiles{"$dir/Makefile"};
329         my $basename = substr( $file, length($dir) + 1 );
330
331         if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
332         elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
333         elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
334         elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
335         elsif ($basename =~ /\.mc$/) { push @{${$make}{"=MC_SRCS"}}, $basename; }
336         elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
337     }
338 }
339
340 ################################################################
341 # update the makefile list in configure.ac
342
343 sub update_makefiles(@)
344 {
345     my (@lines);
346
347     foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
348     {
349         my $file = $makerules{$var};
350         my %make = %{$makefiles{$file}};
351         my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
352         push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
353     }
354     push @lines, "\n";
355
356     foreach my $file (sort @_)
357     {
358         my %make = %{$makefiles{$file}};
359         my $rules = $make{"=rules"};
360         my $args = "";
361         my $is_win16 = $make{"MODULE"} && ($make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}});
362         if ($rules eq $makerules{"MAKE_DLL_RULES"})
363         {
364             (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
365             my $implib = $make{"IMPORTLIB"} || "";
366             my $implib_srcs = defined($make{"IMPLIB_SRCS"}) && join( " ", @{$make{"IMPLIB_SRCS"}} );
367             $args .= "," if $implib || $is_win16;
368             $args .= "enable_win16" if $is_win16;
369             $args .= ",[$implib]" if $implib;
370             $args .= ",[$implib_srcs]" if $implib_srcs;
371             push @lines, "WINE_CONFIG_DLL($name$args)\n";
372         }
373         elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
374         {
375             (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
376             my $install = $dont_install{$name} ? "" : "install";
377             $install .= "bin" if $bin_install{$name};
378             $args .= "," if $is_win16 || $install;
379             $args .= "$install" if $install;
380             $args .= ",enable_win16" if $is_win16;
381             push @lines, "WINE_CONFIG_PROGRAM($name$args)\n";
382         }
383         elsif ($rules eq $makerules{"MAKE_TEST_RULES"})
384         {
385             (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
386             push @lines, "WINE_CONFIG_TEST($dir)\n";
387         }
388         elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"})
389         {
390             (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
391             push @lines, "WINE_CONFIG_LIB($name)\n";
392         }
393         elsif ($file =~ /^tools.*\/Makefile$/)
394         {
395             (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
396             push @lines, "WINE_CONFIG_TOOL($name)\n";
397         }
398         elsif ($file =~ /\/Makefile$/)
399         {
400             (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
401             push @lines, "WINE_CONFIG_MAKEFILE([$name])\n";
402         }
403     }
404
405     # update the source variables in all the makefiles
406
407     foreach my $file (sort @_)
408     {
409         my %make = %{$makefiles{$file}};
410
411         replace_makefile_variable( $file, "LEX_SRCS" );
412         replace_makefile_variable( $file, "BISON_SRCS" );
413         replace_makefile_variable( $file, "MC_SRCS" );
414         replace_makefile_variable( $file, "SVG_SRCS" );
415         replace_makefile_variable( $file, "C_SRCS" );
416         replace_makefile_variable( $file, "RC_SRCS" );
417     }
418
419     push @lines, "dnl End of auto-generated output commands\n";
420     replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl End of auto-generated output commands\n$', @lines);
421 }
422
423
424 ################################################################
425 # process ignore targets for generic source files
426
427 sub update_ignores(@)
428 {
429     my @ignores;
430
431     foreach my $file (sort @_)
432     {
433         my %makefile = %{$makefiles{$file}};
434         my @list;
435
436         foreach my $src (@ignore_srcs)
437         {
438             my @pattern = @{$src};
439             next unless defined $makefile{$pattern[0]};
440             push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
441         }
442         foreach my $f (@list)
443         {
444             push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
445         }
446
447         if (defined $makefile{"IMPORTLIB"})
448         {
449             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
450             {
451                 if ("dlls/$1/" ne $makefile{"=dir"}) { push @ignores, "dlls/lib$1.def"; }
452             }
453             else
454             {
455                 die "invalid importlib name $makefile{IMPORTLIB} in $file";
456             }
457         }
458     }
459     return @ignores;
460 }
461
462
463 ################################################################
464 # update include/Makefile.in
465
466 sub update_includes()
467 {
468     my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
469     my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
470     foreach my $incl (@includes)
471     {
472         if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
473         next if ($incl =~ /\.in$/);
474         if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
475         {
476             if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
477             next;
478         }
479         if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
480         elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
481         elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
482         elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
483         elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
484         elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
485         else { die "unknown file $incl in include dir"; }
486     }
487     replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
488                      "PRIVATE_IDL_H_SRCS = \\\n\t",
489                      join( " \\\n\t", sort @private_idl_srcs ),
490                      "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
491                      join( " \\\n\t", sort @public_idl_srcs ),
492                      "\n\nIDL_TLB_SRCS = \\\n\t",
493                      join( " \\\n\t", sort @tlb_srcs ),
494                      "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
495                      join( " \\\n\t", sort @h_srcs ),
496                      "\n\nEXTRASUBDIRS = ",
497                      join( " ", sort keys %subdirs ),
498                      "\n\nINSTALLDIRS = \\\n" );
499     return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
500 }
501
502
503 ################################################################
504 # update the main .gitignore
505
506 sub update_gitignore(@)
507 {
508     my @ignores = values %makerules;
509
510     foreach my $make (@makefiles)
511     {
512         my %makefile = %{$makefiles{$make}};
513         my $dir = $makefile{"=dir"};
514         if (defined $makefile{"MANPAGES"})
515         {
516             push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
517         }
518         if (defined $makefile{"PROGRAMS"})
519         {
520             push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
521         }
522     }
523
524     # prepend a slash to paths that don't have one
525     @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
526
527     # get rid of duplicates
528     my %ignores = ();
529     foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
530
531     replace_in_file( ".gitignore", undef, undef,
532                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
533                      join("\n", sort keys %ignores), "\n" );
534 }
535
536
537 die "needs to be run from a git checkout" unless -d ".git";
538
539 @all_files = split /\0/, `git ls-files -c -z`;
540 @makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
541
542 foreach my $file (sort values %makerules, @makefiles)
543 {
544     my %make = parse_makefile( $file );
545     $makefiles{$file} = \%make;
546 }
547
548 assign_sources_to_makefiles();
549 update_makefiles( @makefiles );
550 push @ignores, update_includes();
551 push @ignores, update_ignores( @makefiles );
552 update_gitignore( @ignores );