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