Skip to content

Commit

Permalink
package/go: add support for riscv64 architecture
Browse files Browse the repository at this point in the history
Enable the supported "riscv64" GOARCH.

Add a patch to fix a build failure due to GOARCH leaking into the calls to the
go-bootstrap compiler. Unsets the GOARCH before calling go-bootstrap.

PR: golang/go#52362

Signed-off-by: Christian Stewart <christian@paral.in>

---

v1 -> v2:

 - fixed build error with go-bootstrap

v2 -> v3:

 - go: does not support riscv-32bit
  • Loading branch information
paralin committed Apr 14, 2022
1 parent 93fc062 commit 9de5646
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
@@ -1,4 +1,4 @@
From e1382a731a726293e30901038c6870fa77ef6095 Mon Sep 17 00:00:00 2001
From 86fa3da18b5c73cc84dec91d92db4ae95f288bdc Mon Sep 17 00:00:00 2001
From: Angelo Compagnucci <angelo@amarulasolutions.com>
Date: Tue, 8 May 2018 16:08:44 +0200
Subject: [PATCH] build.go: explicit option for crosscompilation
Expand All @@ -17,10 +17,10 @@ Signed-off-by: Anisse Astier <anisse@astier.eu>
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index 99d1db5..eb4097f 100644
index d37c3f83ef..d838485bd3 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -252,12 +252,13 @@ func xinit() {
@@ -273,12 +273,13 @@ func xinit() {
// $CC_FOR_goos_goarch, if set, applies only to goos/goarch.
func compilerEnv(envName, def string) map[string]string {
m := map[string]string{"": def}
Expand All @@ -36,5 +36,5 @@ index 99d1db5..eb4097f 100644
}
m[""] = env
--
2.7.4
2.35.1

@@ -0,0 +1,111 @@
From 00f3f4ee0d477779c87e4e60b08708362670e122 Mon Sep 17 00:00:00 2001
From: Christian Stewart <christian@paral.in>
Date: Thu, 14 Apr 2022 13:34:26 -0700
Subject: [PATCH] build: bootstrap: set environment before generating buildcfg

The GOOS and GOARCH environment variables should be unset before calling
mkbuildcfg. This change fixes a build failure when GOARCH=riscv64.

Building Go toolchain1 using go-1.4-bootstrap-20171003.
src/cmd/compile/internal/ssa/rewriteRISCV64.go:4814
invalid operation: y << x (shift count type int64, must be unsigned integer)

There is a build issue with go1.4 with the riscv64 code: however, why is the
riscv64 code being compiled at all?

GOARCH is set when calling mkbuildcfg, so go1.4 is trying to compile riscv64.
---
src/cmd/dist/buildtool.go | 56 ++++++++++++++++++++-------------------
1 file changed, 29 insertions(+), 27 deletions(-)

diff --git a/src/cmd/dist/buildtool.go b/src/cmd/dist/buildtool.go
index 036f8c52fa..73f6524969 100644
--- a/src/cmd/dist/buildtool.go
+++ b/src/cmd/dist/buildtool.go
@@ -112,9 +112,6 @@ func bootstrapBuildTools() {
}
xprintf("Building Go toolchain1 using %s.\n", goroot_bootstrap)

- mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
- mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
-
// Use $GOROOT/pkg/bootstrap as the bootstrap workspace root.
// We use a subdirectory of $GOROOT/pkg because that's the
// space within $GOROOT where we store all generated objects.
@@ -126,6 +123,34 @@ func bootstrapBuildTools() {
base := pathf("%s/src/bootstrap", workspace)
xmkdirall(base)

+ // Set up environment for invoking Go 1.4 go command.
+ // GOROOT points at Go 1.4 GOROOT,
+ // GOPATH points at our bootstrap workspace,
+ // GOBIN is empty, so that binaries are installed to GOPATH/bin,
+ // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
+ // so that Go 1.4 builds whatever kind of binary it knows how to build.
+ // Restore GOROOT, GOPATH, and GOBIN when done.
+ // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
+ // because setup will take care of those when bootstrapBuildTools returns.
+
+ defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
+ os.Setenv("GOROOT", goroot_bootstrap)
+
+ defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
+ os.Setenv("GOPATH", workspace)
+
+ defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
+ os.Setenv("GOBIN", "")
+
+ os.Setenv("GOOS", "")
+ os.Setenv("GOHOSTOS", "")
+ os.Setenv("GOARCH", "")
+ os.Setenv("GOHOSTARCH", "")
+
+ // Create the build config files.
+ mkbuildcfg(pathf("%s/src/internal/buildcfg/zbootstrap.go", goroot))
+ mkobjabi(pathf("%s/src/cmd/internal/objabi/zbootstrap.go", goroot))
+
// Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
writefile("module bootstrap\n", pathf("%s/%s", base, "go.mod"), 0)
for _, dir := range bootstrapDirs {
@@ -172,30 +197,6 @@ func bootstrapBuildTools() {
})
}

- // Set up environment for invoking Go 1.4 go command.
- // GOROOT points at Go 1.4 GOROOT,
- // GOPATH points at our bootstrap workspace,
- // GOBIN is empty, so that binaries are installed to GOPATH/bin,
- // and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
- // so that Go 1.4 builds whatever kind of binary it knows how to build.
- // Restore GOROOT, GOPATH, and GOBIN when done.
- // Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
- // because setup will take care of those when bootstrapBuildTools returns.
-
- defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
- os.Setenv("GOROOT", goroot_bootstrap)
-
- defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
- os.Setenv("GOPATH", workspace)
-
- defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
- os.Setenv("GOBIN", "")
-
- os.Setenv("GOOS", "")
- os.Setenv("GOHOSTOS", "")
- os.Setenv("GOARCH", "")
- os.Setenv("GOHOSTARCH", "")
-
// Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to
// workaround bugs in Go 1.4's compiler. See discussion thread:
// https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ
@@ -217,6 +218,7 @@ func bootstrapBuildTools() {
cmd = append(cmd, "-toolexec="+tool)
}
cmd = append(cmd, "bootstrap/cmd/...")
+
run(base, ShowOutput|CheckExit, cmd...)

// Copy binaries into tool binary directory.
--
2.35.1

4 changes: 3 additions & 1 deletion package/go/Config.in.host
Expand Up @@ -5,10 +5,12 @@ config BR2_PACKAGE_HOST_GO_TARGET_ARCH_SUPPORTS
depends on BR2_PACKAGE_HOST_GO_BOOTSTRAP_ARCH_SUPPORTS
depends on (BR2_arm && BR2_TOOLCHAIN_SUPPORTS_PIE) || BR2_aarch64 \
|| BR2_i386 || BR2_x86_64 || BR2_powerpc64le \
|| BR2_mips64 || BR2_mips64el || BR2_s390x
|| BR2_mips64 || BR2_mips64el || BR2_riscv || BR2_s390x
depends on !BR2_ARM_CPU_ARMV4
# MIPS R6 support in Go has not yet been developed.
depends on !BR2_MIPS_CPU_MIPS64R6
# Go doesn't support Risc-v 32-bit.
depends on !BR2_RISCV_32

config BR2_PACKAGE_HOST_GO_TARGET_CGO_LINKING_SUPPORTS
bool
Expand Down
2 changes: 2 additions & 0 deletions package/go/go.mk
Expand Up @@ -63,6 +63,8 @@ else ifeq ($(BR2_mips64),y)
GO_GOARCH = mips64
else ifeq ($(BR2_mips64el),y)
GO_GOARCH = mips64le
else ifeq ($(BR2_riscv),y)
GO_GOARCH = riscv64
else ifeq ($(BR2_s390x),y)
GO_GOARCH = s390x
endif
Expand Down

0 comments on commit 9de5646

Please sign in to comment.