Bazel 函数:git_repository vs new_git_repository?

Bazel function: git_repository vs new_git_repository?

函数 git_repositorynew_git_repository 有什么区别?

new_git_repository 可以做 git_repository 做的所有事情吗?如果是这样,为什么我们有两个独立的功能?

(正在阅读 https://docs.bazel.build/versions/main/repo/git.html

通常,如果底层存储库已经 bazelized,则使用 git_repository,否则 new_git_repository

new_git_repository 提供属性 build_filebuild_file_contentgit_repository 没有这些属性。

例如 Google Test 已经在使用 Bazel 作为构建系统(即它在其根目录中提供 WORKSPACE 文件)。要获取它,您只需执行以下操作:

# GoogleTest- Google Testing and Mocking Framework
git_repository(
    name = "googletest",
    commit = "703bd9caab50b139428cea1aaff9974ebee5742e",  # googletest v1.10.0
    remote = "https://github.com/google/googletest",
    shallow_since = "1570114335 -0400",
)

然后您可以轻松地使用 Google 测试作为某些 BUILD 文件中的依赖项:

cc_test(
    name = "tests",
    timeout = "short",
    srcs = ["test.cpp"],
    deps = [
        "@googletest//:gtest_main", # Use GoogleTest as a dependency
    ],
)

new_git_repository 通常在底层依赖没有被 bazelized 的情况下使用。 new_git_repository 让您可以提供一个 BUILD 文件,可以说是注入到正在考虑的存储库中。除此之外,还会为依赖项生成一个简单的 WORKSPACE 文件。通过这种方式,您可以在 git 存储库外部提供一个 BUILD 文件表单,使其与 Bazel 构建系统一起工作。

示例 https://github.com/fmtlib/fmt 没有被 bazelized。您可以编写一个 fmt.BUILD 文件,内容如下:

cc_library(
    name = "fmt",
    srcs = [
        "include/fmt/args.h",
        "include/fmt/chrono.h",
        "include/fmt/color.h",
        "include/fmt/compile.h",
        "include/fmt/core.h",
        "include/fmt/format.h",
        "include/fmt/format-inl.h",
        "include/fmt/locale.h",
        "include/fmt/os.h",
        "include/fmt/ostream.h",
        "include/fmt/printf.h",
        "include/fmt/ranges.h",
        "include/fmt/xchar.h",
        #"src/fmt.cc", # No C++ module support
        "src/format.cc",
        "src/os.cc",
    ],
    hdrs = [
        "include/fmt/core.h",
        "include/fmt/format.h",
    ],
    includes = [
        "include",
        "src",
    ],
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
)

现在您可以使用new_git_repository提供(注入)上述构建文件,例如:

new_git_repository(
    name = "fmt",
    build_file = "//third_party:fmt.BUILD",
    ...
)

顺便说一句:工作区规则 local_repository 也有两种形式:local_repositorynew_local_repository。同样,新变体也接受外部 BUILD 文件。较旧版本的 Bazel (< 0.29.0) 也有 new_http_archive - 无论如何,当前版本的 Bazel (4.2.1) 只有 http_archive 也接受属性 build_filebuild_file_content.