Move some more autogenerated files to intermediate directory
[reactos.git] / reactos / tools / rbuild / project.cpp
index 297a63e..d68d4ac 100644 (file)
@@ -19,6 +19,7 @@
 #include <assert.h>
 
 #include "rbuild.h"
+#include "backend/backend.h"
 
 using std::string;
 using std::vector;
@@ -48,43 +49,79 @@ Environment::GetEnvironmentVariablePathOrDefault ( const string& name,
 /* static */ string
 Environment::GetIntermediatePath ()
 {
+       string defaultIntermediate =
+               string( "obj-" ) + GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT", "i386" );
        return GetEnvironmentVariablePathOrDefault ( "ROS_INTERMEDIATE",
-                                                    "obj-i386" );
+                                                    defaultIntermediate );
 }
 
 /* static */ string
 Environment::GetOutputPath ()
 {
+       string defaultOutput =
+               string( "output-" ) + GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT", "i386" );
        return GetEnvironmentVariablePathOrDefault ( "ROS_OUTPUT",
-                                                    "output-i386" );
+                                                    defaultOutput );
 }
 
 /* static */ string
 Environment::GetInstallPath ()
 {
+       string defaultInstall =
+               string( "reactos." ) + GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT", "" );
        return GetEnvironmentVariablePathOrDefault ( "ROS_INSTALL",
+                                                    defaultInstall );
+}
+
+/* static */ string
+Environment::GetCdOutputPath ()
+{
+       return GetEnvironmentVariablePathOrDefault ( "ROS_CDOUTPUT",
                                                     "reactos" );
 }
 
+/* static */ string
+Environment::GetAutomakeFile ( const std::string& defaultFile )
+{
+       return GetEnvironmentVariablePathOrDefault ( "ROS_AUTOMAKE",
+                                                    defaultFile );
+}
+
 ParseContext::ParseContext ()
        : ifData (NULL),
          compilationUnit (NULL)
 {
 }
 
-Project::Project ( const string& filename )
+
+FileLocation::FileLocation ( Directory* directory,
+                             std::string filename )
+                             : directory (directory),
+                               filename (filename)
+{
+}
+
+
+Project::Project ( const Configuration& configuration,
+                   const string& filename )
        : xmlfile (filename),
          node (NULL),
-         head (NULL)
+         head (NULL),
+         configuration (configuration)
 {
+       _backend = NULL;
        ReadXml();
 }
 
 Project::~Project ()
 {
        size_t i;
+       if ( _backend )
+               delete _backend;
+#ifdef NOT_NEEDED_SINCE_THESE_ARE_CLEANED_BY_IFABLE_DATA
        for ( i = 0; i < modules.size (); i++ )
                delete modules[i];
+#endif
        for ( i = 0; i < linkerFlags.size (); i++ )
                delete linkerFlags[i];
        for ( i = 0; i < cdfiles.size (); i++ )
@@ -203,7 +240,7 @@ Project::WriteConfigurationFile ()
 
        s = s + sprintf ( s, "#endif /* __INCLUDE_CONFIG_H */\n" );
 
-       FileSupportCode::WriteIfChanged ( buf, "include" + sSep + "roscfg.h" );
+       FileSupportCode::WriteIfChanged ( buf, Environment::GetIntermediatePath() + sSep + "include" + sSep + "roscfg.h" );
 
        free ( buf );
 }
@@ -227,7 +264,7 @@ Project::ReadXml ()
                {
                        node = head->subElements[i];
                        string path;
-                       this->ProcessXML ( path );
+                       ProcessXML ( path );
                        return;
                }
        }
@@ -235,7 +272,7 @@ Project::ReadXml ()
        if (node == NULL)
                node = head->subElements[0];
 
-       throw InvalidBuildFileException (
+       throw XMLInvalidBuildFileException (
                node->location,
                "Document contains no 'project' tag." );
 }
@@ -255,7 +292,7 @@ Project::ProcessXML ( const string& path )
 
        att = node->GetAttribute ( "makefile", true );
        assert(att);
-       makefile = att->value;
+       makefile = Environment::GetAutomakeFile ( att->value );
 
        size_t i;
        for ( i = 0; i < node->subElements.size (); i++ )
@@ -263,11 +300,36 @@ Project::ProcessXML ( const string& path )
                ParseContext parseContext;
                ProcessXMLSubElement ( *node->subElements[i], path, parseContext );
        }
-       for ( i = 0; i < modules.size (); i++ )
-               modules[i]->ProcessXML ();
+       
+       non_if_data.ProcessXML ();
+
+       non_if_data.ExtractModules( modules );
+
+       for ( i = 0; i < non_if_data.ifs.size (); i++ )
+       {
+               const Property *property = 
+                   LookupProperty( non_if_data.ifs[i]->property );
+
+               if( !property ) continue;
+
+               bool conditionTrue = 
+                       (non_if_data.ifs[i]->negated && 
+                        (property->value != non_if_data.ifs[i]->value)) ||
+                       (property->value == non_if_data.ifs[i]->value);
+               if ( conditionTrue )
+                       non_if_data.ifs[i]->data.ExtractModules( modules );
+               else
+               {
+                       If * if_data = non_if_data.ifs[i];
+                       non_if_data.ifs.erase ( non_if_data.ifs.begin () + i );
+                       delete if_data;
+                       i--;
+               }
+       }
        for ( i = 0; i < linkerFlags.size (); i++ )
                linkerFlags[i]->ProcessXML ();
-       non_if_data.ProcessXML ();
+       for ( i = 0; i < modules.size (); i++ )
+               modules[i]->ProcessXML ();
        for ( i = 0; i < cdfiles.size (); i++ )
                cdfiles[i]->ProcessXML ();
        for ( i = 0; i < installfiles.size (); i++ )
@@ -280,21 +342,22 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
                                 ParseContext& parseContext )
 {
        bool subs_invalid = false;
+       If* pOldIf = parseContext.ifData;
+       
        string subpath(path);
        if ( e.name == "module" )
        {
-               if ( parseContext.ifData )
-                       throw InvalidBuildFileException (
-                               e.location,
-                               "<module> is not a valid sub-element of <if>" );
                Module* module = new Module ( *this, e, path );
                if ( LocateModule ( module->name ) )
-                       throw InvalidBuildFileException (
+                       throw XMLInvalidBuildFileException (
                                node->location,
                                "module name conflict: '%s' (originally defined at %s)",
                                module->name.c_str(),
                                module->node.location.c_str() );
-               modules.push_back ( module );
+               if ( parseContext.ifData )
+                   parseContext.ifData->data.modules.push_back( module );
+               else
+                   non_if_data.modules.push_back ( module );
                return; // defer processing until later
        }
        else if ( e.name == "cdfile" )
@@ -312,8 +375,9 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        else if ( e.name == "directory" )
        {
                const XMLAttribute* att = e.GetAttribute ( "name", true );
+               const XMLAttribute* base = e.GetAttribute ( "root", false );
                assert(att);
-               subpath = GetSubPath ( e.location, path, att->value );
+               subpath = GetSubPath ( *this, e.location, path, base, att->value );
        }
        else if ( e.name == "include" )
        {
@@ -349,7 +413,6 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "if" )
        {
-               If* pOldIf = parseContext.ifData;
                parseContext.ifData = new If ( e, *this, NULL );
                if ( pOldIf )
                        pOldIf->data.ifs.push_back ( parseContext.ifData );
@@ -359,7 +422,6 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "ifnot" )
        {
-               If* pOldIf = parseContext.ifData;
                parseContext.ifData = new If ( e, *this, NULL, true );
                if ( pOldIf )
                        pOldIf->data.ifs.push_back ( parseContext.ifData );
@@ -376,12 +438,16 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
                        non_if_data.properties.push_back ( property );
        }
        if ( subs_invalid && e.subElements.size() )
-               throw InvalidBuildFileException (
+       {
+               throw XMLInvalidBuildFileException (
                        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, parseContext );
+
+       parseContext.ifData = pOldIf;
 }
 
 Module*
@@ -408,7 +474,7 @@ Project::LocateModule ( const string& name ) const
        return NULL;
 }
 
-std::string
+const std::string&
 Project::GetProjectFilename () const
 {
        return xmlfile;