store pointer to Backend in the Project class so properties of the Backend object...
[reactos.git] / reactos / tools / rbuild / rbuild.cpp
index 617b878..e838e86 100644 (file)
-// rbuild.cpp\r
-\r
-#ifdef _MSC_VER\r
-#pragma warning ( disable : 4786 )\r
-#endif//_MSC_VER\r
-\r
-#include <stdio.h>\r
-#include <io.h>\r
-#include <assert.h>\r
-#include "rbuild.h"\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
-XMLFile::XMLFile()\r
-{\r
-}\r
-\r
-void 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 XMLFile::open(const char* filename)\r
-{\r
-       close();\r
-       FILE* f = fopen ( filename, "r" );\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 XMLFile::next_token()\r
-{\r
-       _p += strspn ( _p, WS );\r
-}\r
-\r
-bool XMLFile::next_is_text()\r
-{\r
-       return *_p != '<';\r
-}\r
-\r
-bool 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 XMLFile::get_token(string& token)\r
-{\r
-       const char* tokend;\r
-       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
-#if 0\r
-bool XMLFile::getc(char& c)\r
-{\r
-       while ( _bufidx >= _buf.size() )\r
-       {\r
-               static buf[4096];\r
-               if ( !fgets ( buf, sizeof(buf), _f.back() ) )\r
-               {\r
-                       fclose ( _f.back() );\r
-                       f.pop_back();\r
-                       continue;\r
-               }\r
-               _buf = buf;\r
-               _bufidx = 0;\r
-               // REM TODO FIXME - check for and load includes here...\r
-               /*char* p = &_buf[0];\r
-               p += strspn ( p, " \t" );\r
-               if ( *p++ != '#' )\r
-                       break;\r
-               p += strspn ( p, " \t" );\r
-               if ( strncmp ( p, "include", 7 ) )\r
-                       break;\r
-               p += 7;\r
-               if ( !isspace(*p++) )\r
-                       break;*/\r
-\r
-       }\r
-       c = _buf[_bufidx++];\r
-       return true;\r
-}\r
-#endif\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
-XMLAttribute::XMLAttribute(const XMLAttribute& src)\r
-{\r
-       name = src.name;\r
-       value = src.value;\r
-}\r
-\r
-XMLAttribute& XMLAttribute::operator=(const XMLAttribute& src)\r
-{\r
-       name = src.name;\r
-       value = src.value;\r
-       return *this;\r
-}\r
-\r
-\r
-XMLElement::XMLElement()\r
-{\r
-}\r
-\r
-// Parse() returns true if you need to look for a </tag> for\r
-// this one...\r
-bool 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
-       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 ( XMLAttribute ( attribute, value ) );\r
-       }\r
-       return !( *p == '/' ) && !end_tag;\r
-}\r
-\r
-\r
-XMLElement* XMLParse(XMLFile& f,\r
-                     bool* pend_tag = NULL)\r
-{\r
-       string token;\r
-       if ( !f.get_token(token) )\r
-               return NULL;\r
-       XMLElement* e = new XMLElement;\r
-       bool end_tag;\r
-       if ( !e->Parse ( token, end_tag ) )\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
-       while ( f.more_tokens() )\r
-       {\r
-               if ( f.next_is_text() )\r
-               {\r
-                       if ( !f.get_token ( token ) )\r
-                       {\r
-                               printf ( "internal tool error - get_token() failed when more_tokens() returned true\n" );\r
-                               break;\r
-                       }\r
-                       if ( e->subElements.size() )\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\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, &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
-                       e->subElements.push_back ( e2 );\r
-               }\r
-       }\r
-       return e;\r
-}\r
-\r
-int main ( int argc, char** argv )\r
-{\r
-       XMLFile f;\r
-       if ( !f.open ( "rbuild.xml" ) )\r
-       {\r
-               printf ( "couldn't open rbuild.xml!\n" );\r
-               return -1;\r
-       }\r
-\r
-       XMLElement* head = XMLParse(f);\r
-       if (head)\r
-       {\r
-               // REM TODO FIXME actually do something with the parsed info\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 <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 = "ReactOS.xml";
+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 )
+{
+       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;
+               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')
+                       {
+                               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] 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 ( "  -r{file.xml}  Name of the root xml file. Default is ReactOS.xml.\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 ( "\n" );
+               printf ( "  buildsystem   Target build system. Can be one of:\n" );
+               printf ( "                 mingw   MinGW\n" );
+               printf ( "                 devcpp  DevC++\n" );
+               printf ( "                 msvc    MS Visual Studio\n" );
+               return 1;
+       }
+       try
+       {
+               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;
+       }
+}