Support for non-standard module base addresses
[reactos.git] / reactos / tools / rbuild / module.cpp
index e8ea879..47157e4 100644 (file)
@@ -1,5 +1,3 @@
-// module.cpp\r
-\r
 #include "pch.h"\r
 #include <assert.h>\r
 \r
@@ -21,11 +19,43 @@ FixSeparator ( const string& s )
        return s2;\r
 }\r
 \r
+string\r
+GetExtension ( const string& filename )\r
+{\r
+       size_t index = filename.find_last_of ( '/' );\r
+       if (index == string::npos) index = 0;\r
+       string tmp = filename.substr( index, filename.size() - index );\r
+       size_t ext_index = tmp.find_last_of( '.' );\r
+       if (ext_index != string::npos) \r
+               return filename.substr ( index + ext_index, filename.size() );\r
+       return "";\r
+}\r
+\r
+string\r
+GetDirectory ( const string& filename )\r
+{\r
+       size_t index = filename.find_last_of ( CSEP );\r
+       if ( index == string::npos )\r
+               return filename;\r
+       else\r
+               return filename.substr ( 0, index );\r
+}\r
+\r
+string\r
+NormalizeFilename ( const string& filename )\r
+{\r
+       Path path;\r
+       string normalizedPath = path.Fixup ( filename, true );\r
+       string relativeNormalizedPath = path.RelativeFromWorkingDirectory ( normalizedPath );\r
+       return FixSeparator ( relativeNormalizedPath );\r
+}\r
+\r
 Module::Module ( const Project& project,\r
                  const XMLElement& moduleNode,\r
                  const string& modulePath )\r
-       : project(project),\r
-         node(moduleNode)\r
+       : project (project),\r
+         node (moduleNode),\r
+         importLibrary (NULL)\r
 {\r
        if ( node.name != "module" )\r
                throw Exception ( "internal tool error: Module created with non-<module> node" );\r
@@ -38,15 +68,31 @@ Module::Module ( const Project& project,
 \r
        att = moduleNode.GetAttribute ( "type", true );\r
        assert(att);\r
-       stype = att->value;\r
-       strlwr ( &stype[0] );\r
-       etype = GetModuleType ( node.location, *att );\r
+       type = GetModuleType ( node.location, *att );\r
 \r
        att = moduleNode.GetAttribute ( "extension", false );\r
-       if (att != NULL)\r
+       if ( att != NULL )\r
                extension = att->value;\r
        else\r
                extension = GetDefaultModuleExtension ();\r
+\r
+       att = moduleNode.GetAttribute ( "entrypoint", false );\r
+       if ( att != NULL )\r
+               entrypoint = att->value;\r
+       else\r
+               entrypoint = GetDefaultModuleEntrypoint ();\r
+\r
+       att = moduleNode.GetAttribute ( "baseaddress", false );\r
+       if ( att != NULL )\r
+               baseaddress = att->value;\r
+       else\r
+               baseaddress = GetDefaultModuleBaseaddress ();\r
+\r
+       att = moduleNode.GetAttribute ( "mangledsymbols", false );\r
+       if ( att != NULL )\r
+               mangledSymbols = att->value != "false";\r
+       else\r
+               mangledSymbols = false;\r
 }\r
 \r
 Module::~Module ()\r
@@ -64,6 +110,12 @@ Module::~Module ()
                delete invocations[i];\r
        for ( i = 0; i < dependencies.size(); i++ )\r
                delete dependencies[i];\r
+       for ( i = 0; i < ifs.size(); i++ )\r
+               delete ifs[i];\r
+       for ( i = 0; i < compilerFlags.size(); i++ )\r
+               delete compilerFlags[i];\r
+       for ( i = 0; i < linkerFlags.size(); i++ )\r
+               delete linkerFlags[i];\r
 }\r
 \r
 void\r
@@ -84,21 +136,47 @@ Module::ProcessXML()
                invocations[i]->ProcessXML ();\r
        for ( i = 0; i < dependencies.size(); i++ )\r
                dependencies[i]->ProcessXML ();\r
+       for ( i = 0; i < ifs.size(); i++ )\r
+               ifs[i]->ProcessXML();\r
+       for ( i = 0; i < compilerFlags.size(); i++ )\r
+               compilerFlags[i]->ProcessXML();\r
+       for ( i = 0; i < linkerFlags.size(); i++ )\r
+               linkerFlags[i]->ProcessXML();\r
 }\r
 \r
 void\r
 Module::ProcessXMLSubElement ( const XMLElement& e,\r
-                               const string& path )\r
+                               const string& path,\r
+                               If* pIf /*= NULL*/ )\r
 {\r
        bool subs_invalid = false;\r
        string subpath ( path );\r
        if ( e.name == "file" && e.value.size () > 0 )\r
        {\r
-               files.push_back ( new File ( FixSeparator ( path + CSEP + e.value ) ) );\r
+               bool first = false;\r
+               const XMLAttribute* att = e.GetAttribute ( "first", false );\r
+               if ( att )\r
+               {\r
+                       if ( !stricmp ( att->value.c_str(), "true" ) )\r
+                               first = true;\r
+                       else if ( stricmp ( att->value.c_str(), "false" ) )\r
+                               throw InvalidBuildFileException (\r
+                                       e.location,\r
+                                       "attribute 'first' of <file> element can only be 'true' or 'false'" );\r
+               }\r
+               File* pFile = new File ( FixSeparator ( path + CSEP + e.value ), first );\r
+               if ( pIf )\r
+                       pIf->files.push_back ( pFile );\r
+               else\r
+                       files.push_back ( pFile );\r
                subs_invalid = true;\r
        }\r
        else if ( e.name == "library" && e.value.size () )\r
        {\r
+               if ( pIf )\r
+                       throw InvalidBuildFileException (\r
+                               e.location,\r
+                               "<library> is not a valid sub-element of <if>" );\r
                libraries.push_back ( new Library ( e, *this, e.value ) );\r
                subs_invalid = true;\r
        }\r
@@ -110,31 +188,86 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        }\r
        else if ( e.name == "include" )\r
        {\r
-               includes.push_back ( new Include ( project, this, e ) );\r
+               Include* include = new Include ( project, this, e );\r
+               if ( pIf )\r
+                       pIf->includes.push_back ( include );\r
+               else\r
+                       includes.push_back ( include );\r
                subs_invalid = true;\r
        }\r
        else if ( e.name == "define" )\r
        {\r
-               defines.push_back ( new Define ( project, this, e ) );\r
+               Define* pDefine = new Define ( project, this, e );\r
+               if ( pIf )\r
+                       pIf->defines.push_back ( pDefine );\r
+               else\r
+                       defines.push_back ( pDefine );\r
                subs_invalid = true;\r
        }\r
        else if ( e.name == "invoke" )\r
        {\r
+               if ( pIf )\r
+                       throw InvalidBuildFileException (\r
+                               e.location,\r
+                               "<invoke> is not a valid sub-element of <if>" );\r
                invocations.push_back ( new Invoke ( e, *this ) );\r
                subs_invalid = false;\r
        }\r
        else if ( e.name == "dependency" )\r
        {\r
+               if ( pIf )\r
+                       throw InvalidBuildFileException (\r
+                               e.location,\r
+                               "<dependency> is not a valid sub-element of <if>" );\r
                dependencies.push_back ( new Dependency ( e, *this ) );\r
                subs_invalid = true;\r
        }\r
+       else if ( e.name == "importlibrary" )\r
+       {\r
+               if ( pIf )\r
+                       throw InvalidBuildFileException (\r
+                               e.location,\r
+                               "<importlibrary> is not a valid sub-element of <if>" );\r
+               if ( importLibrary )\r
+                       throw InvalidBuildFileException (\r
+                               e.location,\r
+                               "Only one <importlibrary> is valid per module" );\r
+               importLibrary = new ImportLibrary ( e, *this );\r
+               subs_invalid = true;\r
+       }\r
+       else if ( e.name == "if" )\r
+       {\r
+               If* pOldIf = pIf;\r
+               pIf = new If ( e, project, this );\r
+               if ( pOldIf )\r
+                       pOldIf->ifs.push_back ( pIf );\r
+               else\r
+                       ifs.push_back ( pIf );\r
+               subs_invalid = false;\r
+       }\r
+       else if ( e.name == "compilerflag" )\r
+       {\r
+               compilerFlags.push_back ( new CompilerFlag ( project, this, e ) );\r
+               subs_invalid = true;\r
+       }\r
+       else if ( e.name == "linkerflag" )\r
+       {\r
+               linkerFlags.push_back ( new LinkerFlag ( project, this, e ) );\r
+               subs_invalid = true;\r
+       }\r
+       else if ( e.name == "property" )\r
+       {\r
+               throw InvalidBuildFileException (\r
+                       e.location,\r
+                       "<property> is not a valid sub-element of <module>" );\r
+       }\r
        if ( subs_invalid && e.subElements.size() > 0 )\r
                throw InvalidBuildFileException (\r
                        e.location,\r
                        "<%s> cannot have sub-elements",\r
                        e.name.c_str() );\r
        for ( size_t i = 0; i < e.subElements.size (); i++ )\r
-               ProcessXMLSubElement ( *e.subElements[i], subpath );\r
+               ProcessXMLSubElement ( *e.subElements[i], subpath, pIf );\r
 }\r
 \r
 ModuleType\r
@@ -144,8 +277,30 @@ Module::GetModuleType ( const string& location, const XMLAttribute& attribute )
                return BuildTool;\r
        if ( attribute.value == "staticlibrary" )\r
                return StaticLibrary;\r
+       if ( attribute.value == "objectlibrary" )\r
+               return ObjectLibrary;\r
+       if ( attribute.value == "kernel" )\r
+               return Kernel;\r
        if ( attribute.value == "kernelmodedll" )\r
                return KernelModeDLL;\r
+       if ( attribute.value == "kernelmodedriver" )\r
+               return KernelModeDriver;\r
+       if ( attribute.value == "nativedll" )\r
+               return NativeDLL;\r
+       if ( attribute.value == "nativecui" )\r
+               return NativeCUI;\r
+       if ( attribute.value == "win32dll" )\r
+               return Win32DLL;\r
+       if ( attribute.value == "win32cui" )\r
+               return Win32CUI;\r
+       if ( attribute.value == "win32gui" )\r
+               return Win32GUI;\r
+       if ( attribute.value == "bootloader" )\r
+               return BootLoader;\r
+       if ( attribute.value == "bootsector" )\r
+               return BootSector;\r
+       if ( attribute.value == "iso" )\r
+               return Iso;\r
        throw InvalidAttributeValueException ( location,\r
                                               attribute.name,\r
                                               attribute.value );\r
@@ -154,21 +309,130 @@ Module::GetModuleType ( const string& location, const XMLAttribute& attribute )
 string\r
 Module::GetDefaultModuleExtension () const\r
 {\r
-       switch (etype)\r
+       switch (type)\r
        {\r
                case BuildTool:\r
                        return EXEPOSTFIX;\r
                case StaticLibrary:\r
                        return ".a";\r
+               case ObjectLibrary:\r
+                       return ".o";\r
+               case Kernel:\r
+               case NativeCUI:\r
+               case Win32CUI:\r
+               case Win32GUI:\r
+                       return ".exe";\r
                case KernelModeDLL:\r
+               case NativeDLL:\r
+               case Win32DLL:\r
                        return ".dll";\r
+               case KernelModeDriver:\r
+               case BootLoader:\r
+                       return ".sys";\r
+               case BootSector:\r
+                       return ".o";\r
+               case Iso:\r
+                       return ".iso";\r
+       }\r
+       throw InvalidOperationException ( __FILE__,\r
+                                         __LINE__ );\r
+}\r
+\r
+string\r
+Module::GetDefaultModuleEntrypoint () const\r
+{\r
+       switch (type)\r
+       {\r
+               case Kernel:\r
+                       return "_NtProcessStartup";\r
+               case KernelModeDLL:\r
+                       return "_DriverEntry@8";\r
+               case NativeDLL:\r
+                       return "_DllMainCRTStartup@12";\r
+               case NativeCUI:\r
+                       return "_NtProcessStartup@4";\r
+               case Win32DLL:\r
+                       return "_DllMain@12";\r
+               case Win32CUI:\r
+                       return "_mainCRTStartup";\r
+               case Win32GUI:\r
+                       return "_WinMainCRTStartup";\r
+               case KernelModeDriver:\r
+                       return "_DriverEntry@8";\r
+               case BuildTool:\r
+               case StaticLibrary:\r
+               case ObjectLibrary:\r
+               case BootLoader:\r
+               case BootSector:\r
+               case Iso:\r
+                       return "";\r
+       }\r
+       throw InvalidOperationException ( __FILE__,\r
+                                         __LINE__ );\r
+}\r
+\r
+string\r
+Module::GetDefaultModuleBaseaddress () const\r
+{\r
+       switch (type)\r
+       {\r
+               case Kernel:\r
+                       return "0xc0000000";\r
+               case KernelModeDLL:\r
+                       return "0x10000";\r
+               case NativeDLL:\r
+                       return "0x10000";\r
+               case NativeCUI:\r
+                       return "0x10000";\r
+               case Win32DLL:\r
+                       return "0x10000";\r
+               case Win32CUI:\r
+                       return "0x00400000";\r
+               case Win32GUI:\r
+                       return "0x00400000";\r
+               case KernelModeDriver:\r
+                       return "0x10000";\r
+               case BuildTool:\r
+               case StaticLibrary:\r
+               case ObjectLibrary:\r
+               case BootLoader:\r
+               case BootSector:\r
+               case Iso:\r
+                       return "";\r
+       }\r
+       throw InvalidOperationException ( __FILE__,\r
+                                         __LINE__ );\r
+}\r
+\r
+bool\r
+Module::HasImportLibrary () const\r
+{\r
+       return importLibrary != NULL;\r
+}\r
+\r
+string\r
+Module::GetTargetName () const\r
+{\r
+       return name + extension;\r
+}\r
+\r
+string\r
+Module::GetDependencyPath () const\r
+{\r
+       if ( HasImportLibrary () )\r
+       {\r
+               return ssprintf ( "dk%snkm%slib%slib%s.a",\r
+                                 SSEP,\r
+                                 SSEP,\r
+                                 SSEP,\r
+                                 name.c_str () );\r
        }\r
-       throw InvalidOperationException (__FILE__,\r
-                                        __LINE__);\r
+       else\r
+               return GetPath();\r
 }\r
 \r
 string\r
-Module::GetBasePath() const\r
+Module::GetBasePath () const\r
 {\r
        return path;\r
 }\r
@@ -176,7 +440,13 @@ Module::GetBasePath() const
 string\r
 Module::GetPath () const\r
 {\r
-       return path + CSEP + name + extension;\r
+       return path + CSEP + GetTargetName ();\r
+}\r
+\r
+string\r
+Module::GetPathWithPrefix ( const string& prefix ) const\r
+{\r
+       return path + CSEP + prefix + GetTargetName ();\r
 }\r
 \r
 string\r
@@ -206,9 +476,38 @@ Module::GetInvocationTarget ( const int index ) const
                          index );\r
 }\r
 \r
+bool\r
+Module::HasFileWithExtensions ( const std::string& extension1,\r
+                                   const std::string& extension2 ) const\r
+{\r
+       for ( size_t i = 0; i < files.size (); i++ )\r
+       {\r
+               File& file = *files[i];\r
+               string extension = GetExtension ( file.name );\r
+               if ( extension == extension1 || extension == extension2 )\r
+                       return true;\r
+       }\r
+       return false;\r
+}\r
 \r
-File::File ( const string& _name )\r
-       : name(_name)\r
+void\r
+Module::InvokeModule () const\r
+{\r
+       for ( size_t i = 0; i < invocations.size (); i++ )\r
+       {\r
+               Invoke& invoke = *invocations[i];\r
+               string command = invoke.invokeModule->GetPath () + " " + invoke.GetParameters ();\r
+               printf ( "Executing '%s'\n\n", command.c_str () );\r
+               int exitcode = system ( command.c_str () );\r
+               if ( exitcode != 0 )\r
+                       throw InvocationFailedException ( command,\r
+                                                         exitcode );\r
+       }\r
+}\r
+\r
+\r
+File::File ( const string& _name, bool _first )\r
+       : name(_name), first(_first)\r
 {\r
 }\r
 \r
@@ -331,11 +630,45 @@ Invoke::GetTargets () const
                InvokeFile& file = *output[i];\r
                if ( targets.length () > 0 )\r
                        targets += " ";\r
-               targets += file.name;\r
+               targets += NormalizeFilename ( file.name );\r
        }\r
        return targets;\r
 }\r
 \r
+string\r
+Invoke::GetParameters () const\r
+{\r
+       string parameters ( "" );\r
+       size_t i;\r
+       for ( i = 0; i < output.size (); i++ )\r
+       {\r
+               if ( parameters.length () > 0)\r
+                       parameters += " ";\r
+               InvokeFile& invokeFile = *output[i];\r
+               if ( invokeFile.switches.length () > 0 )\r
+               {\r
+                       parameters += invokeFile.switches;\r
+                       parameters += " ";\r
+               }\r
+               parameters += invokeFile.name;\r
+       }\r
+\r
+       for ( i = 0; i < input.size (); i++ )\r
+       {\r
+               if ( parameters.length () > 0 )\r
+                       parameters += " ";\r
+               InvokeFile& invokeFile = *input[i];\r
+               if ( invokeFile.switches.length () > 0 )\r
+               {\r
+                       parameters += invokeFile.switches;\r
+                       parameters += " ";\r
+               }\r
+               parameters += invokeFile.name ;\r
+       }\r
+\r
+       return parameters;\r
+}\r
+\r
 \r
 InvokeFile::InvokeFile ( const XMLElement& _node,\r
                          const string& _name )\r
@@ -373,3 +706,77 @@ Dependency::ProcessXML()
                                                  module.name.c_str(),\r
                                                  node.value.c_str() );\r
 }\r
+\r
+\r
+ImportLibrary::ImportLibrary ( const XMLElement& _node,\r
+                               const Module& _module )\r
+       : node (_node),\r
+         module (_module)\r
+{\r
+       const XMLAttribute* att = _node.GetAttribute ( "basename", false );\r
+       if (att != NULL)\r
+               basename = att->value;\r
+       else\r
+               basename = module.name;\r
+\r
+       att = _node.GetAttribute ( "definition", true );\r
+       assert (att);\r
+       definition = FixSeparator(att->value);\r
+}\r
+\r
+\r
+If::If ( const XMLElement& node_,\r
+         const Project& project_,\r
+         const Module* module_ )\r
+       : node(node_), project(project_), module(module_)\r
+{\r
+       const XMLAttribute* att;\r
+\r
+       att = node.GetAttribute ( "property", true );\r
+       assert(att);\r
+       property = att->value;\r
+\r
+       att = node.GetAttribute ( "value", true );\r
+       assert(att);\r
+       value = att->value;\r
+}\r
+\r
+If::~If ()\r
+{\r
+       size_t i;\r
+       for ( i = 0; i < files.size(); i++ )\r
+               delete files[i];\r
+       for ( i = 0; i < includes.size(); i++ )\r
+               delete includes[i];\r
+       for ( i = 0; i < defines.size(); i++ )\r
+               delete defines[i];\r
+       for ( i = 0; i < ifs.size(); i++ )\r
+               delete ifs[i];\r
+}\r
+\r
+void\r
+If::ProcessXML()\r
+{\r
+}\r
+\r
+\r
+Property::Property ( const XMLElement& node_,\r
+                     const Project& project_,\r
+                     const Module* module_ )\r
+       : node(node_), project(project_), module(module_)\r
+{\r
+       const XMLAttribute* att;\r
+\r
+       att = node.GetAttribute ( "name", true );\r
+       assert(att);\r
+       name = att->value;\r
+\r
+       att = node.GetAttribute ( "value", true );\r
+       assert(att);\r
+       value = att->value;\r
+}\r
+\r
+void\r
+Property::ProcessXML()\r
+{\r
+}\r