make_makefiles: Fix handling of .mc files.
[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" => "dlls/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 # Default patterns for top-level .gitignore
59 my @ignores = (
60     "*.[oa]",
61     "*.ok",
62     "*.res",
63     "*.so",
64     "/autom4te.cache",
65     "/config.cache",
66     "/config.log",
67     "/config.status",
68     "/TAGS",
69     "/tags",
70     "Makefile",
71     "dlldata.c",
72     "dlls/*/*.def",
73     "dlls/*/tests/*crosstest.exe",
74     "dlls/*/tests/testlist.c",
75     "include/config.h",
76     "include/stamp-h",
77     "programs/winetest/*_test.exe",
78     "programs/winetest/*_test.rc",
79     "programs/winetest/build.rc",
80 );
81
82 # Source files and their resulting target to ignore
83 my @ignore_srcs = (
84     [ 'BISON_SRCS',   '\.y',   '.tab.c' ],
85     [ 'BISON_SRCS',   '\.y',   '.tab.h' ],
86     [ 'LEX_SRCS',     '\.l',   '.yy.c' ],
87     [ 'MC_SRCS',      '\.mc',  '.mc.rc' ],
88     [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
89     [ 'IDL_H_SRCS',   '\.idl', '.h' ],
90     [ 'IDL_C_SRCS',   '\.idl', '.h' ],
91     [ 'IDL_I_SRCS',   '\.idl', '.h' ],
92     [ 'IDL_P_SRCS',   '\.idl', '.h' ],
93     [ 'IDL_S_SRCS',   '\.idl', '.h' ],
94     [ 'IDL_C_SRCS',   '\.idl', '_c.c' ],
95     [ 'IDL_I_SRCS',   '\.idl', '_i.c' ],
96     [ 'IDL_P_SRCS',   '\.idl', '_p.c' ],
97     [ 'IDL_S_SRCS',   '\.idl', '_s.c' ],
98 );
99
100 my %exported_wine_headers = (
101     "wine/debug.h" => 1,
102     "wine/exception.h" => 1,
103     "wine/library.h" => 1,
104     "wine/unicode.h" => 1,
105     "wine/itss.idl" => 1,
106     "wine/svcctl.idl" => 1,
107 );
108
109 my %private_idl_headers = (
110     "axcore.idl" => 1,
111     "axextend.idl" => 1,
112     "binres.idl" => 1,
113     "crtrow.idl" => 1,
114     "dbccmd.idl" => 1,
115     "dbcses.idl" => 1,
116     "dbdsad.idl" => 1,
117     "dbinit.idl" => 1,
118     "dbprop.idl" => 1,
119     "dbs.idl" => 1,
120     "devenum.idl" => 1,
121     "dyngraph.idl" => 1,
122     "opnrst.idl" => 1,
123     "sesprp.idl" => 1,
124     "vmrender.idl" => 1,
125     "wine/wined3d.idl" => 1,
126     "wine/winedxgi.idl" => 1,
127 );
128
129 my %ignored_source_files = (
130     "dlls/wineps.drv/afm2c.c" => 1,
131     "dlls/wineps.drv/mkagl.c" => 1,
132     "programs/winetest/dist.rc" => 1,
133 );
134
135 my (@all_files, @makefiles, %makefiles);
136
137 sub dirname($)
138 {
139     my $ret = shift;
140     return "" unless $ret =~ /\//;
141     $ret =~ s!/[^/]*$!!;
142     return $ret;
143 }
144
145 # update a file if changed
146 sub update_file($)
147 {
148     my $file = shift;
149     my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
150     if (!$ret)
151     {
152         unlink "$file.new";
153     }
154     else
155     {
156         rename "$file.new", "$file";
157         print "$file updated\n";
158         if ($file eq "configure.ac")
159         {
160             system "autoconf";
161             print "configure updated\n";
162         }
163     }
164     return $ret;
165 }
166
167 # replace some lines in a file between two markers
168 sub replace_in_file($$$@)
169 {
170     my $file = shift;
171     my $start = shift;
172     my $end = shift;
173
174     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
175
176     if (defined($start))
177     {
178         open OLD_FILE, "$file" or die "cannot open $file";
179         while (<OLD_FILE>)
180         {
181             last if /$start/;
182             print NEW_FILE $_;
183         }
184     }
185
186     print NEW_FILE @_;
187
188     if (defined($end))
189     {
190         my $skip=1;
191         while (<OLD_FILE>)
192         {
193             print NEW_FILE $_ unless $skip;
194             $skip = 0 if /$end/;
195         }
196     }
197
198     close OLD_FILE if defined($start);
199     close NEW_FILE;
200     return update_file($file);
201 }
202
203 # replace a variable in a makefile
204 sub replace_makefile_variable($$)
205 {
206     my ($file, $var) = @_;
207     my $make = $makefiles{$file};
208
209     return unless defined ${$make}{"=$var"};
210
211     my @values = @{${$make}{"=$var"}};
212     ${$make}{$var} = \@values;
213
214     open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
215
216     open OLD_FILE, "$file.in" or die "cannot open $file.in";
217     while (<OLD_FILE>)
218     {
219         if (/^\s*($var\s+)=/)
220         {
221             # try to preserve formatting
222             my $prefix = $1;
223             my $multiline = /\\$/ || (@values > 1);
224             while (/\\$/)
225             {
226                 $_ = <OLD_FILE>;
227                 last unless $_;
228             }
229             if ($multiline)
230             {
231                 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
232             }
233             else
234             {
235                 print NEW_FILE "$prefix= @values\n";
236             }
237             next;
238         }
239         print NEW_FILE $_;
240     }
241     close OLD_FILE;
242     close NEW_FILE;
243     return update_file("$file.in");
244 }
245
246 # parse the specified makefile to identify the rules file
247 sub parse_makefile($)
248 {
249     my $file = shift;
250     my %make;
251
252     ($make{"=dir"} = $file) =~ s/[^\/]+$//;
253
254     open MAKE, "$file.in" or die "cannot open $file.in\n";
255
256     while (<MAKE>)
257     {
258         chomp;
259         next if (/^\s*#/);
260         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
261         next if (/^\s*$/);
262
263         if (/^\@(MAKE.*RULES)\@/)
264         {
265             my $var = $1;
266             $make{"=rules"} = $makerules{$var};
267             next;
268         }
269         if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
270         {
271             $make{$1} = $2;
272             next;
273         }
274         if (/^\s*(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|C_SRCS|MC_SRCS|RC_SRCS|SVG_SRCS|C_SRCS16|RC_SRCS16|SPEC_SRCS16|EXTRA_OBJS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
275         {
276             my @list = split(/\s+/, $2);
277             $make{$1} = \@list;
278             next;
279         }
280     }
281     return %make;
282 }
283
284 # assign source files to their respective makefile
285 sub assign_sources_to_makefiles()
286 {
287     foreach my $file (@all_files)
288     {
289         next if defined $ignored_source_files{$file};
290         my $dir = dirname( $file );
291
292         while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
293         next unless $dir;
294
295         die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
296
297         my $make = $makefiles{"$dir/Makefile"};
298         my $basename = substr( $file, length($dir) + 1 );
299
300         if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
301         elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
302         elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
303         elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
304         elsif ($basename =~ /\.mc$/) { push @{${$make}{"=MC_SRCS"}}, $basename; }
305         elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
306     }
307 }
308
309 ################################################################
310 # update the makefile list in configure.ac
311
312 sub update_makefiles(@)
313 {
314     my (@lines);
315
316     foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
317     {
318         my $file = $makerules{$var};
319         my %make = %{$makefiles{$file}};
320         my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
321         push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
322     }
323     push @lines, "\n";
324
325     foreach my $file (sort @_)
326     {
327         my %make = %{$makefiles{$file}};
328         my $rules = $make{"=rules"};
329         my $args = "";
330         if ($rules eq $makerules{"MAKE_DLL_RULES"})
331         {
332             $args = ",[dlls],[ALL_DLL_DIRS]";
333             $args .= ",[enable_win16]" if $make{"MODULE"} =~ /(16|\.vxd)$/;
334         }
335         elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"}) { $args = ",[dlls],[ALL_IMPLIB_DIRS]"; }
336         elsif ($rules eq $makerules{"MAKE_TEST_RULES"}) { $args = ",[dlls],[ALL_TEST_DIRS],[enable_tests]"; }
337         elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
338         {
339             (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
340             $args = ",[programs],[ALL_PROGRAM_DIRS";
341             $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
342             $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
343             $args .= "]";
344             $args .= ",[enable_win16]" if $make{"MODULE"} =~ /16$/;
345         }
346         elsif ($file =~ /^[^\/]*\/Makefile$/) { $args = ",[],[ALL_TOP_DIRS]"; }
347         push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules]$args)\n";
348     }
349
350     # update the source variables in all the makefiles
351
352     foreach my $file (sort @_)
353     {
354         my %make = %{$makefiles{$file}};
355
356         replace_makefile_variable( $file, "LEX_SRCS" );
357         replace_makefile_variable( $file, "BISON_SRCS" );
358         replace_makefile_variable( $file, "MC_SRCS" );
359         replace_makefile_variable( $file, "SVG_SRCS" );
360         replace_makefile_variable( $file, "C_SRCS" ) unless defined $make{"C_SRCS16"};
361         replace_makefile_variable( $file, "RC_SRCS" ) unless defined $make{"RC_SRCS16"};
362     }
363
364     push @lines, "\ndnl Build dependencies for test files compiled into winetest\n";
365     replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl Build dependencies for test files compiled into winetest$', @lines);
366 }
367
368
369 ################################################################
370 # process ignore targets for generic source files
371
372 sub update_ignores(@)
373 {
374     my @ignores;
375
376     foreach my $file (sort @_)
377     {
378         my %makefile = %{$makefiles{$file}};
379         my @list;
380
381         foreach my $src (@ignore_srcs)
382         {
383             my @pattern = @{$src};
384             next unless defined $makefile{$pattern[0]};
385             push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
386         }
387         foreach my $f (@list)
388         {
389             push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
390         }
391     }
392     return @ignores;
393 }
394
395 ################################################################
396 # update dlls/Makefile.in
397
398 sub update_dlls(@)
399 {
400     my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
401     my $text = "";
402     my @ignores = ();
403
404     foreach my $make (@_)
405     {
406         my %makefile = %{$makefiles{$make}};
407         next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
408
409         next unless defined $makefile{"MODULE"};
410         my $module = $makefile{"MODULE"};
411         (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
412
413         if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
414         {
415             $staticlib_dirs{$module} = $dir;
416             die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
417         }
418         else
419         {
420             die "invalid module $module" unless $module =~ /\./;
421             (my $mod = $module) =~ s/\.dll$//;
422             die "invalid directory $dir for module $module\n" unless $mod eq $dir;
423             $directories{$module} = $dir;
424         }
425
426         if (defined $makefile{"IMPORTLIB"})
427         {
428             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
429             {
430                 $importlibs{$module} = $1;
431             }
432             else
433             {
434                 die "invalid importlib name $makefile{IMPORTLIB} in $make";
435             }
436         }
437
438         $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
439
440         if (defined $makefile{"SPEC_SRCS16"})
441         {
442             my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
443             $altnames{$module} = \@list;
444         }
445         if (defined $makefile{"EXTRA_OBJS16"})
446         {
447             foreach my $obj (@{$makefile{"EXTRA_OBJS16"}})
448             {
449                 if ($obj =~ /^(.*\.(exe|mod))\.o/) { push @{$altnames{$module}}, $1; }
450             }
451         }
452     }
453
454     # output the list of 16-bit files
455
456     my @targets16 = ();
457     foreach my $mod (sort keys %directories)
458     {
459         next unless defined $altnames{$mod};
460         foreach my $i (sort @{$altnames{$mod}})
461         {
462             push @targets16, $i . "16";
463         }
464     }
465     $text .= "# 16-bit dlls\n\n";
466     $text .= "WIN16_FILES = \\\n";
467     $text .=  "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
468     $text .= "\@MAKE_RULES\@\n\n";
469
470     # output the all: target
471
472     $text .= "# Main target\n\n";
473     $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
474
475     # output the lib name -> directory rules
476
477     $text .= "# Placeholders for 16-bit libraries\n\n";
478     foreach my $mod (sort keys %directories)
479     {
480         next unless defined $altnames{$mod};
481         $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
482         $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
483     }
484
485     # output the import libraries rules
486
487     $text .= "# Import libraries\n\n";
488     $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
489
490     my @lib_symlinks = ();
491     foreach my $mod (sort keys %importlibs)
492     {
493         my $dir = $directories{$mod};
494         my $lib = $importlibs{$mod};
495         if ($lib ne $dir) { push @lib_symlinks, $mod; }
496     }
497     $text .= "IMPORT_SYMLINKS =";
498     foreach my $mod (sort @lib_symlinks)
499     {
500         $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
501     }
502
503     $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
504     foreach my $mod (sort keys %staticlib_dirs)
505     {
506         $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
507     }
508     foreach my $mod (sort keys %importlibs)
509     {
510         $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
511         next unless defined $static_implibs{$mod};
512         $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
513     }
514     $text .= "\n\nCROSS_IMPLIBS =";
515     foreach my $mod (sort @lib_symlinks)
516     {
517         $text .= sprintf " \\\n\tlib%s.a", $importlibs{$mod};
518     }
519     foreach my $mod (sort keys %importlibs)
520     {
521         next if defined $static_implibs{$mod};
522         $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.a";
523     }
524     $text .= "\n\n";
525     $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
526     $text .= "implib: \$(IMPORT_LIBS)\n\n";
527     $text .= "testsubdirs: \$(TESTSUBDIRS)\n\n";
528     $text .= ".PHONY: implib testsubdirs\n\n";
529
530     foreach my $mod (sort keys %importlibs)
531     {
532         my $dir = $directories{$mod};
533         my $lib = $importlibs{$mod};
534         my $spec = $mod;
535         $spec =~ s/\.dll$//;
536         if (defined($static_implibs{$mod}))
537         {
538             $text .= sprintf "%s/lib%s.def: %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
539             $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.def\n\n", $dir, $lib;
540             $text .= sprintf "%s/lib%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
541             $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
542         }
543         else
544         {
545             $text .= sprintf "%s/lib%s.def %s/lib%s.a: %s/%s.spec \$(WINEBUILD)\n",
546                              $dir, $lib, $dir, $lib, $dir, $spec;
547             $text .= sprintf "\t\@cd %s && \$(MAKE) `basename \$\@`\n\n", $dir;
548         }
549     }
550     foreach my $mod (sort @lib_symlinks)
551     {
552         my $dir = $directories{$mod};
553         my $lib = "lib" . $importlibs{$mod};
554         $text .= sprintf "%s.a: %s/%s.a\n", $lib, $dir, $lib;
555         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.a \$@\n\n", $dir, $lib;
556         $text .= sprintf "%s.def: %s/%s.def\n", $lib, $dir, $lib;
557         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.def \$@\n\n", $dir, $lib;
558     }
559
560     $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
561     $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
562
563     # output the inter-dll dependencies and rules
564
565     $text .= "# Map library name to the corresponding directory\n\n";
566
567     foreach my $mod (sort keys %staticlib_dirs)
568     {
569         $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
570     }
571     $text .= "\n# Misc rules\n";
572
573     replace_in_file( "dlls/Makefile.in",
574                      '^# 16-bit dlls',
575                      '^# Misc rules',
576                      $text );
577
578     # .gitignore file
579
580     foreach my $mod (sort @lib_symlinks)
581     {
582         push @ignores, "dlls/lib$importlibs{$mod}.def";
583     }
584     foreach my $mod (sort keys %directories)
585     {
586         next unless defined $altnames{$mod};
587         push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
588     }
589
590     return @ignores;
591 }
592
593
594 ################################################################
595 # update include/Makefile.in
596
597 sub update_includes()
598 {
599     my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
600     my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
601     foreach my $incl (@includes)
602     {
603         if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
604         next if ($incl =~ /\.in$/);
605         if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
606         {
607             if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
608             next;
609         }
610         if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
611         elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
612         elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
613         elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
614         elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
615         elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
616         else { die "unknown file $incl in include dir"; }
617     }
618     replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
619                      "PRIVATE_IDL_H_SRCS = \\\n\t",
620                      join( " \\\n\t", sort @private_idl_srcs ),
621                      "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
622                      join( " \\\n\t", sort @public_idl_srcs ),
623                      "\n\nIDL_TLB_SRCS = \\\n\t",
624                      join( " \\\n\t", sort @tlb_srcs ),
625                      "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
626                      join( " \\\n\t", sort @h_srcs ),
627                      "\n\nEXTRASUBDIRS = ",
628                      join( " ", sort keys %subdirs ),
629                      "\n\nINSTALLDIRS = \\\n" );
630     return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
631 }
632
633
634 ################################################################
635 # update the main .gitignore
636
637 sub update_gitignore(@)
638 {
639     my @ignores = values %makerules;
640
641     foreach my $make (@makefiles)
642     {
643         my %makefile = %{$makefiles{$make}};
644         my $dir = $makefile{"=dir"};
645         if (defined $makefile{"MANPAGES"})
646         {
647             push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
648         }
649         if (defined $makefile{"PROGRAMS"})
650         {
651             push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
652         }
653     }
654
655     # prepend a slash to paths that don't have one
656     @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
657
658     # get rid of duplicates
659     my %ignores = ();
660     foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
661
662     replace_in_file( ".gitignore", undef, undef,
663                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
664                      join("\n", sort keys %ignores), "\n" );
665 }
666
667
668 die "needs to be run from a git checkout" unless -d ".git";
669
670 @all_files = split /\0/, `git ls-files -c -z`;
671 @makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
672
673 foreach my $file (sort values %makerules, @makefiles)
674 {
675     my %make = parse_makefile( $file );
676     $makefiles{$file} = \%make;
677 }
678
679 assign_sources_to_makefiles();
680 update_makefiles( @makefiles );
681 push @ignores, update_includes();
682 push @ignores, update_ignores( @makefiles );
683 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
684 update_gitignore( @ignores );