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