Gentoo Archives: gentoo-dev

From: William Hubbs <williamh@g.o>
To: gentoo-dev@l.g.o
Cc: William Hubbs <williamh@g.o>
Subject: [gentoo-dev] [PATCH 2/2] go-module-vendor.eclass: new eclass for go modules that do not vendor
Date: Sat, 21 Sep 2019 22:12:09
Message-Id: 20190921221032.10777-3-williamh@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/2] introduce new eclasses to handle go modules by William Hubbs
1 This eclass adds a src_unpack function that supports the EGO_VENDOR
2 variable for vendoring modules.
3 ---
4 eclass/go-module-vendor.eclass | 133 +++++++++++++++++++++++++++++++++
5 1 file changed, 133 insertions(+)
6 create mode 100644 eclass/go-module-vendor.eclass
7
8 diff --git a/eclass/go-module-vendor.eclass b/eclass/go-module-vendor.eclass
9 new file mode 100644
10 index 00000000000..af9007df411
11 --- /dev/null
12 +++ b/eclass/go-module-vendor.eclass
13 @@ -0,0 +1,133 @@
14 +# Copyright 2019 gentoo authors
15 +# Distributed under the terms of the GNU General Public License v2
16 +
17 +# @ECLASS: go-module-vendor.eclass
18 +# @MAINTAINER:
19 +# William Hubbs <williamh@g.o>
20 +# @SUPPORTED_EAPIS: 7
21 +# @BLURB: Eclass for building software written in the go
22 +# programming language that uses go modules and does not vendor.
23 +# @DESCRIPTION:
24 +# This eclass provides a src_unpack function which supports vendoring
25 +# dependencies for software written in the go programming language that
26 +# uses go modules.
27 +#
28 +# You will know the software you are packaging uses modules because
29 +# it will have files named go.sum and go.mod in its top-level source
30 +# directory. If it does not have these files, use the golang-* eclasses.
31 +#
32 +# If there is also a directory named vendor in the top level source directory
33 +# of your package, use the golang-module eclass instead of this one.
34 +#
35 +# This eclass provides a src_unpack function which unpacks the
36 +# first tarball mentioned in SRC_URI to the usual location and unpacks
37 +# any modules mentioned in EGO_VENDOR to ${S}/vendor.
38 +#
39 +# Please note that this eclass currently handles only tarballs
40 +# (.tar.gz), but support for more formats may be added in the future.
41 +#
42 +# Since Go programs are statically linked, it is important that your ebuild's
43 +# LICENSE= setting includes the licenses of all statically linked
44 +# dependencies. So please make sure it is accurate.
45 +#
46 +# @EXAMPLE:
47 +#
48 +# @CODE
49 +# EGO_VENDOR=(
50 +# "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
51 +# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
52 +# )
53 +#
54 +# inherit go-module-vendor
55 +#
56 +# SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
57 +# ${EGO_VENDOR_URI}"
58 +# @CODE
59 +#
60 +# The above example will extract the tarball to ${S} and
61 +# extract the contents of EGO_VENDOR to ${S}/vendor.
62 +
63 +inherit go-module
64 +
65 +case ${EAPI:-0} in
66 + 7) ;;
67 + *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
68 +esac
69 +
70 +if [[ -z ${_GO_MODULE_VENDOR} ]]; then
71 +
72 +_GO_MODULE_VENDOR=1
73 +
74 +EXPORT_FUNCTIONS src_unpack
75 +
76 +# @ECLASS-VARIABLE: EGO_VENDOR
77 +# @DESCRIPTION:
78 +# This variable contains a list of vendored packages.
79 +# The items of this array are strings that contain the
80 +# import path and the git commit hash for a vendored package.
81 +# If the import path does not start with github.com, the third argument
82 +# can be used to point to a github repository.
83 +
84 +declare -arg EGO_VENDOR
85 +
86 +_go-module-vendor_set_vendor_uri() {
87 + EGO_VENDOR_URI=
88 + local lib
89 + for lib in "${EGO_VENDOR[@]}"; do
90 + lib=(${lib})
91 + if [[ -n ${lib[2]} ]]; then
92 + EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
93 + else
94 + EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
95 + fi
96 + done
97 + readonly EGO_VENDOR_URI
98 +}
99 +
100 +_go-module-vendor_set_vendor_uri
101 +unset -f _go-module-vendor_set_vendor_uri
102 +
103 +_go-module-vendor_dovendor() {
104 + local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
105 + rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
106 + mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
107 + tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
108 + -f "${DISTDIR}/${TARBALL}" || die
109 +}
110 +
111 +# @FUNCTION: go-module-vendor_src_unpack
112 +# @DESCRIPTION:
113 +# Extract the first archive from ${A} to ${S}, then extract
114 +# any sources mentioned in ${EGO_VENDOR} to ${S}/vendor.
115 +go-module-vendor_src_unpack() {
116 + local lib vendor_path x
117 + set -- ${A}
118 + x="$1"
119 + mkdir -p "${S}" || die
120 + tar -C "${S}/" -x --strip-components 1 \
121 + -f "${DISTDIR}/${x}" || die
122 +
123 + if [[ -d "${S}/vendor" ]]; then
124 + eerror "Upstream for ${P}.ebuild vendors dependencies."
125 + die "This ebuild should inherit go-module.eclass"
126 + fi
127 +
128 + if [[ -n "${EGO_VENDOR}" ]]; then
129 + vendor_path="${S}/vendor"
130 + mkdir -p "${vendor_path}" || die
131 + for lib in "${EGO_VENDOR[@]}"; do
132 + lib=(${lib})
133 + if [[ -n ${lib[2]} ]]; then
134 + einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz"
135 + _go-module_dovendor "${vendor_path}" ${lib[0]} \
136 + ${lib[2]//\//-}-${lib[1]}.tar.gz
137 + else
138 + einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz"
139 + _go-module_dovendor "${vendor_path}" ${lib[0]} \
140 + ${lib[0]//\//-}-${lib[1]}.tar.gz
141 + fi
142 + done
143 + fi
144 +}
145 +
146 +fi
147 --
148 2.21.0

Replies