make_makefiles: Merge the generated gitignores in dlls and programs into the top...
[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 # Default patterns for top-level .gitignore
71 my @ignores = (
72     "*.[oa]",
73     "*.avi",
74     "*.bmp",
75     "*.chm",
76     "*.cur",
77     "*.ico",
78     "*.mc.rc",
79     "*.res",
80     "*.so",
81     "*.tab.[ch]",
82     "*.tlb",
83     "*.yy.c",
84     "*_[cips].c",
85     "/autom4te.cache",
86     "/config.cache",
87     "/config.log",
88     "/config.status",
89     "/TAGS",
90     "/tags",
91     "Makefile"
92 );
93
94 my (@makefiles, %makefiles);
95
96 # update a file if changed
97 sub update_file($)
98 {
99     my $file = shift;
100     my $ret = system "cmp $file $file.new >/dev/null";
101     if (!$ret)
102     {
103         unlink "$file.new";
104     }
105     else
106     {
107         rename "$file.new", "$file";
108         print "$file updated\n";
109         if ($file eq "configure.ac")
110         {
111             system "autoconf";
112             print "configure updated\n";
113         }
114     }
115     return $ret;
116 }
117
118 # replace some lines in a file between two markers
119 sub replace_in_file($$$@)
120 {
121     my $file = shift;
122     my $start = shift;
123     my $end = shift;
124
125     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
126
127     if (defined($start))
128     {
129         open OLD_FILE, "$file" or die "cannot open $file";
130         while (<OLD_FILE>)
131         {
132             last if /$start/;
133             print NEW_FILE $_;
134         }
135     }
136
137     print NEW_FILE @_;
138
139     if (defined($end))
140     {
141         my $skip=1;
142         while (<OLD_FILE>)
143         {
144             print NEW_FILE $_ unless $skip;
145             $skip = 0 if /$end/;
146         }
147     }
148
149     close OLD_FILE if defined($start);
150     close NEW_FILE;
151     return update_file($file);
152 }
153
154 # parse the specified makefile to identify the rules file
155 sub parse_makefile($)
156 {
157     my $file = shift;
158     my %make;
159
160     ($make{"=dir"} = $file) =~ s/[^\/]+$//;
161
162     open MAKE, "$file.in" or die "cannot open $file.in\n";
163
164     while (<MAKE>)
165     {
166         chomp;
167         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
168
169         if (/^\@(MAKE.*RULES)\@/)
170         {
171             my $var = $1;
172             $make{"=rules"} = $makerules{$var};
173             next;
174         }
175         if (/^(MODULE|IMPORTLIB)\s*=\s*(.*)/)
176         {
177             $make{$1} = $2;
178             next;
179         }
180         if (/^(IDL_H_SRCS|IMPLIB_SRCS|SPEC_SRCS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
181         {
182             my @list = split(/\s+/, $2);
183             $make{$1} = \@list;
184             next;
185         }
186         if (/^\#\s*MKDLL_SKIP/ || /^\#\s*MKPROG_SKIP/)
187         {
188             $make{"=skip"} = 1;
189             next;
190         }
191     }
192     return %make;
193 }
194
195 if (-d ".git")
196 {
197     @makefiles = map { s/\.in$//; $_; } split /\s/, `git ls-files -c Makefile.in \\*/Makefile.in`;
198 }
199 else
200 {
201     @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
202 }
203
204 foreach my $file (sort values %makerules, @makefiles)
205 {
206     my %make = parse_makefile( $file );
207     $makefiles{$file} = \%make;
208 }
209
210 ################################################################
211 # update the makefile list in configure.ac
212
213 my @lines = ();
214
215 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
216 {
217     push @lines, "$var=$makerules{$var}\n";
218     push @lines, "AC_SUBST_FILE($var)\n\n";
219 }
220
221 replace_in_file( "configure.ac", '^MAKE_RULES', '\]\)$',
222                  @lines,
223                  "AC_CONFIG_FILES([\n",
224                  join ("\n", (sort values %makerules), (sort @makefiles) ), "])\n" );
225
226
227 ################################################################
228 # update the tests list in programs/winetest/Makefile.in and programs/winetest/winetest.rc
229
230 sub update_winetest(@)
231 {
232     my (@tests, @lines);
233
234     foreach my $file (@_)
235     {
236         if ($file =~ /^dlls\/(.*)\/tests\/Makefile/) { push @tests, $1; }
237     }
238     push @lines, "TESTBINS =";
239     push @lines, map { " \\\n\t" . $_ . "_test.exe"; } sort @tests;
240     push @lines, "\n\n";
241
242     foreach my $test (sort @tests)
243     {
244         push @lines, "${test}_test.exe: \$(DLLDIR)/$test/tests/${test}_test.exe\$(DLLEXT)\n";
245         push @lines, "\tcp \$(DLLDIR)/$test/tests/${test}_test.exe\$(DLLEXT) \$\@ && \$(STRIP) \$\@\n";
246     }
247     push @lines, "\n# Special rules\n";
248
249     replace_in_file( "programs/winetest/Makefile.in", '^TESTBINS\s*=', '^# Special rules', @lines );
250
251     replace_in_file( "programs/winetest/winetest.rc", ' TESTRES ', undef,
252                      map { $_ . "_test.exe TESTRES \"" . $_ . "_test.exe\"\n"; } sort @tests );
253
254     # return a list of test exe files for .gitignore
255     return map { "programs/winetest/" . $_ . "_test.exe"; } sort @tests;
256 }
257
258
259 ################################################################
260 # update the makefile list in Makefile.in
261
262 my @targets;
263 my @depends;
264
265 foreach my $file (sort values %makerules)
266 {
267     push @targets, $file;
268     my %make = %{$makefiles{$file}};
269     if (!defined($make{"=rules"})) { push @depends, "$file: $file.in"; }
270     else { push @depends, "$file: $file.in Make.rules"; }
271 }
272
273 foreach my $file (sort @makefiles)
274 {
275     push @targets, $file unless $file eq "Makefile";
276     my $dep = ${$makefiles{$file}}{"=rules"};
277     push @depends, "$file: $file.in $dep";
278 }
279
280 @lines = ();
281 push @lines, "ALL_MAKEFILES = \\\n\t";
282 push @lines, join (" \\\n\t", @targets ), "\n\n";
283 push @lines, "Makefile \$(ALL_MAKEFILES): config.status\n";
284 push @lines, "\t\@./config.status \$\@\n\n";
285 push @lines, "\$(RECURSE_TARGETS) \$(MAKEDEP): \$(ALL_MAKEFILES)\n\n";
286 push @lines, "distclean::\n";
287 push @lines, "\t\$(RM) Makefile \$(ALL_MAKEFILES)\n\n";
288 push @lines, join ("\n", @depends ), "\n";
289
290 replace_in_file( "Makefile.in", '^ALL_MAKEFILES\s*=', undef, @lines );
291
292
293 ################################################################
294 # update dlls/Makefile.in
295
296 sub update_dlls(@)
297 {
298     my (%directories, %testdirs, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
299     my $text = "";
300
301     my @ignores =
302     (
303      "dlls/*/tests/testlist.c",
304      "dlls/*/tests/*.ok",
305     );
306
307     sub needs_symlink($$)
308     {
309         my ($mod, $dir) = @_;
310         $mod =~ s/\.dll$//;
311         return $mod ne $dir;
312     }
313
314     foreach my $make (@_)
315     {
316         if ($make =~ /dlls\/(.*)\/tests\/Makefile/)
317         {
318             $testdirs{$1} = "$1/tests";
319             next;
320         }
321         my %makefile = %{$makefiles{$make}};
322
323         next unless defined $makefile{"MODULE"};
324         my $module = $makefile{"MODULE"};
325         (my $dir = $make) =~ s/^dlls\/(.*)\/[^\/]+$/$1/;
326
327         if ($module =~ /^lib.*\.a$/)
328         {
329             $staticlib_dirs{$module} = $dir;
330             die "invalid module $module in dir $staticlib_dirs{$module}\n" if "lib$staticlib_dirs{$module}.a" ne $module;
331         }
332         else
333         {
334             $directories{$module} = $dir;
335         }
336
337         if (defined $makefile{"IMPORTLIB"})
338         {
339             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)\.\$\(IMPLIBEXT\)/)
340             {
341                 $importlibs{$module} = $1;
342             }
343             else
344             {
345                 die "invalid importlib name $makefile{IMPORTLIB} in $make";
346             }
347         }
348
349         $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
350
351         if (defined $makefile{"SPEC_SRCS16"})
352         {
353             my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
354             $altnames{$module} = \@list;
355         }
356
357         if (defined $makefile{"IDL_H_SRCS"})
358         {
359             push @ignores, map { $_ =~ s/(.*)\.idl$/dlls\/$dir\/$1.h/; $_; } @{$makefile{"IDL_H_SRCS"}};
360         }
361     }
362
363     # output special dlls configure definitions
364
365     $text .= "# special configure-dependent targets\n\n";
366     my %specials = ();
367     foreach my $mod (sort keys %special_dlls)
368     {
369         $specials{$special_dlls{$mod}} .= " " . $mod;
370     }
371     foreach my $i (sort keys %specials)
372     {
373         $text .= $i . " =" . $specials{$i} . "\n";
374     }
375     $text .= "EXTRADIRS =";
376     foreach my $i (sort keys %specials) { $text .= sprintf " \@%s\@", $i; }
377     $text .= "\n\n";
378
379     # output the subdirs list
380
381     $text .= "# Subdir list\n\n";
382     $text .= "BASEDIRS =";
383     foreach my $dir (sort values %directories)
384     {
385         next if defined($special_dlls{$dir});  # skip special dlls
386         $text .= " \\\n\t" . $dir;
387     }
388
389     $text .= "\n\nIMPLIBSUBDIRS = \\\n\t";
390     $text .=  join " \\\n\t", sort values %staticlib_dirs;
391
392     $text .= "\n\nTESTSUBDIRS = \\\n\t";
393     $text .= join " \\\n\t", sort values %testdirs;
394
395     $text .=  "\n\nSUBDIRS = \\\n\t";
396     $text .= join " \\\n\t", "\$(BASEDIRS)", "\$(IMPLIBSUBDIRS)", "\$(TESTSUBDIRS)", sort keys %special_dlls;
397
398     $text .= "\n\nBUILDSUBDIRS   = \$(BASEDIRS) \$(EXTRADIRS) \$(TESTSUBDIRS)\n";
399     $text .= "INSTALLSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS) \$(IMPLIBSUBDIRS)\n";
400     $text .= "DOCSUBDIRS     = \$(BASEDIRS) \$(EXTRADIRS)\n";
401
402     # output the all: target
403
404     my %targets = ();  # use a hash to get rid of duplicate target names
405     my %targets16 = ();
406     foreach my $mod (sort keys %directories)
407     {
408         next if defined($special_dlls{$directories{$mod}});  # skip special dlls
409         $targets{$mod . ".so"} = 1 if needs_symlink($mod, $directories{$mod});
410         next unless defined $altnames{$mod};
411         foreach my $i (sort @{$altnames{$mod}})
412         {
413             $targets16{$i . "16"} = $mod;
414         }
415     }
416
417     $text .= "\n\@MAKE_RULES\@\n\n";
418     $text .= "# Symbolic links\n\n";
419     $text .= "WIN16_FILES = \\\n";
420     $text .=  "\t" . join( " \\\n\t", sort keys %targets16 ) . "\n\n";
421     $text .= "SYMLINKS_SO = \\\n";
422     $text .= "\t\@WIN16_FILES\@ \\\n";
423     $text .= "\t" . join( " \\\n\t", sort keys %targets ) . "\n\n";
424     $text .= "# Main target\n\n";
425     $text .= "all: \$(BUILDSUBDIRS) symlinks\$(DLLEXT)\n\n";
426     $text .= ".PHONY: symlinks symlinks.so implib\n\n";
427     $text .= "symlinks.so: \$(SYMLINKS_SO)\n\n";
428     $text .= "symlinks: \$(BUILDSUBDIRS)\n\n";
429
430     # output the lib name -> directory rules
431
432     $text .= "# Map symlink name to the corresponding library\n\n";
433     foreach my $mod (sort keys %directories)
434     {
435         next unless needs_symlink($mod, $directories{$mod});
436         $text .= sprintf "%s.so: %s/%s.so\n", $mod, $directories{$mod}, $mod;
437         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.so \$@\n\n", $directories{$mod}, $mod;
438     }
439
440     $text .= "# Placeholders for 16-bit libraries\n\n";
441     foreach my $mod (sort keys %directories)
442     {
443         next unless defined $altnames{$mod};
444         $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
445         $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
446     }
447
448     # output the import libraries rules
449
450     $text .= "# Import libraries\n\n";
451     $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
452
453     my @lib_symlinks = ();
454     foreach my $mod (sort keys %importlibs)
455     {
456         my $dir = $directories{$mod};
457         my $lib = $importlibs{$mod};
458         if ($lib ne "lib" . $dir) { push @lib_symlinks, $mod; }
459     }
460     $text .= "IMPORT_SYMLINKS =";
461     foreach my $mod (sort @lib_symlinks)
462     {
463         $text .= sprintf " \\\n\t%s.\$(IMPLIBEXT)", $importlibs{$mod};
464     }
465
466     $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
467     foreach my $mod (sort keys %staticlib_dirs)
468     {
469         $text .= sprintf " \\\n\t%s/%s", $staticlib_dirs{$mod}, $mod;
470     }
471     foreach my $mod (sort keys %importlibs)
472     {
473         my $dir = $directories{$mod};
474         my $def = $mod;
475         $def =~ s/\.(dll|drv)$//;
476         $text .= sprintf " \\\n\t%s/lib%s.\$(IMPLIBEXT)", $dir, $def;
477         next unless defined $static_implibs{$mod};
478         $text .= sprintf " \\\n\t%s/lib%s.\$(STATIC_IMPLIBEXT)", $dir, $def
479     }
480     $text .= "\n\n";
481     $text .= "implib: \$(IMPORT_LIBS)\n\n";
482
483     foreach my $mod (sort keys %importlibs)
484     {
485         my $dir = $directories{$mod};
486         my $lib = $importlibs{$mod};
487         my $spec = $mod;
488         $spec =~ s/\.dll$//;
489         $text .= sprintf "%s/%s.\$(IMPLIBEXT): %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
490         $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(IMPLIBEXT)\n\n", $dir, $lib;
491         next unless $static_implibs{$mod};
492         $text .= sprintf "%s/%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
493         $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
494     }
495     foreach my $mod (sort @lib_symlinks)
496     {
497         my $dir = $directories{$mod};
498         my $lib = $importlibs{$mod} . ".\$(IMPLIBEXT)";
499         $text .= sprintf "%s: %s/%s\n", $lib, $dir, $lib;
500         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s \$@\n\n", $dir, $lib;
501     }
502
503     $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
504     $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
505
506     # output the inter-dll dependencies and rules
507
508     $text .= "# Map library name to the corresponding directory\n\n";
509
510     foreach my $mod (sort keys %directories)
511     {
512         next unless needs_symlink($mod, $directories{$mod});
513         $text .= sprintf "%s/%s.so: %s\n", $directories{$mod}, $mod, $directories{$mod};
514     }
515     foreach my $mod (sort keys %staticlib_dirs)
516     {
517         $text .= sprintf "%s/%s: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
518     }
519     $text .= "\n# Misc rules\n";
520
521     replace_in_file( "dlls/Makefile.in",
522                      '^# special configure-dependent targets',
523                      '^# Misc rules',
524                      $text );
525
526     # .gitignore file
527
528     foreach my $mod (sort @lib_symlinks)
529     {
530         push @ignores, "dlls/$importlibs{$mod}.def";
531     }
532     foreach my $mod (sort keys %directories)
533     {
534         next unless defined $altnames{$mod};
535         push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
536     }
537     foreach my $mod (sort keys %importlibs)
538     {
539         my $dir = $directories{$mod};
540         my $def = $mod;
541         $def =~ s/\.(dll|drv)$//;
542         push @ignores, "dlls/$dir/lib$def.def";
543     }
544
545     return @ignores;
546 }
547
548
549 ################################################################
550 # update programs/Makefile.in
551
552 sub update_progs(@)
553 {
554     my (@subdirs, @install_subdirs, @install_progs);
555
556     my @ignores = ();
557
558     foreach my $make (@_)
559     {
560         my %makefile = %{$makefiles{$make}};
561         my $module = $makefile{"MODULE"};
562         (my $dir = $make) =~ s/^programs\/(.*)\/Makefile$/$1/;
563         die "Invalid module $module in $make" unless "$dir.exe" eq $module;
564         next if defined $makefile{"=skip"};
565         push @subdirs, $dir;
566         push @ignores, "programs/$dir/$dir";
567         push @install_subdirs, $dir unless $dont_install{$dir};
568         push @install_progs, $dir if $bin_install{$dir};
569     }
570
571     replace_in_file( "programs/Makefile.in", '^SUBDIRS\s*=', '^INSTALLDIRS',
572                      "SUBDIRS = \\\n\t",
573                      join( " \\\n\t", @subdirs ),
574                      "\n\n# Sub-directories to run make install into\nINSTALLSUBDIRS = \\\n\t",
575                      join( " \\\n\t", @install_subdirs ),
576                      "\n\n# Programs to install in bin directory\nINSTALLPROGS = \\\n\t",
577                      join( " \\\n\t", @install_progs ),
578                      "\n\nINSTALLDIRS = \$(DESTDIR)\$(bindir)\n" );
579
580     return @ignores;
581 }
582
583
584 ################################################################
585 # update the main .gitignore
586
587 sub update_gitignore(@)
588 {
589     my @ignores = values %makerules;
590
591     foreach my $make (@makefiles)
592     {
593         my %makefile = %{$makefiles{$make}};
594         my $dir = $makefile{"=dir"};
595         if (defined $makefile{"MANPAGES"})
596         {
597             push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
598         }
599         if (defined $makefile{"PROGRAMS"})
600         {
601             push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
602         }
603     }
604
605     # prepend a slash to paths that don't have one
606     @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
607
608     push @ignores, @_;
609
610     replace_in_file( ".gitignore", undef, undef,
611                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
612                      join("\n", sort @ignores), "\n" );
613 }
614
615
616 push @ignores, update_winetest( @makefiles );
617 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
618 push @ignores, update_progs( sort grep /^programs\/.*\/Makefile$/, @makefiles );
619 update_gitignore( @ignores );