makefiles: Create the wine symlink from configure.
[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 # Dlls and programs that are 16-bit specific
59 my %modules16 =
60 (
61   "ifsmgr.vxd" => 1,
62   "mmdevldr.vxd" => 1,
63   "monodebg.vxd" => 1,
64   "vdhcp.vxd" => 1,
65   "vmm.vxd" => 1,
66   "vnbt.vxd" => 1,
67   "vnetbios.vxd" => 1,
68   "vtdapi.vxd" => 1,
69   "vwin32.vxd" => 1,
70   "w32skrnl.dll" => 1,
71   "winevdm.exe" => 1,
72   "wow32.dll" => 1,
73 );
74
75 # Default patterns for top-level .gitignore
76 my @ignores = (
77     "*.[oa]",
78     "*.fake",
79     "*.ok",
80     "*.res",
81     "*.so",
82     "/autom4te.cache",
83     "/config.cache",
84     "/config.log",
85     "/config.status",
86     "/configure.lineno",
87     "/TAGS",
88     "/tags",
89     "/wine",
90     "Makefile",
91     "dlldata.c",
92     "dlls/*/*.def",
93     "dlls/*/tests/*crosstest.exe",
94     "dlls/*/tests/testlist.c",
95     "include/config.h",
96     "include/stamp-h",
97     "programs/winetest/*_test.exe",
98     "programs/winetest/*_test.rc",
99     "programs/winetest/build.rc",
100 );
101
102 # Source files and their resulting target to ignore
103 my @ignore_srcs = (
104     [ 'BISON_SRCS',   '\.y',   '.tab.c' ],
105     [ 'BISON_SRCS',   '\.y',   '.tab.h' ],
106     [ 'LEX_SRCS',     '\.l',   '.yy.c' ],
107     [ 'MC_SRCS',      '\.mc',  '.mc.rc' ],
108     [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
109     [ 'IDL_H_SRCS',   '\.idl', '.h' ],
110     [ 'IDL_C_SRCS',   '\.idl', '.h' ],
111     [ 'IDL_I_SRCS',   '\.idl', '.h' ],
112     [ 'IDL_P_SRCS',   '\.idl', '.h' ],
113     [ 'IDL_S_SRCS',   '\.idl', '.h' ],
114     [ 'IDL_C_SRCS',   '\.idl', '_c.c' ],
115     [ 'IDL_I_SRCS',   '\.idl', '_i.c' ],
116     [ 'IDL_P_SRCS',   '\.idl', '_p.c' ],
117     [ 'IDL_S_SRCS',   '\.idl', '_s.c' ],
118 );
119
120 my %exported_wine_headers = (
121     "wine/debug.h" => 1,
122     "wine/exception.h" => 1,
123     "wine/library.h" => 1,
124     "wine/unicode.h" => 1,
125     "wine/itss.idl" => 1,
126     "wine/svcctl.idl" => 1,
127 );
128
129 my %private_idl_headers = (
130     "access.idl" => 1,
131     "asynot.idl" => 1,
132     "asysta.idl" => 1,
133     "axcore.idl" => 1,
134     "axextend.idl" => 1,
135     "binres.idl" => 1,
136     "cmdbas.idl" => 1,
137     "cmdtxt.idl" => 1,
138     "crtrow.idl" => 1,
139     "dbccmd.idl" => 1,
140     "dbcses.idl" => 1,
141     "dbdsad.idl" => 1,
142     "dbinit.idl" => 1,
143     "dbprop.idl" => 1,
144     "dbs.idl" => 1,
145     "devenum.idl" => 1,
146     "dyngraph.idl" => 1,
147     "opnrst.idl" => 1,
148     "row.idl" => 1,
149     "rowchg.idl" => 1,
150     "rstbas.idl" => 1,
151     "rstinf.idl" => 1,
152     "rstloc.idl" => 1,
153     "sesprp.idl" => 1,
154     "vmrender.idl" => 1,
155     "wine/wined3d.idl" => 1,
156     "wine/winedxgi.idl" => 1,
157 );
158
159 my %ignored_source_files = (
160     "dlls/wineps.drv/afm2c.c" => 1,
161     "dlls/wineps.drv/mkagl.c" => 1,
162     "programs/winetest/dist.rc" => 1,
163 );
164
165 my (@all_files, @makefiles, %makefiles);
166
167 sub dirname($)
168 {
169     my $ret = shift;
170     return "" unless $ret =~ /\//;
171     $ret =~ s!/[^/]*$!!;
172     return $ret;
173 }
174
175 # update a file if changed
176 sub update_file($)
177 {
178     my $file = shift;
179     my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
180     if (!$ret)
181     {
182         unlink "$file.new";
183     }
184     else
185     {
186         rename "$file.new", "$file";
187         print "$file updated\n";
188         if ($file eq "configure.ac")
189         {
190             system "autoconf";
191             print "configure updated\n";
192         }
193     }
194     return $ret;
195 }
196
197 # replace some lines in a file between two markers
198 sub replace_in_file($$$@)
199 {
200     my $file = shift;
201     my $start = shift;
202     my $end = shift;
203
204     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
205
206     if (defined($start))
207     {
208         open OLD_FILE, "$file" or die "cannot open $file";
209         while (<OLD_FILE>)
210         {
211             last if /$start/;
212             print NEW_FILE $_;
213         }
214     }
215
216     print NEW_FILE @_;
217
218     if (defined($end))
219     {
220         my $skip=1;
221         while (<OLD_FILE>)
222         {
223             print NEW_FILE $_ unless $skip;
224             $skip = 0 if /$end/;
225         }
226     }
227
228     close OLD_FILE if defined($start);
229     close NEW_FILE;
230     return update_file($file);
231 }
232
233 # replace a variable in a makefile
234 sub replace_makefile_variable($$)
235 {
236     my ($file, $var) = @_;
237     my $make = $makefiles{$file};
238
239     return unless defined ${$make}{"=$var"};
240
241     my @values = @{${$make}{"=$var"}};
242     ${$make}{$var} = \@values;
243
244     open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";
245
246     open OLD_FILE, "$file.in" or die "cannot open $file.in";
247     while (<OLD_FILE>)
248     {
249         if (/^\s*($var\s+)=/)
250         {
251             # try to preserve formatting
252             my $prefix = $1;
253             my $multiline = /\\$/ || (@values > 1);
254             while (/\\$/)
255             {
256                 $_ = <OLD_FILE>;
257                 last unless $_;
258             }
259             if ($multiline)
260             {
261                 print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
262             }
263             else
264             {
265                 print NEW_FILE "$prefix= @values\n";
266             }
267             next;
268         }
269         print NEW_FILE $_;
270     }
271     close OLD_FILE;
272     close NEW_FILE;
273     return update_file("$file.in");
274 }
275
276 # parse the specified makefile to identify the rules file
277 sub parse_makefile($)
278 {
279     my $file = shift;
280     my %make;
281
282     ($make{"=dir"} = $file) =~ s/[^\/]+$//;
283
284     open MAKE, "$file.in" or die "cannot open $file.in\n";
285
286     while (<MAKE>)
287     {
288         chomp;
289         next if (/^\s*#/);
290         while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
291         next if (/^\s*$/);
292
293         if (/^\@(MAKE.*RULES)\@/)
294         {
295             my $var = $1;
296             $make{"=rules"} = $makerules{$var};
297             next;
298         }
299         if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
300         {
301             $make{$1} = $2;
302             next;
303         }
304         if (/^\s*(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|C_SRCS|MC_SRCS|RC_SRCS|SVG_SRCS|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
305         {
306             my @list = split(/\s+/, $2);
307             $make{$1} = \@list;
308             next;
309         }
310     }
311     return %make;
312 }
313
314 # assign source files to their respective makefile
315 sub assign_sources_to_makefiles()
316 {
317     foreach my $file (@all_files)
318     {
319         next if defined $ignored_source_files{$file};
320         my $dir = dirname( $file );
321
322         while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
323         next unless $dir;
324
325         die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
326
327         my $make = $makefiles{"$dir/Makefile"};
328         my $basename = substr( $file, length($dir) + 1 );
329
330         if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
331         elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
332         elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
333         elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
334         elsif ($basename =~ /\.mc$/) { push @{${$make}{"=MC_SRCS"}}, $basename; }
335         elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
336     }
337 }
338
339 ################################################################
340 # update the makefile list in configure.ac
341
342 sub update_makefiles(@)
343 {
344     my (@lines);
345
346     foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
347     {
348         my $file = $makerules{$var};
349         my %make = %{$makefiles{$file}};
350         my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
351         push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
352     }
353     push @lines, "\n";
354
355     foreach my $file (sort @_)
356     {
357         my %make = %{$makefiles{$file}};
358         my $rules = $make{"=rules"};
359         my $args = "";
360         my $is_win16 = $make{"MODULE"} && ($make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}});
361         if ($rules eq $makerules{"MAKE_DLL_RULES"})
362         {
363             (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
364             my $implib = $make{"IMPORTLIB"} || "";
365             my $implib_srcs = defined($make{"IMPLIB_SRCS"}) && join( " ", @{$make{"IMPLIB_SRCS"}} );
366             $args .= "," if $implib || $is_win16;
367             $args .= "enable_win16" if $is_win16;
368             $args .= ",[$implib]" if $implib;
369             $args .= ",[$implib_srcs]" if $implib_srcs;
370             push @lines, "WINE_CONFIG_DLL($name$args)\n";
371         }
372         elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
373         {
374             (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
375             $args = ",[ALL_PROGRAM_DIRS";
376             $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
377             $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
378             $args .= "]";
379             $args .= ",enable_win16" if $is_win16;
380             push @lines, "WINE_CONFIG_PROGRAM($name$args)\n";
381         }
382         elsif ($rules eq $makerules{"MAKE_TEST_RULES"})
383         {
384             (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
385             push @lines, "WINE_CONFIG_TEST($dir)\n";
386         }
387         elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"})
388         {
389             (my $name = $file) =~ s/^dlls\/(.*)\/Makefile/$1/;
390             push @lines, "WINE_CONFIG_LIB($name)\n";
391         }
392         elsif ($file =~ /^[^\/]*\/Makefile$/)
393         {
394             push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules],[],[ALL_TOP_DIRS])\n";
395         }
396         else
397         {
398             push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules])\n";
399         }
400     }
401
402     # update the source variables in all the makefiles
403
404     foreach my $file (sort @_)
405     {
406         my %make = %{$makefiles{$file}};
407
408         replace_makefile_variable( $file, "LEX_SRCS" );
409         replace_makefile_variable( $file, "BISON_SRCS" );
410         replace_makefile_variable( $file, "MC_SRCS" );
411         replace_makefile_variable( $file, "SVG_SRCS" );
412         replace_makefile_variable( $file, "C_SRCS" );
413         replace_makefile_variable( $file, "RC_SRCS" );
414     }
415
416     push @lines, "\ndnl Build dependencies for test files compiled into winetest\n";
417     replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl Build dependencies for test files compiled into winetest$', @lines);
418 }
419
420
421 ################################################################
422 # process ignore targets for generic source files
423
424 sub update_ignores(@)
425 {
426     my @ignores;
427
428     foreach my $file (sort @_)
429     {
430         my %makefile = %{$makefiles{$file}};
431         my @list;
432
433         foreach my $src (@ignore_srcs)
434         {
435             my @pattern = @{$src};
436             next unless defined $makefile{$pattern[0]};
437             push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
438         }
439         foreach my $f (@list)
440         {
441             push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
442         }
443
444         if (defined $makefile{"IMPORTLIB"})
445         {
446             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
447             {
448                 if ("dlls/$1/" ne $makefile{"=dir"}) { push @ignores, "dlls/lib$1.def"; }
449             }
450             else
451             {
452                 die "invalid importlib name $makefile{IMPORTLIB} in $file";
453             }
454         }
455     }
456     return @ignores;
457 }
458
459
460 ################################################################
461 # update include/Makefile.in
462
463 sub update_includes()
464 {
465     my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
466     my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
467     foreach my $incl (@includes)
468     {
469         if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
470         next if ($incl =~ /\.in$/);
471         if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
472         {
473             if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
474             next;
475         }
476         if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
477         elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
478         elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
479         elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
480         elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
481         elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
482         else { die "unknown file $incl in include dir"; }
483     }
484     replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
485                      "PRIVATE_IDL_H_SRCS = \\\n\t",
486                      join( " \\\n\t", sort @private_idl_srcs ),
487                      "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
488                      join( " \\\n\t", sort @public_idl_srcs ),
489                      "\n\nIDL_TLB_SRCS = \\\n\t",
490                      join( " \\\n\t", sort @tlb_srcs ),
491                      "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
492                      join( " \\\n\t", sort @h_srcs ),
493                      "\n\nEXTRASUBDIRS = ",
494                      join( " ", sort keys %subdirs ),
495                      "\n\nINSTALLDIRS = \\\n" );
496     return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
497 }
498
499
500 ################################################################
501 # update the main .gitignore
502
503 sub update_gitignore(@)
504 {
505     my @ignores = values %makerules;
506
507     foreach my $make (@makefiles)
508     {
509         my %makefile = %{$makefiles{$make}};
510         my $dir = $makefile{"=dir"};
511         if (defined $makefile{"MANPAGES"})
512         {
513             push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
514         }
515         if (defined $makefile{"PROGRAMS"})
516         {
517             push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
518         }
519     }
520
521     # prepend a slash to paths that don't have one
522     @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;
523
524     # get rid of duplicates
525     my %ignores = ();
526     foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
527
528     replace_in_file( ".gitignore", undef, undef,
529                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
530                      join("\n", sort keys %ignores), "\n" );
531 }
532
533
534 die "needs to be run from a git checkout" unless -d ".git";
535
536 @all_files = split /\0/, `git ls-files -c -z`;
537 @makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
538
539 foreach my $file (sort values %makerules, @makefiles)
540 {
541     my %make = parse_makefile( $file );
542     $makefiles{$file} = \%make;
543 }
544
545 assign_sources_to_makefiles();
546 update_makefiles( @makefiles );
547 push @ignores, update_includes();
548 push @ignores, update_ignores( @makefiles );
549 update_gitignore( @ignores );