golang: Add support for setting gcflags/ldflags from package Makefile
authorJeffery To <redacted>
Wed, 20 Mar 2019 20:26:10 +0000 (04:26 +0800)
committerYousong Zhou <redacted>
Thu, 21 Mar 2019 02:10:21 +0000 (10:10 +0800)
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 <redacted>
lang/golang/golang-package.mk
lang/golang/golang-values.mk

index 887d54dadc522956dc8fd6892775c476f58c4ae0..7522a9f4fa6f85e86cb99631d6f22246ff8b2915 100644 (file)
@@ -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=$$$$? ; \
index caae21efb7a4faf724f951401c6b75044a14f7f2..78ad1b3de6d540e0e068b16e29b0952aff114679 100644 (file)
@@ -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 \
git clone https://git.99rst.org/PROJECT