Revert "winex11.drv: Optimise getting the bits of a DIB after calling SetDIBits."
[wine] / tools / winapi / msvcmaker
1 #! /usr/bin/perl -w
2
3 # Copyright 2002 Patrik Stridvall
4
5 use strict;
6
7 BEGIN {
8     $0 =~ m%^(.*?/?tools)/winapi/msvcmaker$%;
9     require "$1/winapi/setup.pm";
10 }
11
12 use setup qw($current_dir $wine_dir);
13 use lib $setup::winapi_dir;
14 use config qw(get_spec_files get_makefile_in_files);
15 use output qw($output);
16 use util qw(replace_file);
17
18 use msvcmaker_options qw($options);
19
20 if($options->progress) {
21     $output->enable_progress;
22 } else {
23     $output->disable_progress;
24 }
25
26 ########################################################################
27 # main
28
29 my @spec_files = get_spec_files("winelib");
30 my @makefile_in_files = get_makefile_in_files("winelib");
31
32 my $wine = 1;
33
34 my $output_prefix_dir = "Output";
35 my $no_release = 1;
36
37 my %modules;
38
39 sub read_spec_file($) {
40     my $spec_file = shift;
41
42     my $module = $spec_file;
43     $module =~ s%^.*?([^/]+)\.spec$%$1%;
44     $module .= ".dll" if $module !~ /\./;
45
46     my $type = "win32";
47
48     open(IN, "< $wine_dir/$spec_file") || die "Error: Can't open $wine_dir/$spec_file: $!\n";
49
50     my $header = 1;
51     my $lookahead = 0;
52     while($lookahead || defined($_ = <IN>)) {
53         $lookahead = 0;
54
55         s/^\s*?(.*?)\s*$/$1/; # remove whitespace at beginning and end of line
56         s/^(.*?)\s*#.*$/$1/;  # remove comments
57         /^$/ && next;         # skip empty lines
58
59         if($header)  {
60             if(/^(?:\d+|@)/) {
61                 $header = 0;
62                 $lookahead = 1;
63             }
64             next;
65         }
66
67         if(/^(\d+|@)\s+pascal(?:16)?/) {
68             $type = "win16";
69             last;
70         }
71     }
72     close(IN);
73
74     # FIXME: Kludge
75     if($module =~ /^(?:(?:imm|ole2conv|ole2prox|ole2thk|rasapi16|msacm|windebug)\.dll|comm\.drv)$/) {
76         $type = "win16";
77     }
78
79     if($type eq "win32") {
80         $modules{$module}{module} = $module;
81         $modules{$module}{type} = $type;
82         $modules{$module}{spec_file} = $spec_file;
83     }
84 }
85
86 if ($options->wine || $options->winetest) {
87     foreach my $spec_file (@spec_files) {
88         read_spec_file($spec_file);
89     }
90 }
91
92 my @gdi32_dirs = qw(dlls/gdi32/enhmfdrv dlls/gdi32/mfdrv);
93
94 push @makefile_in_files, "libs/wine/Makefile.in";
95 push @makefile_in_files, "tools/winebuild/Makefile.in";
96
97 sub filter_files($$) {
98     my $files = shift;
99     my $filter = shift;
100
101     my $filtered_files = [];
102     my $rest_of_files = [];
103     foreach my $file (@$files) {
104         if($file =~ /$filter/) {
105             $file =~ s%.*?([^/]+)$%./$1%; # FIXME: Kludge
106             push @$filtered_files, $file;
107         } else {
108             push @$rest_of_files, $file;
109         }
110     }
111     return ($rest_of_files, $filtered_files);
112 }
113
114 my %wine_test_dsp_files;
115
116 MAKEFILE_IN: foreach my $makefile_in_file (@makefile_in_files) {
117     open(IN, "< $wine_dir/$makefile_in_file") || die "Error: Can't open $wine_dir/$makefile_in_file: $!\n";
118
119     my $topobjdir;
120     my $module;
121     my $testdll;
122     my @imports;
123
124     my %vars;
125
126     my $again = 0;
127     my $lookahead = 0;
128
129     if($makefile_in_file eq "loader/Makefile.in") {
130         next;
131     }
132
133     while($again || defined(my $line = <IN>)) {
134         if(!$again) {
135             chomp $line;
136             if($lookahead) {
137                 $lookahead = 0;
138                 $_ .= "\n" . $line;
139             } else {
140                 $_ = $line;
141             }
142         } else {
143             $again = 0;
144         }
145
146         s/^\s*?(.*?)\s*$/$1/; # remove whitespace at beginning and end of line
147         s/^(.*?)\s*#.*$/$1/;  # remove comments
148         /^$/ && next;         # skip empty lines
149
150         if(s/\\$/ /s) {
151             $lookahead = 1;
152             next;
153         }
154
155         if(/^MODULE\s*=\s*([\w\.]+)$/) {
156             $module = $1;
157
158             if($module eq "none") {
159                 if($makefile_in_file eq "tools/winebuild/Makefile.in") {
160                     $module = "winebuild.exe";
161                 } else {
162                     next MAKEFILE_IN;
163                 }
164             }
165         } elsif(/^LIBRARY\s*=\s*(\S+)\s*$/) {
166             $module = $1  . "\.lib";
167         } elsif(/^TOPOBJDIR\s*=\s*(\S+)\s*$/) {
168             $topobjdir = $1;
169         } elsif (/^TESTDLL\s*=\s*(\S+)\s*$/) {
170             $testdll = $1;
171         } elsif (/^IMPORTS\s*=\s*/) {
172             push @imports, grep !/^ntdll$/, split /\s+/s, $';
173         } elsif (/^EXTRALIBS\s*=\s*/) {
174             push @imports, map { /^-l(dxerr8|dxerr9|dxguid|strmiids|uuid)$/ ? $1 : () } split /\s+/s, $';
175         } elsif (/^CTESTS\s*=\s*/) {
176             my @files = split /\s+/s, $';
177
178             my $dir = $makefile_in_file;
179             $dir =~ s/\/Makefile\.in$//;
180
181             my $dsp_file = $testdll;
182             $dsp_file =~ s/\.(dll|drv)$/_test.dsp/;
183             $dsp_file = "$dir/$dsp_file";
184
185             $wine_test_dsp_files{$dsp_file}{files} = [@files, "testlist.c"];
186             $wine_test_dsp_files{$dsp_file}{imports} = [@imports];
187         } elsif(/^(\w+)\s*=\s*/) {
188             my $var = $1;
189             my @files = split /\s+/s, $';
190
191             @files = map {
192                 if(/^\$\((\w+):\%=(.*?)\%(.*?)\)$/) {
193                     my @list = @{$vars{$1}};
194                     my $prefix = $2;
195                     my $suffix = $3;
196                     foreach my $item (@list) {
197                         $item = "$prefix$item$suffix";
198                     }
199                     @list;
200                 } elsif(/^\$\(TOPOBJDIR\)(.*?)$/) {
201                     "$topobjdir$1";
202                 } elsif(/^\$/) {
203                     print STDERR "unknown variable '$_'\n" if 0;
204                     ();
205                 } else {
206                     $_;
207                 }
208             } @files;
209
210             $vars{$var} = \@files;
211         }
212     }
213
214     close(IN);
215
216     next if !$module;
217
218     my $c_srcs = [];
219     my $source_files = [];
220     if(exists($vars{C_SRCS})) {
221         $c_srcs = [sort(@{$vars{C_SRCS}})];
222         $source_files = [sort(@{$vars{C_SRCS}})];
223     }
224
225     my $header_files = [];
226     if(exists($vars{H_SRCS})) {
227         $header_files = [sort(@{$vars{H_SRCS}})];
228     }
229
230     my $resource_files = [];
231     if(exists($vars{RC_SRCS})) {
232         $resource_files = [sort(@{$vars{RC_SRCS}})];
233     }
234
235     my $project = $module;
236     $project =~ s/\.(?:dll|exe|lib)$//;
237     $project =~ y/./_/;
238
239     my $type;
240     if($module =~ /\.exe$/) {
241         $type = "exe";
242     } elsif($module =~ /\.lib$/) {
243         $type = "lib";
244     } else {
245         $type = "dll";
246     }
247
248     my $dsp_file = $makefile_in_file;
249     $dsp_file =~ s/Makefile.in$/$project.dsp/;
250
251     if($module eq "gdi32.dll") {
252         foreach my $dir (@gdi32_dirs) {
253             my $dir2 = $dir;
254             $dir2 =~ s%^.*?/([^/]+)$%$1%;
255
256             my $module = "gdi32_$dir2.lib";
257             $module =~ s%/%_%g;
258
259             my $project = "gdi32_$dir2";
260             $project =~ s%/%_%g;
261
262             my $type = "lib";
263             my $dsp_file = "$dir/$project.dsp";
264
265             ($source_files, my $local_source_files) = filter_files($source_files, "$dir2/");
266             ($header_files, my $local_header_files) = filter_files($header_files, "$dir2/");
267             ($resource_files, my $local_resource_files) = filter_files($resource_files, "$dir2/");
268
269             $modules{$module}{wine} = 1;
270             $modules{$module}{winetest} = 0;
271             $modules{$module}{project} = $project;
272             $modules{$module}{type} = $type;
273             $modules{$module}{dsp_file} = $dsp_file;
274             $modules{$module}{c_srcs} = $c_srcs;
275             $modules{$module}{source_files} = $local_source_files;
276             $modules{$module}{header_files} = $local_header_files;
277             $modules{$module}{resource_files} = $local_resource_files;
278             $modules{$module}{imports} = [];
279         }
280     }
281
282     $modules{$module}{wine} = 1;
283     $modules{$module}{winetest} = 0;
284     $modules{$module}{project} = $project;
285     $modules{$module}{type} = $type;
286     $modules{$module}{dsp_file} = $dsp_file;
287     $modules{$module}{c_srcs} = $c_srcs;
288     $modules{$module}{source_files} = $source_files;
289     $modules{$module}{header_files} = $header_files;
290     $modules{$module}{resource_files} = $resource_files;
291     $modules{$module}{imports} = [];
292 }
293
294 $wine_test_dsp_files{"wineruntests.dsp"}{files} = ["runtests.c"];
295 $wine_test_dsp_files{"wineruntests.dsp"}{imports} = [];
296
297 $wine_test_dsp_files{"winetest.dsp"}{files} = [
298   'include/wine/exception.h',
299   'include/wine/test.h',
300   'include/wine/unicode.h',
301   'winetest.c'
302 ];
303 $wine_test_dsp_files{"winetest.dsp"}{imports} = [];
304
305 my %runtests = ();
306
307 foreach my $dsp_file (keys(%wine_test_dsp_files)) {
308     my $project = $dsp_file;
309     $project =~ s%^(?:.*?/)?([^/]+)\.dsp$%$1%;
310
311     my @files = @{$wine_test_dsp_files{$dsp_file}{files}};
312     my @imports = @{$wine_test_dsp_files{$dsp_file}{imports}};
313
314     my $type;
315     my $c_srcs = [];
316     my $source_files = [];
317     my $header_files = [];
318     my $resource_files = [];
319
320     my @tests = ();
321
322     if ($project eq "winetest") {
323         $type = "lib";
324         $c_srcs = [@files];
325         $source_files = [@files];
326         $header_files = [];
327         $resource_files = [];
328     } elsif ($project eq "wineruntests") {
329         $type = "exe";
330         $c_srcs = [@files];
331         $source_files = [@files];
332         $header_files = [];
333         $resource_files = [];
334     } else {
335         $type = "exe";
336         $c_srcs = [@files];
337         $source_files = [@files];
338         $header_files = [];
339         $resource_files = [];
340         
341         @tests = map {
342             if (/^testlist\.c$/) {
343                 ();
344             } else {
345                 s/\.c$//;
346                 $_;
347             }
348         } @files;
349
350         $runtests{$dsp_file} = [@tests];
351     }
352     my $module = "$project.$type";
353
354     $modules{$module}{wine} = 0;
355     $modules{$module}{winetest} = 1;
356
357     $modules{$module}{project} = $project;
358     $modules{$module}{type} = $type;
359     $modules{$module}{dsp_file} = $dsp_file;
360     $modules{$module}{c_srcs} = $c_srcs;
361     $modules{$module}{source_files} = $source_files;
362     $modules{$module}{header_files} = $header_files;
363     $modules{$module}{resource_files} = $resource_files;
364     $modules{$module}{imports} = [@imports];
365
366     $modules{$module}{tests} = [@tests];
367 }
368
369 foreach my $module (sort(keys(%modules))) {
370     if($module =~ /^(?:ttydrv.dll|x11drv.dll)$/) {
371         delete $modules{$module};
372     }
373 }
374
375 my @modules = ();
376 foreach my $module (sort(keys(%modules))) {
377     if (($options->wine && $modules{$module}{wine}) ||
378         ($options->winetest && $modules{$module}{winetest}))
379     {
380         push @modules, $module;
381     }
382 }
383
384 my $progress_output;
385 my $progress_current = 0;
386 my $progress_max = scalar(@modules);
387
388 foreach my $module (@modules) {
389     my $dsp_file = $modules{$module}{dsp_file};
390     replace_file("$wine_dir/$dsp_file", \&_generate_dsp, $module);
391 }
392
393 sub _generate_dsp($$) {
394     local *OUT = shift;
395
396     my $module = shift;
397
398     my $dsp_file = $modules{$module}{dsp_file};
399     my $project = $modules{$module}{project};
400     my @imports = @{$modules{$module}{imports}};
401
402     my $lib = ($modules{$module}{type} eq "lib");
403     my $dll = ($modules{$module}{type} eq "dll");
404     my $exe = ($modules{$module}{type} eq "exe");
405
406     my $console = $exe; # FIXME: Not always correct
407
408     my $msvc_wine_dir = do {
409         my @parts = split(m%/%, $dsp_file);
410         if($#parts == 1) {
411             "..";
412         } elsif($#parts == 2) {
413             "..\\..";
414         } else {
415             "..\\..\\..";
416         }
417     };
418     my $wine_include_dir = "$msvc_wine_dir\\include";
419
420     $progress_current++;
421     $output->progress("$dsp_file (file $progress_current of $progress_max)");
422
423     my @c_srcs = @{$modules{$module}{c_srcs}};
424     my @source_files = @{$modules{$module}{source_files}};
425     my @header_files = @{$modules{$module}{header_files}};
426     my @resource_files = @{$modules{$module}{resource_files}};
427
428     if ($project !~ /^wine(?:build|runtests|test)?$/ &&
429         $project !~ /^(?:gdi32)_.+?$/ &&
430         $project !~ /_test$/)
431     {
432         push @source_files, "$project.spec";
433         @source_files = sort(@source_files);
434     }
435
436     my $no_cpp = 1;
437     my $no_msvc_headers = 1;
438     if ($project =~ /^wine(?:runtests|test)$/ || $project =~ /_test$/) {
439         $no_msvc_headers = 0;
440     }
441
442     my @cfgs;
443
444     push @cfgs, "$project - Win32";
445
446     if (!$no_cpp) {
447         my @_cfgs;
448         foreach my $cfg (@cfgs) {
449             push @_cfgs, "$cfg C";
450             push @_cfgs, "$cfg C++";
451         }
452         @cfgs = @_cfgs;
453     }
454
455     if (!$no_release) {
456         my @_cfgs;
457         foreach my $cfg (@cfgs) {
458             push @_cfgs, "$cfg Debug";
459             push @_cfgs, "$cfg Release";
460         }
461         @cfgs = @_cfgs;
462     }
463
464     if (!$no_msvc_headers) {
465         my @_cfgs;
466         foreach my $cfg (@cfgs) {
467             push @_cfgs, "$cfg MSVC Headers";
468             push @_cfgs, "$cfg Wine Headers";
469         }
470         @cfgs = @_cfgs;
471     }
472
473     my $default_cfg = $cfgs[$#cfgs];
474
475     print OUT "# Microsoft Developer Studio Project File - Name=\"$project\" - Package Owner=<4>\r\n";
476     print OUT "# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n";
477     print OUT "# ** DO NOT EDIT **\r\n";
478     print OUT "\r\n";
479
480     if ($lib) {
481         print OUT "# TARGTYPE \"Win32 (x86) Static Library\" 0x0104\r\n";
482     } elsif ($dll) {
483         print OUT "# TARGTYPE \"Win32 (x86) Dynamic-Link Library\" 0x0102\r\n";
484     } else {
485         print OUT "# TARGTYPE \"Win32 (x86) Console Application\" 0x0103\r\n";
486     }
487     print OUT "\r\n";
488
489     print OUT "CFG=$default_cfg\r\n";
490     print OUT "!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n";
491     print OUT "!MESSAGE use the Export Makefile command and run\r\n";
492     print OUT "!MESSAGE \r\n";
493     print OUT "!MESSAGE NMAKE /f \"$project.mak\".\r\n";
494     print OUT "!MESSAGE \r\n";
495     print OUT "!MESSAGE You can specify a configuration when running NMAKE\r\n";
496     print OUT "!MESSAGE by defining the macro CFG on the command line. For example:\r\n";
497     print OUT "!MESSAGE \r\n";
498     print OUT "!MESSAGE NMAKE /f \"$project.mak\" CFG=\"$default_cfg\"\r\n";
499     print OUT "!MESSAGE \r\n";
500     print OUT "!MESSAGE Possible choices for configuration are:\r\n";
501     print OUT "!MESSAGE \r\n";
502     foreach my $cfg (@cfgs) {
503         if ($lib) {
504             print OUT "!MESSAGE \"$cfg\" (based on \"Win32 (x86) Static Library\")\r\n";
505         } elsif ($dll) {
506             print OUT "!MESSAGE \"$cfg\" (based on \"Win32 (x86) Dynamic-Link Library\")\r\n";
507         } else {
508             print OUT "!MESSAGE \"$cfg\" (based on \"Win32 (x86) Console Application\")\r\n";
509         }
510     }
511     print OUT "!MESSAGE \r\n";
512     print OUT "\r\n";
513
514     print OUT "# Begin Project\r\n";
515     print OUT "# PROP AllowPerConfigDependencies 0\r\n";
516     print OUT "# PROP Scc_ProjName \"\"\r\n";
517     print OUT "# PROP Scc_LocalPath \"\"\r\n";
518     print OUT "CPP=cl.exe\r\n";
519     print OUT "MTL=midl.exe\r\n" if !$lib && !$exe;
520     print OUT "RSC=rc.exe\r\n";
521     print OUT "\r\n";
522
523     my $n = 0;
524
525     my $output_dir;
526     foreach my $cfg (@cfgs) {
527         if($#cfgs == 0) {
528             # Nothing
529         } elsif($n == 0) {
530             print OUT "!IF  \"\$(CFG)\" == \"$cfg\"\r\n";
531             print OUT "\r\n";
532         } else {
533             print OUT "\r\n";
534             print OUT "!ELSEIF  \"\$(CFG)\" == \"$cfg\"\r\n";
535             print OUT "\r\n";
536         }
537
538         my $debug = ($cfg !~ /Release/);
539         my $msvc_headers = ($cfg =~ /MSVC Headers/);
540
541         print OUT "# PROP BASE Use_MFC 0\r\n";
542
543         if($debug) {
544             print OUT "# PROP BASE Use_Debug_Libraries 1\r\n";
545         } else {
546             print OUT "# PROP BASE Use_Debug_Libraries 0\r\n";
547         }
548
549         $output_dir = $cfg;
550         $output_dir =~ s/^$project - //;
551         $output_dir =~ s/ /_/g;
552         $output_dir =~ s/C\+\+/Cxx/g;
553         if($output_prefix_dir) {
554             $output_dir = "$output_prefix_dir\\$output_dir";
555         }
556
557         print OUT "# PROP BASE Output_Dir \"$output_dir\"\r\n";
558         print OUT "# PROP BASE Intermediate_Dir \"$output_dir\"\r\n";
559
560         print OUT "# PROP BASE Target_Dir \"\"\r\n";
561
562         print OUT "# PROP Use_MFC 0\r\n";
563         if($debug) {
564             print OUT "# PROP Use_Debug_Libraries 1\r\n";
565         } else {
566             print OUT "# PROP Use_Debug_Libraries 0\r\n";
567         }
568         print OUT "# PROP Output_Dir \"$output_dir\"\r\n";
569         print OUT "# PROP Intermediate_Dir \"$output_dir\"\r\n";
570
571         print OUT "# PROP Ignore_Export_Lib 0\r\n" if $dll;
572         print OUT "# PROP Target_Dir \"\"\r\n";
573
574         my @defines;
575         if($debug) {
576             if($lib || $exe) {
577                 print OUT "# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od";
578                 @defines = (qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 WIN32 _DEBUG _MBCS _LIB));
579             } else {
580                 print OUT "# ADD BASE CPP /nologo /MDd /W3 /Gm /GX /Zi /Od";
581                 @defines = (qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 WIN32 _DEBUG _WINDOWS _MBCS _USRDLL), ("\U${project}\E_EXPORTS"));
582             }
583         } else {
584             if($lib || $exe) {
585                 print OUT "# ADD BASE CPP /nologo /W3 /GX /O2";
586                 @defines = (qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 WIN32 NDEBUG _MBCS _LIB));
587             } else {
588                 print OUT "# ADD BASE CPP /nologo /MD /W3 /GX /O2";
589                 @defines = (qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 WIN32 NDEBUG _WINDOWS _MBCS _USRDLL), ("\U${project}\E_EXPORTS"));
590             }
591         }
592
593         foreach my $define (@defines) {
594             if ($define !~ /=/) {
595                 print OUT " /D \"$define\"";
596             } else {
597                 print OUT " /D $define";
598             }
599         }
600         print OUT " /YX" if $lib || $exe;
601         print OUT " /FD";
602         print OUT " /GZ" if $debug;
603         print OUT " /c";
604         print OUT "\r\n";
605
606         my @defines2 = qw(_CRT_SECURE_NO_DEPRECATE _CRT_NONSTDC_NO_DEPRECATE);
607         if($debug) {
608             if($lib) {
609                 print OUT "# ADD CPP /nologo /MDd /W3 /Gm /GX /Zi /Od";
610                 push @defines2, qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 WIN32 _DEBUG _WINDOWS _MBCS _LIB);
611             } else {
612                 print OUT "# ADD CPP /nologo /MDd /W3 /Gm /GX /Zi /Od";
613                 push @defines2, qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x700 _DEBUG WIN32 _WINDOWS _MBCS _USRDLL);
614             }
615         } else {
616             if($lib) {
617                 print OUT "# ADD CPP /nologo /MD /W3 /GX /O2";
618                 push @defines2, qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 WIN32 NDEBUG _WINDOWS _MBCS _LIB);
619             } else {
620                 print OUT "# ADD CPP /nologo /MD /W3 /GX /O2";
621                 push @defines2, qw(WINVER=0x0600 _WIN32_WINNT=0x0600 _WIN32_IE=0x0700 NDEBUG WIN32 _WINDOWS _MBCS _USRDLL);
622             }
623         }
624
625         my @includes = ();
626         if($wine) {
627             push @defines2, "_\U${project}\E_";
628             push @defines2, qw(__WINESRC__) if $project !~ /^(?:wine(?:build|test)|.*?_test)$/;
629             if ($msvc_headers) {
630                 push @defines2, qw(__WINE_USE_NATIVE_HEADERS);
631             }
632             my $output_dir2 = $output_dir;
633             $output_dir2 =~ s/\\/\\\\/g;
634             push @defines2, "__WINETEST_OUTPUT_DIR=\\\"$output_dir2\\\"";
635             push @defines2, qw(__i386__ _X86_);
636
637             if ($project =~ /_test$/) {
638                 push @includes, "$msvc_wine_dir\\$output_dir";
639             }
640
641             if (!$msvc_headers || $project eq "winetest") {
642                 push @includes, $wine_include_dir;
643             }
644         }
645
646         if($wine) {
647             foreach my $include (@includes) {
648                 print OUT " /I \"$include\"";
649             }
650         }
651
652         foreach my $define (@defines2) {
653             if ($define !~ /=/) {
654                 print OUT " /D \"$define\"";
655             } else {
656                 print OUT " /D $define";
657             }
658         }
659         print OUT " /D inline=__inline" if $wine;
660         print OUT " /D \"__STDC__\"" if 0 && $wine;
661
662         print OUT " /YX" if $lib;
663         print OUT " /FR" if !$lib;
664         print OUT " /FD";
665         print OUT " /GZ" if $debug;
666         print OUT " /c";
667         print OUT " /TP" if !$no_cpp;
668         print OUT "\r\n";
669
670         if($debug) {
671             print OUT "# SUBTRACT CPP /X /YX\r\n" if $dll;
672             print OUT "# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n" if $dll;
673             print OUT "# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /win32\r\n" if $dll;
674             print OUT "# ADD BASE RSC /l 0x41d /d \"_DEBUG\"\r\n";
675             print OUT "# ADD RSC /l 0x41d";
676             if($wine) {
677                 foreach my $include (@includes) {
678                     print OUT " /i \"$include\"";
679                 }
680             }
681             print OUT " /d \"_DEBUG\"\r\n";
682         } else {
683             print OUT "# SUBTRACT CPP /YX\r\n" if $dll;
684             print OUT "# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n" if $dll;
685             print OUT "# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /win32\r\n" if $dll;
686             print OUT "# ADD BASE RSC /l 0x41d /d \"NDEBUG\"\r\n";
687             print OUT "# ADD RSC /l 0x41d";
688             if($wine) {
689                 foreach my $include (@includes) {
690                     print OUT " /i \"$include\"";
691                 }
692             }
693             print OUT "/d \"NDEBUG\"\r\n";
694         }
695         print OUT "BSC32=bscmake.exe\r\n";
696         print OUT "# ADD BASE BSC32 /nologo\r\n";
697         print OUT "# ADD BSC32 /nologo\r\n";
698
699         if($exe || $dll) {
700             print OUT "LINK32=link.exe\r\n";
701             print OUT "# ADD BASE LINK32";
702             my @libraries = qw(kernel32.lib user32.lib gdi32.lib winspool.lib
703                                comdlg32.lib advapi32.lib shell32.lib ole32.lib
704                                oleaut32.lib uuid.lib odbc32.lib odbccp32.lib);
705             foreach my $library (@libraries) {
706                 print OUT " $library";
707             }
708             print OUT " /nologo";
709             print OUT " /dll" if $dll;
710             print OUT " /subsystem:console" if $console;
711             print OUT " /debug" if $debug;
712             print OUT " /machine:I386";
713             print OUT " /pdbtype:sept" if $debug;
714             print OUT "\r\n";
715
716             print OUT "# ADD LINK32";
717             print OUT " libcmt.lib" if $project =~ /^ntdll$/; # FIXME: Kludge
718             foreach my $import (@imports) {
719                 print OUT " $import.lib" if ($import ne "msvcrt");
720             }
721             print OUT " /nologo";
722             print OUT " /dll" if $dll;
723             print OUT " /subsystem:console" if $console;
724             print OUT " /debug" if $debug;
725             print OUT " /machine:I386";
726             print OUT " /nodefaultlib" if $project =~ /^ntdll$/; # FIXME: Kludge
727             print OUT " /def:\"$project.def\"" if $dll;
728             print OUT " /pdbtype:sept" if $debug;
729             print OUT "\r\n";
730         } else {
731             print OUT "LIB32=link.exe -lib\r\n";
732             print OUT "# ADD BASE LIB32 /nologo\r\n";
733             print OUT "# ADD LIB32 /nologo\r\n";
734         }
735
736         $n++;
737     }
738
739     if($#cfgs != 0) {
740         print OUT "\r\n";
741         print OUT "!ENDIF \r\n";
742         print OUT "\r\n";
743     }
744
745     print OUT "# Begin Target\r\n";
746     print OUT "\r\n";
747     foreach my $cfg (@cfgs) {
748         print OUT "# Name \"$cfg\"\r\n";
749     }
750
751     print OUT "# Begin Group \"Source Files\"\r\n";
752     print OUT "\r\n";
753     print OUT "# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\"\r\n";
754
755     if ($project eq "winebuild") {
756         for my $ source_file ("getopt.c", "getopt1.c", "mkstemps.c")
757         {
758             print OUT "# Begin Source File\r\n";
759             print OUT "\r\n";
760             print OUT "SOURCE=..\\..\\libs\\port\\$source_file\r\n";
761             print OUT "# End Source File\r\n";
762         }
763     }
764
765     foreach my $source_file (@source_files) {
766         $source_file =~ s%/%\\%g;
767         if($source_file !~ /^\./) {
768             $source_file = ".\\$source_file";
769         }
770
771         print OUT "# Begin Source File\r\n";
772         print OUT "\r\n";
773
774         print OUT "SOURCE=$source_file\r\n";
775
776         if($source_file =~ /^(.*?)\.spec$/) {
777             my $basename = $1;
778
779             my $spec_file = $source_file;
780             my $def_file = "$basename.def";
781
782             my $srcdir = "."; # FIXME: Is this really always correct?
783
784             print OUT "# Begin Custom Build\r\n";
785             print OUT "InputPath=$spec_file\r\n";
786             print OUT "\r\n";
787             print OUT "BuildCmds= \\\r\n";
788             print OUT "\t..\\..\\tools\\winebuild\\$output_dir\\winebuild.exe --def $spec_file > $def_file\r\n";
789             print OUT "\r\n";
790             print OUT "\"$def_file\" : \$(SOURCE) \"\$(INTDIR)\" \"\$(OUTDIR)\"\r\n";
791             print OUT "   \$(BuildCmds)\r\n";
792             print OUT "# End Custom Build\r\n";
793         } elsif($source_file =~ /([^\\]*?\.h)$/) {
794             my $h_file = $1;
795
796             foreach my $cfg (@cfgs) {
797                 if($#cfgs == 0) {
798                     # Nothing
799                 } elsif($n == 0) {
800                     print OUT "!IF  \"\$(CFG)\" == \"$cfg\"\r\n";
801                     print OUT "\r\n";
802                 } else {
803                     print OUT "\r\n";
804                     print OUT "!ELSEIF  \"\$(CFG)\" == \"$cfg\"\r\n";
805                     print OUT "\r\n";
806                 }
807
808                 $output_dir = $cfg;
809                 $output_dir =~ s/^$project - //;
810                 $output_dir =~ s/ /_/g;
811                 $output_dir =~ s/C\+\+/Cxx/g;
812                 if($output_prefix_dir) {
813                     $output_dir = "$output_prefix_dir\\$output_dir";
814                 }
815
816                 print OUT "# Begin Custom Build\r\n";
817                 print OUT "OutDir=$output_dir\r\n";
818                 print OUT "InputPath=$source_file\r\n";
819                 print OUT "\r\n";
820                 print OUT "\"\$(OutDir)\\wine\\$h_file\" : \$(SOURCE) \"\$(INTDIR)\" \"\$(OUTDIR)\"\r\n";
821                 print OUT "\tcopy \"\$(InputPath)\" \"\$(OutDir)\\wine\"\r\n";
822                 print OUT "\r\n";
823                 print OUT "# End Custom Build\r\n";
824             }
825
826             if($#cfgs != 0) {
827                 print OUT "\r\n";
828                 print OUT "!ENDIF \r\n";
829                 print OUT "\r\n";
830             }
831         }
832
833         print OUT "# End Source File\r\n";
834     }
835     print OUT "# End Group\r\n";
836
837     print OUT "# Begin Group \"Header Files\"\r\n";
838     print OUT "\r\n";
839     print OUT "# PROP Default_Filter \"h;hpp;hxx;hm;inl\"\r\n";
840     foreach my $header_file (@header_files) {
841         print OUT "# Begin Source File\r\n";
842         print OUT "\r\n";
843         print OUT "SOURCE=.\\$header_file\r\n";
844         print OUT "# End Source File\r\n";
845     }
846     print OUT "# End Group\r\n";
847
848
849
850     print OUT "# Begin Group \"Resource Files\"\r\n";
851     print OUT "\r\n";
852     print OUT "# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe\"\r\n";
853     foreach my $resource_file (@resource_files) {
854         print OUT "# Begin Source File\r\n";
855         print OUT "\r\n";
856         print OUT "SOURCE=.\\$resource_file\r\n";
857         print OUT "# End Source File\r\n";
858     }
859     print OUT "# End Group\r\n";
860
861     print OUT "# End Target\r\n";
862     print OUT "# End Project\r\n";
863
864     close(OUT);
865 }
866
867 sub _generate_dsw_header($) {
868     local *OUT = shift;
869
870     print OUT "Microsoft Developer Studio Workspace File, Format Version 6.00\r\n";
871     print OUT "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n";
872     print OUT "\r\n";
873 }
874
875 sub _generate_dsw_project($$$$) {
876     local *OUT = shift;
877
878     my $project = shift;
879     my $dsp_file = shift;
880     my @dependencies = @{(shift)};
881
882     $dsp_file = "./$dsp_file";
883     $dsp_file =~ y%/%\\%;
884     
885     @dependencies = sort(@dependencies);
886
887     print OUT "###############################################################################\r\n";
888     print OUT "\r\n";
889     print OUT "Project: \"$project\"=$dsp_file - Package Owner=<4>\r\n";
890     print OUT "\r\n";
891     print OUT "Package=<5>\r\n";
892     print OUT "{{{\r\n";
893     print OUT "}}}\r\n";
894     print OUT "\r\n";
895     print OUT "Package=<4>\r\n";
896     print OUT "{{{\r\n";
897     foreach my $dependency (@dependencies) {
898         print OUT "    Begin Project Dependency\r\n";
899         print OUT "    Project_Dep_Name $dependency\r\n";
900         print OUT "    End Project Dependency\r\n";
901     }
902     print OUT "}}}\r\n";
903     print OUT "\r\n";
904 }
905
906 sub _generate_dsw_footer($) {
907     local *OUT = shift;
908
909     print OUT "###############################################################################\r\n";
910     print OUT "\r\n";
911     print OUT "Global:\r\n";
912     print OUT "\r\n";
913     print OUT "Package=<5>\r\n";
914     print OUT "{{{\r\n";
915     print OUT "}}}\r\n";
916     print OUT "\r\n";
917     print OUT "Package=<3>\r\n";
918     print OUT "{{{\r\n";
919     print OUT "}}}\r\n";
920     print OUT "\r\n";
921     print OUT "###############################################################################\r\n";
922     print OUT "\r\n";
923 }
924
925 if ($options->wine) {
926     my $dsw_file = "wine.dsw";
927     $output->progress("$dsw_file");
928     replace_file("$wine_dir/$dsw_file", \&_generate_wine_dsw);
929 }
930
931 sub _generate_wine_dsw($) {
932     local *OUT = shift;
933
934     _generate_dsw_header(\*OUT);
935     foreach my $module (sort(keys(%modules))) {
936         next if $module =~ /(?:winetest\.lib|wineruntests\.exe|_test\.exe)$/;
937
938         my $project = $modules{$module}{project};
939         my $dsp_file = $modules{$module}{dsp_file};
940
941         my @dependencies;
942         if($project eq "wine") {
943             @dependencies = ();
944         } elsif($project eq "winebuild") {
945             @dependencies = ("wine");
946         } elsif($project =~ /^(?:gdi32)_.+?$/) {
947             @dependencies = ();
948         } else {
949             @dependencies = ("wine", "winebuild");
950         }
951
952         if($project =~ /^gdi32$/) {
953             foreach my $dir (@gdi32_dirs) {
954                 my $dir2 = $dir;
955                 $dir2 =~ s%^.*?/([^/]+)$%$1%;
956
957                 my $module = "gdi32_$dir2";
958                 $module =~ s%/%_%g;
959                 push @dependencies, $module;
960             }
961         }
962
963         _generate_dsw_project(\*OUT, $project, $dsp_file, \@dependencies);
964     }
965     _generate_dsw_footer(\*OUT);
966
967     return 1;
968 }
969
970 if ($options->winetest) {
971     my $dsw_file = "winetest.dsw";
972     $output->progress("$dsw_file");
973     replace_file("$wine_dir/$dsw_file", \&_generate_winetest_dsw);
974 }
975
976 sub _generate_winetest_dsw($) {
977     local *OUT = shift;
978
979     _generate_dsw_header(\*OUT);
980
981     my @runtests_dependencies = ();
982     foreach my $module (sort(keys(%modules))) {
983         next if $module !~ /(?:winetest\.lib|wineruntests\.exe|_test\.exe)$/;
984         next if $module eq "wineruntests";
985
986         my $project = $modules{$module}{project};
987
988         push @runtests_dependencies, $project;
989     }
990
991     foreach my $module (sort(keys(%modules))) {
992         next if $module !~ /(?:winetest\.lib|wineruntests\.exe|_test\.exe)$/;
993
994         my $project = $modules{$module}{project};
995         my $dsp_file = $modules{$module}{dsp_file};
996
997         my @dependencies;
998         if($project =~ /^winetest$/) {
999             @dependencies = ();
1000         } elsif($project =~ /^wineruntests$/) {
1001             @dependencies = @runtests_dependencies;
1002         } else {
1003             @dependencies = ("winetest");
1004         }
1005
1006         _generate_dsw_project(\*OUT, $project, $dsp_file, \@dependencies);
1007     }
1008
1009     _generate_dsw_footer(\*OUT);
1010 }
1011
1012 if ($options->winetest) {
1013     foreach my $module (sort(keys(%modules))) {
1014         next if $module !~ /_test\.exe$/;
1015
1016         my $project = $modules{$module}{project};
1017         my $dsp_file = $modules{$module}{dsp_file};
1018         my @tests = @{$modules{$module}{tests}};
1019
1020         my $testlist_c = $dsp_file;
1021         $testlist_c =~ s%[^/]*\.dsp$%testlist.c%;
1022
1023         replace_file("$wine_dir/$testlist_c", \&_generate_testlist_c, \@tests);
1024     }
1025 }
1026
1027 # ***** Keep in sync with tools/make_ctests *****
1028 sub _generate_testlist_c($$) {
1029     local *OUT = shift;
1030
1031     my @tests = @{(shift)};
1032
1033     print OUT "/* Automatically generated file; DO NOT EDIT!! */\n";
1034     print OUT "\n";
1035     print OUT "/* stdarg.h is needed for Winelib */\n";
1036     print OUT "#include <stdarg.h>\n";
1037     print OUT "#include <stdio.h>\n";
1038     print OUT "#include <stdlib.h>\n";
1039     print OUT "#include \"windef.h\"\n";
1040     print OUT "#include \"winbase.h\"\n";
1041     print OUT "\n";
1042     print OUT "#define STANDALONE\n";
1043     print OUT "#include \"wine/test.h\"\n";
1044     print OUT "\n";
1045     foreach my $test (@tests) {
1046         print OUT "extern void func_$test(void);\n";
1047     }
1048     print OUT "\n";
1049     print OUT "const struct test winetest_testlist[] =\n";
1050     print OUT "{\n";
1051     foreach my $test (@tests) {
1052         print OUT "    { \"$test\", func_$test },\n";
1053     }
1054     print OUT "    { 0, 0 }\n";
1055     print OUT "};\n";
1056 }
1057
1058 if ($options->winetest) {
1059     replace_file("$wine_dir/runtests.c", \&_generate_runtests_c);
1060 }
1061
1062 sub _generate_runtests_c($) {
1063     local *OUT = shift;
1064
1065     print OUT "/* Automatically generated file; DO NOT EDIT!! */\n";
1066
1067     print OUT "\n";
1068     print OUT "#include <stdio.h>\n";
1069     print OUT "#include <stdlib.h>\n";
1070     print OUT "\n";
1071
1072     print OUT "int main(int argc, char *argv[])\n";
1073     print OUT "{\n";
1074     print OUT "    char output_dir[] = __WINETEST_OUTPUT_DIR;\n";
1075     print OUT "    char command[4096];\n";
1076     print OUT "\n";
1077     foreach my $dsp_file (keys(%runtests)) {
1078         my @tests =  @{$runtests{$dsp_file}};
1079
1080         my $project = $dsp_file;
1081         $project =~ s%^(.*?)/?([^/]+)\.dsp$%$2%;
1082         my $dir = $1;
1083         $dir =~ s%/%\\\\%g; 
1084
1085         foreach my $test (@tests) {
1086             print OUT "    sprintf(command, \"$dir\\\\%s\\\\$project.exe $test\", output_dir);\n";
1087             print OUT "    system(command);\n";
1088             print OUT "\n";
1089         }
1090     }
1091     print OUT "    return 0;\n";
1092     print OUT "}\n";
1093 }
1094
1095 if ($options->winetest) {
1096     replace_file("$wine_dir/winetest.c", \&_generate_winetest_c);
1097 }
1098
1099 sub _generate_winetest_c($) {
1100     local *OUT = shift;
1101
1102     print OUT "/* Automatically generated file; DO NOT EDIT!! */\n\n";
1103
1104     print OUT "/* Force the linker to generate a .lib file */\n";
1105     print OUT "void __wine_dummy_lib_function(void)\n{\n}\n\n";
1106 }
1107
1108 if ($options->wine) {
1109     my $config_h = "include/config.h";
1110
1111     $output->progress("$config_h");
1112
1113     replace_file("$wine_dir/$config_h", \&_generate_config_h);
1114 }
1115
1116 sub _generate_config_h($) {
1117     local *OUT = shift;
1118
1119     print OUT "#define __WINE_CONFIG_H\n";
1120     print OUT "\n";
1121
1122     my @headers = qw(direct.h fcntl.h io.h string.h process.h);
1123     foreach my $header (@headers) {
1124         $header =~ y/\.\//__/;
1125         print OUT "#define HAVE_\U$header\E\n";
1126         print OUT "\n";
1127     }
1128
1129     my @functions = qw(
1130         _alldiv _allmul _allrem _aulldiv _aullrem
1131         _access _chdir _close _lseek _mkdir _open _pclose _popen _read _rmdir _write _stat
1132         _snprintf _spawnvp _stricmp _strnicmp _vsnprintf _wcsicmp
1133         ecvt fcvt gcvt
1134         memmove
1135         strerror
1136         wcslen
1137     );
1138     foreach my $function (@functions) {
1139         print OUT "#define HAVE_\U$function\E 1\n";
1140         print OUT "\n";
1141     }
1142
1143     if(0) {
1144         print OUT "#define NEED_STDCALL_DECORATION 1\n";
1145         print OUT "\n";
1146     }
1147
1148     print OUT "#define X_DISPLAY_MISSING 1\n";
1149     print OUT "\n";
1150
1151     print OUT "/* Define to a macro to generate an assembly function directive */\n";
1152     print OUT "#define __ASM_FUNC(name) \"\"\n";
1153     print OUT "\n";
1154
1155     print OUT "/* Define to a macro to generate an assembly name from a C symbol */\n";
1156     print OUT "#define __ASM_NAME(name) name\n";
1157     print OUT "\n";
1158
1159     print OUT "/* Define to the assembler keyword used to specify a word value */\n";
1160     print OUT "#define __ASM_SHORT \".short\"\n";
1161     print OUT "\n";
1162
1163     print OUT "/* Define to the assembler keyword used to specify an ASCII string */\n";
1164     print OUT "#define __ASM_STRING \".string\"\n";
1165     print OUT "\n";
1166
1167     print OUT "/* Define to the address where bug reports for this package should be sent. */\n";
1168     print OUT "#define PACKAGE_BUGREPORT \"\"\n";
1169     print OUT "\n";
1170
1171     print OUT "/* Define to the full name of this package. */\n";
1172     print OUT "#define PACKAGE_NAME \"Wine\"\n";
1173     print OUT "\n";
1174
1175     print OUT "/* Define to the full name and version of this package. */\n";
1176     print OUT "#define PACKAGE_STRING \"Wine YYYYMMDD\"\n";
1177     print OUT "\n";
1178
1179     print OUT "/* Define to the one symbol short name of this package. */\n";
1180     print OUT "#define PACKAGE_TARNAME \"wine\"\n";
1181     print OUT "\n";
1182
1183     print OUT "/* Define to the version of this package. */\n";
1184     print OUT "#define PACKAGE_VERSION \"YYYYMMDD\"\n";
1185     print OUT "\n";
1186
1187     close(OUT);
1188 }