dlls: Merged the make_dlls script into the global make_makefiles.
[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 # Make rules files
23 my %makerules =
24 (
25  "MAKE_RULES" => "Make.rules",
26  "MAKE_DLL_RULES" => "dlls/Makedll.rules",
27  "MAKE_IMPLIB_RULES" => "dlls/Makeimplib.rules",
28  "MAKE_TEST_RULES" => "dlls/Maketest.rules",
29  "MAKE_PROG_RULES" => "programs/Makeprog.rules",
30 );
31
32 # Programs that we want to install in the bin directory too
33 my %bin_install =
34 (
35   "msiexec" => 1,
36   "notepad" => 1,
37   "progman" => 1,
38   "regedit" => 1,
39   "regsvr32" => 1,
40   "uninstaller" => 1,
41   "wineboot" => 1,
42   "winebrowser" => 1,
43   "winecfg" => 1,
44   "wineconsole" => 1,
45   "winedbg" => 1,
46   "winefile" => 1,
47   "winemine" => 1,
48   "winepath" => 1,
49   "winhelp" => 1,
50 );
51
52 # Programs that we don't want to install at all
53 my %dont_install =
54 (
55   "cmdlgtst" => 1,
56   "view" => 1,
57   "winetest" => 1,
58 );
59
60 # Special dlls that can be switched on or off by configure
61 my %special_dlls =
62 (
63   "glu32"    => "GLU32FILES",
64   "opengl32" => "OPENGLFILES",
65   "wined3d"  => "OPENGLFILES",
66   "winex11.drv" => "XFILES",
67   "winequartz.drv" => "QUARTZFILES"
68 );
69
70 my (@makefiles, %makefiles);
71
72 # update a file if changed
73 sub update_file($)
74 {
75     my $file = shift;
76     my $ret = system "cmp $file $file.new >/dev/null";
77     if (!$ret)
78     {
79         unlink "$file.new";
80         #print "$file is unchanged\n";
81     }
82     else
83     {
84         rename "$file.new", "$file";
85         print "$file updated\n";
86     }
87     return $ret;
88 }
89
90 # replace some lines in a file between two markers
91 sub replace_in_file($$$@)
92 {
93     my $file = shift;
94     my $start = shift;
95     my $end = shift;
96
97     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
98
99     if (defined($start))
100     {
101         open OLD_FILE, "$file" or die "cannot open $file";
102         while (<OLD_FILE>)
103         {
104             last if /$start/;
105             print NEW_FILE $_;
106         }
107     }
108
109     print NEW_FILE @_;
110
111     if (defined($end))
112     {
113         my $skip=1;
114         while (<OLD_FILE>)
115         {
116             print NEW_FILE $_ unless $skip;
117             $skip = 0 if /$end/;
118         }
119     }
120
121     close OLD_FILE if defined($start);
122     close NEW_FILE;
123     return update_file($file);
124 }
125
126 # parse the specified makefile to identify the rules file
127 sub parse_makefile($)
128 {
129     my $file = shift;
130     my %make;
131
132     open MAKE, "$file.in" or die "cannot open $file.in\n";
133
134     while (<MAKE>)
135     {
136         chomp;
137         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
138
139         if (/^\@(MAKE.*RULES)\@/)
140         {
141             my $var = $1;
142             $make{"=rules"} = $makerules{$var};
143             next;
144         }
145         if (/^(MODULE|IMPORTLIB|IMPLIB_SRCS|SPEC_SRCS16)\s*=\s*(.*)/)
146         {
147             $make{$1} = $2;
148             next;
149         }
150         if (/^\#\s*MKDLL_SKIP/ || /^\#\s*MKPROG_SKIP/)
151         {
152             $make{"=skip"} = 1;
153             next;
154         }
155     }
156     return %make;
157 }
158
159 if (-d ".git")
160 {
161     @makefiles = map { s/\.in$//; $_; } split /\s/, `git ls-files -c Makefile.in \\*/Makefile.in`;
162 }
163 else
164 {
165     @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
166 }
167
168 foreach my $file (sort values %makerules, @makefiles)
169 {
170     my %make = parse_makefile( $file );
171     $makefiles{$file} = \%make;
172 }
173
174 ################################################################
175 # update the makefile list in configure.ac
176
177 my @lines = ();
178
179 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
180 {
181     push @lines, "$var=$makerules{$var}\n";
182     push @lines, "AC_SUBST_FILE($var)\n\n";
183 }
184
185 replace_in_file( "configure.ac", '^MAKE_RULES', '\]\)$',
186                  @lines,
187                  "AC_CONFIG_FILES([\n",
188                  join ("\n", (sort values %makerules), (sort @makefiles) ), "])\n" );
189
190
191 ################################################################
192 # update the tests list in programs/winetest/Makefile.in and programs/winetest/winetest.rc
193
194 my %modules = ( "user" => "user32" );
195 my %tests;
196 @lines = ( "TESTBINS =" );
197
198 foreach my $file (sort grep /^dlls\/.*\/tests\/Makefile/, @makefiles)
199 {
200     if ($file =~ /^dlls\/(.*)\/tests\/Makefile/)
201     {
202         my $dir = $1;
203         my $mod = $modules{$dir} || $dir;
204         $tests{$mod} = $dir;
205         push @lines, " \\\n\t${mod}_test.exe";
206     }
207 }
208 push @lines, "\n\n";
209
210 foreach my $test (sort keys %tests)
211 {
212     my $dir = $tests{$test};
213     push @lines, "${test}_test.exe: \$(DLLDIR)/$dir/tests/${test}_test.exe\$(DLLEXT)\n";
214     push @lines, "\tcp \$(DLLDIR)/$dir/tests/${test}_test.exe\$(DLLEXT) \$\@ && \$(STRIP) \$\@\n";
215 }
216 push @lines, "\n# Special rules\n";
217
218 replace_in_file( "programs/winetest/Makefile.in", '^TESTBINS\s*=', '^# Special rules', @lines );
219
220 @lines = ();
221 foreach my $test (sort keys %tests)
222 {
223     push @lines, "${test}_test.exe TESTRES \"${test}_test.exe\"\n";
224 }
225
226 replace_in_file( "programs/winetest/winetest.rc", ' TESTRES ', undef, @lines );
227
228 ################################################################
229 # update the makefile list in Makefile.in
230
231 my @targets;
232 my @depends;
233
234 foreach my $file (sort values %makerules)
235 {
236     push @targets, $file;
237     my %make = %{$makefiles{$file}};
238     if (!defined($make{"=rules"})) { push @depends, "$file: $file.in"; }
239     else { push @depends, "$file: $file.in Make.rules"; }
240 }
241
242 foreach my $file (sort @makefiles)
243 {
244     push @targets, $file unless $file eq "Makefile";
245     my $dep = ${$makefiles{$file}}{"=rules"};
246     push @depends, "$file: $file.in $dep";
247 }
248
249 @lines = ();
250 push @lines, "ALL_MAKEFILES = \\\n\t";
251 push @lines, join (" \\\n\t", @targets ), "\n\n";
252 push @lines, "Makefile \$(ALL_MAKEFILES): config.status\n";
253 push @lines, "\t\@./config.status \$\@\n\n";
254 push @lines, "\$(RECURSE_TARGETS) \$(MAKEDEP): \$(ALL_MAKEFILES)\n\n";
255 push @lines, "distclean::\n";
256 push @lines, "\t\$(RM) Makefile \$(ALL_MAKEFILES)\n\n";
257 push @lines, join ("\n", @depends ), "\n";
258
259 replace_in_file( "Makefile.in", '^ALL_MAKEFILES\s*=', undef, @lines );
260
261
262 ################################################################
263 # update dlls/Makefile.in
264
265 sub update_dlls(@)
266 {
267     my (%directories, %testdirs, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
268     my $text = "";
269
270     my @ignores =
271     (
272      "/Makedll.rules",
273      "/Makeimplib.rules",
274      "/Maketest.rules",
275      "*/tests/testlist.c",
276      "*/tests/*.ok",
277     );
278
279     sub needs_symlink($$)
280     {
281         my ($mod, $dir) = @_;
282         $mod =~ s/\.dll$//;
283         return $mod ne $dir;
284     }
285
286     foreach my $make (@_)
287     {
288         if ($make =~ /dlls\/(.*)\/tests\/Makefile/)
289         {
290             $testdirs{$1} = "$1/tests";
291             next;
292         }
293         my %makefile = %{$makefiles{$make}};
294
295         next unless defined $makefile{"MODULE"};
296         my $module = $makefile{"MODULE"};
297
298         if ($module =~ /^lib.*\.a$/)
299         {
300             ($staticlib_dirs{$module} = $make) =~ s/^dlls\/(.*)\/[^\/]+$/$1/;
301             die "invalid module $module in dir $staticlib_dirs{$module}\n" if "lib$staticlib_dirs{$module}.a" ne $module;
302         }
303         else
304         {
305             ($directories{$module} = $make) =~ s/^dlls\/(.*)\/[^\/]+$/$1/;
306         }
307
308         if (defined $makefile{"IMPORTLIB"})
309         {
310             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)\.\$\(IMPLIBEXT\)/)
311             {
312                 $importlibs{$module} = $1;
313             }
314             else
315             {
316                 die "invalid importlib name $makefile{IMPORTLIB} in $make";
317             }
318         }
319
320         $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
321
322         if (defined $makefile{"SPEC_SRCS16"})
323         {
324             my @list = split(/\s+/, $makefile{"SPEC_SRCS16"});
325             @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @list;
326             $altnames{$module} = \@list;
327         }
328     }
329
330     # output special dlls configure definitions
331
332     $text .= "# special configure-dependent targets\n\n";
333     my %specials = ();
334     foreach my $mod (sort keys %special_dlls)
335     {
336         $specials{$special_dlls{$mod}} .= " " . $mod;
337     }
338     foreach my $i (sort keys %specials)
339     {
340         $text .= $i . " =" . $specials{$i} . "\n";
341     }
342     $text .= "EXTRADIRS =";
343     foreach my $i (sort keys %specials) { $text .= sprintf " \@%s\@", $i; }
344     $text .= "\n\n";
345
346     # output the subdirs list
347
348     $text .= "# Subdir list\n\n";
349     $text .= "BASEDIRS =";
350     foreach my $dir (sort values %directories)
351     {
352         next if defined($special_dlls{$dir});  # skip special dlls
353         $text .= " \\\n\t" . $dir;
354     }
355
356     $text .= "\n\nIMPLIBSUBDIRS = \\\n\t";
357     $text .=  join " \\\n\t", sort values %staticlib_dirs;
358
359     $text .= "\n\nTESTSUBDIRS = \\\n\t";
360     $text .= join " \\\n\t", sort values %testdirs;
361
362     $text .=  "\n\nSUBDIRS = \\\n\t";
363     $text .= join " \\\n\t", "\$(BASEDIRS)", "\$(IMPLIBSUBDIRS)", "\$(TESTSUBDIRS)", sort keys %special_dlls;
364
365     $text .= "\n\nBUILDSUBDIRS   = \$(BASEDIRS) \$(EXTRADIRS) \$(TESTSUBDIRS)\n";
366     $text .= "INSTALLSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS) \$(IMPLIBSUBDIRS)\n";
367     $text .= "DOCSUBDIRS     = \$(BASEDIRS) \$(EXTRADIRS)\n";
368
369     # output the all: target
370
371     my %targets = ();  # use a hash to get rid of duplicate target names
372     my %targets16 = ();
373     foreach my $mod (sort keys %directories)
374     {
375         next if defined($special_dlls{$directories{$mod}});  # skip special dlls
376         $targets{$mod . ".so"} = 1 if needs_symlink($mod, $directories{$mod});
377         next unless defined $altnames{$mod};
378         foreach my $i (sort @{$altnames{$mod}})
379         {
380             $targets16{$i . "16"} = $mod;
381         }
382     }
383
384     $text .= "\n\@MAKE_RULES\@\n\n";
385     $text .= "# Symbolic links\n\n";
386     $text .= "WIN16_FILES = \\\n";
387     $text .=  "\t" . join( " \\\n\t", sort keys %targets16 ) . "\n\n";
388     $text .= "SYMLINKS_SO = \\\n";
389     $text .= "\t\@WIN16_FILES\@ \\\n";
390     $text .= "\t" . join( " \\\n\t", sort keys %targets ) . "\n\n";
391     $text .= "# Main target\n\n";
392     $text .= "all: \$(BUILDSUBDIRS) symlinks\$(DLLEXT)\n\n";
393     $text .= ".PHONY: symlinks symlinks.so implib\n\n";
394     $text .= "symlinks.so: \$(SYMLINKS_SO)\n\n";
395     $text .= "symlinks: \$(BUILDSUBDIRS)\n\n";
396
397     # output the lib name -> directory rules
398
399     $text .= "# Map symlink name to the corresponding library\n\n";
400     foreach my $mod (sort keys %directories)
401     {
402         next unless needs_symlink($mod, $directories{$mod});
403         $text .= sprintf "%s.so: %s/%s.so\n", $mod, $directories{$mod}, $mod;
404         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.so \$@\n\n", $directories{$mod}, $mod;
405     }
406
407     $text .= "# Placeholders for 16-bit libraries\n\n";
408     foreach my $mod (sort keys %directories)
409     {
410         next unless defined $altnames{$mod};
411         $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
412         $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
413     }
414
415     # output the import libraries rules
416
417     $text .= "# Import libraries\n\n";
418     $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
419
420     my @lib_symlinks = ();
421     foreach my $mod (sort keys %importlibs)
422     {
423         my $dir = $directories{$mod};
424         my $lib = $importlibs{$mod};
425         if ($lib ne "lib" . $dir) { push @lib_symlinks, $mod; }
426     }
427     $text .= "IMPORT_SYMLINKS =";
428     foreach my $mod (sort @lib_symlinks)
429     {
430         $text .= sprintf " \\\n\t%s.\$(IMPLIBEXT)", $importlibs{$mod};
431     }
432
433     $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
434     foreach my $mod (sort keys %staticlib_dirs)
435     {
436         $text .= sprintf " \\\n\t%s/%s", $staticlib_dirs{$mod}, $mod;
437     }
438     foreach my $mod (sort keys %importlibs)
439     {
440         my $dir = $directories{$mod};
441         my $def = $mod;
442         $def =~ s/\.(dll|drv)$//;
443         $text .= sprintf " \\\n\t%s/lib%s.\$(IMPLIBEXT)", $dir, $def;
444         next unless defined $static_implibs{$mod};
445         $text .= sprintf " \\\n\t%s/lib%s.\$(STATIC_IMPLIBEXT)", $dir, $def
446     }
447     $text .= "\n\n";
448     $text .= "implib: \$(IMPORT_LIBS)\n\n";
449
450     foreach my $mod (sort keys %importlibs)
451     {
452         my $dir = $directories{$mod};
453         my $lib = $importlibs{$mod};
454         my $spec = $mod;
455         $spec =~ s/\.dll$//;
456         $text .= sprintf "%s/%s.\$(IMPLIBEXT): %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
457         $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(IMPLIBEXT)\n\n", $dir, $lib;
458         next unless $static_implibs{$mod};
459         $text .= sprintf "%s/%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
460         $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
461     }
462     foreach my $mod (sort @lib_symlinks)
463     {
464         my $dir = $directories{$mod};
465         my $lib = $importlibs{$mod} . ".\$(IMPLIBEXT)";
466         $text .= sprintf "%s: %s/%s\n", $lib, $dir, $lib;
467         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s \$@\n\n", $dir, $lib;
468     }
469
470     $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
471     $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
472
473     # output the inter-dll dependencies and rules
474
475     $text .= "# Map library name to the corresponding directory\n\n";
476
477     foreach my $mod (sort keys %directories)
478     {
479         next unless needs_symlink($mod, $directories{$mod});
480         $text .= sprintf "%s/%s.so: %s\n", $directories{$mod}, $mod, $directories{$mod};
481     }
482     foreach my $mod (sort keys %staticlib_dirs)
483     {
484         $text .= sprintf "%s/%s: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
485     }
486     $text .= "\n# Misc rules\n";
487
488     replace_in_file( "dlls/Makefile.in",
489                      '^# special configure-dependent targets',
490                      '^# Misc rules',
491                      $text );
492
493     # .gitignore file
494
495     foreach my $mod (sort @lib_symlinks)
496     {
497         push @ignores, "/$importlibs{$mod}.def";
498     }
499     foreach my $mod (sort keys %directories)
500     {
501         next unless defined $altnames{$mod};
502         push @ignores, map { "/" . $_ . "16"; } @{$altnames{$mod}};
503     }
504     foreach my $mod (sort keys %importlibs)
505     {
506         my $dir = $directories{$mod};
507         my $def = $mod;
508         $def =~ s/\.(dll|drv)$//;
509         push @ignores, "$dir/lib$def.def";
510     }
511
512     replace_in_file( "dlls/.gitignore", undef, undef,
513                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
514                      join("\n", sort @ignores), "\n" );
515 }
516
517
518
519 ################################################################
520 # update programs/Makefile.in and programs/.gitignore
521
522 sub update_progs(@)
523 {
524     my (@subdirs, @install_subdirs, @install_progs);
525
526     my @ignores =
527     (
528      "/Makeprog.rules",
529      "/wineapploader",
530      "/winelauncher",
531     );
532
533     foreach my $make (@_)
534     {
535         my %makefile = %{$makefiles{$make}};
536         my $module = $makefile{"MODULE"};
537         (my $dir = $make) =~ s/^programs\/(.*)\/Makefile$/$1/;
538         die "Invalid module $module in $make" unless "$dir.exe" eq $module;
539         next if defined $makefile{"=skip"};
540         push @subdirs, $dir;
541         push @ignores, "$dir/$dir";
542         push @install_subdirs, $dir unless $dont_install{$dir};
543         push @install_progs, $dir if $bin_install{$dir};
544     }
545
546     replace_in_file( "programs/Makefile.in", '^SUBDIRS\s*=', '^INSTALLDIRS',
547                      "SUBDIRS = \\\n\t",
548                      join( " \\\n\t", @subdirs ),
549                      "\n\n# Sub-directories to run make install into\nINSTALLSUBDIRS = \\\n\t",
550                      join( " \\\n\t", @install_subdirs ),
551                      "\n\n# Programs to install in bin directory\nINSTALLPROGS = \\\n\t",
552                      join( " \\\n\t", @install_progs ),
553                      "\n\nINSTALLDIRS = \$(DESTDIR)\$(bindir)\n" );
554
555     replace_in_file( "programs/.gitignore", undef, undef,
556                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
557                      join("\n", sort @ignores), "\n" );
558 }
559
560 update_dlls( sort grep /^dlls\//, @makefiles );
561 update_progs( sort grep /^programs\/.*\/Makefile$/, @makefiles );