gpt4 book ai didi

bazel - 我可以从 .bzl 文件加载通用规则吗?

转载 作者:行者123 更新时间:2023-12-04 10:20:59 36 4
gpt4 key购买 nike

我们经常需要每个技术堆栈的通用规则组合。
这目前在 WORKSPACE 中浪费了大量空间 - 它们应该在多个存储库中保持同步。在 buildifier 之后有 50 多行,并且包含太多的 url、版本和哈希值。

现在说我有一个“技术堆栈”存储库并执行类似的操作

load("@techstack_repo//mylang.bzl", "load_rules")
load_rules()

哪里 load_rules将加载和初始化固定版本,例如rules_go、bazel-gazelle、rules_docker、rules_proto 并以正确的顺序初始化它们,以便它们在 WORKSPACE 中可见?

我没有在我的测试中使用它,因为 load显然不能在 bzl 文件中的函数中运行 - 它本身不是函数。

有没有办法做到这一点?

这是我为 Java 测试的示例:
load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
load(
"@io_grpc_grpc_java//:repositories.bzl",
"IO_GRPC_GRPC_JAVA_ARTIFACTS",
"IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS",
"grpc_java_repositories",
)
load("@rules_jvm_external//:defs.bzl", "maven_install")

def prepare_stack(maven_deps = []):
container_repositories()
container_deps()
container_pull(
name = "java_base",
# https://console.cloud.google.com/gcr/images/distroless/GLOBAL/java-debian10
# tag = "11", # OpenJDK 11 as of 2020-03-04
digest = "sha256:eda9e5ae2facccc9c7016f0c2d718d2ee352743bda81234783b64aaa402679b6",
registry = "gcr.io",
repository = "distroless/java-debian10",
)
rules_proto_dependencies()
rules_proto_toolchains()
maven_install(
artifacts = maven_deps + IO_GRPC_GRPC_JAVA_ARTIFACTS,
# for improved debugging in IDE
fetch_sources = True,
generate_compat_repositories = True,
override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
repositories = [
"https://repo.maven.apache.org/maven2/",
"https://repo1.maven.org/maven2",
],
strict_visibility = True,
)
grpc_java_repositories()

... 全部 http_archive对规则 repo 的调用位于 WORKSPACE我想把它们搬到这里,但这根本不起作用。
按原样,我收到此错误:
ERROR: Failed to load Starlark extension '@rules_python//python:pip.bzl'.
Cycle in the workspace file detected. This indicates that a repository is used prior to being defined.
The following chain of repository dependencies lead to the missing definition.
- @rules_python
This could either mean you have to add the '@rules_python' repository with a statement like `http_archive` in your WORKSPACE file (note that transitive dependencies are not added automatically), or move an existing definition earlier in your WORKSPACE file.

添加 rules_python 也无济于事。

最佳答案

我找到了一个解决方案:

把它分成两个文件。
一个像这样的进口:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

def declare():
maybe(
git_repository,
name = "rules_cc",
commit = "34ca16f4aa4bf2a5d3e4747229202d6cb630ebab",
remote = "https://github.com/bazelbuild/rules_cc.git",
shallow_since = "1584036492 -0700",
)
# ... for me requires at least rules_cc, rules_python, bazel_skylib
# for later proto, docker, go, java support

另一个使用声明的外部来源:
# go
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

# protobuf
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

# container
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
load("@io_bazel_rules_docker//go:image.bzl", go_image_repositories = "repositories")

def init_rules():
go_rules_dependencies()
go_register_toolchains()
gazelle_dependencies()
rules_proto_dependencies()
rules_proto_toolchains()
container_repositories()
container_deps()
go_image_repositories()
container_pull(
name = "go_static",
digest = "sha256:9b60270ec0991bc4f14bda475e8cae75594d8197d0ae58576ace84694aa75d7a",
registry = "gcr.io",
repository = "distroless/static",
)

这有点麻烦,但可以使用 http_archive 获取这个 repo或 git_repository , 加载第一个文件并调用 declare并为 init_rules 加载第二个并称之为。

它可能有点令人费解,但它仍然有助于统一堆栈并简化您的 WORKSPACE .

关于bazel - 我可以从 .bzl 文件加载通用规则吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60853736/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com