My goal here is simple: I want a custom c toolchain where I can specify my own compilers and compiler options. I have boiled down my current issue into a contrived example repo here: http://github.com/btmcg/bazel_toolchain
I followed the tutorial described at https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html to use my custom built version of gcc. This seems to work okay until I add a cc_proto_library. Bazel appears to use my custom toolchain to build protoc/zlib and errors out because of the warning flags I have in my custom toolchain. I was under the impression that --host_crosstool_top=@bazel_tools//tools/cpp:toolchain would tell Bazel to ignore my custom toolchain, however that doesn't seem to be the case. I would expect it to use /bin/gcc and not my custom gcc or warnings. Any help would be appreciated
Here is where the build currently fails:
$ 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
CodePudding user response:
Bazel is going to build parts of protobuf twice, once for the host and once for the target. extension_set.cc is one of those. --host_crosstool_top will be used when compiling for the host to build protoc to be run later in the build. Your custom toolchain will be used when building those same files for the target as part of the final binary.
Your build failure is when building for the target with your custom toolchain. You'll need to make that work somehow. I often end up modifying BUILD files to add -Wno-* copts so they work with my toolchains. --per_file_copt could be used instead.
If you want to just build the host version while setting things up, write a simple genrule like this:
genrule(
name = "abc",
tools = [ "@com_google_protobuf//:protoc" ],
outs = [ "dummy" ],
cmd = "touch $@",
)
If you bazel build abc, then bazel will build protoc for the host, even though it's not actually used by the cmd.
