From b97526048927dc722d0e45de1cec853acc67f9d9 Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Tue, 22 Jun 2021 02:18:49 +0000 Subject: [PATCH] package/go: use host compiler when go-bootstrap unsupported All Go compiler versions > 1.4.x (old) are written in Go, and require a existing compiled Go version to use to build from source. https://golang.org/doc/install/source#bootstrapFromSource The process for "bootstrapping" the Go compiler in Buildroot is: 1. Compile a C/C++ cross-compiler (gcc) as the host toolchain. 2. Build go-bootstrap-stage1 (which is Go 1.4.x and written in C) 3. Build go-bootstrap-stage2 (which is Go 1.19.x and written in Go) 3. Build go 1.20 (written in Go) using go-bootstrap-stage2. go-bootstrap-stage1 does not work on 64-bit arm. The Go 1.4.x bootstrap compiler is compatible with x86, x86_64, and arm (32 bit) only. This patch adds a fallback to require a host Go compiler to build host-go when BR2_PACKAGE_HOST_GO_BOOTSTRAP_STAGE2_ARCH_SUPPORTS is not set. Signed-off-by: Christian Stewart --- changes prior to inclusion in this series: - thanks Thomas for the review & suggestions - added NEEDS_HOST_GO boolean - added dependency checks to support/dependencies/dependencies.sh - removed unnecessary changes to go-bootstrap package - add dependency on toolchain if Cgo is enabled - updates for go1.20 - updates for go-bootstrap-stage{1,2} changes from v1 -> v2: - remove whitespace fix in bootstrap stage2 Signed-off-by: Christian Stewart --- Config.in | 4 ++++ package/go/Config.in.host | 2 +- package/go/go.mk | 14 ++++++++++++-- support/dependencies/dependencies.sh | 4 ++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Config.in b/Config.in index 0d7641633c..c70ce94d94 100644 --- a/Config.in +++ b/Config.in @@ -58,6 +58,10 @@ config BR2_HOST_GCC_AT_LEAST_9 # When adding new entries above, be sure to update # the HOSTCC_MAX_VERSION variable in the Makefile. +# Hidden boolean selected if bootstrapping Go w/ GCC is not supported. +config BR2_NEEDS_HOST_GO + bool + # Hidden boolean selected by packages in need of Java in order to build # (example: kodi) config BR2_NEEDS_HOST_JAVA diff --git a/package/go/Config.in.host b/package/go/Config.in.host index b87b862cec..7f049ff1ae 100644 --- a/package/go/Config.in.host +++ b/package/go/Config.in.host @@ -30,4 +30,4 @@ config BR2_PACKAGE_HOST_GO_TARGET_CGO_LINKING_SUPPORTS config BR2_PACKAGE_HOST_GO_HOST_ARCH_SUPPORTS bool default y - depends on BR2_PACKAGE_HOST_GO_BOOTSTRAP_STAGE2_ARCH_SUPPORTS + select BR2_NEEDS_HOST_GO if !BR2_PACKAGE_HOST_GO_BOOTSTRAP_STAGE2_ARCH_SUPPORTS diff --git a/package/go/go.mk b/package/go/go.mk index 4db703a301..68f6d9e36b 100644 --- a/package/go/go.mk +++ b/package/go/go.mk @@ -12,7 +12,6 @@ GO_LICENSE = BSD-3-Clause GO_LICENSE_FILES = LICENSE GO_CPE_ID_VENDOR = golang -HOST_GO_DEPENDENCIES = host-go-bootstrap-stage2 HOST_GO_GOPATH = $(HOST_DIR)/share/go-path HOST_GO_HOST_CACHE = $(HOST_DIR)/share/host-go-cache HOST_GO_ROOT = $(HOST_DIR)/lib/go @@ -109,6 +108,11 @@ else # !BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS HOST_GO_CGO_ENABLED = 1 endif # BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS +ifeq ($(HOST_GO_CGO_ENABLED),1) +# For cgo support the toolchain needs to be available. +HOST_GO_DEPENDENCIES += toolchain +endif + # For the convenience of host golang packages HOST_GO_HOST_ENV = \ $(HOST_GO_COMMON_ENV) \ @@ -126,7 +130,6 @@ HOST_GO_HOST_ENV = \ HOST_GO_MAKE_ENV = \ GO111MODULE=off \ GOCACHE=$(HOST_GO_HOST_CACHE) \ - GOROOT_BOOTSTRAP=$(HOST_GO_BOOTSTRAP_STAGE2_ROOT) \ GOROOT_FINAL=$(HOST_GO_ROOT) \ GOROOT="$(@D)" \ GOBIN="$(@D)/bin" \ @@ -136,6 +139,13 @@ HOST_GO_MAKE_ENV = \ CGO_ENABLED=$(HOST_GO_CGO_ENABLED) \ $(HOST_GO_CROSS_ENV) +# Use the Go compiler bootstrapped by Buildroot if available. +# Otherwise, use the host Go compiler. +ifeq ($(BR2_PACKAGE_HOST_GO_BOOTSTRAP_STAGE2_ARCH_SUPPORTS),y) +HOST_GO_DEPENDENCIES += host-go-bootstrap-stage2 +HOST_GO_MAKE_ENV += GOROOT_BOOTSTRAP=$(HOST_GO_BOOTSTRAP_STAGE2_ROOT) +endif + define HOST_GO_BUILD_CMDS cd $(@D)/src && \ $(HOST_GO_MAKE_ENV) ./make.bash $(if $(VERBOSE),-v) diff --git a/support/dependencies/dependencies.sh b/support/dependencies/dependencies.sh index 4353e9585e..943c7d0daf 100755 --- a/support/dependencies/dependencies.sh +++ b/support/dependencies/dependencies.sh @@ -217,6 +217,10 @@ if grep -q ^BR2_NEEDS_HOST_UTF8_LOCALE=y $BR2_CONFIG ; then fi fi +if grep -q ^BR2_NEEDS_HOST_GO=y $BR2_CONFIG ; then + check_prog_host "go" +fi + if grep -q ^BR2_NEEDS_HOST_JAVA=y $BR2_CONFIG ; then check_prog_host "java" JAVA_GCJ=$(java -version 2>&1 | grep gcj)