用于非Bazel包的Bazel工作区 [英] bazel workspace for non-bazel packages
问题描述
我正在使用包graphviz作为服务的一部分,为了使用它,我像这样开始BazelWORKSPACE文件
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"])
)
"""
)
...
它的问题在于它依赖于graphviz
被下载、预安装并出现在路径/usr/local/Cellar/graphviz/2.49.1
中。有没有办法让它成为Bazel构建过程的一部分,这样如果它不存在,它就会被取出并放在正确的位置?
推荐答案
您可以使用http_archive
下载Graphviz的发布归档文件之一:
https://docs.bazel.build/versions/main/repo/http.html#http_archive
来自https://graphviz.org/download/source/2.49.1版本于https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/2.49.1/graphviz-2.49.1.tar.gz
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()
内部版本(例如在//Third_Party/graph viz中):
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
编辑:将Rules_Foreign_cc与raphviz一起使用的示例:
工作空间:
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
这篇关于用于非Bazel包的Bazel工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!