aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/rule.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2021-05-31 16:32:40 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2021-06-08 15:43:08 +0200
commit0baeb5209d3a111a53070c032d7cdb1e609e3516 (patch)
tree61e34e8998b5724274aa2c477608d9fc67b39c1a /libbuild2/rule.hxx
parent1346f4cd0d20a5dc7e0471edbbb6ce00f2da5c18 (diff)
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: <exe{~'/(.*)/'} file{^'/\1.map/'}>: 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: <exe{~'/(.*)/'} file{^'/\1.map/'}>: 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: <exe{hello} file{hello.map>: cxx{hello} hxx{hello} hxx{common}
Diffstat (limited to 'libbuild2/rule.hxx')
-rw-r--r--libbuild2/rule.hxx94
1 files changed, 85 insertions, 9 deletions
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<action, 1> 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<const rule&> (*this)) {}
+ rule_match (move (name), static_cast<const rule&> (*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<action>
+ 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 "<ad hoc X recipe>"
- // 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<timestamp>&) 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<shared_ptr<adhoc_rule>, 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<shared_ptr<adhoc_rule>, 1>& rules;
+
+ explicit
+ fallback_rule (const small_vector<shared_ptr<adhoc_rule>, 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