Move some more autogenerated files to intermediate directory
[reactos.git] / reactos / tools / rbuild / project.cpp
index 464626a..d68d4ac 100644 (file)
@@ -1,8 +1,25 @@
-
+/*
+ * Copyright (C) 2005 Casper S. Hornstrup
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
 #include "pch.h"
 #include <assert.h>
 
 #include "rbuild.h"
+#include "backend/backend.h"
 
 using std::string;
 using std::vector;
@@ -32,38 +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++ )
@@ -182,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 );
 }
@@ -206,12 +264,15 @@ Project::ReadXml ()
                {
                        node = head->subElements[i];
                        string path;
-                       this->ProcessXML ( path );
+                       ProcessXML ( path );
                        return;
                }
        }
 
-       throw InvalidBuildFileException (
+       if (node == NULL)
+               node = head->subElements[0];
+
+       throw XMLInvalidBuildFileException (
                node->location,
                "Document contains no 'project' tag." );
 }
@@ -231,16 +292,44 @@ 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++ )
-               ProcessXMLSubElement ( *node->subElements[i], path );
-       for ( i = 0; i < modules.size (); i++ )
-               modules[i]->ProcessXML ();
+       {
+               ParseContext parseContext;
+               ProcessXMLSubElement ( *node->subElements[i], path, parseContext );
+       }
+       
+       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++ )
@@ -250,24 +339,25 @@ Project::ProcessXML ( const string& path )
 void
 Project::ProcessXMLSubElement ( const XMLElement& e,
                                 const string& path,
-                                If* pIf )
+                                ParseContext& parseContext )
 {
        bool subs_invalid = false;
+       If* pOldIf = parseContext.ifData;
+       
        string subpath(path);
        if ( e.name == "module" )
        {
-               if ( pIf )
-                       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" )
@@ -285,14 +375,15 @@ 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" )
        {
-               Include* include = new Include ( *this, e );
-               if ( pIf )
-                       pIf->data.includes.push_back ( include );
+               Include* include = new Include ( *this, &e );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.includes.push_back ( include );
                else
                        non_if_data.includes.push_back ( include );
                subs_invalid = true;
@@ -300,8 +391,8 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        else if ( e.name == "define" )
        {
                Define* define = new Define ( *this, e );
-               if ( pIf )
-                       pIf->data.defines.push_back ( define );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.defines.push_back ( define );
                else
                        non_if_data.defines.push_back ( define );
                subs_invalid = true;
@@ -309,8 +400,8 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        else if ( e.name == "compilerflag" )
        {
                CompilerFlag* pCompilerFlag = new CompilerFlag ( *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;
@@ -322,39 +413,41 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        }
        else if ( e.name == "if" )
        {
-               If* pOldIf = pIf;
-               pIf = new If ( e, *this, NULL );
+               parseContext.ifData = new If ( e, *this, NULL );
                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, *this, NULL, true );
+               parseContext.ifData = new If ( e, *this, NULL, 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 == "property" )
        {
                Property* property = new Property ( e, *this, NULL );
-               if ( pIf )
-                       pIf->data.properties.push_back ( property );
+               if ( parseContext.ifData )
+                       parseContext.ifData->data.properties.push_back ( property );
                else
                        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, pIf );
+               ProcessXMLSubElement ( *e.subElements[i], subpath, parseContext );
+
+       parseContext.ifData = pOldIf;
 }
 
 Module*
@@ -381,10 +474,8 @@ Project::LocateModule ( const string& name ) const
        return NULL;
 }
 
-std::string
+const std::string&
 Project::GetProjectFilename () const
 {
        return xmlfile;
 }
-
-