From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1571841-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id F17BD158099
	for <garchives@archives.gentoo.org>; Mon, 20 Nov 2023 23:27:35 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id E3F8E2BC01D;
	Mon, 20 Nov 2023 23:27:34 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id C7D852BC01D
	for <gentoo-commits@lists.gentoo.org>; Mon, 20 Nov 2023 23:27:34 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id B941A335D4F
	for <gentoo-commits@lists.gentoo.org>; Mon, 20 Nov 2023 23:27:33 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id C41EBA64
	for <gentoo-commits@lists.gentoo.org>; Mon, 20 Nov 2023 23:27:31 +0000 (UTC)
From: "James Le Cuirot" <chewi@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "James Le Cuirot" <chewi@gentoo.org>
Message-ID: <1700522722.878d04daaf34765e6224e58139a9c45921d7a0c3.chewi@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
X-VCS-Repository: repo/gentoo
X-VCS-Files: eclass/go-env.eclass
X-VCS-Directories: eclass/
X-VCS-Committer: chewi
X-VCS-Committer-Name: James Le Cuirot
X-VCS-Revision: 878d04daaf34765e6224e58139a9c45921d7a0c3
X-VCS-Branch: master
Date: Mon, 20 Nov 2023 23:27:31 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 5b68d0be-9dc4-46e9-a768-ed7391843d6a
X-Archives-Hash: 10cb3e1c547baa9be05ee2d214e861d8

commit:     878d04daaf34765e6224e58139a9c45921d7a0c3
Author:     Thilo Fromm <thilo.alexander <AT> gmail <DOT> com>
AuthorDate: Wed Nov  8 07:34:35 2023 +0000
Commit:     James Le Cuirot <chewi <AT> gentoo <DOT> org>
CommitDate: Mon Nov 20 23:25:22 2023 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=878d04da

eclass/go-env.eclass: add helper to set compile env

This change adds a helper function to explicitly set CC, CXX, and
GOARCH, and carrying over CFLAGS, LDFLAGS and friends to CGO
equivalents, to provide a minimal sane compile environment for Go.
It enables Go builds to play nice with crossdev's wrappers for
emerge/ebuild etc. Previously, Go ebuilds emitted binaries for the host
architecture.

For example, when running on an x86_64 host:
   emerge-aarch64-cross-linux-gnu foo
will now correctly emerge Go package "foo" for aarch64 instead of
x86_64.

The eclass provides a single helper function
    go-env_set_compile_environment()
intended to be called by other Go eclasses in an early build stage.
Ebuilds may also explicitly call this function.

Signed-off-by: Thilo Fromm <thilo.alexander <AT> gmail.com>
Signed-off-by: James Le Cuirot <chewi <AT> gentoo.org>

 eclass/go-env.eclass | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/eclass/go-env.eclass b/eclass/go-env.eclass
new file mode 100644
index 000000000000..ba4f6c3fbb59
--- /dev/null
+++ b/eclass/go-env.eclass
@@ -0,0 +1,48 @@
+# Copyright 2023 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: go-env.eclass
+# @MAINTAINER:
+# Flatcar Linux Maintainers <infra@flatcar-linux.org>
+# @AUTHOR:
+# Flatcar Linux Maintainers <infra@flatcar-linux.org>
+# @BLURB: Helper eclass for setting the Go compile environment. Required for cross-compiling.
+# @DESCRIPTION:
+# This eclass includes a helper function 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.
+
+if [[ -z ${_GO_ENV_ECLASS} ]]; then
+_GO_ENV_ECLASS=1
+
+inherit toolchain-funcs
+
+# @FUNCTION: go-env_set_compile_environment
+# @DESCRIPTION:
+# Set up basic compile environment: CC, CXX, and GOARCH.
+# Also carry over CFLAGS, LDFLAGS and friends.
+# Required for cross-compiling with crossdev.
+# If not set, host defaults will be used and the resulting binaries are host arch.
+# (e.g. "emerge-aarch64-cross-linux-gnu foo" run on x86_64 will emerge "foo" for x86_64
+#  instead of aarch64)
+go-env_set_compile_environment() {
+	local arch="$(tc-arch)"
+	case "${arch}" in
+		x86)	GOARCH="386" ;;
+		x64-*)	GOARCH="amd64" ;;
+		ppc64)	if [[ "$(tc-endian)" == "big" ]] ; then
+					GOARCH="ppc64"
+				else
+					GOARCH="ppc64le"
+				fi ;;
+			*)	GOARCH="${arch}" ;;
+	esac
+
+	tc-export CC CXX
+	export GOARCH
+	export CGO_CFLAGS="${CGO_CFLAGS:-$CFLAGS}"
+	export CGO_CPPFLAGS="${CGO_CPPFLAGS:-$CPPFLAGS}"
+	export CGO_CXXFLAGS="${CGO_CXXFLAGS:-$CXXFLAGS}"
+	export CGO_LDFLAGS="${CGO_LDFLAGS:-$LDFLAGS}"
+}
+
+fi