Bazel $(location) 宏的绝对路径
Absolute path of Bazel $(location) macro
我正在使用 $(location)
将构建的 java_library 的位置传递给我的 java_binary(简化目标):
java_binary(
name = "my_bin",
main_class = "Bin",
srcs = [
"Bin.java"
],
deps = [
":my_lib"
],
jvm_flags = [
"-Dmy_library_path=$(location :my_lib)"
]
)
java_library(
name = "my_lib",
srcs = [
"Lib.java"
]
)
看起来这个位置是相对于原始 运行ning 位置的,这意味着当我在当前目录中使用 --run_under
到 运行 我的目标时,它会中断并且位置无效。
有没有办法获取绝对位置,或者有没有更便携的方法将 jar 路径传递给我的二进制文件?
写一个sh_binary to wrap it, and then use runfiles.bash在运行时查找路径。像这样(在将 my_bin
重命名为 my_bin_impl
并删除 jvm_flags
之后):
sh_binary(
name = "my_bin",
srcs = [
"my_bin_wrapper.sh",
],
data = [
":my_bin_impl",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)
和 my_bin_wrapper.sh
:
#!/bin/bash
# --- 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 "[=11=].runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "[=11=].runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "[=11=].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 ---
exec "$(rlocation "my_repository/my_package/my_bin_impl")" --jvm_flag=-Dmy_library_path="$(rlocation "my_repository/my_package/my_lib")" "$@"
如果你想像这样执行多个二进制文件,你可以包装生成包装器并在 macro.
中创建 sh_binary
我正在使用 $(location)
将构建的 java_library 的位置传递给我的 java_binary(简化目标):
java_binary(
name = "my_bin",
main_class = "Bin",
srcs = [
"Bin.java"
],
deps = [
":my_lib"
],
jvm_flags = [
"-Dmy_library_path=$(location :my_lib)"
]
)
java_library(
name = "my_lib",
srcs = [
"Lib.java"
]
)
看起来这个位置是相对于原始 运行ning 位置的,这意味着当我在当前目录中使用 --run_under
到 运行 我的目标时,它会中断并且位置无效。
有没有办法获取绝对位置,或者有没有更便携的方法将 jar 路径传递给我的二进制文件?
写一个sh_binary to wrap it, and then use runfiles.bash在运行时查找路径。像这样(在将 my_bin
重命名为 my_bin_impl
并删除 jvm_flags
之后):
sh_binary(
name = "my_bin",
srcs = [
"my_bin_wrapper.sh",
],
data = [
":my_bin_impl",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
],
)
和 my_bin_wrapper.sh
:
#!/bin/bash
# --- 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 "[=11=].runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "[=11=].runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "[=11=].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 ---
exec "$(rlocation "my_repository/my_package/my_bin_impl")" --jvm_flag=-Dmy_library_path="$(rlocation "my_repository/my_package/my_lib")" "$@"
如果你想像这样执行多个二进制文件,你可以包装生成包装器并在 macro.
中创建sh_binary