From 477157b4d4a3717a1e0d4ed2c986e566296e4779 Mon Sep 17 00:00:00 2001 From: Royce Mitchell III Date: Thu, 6 Jan 2005 02:55:11 +0000 Subject: [PATCH] Factories for Backend creation svn path=/branches/xmlbuildsystem/; revision=12845 --- reactos/tools/rbuild/backend/backend.cpp | 29 +++++++++++ reactos/tools/rbuild/backend/backend.h | 25 +++++++++ reactos/tools/rbuild/backend/mingw/mingw.cpp | 33 ++++++++---- reactos/tools/rbuild/backend/mingw/mingw.h | 3 ++ .../rbuild/backend/mingw/modulehandler.cpp | 25 +++++---- reactos/tools/rbuild/exception.cpp | 21 ++++++-- reactos/tools/rbuild/exception.h | 14 +++++ reactos/tools/rbuild/module.cpp | 8 +-- reactos/tools/rbuild/project.cpp | 3 +- reactos/tools/rbuild/rbuild.cpp | 10 +++- reactos/tools/rbuild/ssprintf.cpp | 51 ++++++++++++------- 11 files changed, 176 insertions(+), 46 deletions(-) diff --git a/reactos/tools/rbuild/backend/backend.cpp b/reactos/tools/rbuild/backend/backend.cpp index 000f4fd9dd5..4375d21e1fa 100644 --- a/reactos/tools/rbuild/backend/backend.cpp +++ b/reactos/tools/rbuild/backend/backend.cpp @@ -4,6 +4,35 @@ #include "../Rbuild.h" #include "backend.h" +#include "mingw/mingw.h" + +using std::string; +using std::vector; + +vector Backend::factories; + +/*static*/ void +Backend::InitFactories() +{ + factories.push_back ( new Factory ( "mingw", MingwBackend::Factory ) ); +} + +/*static*/ Backend* +Backend::Create ( const std::string& name, Project& project ) +{ + string sname ( name ); + strlwr ( &sname[0] ); + if ( !factories.size() ) + throw Exception ( "internal tool error: no registered factories" ); + for ( size_t i = 0; i < factories.size(); i++ ) + { + if ( sname == factories[i]->name ) + return (factories[i]->factory) ( project ); + } + throw UnknownBackendException ( sname ); + return NULL; +} + Backend::Backend ( Project& project ) : ProjectNode ( project ) { diff --git a/reactos/tools/rbuild/backend/backend.h b/reactos/tools/rbuild/backend/backend.h index 32d4b206b97..5ed778fb0df 100644 --- a/reactos/tools/rbuild/backend/backend.h +++ b/reactos/tools/rbuild/backend/backend.h @@ -3,11 +3,36 @@ #include "../rbuild.h" +class Backend; + +typedef Backend* BackendFactory ( Project& project ); + class Backend { + class Factory + { + public: + std::string name; + BackendFactory* factory; + + Factory ( const std::string& name_, BackendFactory* factory_ ) + : name(name_), factory(factory_) + { + } + }; + + static std::vector factories; + public: + static void InitFactories(); + static Backend* Create ( const std::string& name, Project& project ); + +protected: Backend ( Project& project ); + +public: virtual void Process () = 0; + protected: Project& ProjectNode; }; diff --git a/reactos/tools/rbuild/backend/mingw/mingw.cpp b/reactos/tools/rbuild/backend/mingw/mingw.cpp index 56b43f08057..0b1a935a4f0 100644 --- a/reactos/tools/rbuild/backend/mingw/mingw.cpp +++ b/reactos/tools/rbuild/backend/mingw/mingw.cpp @@ -6,10 +6,17 @@ using std::string; using std::vector; +Backend* +MingwBackend::Factory ( Project& project ) +{ + return new MingwBackend ( project ); +} + #ifdef WIN32 #define EXEPOSTFIX ".exe" #define SEP "\\" -string FixSep ( const string& s ) +string +FixSep ( const string& s ) { string s2(s); char* p = strchr ( &s2[0], '/' ); @@ -23,7 +30,8 @@ string FixSep ( const string& s ) #else #define EXEPOSTFIX #define SEP "/" -string FixSep ( const string& s ) +string +FixSep ( const string& s ) { string s2(s); char* p = strchr ( &s2[0], '\\' ); @@ -41,7 +49,8 @@ MingwBackend::MingwBackend ( Project& project ) { } -void MingwBackend::Process () +void +MingwBackend::Process () { CreateMakefile (); GenerateHeader (); @@ -54,25 +63,29 @@ void MingwBackend::Process () CloseMakefile (); } -void MingwBackend::CreateMakefile () +void +MingwBackend::CreateMakefile () { fMakefile = fopen ( ProjectNode.makefile.c_str (), "w" ); if ( !fMakefile ) throw AccessDeniedException ( ProjectNode.makefile ); } -void MingwBackend::CloseMakefile () +void +MingwBackend::CloseMakefile () { if (fMakefile) fclose ( fMakefile ); } -void MingwBackend::GenerateHeader () +void +MingwBackend::GenerateHeader () { fprintf ( fMakefile, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT 'ReactOS.xml' INSTEAD\n\n" ); } -void MingwBackend::GenerateAllTarget () +void +MingwBackend::GenerateAllTarget () { fprintf ( fMakefile, "all: " ); for ( size_t i = 0; i < ProjectNode.modules.size (); i++ ) @@ -86,7 +99,8 @@ void MingwBackend::GenerateAllTarget () fprintf ( fMakefile, "\n\n" ); } -void MingwBackend::ProcessModule ( Module& module ) +void +MingwBackend::ProcessModule ( Module& module ) { MingwModuleHandlerList moduleHandlers; GetModuleHandlers ( moduleHandlers ); @@ -101,7 +115,8 @@ void MingwBackend::ProcessModule ( Module& module ) } } -void MingwBackend::GetModuleHandlers ( MingwModuleHandlerList& moduleHandlers ) +void +MingwBackend::GetModuleHandlers ( MingwModuleHandlerList& moduleHandlers ) { moduleHandlers.push_back ( new MingwKernelModuleHandler ( fMakefile ) ); } diff --git a/reactos/tools/rbuild/backend/mingw/mingw.h b/reactos/tools/rbuild/backend/mingw/mingw.h index ebdf7fbe086..94cf4aa7b3d 100644 --- a/reactos/tools/rbuild/backend/mingw/mingw.h +++ b/reactos/tools/rbuild/backend/mingw/mingw.h @@ -20,7 +20,10 @@ public: class MingwBackend : public Backend { public: + static Backend* Factory ( Project& project ); +protected: MingwBackend ( Project& project ); +public: virtual void Process (); private: void ProcessModule ( Module& module ); diff --git a/reactos/tools/rbuild/backend/mingw/modulehandler.cpp b/reactos/tools/rbuild/backend/mingw/modulehandler.cpp index f2653ca752e..23f8205c3f6 100644 --- a/reactos/tools/rbuild/backend/mingw/modulehandler.cpp +++ b/reactos/tools/rbuild/backend/mingw/modulehandler.cpp @@ -12,15 +12,17 @@ MingwModuleHandler::MingwModuleHandler ( FILE* fMakefile ) { } -string MingwModuleHandler::GetModuleDependencies ( Module& module ) +string +MingwModuleHandler::GetModuleDependencies ( Module& module ) { - string dependencies ( "" ); - - for ( size_t i = 0; i < module.libraries.size(); i++ ) + if ( !module.libraries.size() ) + return ""; + + string dependencies ( module.libraries[0]->name ); + + for ( size_t i = 1; i < module.libraries.size(); i++ ) { - if (dependencies.size () > 0) - dependencies += " "; - dependencies += module.libraries[i]->name; + dependencies += " " + module.libraries[i]->name; } return dependencies; } @@ -31,17 +33,20 @@ MingwKernelModuleHandler::MingwKernelModuleHandler ( FILE* fMakefile ) { } -bool MingwKernelModuleHandler::CanHandleModule ( Module& module ) +bool +MingwKernelModuleHandler::CanHandleModule ( Module& module ) { return true; } -void MingwKernelModuleHandler::Process ( Module& module ) +void +MingwKernelModuleHandler::Process ( Module& module ) { GenerateKernelModuleTarget ( module ); } -void MingwKernelModuleHandler::GenerateKernelModuleTarget ( Module& module ) +void +MingwKernelModuleHandler::GenerateKernelModuleTarget ( Module& module ) { fprintf ( fMakefile, "%s: %s", module.name.c_str (), diff --git a/reactos/tools/rbuild/exception.cpp b/reactos/tools/rbuild/exception.cpp index 3027b4274bb..db950640c6a 100644 --- a/reactos/tools/rbuild/exception.cpp +++ b/reactos/tools/rbuild/exception.cpp @@ -61,7 +61,7 @@ InvalidBuildFileException::InvalidBuildFileException() } -XMLSyntaxErrorException::XMLSyntaxErrorException ( const std::string& location, +XMLSyntaxErrorException::XMLSyntaxErrorException ( const string& location, const char* message, ... ) { @@ -72,19 +72,30 @@ XMLSyntaxErrorException::XMLSyntaxErrorException ( const std::string& location, } -RequiredAttributeNotFoundException::RequiredAttributeNotFoundException(const std::string& attributeName, - const std::string& elementName) +RequiredAttributeNotFoundException::RequiredAttributeNotFoundException(const string& attributeName, + const string& elementName) : InvalidBuildFileException ( "Required attribute '%s' not found on '%s'.", attributeName.c_str (), elementName.c_str ()) { } -InvalidAttributeValueException::InvalidAttributeValueException(const std::string& name, - const std::string& value) +InvalidAttributeValueException::InvalidAttributeValueException(const string& name, + const string& value) : InvalidBuildFileException ( "Attribute '%s' has an invalid value '%s'.", name.c_str (), value.c_str ()) { } + +BackendNameConflictException::BackendNameConflictException ( const string& name ) + : Exception ( "Backend name conflict: '%s'", name.c_str() ) +{ +} + + +UnknownBackendException::UnknownBackendException ( const string& name ) + : Exception ( "Unknown Backend requested: '%s'", name.c_str() ) +{ +} diff --git a/reactos/tools/rbuild/exception.h b/reactos/tools/rbuild/exception.h index 3e2207f3cbc..6746aed5648 100644 --- a/reactos/tools/rbuild/exception.h +++ b/reactos/tools/rbuild/exception.h @@ -67,4 +67,18 @@ public: const std::string& value); }; + +class BackendNameConflictException : public Exception +{ +public: + BackendNameConflictException ( const std::string& name ); +}; + + +class UnknownBackendException : public Exception +{ +public: + UnknownBackendException ( const std::string& name ); +}; + #endif /* __EXCEPTION_H */ diff --git a/reactos/tools/rbuild/module.cpp b/reactos/tools/rbuild/module.cpp index 30734645eec..7b6018a822a 100644 --- a/reactos/tools/rbuild/module.cpp +++ b/reactos/tools/rbuild/module.cpp @@ -27,8 +27,9 @@ Module::~Module () delete libraries[i]; } -void Module::ProcessXML ( const XMLElement& e, - const string& path ) +void +Module::ProcessXML ( const XMLElement& e, + const string& path ) { string subpath ( path ); if ( e.name == "file" && e.value.size () ) @@ -49,7 +50,8 @@ void Module::ProcessXML ( const XMLElement& e, ProcessXML ( *e.subElements[i], subpath ); } -ModuleType Module::GetModuleType ( const XMLAttribute& attribute ) +ModuleType +Module::GetModuleType ( const XMLAttribute& attribute ) { if ( attribute.value == "buildtool" ) return BuildTool; diff --git a/reactos/tools/rbuild/project.cpp b/reactos/tools/rbuild/project.cpp index e79bcf3cd5b..c561ef30967 100644 --- a/reactos/tools/rbuild/project.cpp +++ b/reactos/tools/rbuild/project.cpp @@ -25,7 +25,8 @@ Project::~Project () delete head; } -void Project::ReadXml () +void +Project::ReadXml () { Path path; diff --git a/reactos/tools/rbuild/rbuild.cpp b/reactos/tools/rbuild/rbuild.cpp index 2999e957849..fcc1de5bffb 100644 --- a/reactos/tools/rbuild/rbuild.cpp +++ b/reactos/tools/rbuild/rbuild.cpp @@ -17,11 +17,19 @@ using std::vector; int main ( int argc, char** argv ) { + Backend::InitFactories(); + if ( argc != 2 ) + { + printf ( "syntax: rbuild {buildtarget}\n" ); + return 1; + } + string buildtarget ( argv[1] ); + strlwr ( &buildtarget[0] ); try { string projectFilename ( "ReactOS.xml" ); Project project ( projectFilename ); - Backend* backend = new MingwBackend ( project ); + Backend* backend = Backend::Create ( buildtarget, project ); backend->Process (); delete backend; diff --git a/reactos/tools/rbuild/ssprintf.cpp b/reactos/tools/rbuild/ssprintf.cpp index 468a5ae14f4..dbd2bc2a34a 100644 --- a/reactos/tools/rbuild/ssprintf.cpp +++ b/reactos/tools/rbuild/ssprintf.cpp @@ -34,7 +34,8 @@ typedef struct { unsigned int empty:16; } ieee_long_double_t; -std::string ssprintf ( const char* fmt, ... ) +std::string +ssprintf ( const char* fmt, ... ) { va_list arg; va_start(arg, fmt); @@ -43,7 +44,8 @@ std::string ssprintf ( const char* fmt, ... ) return f; } -std::wstring sswprintf ( const wchar_t* fmt, ... ) +std::wstring +sswprintf ( const wchar_t* fmt, ... ) { va_list arg; va_start(arg, fmt); @@ -62,7 +64,8 @@ std::wstring sswprintf ( const wchar_t* fmt, ... ) #define ZEROTRUNC 128 /* truncate zero 's */ -static int skip_atoi(const char **s) +static int +skip_atoi(const char **s) { int i=0; @@ -71,7 +74,8 @@ static int skip_atoi(const char **s) return i; } -static int skip_wtoi(const wchar_t **s) +static int +skip_wtoi(const wchar_t **s) { int i=0; @@ -81,7 +85,8 @@ static int skip_wtoi(const wchar_t **s) } -static int do_div(LONGLONG *n,int base) +static int +do_div(LONGLONG *n,int base) { int __res = ((ULONGLONG) *n) % (unsigned) base; *n = ((ULONGLONG) *n) / (unsigned) base; @@ -89,7 +94,8 @@ static int do_div(LONGLONG *n,int base) } -static bool number(std::string& f, LONGLONG num, int base, int size, int precision ,int type) +static bool +number(std::string& f, LONGLONG num, int base, int size, int precision ,int type) { char c,sign,tmp[66]; const char *digits="0123456789abcdefghijklmnopqrstuvwxyz"; @@ -165,7 +171,8 @@ static bool number(std::string& f, LONGLONG num, int base, int size, int precisi return true; } -static bool wnumber(std::wstring& f, LONGLONG num, int base, int size, int precision ,int type) +static bool +wnumber(std::wstring& f, LONGLONG num, int base, int size, int precision ,int type) { wchar_t c,sign,tmp[66]; const wchar_t *digits = L"0123456789abcdefghijklmnopqrstuvwxyz"; @@ -242,7 +249,8 @@ static bool wnumber(std::wstring& f, LONGLONG num, int base, int size, int preci } -static bool numberf(std::string& f, double __n, char exp_sign, int size, int precision, int type) +static bool +numberf(std::string& f, double __n, char exp_sign, int size, int precision, int type) { double exponent = 0.0; double e; @@ -435,7 +443,8 @@ static bool numberf(std::string& f, double __n, char exp_sign, int size, int pr return true; } -static bool wnumberf(std::wstring& f, double __n, wchar_t exp_sign, int size, int precision, int type) +static bool +wnumberf(std::wstring& f, double __n, wchar_t exp_sign, int size, int precision, int type) { double exponent = 0.0; double e; @@ -628,7 +637,8 @@ static bool wnumberf(std::wstring& f, double __n, wchar_t exp_sign, int size, i return true; } -static bool numberfl(std::string& f, long double __n, char exp_sign, int size, int precision, int type) +static bool +numberfl(std::string& f, long double __n, char exp_sign, int size, int precision, int type) { long double exponent = 0.0; long double e; @@ -834,7 +844,8 @@ static bool numberfl(std::string& f, long double __n, char exp_sign, int size, return true; } -static bool wnumberfl(std::wstring& f, long double __n, wchar_t exp_sign, int size, int precision, int type) +static bool +wnumberfl(std::wstring& f, long double __n, wchar_t exp_sign, int size, int precision, int type) { long double exponent = 0.0; long double e; @@ -1035,7 +1046,8 @@ static bool wnumberfl(std::wstring& f, long double __n, wchar_t exp_sign, int s return true; } -static int do_string(std::string& f, const char* s, int len, int field_width, int precision, int flags) +static int +do_string(std::string& f, const char* s, int len, int field_width, int precision, int flags) { int i, done = 0; if (s == NULL) @@ -1076,7 +1088,8 @@ static int do_string(std::string& f, const char* s, int len, int field_width, in return done; } -static int do_wstring(std::wstring& f, const wchar_t* s, int len, int field_width, int precision, int flags) +static int +do_wstring(std::wstring& f, const wchar_t* s, int len, int field_width, int precision, int flags) { int i, done = 0; if (s == NULL) @@ -1117,7 +1130,8 @@ static int do_wstring(std::wstring& f, const wchar_t* s, int len, int field_widt return done; } -static int stringw(std::string& f, const wchar_t* sw, int len, int field_width, int precision, int flags) +static int +stringw(std::string& f, const wchar_t* sw, int len, int field_width, int precision, int flags) { int i, done = 0; if (sw == NULL) @@ -1169,7 +1183,8 @@ static int stringw(std::string& f, const wchar_t* sw, int len, int field_width, return done; } -static int wstringa(std::wstring& f, const char* sa, int len, int field_width, int precision, int flags) +static int +wstringa(std::wstring& f, const char* sa, int len, int field_width, int precision, int flags) { int i, done = 0; if (sa == NULL) @@ -1219,7 +1234,8 @@ static int wstringa(std::wstring& f, const char* sa, int len, int field_width, i #define _isnanl _isnan #define _finitel _finite -std::string ssvprintf ( const char *fmt, va_list args ) +std::string +ssvprintf ( const char *fmt, va_list args ) { ULONGLONG num; int base; @@ -1569,7 +1585,8 @@ std::string ssvprintf ( const char *fmt, va_list args ) return f; } -std::wstring sswvprintf ( const wchar_t* fmt, va_list args ) +std::wstring +sswvprintf ( const wchar_t* fmt, va_list args ) { ULONGLONG num; int base; -- 2.17.1