question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cross compilation error when import both c toolchain and rules_go

See original GitHub issue

Description of the problem / feature request:

I wirte a test case for cross compile C file for ARM64 on x86_64 and it works well. However After I put rules go in WORKSPACE, and build the C file again. It shows

# bazel build --config aarch64 //example:hello
INFO: SHA256 (https://golang.org/dl/?mode=json&include=all) = c90367386219502d1d2f3c7fa5825884166e6fc2b260f95d3bb85c557aeaf10e
ERROR: While resolving toolchains for target //example:hello: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.
ERROR: Analysis of target '//example:hello' failed; build aborted: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.
INFO: Elapsed time: 4.022s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (13 packages loaded, 99 targets configured)

Following is my WORKSPACE and .bazelrc WORKSPACE

workspace(name = "bazel_cc_toolchains")
load(
    "@bazel_tools//tools/build_defs/repo:http.bzl",
    "http_archive",
)

http_archive(
    name = "coral_crosstool",
    sha256 = "088ef98b19a45d7224be13636487e3af57b1564880b67df7be8b3b7eee4a1bfc",
    strip_prefix = "crosstool-142e930ac6bf1295ff3ba7ba2b5b6324dfb42839",
    urls = [
        "https://github.com/google-coral/crosstool/archive/142e930ac6bf1295ff3ba7ba2b5b6324dfb42839.tar.gz",
    ],
)

load("@coral_crosstool//:configure.bzl", "cc_crosstool")

cc_crosstool(name = "crosstool")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "7904dbecbaffd068651916dce77ff3437679f9d20e1a7956bff43826e7645fcc",
    urls = [
        "https://github.com/bazelbuild/rules_go/releases/download/v0.25.1/rules_go-v0.25.1.tar.gz",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains(version = "1.15.6")

.bazelrc

build:aarch64 --host_crosstool_top=@crosstool//:toolchains --crosstool_top=@crosstool//:toolchains
build:aarch64 --compiler=gcc --cpu=aarch64
build:aarch64 --platforms=@io_bazel_rules_go//go/toolchain:linux_arm64

Bugs: what’s the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Following https://github.com/google-coral/crosstool to apply cross compile toolchain for C/C++ on x86 Following https://github.com/bazelbuild/rules_go to add rule_go into WORKSPACE Use bazel to build a c file.

What operating system are you running Bazel on?

Ubuntu Linux ubuntu 5.9.0+ #5 SMP Thu Nov 12 07:40:27 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

What’s the output of bazel info release?

release 3.7.2

Have you found anything relevant by searching the web?

There are some discuss for same issue in following link, but no answer in it. https://github.com/bazelbuild/rules_go/issues/2089

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
katrecommented, Jan 26, 2021

This is an interaction between rules_go, which uses toolchain resolution, and rules_cc, which currently does not. For Bazel, the cc toolchains that are autogenerated by tools/cpp/cc_configure.bzl are only for the host platform. I haven’t looked into the coral-crosstool you are using, but I suspect that this is creating the legacy cc_toolchain_suite target, but isn’t actually creating toolchain entries and using register_toolchains to make Bazel aware of them.

So, to fix your issue, you need to do the following:

  1. Update coral-crosstool to create toolchain targets and call register_toolchains in their cc_crosstool macro.
  2. Always pass --incompatible_enable_cc_toolchain_resolution, so that the toolchain entries are used. You can add this to a bazelrc to make it simpler.
  3. Instead of using --crosstool_top, --host_crosstool_top, and --cpu, always use --platforms to specify your target platform. a. You may want to write a custom platform target in your project, instead of relying on the one from io_bazel_rules_go, just to ensure you have control over what your target platfom is.

I don’t know who is behind coral-crosstool, but the work they need to do isn’t difficult, and is very similar to the current toolchain targets generated by Bazel in tools/cpp/BUILD.toolchains.tpl (although the specific target constraints will change based on the compiler, obviously). The registration for Bazel’s cc toolchains is in tools/cpp/cc_configure.bzl:186.

This is not a Bazel failure, per se.

1reaction
gregestrencommented, Jan 26, 2021

@katre - at a super-quick glance, does this look like the actual toolchain is missing? Which group seems best to route this to?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with cross compilation - Stack Overflow
I would like to compile a simple test program in c with an ARM toolchain. My Makefile is as follows: CC=/project_path/arm-linux-uclibcgnueabi/ ...
Read more >
Using Zig As Cross Platform C Toolchain - Ruoyu Sun
Currently, I do not use a cross-platform compilation toolchain ... There are two main approaches to import C code: @cImport and zig ...
Read more >
Cross-compilation in Rust - Sylvain Kerkour
To increase the number of potential targets, we are going to use cross-compilation: we will compile a program from a Host Operating System ......
Read more >
Tag Archives: cross compilation - Dave Cheney
It is currently not possible to produce a cgo enabled binary when cross compiling from one operating system to another. This is because...
Read more >
C / C++ IDE (CDT) » Adding new "toolchain" to Eclipse.
works to compile C or C++ code on my Pi Zero with Raspian Jessie. ... I Eclipse terminology - I have can choose...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found