From 6016dad5cfbaa93d4acbf4edcc709d81bb6e5148 Mon Sep 17 00:00:00 2001 From: "Yann E. MORIN" Date: Thu, 1 Aug 2024 10:45:46 +0200 Subject: [PATCH] package/go: expose host CGO linking support Even when configured for cross-compilation, the go compiler is always able to build natively as well, and this is was we use in Buildroot to build host go packages. This implies that when the target has limitations, those limitations thus also apply to the host builds. This means that, when there is no CGO linking support for the target, the compiler is built without CGO linking support, and thus CGO linking is also not available for the host builds. Of course, when there is no go support for the target, the CGO linking support only depends on the host architecture. Add a new Kconfig symbol that repesent whether CGO linking is available for the host; host packages can then depend on that symbol, like the target variants do on the corresponding target symbol. The dependencies of this symbol are a bit complicated. Fortunately, because it is a blind symbol, we can write it with a combination of "default y" and "depends on" statements. As mentioned, CGO for the host is available if CGO is available for the target, but also if Go is not available for the target at all. In addition, Go must of course be available for the host. There are also the toolchain constraints of CGO. We exclude MIPS64 explicitly based on BR2_HOSTARCH. For the host, we always assume that dynamic library and threads are available so we don't have conditions for that. Signed-off-by: Yann E. MORIN Cc: Thomas Perale Cc: Christian Stewart Cc: Anisse Astier Signed-off-by: Arnout Vandecappelle --- docs/manual/adding-packages-golang.adoc | 3 ++- package/go/Config.in.host | 12 ++++++++++++ package/go/go.mk | 7 ++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/manual/adding-packages-golang.adoc b/docs/manual/adding-packages-golang.adoc index 3ddbe57afe..aa25426591 100644 --- a/docs/manual/adding-packages-golang.adoc +++ b/docs/manual/adding-packages-golang.adoc @@ -49,7 +49,8 @@ infrastructure should depend on +BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS+ because Buildroot will automatically add a dependency on +host-go+ to such packages. If you need CGO support in your package, you must add a dependency on -+BR2_PACKAGE_HOST_GO_TARGET_CGO_LINKING_SUPPORTS+. ++BR2_PACKAGE_HOST_GO_TARGET_CGO_LINKING_SUPPORTS+; for host packages, +add a dependency on +BR2_PACKAGE_HOST_GO_HOST_CGO_LINKING_SUPPORTS+. The main macro of the Go package infrastructure is +golang-package+. It is similar to the +generic-package+ macro. The diff --git a/package/go/Config.in.host b/package/go/Config.in.host index 11ce6df369..eefca03b02 100644 --- a/package/go/Config.in.host +++ b/package/go/Config.in.host @@ -36,6 +36,18 @@ config BR2_PACKAGE_HOST_GO_HOST_ARCH_SUPPORTS default y depends on BR2_PACKAGE_HOST_GO_BOOTSTRAP_STAGE3_ARCH_SUPPORTS +# CGO linking for the host. Since we use the same compiler for target +# and host, if the target can't do CGO linking, then the host can't. +# But if the target is not supported by Go, then the host can do CGO +# linking (except when it itself does not support CGO linking). +config BR2_PACKAGE_HOST_GO_HOST_CGO_LINKING_SUPPORTS + bool + default y if BR2_PACKAGE_HOST_GO_TARGET_CGO_LINKING_SUPPORTS + default y if !BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS + depends on BR2_PACKAGE_HOST_GO_HOST_ARCH_SUPPORTS + depends on BR2_HOSTARCH != "mips64" + depends on BR2_HOSTARCH != "mips64el" + # Go packages should select BR2_PACKAGE_HOST_GO config BR2_PACKAGE_HOST_GO bool diff --git a/package/go/go.mk b/package/go/go.mk index 660c7a9bad..7e0dfc5880 100644 --- a/package/go/go.mk +++ b/package/go/go.mk @@ -91,10 +91,11 @@ else HOST_GO_CGO_ENABLED = 0 endif else # !BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS -# If the target arch does not support go, host-go can still be used to build -# packages for the host, and enable cgo. No need to set all the arch stuff -#since we will not be cross-compiling. +ifeq ($(BR2_PACKAGE_HOST_GO_HOST_CGO_LINKING_SUPPORTS),y) HOST_GO_CGO_ENABLED = 1 +else # !BR2_PACKAGE_HOST_GO_HOST_CGO_LINKING_SUPPORTS +HOST_GO_CGO_ENABLED = 0 +endif # BR2_PACKAGE_HOST_GO_HOST_CGO_LINKING_SUPPORTS endif # BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS # Ensure the toolchain is available, whatever the provider HOST_GO_DEPENDENCIES += $(HOST_GO_DEPENDENCIES_CGO)