meson.eclass: Don't mix host *FLAGS with build *FLAGS
[gentoo.git] / eclass / golang-vcs-snapshot.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: golang-vcs-snapshot.eclass
5 # @MAINTAINER:
6 # William Hubbs <williamh@gentoo.org>
7 # @BLURB: support eclass for unpacking VCS snapshot tarballs for
8 # software written in the Go programming language
9 # @DESCRIPTION:
10 # This eclass provides a convenience src_unpack() which unpacks the
11 # first tarball mentioned in SRC_URI to its appropriate location in
12 # ${WORKDIR}/${P}, treating ${WORKDIR}/${P} as a go workspace.
13 # Also, it provides a downstream method of vendoring packages.
14 #
15 # The location where the tarball is extracted is defined as
16 # ${WORKDIR}/${P}/src/${EGO_PN}. The location of vendored packages is
17 # defined as ${WORKDIR}/${P}/src/${EGO_PN%/*}/vendor to match Go's
18 # vendoring setup.
19 #
20 # The typical use case is VCS snapshots coming from github, bitbucket
21 # and similar services.
22 #
23 # Please note that this eclass currently handles only tarballs
24 # (.tar.gz), but support for more formats may be added in the future.
25 #
26 # @EXAMPLE:
27 #
28 # @CODE
29 # EGO_PN=github.com/user/package
30 # EGO_VENDOR=(
31 #       "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
32 # "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
33 # )
34 #
35 # inherit golang-vcs-snapshot
36 #
37 # SRC_URI="https://github.com/example/${PN}/tarball/v${PV} -> ${P}.tar.gz
38 # ${EGO_VENDOR_URI}"
39 # @CODE
40 #
41 # The above example will extract the tarball to
42 # ${WORKDIR}/${P}/src/github.com/user/package
43 # and add the vendored tarballs to ${WORKDIR}/src/${EGO_PN}/vendor
44
45 inherit golang-base
46
47 case ${EAPI:-0} in
48         5|6) ;;
49         *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
50 esac
51
52 EXPORT_FUNCTIONS src_unpack
53
54 # @ECLASS-VARIABLE: EGO_VENDOR
55 # @DESCRIPTION:
56 # This variable contains a list of vendored packages.
57 # The items of this array are strings that contain the
58 # import path and the git commit hash for a vendored package.
59 # If the import path does not start with github.com, the third argument
60 # can be used to point to a github repository.
61
62 declare -arg EGO_VENDOR
63
64 _golang-vcs-snapshot_set_vendor_uri() {
65         EGO_VENDOR_URI=
66         local lib
67         for lib in "${EGO_VENDOR[@]}"; do
68                 lib=(${lib})
69                 if [[ -n ${lib[2]} ]]; then
70                         EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
71                 else
72                         EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
73                 fi
74         done
75         readonly EGO_VENDOR_URI
76 }
77
78 _golang-vcs-snapshot_set_vendor_uri
79 unset -f _golang-vcs-snapshot_set_vendor_uri
80
81 _golang-vcs-snapshot_dovendor() {
82         local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
83         rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
84         mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
85         tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
86                 -f "${DISTDIR}"/${TARBALL} || die
87 }
88
89 # @FUNCTION: golang-vcs-snapshot_src_unpack
90 # @DESCRIPTION:
91 # Extract the first archive from ${A} to the appropriate location for GOPATH.
92 golang-vcs-snapshot_src_unpack() {
93         local lib vendor_path x
94         ego_pn_check
95         set -- ${A}
96         x="$1"
97         mkdir -p "${WORKDIR}/${P}/src/${EGO_PN%/...}" || die
98         tar -C "${WORKDIR}/${P}/src/${EGO_PN%/...}" -x --strip-components 1 \
99                 -f "${DISTDIR}/${x}" || die
100
101         if [[ -n "${EGO_VENDOR}" ]]; then
102                 vendor_path="${WORKDIR}/${P}/src/${EGO_PN%/...}/vendor"
103                 mkdir -p "${vendor_path}" || die
104                 for lib in "${EGO_VENDOR[@]}"; do
105                         lib=(${lib})
106                         if [[ -n ${lib[2]} ]]; then
107                                 einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz"
108                                 _golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
109                                         ${lib[2]//\//-}-${lib[1]}.tar.gz
110                         else
111                                 einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz"
112                                 _golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
113                                 ${lib[0]//\//-}-${lib[1]}.tar.gz
114                         fi
115                 done
116         fi
117 }