From: Mikko Rasa Date: Sun, 14 Apr 2013 11:50:52 +0000 (+0300) Subject: Support conditionals inside components X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5ed38947b3fabd977a7f68b512115fe1456ea096;p=builder.git Support conditionals inside components --- diff --git a/source/architecture.cpp b/source/architecture.cpp index fd31bcd..7faefeb 100644 --- a/source/architecture.cpp +++ b/source/architecture.cpp @@ -124,16 +124,17 @@ Architecture::Architecture(Builder &b, const string &spec): bool Architecture::match_name(const string &pattern) const { - vector parts = split(pattern, "-"); + bool negate = (pattern[0]=='!'); + vector parts = split(pattern.substr(negate), "-"); for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) { string part = resolve_alias(*i); if((part=="32" && bits==32) || (part=="64" && bits==64)) ; else if(part!=type && part!=cpu && part!=system) - return false; + return negate; } - return true; + return !negate; } string Architecture::resolve_alias(const string &part) const diff --git a/source/component.cpp b/source/component.cpp index d1849f4..5104cdf 100644 --- a/source/component.cpp +++ b/source/component.cpp @@ -243,6 +243,8 @@ Component::SourceList Component::collect_source_files() const Component::Loader::Loader(Component &c): DataFile::ObjectLoader(c) { + add("if_arch", &Loader::if_arch); + add("if_feature", &Loader::if_feature); add("source", &Loader::source); add("install", &Component::install); add("install_map", &Loader::install_map); @@ -252,6 +254,24 @@ Component::Loader::Loader(Component &c): add("use", &Loader::use); } +void Component::Loader::if_arch(const string &cond) +{ + bool match = obj.package.get_builder().get_current_arch().match_name(cond); + obj.package.get_builder().get_logger().log("configure", + format("%s/%s: arch %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not "))); + if(match) + load_sub_with(*this); +} + +void Component::Loader::if_feature(const string &cond) +{ + bool match = obj.package.match_feature(cond); + obj.package.get_builder().get_logger().log("configure", + format("%s/%s: feature %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not "))); + if(match) + load_sub_with(*this); +} + void Component::Loader::source(const string &s) { obj.sources.push_back((obj.package.get_source_directory()/s).str()); diff --git a/source/component.h b/source/component.h index 2e9e534..83a8baa 100644 --- a/source/component.h +++ b/source/component.h @@ -24,6 +24,8 @@ public: public: Loader(Component &); private: + void if_arch(const std::string &); + void if_feature(const std::string &); void source(const std::string &); void require(const std::string &); void build_info(); diff --git a/source/package.cpp b/source/package.cpp index 6cbc932..05bebcc 100644 --- a/source/package.cpp +++ b/source/package.cpp @@ -38,9 +38,7 @@ Package::Loader::Loader(Package &p): void Package::Loader::if_arch(const string &cond) { - const Architecture &arch = obj.builder.get_current_arch(); - bool negate = (cond[0]=='!'); - bool match = (arch.match_name(cond.substr(negate))!=negate); + bool match = obj.builder.get_current_arch().match_name(cond); obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not "))); if(match) load_sub_with(*this); diff --git a/source/sourcepackage.cpp b/source/sourcepackage.cpp index 8ad12d3..cb4f978 100644 --- a/source/sourcepackage.cpp +++ b/source/sourcepackage.cpp @@ -67,6 +67,27 @@ FS::Path SourcePackage::get_out_dir() const return source_dir/arch.get_name(); } +bool SourcePackage::match_feature(const string &cond) const +{ + string::size_type equals = cond.find('='); + if(equals!=string::npos) + { + if(equals==0) + throw invalid_argument("SourcePackage::match_feature"); + bool negate = cond[equals-1]=='!'; + string feat = cond.substr(0, equals-negate); + string value = config.get_option("with_"+feat).value; + return (value==cond.substr(equals+1))!=negate; + } + else + { + bool negate = (cond[0]=='!'); + string feat = cond.substr(negate); + string value = config.get_option("with_"+feat).value; + return lexical_cast(value)!=negate; + } +} + void SourcePackage::do_prepare() { BuildInfo final_build_info; @@ -231,25 +252,7 @@ void SourcePackage::Loader::headers(const string &n) void SourcePackage::Loader::if_feature(const string &cond) { - bool match = false; - string::size_type equals = cond.find('='); - if(equals!=string::npos) - { - if(equals==0) - throw invalid_argument("SourcePackage::Loader::if_feature"); - bool negate = cond[equals-1]=='!'; - string name = cond.substr(0, equals-negate); - string value = obj.config.get_option("with_"+name).value; - match = (value==cond.substr(equals+1))!=negate; - value = cond.substr(equals+1); - } - else - { - bool negate = (cond[0]=='!'); - string name = cond.substr(negate); - string value = obj.config.get_option("with_"+name).value; - match = lexical_cast(value)!=negate; - } + bool match = obj.match_feature(cond); obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not "))); if(match) load_sub_with(*this); diff --git a/source/sourcepackage.h b/source/sourcepackage.h index f0a58b7..0254cb9 100644 --- a/source/sourcepackage.h +++ b/source/sourcepackage.h @@ -85,6 +85,7 @@ public: Msp::FS::Path get_out_dir() const; const ComponentList &get_components() const { return components; } const Config &get_config() const { return config; } + bool match_feature(const std::string &) const; void set_build_type(const BuildType &); const BuildInfo &get_build_info() const { return build_info; } Builder &get_builder() const { return builder; }