调用 llvm-config --prefix 并在 BUILD 规则中使用它
Call llvm-config --prefix and use it in a BUILD rule
我在 WORKSPACE 中有以下规则:
new_local_repository(
name = "llvm",
path = "/opt/local/libexec/llvm-4.0",
build_file= "llvm.BUILD")
我现在想为 llvm 使用硬编码路径。 llvm-config --prefix
可以给我llvm的目录。得到这个的正确方法是什么?我可以只使用标准 python 命令(例如 subprocess
包)吗?
您可以创建一个自定义 repository_rule,您可以在其中调用 repository_ctx.execute("llvm-config --prefix")
并使该值在您的包中可用。
我建议您先阅读有关 repository_rule
的内容,如果有任何不清楚的地方,请随时询问。
正如@abergmeier 所说,您可以创建自定义存储库规则,运行 命令,创建指向其输出的符号链接,并为存储库创建 BUILD 文件:
工作空间文件:
workspace(name = "io_bazel")
load("//foo:repo.bzl", "llvm_configure")
llvm_configure(name = "local_config_llvm")
foo/repo.bzl:
def _impl(repository_ctx):
result = repository_ctx.execute(["echo", "/tmp/dummy/path"])
llvm_path = result.stdout.splitlines()[0]
repository_ctx.symlink(llvm_path, "llvm-4.0")
repository_ctx.file("BUILD", """
filegroup(
name = "llvm_files",
srcs = glob(["llvm-4.0/**"]),
visibility = ["//visibility:public"],
)
""")
llvm_configure = repository_rule(
implementation=_impl,
local = True,
environ = [])
回购中的目标:
$ bazel query @local_config_llvm//:*
@local_config_llvm//:llvm_files
@local_config_llvm//:llvm-4.0/a.txt
@local_config_llvm//:BUILD
生成的文件:
$ ls -la $(bazel info output_base)/external/local_config_llvm
total 16
drwxr-x--- 2 laszlocsomor eng 4096 May 12 13:06 .
drwxr-x--- 6 laszlocsomor eng 4096 May 12 13:06 ..
-rwxr-x--x 1 laszlocsomor eng 115 May 12 13:06 BUILD
lrwxrwxrwx 1 laszlocsomor eng 15 May 12 13:06 llvm-4.0 -> /tmp/dummy/path
-rw-r----- 1 laszlocsomor eng 116 May 12 13:06 WORKSPACE
$ cat $(bazel info output_base)/external/local_config_llvm/BUILD
filegroup(
name = "llvm_files",
srcs = glob(["llvm-4.0/**"]),
visibility = ["//visibility:public"],
)
$ cat $(bazel info output_base)/external/local_config_llvm/WORKSPACE
# DO NOT EDIT: automatically generated WORKSPACE file for llvm_configure rule
workspace(name = "local_config_llvm")
我在 WORKSPACE 中有以下规则:
new_local_repository(
name = "llvm",
path = "/opt/local/libexec/llvm-4.0",
build_file= "llvm.BUILD")
我现在想为 llvm 使用硬编码路径。 llvm-config --prefix
可以给我llvm的目录。得到这个的正确方法是什么?我可以只使用标准 python 命令(例如 subprocess
包)吗?
您可以创建一个自定义 repository_rule,您可以在其中调用 repository_ctx.execute("llvm-config --prefix")
并使该值在您的包中可用。
我建议您先阅读有关 repository_rule
的内容,如果有任何不清楚的地方,请随时询问。
正如@abergmeier 所说,您可以创建自定义存储库规则,运行 命令,创建指向其输出的符号链接,并为存储库创建 BUILD 文件:
工作空间文件:
workspace(name = "io_bazel")
load("//foo:repo.bzl", "llvm_configure")
llvm_configure(name = "local_config_llvm")
foo/repo.bzl:
def _impl(repository_ctx):
result = repository_ctx.execute(["echo", "/tmp/dummy/path"])
llvm_path = result.stdout.splitlines()[0]
repository_ctx.symlink(llvm_path, "llvm-4.0")
repository_ctx.file("BUILD", """
filegroup(
name = "llvm_files",
srcs = glob(["llvm-4.0/**"]),
visibility = ["//visibility:public"],
)
""")
llvm_configure = repository_rule(
implementation=_impl,
local = True,
environ = [])
回购中的目标:
$ bazel query @local_config_llvm//:*
@local_config_llvm//:llvm_files
@local_config_llvm//:llvm-4.0/a.txt
@local_config_llvm//:BUILD
生成的文件:
$ ls -la $(bazel info output_base)/external/local_config_llvm
total 16
drwxr-x--- 2 laszlocsomor eng 4096 May 12 13:06 .
drwxr-x--- 6 laszlocsomor eng 4096 May 12 13:06 ..
-rwxr-x--x 1 laszlocsomor eng 115 May 12 13:06 BUILD
lrwxrwxrwx 1 laszlocsomor eng 15 May 12 13:06 llvm-4.0 -> /tmp/dummy/path
-rw-r----- 1 laszlocsomor eng 116 May 12 13:06 WORKSPACE
$ cat $(bazel info output_base)/external/local_config_llvm/BUILD
filegroup(
name = "llvm_files",
srcs = glob(["llvm-4.0/**"]),
visibility = ["//visibility:public"],
)
$ cat $(bazel info output_base)/external/local_config_llvm/WORKSPACE
# DO NOT EDIT: automatically generated WORKSPACE file for llvm_configure rule
workspace(name = "local_config_llvm")