Bazel rules_foreign_cc - //:<target>取决于存储库@local_config_cc中无法获取的 @local_config_cc//:cc-compile



我是巴泽尔的绝对初学者。我正试图通过rules_forging_cc 使用bazel构建一个基于CMake的应用程序

一般信息:

  • 操作系统:Ubuntu 20.04 LTS
  • Bazel版本:已试用3.4.0和5.0.0
  • CMake版本:3.19.5

我的目录结构看起来像这个

ROOT
|-----BUILD
|-----WORKSPACE
|-----<my_cmake_based_app>-<commit_id>.tar.gz

<my_cmake_based_app>在其根目录下有一个CMakeLists.txt。

WORKSPACE文件如下所示:

workspace(name = "rules_foreign_cc_usage_example")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Rule repository, note that it's recommended to use a pinned commit to a released version of the rules
http_archive(
name = "rules_foreign_cc",
# TODO: Get the latest sha256 value from a bazel debug message or the latest 
#       release on the releases page: https://github.com/bazelbuild/rules_foreign_cc/releases
#
# sha256 = "...",
strip_prefix = "rules_foreign_cc-0.7.0",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.7.0.tar.gz",
)
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
# This sets up some common toolchains for building targets. For more details, please see
# https://github.com/bazelbuild/rules_foreign_cc/tree/main/docs#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies()
_ALL_CONTENT = """
filegroup(
name = "all_srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
"""
http_archive(
name = "<target>",
strip_prefix = "<my_cmake_based_app>-<commit_id>",
build_file_content = _ALL_CONTENT,
url = "file:///<absolute/path/to>/<my_cmake_based_app>-<commit_id>.tar.gz",
)

我的BUILD文件如下:

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
cmake(
name = "<target>",
cache_entries = {
"CMAKE_C_FLAGS": "-fPIC",
},
lib_source = "@<target>//:all_srcs",
out_shared_libs = ["lib<target>.so"],
)

用于启动构建。我使用以下命令:

bazel build //:<target>

错误日志如下:

DEBUG: Rule 'rules_foreign_cc' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "1df78c7d7eed2dc21b8b325a2853c31933a81e7b780f9a59a5d078be9008b13a"
DEBUG: Repository rules_foreign_cc instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
INFO: Repository local_config_cc instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule cc_autoconf defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl:143:30: in <toplevel>
ERROR: An error occurred during the fetch of repository 'local_config_cc':
Traceback (most recent call last):
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl", line 125
configure_unix_toolchain(<3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 352, in configure_unix_toolchain
_find_generic(repository_ctx, <3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 314, in _find_generic
repository_ctx.which(result)
Program argument of which() may not contains a / or a  ('x86_64-<name_of_sdk>-linux-gcc  -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
INFO: Repository cmake-3.22.0-linux-x86_64 instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
INFO: Repository <target> instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
ERROR: /home/ubuntu/<ROOT>/BUILD:3:6: //:<target> depends on @local_config_cc//:cc-compiler-k8 in repository @local_config_cc which failed to fetch. no such package '@local_config_cc//': Traceback (most recent call last):
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl", line 125
configure_unix_toolchain(<3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 352, in configure_unix_toolchain
_find_generic(repository_ctx, <3 more arguments>)
File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 314, in _find_generic
repository_ctx.which(result)
Program argument of which() may not contains a / or a  ('x86_64-<name_of_sdk>-linux-gcc  -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
ERROR: Analysis of target '//:<target>' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.127s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

我的一些显而易见的问题是:

  • 我不知道这个@local_config_cc是从哪里来的
  • 我是不是错过了要添加到WORKSPACE/BUILD文件中的内容?或
  • 我的目录结构一团糟吗

顺便说一句,由于这是我第一次在stackoverflow上发布内容,我非常感谢任何提示,以便我将来能以更好的方式提问。

谢谢!

EDIT:仅供参考,我能够在现有系统上构建rules_forging_cc中给出的示例。

编辑:仅供参考,我使用的系统是AWS EC2实例

@local_config_cc是Bazel的自动C++工具链设置的一部分。Bazel和您系统上的C++工具链之间似乎出了问题,因为这是错误:

Program argument of which() may not contains a / or a  ('x86_64-<name_of_sdk>-linux-gcc  -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)

因此,which的参数看起来像某个命令,而它应该只是一个c++工具的名称,比如";gcc";。Bazel也将研究环境变量来找到这些工具。CC环境变量中有什么内容吗?

如果没有,尝试先构建一些简单的东西可能会很有用,例如:

main.c:

#include <stdio.h>
int main() {
printf("hello worldn");
return 0;
}

BUILD:

cc_binary(
name = "main",
srcs = ["main.c"],
)

然后:

$ bazel run main
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:main (35 packages loaded, 146 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
bazel-bin/main
INFO: Elapsed time: 3.657s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
hello world

更一般地说,在您的工作空间中有一个tar.gz档案,然后在workspace文件[1]的http_archive规则中引用该档案,这有点不寻常。如果你有具体的原因或其他背景,谈论这些可能会有所帮助。否则,您基本上只是使用Bazel来解开存档并对内容运行make,这可以在两行shell脚本中完成:(通常,您的构建文件和工作区文件将是代码存储库本身的一部分。

[1] 此外,在工作区文件(或任何地方(中放置绝对路径会使您的设置不可移植

最新更新