diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-11-15 17:13:38 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-11-16 15:09:05 +0100 |
commit | c3b10ee7c9320ed6cf5daec6310b0296812ea86a (patch) | |
tree | 1ea7b015ea9cc2a30fb896f689b4ea5da0e41cc0 | |
parent | 7027ce395a7aa835dd261fce0a554265c9753006 (diff) | |
download | justbuild-c3b10ee7c9320ed6cf5daec6310b0296812ea86a.tar.gz |
Make "config" accept a computed target
... and thus allowing the "business logic" in the configuration
target (e.g., setting defaults and derived options) to be shared
by many targets.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | doc/concepts/built-in-rules.org | 9 | ||||
-rw-r--r-- | src/buildtool/build_engine/target_map/built_in_rules.cpp | 11 | ||||
-rw-r--r-- | test/end-to-end/targets/TARGETS | 7 | ||||
-rw-r--r-- | test/end-to-end/targets/configure-target.sh | 99 |
5 files changed, 122 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d50058f2..9b5b9846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Initial stable release. ### Important changes since `1.0.0~beta5` +- The "configure" built-in rule now evaluates "target" - Option `--dump-vars` added to `just analyse` - Rule fixes in propagating `ENV` - Launcher functionality added to `just-mr` diff --git a/doc/concepts/built-in-rules.org b/doc/concepts/built-in-rules.org index 0da0297f..28784193 100644 --- a/doc/concepts/built-in-rules.org +++ b/doc/concepts/built-in-rules.org @@ -125,10 +125,11 @@ map is empty. ** ~"configure"~ -The ~"configure"~ rule allows to configure a target with a given configuration. -Similar to the ~"export"~ rule, the field ~"target"~ must name a single target -(not a list). The ~"config"~ field is evaluated and must result in a map, which -is used as configuration for the given target. +The ~"configure"~ rule allows to configure a target with a given +configuration. The field ~"target"~ is evaluated and the result +of the evaluation must name a single target (not a list). The +~"config"~ field is evaluated and must result in a map, which is +used as configuration for the given target. This rule uses the given configuration to overlay the current environment for evaluating the given target, and thereby performs a configuration transition. It diff --git a/src/buildtool/build_engine/target_map/built_in_rules.cpp b/src/buildtool/build_engine/target_map/built_in_rules.cpp index a34dfb03..763385e2 100644 --- a/src/buildtool/build_engine/target_map/built_in_rules.cpp +++ b/src/buildtool/build_engine/target_map/built_in_rules.cpp @@ -953,7 +953,16 @@ void ConfigureRule( } auto param_config = key.config.Prune(*param_vars); - auto configured_target_name = desc->ReadExpression("target"); + auto configured_target_name_exp = desc->ReadExpression("target"); + if (not configured_target_name_exp) { + return; + } + auto configured_target_name = configured_target_name_exp.Evaluate( + param_config, {}, [logger](std::string const& msg) { + (*logger)( + fmt::format("Evaluating 'target' failed with error:\n{}", msg), + true); + }); if (not configured_target_name) { return; } diff --git a/test/end-to-end/targets/TARGETS b/test/end-to-end/targets/TARGETS index 08b48284..31f4f900 100644 --- a/test/end-to-end/targets/TARGETS +++ b/test/end-to-end/targets/TARGETS @@ -22,6 +22,12 @@ , "test": ["glob.sh"] , "deps": [["test/end-to-end", "tool-under-test"]] } +, "configure target name": + { "type": ["@", "rules", "shell/test", "script"] + , "name": ["configure-target"] + , "test": ["configure-target.sh"] + , "deps": [["test/end-to-end", "tool-under-test"]] + } , "TESTS": { "type": "install" , "tainted": ["test"] @@ -30,6 +36,7 @@ , "repository naming" , "resolution of built-in rules" , "glob expansion" + , "configure target name" ] } } diff --git a/test/end-to-end/targets/configure-target.sh b/test/end-to-end/targets/configure-target.sh new file mode 100644 index 00000000..7c4dd9fd --- /dev/null +++ b/test/end-to-end/targets/configure-target.sh @@ -0,0 +1,99 @@ +#!/bin/sh +# Copyright 2022 Huawei Cloud Computing Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +TOOL=$(realpath ./bin/tool-under-test) +mkdir -p .root +BUILDROOT=$(realpath .root) +mkdir -p out +OUTDIR=$(realpath out) + +mkdir src +cd src +touch ROOT +cat > TARGETS <<'EOI' +{ "configured": + { "type": "configure" + , "arguments_config": ["TARGET", "FOO", "BAR"] + , "target": {"type": "var", "name": "TARGET"} + , "config": + { "type": "let*" + , "bindings": + [ ["FOO", {"type": "var", "name": "FOO", "default": "foo"}] + , ["BAR", {"type": "var", "name": "BAR", "default": "bar"}] + , [ "FOOBAR" + , { "type": "join" + , "$1": + [{"type": "var", "name": "FOO"}, {"type": "var", "name": "BAR"}] + } + ] + ] + , "body": {"type": "env", "vars": ["FOO", "BAR", "FOOBAR"]} + } + } +, "foobar": + { "type": "generic" + , "outs": ["out.txt"] + , "arguments_config": ["FOOBAR"] + , "cmds": + [ { "type": "join" + , "separator": " " + , "$1": + [ "echo" + , "-n" + , {"type": "join_cmd", "$1": [{"type": "var", "name": "FOOBAR"}]} + , "> out.txt" + ] + } + ] + } +, "bar": + { "type": "file_gen" + , "arguments_config": ["BAR"] + , "name": "out.txt" + , "data": {"type": "var", "name": "BAR"} + } +} +EOI +echo -n unrelated > out.txt + + +echo +${TOOL} install --local-build-root "${BUILDROOT}" -o "${OUTDIR}" \ + -D '{"TARGET": "foobar", "FOO": "_F_O_O_"}' configured 2>&1 +echo +cat "${OUTDIR}/out.txt" +echo +[ $(cat "${OUTDIR}/out.txt") = "_F_O_O_bar" ] + +echo +${TOOL} install --local-build-root "${BUILDROOT}" -o "${OUTDIR}" \ + -D '{"TARGET": "bar", "FOO": "_F_O_O_"}' configured 2>&1 +echo +cat "${OUTDIR}/out.txt" +echo +[ $(cat "${OUTDIR}/out.txt") = "bar" ] + +echo +${TOOL} install --local-build-root "${BUILDROOT}" -o "${OUTDIR}" \ + -D '{"TARGET": "out.txt", "FOO": "_F_O_O_"}' configured 2>&1 +echo +cat "${OUTDIR}/out.txt" +echo +[ $(cat "${OUTDIR}/out.txt") = "unrelated" ] + + +echo DONE |