From d497c47dbb0c22ad99e4d7f99b41a5bd57eb1fcb Mon Sep 17 00:00:00 2001 From: Daniel P. Berrangé Date: Sep 18 2019 15:57:57 +0000 Subject: [PATCH 1/2] macros: remove arch conditional for setting pie flag The use of the %ifnarch conditional to control use of the '-buildmode pie' flag presents a problem for future patches which aim to allow Go build flags to be assigned to an shell env variable. There is no way to get rid of the leading and trailing newlines as %ifnarch must be on a separate line from the %{expand: clause. To eliminate this conditional, create two new files, macros.go-compilers-golang-{pie,nopie} which both define a %gocompilerflags macro. Only one of the two files should be installed, depending on whether the build architecture supports pie or not. Signed-off-by: Daniel P. Berrangé --- diff --git a/rpm/macros.d/macros.go-compilers-golang b/rpm/macros.d/macros.go-compilers-golang index 5b421d3..b928068 100644 --- a/rpm/macros.d/macros.go-compilers-golang +++ b/rpm/macros.d/macros.go-compilers-golang @@ -25,17 +25,12 @@ %gobuild(o:) %{expand: # https://bugzilla.redhat.com/show_bug.cgi?id=995136#c12 %global _dwz_low_mem_die_limit 0 - %ifnarch ppc64 %{?gobuilddir:GOPATH="%{gobuilddir}:${GOPATH:+${GOPATH}:}%{?gopath}"} GO111MODULE=off \\ - go build -buildmode pie -compiler gc -tags="rpm_crashtraceback ${BUILDTAGS:-}" -ldflags "${LDFLAGS:-}%{?currentgoldflags} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '%__global_ldflags %{?__golang_extldflags}'" -a -v -x %{?**}; - %else - %{?gobuilddir:GOPATH="%{gobuilddir}:${GOPATH:+${GOPATH}:}%{?gopath}"} GO111MODULE=off \\ - go build -compiler gc -tags="rpm_crashtraceback ${BUILDTAGS:-}" -ldflags "${LDFLAGS:-}%{?currentgoldflags} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '%__global_ldflags %{?__golang_extldflags}'" -a -v -x %{?**}; - %endif + go build %{?gocompilerflags} -tags="rpm_crashtraceback ${BUILDTAGS:-}" -ldflags "${LDFLAGS:-}%{?currentgoldflags} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '%__global_ldflags %{?__golang_extldflags}'" -a -v -x %{?**}; } ${workroot}${GOPATH:+:${GOPATH}} # Define commands for testing -%gotestflags -buildmode pie -compiler gc +%gotestflags %{gocompilerflags} %gotestextldflags %__global_ldflags %{?__golang_extldflags} %gotest() GO111MODULE=off go test %{gotestflags} -ldflags "${LDFLAGS:-}%{?currentgoldflags} -extldflags '%{gotestextldflags}'" %{?**}; diff --git a/rpm/macros.d/macros.go-compilers-golang-nopie b/rpm/macros.d/macros.go-compilers-golang-nopie new file mode 100644 index 0000000..a221a7c --- /dev/null +++ b/rpm/macros.d/macros.go-compilers-golang-nopie @@ -0,0 +1,2 @@ + +%gocompilerflags -compiler gc diff --git a/rpm/macros.d/macros.go-compilers-golang-pie b/rpm/macros.d/macros.go-compilers-golang-pie new file mode 100644 index 0000000..a190dea --- /dev/null +++ b/rpm/macros.d/macros.go-compilers-golang-pie @@ -0,0 +1,2 @@ + +%gocompilerflags -buildmode pie -compiler gc From 67b4fbbbfce0986ac46cd1329bf85a18ea7a43d2 Mon Sep 17 00:00:00 2001 From: Daniel P. Berrangé Date: Sep 19 2019 10:36:33 +0000 Subject: [PATCH 2/2] macros: define a %gobuildflags macro Using the %gobuild macro is fine for a project where the go code is the only thing being built, and can be built directly by invoking the Go toolchain from RPM. In more complex cases though, the Go code is just a small part of the project and the Go toolchain is invoked by a build system such as make (possibly automake), or meson. In such a case we need to be able to tell this build system what flags to pass to the compiler. The %gobuildflags macros services this purpose allowing a RPM spec todo GOBUILDFLAGS="%gobuildflags" %configure or %make GOBUILDFLAGS="%gobuildflags" Ideally the %gobuild macro would in turn reference the %gobuildflags macro, but that does not appear possible given the semantics around quote expansion and escaping across RPM and shell. Signed-off-by: Daniel P. Berrangé --- diff --git a/rpm/macros.d/macros.go-compilers-golang b/rpm/macros.d/macros.go-compilers-golang index b928068..8df6b73 100644 --- a/rpm/macros.d/macros.go-compilers-golang +++ b/rpm/macros.d/macros.go-compilers-golang @@ -20,6 +20,17 @@ # # SPDX-License-Identifier: GPL-3.0-or-later +# This *must* be all on one line, as it will be used in shell +# assignments. eg +# +# GOBUILDFLAGS="%gobuildflags" %configure +# +# Or +# +# %make GOBUILDFLAGS="%gobuildflags" +# +%gobuildflags() %{expand:%{gocompilerflags} -tags=\\"rpm_crashtraceback \\" -ldflags \\"${LDFLAGS:-}%{?currentgoldflags} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '%__global_ldflags %{?__golang_extldflags}'\\" -a -v -x} + # Define commands for building # BUILD_ID can be generated for golang build no matter of debuginfo %gobuild(o:) %{expand: