From 23a8204d60a7f189fa4659f51b828599fc5838a3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 31 May 2021 16:32:40 +0200 Subject: Implement ad hoc regex pattern rule support An ad hoc pattern rule consists of a pattern that mimics a dependency declaration followed by one or more recipes. For example: exe{~'/(.*)/'}: cxx{~'/\1/'} {{ $cxx.path -o $path($>) $path($<[0]) }} If a pattern matches a dependency declaration of a target, then the recipe is used to perform the corresponding operation on this target. For example, the following dependency declaration matches the above pattern which means the rule's recipe will be used to update this target: exe{hello}: cxx{hello} While the following declarations do not match the above pattern: exe{hello}: c{hello} # Type mismatch. exe{hello}: cxx{howdy} # Name mismatch. On the left hand side of `:` in the pattern we can have a single target or an ad hoc target group. The single target or the first (primary) ad hoc group member must be a regex pattern (~). The rest of the ad hoc group members can be patterns or substitutions (^). For example: : cxx{~'/\1/'} {{ $cxx.path -o $path($>[0]) "-Wl,-Map=$path($>[1])" $path($<[0]) }} On the left hand side of `:` in the pattern we have prerequisites which can be patterns, substitutions, or non-patterns. For example: : cxx{~'/\1/'} hxx{^'/\1/'} hxx{common} {{ $cxx.path -o $path($>[0]) "-Wl,-Map=$path($>[1])" $path($<[0]) }} Substitutions on the left hand side of `:` and substitutions and non-patterns on the right hand side are added to the dependency declaration. For example, given the above rule and dependency declaration, the effective dependency is going to be: : cxx{hello} hxx{hello} hxx{common} --- libbuild2/rule.hxx | 94 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 9 deletions(-) (limited to 'libbuild2/rule.hxx') diff --git a/libbuild2/rule.hxx b/libbuild2/rule.hxx index af89124..67c0f6d 100644 --- a/libbuild2/rule.hxx +++ b/libbuild2/rule.hxx @@ -143,18 +143,32 @@ namespace build2 // Ad hoc rule. // + // Used for both ad hoc pattern rules and ad hoc recipes. For recipes, it's + // essentially a rule of one case. Note that when used as part of a pattern, + // the implementation cannot use the match_extra::buffer nor the target + // auxilary data storage. + // // Note: not exported. // + class adhoc_rule_pattern; + class adhoc_rule: public rule { public: - location_value loc; // Buildfile location of the recipe. - size_t braces; // Number of braces in multi-brace tokens. + location_value loc; // Buildfile location of the recipe. + size_t braces; // Number of braces in multi-brace tokens. + small_vector actions; // Actions this rule is for. - adhoc_rule (const char* name, const location& l, size_t b) + // If not NULL then this rule (recipe, really) belongs to an ad hoc + // pattern rule and match() should call the pattern's match() and + // apply() should call the pattern's apply_*() functions (see below). + // + const adhoc_rule_pattern* pattern = nullptr; + + adhoc_rule (string name, const location& l, size_t b) : loc (l), braces (b), - rule_match (name, static_cast (*this)) {} + rule_match (move (name), static_cast (*this)) {} // Set the rule text, handle any recipe-specific attributes, and return // true if the recipe builds anything in the build/recipes/ directory and @@ -164,8 +178,11 @@ namespace build2 // the scope of the recipe (not necessarily the same as the target's base // scope). // + // Note that this function is called after the actions member has been + // populated. + // virtual bool - recipe_text (context&, const scope&, const target*, const adhoc_actions&, + recipe_text (context&, const scope&, const target*, string&&, attributes&) = 0; public: @@ -174,7 +191,7 @@ namespace build2 // to provide a fallback implementation of a reverse operation if it is // providing the other half. // - virtual optional + virtual bool reverse_fallback (action, const target_type&) const; // The default implementation forwards to the pattern's match() if there @@ -195,9 +212,8 @@ namespace build2 // public: // The name in rule_match is used as a hint and as a name in diagnostics. - // The former does not apply to us (but will apply to ad hoc rules) while - // latter does. As a result, we use special-looking "" - // names. + // The former does not apply to ad hoc recipes (but does apply to ad hoc + // rules). // const build2::rule_match rule_match; @@ -224,6 +240,66 @@ namespace build2 virtual recipe apply (action, target&, match_extra&, const optional&) const = 0; }; + + // Ad hoc rule pattern. + // + // Note: exported since may be accessed by ad hoc recipe implementation. + // + class LIBBUILD2_SYMEXPORT adhoc_rule_pattern + { + public: + const scope& rule_scope; + const string rule_name; + const target_type& type; // Primary target type. + small_vector, 1> rules; // Really a unique_ptr. + + adhoc_rule_pattern (const scope& s, string n, const target_type& t) + : rule_scope (s), + rule_name (move (n)), + type (t), + fallback_rule_ (rules) {} + + virtual + ~adhoc_rule_pattern (); + + public: + virtual bool + match (action, target&, const string&, match_extra&) const = 0; + + virtual void + apply_adhoc_members (action, target&, match_extra&) const = 0; + + virtual void + apply_prerequisites (action, target&, match_extra&) const = 0; + + // Dump support. + // + virtual void + dump (ostream&) const = 0; + + // Gory implementation details (see match_impl()). + // + public: + class fallback_rule: public rule + { + public: + const small_vector, 1>& rules; + + explicit + fallback_rule (const small_vector, 1>& rs) + : rules (rs) {} + + // Dummy (never called). + // + virtual bool + match (action, target&, const string&, match_extra&) const override; + + virtual recipe + apply (action, target&, match_extra&) const override; + }; + + fallback_rule fallback_rule_; + }; } #endif // LIBBUILD2_RULE_HXX -- cgit v1.1