Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross compilation error when import both c toolchain and rules_go #12859

Closed
zhlhahaha opened this issue Jan 20, 2021 · 7 comments
Closed

cross compilation error when import both c toolchain and rules_go #12859

zhlhahaha opened this issue Jan 20, 2021 · 7 comments
Labels
more data needed P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Configurability platforms, toolchains, cquery, select(), config transitions type: support / not a bug (process)

Comments

@zhlhahaha
Copy link

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.
bazel-contrib/rules_go#2089

@zhlhahaha
Copy link
Author

cc @rmohr

@zhlhahaha
Copy link
Author

Here is some update for this issue.

  1. the root cause for this is
    ToolchainResolution: Type @bazel_tools//tools/cpp:toolchain_type: target platform @io_bazel_rules_arm64: No toolchains found.
  2. It seems cpp toolchain is not works with platform, which is found from following issue
    Platform/toolchain support for C++ rules #6516
  3. Following document may help, still doing investigation on this
    https://github.com/bazelbuild/bazel/blob/de98177964d24e558ca9a2b3ef57d2dfcad5540b/site/docs/integrating-with-rules-cc.md

@gregestren
Copy link
Contributor

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

@katre
Copy link
Member

katre commented 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.

@katre
Copy link
Member

katre commented Jan 26, 2021

Looking further, looks like coral is already writing toolchain targets, you just need to register them, with register_toolchains("@crosstool//:all" in your WORKSPACE. At that point the build using --platforms should work.

@zhlhahaha
Copy link
Author

Thanks @katre , the explanation is pretty clear. I will give it a try and write down solution here in case other guys meet same problem and look for solution. Then I will close this issue.

@oquenchil oquenchil added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Configurability platforms, toolchains, cquery, select(), config transitions type: support / not a bug (process) more data needed labels Feb 4, 2021
@aiuto
Copy link
Contributor

aiuto commented Oct 6, 2021

Closing on staleness.

@aiuto aiuto closed this as completed Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more data needed P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Configurability platforms, toolchains, cquery, select(), config transitions type: support / not a bug (process)
Projects
None yet
Development

No branches or pull requests

5 participants