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