make_makefiles: Parse the makefiles to find the correct rules file dependency.
[wine] / tools / make_makefiles
1 #!/usr/bin/perl -w
2 #
3 # Build the auto-generated parts of the Wine makefiles.
4 #
5 # Copyright 2006 Alexandre Julliard
6 #
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.
11 #
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.
16 #
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
20 #
21
22 my %makerules =
23 (
24  "MAKE_RULES" => "Make.rules",
25  "MAKE_DLL_RULES" => "dlls/Makedll.rules",
26  "MAKE_IMPLIB_RULES" => "dlls/Makeimplib.rules",
27  "MAKE_TEST_RULES" => "dlls/Maketest.rules",
28  "MAKE_PROG_RULES" => "programs/Makeprog.rules",
29 );
30
31 my (@makefiles, %makefiles);
32
33 # update a file if changed
34 sub update_file($)
35 {
36     my $file = shift;
37     my $ret = system "cmp $file $file.new >/dev/null";
38     if (!$ret)
39     {
40         unlink "$file.new";
41         #print "$file is unchanged\n";
42     }
43     else
44     {
45         rename "$file.new", "$file";
46         print "$file updated\n";
47     }
48     return $ret;
49 }
50
51 # replace some lines in a file between two markers
52 sub replace_in_file($$$@)
53 {
54     my $file = shift;
55     my $start = shift;
56     my $end = shift;
57
58     open OLD_FILE, "$file" or die "cannot open $file";
59     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
60
61     while (<OLD_FILE>)
62     {
63         last if /$start/;
64         print NEW_FILE $_;
65     }
66
67     print NEW_FILE @_;
68
69     if (defined($end))
70     {
71         my $skip=1;
72         while (<OLD_FILE>)
73         {
74             print NEW_FILE $_ unless $skip;
75             $skip = 0 if /$end/;
76         }
77     }
78
79     close OLD_FILE;
80     close NEW_FILE;
81     return update_file($file);
82 }
83
84 # parse the specified makefile to identify the rules file
85 sub parse_makefile($)
86 {
87     my $file = shift;
88
89     open MAKE, "$file.in" or die "cannot open $file.in\n";
90
91     while (<MAKE>)
92     {
93         chomp;
94         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
95
96         if (/^\@(MAKE.*RULES)\@/)
97         {
98             my $var = $1;
99             $makefiles{$file} = $makerules{$var};
100             return;
101         }
102     }
103 }
104
105 if (-d ".git")
106 {
107     @makefiles = map { s/\.in$//; $_; } split /\s/, `git ls-files -c Makefile.in \\*/Makefile.in`;
108 }
109 else
110 {
111     @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
112 }
113
114 foreach my $file (sort values %makerules, @makefiles)
115 {
116     parse_makefile( $file );
117 }
118
119 ################################################################
120 # update the makefile list in configure.ac
121
122 my @lines = ();
123
124 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
125 {
126     push @lines, "$var=$makerules{$var}\n";
127     push @lines, "AC_SUBST_FILE($var)\n\n";
128 }
129
130 replace_in_file( "configure.ac", '^MAKE_RULES', '\]\)$',
131                  @lines,
132                  "AC_CONFIG_FILES([\n",
133                  join ("\n", (sort values %makerules), (sort @makefiles) ), "])\n" );
134
135
136 ################################################################
137 # update the tests list in programs/winetest/Makefile.in and programs/winetest/winetest.rc
138
139 my %modules = ( "gdi" => "gdi32", "user" => "user32" );
140 my %tests;
141 @lines = ( "TESTBINS =" );
142
143 foreach my $file (sort grep /^dlls\/.*\/tests\/Makefile/, @makefiles)
144 {
145     if ($file =~ /^dlls\/(.*)\/tests\/Makefile/)
146     {
147         my $dir = $1;
148         my $mod = $modules{$dir} || $dir;
149         $tests{$mod} = $dir;
150         push @lines, " \\\n\t${mod}_test.exe";
151     }
152 }
153 push @lines, "\n\n";
154
155 foreach my $test (sort keys %tests)
156 {
157     my $dir = $tests{$test};
158     push @lines, "${test}_test.exe: \$(DLLDIR)/$dir/tests/${test}_test.exe\$(DLLEXT)\n";
159     push @lines, "\tcp \$(DLLDIR)/$dir/tests/${test}_test.exe\$(DLLEXT) \$\@ && \$(STRIP) \$\@\n";
160 }
161 push @lines, "\n# Special rules\n";
162
163 replace_in_file( "programs/winetest/Makefile.in", '^TESTBINS\s*=', '^# Special rules', @lines );
164
165 @lines = ();
166 foreach my $test (sort keys %tests)
167 {
168     push @lines, "${test}_test.exe TESTRES \"${test}_test.exe\"\n";
169 }
170
171 replace_in_file( "programs/winetest/winetest.rc", ' TESTRES ', undef, @lines );
172
173 ################################################################
174 # update the makefile list in Makefile.in
175
176 my @targets;
177 my @depends;
178
179 foreach my $file (sort values %makerules)
180 {
181     push @targets, $file;
182     if (!defined($makefiles{$file})) { push @depends, "$file: $file.in"; }
183     else { push @depends, "$file: $file.in Make.rules"; }
184 }
185
186 foreach my $file (sort @makefiles)
187 {
188     push @targets, $file unless $file eq "Makefile";
189     my $dep = $makefiles{$file};
190     push @depends, "$file: $file.in $dep";
191 }
192
193 @lines = ();
194 push @lines, "ALL_MAKEFILES = \\\n\t";
195 push @lines, join (" \\\n\t", @targets ), "\n\n";
196 push @lines, "Makefile \$(ALL_MAKEFILES): config.status\n";
197 push @lines, "\t\@./config.status \$\@\n\n";
198 push @lines, "\$(RECURSE_TARGETS) \$(MAKEDEP): \$(ALL_MAKEFILES)\n\n";
199 push @lines, "distclean::\n";
200 push @lines, "\t\$(RM) Makefile \$(ALL_MAKEFILES)\n\n";
201 push @lines, join ("\n", @depends ), "\n";
202
203 replace_in_file( "Makefile.in", '^ALL_MAKEFILES\s*=', undef, @lines );
204
205
206 ################################################################
207 # update dlls/Makefile.in
208
209 my @dll_makefiles = grep /^dlls\//, @makefiles;
210 system "dlls/make_dlls", @dll_makefiles;
211
212
213 ################################################################
214 # update programs/Makefile.in
215
216 my @prog_makefiles = grep /^programs\//, @makefiles;
217 system "programs/make_progs", @prog_makefiles;