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