Skip to content

Commit

Permalink
package/go: use host compiler when go-bootstrap unsupported
Browse files Browse the repository at this point in the history
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 (which is Go 1.4.x and written in C)
3. Build go 1.16.x (written in Go) using go-bootstrap.

The problem is that step 2 - build go-bootstrap - does not work on 64-bit arm.
The Go compiler from 1.4.x is compatible with x86, x86_64, and arm (32 bit).

This patch instead uses the host Go compiler to bootstrap host-go when
BR2_PACKAGE_HOST_GO_BOOTSTRAP_ARCH_SUPPORTS is not set. This is similar to how
the host GCC is used to bootstrap the Buildroot toolchain.

Signed-off-by: Christian Stewart <[email protected]>

---

v1 -> v2:

 - 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

v2 -> v3:

 - add dependency on toolchain if Cgo is enabled

Signed-off-by: Christian Stewart <[email protected]>
  • Loading branch information
paralin committed Oct 15, 2022
1 parent 991d5f0 commit b326d50
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions package/go-bootstrap/Config.in.host
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
config BR2_PACKAGE_HOST_GO_BOOTSTRAP_ARCH_SUPPORTS
bool
# See src/cmd/dist/unix.c for the list of support
# architectures
# See src/cmd/dist/unix.c for the list of supported architectures.
default y if BR2_HOSTARCH = "x86"
default y if BR2_HOSTARCH = "x86_64"
default y if BR2_HOSTARCH = "arm"
5 changes: 3 additions & 2 deletions package/go/Config.in.host
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
config BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS
bool
default y
depends on BR2_PACKAGE_HOST_GO_BOOTSTRAP_ARCH_SUPPORTS
# See src/go/build/syslist.go for a list of supported architectures
# See https://go.dev/doc/install/source#environment
depends on (BR2_arm && BR2_TOOLCHAIN_SUPPORTS_PIE) || BR2_aarch64 \
|| BR2_i386 || BR2_x86_64 || BR2_powerpc64le \
|| BR2_mips64 || BR2_mips64el || BR2_riscv || BR2_s390x
Expand All @@ -28,4 +29,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_ARCH_SUPPORTS
select BR2_NEEDS_HOST_GO if !BR2_PACKAGE_HOST_GO_BOOTSTRAP_ARCH_SUPPORTS
14 changes: 12 additions & 2 deletions package/go/go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ GO_LICENSE = BSD-3-Clause
GO_LICENSE_FILES = LICENSE
GO_CPE_ID_VENDOR = golang

HOST_GO_DEPENDENCIES = host-go-bootstrap
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
Expand Down Expand Up @@ -108,6 +107,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) \
Expand All @@ -125,7 +129,6 @@ HOST_GO_HOST_ENV = \
HOST_GO_MAKE_ENV = \
GO111MODULE=off \
GOCACHE=$(HOST_GO_HOST_CACHE) \
GOROOT_BOOTSTRAP=$(HOST_GO_BOOTSTRAP_ROOT) \
GOROOT_FINAL=$(HOST_GO_ROOT) \
GOROOT="$(@D)" \
GOBIN="$(@D)/bin" \
Expand All @@ -135,6 +138,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_ARCH_SUPPORTS),y)
HOST_GO_DEPENDENCIES += host-go-bootstrap
HOST_GO_MAKE_ENV += GOROOT_BOOTSTRAP=$(HOST_GO_BOOTSTRAP_ROOT)
endif

define HOST_GO_BUILD_CMDS
cd $(@D)/src && \
$(HOST_GO_MAKE_ENV) ./make.bash $(if $(VERBOSE),-v)
Expand Down
4 changes: 4 additions & 0 deletions support/dependencies/dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ if grep ^BR2_NEEDS_HOST_UTF8_LOCALE=y $BR2_CONFIG > /dev/null; 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)
Expand Down

0 comments on commit b326d50

Please sign in to comment.