wbemprox: Add stub dll.
[wine] / tools / winemaker
1 #!/usr/bin/perl -w
2 use strict;
3
4 # Copyright 2000-2004 Francois Gouget for CodeWeavers
5 # Copyright 2004 Dimitrie O. Paun
6 # Copyright 2009 AndrĂ© Hentschel
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #
22
23 my $version="0.7.1";
24
25 use Cwd;
26 use File::Basename;
27 use File::Copy;
28
29
30
31 #####
32 #
33 # Options
34 #
35 #####
36
37 # The following constants define what we do with the case of filenames
38
39 ##
40 # Never rename a file to lowercase
41 my $OPT_LOWER_NONE=0;
42
43 ##
44 # Rename all files to lowercase
45 my $OPT_LOWER_ALL=1;
46
47 ##
48 # Rename only files that are all uppercase to lowercase
49 my $OPT_LOWER_UPPERCASE=2;
50
51
52 # The following constants define whether to ask questions or not
53
54 ##
55 # No (synonym of never)
56 my $OPT_ASK_NO=0;
57
58 ##
59 # Yes (always)
60 my $OPT_ASK_YES=1;
61
62 ##
63 # Skip the questions till the end of this scope
64 my $OPT_ASK_SKIP=-1;
65
66
67 # The following constants define the architecture
68
69 ##
70 # 32-Bit Target
71 my $OPT_ARCH_32=32;
72
73 ##
74 # 64-Bit Target
75 my $OPT_ARCH_64=64;
76
77
78 # General options
79
80 ##
81 # This is the directory in which winemaker will operate.
82 my $opt_work_dir;
83
84 ##
85 # This is the file in which winemaker will operate if a project file is specified.
86 my $opt_work_file;
87
88 ##
89 # Make a backup of the files
90 my $opt_backup;
91
92 ##
93 # Defines which files to rename
94 my $opt_lower;
95
96 ##
97 # If we don't find the file referenced by an include, lower it
98 my $opt_lower_include;
99
100 ##
101 # If true then winemaker should not attempt to fix the source.  This is
102 # useful if the source is known to be already in a suitable form and is
103 # readonly
104 my $opt_no_source_fix;
105
106 # Options for the 'Source' method
107
108 ##
109 # Specifies that we have only one target so that all sources relate
110 # to this target. By default this variable is left undefined which
111 # means winemaker should try to find out by itself what the targets
112 # are. If not undefined then this contains the name of the default
113 # target (without the extension).
114 my $opt_single_target;
115
116 ##
117 # If '$opt_single_target' has been specified then this is the type of
118 # that target. Otherwise it specifies whether the default target type
119 # is guiexe or cuiexe.
120 my $opt_target_type;
121
122 ##
123 # Contains the default set of flags to be used when creating a new target.
124 my $opt_flags;
125
126 ##
127 # Contains 32 for 32-Bit-Targets and 64 for 64-Bit-Targets
128 my $opt_arch;
129
130 ##
131 # If true then winemaker should ask questions to the user as it goes
132 # along.
133 my $opt_is_interactive;
134 my $opt_ask_project_options;
135 my $opt_ask_target_options;
136
137 ##
138 # If false then winemaker should not generate the makefiles.
139 my $opt_no_generated_files;
140
141 ##
142 # Specifies not to print the banner if set.
143 my $opt_no_banner;
144
145
146
147 #####
148 #
149 # Target modelization
150 #
151 #####
152
153 # The description of a target is stored in an array. The constants
154 # below identify what is stored at each index of the array.
155
156 ##
157 # This is the name of the target.
158 my $T_NAME=0;
159
160 ##
161 # Defines the type of target we want to build. See the TT_xxx
162 # constants below
163 my $T_TYPE=1;
164
165 ##
166 # This is a bitfield containing flags refining the way the target
167 # should be handled. See the TF_xxx constants below
168 my $T_FLAGS=2;
169
170 ##
171 # This is a reference to an array containing the list of the
172 # resp. C, C++, RC, other (.h, .hxx, etc.) source files.
173 my $T_SOURCES_C=3;
174 my $T_SOURCES_CXX=4;
175 my $T_SOURCES_RC=5;
176 my $T_SOURCES_MISC=6;
177
178 ##
179 # This is a reference to an array containing the list of
180 # C compiler options
181 my $T_CEXTRA=7;
182
183 ##
184 # This is a reference to an array containing the list of
185 # C++ compiler options
186 my $T_CXXEXTRA=8;
187
188 ##
189 # This is a reference to an array containing the list of
190 # RC compiler options
191 my $T_RCEXTRA=9;
192
193 ##
194 # This is a reference to an array containing the list of macro
195 # definitions
196 my $T_DEFINES=10;
197
198 ##
199 # This is a reference to an array containing the list of directory
200 # names that constitute the include path
201 my $T_INCLUDE_PATH=11;
202
203 ##
204 # Flags for the linker
205 my $T_LDFLAGS=12;
206
207 ##
208 # Same as T_INCLUDE_PATH but for the dll search path
209 my $T_DLL_PATH=13;
210
211 ##
212 # The list of Windows dlls to import
213 my $T_DLLS=14;
214
215 ##
216 # Same as T_INCLUDE_PATH but for the library search path
217 my $T_LIBRARY_PATH=15;
218
219 ##
220 # The list of Unix libraries to link with
221 my $T_LIBRARIES=16;
222
223 ##
224 # The list of dependencies between targets
225 my $T_DEPENDS=17;
226
227
228 # The following constants define the recognized types of target
229
230 ##
231 # This is not a real target. This type of target is used to collect
232 # the sources that don't seem to belong to any other target. Thus no
233 # real target is generated for them, we just put the sources of the
234 # fake target in the global source list.
235 my $TT_SETTINGS=0;
236
237 ##
238 # For executables in the windows subsystem
239 my $TT_GUIEXE=1;
240
241 ##
242 # For executables in the console subsystem
243 my $TT_CUIEXE=2;
244
245 ##
246 # For dynamically linked libraries
247 my $TT_DLL=3;
248
249
250 # The following constants further refine how the target should be handled
251
252 ##
253 # This target is an MFC-based target
254 my $TF_MFC=4;
255
256 ##
257 # User has specified --nomfc option for this target or globally
258 my $TF_NOMFC=8;
259
260 ##
261 # --nodlls option: Do not use standard DLL set
262 my $TF_NODLLS=16;
263
264 ##
265 # --nomsvcrt option: Do not link with msvcrt
266 my $TF_NOMSVCRT=32;
267
268 ##
269 # Initialize a target:
270 # - set the target type to TT_SETTINGS, i.e. no real target will
271 #   be generated.
272 sub target_init($)
273 {
274   my $target=$_[0];
275
276   @$target[$T_TYPE]=$TT_SETTINGS;
277   # leaving $T_INIT undefined
278   @$target[$T_FLAGS]=$opt_flags;
279   @$target[$T_SOURCES_C]=[];
280   @$target[$T_SOURCES_CXX]=[];
281   @$target[$T_SOURCES_RC]=[];
282   @$target[$T_SOURCES_MISC]=[];
283   @$target[$T_CEXTRA]=[];
284   @$target[$T_CXXEXTRA]=[];
285   @$target[$T_RCEXTRA]=[];
286   @$target[$T_DEFINES]=[];
287   @$target[$T_INCLUDE_PATH]=[];
288   @$target[$T_LDFLAGS]=[];
289   @$target[$T_DLL_PATH]=[];
290   @$target[$T_DLLS]=[];
291   @$target[$T_LIBRARY_PATH]=[];
292   @$target[$T_LIBRARIES]=[];
293 }
294
295
296
297 #####
298 #
299 # Project modelization
300 #
301 #####
302
303 # First we have the notion of project. A project is described by an
304 # array (since we don't have structs in perl). The constants below
305 # identify what is stored at each index of the array.
306
307 ##
308 # This is the path in which this project is located. In other
309 # words, this is the path to  the Makefile.
310 my $P_PATH=0;
311
312 ##
313 # This index contains a reference to an array containing the project-wide
314 # settings. The structure of that arrray is actually identical to that of
315 # a regular target since it can also contain extra sources.
316 my $P_SETTINGS=1;
317
318 ##
319 # This index contains a reference to an array of targets for this
320 # project. Each target describes how an executable or library is to
321 # be built. For each target this description takes the same form as
322 # that of the project: an array. So this entry is an array of arrays.
323 my $P_TARGETS=2;
324
325 ##
326 # Initialize a project:
327 # - set the project's path
328 # - initialize the target list
329 # - create a default target (will be removed later if unnecessary)
330 sub project_init($$$)
331 {
332   my ($project, $path, $global_settings)=@_;
333
334   my $project_settings=[];
335   target_init($project_settings);
336   @$project_settings[$T_DEFINES]=[@{@$global_settings[$T_DEFINES]}];
337   @$project_settings[$T_INCLUDE_PATH]=[@{@$global_settings[$T_INCLUDE_PATH]}];
338   @$project_settings[$T_DLL_PATH]=[@{@$global_settings[$T_DLL_PATH]}];
339   @$project_settings[$T_DLLS]=[@{@$global_settings[$T_DLLS]}];
340   @$project_settings[$T_LIBRARY_PATH]=[@{@$global_settings[$T_LIBRARY_PATH]}];
341   @$project_settings[$T_LIBRARIES]=[@{@$global_settings[$T_LIBRARIES]}];
342
343   @$project[$P_PATH]=$path;
344   @$project[$P_SETTINGS]=$project_settings;
345   @$project[$P_TARGETS]=[];
346 }
347
348
349
350 #####
351 #
352 # Global variables
353 #
354 #####
355
356 my %warnings;
357
358 my %templates;
359
360 ##
361 # This maps a directory name to a reference to an array listing
362 # its contents (files and directories)
363 my %directories;
364
365 ##
366 # Contains the list of all projects. This list tells us what are
367 # the subprojects of the main Makefile and where we have to generate
368 # Makefiles.
369 my @projects=();
370
371 ##
372 # This is the main project, i.e. the one in the "." directory.
373 # It may well be empty in which case the main Makefile will only
374 # call out subprojects.
375 my @main_project;
376
377 ##
378 # Contains the defaults for the include path, etc.
379 # We store the defaults as if this were a target except that we only
380 # exploit the defines, include path, library path, library list and misc
381 # sources fields.
382 my @global_settings;
383
384
385
386 #####
387 #
388 # Utility functions
389 #
390 #####
391
392 ##
393 # Cleans up a name to make it an acceptable Makefile
394 # variable name.
395 sub canonize($)
396 {
397   my $name=$_[0];
398
399   $name =~ tr/a-zA-Z0-9_/_/c;
400   return $name;
401 }
402
403 ##
404 # Returns true is the specified pathname is absolute.
405 # Note: pathnames that start with a variable '$' or
406 # '~' are considered absolute.
407 sub is_absolute($)
408 {
409   my $path=$_[0];
410
411   return ($path =~ /^[\/~\$]/);
412 }
413
414 ##
415 # Retrieves the contents of the specified directory.
416 # We either get it from the directories hashtable which acts as a
417 # cache, or use opendir, readdir, closedir and store the result
418 # in the hashtable.
419 sub get_directory_contents($)
420 {
421   my $dirname=$_[0];
422   my $directory;
423
424   #print "getting the contents of $dirname\n";
425
426   # check for a cached version
427   $dirname =~ s+/$++;
428   if ($dirname eq "") {
429     $dirname=cwd;
430   }
431   $directory=$directories{$dirname};
432   if (defined $directory) {
433     #print "->@$directory\n";
434     return $directory;
435   }
436
437   # Read this directory
438   if (opendir(DIRECTORY, "$dirname")) {
439     my @files=readdir DIRECTORY;
440     closedir(DIRECTORY);
441     $directory=\@files;
442   } else {
443     # Return an empty list
444     #print "error: cannot open $dirname\n";
445     my @files;
446     $directory=\@files;
447   }
448   #print "->@$directory\n";
449   $directories{$dirname}=$directory;
450   return $directory;
451 }
452
453 ##
454 # Removes a directory from the cache.
455 # This is needed if one of its files or subdirectory has been renamed.
456 sub clear_directory_cache($)
457 {
458     my ($dirname)=@_;
459     delete $directories{$dirname};
460 }
461
462
463 #####
464 #
465 # 'Source'-based Project analysis
466 #
467 #####
468
469 ##
470 # Allows the user to specify makefile and target specific options
471 # - target: the structure in which to store the results
472 # - options: the string containing the options
473 sub source_set_options($$)
474 {
475   my $target=$_[0];
476   my $options=$_[1];
477
478   #FIXME: we must deal with escaping of stuff and all
479   foreach my $option (split / /,$options) {
480     if (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-D/) {
481       push @{@$target[$T_DEFINES]},$option;
482     } elsif (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-I/) {
483       push @{@$target[$T_INCLUDE_PATH]},$option;
484     } elsif ($option =~ /^-P/) {
485       push @{@$target[$T_DLL_PATH]},"-L$'";
486     } elsif ($option =~ /^-i/) {
487       push @{@$target[$T_DLLS]},"$'";
488     } elsif ($option =~ /^-L/) {
489       push @{@$target[$T_LIBRARY_PATH]},$option;
490     } elsif ($option =~ /^-l/) {
491       push @{@$target[$T_LIBRARIES]},"$'";
492     } elsif ($option =~ /^--mfc/) {
493       @$target[$T_FLAGS]|=$TF_MFC;
494       @$target[$T_FLAGS]&=~$TF_NOMFC;
495     } elsif ($option =~ /^--nomfc/) {
496       @$target[$T_FLAGS]&=~$TF_MFC;
497       @$target[$T_FLAGS]|=$TF_NOMFC;
498     } elsif ($option =~ /^--nodlls/) {
499       @$target[$T_FLAGS]|=$TF_NODLLS;
500     } elsif ($option =~ /^--nomsvcrt/) {
501       @$target[$T_FLAGS]|=$TF_NOMSVCRT;
502     } else {
503       print STDERR "error: unknown option \"$option\"\n";
504       return 0;
505     }
506   }
507   return 1;
508 }
509
510 ##
511 # Scans the specified project file to:
512 # - get a list of targets for this project
513 # - get some settings
514 # - get the list of source files
515 sub source_scan_project_file($$$);
516 sub source_scan_project_file($$$)
517 {
518     # a reference to the parent's project
519     my $parent_project=$_[0];
520     # 0 if it is a single project, 1 if it is part of a workspace
521     my $is_sub_project=$_[1];
522     # the name of the project file, with complete path, or without if in
523     # the same directory
524     my $filename=$_[2];
525
526     # reference to the project for this file. May not be used
527     my $project;
528     # list of targets found in the current file
529     my %targets;
530     # list of sources found in the current file
531     my @sources_c=();
532     my @sources_cxx=();
533     my @sources_rc=();
534     my @sources_misc=();
535     # some more settings
536     my $path=dirname($filename);
537     my $prj_target_cflags;
538     my $prj_target_ldflags;
539     my $prj_target_libs;
540     my $prj_name;
541     my $found_cfg=0;
542     my $prj_cfg;
543     my $prj_target_type=1;
544     my @prj_target_options;
545
546     if (!($path=~/\/$/)) {
547         $path.="/";
548     }
549
550     if (defined $opt_single_target or $is_sub_project == 0) {
551         # Either there is a single target and thus a single project,
552         # or we are a single project-file for which a project
553         # already exists
554         $project=$parent_project;
555     } else {
556         $project=[];
557         project_init($project, $path, \@global_settings);
558     }
559     my $project_settings=@$project[$P_SETTINGS];
560
561     if ($filename =~ /.dsp$/i) {
562         # First find out what this project file contains:
563         # collect all sources, find targets and settings
564         if (!open(FILEI,$filename)) {
565             print STDERR "error: unable to open $filename for reading:\n";
566             print STDERR "       $!\n";
567             return;
568         }
569         my $sfilet;
570         while (<FILEI>) {
571             # Remove any trailing CtrlZ, which isn't strictly in the file
572             if (/\x1A/) {
573                 s/\x1A//;
574                 last if (/^$/)
575             }
576
577             # Remove any trailing CrLf
578             s/\r\n$/\n/;
579             if (!/\n$/) {
580                 # Make sure all lines are '\n' terminated
581                 $_ .= "\n";
582             }
583
584             if (/^\# Microsoft Developer Studio Project File - Name=\"([^\"]+)/) {
585                 $prj_name="$1.exe";
586                 $targets{$prj_name}=1;
587                 #print $prj_name;
588                 next;
589             } elsif (/^# TARGTYPE/) {
590                 if (/[[:space:]]0x0101$/) {
591                     # Win32 (x86) Application
592                     $prj_target_type=1;
593                 }elsif (/[[:space:]]0x0102$/) {
594                     # Win32 (x86) Dynamic-Link Library
595                     $prj_target_type=3;
596                 }elsif (/[[:space:]]0x0103$/) {
597                     # Win32 (x86) Console Application
598                     $prj_target_type=2;
599                 }elsif (/[[:space:]]0x0104$/) {
600                     # Win32 (x86) Static Library
601                 }
602                 next;
603             } elsif (/^# ADD CPP(.*)/ && $found_cfg==1) {
604                 $prj_target_cflags=$1;
605                 @prj_target_options=split(" /", $prj_target_cflags);
606                 $prj_target_cflags="";
607                 foreach ( @prj_target_options ) {
608                     if ($_ eq "") {
609                         # empty
610                     } elsif (/nologo/) {
611                         # Suppress Startup Banner and Information Messages
612                     } elsif (/^W0$/) {
613                         # Turns off all warning messages
614                         $prj_target_cflags.="-w ";
615                     } elsif (/^W[123]$/) {
616                         # Warning Level
617                         $prj_target_cflags.="-W ";
618                     } elsif (/^W4$/) {
619                         # Warning Level
620                         $prj_target_cflags.="-Wall ";
621                     } elsif (/^WX$/) {
622                         # Warnings As Errors
623                         $prj_target_cflags.="-Werror ";
624                     } elsif (/^Gm$/) {
625                         # Enable Minimal Rebuild
626                     } elsif (/^GX$/) {
627                         # Enable Exception Handling
628                         $prj_target_cflags.="-fexceptions ";
629                     } elsif (/^Z[d7iI]$/) {
630                         # Debug Info
631                         $prj_target_cflags.="-g ";
632                     } elsif (/^Od$/) {
633                         # Disable Optimizations
634                         $prj_target_cflags.="-O0 ";
635                     } elsif (/^O1$/) {
636                         # Minimize Size
637                         $prj_target_cflags.="-Os ";
638                     } elsif (/^O2$/) {
639                         # Maximize Speed
640                         $prj_target_cflags.="-O2 ";
641                     } elsif (/^Ob0$/) {
642                         # Disables inline Expansion
643                         $prj_target_cflags.="-fno-inline ";
644                     } elsif (/^Ob1$/) {
645                         #In-line Function Expansion
646                         $prj_target_cflags.="-finline-functions ";
647                     } elsif (/^Ob2$/) {
648                         # auto In-line Function Expansion
649                         $prj_target_cflags.="-finline-functions ";
650                     } elsif (/^Ox$/) {
651                         # Use maximum optimization
652                         $prj_target_cflags.="-O3 ";
653                     } elsif (/^Oy$/) {
654                         # Frame-Pointer Omission
655                         $prj_target_cflags.="-fomit-frame-pointer ";
656                     } elsif (/^Oy-$/) {
657                         # Frame-Pointer Omission
658                         $prj_target_cflags.="-fno-omit-frame-pointer ";
659                     } elsif (/^GZ$/) {
660                         # Catch Release-Build Errors in Debug Build
661                     } elsif (/^M[DLT]d?$/) {
662                         # Use Multithreaded Run-Time Library
663                     } elsif (/^D\s*\"(.*)\"/) {
664                         # Preprocessor Definitions
665                         $prj_target_cflags.="-D".$1." ";
666                     } elsif (/^I/) {
667                         # Additional Include Directories
668                         #$prj_target_cflags.="-I" fixpath(option)
669                     } elsif (/^U\s*\"(.*)\"/) {
670                         # Undefines a previously defined symbol
671                         $prj_target_cflags.="-U".$1." ";
672                     } elsif (/^Fp/) {
673                         # Name .PCH File
674                     } elsif (/^F[Rr]/) {
675                         # Create .SBR File
676                     } elsif (/^YX$/) {
677                         # Automatic Use of Precompiled Headers
678                     } elsif (/^FD$/) {
679                         # Generate File Dependencies
680                     } elsif (/^c$/) {
681                         # Compile Without Linking
682                         # this option is always present and is already specified in the suffix rules
683                     } elsif (/^GB$/) {
684                         # Blend Optimization
685                         $prj_target_cflags.="-mcpu=pentiumpro -D_M_IX86=500 ";
686                     } elsif (/^G6$/) {
687                         # Pentium Pro Optimization
688                         $prj_target_cflags.="-march=pentiumpro -D_M_IX86=600 ";
689                     } elsif (/^G5$/) {
690                         # Pentium Optimization
691                         $prj_target_cflags.="-mcpu=pentium -D_M_IX86=500 ";
692                     } elsif (/^G3$/) {
693                         # 80386 Optimization
694                         $prj_target_cflags.="-mcpu=i386 -D_M_IX86=300 ";
695                     } elsif (/^G4$/) {
696                         # 80486 Optimization
697                         $prj_target_cflags.="-mcpu=i486 -D_M_IX86=400 ";
698                     } elsif (/^Yc/) {
699                         # Create Precompiled Header
700                     } elsif (/^Yu/) {
701                         # Use Precompiled Header
702                     } elsif (/^Za$/) {
703                         # Disable Language Extensions
704                         $prj_target_cflags.="-ansi ";
705                     } elsif (/^Ze$/) {
706                         # Enable Microsoft Extensions
707                     } elsif (/^Zm[[:digit:]]+$/) {
708                         # Specify Memory Allocation Limit
709                     } elsif (/^Zp1?$/) {
710                         # Packs structures on 1-byte boundaries
711                         $prj_target_cflags.="-fpack-struct ";
712                     } elsif (/^Zp(2|4|8|16)$/) {
713                         # Struct Member Alignment
714                         $prj_target_cflags.="-fpack-struct=".$1;
715                     } else {
716                         print "C compiler option $_ not implemented\n";
717                     }
718                 }
719
720                 #print "\nOptions: $prj_target_cflags\n";
721                 next;
722             } elsif (/^# ADD LINK32(.*)/ && $found_cfg==1) {
723                 $prj_target_ldflags=$1;
724                 @prj_target_options=split(" /", $prj_target_ldflags);
725                 $prj_target_ldflags="";
726                 $prj_target_libs=$prj_target_options[0];
727                 #print "\n$prj_target_libs bevor\n";
728                 $prj_target_libs=~s/\\/\//g;
729                 $prj_target_libs=~s/\.lib//g;
730                 $prj_target_libs=~s/\s+/ -l/g;
731                 #print "\n$prj_target_libs after\n";
732                 shift (@prj_target_options);
733                 foreach ( @prj_target_options ) {
734                     if ($_ eq "") {
735                         # empty
736                     } elsif (/^base:(.*)/) {
737                         # Base Address
738                         $prj_target_ldflags.="--image-base ".$1." ";
739                     } elsif (/^debug$/) {
740                         # Generate Debug Info
741                     } elsif (/^dll$/) {
742                         # Build a DLL
743                         $prj_target_type=3;
744                     } elsif (/^incremental:[[:alpha:]]+$/) {
745                         # Link Incrmentally
746                     } elsif (/^implib:/) {
747                         # Name import library
748                     } elsif (/^libpath:\"(.*)\"/) {
749                         # Additional Libpath
750                         push @{@$project_settings[$T_DLL_PATH]},"-L$1";
751                     } elsif (/^machine:[[:alnum:]]+$/) {
752                         # Specify Target Platform
753                     } elsif (/^map/) {
754                         # Generate Mapfile
755                         if (/^map:(.*)/) {
756                             $prj_target_ldflags.="-Map ".$1." ";
757                         } else {
758                             $prj_target_ldflags.="-Map ".$prj_name.".map ";
759                         }
760                     } elsif (/^nologo$/) {
761                         # Suppress Startup Banner and Information Messages
762                     } elsif (/^out:/) {
763                         # Output File Name
764                         # may use it as Target?
765                     } elsif (/^pdbtype:/) {
766                         # Program Database Storage
767                     } elsif (/^subsystem:/) {
768                         # Specify Subsystem
769                     } elsif (/^version:[[:digit:].]+$/) {
770                         # Version Information
771                     } else {
772                         print "Linker option $_ not implemented\n";
773                     }
774                 }
775                 next;
776             } elsif (/^LIB32=/ && $found_cfg==1) {
777                 #$libflag = 1;
778                 next;
779             } elsif (/^SOURCE=(.*)$/) {
780                 my @components=split /[\/\\]+/, $1;
781                 $sfilet=search_from($path, \@components);
782                 if ($sfilet =~ /\.(exe|dll)$/i) {
783                     $targets{$sfilet}=1;
784                 } elsif ($sfilet =~ /\.c$/i and $sfilet !~ /\.(dbg|spec)\.c$/) {
785                     push @sources_c,$sfilet;
786                 } elsif ($sfilet =~ /\.(cpp|cxx)$/i) {
787                     if ($sfilet =~ /^stdafx.cpp$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
788                         push @sources_misc,$sfilet;
789                         @$project_settings[$T_FLAGS]|=$TF_MFC;
790                     } else {
791                         push @sources_cxx,$sfilet;
792                     }
793                 } elsif ($sfilet =~ /\.rc$/i) {
794                     push @sources_rc,$sfilet;
795                 } elsif ($sfilet =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
796                     push @sources_misc,$sfilet;
797                     if ($sfilet =~ /^stdafx.h$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
798                         @$project_settings[$T_FLAGS]|=$TF_MFC;
799                     }
800                 }
801                 next;
802
803             } elsif (/^# (Begin|End) Source File/) {
804                 # Source-Files already handled
805                 next;
806             } elsif (/^# (Begin|End) Group/) {
807                 # Groups are ignored
808                 next;
809             } elsif (/^# (Begin|End) Custom Build/) {
810                 # Custom Builds are ignored
811                 next;
812             } elsif (/^# ADD LIB32 /) {
813                 #"ARFLAGS=rus"
814                 next;
815             } elsif (/^# Begin Target$/) {
816                 # Targets are ignored
817                 next;
818             } elsif (/^# End Target$/) {
819                 # Targets are ignored
820                 next;
821             } elsif (/^!/) {
822                 if ($found_cfg == 1) {
823                     $found_cfg=0;
824                 }
825                 if (/if (.*)\(CFG\)" == "(.*)"/i) {
826                     if ($2 eq $prj_cfg) {
827                         $found_cfg=1;
828                     }
829                 }
830                 next;
831             } elsif (/^CFG=(.*)/i) {
832                 $prj_cfg=$1;
833                 next;
834             }
835                 else { # Line recognized
836                 # print "|\n";
837             }
838         }
839         close(FILEI);
840
841         push @{@$project_settings[$T_LIBRARIES]},$prj_target_libs;
842         push @{@$project_settings[$T_CEXTRA]},$prj_target_cflags;
843         push @{@$project_settings[$T_CXXEXTRA]},$prj_target_cflags;
844         push @{@$project_settings[$T_LDFLAGS]},$prj_target_ldflags;
845     } elsif ($filename =~ /.vcproj$/i) {
846         # Import des Moduls XML::Simple
847         use XML::Simple;
848
849         my $project_xml = XMLin($filename, forcearray=>1);
850
851         $targets{$project_xml->{'Name'}.".exe"}=1;
852         my $sfilet;
853         for my $vc_files (@{$project_xml->{'Files'}}) {
854             for my $vc_filter (@{$vc_files->{'Filter'}}) {
855                 for my $vc_file (@{$vc_filter->{'File'}}) {
856                     $sfilet=$vc_file->{'RelativePath'};
857                     $sfilet=~s/\\\\/\\/g; #remove double backslash
858                     $sfilet=~s/^\.\\//; #remove starting 'this directory'
859                     $sfilet=~s/\\/\//g; #make slashes out of backslashes
860                     if ($sfilet =~ /\.(exe|dll)$/i) {
861                         $targets{$sfilet}=1;
862                     } elsif ($sfilet =~ /\.c$/i and $sfilet !~ /\.(dbg|spec)\.c$/) {
863                         push @sources_c,$sfilet;
864                     } elsif ($sfilet =~ /\.(cpp|cxx)$/i) {
865                         if ($sfilet =~ /^stdafx.cpp$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
866                             push @sources_misc,$sfilet;
867                             @$project_settings[$T_FLAGS]|=$TF_MFC;
868                         } else {
869                             push @sources_cxx,$sfilet;
870                         }
871                     } elsif ($sfilet =~ /\.rc$/i) {
872                         push @sources_rc,$sfilet;
873                     } elsif ($sfilet =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
874                         push @sources_misc,$sfilet;
875                         if ($sfilet =~ /^stdafx.h$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
876                             @$project_settings[$T_FLAGS]|=$TF_MFC;
877                         }
878                     }
879                 }
880             }
881         }
882         $prj_target_cflags="";
883         for my $vc_configurations (@{$project_xml->{'Configurations'}}) {
884             for my $vc_configuration (@{$vc_configurations->{'Configuration'}}) {
885                 for my $vc_tool (@{$vc_configuration->{'Tool'}}) {
886                     if ($vc_tool->{'Name'} ne 'VCCLCompilerTool') { next; }
887                     if (defined $vc_tool->{'Optimization'}) {$prj_target_cflags.="-O".$vc_tool->{'Optimization'}." ";}
888                     if (defined $vc_tool->{'WarningLevel'}) {
889                         if ($vc_tool->{'WarningLevel'}==0) {
890                             $prj_target_cflags.="-w ";
891                         } elsif ($vc_tool->{'WarningLevel'}<4) {
892                             $prj_target_cflags.="-W ";
893                         } elsif ($vc_tool->{'WarningLevel'}==4) {
894                             $prj_target_cflags.="-Wall ";
895                         } elsif ($vc_tool->{'WarningLevel'} eq "X") {
896                             $prj_target_cflags.="-Werror ";
897                         }
898                     }
899                     if (defined $vc_tool->{'PreprocessorDefinitions'}) {
900                         $vc_tool->{'PreprocessorDefinitions'}=~s/;/ -D/g;
901                         $prj_target_cflags.="-D".$vc_tool->{'PreprocessorDefinitions'}." ";
902                     }
903                     if (defined $vc_tool->{'AdditionalIncludeDirectories'}) {
904                         $vc_tool->{'AdditionalIncludeDirectories'}=~s/\\/\//g;
905                         $vc_tool->{'AdditionalIncludeDirectories'}=~s/;/ -I/g;
906                         push @{@$project_settings[$T_INCLUDE_PATH]},"-I".$vc_tool->{'AdditionalIncludeDirectories'};
907                     }
908                 }
909                 last;
910             }
911         }
912         push @{@$project_settings[$T_CEXTRA]},$prj_target_cflags;
913         push @{@$project_settings[$T_CXXEXTRA]},$prj_target_cflags;
914     }
915
916     my $target_count;
917     $target_count=keys %targets;
918
919
920     # Add this project to the project list, except for
921     # the main project which is already in the list.
922     if ($is_sub_project == 1) {
923         push @projects,$project;
924     }
925
926     # Ask for project-wide options
927     if ($opt_ask_project_options == $OPT_ASK_YES) {
928         my $flag_desc="";
929         if ((@$project_settings[$T_FLAGS] & $TF_MFC)!=0) {
930             $flag_desc="mfc";
931         }
932         print "* Type any project-wide options (-D/-I/-P/-i/-L/-l/--mfc),\n";
933         if (defined $flag_desc) {
934             print "* (currently $flag_desc)\n";
935         }
936         print "* or 'skip' to skip the target specific options,\n";
937         print "* or 'never' to not be asked this question again:\n";
938         while (1) {
939             my $options=<STDIN>;
940             chomp $options;
941             if ($options eq "skip") {
942                 $opt_ask_target_options=$OPT_ASK_SKIP;
943                 last;
944             } elsif ($options eq "never") {
945                 $opt_ask_project_options=$OPT_ASK_NO;
946                 last;
947             } elsif (source_set_options($project_settings,$options)) {
948                 last;
949             }
950             print "Please re-enter the options:\n";
951         }
952     }
953
954     # - Create the targets
955     # - Check if we have both libraries and programs
956     # - Match each target with source files (sort in reverse
957     #   alphabetical order to get the longest matches first)
958     my @local_dlls=();
959     my @local_depends=();
960     my @exe_list=();
961     foreach my $target_name (map (lc, (sort { $b cmp $a } keys %targets))) {
962         # Create the target...
963         my $target=[];
964         target_init($target);
965         @$target[$T_NAME]=$target_name;
966         @$target[$T_FLAGS]|=@$project_settings[$T_FLAGS];
967         if ($target_name =~ /\.dll$/) {
968             @$target[$T_TYPE]=$TT_DLL;
969             push @local_depends,"$target_name.so";
970             push @local_dlls,$target_name;
971             my $canon=canonize($target_name);
972             push @{@$target[$T_LDFLAGS]},("-shared","\$(${canon}_MODULE:%=%.spec)");
973         } else {
974             @$target[$T_TYPE]=$opt_target_type;
975             push @exe_list,$target;
976             push @{@$target[$T_LDFLAGS]},(@$target[$T_TYPE] == $TT_CUIEXE ? "-mconsole" : "-mwindows");
977         }
978         my $basename=$target_name;
979         $basename=~ s/\.(dll|exe)$//i;
980         # This is the default link list of Visual Studio
981         my @std_imports=qw(odbc32 ole32 oleaut32 winspool odbccp32);
982         my @std_libraries=qw(uuid);
983         if ((@$target[$T_FLAGS] & $TF_NODLLS) == 0) {
984             @$target[$T_DLLS]=\@std_imports;
985             @$target[$T_LIBRARIES]=\@std_libraries;
986         } else {
987             @$target[$T_DLLS]=[];
988             @$target[$T_LIBRARIES]=[];
989         }
990         if ((@$target[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
991             push @{@$target[$T_LDFLAGS]},"-mno-cygwin";
992             push @{@$target[$T_LDFLAGS]},"-m$opt_arch";
993         }
994         push @{@$project[$P_TARGETS]},$target;
995
996         # Ask for target-specific options
997         if ($opt_ask_target_options == $OPT_ASK_YES) {
998             my $flag_desc="";
999             if ((@$target[$T_FLAGS] & $TF_MFC)!=0) {
1000                 $flag_desc=" (mfc";
1001             }
1002             if ($flag_desc ne "") {
1003                 $flag_desc.=")";
1004             }
1005             print "* Specify any link option (-P/-i/-L/-l/--mfc) specific to the target\n";
1006             print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
1007             while (1) {
1008             my $options=<STDIN>;
1009             chomp $options;
1010             if ($options eq "never") {
1011                 $opt_ask_target_options=$OPT_ASK_NO;
1012                 last;
1013             } elsif (source_set_options($target,$options)) {
1014                 last;
1015             }
1016             print "Please re-enter the options:\n";
1017             }
1018         }
1019         if (@$target[$T_FLAGS] & $TF_MFC) {
1020             @$project_settings[$T_FLAGS]|=$TF_MFC;
1021             push @{@$target[$T_DLL_PATH]},"\$(MFC_LIBRARY_PATH)";
1022             push @{@$target[$T_DLLS]},"mfc.dll";
1023             # FIXME: Link with the MFC in the Unix sense, until we
1024             # start exporting the functions properly.
1025             push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
1026             push @{@$target[$T_LIBRARIES]},"mfc";
1027         }
1028
1029         # Match sources...
1030         if ($target_count == 1) {
1031             push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
1032             @$project_settings[$T_SOURCES_C]=[];
1033             @sources_c=();
1034
1035             push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
1036             @$project_settings[$T_SOURCES_CXX]=[];
1037             @sources_cxx=();
1038
1039             push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
1040             @$project_settings[$T_SOURCES_RC]=[];
1041             @sources_rc=();
1042
1043             push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
1044             # No need for sorting these sources
1045             @$project_settings[$T_SOURCES_MISC]=[];
1046             @sources_misc=();
1047         }
1048         @$target[$T_SOURCES_C]=[sort @{@$target[$T_SOURCES_C]}];
1049         @$target[$T_SOURCES_CXX]=[sort @{@$target[$T_SOURCES_CXX]}];
1050         @$target[$T_SOURCES_RC]=[sort @{@$target[$T_SOURCES_RC]}];
1051         @$target[$T_SOURCES_MISC]=[sort @{@$target[$T_SOURCES_MISC]}];
1052     }
1053     if ($opt_ask_target_options == $OPT_ASK_SKIP) {
1054         $opt_ask_target_options=$OPT_ASK_YES;
1055     }
1056
1057     if ((@$project_settings[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
1058         push @{@$project_settings[$T_CEXTRA]},"-mno-cygwin";
1059         push @{@$project_settings[$T_CXXEXTRA]},"-mno-cygwin";
1060     }
1061
1062     if (@$project_settings[$T_FLAGS] & $TF_MFC) {
1063         push @{@$project_settings[$T_INCLUDE_PATH]},"\$(MFC_INCLUDE_PATH)";
1064     }
1065     # The sources that did not match, if any, go to the extra
1066     # source list of the project settings
1067     foreach my $source (@sources_c) {
1068         if ($source ne "") {
1069             push @{@$project_settings[$T_SOURCES_C]},$source;
1070         }
1071     }
1072     @$project_settings[$T_SOURCES_C]=[sort @{@$project_settings[$T_SOURCES_C]}];
1073     foreach my $source (@sources_cxx) {
1074         if ($source ne "") {
1075             push @{@$project_settings[$T_SOURCES_CXX]},$source;
1076         }
1077     }
1078     @$project_settings[$T_SOURCES_CXX]=[sort @{@$project_settings[$T_SOURCES_CXX]}];
1079     foreach my $source (@sources_rc) {
1080         if ($source ne "") {
1081             push @{@$project_settings[$T_SOURCES_RC]},$source;
1082         }
1083     }
1084     @$project_settings[$T_SOURCES_RC]=[sort @{@$project_settings[$T_SOURCES_RC]}];
1085     foreach my $source (@sources_misc) {
1086         if ($source ne "") {
1087             push @{@$project_settings[$T_SOURCES_MISC]},$source;
1088         }
1089     }
1090     @$project_settings[$T_SOURCES_MISC]=[sort @{@$project_settings[$T_SOURCES_MISC]}];
1091 }
1092
1093 ##
1094 # Scans the specified workspace file to find the project files
1095 sub source_scan_workspace_file($);
1096 sub source_scan_workspace_file($)
1097 {
1098     my $filename=$_[0];
1099     my $path=dirname($filename);
1100     my @components;
1101
1102     if (! -e $filename) {
1103         return;
1104     }
1105
1106     if (!open(FILEIWS,$filename)) {
1107         print STDERR "error: unable to open $filename for reading:\n";
1108         print STDERR "       $!\n";
1109         return;
1110     }
1111
1112     my $prj_name;
1113     my $prj_path;
1114
1115     if ($filename =~ /.dsw$/i) {
1116         while (<FILEIWS>) {
1117             # Remove any trailing CrLf
1118             s/\r\n$/\n/;
1119
1120             # catch a project definition
1121             if (/^Project:\s\"(.*)\"=(.*)\s-/) {
1122                 $prj_name=$1;
1123                 $prj_path=$2;
1124                 @components=split /[\/\\]+/, $prj_path;
1125                 $prj_path=search_from($path, \@components);
1126                 print "Name: $prj_name\nPath: $prj_path\n";
1127                 source_scan_project_file(\@main_project,1,$prj_path);
1128                 next;
1129             } elsif (/^#/) {
1130                 # ignore Comments
1131             } elsif (/\w:/) {
1132                 print STDERR "unknown section $_\n";
1133             } elsif (/^Microsoft(.*)Studio(.*)File,\sFormat Version\s(.*)/) {
1134                 print "\nFileversion: $3\n";
1135             }
1136         }
1137         close(FILEIWS);
1138     } elsif ($filename =~ /.sln$/i) {
1139         while (<FILEIWS>) {
1140             # Remove any trailing CrLf
1141             s/\r\n$/\n/;
1142
1143             # catch a project definition
1144             if (/^Project(.*)=\s*"(.*)",\s*"(.*)",\s*"(.*)"/) {
1145                 $prj_name=$2;
1146                 $prj_path=$3;
1147                 @components=split /[\/\\]+/, $3;
1148                 $prj_path=search_from($path, \@components);
1149                 print "Name: $prj_name\nPath: $prj_path\n";
1150                 source_scan_project_file(\@main_project,1,$prj_path);
1151                 next;
1152             } elsif (/^Microsoft(.*)Studio(.*)File,\sFormat Version\s(.*)/) {
1153                 print "\nFileversion: $3\n";
1154             }
1155         }
1156         close(FILEIWS);
1157     }
1158
1159     @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
1160 }
1161
1162 ##
1163 # Scans the specified directory to:
1164 # - see if we should create a Makefile in this directory. We normally do
1165 #   so if we find a project file and sources
1166 # - get a list of targets for this directory
1167 # - get the list of source files
1168 sub source_scan_directory($$$$);
1169 sub source_scan_directory($$$$)
1170 {
1171   # a reference to the parent's project
1172   my $parent_project=$_[0];
1173   # the full relative path to the current directory, including a
1174   # trailing '/', or an empty string if this is the top level directory
1175   my $path=$_[1];
1176   # the name of this directory, including a trailing '/', or an empty
1177   # string if this is the top level directory
1178   my $dirname=$_[2];
1179   # if set then no targets will be looked for and the sources will all
1180   # end up in the parent_project's 'misc' bucket
1181   my $no_target=$_[3];
1182
1183   # reference to the project for this directory. May not be used
1184   my $project;
1185   # list of targets found in the 'current' directory
1186   my %targets;
1187   # list of sources found in the current directory
1188   my @sources_c=();
1189   my @sources_cxx=();
1190   my @sources_rc=();
1191   my @sources_misc=();
1192   # true if this directory contains a Windows project
1193   my $has_win_project=0;
1194   # true if this directory contains headers
1195   my $has_headers=0;
1196   # If we don't find any executable/library then we might make up targets
1197   # from the list of .dsp/.mak files we find since they usually have the
1198   # same name as their target.
1199   my @dsp_files=();
1200   my @mak_files=();
1201
1202   if (defined $opt_single_target or $dirname eq "") {
1203     # Either there is a single target and thus a single project,
1204     # or we are in the top level directory for which a project
1205     # already exists
1206     $project=$parent_project;
1207   } else {
1208     $project=[];
1209     project_init($project, $path, \@global_settings);
1210   }
1211   my $project_settings=@$project[$P_SETTINGS];
1212
1213   # First find out what this directory contains:
1214   # collect all sources, targets and subdirectories
1215   my $directory=get_directory_contents($path);
1216   foreach my $dentry (@$directory) {
1217     if ($dentry =~ /^\./) {
1218       next;
1219     }
1220     my $fullentry="$path$dentry";
1221     if (-d "$fullentry") {
1222       if ($dentry =~ /^(Release|Debug)/i) {
1223         # These directories are often used to store the object files and the
1224         # resulting executable/library. They should not contain anything else.
1225         my @candidates=grep /\.(exe|dll)$/i, @{get_directory_contents("$fullentry")};
1226         foreach my $candidate (@candidates) {
1227           $targets{$candidate}=1;
1228         }
1229       } elsif ($dentry =~ /^include/i) {
1230         # This directory must contain headers we're going to need
1231         push @{@$project_settings[$T_INCLUDE_PATH]},"-I$dentry";
1232         source_scan_directory($project,"$fullentry/","$dentry/",1);
1233       } else {
1234         # Recursively scan this directory. Any source file that cannot be
1235         # attributed to a project in one of the subdirectories will be
1236         # attributed to this project.
1237         source_scan_directory($project,"$fullentry/","$dentry/",$no_target);
1238       }
1239     } elsif (-f "$fullentry") {
1240       if ($dentry =~ /\.(exe|dll)$/i) {
1241         $targets{$dentry}=1;
1242       } elsif ($dentry =~ /\.c$/i and $dentry !~ /\.(dbg|spec)\.c$/) {
1243         push @sources_c,"$dentry";
1244       } elsif ($dentry =~ /\.(cpp|cxx)$/i) {
1245         if ($dentry =~ /^stdafx.cpp$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
1246           push @sources_misc,"$dentry";
1247           @$project_settings[$T_FLAGS]|=$TF_MFC;
1248         } else {
1249           push @sources_cxx,"$dentry";
1250         }
1251       } elsif ($dentry =~ /\.rc$/i) {
1252         push @sources_rc,"$dentry";
1253       } elsif ($dentry =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
1254         $has_headers=1;
1255         push @sources_misc,"$dentry";
1256         if ($dentry =~ /^stdafx.h$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
1257           @$project_settings[$T_FLAGS]|=$TF_MFC;
1258         }
1259       } elsif ($dentry =~ /\.dsp$/i) {
1260         push @dsp_files,"$dentry";
1261         $has_win_project=1;
1262       } elsif ($dentry =~ /\.mak$/i) {
1263         push @mak_files,"$dentry";
1264         $has_win_project=1;
1265       } elsif ($dentry =~ /^makefile/i) {
1266         $has_win_project=1;
1267       }
1268     }
1269   }
1270
1271   if ($has_headers) {
1272     push @{@$project_settings[$T_INCLUDE_PATH]},"-I.";
1273   }
1274   # If we have a single target then all we have to do is assign
1275   # all the sources to it and we're done
1276   # FIXME: does this play well with the --interactive mode?
1277   if ($opt_single_target) {
1278     my $target=@{@$project[$P_TARGETS]}[0];
1279     push @{@$target[$T_SOURCES_C]},map "$path$_",@sources_c;
1280     push @{@$target[$T_SOURCES_CXX]},map "$path$_",@sources_cxx;
1281     push @{@$target[$T_SOURCES_RC]},map "$path$_",@sources_rc;
1282     push @{@$target[$T_SOURCES_MISC]},map "$path$_",@sources_misc;
1283     return;
1284   }
1285   if ($no_target) {
1286     my $parent_settings=@$parent_project[$P_SETTINGS];
1287     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_c;
1288     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_cxx;
1289     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_rc;
1290     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
1291     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
1292     return;
1293   }
1294
1295   my $source_count=@sources_c+@sources_cxx+@sources_rc+
1296                    @{@$project_settings[$T_SOURCES_C]}+
1297                    @{@$project_settings[$T_SOURCES_CXX]}+
1298                    @{@$project_settings[$T_SOURCES_RC]};
1299   if ($source_count == 0) {
1300     # A project without real sources is not a project, get out!
1301     if ($project!=$parent_project) {
1302       my $parent_settings=@$parent_project[$P_SETTINGS];
1303       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
1304       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
1305     }
1306     return;
1307   }
1308   #print "targets=",%targets,"\n";
1309   #print "target_count=$target_count\n";
1310   #print "has_win_project=$has_win_project\n";
1311   #print "dirname=$dirname\n";
1312
1313   my $target_count;
1314   if (($has_win_project != 0) or ($dirname eq "")) {
1315     # Deal with cases where we could not find any executable/library, and
1316     # thus have no target, although we did find some sort of windows project.
1317     $target_count=keys %targets;
1318     if ($target_count == 0) {
1319       # Try to come up with a target list based on .dsp/.mak files
1320       my $prj_list;
1321       if (@dsp_files > 0) {
1322         $prj_list=\@dsp_files;
1323       } else {
1324         $prj_list=\@mak_files;
1325       }
1326       foreach my $filename (@$prj_list) {
1327         $filename =~ s/\.(dsp|mak)$//i;
1328         if ($opt_target_type == $TT_DLL) {
1329           $filename = "$filename.dll";
1330         }
1331         $targets{$filename}=1;
1332       }
1333       $target_count=keys %targets;
1334       if ($target_count == 0) {
1335         # Still nothing, try the name of the directory
1336         my $name;
1337         if ($dirname eq "") {
1338           # Bad luck, this is the top level directory!
1339           $name=(split /\//, cwd)[-1];
1340         } else {
1341           $name=$dirname;
1342           # Remove the trailing '/'. Also eliminate whatever is after the last
1343           # '.' as it is likely to be meaningless (.orig, .new, ...)
1344           $name =~ s+(/|\.[^.]*)$++;
1345           if ($name eq "src") {
1346             # 'src' is probably a subdirectory of the real project directory.
1347             # Try again with the parent (if any).
1348             my $parent=$path;
1349             if ($parent =~ s+([^/]*)/[^/]*/$+$1+) {
1350               $name=$parent;
1351             } else {
1352               $name=(split /\//, cwd)[-1];
1353             }
1354           }
1355         }
1356         $name =~ s+(/|\.[^.]*)$++;
1357         if ($opt_target_type == $TT_DLL) {
1358           $name = canonize($name).".dll";
1359         } else {
1360           $name = canonize($name).".exe";
1361         }
1362         $targets{$name}=1;
1363       }
1364     }
1365
1366     # Ask confirmation to the user if he wishes so
1367     if ($opt_is_interactive == $OPT_ASK_YES) {
1368       my $target_list=join " ",keys %targets;
1369       print "\n*** In ",($path?$path:"./"),"\n";
1370       print "* winemaker found the following list of (potential) targets\n";
1371       print "*   $target_list\n";
1372       print "* Type enter to use it as is, your own comma-separated list of\n";
1373       print "* targets, 'none' to assign the source files to a parent directory,\n";
1374       print "* or 'ignore' to ignore everything in this directory tree.\n";
1375       print "* Target list:\n";
1376       $target_list=<STDIN>;
1377       chomp $target_list;
1378       if ($target_list eq "") {
1379         # Keep the target list as is, i.e. do nothing
1380       } elsif ($target_list eq "none") {
1381         # Empty the target list
1382         undef %targets;
1383       } elsif ($target_list eq "ignore") {
1384         # Ignore this subtree altogether
1385         return;
1386       } else {
1387         undef %targets;
1388         foreach my $target (split /,/,$target_list) {
1389           $target =~ s+^\s*++;
1390           $target =~ s+\s*$++;
1391           $targets{$target}=1;
1392         }
1393       }
1394     }
1395   }
1396
1397   # If we have no project at this level, then transfer all
1398   # the sources to the parent project
1399   $target_count=keys %targets;
1400   if ($target_count == 0) {
1401     if ($project!=$parent_project) {
1402       my $parent_settings=@$parent_project[$P_SETTINGS];
1403       push @{@$parent_settings[$T_SOURCES_C]},map "$dirname$_",@sources_c;
1404       push @{@$parent_settings[$T_SOURCES_CXX]},map "$dirname$_",@sources_cxx;
1405       push @{@$parent_settings[$T_SOURCES_RC]},map "$dirname$_",@sources_rc;
1406       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
1407       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
1408     }
1409     return;
1410   }
1411
1412   # Otherwise add this project to the project list, except for
1413   # the main project which is already in the list.
1414   if ($dirname ne "") {
1415     push @projects,$project;
1416   }
1417
1418   # Ask for project-wide options
1419   if ($opt_ask_project_options == $OPT_ASK_YES) {
1420     my $flag_desc="";
1421     if ((@$project_settings[$T_FLAGS] & $TF_MFC)!=0) {
1422       $flag_desc="mfc";
1423     }
1424     print "* Type any project-wide options (-D/-I/-P/-i/-L/-l/--mfc),\n";
1425     if (defined $flag_desc) {
1426       print "* (currently $flag_desc)\n";
1427     }
1428     print "* or 'skip' to skip the target specific options,\n";
1429     print "* or 'never' to not be asked this question again:\n";
1430     while (1) {
1431       my $options=<STDIN>;
1432       chomp $options;
1433       if ($options eq "skip") {
1434         $opt_ask_target_options=$OPT_ASK_SKIP;
1435         last;
1436       } elsif ($options eq "never") {
1437         $opt_ask_project_options=$OPT_ASK_NO;
1438         last;
1439       } elsif (source_set_options($project_settings,$options)) {
1440         last;
1441       }
1442       print "Please re-enter the options:\n";
1443     }
1444   }
1445
1446   # - Create the targets
1447   # - Check if we have both libraries and programs
1448   # - Match each target with source files (sort in reverse
1449   #   alphabetical order to get the longest matches first)
1450   my @local_dlls=();
1451   my @local_depends=();
1452   my @exe_list=();
1453   foreach my $target_name (map (lc, (sort { $b cmp $a } keys %targets))) {
1454     # Create the target...
1455     my $target=[];
1456     target_init($target);
1457     @$target[$T_NAME]=$target_name;
1458     @$target[$T_FLAGS]|=@$project_settings[$T_FLAGS];
1459     if ($target_name =~ /\.dll$/) {
1460       @$target[$T_TYPE]=$TT_DLL;
1461       push @local_depends,"$target_name.so";
1462       push @local_dlls,$target_name;
1463       my $canon=canonize($target_name);
1464       push @{@$target[$T_LDFLAGS]},("-shared","\$(${canon}_MODULE:%=%.spec)");
1465     } else {
1466       @$target[$T_TYPE]=$opt_target_type;
1467       push @exe_list,$target;
1468       push @{@$target[$T_LDFLAGS]},(@$target[$T_TYPE] == $TT_CUIEXE ? "-mconsole" : "-mwindows");
1469     }
1470     my $basename=$target_name;
1471     $basename=~ s/\.(dll|exe)$//i;
1472     # This is the default link list of Visual Studio
1473     my @std_imports=qw(odbc32 ole32 oleaut32 winspool odbccp32);
1474     my @std_libraries=qw(uuid);
1475     if ((@$target[$T_FLAGS] & $TF_NODLLS) == 0) {
1476       @$target[$T_DLLS]=\@std_imports;
1477       @$target[$T_LIBRARIES]=\@std_libraries;
1478     } else {
1479       @$target[$T_DLLS]=[];
1480       @$target[$T_LIBRARIES]=[];
1481     }
1482     if ((@$target[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
1483       push @{@$target[$T_LDFLAGS]},"-mno-cygwin";
1484       push @{@$target[$T_LDFLAGS]},"-m$opt_arch";
1485     }
1486     push @{@$project[$P_TARGETS]},$target;
1487
1488     # Ask for target-specific options
1489     if ($opt_ask_target_options == $OPT_ASK_YES) {
1490       my $flag_desc="";
1491       if ((@$target[$T_FLAGS] & $TF_MFC)!=0) {
1492         $flag_desc=" (mfc";
1493       }
1494       if ($flag_desc ne "") {
1495         $flag_desc.=")";
1496       }
1497       print "* Specify any link option (-P/-i/-L/-l/--mfc) specific to the target\n";
1498       print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
1499       while (1) {
1500         my $options=<STDIN>;
1501         chomp $options;
1502         if ($options eq "never") {
1503           $opt_ask_target_options=$OPT_ASK_NO;
1504           last;
1505         } elsif (source_set_options($target,$options)) {
1506           last;
1507         }
1508         print "Please re-enter the options:\n";
1509       }
1510     }
1511     if (@$target[$T_FLAGS] & $TF_MFC) {
1512       @$project_settings[$T_FLAGS]|=$TF_MFC;
1513       push @{@$target[$T_DLL_PATH]},"\$(MFC_LIBRARY_PATH)";
1514       push @{@$target[$T_DLLS]},"mfc.dll";
1515       # FIXME: Link with the MFC in the Unix sense, until we
1516       # start exporting the functions properly.
1517       push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
1518       push @{@$target[$T_LIBRARIES]},"mfc";
1519     }
1520
1521     # Match sources...
1522     if ($target_count == 1) {
1523       push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
1524       @$project_settings[$T_SOURCES_C]=[];
1525       @sources_c=();
1526
1527       push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
1528       @$project_settings[$T_SOURCES_CXX]=[];
1529       @sources_cxx=();
1530
1531       push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
1532       @$project_settings[$T_SOURCES_RC]=[];
1533       @sources_rc=();
1534
1535       push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
1536       # No need for sorting these sources
1537       @$project_settings[$T_SOURCES_MISC]=[];
1538       @sources_misc=();
1539     } else {
1540       foreach my $source (@sources_c) {
1541         if ($source =~ /^$basename/i) {
1542           push @{@$target[$T_SOURCES_C]},$source;
1543           $source="";
1544         }
1545       }
1546       foreach my $source (@sources_cxx) {
1547         if ($source =~ /^$basename/i) {
1548           push @{@$target[$T_SOURCES_CXX]},$source;
1549           $source="";
1550         }
1551       }
1552       foreach my $source (@sources_rc) {
1553         if ($source =~ /^$basename/i) {
1554           push @{@$target[$T_SOURCES_RC]},$source;
1555           $source="";
1556         }
1557       }
1558       foreach my $source (@sources_misc) {
1559         if ($source =~ /^$basename/i) {
1560           push @{@$target[$T_SOURCES_MISC]},$source;
1561           $source="";
1562         }
1563       }
1564     }
1565     @$target[$T_SOURCES_C]=[sort @{@$target[$T_SOURCES_C]}];
1566     @$target[$T_SOURCES_CXX]=[sort @{@$target[$T_SOURCES_CXX]}];
1567     @$target[$T_SOURCES_RC]=[sort @{@$target[$T_SOURCES_RC]}];
1568     @$target[$T_SOURCES_MISC]=[sort @{@$target[$T_SOURCES_MISC]}];
1569   }
1570   if ($opt_ask_target_options == $OPT_ASK_SKIP) {
1571     $opt_ask_target_options=$OPT_ASK_YES;
1572   }
1573
1574   if ((@$project_settings[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
1575     push @{@$project_settings[$T_CEXTRA]},"-mno-cygwin";
1576     push @{@$project_settings[$T_CXXEXTRA]},"-mno-cygwin";
1577   }
1578
1579   if (@$project_settings[$T_FLAGS] & $TF_MFC) {
1580     push @{@$project_settings[$T_INCLUDE_PATH]},"\$(MFC_INCLUDE_PATH)";
1581   }
1582   # The sources that did not match, if any, go to the extra
1583   # source list of the project settings
1584   foreach my $source (@sources_c) {
1585     if ($source ne "") {
1586       push @{@$project_settings[$T_SOURCES_C]},$source;
1587     }
1588   }
1589   @$project_settings[$T_SOURCES_C]=[sort @{@$project_settings[$T_SOURCES_C]}];
1590   foreach my $source (@sources_cxx) {
1591     if ($source ne "") {
1592       push @{@$project_settings[$T_SOURCES_CXX]},$source;
1593     }
1594   }
1595   @$project_settings[$T_SOURCES_CXX]=[sort @{@$project_settings[$T_SOURCES_CXX]}];
1596   foreach my $source (@sources_rc) {
1597     if ($source ne "") {
1598       push @{@$project_settings[$T_SOURCES_RC]},$source;
1599     }
1600   }
1601   @$project_settings[$T_SOURCES_RC]=[sort @{@$project_settings[$T_SOURCES_RC]}];
1602   foreach my $source (@sources_misc) {
1603     if ($source ne "") {
1604       push @{@$project_settings[$T_SOURCES_MISC]},$source;
1605     }
1606   }
1607   @$project_settings[$T_SOURCES_MISC]=[sort @{@$project_settings[$T_SOURCES_MISC]}];
1608
1609   # Finally if we are building both libraries and programs in
1610   # this directory, then the programs should be linked with all
1611   # the libraries
1612   if (@local_dlls > 0 and @exe_list > 0) {
1613     foreach my $target (@exe_list) {
1614       push @{@$target[$T_DLL_PATH]},"-L.";
1615       push @{@$target[$T_DLLS]},@local_dlls;
1616     }
1617   }
1618 }
1619
1620 ##
1621 # Scan the source directories in search of things to build
1622 sub source_scan()
1623 {
1624   # If there's a single target then this is going to be the default target
1625   if (defined $opt_single_target) {
1626     # Create the main target
1627     my $main_target=[];
1628     target_init($main_target);
1629     @$main_target[$T_NAME]=$opt_single_target;
1630     @$main_target[$T_TYPE]=$opt_target_type;
1631
1632     # Add it to the list
1633     push @{$main_project[$P_TARGETS]},$main_target;
1634   }
1635
1636   # The main directory is always going to be there
1637   push @projects,\@main_project;
1638
1639     if (defined $opt_work_dir) {
1640         # Now scan the directory tree looking for source files and, maybe, targets
1641         print "Scanning the source directories...\n";
1642         source_scan_directory(\@main_project,"","",0);
1643         @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
1644     } elsif (defined $opt_work_file) {
1645         if ($opt_work_file =~ /.dsp$/i or $opt_work_file =~ /.vcproj$/i) {
1646             source_scan_project_file(\@main_project,0,$opt_work_file);
1647         } elsif ($opt_work_file =~ /.dsw$/i or $opt_work_file =~ /.sln$/i) {
1648             source_scan_workspace_file($opt_work_file);
1649         }
1650     }
1651 }
1652
1653 #####
1654 #
1655 # Source search
1656 #
1657 #####
1658
1659 ##
1660 # Performs a directory traversal and renames the files so that:
1661 # - they have the case desired by the user
1662 # - their extension is of the appropriate case
1663 # - they don't contain annoying characters like ' ', '$', '#', ...
1664 # But only perform these changes for source files and directories.
1665 sub fix_file_and_directory_names($);
1666 sub fix_file_and_directory_names($)
1667 {
1668   my $dirname=$_[0];
1669
1670   my $directory=get_directory_contents($dirname);
1671   foreach my $dentry (@$directory)
1672   {
1673       if ($dentry =~ /^\./ or $dentry eq "CVS") {
1674           next;
1675       }
1676       # Set $warn to 1 if the user should be warned of the renaming
1677       my $warn;
1678       my $new_name=$dentry;
1679
1680       if (-f "$dirname/$dentry")
1681       {
1682           # Don't rename Winemaker's makefiles
1683           next if ($dentry eq "Makefile" and
1684                    `head -n 1 "$dirname/$dentry"` =~ /Generated by Winemaker/);
1685
1686           # Leave non-source files alone
1687           next if ($new_name !~ /(^makefile|\.(c|cpp|h|rc))$/i);
1688
1689           # Only all lowercase extensions are supported (because of
1690           # rules like '.c.o:'.
1691           $new_name =~ s/\.C$/.c/;
1692           $new_name =~ s/\.cpp$/.cpp/i;
1693           $warn=1 if ($new_name =~ s/\.cxx$/.cpp/i);
1694           $new_name =~ s/\.rc$/.rc/i;
1695           # And this last one is to avoid confusion then running make
1696           $warn=1 if ($new_name =~ s/^makefile$/makefile.win/i);
1697       }
1698
1699       # Adjust the case to the user's preferences
1700       if (($opt_lower == $OPT_LOWER_ALL and $dentry =~ /[A-Z]/) or
1701           ($opt_lower == $OPT_LOWER_UPPERCASE and $dentry !~ /[a-z]/)
1702          ) {
1703           $new_name=lc $new_name;
1704       }
1705
1706       # autoconf and make don't support these characters well
1707       $new_name =~ s/[ \$]/_/g;
1708
1709       # And finally, perform the renaming
1710       if ($new_name ne $dentry)
1711       {
1712           if ($warn) {
1713               print STDERR "warning: in \"$dirname\", renaming \"$dentry\" to \"$new_name\"\n";
1714           }
1715           if (!rename("$dirname/$dentry","$dirname/$new_name")) {
1716               print STDERR "error: in \"$dirname\", unable to rename \"$dentry\" to \"$new_name\"\n";
1717               print STDERR "       $!\n";
1718               $new_name=$dentry;
1719           }
1720           else
1721           {
1722               clear_directory_cache($dirname);
1723           }
1724       }
1725       if (-d "$dirname/$new_name") {
1726           fix_file_and_directory_names("$dirname/$new_name");
1727       }
1728   }
1729 }
1730
1731
1732
1733 #####
1734 #
1735 # Source fixup
1736 #
1737 #####
1738
1739 ##
1740 # Try to find a file for the specified filename. The attempt is
1741 # case-insensitive which is why it's not trivial. If a match is
1742 # found then we return the pathname with the correct case.
1743 sub search_from($$)
1744 {
1745   my $dirname=$_[0];
1746   my $path=$_[1];
1747   my $real_path="";
1748
1749   if ($dirname eq "" or $dirname eq "." or $dirname eq "./") {
1750     $dirname=cwd;
1751   } elsif ($dirname !~ m+^/+) {
1752     $dirname=cwd . "/" . $dirname;
1753   }
1754   if ($dirname !~ m+/$+) {
1755     $dirname.="/";
1756   }
1757
1758   foreach my $component (@$path) {
1759     $component=~s/^\"//;
1760     $component=~s/\"$//;
1761     #print "    looking for $component in \"$dirname\"\n";
1762     if ($component eq ".") {
1763       # Pass it as is
1764       $real_path.="./";
1765     } elsif ($component eq "..") {
1766       # Go up one level
1767       $dirname=dirname($dirname) . "/";
1768       $real_path.="../";
1769     } else {
1770       # The file/directory may have been renamed before. Also try to
1771       # match the renamed file.
1772       my $renamed=$component;
1773       $renamed =~ s/[ \$]/_/g;
1774       if ($renamed eq $component) {
1775         undef $renamed;
1776       }
1777
1778       my $directory=get_directory_contents $dirname;
1779       my $found;
1780       foreach my $dentry (@$directory) {
1781         if ($dentry =~ /^\Q$component\E$/i or
1782             (defined $renamed and $dentry =~ /^$renamed$/i)
1783            ) {
1784           $dirname.="$dentry/";
1785           $real_path.="$dentry/";
1786           $found=1;
1787           last;
1788         }
1789       }
1790       if (!defined $found) {
1791         # Give up
1792         #print "    could not find $component in $dirname\n";
1793         return;
1794       }
1795     }
1796   }
1797   $real_path=~ s+/$++;
1798   #print "    -> found $real_path\n";
1799   return $real_path;
1800 }
1801
1802 ##
1803 # Performs a case-insensitive search for the specified file in the
1804 # include path.
1805 # $line is the line number that should be referenced when an error occurs
1806 # $filename is the file we are looking for
1807 # $dirname is the directory of the file containing the '#include' directive
1808 #    if '"' was used, it is an empty string otherwise
1809 # $project and $target specify part of the include path
1810 sub get_real_include_name($$$$$)
1811 {
1812   my $line=$_[0];
1813   my $filename=$_[1];
1814   my $dirname=$_[2];
1815   my $project=$_[3];
1816   my $target=$_[4];
1817
1818   if ($filename =~ /^([a-zA-Z]:)?[\/]/ or $filename =~ /^[a-zA-Z]:[\/]?/) {
1819     # This is not a relative path, we cannot make any check
1820     my $warning="path:$filename";
1821     if (!defined $warnings{$warning}) {
1822       $warnings{$warning}="1";
1823       print STDERR "warning: cannot check the case of absolute pathnames:\n";
1824       print STDERR "$line:   $filename\n";
1825     }
1826   } else {
1827     # Here's how we proceed:
1828     # - split the filename we look for into its components
1829     # - then for each directory in the include path
1830     #   - trace the directory components starting from that directory
1831     #   - if we fail to find a match at any point then continue with
1832     #     the next directory in the include path
1833     #   - otherwise, rejoice, our quest is over.
1834     my @file_components=split /[\/\\]+/, $filename;
1835     #print "  Searching for $filename from @$project[$P_PATH]\n";
1836
1837     my $real_filename;
1838     if ($dirname ne "") {
1839       # This is an 'include ""' -> look in dirname first.
1840       #print "    in $dirname (include \"\")\n";
1841       $real_filename=search_from($dirname,\@file_components);
1842       if (defined $real_filename) {
1843         return $real_filename;
1844       }
1845     }
1846     my $project_settings=@$project[$P_SETTINGS];
1847     foreach my $include (@{@$target[$T_INCLUDE_PATH]}, @{@$project_settings[$T_INCLUDE_PATH]}) {
1848       my $dirname=$include;
1849       $dirname=~ s+^-I++;
1850       if (!is_absolute($dirname)) {
1851         $dirname="@$project[$P_PATH]$dirname";
1852       } else {
1853         $dirname=~ s+^\$\(TOPSRCDIR\)/++;
1854         $dirname=~ s+^\$\(SRCDIR\)/+@$project[$P_PATH]+;
1855       }
1856       #print "    in $dirname\n";
1857       $real_filename=search_from("$dirname",\@file_components);
1858       if (defined $real_filename) {
1859         return $real_filename;
1860       }
1861     }
1862     my $dotdotpath=@$project[$P_PATH];
1863     $dotdotpath =~ s/[^\/]+/../g;
1864     foreach my $include (@{$global_settings[$T_INCLUDE_PATH]}) {
1865       my $dirname=$include;
1866       $dirname=~ s+^-I++;
1867       $dirname=~ s+^\$\(TOPSRCDIR\)\/++;
1868       $dirname=~ s+^\$\(SRCDIR\)\/+@$project[$P_PATH]+;
1869       #print "    in $dirname  (global setting)\n";
1870       $real_filename=search_from("$dirname",\@file_components);
1871       if (defined $real_filename) {
1872         return $real_filename;
1873       }
1874     }
1875   }
1876   $filename =~ s+\\\\+/+g; # in include ""
1877   $filename =~ s+\\+/+g; # in include <> !
1878   if ($opt_lower_include) {
1879     return lc "$filename";
1880   }
1881   return $filename;
1882 }
1883
1884 sub print_pack($$$)
1885 {
1886   my $indent=$_[0];
1887   my $size=$_[1];
1888   my $trailer=$_[2];
1889
1890   if ($size =~ /^(1|2|4|8)$/) {
1891     print FILEO "$indent#include <pshpack$size.h>$trailer";
1892   } else {
1893     print FILEO "$indent/* winemaker:warning: Unknown size \"$size\". Defaulting to 4 */\n";
1894     print FILEO "$indent#include <pshpack4.h>$trailer";
1895   }
1896 }
1897
1898 ##
1899 # 'Parses' a source file and fixes constructs that would not work with
1900 # Winelib. The parsing is rather simple and not all non-portable features
1901 # are corrected. The most important feature that is corrected is the case
1902 # and path separator of '#include' directives. This requires that each
1903 # source file be associated to a project & target so that the proper
1904 # include path is used.
1905 # Also note that the include path is relative to the directory in which the
1906 # compiler is run, i.e. that of the project, not to that of the file.
1907 sub fix_file($$$)
1908 {
1909   my $filename=$_[0];
1910   my $project=$_[1];
1911   my $target=$_[2];
1912   $filename="@$project[$P_PATH]$filename";
1913   if (! -e $filename) {
1914     return;
1915   }
1916
1917   my $is_rc=($filename =~ /\.(rc2?|dlg)$/i);
1918   my $dirname=dirname($filename);
1919   my $is_mfc=0;
1920   if (defined $target and (@$target[$T_FLAGS] & $TF_MFC)) {
1921     $is_mfc=1;
1922   }
1923
1924   print "  $filename\n";
1925   #FIXME:assuming that because there is a .bak file, this is what we want is
1926   #probably flawed. Or is it???
1927   if (! -e "$filename.bak") {
1928     if (!copy("$filename","$filename.bak")) {
1929       print STDERR "error: unable to make a backup of $filename:\n";
1930       print STDERR "       $!\n";
1931       return;
1932     }
1933   }
1934   if (!open(FILEI,"$filename.bak")) {
1935     print STDERR "error: unable to open $filename.bak for reading:\n";
1936     print STDERR "       $!\n";
1937     return;
1938   }
1939   if (!open(FILEO,">$filename")) {
1940     print STDERR "error: unable to open $filename for writing:\n";
1941     print STDERR "       $!\n";
1942     return;
1943   }
1944   my $line=0;
1945   my $modified=0;
1946   my $rc_block_depth=0;
1947   my $rc_textinclude_state=0;
1948   my @pack_stack;
1949   while (<FILEI>) {
1950     # Remove any trailing CtrlZ, which isn't strictly in the file
1951     if (/\x1A/) {
1952       s/\x1A//;
1953       last if (/^$/)
1954     }
1955     $line++;
1956     s/\r\n$/\n/;
1957     if (!/\n$/) {
1958       # Make sure all files are '\n' terminated
1959       $_ .= "\n";
1960     }
1961     if ($is_rc and !$is_mfc and /^(\s*)(\#\s*include\s*)\"afxres\.h\"/) {
1962       # VC6 automatically includes 'afxres.h', an MFC specific header, in
1963       # the RC files it generates (even in non-MFC projects). So we replace
1964       # it with 'winresrc.h' its very close standard cousin so that non MFC
1965       # projects can compile in Wine without the MFC sources.
1966       my $warning="mfc:afxres.h";
1967       if (!defined $warnings{$warning}) {
1968         $warnings{$warning}="1";
1969         print STDERR "warning: In non-MFC projects, winemaker replaces the MFC specific header 'afxres.h' with 'winresrc.h'\n";
1970         print STDERR "warning: the above warning is issued only once\n";
1971       }
1972       print FILEO "$1/* winemaker: $2\"afxres.h\" */\n";
1973       print FILEO "$1/* winemaker:warning: 'afxres.h' is an MFC specific header. Replacing it with 'winresrc.h' */\n";
1974       print FILEO "$1$2\"winresrc.h\"$'";
1975       $modified=1;
1976
1977     } elsif (/^(\s*\#\s*include\s*)([\"<])([^\"]+)([\">])/) {
1978       my $from_file=($2 eq "<"?"":$dirname);
1979       my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1980       print FILEO "$1$2$real_include_name$4$'";
1981       $modified|=($real_include_name ne $3);
1982
1983     } elsif (s/^(\s*)(\#\s*pragma\s+pack\s*\(\s*)//) {
1984       # Pragma pack handling
1985       #
1986       # pack_stack is an array of references describing the stack of
1987       # pack directives currently in effect. Each directive if described
1988       # by a reference to an array containing:
1989       # - "push" for pack(push,...) directives, "" otherwise
1990       # - the directive's identifier at index 1
1991       # - the directive's alignment value at index 2
1992       #
1993       # Don't believe a word of what the documentation says: it's all wrong.
1994       # The code below is based on the actual behavior of Visual C/C++ 6.
1995       my $pack_indent=$1;
1996       my $pack_header=$2;
1997       if (/^(\))/) {
1998         # pragma pack()
1999         # Pushes the default stack alignment
2000         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2001         print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
2002         print_pack($pack_indent,4,$');
2003         push @pack_stack, [ "", "", 4 ];
2004
2005       } elsif (/^(pop\s*(,\s*\d+\s*)?\))/) {
2006         # pragma pack(pop)
2007         # pragma pack(pop,n)
2008         # Goes up the stack until it finds a pack(push,...), and pops it
2009         # Ignores any pack(n) entry
2010         # Issues a warning if the pack is of the form pack(push,label)
2011         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2012         my $pack_comment=$';
2013         $pack_comment =~ s/^\s*//;
2014         if ($pack_comment ne "") {
2015           print FILEO "$pack_indent$pack_comment";
2016         }
2017         while (1) {
2018           my $alignment=pop @pack_stack;
2019           if (!defined $alignment) {
2020             print FILEO "$pack_indent/* winemaker:warning: No pack(push,...) found. All the stack has been popped */\n";
2021             last;
2022           }
2023           if (@$alignment[1]) {
2024             print FILEO "$pack_indent/* winemaker:warning: Anonymous pop of pack(push,@$alignment[1]) (@$alignment[2]) */\n";
2025           }
2026           print FILEO "$pack_indent#include <poppack.h>\n";
2027           if (@$alignment[0]) {
2028             last;
2029           }
2030         }
2031
2032       } elsif (/^(pop\s*,\s*(\w+)\s*(,\s*\d+\s*)?\))/) {
2033         # pragma pack(pop,label[,n])
2034         # Goes up the stack until finding a pack(push,...) and pops it.
2035         # 'n', if specified, is ignored.
2036         # Ignores any pack(n) entry
2037         # Issues a warning if the label of the pack does not match,
2038         # or if it is in fact a pack(push,n)
2039         my $label=$2;
2040         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2041         my $pack_comment=$';
2042         $pack_comment =~ s/^\s*//;
2043         if ($pack_comment ne "") {
2044           print FILEO "$pack_indent$pack_comment";
2045         }
2046         while (1) {
2047           my $alignment=pop @pack_stack;
2048           if (!defined $alignment) {
2049             print FILEO "$pack_indent/* winemaker:warning: No pack(push,$label) found. All the stack has been popped */\n";
2050             last;
2051           }
2052           if (@$alignment[1] and @$alignment[1] ne $label) {
2053             print FILEO "$pack_indent/* winemaker:warning: Push/pop mismatch: \"@$alignment[1]\" (@$alignment[2]) != \"$label\" */\n";
2054           }
2055           print FILEO "$pack_indent#include <poppack.h>\n";
2056           if (@$alignment[0]) {
2057             last;
2058           }
2059         }
2060
2061       } elsif (/^(push\s*\))/) {
2062         # pragma pack(push)
2063         # Push the current alignment
2064         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2065         if (@pack_stack > 0) {
2066           my $alignment=$pack_stack[$#pack_stack];
2067           print_pack($pack_indent,@$alignment[2],$');
2068           push @pack_stack, [ "push", "", @$alignment[2] ];
2069         } else {
2070           print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
2071           print_pack($pack_indent,4,$');
2072           push @pack_stack, [ "push", "", 4 ];
2073         }
2074
2075       } elsif (/^((push\s*,\s*)?(\d+)\s*\))/) {
2076         # pragma pack([push,]n)
2077         # Push new alignment n
2078         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2079         print_pack($pack_indent,$3,"$'");
2080         push @pack_stack, [ ($2 ? "push" : ""), "", $3 ];
2081
2082       } elsif (/^((\w+)\s*\))/) {
2083         # pragma pack(label)
2084         # label must in fact be a macro that resolves to an integer
2085         # Then behaves like 'pragma pack(n)'
2086         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2087         print FILEO "$pack_indent/* winemaker:warning: Assuming $2 == 4 */\n";
2088         print_pack($pack_indent,4,$');
2089         push @pack_stack, [ "", "", 4 ];
2090
2091       } elsif (/^(push\s*,\s*(\w+)\s*(,\s*(\d+)\s*)?\))/) {
2092         # pragma pack(push,label[,n])
2093         # Pushes a new label on the stack. It is possible to push the same
2094         # label multiple times. If 'n' is omitted then the alignment is
2095         # unchanged. Otherwise it becomes 'n'.
2096         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
2097         my $size;
2098         if (defined $4) {
2099           $size=$4;
2100         } elsif (@pack_stack > 0) {
2101           my $alignment=$pack_stack[$#pack_stack];
2102           $size=@$alignment[2];
2103         } else {
2104           print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
2105           $size=4;
2106         }
2107         print_pack($pack_indent,$size,$');
2108         push @pack_stack, [ "push", $2, $size ];
2109
2110       } else {
2111         # pragma pack(???               -> What's that?
2112         print FILEO "$pack_indent/* winemaker:warning: Unknown type of pragma pack directive */\n";
2113         print FILEO "$pack_indent$pack_header$_";
2114
2115       }
2116       $modified=1;
2117
2118     } elsif ($is_rc) {
2119       if ($rc_block_depth == 0 and /^(\w+\s+(BITMAP|CURSOR|FONT|FONTDIR|ICON|MESSAGETABLE|TEXT|RTF)\s+((DISCARDABLE|FIXED|IMPURE|LOADONCALL|MOVEABLE|PRELOAD|PURE)\s+)*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
2120         my $from_file=($5 eq "<"?"":$dirname);
2121         my $real_include_name=get_real_include_name($line,$6,$from_file,$project,$target);
2122         print FILEO "$1$5$real_include_name$7$'";
2123         $modified|=($real_include_name ne $6);
2124
2125       } elsif (/^(\s*RCINCLUDE\s*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
2126         my $from_file=($2 eq "<"?"":$dirname);
2127         my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
2128         print FILEO "$1$2$real_include_name$4$'";
2129         $modified|=($real_include_name ne $3);
2130
2131       } elsif ($is_rc and !$is_mfc and $rc_block_depth == 0 and /^\s*\d+\s+TEXTINCLUDE\s*/) {
2132         $rc_textinclude_state=1;
2133         print FILEO;
2134
2135       } elsif ($rc_textinclude_state == 3 and /^(\s*\"\#\s*include\s*\"\")afxres\.h(\"\"\\r\\n\")/) {
2136         print FILEO "$1winresrc.h$2$'";
2137         $modified=1;
2138
2139       } elsif (/^\s*BEGIN(\W.*)?$/) {
2140         $rc_textinclude_state|=2;
2141         $rc_block_depth++;
2142         print FILEO;
2143
2144       } elsif (/^\s*END(\W.*)?$/) {
2145         $rc_textinclude_state=0;
2146         if ($rc_block_depth>0) {
2147           $rc_block_depth--;
2148         }
2149         print FILEO;
2150
2151       } else {
2152         print FILEO;
2153       }
2154
2155     } else {
2156       print FILEO;
2157     }
2158   }
2159
2160   close(FILEI);
2161   close(FILEO);
2162   if ($opt_backup == 0 or $modified == 0) {
2163     if (!unlink("$filename.bak")) {
2164       print STDERR "error: unable to delete $filename.bak:\n";
2165       print STDERR "       $!\n";
2166     }
2167   }
2168 }
2169
2170 ##
2171 # Analyzes each source file in turn to find and correct issues
2172 # that would cause it not to compile.
2173 sub fix_source()
2174 {
2175   print "Fixing the source files...\n";
2176   foreach my $project (@projects) {
2177     foreach my $target (@$project[$P_SETTINGS],@{@$project[$P_TARGETS]}) {
2178       foreach my $source (@{@$target[$T_SOURCES_C]}, @{@$target[$T_SOURCES_CXX]}, @{@$target[$T_SOURCES_RC]}, @{@$target[$T_SOURCES_MISC]}) {
2179         fix_file($source,$project,$target);
2180       }
2181     }
2182   }
2183 }
2184
2185
2186
2187 #####
2188 #
2189 # File generation
2190 #
2191 #####
2192
2193 ##
2194 # A convenience function to generate all the lists (defines,
2195 # C sources, C++ source, etc.) in the Makefile
2196 sub generate_list($$$;$)
2197 {
2198   my $name=$_[0];
2199   my $last=$_[1];
2200   my $list=$_[2];
2201   my $data=$_[3];
2202   my $first=$name;
2203
2204   if ($name) {
2205     printf FILEO "%-22s=",$name;
2206   }
2207   if (defined $list) {
2208     foreach my $item (@$list) {
2209       my $value;
2210       if (defined $data) {
2211         $value=&$data($item);
2212       } else {
2213         $value=$item;
2214       }
2215       if ($value ne "") {
2216         if ($first) {
2217           print FILEO " $value";
2218           $first=0;
2219         } else {
2220           print FILEO " \\\n\t\t\t$value";
2221         }
2222       }
2223     }
2224   }
2225   if ($last) {
2226     print FILEO "\n";
2227   }
2228 }
2229
2230 ##
2231 # Generates a project's Makefile and all the target files
2232 sub generate_project_files($)
2233 {
2234   my $project=$_[0];
2235   my $project_settings=@$project[$P_SETTINGS];
2236   my @dll_list=();
2237   my @exe_list=();
2238
2239   # Then sort the targets and separate the libraries from the programs
2240   foreach my $target (sort { @$a[$T_NAME] cmp @$b[$T_NAME] } @{@$project[$P_TARGETS]}) {
2241     if (@$target[$T_TYPE] == $TT_DLL) {
2242       push @dll_list,$target;
2243     } else {
2244       push @exe_list,$target;
2245     }
2246   }
2247   @$project[$P_TARGETS]=[];
2248   push @{@$project[$P_TARGETS]}, @dll_list;
2249   push @{@$project[$P_TARGETS]}, @exe_list;
2250
2251   if (!open(FILEO,">@$project[$P_PATH]Makefile")) {
2252     print STDERR "error: could not open \"@$project[$P_PATH]/Makefile\" for writing\n";
2253     print STDERR "       $!\n";
2254     return;
2255   }
2256
2257   print FILEO "### Generated by Winemaker $version\n";
2258   print FILEO "\n\n";
2259
2260   generate_list("SRCDIR",1,[ "." ]);
2261   if (@$project[$P_PATH] eq "") {
2262     # This is the main project. It is also responsible for recursively
2263     # calling the other projects
2264     generate_list("SUBDIRS",1,\@projects,sub
2265                   {
2266                     if ($_[0] != \@main_project) {
2267                       my $subdir=@{$_[0]}[$P_PATH];
2268                       $subdir =~ s+/$++;
2269                       return $subdir;
2270                     }
2271                     # Eliminating the main project by returning undefined!
2272                   });
2273   }
2274   if (@{@$project[$P_TARGETS]} > 0) {
2275     generate_list("DLLS",1,\@dll_list,sub
2276                   {
2277                     return @{$_[0]}[$T_NAME];
2278                   });
2279     generate_list("EXES",1,\@exe_list,sub
2280                   {
2281                     return "@{$_[0]}[$T_NAME]";
2282                   });
2283     print FILEO "\n\n\n";
2284
2285     print FILEO "### Common settings\n\n";
2286     # Make it so that the project-wide settings override the global settings
2287     generate_list("CEXTRA",1,@$project_settings[$T_CEXTRA]);
2288     generate_list("CXXEXTRA",1,@$project_settings[$T_CXXEXTRA]);
2289     generate_list("RCEXTRA",1,@$project_settings[$T_RCEXTRA]);
2290     generate_list("DEFINES",1,@$project_settings[$T_DEFINES]);
2291     generate_list("INCLUDE_PATH",1,@$project_settings[$T_INCLUDE_PATH]);
2292     generate_list("DLL_PATH",1,@$project_settings[$T_DLL_PATH]);
2293     generate_list("DLL_IMPORTS",1,@$project_settings[$T_DLLS]);
2294     generate_list("LIBRARY_PATH",1,@$project_settings[$T_LIBRARY_PATH]);
2295     generate_list("LIBRARIES",1,@$project_settings[$T_LIBRARIES]);
2296     print FILEO "\n\n";
2297
2298     my $extra_source_count=@{@$project_settings[$T_SOURCES_C]}+
2299                            @{@$project_settings[$T_SOURCES_CXX]}+
2300                            @{@$project_settings[$T_SOURCES_RC]};
2301     my $no_extra=($extra_source_count == 0);
2302     if (!$no_extra) {
2303       print FILEO "### Extra source lists\n\n";
2304       generate_list("EXTRA_C_SRCS",1,@$project_settings[$T_SOURCES_C]);
2305       generate_list("EXTRA_CXX_SRCS",1,@$project_settings[$T_SOURCES_CXX]);
2306       generate_list("EXTRA_RC_SRCS",1,@$project_settings[$T_SOURCES_RC]);
2307       print FILEO "\n";
2308       generate_list("EXTRA_OBJS",1,["\$(EXTRA_C_SRCS:.c=.o)","\$(EXTRA_CXX_SRCS:.cpp=.o)"]);
2309       print FILEO "\n\n\n";
2310     }
2311
2312     # Iterate over all the targets...
2313     foreach my $target (@{@$project[$P_TARGETS]}) {
2314       print FILEO "### @$target[$T_NAME] sources and settings\n\n";
2315       my $canon=canonize("@$target[$T_NAME]");
2316       $canon =~ s+_so$++;
2317
2318       generate_list("${canon}_MODULE",1,[@$target[$T_NAME]]);
2319       generate_list("${canon}_C_SRCS",1,@$target[$T_SOURCES_C]);
2320       generate_list("${canon}_CXX_SRCS",1,@$target[$T_SOURCES_CXX]);
2321       generate_list("${canon}_RC_SRCS",1,@$target[$T_SOURCES_RC]);
2322       generate_list("${canon}_LDFLAGS",1,@$target[$T_LDFLAGS]);
2323       generate_list("${canon}_DLL_PATH",1,@$target[$T_DLL_PATH]);
2324       generate_list("${canon}_DLLS",1,@$target[$T_DLLS]);
2325       generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH]);
2326       generate_list("${canon}_LIBRARIES",1,@$target[$T_LIBRARIES]);
2327       print FILEO "\n";
2328       generate_list("${canon}_OBJS",1,["\$(${canon}_C_SRCS:.c=.o)","\$(${canon}_CXX_SRCS:.cpp=.o)","\$(${canon}_RC_SRCS:.rc=.res)"]);
2329       print FILEO "\n\n\n";
2330     }
2331     print FILEO "### Global source lists\n\n";
2332     generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub
2333                   {
2334                     my $canon=canonize(@{$_[0]}[$T_NAME]);
2335                     $canon =~ s+_so$++;
2336                     return "\$(${canon}_C_SRCS)";
2337                   });
2338     if (!$no_extra) {
2339       generate_list("",1,[ "\$(EXTRA_C_SRCS)" ]);
2340     }
2341     generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub
2342                   {
2343                     my $canon=canonize(@{$_[0]}[$T_NAME]);
2344                     $canon =~ s+_so$++;
2345                     return "\$(${canon}_CXX_SRCS)";
2346                   });
2347     if (!$no_extra) {
2348       generate_list("",1,[ "\$(EXTRA_CXX_SRCS)" ]);
2349     }
2350     generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub
2351                   {
2352                     my $canon=canonize(@{$_[0]}[$T_NAME]);
2353                     $canon =~ s+_so$++;
2354                     return "\$(${canon}_RC_SRCS)";
2355                   });
2356     if (!$no_extra) {
2357       generate_list("",1,[ "\$(EXTRA_RC_SRCS)" ]);
2358     }
2359   }
2360   print FILEO "\n\n";
2361   print FILEO "### Tools\n\n";
2362   print FILEO "CC = winegcc\n";
2363   print FILEO "CXX = wineg++\n";
2364   print FILEO "RC = wrc\n";
2365   print FILEO "\n\n";
2366
2367   print FILEO "### Generic targets\n\n";
2368   print FILEO "all:";
2369   if (@$project[$P_PATH] eq "") {
2370     print FILEO " \$(SUBDIRS)";
2371   }
2372   if (@{@$project[$P_TARGETS]} > 0) {
2373     print FILEO " \$(DLLS:%=%.so) \$(EXES:%=%.so)";
2374   }
2375   print FILEO "\n\n";
2376   print FILEO "### Build rules\n";
2377   print FILEO "\n";
2378   print FILEO ".PHONY: all clean dummy\n";
2379   print FILEO "\n";
2380   print FILEO "\$(SUBDIRS): dummy\n";
2381   print FILEO "\t\@cd \$\@ && \$(MAKE)\n";
2382   print FILEO "\n";
2383   print FILEO "# Implicit rules\n";
2384   print FILEO "\n";
2385   print FILEO ".SUFFIXES: .cpp .rc .res\n";
2386   print FILEO "DEFINCL = \$(INCLUDE_PATH) \$(DEFINES) \$(OPTIONS)\n";
2387   print FILEO "\n";
2388   print FILEO ".c.o:\n";
2389   print FILEO "\t\$(CC) -c \$(CFLAGS) \$(CEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
2390   print FILEO "\n";
2391   print FILEO ".cpp.o:\n";
2392   print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
2393   print FILEO "\n";
2394   print FILEO ".cxx.o:\n";
2395   print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
2396   print FILEO "\n";
2397   print FILEO ".rc.res:\n";
2398   print FILEO "\t\$(RC) \$(RCFLAGS) \$(RCEXTRA) \$(DEFINCL) -fo\$@ \$<\n";
2399   print FILEO "\n";
2400   print FILEO "# Rules for cleaning\n";
2401   print FILEO "\n";
2402   print FILEO "CLEAN_FILES     = y.tab.c y.tab.h lex.yy.c core *.orig *.rej \\\n";
2403   print FILEO "                  \\\\\\#*\\\\\\# *~ *% .\\\\\\#*\n";
2404   print FILEO "\n";
2405   print FILEO "clean:: \$(SUBDIRS:%=%/__clean__) \$(EXTRASUBDIRS:%=%/__clean__)\n";
2406   print FILEO "\t\$(RM) \$(CLEAN_FILES) \$(RC_SRCS:.rc=.res) \$(C_SRCS:.c=.o) \$(CXX_SRCS:.cpp=.o)\n";
2407   print FILEO "\t\$(RM) \$(DLLS:%=%.so) \$(EXES:%=%.so) \$(EXES:%.exe=%)\n";
2408   print FILEO "\n";
2409   print FILEO "\$(SUBDIRS:%=%/__clean__): dummy\n";
2410   print FILEO "\tcd `dirname \$\@` && \$(MAKE) clean\n";
2411   print FILEO "\n";
2412   print FILEO "\$(EXTRASUBDIRS:%=%/__clean__): dummy\n";
2413   print FILEO "\t-cd `dirname \$\@` && \$(RM) \$(CLEAN_FILES)\n";
2414   print FILEO "\n";
2415
2416   if (@{@$project[$P_TARGETS]} > 0) {
2417     print FILEO "### Target specific build rules\n";
2418     print FILEO "DEFLIB = \$(LIBRARY_PATH) \$(LIBRARIES) \$(DLL_PATH) \$(DLL_IMPORTS:%=-l%)\n\n";
2419     foreach my $target (@{@$project[$P_TARGETS]}) {
2420       my $canon=canonize("@$target[$T_NAME]");
2421       $canon =~ s/_so$//;
2422
2423       print FILEO "\$(${canon}_MODULE).so: \$(${canon}_OBJS)\n";
2424       if (@{@$target[$T_SOURCES_CXX]} > 0 or @{@$project_settings[$T_SOURCES_CXX]} > 0) {
2425         print FILEO "\t\$(CXX)";
2426       } else {
2427         print FILEO "\t\$(CC)";
2428       }
2429       print FILEO " \$(${canon}_LDFLAGS) -o \$\@ \$(${canon}_OBJS) \$(${canon}_LIBRARY_PATH) \$(DEFLIB) \$(${canon}_DLLS:%=-l%) \$(${canon}_LIBRARIES:%=-l%)\n";
2430       print FILEO "\n\n";
2431     }
2432   }
2433   close(FILEO);
2434
2435 }
2436
2437
2438 ##
2439 # This is where we finally generate files. In fact this method does not
2440 # do anything itself but calls the methods that do the actual work.
2441 sub generate()
2442 {
2443   print "Generating project files...\n";
2444
2445   foreach my $project (@projects) {
2446     my $path=@$project[$P_PATH];
2447     if ($path eq "") {
2448       $path=".";
2449     } else {
2450       $path =~ s+/$++;
2451     }
2452     print "  $path\n";
2453     generate_project_files($project);
2454   }
2455 }
2456
2457
2458
2459 #####
2460 #
2461 # Option defaults
2462 #
2463 #####
2464
2465 $opt_backup=1;
2466 $opt_lower=$OPT_LOWER_UPPERCASE;
2467 $opt_lower_include=1;
2468
2469 $opt_work_dir=undef;
2470 $opt_single_target=undef;
2471 $opt_target_type=$TT_GUIEXE;
2472 $opt_flags=0;
2473 $opt_arch=$OPT_ARCH_32;
2474 $opt_is_interactive=$OPT_ASK_NO;
2475 $opt_ask_project_options=$OPT_ASK_NO;
2476 $opt_ask_target_options=$OPT_ASK_NO;
2477 $opt_no_generated_files=0;
2478 $opt_no_source_fix=0;
2479 $opt_no_banner=0;
2480
2481
2482
2483 #####
2484 #
2485 # Main
2486 #
2487 #####
2488
2489 sub print_banner()
2490 {
2491   print "Winemaker $version\n";
2492   print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
2493   print "Copyright 2004 Dimitrie O. Paun\n";
2494   print "Copyright 2009 AndrĂ© Hentschel\n";
2495 }
2496
2497 sub usage()
2498 {
2499   print_banner();
2500   print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup] [--nosource-fix]\n";
2501   print STDERR "                 [--lower-none|--lower-all|--lower-uppercase]\n";
2502   print STDERR "                 [--lower-include|--nolower-include] [--mfc|--nomfc]\n";
2503   print STDERR "                 [--guiexe|--windows|--cuiexe|--console|--dll]\n";
2504   print STDERR "                 [-Dmacro[=defn]] [-Idir] [-Pdir] [-idll] [-Ldir] [-llibrary]\n";
2505   print STDERR "                 [--nodlls] [--nomsvcrt] [--interactive] [--single-target name]\n";
2506   print STDERR "                 [--generated-files|--nogenerated-files]\n";
2507   print STDERR "                 [--wine64]\n";
2508   print STDERR "                 work_directory|project_file|workspace_file\n";
2509   print STDERR "\nWinemaker is designed to recursively convert all the Windows sources found in\n";
2510   print STDERR "the specified directory so that they can be compiled with Winelib. During this\n";
2511   print STDERR "process it will modify and rename some of the files in that directory.\n";
2512   print STDERR "\tPlease read the manual page before use.\n";
2513   exit (2);
2514 }
2515
2516 target_init(\@global_settings);
2517
2518 while (@ARGV>0) {
2519   my $arg=shift @ARGV;
2520   # General options
2521   if ($arg eq "--nobanner") {
2522     $opt_no_banner=1;
2523   } elsif ($arg eq "--backup") {
2524     $opt_backup=1;
2525   } elsif ($arg eq "--nobackup") {
2526     $opt_backup=0;
2527   } elsif ($arg eq "--single-target") {
2528     $opt_single_target=shift @ARGV;
2529   } elsif ($arg eq "--lower-none") {
2530     $opt_lower=$OPT_LOWER_NONE;
2531   } elsif ($arg eq "--lower-all") {
2532     $opt_lower=$OPT_LOWER_ALL;
2533   } elsif ($arg eq "--lower-uppercase") {
2534     $opt_lower=$OPT_LOWER_UPPERCASE;
2535   } elsif ($arg eq "--lower-include") {
2536     $opt_lower_include=1;
2537   } elsif ($arg eq "--nolower-include") {
2538     $opt_lower_include=0;
2539   } elsif ($arg eq "--nosource-fix") {
2540     $opt_no_source_fix=1;
2541   } elsif ($arg eq "--generated-files") {
2542     $opt_no_generated_files=0;
2543   } elsif ($arg eq "--nogenerated-files") {
2544     $opt_no_generated_files=1;
2545   } elsif ($arg eq "--wine64") {
2546     $opt_arch=$OPT_ARCH_64;
2547   } elsif ($arg =~ /^-D/) {
2548     push @{$global_settings[$T_DEFINES]},$arg;
2549   } elsif ($arg =~ /^-I/) {
2550     push @{$global_settings[$T_INCLUDE_PATH]},$arg;
2551   } elsif ($arg =~ /^-P/) {
2552     push @{$global_settings[$T_DLL_PATH]},"-L$'";
2553   } elsif ($arg =~ /^-i/) {
2554     push @{$global_settings[$T_DLLS]},$';
2555   } elsif ($arg =~ /^-L/) {
2556     push @{$global_settings[$T_LIBRARY_PATH]},$arg;
2557   } elsif ($arg =~ /^-l/) {
2558     push @{$global_settings[$T_LIBRARIES]},$';
2559
2560   # 'Source'-based method options
2561   } elsif ($arg eq "--dll") {
2562     $opt_target_type=$TT_DLL;
2563   } elsif ($arg eq "--guiexe" or $arg eq "--windows") {
2564     $opt_target_type=$TT_GUIEXE;
2565   } elsif ($arg eq "--cuiexe" or $arg eq "--console") {
2566     $opt_target_type=$TT_CUIEXE;
2567   } elsif ($arg eq "--interactive") {
2568     $opt_is_interactive=$OPT_ASK_YES;
2569     $opt_ask_project_options=$OPT_ASK_YES;
2570     $opt_ask_target_options=$OPT_ASK_YES;
2571   } elsif ($arg eq "--mfc") {
2572     $opt_flags|=$TF_MFC;
2573   } elsif ($arg eq "--nomfc") {
2574     $opt_flags&=~$TF_MFC;
2575     $opt_flags|=$TF_NOMFC;
2576   } elsif ($arg eq "--nodlls") {
2577     $opt_flags|=$TF_NODLLS;
2578   } elsif ($arg eq "--nomsvcrt") {
2579     $opt_flags|=$TF_NOMSVCRT;
2580
2581   # Catch errors
2582   } else {
2583     if ($arg ne "--help" and $arg ne "-h" and $arg ne "-?") {
2584         if (!defined $opt_work_dir and !defined $opt_work_file) {
2585             if (-f $arg) {
2586                 $opt_work_file=$arg;
2587             }
2588             else {
2589                 $opt_work_dir=$arg;
2590             }
2591         } else {
2592             print STDERR "error: the work directory, \"$arg\", has already been specified (was \"$opt_work_dir\")\n";
2593             usage();
2594         }
2595     } else {
2596         usage();
2597     }
2598   }
2599 }
2600
2601 if (!defined $opt_work_dir and !defined $opt_work_file) {
2602   print STDERR "error: you must specify the directory or project file containing the sources to be converted\n";
2603   usage();
2604 } elsif (defined $opt_work_dir and !chdir $opt_work_dir) {
2605   print STDERR "error: could not chdir to the work directory\n";
2606   print STDERR "       $!\n";
2607   usage();
2608 }
2609
2610 if ($opt_no_banner == 0) {
2611   print_banner();
2612 }
2613
2614 project_init(\@main_project, "", \@global_settings);
2615
2616 # Fix the file and directory names
2617 fix_file_and_directory_names(".");
2618
2619 # Scan the sources to identify the projects and targets
2620 source_scan();
2621
2622 # Fix the source files
2623 if (! $opt_no_source_fix) {
2624   fix_source();
2625 }
2626
2627 # Generate the Makefile and the spec file
2628 if (! $opt_no_generated_files) {
2629   generate();
2630 }