portage-stable/eclass: add Go cross-comile helper eclass

This change allows cross-compiling of Go packages and will be upstreamed.

Signed-off-by: Thilo Fromm <thilofromm@microsoft.com>
This commit is contained in:
Thilo Fromm 2023-10-26 16:04:03 +02:00
parent 7e493da5fc
commit 4b8f919bd1
5 changed files with 148 additions and 85 deletions

View File

@ -0,0 +1,46 @@
# Copyright 2023 Flatcar Maintainers
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: go-env.eclass
# @MAINTAINER:
# Flatcar Maintainers <infra@flatcar.org>
# @AUTHOR:
# Flatcar Maintainers <infra@flatcar.org>
# @SUPPORTED_EAPIS: 7 8
# @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
# Set up basic compile environment: CC, CXX, and GOARCH.
# 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() {
_arch() {
local arch=$(tc-arch "${CHOST}}")
case "${arch}" in
x86) echo "386" ;;
x64-*) echo "amd64" ;;
ppc64) if [[ "$(tc-endian "${${CHOST}}")" = "big" ]] then
echo "ppc64"
else
echo "ppc64le"
fi ;;
*) echo "${arch}" ;;
esac
}
CC="$(tc-getCC)"
CXX="$(tc-getCXX)"
# Needs explicit export to arrive in go environment.
export GOARCH="$(_arch)"
}
fi

View File

@ -1,4 +1,4 @@
# Copyright 2019-2022 Gentoo Authors
# Copyright 2019-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: go-module.eclass
@ -26,7 +26,9 @@
# If the software has a directory named vendor in its
# top level directory, the only thing you need to do is inherit the
# eclass. If it doesn't, you need to also create a dependency tarball and
# host it somewhere, for example in your dev space.
# host it somewhere, for example in your dev space. It's recommended that
# a format supporting parallel decompression is used and developers should
# use higher levels of compression like '-9' for xz.
#
# Here is an example of how to create a dependency tarball.
# The base directory in the GOMODCACHE setting must be go-mod in order
@ -36,7 +38,7 @@
#
# $ cd /path/to/project
# $ GOMODCACHE="${PWD}"/go-mod go mod download -modcacherw
# $ tar -acf project-1.0-deps.tar.xz go-mod
# $ XZ_OPT='-T0 -9' tar -acf project-1.0-deps.tar.xz go-mod
#
# @CODE
#
@ -63,20 +65,19 @@ case ${EAPI} in
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
if [[ -z ${_GO_MODULE} ]]; then
if [[ -z ${_GO_MODULE_ECLASS} ]]; then
_GO_MODULE_ECLASS=1
_GO_MODULE=1
inherit multiprocessing toolchain-funcs go-env
if [[ ! ${GO_OPTIONAL} ]]; then
BDEPEND=">=dev-lang/go-1.16"
BDEPEND=">=dev-lang/go-1.18"
# Workaround for pkgcheck false positive: https://github.com/pkgcore/pkgcheck/issues/214
# MissingUnpackerDep: version ...: missing BDEPEND="app-arch/unzip"
# Added here rather than to each affected package, so it can be cleaned up just
# once when pkgcheck is improved.
BDEPEND+=" app-arch/unzip"
EXPORT_FUNCTIONS src_unpack
fi
# Force go to build in module mode.
@ -93,10 +94,12 @@ export GOCACHE="${T}/go-build"
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="-modcacherw -v -x"
export GOFLAGS="-buildvcs=false -modcacherw -v -x"
# Do not complain about CFLAGS etc since go projects do not use them.
QA_FLAGS_IGNORED='.*'
@ -104,8 +107,8 @@ QA_FLAGS_IGNORED='.*'
# Go packages should not be stripped with strip(1).
RESTRICT+=" strip"
# @ECLASS-VARIABLE: EGO_SUM
# @DEPRECATED:
# @ECLASS_VARIABLE: EGO_SUM
# @DEPRECATED: none
# @DESCRIPTION:
# This is replaced by a dependency tarball, see above for how to create
# one.
@ -152,8 +155,8 @@ RESTRICT+=" strip"
# This decision does NOT weaken Go module security, as Go will verify the
# go.sum copy of the Hash1 values during building of the package.
# @ECLASS-VARIABLE: _GOMODULE_GOPROXY_BASEURI
# @DEPRECATED:
# @ECLASS_VARIABLE: _GOMODULE_GOPROXY_BASEURI
# @DEPRECATED: none
# @DESCRIPTION:
# Golang module proxy service to fetch module files from. Note that the module
# proxy generally verifies modules via the Hash1 code.
@ -175,15 +178,15 @@ RESTRICT+=" strip"
# below, so please do not rely on it.
: "${_GOMODULE_GOPROXY_BASEURI:=mirror://goproxy/}"
# @ECLASS-VARIABLE: _GOMODULE_GOSUM_REVERSE_MAP
# @DEPRECATED:
# @ECLASS_VARIABLE: _GOMODULE_GOSUM_REVERSE_MAP
# @DEPRECATED: none
# @DESCRIPTION:
# Mapping back from Gentoo distfile name to upstream distfile path.
# Associative array to avoid O(N*M) performance when populating the GOPROXY
# directory structure.
declare -A -g _GOMODULE_GOSUM_REVERSE_MAP
# @ECLASS-VARIABLE: GO_OPTIONAL
# @ECLASS_VARIABLE: GO_OPTIONAL
# @DEFAULT_UNSET
# @PRE_INHERIT
# @DESCRIPTION:
@ -206,7 +209,7 @@ ego() {
}
# @FUNCTION: go-module_set_globals
# @DEPRECATED:
# @DEPRECATED: none
# @DESCRIPTION:
# Convert the information in EGO_SUM for other usage in the ebuild.
# - Populates EGO_SUM_SRC_URI that can be added to SRC_URI
@ -259,7 +262,22 @@ go-module_set_globals() {
continue
fi
_dir=$(_go-module_gomod_encode "${module}")
# Encode the name(path) of a Golang module in the format expected by Goproxy.
# Upper letters are replaced by their lowercase version with a '!' prefix.
# The transformed result of 'module' is stored in the '_dir' variable.
#
## Python:
# return re.sub('([A-Z]{1})', r'!\1', s).lower()
## Sed:
## This uses GNU Sed extension \l to downcase the match
# echo "${module}" |sed 's,[A-Z],!\l&,g'
local re _dir lower
_dir="${module}"
re='(.*)([A-Z])(.*)'
while [[ ${_dir} =~ ${re} ]]; do
lower='!'"${BASH_REMATCH[2],}"
_dir="${BASH_REMATCH[1]}${lower}${BASH_REMATCH[3]}"
done
for _ext in "${exts[@]}" ; do
# Relative URI within a GOPROXY for a file
@ -297,7 +315,7 @@ go-module_set_globals() {
}
# @FUNCTION: go-module_setup_proxy
# @DEPRECATED:
# @DEPRECATED: none
# @DESCRIPTION:
# If your ebuild redefines src_unpack and uses EGO_SUM you need to call
# this function in src_unpack.
@ -340,11 +358,18 @@ go-module_setup_proxy() {
# @FUNCTION: go-module_src_unpack
# @DESCRIPTION:
# If EGO_SUM is set, unpack the base tarball(s) and set up the
# local go proxy. Also warn that this usage is deprecated.
# - Otherwise, if EGO_VENDOR is set, bail out.
# - Otherwise do a normal unpack.
# Sets up GOFLAGS for the system and then unpacks based on the following rules:
# 1. If EGO_SUM is set, unpack the base tarball(s) and set up the
# local go proxy. This mode is deprecated.
# 2. Otherwise, if EGO_VENDOR is set, bail out, as this functionality was removed.
# 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 "This ebuild uses EGO_SUM which is deprecated"
eqawarn "Please migrate to a dependency tarball"
@ -355,11 +380,19 @@ go-module_src_unpack() {
die "Please update this ebuild"
else
default
if [[ ! -d "${S}"/vendor ]]; then
cd "${S}"
local nf
[[ -n ${NONFATAL_VERIFY} ]] && nf=nonfatal
${nf} ego mod verify
fi
fi
go-env_set_compile_environment
}
# @FUNCTION: _go-module_src_unpack_gosum
# @DEPRECATED:
# @DEPRECATED: none
# @DESCRIPTION:
# Populate a GOPROXY directory hierarchy with distfiles from EGO_SUM and
# unpack the base distfiles.
@ -405,7 +438,7 @@ _go-module_src_unpack_gosum() {
}
# @FUNCTION: _go-module_gosum_synthesize_files
# @DEPRECATED:
# @DEPRECATED: none
# @DESCRIPTION:
# Given a path & version, populate all Goproxy metadata files which aren't
# needed to be downloaded directly.
@ -433,7 +466,7 @@ _go-module_gosum_synthesize_files() {
}
# @FUNCTION: _go-module_src_unpack_verify_gosum
# @DEPRECATED:
# @DEPRECATED: none
# @DESCRIPTION:
# Validate the Go modules declared by EGO_SUM are sufficient to cover building
# the package, without actually building it yet.
@ -481,31 +514,8 @@ go-module_live_vendor() {
popd >& /dev/null || die
}
# @FUNCTION: _go-module_gomod_encode
# @DEPRECATED:
# @DESCRIPTION:
# Encode the name(path) of a Golang module in the format expected by Goproxy.
#
# Upper letters are replaced by their lowercase version with a '!' prefix.
#
_go-module_gomod_encode() {
## Python:
# return re.sub('([A-Z]{1})', r'!\1', s).lower()
## Sed:
## This uses GNU Sed extension \l to downcase the match
#echo "${module}" |sed 's,[A-Z],!\l&,g'
#
# Bash variant:
debug-print-function "${FUNCNAME}" "$@"
#local re input lower
re='(.*)([A-Z])(.*)'
input="${1}"
while [[ ${input} =~ ${re} ]]; do
lower='!'"${BASH_REMATCH[2],}"
input="${BASH_REMATCH[1]}${lower}${BASH_REMATCH[3]}"
done
echo "${input}"
}
fi
if [[ ! ${GO_OPTIONAL} ]]; then
EXPORT_FUNCTIONS src_unpack
fi

View File

@ -1,12 +1,12 @@
# Copyright 1999-2021 Gentoo Authors
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: golang-base.eclass
# @MAINTAINER:
# William Hubbs <williamh@gentoo.org>
# @SUPPORTED_EAPIS: 5 6 7
# @DEPRECATED: go-module.eclass
# @BLURB: Eclass that provides base functions for Go packages.
# @DEPRECATED: go-module.eclass
# @DESCRIPTION:
# This eclass provides base functions for software written in the Go
# programming language; it also provides the build-time dependency on
@ -40,7 +40,7 @@ RESTRICT="strip"
# force GO111MODULE to be auto for bug https://bugs.gentoo.org/771129
export GO111MODULE=auto
# @ECLASS-VARIABLE: EGO_PN
# @ECLASS_VARIABLE: EGO_PN
# @REQUIRED
# @DESCRIPTION:
# This is the import path for the go package to build. Please emerge

View File

@ -1,10 +1,10 @@
# Copyright 1999-2021 Gentoo Authors
# Copyright 1999-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: golang-vcs-snapshot.eclass
# @MAINTAINER:
# William Hubbs <williamh@gentoo.org>
# @SUPPORTED_EAPIS: 5 6 7
# @SUPPORTED_EAPIS: 6 7
# @PROVIDES: golang-base
# @BLURB: eclass to unpack VCS snapshot tarballs for Go software
# @DEPRECATED: go-module.eclass
@ -44,16 +44,17 @@
# ${WORKDIR}/${P}/src/github.com/user/package
# and add the vendored tarballs to ${WORKDIR}/src/${EGO_PN}/vendor
inherit golang-base
case ${EAPI:-0} in
5|6|7) ;;
*) die "${ECLASS} API in EAPI ${EAPI} not yet established."
case ${EAPI} in
6|7) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
EXPORT_FUNCTIONS src_unpack
if [[ -z ${_GOLANG_VCS_SNAPSHOT_ECLASS} ]]; then
_GOLANG_VCS_SNAPSHOT_ECLASS=1
# @ECLASS-VARIABLE: EGO_VENDOR
inherit golang-base go-env
# @ECLASS_VARIABLE: EGO_VENDOR
# @DESCRIPTION:
# This variable contains a list of vendored packages.
# The items of this array are strings that contain the
@ -82,7 +83,7 @@ unset -f _golang-vcs-snapshot_set_vendor_uri
_golang-vcs-snapshot_dovendor() {
local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
rm -rf "${VENDOR_PATH}/${VENDORPN}" || die
mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
-f "${DISTDIR}"/${TARBALL} || die
@ -91,6 +92,7 @@ _golang-vcs-snapshot_dovendor() {
# @FUNCTION: golang-vcs-snapshot_src_unpack
# @DESCRIPTION:
# Extract the first archive from ${A} to the appropriate location for GOPATH.
# Set compile env via go-env.
golang-vcs-snapshot_src_unpack() {
local lib vendor_path x
ego_pn_check
@ -116,4 +118,10 @@ golang-vcs-snapshot_src_unpack() {
fi
done
fi
go-env_set_compile_environment
}
fi
EXPORT_FUNCTIONS src_unpack

View File

@ -1,10 +1,10 @@
# Copyright 1999-2021 Gentoo Authors
# Copyright 1999-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: golang-vcs.eclass
# @MAINTAINER:
# William Hubbs <williamh@gentoo.org>
# @SUPPORTED_EAPIS: 5 6 7
# @SUPPORTED_EAPIS: 6 7
# @PROVIDES: golang-base
# @BLURB: Eclass for fetching and unpacking go repositories.
# @DEPRECATED: go-module.eclass
@ -12,25 +12,19 @@
# This eclass is written to ease the maintenance of live ebuilds
# of software written in the Go programming language.
inherit estack eutils golang-base
case "${EAPI:-0}" in
5|6|7)
;;
*)
die "${ECLASS}: Unsupported eapi (EAPI=${EAPI})"
;;
case ${EAPI} in
6|7) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
EXPORT_FUNCTIONS src_unpack
if [[ -z ${_GOLANG_VCS_ECLASS} ]]; then
_GOLANG_VCS_ECLASS=1
if [[ -z ${_GOLANG_VCS} ]]; then
_GOLANG_VCS=1
inherit estack golang-base go-env
PROPERTIES+=" live"
# @ECLASS-VARIABLE: EGO_PN
# @ECLASS_VARIABLE: EGO_PN
# @REQUIRED
# @DESCRIPTION:
# This is the import path for the go package(s). Please emerge dev-lang/go
@ -42,7 +36,7 @@ PROPERTIES+=" live"
# EGO_PN="github.com/user1/package1 github.com/user2/package2"
# @CODE
# @ECLASS-VARIABLE: EGO_STORE_DIR
# @ECLASS_VARIABLE: EGO_STORE_DIR
# @USER_VARIABLE
# @DESCRIPTION:
# Storage directory for Go sources.
@ -52,12 +46,12 @@ PROPERTIES+=" live"
#
# EGO_STORE_DIR=${DISTDIR}/go-src
# @ECLASS-VARIABLE: EVCS_OFFLINE
# @ECLASS_VARIABLE: EVCS_OFFLINE
# @DEFAULT_UNSET
# @DESCRIPTION:
# If non-empty, this variable prevents any online operations.
# @ECLASS-VARIABLE: EVCS_UMASK
# @ECLASS_VARIABLE: EVCS_UMASK
# @DEFAULT_UNSET
# @DESCRIPTION:
# Set this variable to a custom umask. This is intended to be set by
@ -69,11 +63,12 @@ PROPERTIES+=" live"
# @INTERNAL
# @DESCRIPTION:
# Create EGO_STORE_DIR if necessary.
# Set compile env via go-env.
_golang-vcs_env_setup() {
debug-print-function ${FUNCNAME} "$@"
local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
: ${EGO_STORE_DIR:=${distdir}/go-src}
: "${EGO_STORE_DIR:=${distdir}/go-src}"
[[ -n ${EVCS_UMASK} ]] && eumask_push $EVCS_UMASK
@ -90,6 +85,8 @@ _golang-vcs_env_setup() {
mkdir -p "${WORKDIR}/${P}/src" ||
die "${ECLASS}: unable to create ${WORKDIR}/${P}"
return 0
go-env_set_compile_environment
}
# @FUNCTION: _golang-vcs_fetch
@ -139,3 +136,5 @@ golang-vcs_src_unpack() {
}
fi
EXPORT_FUNCTIONS src_unpack