Fix signed/unsigned comparison warnings.
[wine] / tools / winemaker
1 #!/usr/bin/perl -w
2 use strict;
3
4 # Copyright 2000-2004 Francois Gouget for CodeWeavers
5 # Copyright 2004 Dimitrie O. Paun
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 #
21
22 my $version="0.6.0";
23
24 use Cwd;
25 use File::Basename;
26 use File::Copy;
27
28
29
30 #####
31 #
32 # Options
33 #
34 #####
35
36 # The following constants define what we do with the case of filenames
37
38 ##
39 # Never rename a file to lowercase
40 my $OPT_LOWER_NONE=0;
41
42 ##
43 # Rename all files to lowercase
44 my $OPT_LOWER_ALL=1;
45
46 ##
47 # Rename only files that are all uppercase to lowercase
48 my $OPT_LOWER_UPPERCASE=2;
49
50
51 # The following constants define whether to ask questions or not
52
53 ##
54 # No (synonym of never)
55 my $OPT_ASK_NO=0;
56
57 ##
58 # Yes (always)
59 my $OPT_ASK_YES=1;
60
61 ##
62 # Skip the questions till the end of this scope
63 my $OPT_ASK_SKIP=-1;
64
65
66 # General options
67
68 ##
69 # This is the directory in which winemaker will operate.
70 my $opt_work_dir;
71
72 ##
73 # Make a backup of the files
74 my $opt_backup;
75
76 ##
77 # Defines which files to rename
78 my $opt_lower;
79
80 ##
81 # If we don't find the file referenced by an include, lower it
82 my $opt_lower_include;
83
84 ##
85 # If true then winemaker should not attempt to fix the source.  This is
86 # useful if the source is known to be already in a suitable form and is
87 # readonly
88 my $opt_no_source_fix;
89
90 # Options for the 'Source' method
91
92 ##
93 # Specifies that we have only one target so that all sources relate
94 # to this target. By default this variable is left undefined which
95 # means winemaker should try to find out by itself what the targets
96 # are. If not undefined then this contains the name of the default
97 # target (without the extension).
98 my $opt_single_target;
99
100 ##
101 # If '$opt_single_target' has been specified then this is the type of
102 # that target. Otherwise it specifies whether the default target type
103 # is guiexe or cuiexe.
104 my $opt_target_type;
105
106 ##
107 # Contains the default set of flags to be used when creating a new target.
108 my $opt_flags;
109
110 ##
111 # If true then winemaker should ask questions to the user as it goes
112 # along.
113 my $opt_is_interactive;
114 my $opt_ask_project_options;
115 my $opt_ask_target_options;
116
117 ##
118 # If false then winemaker should not generate the makefiles.
119 my $opt_no_generated_files;
120
121 ##
122 # Specifies not to print the banner if set.
123 my $opt_no_banner;
124
125
126
127 #####
128 #
129 # Target modelization
130 #
131 #####
132
133 # The description of a target is stored in an array. The constants
134 # below identify what is stored at each index of the array.
135
136 ##
137 # This is the name of the target.
138 my $T_NAME=0;
139
140 ##
141 # Defines the type of target we want to build. See the TT_xxx
142 # constants below
143 my $T_TYPE=1;
144
145 ##
146 # This is a bitfield containing flags refining the way the target
147 # should be handled. See the TF_xxx constants below
148 my $T_FLAGS=2;
149
150 ##
151 # This is a reference to an array containing the list of the
152 # resp. C, C++, RC, other (.h, .hxx, etc.) source files.
153 my $T_SOURCES_C=3;
154 my $T_SOURCES_CXX=4;
155 my $T_SOURCES_RC=5;
156 my $T_SOURCES_MISC=6;
157
158 ##
159 # This is a reference to an array containing the list of
160 # C compiler options
161 my $T_CEXTRA=7;
162
163 ##
164 # This is a reference to an array containing the list of
165 # C++ compiler options
166 my $T_CXXEXTRA=8;
167
168 ##
169 # This is a reference to an array containing the list of
170 # RC compiler options
171 my $T_RCEXTRA=9;
172
173 ##
174 # This is a reference to an array containing the list of macro
175 # definitions
176 my $T_DEFINES=10;
177
178 ##
179 # This is a reference to an array containing the list of directory
180 # names that constitute the include path
181 my $T_INCLUDE_PATH=11;
182
183 ##
184 # Flags for the linker
185 my $T_LDFLAGS=12;
186
187 ##
188 # Same as T_INCLUDE_PATH but for the dll search path
189 my $T_DLL_PATH=13;
190
191 ##
192 # The list of Windows dlls to import
193 my $T_DLLS=14;
194
195 ##
196 # Same as T_INCLUDE_PATH but for the library search path
197 my $T_LIBRARY_PATH=15;
198
199 ##
200 # The list of Unix libraries to link with
201 my $T_LIBRARIES=16;
202
203 ##
204 # The list of dependencies between targets
205 my $T_DEPENDS=17;
206
207
208 # The following constants define the recognized types of target
209
210 ##
211 # This is not a real target. This type of target is used to collect
212 # the sources that don't seem to belong to any other target. Thus no
213 # real target is generated for them, we just put the sources of the
214 # fake target in the global source list.
215 my $TT_SETTINGS=0;
216
217 ##
218 # For executables in the windows subsystem
219 my $TT_GUIEXE=1;
220
221 ##
222 # For executables in the console subsystem
223 my $TT_CUIEXE=2;
224
225 ##
226 # For dynamically linked libraries
227 my $TT_DLL=3;
228
229
230 # The following constants further refine how the target should be handled
231
232 ##
233 # This target is an MFC-based target
234 my $TF_MFC=4;
235
236 ##
237 # User has specified --nomfc option for this target or globally
238 my $TF_NOMFC=8;
239
240 ##
241 # --nodlls option: Do not use standard DLL set
242 my $TF_NODLLS=16;
243
244 ##
245 # --nomsvcrt option: Do not link with msvcrt
246 my $TF_NOMSVCRT=32;
247
248 ##
249 # Initialize a target:
250 # - set the target type to TT_SETTINGS, i.e. no real target will
251 #   be generated.
252 sub target_init($)
253 {
254   my $target=$_[0];
255
256   @$target[$T_TYPE]=$TT_SETTINGS;
257   # leaving $T_INIT undefined
258   @$target[$T_FLAGS]=$opt_flags;
259   @$target[$T_SOURCES_C]=[];
260   @$target[$T_SOURCES_CXX]=[];
261   @$target[$T_SOURCES_RC]=[];
262   @$target[$T_SOURCES_MISC]=[];
263   @$target[$T_CEXTRA]=[];
264   @$target[$T_CXXEXTRA]=[];
265   @$target[$T_RCEXTRA]=[];
266   @$target[$T_DEFINES]=[];
267   @$target[$T_INCLUDE_PATH]=[];
268   @$target[$T_LDFLAGS]=[];
269   @$target[$T_DLL_PATH]=[];
270   @$target[$T_DLLS]=[];
271   @$target[$T_LIBRARY_PATH]=[];
272   @$target[$T_LIBRARIES]=[];
273 }
274
275
276
277 #####
278 #
279 # Project modelization
280 #
281 #####
282
283 # First we have the notion of project. A project is described by an
284 # array (since we don't have structs in perl). The constants below
285 # identify what is stored at each index of the array.
286
287 ##
288 # This is the path in which this project is located. In other
289 # words, this is the path to  the Makefile.
290 my $P_PATH=0;
291
292 ##
293 # This index contains a reference to an array containing the project-wide
294 # settings. The structure of that arrray is actually identical to that of
295 # a regular target since it can also contain extra sources.
296 my $P_SETTINGS=1;
297
298 ##
299 # This index contains a reference to an array of targets for this
300 # project. Each target describes how an executable or library is to
301 # be built. For each target this description takes the same form as
302 # that of the project: an array. So this entry is an array of arrays.
303 my $P_TARGETS=2;
304
305 ##
306 # Initialize a project:
307 # - set the project's path
308 # - initialize the target list
309 # - create a default target (will be removed later if unnecessary)
310 sub project_init($$)
311 {
312   my $project=$_[0];
313   my $path=$_[1];
314
315   my $project_settings=[];
316   target_init($project_settings);
317
318   @$project[$P_PATH]=$path;
319   @$project[$P_SETTINGS]=$project_settings;
320   @$project[$P_TARGETS]=[];
321 }
322
323
324
325 #####
326 #
327 # Global variables
328 #
329 #####
330
331 my %warnings;
332
333 my %templates;
334
335 ##
336 # Contains the list of all projects. This list tells us what are
337 # the subprojects of the main Makefile and where we have to generate
338 # Makefiles.
339 my @projects=();
340
341 ##
342 # This is the main project, i.e. the one in the "." directory.
343 # It may well be empty in which case the main Makefile will only
344 # call out subprojects.
345 my @main_project;
346
347 ##
348 # Contains the defaults for the include path, etc.
349 # We store the defaults as if this were a target except that we only
350 # exploit the defines, include path, library path, library list and misc
351 # sources fields.
352 my @global_settings;
353
354
355
356 #####
357 #
358 # Utility functions
359 #
360 #####
361
362 ##
363 # Cleans up a name to make it an acceptable Makefile
364 # variable name.
365 sub canonize($)
366 {
367   my $name=$_[0];
368
369   $name =~ tr/a-zA-Z0-9_/_/c;
370   return $name;
371 }
372
373 ##
374 # Returns true is the specified pathname is absolute.
375 # Note: pathnames that start with a variable '$' or
376 # '~' are considered absolute.
377 sub is_absolute($)
378 {
379   my $path=$_[0];
380
381   return ($path =~ /^[\/~\$]/);
382 }
383
384 ##
385 # Performs a binary search looking for the specified item
386 sub bsearch($$)
387 {
388   my $array=$_[0];
389   my $item=$_[1];
390   my $last=@{$array}-1;
391   my $first=0;
392
393   while ($first<=$last) {
394     my $index=int(($first+$last)/2);
395     my $cmp=@$array[$index] cmp $item;
396     if ($cmp<0) {
397       $first=$index+1;
398     } elsif ($cmp>0) {
399       $last=$index-1;
400     } else {
401       return $index;
402     }
403   }
404 }
405
406
407
408 #####
409 #
410 # 'Source'-based Project analysis
411 #
412 #####
413
414 ##
415 # Allows the user to specify makefile and target specific options
416 # - target: the structure in which to store the results
417 # - options: the string containing the options
418 sub source_set_options($$)
419 {
420   my $target=$_[0];
421   my $options=$_[1];
422
423   #FIXME: we must deal with escaping of stuff and all
424   foreach my $option (split / /,$options) {
425     if (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-D/) {
426       push @{@$target[$T_DEFINES]},$option;
427     } elsif (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-I/) {
428       push @{@$target[$T_INCLUDE_PATH]},$option;
429     } elsif ($option =~ /^-P/) {
430       push @{@$target[$T_DLL_PATH]},"-L$'";
431     } elsif ($option =~ /^-i/) {
432       push @{@$target[$T_DLLS]},"$'";
433     } elsif ($option =~ /^-L/) {
434       push @{@$target[$T_LIBRARY_PATH]},$option;
435     } elsif ($option =~ /^-l/) {
436       push @{@$target[$T_LIBRARIES]},"$'";
437     } elsif ($option =~ /^--mfc/) {
438       @$target[$T_FLAGS]|=$TF_MFC;
439       @$target[$T_FLAGS]&=~$TF_NOMFC;
440     } elsif ($option =~ /^--nomfc/) {
441       @$target[$T_FLAGS]&=~$TF_MFC;
442       @$target[$T_FLAGS]|=$TF_NOMFC;
443     } elsif ($option =~ /^--nodlls/) {
444       @$target[$T_FLAGS]|=$TF_NODLLS;
445     } elsif ($option =~ /^--nomsvcrt/) {
446       @$target[$T_FLAGS]|=$TF_NOMSVCRT;
447     } else {
448       print STDERR "error: unknown option \"$option\"\n";
449       return 0;
450     }
451   }
452   return 1;
453 }
454
455 ##
456 # Scans the specified directory to:
457 # - see if we should create a Makefile in this directory. We normally do
458 #   so if we find a project file and sources
459 # - get a list of targets for this directory
460 # - get the list of source files
461 sub source_scan_directory($$$$);
462 sub source_scan_directory($$$$)
463 {
464   # a reference to the parent's project
465   my $parent_project=$_[0];
466   # the full relative path to the current directory, including a
467   # trailing '/', or an empty string if this is the top level directory
468   my $path=$_[1];
469   # the name of this directory, including a trailing '/', or an empty
470   # string if this is the top level directory
471   my $dirname=$_[2];
472   # if set then no targets will be looked for and the sources will all
473   # end up in the parent_project's 'misc' bucket
474   my $no_target=$_[3];
475
476   # reference to the project for this directory. May not be used
477   my $project;
478   # list of targets found in the 'current' directory
479   my %targets;
480   # list of sources found in the current directory
481   my @sources_c=();
482   my @sources_cxx=();
483   my @sources_rc=();
484   my @sources_misc=();
485   # true if this directory contains a Windows project
486   my $has_win_project=0;
487   # true if this directory contains headers
488   my $has_headers=0;
489   # If we don't find any executable/library then we might make up targets
490   # from the list of .dsp/.mak files we find since they usually have the
491   # same name as their target.
492   my @dsp_files=();
493   my @mak_files=();
494
495   if (defined $opt_single_target or $dirname eq "") {
496     # Either there is a single target and thus a single project,
497     # or we are in the top level directory for which a project
498     # already exists
499     $project=$parent_project;
500   } else {
501     $project=[];
502     project_init($project,$path);
503   }
504   my $project_settings=@$project[$P_SETTINGS];
505
506   # First find out what this directory contains:
507   # collect all sources, targets and subdirectories
508   my $directory=get_directory_contents($path);
509   foreach my $dentry (@$directory) {
510     if ($dentry =~ /^\./) {
511       next;
512     }
513     my $fullentry="$path$dentry";
514     if (-d "$fullentry") {
515       if ($dentry =~ /^(Release|Debug)/i) {
516         # These directories are often used to store the object files and the
517         # resulting executable/library. They should not contain anything else.
518         my @candidates=grep /\.(exe|dll)$/i, @{get_directory_contents("$fullentry")};
519         foreach my $candidate (@candidates) {
520           $targets{$candidate}=1;
521         }
522       } elsif ($dentry =~ /^include/i) {
523         # This directory must contain headers we're going to need
524         push @{@$project_settings[$T_INCLUDE_PATH]},"-I$dentry";
525         source_scan_directory($project,"$fullentry/","$dentry/",1);
526       } else {
527         # Recursively scan this directory. Any source file that cannot be
528         # attributed to a project in one of the subdirectories will be
529         # attributed to this project.
530         source_scan_directory($project,"$fullentry/","$dentry/",$no_target);
531       }
532     } elsif (-f "$fullentry") {
533       if ($dentry =~ /\.(exe|dll)$/i) {
534         $targets{$dentry}=1;
535       } elsif ($dentry =~ /\.c$/i and $dentry !~ /\.(dbg|spec)\.c$/) {
536         push @sources_c,"$dentry";
537       } elsif ($dentry =~ /\.(cpp|cxx)$/i) {
538         if ($dentry =~ /^stdafx.cpp$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
539           push @sources_misc,"$dentry";
540           @$project_settings[$T_FLAGS]|=$TF_MFC;
541         } else {
542           push @sources_cxx,"$dentry";
543         }
544       } elsif ($dentry =~ /\.rc$/i) {
545         push @sources_rc,"$dentry";
546       } elsif ($dentry =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
547         $has_headers=1;
548         push @sources_misc,"$dentry";
549         if ($dentry =~ /^stdafx.h$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
550           @$project_settings[$T_FLAGS]|=$TF_MFC;
551         }
552       } elsif ($dentry =~ /\.dsp$/i) {
553         push @dsp_files,"$dentry";
554         $has_win_project=1;
555       } elsif ($dentry =~ /\.mak$/i) {
556         push @mak_files,"$dentry";
557         $has_win_project=1;
558       } elsif ($dentry =~ /^makefile/i) {
559         $has_win_project=1;
560       }
561     }
562   }
563   closedir(DIRECTORY);
564
565   if ($has_headers) {
566     push @{@$project_settings[$T_INCLUDE_PATH]},"-I.";
567   }
568   # If we have a single target then all we have to do is assign
569   # all the sources to it and we're done
570   # FIXME: does this play well with the --interactive mode?
571   if ($opt_single_target) {
572     my $target=@{@$project[$P_TARGETS]}[0];
573     push @{@$target[$T_SOURCES_C]},map "$path$_",@sources_c;
574     push @{@$target[$T_SOURCES_CXX]},map "$path$_",@sources_cxx;
575     push @{@$target[$T_SOURCES_RC]},map "$path$_",@sources_rc;
576     push @{@$target[$T_SOURCES_MISC]},map "$path$_",@sources_misc;
577     return;
578   }
579   if ($no_target) {
580     my $parent_settings=@$parent_project[$P_SETTINGS];
581     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_c;
582     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_cxx;
583     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_rc;
584     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
585     push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
586     return;
587   }
588
589   my $source_count=@sources_c+@sources_cxx+@sources_rc+
590                    @{@$project_settings[$T_SOURCES_C]}+
591                    @{@$project_settings[$T_SOURCES_CXX]}+
592                    @{@$project_settings[$T_SOURCES_RC]};
593   if ($source_count == 0) {
594     # A project without real sources is not a project, get out!
595     if ($project!=$parent_project) {
596       my $parent_settings=@$parent_project[$P_SETTINGS];
597       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
598       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
599     }
600     return;
601   }
602   #print "targets=",%targets,"\n";
603   #print "target_count=$target_count\n";
604   #print "has_win_project=$has_win_project\n";
605   #print "dirname=$dirname\n";
606
607   my $target_count;
608   if (($has_win_project != 0) or ($dirname eq "")) {
609     # Deal with cases where we could not find any executable/library, and
610     # thus have no target, although we did find some sort of windows project.
611     $target_count=keys %targets;
612     if ($target_count == 0) {
613       # Try to come up with a target list based on .dsp/.mak files
614       my $prj_list;
615       if (@dsp_files > 0) {
616         $prj_list=\@dsp_files;
617       } else {
618         $prj_list=\@mak_files;
619       }
620       foreach my $filename (@$prj_list) {
621         $filename =~ s/\.(dsp|mak)$//i;
622         if ($opt_target_type == $TT_DLL) {
623           $filename = "$filename.dll";
624         }
625         $targets{$filename}=1;
626       }
627       $target_count=keys %targets;
628       if ($target_count == 0) {
629         # Still nothing, try the name of the directory
630         my $name;
631         if ($dirname eq "") {
632           # Bad luck, this is the top level directory!
633           $name=(split /\//, cwd)[-1];
634         } else {
635           $name=$dirname;
636           # Remove the trailing '/'. Also eliminate whatever is after the last
637           # '.' as it is likely to be meaningless (.orig, .new, ...)
638           $name =~ s+(/|\.[^.]*)$++;
639           if ($name eq "src") {
640             # 'src' is probably a subdirectory of the real project directory.
641             # Try again with the parent (if any).
642             my $parent=$path;
643             if ($parent =~ s+([^/]*)/[^/]*/$+$1+) {
644               $name=$parent;
645             } else {
646               $name=(split /\//, cwd)[-1];
647             }
648           }
649         }
650         $name =~ s+(/|\.[^.]*)$++;
651         if ($opt_target_type == $TT_DLL) {
652           $name = "$name.dll";
653         } else {
654           $name = "$name.exe";
655         }
656         $targets{$name}=1;
657       }
658     }
659
660     # Ask confirmation to the user if he wishes so
661     if ($opt_is_interactive == $OPT_ASK_YES) {
662       my $target_list=join " ",keys %targets;
663       print "\n*** In ",($path?$path:"./"),"\n";
664       print "* winemaker found the following list of (potential) targets\n";
665       print "*   $target_list\n";
666       print "* Type enter to use it as is, your own comma-separated list of\n";
667       print "* targets, 'none' to assign the source files to a parent directory,\n";
668       print "* or 'ignore' to ignore everything in this directory tree.\n";
669       print "* Target list:\n";
670       $target_list=<STDIN>;
671       chomp $target_list;
672       if ($target_list eq "") {
673         # Keep the target list as is, i.e. do nothing
674       } elsif ($target_list eq "none") {
675         # Empty the target list
676         undef %targets;
677       } elsif ($target_list eq "ignore") {
678         # Ignore this subtree altogether
679         return;
680       } else {
681         undef %targets;
682         foreach my $target (split /,/,$target_list) {
683           $target =~ s+^\s*++;
684           $target =~ s+\s*$++;
685           $targets{$target}=1;
686         }
687       }
688     }
689   }
690
691   # If we have no project at this level, then transfer all
692   # the sources to the parent project
693   $target_count=keys %targets;
694   if ($target_count == 0) {
695     if ($project!=$parent_project) {
696       my $parent_settings=@$parent_project[$P_SETTINGS];
697       push @{@$parent_settings[$T_SOURCES_C]},map "$dirname$_",@sources_c;
698       push @{@$parent_settings[$T_SOURCES_CXX]},map "$dirname$_",@sources_cxx;
699       push @{@$parent_settings[$T_SOURCES_RC]},map "$dirname$_",@sources_rc;
700       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
701       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
702     }
703     return;
704   }
705
706   # Otherwise add this project to the project list, except for
707   # the main project which is already in the list.
708   if ($dirname ne "") {
709     push @projects,$project;
710   }
711
712   # Ask for project-wide options
713   if ($opt_ask_project_options == $OPT_ASK_YES) {
714     my $flag_desc="";
715     if ((@$project_settings[$T_FLAGS] & $TF_MFC)!=0) {
716       $flag_desc="mfc";
717     }
718     print "* Type any project-wide options (-D/-I/-P/-i/-L/-l/--mfc),\n";
719     if (defined $flag_desc) {
720       print "* (currently $flag_desc)\n";
721     }
722     print "* or 'skip' to skip the target specific options,\n";
723     print "* or 'never' to not be asked this question again:\n";
724     while (1) {
725       my $options=<STDIN>;
726       chomp $options;
727       if ($options eq "skip") {
728         $opt_ask_target_options=$OPT_ASK_SKIP;
729         last;
730       } elsif ($options eq "never") {
731         $opt_ask_project_options=$OPT_ASK_NO;
732         last;
733       } elsif (source_set_options($project_settings,$options)) {
734         last;
735       }
736       print "Please re-enter the options:\n";
737     }
738   }
739
740   # - Create the targets
741   # - Check if we have both libraries and programs
742   # - Match each target with source files (sort in reverse
743   #   alphabetical order to get the longest matches first)
744   my @local_dlls=();
745   my @local_depends=();
746   my @exe_list=();
747   foreach my $target_name (map (lc, (sort { $b cmp $a } keys %targets))) {
748     # Create the target...
749     my $target=[];
750     target_init($target);
751     @$target[$T_NAME]=$target_name;
752     @$target[$T_FLAGS]|=@$project_settings[$T_FLAGS];
753     if ($target_name =~ /\.dll$/) {
754       @$target[$T_TYPE]=$TT_DLL;
755       push @local_depends,"$target_name.so";
756       push @local_dlls,$target_name;
757     } else {
758       @$target[$T_TYPE]=$opt_target_type;
759       push @exe_list,$target;
760       push @{@$target[$T_LDFLAGS]},(@$target[$T_TYPE] == $TT_CUIEXE ? "-mconsole" : "-mwindows");
761     }
762     my $basename=$target_name;
763     $basename=~ s/\.(dll|exe)$//i;
764     # This is the default link list of Visual Studio, except odbccp32
765     # which we don't have in Wine.
766     my @std_imports=qw(odbc32 ole32 oleaut32 winspool);
767     my @std_libraries=qw(uuid);
768     if ((@$target[$T_FLAGS] & $TF_NODLLS) == 0) {
769       @$target[$T_DLLS]=\@std_imports;
770       @$target[$T_LIBRARIES]=\@std_libraries;
771     } else {
772       @$target[$T_DLLS]=[];
773       @$target[$T_LIBRARIES]=[];
774     }
775     if ((@$target[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
776       push @{@$target[$T_LDFLAGS]},"-mno-cygwin";
777     }
778     push @{@$project[$P_TARGETS]},$target;
779
780     # Ask for target-specific options
781     if ($opt_ask_target_options == $OPT_ASK_YES) {
782       my $flag_desc="";
783       if ((@$target[$T_FLAGS] & $TF_MFC)!=0) {
784         $flag_desc=" (mfc";
785       }
786       if ($flag_desc ne "") {
787         $flag_desc.=")";
788       }
789       print "* Specify any link option (-P/-i/-L/-l/--mfc) specific to the target\n";
790       print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
791       while (1) {
792         my $options=<STDIN>;
793         chomp $options;
794         if ($options eq "never") {
795           $opt_ask_target_options=$OPT_ASK_NO;
796           last;
797         } elsif (source_set_options($target,$options)) {
798           last;
799         }
800         print "Please re-enter the options:\n";
801       }
802     }
803     if (@$target[$T_FLAGS] & $TF_MFC) {
804       @$project_settings[$T_FLAGS]|=$TF_MFC;
805       push @{@$target[$T_DLL_PATH]},"\$(MFC_LIBRARY_PATH)";
806       push @{@$target[$T_DLLS]},"mfc.dll";
807       # FIXME: Link with the MFC in the Unix sense, until we
808       # start exporting the functions properly.
809       push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
810       push @{@$target[$T_LIBRARIES]},"mfc";
811     }
812
813     # Match sources...
814     if ($target_count == 1) {
815       push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
816       @$project_settings[$T_SOURCES_C]=[];
817       @sources_c=();
818
819       push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
820       @$project_settings[$T_SOURCES_CXX]=[];
821       @sources_cxx=();
822
823       push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
824       @$project_settings[$T_SOURCES_RC]=[];
825       @sources_rc=();
826
827       push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
828       # No need for sorting these sources
829       @$project_settings[$T_SOURCES_MISC]=[];
830       @sources_misc=();
831     } else {
832       foreach my $source (@sources_c) {
833         if ($source =~ /^$basename/i) {
834           push @{@$target[$T_SOURCES_C]},$source;
835           $source="";
836         }
837       }
838       foreach my $source (@sources_cxx) {
839         if ($source =~ /^$basename/i) {
840           push @{@$target[$T_SOURCES_CXX]},$source;
841           $source="";
842         }
843       }
844       foreach my $source (@sources_rc) {
845         if ($source =~ /^$basename/i) {
846           push @{@$target[$T_SOURCES_RC]},$source;
847           $source="";
848         }
849       }
850       foreach my $source (@sources_misc) {
851         if ($source =~ /^$basename/i) {
852           push @{@$target[$T_SOURCES_MISC]},$source;
853           $source="";
854         }
855       }
856     }
857     @$target[$T_SOURCES_C]=[sort @{@$target[$T_SOURCES_C]}];
858     @$target[$T_SOURCES_CXX]=[sort @{@$target[$T_SOURCES_CXX]}];
859     @$target[$T_SOURCES_RC]=[sort @{@$target[$T_SOURCES_RC]}];
860     @$target[$T_SOURCES_MISC]=[sort @{@$target[$T_SOURCES_MISC]}];
861   }
862   if ($opt_ask_target_options == $OPT_ASK_SKIP) {
863     $opt_ask_target_options=$OPT_ASK_YES;
864   }
865
866   if ((@$project_settings[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
867     push @{@$project_settings[$T_CEXTRA]},"-mno-cygwin";
868     push @{@$project_settings[$T_CXXEXTRA]},"-mno-cygwin";
869   }
870
871   if (@$project_settings[$T_FLAGS] & $TF_MFC) {
872     push @{@$project_settings[$T_INCLUDE_PATH]},"\$(MFC_INCLUDE_PATH)";
873   }
874   # The sources that did not match, if any, go to the extra
875   # source list of the project settings
876   foreach my $source (@sources_c) {
877     if ($source ne "") {
878       push @{@$project_settings[$T_SOURCES_C]},$source;
879     }
880   }
881   @$project_settings[$T_SOURCES_C]=[sort @{@$project_settings[$T_SOURCES_C]}];
882   foreach my $source (@sources_cxx) {
883     if ($source ne "") {
884       push @{@$project_settings[$T_SOURCES_CXX]},$source;
885     }
886   }
887   @$project_settings[$T_SOURCES_CXX]=[sort @{@$project_settings[$T_SOURCES_CXX]}];
888   foreach my $source (@sources_rc) {
889     if ($source ne "") {
890       push @{@$project_settings[$T_SOURCES_RC]},$source;
891     }
892   }
893   @$project_settings[$T_SOURCES_RC]=[sort @{@$project_settings[$T_SOURCES_RC]}];
894   foreach my $source (@sources_misc) {
895     if ($source ne "") {
896       push @{@$project_settings[$T_SOURCES_MISC]},$source;
897     }
898   }
899   @$project_settings[$T_SOURCES_MISC]=[sort @{@$project_settings[$T_SOURCES_MISC]}];
900
901   # Finally if we are building both libraries and programs in
902   # this directory, then the programs should be linked with all
903   # the libraries
904   if (@local_dlls > 0 and @exe_list > 0) {
905     foreach my $target (@exe_list) {
906       push @{@$target[$T_DLL_PATH]},"-L.";
907       push @{@$target[$T_DLLS]},@local_dlls;
908     }
909   }
910 }
911
912 ##
913 # Scan the source directories in search of things to build
914 sub source_scan()
915 {
916   # If there's a single target then this is going to be the default target
917   if (defined $opt_single_target) {
918     # Create the main target
919     my $main_target=[];
920     target_init($main_target);
921     @$main_target[$T_NAME]=$opt_single_target;
922     @$main_target[$T_TYPE]=$opt_target_type;
923
924     # Add it to the list
925     push @{$main_project[$P_TARGETS]},$main_target;
926   }
927
928   # The main directory is always going to be there
929   push @projects,\@main_project;
930
931   # Now scan the directory tree looking for source files and, maybe, targets
932   print "Scanning the source directories...\n";
933   source_scan_directory(\@main_project,"","",0);
934
935   @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
936 }
937
938 #####
939 #
940 # Source search
941 #
942 #####
943
944 ##
945 # Performs a directory traversal and renames the files so that:
946 # - they have the case desired by the user
947 # - their extension is of the appropriate case
948 # - they don't contain annoying characters like ' ', '$', '#', ...
949 sub fix_file_and_directory_names($);
950 sub fix_file_and_directory_names($)
951 {
952   my $dirname=$_[0];
953
954   if (opendir(DIRECTORY, "$dirname")) {
955     foreach my $dentry (readdir DIRECTORY) {
956       if ($dentry =~ /^\./ or $dentry eq "CVS") {
957         next;
958       }
959       # Set $warn to 1 if the user should be warned of the renaming
960       my $warn=0;
961
962       # autoconf and make don't support these characters well
963       my $new_name=$dentry;
964       $new_name =~ s/[ \$]/_/g;
965
966       # Only all lowercase extensions are supported (because of the
967       # transformations ':.c=.o') .
968       if (-f "$dirname/$new_name") {
969         if ($new_name =~ /\.C$/) {
970           $new_name =~ s/\.C$/.c/;
971         }
972         if ($new_name =~ /\.cpp$/i) {
973           $new_name =~ s/\.cpp$/.cpp/i;
974         }
975         if ($new_name =~ s/\.cxx$/.cpp/i) {
976           $warn=1;
977         }
978         if ($new_name =~ /\.rc$/i) {
979           $new_name =~ s/\.rc$/.rc/i;
980         }
981         # And this last one is to avoid confusion then running make
982         if ($new_name =~ s/^makefile$/makefile.win/) {
983           $warn=1;
984         }
985       }
986
987       # Adjust the case to the user's preferences
988       if (($opt_lower == $OPT_LOWER_ALL and $dentry =~ /[A-Z]/) or
989           ($opt_lower == $OPT_LOWER_UPPERCASE and $dentry !~ /[a-z]/)
990          ) {
991         $new_name=lc $new_name;
992       }
993
994       # And finally, perform the renaming
995       if ($new_name ne $dentry) {
996         if ($warn) {
997           print STDERR "warning: in \"$dirname\", renaming \"$dentry\" to \"$new_name\"\n";
998         }
999         if (!rename("$dirname/$dentry","$dirname/$new_name")) {
1000           print STDERR "error: in \"$dirname\", unable to rename \"$dentry\" to \"$new_name\"\n";
1001           print STDERR "       $!\n";
1002           $new_name=$dentry;
1003         }
1004       }
1005       if (-d "$dirname/$new_name") {
1006         fix_file_and_directory_names("$dirname/$new_name");
1007       }
1008     }
1009     closedir(DIRECTORY);
1010   }
1011 }
1012
1013
1014
1015 #####
1016 #
1017 # Source fixup
1018 #
1019 #####
1020
1021 ##
1022 # This maps a directory name to a reference to an array listing
1023 # its contents (files and directories)
1024 my %directories;
1025
1026 ##
1027 # Retrieves the contents of the specified directory.
1028 # We either get it from the directories hashtable which acts as a
1029 # cache, or use opendir, readdir, closedir and store the result
1030 # in the hashtable.
1031 sub get_directory_contents($)
1032 {
1033   my $dirname=$_[0];
1034   my $directory;
1035
1036   #print "getting the contents of $dirname\n";
1037
1038   # check for a cached version
1039   $dirname =~ s+/$++;
1040   if ($dirname eq "") {
1041     $dirname=cwd;
1042   }
1043   $directory=$directories{$dirname};
1044   if (defined $directory) {
1045     #print "->@$directory\n";
1046     return $directory;
1047   }
1048
1049   # Read this directory
1050   if (opendir(DIRECTORY, "$dirname")) {
1051     my @files=readdir DIRECTORY;
1052     closedir(DIRECTORY);
1053     $directory=\@files;
1054   } else {
1055     # Return an empty list
1056     #print "error: cannot open $dirname\n";
1057     my @files;
1058     $directory=\@files;
1059   }
1060   #print "->@$directory\n";
1061   $directories{$dirname}=$directory;
1062   return $directory;
1063 }
1064
1065 ##
1066 # Try to find a file for the specified filename. The attempt is
1067 # case-insensitive which is why it's not trivial. If a match is
1068 # found then we return the pathname with the correct case.
1069 sub search_from($$)
1070 {
1071   my $dirname=$_[0];
1072   my $path=$_[1];
1073   my $real_path="";
1074
1075   if ($dirname eq "" or $dirname eq ".") {
1076     $dirname=cwd;
1077   } elsif ($dirname =~ m+^[^/]+) {
1078     $dirname=cwd . "/" . $dirname;
1079   }
1080   if ($dirname !~ m+/$+) {
1081     $dirname.="/";
1082   }
1083
1084   foreach my $component (@$path) {
1085     #print "    looking for $component in \"$dirname\"\n";
1086     if ($component eq ".") {
1087       # Pass it as is
1088       $real_path.="./";
1089     } elsif ($component eq "..") {
1090       # Go up one level
1091       $dirname=dirname($dirname) . "/";
1092       $real_path.="../";
1093     } else {
1094       # The file/directory may have been renamed before. Also try to
1095       # match the renamed file.
1096       my $renamed=$component;
1097       $renamed =~ s/[ \$]/_/g;
1098       if ($renamed eq $component) {
1099         undef $renamed;
1100       }
1101
1102       my $directory=get_directory_contents $dirname;
1103       my $found;
1104       foreach my $dentry (@$directory) {
1105         if ($dentry =~ /^$component$/i or
1106             (defined $renamed and $dentry =~ /^$renamed$/i)
1107            ) {
1108           $dirname.="$dentry/";
1109           $real_path.="$dentry/";
1110           $found=1;
1111           last;
1112         }
1113       }
1114       if (!defined $found) {
1115         # Give up
1116         #print "    could not find $component in $dirname\n";
1117         return;
1118       }
1119     }
1120   }
1121   $real_path=~ s+/$++;
1122   #print "    -> found $real_path\n";
1123   return $real_path;
1124 }
1125
1126 ##
1127 # Performs a case-insensitive search for the specified file in the
1128 # include path.
1129 # $line is the line number that should be referenced when an error occurs
1130 # $filename is the file we are looking for
1131 # $dirname is the directory of the file containing the '#include' directive
1132 #    if '"' was used, it is an empty string otherwise
1133 # $project and $target specify part of the include path
1134 sub get_real_include_name($$$$$)
1135 {
1136   my $line=$_[0];
1137   my $filename=$_[1];
1138   my $dirname=$_[2];
1139   my $project=$_[3];
1140   my $target=$_[4];
1141
1142   if ($filename =~ /^([a-zA-Z]:)?[\/]/ or $filename =~ /^[a-zA-Z]:[\/]?/) {
1143     # This is not a relative path, we cannot make any check
1144     my $warning="path:$filename";
1145     if (!defined $warnings{$warning}) {
1146       $warnings{$warning}="1";
1147       print STDERR "warning: cannot check the case of absolute pathnames:\n";
1148       print STDERR "$line:   $filename\n";
1149     }
1150   } else {
1151     # Here's how we proceed:
1152     # - split the filename we look for into its components
1153     # - then for each directory in the include path
1154     #   - trace the directory components starting from that directory
1155     #   - if we fail to find a match at any point then continue with
1156     #     the next directory in the include path
1157     #   - otherwise, rejoice, our quest is over.
1158     my @file_components=split /[\/\\]+/, $filename;
1159     #print "  Searching for $filename from @$project[$P_PATH]\n";
1160
1161     my $real_filename;
1162     if ($dirname ne "") {
1163       # This is an 'include ""' -> look in dirname first.
1164       #print "    in $dirname (include \"\")\n";
1165       $real_filename=search_from($dirname,\@file_components);
1166       if (defined $real_filename) {
1167         return $real_filename;
1168       }
1169     }
1170     my $project_settings=@$project[$P_SETTINGS];
1171     foreach my $include (@{@$target[$T_INCLUDE_PATH]}, @{@$project_settings[$T_INCLUDE_PATH]}) {
1172       my $dirname=$include;
1173       $dirname=~ s+^-I++;
1174       if (!is_absolute($dirname)) {
1175         $dirname="@$project[$P_PATH]$dirname";
1176       } else {
1177         $dirname=~ s+^\$\(TOPSRCDIR\)/++;
1178         $dirname=~ s+^\$\(SRCDIR\)/+@$project[$P_PATH]+;
1179       }
1180       #print "    in $dirname\n";
1181       $real_filename=search_from("$dirname",\@file_components);
1182       if (defined $real_filename) {
1183         return $real_filename;
1184       }
1185     }
1186     my $dotdotpath=@$project[$P_PATH];
1187     $dotdotpath =~ s/[^\/]+/../g;
1188     foreach my $include (@{$global_settings[$T_INCLUDE_PATH]}) {
1189       my $dirname=$include;
1190       $dirname=~ s+^-I++;
1191       $dirname=~ s+^\$\(TOPSRCDIR\)\/++;
1192       $dirname=~ s+^\$\(SRCDIR\)\/+@$project[$P_PATH]+;
1193       #print "    in $dirname  (global setting)\n";
1194       $real_filename=search_from("$dirname",\@file_components);
1195       if (defined $real_filename) {
1196         return $real_filename;
1197       }
1198     }
1199   }
1200   $filename =~ s+\\\\+/+g; # in include ""
1201   $filename =~ s+\\+/+g; # in include <> !
1202   if ($opt_lower_include) {
1203     return lc "$filename";
1204   }
1205   return $filename;
1206 }
1207
1208 sub print_pack($$$)
1209 {
1210   my $indent=$_[0];
1211   my $size=$_[1];
1212   my $trailer=$_[2];
1213
1214   if ($size =~ /^(1|2|4|8)$/) {
1215     print FILEO "$indent#include <pshpack$size.h>$trailer";
1216   } else {
1217     print FILEO "$indent/* winemaker:warning: Unknown size \"$size\". Defaulting to 4 */\n";
1218     print FILEO "$indent#include <pshpack4.h>$trailer";
1219   }
1220 }
1221
1222 ##
1223 # 'Parses' a source file and fixes constructs that would not work with
1224 # Winelib. The parsing is rather simple and not all non-portable features
1225 # are corrected. The most important feature that is corrected is the case
1226 # and path separator of '#include' directives. This requires that each
1227 # source file be associated to a project & target so that the proper
1228 # include path is used.
1229 # Also note that the include path is relative to the directory in which the
1230 # compiler is run, i.e. that of the project, not to that of the file.
1231 sub fix_file($$$)
1232 {
1233   my $filename=$_[0];
1234   my $project=$_[1];
1235   my $target=$_[2];
1236   $filename="@$project[$P_PATH]$filename";
1237   if (! -e $filename) {
1238     return;
1239   }
1240
1241   my $is_rc=($filename =~ /\.(rc2?|dlg)$/i);
1242   my $dirname=dirname($filename);
1243   my $is_mfc=0;
1244   if (defined $target and (@$target[$T_FLAGS] & $TF_MFC)) {
1245     $is_mfc=1;
1246   }
1247
1248   print "  $filename\n";
1249   #FIXME:assuming that because there is a .bak file, this is what we want is
1250   #probably flawed. Or is it???
1251   if (! -e "$filename.bak") {
1252     if (!copy("$filename","$filename.bak")) {
1253       print STDERR "error: unable to make a backup of $filename:\n";
1254       print STDERR "       $!\n";
1255       return;
1256     }
1257   }
1258   if (!open(FILEI,"$filename.bak")) {
1259     print STDERR "error: unable to open $filename.bak for reading:\n";
1260     print STDERR "       $!\n";
1261     return;
1262   }
1263   if (!open(FILEO,">$filename")) {
1264     print STDERR "error: unable to open $filename for writing:\n";
1265     print STDERR "       $!\n";
1266     return;
1267   }
1268   my $line=0;
1269   my $modified=0;
1270   my $rc_block_depth=0;
1271   my $rc_textinclude_state=0;
1272   my @pack_stack;
1273   while (<FILEI>) {
1274     # Remove any trailing CtrlZ, which isn't strictly in the file
1275     if (/\x1A/) {
1276       s/\x1A//;
1277       last if (/^$/)
1278     }
1279     $line++;
1280     s/\r\n$/\n/;
1281     if (!/\n$/) {
1282       # Make sure all files are '\n' terminated
1283       $_ .= "\n";
1284     }
1285     if ($is_rc and !$is_mfc and /^(\s*)(\#\s*include\s*)\"afxres\.h\"/) {
1286       # VC6 automatically includes 'afxres.h', an MFC specific header, in
1287       # the RC files it generates (even in non-MFC projects). So we replace
1288       # it with 'winres.h' its very close standard cousin so that non MFC
1289       # projects can compile in Wine without the MFC sources.
1290       my $warning="mfc:afxres.h";
1291       if (!defined $warnings{$warning}) {
1292         $warnings{$warning}="1";
1293         print STDERR "warning: In non-MFC projects, winemaker replaces the MFC specific header 'afxres.h' with 'winres.h'\n";
1294         print STDERR "warning: the above warning is issued only once\n";
1295       }
1296       print FILEO "$1/* winemaker: $2\"afxres.h\" */\n";
1297       print FILEO "$1/* winemaker:warning: 'afxres.h' is an MFC specific header. Replacing it with 'winres.h' */\n";
1298       print FILEO "$1$2\"winres.h\"$'";
1299       $modified=1;
1300
1301     } elsif (/^(\s*\#\s*include\s*)([\"<])([^\"]+)([\">])/) {
1302       my $from_file=($2 eq "<"?"":$dirname);
1303       my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1304       print FILEO "$1$2$real_include_name$4$'";
1305       $modified|=($real_include_name ne $3);
1306
1307     } elsif (s/^(\s*)(\#\s*pragma\s+pack\s*\(\s*)//) {
1308       # Pragma pack handling
1309       #
1310       # pack_stack is an array of references describing the stack of
1311       # pack directives currently in effect. Each directive if described
1312       # by a reference to an array containing:
1313       # - "push" for pack(push,...) directives, "" otherwise
1314       # - the directive's identifier at index 1
1315       # - the directive's alignement value at index 2
1316       #
1317       # Don't believe a word of what the documentation says: it's all wrong.
1318       # The code below is based on the actual behavior of Visual C/C++ 6.
1319       my $pack_indent=$1;
1320       my $pack_header=$2;
1321       if (/^(\))/) {
1322         # pragma pack()
1323         # Pushes the default stack alignment
1324         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1325         print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1326         print_pack($pack_indent,4,$');
1327         push @pack_stack, [ "", "", 4 ];
1328
1329       } elsif (/^(pop\s*(,\s*\d+\s*)?\))/) {
1330         # pragma pack(pop)
1331         # pragma pack(pop,n)
1332         # Goes up the stack until it finds a pack(push,...), and pops it
1333         # Ignores any pack(n) entry
1334         # Issues a warning if the pack is of the form pack(push,label)
1335         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1336         my $pack_comment=$';
1337         $pack_comment =~ s/^\s*//;
1338         if ($pack_comment ne "") {
1339           print FILEO "$pack_indent$pack_comment";
1340         }
1341         while (1) {
1342           my $alignment=pop @pack_stack;
1343           if (!defined $alignment) {
1344             print FILEO "$pack_indent/* winemaker:warning: No pack(push,...) found. All the stack has been popped */\n";
1345             last;
1346           }
1347           if (@$alignment[1]) {
1348             print FILEO "$pack_indent/* winemaker:warning: Anonymous pop of pack(push,@$alignment[1]) (@$alignment[2]) */\n";
1349           }
1350           print FILEO "$pack_indent#include <poppack.h>\n";
1351           if (@$alignment[0]) {
1352             last;
1353           }
1354         }
1355
1356       } elsif (/^(pop\s*,\s*(\w+)\s*(,\s*\d+\s*)?\))/) {
1357         # pragma pack(pop,label[,n])
1358         # Goes up the stack until finding a pack(push,...) and pops it.
1359         # 'n', if specified, is ignored.
1360         # Ignores any pack(n) entry
1361         # Issues a warning if the label of the pack does not match,
1362         # or if it is in fact a pack(push,n)
1363         my $label=$2;
1364         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1365         my $pack_comment=$';
1366         $pack_comment =~ s/^\s*//;
1367         if ($pack_comment ne "") {
1368           print FILEO "$pack_indent$pack_comment";
1369         }
1370         while (1) {
1371           my $alignment=pop @pack_stack;
1372           if (!defined $alignment) {
1373             print FILEO "$pack_indent/* winemaker:warning: No pack(push,$label) found. All the stack has been popped */\n";
1374             last;
1375           }
1376           if (@$alignment[1] and @$alignment[1] ne $label) {
1377             print FILEO "$pack_indent/* winemaker:warning: Push/pop mismatch: \"@$alignment[1]\" (@$alignment[2]) != \"$label\" */\n";
1378           }
1379           print FILEO "$pack_indent#include <poppack.h>\n";
1380           if (@$alignment[0]) {
1381             last;
1382           }
1383         }
1384
1385       } elsif (/^(push\s*\))/) {
1386         # pragma pack(push)
1387         # Push the current alignment
1388         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1389         if (@pack_stack > 0) {
1390           my $alignment=$pack_stack[$#pack_stack];
1391           print_pack($pack_indent,@$alignment[2],$');
1392           push @pack_stack, [ "push", "", @$alignment[2] ];
1393         } else {
1394           print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1395           print_pack($pack_indent,4,$');
1396           push @pack_stack, [ "push", "", 4 ];
1397         }
1398
1399       } elsif (/^((push\s*,\s*)?(\d+)\s*\))/) {
1400         # pragma pack([push,]n)
1401         # Push new alignment n
1402         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1403         print_pack($pack_indent,$3,"$'");
1404         push @pack_stack, [ ($2 ? "push" : ""), "", $3 ];
1405
1406       } elsif (/^((\w+)\s*\))/) {
1407         # pragma pack(label)
1408         # label must in fact be a macro that resolves to an integer
1409         # Then behaves like 'pragma pack(n)'
1410         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1411         print FILEO "$pack_indent/* winemaker:warning: Assuming $2 == 4 */\n";
1412         print_pack($pack_indent,4,$');
1413         push @pack_stack, [ "", "", 4 ];
1414
1415       } elsif (/^(push\s*,\s*(\w+)\s*(,\s*(\d+)\s*)?\))/) {
1416         # pragma pack(push,label[,n])
1417         # Pushes a new label on the stack. It is possible to push the same
1418         # label multiple times. If 'n' is omitted then the alignment is
1419         # unchanged. Otherwise it becomes 'n'.
1420         print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1421         my $size;
1422         if (defined $4) {
1423           $size=$4;
1424         } elsif (@pack_stack > 0) {
1425           my $alignment=$pack_stack[$#pack_stack];
1426           $size=@$alignment[2];
1427         } else {
1428           print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1429           $size=4;
1430         }
1431         print_pack($pack_indent,$size,$');
1432         push @pack_stack, [ "push", $2, $size ];
1433
1434       } else {
1435         # pragma pack(???               -> What's that?
1436         print FILEO "$pack_indent/* winemaker:warning: Unknown type of pragma pack directive */\n";
1437         print FILEO "$pack_indent$pack_header$_";
1438
1439       }
1440       $modified=1;
1441
1442     } elsif ($is_rc) {
1443       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]+)([\">]?)/) {
1444         my $from_file=($5 eq "<"?"":$dirname);
1445         my $real_include_name=get_real_include_name($line,$6,$from_file,$project,$target);
1446         print FILEO "$1$5$real_include_name$7$'";
1447         $modified|=($real_include_name ne $6);
1448
1449       } elsif (/^(\s*RCINCLUDE\s*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
1450         my $from_file=($2 eq "<"?"":$dirname);
1451         my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1452         print FILEO "$1$2$real_include_name$4$'";
1453         $modified|=($real_include_name ne $3);
1454
1455       } elsif ($is_rc and !$is_mfc and $rc_block_depth == 0 and /^\s*\d+\s+TEXTINCLUDE\s*/) {
1456         $rc_textinclude_state=1;
1457         print FILEO;
1458
1459       } elsif ($rc_textinclude_state == 3 and /^(\s*\"\#\s*include\s*\"\")afxres\.h(\"\"\\r\\n\")/) {
1460         print FILEO "$1winres.h$2$'";
1461         $modified=1;
1462
1463       } elsif (/^\s*BEGIN(\W.*)?$/) {
1464         $rc_textinclude_state|=2;
1465         $rc_block_depth++;
1466         print FILEO;
1467
1468       } elsif (/^\s*END(\W.*)?$/) {
1469         $rc_textinclude_state=0;
1470         if ($rc_block_depth>0) {
1471           $rc_block_depth--;
1472         }
1473         print FILEO;
1474
1475       } else {
1476         print FILEO;
1477       }
1478
1479     } else {
1480       print FILEO;
1481     }
1482   }
1483
1484   close(FILEI);
1485   close(FILEO);
1486   if ($opt_backup == 0 or $modified == 0) {
1487     if (!unlink("$filename.bak")) {
1488       print STDERR "error: unable to delete $filename.bak:\n";
1489       print STDERR "       $!\n";
1490     }
1491   }
1492 }
1493
1494 ##
1495 # Analyzes each source file in turn to find and correct issues
1496 # that would cause it not to compile.
1497 sub fix_source()
1498 {
1499   print "Fixing the source files...\n";
1500   foreach my $project (@projects) {
1501     foreach my $target (@$project[$P_SETTINGS],@{@$project[$P_TARGETS]}) {
1502       foreach my $source (@{@$target[$T_SOURCES_C]}, @{@$target[$T_SOURCES_CXX]}, @{@$target[$T_SOURCES_RC]}, @{@$target[$T_SOURCES_MISC]}) {
1503         fix_file($source,$project,$target);
1504       }
1505     }
1506   }
1507 }
1508
1509
1510
1511 #####
1512 #
1513 # File generation
1514 #
1515 #####
1516
1517 ##
1518 # A convenience function to generate all the lists (defines,
1519 # C sources, C++ source, etc.) in the Makefile
1520 sub generate_list($$$;$)
1521 {
1522   my $name=$_[0];
1523   my $last=$_[1];
1524   my $list=$_[2];
1525   my $data=$_[3];
1526   my $first=$name;
1527
1528   if ($name) {
1529     printf FILEO "%-22s=",$name;
1530   }
1531   if (defined $list) {
1532     foreach my $item (@$list) {
1533       my $value;
1534       if (defined $data) {
1535         $value=&$data($item);
1536       } else {
1537         $value=$item;
1538       }
1539       if ($value ne "") {
1540         if ($first) {
1541           print FILEO " $value";
1542           $first=0;
1543         } else {
1544           print FILEO " \\\n\t\t\t$value";
1545         }
1546       }
1547     }
1548   }
1549   if ($last) {
1550     print FILEO "\n";
1551   }
1552 }
1553
1554 ##
1555 # Generates a project's Makefile.in and all the target files
1556 sub generate_project_files($)
1557 {
1558   my $project=$_[0];
1559   my $project_settings=@$project[$P_SETTINGS];
1560   my @dll_list=();
1561   my @exe_list=();
1562
1563   # Then sort the targets and separate the libraries from the programs
1564   foreach my $target (sort { @$a[$T_NAME] cmp @$b[$T_NAME] } @{@$project[$P_TARGETS]}) {
1565     if (@$target[$T_TYPE] == $TT_DLL) {
1566       push @dll_list,$target;
1567     } else {
1568       push @exe_list,$target;
1569     }
1570   }
1571   @$project[$P_TARGETS]=[];
1572   push @{@$project[$P_TARGETS]}, @dll_list;
1573   push @{@$project[$P_TARGETS]}, @exe_list;
1574
1575   if (!open(FILEO,">@$project[$P_PATH]Makefile")) {
1576     print STDERR "error: could not open \"@$project[$P_PATH]/Makefile\" for writing\n";
1577     print STDERR "       $!\n";
1578     return;
1579   }
1580
1581   print FILEO "### Generated by Winemaker\n";
1582   print FILEO "\n\n";
1583
1584   generate_list("SRCDIR",1,[ "." ]);
1585   if (@$project[$P_PATH] eq "") {
1586     # This is the main project. It is also responsible for recursively
1587     # calling the other projects
1588     generate_list("SUBDIRS",1,\@projects,sub
1589                   {
1590                     if ($_[0] != \@main_project) {
1591                       my $subdir=@{$_[0]}[$P_PATH];
1592                       $subdir =~ s+/$++;
1593                       return $subdir;
1594                     }
1595                     # Eliminating the main project by returning undefined!
1596                   });
1597   }
1598   if (@{@$project[$P_TARGETS]} > 0) {
1599     generate_list("DLLS",1,\@dll_list,sub
1600                   {
1601                     return @{$_[0]}[$T_NAME];
1602                   });
1603     generate_list("EXES",1,\@exe_list,sub
1604                   {
1605                     return "@{$_[0]}[$T_NAME]";
1606                   });
1607     print FILEO "\n\n\n";
1608
1609     print FILEO "### Common settings\n\n";
1610     # Make it so that the project-wide settings override the global settings
1611     generate_list("CEXTRA",1,@$project_settings[$T_CEXTRA]);
1612     generate_list("CXXEXTRA",1,@$project_settings[$T_CXXEXTRA]);
1613     generate_list("RCEXTRA",1,@$project_settings[$T_RCEXTRA]);
1614     generate_list("INCLUDE_PATH",1,@$project_settings[$T_INCLUDE_PATH]);
1615     generate_list("DLL_PATH",1,@$project_settings[$T_DLL_PATH]);
1616     generate_list("LIBRARY_PATH",1,@$project_settings[$T_LIBRARY_PATH]);
1617     generate_list("LIBRARIES",1,@$project_settings[$T_LIBRARIES]);
1618     print FILEO "\n\n";
1619
1620     my $extra_source_count=@{@$project_settings[$T_SOURCES_C]}+
1621                            @{@$project_settings[$T_SOURCES_CXX]}+
1622                            @{@$project_settings[$T_SOURCES_RC]};
1623     my $no_extra=($extra_source_count == 0);
1624     if (!$no_extra) {
1625       print FILEO "### Extra source lists\n\n";
1626       generate_list("EXTRA_C_SRCS",1,@$project_settings[$T_SOURCES_C]);
1627       generate_list("EXTRA_CXX_SRCS",1,@$project_settings[$T_SOURCES_CXX]);
1628       generate_list("EXTRA_RC_SRCS",1,@$project_settings[$T_SOURCES_RC]);
1629       print FILEO "\n";
1630       generate_list("EXTRA_OBJS",1,["\$(EXTRA_C_SRCS:.c=.o)","\$(EXTRA_CXX_SRCS:.cpp=.o)"]);
1631       print FILEO "\n\n\n";
1632     }
1633
1634     # Iterate over all the targets...
1635     foreach my $target (@{@$project[$P_TARGETS]}) {
1636       print FILEO "### @$target[$T_NAME] sources and settings\n\n";
1637       my $canon=canonize("@$target[$T_NAME]");
1638       $canon =~ s+_so$++;
1639
1640       generate_list("${canon}_MODULE",1,[@$target[$T_NAME]]);
1641       generate_list("${canon}_C_SRCS",1,@$target[$T_SOURCES_C]);
1642       generate_list("${canon}_CXX_SRCS",1,@$target[$T_SOURCES_CXX]);
1643       generate_list("${canon}_RC_SRCS",1,@$target[$T_SOURCES_RC]);
1644       generate_list("${canon}_LDFLAGS",1,@$target[$T_LDFLAGS]);
1645       generate_list("${canon}_DLL_PATH",1,@$target[$T_DLL_PATH]);
1646       generate_list("${canon}_DLLS",1,@$target[$T_DLLS]);
1647       generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH]);
1648       generate_list("${canon}_LIBRARIES",1,@$target[$T_LIBRARIES]);
1649       print FILEO "\n";
1650       generate_list("${canon}_OBJS",1,["\$(${canon}_C_SRCS:.c=.o)","\$(${canon}_CXX_SRCS:.cpp=.o)","\$(${canon}_RC_SRCS:.rc=.res)"]);
1651       print FILEO "\n\n\n";
1652     }
1653     print FILEO "### Global source lists\n\n";
1654     generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub
1655                   {
1656                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1657                     $canon =~ s+_so$++;
1658                     return "\$(${canon}_C_SRCS)";
1659                   });
1660     if (!$no_extra) {
1661       generate_list("",1,[ "\$(EXTRA_C_SRCS)" ]);
1662     }
1663     generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub
1664                   {
1665                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1666                     $canon =~ s+_so$++;
1667                     return "\$(${canon}_CXX_SRCS)";
1668                   });
1669     if (!$no_extra) {
1670       generate_list("",1,[ "\$(EXTRA_CXX_SRCS)" ]);
1671     }
1672     generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub
1673                   {
1674                     my $canon=canonize(@{$_[0]}[$T_NAME]);
1675                     $canon =~ s+_so$++;
1676                     return "\$(${canon}_RC_SRCS)";
1677                   });
1678     if (!$no_extra) {
1679       generate_list("",1,[ "\$(EXTRA_RC_SRCS)" ]);
1680     }
1681   }
1682   print FILEO "\n\n";
1683   print FILEO "### Tools\n\n";
1684   print FILEO "CC = winegcc\n";
1685   print FILEO "CXX = wineg++\n";
1686   print FILEO "RC = wrc\n";
1687   print FILEO "WINEBUILD = winebuild\n";
1688   print FILEO "\n\n";
1689
1690   print FILEO "### Generic targets\n\n";
1691   print FILEO "all:";
1692   if (@$project[$P_PATH] eq "") {
1693     print FILEO " \$(SUBDIRS)";
1694   }
1695   if (@{@$project[$P_TARGETS]} > 0) {
1696     print FILEO " \$(DLLS:%=%.so) \$(EXES:%=%.so)";
1697   }
1698   print FILEO "\n\n";
1699   print FILEO "### Build rules\n";
1700   print FILEO "\n";
1701   print FILEO ".PHONY: all clean dummy\n";
1702   print FILEO "\n";
1703   print FILEO "\$(SUBDIRS): dummy\n";
1704   print FILEO "\t\@cd \$\@ && \$(MAKE)\n";
1705   print FILEO "\n";
1706   print FILEO "# Implicit rules\n";
1707   print FILEO "\n";
1708   print FILEO ".SUFFIXES: .cpp .rc .res\n";
1709   print FILEO "DEFINCL = \$(INCLUDE_PATH) \$(DEFINES) \$(OPTIONS)\n";
1710   print FILEO "\n";
1711   print FILEO ".c.o:\n";
1712   print FILEO "\t\$(CC) -c \$(CFLAGS) \$(CEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
1713   print FILEO "\n";
1714   print FILEO ".cpp.o:\n";
1715   print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
1716   print FILEO "\n";
1717   print FILEO ".cxx.o:\n";
1718   print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
1719   print FILEO "\n";
1720   print FILEO ".rc.res:\n";
1721   print FILEO "\t\$(RC) \$(RCFLAGS) \$(RCEXTRA) \$(DEFINCL) -fo\$@ \$<\n";
1722   print FILEO "\n";
1723   print FILEO "# Rules for cleaning\n";
1724   print FILEO "\n";
1725   print FILEO "CLEAN_FILES     = *.dbg.c y.tab.c y.tab.h lex.yy.c \\\n";
1726   print FILEO "                  core *.orig *.rej \\\n";
1727   print FILEO "                  \\\\\\#*\\\\\\# *~ *% .\\\\\\#*\n";
1728   print FILEO "\n";
1729   print FILEO "clean:: \$(SUBDIRS:%=%/__clean__) \$(EXTRASUBDIRS:%=%/__clean__)\n";
1730   print FILEO "\t\$(RM) \$(CLEAN_FILES) \$(RC_SRCS:.rc=.res) \$(C_SRCS:.c=.o) \$(CXX_SRCS:.cpp=.o)\n";
1731   print FILEO "\t\$(RM) \$(DLLS:%=%.dbg.o) \$(DLLS:%=%.so)\n";
1732   print FILEO "\t\$(RM) \$(EXES:%=%.dbg.o) \$(EXES:%=%.so) \$(EXES:%.exe=%)\n";
1733   print FILEO "\n";
1734   print FILEO "\$(SUBDIRS:%=%/__clean__): dummy\n";
1735   print FILEO "\tcd `dirname \$\@` && \$(MAKE) clean\n";
1736   print FILEO "\n";
1737   print FILEO "\$(EXTRASUBDIRS:%=%/__clean__): dummy\n";
1738   print FILEO "\t-cd `dirname \$\@` && \$(RM) \$(CLEAN_FILES)\n";
1739   print FILEO "\n";
1740
1741   if (@{@$project[$P_TARGETS]} > 0) {
1742     print FILEO "### Target specific build rules\n\n";
1743     foreach my $target (@{@$project[$P_TARGETS]}) {
1744       my $canon=canonize("@$target[$T_NAME]");
1745       $canon =~ s/_so$//;
1746
1747       print FILEO "\$(${canon}_MODULE).dbg.c: \$(${canon}_C_SRCS) \$(${canon}_CXX_SRCS)\n";
1748       print FILEO "\t\$(WINEBUILD) -o \$\@ --debug -C\$(SRCDIR) \$(${canon}_C_SRCS) \$(${canon}_CXX_SRCS)\n";
1749       print FILEO "\n";
1750       print FILEO "\$(${canon}_MODULE).so: \$(${canon}_MODULE).dbg.o \$(${canon}_OBJS)\n";
1751       if (@{@$target[$T_SOURCES_CXX]} > 0 or @{@$project_settings[$T_SOURCES_CXX]} > 0) {
1752         print FILEO "\t\$(CXX)";
1753       } else {
1754         print FILEO "\t\$(CC)";
1755       }
1756       print FILEO " \$(${canon}_LDFLAGS) -o \$\@ \$(${canon}_OBJS) \$(${canon}_MODULE).dbg.o \$(${canon}_LIBRARY_PATH) \$(LIBRARY_PATH) \$(${canon}_DLLS:%=-l%) \$(${canon}_LIBRARIES:%=-l%)\n";
1757       print FILEO "\n\n";
1758     }
1759   }
1760   close(FILEO);
1761
1762 }
1763
1764
1765 ##
1766 # This is where we finally generate files. In fact this method does not
1767 # do anything itself but calls the methods that do the actual work.
1768 sub generate()
1769 {
1770   print "Generating project files...\n";
1771
1772   foreach my $project (@projects) {
1773     my $path=@$project[$P_PATH];
1774     if ($path eq "") {
1775       $path=".";
1776     } else {
1777       $path =~ s+/$++;
1778     }
1779     print "  $path\n";
1780     generate_project_files($project);
1781   }
1782 }
1783
1784
1785
1786 #####
1787 #
1788 # Option defaults
1789 #
1790 #####
1791
1792 $opt_backup=1;
1793 $opt_lower=$OPT_LOWER_UPPERCASE;
1794 $opt_lower_include=1;
1795
1796 $opt_work_dir=undef;
1797 $opt_single_target=undef;
1798 $opt_target_type=$TT_GUIEXE;
1799 $opt_flags=0;
1800 $opt_is_interactive=$OPT_ASK_NO;
1801 $opt_ask_project_options=$OPT_ASK_NO;
1802 $opt_ask_target_options=$OPT_ASK_NO;
1803 $opt_no_generated_files=0;
1804 $opt_no_source_fix=0;
1805 $opt_no_banner=0;
1806
1807
1808
1809 #####
1810 #
1811 # Main
1812 #
1813 #####
1814
1815 sub print_banner()
1816 {
1817   print "Winemaker $version\n";
1818   print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
1819 }
1820
1821 sub usage()
1822 {
1823   print_banner();
1824   print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup] [--nosource-fix]\n";
1825   print STDERR "                 [--lower-none|--lower-all|--lower-uppercase]\n";
1826   print STDERR "                 [--lower-include|--nolower-include] [--mfc|--nomfc]\n";
1827   print STDERR "                 [--guiexe|--windows|--cuiexe|--console|--dll]\n";
1828   print STDERR "                 [-Dmacro[=defn]] [-Idir] [-Pdir] [-idll] [-Ldir] [-llibrary]\n";
1829   print STDERR "                 [--nodlls] [--nomsvcrt] [--interactive] [--single-target name]\n";
1830   print STDERR "                 [--generated-files|--nogenerated-files]\n";
1831   print STDERR "                 work_directory\n";
1832   print STDERR "\nWinemaker is designed to recursively convert all the Windows sources found in\n";
1833   print STDERR "the specified directory so that they can be compiled with Winelib. During this\n";
1834   print STDERR "process it will modify and rename some of the files in that directory.\n";
1835   print STDERR "\tPlease read the manual page before use.\n";
1836   exit (2);
1837 }
1838
1839 target_init(\@global_settings);
1840
1841 while (@ARGV>0) {
1842   my $arg=shift @ARGV;
1843   # General options
1844   if ($arg eq "--nobanner") {
1845     $opt_no_banner=1;
1846   } elsif ($arg eq "--backup") {
1847     $opt_backup=1;
1848   } elsif ($arg eq "--nobackup") {
1849     $opt_backup=0;
1850   } elsif ($arg eq "--single-target") {
1851     $opt_single_target=shift @ARGV;
1852   } elsif ($arg eq "--lower-none") {
1853     $opt_lower=$OPT_LOWER_NONE;
1854   } elsif ($arg eq "--lower-all") {
1855     $opt_lower=$OPT_LOWER_ALL;
1856   } elsif ($arg eq "--lower-uppercase") {
1857     $opt_lower=$OPT_LOWER_UPPERCASE;
1858   } elsif ($arg eq "--lower-include") {
1859     $opt_lower_include=1;
1860   } elsif ($arg eq "--nolower-include") {
1861     $opt_lower_include=0;
1862   } elsif ($arg eq "--nosource-fix") {
1863     $opt_no_source_fix=1;
1864   } elsif ($arg eq "--generated-files") {
1865     $opt_no_generated_files=0;
1866   } elsif ($arg eq "--nogenerated-files") {
1867     $opt_no_generated_files=1;
1868   } elsif ($arg =~ /^-D/) {
1869     push @{$global_settings[$T_DEFINES]},$arg;
1870   } elsif ($arg =~ /^-I/) {
1871     push @{$global_settings[$T_INCLUDE_PATH]},$arg;
1872   } elsif ($arg =~ /^-P/) {
1873     push @{$global_settings[$T_DLL_PATH]},"-L$'";
1874   } elsif ($arg =~ /^-i/) {
1875     push @{$global_settings[$T_DLLS]},$';
1876   } elsif ($arg =~ /^-L/) {
1877     push @{$global_settings[$T_LIBRARY_PATH]},$arg;
1878   } elsif ($arg =~ /^-l/) {
1879     push @{$global_settings[$T_LIBRARIES]},$';
1880
1881   # 'Source'-based method options
1882   } elsif ($arg eq "--dll") {
1883     $opt_target_type=$TT_DLL;
1884   } elsif ($arg eq "--guiexe" or $arg eq "--windows") {
1885     $opt_target_type=$TT_GUIEXE;
1886   } elsif ($arg eq "--cuiexe" or $arg eq "--console") {
1887     $opt_target_type=$TT_CUIEXE;
1888   } elsif ($arg eq "--interactive") {
1889     $opt_is_interactive=$OPT_ASK_YES;
1890     $opt_ask_project_options=$OPT_ASK_YES;
1891     $opt_ask_target_options=$OPT_ASK_YES;
1892   } elsif ($arg eq "--mfc") {
1893     $opt_flags|=$TF_MFC;
1894   } elsif ($arg eq "--nomfc") {
1895     $opt_flags&=~$TF_MFC;
1896     $opt_flags|=$TF_NOMFC;
1897   } elsif ($arg eq "--nodlls") {
1898     $opt_flags|=$TF_NODLLS;
1899   } elsif ($arg eq "--nomsvcrt") {
1900     $opt_flags|=$TF_NOMSVCRT;
1901
1902   # Catch errors
1903   } else {
1904     if ($arg ne "--help" and $arg ne "-h" and $arg ne "-?") {
1905       if (!defined $opt_work_dir) {
1906         $opt_work_dir=$arg;
1907       } else {
1908         print STDERR "error: the work directory, \"$arg\", has already been specified (was \"$opt_work_dir\")\n";
1909         usage();
1910       }
1911     } else {
1912       usage();
1913     }
1914   }
1915 }
1916
1917 if (!defined $opt_work_dir) {
1918   print STDERR "error: you must specify the directory containing the sources to be converted\n";
1919   usage();
1920 } elsif (!chdir $opt_work_dir) {
1921   print STDERR "error: could not chdir to the work directory\n";
1922   print STDERR "       $!\n";
1923   usage();
1924 }
1925
1926 if ($opt_no_banner == 0) {
1927   print_banner();
1928 }
1929
1930 project_init(\@main_project,"");
1931
1932 # Fix the file and directory names
1933 fix_file_and_directory_names(".");
1934
1935 # Scan the sources to identify the projects and targets
1936 source_scan();
1937
1938 # Fix the source files
1939 if (! $opt_no_source_fix) {
1940   fix_source();
1941 }
1942
1943 # Generate the Makefile and the spec file
1944 if (! $opt_no_generated_files) {
1945   generate();
1946 }