我需要使用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
Run Code Online (Sandbox Code Playgroud)
并在WORKSPACE文件中编写如下内容:
load("//:downloadgithubrepo.bzl", "github")
Run Code Online (Sandbox Code Playgroud)
并需要一个BUILD文件(也位于项目根目录)来调用构建,其内容如下:
cc_library(
name = "testrun",
srcs = "main.c",
)
Run Code Online (Sandbox Code Playgroud)
我必须添加main.c文件,否则构建会失败-这是一个问题,真正的问题是这不起作用,因为构建正在传递中,但是GitHub存储库未下载。
我完全走对了吗?有人做过这样的事吗?
What you're looking might already be implemented in the new_git_repository
repository rule, or the git_repository
rule if the GitHub project already has Bazel BUILD
files wired in.
If the GitHub project does not have BUILD files, a BUILD file is required when using new_git_repository
. For example, if you want to depend on a file target (e.g. /foo/bar.txt
) or rule target (e.g. a cc_library
) in https://github.com/example/repository
, and the repository does not have BUILD files, write these lines in your project's WORKSPACE
file:
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 = ["..."],
""",
)
Run Code Online (Sandbox Code Playgroud)
In your BUILD
file, reference the external repository's targets using the @
prefix:
cc_library(
name = "testrun",
srcs = ["main.c"],
data = ["@example_repository//:foo/bar.txt"],
deps = ["@example_repository//:remote_cc_library"],
)
Run Code Online (Sandbox Code Playgroud)
When you run bazel build //:testrun
, Bazel will..
//:testrun
, which include the file main.c
and targets from the external repository @example_repository
.example_repository
, and finds the new_git_repository
declaration.git clone
on the remote
attribute specified in the example_repository
declaration. build_file_content
string at the project root of the cloned repository.@example_repository//:foo/bar.txt
and @example_repository//:remote_cc_library
//:testrun
cc_library
.//:testrun
.If the GitHub project does have BUILD files, you do not need to provide an BUILD file. You can refer to the targets directly after specifying the WORKSPACE dependency with git_repository
:
git_repository(
name = "example_repository",
remote = "https://github.com/example/repository.git",
)
Run Code Online (Sandbox Code Playgroud)
For more information, check out Bazel's documentation on External Repositories.