Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: William Hubbs <williamh@g.o>
Subject: Re: [gentoo-dev] [PATCH v2 1/1] go-module.eclass: new eclass for go modules
Date: Thu, 26 Sep 2019 19:49:58
Message-Id: 42d4b7dc97d86cf748c9d18f97e21eb5f68b1dc1.camel@gentoo.org
In Reply to: [gentoo-dev] [PATCH v2 1/1] go-module.eclass: new eclass for go modules by William Hubbs
1 On Wed, 2019-09-25 at 19:24 -0500, William Hubbs wrote:
2 > This eclass includes the basic settings and src_unpack/pkg_postinst
3 > functions for Go modules.
4 >
5 > Signed-off-by: William Hubbs <williamh@g.o>
6 > ---
7 > eclass/go-module.eclass | 176 ++++++++++++++++++++++++++++++++++++++++
8 > 1 file changed, 176 insertions(+)
9 > create mode 100644 eclass/go-module.eclass
10 >
11 > diff --git a/eclass/go-module.eclass b/eclass/go-module.eclass
12 > new file mode 100644
13 > index 00000000000..f920457f3ab
14 > --- /dev/null
15 > +++ b/eclass/go-module.eclass
16 > @@ -0,0 +1,176 @@
17 > +# Copyright 2019 Gentoo authors
18 > +# Distributed under the terms of the GNU General Public License v2
19 > +
20 > +# @ECLASS: go-module.eclass
21 > +# @MAINTAINER:
22 > +# William Hubbs <williamh@g.o>
23 > +# @SUPPORTED_EAPIS: 7
24 > +# @BLURB: basic eclass for building software written as go modules
25 > +# @DESCRIPTION:
26 > +# This eclass provides basic settings and functions
27 > +# needed by all software written in the go programming language that uses
28 > +# go modules.
29 > +#
30 > +# You will know the software you are packaging uses modules because
31 > +# it will have files named go.sum and go.mod in its top-level source
32 > +# directory. If it does not have these files, use the golang-* eclasses.
33 > +#
34 > +# If it has these files and a directory named vendor in its top-level
35 > +# source directory, you only need to inherit the eclass since upstream
36 > +# is vendoring the dependencies.
37 > +#
38 > +# If it does not have a vendor directory, you should use the EGO_VENDOR
39 > +# variable and the go-module_vendor_uris function as shown in the
40 > +# example below to handle dependencies.
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 > +#
50 > +# inherit go-module
51 > +#
52 > +# EGO_VENDOR=(
53 > +# "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
54 > +# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
55 > +# )
56 > +#
57 > +# SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
58 > +# $(go-module_vendor_uris)"
59 > +#
60 > +# @CODE
61 > +
62 > +case ${EAPI:-0} in
63 > + 7) ;;
64 > + *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
65 > +esac
66 > +
67 > +if [[ -z ${_GO_MODULE} ]]; then
68 > +
69 > +_GO_MODULE=1
70 > +
71 > +BDEPEND=">=dev-lang/go-1.12"
72 > +
73 > +# Force go to build in module mode.
74 > +# In this mode the GOPATH environment variable is ignored.
75 > +# this will become the default in the future.
76 > +export GO111MODULE=on
77 > +
78 > +# The following go flags should be used for all builds.
79 > +# -mod=vendor stopps downloading of dependencies from the internet.
80 > +# -v prints the names of packages as they are compiled
81 > +# -x prints commands as they are executed
82 > +export GOFLAGS="-mod=vendor -v -x"
83 > +
84 > +# Do not complain about CFLAGS etc since go projects do not use them.
85 > +QA_FLAGS_IGNORED='.*'
86 > +
87 > +# Go packages should not be stripped with strip(1).
88 > +RESTRICT="strip"
89 > +
90 > +EXPORT_FUNCTIONS src_unpack pkg_postinst
91 > +
92 > +# @ECLASS-VARIABLE: EGO_VENDOR
93 > +# @DESCRIPTION:
94 > +# This variable contains a list of vendored packages.
95 > +# The items of this array are strings that contain the
96 > +# import path and the git commit hash for a vendored package.
97 > +# If the import path does not start with github.com, the third argument
98 > +# can be used to point to a github repository.
99 > +
100 > +# @FUNCTION: go-module_vendor_uris
101 > +# @DESCRIPTION:
102 > +# Convert the information in EGO_VENDOR to a format suitable for
103 > +# SRC_URI.
104 > +# A call to this function should be added to SRC_URI in your ebuild if
105 > +# the upstream package does not include vendored dependencies.
106 > +go-module_vendor_uris() {
107 > + local hash import line repo _
108 > + for line in "${EGO_VENDOR[@]}"; do
109 > + read -r import hash repo _ <<< "${line}"
110 > + if [[ "$_" ]]; then
111
112 Nit: you don't need to quote stuff inside double brackets (except for
113 arg to '==', if you don't want wildcards to work there).
114
115 > + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
116 > + eerror "${line}"
117 > + die "Invalid EGO_VENDOR format"
118 > + fi
119 > + : "${repo:=${import}}"
120 > + echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz"
121 > + done
122 > +}
123 > +
124 > +# @FUNCTION: go-module_src_unpack
125 > +# @DESCRIPTION:
126 > +# Extract all archives in ${a} which are not nentioned in ${EGO_VENDOR}
127 > +# to their usual locations then extract all archives mentioned in
128 > +# ${EGO_VENDOR} to ${S}/vendor.
129 > +go-module_src_unpack() {
130 > + debug-print-function ${FUNCNAME} "$@"
131 > + local f hash import line repo tarball vendor_tarballs _
132 > + vendor_tarballs=
133
134 Why not make it an array?
135
136 > + for line in "${EGO_VENDOR[@]}"; do
137 > + read -r import hash repo _ <<< "${line}"
138 > + if [[ "$_" ]]; then
139 > + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
140 > + eerror "${line}"
141 > + die "Invalid EGO_VENDOR format"
142 > + fi
143 > + : "${repo:=${import}}"
144 > + tarball=${repo//\//-}-${hash}.tar.gz
145 > + vendor_tarballs+=" ${tarball}"
146 > + done
147 > + for f in $A; do
148 > + [[ -n "${vendor_tarballs}" ]] && has $f $vendor_tarballs && continue
149 > + unpack $f
150 > + done
151 > +
152 > + [[ -z "${vendor_tarballs}" ]] && return
153 > + ebegin "Vendoring dependencies"
154 > + for line in "${EGO_VENDOR[@]}"; do
155 > + read -r import hash repo _ <<< "${line}"
156 > + : "${repo:=${import}}"
157 > + tarball=${repo//\//-}-${hash}.tar.gz
158 > + einfo "Vendoring ${import} ${tarball}"
159 > + rm -fr "${S}/vendor/${import}" || die
160 > + mkdir -p "${S}/vendor/${import}" || die
161 > + tar -C "${S}/vendor/${import}" -x --strip-components 1 \
162 > + -f "${DISTDIR}/${tarball}" || die
163 > + done
164 > + eend
165
166 You have output between ebegin and eend, so eend will go on the wrong
167 line. So just replace it with einfo, I guess. You may use ebegin/eend
168 for individual tarballs, and I suppose add some indentation to it.
169
170 > +}
171 > +
172 > +# @FUNCTION: go-module_live_vendor
173 > +# @DESCRIPTION:
174 > +# This function is used in live ebuilds to vendor the dependencies when
175 > +# upstream doesn't vendor them.
176 > +go-module_live_vendor() {
177 > + debug-print-function ${FUNCNAME} "$@"
178 > +
179 > + has live ${PROPERTIES} ||
180 > + die "${FUNCNAME} only allowed in live ebuilds"
181 > + [[ "${EBUILD_PHASE}" == unpack ]] ||
182 > + die "${FUNCNAME} only allowed in src_unpack"
183 > + [[ -d "${S}"/vendor ]] ||
184 > + die "${FUNCNAME} only allowed when upstream isn't vendoring"
185 > +
186 > + cd "${S}" || die
187
188 Didn't I suggest pushd/popd?
189
190 > + go mod vendor || die
191 > +}
192 > +
193 > +# @FUNCTION: go-module_pkg_postinst
194 > +# @DESCRIPTION:
195 > +# Display a warning about security updates for Go programs.
196 > +go-module_pkg_postinst() {
197 > + debug-print-function ${FUNCNAME} "$@"
198 > + ewarn "${PN} is written in the Go programming language."
199 > + ewarn "Since this language is statically linked, security"
200 > + ewarn "updates will be handled in individual packages and will be"
201 > + ewarn "difficult for us to track as a distribution."
202 > + ewarn "For this reason, please update any go packages asap when new"
203 > + ewarn "versions enter the tree or go stable if you are running the"
204 > + ewarn "stable tree."
205 > +}
206 > +
207 > +fi
208
209 --
210 Best regards,
211 Michał Górny

Attachments

File name MIME type
signature.asc application/pgp-signature