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