如何将工具添加到 Bazel genrule 中的 PATH 环境变量

问题描述 投票:0回答:1

鉴于下面的 Bazel

BUILD
文件,我怎样才能获得
compiler_files
文件组顶层的绝对路径?我尝试过使用
$(location //X:Y)
及其变体,但我只是得到了大量文件列表及其相对于沙箱的路径。使用
tools
属性中提供的工具路径更新 genrule 中的 PATH 环境变量的惯用方法是什么?

package(default_visibility = ['//visibility:public'])

filegroup(
    name = "script",
    srcs = ["src/inspect.sh"],
)

genrule(
    name = "inspect",
    srcs = [":script"],
    cmd = """
# Instead of /tmp below I need to refer to the path of one of the tools
# export PATH=/tmp:$$PATH
source $(location :script)
cp output.txt $(RULEDIR)/
""",
    tools = [
        "@emsdk//emscripten_toolchain:compiler_files"
    ],
    outs = ["generated.txt"]
)
bazel
1个回答
0
投票

一个常见的技巧是直接依赖于要添加到

$PATH
的目录中的文件,然后在
genrule
脚本中引用其目录名称。例如,添加
emcc.sh
的位置:

genrule(
    name = "inspect",
    srcs = [":script"],
    cmd = """
# export PATH=$$(dirname $(location @emsdk//emscripten_toolchain:emcc.sh):$$PATH
source $(location :script)
cp output.txt $(RULEDIR)/
""",
    tools = [
        "@emsdk//emscripten_toolchain:emcc.sh",
        "@emsdk//emscripten_toolchain:compiler_files",
    ],
    outs = ["generated.txt"]
)
© www.soinside.com 2019 - 2024. All rights reserved.