我是bazel的绝对初学者。我正在尝试通过rules_foreign_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 上发布内容,因此我非常感谢任何提示,以便我将来可以以更好的方式提出问题。
谢谢!
编辑:仅供参考,我能够在我现有的系统上构建rules_foreign_cc中给出的示例。
编辑:仅供参考,我使用的系统是 AWS EC2 实例