Bazel 函数:git_repository 与 new_git_repository?

sam*_*sam 5 bazel

git_repository函数和之间有什么区别new_git_repository

new_git_repository做到一切git_repository吗?如果是这样,为什么我们有两个独立的功能?

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

Ver*_*ahn 5

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

new_git_repository提供属性build_file,而不build_file_content具有git_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",
)
Run Code Online (Sandbox Code Playgroud)

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

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

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"],
)
Run Code Online (Sandbox Code Playgroud)

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

new_git_repository(
    name = "fmt",
    build_file = "//third_party:fmt.BUILD",
    ...
)
Run Code Online (Sandbox Code Playgroud)

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