rsaenh: Cast-qual warning fix.
[wine] / dlls / make_dlls
1 #!/usr/bin/perl -w
2 #
3 # Update the dll dependencies in the dlls main Makefile.in.
4 #
5 # Copyright 2001 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 use strict;
23
24 my %directories = ();
25 my %testdirs = ();
26 my %importlibs = ();
27 my %static_implibs = ();
28 my %staticlib_dirs = ();
29 my %altnames = ();
30
31 # list of special dlls that can be switched on or off by configure
32 my %special_dlls =
33 (
34   "glu32"    => "GLU32FILES",
35   "glut32"   => "GLUT32FILES",
36   "opengl32" => "OPENGLFILES",
37   "wined3d"  => "OPENGLFILES",
38   "winex11.drv" => "XFILES",
39   "winequartz.drv" => "QUARTZFILES"
40 );
41
42 sub needs_symlink($)
43 {
44     (my $mod = $_[0]) =~ s/\.dll$//;
45     return $mod ne $directories{$_[0]};
46 }
47
48 sub update_file($)
49 {
50     my $file = shift;
51     if (!system "cmp $file $file.new >/dev/null")
52     {
53         unlink "$file.new";
54         print "$file is unchanged\n";
55     }
56     else
57     {
58         rename "$file.new", "$file";
59         print "$file updated\n";
60         if (-x "./config.status" && $file =~ /(.*Makefile)\.in$/)
61         {
62             system "./config.status", $1;
63         }
64     }
65 }
66
67 # if we are inside the dlls dir, go up one level
68 if (! -f "configure.ac" && -f "../configure.ac") { chdir(".."); }
69
70 my @args = @ARGV;
71 if (!@args) { @args = map { s/^(.*)\.in/$1/; $_; } split(/\s/,`find dlls -name Makefile.in -print`); }
72
73 foreach my $i (@args)
74 {
75     if ($i =~ /dlls\/(.*)\/tests\/Makefile/)
76     {
77         $testdirs{$1} = "$1/tests";
78         next;
79     }
80
81     open MAKE, "$i.in" or die "cannot open $i.in\n";
82
83     my $module = undef;
84     my $dir = $i;
85
86     while (<MAKE>)
87     {
88         chop;
89         # EPP hack to disable this DLL... the MKDLL_SKIP comment must appear
90         # at the very top of the Makefile.in
91         last if (/^\#\s*MKDLL_SKIP/);
92
93         if (/^MODULE\s*=\s*([a-zA-Z0-9_.]+)/)
94         {
95             $module = $1;
96             if ($module =~ /^lib.*\.a$/)
97             {
98                 ($staticlib_dirs{$module} = $i) =~ s/^dlls\/(.*)\/[^\/]+$/$1/;
99                 die "invalid module $module in dir $staticlib_dirs{$module}\n" if "lib$staticlib_dirs{$module}.a" ne $module;
100             }
101             else
102             {
103                 ($directories{$module} = $i) =~ s/^dlls\/(.*)\/[^\/]+$/$1/;
104             }
105             next;
106         }
107         if (/^IMPORTLIB\s*=\s*([a-zA-Z0-9_.]+)\.\$\(IMPLIBEXT\)/)
108         {
109             $importlibs{$module} = $1;
110             next;
111         }
112         if (/^IMPLIB_SRCS\s*=/)
113         {
114             $static_implibs{$module} = 1;
115             next;
116         }
117         if (/^SPEC_SRCS16\s*=\s*(.*)/)
118         {
119             my $specs = $1;
120             while ($specs =~ /\s*(.*)\\$/) { $specs = $1 . <MAKE>; }
121             my @list = split(/\s+/,$specs);
122             @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @list;
123             $altnames{$module} = \@list;
124             next;
125         }
126     }
127     close MAKE;
128 }
129
130 open NEWMAKE,">dlls/Makefile.in.new" or die "cannot create dlls/Makefile.in.new";
131
132 ################################################################
133 # makefile header
134
135 print NEWMAKE <<EOF;
136 # Automatically generated by make_dlls; DO NOT EDIT!!
137
138 TOPSRCDIR = \@top_srcdir\@
139 TOPOBJDIR = ..
140 SRCDIR    = \@srcdir\@
141 VPATH     = \@srcdir\@
142
143 EOF
144
145 ################################################################
146 # output special dlls configure definitions
147
148 printf NEWMAKE "# special configure-dependent targets\n\n";
149 my %specials = ();
150 foreach my $mod (sort keys %special_dlls)
151 {
152     $specials{$special_dlls{$mod}} .= " " . $mod;
153 }
154 foreach my $i (sort keys %specials)
155 {
156     printf NEWMAKE "%s =%s\n", $i, $specials{$i};
157 }
158 printf NEWMAKE "EXTRADIRS =";
159 foreach my $i (sort keys %specials) { printf NEWMAKE " \@%s\@", $i; }
160 printf NEWMAKE "\n\n";
161
162
163 ################################################################
164 # output the subdirs list
165
166 print NEWMAKE "# Subdir list\n\nBASEDIRS =";
167 foreach my $dir (sort values %directories)
168 {
169     next if defined($special_dlls{$dir});  # skip special dlls
170     printf NEWMAKE " \\\n\t%s", $dir;
171 }
172
173 print NEWMAKE "\n\nIMPLIBSUBDIRS = \\\n\t";
174 print NEWMAKE join " \\\n\t", sort values %staticlib_dirs;
175
176 print NEWMAKE "\n\nTESTSUBDIRS = \\\n\t";
177 print NEWMAKE join " \\\n\t", sort values %testdirs;
178
179 print NEWMAKE "\n\nSUBDIRS = \\\n\t";
180 print NEWMAKE join " \\\n\t", "\$(BASEDIRS)", "\$(IMPLIBSUBDIRS)", "\$(TESTSUBDIRS)",
181                               sort keys %special_dlls;
182 print NEWMAKE <<EOF;
183
184
185 BUILDSUBDIRS   = \$(BASEDIRS) \$(EXTRADIRS) \$(TESTSUBDIRS)
186 INSTALLSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS) \$(IMPLIBSUBDIRS)
187 DOCSUBDIRS     = \$(BASEDIRS) \$(EXTRADIRS)
188 EOF
189
190 ################################################################
191 # output the all: target
192
193 my %targets = ();  # use a hash to get rid of duplicate target names
194 my %targets16 = ();
195 foreach my $mod (sort keys %directories)
196 {
197     next if defined($special_dlls{$directories{$mod}});  # skip special dlls
198     $targets{$mod . ".so"} = 1 if needs_symlink($mod);
199     next unless defined $altnames{$mod};
200     foreach my $i (sort @{$altnames{$mod}})
201     {
202         $targets16{$i . "16"} = $mod;
203     }
204 }
205
206 print NEWMAKE <<EOF;
207
208 \@MAKE_RULES\@
209
210 # Symbolic links
211
212 WIN16_FILES = \\
213 EOF
214 printf NEWMAKE "\t%s\n", join( " \\\n\t", sort keys %targets16 );
215
216 print NEWMAKE <<EOF;
217
218 SYMLINKS_SO = \\
219         \@WIN16_FILES\@ \\
220 EOF
221 printf NEWMAKE "\t%s\n", join( " \\\n\t", sort keys %targets );
222
223 print NEWMAKE <<EOF;
224
225 # Main target
226
227 all: \$(BUILDSUBDIRS) symlinks\$(DLLEXT)
228
229 .PHONY: symlinks symlinks.so implib
230
231 symlinks.so: \$(SYMLINKS_SO)
232
233 symlinks: \$(BUILDSUBDIRS)
234
235 EOF
236
237 ################################################################
238 # output the lib name -> directory rules
239
240 print NEWMAKE "# Map symlink name to the corresponding library\n\n";
241 foreach my $mod (sort keys %directories)
242 {
243     next unless needs_symlink($mod);
244     printf NEWMAKE "%s.so: %s/%s.so\n", $mod, $directories{$mod}, $mod;
245     printf NEWMAKE "\t\$(RM) \$@ && \$(LN_S) %s/%s.so \$@\n\n", $directories{$mod}, $mod;
246 }
247
248 print NEWMAKE "# Placeholders for 16-bit libraries\n\n";
249 foreach my $mod (sort keys %directories)
250 {
251     next unless defined $altnames{$mod};
252     printf NEWMAKE "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
253     printf NEWMAKE "\techo \"%s\" >\$\@\n\n", $mod;
254 }
255
256 ################################################################
257 # output the import libraries rules
258
259 print NEWMAKE "# Import libraries\n\n";
260 print NEWMAKE "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
261
262 my @lib_symlinks = ();
263 foreach my $mod (sort keys %importlibs)
264 {
265     my $dir = $directories{$mod};
266     my $lib = $importlibs{$mod};
267     if ($lib ne "lib" . $dir) { push @lib_symlinks, $mod; }
268 }
269 print NEWMAKE "IMPORT_SYMLINKS =";
270 foreach my $mod (sort @lib_symlinks)
271 {
272     printf NEWMAKE " \\\n\t%s.\$(IMPLIBEXT)", $importlibs{$mod};
273 }
274
275 print NEWMAKE "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
276 foreach my $mod (sort keys %staticlib_dirs)
277 {
278     printf NEWMAKE " \\\n\t%s/%s", $staticlib_dirs{$mod}, $mod;
279 }
280 foreach my $mod (sort keys %importlibs)
281 {
282     my $dir = $directories{$mod};
283     my $def = $mod;
284     $def =~ s/\.(dll|drv)$//;
285     printf NEWMAKE " \\\n\t%s/lib%s.\$(IMPLIBEXT)", $dir, $def;
286     printf NEWMAKE " \\\n\t%s/lib%s.\$(STATIC_IMPLIBEXT)", $dir, $def if $static_implibs{$mod};
287 }
288 print NEWMAKE "\n\n";
289 print NEWMAKE "implib: \$(IMPORT_LIBS)\n\n";
290
291 foreach my $mod (sort keys %importlibs)
292 {
293     my $dir = $directories{$mod};
294     my $lib = $importlibs{$mod};
295     my $spec = $mod;
296     $spec =~ s/\.dll$//;
297     printf NEWMAKE "%s/%s.\$(IMPLIBEXT): %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
298     printf NEWMAKE "\t\@cd %s && \$(MAKE) %s.\$(IMPLIBEXT)\n\n", $dir, $lib;
299     next unless $static_implibs{$mod};
300     printf NEWMAKE "%s/%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
301     printf NEWMAKE "\t\@cd %s && \$(MAKE) %s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
302 }
303 foreach my $mod (sort @lib_symlinks)
304 {
305     my $dir = $directories{$mod};
306     my $lib = $importlibs{$mod} . ".\$(IMPLIBEXT)";
307     printf NEWMAKE "%s: %s/%s\n", $lib, $dir, $lib;
308     printf NEWMAKE "\t\$(RM) \$@ && \$(LN_S) %s/%s \$@\n\n", $dir, $lib;
309 }
310
311 print NEWMAKE <<EOF;
312 \$(BUILDSUBDIRS): \$(IMPORT_LIBS)
313 \$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)
314
315 EOF
316
317 ################################################################
318 # output the inter-dll dependencies and rules
319
320 print NEWMAKE "# Map library name to the corresponding directory\n\n";
321
322 foreach my $mod (sort keys %directories)
323 {
324     next unless needs_symlink($mod);
325     printf NEWMAKE "%s/%s.so: %s\n", $directories{$mod}, $mod, $directories{$mod};
326 }
327 foreach my $mod (sort keys %staticlib_dirs)
328 {
329     printf NEWMAKE "%s/%s: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
330 }
331
332 ################################################################
333 # makefile trailer
334
335 print NEWMAKE <<EOF;
336
337 # Misc rules
338
339 install-lib:: \$(INSTALLSUBDIRS:%=%/__install-lib__)
340
341 install-dev:: \$(INSTALLSUBDIRS:%=%/__install-dev__)
342
343 uninstall::
344         -rmdir \$(DESTDIR)\$(dlldir)
345
346 clean::
347         \$(RM) \$(IMPORT_SYMLINKS) \$(WIN16_FILES)
348 EOF
349
350 close NEWMAKE;
351 update_file("dlls/Makefile.in");
352
353 ################################################################
354 # .gitignore file
355
356 open GITIGNORE, ">dlls/.gitignore.new" or die "cannot create dlls/.gitignore.new";
357 print GITIGNORE "# Automatically generated by make_dlls; DO NOT EDIT!!\n";
358
359 my @ignores =
360 (
361  "/Makedll.rules",
362  "/Makeimplib.rules",
363  "/Maketest.rules",
364  "*/tests/testlist.c",
365  "*/tests/*.ok",
366 );
367
368 foreach my $mod (sort @lib_symlinks)
369 {
370     push @ignores, "/$importlibs{$mod}.def";
371 }
372 foreach my $mod (sort keys %directories)
373 {
374     next unless defined $altnames{$mod};
375     push @ignores, map { "/" . $_ . "16"; } @{$altnames{$mod}};
376 }
377 foreach my $mod (sort keys %importlibs)
378 {
379     my $dir = $directories{$mod};
380     my $def = $mod;
381     $def =~ s/\.(dll|drv)$//;
382     push @ignores, "$dir/lib$def.def";
383 }
384
385 print GITIGNORE join("\n", sort @ignores) . "\n";
386
387 close GITIGNORE;
388 update_file("dlls/.gitignore");