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 1/1] go-module.eclass: introduce new eclass to handle go modules
Date: Wed, 18 Sep 2019 20:27:20
Message-Id: 20190918202631.8667-2-williamh@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/1] introduce an eclass to handle go modules (round 5) by William Hubbs
1 Signed-off-by: William Hubbs <williamh@g.o>
2 ---
3 eclass/go-module.eclass | 161 ++++++++++++++++++++++++++++++++++++++++
4 1 file changed, 161 insertions(+)
5 create mode 100644 eclass/go-module.eclass
6
7 diff --git a/eclass/go-module.eclass b/eclass/go-module.eclass
8 new file mode 100644
9 index 00000000000..6f609e94542
10 --- /dev/null
11 +++ b/eclass/go-module.eclass
12 @@ -0,0 +1,161 @@
13 +# Copyright 2019 gentoo authors
14 +# Distributed under the terms of the GNU General Public License v2
15 +
16 +# @ECLASS: go-module.eclass
17 +# @MAINTAINER:
18 +# William Hubbs <williamh@g.o>
19 +# @SUPPORTED_EAPIS: 7
20 +# @BLURB: basic eclass for building software written in the go
21 +# programming language that uses go modules.
22 +# @DESCRIPTION:
23 +# This eclass provides some basic things needed by all software
24 +# written in the go programming language that uses go modules.
25 +#
26 +# You will know the software you are packaging uses modules because
27 +# it will have files named go.sum and go.mod in its top-level source
28 +# directory. If it does not have these files, use the golang-* eclasses.
29 +#
30 +# This eclass provides a src_unpack function which unpacks the
31 +# first tarball mentioned in SRC_URI to the usual location and unpacks
32 +# any modules mentioned in EGO_VENDOR to ${S}/vendor if upstream doesn't
33 +# vendor its own dependencies.
34 +#
35 +# If upstream vendors its dependencies, setting EGO_VENDOR is an error.
36 +#
37 +# Please note that this eclass currently handles only tarballs
38 +# (.tar.gz), but support for more formats may be added in the future.
39 +#
40 +# Since Go programs are statically linked, it is important that your ebuild's
41 +# LICENSE= setting includes the licenses of all statically linked
42 +# dependencies. So please make sure it is accurate.
43 +#
44 +# @EXAMPLE:
45 +#
46 +# @CODE
47 +# EGO_VENDOR=(
48 +# "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
49 +# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
50 +# )
51 +#
52 +# inherit go-module
53 +#
54 +# SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
55 +# ${EGO_VENDOR_URI}"
56 +# @CODE
57 +#
58 +# The above example will extract the tarball to ${S} and
59 +# extract the contents of EGO_VENDOR to ${S}/vendor.
60 +
61 +case ${EAPI:-0} in
62 + 7) ;;
63 + *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
64 +esac
65 +
66 +if [[ -z ${_GO_MODULE} ]]; then
67 +
68 +_GO_MODULE=1
69 +
70 +BDEPEND=">=dev-lang/go-1.12"
71 +
72 +# Force go to build in module mode.
73 +# In this mode the GOPATH environment variable is ignored.
74 +# this will become the default in the future.
75 +export GO111MODULE=on
76 +
77 +# The following go flags should be used for all builds.
78 +# -mod=vendor stopps downloading of dependencies from the internet.
79 +# -v prints the names of packages as they are compiled
80 +# -x prints commands as they are executed
81 +export GOFLAGS="-mod=vendor -v -x"
82 +
83 +# Do not complain about CFLAGS etc since go projects do not use them.
84 +QA_FLAGS_IGNORED='.*'
85 +
86 +# Go packages should not be stripped with strip(1).
87 +RESTRICT="strip"
88 +
89 +EXPORT_FUNCTIONS src_unpack pkg_postinst
90 +
91 +# @ECLASS-VARIABLE: EGO_VENDOR
92 +# @DESCRIPTION:
93 +# This variable contains a list of vendored packages.
94 +# The items of this array are strings that contain the
95 +# import path and the git commit hash for a vendored package.
96 +# If the import path does not start with github.com, the third argument
97 +# can be used to point to a github repository.
98 +
99 +declare -arg EGO_VENDOR
100 +
101 +_go-module_set_vendor_uri() {
102 + EGO_VENDOR_URI=
103 + local lib
104 + for lib in "${EGO_VENDOR[@]}"; do
105 + lib=(${lib})
106 + if [[ -n ${lib[2]} ]]; then
107 + EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
108 + else
109 + EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
110 + fi
111 + done
112 + readonly EGO_VENDOR_URI
113 +}
114 +
115 +_go-module_set_vendor_uri
116 +unset -f _go-module_set_vendor_uri
117 +
118 +_go-module_dovendor() {
119 + local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
120 + rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
121 + mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
122 + tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
123 + -f "${DISTDIR}/${TARBALL}" || die
124 +}
125 +
126 +# @FUNCTION: go-module_src_unpack
127 +# @DESCRIPTION:
128 +# Extract the first archive from ${A} to ${S}, then extract
129 +# any sources mentioned in ${EGO_VENDOR} to ${S}/vendor.
130 +go-module_src_unpack() {
131 + local lib vendor_path x
132 + set -- ${A}
133 + x="$1"
134 + mkdir -p "${S}" || die
135 + tar -C "${S}/" -x --strip-components 1 \
136 + -f "${DISTDIR}/${x}" || die
137 +
138 + if [[ -n "${EGO_VENDOR}" ]]; then
139 + vendor_path="${S}/vendor"
140 + if [[ -d "${vendor_path}" ]]; then
141 + eerror "Upstream for ${P}.ebuild vendors dependencies."
142 + die "Remove EGO_VENDOR from the ebuild."
143 + fi
144 + mkdir -p "${vendor_path}" || die
145 + for lib in "${EGO_VENDOR[@]}"; do
146 + lib=(${lib})
147 + if [[ -n ${lib[2]} ]]; then
148 + einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz"
149 + _go-module_dovendor "${vendor_path}" ${lib[0]} \
150 + ${lib[2]//\//-}-${lib[1]}.tar.gz
151 + else
152 + einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz"
153 + _go-module_dovendor "${vendor_path}" ${lib[0]} \
154 + ${lib[0]//\//-}-${lib[1]}.tar.gz
155 + fi
156 + done
157 + fi
158 +}
159 +
160 +# @FUNCTION: go-module_pkg_postinst
161 +# @DESCRIPTION:
162 +# Display a warning about security updates for Go programs.
163 +go-module_pkg_postinst() {
164 + ewarn "${PN} is written in the Go programming language."
165 + ewarn "Since this language is statically linked, security"
166 + ewarn "updates will be handled in individual packages and will be"
167 + ewarn "difficult for us to track as a distribution."
168 + ewarn "For this reason, please update any go packages asap when new"
169 + ewarn "versions enter the tree or go stable if you are running the"
170 + ewarn "stable tree."
171 +}
172 +
173 +fi
174 --
175 2.21.0

Replies