BobBuildTool/bob

Tool environment not used to evaluate depends conditions

rhubert opened this issue · 3 comments

ATM it's not possible to use Variables provided by tools to evaluate conditions in the depends section of a recipe, e.g.:

root1.yaml

inherit: [basement::rootrecipe]

depends:
 - name: devel::cross-toolchain-x86_64-linux-gnu
   use: [tools, environment]
   forward: True
 - foo

root2.yaml

inherit: [basement::rootrecipe]

depends:
 - name: devel::cross-toolchain-aarch64-linux-gnu
   use: [tools, environment]
   forward: True
 - foo

foo.yaml

depends:
  - if: !expr |
       "${ARCH}" == "x86_64"
    name: bar

Is there any reason for this?

As a quick hack I applied the tools env before evaluating the depends:

diff --git a/pym/bob/input.py b/pym/bob/input.py
index e376a5e4..18c86dc1 100644
--- a/pym/bob/input.py
+++ b/pym/bob/input.py
@@ -2291,6 +2291,19 @@ class Recipe(object):
         depDiffTools = diffTools.copy()
         thisDeps = {}

+        # apply tool environments
+        toolsEnv = set()
+        toolsView = tools.inspect()
+        for i in self.__toolDepPackage:
+            tool = toolsView.get(i)
+            if tool is None: continue
+            if not tool.environment: continue
+            tmp = set(tool.environment.keys())
+            if not tmp.isdisjoint(toolsEnv):
+                self.__raiseIncompatibleTools(toolsView)
+            toolsEnv.update(tmp)
+            env.update(tool.environment)
+
         for dep in self.__deps:
             env.setFunArgs({ "recipe" : self, "sandbox" : bool(sandbox) and sandboxEnabled,
                 "__tools" : tools })

which seams to solve the issue but with unknown side impact. Maybe the update of the env should also be done after each dependency that has been processed to get the env from this as well?

It's currently not possible to test for environment variables provided by tools. These are handled differently and are only made available in recipes that use the respective tool. These variables are merged after the dependency list was iterated. See https://bob-build-tool.readthedocs.io/en/latest/manual/configuration.html#environment-handling. The intention was to treat these variables like privateEnvironment so that they are purely private to the package. Your patch would violate this and also fail for tools that are picked up from dependencies instead of inherited tools.

A possible solution could be that we define something like a get-tool-env string function to query variables of tools, e.g. $(get-tool-env,target-toolchain,ARCH). That would have the benefit of being able to query variables of tools that are not used in the recipe. Unfortunately it's not possible to provide such a function by a plug-in currently because plugins don't have access to tools.

Please see the linked PR. Something like the following should work:

depends:
  - if: !expr |
       get-tool-env("target-toolchain", "ARCH") == "x86_64"
    name: bar

👍 Thanks. I just did a few tests and it works as expected.