From e86fb5eeddc0e0da1079cf573308476b8f0ef1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Franc=CC=A7ois=20Paris?= Date: Sun, 4 Feb 2018 14:06:13 +0000 Subject: [PATCH] Add support for GOARM parameter when cross compiling Go can cross compile to older ARM architectures (e.g. v5). In order to do so one needs to use the GOARM parameter as documented in https://github.com/golang/go/wiki/GoArm --- build.go | 12 ++++++++++-- doc/020_installation.rst | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/build.go b/build.go index ae82e44ee..bdf708b1b 100644 --- a/build.go +++ b/build.go @@ -193,6 +193,7 @@ func showUsage(output io.Writer) { fmt.Fprintf(output, " --enable-cgo use CGO to link against libc\n") fmt.Fprintf(output, " --goos value set GOOS for cross-compilation\n") fmt.Fprintf(output, " --goarch value set GOARCH for cross-compilation\n") + fmt.Fprintf(output, " --goarm value set GOARM for cross-compilation\n") } func verbosePrintf(message string, args ...interface{}) { @@ -218,13 +219,16 @@ func cleanEnv() (env []string) { } // build runs "go build args..." with GOPATH set to gopath. -func build(cwd, goos, goarch, gopath string, args ...string) error { +func build(cwd, goos, goarch, goarm, gopath string, args ...string) error { a := []string{"build"} a = append(a, "-asmflags", fmt.Sprintf("-trimpath=%s", gopath)) a = append(a, "-gcflags", fmt.Sprintf("-trimpath=%s", gopath)) a = append(a, args...) cmd := exec.Command("go", a...) cmd.Env = append(cleanEnv(), "GOPATH="+gopath, "GOARCH="+goarch, "GOOS="+goos) + if goarm != "" { + cmd.Env = append(cmd.Env, "GOARM="+goarm) + } if !enableCGO { cmd.Env = append(cmd.Env, "CGO_ENABLED=0") } @@ -397,6 +401,7 @@ func main() { targetGOOS := runtime.GOOS targetGOARCH := runtime.GOARCH + targetGOARM := "" var outputFilename string @@ -430,6 +435,9 @@ func main() { case "--goarch": skipNext = true targetGOARCH = params[i+1] + case "--goarm": + skipNext = true + targetGOARM = params[i+1] case "-h": showUsage(os.Stdout) return @@ -514,7 +522,7 @@ func main() { "-o", output, config.Main, } - err = build(filepath.Join(gopath, "src"), targetGOOS, targetGOARCH, gopath, args...) + err = build(filepath.Join(gopath, "src"), targetGOOS, targetGOARCH, targetGOARM, gopath, args...) if err != nil { die("build failed: %v\n", err) } diff --git a/doc/020_installation.rst b/doc/020_installation.rst index 45e24c1f9..82b3f35c2 100644 --- a/doc/020_installation.rst +++ b/doc/020_installation.rst @@ -113,6 +113,8 @@ supply the target OS and platform via the command-line options like this $ go run build.go --goos freebsd --goarch 386 + $ go run build.go --goos linux --goarch arm --goarm 6 + The resulting binary is statically linked and does not require any libraries.