您正在查看的内容可能已经在 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 会..
- 分析
//:testrun的依赖关系,包括文件main.c和来自外部存储库@example_repository的目标。
- 查找名为
example_repository 的外部存储库的 WORKSPACE 文件,并找到 new_git_repository 声明。
- 对
example_repository 声明中指定的remote 属性执行git clone。
- 在克隆存储库的项目根目录中写入包含
build_file_content 字符串的 BUILD 文件。
- 分析目标
@example_repository//:foo/bar.txt和@example_repository//:remote_cc_library
- 构建依赖关系,并将它们交给您的
//:testruncc_library。
- 构建
//:testrun。
如果 GitHub 项目有 BUILD 文件,则无需提供 BUILD 文件。使用git_repository指定WORKSPACE依赖后可以直接引用targets:
git_repository(
name = "example_repository",
remote = "https://github.com/example/repository.git",
)
有关更多信息,请查看 Bazel 在 External Repositories 上的文档。