make_msvcX_install_[config] patch by Brezenbak
[reactos.git] / reactos / tools / rbuild / project.cpp
index 453268c..a705c30 100644 (file)
@@ -1,16 +1,93 @@
-
+/*
+ * 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;
 
-Project::Project ( const string& filename )
+/* static */ string
+Environment::GetVariable ( const string& name )
+{
+       char* value = getenv ( name.c_str () );
+       if ( value != NULL && strlen ( value ) > 0 )
+               return ssprintf ( "%s",
+                                 value );
+       else
+               return "";
+}
+
+/* static */ string
+Environment::GetEnvironmentVariablePathOrDefault ( const string& name,
+                                                   const string& defaultValue )
+{
+       const string& environmentVariableValue = Environment::GetVariable ( name );
+       if ( environmentVariableValue.length () > 0 )
+               return NormalizeFilename ( environmentVariableValue );
+       else
+               return defaultValue;
+}
+
+/* static */ string
+Environment::GetIntermediatePath ()
+{
+       return GetEnvironmentVariablePathOrDefault ( "ROS_INTERMEDIATE",
+                                                    "obj-i386" );
+}
+
+/* static */ string
+Environment::GetOutputPath ()
+{
+       return GetEnvironmentVariablePathOrDefault ( "ROS_OUTPUT",
+                                                    "output-i386" );
+}
+
+/* static */ string
+Environment::GetInstallPath ()
+{
+       return GetEnvironmentVariablePathOrDefault ( "ROS_INSTALL",
+                                                    "reactos" );
+}
+
+ParseContext::ParseContext ()
+       : ifData (NULL),
+         compilationUnit (NULL)
+{
+}
+
+
+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)
 {
        ReadXml();
 }
@@ -18,8 +95,12 @@ Project::Project ( const string& filename )
 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++ )
@@ -41,72 +122,49 @@ Project::LookupProperty ( const string& name ) const
        return NULL;
 }
 
-void
-Project::WriteIfChanged ( char* outbuf,
-                             string filename )
+string
+Project::ResolveNextProperty ( string& s ) const
 {
-       FILE* out;
-       unsigned int end;
-       char* cmpbuf;
-       unsigned int stat;
-       
-       out = fopen ( filename.c_str (), "rb" );
-       if ( out == NULL )
-       {
-               out = fopen ( filename.c_str (), "wb" );
-               if ( out == NULL )
-                       throw AccessDeniedException ( filename );
-               fputs ( outbuf, out );
-               fclose ( out );
-               return;
-       }
-       
-       fseek ( out, 0, SEEK_END );
-       end = ftell ( out );
-       cmpbuf = (char*) malloc ( end );
-       if ( cmpbuf == NULL )
-       {
-               fclose ( out );
-               throw OutOfMemoryException ();
-       }
-       
-       fseek ( out, 0, SEEK_SET );
-       stat = fread ( cmpbuf, 1, end, out );
-       if ( stat != end )
-       {
-               free ( cmpbuf );
-               fclose ( out );
-               throw AccessDeniedException ( filename );
-       }
-       if ( end == strlen ( outbuf ) && memcmp ( cmpbuf, outbuf, end ) == 0 )
-       {
-               free ( cmpbuf );
-               fclose ( out );
-               return;
-       }
-       
-       free ( cmpbuf );
-       fclose ( out );
-       out = fopen ( filename.c_str (), "wb" );
-       if ( out == NULL )
+       size_t i = s.find ( "${" );
+       if ( i == string::npos )
+               i = s.find ( "$(" );
+       if ( i != string::npos )
        {
-               throw AccessDeniedException ( filename );
-       }
-       
-       stat = fwrite ( outbuf, 1, strlen ( outbuf ), out);
-       if ( strlen ( outbuf ) != stat )
-       {
-               fclose ( out );
-               throw AccessDeniedException ( filename );
+               string endCharacter;
+               if ( s[i + 1] == '{' )
+                       endCharacter = "}";
+               else
+                       endCharacter = ")";
+               size_t j = s.find ( endCharacter );
+               if ( j != string::npos )
+               {
+                       int propertyNameLength = j - i - 2;
+                       string propertyName = s.substr ( i + 2, propertyNameLength );
+                       const Property* property = LookupProperty ( propertyName );
+                       if ( property != NULL )
+                               return s.replace ( i, propertyNameLength + 3, property->value );
+               }
        }
+       return s;
+}
 
-       fclose ( out );
+string
+Project::ResolveProperties ( const string& s ) const
+{
+       string s2 = s;
+       string s3;
+       do
+       {
+               s3 = s2;
+               s2 = ResolveNextProperty ( s3 );
+       } while ( s2 != s3 );
+       return s2;
 }
 
 void
 Project::SetConfigurationOption ( char* s,
-                                     string name,
-                                     string* alternativeName )
+                                  string name,
+                                  string* alternativeName )
 {
        const Property* property = LookupProperty ( name );
        if ( property != NULL && property->value.length () > 0 )
@@ -161,7 +219,7 @@ Project::WriteConfigurationFile ()
 
        s = s + sprintf ( s, "#endif /* __INCLUDE_CONFIG_H */\n" );
 
-       WriteIfChanged ( buf, "include" SSEP "roscfg.h" );
+       FileSupportCode::WriteIfChanged ( buf, "include" + sSep + "roscfg.h" );
 
        free ( buf );
 }
@@ -169,6 +227,7 @@ Project::WriteConfigurationFile ()
 void
 Project::ExecuteInvocations ()
 {
+       fprintf( stderr, "ExecuteInvocations\n" );
        for ( size_t i = 0; i < modules.size (); i++ )
                modules[i]->InvokeModule ();
 }
@@ -190,7 +249,10 @@ Project::ReadXml ()
                }
        }
 
-       throw InvalidBuildFileException (
+       if (node == NULL)
+               node = head->subElements[0];
+
+       throw XMLInvalidBuildFileException (
                node->location,
                "Document contains no 'project' tag." );
 }
@@ -214,12 +276,39 @@ Project::ProcessXML ( const string& path )
 
        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;
+               }
+       }
        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++ )
@@ -229,24 +318,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" )
@@ -269,9 +359,9 @@ Project::ProcessXMLSubElement ( const XMLElement& e,
        }
        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;
@@ -279,8 +369,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;
@@ -288,8 +378,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;
@@ -301,29 +391,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" )
+       {
+               parseContext.ifData = new If ( e, *this, NULL, true );
+               if ( pOldIf )
+                       pOldIf->data.ifs.push_back ( parseContext.ifData );
+               else
+                       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*
@@ -355,5 +457,3 @@ Project::GetProjectFilename () const
 {
        return xmlfile;
 }
-
-