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