wininet: Use proc instead of enum in FTPFINDNEXTW request.
[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)\s*=\s*(.*)/)
146         {
147             $make{$1} = $2;
148             next;
149         }
150         if (/^(IDL_H_SRCS|IMPLIB_SRCS|SPEC_SRCS16)\s*=\s*(.*)/)
151         {
152             my @list = split(/\s+/, $2);
153             $make{$1} = \@list;
154             next;
155         }
156         if (/^\#\s*MKDLL_SKIP/ || /^\#\s*MKPROG_SKIP/)
157         {
158             $make{"=skip"} = 1;
159             next;
160         }
161     }
162     return %make;
163 }
164
165 if (-d ".git")
166 {
167     @makefiles = map { s/\.in$//; $_; } split /\s/, `git ls-files -c Makefile.in \\*/Makefile.in`;
168 }
169 else
170 {
171     @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
172 }
173
174 foreach my $file (sort values %makerules, @makefiles)
175 {
176     my %make = parse_makefile( $file );
177     $makefiles{$file} = \%make;
178 }
179
180 ################################################################
181 # update the makefile list in configure.ac
182
183 my @lines = ();
184
185 foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
186 {
187     push @lines, "$var=$makerules{$var}\n";
188     push @lines, "AC_SUBST_FILE($var)\n\n";
189 }
190
191 replace_in_file( "configure.ac", '^MAKE_RULES', '\]\)$',
192                  @lines,
193                  "AC_CONFIG_FILES([\n",
194                  join ("\n", (sort values %makerules), (sort @makefiles) ), "])\n" );
195
196
197 ################################################################
198 # update the tests list in programs/winetest/Makefile.in and programs/winetest/winetest.rc
199
200 my %modules = ( "user" => "user32" );
201 my %tests;
202 @lines = ( "TESTBINS =" );
203
204 foreach my $file (sort grep /^dlls\/.*\/tests\/Makefile/, @makefiles)
205 {
206     if ($file =~ /^dlls\/(.*)\/tests\/Makefile/)
207     {
208         my $dir = $1;
209         my $mod = $modules{$dir} || $dir;
210         $tests{$mod} = $dir;
211         push @lines, " \\\n\t${mod}_test.exe";
212     }
213 }
214 push @lines, "\n\n";
215
216 foreach my $test (sort keys %tests)
217 {
218     my $dir = $tests{$test};
219     push @lines, "${test}_test.exe: \$(DLLDIR)/$dir/tests/${test}_test.exe\$(DLLEXT)\n";
220     push @lines, "\tcp \$(DLLDIR)/$dir/tests/${test}_test.exe\$(DLLEXT) \$\@ && \$(STRIP) \$\@\n";
221 }
222 push @lines, "\n# Special rules\n";
223
224 replace_in_file( "programs/winetest/Makefile.in", '^TESTBINS\s*=', '^# Special rules', @lines );
225
226 @lines = ();
227 foreach my $test (sort keys %tests)
228 {
229     push @lines, "${test}_test.exe TESTRES \"${test}_test.exe\"\n";
230 }
231
232 replace_in_file( "programs/winetest/winetest.rc", ' TESTRES ', undef, @lines );
233
234 ################################################################
235 # update the makefile list in Makefile.in
236
237 my @targets;
238 my @depends;
239
240 foreach my $file (sort values %makerules)
241 {
242     push @targets, $file;
243     my %make = %{$makefiles{$file}};
244     if (!defined($make{"=rules"})) { push @depends, "$file: $file.in"; }
245     else { push @depends, "$file: $file.in Make.rules"; }
246 }
247
248 foreach my $file (sort @makefiles)
249 {
250     push @targets, $file unless $file eq "Makefile";
251     my $dep = ${$makefiles{$file}}{"=rules"};
252     push @depends, "$file: $file.in $dep";
253 }
254
255 @lines = ();
256 push @lines, "ALL_MAKEFILES = \\\n\t";
257 push @lines, join (" \\\n\t", @targets ), "\n\n";
258 push @lines, "Makefile \$(ALL_MAKEFILES): config.status\n";
259 push @lines, "\t\@./config.status \$\@\n\n";
260 push @lines, "\$(RECURSE_TARGETS) \$(MAKEDEP): \$(ALL_MAKEFILES)\n\n";
261 push @lines, "distclean::\n";
262 push @lines, "\t\$(RM) Makefile \$(ALL_MAKEFILES)\n\n";
263 push @lines, join ("\n", @depends ), "\n";
264
265 replace_in_file( "Makefile.in", '^ALL_MAKEFILES\s*=', undef, @lines );
266
267
268 ################################################################
269 # update dlls/Makefile.in
270
271 sub update_dlls(@)
272 {
273     my (%directories, %testdirs, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
274     my $text = "";
275
276     my @ignores =
277     (
278      "/Makedll.rules",
279      "/Makeimplib.rules",
280      "/Maketest.rules",
281      "*/tests/testlist.c",
282      "*/tests/*.ok",
283     );
284
285     sub needs_symlink($$)
286     {
287         my ($mod, $dir) = @_;
288         $mod =~ s/\.dll$//;
289         return $mod ne $dir;
290     }
291
292     foreach my $make (@_)
293     {
294         if ($make =~ /dlls\/(.*)\/tests\/Makefile/)
295         {
296             $testdirs{$1} = "$1/tests";
297             next;
298         }
299         my %makefile = %{$makefiles{$make}};
300
301         next unless defined $makefile{"MODULE"};
302         my $module = $makefile{"MODULE"};
303         (my $dir = $make) =~ s/^dlls\/(.*)\/[^\/]+$/$1/;
304
305         if ($module =~ /^lib.*\.a$/)
306         {
307             $staticlib_dirs{$module} = $dir;
308             die "invalid module $module in dir $staticlib_dirs{$module}\n" if "lib$staticlib_dirs{$module}.a" ne $module;
309         }
310         else
311         {
312             $directories{$module} = $dir;
313         }
314
315         if (defined $makefile{"IMPORTLIB"})
316         {
317             if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)\.\$\(IMPLIBEXT\)/)
318             {
319                 $importlibs{$module} = $1;
320             }
321             else
322             {
323                 die "invalid importlib name $makefile{IMPORTLIB} in $make";
324             }
325         }
326
327         $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};
328
329         if (defined $makefile{"SPEC_SRCS16"})
330         {
331             my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
332             $altnames{$module} = \@list;
333         }
334
335         if (defined $makefile{"IDL_H_SRCS"})
336         {
337             push @ignores, map { $_ =~ s/(.*)\.idl$/$dir\/$1.h/; $_; } @{$makefile{"IDL_H_SRCS"}};
338         }
339     }
340
341     # output special dlls configure definitions
342
343     $text .= "# special configure-dependent targets\n\n";
344     my %specials = ();
345     foreach my $mod (sort keys %special_dlls)
346     {
347         $specials{$special_dlls{$mod}} .= " " . $mod;
348     }
349     foreach my $i (sort keys %specials)
350     {
351         $text .= $i . " =" . $specials{$i} . "\n";
352     }
353     $text .= "EXTRADIRS =";
354     foreach my $i (sort keys %specials) { $text .= sprintf " \@%s\@", $i; }
355     $text .= "\n\n";
356
357     # output the subdirs list
358
359     $text .= "# Subdir list\n\n";
360     $text .= "BASEDIRS =";
361     foreach my $dir (sort values %directories)
362     {
363         next if defined($special_dlls{$dir});  # skip special dlls
364         $text .= " \\\n\t" . $dir;
365     }
366
367     $text .= "\n\nIMPLIBSUBDIRS = \\\n\t";
368     $text .=  join " \\\n\t", sort values %staticlib_dirs;
369
370     $text .= "\n\nTESTSUBDIRS = \\\n\t";
371     $text .= join " \\\n\t", sort values %testdirs;
372
373     $text .=  "\n\nSUBDIRS = \\\n\t";
374     $text .= join " \\\n\t", "\$(BASEDIRS)", "\$(IMPLIBSUBDIRS)", "\$(TESTSUBDIRS)", sort keys %special_dlls;
375
376     $text .= "\n\nBUILDSUBDIRS   = \$(BASEDIRS) \$(EXTRADIRS) \$(TESTSUBDIRS)\n";
377     $text .= "INSTALLSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS) \$(IMPLIBSUBDIRS)\n";
378     $text .= "DOCSUBDIRS     = \$(BASEDIRS) \$(EXTRADIRS)\n";
379
380     # output the all: target
381
382     my %targets = ();  # use a hash to get rid of duplicate target names
383     my %targets16 = ();
384     foreach my $mod (sort keys %directories)
385     {
386         next if defined($special_dlls{$directories{$mod}});  # skip special dlls
387         $targets{$mod . ".so"} = 1 if needs_symlink($mod, $directories{$mod});
388         next unless defined $altnames{$mod};
389         foreach my $i (sort @{$altnames{$mod}})
390         {
391             $targets16{$i . "16"} = $mod;
392         }
393     }
394
395     $text .= "\n\@MAKE_RULES\@\n\n";
396     $text .= "# Symbolic links\n\n";
397     $text .= "WIN16_FILES = \\\n";
398     $text .=  "\t" . join( " \\\n\t", sort keys %targets16 ) . "\n\n";
399     $text .= "SYMLINKS_SO = \\\n";
400     $text .= "\t\@WIN16_FILES\@ \\\n";
401     $text .= "\t" . join( " \\\n\t", sort keys %targets ) . "\n\n";
402     $text .= "# Main target\n\n";
403     $text .= "all: \$(BUILDSUBDIRS) symlinks\$(DLLEXT)\n\n";
404     $text .= ".PHONY: symlinks symlinks.so implib\n\n";
405     $text .= "symlinks.so: \$(SYMLINKS_SO)\n\n";
406     $text .= "symlinks: \$(BUILDSUBDIRS)\n\n";
407
408     # output the lib name -> directory rules
409
410     $text .= "# Map symlink name to the corresponding library\n\n";
411     foreach my $mod (sort keys %directories)
412     {
413         next unless needs_symlink($mod, $directories{$mod});
414         $text .= sprintf "%s.so: %s/%s.so\n", $mod, $directories{$mod}, $mod;
415         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.so \$@\n\n", $directories{$mod}, $mod;
416     }
417
418     $text .= "# Placeholders for 16-bit libraries\n\n";
419     foreach my $mod (sort keys %directories)
420     {
421         next unless defined $altnames{$mod};
422         $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
423         $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
424     }
425
426     # output the import libraries rules
427
428     $text .= "# Import libraries\n\n";
429     $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";
430
431     my @lib_symlinks = ();
432     foreach my $mod (sort keys %importlibs)
433     {
434         my $dir = $directories{$mod};
435         my $lib = $importlibs{$mod};
436         if ($lib ne "lib" . $dir) { push @lib_symlinks, $mod; }
437     }
438     $text .= "IMPORT_SYMLINKS =";
439     foreach my $mod (sort @lib_symlinks)
440     {
441         $text .= sprintf " \\\n\t%s.\$(IMPLIBEXT)", $importlibs{$mod};
442     }
443
444     $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
445     foreach my $mod (sort keys %staticlib_dirs)
446     {
447         $text .= sprintf " \\\n\t%s/%s", $staticlib_dirs{$mod}, $mod;
448     }
449     foreach my $mod (sort keys %importlibs)
450     {
451         my $dir = $directories{$mod};
452         my $def = $mod;
453         $def =~ s/\.(dll|drv)$//;
454         $text .= sprintf " \\\n\t%s/lib%s.\$(IMPLIBEXT)", $dir, $def;
455         next unless defined $static_implibs{$mod};
456         $text .= sprintf " \\\n\t%s/lib%s.\$(STATIC_IMPLIBEXT)", $dir, $def
457     }
458     $text .= "\n\n";
459     $text .= "implib: \$(IMPORT_LIBS)\n\n";
460
461     foreach my $mod (sort keys %importlibs)
462     {
463         my $dir = $directories{$mod};
464         my $lib = $importlibs{$mod};
465         my $spec = $mod;
466         $spec =~ s/\.dll$//;
467         $text .= sprintf "%s/%s.\$(IMPLIBEXT): %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
468         $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(IMPLIBEXT)\n\n", $dir, $lib;
469         next unless $static_implibs{$mod};
470         $text .= sprintf "%s/%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
471         $text .= sprintf "\t\@cd %s && \$(MAKE) %s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
472     }
473     foreach my $mod (sort @lib_symlinks)
474     {
475         my $dir = $directories{$mod};
476         my $lib = $importlibs{$mod} . ".\$(IMPLIBEXT)";
477         $text .= sprintf "%s: %s/%s\n", $lib, $dir, $lib;
478         $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s \$@\n\n", $dir, $lib;
479     }
480
481     $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
482     $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";
483
484     # output the inter-dll dependencies and rules
485
486     $text .= "# Map library name to the corresponding directory\n\n";
487
488     foreach my $mod (sort keys %directories)
489     {
490         next unless needs_symlink($mod, $directories{$mod});
491         $text .= sprintf "%s/%s.so: %s\n", $directories{$mod}, $mod, $directories{$mod};
492     }
493     foreach my $mod (sort keys %staticlib_dirs)
494     {
495         $text .= sprintf "%s/%s: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
496     }
497     $text .= "\n# Misc rules\n";
498
499     replace_in_file( "dlls/Makefile.in",
500                      '^# special configure-dependent targets',
501                      '^# Misc rules',
502                      $text );
503
504     # .gitignore file
505
506     foreach my $mod (sort @lib_symlinks)
507     {
508         push @ignores, "/$importlibs{$mod}.def";
509     }
510     foreach my $mod (sort keys %directories)
511     {
512         next unless defined $altnames{$mod};
513         push @ignores, map { "/" . $_ . "16"; } @{$altnames{$mod}};
514     }
515     foreach my $mod (sort keys %importlibs)
516     {
517         my $dir = $directories{$mod};
518         my $def = $mod;
519         $def =~ s/\.(dll|drv)$//;
520         push @ignores, "$dir/lib$def.def";
521     }
522
523     replace_in_file( "dlls/.gitignore", undef, undef,
524                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
525                      join("\n", sort @ignores), "\n" );
526 }
527
528
529
530 ################################################################
531 # update programs/Makefile.in and programs/.gitignore
532
533 sub update_progs(@)
534 {
535     my (@subdirs, @install_subdirs, @install_progs);
536
537     my @ignores =
538     (
539      "/Makeprog.rules",
540      "/wineapploader",
541      "/winelauncher",
542     );
543
544     foreach my $make (@_)
545     {
546         my %makefile = %{$makefiles{$make}};
547         my $module = $makefile{"MODULE"};
548         (my $dir = $make) =~ s/^programs\/(.*)\/Makefile$/$1/;
549         die "Invalid module $module in $make" unless "$dir.exe" eq $module;
550         next if defined $makefile{"=skip"};
551         push @subdirs, $dir;
552         push @ignores, "$dir/$dir";
553         push @install_subdirs, $dir unless $dont_install{$dir};
554         push @install_progs, $dir if $bin_install{$dir};
555     }
556
557     replace_in_file( "programs/Makefile.in", '^SUBDIRS\s*=', '^INSTALLDIRS',
558                      "SUBDIRS = \\\n\t",
559                      join( " \\\n\t", @subdirs ),
560                      "\n\n# Sub-directories to run make install into\nINSTALLSUBDIRS = \\\n\t",
561                      join( " \\\n\t", @install_subdirs ),
562                      "\n\n# Programs to install in bin directory\nINSTALLPROGS = \\\n\t",
563                      join( " \\\n\t", @install_progs ),
564                      "\n\nINSTALLDIRS = \$(DESTDIR)\$(bindir)\n" );
565
566     replace_in_file( "programs/.gitignore", undef, undef,
567                      "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
568                      join("\n", sort @ignores), "\n" );
569 }
570
571 update_dlls( sort grep /^dlls\//, @makefiles );
572 update_progs( sort grep /^programs\/.*\/Makefile$/, @makefiles );