【问题标题】:Pull GitHub repository using Bazel使用 Bazel 拉取 GitHub 存储库
【发布时间】:2018-11-08 14:38:42
【问题描述】:

我需要使用 Bazel 下载整个 GitHub 存储库。由于我对这个工具很陌生,所以我不确定如何实现。

我的主要想法是这样的:

downloadgithubrepo.bzl(与WORKSPACE文件一样位于项目根目录)中编写自定义存储库规则,例如:

def _impl(repository_ctx):
    repository_ctx.download("url_to_zipped_github_repo", output='relative_path_to_output_file')

github = repository_rule(
    implementation = _impl

并在 WORKSPACE 文件中编写如下内容:

load("//:downloadgithubrepo.bzl", "github")

要调用构建,需要一个 BUILD 文件(也位于项目根目录) 其内容如下:

cc_library(
    name = "testrun",
    srcs = "main.c",
)

我必须添加 main.c 文件,否则构建失败 - 这是一个问题,真正的问题是这不起作用,因为构建正在通过但 GitHub 存储库未下载。

我是否走在正确的道路上?以前有人做过类似的事情吗?

【问题讨论】:

    标签: github bazel


    【解决方案1】:

    您正在查看的内容可能已经在 new_git_repository 存储库规则中实现,或者如果 GitHub 项目已经连接了 Bazel BUILD 文件,则在 git_repository 规则中实现。

    如果 GitHub 项目没有有 BUILD 文件,则在使用 new_git_repository 时需要一个 BUILD 文件。例如,如果您想依赖 https://github.com/example/repository 中的 file target (e.g. /foo/bar.txt) or rule target (e.g. a cc_library),并且存储库没有有 BUILD 文件,请将这些行写入项目的 WORKSPACE 文件中:

    new_git_repository(
        name = "example_repository",
        remote = "https://github.com/example/repository.git",
        build_file_content = """
    exports_files(["foo/bar.txt"])
    
    # you can also create targets
    cc_library(
        name = "remote_cc_library",
        srcs = ["..."],
        hdrs = ["..."],
    """,
    )
    

    在您的 BUILD 文件中,使用 @ 前缀引用外部存储库的目标:

    cc_library(
        name = "testrun",
        srcs = ["main.c"],
        data = ["@example_repository//:foo/bar.txt"],
        deps = ["@example_repository//:remote_cc_library"],
    )
    

    当你运行bazel build //:testrun 时,Bazel 会..

    1. 分析//:testrun的依赖关系,包括文件main.c和来自外部存储库@example_repository的目标。
    2. 查找名为 example_repository 的外部存储库的 WORKSPACE 文件,并找到 new_git_repository 声明。
    3. example_repository 声明中指定的remote 属性执行git clone
    4. 在克隆存储库的项目根目录中写入包含 build_file_content 字符串的 BUILD 文件。
    5. 分析目标@example_repository//:foo/bar.txt@example_repository//:remote_cc_library
    6. 构建依赖关系,并将它们交给您的//:testruncc_library
    7. 构建//:testrun

    如果 GitHub 项目 BUILD 文件,则无需提供 BUILD 文件。使用git_repository指定WORKSPACE依赖后可以直接引用targets:

    git_repository(
        name = "example_repository",
        remote = "https://github.com/example/repository.git",
    )
    

    有关更多信息,请查看 Bazel 在 External Repositories 上的文档。

    【讨论】:

    • 谢谢金。我很抱歉延迟回复。你的回答很有帮助,我已经接受了。
    猜你喜欢
    • 2019-12-21
    • 2012-02-17
    • 2013-09-26
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多