We should set this together with the other environment variables,
especially as -buildmode=pie is arch-specific. We now set that based on
the tuple rather than USE flags.

Parallelism is now set via GOMAXPROCS instead of GOFLAGS. This allows
users to specify their own value, which they may want to do because Go
doesn't currently support the GNU Make jobserver.

Signed-off-by: James Le Cuirot <[email protected]>
---
 eclass/go-env.eclass    | 32 ++++++++++++++++++++++++++++++--
 eclass/go-module.eclass | 15 +--------------
 2 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/eclass/go-env.eclass b/eclass/go-env.eclass
index 548c59e69f1d..c13b4c552e19 100644
--- a/eclass/go-env.eclass
+++ b/eclass/go-env.eclass
@@ -12,6 +12,17 @@
 # This eclass includes helper functions for setting the compile environment 
for Go ebuilds.
 # Intended to be called by other Go eclasses in an early build stage, e.g. 
src_unpack.

+# @ECLASS_VARIABLE: GOMAXPROCS
+# @USER_VARIABLE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# The maximum number of processes for the Go runtime to run in parallel. See
+# https://pkg.go.dev/runtime#GOMAXPROCS. If unset, this defaults to the
+# configured number of Make jobs. Unfortunately, Go does not currently support
+# the GNU Make jobserver, so this may not play nicely alongside other build
+# processes. However, Go code is often built without a supporting build system
+# or without other non-Go code, so this should be sufficient in most cases.
+
 case ${EAPI} in
        7|8) ;;
        *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
@@ -20,7 +31,7 @@ esac
 if [[ -z ${_GO_ENV_ECLASS} ]]; then
 _GO_ENV_ECLASS=1

-inherit flag-o-matic toolchain-funcs
+inherit flag-o-matic multiprocessing toolchain-funcs

 # @FUNCTION: go-env_set_compile_environment
 # @DESCRIPTION:
@@ -35,7 +46,24 @@ inherit flag-o-matic toolchain-funcs
 go-env_set_compile_environment() {
        tc-export AR CC CXX FC PKG_CONFIG

-       export GOARCH=$(go-env_goarch)
+       # Go uses all cores by default. Use the configured number of Make jobs, 
but
+       # respect the user value, as described above.
+       : "${GOMAXPROCS=$(get_makeopts_jobs)}"
+       export GOMAXPROCS
+
+       # The following GOFLAGS should be used for all builds.
+       # -x prints commands as they are executed
+       # -v prints the names of packages as they are compiled
+       # -modcacherw makes the build cache read/write
+       # -buildvcs=false omits version control information
+       # -buildmode=pie builds position independent executables
+       export \
+               GOFLAGS="-x -v -modcacherw -buildvcs=false" \
+               GOARCH=$(go-env_goarch)
+
+       case ${GOARCH} in
+               386|amd64|arm*|ppc64le|s390*) GOFLAGS+=" -buildmode=pie" ;;
+       esac

        case ${GOARCH} in
                386) export GO386=$(go-env_go386) ;;
diff --git a/eclass/go-module.eclass b/eclass/go-module.eclass
index 2399fb56e9a9..5294f50d8ad8 100644
--- a/eclass/go-module.eclass
+++ b/eclass/go-module.eclass
@@ -68,7 +68,7 @@ esac
 if [[ -z ${_GO_MODULE_ECLASS} ]]; then
 _GO_MODULE_ECLASS=1

-inherit multiprocessing toolchain-funcs go-env
+inherit toolchain-funcs go-env

 if [[ ! ${GO_OPTIONAL} ]]; then
        BDEPEND=">=dev-lang/go-1.20:="
@@ -93,14 +93,6 @@ export GOCACHE="${T}/go-build"
 # See "go help environment" for information on this setting
 export GOMODCACHE="${WORKDIR}/go-mod"

-# The following go flags should be used for all builds.
-# -buildmode=pie builds position independent executables
-# -buildvcs=false omits version control information
-# -modcacherw makes the build cache read/write
-# -v prints the names of packages as they are compiled
-# -x prints commands as they are executed
-export GOFLAGS="-buildvcs=false -modcacherw -v -x"
-
 # Do not complain about CFLAGS etc since go projects do not use them.
 QA_FLAGS_IGNORED='.*'

@@ -362,11 +354,6 @@ go-module_setup_proxy() {
 # 3. Otherwise, call 'ego mod verify' and then do a normal unpack.
 # Set compile env via go-env.
 go-module_src_unpack() {
-       if use amd64 || use arm || use arm64 ||
-               ( use ppc64 && [[ $(tc-endian) == "little" ]] ) || use s390 || 
use x86; then
-                       GOFLAGS="-buildmode=pie ${GOFLAGS}"
-       fi
-       GOFLAGS="${GOFLAGS} -p=$(makeopts_jobs)"
        if [[ "${#EGO_SUM[@]}" -gt 0 ]]; then
                eqawarn "QA Notice: This ebuild uses EGO_SUM which is 
deprecated"
                eqawarn "Please migrate to a dependency tarball"
--
2.53.0

Reply via email to