Add ROS_ARCH environment variable
[reactos.git] / reactos / tools / rbuild / rbuild.cpp
index 8fe50af..abe255f 100644 (file)
-// rbuild.cpp\r
-\r
-#ifdef _MSC_VER\r
-#pragma warning ( disable : 4786 ) // identifier was truncated to '255' characters in the debug information\r
-#endif//_MSC_VER\r
-\r
-#include <stdio.h>\r
-#include <io.h>\r
-#include <assert.h>\r
-#include <direct.h>\r
-#include "rbuild.h"\r
-\r
-using std::string;\r
-using std::vector;\r
-\r
-#ifdef WIN32\r
-#define getcwd _getcwd\r
-#endif//WIN32\r
-string working_directory;\r
-\r
-#ifdef _MSC_VER\r
-unsigned __int64\r
-#else\r
-unsigned long long\r
-#endif\r
-filelen ( FILE* f )\r
-{\r
-#ifdef WIN32\r
-       return _filelengthi64 ( _fileno(f) );\r
-#elif defined(UNIX)\r
-       struct stat64 file_stat;\r
-       if ( fstat64(fileno(f), &file_stat) != 0 )\r
-               return 0;\r
-       return file_stat.st_size;\r
-#endif\r
-}\r
-\r
-static const char* WS = " \t\r\n";\r
-static const char* WSEQ = " =\t\r\n";\r
-\r
-Path::Path()\r
-{\r
-       string s ( working_directory );\r
-       const char* p = strtok ( &s[0], "/\\" );\r
-       while ( p )\r
-       {\r
-               if ( *p )\r
-                       path.push_back ( p );\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-}\r
-\r
-Path::Path ( const Path& cwd, const string& file )\r
-{\r
-       string s ( cwd.Fixup ( file, false ) );\r
-       const char* p = strtok ( &s[0], "/\\" );\r
-       while ( p )\r
-       {\r
-               if ( *p )\r
-                       path.push_back ( p );\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-}\r
-\r
-string\r
-Path::Fixup ( const string& file, bool include_filename ) const\r
-{\r
-       if ( strchr ( "/\\", file[0] )\r
-#ifdef WIN32\r
-               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-               || file[1] == ':'\r
-#endif//WIN32\r
-               )\r
-       {\r
-               return file;\r
-       }\r
-       vector<string> pathtmp ( path );\r
-       string tmp ( file );\r
-       const char* prev = strtok ( &tmp[0], "/\\" );\r
-       const char* p = strtok ( NULL, "/\\" );\r
-       while ( p )\r
-       {\r
-               if ( !strcmp ( prev, "." ) )\r
-                       ; // do nothing\r
-               else if ( !strcmp ( prev, ".." ) )\r
-               {\r
-                       // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-#ifdef WIN32\r
-                       if ( pathtmp.size() > 1 )\r
-#else\r
-                       if ( pathtmp.size() )\r
-#endif\r
-                               pathtmp.resize ( pathtmp.size() - 1 );\r
-               }\r
-               else\r
-                       pathtmp.push_back ( prev );\r
-               prev = p;\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-       if ( include_filename )\r
-               pathtmp.push_back ( prev );\r
-\r
-       // reuse tmp variable to return recombined path\r
-       tmp.resize(0);\r
-       for ( size_t i = 0; i < pathtmp.size(); i++ )\r
-       {\r
-               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-#ifdef WIN32\r
-               if ( i ) tmp += "/";\r
-#else\r
-               tmp += "/";\r
-#endif\r
-               tmp += pathtmp[i];\r
-       }\r
-       return tmp;\r
-}\r
-\r
-/*static*/ string\r
-Path::RelativeFromWorkingDirectory ( const string& path )\r
-{\r
-       vector<string> vwork, vpath, vout;\r
-       Path::Split ( vwork, working_directory, true );\r
-       Path::Split ( vpath, path, true );\r
-#ifdef WIN32\r
-       // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-       // not possible to do relative across different drive letters\r
-       if ( vwork[0] != vpath[0] )\r
-               return path;\r
-#endif\r
-       size_t i = 0;\r
-       while ( i < vwork.size() && i < vpath.size() && vwork[i] == vpath[i] )\r
-               ++i;\r
-       if ( i < vwork.size() )\r
-       {\r
-               // path goes above our working directory, we will need some ..'s\r
-               for ( size_t j = 0; j < i; j++ )\r
-                       vout.push_back ( ".." );\r
-       }\r
-       while ( i < vpath.size() )\r
-               vout.push_back ( vpath[i++] );\r
-\r
-       // now merge vout into a string again\r
-       string out;\r
-       for ( i = 0; i < vout.size(); i++ )\r
-       {\r
-               // this squirreliness is b/c win32 has drive letters and *nix doesn't...\r
-#ifdef WIN32\r
-               if ( i ) out += "/";\r
-#else\r
-               out += "/";\r
-#endif\r
-               out += vout[i];\r
-       }\r
-       return out;\r
-}\r
-\r
-/*static*/ void\r
-Path::Split ( vector<string>& out,\r
-              const string& path,\r
-              bool include_last )\r
-{\r
-       string s ( path );\r
-       const char* prev = strtok ( &s[0], "/\\" );\r
-       const char* p = strtok ( NULL, "/\\" );\r
-       out.resize ( 0 );\r
-       while ( p )\r
-       {\r
-               out.push_back ( prev );\r
-               prev = p;\r
-               p = strtok ( NULL, "/\\" );\r
-       }\r
-       if ( include_last )\r
-               out.push_back ( prev );\r
-}\r
-\r
-XMLFile::XMLFile()\r
-{\r
-}\r
-\r
-void\r
-XMLFile::close()\r
-{\r
-       while ( _f.size() )\r
-       {\r
-               fclose ( _f.back() );\r
-               _f.pop_back();\r
-       }\r
-       _buf.resize(0);\r
-       _p = _end = NULL;\r
-}\r
-\r
-bool\r
-XMLFile::open(const string& filename)\r
-{\r
-       close();\r
-       FILE* f = fopen ( filename.c_str(), "rb" );\r
-       if ( !f )\r
-               return false;\r
-       unsigned long len = (unsigned long)filelen(f);\r
-       _buf.resize ( len );\r
-       fread ( &_buf[0], 1, len, f );\r
-       _p = _buf.c_str();\r
-       _end = _p + len;\r
-       _f.push_back ( f );\r
-       next_token();\r
-       return true;\r
-}\r
-\r
-// next_token() moves the pointer to next token, which may be\r
-// an xml element or a text element, basically it's a glorified\r
-// skipspace, normally the user of this class won't need to call\r
-// this function\r
-void\r
-XMLFile::next_token()\r
-{\r
-       _p += strspn ( _p, WS );\r
-}\r
-\r
-bool\r
-XMLFile::next_is_text()\r
-{\r
-       return *_p != '<';\r
-}\r
-\r
-bool\r
-XMLFile::more_tokens()\r
-{\r
-       return _p != _end;\r
-}\r
-\r
-// get_token() is used to return a token, and move the pointer\r
-// past the token\r
-bool\r
-XMLFile::get_token(string& token)\r
-{\r
-       const char* tokend;\r
-       if ( !strncmp ( _p, "<!--", 4 ) )\r
-       {\r
-               tokend = strstr ( _p, "-->" );\r
-               if ( !tokend )\r
-                       tokend = _end;\r
-               else\r
-                       tokend += 3;\r
-       }\r
-       else if ( *_p == '<' )\r
-       {\r
-               tokend = strchr ( _p, '>' );\r
-               if ( !tokend )\r
-                       tokend = _end;\r
-               else\r
-                       ++tokend;\r
-       }\r
-       else\r
-       {\r
-               tokend = strchr ( _p, '<' );\r
-               if ( !tokend )\r
-                       tokend = _end;\r
-               while ( tokend > _p && isspace(tokend[-1]) )\r
-                       --tokend;\r
-       }\r
-       if ( tokend == _p )\r
-               return false;\r
-       token = string ( _p, tokend-_p );\r
-       _p = tokend;\r
-       next_token();\r
-       return true;\r
-}\r
-\r
-XMLAttribute::XMLAttribute()\r
-{\r
-}\r
-\r
-XMLAttribute::XMLAttribute(const string& name_,\r
-                           const string& value_)\r
-       : name(name_), value(value_)\r
-{\r
-}\r
-\r
-XMLElement::XMLElement()\r
-       : parentElement(NULL)\r
-{\r
-}\r
-\r
-XMLElement::~XMLElement()\r
-{\r
-       size_t i;\r
-       for ( i = 0; i < attributes.size(); i++ )\r
-               delete attributes[i];\r
-       for ( i = 0; i < subElements.size(); i++ )\r
-               delete subElements[i];\r
-}\r
-\r
-void\r
-XMLElement::AddSubElement ( XMLElement* e )\r
-{\r
-       subElements.push_back ( e );\r
-       e->parentElement = this;\r
-}\r
-\r
-// Parse()\r
-// This function takes a single xml tag ( i.e. beginning with '<' and\r
-// ending with '>', and parses out it's tag name and constituent\r
-// attributes.\r
-// Return Value: returns true if you need to look for a </tag> for\r
-// the one it just parsed...\r
-bool\r
-XMLElement::Parse(const string& token,\r
-                  bool& end_tag)\r
-{\r
-       const char* p = token.c_str();\r
-       assert ( *p == '<' );\r
-       ++p;\r
-       p += strspn ( p, WS );\r
-\r
-       // check if this is a comment\r
-       if ( !strncmp ( p, "!--", 3 ) )\r
-       {\r
-               name = "!--";\r
-               end_tag = false;\r
-               return false; // never look for end tag to a comment\r
-       }\r
-\r
-       end_tag = ( *p == '/' );\r
-       if ( end_tag )\r
-       {\r
-               ++p;\r
-               p += strspn ( p, WS );\r
-       }\r
-       const char* end = strpbrk ( p, WS );\r
-       if ( !end )\r
-       {\r
-               end = strpbrk ( p, "/>" );\r
-               assert ( end );\r
-       }\r
-       name = string ( p, end-p );\r
-       p = end;\r
-       p += strspn ( p, WS );\r
-       while ( *p != '>' && *p != '/' )\r
-       {\r
-               end = strpbrk ( p, WSEQ );\r
-               if ( !end )\r
-               {\r
-                       end = strpbrk ( p, "/>" );\r
-                       assert ( end );\r
-               }\r
-               string attribute ( p, end-p ), value;\r
-               p = end;\r
-               p += strspn ( p, WS );\r
-               if ( *p == '=' )\r
-               {\r
-                       ++p;\r
-                       p += strspn ( p, WS );\r
-                       char quote = 0;\r
-                       if ( strchr ( "\"'", *p ) )\r
-                       {\r
-                               quote = *p++;\r
-                               end = strchr ( p, quote );\r
-                       }\r
-                       else\r
-                       {\r
-                               end = strpbrk ( p, WS );\r
-                       }\r
-                       if ( !end )\r
-                       {\r
-                               end = strchr ( p, '>' );\r
-                               assert(end);\r
-                               if ( end[-1] == '/' )\r
-                                       end--;\r
-                       }\r
-                       value = string ( p, end-p );\r
-                       p = end;\r
-                       if ( quote && *p == quote )\r
-                               p++;\r
-                       p += strspn ( p, WS );\r
-               }\r
-               attributes.push_back ( new XMLAttribute ( attribute, value ) );\r
-       }\r
-       return !( *p == '/' ) && !end_tag;\r
-}\r
-\r
-XMLAttribute*\r
-XMLElement::GetAttribute ( const string& attribute,\r
-                           bool required )\r
-{\r
-       // this would be faster with a tree-based container, but our attribute\r
-       // lists are likely to stay so short as to not be an issue.\r
-       for ( size_t i = 0; i < attributes.size(); i++ )\r
-       {\r
-               if ( attribute == attributes[i]->name )\r
-                       return attributes[i];\r
-       }\r
-       if ( required )\r
-       {\r
-               printf ( "syntax error: attribute '%s' required for <%s>\n",\r
-                       attribute.c_str(), name.c_str() );\r
-       }\r
-       return NULL;\r
-}\r
-\r
-const XMLAttribute*\r
-XMLElement::GetAttribute ( const string& attribute,\r
-                           bool required ) const\r
-{\r
-       // this would be faster with a tree-based container, but our attribute\r
-       // lists are likely to stay so short as to not be an issue.\r
-       for ( size_t i = 0; i < attributes.size(); i++ )\r
-       {\r
-               if ( attribute == attributes[i]->name )\r
-                       return attributes[i];\r
-       }\r
-       if ( required )\r
-       {\r
-               printf ( "syntax error: attribute '%s' required for <%s>\n",\r
-                       attribute.c_str(), name.c_str() );\r
-       }\r
-       return NULL;\r
-}\r
-\r
-// XMLParse()\r
-// This function reads a "token" from the file loaded in XMLFile\r
-// REM TODO FIXME: At the moment it can't handle comments or non-xml tags.\r
-// if it finds a tag that is non-singular, it parses sub-elements and/or\r
-// inner text into the XMLElement that it is building to return.\r
-// Return Value: an XMLElement allocated via the new operator that contains\r
-// it's parsed data. Keep calling this function until it returns NULL\r
-// (no more data)\r
-XMLElement*\r
-XMLParse(XMLFile& f,\r
-         const Path& path,\r
-         bool* pend_tag = NULL)\r
-{\r
-       string token;\r
-       if ( !f.get_token(token) )\r
-               return NULL;\r
-       bool end_tag;\r
-\r
-       while ( token[0] != '<' )\r
-       {\r
-               printf ( "syntax error: expecting xml tag, not '%s'\n", token.c_str() );\r
-               if ( !f.get_token(token) )\r
-                       return NULL;\r
-       }\r
-\r
-       XMLElement* e = new XMLElement;\r
-       bool bNeedEnd = e->Parse ( token, end_tag );\r
-\r
-       if ( e->name == "xi:include" )\r
-       {\r
-               XMLAttribute* att;\r
-               att = e->GetAttribute("href",true);\r
-               if ( att )\r
-               {\r
-                       string file ( path.Fixup(att->value,true) );\r
-                       string top_file ( Path::RelativeFromWorkingDirectory ( file ) );\r
-                       e->attributes.push_back ( new XMLAttribute ( "top_href", top_file ) );\r
-                       XMLFile fInc;\r
-                       if ( !fInc.open ( file ) )\r
-                               printf ( "xi:include error, couldn't find file '%s'\n", file.c_str() );\r
-                       else\r
-                       {\r
-                               Path path2 ( path, att->value );\r
-                               for ( ;; )\r
-                               {\r
-                                       XMLElement* e2 = XMLParse ( fInc, path2 );\r
-                                       if ( !e2 )\r
-                                               break;\r
-                                       e->AddSubElement ( e2 );\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       if ( !bNeedEnd )\r
-       {\r
-               if ( pend_tag )\r
-                       *pend_tag = end_tag;\r
-               else if ( end_tag )\r
-               {\r
-                       delete e;\r
-                       printf ( "syntax error: end tag '%s' not expected\n", token.c_str() );\r
-                       return NULL;\r
-               }\r
-               return e;\r
-       }\r
-       bool bThisMixingErrorReported = false;\r
-       while ( f.more_tokens() )\r
-       {\r
-               if ( f.next_is_text() )\r
-               {\r
-                       if ( !f.get_token ( token ) || !token.size() )\r
-                       {\r
-                               printf ( "internal tool error - get_token() failed when more_tokens() returned true\n" );\r
-                               break;\r
-                       }\r
-                       if ( e->subElements.size() && !bThisMixingErrorReported )\r
-                       {\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
-                               bThisMixingErrorReported = true;\r
-                       }\r
-                       if ( e->value.size() )\r
-                       {\r
-                               printf ( "syntax error: multiple instances of inner text\n" );\r
-                               e->value += " " + token;\r
-                       }\r
-                       else\r
-                               e->value = token;\r
-               }\r
-               else\r
-               {\r
-                       XMLElement* e2 = XMLParse ( f, path, &end_tag );\r
-                       if ( end_tag )\r
-                       {\r
-                               if ( e->name != e2->name )\r
-                                       printf ( "end tag name mismatch\n" );\r
-                               delete e2;\r
-                               break;\r
-                       }\r
-                       if ( e->value.size() && !bThisMixingErrorReported )\r
-                       {\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
-                               bThisMixingErrorReported = true;\r
-                       }\r
-                       e->AddSubElement ( e2 );\r
-               }\r
-       }\r
-       return e;\r
-}\r
-\r
-Project::~Project()\r
-{\r
-       for ( size_t i = 0; i < modules.size(); i++ )\r
-               delete modules[i];\r
-}\r
-\r
-void\r
-Project::ProcessXML ( const XMLElement& e, const string& path )\r
-{\r
-       const XMLAttribute *att;\r
-       string subpath(path);\r
-       if ( e.name == "project" )\r
-       {\r
-               att = e.GetAttribute ( "name", false );\r
-               if ( !att )\r
-                       name = "Unnamed";\r
-               else\r
-                       name = att->value;\r
-       }\r
-       else if ( e.name == "module" )\r
-       {\r
-               att = e.GetAttribute ( "name", true );\r
-               if ( !att )\r
-                       return;\r
-               Module* module = new Module ( e, att->value, path );\r
-               modules.push_back ( module );\r
-               module->ProcessXML ( e, path );\r
-               return;\r
-       }\r
-       else if ( e.name == "directory" )\r
-       {\r
-               // this code is duplicated between Project::ProcessXML() and Module::ProcessXML() :(\r
-               const XMLAttribute* att = e.GetAttribute ( "name", true );\r
-               if ( !att )\r
-                       return;\r
-               subpath = path + "/" + att->value;\r
-       }\r
-       for ( size_t i = 0; i < e.subElements.size(); i++ )\r
-               ProcessXML ( *e.subElements[i], subpath );\r
-}\r
-\r
-int\r
-main ( int argc, char** argv )\r
-{\r
-       // store the current directory for path calculations\r
-       working_directory.resize ( _MAX_PATH );\r
-       working_directory[0] = 0;\r
-       getcwd ( &working_directory[0], working_directory.size() );\r
-       working_directory.resize ( strlen ( working_directory.c_str() ) );\r
-\r
-       XMLFile f;\r
-       Path path;\r
-       string xml_file ( "ReactOS.xml" );\r
-       if ( !f.open ( xml_file ) )\r
-       {\r
-               printf ( "couldn't open ReactOS.xml!\n" );\r
-               return -1;\r
-       }\r
-\r
-       vector<string> xml_dependencies;\r
-       xml_dependencies.push_back ( xml_file );\r
-       for ( ;; )\r
-       {\r
-               XMLElement* head = XMLParse ( f, path );\r
-               if ( !head )\r
-                       break; // end of file\r
-\r
-               if ( head->name == "!--" )\r
-                       continue; // ignore comments\r
-\r
-               if ( head->name != "project" )\r
-               {\r
-                       printf ( "error: expecting 'project', got '%s'\n", head->name.c_str() );\r
-                       continue;\r
-               }\r
-\r
-               Project* proj = new Project;\r
-               proj->ProcessXML ( *head, "." );\r
-\r
-               // REM TODO FIXME actually do something with Project object...\r
-               printf ( "Found %d modules:\n", proj->modules.size() );\r
-               for ( size_t i = 0; i < proj->modules.size(); i++ )\r
-               {\r
-                       Module& m = *proj->modules[i];\r
-                       printf ( "\t%s in folder: %s\n",\r
-                                m.name.c_str(),\r
-                                m.path.c_str() );\r
-                       printf ( "\txml dependencies:\n\t\tReactOS.xml\n" );\r
-                       const XMLElement* e = &m.node;\r
-                       while ( e )\r
-                       {\r
-                               if ( e->name == "xi:include" )\r
-                               {\r
-                                       const XMLAttribute* att = e->GetAttribute("top_href",false);\r
-                                       if ( att )\r
-                                       {\r
-                                               printf ( "\t\t%s\n", att->value.c_str() );\r
-                                       }\r
-                               }\r
-                               e = e->parentElement;\r
-                       }\r
-                       printf ( "\tfiles:\n" );\r
-                       for ( size_t j = 0; j < m.files.size(); j++ )\r
-                       {\r
-                               printf ( "\t\t%s\n", m.files[j]->name.c_str() );\r
-                       }\r
-               }\r
-\r
-               delete proj;\r
-               delete head;\r
-       }\r
-\r
-       return 0;\r
-}\r
+/*
+ * 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 <typeinfo>
+#include <algorithm>
+
+#include <stdio.h>
+#ifdef WIN32
+#include <io.h>
+#endif
+#include <assert.h>
+
+#include "rbuild.h"
+#include "backend/backend.h"
+#include "backend/mingw/mingw.h"
+
+using std::string;
+using std::vector;
+
+static string BuildSystem;
+static string RootXmlFile;
+static Configuration configuration;
+
+bool
+ParseAutomaticDependencySwitch (
+       char switchChar2,
+       char* switchStart )
+{
+       switch ( switchChar2 )
+       {
+               case 'd':
+                       configuration.AutomaticDependencies = false;
+                       break;
+               case 'm':
+                       if ( strlen ( switchStart ) <= 3 )
+                       {
+                               printf ( "Switch -dm requires a module name\n" );
+                               return false;
+                       }
+                       configuration.CheckDependenciesForModuleOnly = true;
+                       configuration.CheckDependenciesForModuleOnlyModule = string(&switchStart[3]);
+                       break;
+               default:
+                       printf ( "Unknown switch -d%c\n",
+                                switchChar2 );
+                       return false;
+       }
+       return true;
+}
+
+bool
+ParseCompilationUnitSwitch (
+       char switchChar2,
+       char* switchStart )
+{
+       switch ( switchChar2 )
+       {
+               case 'd':
+                       configuration.CompilationUnitsEnabled = false;
+                       break;
+               default:
+                       printf ( "Unknown switch -u%c\n",
+                                switchChar2 );
+                       return false;
+       }
+       return true;
+}
+
+bool
+ParseVCProjectSwitch (
+       char switchChar2,
+       char* switchStart )
+{
+       string temp;
+
+       switch ( switchChar2 )
+       {
+               case 's':
+                       if ( strlen ( switchStart ) <= 3 )
+                       {
+                               printf ( "Switch -dm requires a module name\n" );
+                               return false;
+                       }
+                       configuration.VSProjectVersion = string(&switchStart[3]);
+
+                       if (configuration.VSProjectVersion.at(0) == '{') {
+                               printf ( "Error: invalid char {\n" );
+                               return false;
+                       }
+
+                       if (configuration.VSProjectVersion.length() == 1) //7,8
+                               configuration.VSProjectVersion.append(".00");
+
+                       if (configuration.VSProjectVersion.length() == 3) //7.1
+                               configuration.VSProjectVersion.append("0");
+
+                       break;
+               case 'c':
+                       configuration.VSConfigurationType = string (&switchStart[3]);
+                       configuration.InstallFiles = true;
+                       break;
+               case 'o':
+                       if ( strlen ( switchStart ) <= 3 )
+                       {
+                               printf ( "Invalid switch\n" );
+                               return false;
+                       }
+                       temp = string (&switchStart[3]);
+                       if ( temp.find ("configuration") != string::npos )
+                               configuration.UseConfigurationInPath = true;
+                       
+                       if ( temp.find ("version") != string::npos )
+                               configuration.UseVSVersionInPath = true;
+                       break;
+               default:
+                       printf ( "Unknown switch -d%c\n",
+                                switchChar2 );
+                       return false;
+       }
+       return true;
+}
+
+bool
+ParseMakeSwitch ( char switchChar2 )
+{
+       switch ( switchChar2 )
+       {
+               case 'i':
+                       configuration.MakeHandlesInstallDirectories = true;
+                       break;
+               default:
+                       printf ( "Unknown switch -m%c\n",
+                                switchChar2 );
+                       return false;
+       }
+       return true;
+}
+
+bool
+ParseProxyMakefileSwitch ( char switchChar2 )
+{
+       switch ( switchChar2 )
+       {
+               case 's':
+                       configuration.GenerateProxyMakefilesInSourceTree = true;
+                       break;
+               default:
+                       printf ( "Unknown switch -p%c\n",
+                                switchChar2 );
+                       return false;
+       }
+       return true;
+}
+
+bool
+ParseSwitch ( int argc, char** argv, int index )
+{
+       char switchChar = strlen ( argv[index] ) > 1 ? argv[index][1] : ' ';
+       char switchChar2 = strlen ( argv[index] ) > 2 ? argv[index][2] : ' ';
+       switch ( switchChar )
+       {
+               case 'v':
+                       if (switchChar2 == 's' || switchChar2 == 'c' || switchChar2 == 'o')
+                       {
+                               return ParseVCProjectSwitch (
+                                       switchChar2,
+                                       argv[index] );
+                       }
+                       else
+                               configuration.Verbose = true;
+                       break;
+               case 'c':
+                       configuration.CleanAsYouGo = true;
+                       break;
+               case 'd':
+                       return ParseAutomaticDependencySwitch (
+                               switchChar2,
+                               argv[index] );
+               case 'u':
+                       return ParseCompilationUnitSwitch (
+                               switchChar2,
+                               argv[index] );
+               case 'r':
+                       RootXmlFile = string(&argv[index][2]);
+                       break;
+               case 'm':
+                       return ParseMakeSwitch ( switchChar2 );
+               case 'p':
+                       return ParseProxyMakefileSwitch ( switchChar2 );
+               default:
+                       printf (
+                               "Unknown switch -%c\n",
+                               switchChar );
+                       return false;
+       }
+       return true;
+}
+
+bool
+ParseArguments ( int argc, char** argv )
+{
+       if ( argc < 2 )
+               return false;
+       
+       for ( int i = 1; i < argc; i++ )
+       {
+               if ( argv[i][0] == '-' )
+               {
+                       if ( !ParseSwitch ( argc, argv, i ) )
+                               return false;
+               }
+               else
+                       BuildSystem = argv[i];
+       }
+       
+       return true;
+}
+
+int
+main ( int argc, char** argv )
+{
+       InitializeEnvironment ();
+
+       if ( !ParseArguments ( argc, argv ) )
+       {
+               printf ( "Generates project files for buildsystems\n\n" );
+               printf ( "  rbuild [switches] -r{rootfile.rbuild} buildsystem\n\n" );
+               printf ( "Switches:\n" );
+               printf ( "  -v            Be verbose.\n" );
+               printf ( "  -c            Clean as you go. Delete generated files as soon as they are not\n" );
+               printf ( "                needed anymore.\n" );
+               printf ( "  -dd           Disable automatic dependencies.\n" );
+               printf ( "  -dm{module}   Check only automatic dependencies for this module.\n" );
+               printf ( "  -ud           Disable multiple source files per compilation unit.\n" );
+               printf ( "  -mi           Let make handle creation of install directories. Rbuild will\n" );
+               printf ( "                not generate the directories.\n" );
+               printf ( "  -ps           Generate proxy makefiles in source tree instead of the output.\n" );
+               printf ( "                tree.\n" );
+               printf ( "  -vs{version}  Version of MS VS project files. Default is %s.\n", MS_VS_DEF_VERSION );
+               printf ( "  -vo{version|configuration} Adds subdirectory path to the default Intermediate-Outputdirectory.\n" );
+               printf ( "\n" );
+               printf ( "  buildsystem   Target build system. Can be one of:\n" );
+
+               std::map<std::string,Backend::Factory*>::iterator iter;
+               for (iter = Backend::Factory::map_begin(); iter != Backend::Factory::map_end(); iter++)
+               {
+                       Backend::Factory *factory = iter->second;
+                       printf ( "                %-10s %s\n", factory->Name(), factory->Description());
+               }
+               return 1;
+       }
+       try
+       {
+               if ( RootXmlFile.length () == 0 )
+                       throw MissingArgumentException ( "-r" );
+
+               string projectFilename ( RootXmlFile );
+
+               printf ( "Reading build files..." );
+               Project project ( configuration, projectFilename );
+               printf ( "done\n" );
+
+               project.SetBackend ( Backend::Factory::Create (
+                       BuildSystem,
+                       project,
+                       configuration ) );
+
+               project.WriteConfigurationFile ();
+               project.ExecuteInvocations ();
+               project.GetBackend().Process();
+
+               return 0;
+       }
+       catch ( Exception& ex )
+       {
+               printf ( "%s\n", (*ex).c_str () );
+               return 1;
+       }
+       catch ( XMLException& ex )
+       {
+               printf ( "%s\n", (*ex).c_str () );
+               return 1;
+       }
+}