remove unnecessary call to c_str()
[reactos.git] / reactos / tools / rbuild / backend / mingw / modulehandler.cpp
index 4c6e34a..b9195b5 100644 (file)
@@ -12,24 +12,35 @@ using std::map;
 \r
 map<ModuleType,MingwModuleHandler*>*\r
 MingwModuleHandler::handler_map = NULL;\r
+int\r
+MingwModuleHandler::ref = 0;\r
 \r
 FILE*\r
 MingwModuleHandler::fMakefile = NULL;\r
 \r
 MingwModuleHandler::MingwModuleHandler ( ModuleType moduletype )\r
 {\r
-       if ( !handler_map )\r
+       if ( !ref++ )\r
                handler_map = new map<ModuleType,MingwModuleHandler*>;\r
        (*handler_map)[moduletype] = this;\r
 }\r
 \r
-/*static*/ void\r
+MingwModuleHandler::~MingwModuleHandler()\r
+{\r
+       if ( !--ref )\r
+       {\r
+               delete handler_map;\r
+               handler_map = NULL;\r
+       }\r
+}\r
+\r
+void\r
 MingwModuleHandler::SetMakefile ( FILE* f )\r
 {\r
        fMakefile = f;\r
 }\r
 \r
-/*static*/ MingwModuleHandler*\r
+MingwModuleHandler*\r
 MingwModuleHandler::LookupHandler ( const string& location,\r
                                     ModuleType moduletype )\r
 {\r
@@ -72,7 +83,7 @@ MingwModuleHandler::ReplaceExtension ( const string& filename,
 string\r
 MingwModuleHandler::GetModuleArchiveFilename ( const Module& module ) const\r
 {\r
-       return ReplaceExtension ( FixupTargetFilename ( module.GetPath () ).c_str (),\r
+       return ReplaceExtension ( FixupTargetFilename ( module.GetPath () ),\r
                                  ".a" );\r
 }\r
 \r
@@ -89,7 +100,7 @@ MingwModuleHandler::GetImportLibraryDependencies ( const Module& module ) const
                        dependencies += " ";\r
                const Module* importedModule = module.project.LocateModule ( module.libraries[i]->name );\r
                assert ( importedModule != NULL );\r
-               dependencies += FixupTargetFilename ( importedModule->GetPath () ).c_str ();\r
+               dependencies += FixupTargetFilename ( importedModule->GetDependencyPath () ).c_str ();\r
        }\r
        return dependencies;\r
 }\r
@@ -128,15 +139,20 @@ MingwModuleHandler::GetAllDependencies ( const Module& module ) const
 string\r
 MingwModuleHandler::GetSourceFilenames ( const Module& module ) const\r
 {\r
-       if ( module.files.size () == 0 )\r
-               return "";\r
-       \r
+       size_t i;\r
+\r
        string sourceFilenames ( "" );\r
-       for ( size_t i = 0; i < module.files.size (); i++ )\r
+       for ( i = 0; i < module.files.size (); i++ )\r
+               sourceFilenames += " " + module.files[i]->name;\r
+       vector<If*> ifs = module.ifs;\r
+       for ( i = 0; i < ifs.size(); i++ )\r
        {\r
-               if ( sourceFilenames.size () > 0 )\r
-                       sourceFilenames += " ";\r
-               sourceFilenames += module.files[i]->name;\r
+               size_t j;\r
+               If& rIf = *ifs[i];\r
+               for ( j = 0; j < rIf.ifs.size(); j++ )\r
+                       ifs.push_back ( rIf.ifs[j] );\r
+               for ( j = 0; j < rIf.files.size(); j++ )\r
+                       sourceFilenames += " " + rIf.files[j]->name;\r
        }\r
        return sourceFilenames;\r
 }\r
@@ -144,8 +160,14 @@ MingwModuleHandler::GetSourceFilenames ( const Module& module ) const
 string\r
 MingwModuleHandler::GetObjectFilename ( const string& sourceFilename ) const\r
 {\r
+       string newExtension;\r
+       string extension = GetExtension ( sourceFilename );\r
+       if ( extension == ".rc" || extension == ".RC" )\r
+               newExtension = ".coff";\r
+       else\r
+               newExtension = ".o";\r
        return FixupTargetFilename ( ReplaceExtension ( sourceFilename,\r
-                                                       ".o" ) );\r
+                                                           newExtension ) );\r
 }\r
 \r
 string\r
@@ -223,16 +245,6 @@ MingwModuleHandler::GenerateGccIncludeParametersFromVector ( const vector<Includ
        return parameters;\r
 }\r
 \r
-void\r
-MingwModuleHandler::GenerateGccModuleIncludeVariable ( const Module& module ) const\r
-{\r
-       string name ( module.name + "_INCLUDES" );\r
-       fprintf ( fMakefile,\r
-                 "%s := %s\n",\r
-                 name.c_str(),\r
-                 GenerateGccIncludeParameters(module).c_str() );\r
-}\r
-\r
 string\r
 MingwModuleHandler::GenerateGccIncludeParameters ( const Module& module ) const\r
 {\r
@@ -246,54 +258,272 @@ MingwModuleHandler::GenerateGccIncludeParameters ( const Module& module ) const
        return parameters;\r
 }\r
 \r
+\r
 string\r
-MingwModuleHandler::GenerateGccParameters ( const Module& module ) const\r
+MingwModuleHandler::GenerateLinkerParametersFromVector ( const vector<LinkerFlag*>& linkerFlags ) const\r
 {\r
-       string parameters = GenerateGccDefineParameters ( module );\r
-       parameters += ssprintf(" $(%s_INCLUDES)",module.name.c_str());\r
+       string parameters;\r
+       for ( size_t i = 0; i < linkerFlags.size (); i++ )\r
+       {\r
+               LinkerFlag& linkerFlag = *linkerFlags[i];\r
+               if ( parameters.length () > 0 )\r
+                       parameters += " ";\r
+               parameters += linkerFlag.flag;\r
+       }\r
        return parameters;\r
 }\r
 \r
+string\r
+MingwModuleHandler::GenerateLinkerParameters ( const Module& module ) const\r
+{\r
+       return GenerateLinkerParametersFromVector ( module.linkerFlags );\r
+}\r
+\r
+void\r
+MingwModuleHandler::GenerateMacro ( const char* assignmentOperation,\r
+                                    const string& macro,\r
+                                    const vector<Include*>& includes,\r
+                                    const vector<Define*>& defines ) const\r
+{\r
+       size_t i;\r
+       \r
+       fprintf (\r
+               fMakefile,\r
+               "%s %s",\r
+               macro.c_str(),\r
+               assignmentOperation );\r
+       for ( i = 0; i < includes.size(); i++ )\r
+       {\r
+               fprintf (\r
+                       fMakefile,\r
+                       " -I%s",\r
+                       includes[i]->directory.c_str() );\r
+       }\r
+       for ( i = 0; i < defines.size(); i++ )\r
+       {\r
+               Define& d = *defines[i];\r
+               fprintf (\r
+                       fMakefile,\r
+                       " -D%s",\r
+                       d.name.c_str() );\r
+               if ( d.value.size() )\r
+                       fprintf (\r
+                               fMakefile,\r
+                               "=%s",\r
+                               d.value.c_str() );\r
+       }\r
+       fprintf ( fMakefile, "\n" );\r
+}\r
+       \r
+void\r
+MingwModuleHandler::GenerateMacros (\r
+       const char* assignmentOperation,\r
+       const vector<File*>& files,\r
+       const vector<Include*>& includes,\r
+       const vector<Define*>& defines,\r
+       const vector<LinkerFlag*>* linkerFlags,\r
+       const vector<If*>& ifs,\r
+       const string& cflags_macro,\r
+       const string& nasmflags_macro,\r
+       const string& windresflags_macro,\r
+       const string& linkerflags_macro,\r
+       const string& objs_macro) const\r
+{\r
+       size_t i;\r
+\r
+       if ( includes.size() || defines.size() )\r
+       {\r
+               GenerateMacro ( assignmentOperation,\r
+                               cflags_macro,\r
+                               includes,\r
+                               defines );\r
+               GenerateMacro ( assignmentOperation,\r
+                               windresflags_macro,\r
+                               includes,\r
+                               defines );\r
+       }\r
+       \r
+       if ( linkerFlags != NULL )\r
+       {\r
+               string linkerParameters = GenerateLinkerParametersFromVector ( *linkerFlags );\r
+               if ( linkerParameters.size () > 0 )\r
+               {\r
+                       fprintf (\r
+                               fMakefile,\r
+                               "%s %s %s\n",\r
+                               linkerflags_macro.c_str (),\r
+                               assignmentOperation,\r
+                               linkerParameters.c_str() );\r
+               }\r
+       }\r
+       \r
+       if ( files.size() )\r
+       {\r
+               fprintf (\r
+                       fMakefile,\r
+                       "%s %s",\r
+                       objs_macro.c_str(),\r
+                       assignmentOperation );\r
+               for ( i = 0; i < files.size(); i++ )\r
+               {\r
+                       fprintf (\r
+                               fMakefile,\r
+                               "%s%s",\r
+                               ( i%10 == 9 ? "\\\n\t" : " " ),\r
+                               GetObjectFilename(files[i]->name).c_str() );\r
+               }\r
+               fprintf ( fMakefile, "\n" );\r
+       }\r
+\r
+       for ( i = 0; i < ifs.size(); i++ )\r
+       {\r
+               If& rIf = *ifs[i];\r
+               if ( rIf.defines.size() || rIf.includes.size() || rIf.files.size() || rIf.ifs.size() )\r
+               {\r
+                       fprintf (\r
+                               fMakefile,\r
+                               "ifeq (\"$(%s)\",\"%s\")\n",\r
+                               rIf.property.c_str(),\r
+                               rIf.value.c_str() );\r
+                       GenerateMacros (\r
+                               "+=",\r
+                               rIf.files,\r
+                               rIf.includes,\r
+                               rIf.defines,\r
+                               NULL,\r
+                               rIf.ifs,\r
+                               cflags_macro,\r
+                               nasmflags_macro,\r
+                               windresflags_macro,\r
+                               linkerflags_macro,\r
+                               objs_macro );\r
+                       fprintf ( \r
+                               fMakefile,\r
+                               "endif\n\n" );\r
+               }\r
+       }\r
+}\r
+\r
+void\r
+MingwModuleHandler::GenerateMacros (\r
+       const Module& module,\r
+       const string& cflags_macro,\r
+       const string& nasmflags_macro,\r
+       const string& windresflags_macro,\r
+       const string& linkerflags_macro,\r
+       const string& objs_macro) const\r
+{\r
+       GenerateMacros (\r
+               "=",\r
+               module.files,\r
+               module.includes,\r
+               module.defines,\r
+               &module.linkerFlags,\r
+               module.ifs,\r
+               cflags_macro,\r
+               nasmflags_macro,\r
+               windresflags_macro,\r
+               linkerflags_macro,\r
+               objs_macro );\r
+       fprintf ( fMakefile, "\n" );\r
+\r
+       fprintf (\r
+               fMakefile,\r
+               "%s += $(PROJECT_CFLAGS)\n\n",\r
+               cflags_macro.c_str () );\r
+\r
+       fprintf (\r
+               fMakefile,\r
+               "%s += $(PROJECT_RCFLAGS)\n\n",\r
+               windresflags_macro.c_str () );\r
+\r
+       fprintf (\r
+               fMakefile,\r
+               "%s_LFLAGS += $(PROJECT_LFLAGS)\n\n",\r
+               module.name.c_str () );\r
+}\r
+\r
 string\r
 MingwModuleHandler::GenerateGccCommand ( const Module& module,\r
-                                            const string& sourceFilename,\r
-                                            const string& cc ) const\r
+                                         const string& sourceFilename,\r
+                                         const string& cc,\r
+                                         const string& cflagsMacro ) const\r
 {\r
        string objectFilename = GetObjectFilename ( sourceFilename );\r
        return ssprintf ( "%s -c %s -o %s %s\n",\r
                              cc.c_str (),\r
                              sourceFilename.c_str (),\r
                              objectFilename.c_str (),\r
-                             GenerateGccParameters ( module ).c_str () );\r
+                             cflagsMacro.c_str () );\r
 }\r
 \r
 string\r
 MingwModuleHandler::GenerateGccAssemblerCommand ( const Module& module,\r
-                                                     const string& sourceFilename,\r
-                                                     const string& cc ) const\r
+                                                  const string& sourceFilename,\r
+                                                  const string& cc,\r
+                                                  const string& cflagsMacro ) const\r
 {\r
        string objectFilename = GetObjectFilename ( sourceFilename );\r
        return ssprintf ( "%s -x assembler-with-cpp -c %s -o %s -D__ASM__ %s\n",\r
-                             cc.c_str (),\r
+                         cc.c_str (),\r
+                         sourceFilename.c_str (),\r
+                         objectFilename.c_str (),\r
+                         cflagsMacro.c_str () );\r
+}\r
+\r
+string\r
+MingwModuleHandler::GenerateNasmCommand ( const Module& module,\r
+                                          const string& sourceFilename,\r
+                                          const string& nasmflagsMacro ) const\r
+{\r
+       string objectFilename = GetObjectFilename ( sourceFilename );\r
+       return ssprintf ( "%s -f win32 %s -o %s %s\n",\r
+                             "nasm",\r
                              sourceFilename.c_str (),\r
                              objectFilename.c_str (),\r
-                             GenerateGccParameters ( module ).c_str () );\r
+                             nasmflagsMacro.c_str () );\r
+}\r
+\r
+string\r
+MingwModuleHandler::GenerateWindresCommand ( const Module& module,\r
+                                             const string& sourceFilename,\r
+                                             const string& windresflagsMacro ) const\r
+{\r
+       string objectFilename = GetObjectFilename ( sourceFilename );\r
+       return ssprintf ( "%s %s -o %s ${%s}\n",\r
+                             "${windres}",\r
+                             sourceFilename.c_str (),\r
+                             objectFilename.c_str (),\r
+                             windresflagsMacro.c_str () );\r
 }\r
 \r
 string\r
 MingwModuleHandler::GenerateCommand ( const Module& module,\r
-                                         const string& sourceFilename,\r
-                                         const string& cc ) const\r
+                                      const string& sourceFilename,\r
+                                      const string& cc,\r
+                                      const string& cflagsMacro,\r
+                                      const string& nasmflagsMacro,\r
+                                      const string& windresflagsMacro ) const\r
 {\r
        string extension = GetExtension ( sourceFilename );\r
        if ( extension == ".c" || extension == ".C" )\r
                return GenerateGccCommand ( module,\r
                                            sourceFilename,\r
-                                           cc );\r
+                                           cc,\r
+                                           cflagsMacro );\r
        else if ( extension == ".s" || extension == ".S" )\r
                return GenerateGccAssemblerCommand ( module,\r
                                                     sourceFilename,\r
-                                                    cc );\r
+                                                    cc,\r
+                                                    cflagsMacro );\r
+       else if ( extension == ".asm" || extension == ".ASM" )\r
+               return GenerateNasmCommand ( module,\r
+                                            sourceFilename,\r
+                                            nasmflagsMacro );\r
+       else if ( extension == ".rc" || extension == ".RC" )\r
+               return GenerateWindresCommand ( module,\r
+                                               sourceFilename,\r
+                                               windresflagsMacro );\r
 \r
        throw InvalidOperationException ( __FILE__,\r
                                          __LINE__,\r
@@ -302,18 +532,37 @@ MingwModuleHandler::GenerateCommand ( const Module& module,
                                          sourceFilename.c_str () );\r
 }\r
 \r
+string\r
+MingwModuleHandler::GenerateLinkerCommand ( const Module& module,\r
+                                            const string& linker,\r
+                                            const string& linkerParameters,\r
+                                            const string& objectFilenames ) const\r
+{\r
+       string target ( FixupTargetFilename ( module.GetPath () ) );\r
+       string importLibraryDependencies = GetImportLibraryDependencies ( module );\r
+       return ssprintf ( "%s %s -o %s %s %s %s\n",\r
+                         linker.c_str (),\r
+                             linkerParameters.c_str (),\r
+                      target.c_str (),\r
+                      objectFilenames.c_str (),\r
+                      importLibraryDependencies.c_str (),\r
+                      GetLinkerMacro ( module ).c_str () );\r
+}\r
+\r
 void\r
 MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,\r
-                                                   const string& cc) const\r
+                                                const vector<File*>& files,\r
+                                                const vector<If*>& ifs,\r
+                                                const string& cc,\r
+                                                const string& cflagsMacro,\r
+                                                const string& nasmflagsMacro,\r
+                                                const string& windresflagsMacro ) const\r
 {\r
-       if ( module.files.size () == 0 )\r
-               return;\r
-       \r
-       GenerateGccModuleIncludeVariable ( module );\r
+       size_t i;\r
 \r
-       for ( size_t i = 0; i < module.files.size (); i++ )\r
+       for ( i = 0; i < files.size (); i++ )\r
        {\r
-               string sourceFilename = module.files[i]->name;\r
+               string sourceFilename = files[i]->name;\r
                string objectFilename = GetObjectFilename ( sourceFilename );\r
                fprintf ( fMakefile,\r
                          "%s: %s\n",\r
@@ -323,58 +572,151 @@ MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,
                          "\t%s\n",\r
                          GenerateCommand ( module,\r
                                            sourceFilename,\r
-                                           cc ).c_str () );\r
+                                           cc,\r
+                                           cflagsMacro,\r
+                                           nasmflagsMacro,\r
+                                           windresflagsMacro ).c_str () );\r
        }\r
-       \r
-       fprintf ( fMakefile, "\n" );\r
+\r
+       for ( i = 0; i < ifs.size(); i++ )\r
+               GenerateObjectFileTargets ( module,\r
+                                           ifs[i]->files,\r
+                                           ifs[i]->ifs,\r
+                                           cc,\r
+                                           cflagsMacro,\r
+                                           nasmflagsMacro,\r
+                                           windresflagsMacro );\r
 }\r
 \r
 void\r
-MingwModuleHandler::GenerateObjectFileTargetsHost ( const Module& module ) const\r
+MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,\r
+                                                const string& cc,\r
+                                                const string& cflagsMacro,\r
+                                                const string& nasmflagsMacro,\r
+                                                const string& windresflagsMacro ) const\r
 {\r
        GenerateObjectFileTargets ( module,\r
-                                   "${host_gcc}" );\r
+                                   module.files,\r
+                                   module.ifs,\r
+                                   cc,\r
+                                   cflagsMacro,\r
+                                   nasmflagsMacro,\r
+                                   windresflagsMacro );\r
+       fprintf ( fMakefile, "\n" );\r
 }\r
 \r
 void\r
-MingwModuleHandler::GenerateObjectFileTargetsTarget ( const Module& module ) const\r
+MingwModuleHandler::GetCleanTargets ( vector<string>& out,\r
+                                      const vector<File*>& files,\r
+                                      const vector<If*>& ifs ) const\r
 {\r
-       GenerateObjectFileTargets ( module,\r
-                                   "${gcc}" );\r
+       size_t i;\r
+\r
+       for ( i = 0; i < files.size(); i++ )\r
+               out.push_back ( GetObjectFilename(files[i]->name) );\r
+\r
+       for ( i = 0; i < ifs.size(); i++ )\r
+               GetCleanTargets ( out, ifs[i]->files, ifs[i]->ifs );\r
 }\r
 \r
-void\r
+string\r
 MingwModuleHandler::GenerateArchiveTarget ( const Module& module,\r
-                                               const string& ar ) const\r
+                                            const string& ar,\r
+                                            const string& objs_macro ) const\r
 {\r
        string archiveFilename = GetModuleArchiveFilename ( module );\r
-       string sourceFilenames = GetSourceFilenames ( module );\r
-       string objectFilenames = GetObjectFilenames ( module );\r
        \r
        fprintf ( fMakefile,\r
                  "%s: %s\n",\r
                  archiveFilename.c_str (),\r
-                 objectFilenames.c_str ());\r
+                 objs_macro.c_str ());\r
 \r
        fprintf ( fMakefile,\r
                  "\t%s -rc %s %s\n\n",\r
                  ar.c_str (),\r
                  archiveFilename.c_str (),\r
-                 objectFilenames.c_str ());\r
+                 objs_macro.c_str ());\r
+\r
+       return archiveFilename;\r
+}\r
+\r
+string\r
+MingwModuleHandler::GetObjectsMacro ( const Module& module ) const\r
+{\r
+       return ssprintf ( "$(%s_OBJS)",\r
+                         module.name.c_str () );\r
+}\r
+\r
+string\r
+MingwModuleHandler::GetLinkerMacro ( const Module& module ) const\r
+{\r
+       return ssprintf ( "$(%s_LFLAGS)",\r
+                         module.name.c_str () );\r
+}\r
+                                           \r
+void\r
+MingwModuleHandler::GenerateMacrosAndTargets (\r
+       const Module& module,\r
+       const string& cc,\r
+       const string& ar ) const\r
+{\r
+       string cflagsMacro = ssprintf ("%s_CFLAGS", module.name.c_str ());\r
+       string nasmflagsMacro = ssprintf ("%s_NASMFLAGS", module.name.c_str ());\r
+       string windresflagsMacro = ssprintf ("%s_RCFLAGS", module.name.c_str ());\r
+       string linkerFlagsMacro = ssprintf ("%s_LFLAGS", module.name.c_str ());\r
+       string objectsMacro = ssprintf ("%s_OBJS", module.name.c_str ());\r
+\r
+       GenerateMacros ( module,\r
+                       cflagsMacro,\r
+                       nasmflagsMacro,\r
+                       windresflagsMacro,\r
+                       linkerFlagsMacro,\r
+                       objectsMacro );\r
+\r
+       // generate phony target for module name\r
+       fprintf ( fMakefile, ".PHONY: %s\n",\r
+               module.name.c_str() );\r
+       fprintf ( fMakefile, "%s: %s\n\n",\r
+               module.name.c_str(),\r
+               module.GetPath().c_str() );\r
+\r
+       // future references to the macros will be to get their values\r
+       cflagsMacro = ssprintf ("$(%s)", cflagsMacro.c_str ());\r
+       nasmflagsMacro = ssprintf ("$(%s)", nasmflagsMacro.c_str ());\r
+       objectsMacro = ssprintf ("$(%s)", objectsMacro.c_str ());\r
+\r
+       string ar_target = GenerateArchiveTarget ( module, ar, objectsMacro );\r
+       GenerateObjectFileTargets ( module,\r
+                                   cc,\r
+                                   cflagsMacro,\r
+                                   nasmflagsMacro,\r
+                                   windresflagsMacro );\r
+\r
+       vector<string> clean_files;\r
+       clean_files.push_back ( FixupTargetFilename(module.GetPath()) );\r
+       clean_files.push_back ( ar_target );\r
+       GetCleanTargets ( clean_files, module.files, module.ifs );\r
+\r
+       fprintf ( fMakefile, "clean::\n\t-@$(rm)" );\r
+       for ( size_t i = 0; i < clean_files.size(); i++ )\r
+       {\r
+               if ( 9==(i%10) )\r
+                       fprintf ( fMakefile, " 2>NUL\n\t-@$(rm)" );\r
+               fprintf ( fMakefile, " %s", clean_files[i].c_str() );\r
+       }\r
+       fprintf ( fMakefile, " 2>NUL\n\n" );\r
 }\r
 \r
 void\r
-MingwModuleHandler::GenerateArchiveTargetHost ( const Module& module ) const\r
+MingwModuleHandler::GenerateMacrosAndTargetsHost ( const Module& module ) const\r
 {\r
-       GenerateArchiveTarget ( module,\r
-                               "${host_ar}" );\r
+       GenerateMacrosAndTargets ( module, "${host_gcc}", "${host_ar}" );\r
 }\r
 \r
 void\r
-MingwModuleHandler::GenerateArchiveTargetTarget ( const Module& module ) const\r
+MingwModuleHandler::GenerateMacrosAndTargetsTarget ( const Module& module ) const\r
 {\r
-       GenerateArchiveTarget ( module,\r
-                               "${ar}" );\r
+       GenerateMacrosAndTargets ( module, "${gcc}", "${ar}" );\r
 }\r
 \r
 string\r
@@ -482,19 +824,40 @@ MingwModuleHandler::GeneratePreconditionDependencies ( const Module& module ) co
                dependencies += s;\r
        }\r
        \r
+       fprintf ( fMakefile,\r
+                 ".PHONY: %s\n\n",\r
+                 preconditionDependenciesName.c_str () );\r
        fprintf ( fMakefile,\r
                  "%s: %s\n\n",\r
                  preconditionDependenciesName.c_str (),\r
                  dependencies.c_str () );\r
-       fprintf ( fMakefile,\r
-                 "%s: %s\n\n",\r
-                 sourceFilenames.c_str (),\r
-                 preconditionDependenciesName.c_str ());\r
-       fprintf ( fMakefile,\r
-                 ".PHONY: %s\n\n",\r
-                 preconditionDependenciesName.c_str () );\r
+       const char* p = sourceFilenames.c_str();\r
+       const char* end = p + strlen(p);\r
+       while ( p < end )\r
+       {\r
+               const char* p2 = &p[512];\r
+               if ( p2 > end )\r
+                       p2 = end;\r
+               while ( p2 > p && !isspace(*p2) )\r
+                       --p2;\r
+               if ( p == p2 )\r
+               {\r
+                       p2 = strpbrk ( p, " \t" );\r
+                       if ( !p2 )\r
+                               p2 = end;\r
+               }\r
+               fprintf ( fMakefile,\r
+                                 "%.*s: %s\n",\r
+                                 p2-p,\r
+                                 p,\r
+                                 preconditionDependenciesName.c_str ());\r
+               p = p2;\r
+               p += strspn ( p, " \t" );\r
+       }\r
+       fprintf ( fMakefile, "\n" );\r
 }\r
 \r
+\r
 static MingwBuildToolModuleHandler buildtool_handler;\r
 \r
 MingwBuildToolModuleHandler::MingwBuildToolModuleHandler()\r
@@ -515,21 +878,23 @@ MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ( const Module& modul
 {\r
        string target ( FixupTargetFilename ( module.GetPath () ) );\r
        string archiveFilename = GetModuleArchiveFilename ( module );\r
+\r
+       GenerateMacrosAndTargetsHost ( module );\r
+\r
        fprintf ( fMakefile, "%s: %s\n",\r
                  target.c_str (),\r
                  archiveFilename.c_str () );\r
        fprintf ( fMakefile,\r
-                 "\t${host_gcc} -o %s %s\n",\r
+                 "\t${host_gcc} %s -o %s %s\n\n",\r
+                 GetLinkerMacro ( module ).c_str (),\r
                  target.c_str (),\r
                  archiveFilename.c_str () );\r
-       GenerateArchiveTargetHost ( module );\r
-       GenerateObjectFileTargetsHost ( module );\r
 }\r
 \r
 static MingwKernelModuleHandler kernelmodule_handler;\r
 \r
 MingwKernelModuleHandler::MingwKernelModuleHandler ()\r
-       : MingwModuleHandler ( KernelModeDLL )\r
+       : MingwModuleHandler ( Kernel )\r
 {\r
 }\r
 \r
@@ -545,50 +910,55 @@ void
 MingwKernelModuleHandler::GenerateKernelModuleTarget ( const Module& module )\r
 {\r
        static string ros_junk ( "$(ROS_TEMPORARY)" );\r
-       //static string ros_output ( "$(ROS_INTERMEDIATE)" );\r
-       string target ( FixupTargetFilename(module.GetPath()) );\r
-       string workingDirectory = GetWorkingDirectory ( );\r
-       string archiveFilename = GetModuleArchiveFilename ( module );\r
+       string target ( FixupTargetFilename (module.GetPath ()) );\r
+       string workingDirectory = GetWorkingDirectory ();\r
+       string objectsMacro = GetObjectsMacro ( module );\r
        string importLibraryDependencies = GetImportLibraryDependencies ( module );\r
        string base_tmp = ros_junk + module.name + ".base.tmp";\r
        string junk_tmp = ros_junk + module.name + ".junk.tmp";\r
        string temp_exp = ros_junk + module.name + ".temp.exp";\r
+       string gccOptions = ssprintf ("-Wl,-T,%s" SSEP "ntoskrnl.lnk -Wl,--subsystem,native -Wl,--entry,_NtProcessStartup -Wl,--image-base,0xC0000000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll",\r
+                                     module.GetBasePath ().c_str () );\r
+\r
+       GenerateMacrosAndTargetsTarget ( module );\r
+\r
        fprintf ( fMakefile, "%s: %s %s\n",\r
                  target.c_str (),\r
-                 archiveFilename.c_str (),\r
+                 objectsMacro.c_str (),\r
                  importLibraryDependencies.c_str () );\r
        fprintf ( fMakefile,\r
-                 "\t${gcc} -Wl,--entry,_NtProcessStartup -Wl,-T,%s" SSEP "ntoskrnl.lnk -Wl,--subsystem,native -Wl,--image-base,0xC0000000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -Wl,--base-file,%s -nostartfiles -o %s %s %s\n",\r
-                 module.GetBasePath ().c_str (),\r
+                 "\t${gcc} %s %s -Wl,--base-file,%s -o %s %s %s\n",\r
+                 GetLinkerMacro ( module ).c_str (),\r
+                 gccOptions.c_str (),\r
                  base_tmp.c_str (),\r
                  junk_tmp.c_str (),\r
-                 archiveFilename.c_str (),\r
+                 objectsMacro.c_str (),\r
                  importLibraryDependencies.c_str () );\r
        fprintf ( fMakefile,\r
                  "\t${rm} %s\n",\r
                  junk_tmp.c_str () );\r
        fprintf ( fMakefile,\r
-                 "\t${dlltool} --dllname %s --base-file %s --output-exp %s --kill-at\n",\r
+                 "\t${dlltool} --dllname %s --base-file %s --def ntoskrnl/ntoskrnl.def --output-exp %s --kill-at\n",\r
                  target.c_str (),\r
                  base_tmp.c_str (),\r
-                 temp_exp.c_str ());\r
+                 temp_exp.c_str () );\r
        fprintf ( fMakefile,\r
                  "\t${rm} %s\n",\r
                  base_tmp.c_str () );\r
        fprintf ( fMakefile,\r
-                 "\t${ld} -Wl,%s -o %s %s %s\n",\r
+                 "\t${gcc} %s %s -Wl,%s -o %s %s %s\n",\r
+                 GetLinkerMacro ( module ).c_str (),\r
+                 gccOptions.c_str (),\r
                  temp_exp.c_str (),\r
                  target.c_str (),\r
-                 archiveFilename.c_str (),\r
+                 objectsMacro.c_str (),\r
                  importLibraryDependencies.c_str () );\r
        fprintf ( fMakefile,\r
-                 "\t${rm} %s\n",\r
+                 "\t${rm} %s\n\n",\r
                  temp_exp.c_str () );\r
-       \r
-       GenerateArchiveTargetTarget ( module );\r
-       GenerateObjectFileTargetsTarget ( module );\r
 }\r
 \r
+\r
 static MingwStaticLibraryModuleHandler staticlibrary_handler;\r
 \r
 MingwStaticLibraryModuleHandler::MingwStaticLibraryModuleHandler ()\r
@@ -607,6 +977,249 @@ MingwStaticLibraryModuleHandler::Process ( const Module& module )
 void\r
 MingwStaticLibraryModuleHandler::GenerateStaticLibraryModuleTarget ( const Module& module )\r
 {\r
-       GenerateArchiveTargetTarget ( module );\r
-       GenerateObjectFileTargetsTarget ( module );\r
+       GenerateMacrosAndTargetsTarget ( module );\r
+}\r
+\r
+\r
+static MingwKernelModeDLLModuleHandler kernelmodedll_handler;\r
+\r
+MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler ()\r
+       : MingwModuleHandler ( KernelModeDLL )\r
+{\r
+}\r
+\r
+void\r
+MingwKernelModeDLLModuleHandler::Process ( const Module& module )\r
+{\r
+       GeneratePreconditionDependencies ( module );\r
+       GenerateKernelModeDLLModuleTarget ( module );\r
+       GenerateInvocations ( module );\r
+}\r
+\r
+void\r
+MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ( const Module& module )\r
+{\r
+       static string ros_junk ( "$(ROS_TEMPORARY)" );\r
+       string target ( FixupTargetFilename ( module.GetPath () ) );\r
+       string workingDirectory = GetWorkingDirectory ( );\r
+       string archiveFilename = GetModuleArchiveFilename ( module );\r
+       string importLibraryDependencies = GetImportLibraryDependencies ( module );\r
+\r
+       if (module.importLibrary != NULL)\r
+       {\r
+               fprintf ( fMakefile, "%s:\n",\r
+                         module.GetDependencyPath ().c_str () );\r
+\r
+               fprintf ( fMakefile,\r
+                         "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",\r
+                         module.GetTargetName ().c_str (),\r
+                         FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),\r
+                         FixupTargetFilename ( module.GetDependencyPath () ).c_str () );\r
+       }\r
+\r
+       if (module.files.size () > 0)\r
+       {\r
+               GenerateMacrosAndTargetsTarget ( module );\r
+\r
+               fprintf ( fMakefile, "%s: %s %s\n",\r
+                         target.c_str (),\r
+                         archiveFilename.c_str (),\r
+                         importLibraryDependencies.c_str () );\r
+\r
+               string linkerParameters ( "-Wl,--subsystem,native -Wl,--entry,_DriverEntry@8 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll" );\r
+               string linkerCommand = GenerateLinkerCommand ( module,\r
+                                                       "${gcc}",\r
+                                                       linkerParameters,\r
+                                                       archiveFilename );\r
+               fprintf ( fMakefile,\r
+                         "\t%s\n\n",\r
+                         linkerCommand.c_str () );\r
+       }\r
+       else\r
+       {\r
+               fprintf ( fMakefile, "%s:\n",\r
+                         target.c_str ());\r
+               fprintf ( fMakefile, ".PHONY: %s\n\n",\r
+                         target.c_str ());\r
+       }\r
+}\r
+\r
+\r
+static MingwNativeDLLModuleHandler nativedll_handler;\r
+\r
+MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler ()\r
+       : MingwModuleHandler ( NativeDLL )\r
+{\r
+}\r
+\r
+void\r
+MingwNativeDLLModuleHandler::Process ( const Module& module )\r
+{\r
+       GeneratePreconditionDependencies ( module );\r
+       GenerateNativeDLLModuleTarget ( module );\r
+       GenerateInvocations ( module );\r
+}\r
+\r
+void\r
+MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ( const Module& module )\r
+{\r
+       static string ros_junk ( "$(ROS_TEMPORARY)" );\r
+       string target ( FixupTargetFilename ( module.GetPath () ) );\r
+       string workingDirectory = GetWorkingDirectory ( );\r
+       string archiveFilename = GetModuleArchiveFilename ( module );\r
+       string importLibraryDependencies = GetImportLibraryDependencies ( module );\r
+\r
+       if (module.importLibrary != NULL)\r
+       {\r
+               fprintf ( fMakefile, "%s:\n",\r
+                         module.GetDependencyPath ().c_str () );\r
+\r
+               fprintf ( fMakefile,\r
+                         "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",\r
+                         module.GetTargetName ().c_str (),\r
+                         FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),\r
+                         FixupTargetFilename ( module.GetDependencyPath () ).c_str () );\r
+       }\r
+\r
+       if (module.files.size () > 0)\r
+       {\r
+               GenerateMacrosAndTargetsTarget ( module );\r
+\r
+               fprintf ( fMakefile, "%s: %s %s\n",\r
+                         target.c_str (),\r
+                         archiveFilename.c_str (),\r
+                         importLibraryDependencies.c_str () );\r
+\r
+               string linkerParameters ( "-Wl,--subsystem,native -Wl,--entry,_DllMainCRTStartup@12 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -nostdlib -mdll" );\r
+               string linkerCommand = GenerateLinkerCommand ( module,\r
+                                                       "${gcc}",\r
+                                                       linkerParameters,\r
+                                                       archiveFilename );\r
+               fprintf ( fMakefile,\r
+                         "\t%s\n\n",\r
+                         linkerCommand.c_str () );\r
+       }\r
+       else\r
+       {\r
+               fprintf ( fMakefile, "%s:\n\n",\r
+                         target.c_str ());\r
+               fprintf ( fMakefile, ".PHONY: %s\n\n",\r
+                         target.c_str ());\r
+       }\r
+}\r
+\r
+\r
+static MingwWin32DLLModuleHandler win32dll_handler;\r
+\r
+MingwWin32DLLModuleHandler::MingwWin32DLLModuleHandler ()\r
+       : MingwModuleHandler ( Win32DLL )\r
+{\r
+}\r
+\r
+void\r
+MingwWin32DLLModuleHandler::Process ( const Module& module )\r
+{\r
+       GeneratePreconditionDependencies ( module );\r
+       GenerateWin32DLLModuleTarget ( module );\r
+       GenerateInvocations ( module );\r
+}\r
+\r
+void\r
+MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ( const Module& module )\r
+{\r
+       static string ros_junk ( "$(ROS_TEMPORARY)" );\r
+       string target ( FixupTargetFilename ( module.GetPath () ) );\r
+       string workingDirectory = GetWorkingDirectory ( );\r
+       string archiveFilename = GetModuleArchiveFilename ( module );\r
+       string importLibraryDependencies = GetImportLibraryDependencies ( module );\r
+\r
+       if (module.importLibrary != NULL)\r
+       {\r
+               fprintf ( fMakefile, "%s:\n",\r
+                         module.GetDependencyPath ().c_str () );\r
+\r
+               fprintf ( fMakefile,\r
+                         "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",\r
+                         module.GetTargetName ().c_str (),\r
+                         FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),\r
+                         FixupTargetFilename ( module.GetDependencyPath () ).c_str () );\r
+       }\r
+\r
+       if (module.files.size () > 0)\r
+       {\r
+               GenerateMacrosAndTargetsTarget ( module );\r
+\r
+               fprintf ( fMakefile, "%s: %s %s\n",\r
+                         target.c_str (),\r
+                         archiveFilename.c_str (),\r
+                         importLibraryDependencies.c_str () );\r
+\r
+               string linkerParameters ( "-Wl,--subsystem,console -Wl,--entry,_DllMain@12 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -mdll" );\r
+               string linkerCommand = GenerateLinkerCommand ( module,\r
+                                                       "${gcc}",\r
+                                                       linkerParameters,\r
+                                                       archiveFilename );\r
+               fprintf ( fMakefile,\r
+                         "\t%s\n\n",\r
+                         linkerCommand.c_str () );\r
+       }\r
+       else\r
+       {\r
+               fprintf ( fMakefile, "%s:\n\n",\r
+                         target.c_str ());\r
+               fprintf ( fMakefile, ".PHONY: %s\n\n",\r
+                         target.c_str ());\r
+       }\r
+}\r
+\r
+\r
+static MingwWin32GUIModuleHandler win32gui_handler;\r
+\r
+MingwWin32GUIModuleHandler::MingwWin32GUIModuleHandler ()\r
+       : MingwModuleHandler ( Win32GUI )\r
+{\r
+}\r
+\r
+void\r
+MingwWin32GUIModuleHandler::Process ( const Module& module )\r
+{\r
+       GeneratePreconditionDependencies ( module );\r
+       GenerateWin32GUIModuleTarget ( module );\r
+       GenerateInvocations ( module );\r
+}\r
+\r
+void\r
+MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ( const Module& module )\r
+{\r
+       static string ros_junk ( "$(ROS_TEMPORARY)" );\r
+       string target ( FixupTargetFilename ( module.GetPath () ) );\r
+       string workingDirectory = GetWorkingDirectory ( );\r
+       string objectFilenames = GetObjectFilenames ( module );\r
+       string importLibraryDependencies = GetImportLibraryDependencies ( module );\r
+\r
+       if (module.files.size () > 0)\r
+       {\r
+               GenerateMacrosAndTargetsTarget ( module );\r
+\r
+               fprintf ( fMakefile, "%s: %s %s\n",\r
+                         target.c_str (),\r
+                         objectFilenames.c_str (),\r
+                         importLibraryDependencies.c_str () );\r
+\r
+               string linkerParameters ( "-Wl,--subsystem,windows -Wl,--entry,_WinMainCRTStartup -Wl,--image-base,0x00400000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000" );\r
+               string linkerCommand = GenerateLinkerCommand ( module,\r
+                                                       "${gcc}",\r
+                                                       linkerParameters,\r
+                                                       objectFilenames );\r
+               fprintf ( fMakefile,\r
+                         "\t%s\n\n",\r
+                         linkerCommand.c_str () );\r
+       }\r
+       else\r
+       {\r
+               fprintf ( fMakefile, "%s:\n\n",\r
+                         target.c_str ());\r
+               fprintf ( fMakefile, ".PHONY: %s\n\n",\r
+                         target.c_str ());\r
+       }\r
 }\r