From 0d3ec704b056bb75233bcccf1ab6bac7b63d720d Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Thu, 21 Mar 2019 04:26:10 +0800 Subject: [PATCH] golang: Add support for setting gcflags/ldflags from package Makefile This adds several variables for Go package Makefiles: * GO_PKG_GCFLAGS - go tool compile arguments * GO_PKG_LDFLAGS - go tool link arguments * GO_PKG_LDFLAGS_X - go tool link -X definitions Settings these will add the corresponding flags to the go install command line. (Other command line arguments can still be added by passing them as the first argument to GoPackage/Build/Compile.) This also adds Go's runtime environment variables (GOGC, GOMAXPROCS, GOTRACEBACK) to the unexport list. Signed-off-by: Jeffery To --- lang/golang/golang-package.mk | 37 +++++++++++++++++++++++++++++++---- lang/golang/golang-values.mk | 3 ++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lang/golang/golang-package.mk b/lang/golang/golang-package.mk index 887d54dad..7522a9f4f 100644 --- a/lang/golang/golang-package.mk +++ b/lang/golang/golang-package.mk @@ -76,6 +76,28 @@ include $(GO_INCLUDE_DIR)/golang-values.mk # not necessary. # # e.g. GO_PKG_GO_GENERATE:=1 +# +# +# GO_PKG_GCFLAGS - list of arguments, default empty +# +# Additional go tool compile arguments to use when building targets. +# +# e.g. GO_PKG_GCFLAGS:=-N -l +# +# +# GO_PKG_LDFLAGS - list of arguments, default empty +# +# Additional go tool link arguments to use when building targets. +# +# e.g. GO_PKG_LDFLAGS:=-s -w +# +# +# GO_PKG_LDFLAGS_X - list of string variable definitions, default empty +# +# Each definition will be passed as the parameter to the -X go tool +# link argument, i.e. -ldflags "-X importpath.name=value" +# +# e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH) # Credit for this package build process (GoPackage/Build/Configure and # GoPackage/Build/Compile) belong to Debian's dh-golang completely. @@ -247,18 +269,25 @@ define GoPackage/Build/Compile if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \ echo "Building targets" ; \ case $(GO_ARCH) in \ - arm) installsuffix="-installsuffix v$(GO_ARM)" ;; \ - mips|mipsle) installsuffix="-installsuffix $(GO_MIPS)" ;; \ - mips64|mips64le) installsuffix="-installsuffix $(GO_MIPS64)" ;; \ + arm) installsuffix="v$(GO_ARM)" ;; \ + mips|mipsle) installsuffix="$(GO_MIPS)" ;; \ + mips64|mips64le) installsuffix="$(GO_MIPS64)" ;; \ esac ; \ trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \ ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \ + pkg_gcflags="$(GO_PKG_GCFLAGS)" ; \ + pkg_ldflags="$(GO_PKG_LDFLAGS)" ; \ + for def in $(GO_PKG_LDFLAGS_X); do \ + pkg_ldflags="$$$$pkg_ldflags -X $$$$def" ; \ + done ; \ go install \ - $$$$installsuffix \ + $$$${installsuffix:+-installsuffix $$$$installsuffix} \ -gcflags "$$$$trimpath" \ -asmflags "$$$$trimpath" \ -ldflags "$$$$ldflags" \ -v \ + $$$${pkg_gcflags:+-gcflags "$$$$pkg_gcflags"} \ + $$$${pkg_ldflags:+-ldflags "$$$$pkg_ldflags"} \ $(1) \ $$$$targets ; \ retval=$$$$? ; \ diff --git a/lang/golang/golang-values.mk b/lang/golang/golang-values.mk index caae21efb..78ad1b3de 100644 --- a/lang/golang/golang-values.mk +++ b/lang/golang/golang-values.mk @@ -13,7 +13,8 @@ include $(GO_INCLUDE_DIR)/golang-version.mk unexport \ - GOARCH GOBIN GOCACHE GODEBUG GOFLAGS GOHOSTARCH GOOS GOPATH GORACE GOROOT GOTMPDIR GCCGO \ + GOARCH GOBIN GOCACHE GOFLAGS GOHOSTARCH GOOS GOPATH GORACE GOROOT GOTMPDIR GCCGO \ + GOGC GODEBUG GOMAXPROCS GOTRACEBACK \ CGO_ENABLED \ CGO_CFLAGS CGO_CFLAGS_ALLOW CGO_CFLAGS_DISALLOW \ CGO_CPPFLAGS CGO_CPPFLAGS_ALLOW CGO_CPPFLAGS_DISALLOW \