From b928fafab1d9425aae9341806bd2f6178ba1da1f Mon Sep 17 00:00:00 2001
From: Hongxu Jia <hongxu.jia@windriver.com>
Date: Fri, 26 Oct 2018 16:32:50 +0800
Subject: [PATCH] use GOBUILDMODE to set buildmode

While building go itself, the go build system does not support
to set `-buildmode=pie' from environment.

Add GOBUILDMODE to support it which make PIE executables the default
build mode, as PIE executables are required as of Yocto

Refers: https://groups.google.com/forum/#!topic/golang-dev/gRCe5URKewI
Upstream-Status: Denied [upstream choose antoher solution: `17a256b
cmd/go: -buildmode=pie for android/arm']

Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 src/cmd/go/internal/work/build.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go
index b276de5..1683e7e 100644
--- a/src/cmd/go/internal/work/build.go
+++ b/src/cmd/go/internal/work/build.go
@@ -211,7 +211,11 @@ func AddBuildFlags(cmd *base.Command) {
 
 	cmd.Flag.Var((*base.StringsFlag)(&buildAsmflags), "asmflags", "")
 	cmd.Flag.Var(buildCompiler{}, "compiler", "")
-	cmd.Flag.StringVar(&cfg.BuildBuildmode, "buildmode", "default", "")
+	if bm := os.Getenv("GOBUILDMODE"); bm != "" {
+		cmd.Flag.StringVar(&cfg.BuildBuildmode, "buildmode", bm, "")
+	} else {
+		cmd.Flag.StringVar(&cfg.BuildBuildmode, "buildmode", "default", "")
+	}
 	cmd.Flag.Var((*base.StringsFlag)(&buildGcflags), "gcflags", "")
 	cmd.Flag.Var((*base.StringsFlag)(&buildGccgoflags), "gccgoflags", "")
 	cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "")
-- 
2.7.4

