* Implement <autoregister>
[reactos.git] / reactos / tools / rbuild / module.cpp
index 1b5644b..3bd1a5e 100644 (file)
@@ -52,18 +52,26 @@ Replace ( const string& s, const string& find, const string& with )
 }
 
 string
-FixSeparator ( const string& s )
+ChangeSeparator ( const string& s,
+                  const char fromSeparator,
+                  const char toSeparator )
 {
        string s2(s);
-       char* p = strchr ( &s2[0], cBadSep );
+       char* p = strchr ( &s2[0], fromSeparator );
        while ( p )
        {
-               *p++ = cSep;
-               p = strchr ( p, cBadSep );
+               *p++ = toSeparator;
+               p = strchr ( p, fromSeparator );
        }
        return s2;
 }
 
+string
+FixSeparator ( const string& s )
+{
+       return ChangeSeparator ( s, cBadSep, cSep );
+}
+
 string
 FixSeparatorForSystemCommand ( const string& s )
 {
@@ -179,30 +187,36 @@ GetBooleanValue ( const string& value )
                return false;
 }
 
+string
+ToLower ( string filename )
+{
+       for ( size_t i = 1; i < filename.length (); i++ )
+               filename[i] = tolower ( filename[i] );
+       return filename;
+}
+
 IfableData::~IfableData()
 {
        size_t i;
-       for ( i = 0; i < files.size(); i++ )
-               delete files[i];
-       for ( i = 0; i < includes.size(); i++ )
+       for ( i = 0; i < includes.size (); i++ )
                delete includes[i];
-       for ( i = 0; i < defines.size(); i++ )
+       for ( i = 0; i < defines.size (); i++ )
                delete defines[i];
-       for ( i = 0; i < libraries.size(); i++ )
+       for ( i = 0; i < libraries.size (); i++ )
                delete libraries[i];
-       for ( i = 0; i < properties.size(); i++ )
+       for ( i = 0; i < properties.size (); i++ )
                delete properties[i];
-       for ( i = 0; i < compilerFlags.size(); i++ )
+       for ( i = 0; i < compilerFlags.size (); i++ )
                delete compilerFlags[i];
-       for ( i = 0; i < ifs.size(); i++ )
+       for ( i = 0; i < ifs.size (); i++ )
                delete ifs[i];
+       for ( i = 0; i < compilationUnits.size (); i++ )
+               delete compilationUnits[i];
 }
 
 void IfableData::ProcessXML ()
 {
        size_t i;
-       for ( i = 0; i < files.size (); i++ )
-               files[i]->ProcessXML ();
        for ( i = 0; i < includes.size (); i++ )
                includes[i]->ProcessXML ();
        for ( i = 0; i < defines.size (); i++ )
@@ -215,6 +229,8 @@ void IfableData::ProcessXML ()
                compilerFlags[i]->ProcessXML ();
        for ( i = 0; i < ifs.size (); i++ )
                ifs[i]->ProcessXML ();
+       for ( i = 0; i < compilationUnits.size (); i++ )
+               compilationUnits[i]->ProcessXML ();
 }
 
 Module::Module ( const Project& project,
@@ -224,6 +240,7 @@ Module::Module ( const Project& project,
          node (moduleNode),
          importLibrary (NULL),
          bootstrap (NULL),
+         autoRegister(NULL),
          linkerScript (NULL),
          pch (NULL),
          cplusplus (false),
@@ -413,7 +430,10 @@ Module::ProcessXML()
 
        size_t i;
        for ( i = 0; i < node.subElements.size(); i++ )
-               ProcessXMLSubElement ( *node.subElements[i], path );
+       {
+               ParseContext parseContext;
+               ProcessXMLSubElement ( *node.subElements[i], path, parseContext );
+       }
        for ( i = 0; i < invocations.size(); i++ )
                invocations[i]->ProcessXML ();
        for ( i = 0; i < dependencies.size(); i++ )
@@ -429,13 +449,17 @@ Module::ProcessXML()
                linkerScript->ProcessXML();
        if ( pch )
                pch->ProcessXML();
+       if ( autoRegister )
+               autoRegister->ProcessXML();
 }
 
 void
 Module::ProcessXMLSubElement ( const XMLElement& e,
                                const string& path,
-                               If* pIf /*= NULL*/ )
+                               ParseContext& parseContext )
 {
+       If* pOldIf = parseContext.ifData;
+       CompilationUnit* pOldCompilationUnit = parseContext.compilationUnit;
        bool subs_invalid = false;
        string subpath ( path );
        if ( e.name == "file" && e.value.size () > 0 )
@@ -470,8 +494,18 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
                                         first,
                                         switches,
                                         false );
-               if ( pIf )
-                       pIf->data.files.push_back ( pFile );
+               if ( parseContext.compilationUnit )
+                       parseContext.compilationUnit->files.push_back ( pFile );
+               else
+               {
+                       CompilationUnit* pCompilationUnit = new CompilationUnit ( pFile );
+                       if ( parseContext.ifData )
+                               parseContext.ifData->data.compilationUnits.push_back ( pCompilationUnit );
+                       else
+                               non_if_data.compilationUnits.push_back ( pCompilationUnit );
+               }
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.files.push_back ( pFile );
                else
                        non_if_data.files.push_back ( pFile );
                subs_invalid = true;
@@ -479,8 +513,8 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        else if ( e.name == "library" && e.value.size () )
        {
                Library* pLibrary = new Library ( e, *this, e.value );
-               if ( pIf )
-                       pIf->data.libraries.push_back ( pLibrary );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.libraries.push_back ( pLibrary );
                else
                        non_if_data.libraries.push_back ( pLibrary );
                subs_invalid = true;
@@ -494,8 +528,8 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        else if ( e.name == "include" )
        {
                Include* include = new Include ( project, this, &e );
-               if ( pIf )
-                       pIf->data.includes.push_back ( include );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.includes.push_back ( include );
                else
                        non_if_data.includes.push_back ( include );
                subs_invalid = true;
@@ -503,15 +537,15 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        else if ( e.name == "define" )
        {
                Define* pDefine = new Define ( project, this, e );
-               if ( pIf )
-                       pIf->data.defines.push_back ( pDefine );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.defines.push_back ( pDefine );
                else
                        non_if_data.defines.push_back ( pDefine );
                subs_invalid = true;
        }
        else if ( e.name == "invoke" )
        {
-               if ( pIf )
+               if ( parseContext.ifData )
                        throw InvalidBuildFileException (
                                e.location,
                                "<invoke> is not a valid sub-element of <if>" );
@@ -520,7 +554,7 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "dependency" )
        {
-               if ( pIf )
+               if ( parseContext.ifData )
                        throw InvalidBuildFileException (
                                e.location,
                                "<dependency> is not a valid sub-element of <if>" );
@@ -529,7 +563,7 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "importlibrary" )
        {
-               if ( pIf )
+               if ( parseContext.ifData )
                        throw InvalidBuildFileException (
                                e.location,
                                "<importlibrary> is not a valid sub-element of <if>" );
@@ -542,29 +576,27 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "if" )
        {
-               If* pOldIf = pIf;
-               pIf = new If ( e, project, this );
+               parseContext.ifData = new If ( e, project, this );
                if ( pOldIf )
-                       pOldIf->data.ifs.push_back ( pIf );
+                       pOldIf->data.ifs.push_back ( parseContext.ifData );
                else
-                       non_if_data.ifs.push_back ( pIf );
+                       non_if_data.ifs.push_back ( parseContext.ifData );
                subs_invalid = false;
        }
        else if ( e.name == "ifnot" )
        {
-               If* pOldIf = pIf;
-               pIf = new If ( e, project, this, true );
+               parseContext.ifData = new If ( e, project, this, true );
                if ( pOldIf )
-                       pOldIf->data.ifs.push_back ( pIf );
+                       pOldIf->data.ifs.push_back ( parseContext.ifData );
                else
-                       non_if_data.ifs.push_back ( pIf );
+                       non_if_data.ifs.push_back ( parseContext.ifData );
                subs_invalid = false;
        }
        else if ( e.name == "compilerflag" )
        {
                CompilerFlag* pCompilerFlag = new CompilerFlag ( project, this, e );
-               if ( pIf )
-                       pIf->data.compilerFlags.push_back ( pCompilerFlag );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.compilerFlags.push_back ( pCompilerFlag );
                else
                        non_if_data.compilerFlags.push_back ( pCompilerFlag );
                subs_invalid = true;
@@ -601,7 +633,7 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "pch" )
        {
-               if ( pIf )
+               if ( parseContext.ifData )
                        throw InvalidBuildFileException (
                                e.location,
                                "<pch> is not a valid sub-element of <if>" );
@@ -613,13 +645,39 @@ Module::ProcessXMLSubElement ( const XMLElement& e,
                        e, *this, File ( FixSeparator ( path + cSep + e.value ), false, "", true ) );
                subs_invalid = true;
        }
+       else if ( e.name == "compilationunit" )
+       {
+               if ( project.configuration.CompilationUnitsEnabled )
+               {
+                       CompilationUnit* pCompilationUnit = new CompilationUnit ( &project, this, &e );
+                       if ( parseContext.ifData )
+                               parseContext.ifData->data.compilationUnits.push_back ( pCompilationUnit );
+                       else
+                               non_if_data.compilationUnits.push_back ( pCompilationUnit );
+                       parseContext.compilationUnit = pCompilationUnit;
+               }
+               subs_invalid = false;
+       }
+       else if ( e.name == "autoregister" )
+       {
+               if ( autoRegister != NULL)
+               {
+                       throw InvalidBuildFileException ( e.location,
+                                                         "there can be only one <%s> element for a module",
+                                                         e.name.c_str() );
+               }
+               autoRegister = new AutoRegister ( project, this, e );
+               subs_invalid = true;
+       }
        if ( subs_invalid && e.subElements.size() > 0 )
                throw InvalidBuildFileException (
                        e.location,
                        "<%s> cannot have sub-elements",
                        e.name.c_str() );
        for ( size_t i = 0; i < e.subElements.size (); i++ )
-               ProcessXMLSubElement ( *e.subElements[i], subpath, pIf );
+               ProcessXMLSubElement ( *e.subElements[i], subpath, parseContext );
+       parseContext.ifData = pOldIf;
+       parseContext.compilationUnit = pOldCompilationUnit;
 }
 
 ModuleType
@@ -906,11 +964,10 @@ Module::HasFileWithExtension (
        const std::string& extension ) const
 {
        size_t i;
-       for ( i = 0; i < data.files.size (); i++ )
+       for ( i = 0; i < data.compilationUnits.size (); i++ )
        {
-               File& file = *data.files[i];
-               string file_ext = GetExtension ( file.name );
-               if ( !stricmp ( file_ext.c_str (), extension.c_str () ) )
+               CompilationUnit* compilationUnit = data.compilationUnits[i];
+               if ( compilationUnit->HasFileWithExtension ( extension ) )
                        return true;
        }
        for ( i = 0; i < data.ifs.size (); i++ )
@@ -952,13 +1009,6 @@ File::ProcessXML()
 {
 }
 
-bool
-File::IsGeneratedFile () const
-{
-       string extension = GetExtension ( name );
-       return ( extension == ".spec" || extension == ".SPEC" );
-}
-
 
 Library::Library ( const XMLElement& _node,
                    const Module& _module,
@@ -1232,3 +1282,85 @@ void
 PchFile::ProcessXML()
 {
 }
+
+
+AutoRegister::AutoRegister ( const Project& project_,
+                             const Module* module_,
+                             const XMLElement& node_ )
+       : project(project_),
+         module(module_),
+         node(node_)
+{
+       Initialize();
+}
+
+AutoRegister::~AutoRegister ()
+{
+}
+
+bool
+AutoRegister::IsSupportedModuleType ( ModuleType type )
+{
+       switch ( type )
+       {
+               case Win32DLL:
+                       return true;
+               case Kernel:
+               case KernelModeDLL:
+               case NativeDLL:
+               case NativeCUI:
+               case Win32CUI:
+               case Win32GUI:
+               case KernelModeDriver:
+               case BootSector:
+               case BootLoader:
+               case BuildTool:
+               case StaticLibrary:
+               case ObjectLibrary:
+               case Iso:
+               case LiveIso:
+               case Test:
+               case RpcServer:
+               case RpcClient:
+               case Alias:
+                       return false;
+       }
+       throw InvalidOperationException ( __FILE__,
+                                         __LINE__ );
+}
+
+AutoRegisterType
+AutoRegister::GetAutoRegisterType( string type )
+{
+       if ( type == "DllRegisterServer" )
+               return DllRegisterServer;
+       if ( type == "DllInstall" )
+               return DllInstall;
+       if ( type == "Both" )
+               return Both;
+       throw InvalidBuildFileException (
+               node.location,
+               "<autoregister> type attribute must be DllRegisterServer, DllInstall or Both." );
+}
+
+void
+AutoRegister::Initialize ()
+{
+       if ( !IsSupportedModuleType ( module->type ) )
+       {
+               throw InvalidBuildFileException (
+                       node.location,
+                       "<autoregister> is not applicable for this module type." );
+       }
+
+       const XMLAttribute* att = node.GetAttribute ( "infsection", true );
+       infSection = att->value;
+
+       att = node.GetAttribute ( "type", true );
+       type = GetAutoRegisterType ( att->value );
+}
+
+void
+AutoRegister::ProcessXML()
+{
+}