~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/tools/winemaker

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.