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