[RBUILD]
[reactos.git] / reactos / tools / rbuild / backend / mingw / modulehandler.cpp
index b1c70eb..fe0a268 100644 (file)
@@ -12,9 +12,9 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 #include "../../pch.h"
 #include <assert.h>
@@ -63,7 +63,6 @@ string
 GetTargetMacro ( const Module& module, bool with_dollar )
 {
        string s ( module.name );
-       strupr ( &s[0] );
        s += "_TARGET";
        if ( with_dollar )
                return ssprintf ( "$(%s)", s.c_str() );
@@ -291,9 +290,7 @@ MingwModuleHandler::GetModuleArchiveFilename () const
 {
        if ( IsStaticLibrary ( module ) )
                return GetTargetFilename ( module, NULL );
-       return new FileLocation ( IntermediateDirectory,
-                                 module.output->relative_path,
-                                 ReplaceExtension ( module.name, ".temp.a" ) );
+       return NULL;
 }
 
 /*static*/ bool
@@ -519,6 +516,9 @@ MingwModuleHandler::GenerateCleanTarget () const
        }
        fprintf ( fMakefile, " 2>$(NUL)\n" );
 
+       // Clean files generated by external rules
+       fprintf ( fMakefile, "\t-@${rm} $(%s_CLEANFILES) 2>$(NUL)\n", module.name.c_str() );
+
        if( ProxyMakefile::GenerateProxyMakefile(module) )
        {
                DirectoryLocation root;
@@ -1021,7 +1021,7 @@ MingwModuleHandler::GenerateObjectMacros (
        }
        CleanupCompilationUnitVector ( sourceCompilationUnits );
 
-       if ( IsSpecDefinitionFile() )
+       if ( module.IsSpecDefinitionFile() )
        {
                const FileLocation *stubs_file = new FileLocation(
                        IntermediateDirectory,
@@ -1157,9 +1157,9 @@ Rule nasmRule ( "$(eval $(call RBUILD_NASM,$(module_name),$(source),$(dependenci
                 "$(intermediate_dir)$(SEP)", NULL );
 
 /* TODO: move these to rules.mak */
-Rule wmcRule ( "$(intermediate_path_noext).rc $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h: $(WMC_TARGET) $(source) | $(intermediate_dir)\n"
+Rule wmcRule ( "$(intermediate_path_noext).rc $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h: $(wmc_TARGET) $(source) | $(intermediate_dir)\n"
                "\t$(ECHO_WMC)\n"
-               "\t$(Q)$(WMC_TARGET) -i -H $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h -o $(intermediate_path_noext).rc $(source)\n",
+               "\t$(Q)$(wmc_TARGET) -i -H $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h -o $(intermediate_path_noext).rc $(source)\n",
                "$(intermediate_path_noext).rc",
                "$(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h",
                "$(intermediate_dir)$(SEP)", NULL );
@@ -1360,25 +1360,6 @@ MingwModuleHandler::GenerateBuildMapCode ( const FileLocation *mapTarget )
                  "endif\n" );
 }
 
-void
-MingwModuleHandler::GenerateBuildNonSymbolStrippedCode ()
-{
-       fprintf ( fMakefile, "# BUILD NO STRIP CODE\n" );
-
-       fprintf ( fMakefile,
-                 "ifeq ($(ROS_BUILDNOSTRIP),yes)\n" );
-
-       FileLocation nostripFilename ( OutputDirectory,
-                                      module.output->relative_path,
-                                      GetBasename ( module.output->name ) + ".nostrip" + GetExtension ( *module.output ) );
-       CLEAN_FILE ( nostripFilename );
-
-       OutputCopyCommand ( *module.output, nostripFilename );
-
-       fprintf ( fMakefile,
-                 "endif\n" );
-}
-
 void
 MergeStringVector ( const Backend* backend,
                     const vector<FileLocation>& input,
@@ -1439,158 +1420,61 @@ MingwModuleHandler::GenerateCleanObjectsAsYouGoCode () const
 }
 
 void
-MingwModuleHandler::GenerateRunRsymCode () const
+MingwModuleHandler::GenerateLinkerCommand () const
 {
-       fprintf ( fMakefile, "# RUN RSYM CODE\n" );
-       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
-MingwModuleHandler::GenerateRunStripCode () const
-{
-       fprintf ( fMakefile, "# RUN STRIP CODE\n" );
-       fprintf ( fMakefile,
-                 "ifeq ($(ROS_LEAN_AND_MEAN),yes)\n" );
-       fprintf ( fMakefile,
-                 "\t$(ECHO_STRIP)\n" );
-       fprintf ( fMakefile,
-                 "\t${strip} -s -x -X $@\n\n" );
-       fprintf ( fMakefile,
-                 "endif\n" );
-}
-
-void
-MingwModuleHandler::GenerateLinkerCommand (
-       const string& dependencies,
-       const string& linkerParameters,
-       const string& pefixupParameters )
-{
-       const FileLocation *target_file = GetTargetFilename ( module, NULL );
-       const FileLocation *definitionFilename = GetDefinitionFilename ();
-       string linker = "${ld}";
-       string objectsMacro = GetObjectsMacro ( module );
-       string libsMacro = GetLibsMacro ();
-
-       fprintf ( fMakefile, "# LINKER COMMAND\n" );
+       string definitionFilename;
+       
+       const FileLocation *DefinitionFilename = GetDefinitionFilename ();
 
-       string target_macro ( GetTargetMacro ( module ) );
-       string target_folder ( backend->GetFullPath ( *target_file ) );
+       if ( DefinitionFilename ) {
+               definitionFilename = backend->GetFullName (*DefinitionFilename);
+               delete DefinitionFilename;
+       }
 
        string linkerScriptArgument;
-       if ( module.linkerScript != NULL )
-               linkerScriptArgument = ssprintf ( " -T %s", backend->GetFullName ( *module.linkerScript->file ).c_str () );
-       else
-               linkerScriptArgument = "";
-
-       /* check if we need to add default C++ libraries, ie if we have
-        * a C++ user-mode module without the -nostdlib linker flag
-        */
-       bool link_defaultlibs = module.cplusplus &&
-                               linkerParameters.find ("-nostdlib") == string::npos &&
-                               !(module.type == KernelModeDLL || module.type == KernelModeDriver);
-
-       if ( !module.HasImportLibrary() )
-       {
-               fprintf ( fMakefile,
-                       "%s: %s %s $(RSYM_TARGET) $(PEFIXUP_TARGET) | %s\n",
-                       target_macro.c_str (),
-                       definitionFilename ? backend->GetFullName ( *definitionFilename ).c_str () : "",
-                       dependencies.c_str (),
-                       target_folder.c_str () );
-               fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
-
-               fprintf ( fMakefile,
-                         "\t%s %s%s %s %s %s %s -o %s\n",
-                         linker.c_str (),
-                         linkerParameters.c_str (),
-                         linkerScriptArgument.c_str (),
-                         objectsMacro.c_str (),
-                         link_defaultlibs ? "$(PROJECT_LPPFLAGS) " : "",
-                         libsMacro.c_str (),
-                         GetLinkerMacro ().c_str (),
-                         target_macro.c_str () );
+       if ( module.linkerScript != NULL ) {
+               if ( module.project.configuration.Linker == GnuLd )
+                       linkerScriptArgument = ssprintf ( " -T %s", backend->GetFullName ( *module.linkerScript->file ).c_str () );
+               else
+                       fprintf ( stderr,
+                                         "Linker doesn't support linker scripts: linker script %s ignored for module %s\n",
+                                         backend->GetFullName ( *module.linkerScript->file ).c_str (),
+                                         module.name.c_str() );
        }
-       else
-       {
-               FileLocation temp_exp ( IntermediateDirectory,
-                                       module.output->relative_path,
-                                       module.name + ".exp" );
-               CLEAN_FILE ( temp_exp );
 
-               fprintf ( fMakefile,
-                       "%s: %s | %s\n",
-                       backend->GetFullName ( temp_exp ).c_str (),
-                       definitionFilename ? backend->GetFullName ( *definitionFilename ).c_str () : "",
-                       backend->GetFullPath ( temp_exp ).c_str () );
-               fprintf ( fMakefile, "\t$(ECHO_DLLTOOL)\n" );
-
-               fprintf ( fMakefile,
-                         "\t${dlltool} --dllname %s --def %s --output-exp $@%s%s\n",
-                         module.GetDllName ().c_str (),
-                         definitionFilename ? backend->GetFullName ( *definitionFilename ).c_str () : "",
-                         module.mangledSymbols ? "" : " --kill-at",
-                         module.underscoreSymbols ? " --add-underscore" : "" );
-
-               fprintf ( fMakefile,
-                       "%s: %s %s $(RSYM_TARGET) $(PEFIXUP_TARGET) | %s\n",
-                       target_macro.c_str (),
-                       backend->GetFullName ( temp_exp ).c_str (),
-                       dependencies.c_str (),
-                       target_folder.c_str () );
-               fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
-
-               fprintf ( fMakefile,
-                         "\t%s %s%s %s %s %s %s %s -o %s\n",
-
-                         linker.c_str (),
-                         linkerParameters.c_str (),
-                         linkerScriptArgument.c_str (),
-                         backend->GetFullName ( temp_exp ).c_str (),
-                         objectsMacro.c_str (),
-                         link_defaultlibs ? "$(PROJECT_LPPFLAGS) " : "",
-                         libsMacro.c_str (),
-                         GetLinkerMacro ().c_str (),
-                         target_macro.c_str () );
+       string extraLibraries;
+       if ( ModuleHandlerInformations[module.type].DefaultHost == HostFalse ) {
+               if ( module.cplusplus ) {
+                       switch ( module.type )
+                       {
+                       case Win32DLL:
+                       case Win32OCX:
+                       case Win32CUI:
+                       case Win32GUI:
+                       case Win32SCR:
+                               extraLibraries = "$$(PROJECT_CXXLIBS)";
+                               break;
 
-               fprintf ( fMakefile,
-                         "\t$(Q)$(PEFIXUP_TARGET) %s -exports%s\n",
-                         target_macro.c_str (),
-                         pefixupParameters.c_str() );
+                       default:
+                               extraLibraries = "$$(PROJECT_CCLIBS)";
+                               break;
+                       }
+               } else
+                       extraLibraries = "$$(PROJECT_CCLIBS)";
        }
 
-       GenerateBuildMapCode ();
-       GenerateBuildNonSymbolStrippedCode ();
-       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 );
+       delete PassThruCacheDirectory ( new FileLocation ( module.output->directory, module.output->relative_path, "" ) );
+       delete PassThruCacheDirectory ( new FileLocation ( IntermediateDirectory, module.output->relative_path, "" ) );
 
-       fprintf ( fMakefile, "# PHONY TARGET\n" );
        fprintf ( fMakefile,
-                 ".PHONY: %s\n\n",
-                 targetMacro.c_str ());
-       fprintf ( fMakefile, "%s: | %s\n",
-                 targetMacro.c_str (),
-                 backend->GetFullPath ( *target_file ).c_str () );
-
-       delete target_file;
+                         "$(eval $(call RBUILD_LINK_RULE,%s,%s,%s,%s,%s,%s,%s))\n",
+                         module.name.c_str(),
+                         definitionFilename.c_str(),
+                         module.xmlbuildFile.c_str(),
+                         linkerScriptArgument.c_str(),
+                         extraLibraries.c_str(),
+                         module.GetEntryPoint().c_str(),
+                         module.baseaddress.c_str() );
 }
 
 void
@@ -1599,8 +1483,6 @@ MingwModuleHandler::GenerateObjectFileTargets ( const IfableData& data )
        size_t i;
        string moduleDependencies;
 
-       fprintf ( fMakefile, "# OBJECT FILE TARGETS\n" );
-
        const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
        for ( i = 0; i < compilationUnits.size (); i++ )
        {
@@ -1643,8 +1525,6 @@ MingwModuleHandler::GenerateObjectFileTargets ( const IfableData& data )
 void
 MingwModuleHandler::GenerateObjectFileTargets ()
 {
-       fprintf ( fMakefile, "# OBJECT FILE TARGETS\n" );
-
        if ( module.pch && use_pch )
        {
 
@@ -1661,12 +1541,9 @@ MingwModuleHandler::GenerateObjectFileTargets ()
                        pchCxxRule.Execute ( fMakefile, backend, module, module.pch->file, clean_files, dependencies );
                else
                        pchRule.Execute ( fMakefile, backend, module, module.pch->file, clean_files, dependencies );
-
-               fprintf ( fMakefile, "\n" );
        }
 
        GenerateObjectFileTargets ( module.non_if_data );
-       fprintf ( fMakefile, "\n" );
 }
 
 /* caller needs to delete the returned object */
@@ -1674,6 +1551,10 @@ const FileLocation*
 MingwModuleHandler::GenerateArchiveTarget ()
 {
        const FileLocation *archiveFilename = GetModuleArchiveFilename ();
+
+       if ( archiveFilename == NULL )
+               return NULL;
+
        const FileLocation *definitionFilename = GetDefinitionFilename ();
 
        fprintf ( fMakefile, "# ARCHIVE TARGET\n" );
@@ -1688,11 +1569,8 @@ MingwModuleHandler::GenerateArchiveTarget ()
                                                  backend->GetFullName ( *definitionFilename ).c_str () );
 
                fprintf ( fMakefile,
-                         "\t${dlltool} --dllname %s --def %s --output-lib $@%s%s\n",
-                         module.GetDllName ().c_str (),
-                         backend->GetFullName ( *definitionFilename ).c_str (),
-                         module.mangledSymbols ? "" : " --kill-at",
-                         module.underscoreSymbols ? " --add-underscore" : "" );
+                         "\t${dlltool} --def %s --kill-at --output-lib $@\n",
+                         backend->GetFullName ( *definitionFilename ).c_str () );
        }
        else
                arRule1.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
@@ -1728,7 +1606,7 @@ MingwModuleHandler::GetLibsMacro () const
 string
 MingwModuleHandler::GetLinkerMacro () const
 {
-       return ssprintf ( "$(%s_LFLAGS)",
+       return ssprintf ( "$(%s_LDFLAGS)",
                          module.name.c_str () );
 }
 
@@ -1843,7 +1721,7 @@ MingwModuleHandler::GenerateOtherMacros ()
 {
        set<const Define *> used_defs;
 
-       linkerflagsMacro = ssprintf ("%s_LFLAGS", module.name.c_str ());
+       linkerflagsMacro = ssprintf ("%s_LDFLAGS", module.name.c_str ());
        libsMacro = ssprintf("%s_LIBS", module.name.c_str ());
 
        const FileLocation * pchPath = GetPrecompiledHeaderPath ();
@@ -1898,28 +1776,6 @@ MingwModuleHandler::GenerateOtherMacros ()
 
        GenerateParameters ( module.name.c_str(), "+=", module.non_if_data );
 
-       const char *linkerflags = ModuleHandlerInformations[module.type].linkerflags;
-       if ( strlen( linkerflags ) > 0 )
-       {
-               fprintf ( fMakefile,
-                         "%s += %s\n\n",
-                         linkerflagsMacro.c_str (),
-                         linkerflags );
-       }
-
-       // FIXME: make rules for linker, move standard flags there
-       if ( ModuleHandlerInformations[module.type].DefaultHost == HostFalse )
-       {
-               if ( module.cplusplus )
-                       fprintf ( fMakefile,
-                                         "%s+= $(PROJECT_LPPFLAGS)\n\n",
-                                         linkerflagsMacro.c_str () );
-               else
-                       fprintf ( fMakefile,
-                                         "%s+= $(PROJECT_LFLAGS)\n\n",
-                                         linkerflagsMacro.c_str () );
-       }
-
        GenerateMacros (
                "+=",
                module.non_if_data,
@@ -1944,7 +1800,7 @@ MingwModuleHandler::GenerateRules ()
                module.name.c_str () );
        string dependencies = GetTargetMacro ( module );
        if ( module.type == Test )
-               dependencies += " $(REGTESTS_RUN_TARGET)";
+               dependencies += " $(regtests_run_TARGET)";
        fprintf ( fMakefile, "%s: %s\n\n",
                module.name.c_str (),
                dependencies.c_str () );
@@ -1958,11 +1814,13 @@ MingwModuleHandler::GenerateRules ()
        if ( !ReferenceObjects ( module ) )
        {
                const FileLocation* ar_target = GenerateArchiveTarget ();
-               delete ar_target;
+
+               if ( ar_target )
+                       delete ar_target;
        }
 
 
-    spec = IsSpecDefinitionFile();
+    spec = module.IsSpecDefinitionFile();
 
     if(spec)
        {
@@ -2064,17 +1922,17 @@ MingwModuleHandler::GetDefaultDependencies (
        if (module.name != "psdk" &&
                module.name != "dxsdk")
        {
-               dependencies.push_back ( "$(PSDK_TARGET) $(psdk_HEADERS)" );
-               dependencies.push_back ( "$(DXSDK_TARGET) $(dxsdk_HEADERS)" );
+               dependencies.push_back ( "$(psdk_TARGET) $(psdk_HEADERS)" );
+               dependencies.push_back ( "$(dxsdk_TARGET) $(dxsdk_HEADERS)" );
        }
 
        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 ( "$(errcodes_TARGET) $(ERRCODES_MCHEADERS)" );
+               dependencies.push_back ( "$(bugcodes_TARGET) $(BUGCODES_MCHEADERS)" );
+               dependencies.push_back ( "$(ntstatus_TARGET) $(NTSTATUS_MCHEADERS)" );
        }
 
        ///* Check if any dependent library relies on the generated headers */
@@ -2121,23 +1979,6 @@ MingwModuleHandler::GeneratePreconditionDependencies ()
        fprintf ( fMakefile, "\n" );
 }
 
-SpecFileType
-MingwModuleHandler::IsSpecDefinitionFile () const
-{
-    if(!module.importLibrary)
-        return None;
-
-       std::string ext = GetExtension ( *module.importLibrary->source );
-
-    if ( ext == ".spec" )
-        return Spec;
-
-    if ( ext == ".pspec" )
-        return PSpec;
-
-    return None;
-}
-
 /* caller needs to delete the returned object */
 const FileLocation*
 MingwModuleHandler::GetDefinitionFilename () const
@@ -2145,7 +1986,7 @@ MingwModuleHandler::GetDefinitionFilename () const
        if ( module.importLibrary == NULL )
                return NULL;
 
-       if ( IsSpecDefinitionFile () )
+       if ( module.IsSpecDefinitionFile () )
        {
                return new FileLocation ( IntermediateDirectory,
                                                                  module.importLibrary->source->relative_path,
@@ -2159,61 +2000,6 @@ MingwModuleHandler::GetDefinitionFilename () const
        }
 }
 
-void
-MingwModuleHandler::GenerateImportLibraryTarget (
-       const FileLocation *defFilename,
-       const FileLocation *library_target,
-       bool delayimp)
-{
-       string empty = "tools" + sSep + "rbuild" + sSep + "empty.def";
-
-       fprintf ( fMakefile, "# IMPORT LIBRARY RULE\n" );
-
-       fprintf ( fMakefile, "%s:",
-                 backend->GetFullName ( *library_target ).c_str () );
-
-       if ( defFilename )
-       {
-               fprintf ( fMakefile, " %s",
-                         backend->GetFullName ( *defFilename ).c_str () );
-       }
-
-       fprintf ( fMakefile, " | %s\n",
-                 backend->GetFullPath ( *library_target ).c_str () );
-
-       fprintf ( fMakefile, "\t$(ECHO_DLLTOOL)\n" );
-
-       fprintf ( fMakefile,
-                 "\t${dlltool} --dllname %s --def %s %s %s%s%s\n\n",
-                 module.GetDllName ().c_str (),
-                 defFilename ? backend->GetFullName ( *defFilename ).c_str ()
-                             : empty.c_str (),
-                 delayimp ? "--output-delaylib" : "--output-lib",
-                 backend->GetFullName ( *library_target ).c_str (),
-                 module.mangledSymbols ? "" : " --kill-at",
-                 module.underscoreSymbols ? " --add-underscore" : "" );
-}
-
-void
-MingwModuleHandler::GenerateImportLibraryTargetIfNeeded ()
-{
-       if ( module.importLibrary != NULL )
-       {
-               const FileLocation *library_target = GetImportLibraryFilename ( module, &clean_files, false );
-               const FileLocation *delayimp_target = GetImportLibraryFilename ( module, &clean_files, true );
-               const FileLocation *defFilename = GetDefinitionFilename ();
-
-               GenerateImportLibraryTarget(defFilename, library_target, false);
-               GenerateImportLibraryTarget(defFilename, delayimp_target, true);
-
-               if ( defFilename )
-                       delete defFilename;
-               delete library_target;
-               delete delayimp_target;
-
-       }
-}
-
 void
 MingwModuleHandler::GetSpecObjectDependencies (
        vector<FileLocation>& dependencies,
@@ -2403,30 +2189,8 @@ MingwKernelModuleHandler::Process ()
 void
 MingwKernelModuleHandler::GenerateKernelModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro ( module ) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=native -entry=%s -image-base=%s",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters + " $(NTOSKRNL_SHARED)",
-                                       " -sections" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2454,29 +2218,8 @@ MingwKernelModeDLLModuleHandler::Process ()
 void
 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro ( module ) );
-       string workingDirectory = GetWorkingDirectory ();
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=native -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000 -shared",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       " -sections" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2503,29 +2246,8 @@ MingwNativeDLLModuleHandler::Process ()
 void
 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=native -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000 -shared",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2552,29 +2274,8 @@ MingwNativeCUIModuleHandler::Process ()
 void
 MingwNativeCUIModuleHandler::GenerateNativeCUIModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=native -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2642,29 +2343,8 @@ MingwWin32DLLModuleHandler::Process ()
 void
 MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=console -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000 -shared",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2683,29 +2363,8 @@ MingwWin32OCXModuleHandler::Process ()
 void
 MingwWin32OCXModuleHandler::GenerateWin32OCXModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=console -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000 -shared",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2731,29 +2390,8 @@ MingwWin32CUIModuleHandler::Process ()
 void
 MingwWin32CUIModuleHandler::GenerateWin32CUIModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=console -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2779,29 +2417,8 @@ MingwWin32GUIModuleHandler::Process ()
 void
 MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=windows -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -2822,54 +2439,62 @@ void
 MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget ()
 {
        fprintf ( fMakefile, "# BOOT LOADER MODULE TARGET\n" );
-       string targetName ( module.output->name );
        string targetMacro ( GetTargetMacro (module) );
-       string workingDirectory = GetWorkingDirectory ();
-       FileLocation junk_tmp ( TemporaryDirectory,
-                               "",
-                               module.name + ".junk.tmp" );
-       CLEAN_FILE ( junk_tmp );
        string objectsMacro = GetObjectsMacro ( module );
        string libsMacro = GetLibsMacro ();
 
        GenerateRules ();
+       
+       string objectsDir = "${call RBUILD_intermediate_dir,$(" + module.name + "_TARGET)}";
+       string rspFile = objectsDir + "$(SEP)" + module.name + "_objs.rsp";
+
+       /* Generate the rsp rule */
+       fprintf(fMakefile, "%s: $(%s_OBJS) %s | %s\n"
+               "\t$(ECHO_RSP)\n"
+               "\t-@${rm} $@ 2>$(NUL)\n"
+               "\t${cp} $(NUL) $@ >$(NUL)\n"
+               "\t$(foreach obj,$(%s_LIBS),$(Q)echo $(QUOTE)$(subst \\,\\\\,$(obj))$(QUOTE)>>$@$(NL))\n\n",
+               rspFile.c_str(),
+               module.name.c_str(),
+               module.xmlbuildFile.c_str(),
+               objectsDir.c_str(),
+               module.name.c_str());
 
        const FileLocation *target_file = GetTargetFilename ( module, NULL );
        fprintf ( fMakefile, "%s: %s %s | %s\n",
                  targetMacro.c_str (),
-                 objectsMacro.c_str (),
+                 rspFile.c_str(),
                  libsMacro.c_str (),
                  backend->GetFullPath ( *target_file ).c_str () );
 
        fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
 
-       if (Environment::GetArch() == "arm")
-       {
-               fprintf ( fMakefile,
-                        "\t${gcc} -Wl,--subsystem,native -o %s %s %s %s\n",
-                        backend->GetFullName ( junk_tmp ).c_str (),
-                        objectsMacro.c_str (),
-                        libsMacro.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 (),
-                        libsMacro.c_str (),
-                        GetLinkerMacro ().c_str ());
-       }
-       fprintf ( fMakefile,
-                 "\t${objcopy} -O binary %s $@\n",
-                 backend->GetFullName ( junk_tmp ).c_str () );
-       GenerateBuildMapCode ( &junk_tmp );
-       fprintf ( fMakefile,
-                 "\t-@${rm} %s 2>$(NUL)\n",
-                 backend->GetFullName ( junk_tmp ).c_str () );
-
-       delete target_file;
+       string linkerScriptArgument;
+       if ( module.linkerScript != NULL ) {
+        linkerScriptArgument = ssprintf(" -T %s", backend->GetFullName(*module.linkerScript->file).c_str());
+       }
+
+    /* Link the stripped booloader */
+    fprintf(fMakefile,
+            "\t${ld} --strip-all --subsystem native --entry=%s --image-base=%s @%s $(PROJECT_CCLIBS) "
+            "$(BUILTIN_LDFLAGS) $(PROJECT_LDFLAGS) $(LDFLAG_DRIVER) %s -o $@\n",
+            module.GetEntryPoint().c_str(),
+            module.baseaddress.c_str(),
+            rspFile.c_str(),
+            linkerScriptArgument.c_str() );
+
+    /* Link an unstripped version */
+       fprintf(fMakefile,
+               "ifeq ($(ROS_BUILDNOSTRIP),yes)\n"
+               "\t${ld} --subsystem native --entry=%s --image-base=%s @%s $(PROJECT_CCLIBS) "
+               "$(BUILTIN_LDFLAGS) $(PROJECT_LDFLAGS) $(LDFLAG_DRIVER) %s -o %s$(SEP)%s.nostrip.sys\n"
+               "endif\n",
+               module.GetEntryPoint().c_str(),
+               module.baseaddress.c_str(),
+            rspFile.c_str(),
+               linkerScriptArgument.c_str(),
+               backend->GetFullPath(*target_file).c_str(),
+               module.name.c_str());
 }
 
 
@@ -3138,16 +2763,16 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
 
        fprintf ( fMakefile, ".PHONY: %s_CABINET\n\n",
                  module.name.c_str () );
-       fprintf ( fMakefile, "%s_CABINET: all $(CABMAN_TARGET) %s | %s\n",
+       fprintf ( fMakefile, "%s_CABINET: all $(cabman_TARGET) %s | %s\n",
                  module.name.c_str (),
                  backend->GetFullName ( reactosDff ).c_str (),
                  backend->GetFullPath ( bootcdReactos ).c_str () );
        fprintf ( fMakefile,
-                 "\t$(Q)$(CABMAN_TARGET) -C %s -L %s -I -P $(OUTPUT)\n",
+                 "\t$(Q)$(cabman_TARGET) -C %s -L %s -I -P $(OUTPUT)\n",
                  backend->GetFullName ( reactosDff ).c_str (),
                  backend->GetFullPath ( bootcdReactos ).c_str () );
        fprintf ( fMakefile,
-                 "\t$(Q)$(CABMAN_TARGET) -C %s -RC %s -L %s -N -P $(OUTPUT)\n",
+                 "\t$(Q)$(cabman_TARGET) -C %s -RC %s -L %s -N -P $(OUTPUT)\n",
                  backend->GetFullName ( reactosDff ).c_str (),
                  backend->GetFullName ( reactosInf ).c_str (),
                  backend->GetFullPath ( bootcdReactos ).c_str ());
@@ -3167,7 +2792,7 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
        fprintf ( fMakefile, ".PHONY: %s\n\n",
                  module.name.c_str ());
        fprintf ( fMakefile,
-                 "%s: $(%s_OBJS) %s_CABINET %s $(CDMAKE_TARGET) | %s\n",
+                 "%s: $(%s_OBJS) %s_CABINET %s $(cdmake_TARGET) | %s\n",
                  module.name.c_str (),
                  module.name.c_str (),
                  module.name.c_str (),
@@ -3178,7 +2803,7 @@ MingwIsoModuleHandler::GenerateIsoModuleTarget ()
 
        fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
        fprintf ( fMakefile,
-                 "\t$(Q)$(CDMAKE_TARGET) -v -j -m -b %s %s REACTOS %s\n",
+                 "\t$(Q)$(cdmake_TARGET) -v -j -m -b %s %s REACTOS %s\n",
                  backend->GetFullName ( *isoboot ).c_str (),
                  backend->GetFullPath ( bootcd ).c_str (),
                  IsoName.c_str() );
@@ -3299,7 +2924,7 @@ MingwLiveIsoModuleHandler::OutputRegistryCommands ( string& livecdDirectory )
        fprintf ( fMakefile,
                  "\t$(ECHO_MKHIVE)\n" );
        fprintf ( fMakefile,
-                 "\t$(MKHIVE_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%clivecd.inf boot%cbootdata%chiveinst_$(ARCH).inf\n",
+                 "\t$(mkhive_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%clivecd.inf boot%cbootdata%chiveinst_$(ARCH).inf\n",
                  cSep, backend->GetFullPath ( reactosSystem32ConfigDirectory ).c_str (),
                  cSep, cSep, cSep, cSep );
 }
@@ -3357,7 +2982,7 @@ MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
        fprintf ( fMakefile, ".PHONY: %s\n\n",
                  module.name.c_str ());
        fprintf ( fMakefile,
-                 "%s : $(%s_OBJS) %s %s $(MKHIVE_TARGET) $(CDMAKE_TARGET)\n",
+                 "%s : $(%s_OBJS) %s %s $(mkhive_TARGET) $(cdmake_TARGET)\n",
                  module.name.c_str (),
                  module.name.c_str (),
                  backend->GetFullName ( *isoboot) .c_str (),
@@ -3367,7 +2992,7 @@ MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
        OutputRegistryCommands ( livecdDirectory );
        fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
        fprintf ( fMakefile,
-                 "\t$(Q)$(CDMAKE_TARGET) -v -m -j -b %s %s REACTOS %s\n",
+                 "\t$(Q)$(cdmake_TARGET) -v -m -j -b %s %s REACTOS %s\n",
                  backend->GetFullName( *isoboot ).c_str (),
                  backend->GetFullPath ( livecd ).c_str (),
                  IsoName.c_str() );
@@ -3401,29 +3026,8 @@ MingwTestModuleHandler::GetModuleSpecificCompilationUnits ( vector<CompilationUn
 void
 MingwTestModuleHandler::GenerateTestModuleTarget ()
 {
-       string targetMacro ( GetTargetMacro ( module ) );
-       string workingDirectory = GetWorkingDirectory ( );
-       string libsMacro = GetLibsMacro ();
-
-       GenerateImportLibraryTargetIfNeeded ();
-
-       if ( module.non_if_data.compilationUnits.size () > 0 )
-       {
-               GenerateRules ();
-
-               string dependencies = libsMacro + " " + objectsMacro;
-
-               string linkerParameters = ssprintf ( "-subsystem=console -entry=%s -image-base=%s -file-alignment=0x1000 -section-alignment=0x1000",
-                                                    module.GetEntryPoint(!(Environment::GetArch() == "arm")).c_str (),
-                                                    module.baseaddress.c_str () );
-               GenerateLinkerCommand ( dependencies,
-                                       linkerParameters,
-                                       "" );
-       }
-       else
-       {
-               GeneratePhonyTarget();
-       }
+       GenerateRules ();
+       GenerateLinkerCommand ();
 }
 
 
@@ -3456,13 +3060,13 @@ MingwCabinetModuleHandler::Process ()
        GenerateRules ();
 
        const FileLocation *target_file = GetTargetFilename ( module, NULL );
-       fprintf ( fMakefile, "%s: $(CABMAN_TARGET) | %s\n",
+       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
+                 "\t$(Q)$(cabman_TARGET) -M raw -S %s $(%s_SOURCES)\n",      // Escape the asterisk for Make
                  targetMacro.c_str (),
                          module.name.c_str());
 }