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