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