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-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 <christian@paral.in>

---

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 <christian@paral.in>
  • Loading branch information
paralin committed Feb 15, 2023
1 parent 1d79fba commit b975260
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 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
2 changes: 1 addition & 1 deletion package/go/Config.in.host
Original file line number Diff line number Diff line change
Expand Up @@ -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
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-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
Expand Down Expand Up @@ -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) \
Expand All @@ -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" \
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions support/dependencies/dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b975260

Please sign in to comment.