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
74 "dlls/*/tests/*crosstest.exe",
75 "dlls/*/tests/testlist.c",
78 "programs/winetest/*_test.exe",
79 "programs/winetest/*_test.rc",
80 "programs/winetest/build.rc",
83 # Source files and their resulting target to ignore
85 [ 'BISON_SRCS', '\.y', '.tab.c' ],
86 [ 'BISON_SRCS', '\.y', '.tab.h' ],
87 [ 'LEX_SRCS', '\.l', '.yy.c' ],
88 [ 'MC_SRCS', '\.mc', '.mc.rc' ],
89 [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
90 [ 'IDL_H_SRCS', '\.idl', '.h' ],
91 [ 'IDL_C_SRCS', '\.idl', '.h' ],
92 [ 'IDL_I_SRCS', '\.idl', '.h' ],
93 [ 'IDL_P_SRCS', '\.idl', '.h' ],
94 [ 'IDL_S_SRCS', '\.idl', '.h' ],
95 [ 'IDL_C_SRCS', '\.idl', '_c.c' ],
96 [ 'IDL_I_SRCS', '\.idl', '_i.c' ],
97 [ 'IDL_P_SRCS', '\.idl', '_p.c' ],
98 [ 'IDL_S_SRCS', '\.idl', '_s.c' ],
101 my %exported_wine_headers = (
103 "wine/exception.h" => 1,
104 "wine/library.h" => 1,
105 "wine/unicode.h" => 1,
106 "wine/itss.idl" => 1,
107 "wine/svcctl.idl" => 1,
110 my %private_idl_headers = (
126 "wine/wined3d.idl" => 1,
127 "wine/winedxgi.idl" => 1,
130 my %ignored_source_files = (
131 "dlls/wineps.drv/afm2c.c" => 1,
132 "dlls/wineps.drv/mkagl.c" => 1,
133 "programs/winetest/dist.rc" => 1,
136 my (@all_files, @makefiles, %makefiles);
141 return "" unless $ret =~ /\//;
146 # update a file if changed
150 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
157 rename "$file.new", "$file";
158 print "$file updated\n";
159 if ($file eq "configure.ac")
162 print "configure updated\n";
168 # replace some lines in a file between two markers
169 sub replace_in_file($$$@)
175 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
179 open OLD_FILE, "$file" or die "cannot open $file";
194 print NEW_FILE $_ unless $skip;
199 close OLD_FILE if defined($start);
201 return update_file($file);
204 # replace a variable in a makefile
205 sub replace_makefile_variable($$)
207 my ($file, $var) = @_;
208 my $make = $makefiles{$file};
210 return unless defined ${$make}{"=$var"};
212 my @values = @{${$make}{"=$var"}};
213 ${$make}{$var} = \@values;
215 open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
217 open OLD_FILE, "$file.in" or die "cannot open $file.in";
220 if (/^\s*($var\s+)=/)
222 # try to preserve formatting
224 my $multiline = /\\$/ || (@values > 1);
232 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
236 print NEW_FILE "$prefix= @values\n";
244 return update_file("$file.in");
247 # parse the specified makefile to identify the rules file
248 sub parse_makefile($)
253 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
255 open MAKE, "$file.in" or die "cannot open $file.in\n";
261 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
264 if (/^\@(MAKE.*RULES)\@/)
267 $make{"=rules"} = $makerules{$var};
270 if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
275 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*(.*)/)
277 my @list = split(/\s+/, $2);
285 # assign source files to their respective makefile
286 sub assign_sources_to_makefiles()
288 foreach my $file (@all_files)
290 next if defined $ignored_source_files{$file};
291 my $dir = dirname( $file );
293 while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
296 die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
298 my $make = $makefiles{"$dir/Makefile"};
299 my $basename = substr( $file, length($dir) + 1 );
301 if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
302 elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
303 elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
304 elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
305 elsif ($basename =~ /\.mc$/) { push @{${$make}{"=MC_SRCS"}}, $basename; }
306 elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
310 ################################################################
311 # update the makefile list in configure.ac
313 sub update_makefiles(@)
317 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
319 my $file = $makerules{$var};
320 my %make = %{$makefiles{$file}};
321 my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
322 push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
326 foreach my $file (sort @_)
328 my %make = %{$makefiles{$file}};
329 my $rules = $make{"=rules"};
331 if ($rules eq $makerules{"MAKE_DLL_RULES"})
333 $args = ",[dlls],[ALL_DLL_DIRS]";
334 $args .= ",[enable_win16]" if $make{"MODULE"} =~ /(16|\.vxd)$/;
336 elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"}) { $args = ",[dlls],[ALL_IMPLIB_DIRS]"; }
337 elsif ($rules eq $makerules{"MAKE_TEST_RULES"}) { $args = ",[dlls],[ALL_TEST_DIRS],[enable_tests]"; }
338 elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
340 (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
341 $args = ",[programs],[ALL_PROGRAM_DIRS";
342 $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
343 $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
345 $args .= ",[enable_win16]" if $make{"MODULE"} =~ /16$/;
347 elsif ($file =~ /^[^\/]*\/Makefile$/) { $args = ",[],[ALL_TOP_DIRS]"; }
348 push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules]$args)\n";
351 # update the source variables in all the makefiles
353 foreach my $file (sort @_)
355 my %make = %{$makefiles{$file}};
357 replace_makefile_variable( $file, "LEX_SRCS" );
358 replace_makefile_variable( $file, "BISON_SRCS" );
359 replace_makefile_variable( $file, "MC_SRCS" );
360 replace_makefile_variable( $file, "SVG_SRCS" );
361 replace_makefile_variable( $file, "C_SRCS" ) unless defined $make{"C_SRCS16"};
362 replace_makefile_variable( $file, "RC_SRCS" ) unless defined $make{"RC_SRCS16"};
365 push @lines, "\ndnl Build dependencies for test files compiled into winetest\n";
366 replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl Build dependencies for test files compiled into winetest$', @lines);
370 ################################################################
371 # process ignore targets for generic source files
373 sub update_ignores(@)
377 foreach my $file (sort @_)
379 my %makefile = %{$makefiles{$file}};
382 foreach my $src (@ignore_srcs)
384 my @pattern = @{$src};
385 next unless defined $makefile{$pattern[0]};
386 push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
388 foreach my $f (@list)
390 push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/; # skip make variables
396 ################################################################
397 # update dlls/Makefile.in
401 my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
405 foreach my $make (@_)
407 my %makefile = %{$makefiles{$make}};
408 next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
410 next unless defined $makefile{"MODULE"};
411 my $module = $makefile{"MODULE"};
412 (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
414 if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
416 $staticlib_dirs{$module} = $dir;
417 die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
421 die "invalid module $module" unless $module =~ /\./;
422 (my $mod = $module) =~ s/\.dll$//;
423 die "invalid directory $dir for module $module\n" unless $mod eq $dir;
424 $directories{$module} = $dir;
427 if (defined $makefile{"IMPORTLIB"})
429 if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
431 $importlibs{$module} = $1;
435 die "invalid importlib name $makefile{IMPORTLIB} in $make";
439 $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
441 if (defined $makefile{"SPEC_SRCS16"})
443 my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
444 $altnames{$module} = \@list;
446 if (defined $makefile{"EXTRA_OBJS16"})
448 foreach my $obj (@{$makefile{"EXTRA_OBJS16"}})
450 if ($obj =~ /^(.*\.(exe|mod))\.o/) { push @{$altnames{$module}}, $1; }
455 # output the list of 16-bit files
458 foreach my $mod (sort keys %directories)
460 next unless defined $altnames{$mod};
461 foreach my $i (sort @{$altnames{$mod}})
463 push @targets16, $i . "16";
466 $text .= "# 16-bit dlls\n\n";
467 $text .= "WIN16_FILES = \\\n";
468 $text .= "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
469 $text .= "\@MAKE_RULES\@\n\n";
471 # output the all: target
473 $text .= "# Main target\n\n";
474 $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
476 # output the lib name -> directory rules
478 $text .= "# Placeholders for 16-bit libraries\n\n";
479 foreach my $mod (sort keys %directories)
481 next unless defined $altnames{$mod};
482 $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
483 $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
486 # output the import libraries rules
488 $text .= "# Import libraries\n\n";
489 $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
491 my @lib_symlinks = ();
492 foreach my $mod (sort keys %importlibs)
494 my $dir = $directories{$mod};
495 my $lib = $importlibs{$mod};
496 if ($lib ne $dir) { push @lib_symlinks, $mod; }
498 $text .= "IMPORT_SYMLINKS =";
499 foreach my $mod (sort @lib_symlinks)
501 $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
504 $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
505 foreach my $mod (sort keys %staticlib_dirs)
507 $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
509 foreach my $mod (sort keys %importlibs)
511 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
512 next unless defined $static_implibs{$mod};
513 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
515 $text .= "\n\nCROSS_IMPLIBS =";
516 foreach my $mod (sort @lib_symlinks)
518 $text .= sprintf " \\\n\tlib%s.a", $importlibs{$mod};
520 foreach my $mod (sort keys %importlibs)
522 next if defined $static_implibs{$mod};
523 $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.a";
526 $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
527 $text .= "implib: \$(IMPORT_LIBS)\n\n";
528 $text .= "testsubdirs: \$(TESTSUBDIRS)\n\n";
529 $text .= ".PHONY: implib testsubdirs\n\n";
531 foreach my $mod (sort keys %importlibs)
533 my $dir = $directories{$mod};
534 my $lib = $importlibs{$mod};
537 if (defined($static_implibs{$mod}))
539 $text .= sprintf "%s/lib%s.def: %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
540 $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.def\n\n", $dir, $lib;
541 $text .= sprintf "%s/lib%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
542 $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
546 $text .= sprintf "%s/lib%s.def %s/lib%s.a: %s/%s.spec \$(WINEBUILD)\n",
547 $dir, $lib, $dir, $lib, $dir, $spec;
548 $text .= sprintf "\t\@cd %s && \$(MAKE) `basename \$\@`\n\n", $dir;
551 foreach my $mod (sort @lib_symlinks)
553 my $dir = $directories{$mod};
554 my $lib = "lib" . $importlibs{$mod};
555 $text .= sprintf "%s.a: %s/%s.a\n", $lib, $dir, $lib;
556 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.a \$@\n\n", $dir, $lib;
557 $text .= sprintf "%s.def: %s/%s.def\n", $lib, $dir, $lib;
558 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.def \$@\n\n", $dir, $lib;
561 $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
562 $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
564 # output the inter-dll dependencies and rules
566 $text .= "# Map library name to the corresponding directory\n\n";
568 foreach my $mod (sort keys %staticlib_dirs)
570 $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
572 $text .= "\n# Misc rules\n";
574 replace_in_file( "dlls/Makefile.in",
581 foreach my $mod (sort @lib_symlinks)
583 push @ignores, "dlls/lib$importlibs{$mod}.def";
585 foreach my $mod (sort keys %directories)
587 next unless defined $altnames{$mod};
588 push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
595 ################################################################
596 # update include/Makefile.in
598 sub update_includes()
600 my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
601 my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
602 foreach my $incl (@includes)
604 if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
605 next if ($incl =~ /\.in$/);
606 if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
608 if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
611 if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
612 elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
613 elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
614 elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
615 elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
616 elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
617 else { die "unknown file $incl in include dir"; }
619 replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
620 "PRIVATE_IDL_H_SRCS = \\\n\t",
621 join( " \\\n\t", sort @private_idl_srcs ),
622 "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
623 join( " \\\n\t", sort @public_idl_srcs ),
624 "\n\nIDL_TLB_SRCS = \\\n\t",
625 join( " \\\n\t", sort @tlb_srcs ),
626 "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
627 join( " \\\n\t", sort @h_srcs ),
628 "\n\nEXTRASUBDIRS = ",
629 join( " ", sort keys %subdirs ),
630 "\n\nINSTALLDIRS = \\\n" );
631 return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
635 ################################################################
636 # update the main .gitignore
638 sub update_gitignore(@)
640 my @ignores = values %makerules;
642 foreach my $make (@makefiles)
644 my %makefile = %{$makefiles{$make}};
645 my $dir = $makefile{"=dir"};
646 if (defined $makefile{"MANPAGES"})
648 push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
650 if (defined $makefile{"PROGRAMS"})
652 push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
656 # prepend a slash to paths that don't have one
657 @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
659 # get rid of duplicates
661 foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
663 replace_in_file( ".gitignore", undef, undef,
664 "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
665 join("\n", sort keys %ignores), "\n" );
669 die "needs to be run from a git checkout" unless -d ".git";
671 @all_files = split /\0/, `git ls-files -c -z`;
672 @makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
674 foreach my $file (sort values %makerules, @makefiles)
676 my %make = parse_makefile( $file );
677 $makefiles{$file} = \%make;
680 assign_sources_to_makefiles();
681 update_makefiles( @makefiles );
682 push @ignores, update_includes();
683 push @ignores, update_ignores( @makefiles );
684 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
685 update_gitignore( @ignores );