tools: Upgrade the config.guess/config.sub scripts.
[wine.git] / tools / make_makefiles
blob069d0ae9b7c072ed35be9d7983d9950d2ec1e1c7
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 # Programs that we want to install in the bin directory too
25 my %bin_install =
27 "msiexec" => 1,
28 "notepad" => 1,
29 "regedit" => 1,
30 "regsvr32" => 1,
31 "wineboot" => 1,
32 "winecfg" => 1,
33 "wineconsole" => 1,
34 "winedbg" => 1,
35 "winefile" => 1,
36 "winemine" => 1,
37 "winepath" => 1,
40 # Programs that we don't want to install at all
41 my %dont_install =
43 "winetest" => 1,
46 # Dlls and programs that are 16-bit specific
47 my %modules16 =
49 "ifsmgr.vxd" => 1,
50 "mmdevldr.vxd" => 1,
51 "monodebg.vxd" => 1,
52 "vdhcp.vxd" => 1,
53 "vmm.vxd" => 1,
54 "vnbt.vxd" => 1,
55 "vnetbios.vxd" => 1,
56 "vtdapi.vxd" => 1,
57 "vwin32.vxd" => 1,
58 "w32skrnl.dll" => 1,
59 "winevdm.exe" => 1,
60 "wow32.dll" => 1,
63 my %exported_wine_headers = (
64 "wine/debug.h" => 1,
65 "wine/exception.h" => 1,
66 "wine/library.h" => 1,
67 "wine/unicode.h" => 1,
68 "wine/itss.idl" => 1,
69 "wine/svcctl.idl" => 1,
72 my %private_idl_headers = (
73 "access.idl" => 1,
74 "asynot.idl" => 1,
75 "asysta.idl" => 1,
76 "axcore.idl" => 1,
77 "axextend.idl" => 1,
78 "binres.idl" => 1,
79 "chprst.idl" => 1,
80 "cmdbas.idl" => 1,
81 "cmdtxt.idl" => 1,
82 "crtrow.idl" => 1,
83 "dbccmd.idl" => 1,
84 "dbcses.idl" => 1,
85 "dbdsad.idl" => 1,
86 "dbinit.idl" => 1,
87 "dbprop.idl" => 1,
88 "dbs.idl" => 1,
89 "devenum.idl" => 1,
90 "dyngraph.idl" => 1,
91 "errrec.idl" => 1,
92 "opnrst.idl" => 1,
93 "row.idl" => 1,
94 "rowchg.idl" => 1,
95 "rowpos.idl" => 1,
96 "rowpsc.idl" => 1,
97 "rstbas.idl" => 1,
98 "rstinf.idl" => 1,
99 "rstloc.idl" => 1,
100 "rstnot.idl" => 1,
101 "srcrst.idl" => 1,
102 "sesprp.idl" => 1,
103 "vmrender.idl" => 1,
104 "xmldom.idl" => 1,
105 "xmldso.idl" => 1,
106 "wine/winedxgi.idl" => 1,
109 my %ignored_source_files = (
110 "dlls/wineps.drv/afm2c.c" => 1,
111 "dlls/wineps.drv/mkagl.c" => 1,
112 "programs/winetest/dist.rc" => 1,
113 "tools/makedep.c" => 1,
116 my (@linguas, @makefiles, %makefiles);
118 sub dirname($)
120 my $ret = shift;
121 return "" unless $ret =~ /\//;
122 $ret =~ s!/[^/]*$!!;
123 return $ret;
126 # update a file if changed
127 sub update_file($)
129 my $file = shift;
130 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
131 if (!$ret)
133 unlink "$file.new";
135 else
137 rename "$file.new", "$file";
138 print "$file updated\n";
139 if ($file eq "configure.ac")
141 system "autoconf";
142 print "configure updated\n";
145 return $ret;
148 # replace some lines in a file between two markers
149 sub replace_in_file($$$@)
151 my $file = shift;
152 my $start = shift;
153 my $end = shift;
155 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
157 if (defined($start))
159 open OLD_FILE, "$file" or die "cannot open $file";
160 while (<OLD_FILE>)
162 last if /$start/;
163 print NEW_FILE $_;
167 print NEW_FILE @_;
169 if (defined($end))
171 my $skip=1;
172 while (<OLD_FILE>)
174 print NEW_FILE $_ unless $skip;
175 $skip = 0 if /$end/;
179 close OLD_FILE if defined($start);
180 close NEW_FILE;
181 return update_file($file);
184 # replace a variable in a makefile
185 sub replace_makefile_variable($$)
187 my ($file, $var) = @_;
188 my $make = $makefiles{$file};
189 my $replaced = 0;
190 my @values;
192 if (defined ${$make}{"=$var"})
194 @values = @{${$make}{"=$var"}};
195 ${$make}{$var} = \@values;
197 else
199 return unless defined ${$make}{$var};
200 undef ${$make}{$var};
203 open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
205 open OLD_FILE, "$file.in" or die "cannot open $file.in";
206 while (<OLD_FILE>)
208 if (/^\s*($var\s*)=/)
210 # try to preserve formatting
211 my $prefix = $1;
212 my $multiline = /\\$/ || (@values > 1);
213 while (/\\$/)
215 $_ = <OLD_FILE>;
216 last unless $_;
218 if (!@values)
220 # nothing
222 elsif ($multiline)
224 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
226 else
228 print NEW_FILE "$prefix= @values\n";
230 $replaced = 1;
231 next;
233 if (/^\@MAKE/ && !$replaced && @values)
235 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
237 print NEW_FILE $_;
239 close OLD_FILE;
240 close NEW_FILE;
241 return update_file("$file.in");
244 # parse the specified makefile and load the variables
245 sub parse_makefile($)
247 my $file = shift;
248 my %make;
250 ($make{"=dir"} = $file) =~ s/[^\/]+$//;
252 open MAKE, "$file.in" or die "cannot open $file.in\n";
254 while (<MAKE>)
256 chomp;
257 next if (/^\s*#/);
258 while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
259 next if (/^\s*$/);
261 if (/^\s*(MODULE|IMPORTLIB|TESTDLL|PARENTSRC|APPMODE)\s*=\s*(.*)/)
263 my $var = $1;
264 $make{$var} = $2;
265 ${$make{"=flags"}}{"implib"} = 1 if $var eq "IMPORTLIB";
266 next;
268 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)\s*=\s*(.*)/)
270 my $var = $1;
271 my @list = split(/\s+/, $2);
272 $make{$var} = \@list;
273 ${$make{"=flags"}}{"clean"} = 1 if $var eq "PROGRAMS";
274 ${$make{"=flags"}}{"clean"} = 1 if $var eq "EXTRA_TARGETS";
275 next;
277 if (/(install-lib|install-dev|clean)\s*:/)
279 ${$make{"=flags"}}{$1} = 1;
280 next;
282 if (/^\s*(TOPSRCDIR|TOPOBJDIR|SRCDIR|VPATH)\s*=\s*(.*)/)
284 die "Variable $1 in $file.in is obsolete";
286 if (/\@[A-Z_]+\@/) # config.status substitution variable
288 ${$make{"=flags"}}{"config"} = 1;
292 if ($file =~ /^programs\/([^\/]+)\/Makefile/)
294 ${$make{"=flags"}}{"install"} = 1 unless $dont_install{$1};
295 ${$make{"=flags"}}{"installbin"} = 1 if $bin_install{$1};
298 if (defined $make{"=flags"} && defined $make{"MODULE"})
300 die "Custom install-lib rule not allowed in $file" if defined ${$make{"=flags"}}{"install-lib"};
301 die "Custom install-dev rule not allowed in $file" if defined ${$make{"=flags"}}{"install-dev"};
304 return %make;
307 # read pragma makedep flags from a source file
308 sub get_makedep_flags($)
310 my $file = shift;
311 my %flags;
313 open FILE, $file or die "cannot open $file";
314 while (<FILE>)
316 next unless /^#\s*pragma\s+makedep\s+(.*)/;
317 foreach my $flag (split /\s+/, $1)
319 last if $flag eq "depend";
320 $flags{$flag} = 1;
323 close FILE;
324 return %flags;
327 sub get_parent_makefile($)
329 my $file = shift;
330 my %make = %{$makefiles{$file}};
331 my $reldir = $make{"PARENTSRC"} || "";
332 return "" unless $reldir;
333 (my $path = $file) =~ s/\/Makefile$/\//;
334 while ($reldir =~ /^\.\.\//)
336 $reldir =~ s/^\.\.\///;
337 $path =~ s/[^\/]+\/$//;
339 return "$path$reldir/Makefile";
342 # preserve shared source files that are listed in the existing makefile
343 sub preserve_shared_source_files($$$)
345 my ($make, $parent, $var) = @_;
346 my %srcs;
348 return unless defined ${$parent}{"=$var"};
349 foreach my $file (@{${$parent}{"=$var"}}) { $srcs{$file} = 1; }
350 foreach my $file (@{${$make}{"=$var"}}) { $srcs{$file} = 0; }
352 foreach my $file (@{${$make}{$var}})
354 next unless defined $srcs{$file} && $srcs{$file} == 1;
355 push @{${$make}{"=$var"}}, $file;
359 # assign source files to their respective makefile
360 sub assign_sources_to_makefiles(@)
362 foreach my $file (@_)
364 next if defined $ignored_source_files{$file};
365 next if $file =~ /Makefile\.in$/;
366 my $dir = dirname( $file );
367 my $subdir = $dir;
369 while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
370 $subdir =~ s/^$dir\/?//;
371 next unless $dir;
373 die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
375 my $make = $makefiles{"$dir/Makefile"};
376 my $name = substr( $file, length($dir) + 1 );
378 ${$make}{"=flags"}{"clean"} = 1 if $subdir;
380 if ($dir eq "include")
382 next if ($name =~ /\.in$/);
383 if ($name =~ /^wine\// && !$exported_wine_headers{$name})
385 if ($private_idl_headers{$name}) { push @{${$make}{"=PRIVATE_IDL_H_SRCS"}}, $name; }
386 next;
388 if ($name =~ /stdole2\.idl$/) { push @{${$make}{"=IDL_TLB_SRCS"}}, $name; }
389 elsif ($private_idl_headers{$name}) { push @{${$make}{"=SRCDIR_INCLUDES"}}, $name; }
390 elsif ($name =~ /\.h$/) { push @{${$make}{"=SRCDIR_INCLUDES"}}, $name; }
391 elsif ($name =~ /\.x$/) { push @{${$make}{"=XTEMPLATE_SRCS"}}, $name; }
392 elsif ($name =~ /\.rh$/) { push @{${$make}{"=SRCDIR_INCLUDES"}}, $name; }
393 elsif ($name =~ /\.inl$/) { push @{${$make}{"=SRCDIR_INCLUDES"}}, $name; }
394 elsif ($name =~ /\.idl$/) { push @{${$make}{"=PUBLIC_IDL_H_SRCS"}}, $name; }
395 else { die "unknown file $name in include dir"; }
397 else
399 if ($name =~ /\.m$/) { push @{${$make}{"=OBJC_SRCS"}}, $name; }
400 elsif ($name =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $name; }
401 elsif ($name =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $name; }
402 elsif ($name =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $name; }
403 elsif ($name =~ /\.sfd$/) { push @{${$make}{"=FONT_SRCS"}}, $name; }
404 elsif ($name =~ /\.c$/)
406 my %flags = get_makedep_flags( $file );
407 if (defined $flags{"implib"})
409 push @{${$make}{"=IMPLIB_SRCS"}}, $name;
410 ${${$make}{"=flags"}}{"staticimplib"} = 1;
412 push @{${$make}{"=C_SRCS"}}, $name;
414 elsif ($name =~ /\.rc$/)
416 my %flags = get_makedep_flags( $file );
417 ${${$make}{"=flags"}}{"po"} = 1 if defined $flags{"po"};
418 push @{${$make}{"=RC_SRCS"}}, $name;
420 elsif ($name =~ /\.mc$/)
422 push @{${$make}{"=MC_SRCS"}}, $name;
423 ${${$make}{"=flags"}}{"mc"} = 1;
425 elsif ($name =~ /\.idl$/)
427 push @{${$make}{"=IDL_SRCS"}}, $name;
428 ${${$make}{"=flags"}}{"clean"} = 1;
430 elsif ($name =~ /\.man\.in$/)
432 push @{${$make}{"=MANPAGES"}}, $name;
433 ${${$make}{"=flags"}}{"manpage"} = 1;
435 elsif ($name =~ /\.in$/)
437 push @{${$make}{"=IN_SRCS"}}, $name;
442 # add extra variables to include source list
443 my $make = $makefiles{"include/Makefile"};
444 unshift @{${$make}{"=SRCDIR_INCLUDES"}}, "\$(XTEMPLATE_SRCS)";
445 unshift @{${$make}{"=SRCDIR_INCLUDES"}}, "\$(PUBLIC_IDL_H_SRCS)";
446 unshift @{${$make}{"=SRCDIR_INCLUDES"}}, "\$(IDL_TLB_SRCS)";
447 unshift @{${$make}{"=IDL_SRCS"}}, "\$(IDL_H_SRCS) \$(IDL_TLB_SRCS)";
449 # preserve shared source files from the parent makefile
450 foreach my $file (@makefiles)
452 my $make = $makefiles{$file};
453 my $parent = get_parent_makefile( $file );
454 next unless $parent;
455 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "C_SRCS" );
456 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "IDL_SRCS" );
457 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "LEX_SRCS" );
458 preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "BISON_SRCS" );
459 ${${$make}{"=flags"}}{"clean"} = 1 if defined ${$make}{"=IDL_SRCS"} && @{${$make}{"=IDL_SRCS"}};
463 ################################################################
464 # update the makefile list in configure.ac
466 sub update_makefiles(@)
468 my (@lines);
470 foreach my $file (sort @_)
472 my %make = %{$makefiles{$file}};
473 my $args = "";
474 my $is_win16 = $make{"MODULE"} && ($make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}});
475 my $flag_args = defined $make{"=flags"} ? ",[" . join(",",sort keys %{$make{"=flags"}}) ."]" : "";
476 if (defined($make{"TESTDLL"})) # test
478 die "TESTDLL should not be defined in $file" unless $file =~ /\/tests\/Makefile$/;
479 die "MODULE should not be defined in $file" if defined $make{"MODULE"};
480 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
481 (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
482 push @lines, "WINE_CONFIG_TEST($dir$flag_args)\n";
484 elsif (defined($make{"MODULE"}) && $make{"MODULE"} =~ /\.a$/) # import lib
486 die "MODULE should not be defined as static lib in $file" unless $file =~ /^dlls\//;
487 die "APPMODE should not be defined in $file" if defined $make{"APPMODE"};
488 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
489 (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
490 push @lines, "WINE_CONFIG_LIB($name$flag_args)\n";
492 elsif (defined($make{"MODULE"}) && defined($make{"APPMODE"})) # program
494 die "APPMODE should not be defined in $file" unless $file =~ /^programs\//;
495 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
496 (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
497 if ($name =~ /\./)
499 die "Invalid MODULE in $file" unless $make{"MODULE"} eq $name;
501 else
503 die "Invalid MODULE in $file" unless $make{"MODULE"} eq "$name.exe";
505 $args .= "," if $is_win16 || defined $make{"=flags"};
506 $args .= "enable_win16" if $is_win16;
507 push @lines, "WINE_CONFIG_PROGRAM($name$args$flag_args)\n";
509 elsif (defined($make{"MODULE"})) # dll
511 die "APPMODE should be defined in $file" if $file =~ /^programs\//;
512 die "MODULE should not be defined in $file" unless $file =~ /^dlls\//;
513 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
514 (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
515 if ($name =~ /\./)
517 die "Invalid MODULE in $file" unless $make{"MODULE"} eq $name;
519 else
521 die "Invalid MODULE in $file" unless $make{"MODULE"} eq "$name.dll";
523 my $implib = $make{"IMPORTLIB"} || "";
524 $args .= "," if $is_win16 || defined $make{"=flags"};
525 $args .= "enable_win16" if $is_win16;
526 $args .= $flag_args;
527 $args .= ",[$implib]" if $implib && $implib ne $name;
528 push @lines, "WINE_CONFIG_DLL($name$args)\n";
530 elsif ($file =~ /^tools.*\/Makefile$/)
532 die "APPMODE should not be defined in $file" if defined $make{"APPMODE"};
533 die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
534 (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
535 push @lines, "WINE_CONFIG_TOOL($name$flag_args)\n";
537 elsif ($file =~ /\/Makefile$/)
539 (my $name = $file) =~ s/^(.*)\/Makefile/$1/;
540 $args = "[$name]";
541 $args .= "," if defined $make{"=flags"};
542 push @lines, "WINE_CONFIG_MAKEFILE($args$flag_args)\n";
546 push @lines, "\nAC_SUBST([LINGUAS],[\"\\\n", join( " \\\n", sort @linguas ), "\"])\n\n";
548 # update the source variables in all the makefiles
550 foreach my $file (sort @_)
552 replace_makefile_variable( $file, "LEX_SRCS" );
553 replace_makefile_variable( $file, "BISON_SRCS" );
554 replace_makefile_variable( $file, "MC_SRCS" );
555 replace_makefile_variable( $file, "SVG_SRCS" );
556 replace_makefile_variable( $file, "FONT_SRCS" );
557 replace_makefile_variable( $file, "C_SRCS" );
558 replace_makefile_variable( $file, "OBJC_SRCS" );
559 replace_makefile_variable( $file, "RC_SRCS" );
560 replace_makefile_variable( $file, "IDL_SRCS" );
561 replace_makefile_variable( $file, "XTEMPLATE_SRCS" );
562 replace_makefile_variable( $file, "IN_SRCS" );
563 replace_makefile_variable( $file, "IMPLIB_SRCS" );
564 replace_makefile_variable( $file, "MANPAGES" );
565 next unless $file eq "include/Makefile";
566 replace_makefile_variable( $file, "PRIVATE_IDL_H_SRCS" );
567 replace_makefile_variable( $file, "PUBLIC_IDL_H_SRCS" );
568 replace_makefile_variable( $file, "IDL_TLB_SRCS" );
569 replace_makefile_variable( $file, "SRCDIR_INCLUDES" );
572 push @lines, "dnl End of auto-generated output commands\n";
573 replace_in_file( "configure.ac", '^WINE_CONFIG_DLL', '^dnl End of auto-generated output commands\n$', @lines);
577 ################################################################
578 # update the LINGUAS file
580 sub update_linguas(@)
582 replace_in_file( "po/LINGUAS", undef, undef, join("\n", sort @_), "\n" );
586 my $git_dir = $ENV{GIT_DIR} || ".git";
587 die "needs to be run from a git checkout" unless -d $git_dir;
589 my @all_files = split /\0/, `git ls-files -c -z`;
590 @linguas = map { (my $ret = $_) =~ s/^po\/(.*)\.po/$1/; $ret; } grep /^po\/.*\.po$/, @all_files;
591 @makefiles = map { (my $ret = $_) =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
593 foreach my $file (sort @makefiles)
595 my %make = parse_makefile( $file );
596 $makefiles{$file} = \%make;
599 assign_sources_to_makefiles( @all_files );
600 update_linguas( @linguas );
601 update_makefiles( @makefiles );