midimap: Handle MIDI running status.
[wine.git] / tools / make_makefiles
bloba316b42dd7305c77d4b26b7ee87128e2e388a072
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 use strict;
24 # Dlls and programs that are 16-bit specific
25 my %modules16 =
27 "ifsmgr.vxd" => 1,
28 "mmdevldr.vxd" => 1,
29 "monodebg.vxd" => 1,
30 "vdhcp.vxd" => 1,
31 "vmm.vxd" => 1,
32 "vnbt.vxd" => 1,
33 "vnetbios.vxd" => 1,
34 "vtdapi.vxd" => 1,
35 "vwin32.vxd" => 1,
36 "w32skrnl.dll" => 1,
37 "winevdm.exe" => 1,
38 "wow32.dll" => 1,
41 my %exported_wine_headers = (
42 "wine/debug.h" => 1,
43 "wine/exception.h" => 1,
44 "wine/itss.idl" => 1,
45 "wine/svcctl.idl" => 1,
48 my %ignored_source_files = (
49 "dlls/wineps.drv/afm2c.c" => 1,
50 "dlls/wineps.drv/mkagl.c" => 1,
51 "include/config.h.in" => 1,
52 "include/stamp-h.in" => 1,
53 "programs/winetest/dist.rc" => 1,
54 "tools/makedep.c" => 1,
57 my @source_vars = (
58 "BISON_SRCS",
59 "C_SRCS",
60 "FONT_SRCS",
61 "HEADER_SRCS",
62 "IDL_SRCS",
63 "IN_SRCS",
64 "LEX_SRCS",
65 "MANPAGES",
66 "MC_SRCS",
67 "OBJC_SRCS",
68 "PO_SRCS",
69 "RC_SRCS",
70 "SOURCES",
71 "SVG_SRCS",
72 "XTEMPLATE_SRCS"
75 my (@makefiles, %makefiles);
76 my @nls_files;
78 sub dirname($)
80 my $ret = shift;
81 return "" unless $ret =~ /\//;
82 $ret =~ s!/[^/]*$!!;
83 return $ret;
86 # update a file if changed
87 sub update_file($$)
89 my $file = shift;
90 my $new = shift;
92 open FILE, ">$file.new" or die "cannot create $file.new";
93 print FILE $new;
94 close FILE;
95 rename "$file.new", "$file";
96 print "$file updated\n";
97 if ($file eq "configure.ac")
99 system "autoconf";
100 print "configure updated\n";
104 # replace some lines in a file between two markers
105 sub replace_in_file($$$@)
107 my $file = shift;
108 my $start = shift;
109 my $end = shift;
110 my ($old, $new);
112 open OLD_FILE, "$file" or die "cannot open $file";
113 while (<OLD_FILE>)
115 $old .= $_;
116 last if /$start/;
117 $new .= $_;
120 $new .= join "", @_;
122 my $skip = 1;
123 while (<OLD_FILE>)
125 $old .= $_;
126 $new .= $_ unless $skip;
127 $skip = 0 if /$end/;
130 close OLD_FILE;
131 update_file($file, $new) if $old ne $new;
134 # replace all source variables in a makefile
135 sub replace_makefile_variables($)
137 my $file = shift;
138 my $make = $makefiles{$file};
139 my $source_vars_regexp = join "|", @source_vars;
140 my %replaced;
141 my $old;
142 my $new;
144 open OLD_FILE, "$file.in" or die "cannot open $file.in";
145 while (<OLD_FILE>)
147 $old .= $_;
148 if (/^\s*($source_vars_regexp)(\s*)=/)
150 # try to preserve formatting
151 my $var = $1;
152 my $spaces = $2;
153 my $replaced = 0;
154 my @values;
156 if (defined ${$make}{"=$var"})
158 @values = @{${$make}{"=$var"}};
159 ${$make}{$var} = \@values;
161 else
163 undef ${$make}{$var};
165 my $multiline = /\\$/ || (@values > 1);
166 my $old_str = $_;
167 while (/\\$/)
169 $_ = <OLD_FILE>;
170 last unless $_;
171 $old .= $_;
172 $old_str .= $_;
174 my $new_str = "";
175 if (!@values)
177 # nothing
179 elsif ($multiline)
181 $new_str = "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
182 $new .= $new_str;
184 else
186 $new_str = "$var$spaces= @values\n";
187 $new .= $new_str;
189 $replaced{$var} = 1;
190 next;
192 $new .= $_;
194 # if we are using SOURCES, ignore the other variables
195 unless ($replaced{"SOURCES"})
197 foreach my $var (@source_vars)
199 next if defined $replaced{$var};
200 next if $var eq "SOURCES";
201 next unless defined ${$make}{"=$var"};
202 my @values = @{${$make}{"=$var"}};
203 next unless @values;
204 $new .= "\n$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
207 close OLD_FILE;
208 update_file("$file.in", $new) if $old ne $new;
211 # parse the specified makefile and load the variables
212 sub parse_makefile($)
214 my $file = shift;
215 my %make;
217 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
219 open MAKE, "$file.in" or die "cannot open $file.in\n";
221 while (<MAKE>)
223 chomp;
224 next if (/^\s*#/);
225 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
226 next if (/^\s*$/);
228 if (/\@[A-Z_]+\@/) # config.status substitution variable
230 die "Configure substitution is not allowed in $file" unless $file eq "Makefile";
232 if (/^\s*(MODULE|IMPORTLIB|TESTDLL|STATICLIB|PARENTSRC|EXTRADLLFLAGS)\s*=\s*(.*)/)
234 my $var = $1;
235 $make{$var} = $2;
236 next;
238 my $source_vars_regexp = join "|", @source_vars;
239 if (/^\s*($source_vars_regexp|PROGRAMS|EXTRA_TARGETS|EXTRA_OBJS|INSTALL_LIB|INSTALL_DEV)\s*=\s*(.*)/)
241 my $var = $1;
242 my @list = split(/\s+/, $2);
243 $make{$var} = \@list;
244 next;
246 if (/^\s*(TOPSRCDIR|TOPOBJDIR|SRCDIR|VPATH)\s*=\s*(.*)/)
248 die "Variable $1 in $file.in is obsolete";
252 return %make;
255 # read pragma makedep flags from a source file
256 sub get_makedep_flags($)
258 my $file = shift;
259 my %flags;
261 open FILE, $file or die "cannot open $file";
262 if ($file =~ /\.sfd$/)
264 while (<FILE>)
266 next unless /^UComments:\s*\"(.*)\"$/;
267 foreach my $pragma (split /\+AAoA/, $1)
269 next unless $pragma =~ /^#\s*pragma\s+makedep\s+(.*)/;
270 foreach my $flag (split /\s+/, $1)
272 $flags{$flag} = 1;
273 last if $flag eq "font";
278 else
280 while (<FILE>)
282 next unless /^#\s*pragma\s+makedep\s+(.*)/;
283 foreach my $flag (split /\s+/, $1)
285 last if $flag eq "depend";
286 $flags{$flag} = 1;
290 close FILE;
291 return %flags;
294 sub get_parent_makefile($)
296 my $file = shift;
297 my %make = %{$makefiles{$file}};
298 my $reldir = $make{"PARENTSRC"} || "";
299 return "" unless $reldir;
300 (my $path = $file) =~ s/\/Makefile$/\//;
301 while ($reldir =~ /^\.\.\//)
303 $reldir =~ s/^\.\.\///;
304 $path =~ s/[^\/]+\/$//;
306 return "$path$reldir/Makefile";
309 # preserve shared source files that are listed in the existing makefile
310 sub preserve_shared_source_files($$$)
312 my ($make, $parent, $var) = @_;
313 my %srcs;
315 return unless defined ${$parent}{"=$var"};
316 foreach my $file (@{${$parent}{"=$var"}}) { $srcs{$file} = 1; }
317 foreach my $file (@{${$make}{"=$var"}}) { $srcs{$file} = 0; }
319 foreach my $file (@{${$make}{$var}})
321 next unless defined $srcs{$file} && $srcs{$file} == 1;
322 push @{${$make}{"=$var"}}, $file;
326 # assign source files to their respective makefile
327 sub assign_sources_to_makefiles(@)
329 foreach my $file (@_)
331 next if defined $ignored_source_files{$file};
332 next if $file =~ /Makefile\.in$/;
333 my $dir = dirname( $file );
334 my $subdir = $dir;
336 while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
337 $subdir =~ s/^$dir\/?//;
338 next unless $dir;
340 die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
342 my $make = $makefiles{"$dir/Makefile"};
343 my $name = substr( $file, length($dir) + 1 );
345 next if $file =~ /^include\/wine\// && !get_makedep_flags($file) && !$exported_wine_headers{$name};
347 if ($name =~ /\.m$/) { push @{${$make}{"=OBJC_SRCS"}}, $name; }
348 elsif ($name =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $name; }
349 elsif ($name =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $name; }
350 elsif ($name =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $name; }
351 elsif ($name =~ /\.sfd$/)
353 push @{${$make}{"=FONT_SRCS"}}, $name;
355 elsif ($name =~ /\.c$/)
357 push @{${$make}{"=C_SRCS"}}, $name;
359 elsif ($name =~ /\.h$/ || $name =~ /\.rh$/ || $name =~ /\.inl$/ || $name =~ /\.x$/)
361 next if $dir ne "include";
363 elsif ($name =~ /\.rc$/)
365 push @{${$make}{"=RC_SRCS"}}, $name;
367 elsif ($name =~ /\.mc$/)
369 push @{${$make}{"=MC_SRCS"}}, $name;
371 elsif ($name =~ /\.po$/)
373 push @{${$make}{"=PO_SRCS"}}, $name;
375 elsif ($name =~ /\.idl$/)
377 die "no makedep flags specified in $file" unless $dir eq "include" || get_makedep_flags($file);
378 push @{${$make}{"=IDL_SRCS"}}, $name;
380 elsif ($name =~ /\.man\.in$/)
382 push @{${$make}{"=MANPAGES"}}, $name;
384 elsif ($name =~ /\.in$/)
386 push @{${$make}{"=IN_SRCS"}}, $name;
388 elsif ($name =~ /\.spec$/)
390 next unless defined ${$make}{"TESTDLL"};
392 elsif ($name =~ /\.nls$/)
394 push @nls_files, $name if $dir eq "nls";
396 elsif ($dir ne "loader") # loader dir contains misc files
398 next;
400 push @{${$make}{"=SOURCES"}}, $name;
403 # preserve shared source files from the parent makefile
404 foreach my $file (@makefiles)
406 my $make = $makefiles{$file};
407 my $parent = get_parent_makefile( $file );
408 next unless $parent;
409 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "C_SRCS" );
410 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "RC_SRCS" );
411 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "IDL_SRCS" );
412 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "LEX_SRCS" );
413 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "BISON_SRCS" );
417 ################################################################
418 # update the makefile list in configure.ac
420 sub update_makefiles(@)
422 my (@lines);
424 foreach my $file (sort @_)
426 next if $file eq "Makefile";
427 my %make = %{$makefiles{$file}};
428 (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
429 my $args = "";
430 if (defined($make{"TESTDLL"})) # test
432 die "TESTDLL should not be defined in $file" unless $file =~ /\/tests\/Makefile$/;
433 die "MODULE should not be defined in $file" if defined $make{"MODULE"};
434 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
436 elsif (defined($make{"STATICLIB"}))
438 die "MODULE should not be defined in $file" if defined $make{"MODULE"};
439 die "invalid STATICLIB name" unless $make{"STATICLIB"} =~ /\.a$/;
441 elsif (defined($make{"MODULE"})) # dll or program
443 (my $name = $file) =~ s/^(dlls|programs)\/(.*)\/Makefile/$2/;
444 my $dllflags = $make{"EXTRADLLFLAGS"} || "";
445 die "invalid MODULE name" if $make{"MODULE"} =~ /\.a$/;
446 die "MODULE should not be defined in $file" unless $file =~ /^(dlls|programs)\//;
447 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
448 if ($file =~ /^programs\//)
450 die "EXTRADLLFLAGS should be defined in $file" unless $dllflags;
451 die "EXTRADLLFLAGS should contain -mconsole or -mwindows in $file" unless $dllflags =~ /-m(console|windows)/;
452 die "Invalid MODULE in $file" unless ($name =~ /\./ && $make{"MODULE"} eq $name) || $make{"MODULE"} eq "$name.exe";
454 else
456 die "EXTRADLLFLAGS should not contain -mconsole or -mwindows in $file" if $dllflags =~ /-m(console|windows)/;
457 die "Invalid MODULE in $file" unless ($name =~ /\./ && $make{"MODULE"} eq $name) || $make{"MODULE"} eq "$name.dll";
459 if (defined $make{"IMPORTLIB"})
461 die "IMPORTLIB not allowed in programs\n" if $file =~ /^programs\//;
462 die "Invalid IMPORTLIB name in $file" if $make{"IMPORTLIB"} =~ /\./;
464 $args = ",enable_win16" if $make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}};
466 elsif ($file =~ /^tools.*\/Makefile$/)
468 die "MODULE should not be defined in $file" if defined $make{"MODULE"};
469 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
470 die "EXTRADLLFLAGS should not be defined in $file" if defined $make{"EXTRADLLFLAGS"};
471 $args = ",,[test \"x\$enable_tools\" = xno]";
473 push @lines, "WINE_CONFIG_MAKEFILE($dir$args)\n";
476 # update the source variables in all the makefiles
478 foreach my $file (sort @_) { replace_makefile_variables( $file ); }
480 push @lines, "dnl End of auto-generated output commands\n";
481 replace_in_file( "configure.ac", '^WINE_CONFIG_MAKEFILE', '^dnl End of auto-generated output commands\n$', @lines);
484 sub update_wine_inf()
486 my @lines;
487 push @lines, "[NlsFiles]", sort grep(!/^sort/, @nls_files);
488 push @lines, "\n[SortFiles]", sort grep(/^sort/, @nls_files);
489 push @lines, "\n[WineSourceDirs]\n";
490 replace_in_file "loader/wine.inf.in", '^\[NlsFiles\]', '^\[WineSourceDirs\]', join( "\n", @lines );
493 my $git_dir = $ENV{GIT_DIR} || ".git";
494 die "needs to be run from a git checkout" unless -e $git_dir;
496 my @all_files = split /\0/, `git ls-files -c -z`;
497 map { $ignored_source_files{$_} = 1; } split /\0/, `git ls-files -d -z`;
498 @makefiles = map { (my $ret = $_) =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
500 foreach my $file (sort @makefiles)
502 my %make = parse_makefile( $file );
503 $makefiles{$file} = \%make;
506 assign_sources_to_makefiles( @all_files );
507 update_makefiles( @makefiles );
508 update_wine_inf();