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