added more correct linker flags (patch by brezenbak)
[reactos.git] / reactos / tools / rbuild / backend / msvc / vcprojmaker.cpp
index aed7bc3..c3698f6 100644 (file)
 using std::string;\r
 using std::vector;\r
 \r
+#ifdef OUT\r
+#undef OUT\r
+#endif//OUT\r
+\r
 void\r
 MSVCBackend::_generate_vcproj ( const Module& module )\r
 {\r
@@ -49,9 +53,10 @@ MSVCBackend::_generate_vcproj ( const Module& module )
        }\r
 \r
        string module_type = GetExtension(module.GetTargetName());\r
-       bool lib = (module_type == ".lib") || (module_type == ".a");\r
+       bool lib = (module.type == ObjectLibrary) || (module_type == ".lib") || (module_type == ".a");\r
        bool dll = (module_type == ".dll") || (module_type == ".cpl");\r
        bool exe = (module_type == ".exe");\r
+       bool sys = (module_type == ".sys");\r
        // TODO FIXME - need more checks here for 'sys' and possibly 'drv'?\r
 \r
        bool console = exe && (module.type == Win32CUI);\r
@@ -140,7 +145,9 @@ MSVCBackend::_generate_vcproj ( const Module& module )
 \r
        std::vector<std::string> cfgs;\r
 \r
-       cfgs.push_back ( module.name + " - Win32" );\r
+       cfgs.push_back ( "Debug" );\r
+       cfgs.push_back ( "Release" );\r
+    cfgs.push_back ( "Speed" );\r
 \r
        if (!no_cpp)\r
        {\r
@@ -167,6 +174,7 @@ MSVCBackend::_generate_vcproj ( const Module& module )
        }\r
 \r
        string default_cfg = cfgs.back();\r
+       string include_string;\r
 \r
        fprintf ( OUT, "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\r\n" );\r
        fprintf ( OUT, "<VisualStudioProject\r\n" );\r
@@ -177,6 +185,7 @@ MSVCBackend::_generate_vcproj ( const Module& module )
 \r
        fprintf ( OUT, "\tVersion=\"%s\"\r\n", configuration.VSProjectVersion.c_str() );\r
        fprintf ( OUT, "\tName=\"%s\"\r\n", module.name.c_str() );\r
+       fprintf ( OUT, "\tProjectGUID=\"%s\"\r\n", module.guid.c_str() ); \r
        fprintf ( OUT, "\tKeyword=\"Win32Proj\">\r\n" );\r
 \r
        fprintf ( OUT, "\t<Platforms>\r\n" );\r
@@ -184,6 +193,15 @@ MSVCBackend::_generate_vcproj ( const Module& module )
        fprintf ( OUT, "\t\t\tName=\"Win32\"/>\r\n" );\r
        fprintf ( OUT, "\t</Platforms>\r\n" );\r
 \r
+       fprintf ( OUT, "\t<ToolFiles>\r\n" );\r
+       fprintf ( OUT, "\t\t<ToolFile\r\n" );\r
+\r
+       string path = Path::RelativeFromDirectory ( ProjectNode.name, module.GetBasePath() );\r
+       path.erase(path.find(ProjectNode.name, 0), ProjectNode.name.size() + 1);\r
+\r
+       fprintf ( OUT, "\t\t\tRelativePath=\"%sgccasm.rules\"/>\r\n", path.c_str() );\r
+       fprintf ( OUT, "\t</ToolFiles>\r\n" );\r
+\r
        int n = 0;\r
 \r
        std::string output_dir;\r
@@ -217,7 +235,9 @@ MSVCBackend::_generate_vcproj ( const Module& module )
                        {\r
                                if ( multiple_includes )\r
                                        fprintf ( OUT, ";" );\r
+\r
                                fprintf ( OUT, "%s", include.c_str() );\r
+                               include_string += " /I " + include;\r
                                multiple_includes = true;\r
                        }\r
                }\r
@@ -255,6 +275,8 @@ MSVCBackend::_generate_vcproj ( const Module& module )
                {\r
                        if ( i > 0 )\r
                                fprintf ( OUT, ";" );\r
+\r
+                       defines[i] = _replace_str(defines[i], "\"","&quot;"); \r
                        fprintf ( OUT, "%s", defines[i].c_str() );\r
                }\r
                fprintf ( OUT, "\"\r\n" );\r
@@ -276,7 +298,7 @@ MSVCBackend::_generate_vcproj ( const Module& module )
                {\r
                        fprintf ( OUT, "\t\t\t<Tool\r\n" );\r
                        fprintf ( OUT, "\t\t\t\tName=\"VCLibrarianTool\"\r\n" );\r
-                       fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.%s\"/>\r\n", module.name.c_str(), module_type.c_str() );\r
+                       fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.lib\"/>\r\n", module.name.c_str() );\r
                }\r
                else\r
                {\r
@@ -292,14 +314,39 @@ MSVCBackend::_generate_vcproj ( const Module& module )
                        }\r
                        fprintf ( OUT, "\"\r\n" );\r
 \r
-                       fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s.%s\"\r\n", module.name.c_str(), module_type.c_str() );\r
+                       fprintf ( OUT, "\t\t\t\tOutputFile=\"$(OutDir)/%s%s\"\r\n", module.name.c_str(), module_type.c_str() );\r
                        fprintf ( OUT, "\t\t\t\tLinkIncremental=\"%d\"\r\n", debug ? 2 : 1 );\r
                        fprintf ( OUT, "\t\t\t\tGenerateDebugInformation=\"TRUE\"\r\n" );\r
 \r
                        if ( debug )\r
                                fprintf ( OUT, "\t\t\t\tProgramDatabaseFile=\"$(OutDir)/%s.pdb\"\r\n", module.name.c_str() );\r
 \r
-                       fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", console ? 1 : 2 );\r
+                       if ( sys )\r
+                       {\r
+                               fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /DRIVER /ALIGN:0x20 /subsystem:NATIVE /SECTION:INIT,D /FORCE:MULTIPLE /IGNORE:4001,4037,4039,4065,4070,4078,4087,4089,4096\"\r\n" );\r
+                               fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );\r
+                               fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.entrypoint == "" ? "DriverEntry" : module.entrypoint.c_str ());\r
+                               fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", module.baseaddress == "" ? "0x10000" : module.baseaddress.c_str ());   \r
+                       }\r
+                       else if ( exe )\r
+                       {\r
+                               if ( module.type == Kernel || module.type == NativeCUI)\r
+                               {\r
+                                       fprintf ( OUT, "\t\t\t\tAdditionalOptions=\" /SUBSYSTEM:NATIVE /SECTION:INIT,D /ALIGN:4096 /FORCE:MULTIPLE\"\r\n" );\r
+                                       fprintf ( OUT, "\t\t\t\tIgnoreAllDefaultLibraries=\"TRUE\"\r\n" );\r
+                                       fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.entrypoint.c_str ());\r
+                                       fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", module.baseaddress.c_str ());  \r
+                               }\r
+                               else if ( module.type == Win32CUI || module.type == Win32GUI )\r
+                               {\r
+                                       fprintf ( OUT, "\t\t\t\tSubSystem=\"%d\"\r\n", console ? 1 : 2 );\r
+                               }\r
+                       }\r
+                       else if ( dll)\r
+                       {\r
+                               fprintf ( OUT, "\t\t\t\tEntryPointSymbol=\"%s\"\r\n", module.entrypoint == "" ? "DllMain" : module.entrypoint.c_str ());\r
+                               fprintf ( OUT, "\t\t\t\tBaseAddress=\"%s\"\r\n", module.baseaddress == "" ? "0x40000" : module.baseaddress.c_str ());\r
+                       }\r
                        fprintf ( OUT, "\t\t\t\tTargetMachine=\"%d\"/>\r\n", 1 );\r
                }\r
                \r
@@ -344,12 +391,40 @@ MSVCBackend::_generate_vcproj ( const Module& module )
        // Source files\r
        fprintf ( OUT, "\t\t<Filter\r\n" );\r
        fprintf ( OUT, "\t\t\tName=\"Source Files\"\r\n" );\r
-       fprintf ( OUT, "\t\t\tFilter=\"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat\">\r\n" );\r
+       fprintf ( OUT, "\t\t\tFilter=\"cpp;c;cxx;rc;def;r;odl;idl;hpj;bat;S\">\r\n" );\r
        for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )\r
        {\r
-               const string& source_file = DosSeparator(source_files[isrcfile]);\r
+               string source_file = DosSeparator(source_files[isrcfile]);\r
                fprintf ( OUT, "\t\t\t<File\r\n" );\r
                fprintf ( OUT, "\t\t\t\tRelativePath=\"%s\">\r\n", source_file.c_str() );\r
+\r
+               if (configuration.VSProjectVersion < "8.00") {\r
+                       for ( size_t iconfig = 0; iconfig < cfgs.size(); iconfig++ )\r
+                       {\r
+                               std::string& config = cfgs[iconfig];\r
+\r
+                               if ((source_file.find(".idl") != string::npos) || ((source_file.find(".asm") != string::npos || tolower(source_file.at(source_file.size() - 1)) == 's')))\r
+                               {\r
+                                       fprintf ( OUT, "\t\t\t\t<FileConfiguration\r\n" );\r
+                                       fprintf ( OUT, "\t\t\t\t\tName=\"" );\r
+                                       fprintf ( OUT, config.c_str() );\r
+                                       fprintf ( OUT, "|Win32\">\r\n" );\r
+                                       fprintf ( OUT, "\t\t\t\t\t<Tool\r\n" );\r
+                                       if (source_file.find(".idl") != string::npos)\r
+                                       {\r
+                                               fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );\r
+                                               fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(OutDir)\\(InputName).obj\"/>\r\n" );\r
+                                       }\r
+                                       else if ((source_file.find(".asm") != string::npos || tolower(source_file.at(source_file.size() - 1)) == 's'))\r
+                                       {\r
+                                               fprintf ( OUT, "\t\t\t\t\t\tName=\"VCCustomBuildTool\"\r\n" );\r
+                                               fprintf ( OUT, "\t\t\t\t\t\tCommandLine=\"cl /E &quot;$(InputPath)&quot; %s /D__ASM__ | as -o &quot;$(OutDir)\\(InputName).obj&quot;\"\r\n",include_string.c_str() );\r
+                                               fprintf ( OUT, "\t\t\t\t\t\tOutputs=\"$(OutDir)\\(InputName).obj\"/>\r\n" );\r
+                                       }\r
+                                       fprintf ( OUT, "\t\t\t\t</FileConfiguration>\r\n" );\r
+                               }\r
+                       }\r
+               }\r
                fprintf ( OUT, "\t\t\t</File>\r\n" );\r
        }\r
        fprintf ( OUT, "\t\t</Filter>\r\n" );\r
@@ -387,63 +462,165 @@ MSVCBackend::_generate_vcproj ( const Module& module )
        fclose(OUT);\r
 }\r
 \r
-void\r
-MSVCBackend::_generate_sln_header ( FILE* OUT )\r
+std::string\r
+MSVCBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)\r
 {\r
-    if (configuration.VSProjectVersion.empty())\r
-        configuration.VSProjectVersion = MS_VS_DEF_VERSION;\r
+        std::string::size_type pos = string1.find(find_str, 0);\r
+        int intLen = find_str.length();\r
+\r
+        while(std::string::npos != pos)\r
+        {\r
+                string1.replace(pos, intLen, replace_str);\r
+                pos = string1.find(find_str, intLen + pos);\r
+        }\r
 \r
+        return string1;\r
+} \r
+\r
+std::string\r
+MSVCBackend::_get_solution_verion ( void ) {\r
     string version;\r
 \r
+    if (configuration.VSProjectVersion.empty())\r
+        configuration.VSProjectVersion = MS_VS_DEF_VERSION;\r
+\r
     if (configuration.VSProjectVersion == "7.00")\r
-       version = "7.00";\r
+               version = "7.00";\r
 \r
     if (configuration.VSProjectVersion == "7.10")\r
-       version = "8.00";\r
+               version = "8.00";\r
 \r
     if (configuration.VSProjectVersion == "8.00")\r
-       version = "9.00";\r
+               version = "9.00";\r
+\r
+       return version;\r
+}\r
 \r
-    fprintf ( OUT, "Microsoft Visual Studio Solution File, Format Version %s\r\n", version.c_str() );\r
+\r
+void\r
+MSVCBackend::_generate_rules_file ( FILE* OUT )\r
+{\r
+       fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" );\r
+       fprintf ( OUT, "<VisualStudioToolFile\r\n" );\r
+       fprintf ( OUT, "\tName=\"GCC Assembler\"\r\n" );\r
+       fprintf ( OUT, "\tVersion=\"%s\"\r\n", _get_solution_verion().c_str() );\r
+       fprintf ( OUT, "\t>\r\n" );\r
+       fprintf ( OUT, "\t<Rules>\r\n" );\r
+       fprintf ( OUT, "\t\t<CustomBuildRule\r\n" );\r
+       fprintf ( OUT, "\t\t\tName=\"Assembler\"\r\n" );\r
+       fprintf ( OUT, "\t\t\tDisplayName=\"Assembler Files\"\r\n" );\r
+       fprintf ( OUT, "\t\t\tCommandLine=\"cl /E &quot;$(InputPath)&quot; | as -o &quot;$(OutDir)\\$(InputName).obj&quot;\"\r\n" );\r
+       fprintf ( OUT, "\t\t\tOutputs=\"$(OutDir)\\$(InputName).obj\"\r\n" );   \r
+       fprintf ( OUT, "\t\t\tFileExtensions=\"*.S\"\r\n" );\r
+       fprintf ( OUT, "\t\t\tExecutionDescription=\"asm\"\r\n" );\r
+       fprintf ( OUT, "\t\t\t>\r\n" );\r
+       fprintf ( OUT, "\t\t\t<Properties>\r\n" );\r
+       fprintf ( OUT, "\t\t\t</Properties>\r\n" );\r
+       fprintf ( OUT, "\t\t</CustomBuildRule>\r\n" );\r
+       fprintf ( OUT, "\t</Rules>\r\n" );\r
+       fprintf ( OUT, "</VisualStudioToolFile>\r\n" );\r
+}\r
+\r
+void\r
+MSVCBackend::_generate_sln_header ( FILE* OUT )\r
+{\r
+    fprintf ( OUT, "Microsoft Visual Studio Solution File, Format Version %s\r\n", _get_solution_verion().c_str() );\r
     fprintf ( OUT, "# Visual Studio 2005\r\n" );\r
     fprintf ( OUT, "\r\n" );\r
 }\r
 \r
+\r
+void\r
+MSVCBackend::_generate_sln_project (\r
+       FILE* OUT,\r
+       const Module& module,\r
+       std::string vcproj_file,\r
+       std::string sln_guid,\r
+       std::string vcproj_guid,\r
+       const std::vector<Dependency*>& dependencies )\r
+{\r
+       vcproj_file = DosSeparator ( std::string(".\\") + vcproj_file );\r
+\r
+       fprintf ( OUT, "Project(\"%s\") = \"%s\", \"%s\", \"%s\"\r\n", sln_guid.c_str() , module.name.c_str(), vcproj_file.c_str(), vcproj_guid.c_str() );\r
+\r
+       //FIXME: only omit ProjectDependencies in VS 2005 when there are no dependencies\r
+       //NOTE: VS 2002 do not use ProjectSection; it uses GlobalSection instead\r
+       if ((configuration.VSProjectVersion == "7.10") || (dependencies.size() > 0)) {\r
+               fprintf ( OUT, "\tProjectSection(ProjectDependencies) = postProject\r\n" );\r
+               for ( size_t i = 0; i < dependencies.size(); i++ )\r
+               {\r
+                       Dependency& dependency = *dependencies[i];\r
+                       fprintf ( OUT, "\t\t%s = %s\r\n", dependency.module.guid.c_str(), dependency.module.guid.c_str() );\r
+               }\r
+               fprintf ( OUT, "\tEndProjectSection\r\n" );\r
+       }\r
+\r
+       fprintf ( OUT, "EndProject\r\n" );\r
+}\r
+\r
+\r
+void\r
+MSVCBackend::_generate_sln_footer ( FILE* OUT )\r
+{\r
+       fprintf ( OUT, "Global\r\n" );\r
+       fprintf ( OUT, "\tGlobalSection(SolutionConfiguration) = preSolution\r\n" );\r
+       fprintf ( OUT, "\t\tDebug = Debug\r\n" );\r
+       fprintf ( OUT, "\t\tRelease = Release\r\n" );\r
+       fprintf ( OUT, "\tEndGlobalSection\r\n" );\r
+       fprintf ( OUT, "\tGlobalSection(ProjectConfiguration) = postSolution\r\n" );\r
+       for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )\r
+       {\r
+               Module& module = *ProjectNode.modules[i];\r
+               std::string guid = module.guid;\r
+               _generate_sln_configurations ( OUT, guid.c_str() );\r
+       } \r
+       fprintf ( OUT, "\tEndGlobalSection\r\n" );\r
+       fprintf ( OUT, "\tGlobalSection(ExtensibilityGlobals) = postSolution\r\n" );\r
+       fprintf ( OUT, "\tEndGlobalSection\r\n" );\r
+       fprintf ( OUT, "\tGlobalSection(ExtensibilityAddIns) = postSolution\r\n" );\r
+       fprintf ( OUT, "\tEndGlobalSection\r\n" );\r
+       \r
+       if (configuration.VSProjectVersion == "7.00") {\r
+               fprintf ( OUT, "\tGlobalSection(ProjectDependencies) = postSolution\r\n" );\r
+               //FIXME: Add dependencies for VS 2002\r
+               fprintf ( OUT, "\tEndGlobalSection\r\n" );\r
+       }\r
+\r
+       if (configuration.VSProjectVersion == "8.00") {\r
+               fprintf ( OUT, "\tGlobalSection(SolutionProperties) = preSolution\r\n" );\r
+               fprintf ( OUT, "\t\tHideSolutionNode = FALSE\r\n" );\r
+               fprintf ( OUT, "\tEndGlobalSection\r\n" );\r
+       }\r
+\r
+       fprintf ( OUT, "EndGlobal\r\n" );\r
+       fprintf ( OUT, "\r\n" );\r
+}\r
+\r
+\r
+void\r
+MSVCBackend::_generate_sln_configurations ( FILE* OUT, std::string vcproj_guid )\r
+{\r
+       fprintf ( OUT, "\t\t%s.Debug.ActiveCfg = Debug|Win32\r\n", vcproj_guid.c_str() );\r
+       fprintf ( OUT, "\t\t%s.Debug.Build.0 = Debug|Win32\r\n", vcproj_guid.c_str() );\r
+       fprintf ( OUT, "\t\t%s.Debug.Release.ActiveCfg = Release|Win32\r\n", vcproj_guid.c_str() );\r
+       fprintf ( OUT, "\t\t%s.Debug.Release.Build.0 = Release|Win32\r\n", vcproj_guid.c_str() );\r
+}\r
+\r
 void\r
 MSVCBackend::_generate_sln ( FILE* OUT )\r
 {\r
+       string sln_guid = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";\r
+       vector<string> guids;\r
+\r
        _generate_sln_header(OUT);\r
        // TODO FIXME - is it necessary to sort them?\r
        for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )\r
        {\r
                Module& module = *ProjectNode.modules[i];\r
-\r
+               \r
                std::string vcproj_file = VcprojFileName ( module );\r
-               _generate_dsw_project ( OUT, module, vcproj_file, module.dependencies );\r
-    }\r
-//    _generate_dsw_footer ( OUT );\r
+               _generate_sln_project ( OUT, module, vcproj_file, sln_guid, module.guid, module.dependencies );\r
+       }\r
+       _generate_sln_footer ( OUT );\r
 }\r
 \r
-\r
-\r
-/*\r
-       m_devFile << "Microsoft Visual Studio Solution File, Format Version 9.00" << endl;\r
-       m_devFile << "# Visual Studio 2005" << endl;\r
-\r
-       m_devFile << "# FIXME Project listings here" << endl;\r
-       m_devFile << "EndProject" << endl;\r
-       m_devFile << "Global" << endl;\r
-       m_devFile << "  GlobalSection(SolutionConfigurationPlatforms) = preSolution" << endl;\r
-       m_devFile << "          Debug|Win32 = Debug|Win32" << endl;\r
-       m_devFile << "          Release|Win32 = Release|Win32" << endl;\r
-       m_devFile << "  EndGlobalSection" << endl;\r
-       m_devFile << "  GlobalSection(ProjectConfigurationPlatforms) = postSolution" << endl;\r
-       m_devFile << "  #FIXME Project Listings Here" << endl;\r
-       m_devFile << "  EndGlobalSection" << endl;\r
-       m_devFile << "  GlobalSection(SolutionProperties) = preSolution" << endl;\r
-       m_devFile << "          HideSolutionNode = FALSE" << endl;\r
-       m_devFile << "  EndGlobalSection" << endl;\r
-       m_devFile << "EndGlobal" << endl;\r
-\r
-       m_devFile << endl << endl;\r
-*/\r