New loading scheme for Winelib apps, makes them behave like builtin
[wine] / tools / winemaker
index e1e962c..ed01e1e 100755 (executable)
@@ -3,7 +3,22 @@
 # Copyright 2000 Francois Gouget for CodeWeavers
 # fgouget@codeweavers.com
 #
-my $version="0.5.2";
+# 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
+#
+
+my $version="0.5.8";
 
 use Cwd;
 use File::Basename;
@@ -49,6 +64,10 @@ my $OPT_ASK_SKIP=-1;
 
 # General options
 
+##
+# This is the directory in which winemaker will operate.
+my $opt_work_dir;
+
 ##
 # Make a backup of the files
 my $opt_backup;
@@ -61,6 +80,11 @@ my $opt_lower;
 # If we don't find the file referenced by an include, lower it
 my $opt_lower_include;
 
+##
+# 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;
 
 # Options for the 'Source' method
 
@@ -94,6 +118,11 @@ my $opt_ask_target_options;
 # no makefiles, but also no .spec files, no configure.in, etc.
 my $opt_no_generated_files;
 
+##
+# If true then winemaker should not generate the spec files.
+# This is useful if winemaker is being used to create a build environment
+my $opt_no_generated_specs;
+
 ##
 # Specifies not to print the banner if set.
 my $opt_no_banner;
@@ -126,37 +155,45 @@ my $T_INIT=2;
 ##
 # This is a bitfield containing flags refining the way the target 
 # should be handled. See the TF_xxx constants below
-my $T_FLAGS=12;
+my $T_FLAGS=3;
 
 ##
 # This is a reference to an array containing the list of the 
 # resp. C, C++, RC, other (.h, .hxx, etc.) source files.
-my $T_SOURCES_C=3;
-my $T_SOURCES_CXX=4;
-my $T_SOURCES_RC=5;
-my $T_SOURCES_MISC=6;
+my $T_SOURCES_C=4;
+my $T_SOURCES_CXX=5;
+my $T_SOURCES_RC=6;
+my $T_SOURCES_MISC=7;
 
 ##
 # This is a reference to an array containing the list of macro 
 # definitions
-my $T_DEFINES=7;
+my $T_DEFINES=8;
 
 ##
 # This is a reference to an array containing the list of directory 
 # names that constitute the include path
-my $T_INCLUDE_PATH=8;
+my $T_INCLUDE_PATH=9;
+
+##
+# Same as T_INCLUDE_PATH but for the dll search path
+my $T_DLL_PATH=10;
+
+##
+# The list of Windows dlls to import
+my $T_DLLS=11;
 
 ##
 # Same as T_INCLUDE_PATH but for the library search path
-my $T_LIBRARY_PATH=9;
+my $T_LIBRARY_PATH=12;
 
 ##
-# The list of libraries to link with
-my $T_IMPORTS=10;
+# The list of Unix libraries to link with
+my $T_LIBRARIES=13;
 
 ##
 # The list of dependencies between targets
-my $T_DEPENDS=11;
+my $T_DEPENDS=14;
 
 
 # The following constants define the recognized types of target
@@ -212,8 +249,10 @@ sub target_init
   @$target[$T_SOURCES_MISC]=[];
   @$target[$T_DEFINES]=[];
   @$target[$T_INCLUDE_PATH]=[];
+  @$target[$T_DLL_PATH]=[];
+  @$target[$T_DLLS]=[];
   @$target[$T_LIBRARY_PATH]=[];
-  @$target[$T_IMPORTS]=[];
+  @$target[$T_LIBRARIES]=[];
   @$target[$T_DEPENDS]=[];
 }
 
@@ -285,7 +324,6 @@ sub project_init
 #
 #####
 
-my $usage;
 my %warnings;
 
 my %templates;
@@ -389,27 +427,32 @@ sub source_set_options
       push @{@$target[$T_DEFINES]},$option;
     } elsif (@$target[$T_TYPE] == $TT_SETTINGS and $option =~ /^-I/) {
       push @{@$target[$T_INCLUDE_PATH]},$option;
+    } elsif ($option =~ /^-P/) {
+      push @{@$target[$T_DLL_PATH]},"-L$'";
+    } elsif ($option =~ /^-i/) {
+      push @{@$target[$T_DLLS]},"$'";
     } elsif ($option =~ /^-L/) {
       push @{@$target[$T_LIBRARY_PATH]},$option;
     } elsif ($option =~ /^-l/) {
-      push @{@$target[$T_IMPORTS]},$';
-    } elsif (@$target[$T_TYPE] != $TT_DLL and 
-             $option =~ /^--wrap/) {
+      push @{@$target[$T_LIBRARIES]},"$'";
+    } elsif (@$target[$T_TYPE] != $TT_DLL and $option =~ /^--wrap/) {
       @$target[$T_FLAGS]|=$TF_WRAP;
-    } elsif (@$target[$T_TYPE] != $TT_DLL and 
-             $option =~ /^--no-wrap/) {
+    } elsif (@$target[$T_TYPE] != $TT_DLL and $option =~ /^--nowrap/) {
       @$target[$T_FLAGS]&=~$TF_WRAP;
     } elsif ($option =~ /^--mfc/) {
       @$target[$T_FLAGS]|=$TF_MFC;
       if (@$target[$T_TYPE] != $TT_DLL) {
-       @$target[$T_FLAGS]|=$TF_WRAP;
+        @$target[$T_FLAGS]|=$TF_WRAP;
       }
-    } elsif ($option =~ /^--no-mfc/) {
+    } elsif ($option =~ /^--nomfc/) {
+      @$target[$T_FLAGS]&=~$TF_MFC;
       @$target[$T_FLAGS]&=~($TF_MFC|$TF_WRAP);
     } else {
-      print STDERR "warning: unknown option \"$option\", ignoring it\n";
+      print STDERR "error: unknown option \"$option\"\n";
+      return 0;
     }
   }
+  return 1;
 }
 
 ##
@@ -428,6 +471,9 @@ sub source_scan_directory
   # the name of this directory, including a trailing '/', or an empty
   # string if this is the top level directory
   my $dirname=$_[2];
+  # if set then no targets will be looked for and the sources will all 
+  # end up in the parent_project's 'misc' bucket
+  my $no_target=$_[3];
 
   # reference to the project for this directory. May not be used
   my $project;
@@ -477,11 +523,15 @@ sub source_scan_directory
            $targets{$candidate}=1;
          }
        }
+      } elsif ($dentry =~ /^include/i) {
+        # This directory must contain headers we're going to need
+        push @{@$project_settings[$T_INCLUDE_PATH]},"-I$dentry";
+        source_scan_directory($project,"$fullentry/","$dentry/",1);
       } else {
        # Recursively scan this directory. Any source file that cannot be 
-       # attributed to a project in one of the subdirectories will be attributed 
-       # to this project.
-       source_scan_directory($project,"$fullentry/","$dentry/");
+       # attributed to a project in one of the subdirectories will be 
+       # attributed to this project.
+       source_scan_directory($project,"$fullentry/","$dentry/",$no_target);
       }
     } elsif (-f "$fullentry") {
       if ($dentry =~ s/\.exe$//i) {
@@ -493,16 +543,16 @@ sub source_scan_directory
       } elsif ($dentry =~ /\.(cpp|cxx)$/i) {
        if ($dentry =~ /^stdafx.cpp$/i) {
          push @sources_misc,"$dentry";
-         @$project_settings[$T_FLAGS]|=$TF_MFC|$TF_WRAP;
+         @$project_settings[$T_FLAGS]|=$TF_MFC;
        } else {
          push @sources_cxx,"$dentry";
        }
       } elsif ($dentry =~ /\.rc$/i) {
        push @sources_rc,"$dentry";
-      } elsif ($dentry =~ /\.(h|hxx|inl|rc2|dlg)$/i) {
+      } elsif ($dentry =~ /\.(h|hxx|hpp|inl|rc2|dlg)$/i) {
        push @sources_misc,"$dentry";
        if ($dentry =~ /^stdafx.h$/i) {
-         @$project_settings[$T_FLAGS]|=$TF_MFC|$TF_WRAP;
+         @$project_settings[$T_FLAGS]|=$TF_MFC;
        }
       } elsif ($dentry =~ /\.dsp$/i) {
        push @dsp_files,"$dentry";
@@ -528,6 +578,15 @@ sub source_scan_directory
     push @{@$target[$T_SOURCES_MISC]},map "$path$_",@sources_misc;
     return;
   }
+  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;
+    push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
+    return;
+  }
 
   my $source_count=@sources_c+@sources_cxx+@sources_rc+
                    @{@$project_settings[$T_SOURCES_C]}+
@@ -536,7 +595,7 @@ sub source_scan_directory
   if ($source_count == 0) {
     # A project without real sources is not a project, get out!
     if ($project!=$parent_project) {
-      $parent_settings=@$parent_project[$P_SETTINGS];
+      my $parent_settings=@$parent_project[$P_SETTINGS];
       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@sources_misc;
       push @{@$parent_settings[$T_SOURCES_MISC]},map "$dirname$_",@{@$project_settings[$T_SOURCES_MISC]};
     }
@@ -601,7 +660,7 @@ sub source_scan_directory
     # Ask confirmation to the user if he wishes so
     if ($opt_is_interactive == $OPT_ASK_YES) {
       my $target_list=join " ",keys %targets;
-      print "\n*** In $path\n";
+      print "\n*** In ",($path?$path:"./"),"\n";
       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";
@@ -665,20 +724,25 @@ sub source_scan_directory
       }
       $flag_desc.="wrapped";
     }
-    print "* Type any project-wide options (-D/-I/-L/-l/--mfc/--wrap),\n";
+    print "* Type any project-wide options (-D/-I/-P/-i/-L/-l/--mfc/--wrap),\n";
     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";
-    my $options=<STDIN>;
-    chomp $options;
-    if ($options eq "skip") {
-      $opt_ask_target_options=$OPT_ASK_SKIP;
-    } elsif ($options eq "never") {
-      $opt_ask_project_options="never";
-    } else {
-      source_set_options($project_settings,$options);
+    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";
     }
   }
 
@@ -686,9 +750,9 @@ sub source_scan_directory
   # - Check if we have both libraries and programs
   # - Match each target with source files (sort in reverse 
   #   alphabetical order to get the longest matches first)
-  my @local_imports=();
+  my @local_dlls=();
   my @local_depends=();
-  my @program_list=();
+  my @exe_list=();
   foreach $target_name (sort { $b cmp $a } keys %targets) {
     # Create the target...
     my $basename;
@@ -702,13 +766,18 @@ sub source_scan_directory
       @$target[$T_FLAGS]&=~$TF_WRAP;
       $basename=$1;
       push @local_depends,$target_name;
-      push @local_imports,$basename;
+      push @local_dlls,$basename;
     } else {
       @$target[$T_TYPE]=$opt_target_type;
       @$target[$T_INIT]=get_default_init($opt_target_type);
       $basename=$target_name;
-      push @program_list,$target;
+      push @exe_list,$target;
     }
+    # This is the default link list of Visual Studio, except odbccp32 
+    # which we don't have in Wine. Also I add ntdll which seems 
+    # necessary for Winelib.
+    my @std_dlls=qw(advapi32.dll comdlg32.dll gdi32.dll kernel32.dll ntdll.dll odbc32.dll ole32.dll oleaut32.dll shell32.dll user32.dll winspool.drv);
+    @$target[$T_DLLS]=\@std_dlls;
     push @{@$project[$P_TARGETS]},$target;
 
     # Ask for target-specific options
@@ -728,31 +797,47 @@ sub source_scan_directory
       if ($flag_desc ne "") {
        $flag_desc.=")";
       }
-      print "* Specify any link option (-L/-l/--mfc/--wrap) specific to the target\n";
+      print "* Specify any link option (-P/-i/-L/-l/--mfc/--wrap) specific to the target\n";
       print "* \"$target_name\"$flag_desc or 'never' to not be asked this question again:\n";
-      my $options=<STDIN>;
-      chomp $options;
-      if ($options eq "never") {
-       $opt_ask_target_options=$OPT_ASK_NO;
-      } else {
-       source_set_options($target,$options);
+      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";
       }
     }
     if (@$target[$T_FLAGS] & $TF_MFC) {
       @$project_settings[$T_FLAGS]|=$TF_MFC;
+      push @{@$target[$T_DLL_PATH]},"\$(MFC_LIBRARY_PATH)";
+      push @{@$target[$T_DLLS]},"mfc.dll";
+      # FIXME: Link with the MFC in the Unix sense, until we 
+      # start exporting the functions properly.
       push @{@$target[$T_LIBRARY_PATH]},"\$(MFC_LIBRARY_PATH)";
-      push @{@$target[$T_IMPORTS]},"mfc";
+      push @{@$target[$T_LIBRARIES]},"mfc";
     }
 
     # Match sources...
     if ($target_count == 1) {
-      push @{@$target[$T_SOURCES_C]},@sources_c;
-      push @{@$target[$T_SOURCES_CXX]},@sources_cxx;
-      push @{@$target[$T_SOURCES_RC]},@sources_rc;
-      push @{@$target[$T_SOURCES_MISC]},@sources_misc;
+      push @{@$target[$T_SOURCES_C]},@{@$project_settings[$T_SOURCES_C]},@sources_c;
+      @$project_settings[$T_SOURCES_C]=[];
       @sources_c=();
+
+      push @{@$target[$T_SOURCES_CXX]},@{@$project_settings[$T_SOURCES_CXX]},@sources_cxx;
+      @$project_settings[$T_SOURCES_CXX]=[];
       @sources_cxx=();
+
+      push @{@$target[$T_SOURCES_RC]},@{@$project_settings[$T_SOURCES_RC]},@sources_rc;
+      @$project_settings[$T_SOURCES_RC]=[];
       @sources_rc=();
+
+      push @{@$target[$T_SOURCES_MISC]},@{@$project_settings[$T_SOURCES_MISC]},@sources_misc;
+      # No need for sorting these sources
+      @$project_settings[$T_SOURCES_MISC]=[];
       @sources_misc=();
     } else {
       foreach $source (@sources_c) {
@@ -822,10 +907,14 @@ sub source_scan_directory
   # Finally if we are building both libraries and programs in 
   # this directory, then the programs should be linked with all 
   # the libraries
-  if (@local_imports > 0 and @program_list > 0) {
-    foreach $target (@program_list) {
+  if (@local_dlls > 0 and @exe_list > 0) {
+    foreach $target (@exe_list) {
+      push @{@$target[$T_DLL_PATH]},"-L.";
+      push @{@$target[$T_DLLS]},map { "$_.dll" } @local_dlls;
+      # Also link in the Unix sense since none of the functions 
+      # will be exported.
       push @{@$target[$T_LIBRARY_PATH]},"-L.";
-      push @{@$target[$T_IMPORTS]},@local_imports;
+      push @{@$target[$T_LIBRARIES]},@local_dlls;
       push @{@$target[$T_DEPENDS]},@local_depends;
     }
   }
@@ -835,16 +924,20 @@ sub source_scan_directory
 # Scan the source directories in search of things to build
 sub source_scan
 {
-  my $main_target=@{$main_project[$P_TARGETS]}[0];
-
   # If there's a single target then this is going to be the default target
   if (defined $opt_single_target) {
+    # Create the main target
+    my $main_target=[];
+    target_init($main_target);
     if ($opt_target_type == $TT_DLL) {
       @$main_target[$T_NAME]="lib$opt_single_target.so";
     } else {
       @$main_target[$T_NAME]="$opt_single_target";
     }
     @$main_target[$T_TYPE]=$opt_target_type;
+
+    # Add it to the list
+    push @{$main_project[$P_TARGETS]},$main_target;
   }
 
   # The main directory is always going to be there
@@ -852,7 +945,7 @@ sub source_scan
 
   # Now scan the directory tree looking for source files and, maybe, targets
   print "Scanning the source directories...\n";
-  source_scan_directory(\@main_project,"","");
+  source_scan_directory(\@main_project,"","",0);
 
   @projects=sort { @$a[$P_PATH] cmp @$b[$P_PATH] } @projects;
 }
@@ -889,6 +982,7 @@ sub postprocess_targets
        @$wrapper[$T_TYPE]=@$target[$T_TYPE];
        @$wrapper[$T_INIT]=get_default_init(@$target[$T_TYPE]);
        @$wrapper[$T_FLAGS]=$TF_WRAPPER | (@$target[$T_FLAGS] & $TF_MFC);
+       @$wrapper[$T_DLLS]=[ "kernel32.dll", "ntdll.dll", "user32.dll" ];
        push @{@$wrapper[$T_SOURCES_C]},"@$wrapper[$T_NAME]_wrapper.c";
 
        my $index=bsearch(@$target[$T_SOURCES_C],"@$wrapper[$T_NAME]_wrapper.c");
@@ -937,16 +1031,19 @@ sub fix_file_and_directory_names
       my $new_name=$dentry;
       $new_name =~ s/[ \$]/_/g;
 
-      # Our Make.rules supports all-uppercase and all-lowercase extensions.
-      # The others must be fixed.
+      # Only all lowercase extensions are supported (because of the 
+      # transformations ':.c=.o') .
       if (-f "$dirname/$new_name") {
-       if ($new_name =~ /\.cpp/i and $new_name !~ /\.(cpp|CPP)/) {
+       if ($new_name =~ /\.C$/) {
+         $new_name =~ s/\.C$/.c/;
+       }
+       if ($new_name =~ /\.cpp$/i) {
          $new_name =~ s/\.cpp$/.cpp/i;
        }
        if ($new_name =~ s/\.cxx$/.cpp/i) {
          $warn=1;
        }
-       if ($new_name =~ /\.rc/i and $new_name !~ /\.(rc|RC)/) {
+       if ($new_name =~ /\.rc$/i) {
          $new_name =~ s/\.rc$/.rc/i;
        }
        # And this last one is to avoid confusion then running make
@@ -1062,10 +1159,20 @@ sub search_from
       $dirname=dirname($dirname) . "/";
       $real_path.="../";
     } else {
+      # The file/directory may have been renamed before. Also try to 
+      # match the renamed file.
+      my $renamed=$component;
+      $renamed =~ s/[ \$]/_/g;
+      if ($renamed eq $component) {
+        undef $renamed;
+      }
+
       my $directory=get_directory_contents $dirname;
       my $found;
       foreach $dentry (@$directory) {
-       if ($dentry =~ /^$component$/i) {
+       if ($dentry =~ /^$component$/i or
+            (defined $renamed and $dentry =~ /^$renamed$/i)
+           ) {
          $dirname.="$dentry/";
          $real_path.="$dentry/";
          $found=1;
@@ -1135,7 +1242,8 @@ sub get_real_include_name
       if (!is_absolute($dirname)) {
        $dirname="@$project[$P_PATH]$dirname";
       } else {
-       $dirname=~ s+^\$\(TOPSRCDIR\)/++;
+        $dirname=~ s+^\$\(TOPSRCDIR\)/++;
+        $dirname=~ s+^\$\(SRCDIR\)/+@$project[$P_PATH]+;
       }
       #print "    in $dirname\n";
       $real_filename=search_from("$dirname",\@file_components);
@@ -1149,6 +1257,7 @@ sub get_real_include_name
       my $dirname=$include;
       $dirname=~ s+^-I++;
       $dirname=~ s+^\$\(TOPSRCDIR\)\/++;
+      $dirname=~ s+^\$\(SRCDIR\)\/+@$project[$P_PATH]+;
       #print "    in $dirname  (global setting)\n";
       $real_filename=search_from("$dirname",\@file_components);
       if (defined $real_filename) {
@@ -1164,6 +1273,20 @@ sub get_real_include_name
   return $filename;
 }
 
+sub print_pack
+{
+  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";
+  }
+}
+
 ##
 # 'Parses' a source file and fixes constructs that would not work with 
 # Winelib. The parsing is rather simple and not all non-portable features 
@@ -1214,93 +1337,218 @@ sub fix_file
   my $modified=0;
   my $rc_block_depth=0;
   my $rc_textinclude_state=0;
+  my @pack_stack;
   while (<FILEI>) {
+    # Remove any trailing CtrlZ, which isn't strictly in the file
+    if (/\x1A/) {
+      s/\x1A//;
+      last if (/^$/)
+    }
     $line++;
-    $_ =~ s/\r\n$/\n/;
-    if ($is_rc and !$is_mfc and /^(\s*\#\s*include\s*)\"afxres\.h\"/) {
+    s/\r\n$/\n/;
+    if (!/\n$/) {
+      # Make sure all files are '\n' terminated
+      $_ .= "\n";
+    }
+    if ($is_rc and !$is_mfc and /^(\s*)(\#\s*include\s*)\"afxres\.h\"/) {
       # 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 
-      # projects can compile in Wine without the MFC sources. This does not 
-      # harm VC but it will put 'afxres.h' back the next time the file is 
-      # edited.
+      # projects can compile in Wine without the MFC sources.
       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";
       }
-      print FILEO "/* winemaker: $1\"afxres.h\" */\n";
-      print FILEO "$1\"winres.h\"$'";
+      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\"$'";
       $modified=1;
+
     } 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);
-    } elsif (/^(\s*\#\s*pragma\s*pack\s*\((\s*push\s*,?)?\s*)(\w*)(\s*\))/) {
-      my $pragma_header=$1;
-      my $size=$3;
-      my $pragma_trailer=$4;
-      #print "$pragma_header$size$pragma_trailer$'";
-      #print "pragma push: size=$size\n";
-      print FILEO "/* winemaker: $pragma_header$size$pragma_trailer */\n";
-      $line++;
-      if ($size eq "pop") {
-       print FILEO "#include <poppack.h>$'";
-      } elsif ($size eq "1") {
-       print FILEO "#include <pshpack1.h>$'";
-      } elsif ($size eq "2") {
-       print FILEO "#include <pshpack2.h>$'";
-      } elsif ($size eq "8") {
-       print FILEO "#include <pshpack8.h>$'";
-      } elsif ($size eq "4" or $size eq "") {
-       print FILEO "#include <pshpack4.h>$'";
+
+    } elsif (s/^(\s*)(\#\s*pragma\s+pack\s*\(\s*)//) {
+      # Pragma pack handling
+      #
+      # pack_stack is an array of references describing the stack of 
+      # pack directives currently in effect. Each directive if described 
+      # 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=$';
+        $pack_comment =~ s/^\s*//;
+        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=$';
+        $pack_comment =~ s/^\s*//;
+        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
+        # label multiple times. If 'n' is omitted then the alignment is 
+        # 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 ];
+
       } else {
-       my $warning="pack:$size";
-       if (!defined $warnings{$warning}) {
-         $warnings{$warning}="1";
-         print STDERR "warning: assuming that the value of $size is 4 in\n";
-         print STDERR "$line:   $pragma_header$size$pragma_trailer\n"; 
-         print STDERR "warning: the above warning is issued only once\n";
-       }
-       print FILEO "#include <pshpack4.h>$'";
-       $modified=1;
+        # pragma pack(???               -> What's that?
+        print FILEO "$pack_indent/* winemaker:warning: Unknown type of pragma pack directive */\n";
+        print FILEO "$pack_indent$pack_header$_";
+
       }
+      $modified=1;
+
     } elsif ($is_rc) {
-      if ($rc_block_depth == 0 and /^(\w+\s+(BITMAP|CURSOR|FONT|FONTDIR|ICON|MESSAGETABLE|TEXT)\s+((DISCARDABLE|FIXED|IMPURE|LOADONCALL|MOVEABLE|PRELOAD|PURE)\s+)*)([\"<]?)([^\">\r\n]+)([\">]?)/) {
+      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]+)([\">]?)/) {
        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);
+
       } 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);
+
       } elsif ($is_rc and !$is_mfc and $rc_block_depth == 0 and /^\s*\d+\s+TEXTINCLUDE\s*/) {
        $rc_textinclude_state=1;
        print FILEO;
+
       } elsif ($rc_textinclude_state == 3 and /^(\s*\"\#\s*include\s*\"\")afxres\.h(\"\"\\r\\n\")/) {
        print FILEO "$1winres.h$2$'";
        $modified=1;
+
       } elsif (/^\s*BEGIN(\W.*)?$/) {
        $rc_textinclude_state|=2;
        $rc_block_depth++;
        print FILEO;
+
       } elsif (/^\s*END(\W.*)?$/) {
        $rc_textinclude_state=0;
        if ($rc_block_depth>0) {
          $rc_block_depth--;
        }
        print FILEO;
+
       } else {
        print FILEO;
       }
+
     } else {
       print FILEO;
     }
   }
+
   close(FILEI);
   close(FILEO);
   if ($opt_backup == 0 or $modified == 0) {
@@ -1341,6 +1589,9 @@ sub fix_source
 # Generates a target's .spec file
 sub generate_spec_file
 {
+  if ($opt_no_generated_specs) {
+    return;
+  }
   my $path=$_[0];
   my $target=$_[1];
   my $project_settings=$_[2];
@@ -1359,8 +1610,10 @@ sub generate_spec_file
     return;
   }
 
-  my $canon=canonize($basename);
-  print FILEO "name    $canon\n";
+  my $module=$basename;
+  $module =~ s+^lib++;
+  $module=canonize($module);
+  print FILEO "name    $module\n";
   print FILEO "type    win32\n";
   if (@$target[$T_TYPE] == $TT_GUIEXE) {
     print FILEO "mode    guiexe\n";
@@ -1378,20 +1631,30 @@ sub generate_spec_file
     }
     my $rcname=@{@$target[$T_SOURCES_RC]}[0];
     $rcname =~ s+\.rc$++i;
+    $rcname =~ s+([^/\w])+\\$1+g;
     print FILEO "rsrc    $rcname.res\n";
   }
   print FILEO "\n";
-  # FIXME: we should try to remove duplicates in the import list
-  foreach $library (@{$global_settings[$T_IMPORTS]}) {
-    print FILEO "import $library\n";
+  my %dlls;
+  foreach $dll (@{$global_settings[$T_DLLS]}) {
+    if (!defined $dlls{$dll}) {
+      print FILEO "import $dll\n";
+      $dlls{$dll}=1;
+    }
   }
   if (defined $project_settings) {
-    foreach $library (@{@$project_settings[$T_IMPORTS]}) {
-      print FILEO "import $library\n";
+    foreach $dll (@{@$project_settings[$T_DLLS]}) {
+      if (!defined $dlls{$dll}) {
+        print FILEO "import $dll\n";
+        $dlls{$dll}=1;
+      }
     }
   }
-  foreach $library (@{@$target[$T_IMPORTS]}) {
-    print FILEO "import $library\n";
+  foreach $dll (@{@$target[$T_DLLS]}) {
+    if (!defined $dlls{$dll}) {
+      print FILEO "import $dll\n";
+      $dlls{$dll}=1;
+    }
   }
 
   # Don't forget to export the 'Main' function for wrapped executables, 
@@ -1431,11 +1694,12 @@ sub generate_wrapper_file
   my $app_init=(@$target[$T_TYPE]==$TT_GUIEXE?"\"WinMain\"":"\"main\"");
   my $app_mfc=(@$target[$T_FLAGS] & $TF_MFC?"\"mfc\"":NULL);
   foreach $line (@{$templates{"wrapper.c"}}) {
-    $line =~ s/\#\#WINEMAKER_APP_NAME\#\#/$app_name/;
-    $line =~ s/\#\#WINEMAKER_APP_TYPE\#\#/$app_type/;
-    $line =~ s/\#\#WINEMAKER_APP_INIT\#\#/$app_init/;
-    $line =~ s/\#\#WINEMAKER_APP_MFC\#\#/$app_mfc/;
-    print FILEO $line;
+    my $l=$line;
+    $l =~ s/\#\#WINEMAKER_APP_NAME\#\#/$app_name/;
+    $l =~ s/\#\#WINEMAKER_APP_TYPE\#\#/$app_type/;
+    $l =~ s/\#\#WINEMAKER_APP_INIT\#\#/$app_init/;
+    $l =~ s/\#\#WINEMAKER_APP_MFC\#\#/$app_mfc/;
+    print FILEO $l;
   }
   close(FILEO);
 }
@@ -1449,11 +1713,12 @@ sub generate_list
   my $last=$_[1];
   my $list=$_[2];
   my $data=$_[3];
+  my $first=$name;
 
   if ($name) {
-    printf FILEO "%-9s =",$name;
+    printf FILEO "%-22s=",$name;
   }
-  if (defined $list and @$list > 0) {
+  if (defined $list) {
     foreach $item (@$list) {
       my $value;
       if (defined $data) {
@@ -1462,12 +1727,17 @@ sub generate_list
        $value=$item;
       }
       if ($value ne "") {
-       print FILEO " \\\n\t$value";
+       if ($first) {
+         print FILEO " $value";
+         $first=0;
+       } else {
+         print FILEO " \\\n\t\t\t$value";
+       }
       }
     }
   }
   if ($last) {
-    print FILEO "\n\n";
+    print FILEO "\n";
   }
 }
 
@@ -1477,20 +1747,20 @@ sub generate_project_files
 {
   my $project=$_[0];
   my $project_settings=@$project[$P_SETTINGS];
-  my @library_list=();
-  my @program_list=();
+  my @dll_list=();
+  my @exe_list=();
 
   # Then sort the targets and separate the libraries from the programs
   foreach $target (sort { @$a[$T_NAME] cmp @$b[$T_NAME] } @{@$project[$P_TARGETS]}) {
     if (@$target[$T_TYPE] == $TT_DLL) {
-      push @library_list,$target;
+      push @dll_list,$target;
     } else {
-      push @program_list,$target;
+      push @exe_list,$target;
     }
   }
   @$project[$P_TARGETS]=[];
-  push @{@$project[$P_TARGETS]}, @library_list;
-  push @{@$project[$P_TARGETS]}, @program_list;
+  push @{@$project[$P_TARGETS]}, @dll_list;
+  push @{@$project[$P_TARGETS]}, @exe_list;
 
   if (!open(FILEO,">@$project[$P_PATH]Makefile.in")) {
     print STDERR "error: could not open \"@$project[$P_PATH]/Makefile.in\" for writing\n";
@@ -1498,11 +1768,14 @@ sub generate_project_files
     return;
   }
 
+  print FILEO "### Generated by Winemaker\n";
+  print FILEO "\n\n";
+
   print FILEO "### Generic autoconf variables\n\n";
-  print FILEO "TOPSRCDIR = \@top_srcdir\@\n";
-  print FILEO "TOPOBJDIR = .\n";
-  print FILEO "SRCDIR    = \@srcdir\@\n";
-  print FILEO "VPATH     = \@srcdir\@\n";
+  generate_list("TOPSRCDIR",1,[ "\@top_srcdir\@" ]);
+  generate_list("TOPOBJDIR",1,[ "." ]);
+  generate_list("SRCDIR",1,[ "\@srcdir\@" ]);
+  generate_list("VPATH",1,[ "\@srcdir\@" ]);
   print FILEO "\n";
   if (@$project[$P_PATH] eq "") {
     # This is the main project. It is also responsible for recursively 
@@ -1518,62 +1791,46 @@ sub generate_project_files
                  });
   }
   if (@{@$project[$P_TARGETS]} > 0) {
-    generate_list("LIBRARIES",1,\@library_list,sub 
+    generate_list("DLLS",1,\@dll_list,sub
                  {
                    return @{$_[0]}[$T_NAME];
                  });
-    generate_list("PROGRAMS",1,\@program_list,sub 
+    generate_list("EXES",1,\@exe_list,sub
                  {
-                   return @{$_[0]}[$T_NAME];
+                   return "@{$_[0]}[$T_NAME]";
                  });
-    print FILEO "\n\n";
+    print FILEO "\n\n\n";
 
     print FILEO "### Global settings\n\n";
     # Make it so that the project-wide settings override the global settings
-    generate_list("DEFINES",0,@$project_settings[$T_DEFINES],sub 
-                 {
-                   return "$_[0]";
-                 });
-    generate_list("",1,$global_settings[$T_DEFINES],sub 
-                 {
-                   return "$_[0]";
-                 });
-    generate_list("INCLUDE_PATH",$no_extra,@$project_settings[$T_INCLUDE_PATH],sub 
-                 {
-                   return "$_[0]";
-                 });
-    generate_list("",1,$global_settings[$T_INCLUDE_PATH],sub 
+    generate_list("DEFINES",0,@$project_settings[$T_DEFINES]);
+    generate_list("",1,$global_settings[$T_DEFINES]);
+    generate_list("INCLUDE_PATH",$no_extra,@$project_settings[$T_INCLUDE_PATH]);
+    generate_list("",1,$global_settings[$T_INCLUDE_PATH],sub
                  {
-                   if ($_[0] !~ /^-I/) {
+                   if ($_[0] !~ /^-I/ or is_absolute($')) {
                      return "$_[0]";
                    }
-                   if (is_absolute($')) {
-                     return "$_[0]";
-                   }
-                   return "\$(TOPSRCDIR)/$_[0]";
-                 });
-    generate_list("LIBRARY_PATH",$no_extra,@$project_settings[$T_LIBRARY_PATH],sub 
-                 {
-                   return "$_[0]";
+                   return "-I\$(TOPSRCDIR)/$'";
                  });
-    generate_list("",1,$global_settings[$T_LIBRARY_PATH],sub 
+    generate_list("DLL_PATH",$no_extra,@$project_settings[$T_DLL_PATH]);
+    generate_list("",1,$global_settings[$T_DLL_PATH],sub
                  {
-                   if ($_[0] !~ /^-L/) {
+                   if ($_[0] !~ /^-L/ or is_absolute($')) {
                      return "$_[0]";
                    }
-                   if (is_absolute($')) {
-                     return "$_[0]";
-                   }
-                   return "\$(TOPSRCDIR)/$_[0]";
-                 });
-    generate_list("IMPORTS",$no_extra,@$project_settings[$T_IMPORTS],sub 
-                 {
-                   return "$_[0]";
+                   return "-L\$(TOPSRCDIR)/$'";
                  });
-    generate_list("",1,$global_settings[$T_IMPORTS],sub 
+    generate_list("LIBRARY_PATH",$no_extra,@$project_settings[$T_LIBRARY_PATH]);
+    generate_list("",1,$global_settings[$T_LIBRARY_PATH],sub
                  {
-                   return "$_[0]";
+                   if ($_[0] !~ /^-L/ or is_absolute($')) {
+                     return "$_[0]";
+                   }
+                   return "-L\$(TOPSRCDIR)/$'";
                  });
+    generate_list("LIBRARIES",$no_extra,@$project_settings[$T_LIBRARIES]);
+    generate_list("",1,$global_settings[$T_LIBRARIES]);
     print FILEO "\n\n";
 
     my $extra_source_count=@{@$project_settings[$T_SOURCES_C]}+
@@ -1585,13 +1842,14 @@ sub generate_project_files
       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]);
-      print FILEO "EXTRA_OBJS = \$(EXTRA_C_SRCS:.c=.o) \$(EXTRA_CXX_SRCS:.cpp=.o)\n";
-      print FILEO "\n\n";
+      print FILEO "\n";
+      generate_list("EXTRA_OBJS",1,["\$(EXTRA_C_SRCS:.c=.o)","\$(EXTRA_CXX_SRCS:.cpp=.o)"]);
+      print FILEO "\n\n\n";
     }
-    
+
     # Iterate over all the targets...
     foreach $target (@{@$project[$P_TARGETS]}) {
-      print FILEO "\n### @$target[$T_NAME] sources and settings\n\n";
+      print FILEO "### @$target[$T_NAME] sources and settings\n\n";
       my $canon=canonize("@$target[$T_NAME]");
       $canon =~ s+_so$++;
       generate_list("${canon}_C_SRCS",1,@$target[$T_SOURCES_C]);
@@ -1604,24 +1862,17 @@ sub generate_project_files
       } elsif (@$target[$T_FLAGS] & $TF_WRAPPER) {
        $basename.="_wrapper";
       }
-      generate_list("${canon}_SPEC_SRCS",1,[ "$basename.spec"]);
-      generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH],sub 
-                   {
-                     return "$_[0]";
-                   });
-      generate_list("${canon}_IMPORTS",1,@$target[$T_IMPORTS],sub 
-                   {
-                     return "$_[0]";
-                   });
-      generate_list("${canon}_DEPENDS",1,@$target[$T_DEPENDS],sub 
-                   {
-                     return "$_[0]";
-                   });
-      print FILEO "${canon}_OBJS = \$(${canon}_SPEC_SRCS:.spec=.spec.o) \$(${canon}_C_SRCS:.c=.o) \$(${canon}_CXX_SRCS:.cpp=.o) \$(EXTRA_OBJS)\n";
-      print FILEO "\n\n";
+      generate_list("${canon}_SPEC_SRCS",1,[ "$basename.spec" ]);
+      generate_list("${canon}_DLL_PATH",1,@$target[$T_DLL_PATH]);
+      generate_list("${canon}_LIBRARY_PATH",1,@$target[$T_LIBRARY_PATH]);
+      generate_list("${canon}_LIBRARIES",1,@$target[$T_LIBRARIES]);
+      generate_list("${canon}_DEPENDS",1,@$target[$T_DEPENDS]);
+      print FILEO "\n";
+      generate_list("${canon}_OBJS",1,["\$(${canon}_C_SRCS:.c=.o)","\$(${canon}_CXX_SRCS:.cpp=.o)","\$(EXTRA_OBJS)"]);
+      print FILEO "\n\n\n";
     }
     print FILEO "### Global source lists\n\n";
-    generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub 
+    generate_list("C_SRCS",$no_extra,@$project[$P_TARGETS],sub
                  {
                    my $canon=canonize(@{$_[0]}[$T_NAME]);
                    $canon =~ s+_so$++;
@@ -1630,7 +1881,7 @@ sub generate_project_files
     if (!$no_extra) {
       generate_list("",1,[ "\$(EXTRA_C_SRCS)" ]);
     }
-    generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub 
+    generate_list("CXX_SRCS",$no_extra,@$project[$P_TARGETS],sub
                  {
                    my $canon=canonize(@{$_[0]}[$T_NAME]);
                    $canon =~ s+_so$++;
@@ -1639,74 +1890,89 @@ sub generate_project_files
     if (!$no_extra) {
       generate_list("",1,[ "\$(EXTRA_CXX_SRCS)" ]);
     }
-    generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub 
+    generate_list("RC_SRCS",$no_extra,@$project[$P_TARGETS],sub
                  {
                    my $canon=canonize(@{$_[0]}[$T_NAME]);
                    $canon =~ s+_so$++;
                    return "\$(${canon}_RC_SRCS)";
                  });
     if (!$no_extra) {
-      generate_list("",1,@$project_settings[$T_SOURCES_RC]);
+      generate_list("",1,[ "\$(EXTRA_RC_SRCS)" ]);
     }
-    generate_list("SPEC_SRCS",1,@$project[$P_TARGETS],sub 
+    generate_list("SPEC_SRCS",1,@$project[$P_TARGETS],sub
                  {
                    my $canon=canonize(@{$_[0]}[$T_NAME]);
                    $canon =~ s+_so$++;
                    return "\$(${canon}_SPEC_SRCS)";
                  });
-    print FILEO "\n\n";
   }
+  print FILEO "\n\n\n";
 
   print FILEO "### Generic autoconf targets\n\n";
+  print FILEO "all:";
   if (@$project[$P_PATH] eq "") {
-    print FILEO "all: \$(SUBDIRS) \$(LIBRARIES) \$(PROGRAMS)\n";
-  } else {
-    print FILEO "all: \$(LIBRARIES) \$(PROGRAMS)\n";
+    print FILEO " \$(SUBDIRS)";
   }
-  print FILEO "\n";
+  if (@{@$project[$P_TARGETS]} > 0) {
+    print FILEO " \$(DLLS) \$(EXES:%=%.so)";
+  }
+  print FILEO "\n\n";
   print FILEO "\@MAKE_RULES\@\n";
   print FILEO "\n";
   print FILEO "install::\n";
   if (@$project[$P_PATH] eq "") {
     # This is the main project. It is also responsible for recursively 
     # calling the other projects
-    print FILEO "\tfor i in \$(SUBDIRS); do (cd \$\$i; \$(MAKE) install) || exit 1; done\n";
+    print FILEO "\t_list=\"\$(SUBDIRS)\"; for i in \$\$_list; do (cd \$\$i; \$(MAKE) install) || exit 1; done\n";
   }
   if (@{@$project[$P_TARGETS]} > 0) {
-    print FILEO "\tfor i in \$(PROGRAMS); do \$(INSTALL_PROGRAM) \$\$i \$(bindir); done\n";
-    print FILEO "\tfor i in \$(LIBRARIES); do \$(INSTALL_LIBRARY) \$\$i \$(libdir); done\n";
+    print FILEO "\t_list=\"\$(EXES) \$(EXES:%=%.so)\"; for i in \$\$_list; do \$(INSTALL_PROGRAM) \$\$i \$(bindir); done\n";
+    print FILEO "\t_list=\"\$(DLLS)\"; for i in \$\$_list; do \$(INSTALL_PROGRAM) \$\$i \$(libdir); done\n";
   }
   print FILEO "\n";
   print FILEO "uninstall::\n";
   if (@$project[$P_PATH] eq "") {
     # This is the main project. It is also responsible for recursively 
     # calling the other projects
-    print FILEO "\tfor i in \$(SUBDIRS); do (cd \$\$i; \$(MAKE) uninstall) || exit 1; done\n";
+    print FILEO "\t_list=\"\$(SUBDIRS)\"; for i in \$\$_list; do (cd \$\$i; \$(MAKE) uninstall) || exit 1; done\n";
   }
   if (@{@$project[$P_TARGETS]} > 0) {
-    print FILEO "\tfor i in \$(PROGRAMS); do \$(RM) \$(bindir)/\$\$i;done\n";
-    print FILEO "\tfor i in \$(LIBRARIES); do \$(RM) \$(libdir)/\$\$i;done\n";
+    print FILEO "\t_list=\"\$(EXES) \$(EXES:%=%.so)\"; for i in \$\$_list; do \$(RM) \$(bindir)/\$\$i;done\n";
+    print FILEO "\t_list=\"\$(DLLS)\"; for i in \$\$_list; do \$(RM) \$(libdir)/\$\$i;done\n";
   }
   print FILEO "\n\n\n";
-    
+
   if (@{@$project[$P_TARGETS]} > 0) {
     print FILEO "### Target specific build rules\n\n";
     foreach $target (@{@$project[$P_TARGETS]}) {
       my $canon=canonize("@$target[$T_NAME]");
       $canon =~ s/_so$//;
-      print FILEO "\$(${canon}_SPEC_SRCS:.spec=.spec.c): \$(${canon}_RC_SRCS:.rc=.res)\n";
+      print FILEO "\$(${canon}_SPEC_SRCS:.spec=.tmp.o): \$(${canon}_OBJS)\n";
+      print FILEO "\t\$(LDCOMBINE) \$(${canon}_OBJS) -o \$\@\n";
+      print FILEO "\t-\$(STRIP) \$(STRIPFLAGS) \$\@\n";
+      print FILEO "\n";
+      print FILEO "\$(${canon}_SPEC_SRCS:.spec=.spec.c): \$(${canon}_SPEC_SRCS) \$(${canon}_SPEC_SRCS:.spec=.tmp.o) \$(${canon}_RC_SRCS:.rc=.res)\n";
+      print FILEO "\t\$(LD_PATH) \$(WINEBUILD) -fPIC \$(${canon}_DLL_PATH) \$(WINE_DLL_PATH) -sym \$(${canon}_SPEC_SRCS:.spec=.tmp.o) -o \$\@ -spec \$(SRCDIR)/\$(${canon}_SPEC_SRCS)\n";
       print FILEO "\n";
-      print FILEO "@$target[$T_NAME]: \$(${canon}_OBJS) \$(${canon}_DEPENDS) \n";
-      if (@$target[$T_TYPE] eq $TT_DLL) {
-       print FILEO "\t\$(LDSHARED) -shared -Wl,-soname,\$\@ -o \$\@ \$(${canon}_OBJS) \$(${canon}_LIBRARY_PATH) \$(${canon}_IMPORTS:%=-l%) \$(DLL_LINK) \$(LIBS)\n";
+      my $t_name=@$target[$T_NAME];
+      if (@$target[$T_TYPE]!=$TT_DLL) {
+        $t_name.=".so";
+      }
+      print FILEO "$t_name: \$(${canon}_SPEC_SRCS:.spec=.spec.o) \$(${canon}_OBJS) \$(${canon}_DEPENDS) \n";
+      if (@{@$target[$T_SOURCES_CXX]} > 0 or @{@$project_settings[$T_SOURCES_CXX]} > 0) {
+        print FILEO "\t\$(LDXXSHARED)";
       } else {
-       print FILEO "\t\$(CC) -o \$\@ \$(${canon}_OBJS) \$(${canon}_LIBRARY_PATH) \$(${canon}_IMPORTS:%=-l%) \$(DLL_LINK) \$(LIBS)\n";
+        print FILEO "\t\$(LDSHARED)";
       }
-      print FILEO "\n";
+      print FILEO " \$(LDDLLFLAGS) -o \$\@ \$(${canon}_OBJS) \$(${canon}_SPEC_SRCS:.spec=.spec.o) \$(${canon}_LIBRARY_PATH) \$(${canon}_LIBRARIES:%=-l%) \$(DLL_LINK) \$(LIBS)\n";
+      if (@$target[$T_TYPE] ne $TT_DLL) {
+        print FILEO "\ttest -f @$target[$T_NAME] || \$(LN_S) \$(WINE) @$target[$T_NAME]\n";
+      }
+      print FILEO "\n\n";
     }
   }
   close(FILEO);
+
   foreach $target (@{@$project[$P_TARGETS]}) {
     generate_spec_file(@$project[$P_PATH],$target,$project_settings);
     if (@$target[$T_FLAGS] & $TF_WRAPPER) {
@@ -1795,6 +2061,9 @@ sub generate_global_files
       }
     }
   }
+  if (!defined $a_source_file) {
+    $a_source_file="Makefile.in";
+  }
 
   generate_configure("configure.in",$a_source_file);
   unlink("configure");
@@ -1803,7 +2072,7 @@ sub generate_global_files
   }
   # Add execute permission to configure for whoever has the right to read it
   my @st=stat("configure");
-  if (defined @st) {
+  if (@st) {
     my $mode=$st[2];
     $mode|=($mode & 0444) >>2;
     chmod($mode,"configure");
@@ -1867,6 +2136,7 @@ $opt_backup=1;
 $opt_lower=$OPT_LOWER_UPPERCASE;
 $opt_lower_include=1;
 
+# $opt_work_dir=<undefined>
 # $opt_single_target=<undefined>
 $opt_target_type=$TT_GUIEXE;
 $opt_flags=0;
@@ -1874,6 +2144,8 @@ $opt_is_interactive=$OPT_ASK_NO;
 $opt_ask_project_options=$OPT_ASK_NO;
 $opt_ask_target_options=$OPT_ASK_NO;
 $opt_no_generated_files=0;
+$opt_no_generated_specs=0;
+$opt_no_source_fix=0;
 $opt_no_banner=0;
 
 
@@ -1884,6 +2156,32 @@ $opt_no_banner=0;
 #
 #####
 
+sub print_banner
+{
+  print "Winemaker $version\n";
+  print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
+}
+
+sub usage
+{
+  print_banner();
+  print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup] [--nosource-fix]\n";
+  print STDERR "                 [--lower-none|--lower-all|--lower-uppercase]\n";
+  print STDERR "                 [--lower-include|--nolower-include]\n";
+  print STDERR "                 [--guiexe|--windows|--cuiexe|--console|--dll]\n";
+  print STDERR "                 [--wrap|--nowrap] [--mfc|--nomfc]\n";
+  print STDERR "                 [-Dmacro[=defn]] [-Idir] [-Pdir] [-idll] [-Ldir] [-llibrary]\n";
+  print STDERR "                 [--interactive] [--single-target name]\n";
+  print STDERR "                 [--generated-files|--nogenerated-files] [--nogenerated-specs]\n";
+  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);
+}
+
+
 project_init(\@main_project,"");
 
 while (@ARGV>0) {
@@ -1905,21 +2203,29 @@ while (@ARGV>0) {
     $opt_lower=$OPT_LOWER_UPPERCASE;
   } elsif ($arg eq "--lower-include") {
     $opt_lower_include=1;
-  } elsif ($arg eq "--no-lower-include") {
+  } elsif ($arg eq "--nolower-include") {
     $opt_lower_include=0;
+  } elsif ($arg eq "--nosource-fix") {
+    $opt_no_source_fix=1;
   } elsif ($arg eq "--generated-files") {
     $opt_no_generated_files=0;
-  } elsif ($arg eq "--no-generated-files") {
+  } elsif ($arg eq "--nogenerated-files") {
     $opt_no_generated_files=1;
+  } elsif ($arg eq "--nogenerated-specs") {
+    $opt_no_generated_specs=1;
 
   } elsif ($arg =~ /^-D/) {
     push @{$global_settings[$T_DEFINES]},$arg;
   } elsif ($arg =~ /^-I/) {
     push @{$global_settings[$T_INCLUDE_PATH]},$arg;
+  } elsif ($arg =~ /^-P/) {
+    push @{$global_settings[$T_DLL_PATH]},"-L$'";
+  } elsif ($arg =~ /^-i/) {
+    push @{$global_settings[$T_DLLS]},$';
   } elsif ($arg =~ /^-L/) {
     push @{$global_settings[$T_LIBRARY_PATH]},$arg;
   } elsif ($arg =~ /^-l/) {
-    push @{$global_settings[$T_IMPORTS]},$';
+    push @{$global_settings[$T_LIBRARIES]},$';
 
   # 'Source'-based method options
   } elsif ($arg eq "--dll") {
@@ -1937,6 +2243,7 @@ while (@ARGV>0) {
   } elsif ($arg eq "--nowrap") {
     $opt_flags&=~$TF_WRAP;
   } elsif ($arg eq "--mfc") {
+    $opt_flags|=$TF_MFC;
     $opt_flags|=$TF_MFC|$TF_WRAP;
     $needs_mfc=1;
   } elsif ($arg eq "--nomfc") {
@@ -1946,27 +2253,29 @@ while (@ARGV>0) {
   # Catch errors
   } else {
     if ($arg ne "--help" and $arg ne "-h" and $arg ne "-?") {
-      print STDERR "Unknown option: $arg\n";
+      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";
+        usage();
+      }
+    } else {
+      usage();
     }
-    $usage=1;
-    last;
   }
 }
 
-if ($opt_no_banner == 0 or defined $usage) {
-   print "Winemaker $version\n";
-   print "Copyright 2000 Francois Gouget <fgouget\@codeweavers.com> for CodeWeavers\n";
+if (!defined $opt_work_dir) {
+  print STDERR "error: you must specify the directory containing the sources to be converted\n";
+  usage();
+} elsif (!chdir $opt_work_dir) {
+  print STDERR "error: could not chdir to the work directory\n";
+  print STDERR "       $!\n";
+  usage();
 }
 
-if (defined $usage) {
-  print STDERR "Usage: winemaker [--nobanner] [--backup|--nobackup]\n";
-  print STDERR "                 [--lower-none|--lower-all|--lower-uppercase]\n";
-  print STDERR "                 [--guiexe|--windows|--cuiexe|--console|--dll]\n";
-  print STDERR "                 [--wrap|--nowrap] [--mfc|--nomfc]\n";
-  print STDERR "                 [-Dmacro[=defn]] [-Idir] [-Ldir] [-llibrary]\n";
-  print STDERR "                 [--interactive] [--single-target name]\n";
-  print STDERR "                 [--generated-files|--no-generated-files]\n";
-  exit (2);
+if ($opt_no_banner == 0) {
+  print_banner();
 }
 
 # Fix the file and directory names
@@ -1979,7 +2288,9 @@ source_scan();
 postprocess_targets();
 
 # Fix the source files
-fix_source();
+if (! $opt_no_source_fix) {
+  fix_source();
+}
 
 # Generate the Makefile and the spec file
 if (! $opt_no_generated_files) {
@@ -2009,76 +2320,18 @@ AC_PROG_MAKE_SET
 AC_PROG_CC
 AC_PROG_CXX
 AC_PROG_CPP
-AC_PATH_XTRA
-AC_PROG_RANLIB
 AC_PROG_LN_S
-AC_PATH_PROG(LDCONFIG, ldconfig, true, /sbin:/usr/sbin:$PATH)
 
 dnl **** Check for some libraries ****
 
 dnl Check for -lm for BeOS
 AC_CHECK_LIB(m,sqrt)
-dnl Check for -li386 for NetBSD and OpenBSD
-AC_CHECK_LIB(i386,i386_set_ldt)
-dnl Check for -lossaudio for NetBSD
-AC_CHECK_LIB(ossaudio,_oss_ioctl)
 dnl Check for -lw for Solaris
 AC_CHECK_LIB(w,iswalnum)
 dnl Check for -lnsl for Solaris
 AC_CHECK_FUNCS(gethostbyname,, AC_CHECK_LIB(nsl, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lnsl", AC_CHECK_LIB(socket, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lnsl", , -lnsl), -lsocket))
 dnl Check for -lsocket for Solaris
 AC_CHECK_FUNCS(connect,,AC_CHECK_LIB(socket,connect))
-dnl Check for -lxpg4 for FreeBSD
-AC_CHECK_LIB(xpg4,setrunelocale)
-dnl Check for -lmmap for OS/2
-AC_CHECK_LIB(mmap,mmap)
-dnl Check for openpty
-AC_CHECK_FUNCS(openpty,,
-       AC_CHECK_LIB(util,openpty,
-               AC_DEFINE(HAVE_OPENPTY)
-               LIBS="$LIBS -lutil"
-       ))
-
-AC_CHECK_HEADERS(dlfcn.h,
-       AC_CHECK_FUNCS(dlopen,
-               AC_DEFINE(HAVE_DL_API),
-               AC_CHECK_LIB(dl,dlopen,
-                       AC_DEFINE(HAVE_DL_API)
-                       LIBS="$LIBS -ldl",
-               )
-       ),
-)
-AC_SUBST(XLIB)
-AC_SUBST(X_DLLS)
-X_DLLS=""
-AC_SUBST(XFILES)
-XFILES=""
-
-dnl **** Check which curses lib to use ***
-if test "$CURSES" = "yes"
-then
-    AC_CHECK_HEADERS(ncurses.h)
-    if test "$ac_cv_header_ncurses_h" = "yes"
-    then 
-        AC_CHECK_LIB(ncurses,waddch)
-    fi
-    if test "$ac_cv_lib_ncurses_waddch" = "yes"
-    then
-        AC_CHECK_LIB(ncurses,resizeterm,AC_DEFINE(HAVE_RESIZETERM))
-        AC_CHECK_LIB(ncurses,getbkgd,AC_DEFINE(HAVE_GETBKGD))
-    else
-        AC_CHECK_HEADERS(curses.h)
-        if test "$ac_cv_header_curses_h" = "yes"
-        then    
-            AC_CHECK_LIB(curses,waddch)
-            if test "$ac_cv_lib_curses_waddch" = "yes"
-            then
-                AC_CHECK_LIB(curses,resizeterm,AC_DEFINE(HAVE_RESIZETERM))
-                AC_CHECK_LIB(curses,getbkgd,AC_DEFINE(HAVE_GETBKGD))
-            fi
-        fi
-    fi
-fi
 
 dnl **** If ln -s doesn't work, use cp instead ****
 if test "$ac_cv_prog_LN_S" = "ln -s"; then : ; else LN_S=cp ; fi
@@ -2127,38 +2380,46 @@ fi
 dnl **** Check for working dll ****
 
 LDSHARED=""
+LDXXSHARED=""
+LDDLLFLAGS=""
 AC_CACHE_CHECK("whether we can build a Linux dll",
                ac_cv_c_dll_linux,
 [saved_cflags=$CFLAGS
-CFLAGS="$CFLAGS -fPIC -shared -Wl,-soname,conftest.so.1.0"
+CFLAGS="$CFLAGS -fPIC -shared -Wl,-soname,conftest.so.1.0,-Bsymbolic"
 AC_TRY_LINK(,[return 1],ac_cv_c_dll_linux="yes",ac_cv_c_dll_linux="no")
 CFLAGS=$saved_cflags
 ])
 if test "$ac_cv_c_dll_linux" = "yes"
 then
-  LDSHARED="\$(CC) -shared -Wl,-soname,\$(SONAME),-rpath,\$(libdir)"
+  LDSHARED="\$(CC) -shared -Wl,-rpath,\$(libdir)"
+  LDXXSHARED="\$(CXX) -shared -Wl,-rpath,\$(libdir)"
+  LDDLLFLAGS="-Wl,-Bsymbolic"
 else
   AC_CACHE_CHECK(whether we can build a UnixWare (Solaris) dll,
                 ac_cv_c_dll_unixware,
   [saved_cflags=$CFLAGS
-  CFLAGS="$CFLAGS -fPIC -Wl,-G,-h,conftest.so.1.0"
+  CFLAGS="$CFLAGS -fPIC -Wl,-G,-h,conftest.so.1.0,-B,symbolic"
   AC_TRY_LINK(,[return 1],ac_cv_c_dll_unixware="yes",ac_cv_c_dll_unixware="no")
   CFLAGS=$saved_cflags
   ])
   if test "$ac_cv_c_dll_unixware" = "yes"
   then
-    LDSHARED="\$(CC) -Wl,-G,-h,\$(libdir)/\$(SONAME)"
+    LDSHARED="\$(CC) -Wl,-G"
+    LDXXSHARED="\$(CXX) -Wl,-G"
+    LDDLLFLAGS="-Wl,-B,symbolic"
   else
     AC_CACHE_CHECK("whether we can build a NetBSD dll",
                    ac_cv_c_dll_netbsd,
     [saved_cflags=$CFLAGS
-    CFLAGS="$CFLAGS -fPIC -Bshareable -Bforcearchive"
+    CFLAGS="$CFLAGS -fPIC -Wl,-Bshareable,-Bforcearchive"
     AC_TRY_LINK(,[return 1],ac_cv_c_dll_netbsd="yes",ac_cv_c_dll_netbsd="no")
     CFLAGS=$saved_cflags
     ])
     if test "$ac_cv_c_dll_netbsd" = "yes"
     then
-      LDSHARED="ld -Bshareable -Bforcearchive"
+      LDSHARED="\$(CC) -Wl,-Bshareable,-Bforcearchive"
+      LDXXSHARED="\$(CXX) -Wl,-Bshareable,-Bforcearchive"
+      LDDLLFLAGS="" #FIXME
     fi
   fi
 fi
@@ -2167,12 +2428,11 @@ then
   AC_MSG_ERROR([Could not find how to build a dynamically linked library])
 fi
 
-DLLFLAGS="-fPIC"
-DLL_LINK="\$(WINELIB_LIBRARY_PATH) \$(DLLS:%=-l%) \$(IMPORTS:%=-l%) -lwine -lwine_unicode"
+CFLAGS="$CFLAGS -fPIC"
 
-AC_SUBST(DLL_LINK)
-AC_SUBST(DLLFLAGS)
 AC_SUBST(LDSHARED)
+AC_SUBST(LDXXSHARED)
+AC_SUBST(LDDLLFLAGS)
 
 dnl *** check for the need to define __i386__
 
@@ -2227,134 +2487,295 @@ then
     CXXFLAGS="$CXXFLAGS -fno-for-scope"
   fi
 fi
+AC_LANG_C()
 
 dnl **** Test Winelib-related features of the C compiler
 dnl none for now
 
+dnl **** Macros for finding a headers/libraries in a collection of places
+
+dnl AC_PATH_FILE(variable,file,action-if-not-found,default-locations)
+AC_DEFUN(AC_PATH_FILE,[
+AC_MSG_CHECKING([for $2])
+AC_CACHE_VAL(ac_cv_pfile_$1,
+[
+  ac_found=
+  ac_dummy="ifelse([$4], , , [$4])"
+  IFS="${IFS=  }"; ac_save_ifs="$IFS"; IFS=":"
+  for ac_dir in $ac_dummy; do
+    IFS="$ac_save_ifs"
+    if test -z "$ac_dir"
+    then
+      ac_file="$2"
+    else
+      ac_file="$ac_dir/$2"
+    fi
+    if test -f "$ac_file"
+    then
+      ac_found=1
+      ac_cv_pfile_$1="$ac_dir"
+      break
+    fi
+  done
+  ifelse([$3],,,[if test -z "$ac_found"
+    then
+      $3
+    fi
+  ])
+])
+$1="$ac_cv_pfile_$1"
+if test -n "$ac_found" -o -n "[$]$1"
+then
+  AC_MSG_RESULT([$]$1)
+else
+  AC_MSG_RESULT(no)
+fi
+AC_SUBST($1)
+])
+
+dnl AC_PATH_HEADER(variable,header,action-if-not-found,default-locations)
+dnl Note that the above may set variable to an empty value if the header is 
+dnl already in the include path
+AC_DEFUN(AC_PATH_HEADER,[
+AC_MSG_CHECKING([for $2 header])
+AC_CACHE_VAL(ac_cv_pheader_$1,
+[
+  ac_found=
+  ac_dummy="ifelse([$4], , :/usr/local/include, [$4])"
+  save_CPPFLAGS="$CPPFLAGS"
+  IFS="${IFS=  }"; ac_save_ifs="$IFS"; IFS=":"
+  for ac_dir in $ac_dummy; do
+    IFS="$ac_save_ifs"
+    if test -z "$ac_dir"
+    then
+      CPPFLAGS="$save_CPPFLAGS"
+    else
+      CPPFLAGS="-I$ac_dir $save_CPPFLAGS"
+    fi
+    AC_TRY_COMPILE([#include <$2>],,ac_found=1;ac_cv_pheader_$1="$ac_dir";break)
+  done
+  CPPFLAGS="$save_CPPFLAGS"
+  ifelse([$3],,,[if test -z "$ac_found"
+    then
+      $3
+    fi
+  ])
+])
+$1="$ac_cv_pheader_$1"
+if test -n "$ac_found" -o -n "[$]$1"
+then
+  AC_MSG_RESULT([$]$1)
+else
+  AC_MSG_RESULT(no)
+fi
+AC_SUBST($1)
+])
+
+dnl AC_PATH_LIBRARY(variable,libraries,extra libs,action-if-not-found,default-locations)
+AC_DEFUN(AC_PATH_LIBRARY,[
+AC_MSG_CHECKING([for $2])
+AC_CACHE_VAL(ac_cv_plibrary_$1,
+[
+  ac_found=
+  ac_dummy="ifelse([$5], , :/usr/local/lib, [$5])"
+  save_LIBS="$LIBS"
+  IFS="${IFS=  }"; ac_save_ifs="$IFS"; IFS=":"
+  for ac_dir in $ac_dummy; do
+    IFS="$ac_save_ifs"
+    if test -z "$ac_dir"
+    then
+      LIBS="$2 $3 $save_LIBS"
+    else
+      LIBS="-L$ac_dir $2 $3 $save_LIBS"
+    fi
+    AC_TRY_LINK(,,ac_found=1;ac_cv_plibrary_$1="$ac_dir";break)
+  done
+  LIBS="$save_LIBS"
+  ifelse([$4],,,[if test -z "$ac_found"
+    then
+      $4
+    fi
+  ])
+])
+$1="$ac_cv_plibrary_$1"
+if test -n "$ac_found" -o -n "[$]$1"
+then
+  AC_MSG_RESULT([$]$1)
+else
+  AC_MSG_RESULT(no)
+fi
+AC_SUBST($1)
+])
+
 dnl **** Try to find where winelib is located ****
 
-dnl This section is implemented using custom code since autoconf does not 
-dnl provide a standard method. Here is how other people have tried to 
-dnl solve the same (or similar) problem.
-dnl See: http://www.geocrawler.com/archives/3/402/1999/4/50/2131449/
-dnl or   http://www.geocrawler.com/archives/3/402/1999/4/50/2131443/
-dnl      ftp://ftp.slac.stanford.edu/users/langston/autoconf/smr_macros-0.09.tar.gz
-dnl or   http://www.geocrawler.com/archives/3/402/1999/4/50/2131452/
-dnl or   http://www.geocrawler.com/archives/3/402/1999/4/50/2131446/
-
-WINELIB_INCLUDE_ROOT="";
-WINELIB_INCLUDE_PATH="";
-WINELIB_LIBRARY_ROOT="";
-WINELIB_LIBRARY_PATH="";
-WINELIB_TOOL_PATH="";
-WINEBUILD="";
-WRC="";
-
-AC_ARG_WITH(winelib-root,
-[  --with-winelib-root=DIR       take the Winelib includes, libraries and tools from this directory],
+LD_PATH=""
+WINE_INCLUDE_ROOT=""
+WINE_INCLUDE_PATH=""
+WINE_LIBRARY_ROOT=""
+WINE_LIBRARY_PATH=""
+WINE_DLL_ROOT=""
+WINE_DLL_PATH=""
+WINE_TOOL_PATH=""
+WINE=""
+WINEBUILD=""
+WRC=""
+
+AC_ARG_WITH(wine,
+[  --with-wine=DIR           the Wine package (or sources) is in DIR],
 [if test "$withval" != "no"; then
-  WINELIB_ROOT="$withval";
-  WINELIB_INCLUDES="";
-  WINELIB_LIBRARIES="";
-  WINELIB_TOOLS="";
+  WINE_ROOT="$withval";
+  WINE_INCLUDES="";
+  WINE_LIBRARIES="";
+  WINE_TOOLS="";
 else
-  WINELIB_ROOT="";
+  WINE_ROOT="";
 fi])
-if test -n "$WINELIB_ROOT"
+if test -n "$WINE_ROOT"
 then
-  WINELIB_INCLUDE_ROOT="$WINELIB_ROOT/include";
-  WINELIB_LIBRARY_ROOT="$WINELIB_ROOT";
-  WINELIB_TOOL_PATH="$WINELIB_ROOT:$WINELIB_ROOT/bin:$WINELIB_ROOT/tools/wrc:$WINELIB_ROOT/tools/winebuild:$PATH";
+  WINE_INCLUDE_ROOT="$WINE_ROOT/include:$WINE_ROOT/include/wine"
+  WINE_LIBRARY_ROOT="$WINE_ROOT:$WINE_ROOT/lib"
+  WINE_TOOL_PATH="$WINE_ROOT:$WINE_ROOT/bin:$WINE_ROOT/tools/wrc:$WINE_ROOT/tools/winebuild"
 fi
 
-AC_ARG_WITH(winelib-includes,
-[  --with-winelib-includes=DIR   take the Winelib includes from this directory],
+AC_ARG_WITH(wine-includes,
+[  --with-wine-includes=DIR  the Wine includes are in DIR],
 [if test "$withval" != "no"; then
-  WINELIB_INCLUDES="$withval";
+  WINE_INCLUDES="$withval";
 else
-  WINELIB_INCLUDES="";
+  WINE_INCLUDES="";
 fi])
-if test -n "$WINELIB_INCLUDES"
+if test -n "$WINE_INCLUDES"
 then
-  WINELIB_INCLUDE_ROOT="$WINELIB_INCLUDES";
+  WINE_INCLUDE_ROOT="$WINE_INCLUDES"
 fi
 
-AC_ARG_WITH(winelib-libraries,
-[  --with-winelib-libraries=DIR  take the Winelib libraries from this directory],
+AC_ARG_WITH(wine-libraries,
+[  --with-wine-libraries=DIR the Wine libraries are in DIR],
 [if test "$withval" != "no"; then
-  WINELIB_LIBRARIES="$withval";
+  WINE_LIBRARIES="$withval";
 else
-  WINELIB_LIBRARIES="";
+  WINE_LIBRARIES="";
 fi])
-if test -n "$WINELIB_LIBRARIES"
+if test -n "$WINE_LIBRARIES"
 then
-  WINELIB_LIBRARY_ROOT="$WINELIB_LIBRARIES";
+  WINE_LIBRARY_ROOT="$WINE_LIBRARIES"
 fi
 
-AC_ARG_WITH(winelib-tools,
-[  --with-winelib-tools=DIR      take the Winelib tools from this directory],
+AC_ARG_WITH(wine-dlls,
+[  --with-wine-dlls=DIR      the Wine dlls are in DIR],
 [if test "$withval" != "no"; then
-  WINELIB_TOOLS="$withval";
+  WINE_DLLS="$withval";
 else
-  WINELIB_TOOLS="";
+  WINE_DLLS="";
 fi])
-if test -n "$WINELIB_TOOLS"
+if test -n "$WINE_DLLS"
 then
-  WINELIB_TOOL_PATH="$WINELIB_TOOLS:$WINELIB_TOOLS/wrc:$WINELIB_TOOLS/winebuild";
+  WINE_DLL_ROOT="$WINE_DLLS"
 fi
 
-if test -z "$WINELIB_INCLUDE_ROOT"
+AC_ARG_WITH(wine-tools,
+[  --with-wine-tools=DIR     the Wine tools are in DIR],
+[if test "$withval" != "no"; then
+  WINE_TOOLS="$withval";
+else
+  WINE_TOOLS="";
+fi])
+if test -n "$WINE_TOOLS"
 then
-  WINELIB_INCLUDE_ROOT="/usr/include/wine";
+  WINE_TOOL_PATH="$WINE_TOOLS:$WINE_TOOLS/tools/wrc:$WINE_TOOLS/tools/winebuild"
 fi
-if test ! -f "$WINELIB_INCLUDE_ROOT/windows.h"
+
+if test -z "$WINE_INCLUDE_ROOT"
 then
-  AC_MSG_ERROR([Could not find the Winelib includes])
+  WINE_INCLUDE_ROOT=":/usr/include/wine:/usr/local/include/wine:/opt/wine/include:/opt/wine/include/wine";
+else
+  AC_PATH_FILE(WINE_INCLUDE_ROOT,[windef.h],[
+    AC_MSG_ERROR([Could not find the Wine headers (windef.h)])
+  ],$WINE_INCLUDE_ROOT)
+fi
+AC_PATH_HEADER(WINE_INCLUDE_ROOT,[windef.h],[
+  AC_MSG_ERROR([Could not include the Wine headers (windef.h)])
+],$WINE_INCLUDE_ROOT)
+if test -n "$WINE_INCLUDE_ROOT"
+then
+  WINE_INCLUDE_PATH="-I$WINE_INCLUDE_ROOT"
+else
+  WINE_INCLUDE_PATH=""
 fi
-WINELIB_INCLUDE_PATH="-I$WINELIB_INCLUDE_ROOT"
 
-if test -z "$WINELIB_LIBRARY_ROOT"
+if test -z "$WINE_LIBRARY_ROOT"
 then
-  WINELIB_LIBRARY_ROOT="/usr/lib/wine";
+  WINE_LIBRARY_ROOT=":/usr/lib/wine:/usr/local/lib:/usr/local/lib/wine:/opt/wine/lib"
+else
+  AC_PATH_FILE(WINE_LIBRARY_ROOT,[libwine.so],[
+    AC_MSG_ERROR([Could not find the Wine libraries (libwine.so)])
+  ],$WINE_LIBRARY_ROOT)
 fi
-if test ! -f "$WINELIB_LIBRARY_ROOT/libwine.so"
+AC_PATH_LIBRARY(WINE_LIBRARY_ROOT,[-lwine],[],[
+  AC_MSG_ERROR([Could not link with the Wine libraries (libwine.so)])
+],$WINE_LIBRARY_ROOT)
+if test -n "$WINE_LIBRARY_ROOT"
 then
-  if test -f "$WINELIB_LIBRARY_ROOT/lib/libwine.so"
+  WINE_LIBRARY_PATH="-L$WINE_LIBRARY_ROOT"
+  LD_PATH="$WINE_LIBRARY_ROOT"
+else
+  WINE_LIBRARY_PATH=""
+fi
+
+if test -z "$WINE_DLL_ROOT"
+then
+  if test -n "$WINE_LIBRARY_ROOT"
   then
-    WINELIB_LIBRARY_ROOT="$WINELIB_LIBRARY_ROOT/lib";
+    WINE_DLL_ROOT="$WINE_LIBRARY_ROOT:$WINE_LIBRARY_ROOT/dlls"
   else
-    AC_MSG_ERROR([Could not find the Winelib libraries (libwine)])
+    WINE_DLL_ROOT="/lib:/lib/dlls:/usr/lib:/usr/lib/dlls:/usr/local/lib:/usr/local/lib/dlls"
   fi
 fi
-if test -f "$WINELIB_LIBRARY_ROOT/libkernel32.so"
+AC_PATH_FILE(WINE_DLL_ROOT,[libntdll.dll.so],[
+  AC_MSG_ERROR([Could not find the Wine dlls (libntdll.dll.so)])
+],[$WINE_DLL_ROOT])
+
+AC_PATH_LIBRARY(WINE_DLL_ROOT,[-lntdll.dll],[$WINE_LIBRARY_PATH -lwine -lwine_unicode],[
+  AC_MSG_ERROR([Could not link with the Wine dlls (libntdll.dll.so)])
+],[$WINE_DLL_ROOT])
+WINE_DLL_PATH="-L$WINE_DLL_ROOT/wine"
+
+if test -n "$LD_PATH"
 then
-  WINELIB_LIBRARY_PATH="-L$WINELIB_LIBRARY_ROOT";
+  LD_PATH="$LD_PATH:$WINE_DLL_ROOT"
 else
-  if test -f "$WINELIB_LIBRARY_ROOT/dlls/libkernel32.so"
-  then
-    WINELIB_LIBRARY_PATH="-L$WINELIB_LIBRARY_ROOT -L$WINELIB_LIBRARY_ROOT/dlls";
-  else
-    AC_MSG_ERROR([Could not find the Winelib libraries (libkernel32)])
-  fi
+  LD_PATH="$WINE_DLL_ROOT"
 fi
+LD_PATH="LD_LIBRARY_PATH=\"$LD_PATH:\$\$LD_LIBRARY_PATH\""
 
-AC_PATH_PROG(WINEBUILD,winebuild,,$WINELIB_TOOL_PATH)
+if test -z "$WINE_TOOL_PATH"
+then
+  WINE_TOOL_PATH="$PATH:/usr/local/bin:/opt/wine/bin"
+fi
+AC_PATH_PROG(WINE,wine,,$WINE_TOOL_PATH)
+if test -z "$WINE"
+then
+  AC_MSG_ERROR([Could not find Wine's wine tool])
+fi
+AC_PATH_PROG(WINEBUILD,winebuild,,$WINE_TOOL_PATH)
 if test -z "$WINEBUILD"
 then
-  AC_MSG_ERROR([Could not find Winelib's winebuild tool])
+  AC_MSG_ERROR([Could not find Wine's winebuild tool])
 fi
-AC_PATH_PROG(WRC,wrc,,$WINELIB_TOOL_PATH)
+AC_PATH_PROG(WRC,wrc,,$WINE_TOOL_PATH)
 if test -z "$WRC"
 then
-  AC_MSG_ERROR([Could not find Winelib's wrc tool])
+  AC_MSG_ERROR([Could not find Wine's wrc tool])
 fi
 
-AC_SUBST(WINELIB_INCLUDE_ROOT)
-AC_SUBST(WINELIB_INCLUDE_PATH)
-AC_SUBST(WINELIB_LIBRARY_ROOT)
-AC_SUBST(WINELIB_LIBRARY_PATH)
+AC_SUBST(LD_PATH)
+AC_SUBST(WINE_INCLUDE_PATH)
+AC_SUBST(WINE_LIBRARY_PATH)
+AC_SUBST(WINE_DLL_PATH)
 
 dnl **** Try to find where the MFC are located ****
+AC_LANG_CPLUSPLUS()
 
 if test "x$NEEDS_MFC" = "x1"
 then
@@ -2365,8 +2786,8 @@ then
   MFC_LIBRARY_ROOT="";
   MFC_LIBRARY_PATH="";
 
-  AC_ARG_WITH(mfc-root,
-  [  --with-mfc-root=DIR           take the MFC includes and libraries from this directory],
+  AC_ARG_WITH(mfc,
+  [  --with-mfc=DIR            the MFC package (or sources) is in DIR],
   [if test "$withval" != "no"; then
     MFC_ROOT="$withval";
     ATL_INCLUDES="";
@@ -2383,7 +2804,7 @@ then
   fi
 
   AC_ARG_WITH(atl-includes,
-  [  --with-atl-includes=DIR       take the ATL includes from this directory],
+  [  --with-atl-includes=DIR   the ATL includes are in DIR],
   [if test "$withval" != "no"; then
     ATL_INCLUDES="$withval";
   else
@@ -2395,7 +2816,7 @@ then
   fi
 
   AC_ARG_WITH(mfc-includes,
-  [  --with-mfc-includes=DIR       take the MFC includes from this directory],
+  [  --with-mfc-includes=DIR   the MFC includes are in DIR],
   [if test "$withval" != "no"; then
     MFC_INCLUDES="$withval";
   else
@@ -2407,7 +2828,7 @@ then
   fi
 
   AC_ARG_WITH(mfc-libraries,
-  [  --with-mfc-libraries=DIR      take the MFC libraries from this directory],
+  [  --with-mfc-libraries=DIR  the MFC libraries are in DIR],
   [if test "$withval" != "no"; then
     MFC_LIBRARIES="$withval";
   else
@@ -2418,78 +2839,63 @@ then
     MFC_LIBRARY_ROOT="$MFC_LIBRARIES";
   fi
 
-  dnl FIXME: We should have an include path and just iterate through it.
-  dnl These tests become ugly.
+  OLDCPPFLAGS="$CPPFLAGS"
+  dnl FIXME: We should not have defines in any of the include paths
+  CPPFLAGS="$WINE_INCLUDE_PATH -I$WINE_INCLUDE_ROOT/mixedcrt -D_DLL -D_MT $CPPFLAGS"
+  ATL_INCLUDE_PATH="-I\$(WINE_INCLUDE_ROOT)/mixedcrt -D_DLL -D_MT"
   if test -z "$ATL_INCLUDE_ROOT"
   then
-    ATL_INCLUDE_ROOT="/usr/include";
+    ATL_INCLUDE_ROOT=":$WINE_INCLUDE_ROOT/atl:/usr/include/atl:/usr/local/include/atl:/opt/mfc/include/atl:/opt/atl/include"
+  else
+    ATL_INCLUDE_ROOT="$ATL_INCLUDE_ROOT:$ATL_INCLUDE_ROOT/atl:$ATL_INCLUDE_ROOT/atl/include"
   fi
-  if test ! -f "$ATL_INCLUDE_ROOT/atlbase.h"
+  AC_PATH_HEADER(ATL_INCLUDE_ROOT,atldef.h,[
+    AC_MSG_ERROR([Could not find the ATL includes])
+  ],$ATL_INCLUDE_ROOT)
+  if test -n "$ATL_INCLUDE_ROOT"
   then
-    if test -f "$ATL_INCLUDE_ROOT/atl/atlbase.h"
-    then
-      ATL_INCLUDE_ROOT="$ATL_INCLUDE_ROOT/atl"
-    else
-      if test -f "$ATL_INCLUDE_ROOT/atl/include/atlbase.h"
-      then
-        ATL_INCLUDE_ROOT="$ATL_INCLUDE_ROOT/atl/include"
-      else
-        AC_MSG_ERROR([Could not find the ATL includes])
-      fi
-    fi
+    ATL_INCLUDE_PATH="$ATL_INCLUDE_PATH -I$ATL_INCLUDE_ROOT"
   fi
-  ATL_INCLUDE_PATH="-I$ATL_INCLUDE_ROOT"
 
+  MFC_INCLUDE_PATH="$ATL_INCLUDE_PATH"
   if test -z "$MFC_INCLUDE_ROOT"
   then
-    MFC_INCLUDE_ROOT="/usr/include";
+    MFC_INCLUDE_ROOT=":$WINE_INCLUDE_ROOT/mfc:/usr/include/mfc:/usr/local/include/mfc:/opt/mfc/include/mfc:/opt/mfc/include"
+  else
+    MFC_INCLUDE_ROOT="$MFC_INCLUDE_ROOT:$MFC_INCLUDE_ROOT/mfc:$MFC_INCLUDE_ROOT/mfc/include"
   fi
-  if test ! -f "$MFC_INCLUDE_ROOT/afx.h"
+  AC_PATH_HEADER(MFC_INCLUDE_ROOT,afx.h,[
+    AC_MSG_ERROR([Could not find the MFC includes])
+  ],$MFC_INCLUDE_ROOT)
+  if test -n "$MFC_INCLUDE_ROOT" -a "$ATL_INCLUDE_ROOT" != "$MFC_INCLUDE_ROOT"
   then
-    if test -f "$MFC_INCLUDE_ROOT/mfc/afx.h"
-    then
-      MFC_INCLUDE_ROOT="$MFC_INCLUDE_ROOT/mfc"
-    else
-      if test -f "$MFC_INCLUDE_ROOT/mfc/include/afx.h"
-      then
-        MFC_INCLUDE_ROOT="$MFC_INCLUDE_ROOT/mfc/include"
-      else
-        AC_MSG_ERROR([Could not find the MFC includes])
-      fi
-    fi
+    MFC_INCLUDE_PATH="$MFC_INCLUDE_PATH -I$MFC_INCLUDE_ROOT"
   fi
-  MFC_INCLUDE_PATH="-D_DLL -D_MT -I$ATL_INCLUDE_ROOT -I$MFC_INCLUDE_ROOT -I\$(WINELIB_INCLUDE_ROOT)/mixedcrt"
+  CPPFLAGS="$OLDCPPFLAGS"
 
   if test -z "$MFC_LIBRARY_ROOT"
   then
-    MFC_LIBRARY_ROOT="/usr/lib/mfc";
+    MFC_LIBRARY_ROOT=":$WINE_LIBRARY_ROOT:/usr/lib/mfc:/usr/local/lib:/usr/local/lib/mfc:/opt/mfc/lib";
+  else
+    MFC_LIBRARY_ROOT="$MFC_LIBRARY_ROOT:$MFC_LIBRARY_ROOT/lib:$MFC_LIBRARY_ROOT/mfc/src";
   fi
-  if test -f "$MFC_LIBRARY_ROOT/libmfc.so"
+  AC_PATH_LIBRARY(MFC_LIBRARY_ROOT,[-lmfc],[$WINE_LIBRARY_PATH -lwine -lwine_unicode],[
+    AC_MSG_ERROR([Could not find the MFC library])
+  ],$MFC_LIBRARY_ROOT)
+  if test -n "$MFC_LIBRARY_ROOT" -a "$MFC_LIBRARY_ROOT" != "$WINE_LIBRARY_ROOT"
   then
-    MFC_LIBRARY_ROOT="$MFC_LIBRARY_ROOT";
+    MFC_LIBRARY_PATH="-L$MFC_LIBRARY_ROOT"
   else
-    if test -f "$MFC_LIBRARY_ROOT/lib/libmfc.so"
-    then
-      MFC_LIBRARY_ROOT="$MFC_LIBRARY_ROOT/lib";
-    else
-      if test -f "$MFC_LIBRARY_ROOT/mfc/src/libmfc.so"
-      then
-        MFC_LIBRARY_ROOT="$MFC_LIBRARY_ROOT/mfc/src";
-      else
-        AC_MSG_ERROR([Could not find the MFC library (libmfc)])
-      fi
-    fi
+    MFC_LIBRARY_PATH=""
   fi
-  MFC_LIBRARY_PATH="-L$MFC_LIBRARY_ROOT"
 
-  AC_SUBST(ATL_INCLUDE_ROOT)
   AC_SUBST(ATL_INCLUDE_PATH)
-  AC_SUBST(MFC_INCLUDE_ROOT)
   AC_SUBST(MFC_INCLUDE_PATH)
-  AC_SUBST(MFC_LIBRARY_ROOT)
   AC_SUBST(MFC_LIBRARY_PATH)
 fi
 
+AC_LANG_C()
+
 dnl **** Generate output files ****
 
 MAKE_RULES=Make.rules
@@ -2517,16 +2923,14 @@ dnl End:
 # Global rules shared by all makefiles     -*-Makefile-*-
 #
 # Each individual makefile must define the following variables:
-# WINELIB_INCLUDE_ROOT: Winelib includes location
-# WINELIB_LIBRARY_ROOT: Winelib libraries location
 # TOPOBJDIR    : top-level object directory
 # SRCDIR       : source directory for this module
 #
 # Each individual makefile may define the following additional variables:
 #
 # SUBDIRS      : subdirectories that contain a Makefile
-# LIBRARIES    : libraries to be built
-# PROGRAMS     : programs to be built
+# DLLS         : WineLib libraries to be built
+# EXES         : WineLib executables to be built
 #
 # CEXTRA       : extra c flags (e.g. '-Wall')
 # CXXEXTRA     : extra c++ flags (e.g. '-Wall')
@@ -2534,7 +2938,7 @@ dnl End:
 # DEFINES      : defines (e.g. -DSTRICT)
 # INCLUDE_PATH : additional include path
 # LIBRARY_PATH : additional library path
-# IMPORTS      : additional libraries to link with
+# LIBRARIES    : additional Unix libraries to link with
 #
 # C_SRCS       : C sources for the module
 # CXX_SRCS     : C++ sources for the module
@@ -2542,12 +2946,16 @@ dnl End:
 # SPEC_SRCS    : interface definition files
 
 
-# Where is Winelib
+# Where is Wine
 
-WINELIB_INCLUDE_ROOT = @WINELIB_INCLUDE_ROOT@
-WINELIB_INCLUDE_PATH = @WINELIB_INCLUDE_PATH@
-WINELIB_LIBRARY_ROOT = @WINELIB_LIBRARY_ROOT@
-WINELIB_LIBRARY_PATH = @WINELIB_LIBRARY_PATH@
+WINE_INCLUDE_ROOT = @WINE_INCLUDE_ROOT@
+WINE_INCLUDE_PATH = @WINE_INCLUDE_PATH@
+WINE_LIBRARY_ROOT = @WINE_LIBRARY_ROOT@
+WINE_LIBRARY_PATH = @WINE_LIBRARY_PATH@
+WINE_DLL_ROOT     = @WINE_DLL_ROOT@
+WINE_DLL_PATH     = @WINE_DLL_PATH@
+
+LD_PATH           = @LD_PATH@
 
 # Where are the MFC
 
@@ -2563,36 +2971,37 @@ MFC_LIBRARY_PATH = @MFC_LIBRARY_PATH@
 SHELL     = /bin/sh
 CC        = @CC@
 CPP       = @CPP@
+CXX       = @CXX@
+WRC       = @WRC@
 CFLAGS    = @CFLAGS@
 CXXFLAGS  = @CXXFLAGS@
+WRCFLAGS  = -r -L
 OPTIONS   = @OPTIONS@ -D_REENTRANT -DWINELIB
-X_CFLAGS  = @X_CFLAGS@
-X_LIBS    = @X_LIBS@
-XLIB      = @X_PRE_LIBS@ @XLIB@ @X_EXTRA_LIBS@
-DLL_LINK  = @DLL_LINK@
 LIBS      = @LIBS@ $(LIBRARY_PATH)
-YACC      = @YACC@
-LEX       = @LEX@
-LEXLIB    = @LEXLIB@
 LN_S      = @LN_S@
-DIVINCL   = -I$(SRCDIR) $(WINELIB_INCLUDE_PATH) $(INCLUDE_PATH)
-ALLCFLAGS = $(DIVINCL) $(CFLAGS) $(CEXTRA) $(OPTIONS) $(X_CFLAGS) $(DEFINES)
-ALLCXXFLAGS = $(DIVINCL) $(CXXFLAGS) $(CXXEXTRA) $(OPTIONS) $(X_CFLAGS) $(DEFINES)
+ALLFLAGS  = $(DEFINES) -I$(SRCDIR) $(INCLUDE_PATH) $(WINE_INCLUDE_PATH)
+ALLCFLAGS = $(CFLAGS) $(CEXTRA) $(OPTIONS) $(ALLFLAGS)
+ALLCXXFLAGS=$(CXXFLAGS) $(CXXEXTRA) $(OPTIONS) $(ALLFLAGS)
+ALLWRCFLAGS=$(WRCFLAGS) $(WRCEXTRA) $(OPTIONS) $(ALLFLAGS)
+DLL_LINK  = $(LIBRARY_PATH) $(LIBRARIES:%=-l%) $(WINE_LIBRARY_PATH) -lwine -lwine_unicode -lwine_uuid
 LDCOMBINE = ld -r
 LDSHARED  = @LDSHARED@
+LDXXSHARED= @LDXXSHARED@
+LDDLLFLAGS= @LDDLLFLAGS@
+STRIP     = strip
+STRIPFLAGS= --strip-unneeded
 RM        = rm -f
 MV        = mv
 MKDIR     = mkdir -p
+WINE      = @WINE@
 WINEBUILD = @WINEBUILD@
-WRC       = @WRC@
-WRCFLAGS  = -r -L
 @SET_MAKE@
 
 # Installation infos
 
-INSTALL         = @INSTALL@
-INSTALL_PROGRAM = @INSTALL_PROGRAM@
-INSTALL_DATA    = @INSTALL_DATA@
+INSTALL         = install
+INSTALL_PROGRAM = $(INSTALL)
+INSTALL_DATA    = $(INSTALL) -m 644
 prefix          = @prefix@
 exec_prefix     = @exec_prefix@
 bindir          = @bindir@
@@ -2601,110 +3010,28 @@ infodir         = @infodir@
 mandir          = @mandir@
 prog_manext     = 1
 conf_manext     = 5
-CLEAN_FILES     = *.o *.a *.so \\\#*\\\# *~ *% .\\\#* *.orig *.rej \
-                  *.spec.c y.tab.c y.tab.h lex.yy.c core
-
-OBJS = $(SPEC_SRCS:.spec=.spec.o) $(C_SRCS:.c=.o) $(CXX_SRCS:.cpp=.o)
-
-# DLL list
-
-X_DLLS = \
-       ddraw \
-       x11drv
-
-DLLS = \
-       @X_DLLS@ \
-       advapi32 \
-       avifil32 \
-       comctl32 \
-       comdlg32 \
-       crtdll \
-       dciman32 \
-       dinput \
-       dplay \
-       dplayx \
-       dsound \
-       gdi32 \
-       imagehlp \
-       imm32 \
-       joystick.drv \
-       kernel32 \
-       lz32 \
-       mcianim.drv \
-       mciavi.drv \
-       mcicda.drv \
-       mciseq.drv \
-       mciwave.drv \
-       midimap.drv \
-       mpr \
-       msacm.drv \
-       msacm32 \
-       msnet32 \
-       msvfw32 \
-       odbc32 \
-       ole32 \
-       oleaut32 \
-       olecli32 \
-       oledlg \
-       olepro32 \
-       olesvr32 \
-       psapi \
-       rasapi32 \
-       riched32 \
-       rpcrt4 \
-       serialui \
-       shell32 \
-       shfolder \
-       shlwapi \
-       tapi32 \
-       ttydrv \
-       urlmon \
-       user32 \
-       version \
-       w32skrnl \
-       wineoss.drv \
-       wineps \
-       wininet \
-       winmm \
-       winspool.drv \
-       wnaspi32 \
-       wow32 \
-       ws2_32 \
-       wsock32
+
+OBJS            = $(C_SRCS:.c=.o) $(CXX_SRCS:.cpp=.o) \
+                  $(SPEC_SRCS:.spec=.spec.o) 
+CLEAN_FILES     = *.spec.c y.tab.c y.tab.h lex.yy.c \
+                  core *.orig *.rej \
+                  \\\#*\\\# *~ *% .\\\#*
 
 # Implicit rules
 
-.SUFFIXES: .C .cpp .CPP .cxx .CXX .rc .RC .res .spec .spec.c .spec.o
+.SUFFIXES: .cpp .rc .res .tmp.o .spec .spec.c .spec.o
 
 .c.o:
        $(CC) -c $(ALLCFLAGS) -o $@ $<
 
-.C.o:
-       $(CC) -c $(ALLCFLAGS) -o $@ $<
-
 .cpp.o:
        $(CXX) -c $(ALLCXXFLAGS) -o $@ $<
 
-.CPP.o:
-       $(CXX) -c $(ALLCXXFLAGS) -o $@ $<
-
 .cxx.o:
        $(CXX) -c $(ALLCXXFLAGS) -o $@ $<
 
-.CXX.o:
-       $(CXX) -c $(ALLCXXFLAGS) -o $@ $<
-
-.spec.spec.c:
-       $(WINEBUILD) @DLLFLAGS@ -o $@ -spec $<
-
-.spec.c.spec.o:
-       $(CC) -c $(ALLCFLAGS) @GCC_NO_BUILTIN@ -o $@ $<
-
 .rc.res:
-       $(WRC) $(WRCFLAGS) $(WRCEXTRA) $(DIVINCL) -o $@ $<
-
-.RC.res:
-       $(WRC) $(WRCFLAGS) $(WRCEXTRA) $(DIVINCL) -o $@ $<
+       $(LD_PATH) $(WRC) $(ALLWRCFLAGS) -o $@ $<
 
 .PHONY: all install uninstall clean distclean depend dummy
 
@@ -2727,7 +3054,7 @@ $(EXTRASUBDIRS:%=%/__clean__): dummy
        -cd `dirname $@` && $(RM) $(CLEAN_FILES)
 
 clean:: $(SUBDIRS:%=%/__clean__) $(EXTRASUBDIRS:%=%/__clean__)
-       $(RM) $(CLEAN_FILES) $(RC_SRCS:.rc=.res) $(LIBRARIES) $(PROGRAMS)
+       $(RM) $(CLEAN_FILES) $(RC_SRCS:.rc=.res) $(OBJS) $(SPEC_SRCS:.spec=.tmp.o) $(EXES) $(EXES:%=%.so) $(DLLS)
 
 # Rules for installing
 
@@ -2750,6 +3077,10 @@ dummy:
  * Copyright 2000 Francois Gouget <fgouget@codeweavers.com> for CodeWeavers
  */
 
+#ifndef STRICT
+#define STRICT
+#endif
+
 #include <dlfcn.h>
 #include <windows.h>