diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-05-31 16:32:40 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2021-06-08 15:43:08 +0200 |
commit | 0baeb5209d3a111a53070c032d7cdb1e609e3516 (patch) | |
tree | 61e34e8998b5724274aa2c477608d9fc67b39c1a /libbuild2/adhoc-rule-regex-pattern.hxx | |
parent | 1346f4cd0d20a5dc7e0471edbbb6ce00f2da5c18 (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/adhoc-rule-regex-pattern.hxx')
-rw-r--r-- | libbuild2/adhoc-rule-regex-pattern.hxx | 58 |
1 files changed, 58 insertions, 0 deletions
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 <libbuild2/types.hxx> +#include <libbuild2/forward.hxx> +#include <libbuild2/utility.hxx> + +#include <libbuild2/rule.hxx> + +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<element> targets_; // First is the primary target. + vector<element> prereqs_; + }; +} + +#endif // LIBBUILD2_ADHOC_RULE_REGEX_PATTERN_HXX |