make_makefiles: Automatically disable 16-bit modules if enable_win16 isn't set.
[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 use strict;
23
24 # Make rules files
25 my %makerules =
26 (
27  "MAKE_RULES" => "Make.rules",
28  "MAKE_DLL_RULES" => "dlls/Makedll.rules",
29  "MAKE_IMPLIB_RULES" => "dlls/Makeimplib.rules",
30  "MAKE_TEST_RULES" => "dlls/Maketest.rules",
31  "MAKE_PROG_RULES" => "programs/Makeprog.rules",
32 );
33
34 # Programs that we want to install in the bin directory too
35 my %bin_install =
36 (
37   "msiexec" => 1,
38   "notepad" => 1,
39   "regedit" => 1,
40   "regsvr32" => 1,
41   "wineboot" => 1,
42   "winecfg" => 1,
43   "wineconsole" => 1,
44   "winedbg" => 1,
45   "winefile" => 1,
46   "winemine" => 1,
47   "winepath" => 1,
48 );
49
50 # Programs that we don't want to install at all
51 my %dont_install =
52 (
53   "cmdlgtst" => 1,
54   "view" => 1,
55   "winetest" => 1,
56 );
57
58 # Default patterns for top-level .gitignore
59 my @ignores = (
60     "*.[oa]",
61     "*.ok",
62     "*.res",
63     "*.so",
64     "/autom4te.cache",
65     "/config.cache",
66     "/config.log",
67     "/config.status",
68     "/TAGS",
69     "/tags",
70     "Makefile",
71     "dlldata.c",
72     "dlls/*/*.def",
73     "dlls/*/tests/*crosstest.exe",
74     "dlls/*/tests/testlist.c",
75     "include/config.h",
76     "include/stamp-h",
77     "programs/winetest/tests.rc",
78     "programs/winetest/*_test.exe",
79 );
80
81 # Source files and their resulting target to ignore
82 my @ignore_srcs = (
83     [ 'BISON_SRCS',   '\.y',   '.tab.c' ],
84     [ 'BISON_SRCS',   '\.y',   '.tab.h' ],
85     [ 'LEX_SRCS',     '\.l',   '.yy.c' ],
86     [ 'MC_SRCS',      '\.mc',  '.mc.rc' ],
87     [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
88     [ 'IDL_H_SRCS',   '\.idl', '.h' ],
89     [ 'IDL_C_SRCS',   '\.idl', '.h' ],
90     [ 'IDL_I_SRCS',   '\.idl', '.h' ],
91     [ 'IDL_P_SRCS',   '\.idl', '.h' ],
92     [ 'IDL_S_SRCS',   '\.idl', '.h' ],
93     [ 'IDL_C_SRCS',   '\.idl', '_c.c' ],
94     [ 'IDL_I_SRCS',   '\.idl', '_i.c' ],
95     [ 'IDL_P_SRCS',   '\.idl', '_p.c' ],
96     [ 'IDL_S_SRCS',   '\.idl', '_s.c' ],
97 );
98
99 my %exported_wine_headers = (
100     "wine/debug.h" => 1,
101     "wine/exception.h" => 1,
102     "wine/library.h" => 1,
103     "wine/unicode.h" => 1,
104     "wine/itss.idl" => 1,
105     "wine/svcctl.idl" => 1,
106 );
107
108 my %private_idl_headers = (
109     "axcore.idl" => 1,
110     "axextend.idl" => 1,
111     "dbinit.idl" => 1,
112     "dbprop.idl" => 1,
113     "dbs.idl" => 1,
114     "devenum.idl" => 1,
115     "dyngraph.idl" => 1,
116     "vmrender.idl" => 1,
117     "wine/wined3d.idl" => 1,
118     "wine/winedxgi.idl" => 1,
119 );
120
121 my (@makefiles, %makefiles);
122
123 # update a file if changed
124 sub update_file($)
125 {
126     my $file = shift;
127     my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
128     if (!$ret)
129     {
130         unlink "$file.new";
131     }
132     else
133     {
134         rename "$file.new", "$file";
135         print "$file updated\n";
136         if ($file eq "configure.ac")
137         {
138             system "autoconf";
139             print "configure updated\n";
140         }
141     }
142     return $ret;
143 }
144
145 # replace some lines in a file between two markers
146 sub replace_in_file($$$@)
147 {
148     my $file = shift;
149     my $start = shift;
150     my $end = shift;
151
152     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
153
154     if (defined($start))
155     {
156         open OLD_FILE, "$file" or die "cannot open $file";
157         while (<OLD_FILE>)
158         {
159             last if /$start/;
160             print NEW_FILE $_;
161         }
162     }
163
164     print NEW_FILE @_;
165
166     if (defined($end))
167     {
168         my $skip=1;
169         while (<OLD_FILE>)
170         {
171             print NEW_FILE $_ unless $skip;
172             $skip = 0 if /$end/;
173         }
174     }
175
176     close OLD_FILE if defined($start);
177     close NEW_FILE;
178     return update_file($file);
179 }
180
181 # parse the specified makefile to identify the rules file
182 sub parse_makefile($)
183 {
184     my $file = shift;
185     my %make;
186
187     ($make{"=dir"} = $file) =~ s/[^\/]+$//;
188
189     open MAKE, "$file.in" or die "cannot open $file.in\n";
190
191     while (<MAKE>)
192     {
193         chomp;
194         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
195
196         if (/^\@(MAKE.*RULES)\@/)
197         {
198             my $var = $1;
199             $make{"=rules"} = $makerules{$var};
200             next;
201         }
202         if (/^(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
203         {
204             $make{$1} = $2;
205             next;
206         }
207         if (/^(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|MC_SRCS|RC_SRCS|RC_SRCS16|RC_BINARIES|SPEC_SRCS16|EXTRA_OBJS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
208         {
209             my @list = split(/\s+/, $2);
210             $make{$1} = \@list;
211             next;
212         }
213     }
214     return %make;
215 }
216
217
218 ################################################################
219 # update the makefile list in configure.ac
220
221 sub update_makefiles(@)
222 {
223     my (@lines);
224
225     foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
226     {
227         my $file = $makerules{$var};
228         my %make = %{$makefiles{$file}};
229         my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
230         push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
231     }
232     push @lines, "\n";
233
234     foreach my $file (sort @_)
235     {
236         my %make = %{$makefiles{$file}};
237         my $rules = $make{"=rules"};
238         my $args = "";
239         if ($rules eq $makerules{"MAKE_DLL_RULES"})
240         {
241             $args = ",[dlls],[ALL_DLL_DIRS]";
242             $args .= ",[enable_win16]" if $make{"MODULE"} =~ /(16|\.vxd)$/;
243         }
244         elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"}) { $args = ",[dlls],[ALL_IMPLIB_DIRS]"; }
245         elsif ($rules eq $makerules{"MAKE_TEST_RULES"}) { $args = ",[dlls],[ALL_TEST_DIRS],[enable_tests]"; }
246         elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
247         {
248             (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
249             $args = ",[programs],[ALL_PROGRAM_DIRS";
250             $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
251             $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
252             $args .= "]";
253             $args .= ",[enable_win16]" if $make{"MODULE"} =~ /16$/;
254         }
255         elsif ($file =~ /^[^\/]*\/Makefile$/) { $args = ",[],[ALL_TOP_DIRS]"; }
256         push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules]$args)\n";
257     }
258
259     push @lines, "\ndnl Build dependencies for test files compiled into winetest\n";
260     replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl Build dependencies for test files compiled into winetest$', @lines);
261 }
262
263
264 ################################################################
265 # process ignore targets for generic source files
266
267 sub update_ignores(@)
268 {
269     my @ignores;
270
271     foreach my $file (sort @_)
272     {
273         my %makefile = %{$makefiles{$file}};
274         my @list;
275
276         foreach my $src (@ignore_srcs)
277         {
278             my @pattern = @{$src};
279             next unless defined $makefile{$pattern[0]};
280             push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
281         }
282         foreach my $f (@list)
283         {
284             push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
285         }
286     }
287     return @ignores;
288 }
289
290 ################################################################
291 # update dlls/Makefile.in
292
293 sub update_dlls(@)
294 {
295     my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
296     my $text = "";
297     my @ignores = ();
298
299     foreach my $make (@_)
300     {
301         my %makefile = %{$makefiles{$make}};
302         next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
303
304         next unless defined $makefile{"MODULE"};
305         my $module = $makefile{"MODULE"};
306         (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
307
308         if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
309         {
310             $staticlib_dirs{$module} = $dir;
311             die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
312         }
313         else
314         {
315             die "invalid module $module" unless $module =~ /\./;
316             (my $mod = $module) =~ s/\.dll$//;
317             die "invalid directory $dir for module $module\n" unless $mod eq $dir;
318             $directories{$module} = $dir;
319         }
320
321         if (defined $makefile{"IMPORTLIB"})
322         {
323             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
324             {
325                 $importlibs{$module} = $1;
326             }
327             else
328             {
329                 die "invalid importlib name $makefile{IMPORTLIB} in $make";
330             }
331         }
332
333         $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
334
335         if (defined $makefile{"SPEC_SRCS16"})
336         {
337             my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
338             $altnames{$module} = \@list;
339         }
340         if (defined $makefile{"EXTRA_OBJS16"})
341         {
342             foreach my $obj (@{$makefile{"EXTRA_OBJS16"}})
343             {
344                 if ($obj =~ /^(.*\.(exe|mod))\.o/) { push @{$altnames{$module}}, $1; }
345             }
346         }
347     }
348
349     # output the list of 16-bit files
350
351     my @targets16 = ();
352     foreach my $mod (sort keys %directories)
353     {
354         next unless defined $altnames{$mod};
355         foreach my $i (sort @{$altnames{$mod}})
356         {
357             push @targets16, $i . "16";
358         }
359     }
360     $text .= "# 16-bit dlls\n\n";
361     $text .= "WIN16_FILES = \\\n";
362     $text .=  "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
363     $text .= "\@MAKE_RULES\@\n\n";
364
365     # output the all: target
366
367     $text .= "# Main target\n\n";
368     $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
369
370     # output the lib name -> directory rules
371
372     $text .= "# Placeholders for 16-bit libraries\n\n";
373     foreach my $mod (sort keys %directories)
374     {
375         next unless defined $altnames{$mod};
376         $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
377         $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
378     }
379
380     # output the import libraries rules
381
382     $text .= "# Import libraries\n\n";
383     $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
384
385     my @lib_symlinks = ();
386     foreach my $mod (sort keys %importlibs)
387     {
388         my $dir = $directories{$mod};
389         my $lib = $importlibs{$mod};
390         if ($lib ne $dir) { push @lib_symlinks, $mod; }
391     }
392     $text .= "IMPORT_SYMLINKS =";
393     foreach my $mod (sort @lib_symlinks)
394     {
395         $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
396     }
397
398     $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
399     foreach my $mod (sort keys %staticlib_dirs)
400     {
401         $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
402     }
403     foreach my $mod (sort keys %importlibs)
404     {
405         $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
406         next unless defined $static_implibs{$mod};
407         $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
408     }
409     $text .= "\n\nCROSS_IMPLIBS =";
410     foreach my $mod (sort @lib_symlinks)
411     {
412         $text .= sprintf " \\\n\tlib%s.a", $importlibs{$mod};
413     }
414     foreach my $mod (sort keys %importlibs)
415     {
416         next if defined $static_implibs{$mod};
417         $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.a";
418     }
419     $text .= "\n\n";
420     $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
421     $text .= "implib: \$(IMPORT_LIBS)\n\n";
422     $text .= "testsubdirs: \$(TESTSUBDIRS)\n\n";
423     $text .= ".PHONY: implib testsubdirs\n\n";
424
425     foreach my $mod (sort keys %importlibs)
426     {
427         my $dir = $directories{$mod};
428         my $lib = $importlibs{$mod};
429         my $spec = $mod;
430         $spec =~ s/\.dll$//;
431         if (defined($static_implibs{$mod}))
432         {
433             $text .= sprintf "%s/lib%s.def: %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
434             $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.def\n\n", $dir, $lib;
435             $text .= sprintf "%s/lib%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
436             $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
437         }
438         else
439         {
440             $text .= sprintf "%s/lib%s.def %s/lib%s.a: %s/%s.spec \$(WINEBUILD)\n",
441                              $dir, $lib, $dir, $lib, $dir, $spec;
442             $text .= sprintf "\t\@cd %s && \$(MAKE) `basename \$\@`\n\n", $dir;
443         }
444     }
445     foreach my $mod (sort @lib_symlinks)
446     {
447         my $dir = $directories{$mod};
448         my $lib = "lib" . $importlibs{$mod};
449         $text .= sprintf "%s.a: %s/%s.a\n", $lib, $dir, $lib;
450         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.a \$@\n\n", $dir, $lib;
451         $text .= sprintf "%s.def: %s/%s.def\n", $lib, $dir, $lib;
452         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.def \$@\n\n", $dir, $lib;
453     }
454
455     $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
456     $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
457
458     # output the inter-dll dependencies and rules
459
460     $text .= "# Map library name to the corresponding directory\n\n";
461
462     foreach my $mod (sort keys %staticlib_dirs)
463     {
464         $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
465     }
466     $text .= "\n# Misc rules\n";
467
468     replace_in_file( "dlls/Makefile.in",
469                      '^# 16-bit dlls',
470                      '^# Misc rules',
471                      $text );
472
473     # .gitignore file
474
475     foreach my $mod (sort @lib_symlinks)
476     {
477         push @ignores, "dlls/lib$importlibs{$mod}.def";
478     }
479     foreach my $mod (sort keys %directories)
480     {
481         next unless defined $altnames{$mod};
482         push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
483     }
484
485     return @ignores;
486 }
487
488
489 ################################################################
490 # update include/Makefile.in
491
492 sub update_includes()
493 {
494     return unless -d ".git";
495     my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
496     my @includes = map { s/^include\///; $_; } split /\0/, `git ls-files -c -z include`;
497     foreach my $incl (@includes)
498     {
499         if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
500         next if ($incl =~ /\.in$/);
501         if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
502         {
503             if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
504             next;
505         }
506         if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
507         elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
508         elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
509         elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
510         elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
511         elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
512         else { die "unknown file $incl in include dir"; }
513     }
514     replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
515                      "PRIVATE_IDL_H_SRCS = \\\n\t",
516                      join( " \\\n\t", sort @private_idl_srcs ),
517                      "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
518                      join( " \\\n\t", sort @public_idl_srcs ),
519                      "\n\nIDL_TLB_SRCS = \\\n\t",
520                      join( " \\\n\t", sort @tlb_srcs ),
521                      "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
522                      join( " \\\n\t", sort @h_srcs ),
523                      "\n\nEXTRASUBDIRS = ",
524                      join( " ", sort keys %subdirs ),
525                      "\n\nINSTALLDIRS = \\\n" );
526     return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
527 }
528
529
530 ################################################################
531 # update the main .gitignore
532
533 sub update_gitignore(@)
534 {
535     my @ignores = values %makerules;
536
537     foreach my $make (@makefiles)
538     {
539         my %makefile = %{$makefiles{$make}};
540         my $dir = $makefile{"=dir"};
541         if (defined $makefile{"MANPAGES"})
542         {
543             push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
544         }
545         if (defined $makefile{"PROGRAMS"})
546         {
547             push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
548         }
549     }
550
551     # prepend a slash to paths that don't have one
552     @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
553
554     # get rid of duplicates
555     my %ignores = ();
556     foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
557
558     replace_in_file( ".gitignore", undef, undef,
559                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
560                      join("\n", sort keys %ignores), "\n" );
561 }
562
563
564 if (-d ".git")
565 {
566     @makefiles = map { s/\.in$//; $_; } split /\0/, `git ls-files -c -z Makefile.in \\*/Makefile.in`;
567 }
568 else
569 {
570     @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
571 }
572
573 foreach my $file (sort values %makerules, @makefiles)
574 {
575     my %make = parse_makefile( $file );
576     $makefiles{$file} = \%make;
577 }
578
579 update_makefiles( @makefiles );
580 push @ignores, update_includes();
581 push @ignores, update_ignores( @makefiles );
582 push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
583 update_gitignore( @ignores );