使用Bazel在离线情况下(无需互联网连接)构建C++项目。

4
我尝试使用以下命令从Bazel主页(Getting Started)构建Bazel C++ Build教程,但不连接到互联网:bazel build //main:hello-world
Jenkins服务器将没有任何与互联网的连接,因此我无法进行预取或类似操作。是否有一种方法可以在另一台计算机上准备我的C++项目,并将依赖项传输到Jenkins服务器以离线执行?如何实现?我只需要得到以下错误消息中的内容,或者使教程运行起来:
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
INFO: Repository rules_cc instantiated at:
  /DEFAULT.WORKSPACE.SUFFIX:267:6: in <toplevel>
  C:/users/XXX/_bazel_XXX/dq2p42jq/external/bazel_tools/tools/build_defs/repo/utils.bzl:201:18: in maybe
Repository rule http_archive defined at:
  C:/users/XXX/_bazel_XXX/dq2p42jq/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
WARNING: Download from https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip failed: class java.io.IOException Unable to tunnel through proxy. Proxy returns "HTTP/1.1 501 Not Implemented"
WARNING: Download from https://github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip failed: class java.io.IOException Unable to tunnel through proxy. Proxy returns "HTTP/1.1 501 Not Implemented"
ERROR: An error occurred during the fetch of repository 'rules_cc':
   Traceback (most recent call last):
        File "C:/users/XXX/_bazel_XXX/dq2p42jq/external/bazel_tools/tools/build_defs/repo/http.bzl", line 111, column 45, in _http_archive_impl
                download_info = ctx.download_and_extract(
Error in download_and_extract: java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip, https://github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip] to C:/users/XXX/_bazel_XXX/dq2p42jq/external/rules_cc/temp1363696983472254851/b1c40e1de81913a3c40e5948f78719c28152486d.zip: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 501 Not Implemented"
ERROR: Error fetching repository: Traceback (most recent call last):
        File "C:/users/XXX/_bazel_XXX/dq2p42jq/external/bazel_tools/tools/build_defs/repo/http.bzl", line 111, column 45, in _http_archive_impl
                download_info = ctx.download_and_extract(
Error in download_and_extract: java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip, https://github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip] to C:/users/XXX/_bazel_XXX/dq2p42jq/external/rules_cc/temp1363696983472254851/b1c40e1de81913a3c40e5948f78719c28152486d.zip: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 501 Not Implemented"
ERROR: Skipping '//main:hello-world': no such package '@rules_cc//cc': java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip, https://github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip] to C:/users/XXX/_bazel_XXX/dq2p42jq/external/rules_cc/temp1363696983472254851/b1c40e1de81913a3c40e5948f78719c28152486d.zip: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 501 Not Implemented"
WARNING: Target pattern parsing failed.
ERROR: no such package '@rules_cc//cc': java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip, https://github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip] to C:/users/XXX/_bazel_XXX/dq2p42jq/external/rules_cc/temp1363696983472254851/b1c40e1de81913a3c40e5948f78719c28152486d.zip: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 501 Not Implemented"
INFO: Elapsed time: 30.974s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
    currently loading: main

更新: 我尝试了预取(prefetching),但它没有生效。我所做的是:

  1. 运行 bazel fetch //...
  2. 将预取的数据从本地的 bazel info output_base 目录复制到 Jenkins 服务器上(我必须重新创建一个符号链接到安装目录)。
  3. 在 Jenkins 上运行 bazel build --fetch=false //main:hello-world 而不进行重新获取。现在我遇到以下错误:
Loading: 
Loading: 0 packages loaded
WARNING: /DEFAULT.WORKSPACE:1:17: External repository 'bazel_tools' is not up-to-date and fetching is disabled. To update, run the build without the '--nofetch' command line option.
ERROR: error loading package '': Every .bzl file must have a corresponding package, but '@bazel_tools//tools/build_defs/repo:http.bzl' does not have one. Please create a BUILD file in the same or any parent directory. Note that this BUILD file does not need to do anything except exist.
INFO: Elapsed time: 0.298s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
2个回答

2
我找到了一种使用bazel离线构建简单C++项目的解决方案。首先,您需要下载rules_ccrules_java库,因为这些是简单的Bazel C++教程项目中的外部依赖库。我没有找到rules_cc的发布版本,所以我从控制台上的错误消息提到的网址下载了zip文件,当我尝试使用离线bazel构建时,会出现这些错误消息:https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/b1c40e1de81913a3c40e5948f78719c28152486d.zip。当您使用错误的sha密钥(见下文)在本地使用该库时,也会提到sha密钥。可以在此处下载rules_java库:https://github.com/bazelbuild/rules_java/releasesWORKSPACE规范也在那里提到。

然后,您必须将以下内容添加到WORKSPACE文件中。请注意,您必须指向rules_ccrules_java存档文件的本地副本。对于rules_cc,您必须在strip_prefix中提到存档文件的根路径(.zip文件中唯一的第一个根目录)。rules_java没有根目录:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_cc",
    urls = ["file:C:/tmp/b1c40e1de81913a3c40e5948f78719c28152486d.zip"],
    strip_prefix = "rules_cc-b1c40e1de81913a3c40e5948f78719c28152486d",
    sha256 = "d0c573b94a6ef20ef6ff20154a23d0efcb409fb0e1ff0979cec318dfe42f0cdd",
)

http_archive(
    name = "rules_java",
    url = "file:C:/tmp/rules_java-4.0.0.tar.gz",
    sha256 = "34b41ec683e67253043ab1a3d1e8b7c61e4e8edefbcad485381328c934d072fe",
)

0

你可以运行bazel fetch //...在能访问互联网的机器上本地获取数据。然后,您只需将预取数据从bazel info output_base目录复制到Jenkins作业中bazel info output_base即可。


我尝试过了,但是它没有起作用(请参见更新)。 - Yalin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接