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