The original idea is taken from this comment
Note:
The configuration used to build a project which use CGO in some of its deps.
Using @io_bazel_rules_go//go/toolchain:linux_amd64_cgo
alone still got an error (no toolchain matched), the custom toolchain
listed here is intended to be used inside docker container running an ubuntu on arm64 platform with build target x86_64.
Usage
On project's WORKSPACE
:
register_toolchains(
"//toolchain:cc-toolchain-linux-cross-64",
)
On project's root BUILD
:
platform(
name = "gcc_cross_64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
parents = [
"@io_bazel_rules_go//go/toolchain:linux_amd64_cgo",
],
)
Add config (for shorter build syntax) .bazelrc
:
build:go_linux_64 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
build:go_linux_64 --host_platform=@local_config_platform//:host
build:go_linux_64 --crosstool_top=//toolchain:cc_linux_cross_64_suite
build:go_linux_64 --cpu=x86_64
build:go_linux_64 --compiler=gcc-cross
build:go_linux_64 --platforms=:gcc_cross_64
Execute the build using bazel build <target> --config=go_linux_64
Disclaimer:
I never test this solution for different projects, this is more of a self-note.