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