Bazel具有模块注册表作为其内置功能,以便在构建过程中跟踪模块依赖关系并管理构建输出。这使得在构建过程中避免重复构建相同模块,提高构建效率。例如,考虑以下WORKSPACE文件:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_google_protobuf",
sha256 = "c7045a774b5cadeee312ac1f05224b74f3b1613103a23e20c76a39be2796c422",
urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protobuf-all-3.9.0.tar.gz"],
)
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
这个文件中,我们加载了一个名为com_google_protobuf的http_archive,并在其后面调用了一个protobuf_deps()函数。这个函数会注册com_google_protobuf作为一个模块,并指定它的依赖。在构建过程中,Bazel会根据这些依赖关系自动下载和编译依赖的模块。例如,如果我们有一个BUILD文件,它依赖于com_google_protobuf模块:
cc_binary(
name = "my_binary",
srcs = ["my_binary.cc"],
deps = ["@com_google_protobuf//:protobuf"],
)
这个BUILD文件中,我们指定了my_binary需要依赖于@com_google_protobuf//:protobuf。在构建过程中,Bazel会自动下载和编译com_google_protobuf模块,并将其注册到一个模块注册表中。然后,它会从该注册表中检索@com_google_protobuf//:protobuf,以及其他依赖项,并将它们传递给编译器进行