From 0baeb5209d3a111a53070c032d7cdb1e609e3516 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/adhoc-rule-regex-pattern.hxx | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libbuild2/adhoc-rule-regex-pattern.hxx (limited to 'libbuild2/adhoc-rule-regex-pattern.hxx') diff --git a/libbuild2/adhoc-rule-regex-pattern.hxx b/libbuild2/adhoc-rule-regex-pattern.hxx new file mode 100644 index 0000000..ed8bf59 --- /dev/null +++ b/libbuild2/adhoc-rule-regex-pattern.hxx @@ -0,0 +1,58 @@ +// file : libbuild2/adhoc-rule-regex-pattern.hxx -*- C++ -*- +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBUILD2_ADHOC_RULE_REGEX_PATTERN_HXX +#define LIBBUILD2_ADHOC_RULE_REGEX_PATTERN_HXX + +#include +#include +#include + +#include + +namespace build2 +{ + // Ad hoc rule regex pattern. + // + // Note: exported since may be accessed by ad hoc recipe implementation. + // + class LIBBUILD2_SYMEXPORT adhoc_rule_regex_pattern: public adhoc_rule_pattern + { + public: + using name = build2::name; + using scope = build2::scope; + + adhoc_rule_regex_pattern (const scope&, string, const target_type&, + name&&, const location&, + names&&, const location&, + names&&, const location&); + + virtual bool + match (action, target&, const string&, match_extra&) const override; + + virtual void + apply_adhoc_members (action, target&, match_extra&) const override; + + virtual void + apply_prerequisites (action, target&, match_extra&) const override; + + virtual void + dump (ostream&) const override; + + private: + string text_; // Pattern text. + regex regex_; // Pattern regex. + + struct element + { + build2::name name; + const target_type& type; + bool match_ext; // Match extension flag. + }; + + vector targets_; // First is the primary target. + vector prereqs_; + }; +} + +#endif // LIBBUILD2_ADHOC_RULE_REGEX_PATTERN_HXX -- cgit v1.1