使用自定义 C++ 工具链时构建失败 protoc/zlib

Failure building protoc/zlib when using custom c++ toolchain

我的目标很简单:我想要一个自定义的 C++ 工具链,我可以在其中指定我自己的编译器和编译器选项。我已将我当前的问题归结为一个人为的示例回购:http://github.com/btmcg/bazel_toolchain

我按照 https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html 中描述的教程使用我的自定义构建版本的 gcc。在我添加 cc_proto_library 之前,这似乎工作正常。 Bazel 似乎使用我的自定义工具链来构建 protoc/zlib 并由于我在自定义工具链中的警告标志而出错。我的印象是 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain 会告诉 Bazel 忽略我的自定义工具链,但事实并非如此。我希望它使用 /bin/gcc 而不是我的自定义 gcc 或警告。任何帮助将不胜感激

这里是当前构建失败的地方:

$ bazel build --config=gcc_config //main:hello-world --verbose_failures
INFO: Analyzed target //main:hello-world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...                                                                                                                       ERROR: /home/brian/.cache/bazel/_bazel_brian/2fc228b5a7b1bf0ef87f3ee9353aa72d/external/com_google_protobuf/BUILD:155:11: Compiling src/google/protobuf/extension_set.cc failed: (Exit 1): gcc failed: error executing command
  (cd /home/brian/.cache/bazel/_bazel_brian/2fc228b5a7b1bf0ef87f3ee9353aa72d/sandbox/linux-sandbox/404/execroot/__main__ && \
  exec env - \
    LD_LIBRARY_PATH=/home/brian/.local/lib64:/home/brian/.local/lib \
    PATH=/home/brian/.local/tools/bazel/4.2.2/bin:/home/brian/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl \
    PWD=/proc/self/cwd \
  /opt/custom/gcc/11.2.0/bin/gcc -MD -MF bazel-out/k8-fastbuild/bin/external/com_google_protobuf/_objs/protobuf_lite/extension_set.d '-frandom-seed=bazel-out/k8-fastbuild/bin/external/com_google_protobuf/_objs/protobuf_lite/extension_set.o' -iquote external/com_google_protobuf -iquote bazel-out/k8-fastbuild/bin/external/com_google_protobuf -isystem external/com_google_protobuf/src -isystem bazel-out/k8-fastbuild/bin/external/com_google_protobuf/src -xc++ -Werror -Wswitch-default -DHAVE_ZLIB -Wmissing-field-initializers -Woverloaded-virtual -Wno-sign-compare -c external/com_google_protobuf/src/google/protobuf/extension_set.cc -o bazel-out/k8-fastbuild/bin/external/com_google_protobuf/_objs/protobuf_lite/extension_set.o)
Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In function 'bool google::protobuf::internal::{anonymous}::is_packable(google::protobuf::internal::WireFormatLite::WireType)':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:72:10: error: switch missing default case [-Werror=switch-default]
   72 |   switch (type) {
      |          ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'void* google::protobuf::internal::ExtensionSet::MutableRawRepeatedField(int, google::protobuf::internal::FieldType, bool, const google::protobuf::FieldDescriptor*)':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:419:12: error: switch missing default case [-Werror=switch-default]
  419 |     switch (WireFormatLite::FieldTypeToCppType(
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'void google::protobuf::internal::ExtensionSet::RemoveLast(int)':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:855:10: error: switch missing default case [-Werror=switch-default]
  855 |   switch (cpp_type(extension->type)) {
      |          ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'void google::protobuf::internal::ExtensionSet::SwapElements(int, int, int)':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:910:10: error: switch missing default case [-Werror=switch-default]
  910 |   switch (cpp_type(extension->type)) {
      |          ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'void google::protobuf::internal::ExtensionSet::InternalExtensionMergeFrom(const google::protobuf::MessageLite*, int, const google::protobuf::internal::ExtensionSet::Extension&, google::protobuf::Arena*)':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1008:12: error: switch missing default case [-Werror=switch-default]
 1008 |     switch (cpp_type(other_extension.type)) {
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1055:14: error: switch missing default case [-Werror=switch-default]
 1055 |       switch (cpp_type(other_extension.type)) {
      |              ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'bool google::protobuf::internal::ExtensionSet::ParseFieldWithExtensionInfo(int, bool, const google::protobuf::internal::ExtensionInfo&, google::protobuf::io::CodedInputStream*, google::protobuf::internal::FieldSkipper*)':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1322:12: error: switch missing default case [-Werror=switch-default]
 1322 |     switch (extension.type) {
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1378:12: error: switch missing default case [-Werror=switch-default]
 1378 |     switch (extension.type) {
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'void google::protobuf::internal::ExtensionSet::Extension::Clear()':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1612:12: error: switch missing default case [-Werror=switch-default]
 1612 |     switch (cpp_type(type)) {
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'size_t google::protobuf::internal::ExtensionSet::Extension::ByteSize(int) const':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1660:14: error: switch missing default case [-Werror=switch-default]
 1660 |       switch (real_type(type)) {
      |              ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1710:14: error: switch missing default case [-Werror=switch-default]
 1710 |       switch (real_type(type)) {
      |              ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1751:12: error: switch missing default case [-Werror=switch-default]
 1751 |     switch (real_type(type)) {
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'int google::protobuf::internal::ExtensionSet::Extension::GetSize() const':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1799:10: error: switch missing default case [-Werror=switch-default]
 1799 |   switch (cpp_type(type)) {      |          ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'void google::protobuf::internal::ExtensionSet::Extension::Free()':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:1825:12: error: switch missing default case [-Werror=switch-default]
 1825 |     switch (cpp_type(type)) {
      |            ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc: In member function 'uint8_t* google::protobuf::internal::ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray(const google::protobuf::MessageLite*, const google::protobuf::internal::ExtensionSet*, int, uint8_t*, google::protobuf::io::EpsCopyOutputStream*) const':
external/com_google_protobuf/src/google/protobuf/extension_set.cc:2026:14: error: switch missing default case [-Werror=switch-default]
 2026 |       switch (real_type(type)) {
      |              ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc:2060:14: error: switch missing default case [-Werror=switch-default]
 2060 |       switch (real_type(type)) {
      |              ^
external/com_google_protobuf/src/google/protobuf/extension_set.cc:2111:12: error: switch missing default case [-Werror=switch-default]
 2111 |     switch (real_type(type)) {
      |            ^
cc1plus: all warnings being treated as errors
Target //main:hello-world failed to build
INFO: Elapsed time: 3.122s, Critical Path: 3.00s
INFO: 18 processes: 9 internal, 9 linux-sandbox.
FAILED: Build did NOT complete successfully

Bazel 将两次构建 protobuf 的部分,once for the host and once for the target。 extension_set.cc 就是其中之一。 --host_crosstool_top 将在主机编译时使用 protoc 以便稍后在构建中成为 运行。在为目标构建相同的文件作为最终二进制文件的一部分时,将使用您的自定义工具链。

您的构建失败是在使用您的自定义工具链为目标构建时。你需要以某种方式让它工作。我经常最终修改 BUILD 文件以添加 -Wno-* 科普特人,以便它们与我的工具链一起工作。 --per_file_copt 可以代替。

如果您只想在设置时构建主机版本,请像这样编写一个简单的 genrule:

genrule(
    name = "abc",
    tools = [ "@com_google_protobuf//:protoc" ],
    outs = [ "dummy" ],
    cmd = "touch $@",
)

如果您 bazel build abc,那么 bazel 将为主机构建 protoc,即使它实际上并未被 cmd.

使用