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/scope.cxx | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'libbuild2/scope.cxx') diff --git a/libbuild2/scope.cxx b/libbuild2/scope.cxx index f2700c4..ad01aa7 100644 --- a/libbuild2/scope.cxx +++ b/libbuild2/scope.cxx @@ -3,6 +3,7 @@ #include +#include #include #include @@ -30,6 +31,18 @@ namespace build2 // scope // + scope:: + scope (context& c, bool global) + : ctx (c), vars (c, global), target_vars (c, global) + { + } + + scope:: + ~scope () + { + // Definition of adhoc_rule_pattern. + } + pair scope:: lookup_original (const variable& var, const target_key* tk, @@ -649,31 +662,34 @@ namespace build2 } pair> scope:: - find_target_type (name& n, const location& loc) const + find_target_type (name& n, const location& loc, const target_type* tt) const { - const target_type* tt (nullptr); optional ext; string& v (n.value); - // If the target type is specified, resolve it and bail out if not found. - // Otherwise, we know in the end it will resolve to something (if nothing - // else, either dir{} or file{}), so we can go ahead and process the name. + // If the name is typed, resolve the target type it and bail out if not + // found. Otherwise, we know in the end it will resolve to something (if + // nothing else, either dir{} or file{}), so we can go ahead and process + // the name. // - if (n.typed ()) + if (tt == nullptr) { - tt = find_target_type (n.type); + if (n.typed ()) + { + tt = find_target_type (n.type); - if (tt == nullptr) - return make_pair (tt, move (ext)); - } - else - { - // Empty name as well as '.' and '..' signify a directory. Note that - // this logic must be consistent with other places (grep for ".."). - // - if (v.empty () || v == "." || v == "..") - tt = &dir::static_type; + if (tt == nullptr) + return make_pair (tt, move (ext)); + } + else + { + // Empty name as well as '.' and '..' signify a directory. Note that + // this logic must be consistent with other places (grep for ".."). + // + if (v.empty () || v == "." || v == "..") + tt = &dir::static_type; + } } // Directories require special name processing. If we find that more -- cgit v1.1