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