push e5b2529cdc55a2e67d855677d8651b2d2fa8d45f
[wine/hacks.git] / tools / make_makefiles
blob63581032d8fa45f0198e8a9daa704b43ffdd4f25
1 #!/usr/bin/perl -w
3 # Build the auto-generated parts of the Wine makefiles.
5 # Copyright 2006 Alexandre Julliard
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.
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.
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
22 # Make rules files
23 my %makerules =
25 "MAKE_RULES" => "Make.rules",
26 "MAKE_DLL_RULES" => "dlls/Makedll.rules",
27 "MAKE_IMPLIB_RULES" => "dlls/Makeimplib.rules",
28 "MAKE_TEST_RULES" => "dlls/Maketest.rules",
29 "MAKE_PROG_RULES" => "programs/Makeprog.rules",
32 # Programs that we want to install in the bin directory too
33 my %bin_install =
35 "msiexec" => 1,
36 "notepad" => 1,
37 "progman" => 1,
38 "regedit" => 1,
39 "regsvr32" => 1,
40 "uninstaller" => 1,
41 "wineboot" => 1,
42 "winebrowser" => 1,
43 "winecfg" => 1,
44 "wineconsole" => 1,
45 "winedbg" => 1,
46 "winefile" => 1,
47 "winemine" => 1,
48 "winepath" => 1,
51 # Programs that we don't want to install at all
52 my %dont_install =
54 "cmdlgtst" => 1,
55 "view" => 1,
56 "winetest" => 1,
59 # Default patterns for top-level .gitignore
60 my @ignores = (
61 "*.[oa]",
62 "*.ok",
63 "*.res",
64 "*.so",
65 "/autom4te.cache",
66 "/config.cache",
67 "/config.log",
68 "/config.status",
69 "/TAGS",
70 "/tags",
71 "Makefile",
72 "dlldata.c",
73 "dlls/*/*.def",
74 "dlls/*/tests/*crosstest.exe",
75 "dlls/*/tests/testlist.c",
76 "include/config.h",
77 "include/stamp-h"
80 # Source files and their resulting target to ignore
81 my @ignore_srcs = (
82 [ 'BISON_SRCS', '\.y', '.tab.c' ],
83 [ 'BISON_SRCS', '\.y', '.tab.h' ],
84 [ 'LEX_SRCS', '\.l', '.yy.c' ],
85 [ 'MC_SRCS', '\.mc', '.mc.rc' ],
86 [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
87 [ 'IDL_H_SRCS', '\.idl', '.h' ],
88 [ 'IDL_C_SRCS', '\.idl', '.h' ],
89 [ 'IDL_I_SRCS', '\.idl', '.h' ],
90 [ 'IDL_P_SRCS', '\.idl', '.h' ],
91 [ 'IDL_S_SRCS', '\.idl', '.h' ],
92 [ 'IDL_C_SRCS', '\.idl', '_c.c' ],
93 [ 'IDL_I_SRCS', '\.idl', '_i.c' ],
94 [ 'IDL_P_SRCS', '\.idl', '_p.c' ],
95 [ 'IDL_S_SRCS', '\.idl', '_s.c' ],
98 my %exported_wine_headers = (
99 "wine/debug.h" => 1,
100 "wine/exception.h" => 1,
101 "wine/library.h" => 1,
102 "wine/unicode.h" => 1,
103 "wine/itss.idl" => 1,
104 "wine/svcctl.idl" => 1,
107 my %private_idl_headers = (
108 "axcore.idl" => 1,
109 "axextend.idl" => 1,
110 "dbinit.idl" => 1,
111 "dbprop.idl" => 1,
112 "dbs.idl" => 1,
113 "devenum.idl" => 1,
114 "dyngraph.idl" => 1,
115 "vmrender.idl" => 1,
118 my (@makefiles, %makefiles);
120 # update a file if changed
121 sub update_file($)
123 my $file = shift;
124 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
125 if (!$ret)
127 unlink "$file.new";
129 else
131 rename "$file.new", "$file";
132 print "$file updated\n";
133 if ($file eq "configure.ac")
135 system "autoconf";
136 print "configure updated\n";
139 return $ret;
142 # replace some lines in a file between two markers
143 sub replace_in_file($$$@)
145 my $file = shift;
146 my $start = shift;
147 my $end = shift;
149 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
151 if (defined($start))
153 open OLD_FILE, "$file" or die "cannot open $file";
154 while (<OLD_FILE>)
156 last if /$start/;
157 print NEW_FILE $_;
161 print NEW_FILE @_;
163 if (defined($end))
165 my $skip=1;
166 while (<OLD_FILE>)
168 print NEW_FILE $_ unless $skip;
169 $skip = 0 if /$end/;
173 close OLD_FILE if defined($start);
174 close NEW_FILE;
175 return update_file($file);
178 # parse the specified makefile to identify the rules file
179 sub parse_makefile($)
181 my $file = shift;
182 my %make;
184 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
186 open MAKE, "$file.in" or die "cannot open $file.in\n";
188 while (<MAKE>)
190 chomp;
191 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
193 if (/^\@(MAKE.*RULES)\@/)
195 my $var = $1;
196 $make{"=rules"} = $makerules{$var};
197 next;
199 if (/^(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
201 $make{$1} = $2;
202 next;
204 if (/^(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|MC_SRCS|RC_SRCS|RC_SRCS16|RC_BINARIES|SPEC_SRCS16|EXTRA_OBJS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
206 my @list = split(/\s+/, $2);
207 $make{$1} = \@list;
208 next;
210 if (/^\#\s*MKDLL_SKIP/ || /^\#\s*MKPROG_SKIP/)
212 $make{"=skip"} = 1;
213 next;
216 return %make;
220 ################################################################
221 # update the tests list in programs/winetest/Makefile.in and programs/winetest/winetest.rc
223 sub update_winetest(@)
225 my (@tests, @lines);
227 foreach my $file (@_)
229 if ($file =~ /^dlls\/(.*)\/tests\/Makefile/) { push @tests, $1; }
231 push @lines, "TESTBINS =";
232 push @lines, map { " \\\n\t" . $_ . "_test.exe"; } sort @tests;
233 push @lines, "\n\n";
235 foreach my $test (sort @tests)
237 push @lines, "${test}_test.exe: \$(DLLDIR)/$test/tests/${test}_test.exe\$(DLLEXT)\n";
238 push @lines, "\tcp \$(DLLDIR)/$test/tests/${test}_test.exe\$(DLLEXT) \$\@ && \$(STRIP) \$\@\n";
240 push @lines, "\n# Special rules\n";
242 replace_in_file( "programs/winetest/Makefile.in", '^TESTBINS\s*=', '^# Special rules', @lines );
244 replace_in_file( "programs/winetest/winetest.rc", ' TESTRES ', undef,
245 map { $_ . "_test.exe TESTRES \"" . $_ . "_test.exe\"\n"; } sort @tests );
247 # return a list of test exe files for .gitignore
248 return map { "programs/winetest/" . $_ . "_test.exe"; } sort @tests;
252 ################################################################
253 # update the makefile list in configure.ac
255 sub update_makefiles(@)
257 my (@lines);
259 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
261 my $file = $makerules{$var};
262 my %make = %{$makefiles{$file}};
263 my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
264 push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
266 push @lines, "\n";
268 foreach my $file (sort @_)
270 my %make = %{$makefiles{$file}};
271 my $rules = $make{"=rules"};
272 my $args = "";
273 if ($rules eq $makerules{"MAKE_DLL_RULES"}) { $args = ",[dlls],[ALL_DLL_DIRS]"; }
274 elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"}) { $args = ",[dlls],[ALL_IMPLIB_DIRS]"; }
275 elsif ($rules eq $makerules{"MAKE_TEST_RULES"}) { $args = ",[dlls],[ALL_TEST_DIRS]"; }
276 elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
278 (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
279 $args = ",[programs],[ALL_PROGRAM_DIRS";
280 $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
281 $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
282 $args .= "]";
284 push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules]$args)\n";
287 push @lines, "\nAC_OUTPUT\n";
288 replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^AC_OUTPUT$', @lines);
292 ################################################################
293 # process ignore targets for generic source files
295 sub update_ignores(@)
297 my @ignores;
299 foreach my $file (sort @_)
301 my %makefile = %{$makefiles{$file}};
302 my @list;
304 foreach my $src (@ignore_srcs)
306 my @pattern = @{$src};
307 next unless defined $makefile{$pattern[0]};
308 push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
310 push @list, @{$makefile{"RC_BINARIES"}} if defined $makefile{"RC_BINARIES"};
311 foreach my $f (@list)
313 push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/; # skip make variables
316 return @ignores;
319 ################################################################
320 # update dlls/Makefile.in
322 sub update_dlls(@)
324 my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
325 my $text = "";
326 my @ignores = ();
328 foreach my $make (@_)
330 my %makefile = %{$makefiles{$make}};
331 next if defined $makefile{"=skip"};
332 next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
334 next unless defined $makefile{"MODULE"};
335 my $module = $makefile{"MODULE"};
336 (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
338 if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
340 $staticlib_dirs{$module} = $dir;
341 die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
343 else
345 die "invalid module $module" unless $module =~ /\./;
346 (my $mod = $module) =~ s/\.dll$//;
347 die "invalid directory $dir for module $module\n" unless $mod eq $dir;
348 $directories{$module} = $dir;
351 if (defined $makefile{"IMPORTLIB"})
353 if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
355 $importlibs{$module} = $1;
357 else
359 die "invalid importlib name $makefile{IMPORTLIB} in $make";
363 $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
365 if (defined $makefile{"SPEC_SRCS16"})
367 my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
368 $altnames{$module} = \@list;
370 if (defined $makefile{"EXTRA_OBJS16"})
372 foreach my $obj (@{$makefile{"EXTRA_OBJS16"}})
374 if ($obj =~ /^(.*\.(exe|mod))\.o/) { push @{$altnames{$module}}, $1; }
379 # output the list of 16-bit files
381 my @targets16 = ();
382 foreach my $mod (sort keys %directories)
384 next unless defined $altnames{$mod};
385 foreach my $i (sort @{$altnames{$mod}})
387 push @targets16, $i . "16";
390 $text .= "# 16-bit dlls\n\n";
391 $text .= "WIN16_FILES = \\\n";
392 $text .= "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
393 $text .= "\@MAKE_RULES\@\n\n";
395 # output the all: target
397 $text .= "# Main target\n\n";
398 $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
400 # output the lib name -> directory rules
402 $text .= "# Placeholders for 16-bit libraries\n\n";
403 foreach my $mod (sort keys %directories)
405 next unless defined $altnames{$mod};
406 $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
407 $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
410 # output the import libraries rules
412 $text .= "# Import libraries\n\n";
413 $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
415 my @lib_symlinks = ();
416 foreach my $mod (sort keys %importlibs)
418 my $dir = $directories{$mod};
419 my $lib = $importlibs{$mod};
420 if ($lib ne $dir) { push @lib_symlinks, $mod; }
422 $text .= "IMPORT_SYMLINKS =";
423 foreach my $mod (sort @lib_symlinks)
425 $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
428 $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
429 foreach my $mod (sort keys %staticlib_dirs)
431 $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
433 foreach my $mod (sort keys %importlibs)
435 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
436 next unless defined $static_implibs{$mod};
437 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
439 $text .= "\n\nCROSS_IMPLIBS =";
440 foreach my $mod (sort @lib_symlinks)
442 $text .= sprintf " \\\n\tlib%s.a", $importlibs{$mod};
444 foreach my $mod (sort keys %importlibs)
446 next if defined $static_implibs{$mod};
447 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.a";
449 $text .= "\n\n";
450 $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
451 $text .= "implib: \$(IMPORT_LIBS)\n\n";
452 $text .= ".PHONY: implib\n\n";
454 foreach my $mod (sort keys %importlibs)
456 my $dir = $directories{$mod};
457 my $lib = $importlibs{$mod};
458 my $spec = $mod;
459 $spec =~ s/\.dll$//;
460 if (defined($static_implibs{$mod}))
462 $text .= sprintf "%s/lib%s.def: %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
463 $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.def\n\n", $dir, $lib;
464 $text .= sprintf "%s/lib%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
465 $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
467 else
469 $text .= sprintf "%s/lib%s.def %s/lib%s.a: %s/%s.spec \$(WINEBUILD)\n",
470 $dir, $lib, $dir, $lib, $dir, $spec;
471 $text .= sprintf "\t\@cd %s && \$(MAKE) `basename \$\@`\n\n", $dir;
474 foreach my $mod (sort @lib_symlinks)
476 my $dir = $directories{$mod};
477 my $lib = "lib" . $importlibs{$mod};
478 $text .= sprintf "%s.a: %s/%s.a\n", $lib, $dir, $lib;
479 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.a \$@\n\n", $dir, $lib;
480 $text .= sprintf "%s.def: %s/%s.def\n", $lib, $dir, $lib;
481 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.def \$@\n\n", $dir, $lib;
484 $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
485 $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
487 # output the inter-dll dependencies and rules
489 $text .= "# Map library name to the corresponding directory\n\n";
491 foreach my $mod (sort keys %staticlib_dirs)
493 $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
495 $text .= "\n# Misc rules\n";
497 replace_in_file( "dlls/Makefile.in",
498 '^# 16-bit dlls',
499 '^# Misc rules',
500 $text );
502 # .gitignore file
504 foreach my $mod (sort @lib_symlinks)
506 push @ignores, "dlls/lib$importlibs{$mod}.def";
508 foreach my $mod (sort keys %directories)
510 next unless defined $altnames{$mod};
511 push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
514 return @ignores;
518 ################################################################
519 # update include/Makefile.in
521 sub update_includes()
523 return unless -d ".git";
524 my (@h_srcs, @idl_srcs, @tlb_srcs, %subdirs);
525 my @includes = map { s/^include\///; $_; } split /\0/, `git ls-files -c -z include`;
526 foreach my $incl (@includes)
528 if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
529 next if ($incl =~ /^wine\// && !$exported_wine_headers{$incl});
530 if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
531 elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
532 elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
533 elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
534 elsif ($incl =~ /\.idl$/) { push @idl_srcs, $incl; }
536 replace_in_file( "include/Makefile.in", '^IDL_H_SRCS\s*=', '^INSTALLDIRS',
537 "IDL_H_SRCS = \\\n\t",
538 join( " \\\n\t", sort @idl_srcs ),
539 "\n\nIDL_TLB_SRCS = \\\n\t",
540 join( " \\\n\t", sort @tlb_srcs ),
541 "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(IDL_H_SRCS) \\\n\t",
542 join( " \\\n\t", sort @h_srcs ),
543 "\n\nEXTRASUBDIRS = ",
544 join( " ", sort keys %subdirs ),
545 "\n\nINSTALLDIRS = \\\n" );
549 ################################################################
550 # update the main .gitignore
552 sub update_gitignore(@)
554 my @ignores = values %makerules;
556 foreach my $make (@makefiles)
558 my %makefile = %{$makefiles{$make}};
559 my $dir = $makefile{"=dir"};
560 if (defined $makefile{"MANPAGES"})
562 push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
564 if (defined $makefile{"PROGRAMS"})
566 push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
568 if ($dir =~ /^programs\/(.*)\/$/)
570 push @ignores, "$dir$1";
574 # prepend a slash to paths that don't have one
575 @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
577 # get rid of duplicates
578 my %ignores = ();
579 foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
581 replace_in_file( ".gitignore", undef, undef,
582 "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
583 join("\n", sort keys %ignores), "\n" );
587 if (-d ".git")
589 @makefiles = map { s/\.in$//; $_; } split /\0/, `git ls-files -c -z Makefile.in \\*/Makefile.in`;
591 else
593 @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
596 update_includes();
598 foreach my $file (sort values %makerules, @makefiles)
600 my %make = parse_makefile( $file );
601 $makefiles{$file} = \%make;
604 update_makefiles( @makefiles );
605 push @ignores, update_ignores( @makefiles );
606 push @ignores, update_winetest( @makefiles );
607 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
608 update_gitignore( @ignores );