6

I have a python binary that I build with bazel on Mac. Now, I want to create the binary for linux for deployment.

I am following this instruction.

I added

platform(
    name = "linux_x86",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
)

And when built with the --platforms flag it fails

bazel build --platforms=//parser:linux_x86  //parser:parser_bin
ERROR: /private/var/tmp/_bazel_jp/64463e3d7652188cb285edbcf54b686c/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @@bazel_tools//tools/cpp:current_cc_toolchain: 
Traceback (most recent call last):
    File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
    File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 219, column 17, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//parser:linux_x86, Exec platform: @@local_config_platform//:host
ERROR: /private/var/tmp/_bazel_jp/64463e3d7652188cb285edbcf54b686c/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: Analysis of target '//parser:parser_bin' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.127s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

I have a similar setup for my Go code, that worked without any issue. For the two libraries included, the proto library is included in Go as well, so I assume that is not likely an issue.

Complete bazel code.

load("@my_deps//:requirements.bzl", "requirement")

platform(
    name = "linux_x86",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
)

py_library(
    name = "fizz",
    srcs = [
        "BuildAstVisitor.py",
        "FizzLexer.py",
        "FizzParser.py",
        "FizzParserListener.py",
        "FizzParserVisitor.py",
        "PythonLexerBase.py",
        "PythonParserBase.py",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//proto:ast_py_proto",
        requirement("antlr4-python3-runtime"),
    ],
)

py_binary(
    name = "parser_bin",
    srcs = ["main.py"],
    main = "main.py",
    visibility = ["//visibility:public"],
    deps = [":fizz"],
)


If this would help in anyway, when specifying --toolchain_resolution_debug I get

bazel build --platforms=//parser:linux_x86  //parser:parser_bin --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type' 
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform //parser:linux_x86
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-darwin_arm64; mismatching values: aarch64, osx
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-darwin_arm64; mismatching values: aarch64, osx
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-darwin_arm64; mismatching values: osx, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-darwin_arm64; mismatching values: osx, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-darwin_arm64e; mismatching values: osx, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-darwin_arm64e; mismatching values: osx, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-darwin_x86_64; mismatching values: osx, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-darwin_x86_64; mismatching values: osx, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_arm64; mismatching values: ios, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_arm64; mismatching values: ios, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_arm64e; mismatching values: ios, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_arm64e; mismatching values: ios, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_armv7; mismatching values: ios, armv7, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_armv7; mismatching values: ios, armv7, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_i386; mismatching values: ios, i386, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_i386; mismatching values: ios, i386, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_sim_arm64; mismatching values: ios, aarch64, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_sim_arm64; mismatching values: ios, aarch64, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_x86_64; mismatching values: ios, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-ios_x86_64; mismatching values: ios, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-tvos_arm64; mismatching values: tvos, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-tvos_arm64; mismatching values: tvos, aarch64, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-tvos_sim_arm64; mismatching values: tvos, aarch64, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-tvos_sim_arm64; mismatching values: tvos, aarch64, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-tvos_x86_64; mismatching values: tvos, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-tvos_x86_64; mismatching values: tvos, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_arm64; mismatching values: watchos, aarch64, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_arm64; mismatching values: watchos, aarch64, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_arm64_32; mismatching values: watchos, arm64_32, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_arm64_32; mismatching values: watchos, arm64_32, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_armv7k; mismatching values: watchos, armv7k, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_armv7k; mismatching values: watchos, armv7k, device
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_i386; mismatching values: watchos, i386, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_i386; mismatching values: watchos, i386, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_x86_64; mismatching values: watchos, simulator
      ToolchainResolution:   Rejected toolchain @@apple_support~~apple_cc_configure_extension~local_config_apple_cc//:cc-compiler-watchos_x86_64; mismatching values: watchos, simulator
      ToolchainResolution:   Rejected toolchain @@local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@local_config_cc//:cc-compiler-darwin_arm64; mismatching values: aarch64, osx
      ToolchainResolution: No @@bazel_tools//tools/cpp:toolchain_type toolchain found for target platform //parser:linux_x86.
INFO: ToolchainResolution: Target platform //parser:linux_x86: Selected execution platform @@local_config_platform//:host, type @@bazel_tools//tools/python:toolchain_type -> toolchain @@bazel_tools//tools/python:_autodetecting_py_runtime_pair
INFO: ToolchainResolution: Target platform //parser:linux_x86: Selected execution platform @@local_config_platform//:host, 
ERROR: /private/var/tmp/_bazel_jp/64463e3d7652188cb285edbcf54b686c/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @@bazel_tools//tools/cpp:current_cc_toolchain: 
Traceback (most recent call last):
    File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
    File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 219, column 17, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//parser:linux_x86, Exec platform: @@local_config_platform//:host
ERROR: /private/var/tmp/_bazel_jp/64463e3d7652188cb285edbcf54b686c/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: Analysis of target '//parser:parser_bin' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.400s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

1
  • 1
    From what I can tell reading these logs, you're missing a cross-compilation toolchain. The reason you're not having any issues with the Go toolchain is because it has been shipping all supported targets in a single binary ever since version 1.5 so you're effectively running a cross-compatible toolchain no matter the host. Install the cross toolchain for your intended target and the errors should vanish. Commented Sep 4, 2024 at 8:24

2 Answers 2

1

The issue seems to present to the --incompatible_enable_cc_toolchain_resolution flag that was added in Bazel 6 and is enabled by default in Bazel 7.

The built-in Python rules (py_binary in particular) do not seem to handle it correctly when you are also using the --platforms switch to change the target platform.

This is presumably fixed in the new external implementation of Python rules (see https://github.com/bazelbuild/rules_python/issues/1857), but I did not test it as migrating from built-in rules to rules_python is a pain in itself, but probably the best course of action.

If you do not care about cross-compiling C++ code, adding --noincompatible_enable_cc_toolchain_resolution to the project's .bazelrc worked for me and the py_binary has linux_x64 versions of the dependencies downloaded correctly.

Sign up to request clarification or add additional context in comments.

Comments

0

The issue arises because Bazel is unable to find a compatible C++ toolchain for cross-compilation from macOS to Linux. The solution involves configuring the correct toolchains and platforms.

To fix this:

Install the required C++ toolchain for Linux. Set up a cc_toolchain for Linux in your WORKSPACE. Ensure that the appropriate toolchain for the target platform (Linux) is defined and linked to Bazel's toolchain resolution. The Stack Overflow response correctly highlights toolchain mismatch issues.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.