cmd.exe: Add "if defined ..." support.
[wine/multimedia.git] / tools / make_makefiles
blobb21d4ef3e9ea7569199b148a3fe056344b9c66d7
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,
49 "winhelp" => 1,
52 # Programs that we don't want to install at all
53 my %dont_install =
55 "cmdlgtst" => 1,
56 "view" => 1,
57 "winetest" => 1,
60 # Special dlls that can be switched on or off by configure
61 my %special_dlls =
63 "glu32" => "GLU32FILES",
64 "opengl32" => "OPENGLFILES",
65 "wined3d" => "OPENGLFILES",
66 "winex11.drv" => "XFILES",
67 "winequartz.drv" => "QUARTZFILES"
70 # Default patterns for top-level .gitignore
71 my @ignores = (
72 "*.[oa]",
73 "*.so",
74 "/autom4te.cache",
75 "/config.cache",
76 "/config.log",
77 "/config.status",
78 "/TAGS",
79 "/tags",
80 "Makefile",
81 "include/config.h",
82 "include/stamp-h"
85 # Source files and their resulting target to ignore
86 my @ignore_srcs = (
87 [ 'BISON_SRCS', '\.y', '.tab.c' ],
88 [ 'BISON_SRCS', '\.y', '.tab.h' ],
89 [ 'LEX_SRCS', '\.l', '.yy.c' ],
90 [ 'MC_SRCS', '\.mc', '.mc.rc' ],
91 [ 'RC_SRCS', '\.rc', '.res' ],
92 [ 'RC_SRCS16', '\.rc', '.res' ],
93 [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
94 [ 'IDL_H_SRCS', '\.idl', '.h' ],
95 [ 'IDL_C_SRCS', '\.idl', '.h' ],
96 [ 'IDL_I_SRCS', '\.idl', '.h' ],
97 [ 'IDL_P_SRCS', '\.idl', '.h' ],
98 [ 'IDL_S_SRCS', '\.idl', '.h' ],
99 [ 'IDL_C_SRCS', '\.idl', '_c.c' ],
100 [ 'IDL_I_SRCS', '\.idl', '_i.c' ],
101 [ 'IDL_P_SRCS', '\.idl', '_p.c' ],
102 [ 'IDL_S_SRCS', '\.idl', '_s.c' ],
105 my (@makefiles, %makefiles);
107 # update a file if changed
108 sub update_file($)
110 my $file = shift;
111 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
112 if (!$ret)
114 unlink "$file.new";
116 else
118 rename "$file.new", "$file";
119 print "$file updated\n";
120 if ($file eq "configure.ac")
122 system "autoconf";
123 print "configure updated\n";
126 return $ret;
129 # replace some lines in a file between two markers
130 sub replace_in_file($$$@)
132 my $file = shift;
133 my $start = shift;
134 my $end = shift;
136 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
138 if (defined($start))
140 open OLD_FILE, "$file" or die "cannot open $file";
141 while (<OLD_FILE>)
143 last if /$start/;
144 print NEW_FILE $_;
148 print NEW_FILE @_;
150 if (defined($end))
152 my $skip=1;
153 while (<OLD_FILE>)
155 print NEW_FILE $_ unless $skip;
156 $skip = 0 if /$end/;
160 close OLD_FILE if defined($start);
161 close NEW_FILE;
162 return update_file($file);
165 # parse the specified makefile to identify the rules file
166 sub parse_makefile($)
168 my $file = shift;
169 my %make;
171 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
173 open MAKE, "$file.in" or die "cannot open $file.in\n";
175 while (<MAKE>)
177 chomp;
178 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
180 if (/^\@(MAKE.*RULES)\@/)
182 my $var = $1;
183 $make{"=rules"} = $makerules{$var};
184 next;
186 if (/^(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
188 $make{$1} = $2;
189 next;
191 if (/^(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|MC_SRCS|RC_SRCS|RC_SRCS16|RC_BINARIES|SPEC_SRCS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
193 my @list = split(/\s+/, $2);
194 $make{$1} = \@list;
195 next;
197 if (/^\#\s*MKDLL_SKIP/ || /^\#\s*MKPROG_SKIP/)
199 $make{"=skip"} = 1;
200 next;
203 return %make;
206 if (-d ".git")
208 @makefiles = map { s/\.in$//; $_; } split /\s/, `git ls-files -c Makefile.in \\*/Makefile.in`;
210 else
212 @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
215 foreach my $file (sort values %makerules, @makefiles)
217 my %make = parse_makefile( $file );
218 $makefiles{$file} = \%make;
221 ################################################################
222 # update the makefile list in configure.ac
224 my @lines = ();
226 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
228 push @lines, "$var=$makerules{$var}\n";
229 push @lines, "AC_SUBST_FILE($var)\n\n";
232 replace_in_file( "configure.ac", '^MAKE_RULES', '\]\)$',
233 @lines,
234 "AC_CONFIG_FILES([\n",
235 join ("\n", (sort values %makerules), (sort @makefiles) ), "])\n" );
238 ################################################################
239 # update the tests list in programs/winetest/Makefile.in and programs/winetest/winetest.rc
241 sub update_winetest(@)
243 my (@tests, @lines);
245 foreach my $file (@_)
247 if ($file =~ /^dlls\/(.*)\/tests\/Makefile/) { push @tests, $1; }
249 push @lines, "TESTBINS =";
250 push @lines, map { " \\\n\t" . $_ . "_test.exe"; } sort @tests;
251 push @lines, "\n\n";
253 foreach my $test (sort @tests)
255 push @lines, "${test}_test.exe: \$(DLLDIR)/$test/tests/${test}_test.exe\$(DLLEXT)\n";
256 push @lines, "\tcp \$(DLLDIR)/$test/tests/${test}_test.exe\$(DLLEXT) \$\@ && \$(STRIP) \$\@\n";
258 push @lines, "\n# Special rules\n";
260 replace_in_file( "programs/winetest/Makefile.in", '^TESTBINS\s*=', '^# Special rules', @lines );
262 replace_in_file( "programs/winetest/winetest.rc", ' TESTRES ', undef,
263 map { $_ . "_test.exe TESTRES \"" . $_ . "_test.exe\"\n"; } sort @tests );
265 # return a list of test exe files for .gitignore
266 return map { "programs/winetest/" . $_ . "_test.exe"; } sort @tests;
270 ################################################################
271 # update the makefile list in Makefile.in
273 sub update_makefiles(@)
275 my (@targets, @depends);
277 foreach my $file (sort values %makerules)
279 push @targets, $file;
280 my %make = %{$makefiles{$file}};
281 if (!defined($make{"=rules"})) { push @depends, "$file: $file.in"; }
282 else { push @depends, "$file: $file.in Make.rules"; }
285 foreach my $file (sort @_)
287 push @targets, $file unless $file eq "Makefile";
288 my %makefile = %{$makefiles{$file}};
289 my $dep = $makefile{"=rules"};
290 push @depends, "$file: $file.in $dep";
294 @lines = ();
295 push @lines, "ALL_MAKEFILES = \\\n\t";
296 push @lines, join (" \\\n\t", @targets ), "\n\n";
297 push @lines, "Makefile \$(ALL_MAKEFILES): config.status\n";
298 push @lines, "\t\@./config.status \$\@\n\n";
299 push @lines, "\$(RECURSE_TARGETS) \$(MAKEDEP): \$(ALL_MAKEFILES)\n\n";
300 push @lines, "distclean::\n";
301 push @lines, "\t\$(RM) Makefile \$(ALL_MAKEFILES)\n\n";
302 push @lines, join ("\n", @depends ), "\n";
304 replace_in_file( "Makefile.in", '^ALL_MAKEFILES\s*=', undef, @lines );
308 ################################################################
309 # process ignore targets for generic source files
311 sub update_ignores(@)
313 my @ignores;
315 foreach my $file (sort @_)
317 my %makefile = %{$makefiles{$file}};
318 my @list;
320 foreach my $src (@ignore_srcs)
322 my @pattern = @{$src};
323 next unless defined $makefile{$pattern[0]};
324 push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
326 push @list, @{$makefile{"RC_BINARIES"}} if defined $makefile{"RC_BINARIES"};
327 foreach my $f (@list)
329 push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/; # skip make variables
332 return @ignores;
335 ################################################################
336 # update dlls/Makefile.in
338 sub update_dlls(@)
340 my (%directories, %testdirs, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
341 my $text = "";
342 my @ignores = ();
344 foreach my $make (@_)
346 my %makefile = %{$makefiles{$make}};
347 next if defined $makefile{"=skip"};
349 if ($make =~ /dlls\/(.*)\/tests\/Makefile/)
351 $testdirs{$1} = "$1/tests";
352 (my $crosstest = $makefile{"TESTDLL"}) =~ s/\.dll$//;
353 push @ignores, $makefile{"=dir"} . $crosstest . "_crosstest.exe";
354 push @ignores, $makefile{"=dir"} . "testlist.c";
355 push @ignores, $makefile{"=dir"} . "*.ok";
356 next;
359 next unless defined $makefile{"MODULE"};
360 my $module = $makefile{"MODULE"};
361 (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
363 if ($module =~ /^lib.*\.a$/)
365 $staticlib_dirs{$module} = $dir;
366 die "invalid module $module in dir $staticlib_dirs{$module}\n" if "lib$staticlib_dirs{$module}.a" ne $module;
368 else
370 (my $mod = $module) =~ s/\.dll$//;
371 die "invalid directory $dir for module $module\n" unless $mod eq $dir;
372 $directories{$module} = $dir;
375 if (defined $makefile{"IMPORTLIB"})
377 if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)\.\$\(IMPLIBEXT\)/)
379 $importlibs{$module} = $1;
381 else
383 die "invalid importlib name $makefile{IMPORTLIB} in $make";
387 $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
389 if (defined $makefile{"SPEC_SRCS16"})
391 my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
392 $altnames{$module} = \@list;
396 # output special dlls configure definitions
398 $text .= "# special configure-dependent targets\n\n";
399 my %specials = ();
400 foreach my $mod (sort keys %special_dlls)
402 $specials{$special_dlls{$mod}} .= " " . $mod;
404 foreach my $i (sort keys %specials)
406 $text .= $i . " =" . $specials{$i} . "\n";
408 $text .= "EXTRADIRS =";
409 foreach my $i (sort keys %specials) { $text .= sprintf " \@%s\@", $i; }
410 $text .= "\n\n";
412 # output the subdirs list
414 $text .= "# Subdir list\n\n";
415 $text .= "BASEDIRS =";
416 foreach my $dir (sort values %directories)
418 next if defined($special_dlls{$dir}); # skip special dlls
419 $text .= " \\\n\t" . $dir;
422 $text .= "\n\nIMPLIBSUBDIRS = \\\n\t";
423 $text .= join " \\\n\t", sort values %staticlib_dirs;
425 $text .= "\n\nTESTSUBDIRS = \\\n\t";
426 $text .= join " \\\n\t", sort values %testdirs;
428 $text .= "\n\nSUBDIRS = \\\n\t";
429 $text .= join " \\\n\t", "\$(BASEDIRS)", "\$(IMPLIBSUBDIRS)", "\$(TESTSUBDIRS)", sort keys %special_dlls;
431 $text .= "\n\nBUILDSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS) \$(TESTSUBDIRS)\n";
432 $text .= "INSTALLSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS) \$(IMPLIBSUBDIRS)\n";
433 $text .= "DOCSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS)\n";
435 # output the list of 16-bit files
437 my @targets16 = ();
438 foreach my $mod (sort keys %directories)
440 next unless defined $altnames{$mod};
441 foreach my $i (sort @{$altnames{$mod}})
443 push @targets16, $i . "16";
446 $text .= "\n# 16-bit dlls\n\n";
447 $text .= "WIN16_FILES = \\\n";
448 $text .= "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
449 $text .= "\@MAKE_RULES\@\n\n";
451 # output the all: target
453 $text .= "# Main target\n\n";
454 $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
456 # output the lib name -> directory rules
458 $text .= "# Placeholders for 16-bit libraries\n\n";
459 foreach my $mod (sort keys %directories)
461 next unless defined $altnames{$mod};
462 $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
463 $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
466 # output the import libraries rules
468 $text .= "# Import libraries\n\n";
469 $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
471 my @lib_symlinks = ();
472 foreach my $mod (sort keys %importlibs)
474 my $dir = $directories{$mod};
475 my $lib = $importlibs{$mod};
476 if ($lib ne "lib" . $dir) { push @lib_symlinks, $mod; }
478 $text .= "IMPORT_SYMLINKS =";
479 foreach my $mod (sort @lib_symlinks)
481 $text .= sprintf " \\\n\t%s.\$(IMPLIBEXT)", $importlibs{$mod};
484 $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
485 foreach my $mod (sort keys %staticlib_dirs)
487 $text .= sprintf " \\\n\t%s/%s", $staticlib_dirs{$mod}, $mod;
489 foreach my $mod (sort keys %importlibs)
491 my $dir = $directories{$mod};
492 my $def = $mod;
493 $def =~ s/\.(dll|drv)$//;
494 $text .= sprintf " \\\n\t%s/lib%s.\$(IMPLIBEXT)", $dir, $def;
495 next unless defined $static_implibs{$mod};
496 $text .= sprintf " \\\n\t%s/lib%s.\$(STATIC_IMPLIBEXT)", $dir, $def
498 $text .= "\n\n";
499 $text .= "implib: \$(IMPORT_LIBS)\n\n";
500 $text .= ".PHONY: implib\n\n";
502 foreach my $mod (sort keys %importlibs)
504 my $dir = $directories{$mod};
505 my $lib = $importlibs{$mod};
506 my $spec = $mod;
507 $spec =~ s/\.dll$//;
508 $text .= sprintf "%s/%s.\$(IMPLIBEXT): %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
509 $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(IMPLIBEXT)\n\n", $dir, $lib;
510 next unless $static_implibs{$mod};
511 $text .= sprintf "%s/%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
512 $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
514 foreach my $mod (sort @lib_symlinks)
516 my $dir = $directories{$mod};
517 my $lib = $importlibs{$mod} . ".\$(IMPLIBEXT)";
518 $text .= sprintf "%s: %s/%s\n", $lib, $dir, $lib;
519 $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s \$@\n\n", $dir, $lib;
522 $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
523 $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
525 # output the inter-dll dependencies and rules
527 $text .= "# Map library name to the corresponding directory\n\n";
529 foreach my $mod (sort keys %staticlib_dirs)
531 $text .= sprintf "%s/%s: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
533 $text .= "\n# Misc rules\n";
535 replace_in_file( "dlls/Makefile.in",
536 '^# special configure-dependent targets',
537 '^# Misc rules',
538 $text );
540 # .gitignore file
542 foreach my $mod (sort @lib_symlinks)
544 push @ignores, "dlls/$importlibs{$mod}.def";
546 foreach my $mod (sort keys %directories)
548 next unless defined $altnames{$mod};
549 push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
551 foreach my $mod (sort keys %importlibs)
553 my $dir = $directories{$mod};
554 my $def = $mod;
555 $def =~ s/\.(dll|drv)$//;
556 push @ignores, "dlls/$dir/lib$def.def";
559 return @ignores;
563 ################################################################
564 # update programs/Makefile.in
566 sub update_progs(@)
568 my (@subdirs, @install_subdirs, @install_progs);
570 my @ignores = ();
572 foreach my $make (@_)
574 my %makefile = %{$makefiles{$make}};
575 my $module = $makefile{"MODULE"};
576 (my $dir = $make) =~ s/^programs\/(.*)\/Makefile$/$1/;
577 die "Invalid module $module in $make" unless "$dir.exe" eq $module;
578 next if defined $makefile{"=skip"};
579 push @subdirs, $dir;
580 push @ignores, "programs/$dir/$dir";
581 push @install_subdirs, $dir unless $dont_install{$dir};
582 push @install_progs, $dir if $bin_install{$dir};
585 replace_in_file( "programs/Makefile.in", '^SUBDIRS\s*=', '^INSTALLDIRS',
586 "SUBDIRS = \\\n\t",
587 join( " \\\n\t", @subdirs ),
588 "\n\n# Sub-directories to run make install into\nINSTALLSUBDIRS = \\\n\t",
589 join( " \\\n\t", @install_subdirs ),
590 "\n\n# Programs to install in bin directory\nINSTALLPROGS = \\\n\t",
591 join( " \\\n\t", @install_progs ),
592 "\n\nINSTALLDIRS = \$(DESTDIR)\$(bindir)\n" );
594 return @ignores;
598 ################################################################
599 # update the main .gitignore
601 sub update_gitignore(@)
603 my @ignores = values %makerules;
605 foreach my $make (@makefiles)
607 my %makefile = %{$makefiles{$make}};
608 my $dir = $makefile{"=dir"};
609 if (defined $makefile{"MANPAGES"})
611 push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
613 if (defined $makefile{"PROGRAMS"})
615 push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
619 # prepend a slash to paths that don't have one
620 @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
622 push @ignores, @_;
624 replace_in_file( ".gitignore", undef, undef,
625 "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
626 join("\n", sort @ignores), "\n" );
630 update_makefiles( @makefiles );
631 push @ignores, update_ignores( @makefiles );
632 push @ignores, update_winetest( @makefiles );
633 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
634 push @ignores, update_progs( sort grep /^programs\/.*\/Makefile$/, @makefiles );
635 update_gitignore( @ignores );