makefiles: Generate rules for installing IDL include files.
[wine.git] / tools / make_makefiles
blob15257692d0b742ea1e9836bf26993e797b5eebff
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/library.h" => 1,
45 "wine/unicode.h" => 1,
46 "wine/itss.idl" => 1,
47 "wine/svcctl.idl" => 1,
50 my %ignored_source_files = (
51 "dlls/wineps.drv/afm2c.c" => 1,
52 "dlls/wineps.drv/mkagl.c" => 1,
53 "include/config.h.in" => 1,
54 "programs/winetest/dist.rc" => 1,
55 "tools/makedep.c" => 1,
58 my (@linguas, @makefiles, %makefiles);
60 sub dirname($)
62 my $ret = shift;
63 return "" unless $ret =~ /\//;
64 $ret =~ s!/[^/]*$!!;
65 return $ret;
68 # update a file if changed
69 sub update_file($)
71 my $file = shift;
72 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
73 if (!$ret)
75 unlink "$file.new";
77 else
79 rename "$file.new", "$file";
80 print "$file updated\n";
81 if ($file eq "configure.ac")
83 system "autoconf";
84 print "configure updated\n";
87 return $ret;
90 # replace some lines in a file between two markers
91 sub replace_in_file($$$@)
93 my $file = shift;
94 my $start = shift;
95 my $end = shift;
97 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
99 if (defined($start))
101 open OLD_FILE, "$file" or die "cannot open $file";
102 while (<OLD_FILE>)
104 last if /$start/;
105 print NEW_FILE $_;
109 print NEW_FILE @_;
111 if (defined($end))
113 my $skip=1;
114 while (<OLD_FILE>)
116 print NEW_FILE $_ unless $skip;
117 $skip = 0 if /$end/;
121 close OLD_FILE if defined($start);
122 close NEW_FILE;
123 return update_file($file);
126 # replace a variable in a makefile
127 sub replace_makefile_variable($$)
129 my ($file, $var) = @_;
130 my $make = $makefiles{$file};
131 my $replaced = 0;
132 my @values;
134 if (defined ${$make}{"=$var"})
136 @values = @{${$make}{"=$var"}};
137 ${$make}{$var} = \@values;
139 else
141 return unless defined ${$make}{$var};
142 undef ${$make}{$var};
145 open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
147 open OLD_FILE, "$file.in" or die "cannot open $file.in";
148 while (<OLD_FILE>)
150 if (/^\s*($var\s*)=/)
152 # try to preserve formatting
153 my $prefix = $1;
154 my $multiline = /\\$/ || (@values > 1);
155 while (/\\$/)
157 $_ = <OLD_FILE>;
158 last unless $_;
160 if (!@values)
162 # nothing
164 elsif ($multiline)
166 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
168 else
170 print NEW_FILE "$prefix= @values\n";
172 $replaced = 1;
173 next;
175 if (/^\@MAKE/ && !$replaced && @values)
177 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
179 print NEW_FILE $_;
181 close OLD_FILE;
182 close NEW_FILE;
183 return update_file("$file.in");
186 # parse the specified makefile and load the variables
187 sub parse_makefile($)
189 my $file = shift;
190 my %make;
192 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
194 open MAKE, "$file.in" or die "cannot open $file.in\n";
196 while (<MAKE>)
198 chomp;
199 next if (/^\s*#/);
200 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
201 next if (/^\s*$/);
203 if (/^\s*(MODULE|IMPORTLIB|TESTDLL|PARENTSRC|APPMODE)\s*=\s*(.*)/)
205 my $var = $1;
206 $make{$var} = $2;
207 ${$make{"=flags"}}{"implib"} = 1 if $var eq "IMPORTLIB";
208 next;
210 if (/^\s*(BISON_SRCS|LEX_SRCS|IDL_SRCS|IMPLIB_SRCS|C_SRCS|OBJC_SRCS|MC_SRCS|RC_SRCS|SVG_SRCS|FONT_SRCS|IN_SRCS|PROGRAMS|EXTRA_TARGETS|MANPAGES|INSTALL_LIB|INSTALL_DEV)\s*=\s*(.*)/)
212 my $var = $1;
213 my @list = split(/\s+/, $2);
214 $make{$var} = \@list;
215 next;
217 if (/(install-lib|install-dev|clean)\s*:/)
219 ${$make{"=flags"}}{$1} = 1;
220 next;
222 if (/^\s*(TOPSRCDIR|TOPOBJDIR|SRCDIR|VPATH)\s*=\s*(.*)/)
224 die "Variable $1 in $file.in is obsolete";
226 if (/\@[A-Z_]+\@/) # config.status substitution variable
228 ${$make{"=flags"}}{"config"} = 1;
232 if ($file =~ /^programs\/([^\/]+)\/Makefile/)
234 my $prog = $1;
235 if (defined $make{"INSTALL_LIB"})
237 ${$make{"=flags"}}{"install"} = 1 if grep { "$prog.exe" eq $_; } @{$make{"INSTALL_LIB"}};
238 ${$make{"=flags"}}{"installbin"} = 1 if grep { $prog eq $_; } @{$make{"INSTALL_LIB"}};
240 else
242 ${$make{"=flags"}}{"install"} = 1;
246 unless (defined $make{"MODULE"})
248 ${$make{"=flags"}}{"install-lib"} = 1 if defined $make{"INSTALL_LIB"};
249 ${$make{"=flags"}}{"install-dev"} = 1 if defined $make{"INSTALL_DEV"};
251 ${$make{"=flags"}}{"clean"} = 1 if defined $make{"PROGRAMS"} || defined $make{"EXTRA_TARGETS"};
253 if (defined $make{"=flags"} && defined $make{"MODULE"})
255 die "Custom install-lib rule not allowed in $file" if defined ${$make{"=flags"}}{"install-lib"};
256 die "Custom install-dev rule not allowed in $file" if defined ${$make{"=flags"}}{"install-dev"};
259 return %make;
262 # read pragma makedep flags from a source file
263 sub get_makedep_flags($)
265 my $file = shift;
266 my %flags;
268 open FILE, $file or die "cannot open $file";
269 while (<FILE>)
271 next unless /^#\s*pragma\s+makedep\s+(.*)/;
272 foreach my $flag (split /\s+/, $1)
274 last if $flag eq "depend";
275 $flags{$flag} = 1;
278 close FILE;
279 return %flags;
282 sub get_parent_makefile($)
284 my $file = shift;
285 my %make = %{$makefiles{$file}};
286 my $reldir = $make{"PARENTSRC"} || "";
287 return "" unless $reldir;
288 (my $path = $file) =~ s/\/Makefile$/\//;
289 while ($reldir =~ /^\.\.\//)
291 $reldir =~ s/^\.\.\///;
292 $path =~ s/[^\/]+\/$//;
294 return "$path$reldir/Makefile";
297 # preserve shared source files that are listed in the existing makefile
298 sub preserve_shared_source_files($$$)
300 my ($make, $parent, $var) = @_;
301 my %srcs;
303 return unless defined ${$parent}{"=$var"};
304 foreach my $file (@{${$parent}{"=$var"}}) { $srcs{$file} = 1; }
305 foreach my $file (@{${$make}{"=$var"}}) { $srcs{$file} = 0; }
307 foreach my $file (@{${$make}{$var}})
309 next unless defined $srcs{$file} && $srcs{$file} == 1;
310 push @{${$make}{"=$var"}}, $file;
314 # assign source files to their respective makefile
315 sub assign_sources_to_makefiles(@)
317 foreach my $file (@_)
319 next if defined $ignored_source_files{$file};
320 next if $file =~ /Makefile\.in$/;
321 my $dir = dirname( $file );
322 my $subdir = $dir;
324 while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
325 $subdir =~ s/^$dir\/?//;
326 next unless $dir;
328 die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
330 my $make = $makefiles{"$dir/Makefile"};
331 my $name = substr( $file, length($dir) + 1 );
332 my %flags = get_makedep_flags( $file );
334 next if $file =~ /^include\/wine\// && !%flags && !$exported_wine_headers{$name};
336 ${$make}{"=flags"}{"clean"} = 1 if $subdir;
338 if ($name =~ /\.m$/) { push @{${$make}{"=OBJC_SRCS"}}, $name; }
339 elsif ($name =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $name; }
340 elsif ($name =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $name; }
341 elsif ($name =~ /\.x$/) { push @{${$make}{"=XTEMPLATE_SRCS"}}, $name; }
342 elsif ($name =~ /\.rh$/) { push @{${$make}{"=HEADER_SRCS"}}, $name; }
343 elsif ($name =~ /\.inl$/) { push @{${$make}{"=HEADER_SRCS"}}, $name; }
344 elsif ($name =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $name; }
345 elsif ($name =~ /\.sfd$/) { push @{${$make}{"=FONT_SRCS"}}, $name; }
346 elsif ($name =~ /\.c$/)
348 if (defined $flags{"implib"})
350 push @{${$make}{"=IMPLIB_SRCS"}}, $name;
351 ${${$make}{"=flags"}}{"staticimplib"} = 1;
353 push @{${$make}{"=C_SRCS"}}, $name;
355 elsif ($name =~ /\.h$/)
357 next if $dir ne "include";
358 push @{${$make}{"=HEADER_SRCS"}}, $name;
359 ${${$make}{"=flags"}}{"install-dev"} = 1;
361 elsif ($name =~ /\.rc$/)
363 ${${$make}{"=flags"}}{"po"} = 1 if defined $flags{"po"};
364 push @{${$make}{"=RC_SRCS"}}, $name;
366 elsif ($name =~ /\.mc$/)
368 push @{${$make}{"=MC_SRCS"}}, $name;
369 ${${$make}{"=flags"}}{"mc"} = 1;
371 elsif ($name =~ /\.idl$/)
373 die "no makedep flags specified in $file" unless %flags || $dir eq "include";
374 push @{${$make}{"=IDL_SRCS"}}, $name;
375 ${${$make}{"=flags"}}{"clean"} = 1;
377 elsif ($name =~ /\.man\.in$/)
379 push @{${$make}{"=MANPAGES"}}, $name;
380 ${${$make}{"=flags"}}{"manpage"} = 1;
382 elsif ($name =~ /\.in$/)
384 push @{${$make}{"=IN_SRCS"}}, $name;
388 # preserve shared source files from the parent makefile
389 foreach my $file (@makefiles)
391 my $make = $makefiles{$file};
392 my $parent = get_parent_makefile( $file );
393 next unless $parent;
394 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "C_SRCS" );
395 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "IDL_SRCS" );
396 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "LEX_SRCS" );
397 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "BISON_SRCS" );
398 ${${$make}{"=flags"}}{"clean"} = 1 if defined ${$make}{"=IDL_SRCS"} && @{${$make}{"=IDL_SRCS"}};
402 ################################################################
403 # update the makefile list in configure.ac
405 sub update_makefiles(@)
407 my (@lines);
409 foreach my $file (sort @_)
411 my %make = %{$makefiles{$file}};
412 my $args = "";
413 my $is_win16 = $make{"MODULE"} && ($make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}});
414 my $flag_args = defined $make{"=flags"} ? ",[" . join(",",sort keys %{$make{"=flags"}}) ."]" : "";
415 if (defined($make{"TESTDLL"})) # test
417 die "TESTDLL should not be defined in $file" unless $file =~ /\/tests\/Makefile$/;
418 die "MODULE should not be defined in $file" if defined $make{"MODULE"};
419 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
420 (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
421 push @lines, "WINE_CONFIG_TEST($dir$flag_args)\n";
423 elsif (defined($make{"MODULE"}) && $make{"MODULE"} =~ /\.a$/) # import lib
425 die "MODULE should not be defined as static lib in $file" unless $file =~ /^dlls\//;
426 die "APPMODE should not be defined in $file" if defined $make{"APPMODE"};
427 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
428 (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
429 push @lines, "WINE_CONFIG_LIB($name$flag_args)\n";
431 elsif (defined($make{"MODULE"}) && defined($make{"APPMODE"})) # program
433 die "APPMODE should not be defined in $file" unless $file =~ /^programs\//;
434 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
435 (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
436 if ($name =~ /\./)
438 die "Invalid MODULE in $file" unless $make{"MODULE"} eq $name;
440 else
442 die "Invalid MODULE in $file" unless $make{"MODULE"} eq "$name.exe";
444 $args .= "," if $is_win16 || defined $make{"=flags"};
445 $args .= "enable_win16" if $is_win16;
446 push @lines, "WINE_CONFIG_PROGRAM($name$args$flag_args)\n";
448 elsif (defined($make{"MODULE"})) # dll
450 die "APPMODE should be defined in $file" if $file =~ /^programs\//;
451 die "MODULE should not be defined in $file" unless $file =~ /^dlls\//;
452 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
453 (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
454 if ($name =~ /\./)
456 die "Invalid MODULE in $file" unless $make{"MODULE"} eq $name;
458 else
460 die "Invalid MODULE in $file" unless $make{"MODULE"} eq "$name.dll";
462 my $implib = $make{"IMPORTLIB"} || "";
463 $args .= "," if $is_win16 || defined $make{"=flags"};
464 $args .= "enable_win16" if $is_win16;
465 $args .= $flag_args;
466 $args .= ",[$implib]" if $implib && $implib ne $name;
467 push @lines, "WINE_CONFIG_DLL($name$args)\n";
469 elsif ($file =~ /^tools.*\/Makefile$/)
471 die "APPMODE should not be defined in $file" if defined $make{"APPMODE"};
472 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
473 (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
474 push @lines, "WINE_CONFIG_TOOL($name$flag_args)\n";
476 elsif ($file =~ /\/Makefile$/)
478 (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
479 $args = "[$name]";
480 $args .= "," if defined $make{"=flags"};
481 push @lines, "WINE_CONFIG_MAKEFILE($args$flag_args)\n";
485 push @lines, "\nAC_SUBST([LINGUAS],[\"\\\n", join( " \\\n", sort @linguas ), "\"])\n\n";
487 # update the source variables in all the makefiles
489 foreach my $file (sort @_)
491 replace_makefile_variable( $file, "LEX_SRCS" );
492 replace_makefile_variable( $file, "BISON_SRCS" );
493 replace_makefile_variable( $file, "MC_SRCS" );
494 replace_makefile_variable( $file, "SVG_SRCS" );
495 replace_makefile_variable( $file, "FONT_SRCS" );
496 replace_makefile_variable( $file, "C_SRCS" );
497 replace_makefile_variable( $file, "OBJC_SRCS" );
498 replace_makefile_variable( $file, "RC_SRCS" );
499 replace_makefile_variable( $file, "IDL_SRCS" );
500 replace_makefile_variable( $file, "HEADER_SRCS" );
501 replace_makefile_variable( $file, "XTEMPLATE_SRCS" );
502 replace_makefile_variable( $file, "IN_SRCS" );
503 replace_makefile_variable( $file, "IMPLIB_SRCS" );
504 replace_makefile_variable( $file, "MANPAGES" );
507 push @lines, "dnl End of auto-generated output commands\n";
508 replace_in_file( "configure.ac", '^WINE_CONFIG_DLL', '^dnl End of auto-generated output commands\n$', @lines);
512 ################################################################
513 # update the LINGUAS file
515 sub update_linguas(@)
517 replace_in_file( "po/LINGUAS", undef, undef, join("\n", sort @_), "\n" );
521 my $git_dir = $ENV{GIT_DIR} || ".git";
522 die "needs to be run from a git checkout" unless -d $git_dir;
524 my @all_files = split /\0/, `git ls-files -c -z`;
525 @linguas = map { (my $ret = $_) =~ s/^po\/(.*)\.po/$1/; $ret; } grep /^po\/.*\.po$/, @all_files;
526 @makefiles = map { (my $ret = $_) =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
528 foreach my $file (sort @makefiles)
530 my %make = parse_makefile( $file );
531 $makefiles{$file} = \%make;
534 assign_sources_to_makefiles( @all_files );
535 update_linguas( @linguas );
536 update_makefiles( @makefiles );