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
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",
34 # Programs that we want to install in the bin directory too
50 # Programs that we don't want to install at all
58 # Default patterns for top-level .gitignore
75 "dlls/*/tests/*crosstest.exe",
76 "dlls/*/tests/testlist.c",
79 "programs/winetest/*_test.exe",
80 "programs/winetest/*_test.rc",
81 "programs/winetest/build.rc",
84 # Source files and their resulting target to ignore
86 [ 'BISON_SRCS', '\.y', '.tab.c' ],
87 [ 'BISON_SRCS', '\.y', '.tab.h' ],
88 [ 'LEX_SRCS', '\.l', '.yy.c' ],
89 [ 'MC_SRCS', '\.mc', '.mc.rc' ],
90 [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
91 [ 'IDL_H_SRCS', '\.idl', '.h' ],
92 [ 'IDL_C_SRCS', '\.idl', '.h' ],
93 [ 'IDL_I_SRCS', '\.idl', '.h' ],
94 [ 'IDL_P_SRCS', '\.idl', '.h' ],
95 [ 'IDL_S_SRCS', '\.idl', '.h' ],
96 [ 'IDL_C_SRCS', '\.idl', '_c.c' ],
97 [ 'IDL_I_SRCS', '\.idl', '_i.c' ],
98 [ 'IDL_P_SRCS', '\.idl', '_p.c' ],
99 [ 'IDL_S_SRCS', '\.idl', '_s.c' ],
102 my %exported_wine_headers = (
104 "wine/exception.h" => 1,
105 "wine/library.h" => 1,
106 "wine/unicode.h" => 1,
107 "wine/itss.idl" => 1,
108 "wine/svcctl.idl" => 1,
111 my %private_idl_headers = (
137 "wine/wined3d.idl" => 1,
138 "wine/winedxgi.idl" => 1,
141 my %ignored_source_files = (
142 "dlls/wineps.drv/afm2c.c" => 1,
143 "dlls/wineps.drv/mkagl.c" => 1,
144 "programs/winetest/dist.rc" => 1,
147 my (@all_files, @makefiles, %makefiles);
152 return "" unless $ret =~ /\//;
157 # update a file if changed
161 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
168 rename "$file.new", "$file";
169 print "$file updated\n";
170 if ($file eq "configure.ac")
173 print "configure updated\n";
179 # replace some lines in a file between two markers
180 sub replace_in_file($$$@)
186 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
190 open OLD_FILE, "$file" or die "cannot open $file";
205 print NEW_FILE $_ unless $skip;
210 close OLD_FILE if defined($start);
212 return update_file($file);
215 # replace a variable in a makefile
216 sub replace_makefile_variable($$)
218 my ($file, $var) = @_;
219 my $make = $makefiles{$file};
221 return unless defined ${$make}{"=$var"};
223 my @values = @{${$make}{"=$var"}};
224 ${$make}{$var} = \@values;
226 open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
228 open OLD_FILE, "$file.in" or die "cannot open $file.in";
231 if (/^\s*($var\s+)=/)
233 # try to preserve formatting
235 my $multiline = /\\$/ || (@values > 1);
243 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
247 print NEW_FILE "$prefix= @values\n";
255 return update_file("$file.in");
258 # parse the specified makefile to identify the rules file
259 sub parse_makefile($)
264 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
266 open MAKE, "$file.in" or die "cannot open $file.in\n";
272 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
275 if (/^\@(MAKE.*RULES)\@/)
278 $make{"=rules"} = $makerules{$var};
281 if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
286 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*(.*)/)
288 my @list = split(/\s+/, $2);
296 # assign source files to their respective makefile
297 sub assign_sources_to_makefiles()
299 foreach my $file (@all_files)
301 next if defined $ignored_source_files{$file};
302 my $dir = dirname( $file );
304 while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
307 die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
309 my $make = $makefiles{"$dir/Makefile"};
310 my $basename = substr( $file, length($dir) + 1 );
312 if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
313 elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
314 elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
315 elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
316 elsif ($basename =~ /\.mc$/) { push @{${$make}{"=MC_SRCS"}}, $basename; }
317 elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
321 ################################################################
322 # update the makefile list in configure.ac
324 sub update_makefiles(@)
328 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
330 my $file = $makerules{$var};
331 my %make = %{$makefiles{$file}};
332 my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
333 push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
337 foreach my $file (sort @_)
339 my %make = %{$makefiles{$file}};
340 my $rules = $make{"=rules"};
342 if ($rules eq $makerules{"MAKE_DLL_RULES"})
344 (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
345 $args .= ",enable_win16" if $make{"MODULE"} =~ /(16|\.vxd)$/;
346 push @lines, "WINE_CONFIG_DLL($name$args)\n";
348 elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
350 (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
351 $args = ",[ALL_PROGRAM_DIRS";
352 $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
353 $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
355 $args .= ",enable_win16" if $make{"MODULE"} =~ /16$/;
356 push @lines, "WINE_CONFIG_PROGRAM($name$args)\n";
358 elsif ($rules eq $makerules{"MAKE_TEST_RULES"})
360 (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
361 push @lines, "WINE_CONFIG_TEST($dir)\n";
363 elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"})
365 (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
366 push @lines, "WINE_CONFIG_LIB($name)\n";
368 elsif ($file =~ /^[^\/]*\/Makefile$/)
370 push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules],[],[ALL_TOP_DIRS])\n";
374 push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules])\n";
378 # update the source variables in all the makefiles
380 foreach my $file (sort @_)
382 my %make = %{$makefiles{$file}};
384 replace_makefile_variable( $file, "LEX_SRCS" );
385 replace_makefile_variable( $file, "BISON_SRCS" );
386 replace_makefile_variable( $file, "MC_SRCS" );
387 replace_makefile_variable( $file, "SVG_SRCS" );
388 replace_makefile_variable( $file, "C_SRCS" );
389 replace_makefile_variable( $file, "RC_SRCS" );
392 push @lines, "\ndnl Build dependencies for test files compiled into winetest\n";
393 replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl Build dependencies for test files compiled into winetest$', @lines);
397 ################################################################
398 # process ignore targets for generic source files
400 sub update_ignores(@)
404 foreach my $file (sort @_)
406 my %makefile = %{$makefiles{$file}};
409 foreach my $src (@ignore_srcs)
411 my @pattern = @{$src};
412 next unless defined $makefile{$pattern[0]};
413 push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
415 foreach my $f (@list)
417 push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/; # skip make variables
423 ################################################################
424 # update dlls/Makefile.in
428 my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
432 foreach my $make (@_)
434 my %makefile = %{$makefiles{$make}};
435 next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
437 next unless defined $makefile{"MODULE"};
438 my $module = $makefile{"MODULE"};
439 (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
441 if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
443 $staticlib_dirs{$module} = $dir;
444 die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
448 die "invalid module $module" unless $module =~ /\./;
449 (my $mod = $module) =~ s/\.dll$//;
450 die "invalid directory $dir for module $module\n" unless $mod eq $dir;
451 $directories{$module} = $dir;
454 if (defined $makefile{"IMPORTLIB"})
456 if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
458 $importlibs{$module} = $1;
462 die "invalid importlib name $makefile{IMPORTLIB} in $make";
466 $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
469 $text .= "\@MAKE_RULES\@\n\n";
471 # output the all: target
473 $text .= "# Main target\n\n";
474 $text .= "all: \$(BUILDSUBDIRS)\n\n";
476 # output the import libraries rules
478 $text .= "# Import libraries\n\n";
479 $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
481 my @lib_symlinks = ();
482 foreach my $mod (sort keys %importlibs)
484 my $dir = $directories{$mod};
485 my $lib = $importlibs{$mod};
486 if ($lib ne $dir) { push @lib_symlinks, $mod; }
488 $text .= "IMPORT_SYMLINKS =";
489 foreach my $mod (sort @lib_symlinks)
491 $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
494 $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
495 foreach my $mod (sort keys %staticlib_dirs)
497 $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
499 foreach my $mod (sort keys %importlibs)
501 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
502 next unless defined $static_implibs{$mod};
503 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
505 $text .= "\n\nCROSS_IMPLIBS =";
506 foreach my $mod (sort @lib_symlinks)
508 next if $mod =~ /16$/;
509 $text .= sprintf " \\\n\tlib%s.a", $importlibs{$mod};
511 foreach my $mod (sort keys %importlibs)
513 next if $mod =~ /16$/;
514 next if defined $static_implibs{$mod};
515 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.a";
518 $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
519 $text .= "implib: \$(IMPORT_LIBS)\n\n";
520 $text .= "testsubdirs: \$(TESTSUBDIRS)\n\n";
521 $text .= ".PHONY: implib testsubdirs\n\n";
523 foreach my $mod (sort keys %importlibs)
525 my $dir = $directories{$mod};
526 my $lib = $importlibs{$mod};
529 if (defined($static_implibs{$mod}))
531 $text .= sprintf "%s/lib%s.def: %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
532 $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.def\n\n", $dir, $lib;
533 $text .= sprintf "%s/lib%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
534 $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
538 $text .= sprintf "%s/lib%s.def %s/lib%s.a: %s/%s.spec \$(WINEBUILD)\n",
539 $dir, $lib, $dir, $lib, $dir, $spec;
540 $text .= sprintf "\t\@cd %s && \$(MAKE) `basename \$\@`\n\n", $dir;
543 foreach my $mod (sort @lib_symlinks)
545 my $dir = $directories{$mod};
546 my $lib = "lib" . $importlibs{$mod};
547 $text .= sprintf "%s.a: %s/%s.a\n", $lib, $dir, $lib;
548 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.a \$@\n\n", $dir, $lib;
549 $text .= sprintf "%s.def: %s/%s.def\n", $lib, $dir, $lib;
550 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.def \$@\n\n", $dir, $lib;
553 $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
554 $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
556 # output the inter-dll dependencies and rules
558 $text .= "# Map library name to the corresponding directory\n\n";
560 foreach my $mod (sort keys %staticlib_dirs)
562 $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
564 $text .= "\n# Misc rules\n";
566 replace_in_file( "dlls/Makefile.in",
573 foreach my $mod (sort @lib_symlinks)
575 push @ignores, "dlls/lib$importlibs{$mod}.def";
581 ################################################################
582 # update include/Makefile.in
584 sub update_includes()
586 my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
587 my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
588 foreach my $incl (@includes)
590 if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
591 next if ($incl =~ /\.in$/);
592 if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
594 if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
597 if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
598 elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
599 elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
600 elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
601 elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
602 elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
603 else { die "unknown file $incl in include dir"; }
605 replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
606 "PRIVATE_IDL_H_SRCS = \\\n\t",
607 join( " \\\n\t", sort @private_idl_srcs ),
608 "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
609 join( " \\\n\t", sort @public_idl_srcs ),
610 "\n\nIDL_TLB_SRCS = \\\n\t",
611 join( " \\\n\t", sort @tlb_srcs ),
612 "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
613 join( " \\\n\t", sort @h_srcs ),
614 "\n\nEXTRASUBDIRS = ",
615 join( " ", sort keys %subdirs ),
616 "\n\nINSTALLDIRS = \\\n" );
617 return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
621 ################################################################
622 # update the main .gitignore
624 sub update_gitignore(@)
626 my @ignores = values %makerules;
628 foreach my $make (@makefiles)
630 my %makefile = %{$makefiles{$make}};
631 my $dir = $makefile{"=dir"};
632 if (defined $makefile{"MANPAGES"})
634 push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
636 if (defined $makefile{"PROGRAMS"})
638 push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
642 # prepend a slash to paths that don't have one
643 @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
645 # get rid of duplicates
647 foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
649 replace_in_file( ".gitignore", undef, undef,
650 "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
651 join("\n", sort keys %ignores), "\n" );
655 die "needs to be run from a git checkout" unless -d ".git";
657 @all_files = split /\0/, `git ls-files -c -z`;
658 @makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
660 foreach my $file (sort values %makerules, @makefiles)
662 my %make = parse_makefile( $file );
663 $makefiles{$file} = \%make;
666 assign_sources_to_makefiles();
667 update_makefiles( @makefiles );
668 push @ignores, update_includes();
669 push @ignores, update_ignores( @makefiles );
670 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
671 update_gitignore( @ignores );