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