winemaker 57.6 KB
Newer Older
1
#!/usr/bin/perl -w
2
use strict;
3

4
# Copyright 2000-2004 Francois Gouget for CodeWeavers
5
# Copyright 2004 Dimitrie O. Paun
6
#
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

22
my $version="0.6.0";
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

use Cwd;
use File::Basename;
use File::Copy;



#####
#
# Options
#
#####

# The following constants define what we do with the case of filenames

##
# Never rename a file to lowercase
my $OPT_LOWER_NONE=0;

##
# Rename all files to lowercase
my $OPT_LOWER_ALL=1;

##
# Rename only files that are all uppercase to lowercase
my $OPT_LOWER_UPPERCASE=2;


# The following constants define whether to ask questions or not

##
# No (synonym of never)
my $OPT_ASK_NO=0;

##
# Yes (always)
my $OPT_ASK_YES=1;

##
# Skip the questions till the end of this scope
my $OPT_ASK_SKIP=-1;


# General options

68 69 70 71
##
# This is the directory in which winemaker will operate.
my $opt_work_dir;

72 73 74 75 76 77 78 79
##
# Make a backup of the files
my $opt_backup;

##
# Defines which files to rename
my $opt_lower;

80 81 82 83
##
# If we don't find the file referenced by an include, lower it
my $opt_lower_include;

84 85 86 87 88
##
# If true then winemaker should not attempt to fix the source.  This is
# useful if the source is known to be already in a suitable form and is
# readonly
my $opt_no_source_fix;
89 90 91 92

# Options for the 'Source' method

##
93 94 95 96
# Specifies that we have only one target so that all sources relate
# to this target. By default this variable is left undefined which
# means winemaker should try to find out by itself what the targets
# are. If not undefined then this contains the name of the default
97 98 99 100
# target (without the extension).
my $opt_single_target;

##
101 102
# If '$opt_single_target' has been specified then this is the type of
# that target. Otherwise it specifies whether the default target type
103 104 105 106 107 108 109 110
# is guiexe or cuiexe.
my $opt_target_type;

##
# Contains the default set of flags to be used when creating a new target.
my $opt_flags;

##
111
# If true then winemaker should ask questions to the user as it goes
112 113 114 115 116 117
# along.
my $opt_is_interactive;
my $opt_ask_project_options;
my $opt_ask_target_options;

##
118
# If false then winemaker should not generate the makefiles.
119
my $opt_no_generated_files;
120 121 122 123 124 125 126 127 128 129 130 131 132

##
# Specifies not to print the banner if set.
my $opt_no_banner;



#####
#
# Target modelization
#
#####

133
# The description of a target is stored in an array. The constants
134 135 136 137 138 139 140 141 142 143 144 145
# below identify what is stored at each index of the array.

##
# This is the name of the target.
my $T_NAME=0;

##
# Defines the type of target we want to build. See the TT_xxx
# constants below
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;
149 150

##
151
# This is a reference to an array containing the list of the
152
# resp. C, C++, RC, other (.h, .hxx, etc.) source files.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
my $T_SOURCES_C=3;
my $T_SOURCES_CXX=4;
my $T_SOURCES_RC=5;
my $T_SOURCES_MISC=6;

##
# This is a reference to an array containing the list of
# C compiler options
my $T_CEXTRA=7;

##
# This is a reference to an array containing the list of
# C++ compiler options
my $T_CXXEXTRA=8;

##
# This is a reference to an array containing the list of
# RC compiler options
my $T_RCEXTRA=9;
172 173

##
174
# This is a reference to an array containing the list of macro
175
# definitions
176
my $T_DEFINES=10;
177 178

##
179
# This is a reference to an array containing the list of directory
180
# names that constitute the include path
181 182 183 184 185
my $T_INCLUDE_PATH=11;

##
# Flags for the linker
my $T_LDFLAGS=12;
186 187

##
188
# Same as T_INCLUDE_PATH but for the dll search path
189
my $T_DLL_PATH=13;
190 191 192

##
# The list of Windows dlls to import
193
my $T_DLLS=14;
194 195

##
196
# Same as T_INCLUDE_PATH but for the library search path
197
my $T_LIBRARY_PATH=15;
198 199 200

##
# The list of Unix libraries to link with
201
my $T_LIBRARIES=16;
202 203 204

##
# The list of dependencies between targets
205
my $T_DEPENDS=17;
206 207 208 209 210


# 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
# fake target in the global source list.
my $TT_SETTINGS=0;

##
# For executables in the windows subsystem
my $TT_GUIEXE=1;

##
# For executables in the console subsystem
my $TT_CUIEXE=2;

##
# For dynamically linked libraries
my $TT_DLL=3;


# The following constants further refine how the target should be handled

##
# This target is an MFC-based target
my $TF_MFC=4;

236 237 238 239 240 241 242 243
##
# User has specified --nomfc option for this target or globally
my $TF_NOMFC=8;

##
# --nodlls option: Do not use standard DLL set
my $TF_NODLLS=16;

244 245 246 247
##
# --nomsvcrt option: Do not link with msvcrt
my $TF_NOMSVCRT=32;

248 249
##
# Initialize a target:
250 251
# - set the target type to TT_SETTINGS, i.e. no real target will
#   be generated.
252
sub target_init($)
253 254 255 256 257 258 259 260 261 262
{
  my $target=$_[0];

  @$target[$T_TYPE]=$TT_SETTINGS;
  # leaving $T_INIT undefined
  @$target[$T_FLAGS]=$opt_flags;
  @$target[$T_SOURCES_C]=[];
  @$target[$T_SOURCES_CXX]=[];
  @$target[$T_SOURCES_RC]=[];
  @$target[$T_SOURCES_MISC]=[];
263 264 265
  @$target[$T_CEXTRA]=[];
  @$target[$T_CXXEXTRA]=[];
  @$target[$T_RCEXTRA]=[];
266 267
  @$target[$T_DEFINES]=[];
  @$target[$T_INCLUDE_PATH]=[];
268
  @$target[$T_LDFLAGS]=[];
269 270
  @$target[$T_DLL_PATH]=[];
  @$target[$T_DLLS]=[];
271
  @$target[$T_LIBRARY_PATH]=[];
272
  @$target[$T_LIBRARIES]=[];
273 274 275 276 277 278 279 280 281 282
}



#####
#
# Project modelization
#
#####

283 284
# First we have the notion of project. A project is described by an
# array (since we don't have structs in perl). The constants below
285 286 287
# identify what is stored at each index of the array.

##
288
# This is the path in which this project is located. In other
289 290 291 292
# words, this is the path to  the Makefile.
my $P_PATH=0;

##
293 294
# This index contains a reference to an array containing the project-wide
# settings. The structure of that arrray is actually identical to that of
295 296 297 298
# a regular target since it can also contain extra sources.
my $P_SETTINGS=1;

##
299 300 301
# This index contains a reference to an array of targets for this
# project. Each target describes how an executable or library is to
# be built. For each target this description takes the same form as
302 303 304 305 306 307 308 309
# that of the project: an array. So this entry is an array of arrays.
my $P_TARGETS=2;

##
# Initialize a project:
# - set the project's path
# - initialize the target list
# - create a default target (will be removed later if unnecessary)
310
sub project_init($$)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
  my $project=$_[0];
  my $path=$_[1];

  my $project_settings=[];
  target_init($project_settings);

  @$project[$P_PATH]=$path;
  @$project[$P_SETTINGS]=$project_settings;
  @$project[$P_TARGETS]=[];
}



#####
#
# Global variables
#
#####

my %warnings;

my %templates;

335 336 337 338 339
##
# This maps a directory name to a reference to an array listing
# its contents (files and directories)
my %directories;

340
##
341 342
# Contains the list of all projects. This list tells us what are
# the subprojects of the main Makefile and where we have to generate
343 344 345 346
# Makefiles.
my @projects=();

##
347 348
# This is the main project, i.e. the one in the "." directory.
# It may well be empty in which case the main Makefile will only
349 350 351 352 353
# call out subprojects.
my @main_project;

##
# Contains the defaults for the include path, etc.
354
# We store the defaults as if this were a target except that we only
355 356 357 358 359 360 361 362 363 364 365 366 367
# exploit the defines, include path, library path, library list and misc
# sources fields.
my @global_settings;



#####
#
# Utility functions
#
#####

##
368
# Cleans up a name to make it an acceptable Makefile
369
# variable name.
370
sub canonize($)
371 372 373 374 375 376 377 378 379
{
  my $name=$_[0];

  $name =~ tr/a-zA-Z0-9_/_/c;
  return $name;
}

##
# 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($)
383 384 385 386 387 388 389 390
{
  my $path=$_[0];

  return ($path =~ /^[\/~\$]/);
}

##
# Performs a binary search looking for the specified item
391
sub bsearch($$)
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
{
  my $array=$_[0];
  my $item=$_[1];
  my $last=@{$array}-1;
  my $first=0;

  while ($first<=$last) {
    my $index=int(($first+$last)/2);
    my $cmp=@$array[$index] cmp $item;
    if ($cmp<0) {
      $first=$index+1;
    } elsif ($cmp>0) {
      $last=$index-1;
    } else {
      return $index;
    }
  }
}

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
##
# Retrieves the contents of the specified directory.
# We either get it from the directories hashtable which acts as a
# cache, or use opendir, readdir, closedir and store the result
# in the hashtable.
sub get_directory_contents($)
{
  my $dirname=$_[0];
  my $directory;

  #print "getting the contents of $dirname\n";

  # check for a cached version
  $dirname =~ s+/$++;
  if ($dirname eq "") {
    $dirname=cwd;
  }
  $directory=$directories{$dirname};
  if (defined $directory) {
    #print "->@$directory\n";
    return $directory;
  }

  # Read this directory
  if (opendir(DIRECTORY, "$dirname")) {
    my @files=readdir DIRECTORY;
    closedir(DIRECTORY);
    $directory=\@files;
  } else {
    # Return an empty list
    #print "error: cannot open $dirname\n";
    my @files;
    $directory=\@files;
  }
  #print "->@$directory\n";
  $directories{$dirname}=$directory;
  return $directory;
}

450 451 452 453 454 455 456 457 458 459 460 461


#####
#
# 'Source'-based Project analysis
#
#####

##
# Allows the user to specify makefile and target specific options
# - target: the structure in which to store the results
# - options: the string containing the options
462
sub source_set_options($$)
463 464 465 466 467
{
  my $target=$_[0];
  my $options=$_[1];

  #FIXME: we must deal with escaping of stuff and all
468
  foreach my $option (split / /,$options) {
469 470 471 472
    if (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-D/) {
      push @{@$target[$T_DEFINES]},$option;
    } elsif (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-I/) {
      push @{@$target[$T_INCLUDE_PATH]},$option;
473 474 475
    } elsif ($option =~ /^-P/) {
      push @{@$target[$T_DLL_PATH]},"-L$'";
    } elsif ($option =~ /^-i/) {
476
      push @{@$target[$T_DLLS]},"$'";
477 478
    } elsif ($option =~ /^-L/) {
      push @{@$target[$T_LIBRARY_PATH]},$option;
479
    } elsif ($option =~ /^-l/) {
480
      push @{@$target[$T_LIBRARIES]},"$'";
481 482 483
    } elsif ($option =~ /^--mfc/) {
      @$target[$T_FLAGS]|=$TF_MFC;
      @$target[$T_FLAGS]&=~$TF_NOMFC;
484 485
    } elsif ($option =~ /^--nomfc/) {
      @$target[$T_FLAGS]&=~$TF_MFC;
486 487 488
      @$target[$T_FLAGS]|=$TF_NOMFC;
    } elsif ($option =~ /^--nodlls/) {
      @$target[$T_FLAGS]|=$TF_NODLLS;
489 490
    } elsif ($option =~ /^--nomsvcrt/) {
      @$target[$T_FLAGS]|=$TF_NOMSVCRT;
491
    } else {
492 493
      print STDERR "error: unknown option \"$option\"\n";
      return 0;
494 495
    }
  }
496
  return 1;
497 498 499 500
}

##
# Scans the specified directory to:
501
# - see if we should create a Makefile in this directory. We normally do
502 503 504
#   so if we find a project file and sources
# - get a list of targets for this directory
# - get the list of source files
505 506
sub source_scan_directory($$$$);
sub source_scan_directory($$$$)
507 508 509
{
  # a reference to the parent's project
  my $parent_project=$_[0];
510
  # the full relative path to the current directory, including a
511 512 513 514 515
  # trailing '/', or an empty string if this is the top level directory
  my $path=$_[1];
  # the name of this directory, including a trailing '/', or an empty
  # string if this is the top level directory
  my $dirname=$_[2];
516
  # if set then no targets will be looked for and the sources will all
517 518
  # end up in the parent_project's 'misc' bucket
  my $no_target=$_[3];
519 520 521 522 523 524 525 526 527 528 529 530

  # reference to the project for this directory. May not be used
  my $project;
  # list of targets found in the 'current' directory
  my %targets;
  # list of sources found in the current directory
  my @sources_c=();
  my @sources_cxx=();
  my @sources_rc=();
  my @sources_misc=();
  # true if this directory contains a Windows project
  my $has_win_project=0;
531 532
  # true if this directory contains headers
  my $has_headers=0;
533 534
  # If we don't find any executable/library then we might make up targets
  # from the list of .dsp/.mak files we find since they usually have the
535 536 537 538 539
  # same name as their target.
  my @dsp_files=();
  my @mak_files=();

  if (defined $opt_single_target or $dirname eq "") {
540 541
    # Either there is a single target and thus a single project,
    # or we are in the top level directory for which a project
542 543 544 545 546 547
    # already exists
    $project=$parent_project;
  } else {
    $project=[];
    project_init($project,$path);
  }
548
  my $project_settings=@$project[$P_SETTINGS];
549 550 551 552

  # First find out what this directory contains:
  # collect all sources, targets and subdirectories
  my $directory=get_directory_contents($path);
553
  foreach my $dentry (@$directory) {
554 555 556 557 558 559
    if ($dentry =~ /^\./) {
      next;
    }
    my $fullentry="$path$dentry";
    if (-d "$fullentry") {
      if ($dentry =~ /^(Release|Debug)/i) {
560
	# These directories are often used to store the object files and the
561 562
	# resulting executable/library. They should not contain anything else.
	my @candidates=grep /\.(exe|dll)$/i, @{get_directory_contents("$fullentry")};
563
	foreach my $candidate (@candidates) {
564
	  $targets{$candidate}=1;
565
	}
566 567 568
      } elsif ($dentry =~ /^include/i) {
        # This directory must contain headers we're going to need
        push @{@$project_settings[$T_INCLUDE_PATH]},"-I$dentry";
569
        source_scan_directory($project,"$fullentry/","$dentry/",1);
570
      } else {
571 572
	# Recursively scan this directory. Any source file that cannot be
	# attributed to a project in one of the subdirectories will be
573 574
	# attributed to this project.
	source_scan_directory($project,"$fullentry/","$dentry/",$no_target);
575 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 581
	push @sources_c,"$dentry";
      } 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 586 587
	} else {
	  push @sources_cxx,"$dentry";
	}
588 589
      } elsif ($dentry =~ /\.rc$/i) {
	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;
595
	}
596 597 598 599 600 601 602 603 604 605 606 607 608
      } elsif ($dentry =~ /\.dsp$/i) {
	push @dsp_files,"$dentry";
	$has_win_project=1;
      } elsif ($dentry =~ /\.mak$/i) {
	push @mak_files,"$dentry";
	$has_win_project=1;
      } elsif ($dentry =~ /^makefile/i) {
	$has_win_project=1;
      }
    }
  }
  closedir(DIRECTORY);

609 610 611
  if ($has_headers) {
    push @{@$project_settings[$T_INCLUDE_PATH]},"-I.";
  }
612
  # If we have a single target then all we have to do is assign
613 614 615 616 617 618 619 620 621 622
  # all the sources to it and we're done
  # FIXME: does this play well with the --interactive mode?
  if ($opt_single_target) {
    my $target=@{@$project[$P_TARGETS]}[0];
    push @{@$target[$T_SOURCES_C]},map "$path$_",@sources_c;
    push @{@$target[$T_SOURCES_CXX]},map "$path$_",@sources_cxx;
    push @{@$target[$T_SOURCES_RC]},map "$path$_",@sources_rc;
    push @{@$target[$T_SOURCES_MISC]},map "$path$_",@sources_misc;
    return;
  }
623 624 625 626 627 628
  if ($no_target) {
    my $parent_settings=@$parent_project[$P_SETTINGS];
    push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_c;
    push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_cxx;
    push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_rc;
    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 631
    return;
  }
632 633 634 635 636 637 638 639

  my $source_count=@sources_c+@sources_cxx+@sources_rc+
                   @{@$project_settings[$T_SOURCES_C]}+
                   @{@$project_settings[$T_SOURCES_CXX]}+
                   @{@$project_settings[$T_SOURCES_RC]};
  if ($source_count == 0) {
    # A project without real sources is not a project, get out!
    if ($project!=$parent_project) {
640
      my $parent_settings=@$parent_project[$P_SETTINGS];
641 642 643 644 645 646 647 648 649 650 651 652
      push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
      push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
    }
    return;
  }
  #print "targets=",%targets,"\n";
  #print "target_count=$target_count\n";
  #print "has_win_project=$has_win_project\n";
  #print "dirname=$dirname\n";

  my $target_count;
  if (($has_win_project != 0) or ($dirname eq "")) {
653
    # Deal with cases where we could not find any executable/library, and
654 655 656 657 658 659 660 661 662 663
    # thus have no target, although we did find some sort of windows project.
    $target_count=keys %targets;
    if ($target_count == 0) {
      # Try to come up with a target list based on .dsp/.mak files
      my $prj_list;
      if (@dsp_files > 0) {
	$prj_list=\@dsp_files;
      } else {
	$prj_list=\@mak_files;
      }
664
      foreach my $filename (@$prj_list) {
665 666
	$filename =~ s/\.(dsp|mak)$//i;
	if ($opt_target_type == $TT_DLL) {
667
	  $filename = "$filename.dll";
668 669 670 671 672 673 674 675 676 677 678 679
	}
	$targets{$filename}=1;
      }
      $target_count=keys %targets;
      if ($target_count == 0) {
	# Still nothing, try the name of the directory
	my $name;
	if ($dirname eq "") {
	  # Bad luck, this is the top level directory!
	  $name=(split /\//, cwd)[-1];
	} else {
	  $name=$dirname;
680
	  # Remove the trailing '/'. Also eliminate whatever is after the last
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	  # '.' as it is likely to be meaningless (.orig, .new, ...)
	  $name =~ s+(/|\.[^.]*)$++;
	  if ($name eq "src") {
	    # 'src' is probably a subdirectory of the real project directory.
	    # Try again with the parent (if any).
	    my $parent=$path;
	    if ($parent =~ s+([^/]*)/[^/]*/$+$1+) {
	      $name=$parent;
	    } else {
	      $name=(split /\//, cwd)[-1];
	    }
	  }
	}
	$name =~ s+(/|\.[^.]*)$++;
	if ($opt_target_type == $TT_DLL) {
696
	  $name = "$name.dll";
697 698
	} else {
	  $name = "$name.exe";
699 700 701 702 703 704 705 706
	}
	$targets{$name}=1;
      }
    }

    # Ask confirmation to the user if he wishes so
    if ($opt_is_interactive == $OPT_ASK_YES) {
      my $target_list=join " ",keys %targets;
707
      print "\n*** In ",($path?$path:"./"),"\n";
708 709 710 711 712 713
      print "* winemaker found the following list of (potential) targets\n";
      print "*   $target_list\n";
      print "* Type enter to use it as is, your own comma-separated list of\n";
      print "* targets, 'none' to assign the source files to a parent directory,\n";
      print "* or 'ignore' to ignore everything in this directory tree.\n";
      print "* Target list:\n";
714 715 716 717 718 719 720 721 722 723 724 725
      $target_list=<STDIN>;
      chomp $target_list;
      if ($target_list eq "") {
	# Keep the target list as is, i.e. do nothing
      } elsif ($target_list eq "none") {
	# Empty the target list
	undef %targets;
      } elsif ($target_list eq "ignore") {
	# Ignore this subtree altogether
	return;
      } else {
	undef %targets;
726
	foreach my $target (split /,/,$target_list) {
727 728 729 730 731 732 733 734
	  $target =~ s+^\s*++;
	  $target =~ s+\s*$++;
	  $targets{$target}=1;
	}
      }
    }
  }

735
  # If we have no project at this level, then transfer all
736 737 738 739 740 741 742 743 744 745 746 747 748 749
  # the sources to the parent project
  $target_count=keys %targets;
  if ($target_count == 0) {
    if ($project!=$parent_project) {
      my $parent_settings=@$parent_project[$P_SETTINGS];
      push @{@$parent_settings[$T_SOURCES_C]},map "$dirname$_",@sources_c;
      push @{@$parent_settings[$T_SOURCES_CXX]},map "$dirname$_",@sources_cxx;
      push @{@$parent_settings[$T_SOURCES_RC]},map "$dirname$_",@sources_rc;
      push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
      push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
    }
    return;
  }

750
  # Otherwise add this project to the project list, except for
751 752 753 754 755 756 757
  # the main project which is already in the list.
  if ($dirname ne "") {
    push @projects,$project;
  }

  # Ask for project-wide options
  if ($opt_ask_project_options == $OPT_ASK_YES) {
758 759 760 761
    my $flag_desc="";
    if ((@$project_settings[$T_FLAGS] & $TF_MFC)!=0) {
      $flag_desc="mfc";
    }
762
    print "* Type any project-wide options (-D/-I/-P/-i/-L/-l/--mfc),\n";
763 764 765 766 767
    if (defined $flag_desc) {
      print "* (currently $flag_desc)\n";
    }
    print "* or 'skip' to skip the target specific options,\n";
    print "* or 'never' to not be asked this question again:\n";
768 769 770 771 772 773 774 775 776 777 778 779 780
    while (1) {
      my $options=<STDIN>;
      chomp $options;
      if ($options eq "skip") {
        $opt_ask_target_options=$OPT_ASK_SKIP;
        last;
      } elsif ($options eq "never") {
        $opt_ask_project_options=$OPT_ASK_NO;
        last;
      } elsif (source_set_options($project_settings,$options)) {
        last;
      }
      print "Please re-enter the options:\n";
781 782 783 784 785
    }
  }

  # - Create the targets
  # - 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 793 794 795
    # Create the target...
    my $target=[];
    target_init($target);
    @$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 800
      push @local_depends,"$target_name.so";
      push @local_dlls,$target_name;
801 802
    } else {
      @$target[$T_TYPE]=$opt_target_type;
803
      push @exe_list,$target;
804
      push @{@$target[$T_LDFLAGS]},(@$target[$T_TYPE] == $TT_CUIEXE ? "-mconsole" : "-mwindows");
805
    }
806 807
    my $basename=$target_name;
    $basename=~ s/\.(dll|exe)$//i;
808
    # This is the default link list of Visual Studio, except odbccp32
809
    # which we don't have in Wine.
810
    my @std_imports=qw(odbc32 ole32 oleaut32 winspool);
811
    my @std_libraries=qw(uuid);
812
    if ((@$target[$T_FLAGS] & $TF_NODLLS) == 0) {
813
      @$target[$T_DLLS]=\@std_imports;
814
      @$target[$T_LIBRARIES]=\@std_libraries;
815 816
    } else {
      @$target[$T_DLLS]=[];
817
      @$target[$T_LIBRARIES]=[];
818
    }
819 820 821
    if ((@$target[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
      push @{@$target[$T_LDFLAGS]},"-mno-cygwin";
    }
822 823 824 825
    push @{@$project[$P_TARGETS]},$target;

    # Ask for target-specific options
    if ($opt_ask_target_options == $OPT_ASK_YES) {
826 827 828 829 830 831 832
      my $flag_desc="";
      if ((@$target[$T_FLAGS] & $TF_MFC)!=0) {
	$flag_desc=" (mfc";
      }
      if ($flag_desc ne "") {
	$flag_desc.=")";
      }
833
      print "* Specify any link option (-P/-i/-L/-l/--mfc) specific to the target\n";
834
      print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
835 836 837 838 839 840 841 842 843 844
      while (1) {
        my $options=<STDIN>;
        chomp $options;
        if ($options eq "never") {
          $opt_ask_target_options=$OPT_ASK_NO;
          last;
        } elsif (source_set_options($target,$options)) {
          last;
        }
        print "Please re-enter the options:\n";
845 846 847 848
      }
    }
    if (@$target[$T_FLAGS] & $TF_MFC) {
      @$project_settings[$T_FLAGS]|=$TF_MFC;
849 850
      push @{@$target[$T_DLL_PATH]},"\$(MFC_LIBRARY_PATH)";
      push @{@$target[$T_DLLS]},"mfc.dll";
851
      # FIXME: Link with the MFC in the Unix sense, until we
852
      # start exporting the functions properly.
853
      push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
854
      push @{@$target[$T_LIBRARIES]},"mfc";
855 856 857 858
    }

    # Match sources...
    if ($target_count == 1) {
859 860
      push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
      @$project_settings[$T_SOURCES_C]=[];
861
      @sources_c=();
862 863 864

      push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
      @$project_settings[$T_SOURCES_CXX]=[];
865
      @sources_cxx=();
866 867 868

      push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
      @$project_settings[$T_SOURCES_RC]=[];
869
      @sources_rc=();
870 871 872 873

      push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
      # No need for sorting these sources
      @$project_settings[$T_SOURCES_MISC]=[];
874 875
      @sources_misc=();
    } else {
876
      foreach my $source (@sources_c) {
877 878 879 880 881
	if ($source =~ /^$basename/i) {
	  push @{@$target[$T_SOURCES_C]},$source;
	  $source="";
	}
      }
882
      foreach my $source (@sources_cxx) {
883 884 885 886 887
	if ($source =~ /^$basename/i) {
	  push @{@$target[$T_SOURCES_CXX]},$source;
	  $source="";
	}
      }
888
      foreach my $source (@sources_rc) {
889 890 891 892 893
	if ($source =~ /^$basename/i) {
	  push @{@$target[$T_SOURCES_RC]},$source;
	  $source="";
	}
      }
894
      foreach my $source (@sources_misc) {
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	if ($source =~ /^$basename/i) {
	  push @{@$target[$T_SOURCES_MISC]},$source;
	  $source="";
	}
      }
    }
    @$target[$T_SOURCES_C]=[sort @{@$target[$T_SOURCES_C]}];
    @$target[$T_SOURCES_CXX]=[sort @{@$target[$T_SOURCES_CXX]}];
    @$target[$T_SOURCES_RC]=[sort @{@$target[$T_SOURCES_RC]}];
    @$target[$T_SOURCES_MISC]=[sort @{@$target[$T_SOURCES_MISC]}];
  }
  if ($opt_ask_target_options == $OPT_ASK_SKIP) {
    $opt_ask_target_options=$OPT_ASK_YES;
  }

910
  if ((@$project_settings[$T_FLAGS] & $TF_NOMSVCRT) == 0) {
911 912
    push @{@$project_settings[$T_CEXTRA]},"-mno-cygwin";
    push @{@$project_settings[$T_CXXEXTRA]},"-mno-cygwin";
913 914
  }

915 916 917
  if (@$project_settings[$T_FLAGS] & $TF_MFC) {
    push @{@$project_settings[$T_INCLUDE_PATH]},"\$(MFC_INCLUDE_PATH)";
  }
918
  # The sources that did not match, if any, go to the extra
919
  # source list of the project settings
920
  foreach my $source (@sources_c) {
921 922 923 924 925
    if ($source ne "") {
      push @{@$project_settings[$T_SOURCES_C]},$source;
    }
  }
  @$project_settings[$T_SOURCES_C]=[sort @{@$project_settings[$T_SOURCES_C]}];
926
  foreach my $source (@sources_cxx) {
927 928 929 930 931
    if ($source ne "") {
      push @{@$project_settings[$T_SOURCES_CXX]},$source;
    }
  }
  @$project_settings[$T_SOURCES_CXX]=[sort @{@$project_settings[$T_SOURCES_CXX]}];
932
  foreach my $source (@sources_rc) {
933 934 935 936 937
    if ($source ne "") {
      push @{@$project_settings[$T_SOURCES_RC]},$source;
    }
  }
  @$project_settings[$T_SOURCES_RC]=[sort @{@$project_settings[$T_SOURCES_RC]}];
938
  foreach my $source (@sources_misc) {
939 940 941 942 943 944
    if ($source ne "") {
      push @{@$project_settings[$T_SOURCES_MISC]},$source;
    }
  }
  @$project_settings[$T_SOURCES_MISC]=[sort @{@$project_settings[$T_SOURCES_MISC]}];

945 946
  # Finally if we are building both libraries and programs in
  # this directory, then the programs should be linked with all
947
  # the libraries
948
  if (@local_dlls > 0 and @exe_list > 0) {
949
    foreach my $target (@exe_list) {
950
      push @{@$target[$T_DLL_PATH]},"-L.";
951
      push @{@$target[$T_DLLS]},@local_dlls;
952 953 954 955 956 957
    }
  }
}

##
# Scan the source directories in search of things to build
958
sub source_scan()
959 960 961
{
  # If there's a single target then this is going to be the default target
  if (defined $opt_single_target) {
962 963 964
    # Create the main target
    my $main_target=[];
    target_init($main_target);
965
    @$main_target[$T_NAME]=$opt_single_target;
966
    @$main_target[$T_TYPE]=$opt_target_type;
967 968 969

    # Add it to the list
    push @{$main_project[$P_TARGETS]},$main_target;
970 971 972 973 974 975 976
  }

  # The main directory is always going to be there
  push @projects,\@main_project;

  # Now scan the directory tree looking for source files and, maybe, targets
  print "Scanning the source directories...\n";
977
  source_scan_directory(\@main_project,"","",0);
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992

  @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
}

#####
#
# Source search
#
#####

##
# Performs a directory traversal and renames the files so that:
# - they have the case desired by the user
# - their extension is of the appropriate case
# - they don't contain annoying characters like ' ', '$', '#', ...
993 994
sub fix_file_and_directory_names($);
sub fix_file_and_directory_names($)
995 996 997 998
{
  my $dirname=$_[0];

  if (opendir(DIRECTORY, "$dirname")) {
999
    foreach my $dentry (readdir DIRECTORY) {
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
      if ($dentry =~ /^\./ or $dentry eq "CVS") {
	next;
      }
      # Set $warn to 1 if the user should be warned of the renaming
      my $warn=0;

      # autoconf and make don't support these characters well
      my $new_name=$dentry;
      $new_name =~ s/[ \$]/_/g;

1010
      # Only all lowercase extensions are supported (because of the
1011
      # transformations ':.c=.o') .
1012
      if (-f "$dirname/$new_name") {
1013 1014 1015 1016
	if ($new_name =~ /\.C$/) {
	  $new_name =~ s/\.C$/.c/;
	}
	if ($new_name =~ /\.cpp$/i) {
1017 1018 1019 1020 1021
	  $new_name =~ s/\.cpp$/.cpp/i;
	}
	if ($new_name =~ s/\.cxx$/.cpp/i) {
	  $warn=1;
	}
1022
	if ($new_name =~ /\.rc$/i) {
1023 1024 1025 1026 1027 1028 1029 1030 1031
	  $new_name =~ s/\.rc$/.rc/i;
	}
	# And this last one is to avoid confusion then running make
	if ($new_name =~ s/^makefile$/makefile.win/) {
	  $warn=1;
	}
      }

      # Adjust the case to the user's preferences
1032
      if (($opt_lower == $OPT_LOWER_ALL and $dentry =~ /[A-Z]/) or
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
          ($opt_lower == $OPT_LOWER_UPPERCASE and $dentry !~ /[a-z]/)
         ) {
	$new_name=lc $new_name;
      }

      # And finally, perform the renaming
      if ($new_name ne $dentry) {
	if ($warn) {
	  print STDERR "warning: in \"$dirname\", renaming \"$dentry\" to \"$new_name\"\n";
	}
	if (!rename("$dirname/$dentry","$dirname/$new_name")) {
	  print STDERR "error: in \"$dirname\", unable to rename \"$dentry\" to \"$new_name\"\n";
	  print STDERR "       $!\n";
	  $new_name=$dentry;
	}
      }
      if (-d "$dirname/$new_name") {
	fix_file_and_directory_names("$dirname/$new_name");
      }
    }
    closedir(DIRECTORY);
  }
}



#####
#
# Source fixup
#
#####

##
1066 1067
# Try to find a file for the specified filename. The attempt is
# case-insensitive which is why it's not trivial. If a match is
1068
# found then we return the pathname with the correct case.
1069
sub search_from($$)
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
{
  my $dirname=$_[0];
  my $path=$_[1];
  my $real_path="";

  if ($dirname eq "" or $dirname eq ".") {
    $dirname=cwd;
  } elsif ($dirname =~ m+^[^/]+) {
    $dirname=cwd . "/" . $dirname;
  }
  if ($dirname !~ m+/$+) {
    $dirname.="/";
  }

1084
  foreach my $component (@$path) {
1085
    #print "    looking for $component in \"$dirname\"\n";
1086 1087 1088 1089 1090 1091 1092 1093
    if ($component eq ".") {
      # Pass it as is
      $real_path.="./";
    } elsif ($component eq "..") {
      # Go up one level
      $dirname=dirname($dirname) . "/";
      $real_path.="../";
    } else {
1094
      # The file/directory may have been renamed before. Also try to
1095 1096 1097 1098 1099 1100 1101
      # match the renamed file.
      my $renamed=$component;
      $renamed =~ s/[ \$]/_/g;
      if ($renamed eq $component) {
        undef $renamed;
      }

1102 1103
      my $directory=get_directory_contents $dirname;
      my $found;
1104
      foreach my $dentry (@$directory) {
1105 1106 1107
	if ($dentry =~ /^$component$/i or
            (defined $renamed and $dentry =~ /^$renamed$/i)
           ) {
1108 1109 1110 1111 1112 1113 1114 1115
	  $dirname.="$dentry/";
	  $real_path.="$dentry/";
	  $found=1;
	  last;
	}
      }
      if (!defined $found) {
	# Give up
1116
	#print "    could not find $component in $dirname\n";
1117 1118 1119 1120 1121
	return;
      }
    }
  }
  $real_path=~ s+/$++;
1122
  #print "    -> found $real_path\n";
1123 1124 1125 1126
  return $real_path;
}

##
1127
# Performs a case-insensitive search for the specified file in the
1128 1129 1130 1131 1132 1133
# include path.
# $line is the line number that should be referenced when an error occurs
# $filename is the file we are looking for
# $dirname is the directory of the file containing the '#include' directive
#    if '"' was used, it is an empty string otherwise
# $project and $target specify part of the include path
1134
sub get_real_include_name($$$$$)
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
{
  my $line=$_[0];
  my $filename=$_[1];
  my $dirname=$_[2];
  my $project=$_[3];
  my $target=$_[4];

  if ($filename =~ /^([a-zA-Z]:)?[\/]/ or $filename =~ /^[a-zA-Z]:[\/]?/) {
    # This is not a relative path, we cannot make any check
    my $warning="path:$filename";
    if (!defined $warnings{$warning}) {
      $warnings{$warning}="1";
      print STDERR "warning: cannot check the case of absolute pathnames:\n";
      print STDERR "$line:   $filename\n";
    }
  } else {
    # Here's how we proceed:
    # - split the filename we look for into its components
    # - then for each directory in the include path
    #   - trace the directory components starting from that directory
1155
    #   - if we fail to find a match at any point then continue with
1156 1157 1158
    #     the next directory in the include path
    #   - otherwise, rejoice, our quest is over.
    my @file_components=split /[\/\\]+/, $filename;
1159
    #print "  Searching for $filename from @$project[$P_PATH]\n";
1160 1161 1162

    my $real_filename;
    if ($dirname ne "") {
1163 1164
      # This is an 'include ""' -> look in dirname first.
      #print "    in $dirname (include \"\")\n";
1165 1166 1167 1168 1169 1170
      $real_filename=search_from($dirname,\@file_components);
      if (defined $real_filename) {
	return $real_filename;
      }
    }
    my $project_settings=@$project[$P_SETTINGS];
1171
    foreach my $include (@{@$target[$T_INCLUDE_PATH]}, @{@$project_settings[$T_INCLUDE_PATH]}) {
1172 1173 1174 1175 1176
      my $dirname=$include;
      $dirname=~ s+^-I++;
      if (!is_absolute($dirname)) {
	$dirname="@$project[$P_PATH]$dirname";
      } else {
1177 1178
        $dirname=~ s+^\$\(TOPSRCDIR\)/++;
        $dirname=~ s+^\$\(SRCDIR\)/+@$project[$P_PATH]+;
1179 1180
      }
      #print "    in $dirname\n";
1181 1182 1183 1184 1185 1186 1187
      $real_filename=search_from("$dirname",\@file_components);
      if (defined $real_filename) {
	return $real_filename;
      }
    }
    my $dotdotpath=@$project[$P_PATH];
    $dotdotpath =~ s/[^\/]+/../g;
1188
    foreach my $include (@{$global_settings[$T_INCLUDE_PATH]}) {
1189 1190 1191
      my $dirname=$include;
      $dirname=~ s+^-I++;
      $dirname=~ s+^\$\(TOPSRCDIR\)\/++;
1192
      $dirname=~ s+^\$\(SRCDIR\)\/+@$project[$P_PATH]+;
1193
      #print "    in $dirname  (global setting)\n";
1194 1195 1196 1197 1198 1199 1200 1201
      $real_filename=search_from("$dirname",\@file_components);
      if (defined $real_filename) {
	return $real_filename;
      }
    }
  }
  $filename =~ s+\\\\+/+g; # in include ""
  $filename =~ s+\\+/+g; # in include <> !
1202
  if ($opt_lower_include) {
1203 1204 1205 1206 1207
    return lc "$filename";
  }
  return $filename;
}

1208
sub print_pack($$$)
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
{
  my $indent=$_[0];
  my $size=$_[1];
  my $trailer=$_[2];

  if ($size =~ /^(1|2|4|8)$/) {
    print FILEO "$indent#include <pshpack$size.h>$trailer";
  } else {
    print FILEO "$indent/* winemaker:warning: Unknown size \"$size\". Defaulting to 4 */\n";
    print FILEO "$indent#include <pshpack4.h>$trailer";
  }
}

1222
##
1223 1224 1225 1226 1227
# 'Parses' a source file and fixes constructs that would not work with
# Winelib. The parsing is rather simple and not all non-portable features
# are corrected. The most important feature that is corrected is the case
# and path separator of '#include' directives. This requires that each
# source file be associated to a project & target so that the proper
1228
# include path is used.
1229
# Also note that the include path is relative to the directory in which the
1230
# compiler is run, i.e. that of the project, not to that of the file.
1231
sub fix_file($$$)
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
{
  my $filename=$_[0];
  my $project=$_[1];
  my $target=$_[2];
  $filename="@$project[$P_PATH]$filename";
  if (! -e $filename) {
    return;
  }

  my $is_rc=($filename =~ /\.(rc2?|dlg)$/i);
  my $dirname=dirname($filename);
  my $is_mfc=0;
  if (defined $target and (@$target[$T_FLAGS] & $TF_MFC)) {
    $is_mfc=1;
  }

  print "  $filename\n";
1249
  #FIXME:assuming that because there is a .bak file, this is what we want is
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  #probably flawed. Or is it???
  if (! -e "$filename.bak") {
    if (!copy("$filename","$filename.bak")) {
      print STDERR "error: unable to make a backup of $filename:\n";
      print STDERR "       $!\n";
      return;
    }
  }
  if (!open(FILEI,"$filename.bak")) {
    print STDERR "error: unable to open $filename.bak for reading:\n";
    print STDERR "       $!\n";
    return;
  }
  if (!open(FILEO,">$filename")) {
    print STDERR "error: unable to open $filename for writing:\n";
    print STDERR "       $!\n";
    return;
  }
  my $line=0;
  my $modified=0;
  my $rc_block_depth=0;
  my $rc_textinclude_state=0;
1272
  my @pack_stack;
1273
  while (<FILEI>) {
1274 1275 1276 1277 1278
    # Remove any trailing CtrlZ, which isn't strictly in the file
    if (/\x1A/) {
      s/\x1A//;
      last if (/^$/)
    }
1279
    $line++;
1280 1281 1282 1283 1284
    s/\r\n$/\n/;
    if (!/\n$/) {
      # Make sure all files are '\n' terminated
      $_ .= "\n";
    }
1285
    if ($is_rc and !$is_mfc and /^(\s*)(\#\s*include\s*)\"afxres\.h\"/) {
1286 1287 1288
      # VC6 automatically includes 'afxres.h', an MFC specific header, in
      # the RC files it generates (even in non-MFC projects). So we replace
      # it with 'winres.h' its very close standard cousin so that non MFC
1289
      # projects can compile in Wine without the MFC sources.
1290 1291 1292 1293 1294 1295
      my $warning="mfc:afxres.h";
      if (!defined $warnings{$warning}) {
	$warnings{$warning}="1";
	print STDERR "warning: In non-MFC projects, winemaker replaces the MFC specific header 'afxres.h' with 'winres.h'\n";
	print STDERR "warning: the above warning is issued only once\n";
      }
1296 1297 1298
      print FILEO "$1/* winemaker: $2\"afxres.h\" */\n";
      print FILEO "$1/* winemaker:warning: 'afxres.h' is an MFC specific header. Replacing it with 'winres.h' */\n";
      print FILEO "$1$2\"winres.h\"$'";
1299
      $modified=1;
1300

1301 1302 1303 1304 1305
    } elsif (/^(\s*\#\s*include\s*)([\"<])([^\"]+)([\">])/) {
      my $from_file=($2 eq "<"?"":$dirname);
      my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
      print FILEO "$1$2$real_include_name$4$'";
      $modified|=($real_include_name ne $3);
1306 1307 1308 1309

    } elsif (s/^(\s*)(\#\s*pragma\s+pack\s*\(\s*)//) {
      # Pragma pack handling
      #
1310 1311
      # pack_stack is an array of references describing the stack of
      # pack directives currently in effect. Each directive if described
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
      # by a reference to an array containing:
      # - "push" for pack(push,...) directives, "" otherwise
      # - the directive's identifier at index 1
      # - the directive's alignement value at index 2
      #
      # Don't believe a word of what the documentation says: it's all wrong.
      # The code below is based on the actual behavior of Visual C/C++ 6.
      my $pack_indent=$1;
      my $pack_header=$2;
      if (/^(\))/) {
        # pragma pack()
        # Pushes the default stack alignment
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
        print_pack($pack_indent,4,$');
        push @pack_stack, [ "", "", 4 ];

      } elsif (/^(pop\s*(,\s*\d+\s*)?\))/) {
        # pragma pack(pop)
        # pragma pack(pop,n)
        # Goes up the stack until it finds a pack(push,...), and pops it
        # Ignores any pack(n) entry
        # Issues a warning if the pack is of the form pack(push,label)
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        my $pack_comment=$';
1337
        $pack_comment =~ s/^\s*//;
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
        if ($pack_comment ne "") {
          print FILEO "$pack_indent$pack_comment";
        }
        while (1) {
          my $alignment=pop @pack_stack;
          if (!defined $alignment) {
            print FILEO "$pack_indent/* winemaker:warning: No pack(push,...) found. All the stack has been popped */\n";
            last;
          }
          if (@$alignment[1]) {
            print FILEO "$pack_indent/* winemaker:warning: Anonymous pop of pack(push,@$alignment[1]) (@$alignment[2]) */\n";
          }
          print FILEO "$pack_indent#include <poppack.h>\n";
          if (@$alignment[0]) {
            last;
          }
        }

      } elsif (/^(pop\s*,\s*(\w+)\s*(,\s*\d+\s*)?\))/) {
        # pragma pack(pop,label[,n])
        # Goes up the stack until finding a pack(push,...) and pops it.
        # 'n', if specified, is ignored.
        # Ignores any pack(n) entry
        # Issues a warning if the label of the pack does not match,
        # or if it is in fact a pack(push,n)
        my $label=$2;
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        my $pack_comment=$';
1366
        $pack_comment =~ s/^\s*//;
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        if ($pack_comment ne "") {
          print FILEO "$pack_indent$pack_comment";
        }
        while (1) {
          my $alignment=pop @pack_stack;
          if (!defined $alignment) {
            print FILEO "$pack_indent/* winemaker:warning: No pack(push,$label) found. All the stack has been popped */\n";
            last;
          }
          if (@$alignment[1] and @$alignment[1] ne $label) {
            print FILEO "$pack_indent/* winemaker:warning: Push/pop mismatch: \"@$alignment[1]\" (@$alignment[2]) != \"$label\" */\n";
          }
          print FILEO "$pack_indent#include <poppack.h>\n";
          if (@$alignment[0]) {
            last;
          }
        }

      } elsif (/^(push\s*\))/) {
        # pragma pack(push)
        # Push the current alignment
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        if (@pack_stack > 0) {
          my $alignment=$pack_stack[$#pack_stack];
          print_pack($pack_indent,@$alignment[2],$');
          push @pack_stack, [ "push", "", @$alignment[2] ];
        } else {
          print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
          print_pack($pack_indent,4,$');
          push @pack_stack, [ "push", "", 4 ];
        }

      } elsif (/^((push\s*,\s*)?(\d+)\s*\))/) {
        # pragma pack([push,]n)
        # Push new alignment n
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        print_pack($pack_indent,$3,"$'");
        push @pack_stack, [ ($2 ? "push" : ""), "", $3 ];

      } elsif (/^((\w+)\s*\))/) {
        # pragma pack(label)
        # label must in fact be a macro that resolves to an integer
        # Then behaves like 'pragma pack(n)'
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        print FILEO "$pack_indent/* winemaker:warning: Assuming $2 == 4 */\n";
        print_pack($pack_indent,4,$');
        push @pack_stack, [ "", "", 4 ];

      } elsif (/^(push\s*,\s*(\w+)\s*(,\s*(\d+)\s*)?\))/) {
        # pragma pack(push,label[,n])
        # Pushes a new label on the stack. It is possible to push the same
1418
        # label multiple times. If 'n' is omitted then the alignment is
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
        # unchanged. Otherwise it becomes 'n'.
        print FILEO "$pack_indent/* winemaker: $pack_header$1 */\n";
        my $size;
        if (defined $4) {
          $size=$4;
        } elsif (@pack_stack > 0) {
          my $alignment=$pack_stack[$#pack_stack];
          $size=@$alignment[2];
        } else {
          print FILEO "$pack_indent/* winemaker:warning: Using 4 as the default alignment */\n";
          $size=4;
        }
        print_pack($pack_indent,$size,$');
        push @pack_stack, [ "push", $2, $size ];

1434
      } else {
1435 1436 1437 1438
        # pragma pack(???               -> What's that?
        print FILEO "$pack_indent/* winemaker:warning: Unknown type of pragma pack directive */\n";
        print FILEO "$pack_indent$pack_header$_";

1439
      }
1440 1441
      $modified=1;

1442
    } elsif ($is_rc) {
1443
      if ($rc_block_depth == 0 and /^(\w+\s+(BITMAP|CURSOR|FONT|FONTDIR|ICON|MESSAGETABLE|TEXT|RTF)\s+((DISCARDABLE|FIXED|IMPURE|LOADONCALL|MOVEABLE|PRELOAD|PURE)\s+)*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
1444 1445 1446 1447
	my $from_file=($5 eq "<"?"":$dirname);
	my $real_include_name=get_real_include_name($line,$6,$from_file,$project,$target);
	print FILEO "$1$5$real_include_name$7$'";
	$modified|=($real_include_name ne $6);
1448

1449 1450 1451 1452 1453
      } elsif (/^(\s*RCINCLUDE\s*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
	my $from_file=($2 eq "<"?"":$dirname);
	my $real_include_name=get_real_include_name($line,$3,$from_file,$project,$target);
	print FILEO "$1$2$real_include_name$4$'";
	$modified|=($real_include_name ne $3);
1454

1455 1456 1457
      } elsif ($is_rc and !$is_mfc and $rc_block_depth == 0 and /^\s*\d+\s+TEXTINCLUDE\s*/) {
	$rc_textinclude_state=1;
	print FILEO;
1458

1459 1460 1461
      } elsif ($rc_textinclude_state == 3 and /^(\s*\"\#\s*include\s*\"\")afxres\.h(\"\"\\r\\n\")/) {
	print FILEO "$1winres.h$2$'";
	$modified=1;
1462

1463 1464 1465 1466
      } elsif (/^\s*BEGIN(\W.*)?$/) {
	$rc_textinclude_state|=2;
	$rc_block_depth++;
	print FILEO;
1467

1468 1469 1470 1471 1472 1473
      } elsif (/^\s*END(\W.*)?$/) {
	$rc_textinclude_state=0;
	if ($rc_block_depth>0) {
	  $rc_block_depth--;
	}
	print FILEO;
1474

1475 1476 1477
      } else {
	print FILEO;
      }
1478

1479 1480 1481 1482
    } else {
      print FILEO;
    }
  }
1483

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
  close(FILEI);
  close(FILEO);
  if ($opt_backup == 0 or $modified == 0) {
    if (!unlink("$filename.bak")) {
      print STDERR "error: unable to delete $filename.bak:\n";
      print STDERR "       $!\n";
    }
  }
}

##
1495
# Analyzes each source file in turn to find and correct issues
1496
# that would cause it not to compile.
1497
sub fix_source()
1498 1499
{
  print "Fixing the source files...\n";
1500 1501 1502
  foreach my $project (@projects) {
    foreach my $target (@$project[$P_SETTINGS],@{@$project[$P_TARGETS]}) {
      foreach my $source (@{@$target[$T_SOURCES_C]}, @{@$target[$T_SOURCES_CXX]}, @{@$target[$T_SOURCES_RC]}, @{@$target[$T_SOURCES_MISC]}) {
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	fix_file($source,$project,$target);
      }
    }
  }
}



#####
#
# File generation
#
#####

##
1518
# A convenience function to generate all the lists (defines,
1519
# C sources, C++ source, etc.) in the Makefile
1520
sub generate_list($$$;$)
1521 1522 1523 1524 1525
{
  my $name=$_[0];
  my $last=$_[1];
  my $list=$_[2];
  my $data=$_[3];
1526
  my $first=$name;
1527 1528

  if ($name) {
1529
    printf FILEO "%-22s=",$name;
1530
  }
1531
  if (defined $list) {
1532
    foreach my $item (@$list) {
1533 1534 1535 1536 1537 1538 1539
      my $value;
      if (defined $data) {
	$value=&$data($item);
      } else {
	$value=$item;
      }
      if ($value ne "") {
1540 1541 1542 1543 1544 1545
	if ($first) {
	  print FILEO " $value";
	  $first=0;
	} else {
	  print FILEO " \\\n\t\t\t$value";
	}
1546 1547 1548 1549
      }
    }
  }
  if ($last) {
1550
    print FILEO "\n";
1551 1552 1553 1554 1555
  }
}

##
# Generates a project's Makefile.in and all the target files
1556
sub generate_project_files($)
1557 1558 1559
{
  my $project=$_[0];
  my $project_settings=@$project[$P_SETTINGS];
1560 1561
  my @dll_list=();
  my @exe_list=();
1562 1563

  # Then sort the targets and separate the libraries from the programs
1564
  foreach my $target (sort { @$a[$T_NAME] cmp @$b[$T_NAME] } @{@$project[$P_TARGETS]}) {
1565
    if (@$target[$T_TYPE] == $TT_DLL) {
1566
      push @dll_list,$target;
1567
    } else {
1568
      push @exe_list,$target;
1569 1570 1571
    }
  }
  @$project[$P_TARGETS]=[];
1572 1573
  push @{@$project[$P_TARGETS]}, @dll_list;
  push @{@$project[$P_TARGETS]}, @exe_list;
1574

1575 1576
  if (!open(FILEO,">@$project[$P_PATH]Makefile")) {
    print STDERR "error: could not open \"@$project[$P_PATH]/Makefile\" for writing\n";
1577 1578 1579 1580
    print STDERR "       $!\n";
    return;
  }

1581 1582 1583
  print FILEO "### Generated by Winemaker\n";
  print FILEO "\n\n";

1584
  generate_list("SRCDIR",1,[ "." ]);
1585
  if (@$project[$P_PATH] eq "") {
1586
    # This is the main project. It is also responsible for recursively
1587
    # calling the other projects
1588
    generate_list("SUBDIRS",1,\@projects,sub
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
		  {
		    if ($_[0] != \@main_project) {
		      my $subdir=@{$_[0]}[$P_PATH];
		      $subdir =~ s+/$++;
		      return $subdir;
		    }
		    # Eliminating the main project by returning undefined!
		  });
  }
  if (@{@$project[$P_TARGETS]} > 0) {
1599
    generate_list("DLLS",1,\@dll_list,sub
1600 1601 1602
		  {
		    return @{$_[0]}[$T_NAME];
		  });
1603
    generate_list("EXES",1,\@exe_list,sub
1604
		  {
1605
		    return "@{$_[0]}[$T_NAME]";
1606
		  });
1607
    print FILEO "\n\n\n";
1608

1609
    print FILEO "### Common settings\n\n";
1610
    # Make it so that the project-wide settings override the global settings
1611 1612 1613
    generate_list("CEXTRA",1,@$project_settings[$T_CEXTRA]);
    generate_list("CXXEXTRA",1,@$project_settings[$T_CXXEXTRA]);
    generate_list("RCEXTRA",1,@$project_settings[$T_RCEXTRA]);
1614 1615 1616 1617
    generate_list("INCLUDE_PATH",1,@$project_settings[$T_INCLUDE_PATH]);
    generate_list("DLL_PATH",1,@$project_settings[$T_DLL_PATH]);
    generate_list("LIBRARY_PATH",1,@$project_settings[$T_LIBRARY_PATH]);
    generate_list("LIBRARIES",1,@$project_settings[$T_LIBRARIES]);
1618 1619 1620 1621 1622
    print FILEO "\n\n";

    my $extra_source_count=@{@$project_settings[$T_SOURCES_C]}+
                           @{@$project_settings[$T_SOURCES_CXX]}+
                           @{@$project_settings[$T_SOURCES_RC]};
1623
    my $no_extra=($extra_source_count == 0);
1624 1625 1626 1627 1628
    if (!$no_extra) {
      print FILEO "### Extra source lists\n\n";
      generate_list("EXTRA_C_SRCS",1,@$project_settings[$T_SOURCES_C]);
      generate_list("EXTRA_CXX_SRCS",1,@$project_settings[$T_SOURCES_CXX]);
      generate_list("EXTRA_RC_SRCS",1,@$project_settings[$T_SOURCES_RC]);
1629 1630 1631
      print FILEO "\n";
      generate_list("EXTRA_OBJS",1,["\$(EXTRA_C_SRCS:.c=.o)","\$(EXTRA_CXX_SRCS:.cpp=.o)"]);
      print FILEO "\n\n\n";
1632
    }
1633

1634
    # Iterate over all the targets...
1635
    foreach my $target (@{@$project[$P_TARGETS]}) {
1636
      print FILEO "### @$target[$T_NAME] sources and settings\n\n";
1637 1638
      my $canon=canonize("@$target[$T_NAME]");
      $canon =~ s+_so$++;
1639 1640

      generate_list("${canon}_MODULE",1,[@$target[$T_NAME]]);
1641 1642 1643
      generate_list("${canon}_C_SRCS",1,@$target[$T_SOURCES_C]);
      generate_list("${canon}_CXX_SRCS",1,@$target[$T_SOURCES_CXX]);
      generate_list("${canon}_RC_SRCS",1,@$target[$T_SOURCES_RC]);
1644
      generate_list("${canon}_LDFLAGS",1,@$target[$T_LDFLAGS]);
1645
      generate_list("${canon}_DLL_PATH",1,@$target[$T_DLL_PATH]);
1646
      generate_list("${canon}_DLLS",1,@$target[$T_DLLS]);
1647 1648
      generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH]);
      generate_list("${canon}_LIBRARIES",1,@$target[$T_LIBRARIES]);
1649
      print FILEO "\n";
1650
      generate_list("${canon}_OBJS",1,["\$(${canon}_C_SRCS:.c=.o)","\$(${canon}_CXX_SRCS:.cpp=.o)","\$(${canon}_RC_SRCS:.rc=.res)"]);
1651
      print FILEO "\n\n\n";
1652 1653
    }
    print FILEO "### Global source lists\n\n";
1654
    generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub
1655 1656 1657 1658 1659 1660 1661 1662
		  {
		    my $canon=canonize(@{$_[0]}[$T_NAME]);
		    $canon =~ s+_so$++;
		    return "\$(${canon}_C_SRCS)";
		  });
    if (!$no_extra) {
      generate_list("",1,[ "\$(EXTRA_C_SRCS)" ]);
    }
1663
    generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub
1664 1665 1666 1667 1668 1669 1670 1671
		  {
		    my $canon=canonize(@{$_[0]}[$T_NAME]);
		    $canon =~ s+_so$++;
		    return "\$(${canon}_CXX_SRCS)";
		  });
    if (!$no_extra) {
      generate_list("",1,[ "\$(EXTRA_CXX_SRCS)" ]);
    }
1672
    generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub
1673 1674 1675 1676 1677 1678
		  {
		    my $canon=canonize(@{$_[0]}[$T_NAME]);
		    $canon =~ s+_so$++;
		    return "\$(${canon}_RC_SRCS)";
		  });
    if (!$no_extra) {
1679
      generate_list("",1,[ "\$(EXTRA_RC_SRCS)" ]);
1680 1681
    }
  }
1682 1683 1684 1685 1686 1687 1688
  print FILEO "\n\n";
  print FILEO "### Tools\n\n";
  print FILEO "CC = winegcc\n";
  print FILEO "CXX = wineg++\n";
  print FILEO "RC = wrc\n";
  print FILEO "WINEBUILD = winebuild\n";
  print FILEO "\n\n";
1689

1690
  print FILEO "### Generic targets\n\n";
1691
  print FILEO "all:";
1692
  if (@$project[$P_PATH] eq "") {
1693
    print FILEO " \$(SUBDIRS)";
1694
  }
1695
  if (@{@$project[$P_TARGETS]} > 0) {
1696
    print FILEO " \$(DLLS:%=%.so) \$(EXES:%=%.so)";
1697 1698
  }
  print FILEO "\n\n";
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
  print FILEO "### Build rules\n";
  print FILEO "\n";
  print FILEO ".PHONY: all clean dummy\n";
  print FILEO "\n";
  print FILEO "\$(SUBDIRS): dummy\n";
  print FILEO "\t\@cd \$\@ && \$(MAKE)\n";
  print FILEO "\n";
  print FILEO "# Implicit rules\n";
  print FILEO "\n";
  print FILEO ".SUFFIXES: .cpp .rc .res\n";
  print FILEO "DEFINCL = \$(INCLUDE_PATH) \$(DEFINES) \$(OPTIONS)\n";
  print FILEO "\n";
  print FILEO ".c.o:\n";
  print FILEO "\t\$(CC) -c \$(CFLAGS) \$(CEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
  print FILEO "\n";
  print FILEO ".cpp.o:\n";
  print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
  print FILEO "\n";
  print FILEO ".cxx.o:\n";
  print FILEO "\t\$(CXX) -c \$(CXXFLAGS) \$(CXXEXTRA) \$(DEFINCL) -o \$\@ \$<\n";
  print FILEO "\n";
  print FILEO ".rc.res:\n";
  print FILEO "\t\$(RC) \$(RCFLAGS) \$(RCEXTRA) \$(DEFINCL) -fo\$@ \$<\n";
  print FILEO "\n";
  print FILEO "# Rules for cleaning\n";
  print FILEO "\n";
  print FILEO "CLEAN_FILES     = *.dbg.c y.tab.c y.tab.h lex.yy.c \\\n";
  print FILEO "                  core *.orig *.rej \\\n";
  print FILEO "                  \\\\\\#*\\\\\\# *~ *% .\\\\\\#*\n";
  print FILEO "\n";
  print FILEO "clean:: \$(SUBDIRS:%=%/__clean__) \$(EXTRASUBDIRS:%=%/__clean__)\n";
  print FILEO "\t\$(RM) \$(CLEAN_FILES) \$(RC_SRCS:.rc=.res) \$(C_SRCS:.c=.o) \$(CXX_SRCS:.cpp=.o)\n";
  print FILEO "\t\$(RM) \$(DLLS:%=%.dbg.o) \$(DLLS:%=%.so)\n";
  print FILEO "\t\$(RM) \$(EXES:%=%.dbg.o) \$(EXES:%=%.so) \$(EXES:%.exe=%)\n";
  print FILEO "\n";
  print FILEO "\$(SUBDIRS:%=%/__clean__): dummy\n";
  print FILEO "\tcd `dirname \$\@` && \$(MAKE) clean\n";
  print FILEO "\n";
  print FILEO "\$(EXTRASUBDIRS:%=%/__clean__): dummy\n";
  print FILEO "\t-cd `dirname \$\@` && \$(RM) \$(CLEAN_FILES)\n";
  print FILEO "\n";
1740

1741 1742
  if (@{@$project[$P_TARGETS]} > 0) {
    print FILEO "### Target specific build rules\n\n";
1743
    foreach my $target (@{@$project[$P_TARGETS]}) {
1744 1745
      my $canon=canonize("@$target[$T_NAME]");
      $canon =~ s/_so$//;
1746 1747 1748

      print FILEO "\$(${canon}_MODULE).dbg.c: \$(${canon}_C_SRCS) \$(${canon}_CXX_SRCS)\n";
      print FILEO "\t\$(WINEBUILD) -o \$\@ --debug -C\$(SRCDIR) \$(${canon}_C_SRCS) \$(${canon}_CXX_SRCS)\n";
1749
      print FILEO "\n";
1750
      print FILEO "\$(${canon}_MODULE).so: \$(${canon}_MODULE).dbg.o \$(${canon}_OBJS)\n";
1751
      if (@{@$target[$T_SOURCES_CXX]} > 0 or @{@$project_settings[$T_SOURCES_CXX]} > 0) {
1752
        print FILEO "\t\$(CXX)";
1753
      } else {
1754
        print FILEO "\t\$(CC)";
1755
      }
1756
      print FILEO " \$(${canon}_LDFLAGS) -o \$\@ \$(${canon}_OBJS) \$(${canon}_MODULE).dbg.o \$(${canon}_LIBRARY_PATH) \$(LIBRARY_PATH) \$(${canon}_DLLS:%=-l%) \$(${canon}_LIBRARIES:%=-l%)\n";
1757
      print FILEO "\n\n";
1758 1759 1760
    }
  }
  close(FILEO);
1761

1762 1763 1764 1765
}


##
1766
# This is where we finally generate files. In fact this method does not
1767
# do anything itself but calls the methods that do the actual work.
1768
sub generate()
1769 1770 1771
{
  print "Generating project files...\n";

1772
  foreach my $project (@projects) {
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
    my $path=@$project[$P_PATH];
    if ($path eq "") {
      $path=".";
    } else {
      $path =~ s+/$++;
    }
    print "  $path\n";
    generate_project_files($project);
  }
}



#####
#
# Option defaults
#
#####

$opt_backup=1;
$opt_lower=$OPT_LOWER_UPPERCASE;
1794
$opt_lower_include=1;
1795

1796 1797
$opt_work_dir=undef;
$opt_single_target=undef;
1798 1799 1800 1801 1802
$opt_target_type=$TT_GUIEXE;
$opt_flags=0;
$opt_is_interactive=$OPT_ASK_NO;
$opt_ask_project_options=$OPT_ASK_NO;
$opt_ask_target_options=$OPT_ASK_NO;
1803
$opt_no_generated_files=0;
1804
$opt_no_source_fix=0;
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
$opt_no_banner=0;



#####
#
# Main
#
#####

1815
sub print_banner()
1816 1817 1818 1819 1820
{
  print "Winemaker $version\n";
  print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
}

1821
sub usage()
1822 1823
{
  print_banner();
1824
  print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup] [--nosource-fix]\n";
1825
  print STDERR "                 [--lower-none|--lower-all|--lower-uppercase]\n";
1826
  print STDERR "                 [--lower-include|--nolower-include] [--mfc|--nomfc]\n";
1827
  print STDERR "                 [--guiexe|--windows|--cuiexe|--console|--dll]\n";
1828
  print STDERR "                 [-Dmacro[=defn]] [-Idir] [-Pdir] [-idll] [-Ldir] [-llibrary]\n";
1829
  print STDERR "                 [--nodlls] [--nomsvcrt] [--interactive] [--single-target name]\n";
1830
  print STDERR "                 [--generated-files|--nogenerated-files]\n";
1831 1832 1833 1834 1835 1836 1837 1838
  print STDERR "                 work_directory\n";
  print STDERR "\nWinemaker is designed to recursively convert all the Windows sources found in\n";
  print STDERR "the specified directory so that they can be compiled with Winelib. During this\n";
  print STDERR "process it will modify and rename some of the files in that directory.\n";
  print STDERR "\tPlease read the manual page before use.\n";
  exit (2);
}

1839 1840
target_init(\@global_settings);

1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
while (@ARGV>0) {
  my $arg=shift @ARGV;
  # General options
  if ($arg eq "--nobanner") {
    $opt_no_banner=1;
  } elsif ($arg eq "--backup") {
    $opt_backup=1;
  } elsif ($arg eq "--nobackup") {
    $opt_backup=0;
  } elsif ($arg eq "--single-target") {
    $opt_single_target=shift @ARGV;
  } elsif ($arg eq "--lower-none") {
    $opt_lower=$OPT_LOWER_NONE;
  } elsif ($arg eq "--lower-all") {
    $opt_lower=$OPT_LOWER_ALL;
  } elsif ($arg eq "--lower-uppercase") {
    $opt_lower=$OPT_LOWER_UPPERCASE;
1858 1859
  } elsif ($arg eq "--lower-include") {
    $opt_lower_include=1;
1860
  } elsif ($arg eq "--nolower-include") {
1861
    $opt_lower_include=0;
1862 1863
  } elsif ($arg eq "--nosource-fix") {
    $opt_no_source_fix=1;
1864 1865
  } elsif ($arg eq "--generated-files") {
    $opt_no_generated_files=0;
1866
  } elsif ($arg eq "--nogenerated-files") {
1867
    $opt_no_generated_files=1;
1868 1869 1870 1871
  } elsif ($arg =~ /^-D/) {
    push @{$global_settings[$T_DEFINES]},$arg;
  } elsif ($arg =~ /^-I/) {
    push @{$global_settings[$T_INCLUDE_PATH]},$arg;
1872 1873 1874
  } elsif ($arg =~ /^-P/) {
    push @{$global_settings[$T_DLL_PATH]},"-L$'";
  } elsif ($arg =~ /^-i/) {
1875
    push @{$global_settings[$T_DLLS]},$';
1876 1877
  } elsif ($arg =~ /^-L/) {
    push @{$global_settings[$T_LIBRARY_PATH]},$arg;
1878 1879
  } elsif ($arg =~ /^-l/) {
    push @{$global_settings[$T_LIBRARIES]},$';
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892

  # 'Source'-based method options
  } elsif ($arg eq "--dll") {
    $opt_target_type=$TT_DLL;
  } elsif ($arg eq "--guiexe" or $arg eq "--windows") {
    $opt_target_type=$TT_GUIEXE;
  } elsif ($arg eq "--cuiexe" or $arg eq "--console") {
    $opt_target_type=$TT_CUIEXE;
  } elsif ($arg eq "--interactive") {
    $opt_is_interactive=$OPT_ASK_YES;
    $opt_ask_project_options=$OPT_ASK_YES;
    $opt_ask_target_options=$OPT_ASK_YES;
  } elsif ($arg eq "--mfc") {
1893
    $opt_flags|=$TF_MFC;
1894
  } elsif ($arg eq "--nomfc") {
1895 1896 1897 1898
    $opt_flags&=~$TF_MFC;
    $opt_flags|=$TF_NOMFC;
  } elsif ($arg eq "--nodlls") {
    $opt_flags|=$TF_NODLLS;
1899 1900
  } elsif ($arg eq "--nomsvcrt") {
    $opt_flags|=$TF_NOMSVCRT;
1901 1902 1903 1904

  # Catch errors
  } else {
    if ($arg ne "--help" and $arg ne "-h" and $arg ne "-?") {
1905 1906 1907 1908
      if (!defined $opt_work_dir) {
        $opt_work_dir=$arg;
      } else {
        print STDERR "error: the work directory, \"$arg\", has already been specified (was \"$opt_work_dir\")\n";
1909
        usage();
1910 1911
      }
    } else {
1912
      usage();
1913 1914 1915 1916
    }
  }
}

1917 1918
if (!defined $opt_work_dir) {
  print STDERR "error: you must specify the directory containing the sources to be converted\n";
1919
  usage();
1920 1921 1922
} elsif (!chdir $opt_work_dir) {
  print STDERR "error: could not chdir to the work directory\n";
  print STDERR "       $!\n";
1923
  usage();
1924 1925
}

1926 1927
if ($opt_no_banner == 0) {
  print_banner();
1928 1929
}

1930 1931
project_init(\@main_project,"");

1932 1933 1934 1935 1936 1937 1938
# Fix the file and directory names
fix_file_and_directory_names(".");

# Scan the sources to identify the projects and targets
source_scan();

# Fix the source files
1939 1940 1941
if (! $opt_no_source_fix) {
  fix_source();
}
1942 1943

# Generate the Makefile and the spec file
1944
if (! $opt_no_generated_files) {
1945 1946
  generate();
}