如何解决如何在sh_binary中调用bash脚本?
我有两个shell脚本。一个(a)打印"Hello"
,而另一个(b)打印"World"
。我希望有一个同时调用两个脚本并给我"Hello World"
的Bazel目标(c)。
我的尝试:
mkdir BashScriptDemo
cd BashScriptDemo
touch WORKSPACE.bazel # empty WORKSPACE file
touch BUILD.bazel # empty BUILD file
mkdir my_package
cd my_package
echo '3.5.0' > .bazelversion # use Bazel 3.5.0
echo 'echo -n "Hello"' > a.sh # echo Hello
echo 'echo " World!"' > b.sh # echo World!
echo './a.sh\n./b.sh' > c.sh # script c that calls a and b
chmod +x a.sh b.sh c.sh
echo 'sh_binary(name="a",srcs=["a.sh"])' > BUILD.bazel
echo 'sh_binary(name="b",srcs=["b.sh"])' >> BUILD.bazel
echo 'sh_binary(name="c",srcs=["c.sh"],data=[":a",":b"])' >> BUILD.bazel
buildifier BUILD.bazel # optional in the case you have buildifier installed
bazel run //my_package:c # Bazel is an alias to bazelisk
尝试运行脚本时出现此错误:
/home/user/.cache/bazel/_bazel_user/2e3943447179c1e4f0f547d49ba1444f/execroot/__main__/bazel-out/k8-fastbuild/bin/my_package/c: line 1: ./a.sh: No such file or directory
/home/user/.cache/bazel/_bazel_user/2e3943447179c1e4f0f547d49ba1444f/execroot/__main__/bazel-out/k8-fastbuild/bin/my_package/c: line 2: ./b.sh: No such file or directory
如何更改BUILD.bazel
文件(或任何其他文件)以获得所需的输出?
解决方法
runfiles.bash是从Shell脚本中查找到runfiles的路径的首选方法。文档位于链接文件顶部的注释中。
对于您的示例,这应该做到:
QueryInterface
您还需要通过将as<T>()
中的行替换为:来添加来自Shell脚本的依赖项:
# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---
"$(rlocation my_workspace/a)"
"$(rlocation my_workspace/b)"
并通过将其添加到BUILD.bazel
中来命名您的工作空间:
sh_binary(
name = "c",srcs = ["c.sh"],data = [
":a",":b",]),deps = ["@bazel_tools//tools/bash/runfiles"],)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。