Implement DelayedMove and FileExists.
[wine/multimedia.git] / tools / winemaker
blobfa4adeb52364e7a7cf4b3b4beece4a0e8f734ee1
1 #!/usr/bin/perl -w
2 use strict;
4 # Copyright 2000-2004 Francois Gouget for CodeWeavers
5 # Copyright 2004 Dimitrie O. Paun
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.
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.
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
22 my $version="0.6.0";
24 use Cwd;
25 use File::Basename;
26 use File::Copy;
30 #####
32 # Options
34 #####
36 # The following constants define what we do with the case of filenames
39 # Never rename a file to lowercase
40 my $OPT_LOWER_NONE=0;
43 # Rename all files to lowercase
44 my $OPT_LOWER_ALL=1;
47 # Rename only files that are all uppercase to lowercase
48 my $OPT_LOWER_UPPERCASE=2;
51 # The following constants define whether to ask questions or not
54 # No (synonym of never)
55 my $OPT_ASK_NO=0;
58 # Yes (always)
59 my $OPT_ASK_YES=1;
62 # Skip the questions till the end of this scope
63 my $OPT_ASK_SKIP=-1;
66 # General options
69 # This is the directory in which winemaker will operate.
70 my $opt_work_dir;
73 # Make a backup of the files
74 my $opt_backup;
77 # Defines which files to rename
78 my $opt_lower;
81 # If we don't find the file referenced by an include, lower it
82 my $opt_lower_include;
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;
90 # Options for the 'Source' method
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;
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;
107 # Contains the default set of flags to be used when creating a new target.
108 my $opt_flags;
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;
118 # If false then winemaker should not generate the makefiles.
119 my $opt_no_generated_files;
122 # Specifies not to print the banner if set.
123 my $opt_no_banner;
127 #####
129 # Target modelization
131 #####
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.
137 # This is the name of the target.
138 my $T_NAME=0;
141 # Defines the type of target we want to build. See the TT_xxx
142 # constants below
143 my $T_TYPE=1;
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;
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;
159 # This is a reference to an array containing the list of
160 # C compiler options
161 my $T_CEXTRA=7;
164 # This is a reference to an array containing the list of
165 # C++ compiler options
166 my $T_CXXEXTRA=8;
169 # This is a reference to an array containing the list of
170 # RC compiler options
171 my $T_RCEXTRA=9;
174 # This is a reference to an array containing the list of macro
175 # definitions
176 my $T_DEFINES=10;
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;
184 # Flags for the linker
185 my $T_LDFLAGS=12;
188 # Same as T_INCLUDE_PATH but for the dll search path
189 my $T_DLL_PATH=13;
192 # The list of Windows dlls to import
193 my $T_DLLS=14;
196 # Same as T_INCLUDE_PATH but for the library search path
197 my $T_LIBRARY_PATH=15;
200 # The list of Unix libraries to link with
201 my $T_LIBRARIES=16;
204 # The list of dependencies between targets
205 my $T_DEPENDS=17;
208 # The following constants define the recognized types of target
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;
218 # For executables in the windows subsystem
219 my $TT_GUIEXE=1;
222 # For executables in the console subsystem
223 my $TT_CUIEXE=2;
226 # For dynamically linked libraries
227 my $TT_DLL=3;
230 # The following constants further refine how the target should be handled
233 # This target is an MFC-based target
234 my $TF_MFC=4;
237 # User has specified --nomfc option for this target or globally
238 my $TF_NOMFC=8;
241 # --nodlls option: Do not use standard DLL set
242 my $TF_NODLLS=16;
245 # --nomsvcrt option: Do not link with msvcrt
246 my $TF_NOMSVCRT=32;
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($)
254 my $target=$_[0];
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]=[];
277 #####
279 # Project modelization
281 #####
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.
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;
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;
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;
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($$)
312 my $project=$_[0];
313 my $path=$_[1];
315 my $project_settings=[];
316 target_init($project_settings);
318 @$project[$P_PATH]=$path;
319 @$project[$P_SETTINGS]=$project_settings;
320 @$project[$P_TARGETS]=[];
325 #####
327 # Global variables
329 #####
331 my %warnings;
333 my %templates;
336 # This maps a directory name to a reference to an array listing
337 # its contents (files and directories)
338 my %directories;
341 # Contains the list of all projects. This list tells us what are
342 # the subprojects of the main Makefile and where we have to generate
343 # Makefiles.
344 my @projects=();
347 # This is the main project, i.e. the one in the "." directory.
348 # It may well be empty in which case the main Makefile will only
349 # call out subprojects.
350 my @main_project;
353 # Contains the defaults for the include path, etc.
354 # We store the defaults as if this were a target except that we only
355 # exploit the defines, include path, library path, library list and misc
356 # sources fields.
357 my @global_settings;
361 #####
363 # Utility functions
365 #####
368 # Cleans up a name to make it an acceptable Makefile
369 # variable name.
370 sub canonize($)
372 my $name=$_[0];
374 $name =~ tr/a-zA-Z0-9_/_/c;
375 return $name;
379 # Returns true is the specified pathname is absolute.
380 # Note: pathnames that start with a variable '$' or
381 # '~' are considered absolute.
382 sub is_absolute($)
384 my $path=$_[0];
386 return ($path =~ /^[\/~\$]/);
390 # Performs a binary search looking for the specified item
391 sub bsearch($$)
393 my $array=$_[0];
394 my $item=$_[1];
395 my $last=@{$array}-1;
396 my $first=0;
398 while ($first<=$last) {
399 my $index=int(($first+$last)/2);
400 my $cmp=@$array[$index] cmp $item;
401 if ($cmp<0) {
402 $first=$index+1;
403 } elsif ($cmp>0) {
404 $last=$index-1;
405 } else {
406 return $index;
412 # Retrieves the contents of the specified directory.
413 # We either get it from the directories hashtable which acts as a
414 # cache, or use opendir, readdir, closedir and store the result
415 # in the hashtable.
416 sub get_directory_contents($)
418 my $dirname=$_[0];
419 my $directory;
421 #print "getting the contents of $dirname\n";
423 # check for a cached version
424 $dirname =~ s+/$++;
425 if ($dirname eq "") {
426 $dirname=cwd;
428 $directory=$directories{$dirname};
429 if (defined $directory) {
430 #print "->@$directory\n";
431 return $directory;
434 # Read this directory
435 if (opendir(DIRECTORY, "$dirname")) {
436 my @files=readdir DIRECTORY;
437 closedir(DIRECTORY);
438 $directory=\@files;
439 } else {
440 # Return an empty list
441 #print "error: cannot open $dirname\n";
442 my @files;
443 $directory=\@files;
445 #print "->@$directory\n";
446 $directories{$dirname}=$directory;
447 return $directory;
452 #####
454 # 'Source'-based Project analysis
456 #####
459 # Allows the user to specify makefile and target specific options
460 # - target: the structure in which to store the results
461 # - options: the string containing the options
462 sub source_set_options($$)
464 my $target=$_[0];
465 my $options=$_[1];
467 #FIXME: we must deal with escaping of stuff and all
468 foreach my $option (split / /,$options) {
469 if (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-D/) {
470 push @{@$target[$T_DEFINES]},$option;
471 } elsif (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-I/) {
472 push @{@$target[$T_INCLUDE_PATH]},$option;
473 } elsif ($option =~ /^-P/) {
474 push @{@$target[$T_DLL_PATH]},"-L$'";
475 } elsif ($option =~ /^-i/) {
476 push @{@$target[$T_DLLS]},"$'";
477 } elsif ($option =~ /^-L/) {
478 push @{@$target[$T_LIBRARY_PATH]},$option;
479 } elsif ($option =~ /^-l/) {
480 push @{@$target[$T_LIBRARIES]},"$'";
481 } elsif ($option =~ /^--mfc/) {
482 @$target[$T_FLAGS]|=$TF_MFC;
483 @$target[$T_FLAGS]&=~$TF_NOMFC;
484 } elsif ($option =~ /^--nomfc/) {
485 @$target[$T_FLAGS]&=~$TF_MFC;
486 @$target[$T_FLAGS]|=$TF_NOMFC;
487 } elsif ($option =~ /^--nodlls/) {
488 @$target[$T_FLAGS]|=$TF_NODLLS;
489 } elsif ($option =~ /^--nomsvcrt/) {
490 @$target[$T_FLAGS]|=$TF_NOMSVCRT;
491 } else {
492 print STDERR "error: unknown option \"$option\"\n";
493 return 0;
496 return 1;
500 # Scans the specified directory to:
501 # - see if we should create a Makefile in this directory. We normally do
502 # so if we find a project file and sources
503 # - get a list of targets for this directory
504 # - get the list of source files
505 sub source_scan_directory($$$$);
506 sub source_scan_directory($$$$)
508 # a reference to the parent's project
509 my $parent_project=$_[0];
510 # the full relative path to the current directory, including a
511 # trailing '/', or an empty string if this is the top level directory
512 my $path=$_[1];
513 # the name of this directory, including a trailing '/', or an empty
514 # string if this is the top level directory
515 my $dirname=$_[2];
516 # if set then no targets will be looked for and the sources will all
517 # end up in the parent_project's 'misc' bucket
518 my $no_target=$_[3];
520 # reference to the project for this directory. May not be used
521 my $project;
522 # list of targets found in the 'current' directory
523 my %targets;
524 # list of sources found in the current directory
525 my @sources_c=();
526 my @sources_cxx=();
527 my @sources_rc=();
528 my @sources_misc=();
529 # true if this directory contains a Windows project
530 my $has_win_project=0;
531 # true if this directory contains headers
532 my $has_headers=0;
533 # If we don't find any executable/library then we might make up targets
534 # from the list of .dsp/.mak files we find since they usually have the
535 # same name as their target.
536 my @dsp_files=();
537 my @mak_files=();
539 if (defined $opt_single_target or $dirname eq "") {
540 # Either there is a single target and thus a single project,
541 # or we are in the top level directory for which a project
542 # already exists
543 $project=$parent_project;
544 } else {
545 $project=[];
546 project_init($project,$path);
548 my $project_settings=@$project[$P_SETTINGS];
550 # First find out what this directory contains:
551 # collect all sources, targets and subdirectories
552 my $directory=get_directory_contents($path);
553 foreach my $dentry (@$directory) {
554 if ($dentry =~ /^\./) {
555 next;
557 my $fullentry="$path$dentry";
558 if (-d "$fullentry") {
559 if ($dentry =~ /^(Release|Debug)/i) {
560 # These directories are often used to store the object files and the
561 # resulting executable/library. They should not contain anything else.
562 my @candidates=grep /\.(exe|dll)$/i, @{get_directory_contents("$fullentry")};
563 foreach my $candidate (@candidates) {
564 $targets{$candidate}=1;
566 } elsif ($dentry =~ /^include/i) {
567 # This directory must contain headers we're going to need
568 push @{@$project_settings[$T_INCLUDE_PATH]},"-I$dentry";
569 source_scan_directory($project,"$fullentry/","$dentry/",1);
570 } else {
571 # Recursively scan this directory. Any source file that cannot be
572 # attributed to a project in one of the subdirectories will be
573 # attributed to this project.
574 source_scan_directory($project,"$fullentry/","$dentry/",$no_target);
576 } elsif (-f "$fullentry") {
577 if ($dentry =~ /\.(exe|dll)$/i) {
578 $targets{$dentry}=1;
579 } elsif ($dentry =~ /\.c$/i and $dentry !~ /\.(dbg|spec)\.c$/) {
580 push @sources_c,"$dentry";
581 } elsif ($dentry =~ /\.(cpp|cxx)$/i) {
582 if ($dentry =~ /^stdafx.cpp$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
583 push @sources_misc,"$dentry";
584 @$project_settings[$T_FLAGS]|=$TF_MFC;
585 } else {
586 push @sources_cxx,"$dentry";
588 } elsif ($dentry =~ /\.rc$/i) {
589 push @sources_rc,"$dentry";
590 } elsif ($dentry =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
591 $has_headers=1;
592 push @sources_misc,"$dentry";
593 if ($dentry =~ /^stdafx.h$/i && !(@$project_settings[$T_FLAGS] & $TF_NOMFC)) {
594 @$project_settings[$T_FLAGS]|=$TF_MFC;
596 } elsif ($dentry =~ /\.dsp$/i) {
597 push @dsp_files,"$dentry";
598 $has_win_project=1;
599 } elsif ($dentry =~ /\.mak$/i) {
600 push @mak_files,"$dentry";
601 $has_win_project=1;
602 } elsif ($dentry =~ /^makefile/i) {
603 $has_win_project=1;
607 closedir(DIRECTORY);
609 if ($has_headers) {
610 push @{@$project_settings[$T_INCLUDE_PATH]},"-I.";
612 # If we have a single target then all we have to do is assign
613 # all the sources to it and we're done
614 # FIXME: does this play well with the --interactive mode?
615 if ($opt_single_target) {
616 my $target=@{@$project[$P_TARGETS]}[0];
617 push @{@$target[$T_SOURCES_C]},map "$path$_",@sources_c;
618 push @{@$target[$T_SOURCES_CXX]},map "$path$_",@sources_cxx;
619 push @{@$target[$T_SOURCES_RC]},map "$path$_",@sources_rc;
620 push @{@$target[$T_SOURCES_MISC]},map "$path$_",@sources_misc;
621 return;
623 if ($no_target) {
624 my $parent_settings=@$parent_project[$P_SETTINGS];
625 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_c;
626 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_cxx;
627 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_rc;
628 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
629 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
630 return;
633 my $source_count=@sources_c+@sources_cxx+@sources_rc+
634 @{@$project_settings[$T_SOURCES_C]}+
635 @{@$project_settings[$T_SOURCES_CXX]}+
636 @{@$project_settings[$T_SOURCES_RC]};
637 if ($source_count == 0) {
638 # A project without real sources is not a project, get out!
639 if ($project!=$parent_project) {
640 my $parent_settings=@$parent_project[$P_SETTINGS];
641 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
642 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
644 return;
646 #print "targets=",%targets,"\n";
647 #print "target_count=$target_count\n";
648 #print "has_win_project=$has_win_project\n";
649 #print "dirname=$dirname\n";
651 my $target_count;
652 if (($has_win_project != 0) or ($dirname eq "")) {
653 # Deal with cases where we could not find any executable/library, and
654 # thus have no target, although we did find some sort of windows project.
655 $target_count=keys %targets;
656 if ($target_count == 0) {
657 # Try to come up with a target list based on .dsp/.mak files
658 my $prj_list;
659 if (@dsp_files > 0) {
660 $prj_list=\@dsp_files;
661 } else {
662 $prj_list=\@mak_files;
664 foreach my $filename (@$prj_list) {
665 $filename =~ s/\.(dsp|mak)$//i;
666 if ($opt_target_type == $TT_DLL) {
667 $filename = "$filename.dll";
669 $targets{$filename}=1;
671 $target_count=keys %targets;
672 if ($target_count == 0) {
673 # Still nothing, try the name of the directory
674 my $name;
675 if ($dirname eq "") {
676 # Bad luck, this is the top level directory!
677 $name=(split /\//, cwd)[-1];
678 } else {
679 $name=$dirname;
680 # Remove the trailing '/'. Also eliminate whatever is after the last
681 # '.' as it is likely to be meaningless (.orig, .new, ...)
682 $name =~ s+(/|\.[^.]*)$++;
683 if ($name eq "src") {
684 # 'src' is probably a subdirectory of the real project directory.
685 # Try again with the parent (if any).
686 my $parent=$path;
687 if ($parent =~ s+([^/]*)/[^/]*/$+$1+) {
688 $name=$parent;
689 } else {
690 $name=(split /\//, cwd)[-1];
694 $name =~ s+(/|\.[^.]*)$++;
695 if ($opt_target_type == $TT_DLL) {
696 $name = "$name.dll";
697 } else {
698 $name = "$name.exe";
700 $targets{$name}=1;
704 # Ask confirmation to the user if he wishes so
705 if ($opt_is_interactive == $OPT_ASK_YES) {
706 my $target_list=join " ",keys %targets;
707 print "\n*** In ",($path?$path:"./"),"\n";
708 print "* winemaker found the following list of (potential) targets\n";
709 print "* $target_list\n";
710 print "* Type enter to use it as is, your own comma-separated list of\n";
711 print "* targets, 'none' to assign the source files to a parent directory,\n";
712 print "* or 'ignore' to ignore everything in this directory tree.\n";
713 print "* Target list:\n";
714 $target_list=<STDIN>;
715 chomp $target_list;
716 if ($target_list eq "") {
717 # Keep the target list as is, i.e. do nothing
718 } elsif ($target_list eq "none") {
719 # Empty the target list
720 undef %targets;
721 } elsif ($target_list eq "ignore") {
722 # Ignore this subtree altogether
723 return;
724 } else {
725 undef %targets;
726 foreach my $target (split /,/,$target_list) {
727 $target =~ s+^\s*++;
728 $target =~ s+\s*$++;
729 $targets{$target}=1;
735 # If we have no project at this level, then transfer all
736 # the sources to the parent project
737 $target_count=keys %targets;
738 if ($target_count == 0) {
739 if ($project!=$parent_project) {
740 my $parent_settings=@$parent_project[$P_SETTINGS];
741 push @{@$parent_settings[$T_SOURCES_C]},map "$dirname$_",@sources_c;
742 push @{@$parent_settings[$T_SOURCES_CXX]},map "$dirname$_",@sources_cxx;
743 push @{@$parent_settings[$T_SOURCES_RC]},map "$dirname$_",@sources_rc;
744 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
745 push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
747 return;
750 # Otherwise add this project to the project list, except for
751 # the main project which is already in the list.
752 if ($dirname ne "") {
753 push @projects,$project;
756 # Ask for project-wide options
757 if ($opt_ask_project_options == $OPT_ASK_YES) {
758 my $flag_desc="";
759 if ((@$project_settings[$T_FLAGS] & $TF_MFC)!=0) {
760 $flag_desc="mfc";
762 print "* Type any project-wide options (-D/-I/-P/-i/-L/-l/--mfc),\n";
763 if (defined $flag_desc) {
764 print "* (currently $flag_desc)\n";
766 print "* or 'skip' to skip the target specific options,\n";
767 print "* or 'never' to not be asked this question again:\n";
768 while (1) {
769 my $options=<STDIN>;
770 chomp $options;
771 if ($options eq "skip") {
772 $opt_ask_target_options=$OPT_ASK_SKIP;
773 last;
774 } elsif ($options eq "never") {
775 $opt_ask_project_options=$OPT_ASK_NO;
776 last;
777 } elsif (source_set_options($project_settings,$options)) {
778 last;
780 print "Please re-enter the options:\n";
784 # - Create the targets
785 # - Check if we have both libraries and programs
786 # - Match each target with source files (sort in reverse
787 # alphabetical order to get the longest matches first)
788 my @local_dlls=();
789 my @local_depends=();
790 my @exe_list=();
791 foreach my $target_name (map (lc, (sort { $b cmp $a } keys %targets))) {
792 # Create the target...
793 my $target=[];
794 target_init($target);
795 @$target[$T_NAME]=$target_name;
796 @$target[$T_FLAGS]|=@$project_settings[$T_FLAGS];
797 if ($target_name =~ /\.dll$/) {
798 @$target[$T_TYPE]=$TT_DLL;
799 push @local_depends,"$target_name.so";
800 push @local_dlls,$target_name;
801 my $canon=canonize($target_name);
802 push @{@$target[$T_LDFLAGS]},("-shared","\$(${canon}_MODULE:%=%.spec)");
803 } else {
804 @$target[$T_TYPE]=$opt_target_type;
805 push @exe_list,$target;
806 push @{@$target[$T_LDFLAGS]},(@$target[$T_TYPE] == $TT_CUIEXE ? "-mconsole" : "-mwindows");
808 my $basename=$target_name;
809 $basename=~ s/\.(dll|exe)$//i;
810 # This is the default link list of Visual Studio, except odbccp32
811 # which we don't have in Wine.
812 my @std_imports=qw(odbc32 ole32 oleaut32 winspool);
813 my @std_libraries=qw(uuid);
814 if ((@$target[$T_FLAGS] & $TF_NODLLS) == 0) {
815 @$target[$T_DLLS]=\@std_imports;
816 @$target[$T_LIBRARIES]=\@std_libraries;
817 } else {
818 @$target[$T_DLLS]=[];
819 @$target[$T_LIBRARIES]=[];
821 if ((@$target[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
822 push @{@$target[$T_LDFLAGS]},"-mno-cygwin";
824 push @{@$project[$P_TARGETS]},$target;
826 # Ask for target-specific options
827 if ($opt_ask_target_options == $OPT_ASK_YES) {
828 my $flag_desc="";
829 if ((@$target[$T_FLAGS] & $TF_MFC)!=0) {
830 $flag_desc=" (mfc";
832 if ($flag_desc ne "") {
833 $flag_desc.=")";
835 print "* Specify any link option (-P/-i/-L/-l/--mfc) specific to the target\n";
836 print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
837 while (1) {
838 my $options=<STDIN>;
839 chomp $options;
840 if ($options eq "never") {
841 $opt_ask_target_options=$OPT_ASK_NO;
842 last;
843 } elsif (source_set_options($target,$options)) {
844 last;
846 print "Please re-enter the options:\n";
849 if (@$target[$T_FLAGS] & $TF_MFC) {
850 @$project_settings[$T_FLAGS]|=$TF_MFC;
851 push @{@$target[$T_DLL_PATH]},"\$(MFC_LIBRARY_PATH)";
852 push @{@$target[$T_DLLS]},"mfc.dll";
853 # FIXME: Link with the MFC in the Unix sense, until we
854 # start exporting the functions properly.
855 push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
856 push @{@$target[$T_LIBRARIES]},"mfc";
859 # Match sources...
860 if ($target_count == 1) {
861 push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
862 @$project_settings[$T_SOURCES_C]=[];
863 @sources_c=();
865 push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
866 @$project_settings[$T_SOURCES_CXX]=[];
867 @sources_cxx=();
869 push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
870 @$project_settings[$T_SOURCES_RC]=[];
871 @sources_rc=();
873 push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
874 # No need for sorting these sources
875 @$project_settings[$T_SOURCES_MISC]=[];
876 @sources_misc=();
877 } else {
878 foreach my $source (@sources_c) {
879 if ($source =~ /^$basename/i) {
880 push @{@$target[$T_SOURCES_C]},$source;
881 $source="";
884 foreach my $source (@sources_cxx) {
885 if ($source =~ /^$basename/i) {
886 push @{@$target[$T_SOURCES_CXX]},$source;
887 $source="";
890 foreach my $source (@sources_rc) {
891 if ($source =~ /^$basename/i) {
892 push @{@$target[$T_SOURCES_RC]},$source;
893 $source="";
896 foreach my $source (@sources_misc) {
897 if ($source =~ /^$basename/i) {
898 push @{@$target[$T_SOURCES_MISC]},$source;
899 $source="";
903 @$target[$T_SOURCES_C]=[sort @{@$target[$T_SOURCES_C]}];
904 @$target[$T_SOURCES_CXX]=[sort @{@$target[$T_SOURCES_CXX]}];
905 @$target[$T_SOURCES_RC]=[sort @{@$target[$T_SOURCES_RC]}];
906 @$target[$T_SOURCES_MISC]=[sort @{@$target[$T_SOURCES_MISC]}];
908 if ($opt_ask_target_options == $OPT_ASK_SKIP) {
909 $opt_ask_target_options=$OPT_ASK_YES;
912 if ((@$project_settings[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
913 push @{@$project_settings[$T_CEXTRA]},"-mno-cygwin";
914 push @{@$project_settings[$T_CXXEXTRA]},"-mno-cygwin";
917 if (@$project_settings[$T_FLAGS] & $TF_MFC) {
918 push @{@$project_settings[$T_INCLUDE_PATH]},"\$(MFC_INCLUDE_PATH)";
920 # The sources that did not match, if any, go to the extra
921 # source list of the project settings
922 foreach my $source (@sources_c) {
923 if ($source ne "") {
924 push @{@$project_settings[$T_SOURCES_C]},$source;
927 @$project_settings[$T_SOURCES_C]=[sort @{@$project_settings[$T_SOURCES_C]}];
928 foreach my $source (@sources_cxx) {
929 if ($source ne "") {
930 push @{@$project_settings[$T_SOURCES_CXX]},$source;
933 @$project_settings[$T_SOURCES_CXX]=[sort @{@$project_settings[$T_SOURCES_CXX]}];
934 foreach my $source (@sources_rc) {
935 if ($source ne "") {
936 push @{@$project_settings[$T_SOURCES_RC]},$source;
939 @$project_settings[$T_SOURCES_RC]=[sort @{@$project_settings[$T_SOURCES_RC]}];
940 foreach my $source (@sources_misc) {
941 if ($source ne "") {
942 push @{@$project_settings[$T_SOURCES_MISC]},$source;
945 @$project_settings[$T_SOURCES_MISC]=[sort @{@$project_settings[$T_SOURCES_MISC]}];
947 # Finally if we are building both libraries and programs in
948 # this directory, then the programs should be linked with all
949 # the libraries
950 if (@local_dlls > 0 and @exe_list > 0) {
951 foreach my $target (@exe_list) {
952 push @{@$target[$T_DLL_PATH]},"-L.";
953 push @{@$target[$T_DLLS]},@local_dlls;
959 # Scan the source directories in search of things to build
960 sub source_scan()
962 # If there's a single target then this is going to be the default target
963 if (defined $opt_single_target) {
964 # Create the main target
965 my $main_target=[];
966 target_init($main_target);
967 @$main_target[$T_NAME]=$opt_single_target;
968 @$main_target[$T_TYPE]=$opt_target_type;
970 # Add it to the list
971 push @{$main_project[$P_TARGETS]},$main_target;
974 # The main directory is always going to be there
975 push @projects,\@main_project;
977 # Now scan the directory tree looking for source files and, maybe, targets
978 print "Scanning the source directories...\n";
979 source_scan_directory(\@main_project,"","",0);
981 @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
984 #####
986 # Source search
988 #####
991 # Performs a directory traversal and renames the files so that:
992 # - they have the case desired by the user
993 # - their extension is of the appropriate case
994 # - they don't contain annoying characters like ' ', '$', '#', ...
995 sub fix_file_and_directory_names($);
996 sub fix_file_and_directory_names($)
998 my $dirname=$_[0];
1000 if (opendir(DIRECTORY, "$dirname")) {
1001 foreach my $dentry (readdir DIRECTORY) {
1002 if ($dentry =~ /^\./ or $dentry eq "CVS") {
1003 next;
1005 # Set $warn to 1 if the user should be warned of the renaming
1006 my $warn=0;
1008 # autoconf and make don't support these characters well
1009 my $new_name=$dentry;
1010 $new_name =~ s/[ \$]/_/g;
1012 # Only all lowercase extensions are supported (because of the
1013 # transformations ':.c=.o') .
1014 if (-f "$dirname/$new_name") {
1015 if ($new_name =~ /\.C$/) {
1016 $new_name =~ s/\.C$/.c/;
1018 if ($new_name =~ /\.cpp$/i) {
1019 $new_name =~ s/\.cpp$/.cpp/i;
1021 if ($new_name =~ s/\.cxx$/.cpp/i) {
1022 $warn=1;
1024 if ($new_name =~ /\.rc$/i) {
1025 $new_name =~ s/\.rc$/.rc/i;
1027 # And this last one is to avoid confusion then running make
1028 if ($new_name =~ s/^makefile$/makefile.win/) {
1029 $warn=1;
1033 # Adjust the case to the user's preferences
1034 if (($opt_lower == $OPT_LOWER_ALL and $dentry =~ /[A-Z]/) or
1035 ($opt_lower == $OPT_LOWER_UPPERCASE and $dentry !~ /[a-z]/)
1037 $new_name=lc $new_name;
1040 # And finally, perform the renaming
1041 if ($new_name ne $dentry) {
1042 if ($warn) {
1043 print STDERR "warning: in \"$dirname\", renaming \"$dentry\" to \"$new_name\"\n";
1045 if (!rename("$dirname/$dentry","$dirname/$new_name")) {
1046 print STDERR "error: in \"$dirname\", unable to rename \"$dentry\" to \"$new_name\"\n";
1047 print STDERR " $!\n";
1048 $new_name=$dentry;
1051 if (-d "$dirname/$new_name") {
1052 fix_file_and_directory_names("$dirname/$new_name");
1055 closedir(DIRECTORY);
1061 #####
1063 # Source fixup
1065 #####
1068 # Try to find a file for the specified filename. The attempt is
1069 # case-insensitive which is why it's not trivial. If a match is
1070 # found then we return the pathname with the correct case.
1071 sub search_from($$)
1073 my $dirname=$_[0];
1074 my $path=$_[1];
1075 my $real_path="";
1077 if ($dirname eq "" or $dirname eq ".") {
1078 $dirname=cwd;
1079 } elsif ($dirname !~ m+^/+) {
1080 $dirname=cwd . "/" . $dirname;
1082 if ($dirname !~ m+/$+) {
1083 $dirname.="/";
1086 foreach my $component (@$path) {
1087 #print " looking for $component in \"$dirname\"\n";
1088 if ($component eq ".") {
1089 # Pass it as is
1090 $real_path.="./";
1091 } elsif ($component eq "..") {
1092 # Go up one level
1093 $dirname=dirname($dirname) . "/";
1094 $real_path.="../";
1095 } else {
1096 # The file/directory may have been renamed before. Also try to
1097 # match the renamed file.
1098 my $renamed=$component;
1099 $renamed =~ s/[ \$]/_/g;
1100 if ($renamed eq $component) {
1101 undef $renamed;
1104 my $directory=get_directory_contents $dirname;
1105 my $found;
1106 foreach my $dentry (@$directory) {
1107 if ($dentry =~ /^$component$/i or
1108 (defined $renamed and $dentry =~ /^$renamed$/i)
1110 $dirname.="$dentry/";
1111 $real_path.="$dentry/";
1112 $found=1;
1113 last;
1116 if (!defined $found) {
1117 # Give up
1118 #print " could not find $component in $dirname\n";
1119 return;
1123 $real_path=~ s+/$++;
1124 #print " -> found $real_path\n";
1125 return $real_path;
1129 # Performs a case-insensitive search for the specified file in the
1130 # include path.
1131 # $line is the line number that should be referenced when an error occurs
1132 # $filename is the file we are looking for
1133 # $dirname is the directory of the file containing the '#include' directive
1134 # if '"' was used, it is an empty string otherwise
1135 # $project and $target specify part of the include path
1136 sub get_real_include_name($$$$$)
1138 my $line=$_[0];
1139 my $filename=$_[1];
1140 my $dirname=$_[2];
1141 my $project=$_[3];
1142 my $target=$_[4];
1144 if ($filename =~ /^([a-zA-Z]:)?[\/]/ or $filename =~ /^[a-zA-Z]:[\/]?/) {
1145 # This is not a relative path, we cannot make any check
1146 my $warning="path:$filename";
1147 if (!defined $warnings{$warning}) {
1148 $warnings{$warning}="1";
1149 print STDERR "warning: cannot check the case of absolute pathnames:\n";
1150 print STDERR "$line: $filename\n";
1152 } else {
1153 # Here's how we proceed:
1154 # - split the filename we look for into its components
1155 # - then for each directory in the include path
1156 # - trace the directory components starting from that directory
1157 # - if we fail to find a match at any point then continue with
1158 # the next directory in the include path
1159 # - otherwise, rejoice, our quest is over.
1160 my @file_components=split /[\/\\]+/, $filename;
1161 #print " Searching for $filename from @$project[$P_PATH]\n";
1163 my $real_filename;
1164 if ($dirname ne "") {
1165 # This is an 'include ""' -> look in dirname first.
1166 #print " in $dirname (include \"\")\n";
1167 $real_filename=search_from($dirname,\@file_components);
1168 if (defined $real_filename) {
1169 return $real_filename;
1172 my $project_settings=@$project[$P_SETTINGS];
1173 foreach my $include (@{@$target[$T_INCLUDE_PATH]}, @{@$project_settings[$T_INCLUDE_PATH]}) {
1174 my $dirname=$include;
1175 $dirname=~ s+^-I++;
1176 if (!is_absolute($dirname)) {
1177 $dirname="@$project[$P_PATH]$dirname";
1178 } else {
1179 $dirname=~ s+^\$\(TOPSRCDIR\)/++;
1180 $dirname=~ s+^\$\(SRCDIR\)/+@$project[$P_PATH]+;
1182 #print " in $dirname\n";
1183 $real_filename=search_from("$dirname",\@file_components);
1184 if (defined $real_filename) {
1185 return $real_filename;
1188 my $dotdotpath=@$project[$P_PATH];
1189 $dotdotpath =~ s/[^\/]+/../g;
1190 foreach my $include (@{$global_settings[$T_INCLUDE_PATH]}) {
1191 my $dirname=$include;
1192 $dirname=~ s+^-I++;
1193 $dirname=~ s+^\$\(TOPSRCDIR\)\/++;
1194 $dirname=~ s+^\$\(SRCDIR\)\/+@$project[$P_PATH]+;
1195 #print " in $dirname (global setting)\n";
1196 $real_filename=search_from("$dirname",\@file_components);
1197 if (defined $real_filename) {
1198 return $real_filename;
1202 $filename =~ s+\\\\+/+g; # in include ""
1203 $filename =~ s+\\+/+g; # in include <> !
1204 if ($opt_lower_include) {
1205 return lc "$filename";
1207 return $filename;
1210 sub print_pack($$$)
1212 my $indent=$_[0];
1213 my $size=$_[1];
1214 my $trailer=$_[2];
1216 if ($size =~ /^(1|2|4|8)$/) {
1217 print FILEO "$indent#include <pshpack$size.h>$trailer";
1218 } else {
1219 print FILEO "$indent/* winemaker:warning: Unknown size \"$size\". Defaulting to 4 */\n";
1220 print FILEO "$indent#include <pshpack4.h>$trailer";
1225 # 'Parses' a source file and fixes constructs that would not work with
1226 # Winelib. The parsing is rather simple and not all non-portable features
1227 # are corrected. The most important feature that is corrected is the case
1228 # and path separator of '#include' directives. This requires that each
1229 # source file be associated to a project & target so that the proper
1230 # include path is used.
1231 # Also note that the include path is relative to the directory in which the
1232 # compiler is run, i.e. that of the project, not to that of the file.
1233 sub fix_file($$$)
1235 my $filename=$_[0];
1236 my $project=$_[1];
1237 my $target=$_[2];
1238 $filename="@$project[$P_PATH]$filename";
1239 if (! -e $filename) {
1240 return;
1243 my $is_rc=($filename =~ /\.(rc2?|dlg)$/i);
1244 my $dirname=dirname($filename);
1245 my $is_mfc=0;
1246 if (defined $target and (@$target[$T_FLAGS] & $TF_MFC)) {
1247 $is_mfc=1;
1250 print " $filename\n";
1251 #FIXME:assuming that because there is a .bak file, this is what we want is
1252 #probably flawed. Or is it???
1253 if (! -e "$filename.bak") {
1254 if (!copy("$filename","$filename.bak")) {
1255 print STDERR "error: unable to make a backup of $filename:\n";
1256 print STDERR " $!\n";
1257 return;
1260 if (!open(FILEI,"$filename.bak")) {
1261 print STDERR "error: unable to open $filename.bak for reading:\n";
1262 print STDERR " $!\n";
1263 return;
1265 if (!open(FILEO,">$filename")) {
1266 print STDERR "error: unable to open $filename for writing:\n";
1267 print STDERR " $!\n";
1268 return;
1270 my $line=0;
1271 my $modified=0;
1272 my $rc_block_depth=0;
1273 my $rc_textinclude_state=0;
1274 my @pack_stack;
1275 while (<FILEI>) {
1276 # Remove any trailing CtrlZ, which isn't strictly in the file
1277 if (/\x1A/) {
1278 s/\x1A//;
1279 last if (/^$/)
1281 $line++;
1282 s/\r\n$/\n/;
1283 if (!/\n$/) {
1284 # Make sure all files are '\n' terminated
1285 $_ .= "\n";
1287 if ($is_rc and !$is_mfc and /^(\s*)(\#\s*include\s*)\"afxres\.h\"/) {
1288 # VC6 automatically includes 'afxres.h', an MFC specific header, in
1289 # the RC files it generates (even in non-MFC projects). So we replace
1290 # it with 'winres.h' its very close standard cousin so that non MFC
1291 # projects can compile in Wine without the MFC sources.
1292 my $warning="mfc:afxres.h";
1293 if (!defined $warnings{$warning}) {
1294 $warnings{$warning}="1";
1295 print STDERR "warning: In non-MFC projects, winemaker replaces the MFC specific header 'afxres.h' with 'winres.h'\n";
1296 print STDERR "warning: the above warning is issued only once\n";
1298 print FILEO "$1/* winemaker: $2\"afxres.h\" */\n";
1299 print FILEO "$1/* winemaker:warning: 'afxres.h' is an MFC specific header. Replacing it with 'winres.h' */\n";
1300 print FILEO "$1$2\"winres.h\"$'";
1301 $modified=1;
1303 } elsif (/^(\s*\#\s*include\s*)([\"<])([^\"]+)([\">])/) {
1304 my $from_file=($2 eq "<"?"":$dirname);
1305 my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1306 print FILEO "$1$2$real_include_name$4$'";
1307 $modified|=($real_include_name ne $3);
1309 } elsif (s/^(\s*)(\#\s*pragma\s+pack\s*\(\s*)//) {
1310 # Pragma pack handling
1312 # pack_stack is an array of references describing the stack of
1313 # pack directives currently in effect. Each directive if described
1314 # by a reference to an array containing:
1315 # - "push" for pack(push,...) directives, "" otherwise
1316 # - the directive's identifier at index 1
1317 # - the directive's alignement value at index 2
1319 # Don't believe a word of what the documentation says: it's all wrong.
1320 # The code below is based on the actual behavior of Visual C/C++ 6.
1321 my $pack_indent=$1;
1322 my $pack_header=$2;
1323 if (/^(\))/) {
1324 # pragma pack()
1325 # Pushes the default stack alignment
1326 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1327 print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1328 print_pack($pack_indent,4,$');
1329 push @pack_stack, [ "", "", 4 ];
1331 } elsif (/^(pop\s*(,\s*\d+\s*)?\))/) {
1332 # pragma pack(pop)
1333 # pragma pack(pop,n)
1334 # Goes up the stack until it finds a pack(push,...), and pops it
1335 # Ignores any pack(n) entry
1336 # Issues a warning if the pack is of the form pack(push,label)
1337 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1338 my $pack_comment=$';
1339 $pack_comment =~ s/^\s*//;
1340 if ($pack_comment ne "") {
1341 print FILEO "$pack_indent$pack_comment";
1343 while (1) {
1344 my $alignment=pop @pack_stack;
1345 if (!defined $alignment) {
1346 print FILEO "$pack_indent/* winemaker:warning: No pack(push,...) found. All the stack has been popped */\n";
1347 last;
1349 if (@$alignment[1]) {
1350 print FILEO "$pack_indent/* winemaker:warning: Anonymous pop of pack(push,@$alignment[1]) (@$alignment[2]) */\n";
1352 print FILEO "$pack_indent#include <poppack.h>\n";
1353 if (@$alignment[0]) {
1354 last;
1358 } elsif (/^(pop\s*,\s*(\w+)\s*(,\s*\d+\s*)?\))/) {
1359 # pragma pack(pop,label[,n])
1360 # Goes up the stack until finding a pack(push,...) and pops it.
1361 # 'n', if specified, is ignored.
1362 # Ignores any pack(n) entry
1363 # Issues a warning if the label of the pack does not match,
1364 # or if it is in fact a pack(push,n)
1365 my $label=$2;
1366 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1367 my $pack_comment=$';
1368 $pack_comment =~ s/^\s*//;
1369 if ($pack_comment ne "") {
1370 print FILEO "$pack_indent$pack_comment";
1372 while (1) {
1373 my $alignment=pop @pack_stack;
1374 if (!defined $alignment) {
1375 print FILEO "$pack_indent/* winemaker:warning: No pack(push,$label) found. All the stack has been popped */\n";
1376 last;
1378 if (@$alignment[1] and @$alignment[1] ne $label) {
1379 print FILEO "$pack_indent/* winemaker:warning: Push/pop mismatch: \"@$alignment[1]\" (@$alignment[2]) != \"$label\" */\n";
1381 print FILEO "$pack_indent#include <poppack.h>\n";
1382 if (@$alignment[0]) {
1383 last;
1387 } elsif (/^(push\s*\))/) {
1388 # pragma pack(push)
1389 # Push the current alignment
1390 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1391 if (@pack_stack > 0) {
1392 my $alignment=$pack_stack[$#pack_stack];
1393 print_pack($pack_indent,@$alignment[2],$');
1394 push @pack_stack, [ "push", "", @$alignment[2] ];
1395 } else {
1396 print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1397 print_pack($pack_indent,4,$');
1398 push @pack_stack, [ "push", "", 4 ];
1401 } elsif (/^((push\s*,\s*)?(\d+)\s*\))/) {
1402 # pragma pack([push,]n)
1403 # Push new alignment n
1404 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1405 print_pack($pack_indent,$3,"$'");
1406 push @pack_stack, [ ($2 ? "push" : ""), "", $3 ];
1408 } elsif (/^((\w+)\s*\))/) {
1409 # pragma pack(label)
1410 # label must in fact be a macro that resolves to an integer
1411 # Then behaves like 'pragma pack(n)'
1412 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1413 print FILEO "$pack_indent/* winemaker:warning: Assuming $2 == 4 */\n";
1414 print_pack($pack_indent,4,$');
1415 push @pack_stack, [ "", "", 4 ];
1417 } elsif (/^(push\s*,\s*(\w+)\s*(,\s*(\d+)\s*)?\))/) {
1418 # pragma pack(push,label[,n])
1419 # Pushes a new label on the stack. It is possible to push the same
1420 # label multiple times. If 'n' is omitted then the alignment is
1421 # unchanged. Otherwise it becomes 'n'.
1422 print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
1423 my $size;
1424 if (defined $4) {
1425 $size=$4;
1426 } elsif (@pack_stack > 0) {
1427 my $alignment=$pack_stack[$#pack_stack];
1428 $size=@$alignment[2];
1429 } else {
1430 print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
1431 $size=4;
1433 print_pack($pack_indent,$size,$');
1434 push @pack_stack, [ "push", $2, $size ];
1436 } else {
1437 # pragma pack(??? -> What's that?
1438 print FILEO "$pack_indent/* winemaker:warning: Unknown type of pragma pack directive */\n";
1439 print FILEO "$pack_indent$pack_header$_";
1442 $modified=1;
1444 } elsif ($is_rc) {
1445 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]+)([\">]?)/) {
1446 my $from_file=($5 eq "<"?"":$dirname);
1447 my $real_include_name=get_real_include_name($line,$6,$from_file,$project,$target);
1448 print FILEO "$1$5$real_include_name$7$'";
1449 $modified|=($real_include_name ne $6);
1451 } elsif (/^(\s*RCINCLUDE\s*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
1452 my $from_file=($2 eq "<"?"":$dirname);
1453 my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
1454 print FILEO "$1$2$real_include_name$4$'";
1455 $modified|=($real_include_name ne $3);
1457 } elsif ($is_rc and !$is_mfc and $rc_block_depth == 0 and /^\s*\d+\s+TEXTINCLUDE\s*/) {
1458 $rc_textinclude_state=1;
1459 print FILEO;
1461 } elsif ($rc_textinclude_state == 3 and /^(\s*\"\#\s*include\s*\"\")afxres\.h(\"\"\\r\\n\")/) {
1462 print FILEO "$1winres.h$2$'";
1463 $modified=1;
1465 } elsif (/^\s*BEGIN(\W.*)?$/) {
1466 $rc_textinclude_state|=2;
1467 $rc_block_depth++;
1468 print FILEO;
1470 } elsif (/^\s*END(\W.*)?$/) {
1471 $rc_textinclude_state=0;
1472 if ($rc_block_depth>0) {
1473 $rc_block_depth--;
1475 print FILEO;
1477 } else {
1478 print FILEO;
1481 } else {
1482 print FILEO;
1486 close(FILEI);
1487 close(FILEO);
1488 if ($opt_backup == 0 or $modified == 0) {
1489 if (!unlink("$filename.bak")) {
1490 print STDERR "error: unable to delete $filename.bak:\n";
1491 print STDERR " $!\n";
1497 # Analyzes each source file in turn to find and correct issues
1498 # that would cause it not to compile.
1499 sub fix_source()
1501 print "Fixing the source files...\n";
1502 foreach my $project (@projects) {
1503 foreach my $target (@$project[$P_SETTINGS],@{@$project[$P_TARGETS]}) {
1504 foreach my $source (@{@$target[$T_SOURCES_C]}, @{@$target[$T_SOURCES_CXX]}, @{@$target[$T_SOURCES_RC]}, @{@$target[$T_SOURCES_MISC]}) {
1505 fix_file($source,$project,$target);
1513 #####
1515 # File generation
1517 #####
1520 # A convenience function to generate all the lists (defines,
1521 # C sources, C++ source, etc.) in the Makefile
1522 sub generate_list($$$;$)
1524 my $name=$_[0];
1525 my $last=$_[1];
1526 my $list=$_[2];
1527 my $data=$_[3];
1528 my $first=$name;
1530 if ($name) {
1531 printf FILEO "%-22s=",$name;
1533 if (defined $list) {
1534 foreach my $item (@$list) {
1535 my $value;
1536 if (defined $data) {
1537 $value=&$data($item);
1538 } else {
1539 $value=$item;
1541 if ($value ne "") {
1542 if ($first) {
1543 print FILEO " $value";
1544 $first=0;
1545 } else {
1546 print FILEO " \\\n\t\t\t$value";
1551 if ($last) {
1552 print FILEO "\n";
1557 # Generates a project's Makefile and all the target files
1558 sub generate_project_files($)
1560 my $project=$_[0];
1561 my $project_settings=@$project[$P_SETTINGS];
1562 my @dll_list=();
1563 my @exe_list=();
1565 # Then sort the targets and separate the libraries from the programs
1566 foreach my $target (sort { @$a[$T_NAME] cmp @$b[$T_NAME] } @{@$project[$P_TARGETS]}) {
1567 if (@$target[$T_TYPE] == $TT_DLL) {
1568 push @dll_list,$target;
1569 } else {
1570 push @exe_list,$target;
1573 @$project[$P_TARGETS]=[];
1574 push @{@$project[$P_TARGETS]}, @dll_list;
1575 push @{@$project[$P_TARGETS]}, @exe_list;
1577 if (!open(FILEO,">@$project[$P_PATH]Makefile")) {
1578 print STDERR "error: could not open \"@$project[$P_PATH]/Makefile\" for writing\n";
1579 print STDERR " $!\n";
1580 return;
1583 print FILEO "### Generated by Winemaker\n";
1584 print FILEO "\n\n";
1586 generate_list("SRCDIR",1,[ "." ]);
1587 if (@$project[$P_PATH] eq "") {
1588 # This is the main project. It is also responsible for recursively
1589 # calling the other projects
1590 generate_list("SUBDIRS",1,\@projects,sub
1592 if ($_[0] != \@main_project) {
1593 my $subdir=@{$_[0]}[$P_PATH];
1594 $subdir =~ s+/$++;
1595 return $subdir;
1597 # Eliminating the main project by returning undefined!
1600 if (@{@$project[$P_TARGETS]} > 0) {
1601 generate_list("DLLS",1,\@dll_list,sub
1603 return @{$_[0]}[$T_NAME];
1605 generate_list("EXES",1,\@exe_list,sub
1607 return "@{$_[0]}[$T_NAME]";
1609 print FILEO "\n\n\n";
1611 print FILEO "### Common settings\n\n";
1612 # Make it so that the project-wide settings override the global settings
1613 generate_list("CEXTRA",1,@$project_settings[$T_CEXTRA]);
1614 generate_list("CXXEXTRA",1,@$project_settings[$T_CXXEXTRA]);
1615 generate_list("RCEXTRA",1,@$project_settings[$T_RCEXTRA]);
1616 generate_list("INCLUDE_PATH",1,@$project_settings[$T_INCLUDE_PATH]);
1617 generate_list("DLL_PATH",1,@$project_settings[$T_DLL_PATH]);
1618 generate_list("LIBRARY_PATH",1,@$project_settings[$T_LIBRARY_PATH]);
1619 generate_list("LIBRARIES",1,@$project_settings[$T_LIBRARIES]);
1620 print FILEO "\n\n";
1622 my $extra_source_count=@{@$project_settings[$T_SOURCES_C]}+
1623 @{@$project_settings[$T_SOURCES_CXX]}+
1624 @{@$project_settings[$T_SOURCES_RC]};
1625 my $no_extra=($extra_source_count == 0);
1626 if (!$no_extra) {
1627 print FILEO "### Extra source lists\n\n";
1628 generate_list("EXTRA_C_SRCS",1,@$project_settings[$T_SOURCES_C]);
1629 generate_list("EXTRA_CXX_SRCS",1,@$project_settings[$T_SOURCES_CXX]);
1630 generate_list("EXTRA_RC_SRCS",1,@$project_settings[$T_SOURCES_RC]);
1631 print FILEO "\n";
1632 generate_list("EXTRA_OBJS",1,["\$(EXTRA_C_SRCS:.c=.o)","\$(EXTRA_CXX_SRCS:.cpp=.o)"]);
1633 print FILEO "\n\n\n";
1636 # Iterate over all the targets...
1637 foreach my $target (@{@$project[$P_TARGETS]}) {
1638 print FILEO "### @$target[$T_NAME] sources and settings\n\n";
1639 my $canon=canonize("@$target[$T_NAME]");
1640 $canon =~ s+_so$++;
1642 generate_list("${canon}_MODULE",1,[@$target[$T_NAME]]);
1643 generate_list("${canon}_C_SRCS",1,@$target[$T_SOURCES_C]);
1644 generate_list("${canon}_CXX_SRCS",1,@$target[$T_SOURCES_CXX]);
1645 generate_list("${canon}_RC_SRCS",1,@$target[$T_SOURCES_RC]);
1646 generate_list("${canon}_LDFLAGS",1,@$target[$T_LDFLAGS]);
1647 generate_list("${canon}_DLL_PATH",1,@$target[$T_DLL_PATH]);
1648 generate_list("${canon}_DLLS",1,@$target[$T_DLLS]);
1649 generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH]);
1650 generate_list("${canon}_LIBRARIES",1,@$target[$T_LIBRARIES]);
1651 print FILEO "\n";
1652 generate_list("${canon}_OBJS",1,["\$(${canon}_C_SRCS:.c=.o)","\$(${canon}_CXX_SRCS:.cpp=.o)","\$(${canon}_RC_SRCS:.rc=.res)"]);
1653 print FILEO "\n\n\n";
1655 print FILEO "### Global source lists\n\n";
1656 generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub
1658 my $canon=canonize(@{$_[0]}[$T_NAME]);
1659 $canon =~ s+_so$++;
1660 return "\$(${canon}_C_SRCS)";
1662 if (!$no_extra) {
1663 generate_list("",1,[ "\$(EXTRA_C_SRCS)" ]);
1665 generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub
1667 my $canon=canonize(@{$_[0]}[$T_NAME]);
1668 $canon =~ s+_so$++;
1669 return "\$(${canon}_CXX_SRCS)";
1671 if (!$no_extra) {
1672 generate_list("",1,[ "\$(EXTRA_CXX_SRCS)" ]);
1674 generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub
1676 my $canon=canonize(@{$_[0]}[$T_NAME]);
1677 $canon =~ s+_so$++;
1678 return "\$(${canon}_RC_SRCS)";
1680 if (!$no_extra) {
1681 generate_list("",1,[ "\$(EXTRA_RC_SRCS)" ]);
1684 print FILEO "\n\n";
1685 print FILEO "### Tools\n\n";
1686 print FILEO "CC = winegcc\n";
1687 print FILEO "CXX = wineg++\n";
1688 print FILEO "RC = wrc\n";
1689 print FILEO "WINEBUILD = winebuild\n";
1690 print FILEO "\n\n";
1692 print FILEO "### Generic targets\n\n";
1693 print FILEO "all:";
1694 if (@$project[$P_PATH] eq "") {
1695 print FILEO " \$(SUBDIRS)";
1697 if (@{@$project[$P_TARGETS]} > 0) {
1698 print FILEO " \$(DLLS:%=%.so) \$(EXES:%=%.so)";
1700 print FILEO "\n\n";
1701 print FILEO "### Build rules\n";
1702 print FILEO "\n";
1703 print FILEO ".PHONY: all clean dummy\n";
1704 print FILEO "\n";
1705 print FILEO "\$(SUBDIRS): dummy\n";
1706 print FILEO "\t\@cd \$\@ && \$(MAKE)\n";
1707 print FILEO "\n";
1708 print FILEO "# Implicit rules\n";
1709 print FILEO "\n";
1710 print FILEO ".SUFFIXES: .cpp .rc .res\n";
1711 print FILEO "DEFINCL = \$(INCLUDE_PATH) \$(DEFINES) \$(OPTIONS)\n";
1712 print FILEO "\n";
1713 print FILEO ".c.o:\n";
1714 print FILEO "\t\$(CC) -c \$(CFLAGS) \$(CEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
1715 print FILEO "\n";
1716 print FILEO ".cpp.o:\n";
1717 print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
1718 print FILEO "\n";
1719 print FILEO ".cxx.o:\n";
1720 print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
1721 print FILEO "\n";
1722 print FILEO ".rc.res:\n";
1723 print FILEO "\t\$(RC) \$(RCFLAGS) \$(RCEXTRA) \$(DEFINCL) -fo\$@ \$<\n";
1724 print FILEO "\n";
1725 print FILEO "# Rules for cleaning\n";
1726 print FILEO "\n";
1727 print FILEO "CLEAN_FILES = *.dbg.c y.tab.c y.tab.h lex.yy.c \\\n";
1728 print FILEO " core *.orig *.rej \\\n";
1729 print FILEO " \\\\\\#*\\\\\\# *~ *% .\\\\\\#*\n";
1730 print FILEO "\n";
1731 print FILEO "clean:: \$(SUBDIRS:%=%/__clean__) \$(EXTRASUBDIRS:%=%/__clean__)\n";
1732 print FILEO "\t\$(RM) \$(CLEAN_FILES) \$(RC_SRCS:.rc=.res) \$(C_SRCS:.c=.o) \$(CXX_SRCS:.cpp=.o)\n";
1733 print FILEO "\t\$(RM) \$(DLLS:%=%.dbg.o) \$(DLLS:%=%.so)\n";
1734 print FILEO "\t\$(RM) \$(EXES:%=%.dbg.o) \$(EXES:%=%.so) \$(EXES:%.exe=%)\n";
1735 print FILEO "\n";
1736 print FILEO "\$(SUBDIRS:%=%/__clean__): dummy\n";
1737 print FILEO "\tcd `dirname \$\@` && \$(MAKE) clean\n";
1738 print FILEO "\n";
1739 print FILEO "\$(EXTRASUBDIRS:%=%/__clean__): dummy\n";
1740 print FILEO "\t-cd `dirname \$\@` && \$(RM) \$(CLEAN_FILES)\n";
1741 print FILEO "\n";
1743 if (@{@$project[$P_TARGETS]} > 0) {
1744 print FILEO "### Target specific build rules\n";
1745 print FILEO "DEFLIB = \$(LIBRARY_PATH) \$(LIBRARIES) \$(DLL_PATH)\n\n";
1746 foreach my $target (@{@$project[$P_TARGETS]}) {
1747 my $canon=canonize("@$target[$T_NAME]");
1748 $canon =~ s/_so$//;
1750 print FILEO "\$(${canon}_MODULE).dbg.c: \$(${canon}_C_SRCS) \$(${canon}_CXX_SRCS)\n";
1751 print FILEO "\t\$(WINEBUILD) -o \$\@ --debug -C\$(SRCDIR) \$(${canon}_C_SRCS) \$(${canon}_CXX_SRCS)\n";
1752 print FILEO "\n";
1753 print FILEO "\$(${canon}_MODULE).so: \$(${canon}_MODULE).dbg.o \$(${canon}_OBJS)\n";
1754 if (@{@$target[$T_SOURCES_CXX]} > 0 or @{@$project_settings[$T_SOURCES_CXX]} > 0) {
1755 print FILEO "\t\$(CXX)";
1756 } else {
1757 print FILEO "\t\$(CC)";
1759 print FILEO " \$(${canon}_LDFLAGS) -o \$\@ \$(${canon}_OBJS) \$(${canon}_MODULE).dbg.o \$(${canon}_LIBRARY_PATH) \$(DEFLIB) \$(${canon}_DLLS:%=-l%) \$(${canon}_LIBRARIES:%=-l%)\n";
1760 print FILEO "\n\n";
1763 close(FILEO);
1769 # This is where we finally generate files. In fact this method does not
1770 # do anything itself but calls the methods that do the actual work.
1771 sub generate()
1773 print "Generating project files...\n";
1775 foreach my $project (@projects) {
1776 my $path=@$project[$P_PATH];
1777 if ($path eq "") {
1778 $path=".";
1779 } else {
1780 $path =~ s+/$++;
1782 print " $path\n";
1783 generate_project_files($project);
1789 #####
1791 # Option defaults
1793 #####
1795 $opt_backup=1;
1796 $opt_lower=$OPT_LOWER_UPPERCASE;
1797 $opt_lower_include=1;
1799 $opt_work_dir=undef;
1800 $opt_single_target=undef;
1801 $opt_target_type=$TT_GUIEXE;
1802 $opt_flags=0;
1803 $opt_is_interactive=$OPT_ASK_NO;
1804 $opt_ask_project_options=$OPT_ASK_NO;
1805 $opt_ask_target_options=$OPT_ASK_NO;
1806 $opt_no_generated_files=0;
1807 $opt_no_source_fix=0;
1808 $opt_no_banner=0;
1812 #####
1814 # Main
1816 #####
1818 sub print_banner()
1820 print "Winemaker $version\n";
1821 print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
1824 sub usage()
1826 print_banner();
1827 print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup] [--nosource-fix]\n";
1828 print STDERR " [--lower-none|--lower-all|--lower-uppercase]\n";
1829 print STDERR " [--lower-include|--nolower-include] [--mfc|--nomfc]\n";
1830 print STDERR " [--guiexe|--windows|--cuiexe|--console|--dll]\n";
1831 print STDERR " [-Dmacro[=defn]] [-Idir] [-Pdir] [-idll] [-Ldir] [-llibrary]\n";
1832 print STDERR " [--nodlls] [--nomsvcrt] [--interactive] [--single-target name]\n";
1833 print STDERR " [--generated-files|--nogenerated-files]\n";
1834 print STDERR " work_directory\n";
1835 print STDERR "\nWinemaker is designed to recursively convert all the Windows sources found in\n";
1836 print STDERR "the specified directory so that they can be compiled with Winelib. During this\n";
1837 print STDERR "process it will modify and rename some of the files in that directory.\n";
1838 print STDERR "\tPlease read the manual page before use.\n";
1839 exit (2);
1842 target_init(\@global_settings);
1844 while (@ARGV>0) {
1845 my $arg=shift @ARGV;
1846 # General options
1847 if ($arg eq "--nobanner") {
1848 $opt_no_banner=1;
1849 } elsif ($arg eq "--backup") {
1850 $opt_backup=1;
1851 } elsif ($arg eq "--nobackup") {
1852 $opt_backup=0;
1853 } elsif ($arg eq "--single-target") {
1854 $opt_single_target=shift @ARGV;
1855 } elsif ($arg eq "--lower-none") {
1856 $opt_lower=$OPT_LOWER_NONE;
1857 } elsif ($arg eq "--lower-all") {
1858 $opt_lower=$OPT_LOWER_ALL;
1859 } elsif ($arg eq "--lower-uppercase") {
1860 $opt_lower=$OPT_LOWER_UPPERCASE;
1861 } elsif ($arg eq "--lower-include") {
1862 $opt_lower_include=1;
1863 } elsif ($arg eq "--nolower-include") {
1864 $opt_lower_include=0;
1865 } elsif ($arg eq "--nosource-fix") {
1866 $opt_no_source_fix=1;
1867 } elsif ($arg eq "--generated-files") {
1868 $opt_no_generated_files=0;
1869 } elsif ($arg eq "--nogenerated-files") {
1870 $opt_no_generated_files=1;
1871 } elsif ($arg =~ /^-D/) {
1872 push @{$global_settings[$T_DEFINES]},$arg;
1873 } elsif ($arg =~ /^-I/) {
1874 push @{$global_settings[$T_INCLUDE_PATH]},$arg;
1875 } elsif ($arg =~ /^-P/) {
1876 push @{$global_settings[$T_DLL_PATH]},"-L$'";
1877 } elsif ($arg =~ /^-i/) {
1878 push @{$global_settings[$T_DLLS]},$';
1879 } elsif ($arg =~ /^-L/) {
1880 push @{$global_settings[$T_LIBRARY_PATH]},$arg;
1881 } elsif ($arg =~ /^-l/) {
1882 push @{$global_settings[$T_LIBRARIES]},$';
1884 # 'Source'-based method options
1885 } elsif ($arg eq "--dll") {
1886 $opt_target_type=$TT_DLL;
1887 } elsif ($arg eq "--guiexe" or $arg eq "--windows") {
1888 $opt_target_type=$TT_GUIEXE;
1889 } elsif ($arg eq "--cuiexe" or $arg eq "--console") {
1890 $opt_target_type=$TT_CUIEXE;
1891 } elsif ($arg eq "--interactive") {
1892 $opt_is_interactive=$OPT_ASK_YES;
1893 $opt_ask_project_options=$OPT_ASK_YES;
1894 $opt_ask_target_options=$OPT_ASK_YES;
1895 } elsif ($arg eq "--mfc") {
1896 $opt_flags|=$TF_MFC;
1897 } elsif ($arg eq "--nomfc") {
1898 $opt_flags&=~$TF_MFC;
1899 $opt_flags|=$TF_NOMFC;
1900 } elsif ($arg eq "--nodlls") {
1901 $opt_flags|=$TF_NODLLS;
1902 } elsif ($arg eq "--nomsvcrt") {
1903 $opt_flags|=$TF_NOMSVCRT;
1905 # Catch errors
1906 } else {
1907 if ($arg ne "--help" and $arg ne "-h" and $arg ne "-?") {
1908 if (!defined $opt_work_dir) {
1909 $opt_work_dir=$arg;
1910 } else {
1911 print STDERR "error: the work directory, \"$arg\", has already been specified (was \"$opt_work_dir\")\n";
1912 usage();
1914 } else {
1915 usage();
1920 if (!defined $opt_work_dir) {
1921 print STDERR "error: you must specify the directory containing the sources to be converted\n";
1922 usage();
1923 } elsif (!chdir $opt_work_dir) {
1924 print STDERR "error: could not chdir to the work directory\n";
1925 print STDERR " $!\n";
1926 usage();
1929 if ($opt_no_banner == 0) {
1930 print_banner();
1933 project_init(\@main_project,"");
1935 # Fix the file and directory names
1936 fix_file_and_directory_names(".");
1938 # Scan the sources to identify the projects and targets
1939 source_scan();
1941 # Fix the source files
1942 if (! $opt_no_source_fix) {
1943 fix_source();
1946 # Generate the Makefile and the spec file
1947 if (! $opt_no_generated_files) {
1948 generate();