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