我正在使用一个包graphviz作为服务的一部分,为了使用它,我像这样开始bazel WORKSPACE文件
new_local_repository(
name = "graphviz",
path = "/usr/local/Cellar/graphviz/2.49.1",
build_file_content = """
package(default_visibility = ["//visibility:public"])
cc_library(
name = "headers",
srcs = glob(["**/*.dylib"]),
hdrs = glob(["**/*.h"])
)
"""
)
...
它的问题在于它依赖于在path /usr/local/Cellar/graphviz/2.49.1
中下载、预安装和存在的graphviz
。有没有办法让它成为bazel构建过程的一部分,这样如果它不存在,它就会被取出并放在正确的位置?
发布于 2021-10-18 17:59:21
您可以使用http_archive
下载graphviz的一个发布归档文件:https://docs.bazel.build/versions/main/repo/http.html#http_archive
https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/2.49.1/graphviz-2.49.1.tar.gz上提供了https://graphviz.org/download/source/的2.49.1版本
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "graphviz",
url = "https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/2.49.1/graphviz-2.49.1.tar.gz",
strip_prefix = "graphviz-2.49.1",
sha256 = "ba1aa7a209025cb3fc5aca1f2c0114e18ea3ad29c481d75e4d445ad44e0fb0f7",
build_file_content = """
package(default_visibility = ["//visibility:public"])
cc_library(
name = "headers",
srcs = glob(["**/*.dylib"]),
hdrs = glob(["**/*.h"])
)
""",
)
为了回答问题中的“如果它不存在”这一部分,我不知道有一种直接的方法可以在本地安装的东西和下载的东西之间自动切换。http_archive
将始终下载归档文件,而new_local_repository
将始终使用本地内容。
有--override_repository
标志,它用本地存储库替换存储库,例如,--override_repository=graphviz=/usr/local/Cellar/graphviz/2.49.1
将有效地用指向该路径的local_repository
替换http_archive
。然而,bazel会期望在该位置已经有一个工作空间文件和构建文件(即,没有办法指定build_file_content
)
您可以在工作区文件中指定这两个存储库规则,然后使用某种间接性、Starlark标志和select()
使用命令行标志在存储库之间切换。虽然有点复杂,但也不是自动的。如下所示:
工作空间:
http_archive(
name = "graphviz-download",
...,
)
new_local_repository(
name = "graphviz-installed",
...,
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_skylib",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
],
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
)
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
BUILD (例如在//third_party/graphviz中):
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
bool_flag(
name = "use-installed-graphviz",
build_setting_default = False,
)
config_setting(
name = "installed",
flag_values = {
":use-installed-graphviz": "True",
}
)
alias(
name = "headers",
actual = select({
":installed": "@graphviz-installed//:headers",
"//conditions:default": "@graphviz-download//:headers",
})
)
然后您的代码依赖于//third_party/graphviz:headers
,默认情况下,别名将指向下载的版本,标志--//third_party/graphviz:use-installed-graphviz
会将其切换到已安装的版本:
$ bazel cquery --output build //third_party/graphviz:headers
alias(
name = "headers",
actual = "@graphviz-download//:headers",
)
$ bazel cquery --output build //third_party/graphviz:headers --//third_party/graphviz:use-installed-graphviz
alias(
name = "headers",
actual = "@graphviz-installed//:headers",
)
另一种选择是编写(或查找)结合了http_archive
和local_repository
功能的自定义存储库规则,但这可能是一项相当繁重的工作。
一般来说,我认为大多数人只是使用http_archive
并下载依赖项,如果有离线或缓存的特定需求,可以使用--distdir
将已经下载的工件用于远程存储库规则:https://docs.bazel.build/versions/main/guide.html#distribution-files-directories
编辑:在graphviz中使用rules_foreign_cc的示例:
工作空间:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_foreign_cc",
sha256 = "69023642d5781c68911beda769f91fcbc8ca48711db935a75da7f6536b65047f",
strip_prefix = "rules_foreign_cc-0.6.0",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.6.0.tar.gz",
)
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
# This sets up some common toolchains for building targets. For more details, please see
# https://bazelbuild.github.io/rules_foreign_cc/0.6.0/flatten.html#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies()
http_archive(
name = "graphviz",
url = "https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/2.49.1/graphviz-2.49.1.tar.gz",
strip_prefix = "graphviz-2.49.1",
sha256 = "ba1aa7a209025cb3fc5aca1f2c0114e18ea3ad29c481d75e4d445ad44e0fb0f7",
build_file_content = """\
filegroup(
name = "all_srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
""",
)
内部版本:
load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")
# see https://bazelbuild.github.io/rules_foreign_cc/0.6.0/configure_make.html
configure_make(
name = "graphviz",
lib_source = "@graphviz//:all_srcs",
out_shared_libs = ["libcgraph.so"], # or other graphviz libs
)
cc_binary(
name = "foo",
srcs = ["foo.c"],
deps = [":graphviz"],
)
foo.c:
#include "graphviz/cgraph.h"
int main() {
Agraph_t *g;
g = agopen("G", Agdirected, NULL);
agclose(g);
return 0;
}
用法:
$ bazel build foo
INFO: Analyzed target //:foo (0 packages loaded, 2 targets configured).
INFO: Found 1 target...
Target //:foo up-to-date:
bazel-bin/foo
INFO: Elapsed time: 0.229s, Critical Path: 0.06s
INFO: 7 processes: 5 internal, 2 linux-sandbox.
INFO: Build completed successfully, 7 total actions
https://stackoverflow.com/questions/69599763
复制相似问题