使用 Bazel 离线构建 C++ 项目(没有互联网连接)

时间:2021-06-23 17:55:10

标签: c++ build bazel offline

我尝试使用以下命令从 Bazel 主页(入门)构建 Bazel C++ 构建教程,但没有任何互联网连接:bazel build //main:hello-world

Jenkins 服务器将无法连接到 Internet,因此我无法进行预取或类似操作。有没有办法在另一台计算机上准备我的 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

更新: 我试过预取,但它不起作用。我做了什么:

  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 个答案:

答案 0 :(得分:0)

您可以运行 bazel fetch //... 在可以访问 Internet 的机器上本地获取数据。然后,您只需将预取数据:从本地 PC 上的 bazel info output_base 目录复制到 jenkins 作业中的 bazel info output_base

答案 1 :(得分:0)

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

然后您必须将以下内容添加到您的 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",
)