coreos-go-utils: new eclass for Go related utilities

This eclass adopts parts of coreos-go.eclass and the dev-lang/go
ebuilds, providing functionality useful to both types of builds.
This commit is contained in:
Michael Marineau 2016-08-25 12:27:19 -07:00
parent 7d02cc8fd3
commit b19a21acf1
4 changed files with 124 additions and 32 deletions

View File

@ -32,7 +32,7 @@ RDEPEND="app-emulation/runc
S=${WORKDIR}/${P}/src/${EGO_PN}
src_compile() {
export GOARCH=$(go_get_arch)
export GOARCH=$(go_arch)
export CGO_ENABLED=1
export CC=$(tc-getCC)
export CXX=$(tc-getCXX)

View File

@ -34,7 +34,7 @@ src_prepare() {
src_compile() {
# fix up cross-compiling variables
export GOARCH=$(go_get_arch)
export GOARCH=$(go_arch)
export CGO_ENABLED=1
export CC=$(tc-getCC)
export CXX=$(tc-getCXX)

View File

@ -0,0 +1,116 @@
# Copyright 2016 CoreOS, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: $
# @ECLASS: coreos-go-utils.eclass
# @BLURB: Utility functions for Go
# @DESCRIPTION:
# A utility eclass for functions needed when building both Go itself
# and packages that depend on it. It does not set any metadata.
case "${EAPI:-0}" in
5|6) ;;
*) die "Unsupported EAPI=${EAPI} for ${ECLASS}"
esac
inherit flag-o-matic toolchain-funcs
# @FUNCTION: go_arch
# @USAGE: [chost]
# @DESCRIPTION:
# export GOARCH=$(go_arch $CHOST)
go_arch() {
# By chance most portage arch names match Go
local portage_arch=$(tc-arch "${1:-${CHOST}}")
local endian=$(tc-endian "${1:-${CHOST}}")
case "${portage_arch}" in
x86) echo 386;;
x64-*) echo amd64;;
ppc64)
[[ "${endian}" = big ]] && echo ppc64 || echo ppc64le ;;
*) echo "${portage_arch}";;
esac
}
# @FUNCTION: go_arm
# @USAGE: [chost]
# @DESCRIPTION:
# export GOARM=$(go_arm $CHOST)
go_arm() {
case "${1:-${CHOST}}" in
armv5*) echo 5;;
armv6*) echo 6;;
armv7*) echo 7;;
*)
die "unknown GOARM for ${1:-${CHOST}}"
;;
esac
}
# @FUNCTION: go_os
# @USAGE: [chost]
# @DESCRIPTION:
# export GOOS=$(go_os $CHOST)
go_os() {
case "${1:-${CHOST}}" in
*-linux*) echo linux;;
*-darwin*) echo darwin;;
*-freebsd*) echo freebsd;;
*-netbsd*) echo netbsd;;
*-openbsd*) echo openbsd;;
*-solaris*) echo solaris;;
*-cygwin*|*-interix*|*-winnt*)
echo windows
;;
*)
die "unknown GOOS for ${1:-${CHOST}}"
;;
esac
}
# @FUNCTION: go_export
# @USAGE: [chost]
# @DESCRIPTION:
# Export GOARCH, GOOS, GOARM, CC, CXX, and CGO_* environment variables.
go_export() {
export GOARCH=$(go_arch "$@")
export GOOS=$(go_os "$@")
if [[ "${GOARCH}" == "arm" ]]; then
export GOARM=$(go_arm "$@")
fi
# Go's 6l linker does not support PIE, disable so cgo binaries
# which use 6l+gcc for linking can be built correctly.
if gcc-specs-pie; then
append-ldflags -nopie
fi
export CC=$(tc-getCC)
export CXX=$(tc-getCXX)
export CGO_ENABLED=${CGO_ENABLED:-1}
export CGO_CFLAGS="${CFLAGS}"
export CGO_CPPFLAGS="${CPPFLAGS}"
export CGO_CXXFLAGS="${CXXFLAGS}"
export CGO_LDFLAGS="${LDFLAGS}"
}
# @FUNCTION: go_tuple
# @USAGE: [chost]
# @DESCRIPTION:
# The uniqe string defining a Go target, as used in directory names.
# e.g. linux_amd64 for x86_64-pc-linux-gnu
go_tuple()
{
echo "$(go_os $@)_$(go_arch $@)"
}
# @FUNCTION: go_cross_compile
# @USAGE: [chost]
# @DESCRIPTION:
# Check if Go consideres compiling for $CHOST as cross-compilation.
# Since Go's target tuples are smaller than GCC's Go may not consider
# itself as a cross-compiler even if the GCC it is using for cgo is.
go_cross_compile()
{
[[ $(go_tuple ${CBUILD:-${CHOST}}) != $(go_tuple $@) ]]
}

View File

@ -9,26 +9,15 @@
# @DESCRIPTION:
# Name of the Go package unpacked to ${S}, this is required.
[[ ${EAPI} != "5" ]] && die "${ECLASS}: Only EAPI=5 is supported"
case "${EAPI:-0}" in
5|6) ;;
*) die "Unsupported EAPI=${EAPI} for ${ECLASS}"
esac
inherit flag-o-matic multiprocessing toolchain-funcs
inherit coreos-go-utils multiprocessing
DEPEND="dev-lang/go:="
# @FUNCTION: go_get_arch
# @USAGE: export GOARCH=$(go_get_arch)
go_get_arch() {
# By chance most portage arch names match Go
local portage_arch=$(tc-arch ${CHOST})
case "${portage_arch}" in
x86) echo 386;;
x64-*) echo amd64;;
ppc64)
[[ "$(tc-endian)" = big ]] && echo ppc64 || echo ppc64le ;;
*) echo "${portage_arch}";;
esac
}
# @FUNCTION: go_build
# @USAGE: <package-name> [<binary-name>]
go_build() {
@ -55,7 +44,7 @@ go_build() {
coreos-go_src_prepare() {
debug-print-function ${FUNCNAME} "$@"
export GOARCH=$(go_get_arch)
go_export
export GOPATH="${WORKDIR}/gopath"
export GOBIN="${GOPATH}/bin"
@ -68,19 +57,6 @@ coreos-go_src_prepare() {
local package_path="${GOPATH}/src/${COREOS_GO_PACKAGE}"
mkdir -p "${package_path%/*}" || die "${ECLASS}: bad path: ${package_path%/*}"
ln -sT "${S}" "${package_path}" || die "${ECLASS}: bad path: ${S}"
# Go's 6l linker does not support PIE, disable so cgo binaries
# which use 6l+gcc for linking can be built correctly.
if gcc-specs-pie; then
append-ldflags -nopie
fi
export CC=$(tc-getCC)
export CGO_ENABLED=${CGO_ENABLED:-1}
export CGO_CFLAGS="${CFLAGS}"
export CGO_CPPFLAGS="${CPPFLAGS}"
export CGO_CXXFLAGS="${CXXFLAGS}"
export CGO_LDFLAGS="${LDFLAGS}"
}
coreos-go_src_compile() {