Add support for a new environment variable ROS_GENERATE_RSYM to disable RSYM during...
[reactos.git] / reactos / tools / rbuild / backend / mingw / modulehandler.cpp
index 05d9c63..fff325d 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005 Casper S. Hornstrup
+ *               2007-2008 HervĂ© Poussineau
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 #include "../../pch.h"
 #include <assert.h>
+#include <algorithm>
 
 #include "../../rbuild.h"
 #include "mingw.h"
 #include "modulehandler.h"
+#include "rule.h"
 
+using std::set;
 using std::string;
 using std::vector;
 
 #define CLEAN_FILE(f) clean_files.push_back ( (f).name.length () > 0 ? backend->GetFullName ( f ) : backend->GetFullPath ( f ) );
+#define IsStaticLibrary( module ) ( ( module.type == StaticLibrary ) || ( module.type == HostStaticLibrary ) )
 
 MingwBackend*
 MingwModuleHandler::backend = NULL;
@@ -121,7 +126,8 @@ MingwModuleHandler::PassThruCacheDirectory (const FileLocation* file )
        return file;
 }
 
-/*static*/ const FileLocation*
+/* caller needs to delete the returned object */
+const FileLocation*
 MingwModuleHandler::GetTargetFilename (
        const Module& module,
        string_list* pclean_files )
@@ -135,7 +141,8 @@ MingwModuleHandler::GetTargetFilename (
        return target;
 }
 
-/*static*/ const FileLocation*
+/* caller needs to delete the returned object */
+const FileLocation*
 MingwModuleHandler::GetImportLibraryFilename (
        const Module& module,
        string_list* pclean_files )
@@ -149,7 +156,8 @@ MingwModuleHandler::GetImportLibraryFilename (
        return target;
 }
 
-/*static*/ MingwModuleHandler*
+/* caller needs to delete the returned object */
+MingwModuleHandler*
 MingwModuleHandler::InstanciateHandler (
        const Module& module,
        MingwBackend* backend )
@@ -163,6 +171,9 @@ MingwModuleHandler::InstanciateHandler (
                case StaticLibrary:
                        handler = new MingwStaticLibraryModuleHandler ( module );
                        break;
+               case HostStaticLibrary:
+                       handler = new MingwHostStaticLibraryModuleHandler ( module );
+                       break;
                case ObjectLibrary:
                        handler = new MingwObjectLibraryModuleHandler ( module );
                        break;
@@ -179,6 +190,7 @@ MingwModuleHandler::InstanciateHandler (
                case Win32GUI:
                        handler = new MingwWin32GUIModuleHandler ( module );
                        break;
+               case KeyboardLayout:
                case KernelModeDLL:
                        handler = new MingwKernelModeDLLModuleHandler ( module );
                        break;
@@ -188,7 +200,7 @@ MingwModuleHandler::InstanciateHandler (
                case Win32DLL:
                        handler = new MingwWin32DLLModuleHandler ( module );
                        break;
-               case Win32OCX: 
+               case Win32OCX:
                        handler = new MingwWin32OCXModuleHandler ( module );
                        break;
                case KernelModeDriver:
@@ -224,12 +236,21 @@ MingwModuleHandler::InstanciateHandler (
                case RpcClient:
                        handler = new MingwRpcClientModuleHandler ( module );
                        break;
+               case RpcProxy:
+                       handler = new MingwRpcProxyModuleHandler ( module );
+                       break;
                case Alias:
                        handler = new MingwAliasModuleHandler ( module );
                        break;
+               case MessageHeader:
+                       handler = new MingwMessageHeaderModuleHandler (module);
+                       break;
                case IdlHeader:
                        handler = new MingwIdlHeaderModuleHandler ( module );
                        break;
+               case Cabinet:
+                       handler = new MingwCabinetModuleHandler ( module );
+                       break;
                case EmbeddedTypeLib:
                        handler = new MingwEmbeddedTypeLibModuleHandler ( module );
                        break;
@@ -260,47 +281,6 @@ MingwModuleHandler::GetBasename ( const string& filename ) const
        return "";
 }
 
-const FileLocation*
-MingwModuleHandler::GetActualSourceFilename (
-       const FileLocation* file ) const
-{
-       string filename = file->name;
-
-       string extension = GetExtension ( *file );
-       if ( extension == ".spec" || extension == ".SPEC" )
-       {
-               string basename = GetBasename ( filename );
-               return new FileLocation (
-                       IntermediateDirectory,
-                       file->relative_path,
-                       basename + ".stubs.c" );
-       }
-       else if ( extension == ".idl" || extension == ".IDL" )
-       {
-               string basename = GetBasename ( filename );
-               if ( module.type == RpcServer )
-               {
-                       return new FileLocation (
-                               IntermediateDirectory,
-                               file->relative_path,
-                               basename + "_s.c" );
-               }
-               else if ( module.type == RpcClient )
-               {
-                       return new FileLocation (
-                               IntermediateDirectory,
-                               file->relative_path,
-                               basename + "_c.c" );
-               }
-               else
-               {
-                       return new FileLocation ( *file );
-               }
-       }
-       else
-               return new FileLocation ( *file );
-}
-
 string
 MingwModuleHandler::GetExtraDependencies (
        const FileLocation *file ) const
@@ -308,14 +288,21 @@ MingwModuleHandler::GetExtraDependencies (
        string extension = GetExtension ( *file );
        if ( extension == ".idl" || extension == ".IDL" )
        {
-               if ( (module.type == RpcServer) || (module.type == RpcClient) )
-                       return backend->GetFullName ( *GetRpcServerHeaderFilename ( file ) )
-                            + " "
-                            + backend->GetFullName ( *GetRpcClientHeaderFilename ( file ) );
-               else if ( module.type == IdlHeader )
-                       return backend->GetFullName ( *GetIdlHeaderFilename ( file ) );
-               else
+               const FileLocation *header;
+               switch ( module.type )
+               {
+                       case RpcServer: header = GetRpcServerHeaderFilename ( file ); break;
+                       case RpcClient: header = GetRpcClientHeaderFilename ( file ); break;
+                       case RpcProxy: header = GetRpcProxyHeaderFilename ( file ); break;
+                       case IdlHeader: header = GetIdlHeaderFilename ( file ); break;
+                       default: header = NULL; break;
+               }
+               if ( !header )
                        return "";
+
+               string dependencies = backend->GetFullName ( *header );
+               delete header;
+               return " " + dependencies;
        }
        else
                return "";
@@ -325,22 +312,23 @@ string
 MingwModuleHandler::GetCompilationUnitDependencies (
        const CompilationUnit& compilationUnit ) const
 {
-       if ( compilationUnit.files.size () <= 1 )
+       if ( compilationUnit.GetFiles ().size () <= 1 )
                return "";
        vector<string> sourceFiles;
-       for ( size_t i = 0; i < compilationUnit.files.size (); i++ )
+       for ( size_t i = 0; i < compilationUnit.GetFiles ().size (); i++ )
        {
-               File& file = *compilationUnit.files[i];
+               const File& file = *compilationUnit.GetFiles ()[i];
                sourceFiles.push_back ( backend->GetFullName ( file.file ) );
        }
-       return v2s ( sourceFiles, 10 );
+       return string ( " " ) + v2s ( sourceFiles, 10 );
 }
 
+/* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetModuleArchiveFilename () const
 {
-       if ( module.type == StaticLibrary )
-               return new FileLocation ( *GetTargetFilename ( module, NULL ) );
+       if ( IsStaticLibrary ( module ) )
+               return GetTargetFilename ( module, NULL );
        return new FileLocation ( IntermediateDirectory,
                                  module.output->relative_path,
                                  ReplaceExtension ( module.name, ".temp.a" ) );
@@ -363,8 +351,12 @@ MingwModuleHandler::ReferenceObjects (
                return true;
        if ( module.type == RpcClient )
                return true;
+       if ( module.type == RpcProxy )
+               return true;
        if ( module.type == IdlHeader )
                return true;
+       if ( module.type == MessageHeader)
+               return true;
        return false;
 }
 
@@ -386,9 +378,28 @@ MingwModuleHandler::GetImportLibraryDependency (
 {
        string dep;
        if ( ReferenceObjects ( importedModule ) )
+       {
+               const vector<CompilationUnit*>& compilationUnits = importedModule.non_if_data.compilationUnits;
+               size_t i;
+
                dep = GetTargetMacro ( importedModule );
+               for ( i = 0; i < compilationUnits.size (); i++ )
+               {
+                       CompilationUnit& compilationUnit = *compilationUnits[i];
+                       const FileLocation& compilationName = compilationUnit.GetFilename ();
+                       const FileLocation *objectFilename = GetObjectFilename ( &compilationName, importedModule );
+                       if ( GetExtension ( *objectFilename ) == ".h" )
+                               dep += ssprintf ( " $(%s_HEADERS)", importedModule.name.c_str () );
+                       else if ( GetExtension ( *objectFilename ) == ".rc" )
+                               dep += ssprintf ( " $(%s_MCHEADERS)", importedModule.name.c_str () );
+               }
+       }
        else
-               dep = backend->GetFullName ( *GetImportLibraryFilename ( importedModule, NULL ) );
+       {
+               const FileLocation *library_target = GetImportLibraryFilename ( importedModule, NULL );
+               dep = backend->GetFullName ( *library_target );
+               delete library_target;
+       }
        return dep;
 }
 
@@ -426,6 +437,7 @@ MingwModuleHandler::GetModuleDependencies (
        }
        vector<FileLocation> v;
        GetDefinitionDependencies ( v );
+
        for ( size_t i = 0; i < v.size (); i++ )
        {
                const FileLocation& file = v[i];
@@ -433,64 +445,22 @@ MingwModuleHandler::GetModuleDependencies (
        }
 }
 
-void
-MingwModuleHandler::GetSourceFilenames ( vector<FileLocation>& list,
-                                         bool includeGeneratedFiles ) const
-{
-       size_t i;
-
-       const vector<CompilationUnit*>& compilationUnits = module.non_if_data.compilationUnits;
-       for ( i = 0; i < compilationUnits.size (); i++ )
-       {
-               if ( includeGeneratedFiles || !compilationUnits[i]->IsGeneratedFile () )
-               {
-                       const FileLocation* sourceFileLocation = GetActualSourceFilename (
-                               compilationUnits[i]->GetFilename () );
-                       list.push_back ( *sourceFileLocation );
-               }
-       }
-       // intentionally make a copy so that we can append more work in
-       // the middle of processing without having to go recursive
-       vector<If*> v = module.non_if_data.ifs;
-       for ( i = 0; i < v.size (); i++ )
-       {
-               size_t j;
-               If& rIf = *v[i];
-               // check for sub-ifs to add to list
-               const vector<If*>& ifs = rIf.data.ifs;
-               for ( j = 0; j < ifs.size (); j++ )
-                       v.push_back ( ifs[j] );
-               const vector<CompilationUnit*>& compilationUnits = rIf.data.compilationUnits;
-               for ( j = 0; j < compilationUnits.size (); j++ )
-               {
-                       CompilationUnit& compilationUnit = *compilationUnits[j];
-                       if ( includeGeneratedFiles || !compilationUnit.IsGeneratedFile () )
-                       {
-                               const FileLocation* sourceFileLocation = GetActualSourceFilename (
-                                       compilationUnit.GetFilename () );
-                               list.push_back ( *sourceFileLocation );
-                       }
-               }
-       }
-}
-
-void
-MingwModuleHandler::GetSourceFilenamesWithoutGeneratedFiles (
-       vector<FileLocation>& list ) const
-{
-       GetSourceFilenames ( list, false );
-}
-
+/* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetObjectFilename (
        const FileLocation* sourceFile,
-       string_list* pclean_files ) const
+       const Module& module ) const
 {
        DirectoryLocation destination_directory;
        string newExtension;
        string extension = GetExtension ( *sourceFile );
-       if ( extension == ".rc" || extension == ".RC" )
-               newExtension = ".coff";
+
+       if ( module.type == BootSector )
+               return new FileLocation ( *module.output );
+       else if ( extension == ".rc" || extension == ".RC" )
+               newExtension = "_" + module.name + ".coff";
+       else if ( extension == ".mc" || extension == ".MC" )
+               newExtension = ".rc";
        else if ( extension == ".spec" || extension == ".SPEC" )
                newExtension = ".stubs.o";
        else if ( extension == ".idl" || extension == ".IDL" )
@@ -499,11 +469,13 @@ MingwModuleHandler::GetObjectFilename (
                        newExtension = "_s.o";
                else if ( module.type == RpcClient )
                        newExtension = "_c.o";
+               else if ( module.type == RpcProxy )
+                       newExtension = "_p.o";
                else
-                       return NULL;
+                       newExtension = ".h";
        }
        else
-               newExtension = ".o";
+               newExtension = "_" + module.name + ".o";
 
        if ( module.type == BootSector )
                destination_directory = OutputDirectory;
@@ -514,12 +486,8 @@ MingwModuleHandler::GetObjectFilename (
                destination_directory,
                sourceFile->relative_path,
                ReplaceExtension ( sourceFile->name, newExtension ) );
+       PassThruCacheDirectory ( obj_file );
 
-       if ( pclean_files )
-       {
-               string_list& clean_files = *pclean_files;
-               CLEAN_FILE ( *obj_file );
-       }
        return obj_file;
 }
 
@@ -562,6 +530,23 @@ MingwModuleHandler::GenerateCleanTarget () const
                fprintf ( fMakefile, " %s", clean_files[i].c_str() );
        }
        fprintf ( fMakefile, " 2>$(NUL)\n" );
+
+       if( ProxyMakefile::GenerateProxyMakefile(module) )
+       {
+               DirectoryLocation root;
+
+               if ( backend->configuration.GenerateProxyMakefilesInSourceTree )
+                       root = SourceDirectory;
+               else
+                       root = OutputDirectory;
+
+               FileLocation proxyMakefile ( root,
+                                            module.output->relative_path,
+                                           "GNUmakefile" );
+               fprintf ( fMakefile, "\t-@${rm} %s 2>$(NUL)\n",
+                         backend->GetFullName ( proxyMakefile ).c_str () );
+       }
+
        fprintf ( fMakefile, "clean: %s_clean\n\n", module.name.c_str() );
 }
 
@@ -605,21 +590,30 @@ MingwModuleHandler::GetObjectFilenames ()
        {
                if ( objectFilenames.size () > 0 )
                        objectFilenames += " ";
-               objectFilenames += backend->GetFullName ( *GetObjectFilename ( compilationUnits[i]->GetFilename (), NULL ) );
+               const FileLocation& compilationName = compilationUnits[i]->GetFilename ();
+               const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
+               objectFilenames += backend->GetFullName ( *object_file );
+               delete object_file;
        }
        return objectFilenames;
 }
 
 /* static */ string
 MingwModuleHandler::GenerateGccDefineParametersFromVector (
-       const vector<Define*>& defines )
+       const vector<Define*>& defines,
+       set<string>& used_defs)
 {
        string parameters;
+
        for ( size_t i = 0; i < defines.size (); i++ )
        {
                Define& define = *defines[i];
+               if (used_defs.find(define.name) != used_defs.end())
+                       continue;
                if (parameters.length () > 0)
                        parameters += " ";
+               if (define.name.find('(') != string::npos)
+                       parameters += "$(QT)";
                parameters += "-D";
                parameters += define.name;
                if (define.value.length () > 0)
@@ -627,6 +621,9 @@ MingwModuleHandler::GenerateGccDefineParametersFromVector (
                        parameters += "=";
                        parameters += define.value;
                }
+               if (define.name.find('(') != string::npos)
+                       parameters += "$(QT)";
+               used_defs.insert(used_defs.begin(),define.name);
        }
        return parameters;
 }
@@ -634,8 +631,9 @@ MingwModuleHandler::GenerateGccDefineParametersFromVector (
 string
 MingwModuleHandler::GenerateGccDefineParameters () const
 {
-       string parameters = GenerateGccDefineParametersFromVector ( module.project.non_if_data.defines );
-       string s = GenerateGccDefineParametersFromVector ( module.non_if_data.defines );
+       set<string> used_defs;
+       string parameters = GenerateGccDefineParametersFromVector ( module.project.non_if_data.defines, used_defs );
+       string s = GenerateGccDefineParametersFromVector ( module.non_if_data.defines, used_defs );
        if ( s.length () > 0 )
        {
                parameters += " ";
@@ -685,15 +683,14 @@ MingwModuleHandler::GenerateGccIncludeParameters () const
 }
 
 string
-MingwModuleHandler::GenerateCompilerParametersFromVector ( const vector<CompilerFlag*>& compilerFlags ) const
+MingwModuleHandler::GenerateCompilerParametersFromVector ( const vector<CompilerFlag*>& compilerFlags, const CompilerType type ) const
 {
        string parameters;
        for ( size_t i = 0; i < compilerFlags.size (); i++ )
        {
                CompilerFlag& compilerFlag = *compilerFlags[i];
-               if ( parameters.length () > 0 )
-                       parameters += " ";
-               parameters += compilerFlag.flag;
+               if ( compilerFlag.compiler == type )
+                       parameters += " " + compilerFlag.flag;
        }
        return parameters;
 }
@@ -739,12 +736,17 @@ void
 MingwModuleHandler::GenerateMacro (
        const char* assignmentOperation,
        const string& macro,
-       const IfableData& data )
+       const IfableData& data,
+       set<const Define *> *used_defs,
+       bool generatingCompilerMacro )
 {
        size_t i;
        bool generateAssignment;
 
-       generateAssignment = (use_pch && module.pch != NULL ) || data.includes.size () > 0 || data.defines.size () > 0 || data.compilerFlags.size () > 0;
+       if ( generatingCompilerMacro )
+               generateAssignment = (use_pch && module.pch != NULL ) || data.includes.size () > 0 || data.defines.size () > 0 || data.compilerFlags.size () > 0;
+       else
+               generateAssignment = (use_pch && module.pch != NULL ) || data.includes.size () > 0 || data.defines.size () > 0;
        if ( generateAssignment )
        {
                fprintf ( fMakefile,
@@ -753,22 +755,26 @@ MingwModuleHandler::GenerateMacro (
                          assignmentOperation );
        }
 
-       if ( use_pch && module.pch != NULL )
+       const FileLocation *pchFilename = GetPrecompiledHeaderFilename ();
+       if ( pchFilename )
        {
                fprintf ( fMakefile,
                          " -I%s",
-                         backend->GetFullPath ( *GetPrecompiledHeaderFilename () ).c_str () );
+                         backend->GetFullPath ( *pchFilename ).c_str () );
+               delete pchFilename;
        }
 
-       string compilerParameters = GenerateCompilerParametersFromVector ( data.compilerFlags );
-       if ( compilerParameters.size () > 0 )
+       if ( generatingCompilerMacro )
        {
-               fprintf (
-                       fMakefile,
-                       " %s",
-                       compilerParameters.c_str () );
+               string compilerParameters = GenerateCompilerParametersFromVector ( data.compilerFlags, CompilerTypeDontCare );
+               if ( compilerParameters.size () > 0 )
+               {
+                       fprintf (
+                               fMakefile,
+                               "%s",
+                               compilerParameters.c_str () );
+               }
        }
-
        for ( i = 0; i < data.includes.size(); i++ )
        {
                const Include& include = *data.includes[i];
@@ -780,16 +786,44 @@ MingwModuleHandler::GenerateMacro (
        }
        for ( i = 0; i < data.defines.size(); i++ )
        {
-               Define& d = *data.defines[i];
+               const Define& define = *data.defines[i];
+               if ( used_defs )
+               {
+                       set<const Define *>::const_iterator last_define;
+                       for (last_define = used_defs->begin ();
+                            last_define != used_defs->end ();
+                            last_define++)
+                       {
+                               if ( (*last_define)->name != define.name )
+                                       continue;
+                               if ( !define.overridable )
+                               {
+                                       throw InvalidOperationException ( (*last_define)->node->location.c_str (),
+                                                                         0,
+                                                                         "Invalid override of define '%s', already defined at %s",
+                                                                         define.name.c_str (),
+                                                                         define.node->location.c_str () );
+                               }
+                               if ( backend->configuration.Verbose )
+                                       printf("%s: Overriding '%s' already defined at %s\n",
+                                               (*last_define)->node->location.c_str (), define.name.c_str (),
+                                               define.node->location.c_str () );
+                               break;
+                       }
+                       if ( last_define != used_defs->end () )
+                               continue;
+               }
                fprintf (
                        fMakefile,
                        " -D%s",
-                       d.name.c_str() );
-               if ( d.value.size() )
+                       define.name.c_str() );
+               if (define.value.length () > 0)
                        fprintf (
                                fMakefile,
                                "=%s",
-                               d.value.c_str() );
+                               define.value.c_str() );
+               if ( used_defs )
+                       used_defs->insert( used_defs->begin (), &define );
        }
        if ( generateAssignment )
        {
@@ -801,16 +835,21 @@ void
 MingwModuleHandler::GenerateMacros (
        const char* assignmentOperation,
        const IfableData& data,
-       const vector<LinkerFlag*>* linkerFlags )
+       const vector<LinkerFlag*>* linkerFlags,
+       set<const Define *>& used_defs )
 {
        size_t i;
 
        GenerateMacro ( assignmentOperation,
                        cflagsMacro,
-                       data );
+                       data,
+                       &used_defs,
+                       true );
        GenerateMacro ( assignmentOperation,
                        windresflagsMacro,
-                       data );
+                       data,
+                       NULL,
+                       false );
 
        if ( linkerFlags != NULL )
        {
@@ -860,7 +899,8 @@ MingwModuleHandler::GenerateMacros (
                        GenerateMacros (
                                "+=",
                                rIf.data,
-                               NULL );
+                               NULL,
+                               used_defs );
                        fprintf (
                                fMakefile,
                                "endif\n\n" );
@@ -871,7 +911,7 @@ MingwModuleHandler::GenerateMacros (
 void
 MingwModuleHandler::CleanupCompilationUnitVector ( vector<CompilationUnit*>& compilationUnits )
 {
-       for (size_t i = 0; i < compilationUnits.size (); i++)
+       for ( size_t i = 0; i < compilationUnits.size (); i++ )
                delete compilationUnits[i];
 }
 
@@ -880,15 +920,86 @@ MingwModuleHandler::GetModuleSpecificCompilationUnits ( vector<CompilationUnit*>
 {
 }
 
+void
+MingwModuleHandler::GenerateSourceMacros (
+       const char* assignmentOperation,
+       const IfableData& data )
+{
+       size_t i;
+
+       const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
+       vector<const FileLocation *> headers;
+       if ( compilationUnits.size () > 0 )
+       {
+               fprintf (
+                       fMakefile,
+                       "%s %s",
+                       sourcesMacro.c_str (),
+                       assignmentOperation );
+               for ( i = 0; i < compilationUnits.size(); i++ )
+               {
+                       CompilationUnit& compilationUnit = *compilationUnits[i];
+                       const FileLocation& compilationName = compilationUnit.GetFilename ();
+                       fprintf (
+                               fMakefile,
+                               "%s%s",
+                               ( i%10 == 9 ? " \\\n\t" : " " ),
+                               backend->GetFullName ( compilationName ).c_str () );
+               }
+               fprintf ( fMakefile, "\n" );
+       }
+
+       const vector<If*>& ifs = data.ifs;
+       for ( i = 0; i < ifs.size(); i++ )
+       {
+               If& rIf = *ifs[i];
+               if ( rIf.data.defines.size()
+                       || rIf.data.includes.size()
+                       || rIf.data.libraries.size()
+                       || rIf.data.compilationUnits.size()
+                       || rIf.data.compilerFlags.size()
+                       || rIf.data.ifs.size() )
+               {
+                       fprintf (
+                               fMakefile,
+                               "%s (\"$(%s)\",\"%s\")\n",
+                               rIf.negated ? "ifneq" : "ifeq",
+                               rIf.property.c_str(),
+                               rIf.value.c_str() );
+                       GenerateSourceMacros (
+                               "+=",
+                               rIf.data );
+                       fprintf (
+                               fMakefile,
+                               "endif\n\n" );
+               }
+       }
+
+       vector<CompilationUnit*> sourceCompilationUnits;
+       GetModuleSpecificCompilationUnits ( sourceCompilationUnits );
+       for ( i = 0; i < sourceCompilationUnits.size (); i++ )
+       {
+               const FileLocation& compilationName = sourceCompilationUnits[i]->GetFilename ();
+               fprintf (
+                       fMakefile,
+                       "%s += %s\n",
+                       sourcesMacro.c_str(),
+                       backend->GetFullName ( compilationName ).c_str () );
+       }
+       CleanupCompilationUnitVector ( sourceCompilationUnits );
+}
+
 void
 MingwModuleHandler::GenerateObjectMacros (
        const char* assignmentOperation,
-       const IfableData& data,
-       const vector<LinkerFlag*>* linkerFlags )
+       const IfableData& data )
 {
        size_t i;
 
        const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
+       vector<const FileLocation *> headers;
+       vector<const FileLocation *> mcheaders;
+       vector<const FileLocation *> mcresources;
        if ( compilationUnits.size () > 0 )
        {
                for ( i = 0; i < compilationUnits.size (); i++ )
@@ -896,11 +1007,14 @@ MingwModuleHandler::GenerateObjectMacros (
                        CompilationUnit& compilationUnit = *compilationUnits[i];
                        if ( compilationUnit.IsFirstFile () )
                        {
+                               const FileLocation& compilationName = compilationUnit.GetFilename ();
+                               const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
                                fprintf ( fMakefile,
                                        "%s := %s $(%s)\n",
                                        objectsMacro.c_str(),
-                                       backend->GetFullName ( *GetObjectFilename ( compilationUnit.GetFilename (), NULL ) ).c_str (),
+                                       backend->GetFullName ( *object_file ).c_str (),
                                        objectsMacro.c_str() );
+                               delete object_file;
                        }
                }
                fprintf (
@@ -913,17 +1027,85 @@ MingwModuleHandler::GenerateObjectMacros (
                        CompilationUnit& compilationUnit = *compilationUnits[i];
                        if ( !compilationUnit.IsFirstFile () )
                        {
-                               const FileLocation *objectFilename = GetObjectFilename ( compilationUnit.GetFilename (), NULL );
-                               if ( objectFilename )
+                               const FileLocation& compilationName = compilationUnit.GetFilename ();
+                               const FileLocation *objectFilename = GetObjectFilename ( &compilationName, module );
+                               if ( GetExtension ( *objectFilename ) == ".h" )
+                                       headers.push_back ( objectFilename );
+                               else if ( GetExtension ( *objectFilename ) == ".rc" )
+                               {
+                                       const FileLocation *headerFilename = GetMcHeaderFilename ( &compilationUnit.GetFilename () );
+                                       mcheaders.push_back ( headerFilename );
+                                       mcresources.push_back ( objectFilename );
+                               }
+                               else
+                               {
                                        fprintf (
                                                fMakefile,
                                                "%s%s",
                                                ( i%10 == 9 ? " \\\n\t" : " " ),
                                                backend->GetFullName ( *objectFilename ).c_str () );
+                                       delete objectFilename;
+                               }
                        }
                }
                fprintf ( fMakefile, "\n" );
        }
+       if ( headers.size () > 0 )
+       {
+               fprintf (
+                       fMakefile,
+                       "%s_HEADERS %s",
+                       module.name.c_str (),
+                       assignmentOperation );
+               for ( i = 0; i < headers.size (); i++ )
+               {
+                       fprintf (
+                               fMakefile,
+                               "%s%s",
+                               ( i%10 == 9 ? " \\\n\t" : " " ),
+                               backend->GetFullName ( *headers[i] ).c_str () );
+                       delete headers[i];
+               }
+               fprintf ( fMakefile, "\n" );
+       }
+
+       if ( mcheaders.size () > 0 )
+       {
+               fprintf (
+                       fMakefile,
+                       "%s_MCHEADERS %s",
+                       module.name.c_str (),
+                       assignmentOperation );
+               for ( i = 0; i < mcheaders.size (); i++ )
+               {
+                       fprintf (
+                               fMakefile,
+                               "%s%s",
+                               ( i%10 == 9 ? " \\\n\t" : " " ),
+                               backend->GetFullName ( *mcheaders[i] ).c_str () );
+                       delete mcheaders[i];
+               }
+               fprintf ( fMakefile, "\n" );
+       }
+
+       if ( mcresources.size () > 0 )
+       {
+               fprintf (
+                       fMakefile,
+                       "%s_RESOURCES %s",
+                       module.name.c_str (),
+                       assignmentOperation );
+               for ( i = 0; i < mcresources.size (); i++ )
+               {
+                       fprintf (
+                               fMakefile,
+                               "%s%s",
+                               ( i%10 == 9 ? " \\\n\t" : " " ),
+                               backend->GetFullName ( *mcresources[i] ).c_str () );
+                       delete mcresources[i];
+               }
+               fprintf ( fMakefile, "\n" );
+       }
 
        const vector<If*>& ifs = data.ifs;
        for ( i = 0; i < ifs.size(); i++ )
@@ -944,8 +1126,7 @@ MingwModuleHandler::GenerateObjectMacros (
                                rIf.value.c_str() );
                        GenerateObjectMacros (
                                "+=",
-                               rIf.data,
-                               NULL );
+                               rIf.data );
                        fprintf (
                                fMakefile,
                                "endif\n\n" );
@@ -956,208 +1137,179 @@ MingwModuleHandler::GenerateObjectMacros (
        GetModuleSpecificCompilationUnits ( sourceCompilationUnits );
        for ( i = 0; i < sourceCompilationUnits.size (); i++ )
        {
+               const FileLocation& compilationName = sourceCompilationUnits[i]->GetFilename ();
+               const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
                fprintf (
                        fMakefile,
                        "%s += %s\n",
                        objectsMacro.c_str(),
-                       backend->GetFullName ( *GetObjectFilename ( sourceCompilationUnits[i]->GetFilename (), NULL ) ).c_str () );
+                       backend->GetFullName ( *object_file ).c_str () );
+               delete object_file;
        }
        CleanupCompilationUnitVector ( sourceCompilationUnits );
 }
 
+/* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetPrecompiledHeaderFilename () const
 {
-       const string& basePchFilename = module.pch->file.name + ".gch";
+       if ( !module.pch || !use_pch )
+               return NULL;
        return new FileLocation ( IntermediateDirectory,
-                                 module.pch->file.relative_path,
-                                 basePchFilename );
-}
+                                 module.pch->file->relative_path,
+                                 module.pch->file->name + ".gch" );
+}
+
+Rule arRule1 ( "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).a: $($(module_name)_OBJS) | $(INTERMEDIATE)$(SEP)$(source_dir)\n",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).a",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule arRule2 ( "\t$(ECHO_AR)\n"
+              "\t${ar} -rc $@ $($(module_name)_OBJS)\n",
+              NULL );
+Rule arHostRule2 ( "\t$(ECHO_AR)\n"
+                   "\t${host_ar} -rc $@ $($(module_name)_OBJS)\n",
+                   NULL );
+Rule gasRule ( "$(source): ${$(module_name)_precondition}\n"
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o: $(source)$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+               "\t$(ECHO_GAS)\n"
+               "\t${gcc} -x assembler-with-cpp -o $@ -D__ASM__ $($(module_name)_CFLAGS) -c $<\n",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule bootRule ( "$(source): ${$(module_name)_precondition}\n"
+                "$(module_output): $(source)$(dependencies) | $(OUTPUT)$(SEP)$(source_dir)\n"
+                "\t$(ECHO_NASM)\n"
+                "\t$(Q)${nasm} -f win32 $< -o $@ $($(module_name)_NASMFLAGS)\n",
+                "$(OUTPUT)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule nasmRule ( "$(source): ${$(module_name)_precondition}\n"
+                "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o: $(source)$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                "\t$(ECHO_NASM)\n"
+                "\t$(Q)${nasm} -f win32 $< -o $@ $($(module_name)_NASMFLAGS)\n",
+                "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o",
+                "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule windresRule ( "$(source): ${$(module_name)_precondition}\n"
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).coff: $(source)$(dependencies) $(WRC_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir) $(TEMPORARY)\n"
+                   "\t$(ECHO_WRC)\n"
+                   "\t${gcc} -xc -E -DRC_INVOKED ${$(module_name)_RCFLAGS} $(source) > $(TEMPORARY)$(SEP)$(module_name).$(source_name_noext).rci.tmp\n"
+                   "\t$(Q)$(WRC_TARGET) ${$(module_name)_RCFLAGS} $(TEMPORARY)$(SEP)$(module_name).$(source_name_noext).rci.tmp $(TEMPORARY)$(SEP)$(module_name).$(source_name_noext).res.tmp\n"
+                   "\t-@${rm} $(TEMPORARY)$(SEP)$(module_name).$(source_name_noext).rci.tmp 2>$(NUL)\n"
+                   "\t${windres} $(TEMPORARY)$(SEP)$(module_name).$(source_name_noext).res.tmp -o $@\n"
+                   "\t-@${rm} $(TEMPORARY)$(SEP)$(module_name).$(source_name_noext).res.tmp 2>$(NUL)\n",
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).coff",
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule wmcRule ( "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).rc $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h: $(WMC_TARGET) $(source) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+               "\t$(ECHO_WMC)\n"
+               "\t$(Q)$(WMC_TARGET) -i -H $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h -o $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).rc $(source)\n",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).rc",
+               "$(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule winebuildRule ( "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).spec.def: $(source)$(dependencies) $(WINEBUILD_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                     "\t$(ECHO_WINEBLD)\n"
+                     "\t$(Q)$(WINEBUILD_TARGET) $(WINEBUILD_FLAGS) -o $(INTERMEDIATE)$(SEP)$(source_path)$(SEP)$(source_name_noext).spec.def --def -E $(source)\n"
+                     "$(INTERMEDIATE)$(SEP)$(source_path)$(SEP)$(source_name_noext).stubs.c: $(source_path)$(SEP)$(source_name_noext).spec $(WINEBUILD_TARGET)\n"
+                     "\t$(ECHO_WINEBLD)\n"
+                     "\t$(Q)$(WINEBUILD_TARGET) $(WINEBUILD_FLAGS) -o $(INTERMEDIATE)$(SEP)$(source_path)$(SEP)$(source_name_noext).stubs.c --pedll $(source_path)$(SEP)$(source_name_noext).spec\n"
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).stubs.o: $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).stubs.c$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                     "\t$(ECHO_CC)\n"
+                     "\t${gcc} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).spec.def",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).stubs.c",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).stubs.o",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule widlHeaderRule ( "$(source): ${$(module_name)_precondition}\n"
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).h: $(source)$(dependencies) $(WIDL_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                      "\t$(ECHO_WIDL)\n"
+                      "\t$(Q)$(WIDL_TARGET) $($(module_name)_WIDLFLAGS) -h -H $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).h $(source)\n",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).h",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule widlServerRule ( "$(source): ${$(module_name)_precondition}\n"
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.c $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.h: $(source)$(dependencies) $(WIDL_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                      "\t$(ECHO_WIDL)\n"
+                      "\t$(Q)$(WIDL_TARGET) $($(module_name)_WIDLFLAGS) -h -H $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.h -s -S $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.c $(source)\n"
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.o: $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.c $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.h$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                      "\t$(ECHO_CC)\n"
+                      "\t${gcc} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.h",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.c",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_s.o",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule widlClientRule ( "$(source): ${$(module_name)_precondition}\n"
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.c $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.h: $(source)$(dependencies) $(WIDL_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                      "\t$(ECHO_WIDL)\n"
+                      "\t$(Q)$(WIDL_TARGET) $($(module_name)_WIDLFLAGS) -h -H $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.h -c -C $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.c $(source)\n"
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.o: $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.c $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.h$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                      "\t$(ECHO_CC)\n"
+                      "\t${gcc} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.h",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.c",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_c.o",
+                      "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule widlProxyRule ( "$(source): ${$(module_name)_precondition}\n"
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.c $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.h: $(source)$(dependencies) $(WIDL_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                     "\t$(ECHO_WIDL)\n"
+                     "\t$(Q)$(WIDL_TARGET) $($(module_name)_WIDLFLAGS) -h -H $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.h -p -P $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.c $(source)\n"
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.o: $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.c $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.h$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                      "\t$(ECHO_CC)\n"
+                      "\t${gcc} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.h",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.c",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_p.o",
+                     "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule widlTlbRule ( "$(source): ${$(module_name)_precondition}\n"
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(module_name).tlb: $(source)$(dependencies) $(WIDL_TARGET) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                   "\t$(ECHO_WIDL)\n"
+                   "\t$(Q)$(WIDL_TARGET) $($(module_name)_WIDLFLAGS) -t -T $(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext).tlb $(source)\n",
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)", NULL );
+Rule gccRule ( "$(source): ${$(module_name)_precondition}\n"
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o: $(source)$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+               "\t$(ECHO_CC)\n"
+               "\t${gcc} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o", NULL );
+Rule gccHostRule ( "$(source): ${$(module_name)_precondition}\n"
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o: $(source)$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                   "\t$(ECHO_CC)\n"
+                   "\t${host_gcc} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o", NULL );
+Rule gppRule ( "$(source): ${$(module_name)_precondition}\n"
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o: $(source)$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+               "\t$(ECHO_CC)\n"
+               "\t${gpp} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+               "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o", NULL );
+Rule gppHostRule ( "$(source): ${$(module_name)_precondition}\n"
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o: $(source)$(dependencies) | $(INTERMEDIATE)$(SEP)$(source_dir)\n"
+                   "\t$(ECHO_CC)\n"
+                   "\t${host_gpp} -o $@ $($(module_name)_CFLAGS)$(compiler_flags) -c $<\n",
+                   "$(INTERMEDIATE)$(SEP)$(source_dir)$(SEP)$(source_name_noext)_$(module_name).o", NULL );
+Rule emptyRule ( "", NULL );
 
 void
 MingwModuleHandler::GenerateGccCommand (
        const FileLocation* sourceFile,
-       const string& extraDependencies,
-       const string& cc,
-       const string& cflagsMacro )
-{
-       string dependencies = backend->GetFullName ( *sourceFile );
-       if ( extraDependencies != "" )
-               dependencies += " " + extraDependencies;
-       if ( module.pch && use_pch )
-               dependencies += " " + backend->GetFullName ( *GetPrecompiledHeaderFilename () );
-
-       /* WIDL generated headers may be used */
-       vector<FileLocation> rpcDependencies;
-       GetRpcHeaderDependencies ( rpcDependencies );
-       dependencies += " " + v2s ( backend, rpcDependencies, 5 );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       const FileLocation *objectFilename = GetObjectFilename (
-               sourceFile, &clean_files );
-       fprintf ( fMakefile,
-                 "%s: %s | %s\n",
-                 backend->GetFullName ( *objectFilename ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( *objectFilename ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_CC)\n" );
-       fprintf ( fMakefile,
-                "\t%s -c $< -o $@ %s\n",
-                cc.c_str (),
-                cflagsMacro.c_str () );
-}
-
-void
-MingwModuleHandler::GenerateGccAssemblerCommand (
-       const FileLocation* sourceFile,
-       const string& cc,
-       const string& cflagsMacro )
+       const Rule *rule,
+       const string& extraDependencies )
 {
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       const FileLocation *objectFilename = GetObjectFilename (
-               sourceFile, &clean_files );
-       fprintf ( fMakefile,
-                 "%s: %s | %s\n",
-                 backend->GetFullName ( *objectFilename ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( *objectFilename ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_GAS)\n" );
-       fprintf ( fMakefile,
-                 "\t%s -x assembler-with-cpp -c $< -o $@ -D__ASM__ %s\n",
-                 cc.c_str (),
-                 cflagsMacro.c_str () );
-}
+       const FileLocation *pchFilename = GetPrecompiledHeaderFilename ();
+       string dependencies = extraDependencies;
 
-void
-MingwModuleHandler::GenerateNasmCommand (
-       const FileLocation* sourceFile,
-       const string& nasmflagsMacro )
-{
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       const FileLocation *objectFilename = GetObjectFilename (
-               sourceFile, &clean_files );
-       fprintf ( fMakefile,
-                 "%s: %s | %s\n",
-                 backend->GetFullName ( *objectFilename ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( *objectFilename ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_NASM)\n" );
-       fprintf ( fMakefile,
-                 "\t%s -f win32 $< -o $@ %s\n",
-                 "$(Q)${nasm}",
-                 nasmflagsMacro.c_str () );
-}
-
-void
-MingwModuleHandler::GenerateWindresCommand (
-       const FileLocation* sourceFile,
-       const string& windresflagsMacro )
-{
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       const FileLocation *objectFilename = GetObjectFilename ( sourceFile, &clean_files );
-
-       if ( module.useWRC )
-       {
-               string sourceFilenamePart = module.name + "." + ReplaceExtension ( sourceFile->name, "" );
-               FileLocation rciFilename ( TemporaryDirectory,
-                                          "",
-                                          sourceFilenamePart + ".rci.tmp" );
-               FileLocation resFilename ( TemporaryDirectory,
-                                          "",
-                                          sourceFilenamePart + ".res.tmp" );
-
-               fprintf ( fMakefile,
-                         "%s: %s $(WRC_TARGET) | %s\n",
-                         backend->GetFullName ( *objectFilename ).c_str (),
-                         dependencies.c_str (),
-                         backend->GetFullPath ( *objectFilename ).c_str () );
-               fprintf ( fMakefile, "\t$(ECHO_WRC)\n" );
-               fprintf ( fMakefile,
-                        "\t${gcc} -xc -E -DRC_INVOKED ${%s} %s > %s\n",
-                        windresflagsMacro.c_str (),
-                        backend->GetFullName ( *sourceFile ).c_str (),
-                        backend->GetFullName ( rciFilename ).c_str () );
-               fprintf ( fMakefile,
-                        "\t$(Q)$(WRC_TARGET) ${%s} %s %s\n",
-                        windresflagsMacro.c_str (),
-                        backend->GetFullName ( rciFilename ).c_str (),
-                        backend->GetFullName ( resFilename ).c_str () );
-               fprintf ( fMakefile,
-                        "\t-@${rm} %s 2>$(NUL)\n",
-                        backend->GetFullName ( rciFilename ).c_str () );
-               fprintf ( fMakefile,
-                        "\t${windres} %s -o $@\n",
-                        backend->GetFullName ( resFilename ).c_str () );
-               fprintf ( fMakefile,
-                        "\t-@${rm} %s 2>$(NUL)\n",
-                        backend->GetFullName ( resFilename ).c_str () );
-       }
+       string flags;
+       string extension = GetExtension ( *sourceFile );
+       if ( extension == ".cc" || extension == ".cpp" || extension == ".cxx" )
+               flags = GenerateCompilerParametersFromVector ( module.non_if_data.compilerFlags, CompilerTypeCPP );
        else
+               flags = GenerateCompilerParametersFromVector ( module.non_if_data.compilerFlags, CompilerTypeCC );
+
+       if ( pchFilename )
        {
-               fprintf ( fMakefile,
-                         "%s: %s $(WRC_TARGET) | %s\n",
-                         backend->GetFullName ( *objectFilename ).c_str (),
-                         dependencies.c_str (),
-                         backend->GetFullPath ( *objectFilename ).c_str () );
-               fprintf ( fMakefile, "\t$(ECHO_WRC)\n" );
-               fprintf ( fMakefile,
-                        "\t${windres} $(%s) %s -o $@\n",
-                        windresflagsMacro.c_str (),
-                        backend->GetFullName ( *sourceFile ).c_str () );
+               dependencies += " " + backend->GetFullName ( *pchFilename );
+               delete pchFilename;
        }
-}
-
-void
-MingwModuleHandler::GenerateWinebuildCommands (
-       const FileLocation* sourceFile )
-{
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
 
-       string basename = GetBasename ( sourceFile->name );
-       FileLocation def_file ( IntermediateDirectory,
-                               sourceFile->relative_path,
-                               basename + ".spec.def" );
-       CLEAN_FILE ( def_file );
-
-       FileLocation stub_file ( IntermediateDirectory,
-                               sourceFile->relative_path,
-                               basename + ".stubs.c" );
-       CLEAN_FILE ( stub_file );
+       /* WIDL generated headers may be used */
+       vector<FileLocation> rpcDependencies;
+       GetRpcHeaderDependencies ( rpcDependencies );
+       if ( rpcDependencies.size () > 0 )
+               dependencies += " " + v2s ( backend, rpcDependencies, 5 );
 
-       fprintf ( fMakefile,
-                 "%s: %s $(WINEBUILD_TARGET) | %s\n",
-                 backend->GetFullName ( def_file ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( def_file ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_WINEBLD)\n" );
-       fprintf ( fMakefile,
-                 "\t%s -o %s --def -E %s\n",
-                 "$(Q)$(WINEBUILD_TARGET)",
-                 backend->GetFullName ( def_file ).c_str (),
-                 backend->GetFullName ( *sourceFile ).c_str () );
-       fprintf ( fMakefile,
-                 "%s: %s $(WINEBUILD_TARGET)\n",
-                 backend->GetFullName ( stub_file ).c_str (),
-                 backend->GetFullName ( *sourceFile ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_WINEBLD)\n" );
-       fprintf ( fMakefile,
-                 "\t%s -o %s --pedll %s\n",
-                 "$(Q)$(WINEBUILD_TARGET)",
-                 backend->GetFullName ( stub_file ).c_str (),
-                 backend->GetFullName ( *sourceFile ).c_str () );
-}
-
-string
-MingwModuleHandler::GetWidlFlags ( const CompilationUnit& compilationUnit )
-{
-       return compilationUnit.GetSwitches ();
+       rule->Execute ( fMakefile, backend, module, sourceFile, clean_files, dependencies, flags );
 }
 
 string
@@ -1172,6 +1324,7 @@ MingwModuleHandler::GetPropertyValue ( const Module& module, const std::string&
        return string ( "" );
 }
 
+/* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetRpcServerHeaderFilename ( const FileLocation *base ) const
 {
@@ -1179,42 +1332,7 @@ MingwModuleHandler::GetRpcServerHeaderFilename ( const FileLocation *base ) cons
        return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
 }
 
-void
-MingwModuleHandler::GenerateWidlCommandsServer (
-       const CompilationUnit& compilationUnit,
-       const string& widlflagsMacro )
-{
-       const FileLocation* sourceFile = compilationUnit.GetFilename ();
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       string basename = GetBasename ( sourceFile->name );
-
-       const FileLocation *generatedHeaderFilename = GetRpcServerHeaderFilename ( sourceFile );
-       CLEAN_FILE ( *generatedHeaderFilename );
-
-       FileLocation generatedServerFilename ( IntermediateDirectory,
-                                              sourceFile->relative_path,
-                                              basename + "_s.c" );
-       CLEAN_FILE ( generatedServerFilename );
-
-       fprintf ( fMakefile,
-                 "%s %s: %s $(WIDL_TARGET) | %s\n",
-                 backend->GetFullName ( generatedServerFilename ).c_str (),
-                 backend->GetFullName ( *generatedHeaderFilename ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( generatedServerFilename ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_WIDL)\n" );
-       fprintf ( fMakefile,
-                 "\t%s %s %s -h -H %s -s -S %s %s\n",
-                 "$(Q)$(WIDL_TARGET)",
-                 GetWidlFlags ( compilationUnit ).c_str (),
-                 widlflagsMacro.c_str (),
-                 backend->GetFullName ( *generatedHeaderFilename ).c_str (),
-                 backend->GetFullName ( generatedServerFilename ).c_str (),
-                 backend->GetFullName ( *sourceFile ).c_str () );
-}
-
+/* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetRpcClientHeaderFilename ( const FileLocation *base ) const
 {
@@ -1222,204 +1340,98 @@ MingwModuleHandler::GetRpcClientHeaderFilename ( const FileLocation *base ) cons
        return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
 }
 
+/* caller needs to delete the returned object */
 const FileLocation*
-MingwModuleHandler::GetIdlHeaderFilename ( const FileLocation *base ) const
+MingwModuleHandler::GetRpcProxyHeaderFilename ( const FileLocation *base ) const
 {
-       string newname = GetBasename ( base->name ) + ".h";
+       string newname = GetBasename ( base->name ) + "_p.h";
        return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
 }
 
-void
-MingwModuleHandler::GenerateWidlCommandsEmbeddedTypeLib (
-       const CompilationUnit& compilationUnit,
-       const string& widlflagsMacro )
-{
-       const FileLocation* sourceFile = compilationUnit.GetFilename ();
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       string basename = GetBasename ( sourceFile->name );
-
-       FileLocation EmbeddedTypeLibFilename ( IntermediateDirectory,
-                                              sourceFile->relative_path,
-                                              basename + ".tlb" );
-
-       fprintf ( fMakefile,
-                 "%s: %s $(WIDL_TARGET) | %s\n",
-                 GetTargetMacro ( module ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( EmbeddedTypeLibFilename ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_WIDL)\n" );
-       fprintf ( fMakefile,
-                 "\t%s %s %s -t -T %s %s\n",
-                 "$(Q)$(WIDL_TARGET)",
-                 GetWidlFlags ( compilationUnit ).c_str (),
-                 widlflagsMacro.c_str (),
-                 backend->GetFullName ( EmbeddedTypeLibFilename ).c_str(),
-                 backend->GetFullName ( *sourceFile ).c_str () );
-}
-
-void
-MingwModuleHandler::GenerateWidlCommandsClient (
-       const CompilationUnit& compilationUnit,
-       const string& widlflagsMacro )
-{
-       const FileLocation* sourceFile = compilationUnit.GetFilename ();
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       string basename = GetBasename ( sourceFile->name );
-
-       const FileLocation *generatedHeaderFilename = GetRpcClientHeaderFilename ( sourceFile );
-       CLEAN_FILE ( *generatedHeaderFilename );
-
-       FileLocation generatedClientFilename ( IntermediateDirectory,
-                                              sourceFile->relative_path,
-                                              basename + "_c.c" );
-       CLEAN_FILE ( generatedClientFilename );
-
-       fprintf ( fMakefile,
-                 "%s %s: %s $(WIDL_TARGET) | %s\n",
-                 backend->GetFullName ( generatedClientFilename ).c_str (),
-                 backend->GetFullName ( *generatedHeaderFilename ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( generatedClientFilename ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_WIDL)\n" );
-       fprintf ( fMakefile,
-                 "\t%s %s %s -h -H %s -c -C %s %s\n",
-                 "$(Q)$(WIDL_TARGET)",
-                 GetWidlFlags ( compilationUnit ).c_str (),
-                 widlflagsMacro.c_str (),
-                 backend->GetFullName ( *generatedHeaderFilename ).c_str (),
-                 backend->GetFullName ( generatedClientFilename ).c_str (),
-                 backend->GetFullName ( *sourceFile ).c_str () );
-}
-
-void
-MingwModuleHandler::GenerateWidlCommandsIdlHeader (
-       const CompilationUnit& compilationUnit,
-       const string& widlflagsMacro )
+/* caller needs to delete the returned object */
+const FileLocation*
+MingwModuleHandler::GetIdlHeaderFilename ( const FileLocation *base ) const
 {
-       const FileLocation* sourceFile = compilationUnit.GetFilename ();
-       string dependencies = backend->GetFullName ( *sourceFile );
-       dependencies += " " + NormalizeFilename ( module.xmlbuildFile );
-
-       string basename = GetBasename ( sourceFile->name );
-
-       const FileLocation *generatedHeader = GetIdlHeaderFilename ( sourceFile );
-       CLEAN_FILE ( *generatedHeader );
-
-       fprintf ( fMakefile,
-                 "%s: %s $(WIDL_TARGET) | %s\n",
-                 backend->GetFullName( *generatedHeader ).c_str (),
-                 dependencies.c_str (),
-                 backend->GetFullPath ( *generatedHeader ).c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_WIDL)\n" );
-       fprintf ( fMakefile,
-                 "\t%s %s %s -h -H %s %s\n",
-                 "$(Q)$(WIDL_TARGET)",
-                 GetWidlFlags ( compilationUnit ).c_str (),
-                 widlflagsMacro.c_str (),
-                 backend->GetFullName ( *generatedHeader ).c_str (),
-                 backend->GetFullName ( *sourceFile ).c_str () );
+       string newname = GetBasename ( base->name ) + ".h";
+       return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
 }
 
-void
-MingwModuleHandler::GenerateWidlCommands (
-       const CompilationUnit& compilationUnit,
-       const string& widlflagsMacro )
+/* caller needs to delete the returned object */
+const FileLocation*
+MingwModuleHandler::GetMcHeaderFilename ( const FileLocation *base ) const
 {
-       if ( module.type == RpcServer )
-               GenerateWidlCommandsServer ( compilationUnit,
-                                            widlflagsMacro );
-       else if ( module.type == RpcClient )
-               GenerateWidlCommandsClient ( compilationUnit,
-                                            widlflagsMacro );
-       else if ( module.type == EmbeddedTypeLib )
-               GenerateWidlCommandsEmbeddedTypeLib ( compilationUnit,
-                                                                               widlflagsMacro );
-       else // applies also for other module.types which include idl files
-               GenerateWidlCommandsIdlHeader ( compilationUnit,
-                                               widlflagsMacro );
+       string newname = GetBasename ( base->name ) + ".h";
+       return new FileLocation ( IntermediateDirectory, "include/reactos" , newname );
 }
 
 void
 MingwModuleHandler::GenerateCommands (
        const CompilationUnit& compilationUnit,
-       const string& cc,
-       const string& cppc,
-       const string& cflagsMacro,
-       const string& nasmflagsMacro,
-       const string& windresflagsMacro,
-       const string& widlflagsMacro )
-{
-       const FileLocation* sourceFile = compilationUnit.GetFilename ();
-       string extension = GetExtension ( *sourceFile );
-       if ( extension == ".c" || extension == ".C" )
-       {
-               GenerateGccCommand ( sourceFile,
-                                    GetCompilationUnitDependencies ( compilationUnit ),
-                                    cc,
-                                    cflagsMacro );
-               return;
-       }
-       else if ( extension == ".cc" || extension == ".CC" ||
-                 extension == ".cpp" || extension == ".CPP" ||
-                 extension == ".cxx" || extension == ".CXX" )
-       {
-               GenerateGccCommand ( sourceFile,
-                                    GetCompilationUnitDependencies ( compilationUnit ),
-                                    cppc,
-                                    cflagsMacro );
-               return;
-       }
-       else if ( extension == ".s" || extension == ".S" )
-       {
-               GenerateGccAssemblerCommand ( sourceFile,
-                                             cc,
-                                             cflagsMacro );
-               return;
-       }
-       else if ( extension == ".asm" || extension == ".ASM" )
-       {
-               GenerateNasmCommand ( sourceFile,
-                                     nasmflagsMacro );
-               return;
-       }
-       else if ( extension == ".rc" || extension == ".RC" )
+       const string& extraDependencies )
+{
+       const FileLocation& sourceFile = compilationUnit.GetFilename ();
+       string extension = GetExtension ( sourceFile );
+       std::transform ( extension.begin (), extension.end (), extension.begin (), tolower );
+
+       struct
+       {
+               HostType host;
+               ModuleType type;
+               string extension;
+               Rule* rule;
+       } rules[] = {
+               { HostDontCare, TypeDontCare, ".s", &gasRule },
+               { HostDontCare, BootSector, ".asm", &bootRule },
+               { HostDontCare, TypeDontCare, ".asm", &nasmRule },
+               { HostDontCare, TypeDontCare, ".rc", &windresRule },
+               { HostDontCare, TypeDontCare, ".mc", &wmcRule },
+               { HostDontCare, TypeDontCare, ".spec", &winebuildRule },
+               { HostDontCare, RpcServer, ".idl", &widlServerRule },
+               { HostDontCare, RpcClient, ".idl", &widlClientRule },
+               { HostDontCare, RpcProxy, ".idl", &widlProxyRule },
+               { HostDontCare, EmbeddedTypeLib, ".idl", &widlTlbRule },
+               { HostDontCare, TypeDontCare, ".idl", &widlHeaderRule },
+               { HostTrue, TypeDontCare, ".c", &gccHostRule },
+               { HostTrue, TypeDontCare, ".cc", &gppHostRule },
+               { HostTrue, TypeDontCare, ".cpp", &gppHostRule },
+               { HostTrue, TypeDontCare, ".cxx", &gppHostRule },
+               { HostFalse, TypeDontCare, ".c", &gccRule },
+               { HostFalse, TypeDontCare, ".cc", &gppRule },
+               { HostFalse, TypeDontCare, ".cpp", &gppRule },
+               { HostFalse, TypeDontCare, ".cxx", &gppRule },
+               { HostFalse, Cabinet, ".*", &emptyRule }
+       };
+       size_t i;
+       Rule *customRule = NULL;
+
+       for ( i = 0; i < sizeof ( rules ) / sizeof ( rules[0] ); i++ )
        {
-               GenerateWindresCommand ( sourceFile,
-                                        windresflagsMacro );
-               return;
+               if ( rules[i].host != HostDontCare && rules[i].host != module.host )
+                       continue;
+               if ( rules[i].type != TypeDontCare && rules[i].type != module.type )
+                       continue;
+               if ( rules[i].extension != extension && rules[i].extension != ".*")
+                       continue;
+               customRule = rules[i].rule;
+               break;
        }
-       else if ( extension == ".spec" || extension == ".SPEC" )
+
+       if ( extension == ".c" || extension == ".cc" || extension == ".cpp" || extension == ".cxx" )
        {
-               GenerateWinebuildCommands ( sourceFile );
-               GenerateGccCommand ( GetActualSourceFilename ( sourceFile ),
-                                    "",
-                                    cc,
-                                    cflagsMacro );
-               return;
+               GenerateGccCommand ( &sourceFile,
+                                    customRule,
+                                    GetCompilationUnitDependencies ( compilationUnit ) + GetExtraDependencies ( &sourceFile ) + extraDependencies );
        }
-       else if ( extension == ".idl" || extension == ".IDL" )
+       else if ( customRule )
+               customRule->Execute ( fMakefile, backend, module, &sourceFile, clean_files );
+       else
        {
-               GenerateWidlCommands ( compilationUnit,
-                                      widlflagsMacro );
-               if ( (module.type == RpcServer) || (module.type == RpcClient) )
-               {
-                       GenerateGccCommand ( GetActualSourceFilename ( sourceFile ),
-                                            GetExtraDependencies ( sourceFile ),
-                                            cc,
-                                            cflagsMacro );
-               }
-               return;
+               throw InvalidOperationException ( __FILE__,
+                                                 __LINE__,
+                                                 "Unsupported filename extension '%s' in file '%s'",
+                                                 extension.c_str (),
+                                                 backend->GetFullName ( sourceFile ).c_str () );
        }
-
-       throw InvalidOperationException ( __FILE__,
-                                         __LINE__,
-                                         "Unsupported filename extension '%s' in file '%s'",
-                                         extension.c_str (),
-                                         backend->GetFullName ( *sourceFile ).c_str () );
 }
 
 void
@@ -1507,7 +1519,10 @@ MingwModuleHandler::GetObjectsVector ( const IfableData& data,
        for ( size_t i = 0; i < data.compilationUnits.size (); i++ )
        {
                CompilationUnit& compilationUnit = *data.compilationUnits[i];
-               objectFiles.push_back ( *GetObjectFilename ( compilationUnit.GetFilename (), NULL ) );
+               const FileLocation& compilationName = compilationUnit.GetFilename ();
+               const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
+               objectFiles.push_back ( *object_file );
+               delete object_file;
        }
 }
 
@@ -1535,10 +1550,14 @@ MingwModuleHandler::GenerateCleanObjectsAsYouGoCode () const
 void
 MingwModuleHandler::GenerateRunRsymCode () const
 {
+       fprintf ( fMakefile,
+             "ifneq ($(ROS_GENERATE_RSYM),no)\n" );
        fprintf ( fMakefile,
                  "\t$(ECHO_RSYM)\n" );
        fprintf ( fMakefile,
                  "\t$(Q)$(RSYM_TARGET) $@ $@\n\n" );
+       fprintf ( fMakefile,
+             "endif\n" );
 }
 
 void
@@ -1557,26 +1576,28 @@ MingwModuleHandler::GenerateRunStripCode () const
 void
 MingwModuleHandler::GenerateLinkerCommand (
        const string& dependencies,
-       const string& linker,
        const string& linkerParameters,
-       const string& objectsMacro,
-       const string& libsMacro,
        const string& pefixupParameters )
 {
-       string target ( GetTargetMacro ( module ) );
-       string target_folder ( backend->GetFullPath ( *GetTargetFilename ( module, NULL ) ) );
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
        const FileLocation *definitionFilename = GetDefinitionFilename ();
+       string linker = module.cplusplus ? "${gpp}" : "${gcc}";
+       string objectsMacro = GetObjectsMacro ( module );
+       string libsMacro = GetLibsMacro ();
+
+       string target_macro ( GetTargetMacro ( module ) );
+       string target_folder ( backend->GetFullPath ( *target_file ) );
 
        string linkerScriptArgument;
        if ( module.linkerScript != NULL )
-               linkerScriptArgument = ssprintf ( "-Wl,-T,%s", module.linkerScript->directory.c_str () );
+               linkerScriptArgument = ssprintf ( " -Wl,-T,%s", backend->GetFullName ( *module.linkerScript->file ).c_str () );
        else
                linkerScriptArgument = "";
 
        fprintf ( fMakefile,
                "%s: %s %s $(RSYM_TARGET) $(PEFIXUP_TARGET) | %s\n",
-               target.c_str (),
-               backend->GetFullName ( *definitionFilename ).c_str (),
+               target_macro.c_str (),
+               definitionFilename ? backend->GetFullName ( *definitionFilename ).c_str () : "",
                dependencies.c_str (),
                target_folder.c_str () );
        fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
@@ -1585,11 +1606,11 @@ MingwModuleHandler::GenerateLinkerCommand (
        if ( !module.IsDLL () )
        {
                fprintf ( fMakefile,
-                         "\t%s %s %s -o %s %s %s %s\n",
+                         "\t%s %s%s -o %s %s %s %s\n",
                          linker.c_str (),
                          linkerParameters.c_str (),
                          linkerScriptArgument.c_str (),
-                         target.c_str (),
+                         target_macro.c_str (),
                          objectsMacro.c_str (),
                          libsMacro.c_str (),
                          GetLinkerMacro ().c_str () );
@@ -1602,27 +1623,27 @@ MingwModuleHandler::GenerateLinkerCommand (
                CLEAN_FILE ( temp_exp );
 
                fprintf ( fMakefile,
-                         "\t${dlltool} --dllname %s --def %s --output-exp %s %s %s\n",
+                         "\t${dlltool} --dllname %s --def %s --output-exp %s%s%s\n",
                          targetName.c_str (),
-                         backend->GetFullName ( *definitionFilename ).c_str (),
+                         definitionFilename ? backend->GetFullName ( *definitionFilename ).c_str () : "",
                          backend->GetFullName ( temp_exp ).c_str (),
-                         module.mangledSymbols ? "" : "--kill-at",
-                         module.underscoreSymbols ? "--add-underscore" : "" );
+                         module.mangledSymbols ? "" : " --kill-at",
+                         module.underscoreSymbols ? " --add-underscore" : "" );
 
                fprintf ( fMakefile,
-                         "\t%s %s %s %s -o %s %s %s %s\n",
+                         "\t%s %s%s %s -o %s %s %s %s\n",
                          linker.c_str (),
                          linkerParameters.c_str (),
                          linkerScriptArgument.c_str (),
                          backend->GetFullName ( temp_exp ).c_str (),
-                         target.c_str (),
+                         target_macro.c_str (),
                          objectsMacro.c_str (),
                          libsMacro.c_str (),
                          GetLinkerMacro ().c_str () );
 
                fprintf ( fMakefile,
-                         "\t$(Q)$(PEFIXUP_TARGET) %s -exports %s\n",
-                         target.c_str (),
+                         "\t$(Q)$(PEFIXUP_TARGET) %s -exports%s\n",
+                         target_macro.c_str (),
                          pefixupParameters.c_str() );
 
                fprintf ( fMakefile,
@@ -1638,11 +1659,11 @@ MingwModuleHandler::GenerateLinkerCommand (
                //printf ( "%s will have all its functions exported\n",
                //         module.target->name.c_str () );
                fprintf ( fMakefile,
-                         "\t%s %s %s -o %s %s %s %s\n",
+                         "\t%s %s%s -o %s %s %s %s\n",
                          linker.c_str (),
                          linkerParameters.c_str (),
                          linkerScriptArgument.c_str (),
-                         target.c_str (),
+                         target_macro.c_str (),
                          objectsMacro.c_str (),
                          libsMacro.c_str (),
                          GetLinkerMacro ().c_str () );
@@ -1653,42 +1674,51 @@ MingwModuleHandler::GenerateLinkerCommand (
        GenerateRunRsymCode ();
        GenerateRunStripCode ();
        GenerateCleanObjectsAsYouGoCode ();
+
+       if ( definitionFilename )
+               delete definitionFilename;
+       delete target_file;
 }
 
 void
 MingwModuleHandler::GeneratePhonyTarget() const
 {
        string targetMacro ( GetTargetMacro ( module ) );
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
+
        fprintf ( fMakefile,
                  ".PHONY: %s\n\n",
                  targetMacro.c_str ());
        fprintf ( fMakefile, "%s: | %s\n",
                  targetMacro.c_str (),
-                 backend->GetFullPath ( *GetTargetFilename ( module, NULL ) ).c_str () );
+                 backend->GetFullPath ( *target_file ).c_str () );
+
+       delete target_file;
 }
 
 void
-MingwModuleHandler::GenerateObjectFileTargets (
-       const IfableData& data,
-       const string& cc,
-       const string& cppc,
-       const string& cflagsMacro,
-       const string& nasmflagsMacro,
-       const string& windresflagsMacro,
-       const string& widlflagsMacro )
+MingwModuleHandler::GenerateObjectFileTargets ( const IfableData& data )
 {
        size_t i;
+       string moduleDependencies;
 
        const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
+       for ( i = 0; i < compilationUnits.size (); i++ )
+       {
+               CompilationUnit& compilationUnit = *compilationUnits[i];
+               const FileLocation& compilationName = compilationUnit.GetFilename ();
+               const FileLocation *objectFilename = GetObjectFilename ( &compilationName, module );
+               if ( GetExtension ( *objectFilename ) == ".h" )
+                       moduleDependencies += ssprintf ( " $(%s_HEADERS)", module.name.c_str () );
+               else if ( GetExtension ( *objectFilename ) == ".rc" )
+                       moduleDependencies += ssprintf ( " $(%s_RESOURCES)", module.name.c_str () );
+               delete objectFilename;
+       }
+
        for ( i = 0; i < compilationUnits.size (); i++ )
        {
                GenerateCommands ( *compilationUnits[i],
-                                  cc,
-                                  cppc,
-                                  cflagsMacro,
-                                  nasmflagsMacro,
-                                  windresflagsMacro,
-                                  widlflagsMacro );
+                                  moduleDependencies );
                fprintf ( fMakefile,
                          "\n" );
        }
@@ -1696,13 +1726,7 @@ MingwModuleHandler::GenerateObjectFileTargets (
        const vector<If*>& ifs = data.ifs;
        for ( i = 0; i < ifs.size(); i++ )
        {
-               GenerateObjectFileTargets ( ifs[i]->data,
-                                           cc,
-                                           cppc,
-                                           cflagsMacro,
-                                           nasmflagsMacro,
-                                           windresflagsMacro,
-                                           widlflagsMacro );
+               GenerateObjectFileTargets ( ifs[i]->data );
        }
 
        vector<CompilationUnit*> sourceCompilationUnits;
@@ -1710,35 +1734,29 @@ MingwModuleHandler::GenerateObjectFileTargets (
        for ( i = 0; i < sourceCompilationUnits.size (); i++ )
        {
                GenerateCommands ( *sourceCompilationUnits[i],
-                                  cc,
-                                  cppc,
-                                  cflagsMacro,
-                                  nasmflagsMacro,
-                                  windresflagsMacro,
-                                  widlflagsMacro );
+                                  moduleDependencies );
        }
        CleanupCompilationUnitVector ( sourceCompilationUnits );
 }
 
 void
-MingwModuleHandler::GenerateObjectFileTargets (
-       const string& cc,
-       const string& cppc,
-       const string& cflagsMacro,
-       const string& nasmflagsMacro,
-       const string& windresflagsMacro,
-       const string& widlflagsMacro )
+MingwModuleHandler::GenerateObjectFileTargets ()
 {
-       if ( module.pch && use_pch )
+       const FileLocation *pchFilename = GetPrecompiledHeaderFilename ();
+
+       if ( pchFilename )
        {
-               const FileLocation& baseHeaderFile = module.pch->file;
-               const FileLocation *pchFilename = GetPrecompiledHeaderFilename ();
+               string cc = ( module.host == HostTrue ? "${host_gcc}" : "${gcc}" );
+               string cppc = ( module.host == HostTrue ? "${host_gpp}" : "${gpp}" );
+
+               const FileLocation& baseHeaderFile = *module.pch->file;
                CLEAN_FILE ( *pchFilename );
                string dependencies = backend->GetFullName ( baseHeaderFile );
                /* WIDL generated headers may be used */
                vector<FileLocation> rpcDependencies;
                GetRpcHeaderDependencies ( rpcDependencies );
-               dependencies += " " + v2s ( backend, rpcDependencies, 5 );
+               if ( rpcDependencies.size () > 0 )
+                       dependencies += " " + v2s ( backend, rpcDependencies, 5 );
                fprintf ( fMakefile,
                          "%s: %s | %s\n",
                          backend->GetFullName ( *pchFilename ).c_str(),
@@ -1751,48 +1769,39 @@ MingwModuleHandler::GenerateObjectFileTargets (
                          backend->GetFullName ( *pchFilename ).c_str(),
                          cflagsMacro.c_str(),
                          backend->GetFullName ( baseHeaderFile ).c_str() );
+               delete pchFilename;
        }
 
-       GenerateObjectFileTargets ( module.non_if_data,
-                                   cc,
-                                   cppc,
-                                   cflagsMacro,
-                                   nasmflagsMacro,
-                                   windresflagsMacro,
-                                   widlflagsMacro );
+       GenerateObjectFileTargets ( module.non_if_data );
        fprintf ( fMakefile, "\n" );
 }
 
+/* caller needs to delete the returned object */
 const FileLocation*
-MingwModuleHandler::GenerateArchiveTarget ( const string& ar,
-                                            const string& objs_macro ) const
+MingwModuleHandler::GenerateArchiveTarget ()
 {
        const FileLocation *archiveFilename = GetModuleArchiveFilename ();
+       const FileLocation *definitionFilename = GetDefinitionFilename ();
 
-       fprintf ( fMakefile,
-                 "%s: %s | %s\n",
-                 backend->GetFullName ( *archiveFilename ).c_str (),
-                 objs_macro.c_str (),
-                 backend->GetFullPath ( *archiveFilename ).c_str() );
+       arRule1.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
 
-       if ( module.type == StaticLibrary && module.importLibrary )
+       if ( IsStaticLibrary ( module ) && definitionFilename )
        {
-               const FileLocation *definitionFilename ( GetDefinitionFilename () );
-
                fprintf ( fMakefile,
-                         "\t${dlltool} --dllname %s --def %s --output-lib $@ %s %s\n",
+                         "\t${dlltool} --dllname %s --def %s --output-lib $@%s%s\n",
                          module.importLibrary->dllname.c_str (),
                          backend->GetFullName ( *definitionFilename ).c_str (),
-                         module.mangledSymbols ? "" : "--kill-at",
-                         module.underscoreSymbols ? "--add-underscore" : "" );
+                         module.mangledSymbols ? "" : " --kill-at",
+                         module.underscoreSymbols ? " --add-underscore" : "" );
        }
 
-       fprintf ( fMakefile, "\t$(ECHO_AR)\n" );
+       if ( definitionFilename )
+               delete definitionFilename;
 
-       fprintf ( fMakefile,
-                 "\t%s -rc $@ %s\n",
-                 ar.c_str (),
-                 objs_macro.c_str ());
+       if(module.type == HostStaticLibrary)
+               arHostRule2.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
+       else
+               arRule2.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
 
        GenerateCleanObjectsAsYouGoCode ();
 
@@ -1840,7 +1849,25 @@ MingwModuleHandler::GetModuleTargets ( const Module& module )
        if ( ReferenceObjects ( module ) )
                return GetObjectsMacro ( module );
        else
-               return backend->GetFullName ( *GetTargetFilename ( module, NULL ) ).c_str ();
+       {
+               const FileLocation *target_file = GetTargetFilename ( module, NULL );
+               string target = backend->GetFullName ( *target_file ).c_str ();
+               delete target_file;
+               return target;
+       }
+}
+
+void
+MingwModuleHandler::GenerateSourceMacro ()
+{
+       sourcesMacro = ssprintf ( "%s_SOURCES", module.name.c_str ());
+
+       GenerateSourceMacros (
+               "=",
+               module.non_if_data );
+
+       // future references to the macro will be to get its values
+       sourcesMacro = ssprintf ("$(%s)", sourcesMacro.c_str ());
 }
 
 void
@@ -1850,8 +1877,7 @@ MingwModuleHandler::GenerateObjectMacro ()
 
        GenerateObjectMacros (
                "=",
-               module.non_if_data,
-               &module.linkerFlags );
+               module.non_if_data );
 
        // future references to the macro will be to get its values
        objectsMacro = ssprintf ("$(%s)", objectsMacro.c_str ());
@@ -1875,22 +1901,41 @@ MingwModuleHandler::GetRpcHeaderDependencies (
                Library& library = *module.non_if_data.libraries[i];
                if ( library.importedModule->type == RpcServer ||
                     library.importedModule->type == RpcClient ||
+                    library.importedModule->type == RpcProxy ||
                     library.importedModule->type == IdlHeader )
                {
                        for ( size_t j = 0; j < library.importedModule->non_if_data.compilationUnits.size (); j++ )
                        {
                                CompilationUnit& compilationUnit = *library.importedModule->non_if_data.compilationUnits[j];
-                               const FileLocation* sourceFile = compilationUnit.GetFilename ();
-                               string extension = GetExtension ( *sourceFile );
+                               const FileLocation& sourceFile = compilationUnit.GetFilename ();
+                               string extension = GetExtension ( sourceFile );
                                if ( extension == ".idl" || extension == ".IDL" )
                                {
-                                       string basename = GetBasename ( sourceFile->name );
+                                       string basename = GetBasename ( sourceFile.name );
                                        if ( library.importedModule->type == RpcServer )
-                                               dependencies.push_back ( *GetRpcServerHeaderFilename ( sourceFile ) );
+                                       {
+                                               const FileLocation *header = GetRpcServerHeaderFilename ( &sourceFile );
+                                               dependencies.push_back ( *header );
+                                               delete header;
+                                       }
                                        if ( library.importedModule->type == RpcClient )
-                                               dependencies.push_back ( *GetRpcClientHeaderFilename ( sourceFile ) );
+                                       {
+                                               const FileLocation *header = GetRpcClientHeaderFilename ( &sourceFile );
+                                               dependencies.push_back ( *header );
+                                               delete header;
+                                       }
+                                       if ( library.importedModule->type == RpcProxy )
+                                       {
+                                               const FileLocation *header = GetRpcProxyHeaderFilename ( &sourceFile );
+                                               dependencies.push_back ( *header );
+                                               delete header;
+                                       }
                                        if ( library.importedModule->type == IdlHeader )
-                                               dependencies.push_back ( *GetIdlHeaderFilename ( sourceFile ) );
+                                       {
+                                               const FileLocation *header = GetIdlHeaderFilename ( &sourceFile );
+                                               dependencies.push_back ( *header );
+                                               delete header;
+                                       }
                                }
                        }
                }
@@ -1900,6 +1945,8 @@ MingwModuleHandler::GetRpcHeaderDependencies (
 void
 MingwModuleHandler::GenerateOtherMacros ()
 {
+       set<const Define *> used_defs;
+
        cflagsMacro = ssprintf ("%s_CFLAGS", module.name.c_str ());
        nasmflagsMacro = ssprintf ("%s_NASMFLAGS", module.name.c_str ());
        windresflagsMacro = ssprintf ("%s_RCFLAGS", module.name.c_str ());
@@ -1911,7 +1958,17 @@ MingwModuleHandler::GenerateOtherMacros ()
        GenerateMacros (
                "=",
                module.non_if_data,
-               &module.linkerFlags );
+               &module.linkerFlags,
+               used_defs );
+
+       if ( module.host == HostFalse )
+       {
+               GenerateMacros (
+                       "+=",
+                       module.project.non_if_data,
+                       NULL,
+                       used_defs );
+       }
 
        vector<FileLocation> s;
        if ( module.importLibrary )
@@ -1920,10 +1977,10 @@ MingwModuleHandler::GenerateOtherMacros ()
                for ( size_t i = 0; i < compilationUnits.size (); i++ )
                {
                        CompilationUnit& compilationUnit = *compilationUnits[i];
-                       const FileLocation* sourceFile = compilationUnit.GetFilename ();
-                       string extension = GetExtension ( *sourceFile );
+                       const FileLocation& sourceFile = compilationUnit.GetFilename ();
+                       string extension = GetExtension ( sourceFile );
                        if ( extension == ".spec" || extension == ".SPEC" )
-                               GetSpecObjectDependencies ( s, sourceFile );
+                               GetSpecObjectDependencies ( s, &sourceFile );
                }
        }
        if ( s.size () > 0 )
@@ -1939,7 +1996,12 @@ MingwModuleHandler::GenerateOtherMacros ()
                fprintf ( fMakefile, "\n" );
        }
 
-       string globalCflags = "-g";
+       string globalCflags = "";
+       if ( module.host == HostFalse )
+               globalCflags += " $(PROJECT_CFLAGS)";
+       else
+               globalCflags += " -Wall -Wpointer-arith -D__REACTOS__";
+       globalCflags += " -g";
        if ( backend->usePipe )
                globalCflags += " -pipe";
        if ( !module.allowWarnings )
@@ -1949,16 +2011,17 @@ MingwModuleHandler::GenerateOtherMacros ()
                if ( module.cplusplus )
                        globalCflags += " $(HOST_CPPFLAGS)";
                else
-                       globalCflags += " $(HOST_CFLAGS)";
+                       globalCflags += " -Wno-strict-aliasing $(HOST_CFLAGS)";
        }
        else
        {
-               globalCflags += " -nostdinc";
                if ( module.cplusplus )
                {
                        // HACK: use host headers when building C++
                        globalCflags += " $(HOST_CPPFLAGS)";
                }
+               else
+                       globalCflags += " -nostdinc";
        }
 
        // Always force disabling of sibling calls optimisation for GCC
@@ -1967,25 +2030,35 @@ MingwModuleHandler::GenerateOtherMacros ()
 
        fprintf (
                fMakefile,
-               "%s += $(PROJECT_CFLAGS) %s\n",
+               "%s +=%s\n",
                cflagsMacro.c_str (),
                globalCflags.c_str () );
 
-       fprintf (
-               fMakefile,
-               "%s += $(PROJECT_RCFLAGS)\n",
-               windresflagsMacro.c_str () );
+       if ( module.host == HostFalse )
+       {
+               fprintf (
+                       fMakefile,
+                       "%s += $(PROJECT_RCFLAGS)\n",
+                       windresflagsMacro.c_str () );
 
-       fprintf (
-               fMakefile,
-               "%s += $(PROJECT_WIDLFLAGS) -I%s\n",
-               widlflagsMacro.c_str (),
-               module.output->relative_path.c_str () );
+               fprintf (
+                       fMakefile,
+                       "%s += $(PROJECT_WIDLFLAGS) -I%s\n",
+                       widlflagsMacro.c_str (),
+                       module.output->relative_path.c_str () );
 
-       fprintf (
-               fMakefile,
-               "%s_LFLAGS += $(PROJECT_LFLAGS) -g\n",
-               module.name.c_str () );
+               fprintf (
+                       fMakefile,
+                       "%s_LFLAGS += $(PROJECT_LFLAGS) -g\n",
+                       module.name.c_str () );
+       }
+       else
+       {
+               fprintf (
+                       fMakefile,
+                       "%s_LFLAGS += $(HOST_LFLAGS)\n",
+                       module.name.c_str () );
+       }
 
        fprintf (
                fMakefile,
@@ -2020,7 +2093,7 @@ MingwModuleHandler::GenerateOtherMacros ()
                          linkerflags.c_str () );
        }
 
-       if ( module.type == StaticLibrary && module.isStartupLib )
+       if ( IsStaticLibrary ( module ) && module.isStartupLib )
        {
                fprintf ( fMakefile,
                          "%s += -Wno-main\n\n",
@@ -2038,18 +2111,6 @@ MingwModuleHandler::GenerateOtherMacros ()
 void
 MingwModuleHandler::GenerateRules ()
 {
-       string cc = ( module.host == HostTrue ? "${host_gcc}" : "${gcc}" );
-       string cppc = ( module.host == HostTrue ? "${host_gpp}" : "${gpp}" );
-       string ar = ( module.host == HostTrue ? "${host_ar}" : "${ar}" );
-
-       if ( module.name != "zlib" ) /* Avoid make warning */
-       {
-               FileLocation proxyMakefile ( OutputDirectory,
-                                            module.output->relative_path,
-                                           "makefile" );
-               CLEAN_FILE ( proxyMakefile );
-       }
-
        string targetMacro = GetTargetMacro ( module );
        //CLEAN_FILE ( targetMacro );
        CLEAN_FILE ( FileLocation ( SourceDirectory, "", targetMacro ) );
@@ -2072,16 +2133,11 @@ MingwModuleHandler::GenerateRules ()
 
        if ( !ReferenceObjects ( module ) )
        {
-               const FileLocation* ar_target = GenerateArchiveTarget ( ar, objectsMacro );
-               CLEAN_FILE ( *ar_target );
+               const FileLocation* ar_target = GenerateArchiveTarget ();
+               delete ar_target;
        }
 
-       GenerateObjectFileTargets ( cc,
-                                   cppc,
-                                   cflagsMacro,
-                                   nasmflagsMacro,
-                                   windresflagsMacro,
-                                   widlflagsMacro );
+       GenerateObjectFileTargets ();
 }
 
 void
@@ -2161,24 +2217,47 @@ MingwModuleHandler::GetDefaultDependencies (
        string_list& dependencies ) const
 {
        /* Avoid circular dependency */
-       if ( module.type != BuildTool
-               && module.name != "zlib"
-               && module.name != "hostzlib" )
+       if ( module.host == HostTrue )
+               return;
 
-               dependencies.push_back ( "$(INIT)" );
+       if (module.name != "psdk" && 
+               module.name != "dxsdk")
+       {
+               dependencies.push_back ( "$(PSDK_TARGET) $(psdk_HEADERS)" );
+               dependencies.push_back ( "$(DXSDK_TARGET) $(dxsdk_HEADERS)" );
+       }
 
-       if ( module.type != BuildTool
-               && module.name != "psdk" )
+       if (module.name != "errcodes" && 
+               module.name != "bugcodes" &&
+               module.name != "ntstatus")
+       {
+               dependencies.push_back ( "$(ERRCODES_TARGET) $(ERRCODES_MCHEADERS)" );
+               dependencies.push_back ( "$(BUGCODES_TARGET) $(BUGCODES_MCHEADERS)" );
+               dependencies.push_back ( "$(NTSTATUS_TARGET) $(NTSTATUS_MCHEADERS)" );
+       }
 
-               dependencies.push_back ( "$(PSDK_TARGET)" );
+       ///* Check if any dependent library relies on the generated headers */
+       //for ( size_t i = 0; i < module.project.modules.size (); i++ )
+       //{
+       //      const Module& m = *module.project.modules[i];
+       //      for ( size_t j = 0; j < m.non_if_data.compilationUnits.size (); j++ )
+       //      {
+       //              CompilationUnit& compilationUnit = *m.non_if_data.compilationUnits[j];
+       //              const FileLocation& sourceFile = compilationUnit.GetFilename ();
+       //              string extension = GetExtension ( sourceFile );
+       //              if (extension == ".mc" || extension == ".MC" )
+       //              {
+       //                      string dependency = ssprintf ( "$(%s_MCHEADERS)", m.name.c_str () );
+       //                      dependencies.push_back ( dependency );
+       //              }
+       //      }
+       //}
 }
 
 void
 MingwModuleHandler::GeneratePreconditionDependencies ()
 {
        string preconditionDependenciesName = GetPreconditionDependenciesName ();
-       vector<FileLocation> sourceFilenames;
-       GetSourceFilenamesWithoutGeneratedFiles ( sourceFilenames );
        string_list dependencies;
        GetDefaultDependencies ( dependencies );
        GetModuleDependencies ( dependencies );
@@ -2197,13 +2276,6 @@ MingwModuleHandler::GeneratePreconditionDependencies ()
                fprintf ( fMakefile, "\n\n" );
        }
 
-       for ( size_t i = 0; i < sourceFilenames.size(); i++ )
-       {
-               fprintf ( fMakefile,
-                         "%s: ${%s}\n",
-                         backend->GetFullName ( sourceFilenames[i] ).c_str (),
-                         preconditionDependenciesName.c_str ());
-       }
        fprintf ( fMakefile, "\n" );
 }
 
@@ -2217,23 +2289,22 @@ MingwModuleHandler::IsWineModule () const
        return ( index != string::npos );
 }
 
+/* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetDefinitionFilename () const
 {
-       if ( module.importLibrary != NULL )
-       {
-               DirectoryLocation directory;
-               if ( IsWineModule () )
-                       directory = IntermediateDirectory;
-               else
-                       directory = SourceDirectory;
+       if ( module.importLibrary == NULL )
+               return NULL;
 
-               return new FileLocation ( directory, 
-                                         module.importLibrary->source->relative_path,
-                                         module.importLibrary->source->name );
-       }
+       DirectoryLocation directory;
+       if ( IsWineModule () )
+               directory = IntermediateDirectory;
        else
-               return new FileLocation ( SourceDirectory, "tools" + sSep + "rbuild", "empty.def" );
+               directory = SourceDirectory;
+
+       return new FileLocation ( directory,
+                                 module.importLibrary->source->relative_path,
+                                 module.importLibrary->source->name );
 }
 
 void
@@ -2243,15 +2314,21 @@ MingwModuleHandler::GenerateImportLibraryTargetIfNeeded ()
        {
                const FileLocation *library_target = GetImportLibraryFilename ( module, &clean_files );
                const FileLocation *defFilename = GetDefinitionFilename ();
+               string empty = "tools" + sSep + "rbuild" + sSep + "empty.def";
 
                vector<FileLocation> deps;
                GetDefinitionDependencies ( deps );
 
                fprintf ( fMakefile, "# IMPORT LIBRARY RULE:\n" );
 
-               fprintf ( fMakefile, "%s: %s",
-                         backend->GetFullName ( *library_target ).c_str (),
-                         backend->GetFullName ( *defFilename ).c_str () );
+               fprintf ( fMakefile, "%s:",
+                         backend->GetFullName ( *library_target ).c_str () );
+
+               if ( defFilename )
+               {
+                       fprintf ( fMakefile, " %s",
+                                 backend->GetFullName ( *defFilename ).c_str () );
+               }
 
                size_t i, iend = deps.size();
                for ( i = 0; i < iend; i++ )
@@ -2259,17 +2336,22 @@ MingwModuleHandler::GenerateImportLibraryTargetIfNeeded ()
                                  backend->GetFullName ( deps[i] ).c_str () );
 
                fprintf ( fMakefile, " | %s\n",
-                         backend->GetFullPath ( *GetImportLibraryFilename ( module, NULL ) ).c_str () );
+                         backend->GetFullPath ( *library_target ).c_str () );
 
                fprintf ( fMakefile, "\t$(ECHO_DLLTOOL)\n" );
 
                fprintf ( fMakefile,
-                         "\t${dlltool} --dllname %s --def %s --output-lib %s %s %s\n\n",
+                         "\t${dlltool} --dllname %s --def %s --output-lib %s%s%s\n\n",
                          module.output->name.c_str (),
-                         backend->GetFullName ( *defFilename ).c_str (),
+                         defFilename ? backend->GetFullName ( *defFilename ).c_str ()
+                                     : empty.c_str (),
                          backend->GetFullName ( *library_target ).c_str (),
-                         module.mangledSymbols ? "" : "--kill-at",
-                         module.underscoreSymbols ? "--add-underscore" : "" );
+                         module.mangledSymbols ? "" : " --kill-at",
+                         module.underscoreSymbols ? " --add-underscore" : "" );
+
+               if ( defFilename )
+                       delete defFilename;
+               delete library_target;
        }
 }
 
@@ -2291,18 +2373,39 @@ MingwModuleHandler::GetSpecObjectDependencies (
        dependencies.push_back ( stubsDependency );
 }
 
+void
+MingwModuleHandler::GetMcObjectDependencies (
+       vector<FileLocation>& dependencies,
+       const FileLocation *file ) const
+{
+       string basename = GetBasename ( file->name );
+
+       FileLocation defDependency ( IntermediateDirectory,
+                                    "include/reactos",
+                                    basename + ".h" );
+       dependencies.push_back ( defDependency );
+
+       FileLocation stubsDependency ( IntermediateDirectory,
+                                      file->relative_path,
+                                    basename + ".rc" );
+       dependencies.push_back ( stubsDependency );
+}
+
 void
 MingwModuleHandler::GetWidlObjectDependencies (
        vector<FileLocation>& dependencies,
        const FileLocation *file ) const
 {
        string basename = GetBasename ( file->name );
+       const FileLocation *generatedHeaderFilename = GetRpcServerHeaderFilename ( file );
 
        FileLocation serverSourceDependency ( IntermediateDirectory,
                                              file->relative_path,
                                              basename + "_s.c" );
        dependencies.push_back ( serverSourceDependency );
-       dependencies.push_back ( *GetRpcServerHeaderFilename ( file ) );
+       dependencies.push_back ( *generatedHeaderFilename );
+
+       delete generatedHeaderFilename;
 }
 
 void
@@ -2312,15 +2415,15 @@ MingwModuleHandler::GetDefinitionDependencies (
        const vector<CompilationUnit*>& compilationUnits = module.non_if_data.compilationUnits;
        for ( size_t i = 0; i < compilationUnits.size (); i++ )
        {
-               CompilationUnit& compilationUnit = *compilationUnits[i];
-               const FileLocation* sourceFile = compilationUnit.GetFilename ();
-               string extension = GetExtension ( *sourceFile );
+               const CompilationUnit& compilationUnit = *compilationUnits[i];
+               const FileLocation& sourceFile = compilationUnit.GetFilename ();
+               string extension = GetExtension ( sourceFile );
                if ( extension == ".spec" || extension == ".SPEC" )
-                       GetSpecObjectDependencies ( dependencies, sourceFile );
+                       GetSpecObjectDependencies ( dependencies, &sourceFile );
                if ( extension == ".idl" || extension == ".IDL" )
                {
-                       if ( ( module.type == RpcServer ) || ( module.type == RpcClient ) )
-                               GetWidlObjectDependencies ( dependencies, sourceFile );
+                       if ( ( module.type == RpcServer ) || ( module.type == RpcClient ) || ( module.type == RpcProxy ) )
+                               GetWidlObjectDependencies ( dependencies, &sourceFile );
                }
        }
 }
@@ -2380,11 +2483,12 @@ MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ()
        else
                linker = "${host_gcc}";
 
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
        fprintf ( fMakefile, "%s: %s %s | %s\n",
                  targetMacro.c_str (),
                  objectsMacro.c_str (),
                  linkDepsMacro.c_str (),
-                 backend->GetFullPath ( *GetTargetFilename ( module, NULL ) ).c_str () );
+                 backend->GetFullPath ( *target_file ).c_str () );
        fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
        fprintf ( fMakefile,
                  "\t%s %s -o $@ %s %s\n\n",
@@ -2392,6 +2496,8 @@ MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ()
                  GetLinkerMacro ().c_str (),
                  objectsMacro.c_str (),
                  libsMacro.c_str () );
+
+       delete target_file;
 }
 
 
@@ -2413,9 +2519,7 @@ MingwKernelModuleHandler::GenerateKernelModuleTarget ()
 {
        string targetMacro ( GetTargetMacro ( module ) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2425,17 +2529,13 @@ MingwKernelModuleHandler::GenerateKernelModuleTarget ()
 
                string dependencies = linkDepsMacro + " " + objectsMacro;
 
-               string linkerParameters = ssprintf ( "-Wl,-T,%s%cntoskrnl.lnk -Wl,--subsystem,native -Wl,--entry,%s -Wl,--image-base,%s -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -shared",
-                                                    module.output->relative_path.c_str (),
-                                                    cSep,
-                                                    module.GetEntryPoint(true).c_str (),
+               string linkerParameters = ssprintf ( "-Wl,--subsystem,native -Wl,--entry,%s -Wl,--image-base,%s",
+                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
                                                     module.baseaddress.c_str () );
+
                GenerateLinkerCommand ( dependencies,
-                                       "${gcc}",
-                                       linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
-                                       "-sections" );
+                                       linkerParameters + " $(NTOSKRNL_SHARED)",
+                                       " -sections" );
        }
        else
        {
@@ -2464,6 +2564,26 @@ MingwStaticLibraryModuleHandler::GenerateStaticLibraryModuleTarget ()
 }
 
 
+MingwHostStaticLibraryModuleHandler::MingwHostStaticLibraryModuleHandler (
+       const Module& module_ )
+
+       : MingwModuleHandler ( module_ )
+{
+}
+
+void
+MingwHostStaticLibraryModuleHandler::Process ()
+{
+       GenerateHostStaticLibraryModuleTarget ();
+}
+
+void
+MingwHostStaticLibraryModuleHandler::GenerateHostStaticLibraryModuleTarget ()
+{
+       GenerateRules ();
+}
+
+
 MingwObjectLibraryModuleHandler::MingwObjectLibraryModuleHandler (
        const Module& module_ )
 
@@ -2522,9 +2642,7 @@ MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ()
 {
        string targetMacro ( GetTargetMacro ( module ) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2538,11 +2656,8 @@ MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ()
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       "${gcc}",
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
-                                       "-sections" );
+                                       " -sections" );
        }
        else
        {
@@ -2576,9 +2691,7 @@ MingwKernelModeDriverModuleHandler::GenerateKernelModeDriverModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ();
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2592,11 +2705,8 @@ MingwKernelModeDriverModuleHandler::GenerateKernelModeDriverModuleTarget ()
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       "${gcc}",
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
-                                       "-sections" );
+                                       " -sections" );
        }
        else
        {
@@ -2629,9 +2739,7 @@ MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2645,10 +2753,7 @@ MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ()
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       "${gcc}",
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -2682,9 +2787,7 @@ MingwNativeCUIModuleHandler::GenerateNativeCUIModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2698,10 +2801,7 @@ MingwNativeCUIModuleHandler::GenerateNativeCUIModuleTarget ()
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       "${gcc}",
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -2725,19 +2825,49 @@ MingwWin32OCXModuleHandler::MingwWin32OCXModuleHandler (
 {
 }
 
+static bool
+LinksToCrt( Module &module )
+{
+       for ( size_t i = 0; i < module.non_if_data.libraries.size (); i++ )
+       {
+               Library& library = *module.non_if_data.libraries[i];
+               if ( library.name == "libcntpr" || library.name == "crt" )
+                       return true;
+       }
+       return false;
+}
+
 static void
 MingwAddImplicitLibraries( Module &module )
 {
        Library* pLibrary;
+       bool links_to_crt;
+
+       if ( module.type != Win32DLL
+         && module.type != Win32OCX
+         && module.type != Win32CUI
+         && module.type != Win32GUI
+         && module.type != Win32SCR)
+       {
+               // no implicit libraries
+               return;
+       }
+
+       links_to_crt = LinksToCrt ( module );
 
        if ( !module.isDefaultEntryPoint )
        {
                if ( module.GetEntryPoint(false) == "0" )
                {
-                       pLibrary = new Library ( module, "mingw_common" );
-                       module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() , pLibrary );
-                       pLibrary = new Library ( module, "msvcrt" );
-                       module.non_if_data.libraries.push_back ( pLibrary );
+                       if ( !links_to_crt )
+                       {
+                               pLibrary = new Library ( module, "mingw_common" );
+                               module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() , pLibrary );
+
+                               pLibrary = new Library ( module, "msvcrt" );
+                               module.non_if_data.libraries.push_back ( pLibrary );
+                               links_to_crt = true;
+                       }
                }
                return;
        }
@@ -2756,7 +2886,7 @@ MingwAddImplicitLibraries( Module &module )
        pLibrary = new Library ( module, "mingw_common" );
        module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() + 1, pLibrary );
 
-       if ( module.name != "msvcrt" )
+       if ( !links_to_crt )
        {
                // always link in msvcrt to get the basic routines
                pLibrary = new Library ( module, "msvcrt" );
@@ -2782,9 +2912,7 @@ MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2794,20 +2922,11 @@ MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ()
 
                string dependencies = linkDepsMacro + " " + objectsMacro;
 
-               string linker;
-               if ( module.cplusplus )
-                       linker = "${gpp}";
-               else
-                       linker = "${gcc}";
-
                string linkerParameters = ssprintf ( "-Wl,--subsystem,console -Wl,--entry,%s -Wl,--image-base,%s -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -shared",
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       linker,
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -2835,9 +2954,7 @@ MingwWin32OCXModuleHandler::GenerateWin32OCXModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2847,20 +2964,11 @@ MingwWin32OCXModuleHandler::GenerateWin32OCXModuleTarget ()
 
                string dependencies = linkDepsMacro + " " + objectsMacro;
 
-               string linker;
-               if ( module.cplusplus )
-                       linker = "${gpp}";
-               else
-                       linker = "${gcc}";
-
                string linkerParameters = ssprintf ( "-Wl,--subsystem,console -Wl,--entry,%s -Wl,--image-base,%s -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -shared",
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       linker,
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -2895,9 +3003,7 @@ MingwWin32CUIModuleHandler::GenerateWin32CUIModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2907,20 +3013,11 @@ MingwWin32CUIModuleHandler::GenerateWin32CUIModuleTarget ()
 
                string dependencies = linkDepsMacro + " " + objectsMacro;
 
-               string linker;
-               if ( module.cplusplus )
-                       linker = "${gpp}";
-               else
-                       linker = "${gcc}";
-
                string linkerParameters = ssprintf ( "-Wl,--subsystem,console -Wl,--entry,%s -Wl,--image-base,%s -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000",
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       linker,
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -2955,9 +3052,7 @@ MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ()
 {
        string targetMacro ( GetTargetMacro (module) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -2967,20 +3062,11 @@ MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ()
 
                string dependencies = linkDepsMacro + " " + objectsMacro;
 
-               string linker;
-               if ( module.cplusplus )
-                       linker = "${gpp}";
-               else
-                       linker = "${gcc}";
-
                string linkerParameters = ssprintf ( "-Wl,--subsystem,windows -Wl,--entry,%s -Wl,--image-base,%s -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000",
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       linker,
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -3019,20 +3105,33 @@ MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget ()
 
        GenerateRules ();
 
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
        fprintf ( fMakefile, "%s: %s %s | %s\n",
                  targetMacro.c_str (),
                  objectsMacro.c_str (),
                  linkDepsMacro.c_str (),
-                 backend->GetFullPath ( *GetTargetFilename ( module, NULL ) ).c_str () );
+                 backend->GetFullPath ( *target_file ).c_str () );
 
        fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
 
-       fprintf ( fMakefile,
-                 "\t${ld} %s -N -Ttext=0x8000 -o %s %s %s\n",
-                 GetLinkerMacro ().c_str (),
-                 backend->GetFullName ( junk_tmp ).c_str (),
-                 objectsMacro.c_str (),
-                 linkDepsMacro.c_str () );
+       if (Environment::GetArch() == "arm")
+       {
+               fprintf ( fMakefile,
+                        "\t${gcc} -Wl,--subsystem,native -Wl,--section-start,startup=0x8000 -o %s %s %s %s\n",
+                        backend->GetFullName ( junk_tmp ).c_str (),
+                        objectsMacro.c_str (),
+                        linkDepsMacro.c_str (),
+                        GetLinkerMacro ().c_str ());
+       }
+       else
+       {
+               fprintf ( fMakefile,
+                        "\t${gcc} -Wl,--subsystem,native -Wl,-Ttext,0x8000 -o %s %s %s %s\n",
+                        backend->GetFullName ( junk_tmp ).c_str (),
+                        objectsMacro.c_str (),
+                        linkDepsMacro.c_str (),
+                        GetLinkerMacro ().c_str ());
+       }
        fprintf ( fMakefile,
                  "\t${objcopy} -O binary %s $@\n",
                  backend->GetFullName ( junk_tmp ).c_str () );
@@ -3040,6 +3139,8 @@ MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget ()
        fprintf ( fMakefile,
                  "\t-@${rm} %s 2>$(NUL)\n",
                  backend->GetFullName ( junk_tmp ).c_str () );
+
+       delete target_file;
 }
 
 
@@ -3109,17 +3210,18 @@ MingwBootProgramModuleHandler::GenerateBootProgramModuleTarget ()
 
        GenerateRules ();
 
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
        fprintf ( fMakefile, "%s: %s %s %s | %s\n",
                  targetMacro.c_str (),
                  objectsMacro.c_str (),
                  linkDepsMacro.c_str (),
                  payload->name.c_str (),
-                 backend->GetFullPath ( *GetTargetFilename ( module, NULL ) ).c_str () );
+                 backend->GetFullPath ( *target_file ).c_str () );
 
        fprintf ( fMakefile, "\t$(ECHO_BOOTPROG)\n" );
 
        fprintf ( fMakefile, "\t$(%s_PREPARE) $(OUTPUT)$(SEP)%s %s\n",
-               module.buildtype.c_str (), 
+               module.buildtype.c_str (),
                NormalizeFilename( backend->GetFullName ( *payload->output ) ).c_str (),
                backend->GetFullName ( junk_cpy ).c_str () );
 
@@ -3144,6 +3246,8 @@ MingwBootProgramModuleHandler::GenerateBootProgramModuleTarget ()
                  backend->GetFullName ( junk_tmp ).c_str (),
                  backend->GetFullName ( junk_elf ).c_str (),
                  backend->GetFullName ( junk_cpy ).c_str () );
+
+       delete target_file;
 }
 
 
@@ -3172,7 +3276,9 @@ MingwIsoModuleHandler::OutputBootstrapfileCopyCommands (
                if ( m.bootstrap != NULL )
                {
                        FileLocation targetFile ( OutputDirectory,
-                                                 bootcdDirectory + sSep + m.bootstrap->base,
+                                                 m.bootstrap->base.length () > 0
+                                                          ? bootcdDirectory + sSep + m.bootstrap->base
+                                                          : bootcdDirectory,
                                                  m.bootstrap->nameoncd );
                        OutputCopyCommand ( *m.output, targetFile );
                }
@@ -3207,7 +3313,9 @@ MingwIsoModuleHandler::GetBootstrapCdDirectories ( vector<FileLocation>& out,
                if ( m.bootstrap != NULL )
                {
                        FileLocation targetDirectory ( OutputDirectory,
-                                                      bootcdDirectory + sSep + m.bootstrap->base,
+                                                      m.bootstrap->base.length () > 0
+                                                          ? bootcdDirectory + sSep + m.bootstrap->base
+                                                          : bootcdDirectory,
                                                       "" );
                        out.push_back ( targetDirectory );
                }
@@ -3297,12 +3405,18 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
                vSourceFiles.push_back ( srcunattend );
 
        // bootsector
-       FileLocation isoboot ( OutputDirectory,
-                              "boot" + sSep + "freeldr" + sSep + "bootsect",
-                              module.type == IsoRegTest
-                                  ? "isobtrt.o"
-                                  : "isoboot.o" );
-       vSourceFiles.push_back ( isoboot );
+       const Module* bootModule = module.bootSector->bootSectorModule;
+
+       if (!bootModule)
+       {
+               throw InvalidOperationException ( module.node.location.c_str(),
+                                                                                 0,
+                                                                                 "Invalid bootsector. module '%s' requires <bootsector>",
+                                                                                 module.name.c_str ());
+       }
+
+       const FileLocation *isoboot = bootModule->output;
+       vSourceFiles.push_back ( *isoboot );
 
        // prepare reactos.dff and reactos.inf
        FileLocation reactosDff ( SourceDirectory,
@@ -3311,15 +3425,14 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
        FileLocation reactosInf ( bootcdReactos.directory,
                                  bootcdReactos.relative_path,
                                  "reactos.inf" );
-       vSourceFiles.push_back ( reactosDff );
 
-       string IsoName;
-
-       if (module.type == IsoRegTest)
-               IsoName = "ReactOS-RegTest.iso";
-       else
-               IsoName = "ReactOS.iso";
+       vSourceFiles.push_back ( reactosDff );
 
+       /* 
+               We use only the name and not full FileLocation(ouput) because Iso/LiveIso are an exception to the general rule.
+               Iso/LiveIso outputs are generated in code base root 
+       */
+       string IsoName = module.output->name;
 
        string sourceFiles = v2s ( backend, vSourceFiles, 5 );
 
@@ -3334,11 +3447,10 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
        fprintf ( fMakefile,
                  "%s: all %s %s %s $(CABMAN_TARGET) $(CDMAKE_TARGET) %s\n",
                  module.name.c_str (),
-                 backend->GetFullName ( isoboot ).c_str (),
+                 backend->GetFullName ( *isoboot ).c_str (),
                  sourceFiles.c_str (),
                  cdFiles.c_str (),
                  cdDirectories.c_str () );
-       fprintf ( fMakefile, "\t$(ECHO_CABMAN)\n" );
        fprintf ( fMakefile,
                  "\t$(Q)$(CABMAN_TARGET) -C %s -L %s -I -P $(OUTPUT)\n",
                  backend->GetFullName ( reactosDff ).c_str (),
@@ -3360,7 +3472,7 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
        fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
        fprintf ( fMakefile,
                  "\t$(Q)$(CDMAKE_TARGET) -v -j -m -b %s %s REACTOS %s\n",
-                 backend->GetFullName ( isoboot ).c_str (),
+                 backend->GetFullName ( *isoboot ).c_str (),
                  backend->GetFullPath ( bootcd ).c_str (),
                  IsoName.c_str() );
        fprintf ( fMakefile,
@@ -3482,21 +3594,26 @@ MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
        string livecdDirectory = module.name;
        FileLocation livecd ( OutputDirectory, livecdDirectory, "" );
 
-       string bootloader;
        string IsoName;
 
-       if (module.name == "livecdregtest")
-       {
-               bootloader = "isobtrt.o";
-               IsoName = "ReactOS-LiveCD-RegTest.iso";
-       }
-       else
+       // bootsector
+       const Module* bootModule = module.bootSector->bootSectorModule;
+
+       if (!bootModule)
        {
-               bootloader = "isoboot.o";
-               IsoName = "ReactOS-LiveCD.iso";
+               throw InvalidOperationException ( module.node.location.c_str(),
+                                                                                 0,
+                                                                                 "Invalid bootsector. module '%s' requires <bootsector>",
+                                                                                 module.name.c_str ());
        }
 
-       FileLocation isoboot ( OutputDirectory, "boot" + sSep + "freeldr" + sSep + "bootsect", bootloader );
+       const FileLocation *isoboot = bootModule->output;
+
+       /* 
+               We use only the name and not full FileLocation(ouput) because Iso/LiveIso are an exception to the general rule.
+               Iso/LiveIso outputs are generated in code base root 
+       */
+       IsoName = module.output->name;
 
        string reactosDirectory = "reactos";
        string livecdReactosNoFixup = livecdDirectory + sSep + reactosDirectory;
@@ -3510,7 +3627,7 @@ MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
        fprintf ( fMakefile,
                  "%s: all %s %s $(MKHIVE_TARGET) $(CDMAKE_TARGET)\n",
                  module.name.c_str (),
-                 backend->GetFullName ( isoboot) .c_str (),
+                 backend->GetFullName ( *isoboot) .c_str (),
                  backend->GetFullPath ( livecdReactos ).c_str () );
        OutputModuleCopyCommands ( livecdDirectory,
                                   reactosDirectory );
@@ -3522,7 +3639,7 @@ MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
        fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
        fprintf ( fMakefile,
                  "\t$(Q)$(CDMAKE_TARGET) -v -m -j -b %s %s REACTOS %s\n",
-                 backend->GetFullName( isoboot ).c_str (),
+                 backend->GetFullName( *isoboot ).c_str (),
                  backend->GetFullPath ( livecd ).c_str (),
                  IsoName.c_str() );
        fprintf ( fMakefile,
@@ -3543,6 +3660,7 @@ MingwTestModuleHandler::Process ()
        GenerateTestModuleTarget ();
 }
 
+/* caller needs to delete the returned object */
 void
 MingwTestModuleHandler::GetModuleSpecificCompilationUnits ( vector<CompilationUnit*>& compilationUnits )
 {
@@ -3556,9 +3674,7 @@ MingwTestModuleHandler::GenerateTestModuleTarget ()
 {
        string targetMacro ( GetTargetMacro ( module ) );
        string workingDirectory = GetWorkingDirectory ( );
-       string objectsMacro = GetObjectsMacro ( module );
        string linkDepsMacro = GetLinkingDependenciesMacro ();
-       string libsMacro = GetLibsMacro ();
 
        GenerateImportLibraryTargetIfNeeded ();
 
@@ -3568,20 +3684,11 @@ MingwTestModuleHandler::GenerateTestModuleTarget ()
 
                string dependencies = linkDepsMacro + " " + objectsMacro;
 
-               string linker;
-               if ( module.cplusplus )
-                       linker = "${gpp}";
-               else
-                       linker = "${gcc}";
-
                string linkerParameters = ssprintf ( "-Wl,--subsystem,console -Wl,--entry,%s -Wl,--image-base,%s -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000",
                                                     module.GetEntryPoint(true).c_str (),
                                                     module.baseaddress.c_str () );
                GenerateLinkerCommand ( dependencies,
-                                       linker,
                                        linkerParameters,
-                                       objectsMacro,
-                                       libsMacro,
                                        "" );
        }
        else
@@ -3619,6 +3726,20 @@ MingwRpcClientModuleHandler::Process ()
 }
 
 
+MingwRpcProxyModuleHandler::MingwRpcProxyModuleHandler (
+       const Module& module_ )
+
+       : MingwModuleHandler ( module_ )
+{
+}
+
+void
+MingwRpcProxyModuleHandler::Process ()
+{
+       GenerateRules ();
+}
+
+
 MingwAliasModuleHandler::MingwAliasModuleHandler (
        const Module& module_ )
 
@@ -3631,6 +3752,19 @@ MingwAliasModuleHandler::Process ()
 {
 }
 
+MingwMessageHeaderModuleHandler::MingwMessageHeaderModuleHandler (
+       const Module& module_ )
+
+       : MingwModuleHandler ( module_ )
+{
+}
+
+void
+MingwMessageHeaderModuleHandler::Process ()
+{
+       GenerateRules ();
+}
+
 MingwIdlHeaderModuleHandler::MingwIdlHeaderModuleHandler (
        const Module& module_ )
 
@@ -3644,6 +3778,32 @@ MingwIdlHeaderModuleHandler::Process ()
        GenerateRules ();
 }
 
+MingwCabinetModuleHandler::MingwCabinetModuleHandler (
+       const Module& module_ )
+
+       : MingwModuleHandler ( module_ )
+{
+}
+
+void
+MingwCabinetModuleHandler::Process ()
+{
+       string targetMacro ( GetTargetMacro (module) );
+
+       GenerateRules ();
+       
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
+       fprintf ( fMakefile, "%s: $(CABMAN_TARGET) | %s\n",
+                 targetMacro.c_str (),
+                 backend->GetFullPath ( *target_file ).c_str () );
+
+       fprintf ( fMakefile, "\t$(ECHO_CABMAN)\n" );
+       fprintf ( fMakefile,
+                 "\t$(Q)$(CABMAN_TARGET) -M raw -S %s $(%s_SOURCES)\n",      // Escape the asterisk for Make
+                 targetMacro.c_str (),
+                         module.name.c_str());
+}
+
 MingwElfExecutableModuleHandler::MingwElfExecutableModuleHandler (
        const Module& module_ )
 
@@ -3663,17 +3823,20 @@ MingwElfExecutableModuleHandler::Process ()
 
        GenerateRules ();
 
+       const FileLocation *target_file = GetTargetFilename ( module, NULL );
        fprintf ( fMakefile, "%s: %s %s | %s\n",
                  targetMacro.c_str (),
                  objectsMacro.c_str (),
                  linkDepsMacro.c_str (),
-                 backend->GetFullPath ( *GetTargetFilename ( module, NULL ) ).c_str () );
+                 backend->GetFullPath ( *target_file ).c_str () );
 
        fprintf ( fMakefile, "\t$(ECHO_BOOTPROG)\n" );
 
-       fprintf ( fMakefile, "\t${ld} $(%s_LINKFORMAT) %s %s -g -o %s\n",
+       fprintf ( fMakefile, "\t${gcc} $(%s_LINKFORMAT) %s %s -g -o %s\n",
                  module.buildtype.c_str(),
                  objectsMacro.c_str(),
                  libsMacro.c_str(),
                  targetMacro.c_str () );
+
+       delete target_file;
 }