From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 4A4C215823F for ; Sun, 19 Nov 2023 12:34:59 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 815EC2BC044; Sun, 19 Nov 2023 12:34:32 +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)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 1E40A2BC03E for ; Sun, 19 Nov 2023 12:34:32 +0000 (UTC) From: James Le Cuirot To: gentoo-dev Cc: Thilo Fromm , James Le Cuirot Subject: [gentoo-dev] [PATCH 1/4] eclass/go-env.eclass: add helper to set compile env Date: Sun, 19 Nov 2023 12:30:44 +0000 Message-ID: <20231119123410.17748-2-chewi@gentoo.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231119123410.17748-1-chewi@gentoo.org> References: <20231119123410.17748-1-chewi@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Archives-Salt: ab85639f-1628-4a87-ab1b-1acc3fa49faf X-Archives-Hash: 756ad63f7344ad870a70bbee630e6a81 From: Thilo Fromm 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 Signed-off-by: James Le Cuirot --- eclass/go-env.eclass | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 eclass/go-env.eclass 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 +# @AUTHOR: +# Flatcar Linux Maintainers +# @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 -- 2.42.1