eclass: Add golang-* eclasses from gentoo upstream

These are used by some of the new packages like app-emulation/containerd and
app-emulation/runc etc. Taken at upstream commit
d9abb73c24f7bd9fb93fff5664e01ad2c7b4b4f1.

BUG=None
TEST=trybots

Change-Id: I3d54c866c44d66120fffdf93cdfb750670e25fb0
Reviewed-on: https://chromium-review.googlesource.com/344643
Commit-Ready: Aditya Kali <adityakali@google.com>
Tested-by: Aditya Kali <adityakali@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/eclass/golang-base.eclass b/eclass/golang-base.eclass
new file mode 100644
index 0000000..0798040
--- /dev/null
+++ b/eclass/golang-base.eclass
@@ -0,0 +1,82 @@
+# Copyright 1999-2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+# @ECLASS: golang-build.eclass
+# @MAINTAINER:
+# William Hubbs <williamh@gentoo.org>
+# @BLURB: Eclass that provides base functions for Go packages.
+# @DESCRIPTION:
+# This eclass provides base functions for software written in the Go
+# programming language; it also provides the build-time dependency on
+# dev-lang/go.
+
+case "${EAPI:-0}" in
+	5|6)
+		;;
+	*)
+		die "${ECLASS}: Unsupported eapi (EAPI=${EAPI})"
+		;;
+esac
+
+if [[ -z ${_GOLANG_BASE} ]]; then
+
+_GOLANG_BASE=1
+
+DEPEND=">=dev-lang/go-1.4.2:="
+
+# Do not complain about CFLAGS etc since go projects do not use them.
+QA_FLAGS_IGNORED='.*'
+
+STRIP_MASK="*.a"
+
+# @ECLASS-VARIABLE: EGO_PN
+# @REQUIRED
+# @DESCRIPTION:
+# This is the import path for the go package to build. Please emerge
+# dev-lang/go and read "go help importpath" for syntax.
+#
+# Example:
+# @CODE
+# EGO_PN=github.com/user/package
+# @CODE
+
+# @FUNCTION: ego_pn_check
+# @DESCRIPTION:
+# Make sure EGO_PN has a value.
+ego_pn_check() {
+	[[ -z "${EGO_PN}" ]] &&
+		die "${ECLASS}.eclass: EGO_PN is not set"
+	return 0
+}
+
+# @FUNCTION: get_golibdir
+# @DESCRIPTION:
+# Return the non-prefixed library directory where Go packages
+# should be installed
+get_golibdir() {
+	echo /usr/lib/go-gentoo
+}
+
+# @FUNCTION: get_golibdir_gopath
+# @DESCRIPTION:
+# Return the library directory where Go packages should be installed
+# This is the prefixed version which should be included in GOPATH
+get_golibdir_gopath() {
+	echo "${EPREFIX}$(get_golibdir)"
+}
+
+# @FUNCTION: golang_install_pkgs
+# @DESCRIPTION:
+# Install Go packages.
+# This function assumes that $cwd is a Go workspace.
+golang_install_pkgs() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	ego_pn_check
+	insinto "$(get_golibdir)"
+	insopts -m0644 -p # preserve timestamps for bug 551486
+	doins -r pkg src
+}
+
+fi
diff --git a/eclass/golang-build.eclass b/eclass/golang-build.eclass
new file mode 100644
index 0000000..cc2a2ec
--- /dev/null
+++ b/eclass/golang-build.eclass
@@ -0,0 +1,84 @@
+# Copyright 1999-2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+# @ECLASS: golang-build.eclass
+# @MAINTAINER:
+# William Hubbs <williamh@gentoo.org>
+# @BLURB: Eclass for compiling go packages.
+# @DESCRIPTION:
+# This eclass provides default  src_compile, src_test and src_install
+# functions for software written in the Go programming language.
+
+inherit golang-base
+
+case "${EAPI:-0}" in
+	5|6)
+		;;
+	*)
+		die "${ECLASS}: Unsupported eapi (EAPI=${EAPI})"
+		;;
+esac
+
+EXPORT_FUNCTIONS src_compile src_install src_test
+
+if [[ -z ${_GOLANG_BUILD} ]]; then
+
+_GOLANG_BUILD=1
+
+# @ECLASS-VARIABLE: EGO_BUILD_FLAGS
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# This allows you to pass build flags to the Go compiler. These flags
+# are common to the "go build" and "go install" commands used below.
+# Please emerge dev-lang/go and run "go help build" for the
+# documentation for these flags.
+#
+# Example:
+# @CODE
+# EGO_BUILD_FLAGS="-ldflags \"-X main.version ${PV}\""
+# @CODE
+
+# @ECLASS-VARIABLE: EGO_PN
+# @REQUIRED
+# @DESCRIPTION:
+# This is the import path for the go package(s) to build. Please emerge
+# dev-lang/go and read "go help importpath" for syntax.
+#
+# Example:
+# @CODE
+# EGO_PN=github.com/user/package
+# @CODE
+
+golang-build_src_compile() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	ego_pn_check
+	set -- env GOPATH="${WORKDIR}/${P}:$(get_golibdir_gopath)" \
+		go build -v -work -x ${EGO_BUILD_FLAGS} "${EGO_PN}"
+	echo "$@"
+	"$@" || die
+}
+
+golang-build_src_install() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	ego_pn_check
+	set -- env GOPATH="${WORKDIR}/${P}:$(get_golibdir_gopath)" \
+		go install -v -work -x ${EGO_BUILD_FLAGS} "${EGO_PN}"
+	echo "$@"
+	"$@" || die
+	golang_install_pkgs
+}
+
+golang-build_src_test() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	ego_pn_check
+	set -- env GOPATH="${WORKDIR}/${P}:$(get_golibdir_gopath)" \
+		go test -v -work -x "${EGO_PN}"
+	echo "$@"
+	"$@" || die
+}
+
+fi
diff --git a/eclass/golang-vcs-snapshot.eclass b/eclass/golang-vcs-snapshot.eclass
new file mode 100644
index 0000000..2d84c28
--- /dev/null
+++ b/eclass/golang-vcs-snapshot.eclass
@@ -0,0 +1,56 @@
+# Copyright 1999-2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+# @ECLASS: golang-vcs-snapshot.eclass
+# @MAINTAINER:
+# William Hubbs <williamh@gentoo.org>
+# @BLURB: support eclass for unpacking VCS snapshot tarballs for
+# software written in the Go programming language
+# @DESCRIPTION:
+# This eclass provides a convenience src_unpack() which unpacks the
+# first tarball mentioned in SRC_URI to its appropriate location in
+# ${WORKDIR}/${P}, treating ${WORKDIR}/${P} as a go workspace.
+#
+# The location where the tarball is extracted is defined as
+# ${WORKDIR}/${P}/src/${EGO_PN}.
+#
+# The typical use case is VCS snapshots coming from github, bitbucket
+# and similar services.
+#
+# Please note that this eclass currently handles only tarballs
+# (.tar.gz), but support for more formats may be added in the future.
+#
+# @EXAMPLE:
+#
+# @CODE
+# EGO_PN=github.com/user/package
+# inherit golang-vcs-snapshot
+#
+# SRC_URI="http://github.com/example/${PN}/tarball/v${PV} -> ${P}.tar.gz"
+# @CODE
+#
+# The above example will extract the tarball to
+# ${WORKDIR}/${P}/src/github.com/user/package
+
+inherit golang-base
+
+case ${EAPI:-0} in
+	5|6) ;;
+	*) die "${ECLASS} API in EAPI ${EAPI} not yet established."
+esac
+
+EXPORT_FUNCTIONS src_unpack
+
+# @FUNCTION: golang-vcs-snapshot_src_unpack
+# @DESCRIPTION:
+# Extract the first archive from ${A} to the appropriate location for GOPATH.
+golang-vcs-snapshot_src_unpack() {
+	local x
+	ego_pn_check
+	set -- ${A}
+	x="$1"
+	mkdir -p "${WORKDIR}/${P}/src/${EGO_PN%/...}" || die
+	tar -C "${WORKDIR}/${P}/src/${EGO_PN%/...}" -x --strip-components 1 \
+		-f "${DISTDIR}/${x}" || die
+}
diff --git a/eclass/golang-vcs.eclass b/eclass/golang-vcs.eclass
new file mode 100644
index 0000000..6448ddd
--- /dev/null
+++ b/eclass/golang-vcs.eclass
@@ -0,0 +1,136 @@
+# Copyright 1999-2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+# @ECLASS: golang-vcs.eclass
+# @MAINTAINER:
+# William Hubbs <williamh@gentoo.org>
+# @BLURB: Eclass for fetching and unpacking go repositories.
+# @DESCRIPTION:
+# This eclass is written to ease the maintenance of live ebuilds
+# of software written in the Go programming language.
+
+inherit eutils golang-base
+
+case "${EAPI:-0}" in
+	5|6)
+		;;
+	*)
+		die "${ECLASS}: Unsupported eapi (EAPI=${EAPI})"
+		;;
+esac
+
+EXPORT_FUNCTIONS src_unpack
+
+if [[ -z ${_GOLANG_VCS} ]]; then
+
+_GOLANG_VCS=1
+
+# @ECLASS-VARIABLE: EGO_PN
+# @REQUIRED
+# @DESCRIPTION:
+# This is the import path for the go package(s). Please emerge dev-lang/go
+# and read "go help importpath" for syntax.
+#
+# Example:
+# @CODE
+# EGO_PN="github.com/user/package"
+# EGO_PN="github.com/user1/package1 github.com/user2/package2"
+# @CODE
+
+# @ECLASS-VARIABLE: EGO_STORE_DIR
+# @DESCRIPTION:
+# Storage directory for Go sources.
+#
+# This is intended to be set by the user in make.conf. Ebuilds must not set
+# it.
+#
+# EGO_STORE_DIR=${DISTDIR}/go-src
+
+# @ECLASS-VARIABLE: EVCS_OFFLINE
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# If non-empty, this variable prevents any online operations.
+
+# @ECLASS-VARIABLE: EVCS_UMASK
+# @DEFAULT_UNSET
+# @DESCRIPTION:
+# Set this variable to a custom umask. This is intended to be set by
+# users. By setting this to something like 002, it can make life easier
+# for people who do development as non-root (but are in the portage
+# group) and use FEATURES=userpriv.
+
+# @FUNCTION: _golang-vcs_env_setup
+# @INTERNAL
+# @DESCRIPTION:
+# Create EGO_STORE_DIR if necessary.
+_golang-vcs_env_setup() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
+	: ${EGO_STORE_DIR:=${distdir}/go-src}
+
+	[[ -n ${EVCS_UMASK} ]] && eumask_push $EVCS_UMASK
+
+	if [[ ! -d ${EGO_STORE_DIR} ]]; then
+		(
+			addwrite /
+			mkdir -p "${EGO_STORE_DIR}"
+		) || die "${ECLASS}: unable to create ${EGO_STORE_DIR}"
+	fi
+
+	addwrite "${EGO_STORE_DIR}"
+
+	[[ -n ${EVCS_UMASK} ]] && eumask_pop
+	mkdir -p "${WORKDIR}/${P}/src" ||
+		die "${ECLASS}: unable to create ${WORKDIR}/${P}"
+	return 0
+}
+
+# @FUNCTION: _golang-vcs_fetch
+# @INTERNAL
+# @DESCRIPTION:
+# Retrieve the EGO_PN go package along with its dependencies.
+_golang-vcs_fetch() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	ego_pn_check
+
+	if [[ -z ${EVCS_OFFLINE} ]]; then
+		[[ -n ${EVCS_UMASK} ]] && eumask_push ${EVCS_UMASK}
+
+		set -- env GOPATH="${EGO_STORE_DIR}" go get -d -t -u -v -x "${EGO_PN}"
+		echo "$@"
+		"$@" || die
+		# The above dies if you pass repositories in EGO_PN instead of
+		# packages, e.g. golang.org/x/tools instead of golang.org/x/tools/cmd/vet.
+		# This is being discussed in the following upstream issue:
+		# https://github.com/golang/go/issues/11090
+
+		[[ -n ${EVCS_UMASK} ]] && eumask_pop
+	fi
+	local go_srcpath="${WORKDIR}/${P}/src/${EGO_PN%/...}"
+	set -- mkdir -p "${go_srcpath}"
+	echo "$@"
+	"$@" || die "Unable to create ${go_srcpath}"
+	set -- cp -r	"${EGO_STORE_DIR}/src/${EGO_PN%/...}" \
+		"${go_srcpath}/.."
+	echo "$@"
+	"$@" || die "Unable to copy sources to ${go_srcpath}"
+	return 0
+}
+
+golang-vcs_src_fetch() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	_golang-vcs_env_setup
+	_golang-vcs_fetch
+}
+
+golang-vcs_src_unpack() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	golang-vcs_src_fetch
+}
+
+fi