1

我是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 实例

4

1 回答 1

0

@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看起来像一些命令,而它应该只是像“gcc”这样的 c++ 工具的名称。Bazel 也会查看环境变量以找到这些工具。CC环境变量中有什么东西吗?

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

main.c

#include <stdio.h>
int main() {
  printf("hello world\n");
  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] 此外,将绝对路径放在您的工作区文件(或真正的任何地方)中会使您的设置不可移植

于 2022-02-23T21:21:51.013 回答