1 |
On Fri, 2019-09-27 at 10:07 -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 | 178 ++++++++++++++++++++++++++++++++++++++++ |
8 |
> 1 file changed, 178 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..84f707d8068 |
14 |
> --- /dev/null |
15 |
> +++ b/eclass/go-module.eclass |
16 |
> @@ -0,0 +1,178 @@ |
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 x |
108 |
> + for line in "${EGO_VENDOR[@]}"; do |
109 |
> + read -r import hash repo x <<< "${line}" |
110 |
> + if [[ -n $x ]]; then |
111 |
> + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild" |
112 |
> + eerror "${line}" |
113 |
> + eerror "Trailing information is: \"$x\"" |
114 |
> + die "Invalid EGO_VENDOR format" |
115 |
> + fi |
116 |
> + : "${repo:=${import}}" |
117 |
> + echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz" |
118 |
> + done |
119 |
> +} |
120 |
> + |
121 |
> +# @FUNCTION: go-module_src_unpack |
122 |
> +# @DESCRIPTION: |
123 |
> +# Extract all archives in ${a} which are not nentioned in ${EGO_VENDOR} |
124 |
> +# to their usual locations then extract all archives mentioned in |
125 |
> +# ${EGO_VENDOR} to ${S}/vendor. |
126 |
> +go-module_src_unpack() { |
127 |
> + debug-print-function ${FUNCNAME} "$@" |
128 |
> + local f hash import line repo tarball vendor_tarballs x |
129 |
> + vendor_tarballs=() |
130 |
> + for line in "${EGO_VENDOR[@]}"; do |
131 |
> + read -r import hash repo x <<< "${line}" |
132 |
> + if [[ -n $x ]]; then |
133 |
> + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild" |
134 |
> + eerror "${line}" |
135 |
> + die "Invalid EGO_VENDOR format" |
136 |
> + fi |
137 |
> + : "${repo:=${import}}" |
138 |
> + tarball=${repo//\//-}-${hash}.tar.gz |
139 |
|
140 |
Technically speaking, this variable seems unnecessary -- you can append |
141 |
the whole string straight to vendor_tarballs. |
142 |
|
143 |
> + vendor_tarballs+=("${tarball}") |
144 |
> + done |
145 |
> + for f in $A; do |
146 |
> + [[ -n ${vendor_tarballs[*]} ]] && has $f ${vendor_tarballs[@]} && |
147 |
|
148 |
Technically, at least "${vendor_tarballs[@]}" should be quoted. |
149 |
|
150 |
> + continue |
151 |
> + unpack $f |
152 |
> + done |
153 |
> + |
154 |
> + [[ -z ${vendor_tarballs[*]} ]] && return |
155 |
> + for line in "${EGO_VENDOR[@]}"; do |
156 |
> + read -r import hash repo _ <<< "${line}" |
157 |
> + : "${repo:=${import}}" |
158 |
> + tarball=${repo//\//-}-${hash}.tar.gz |
159 |
> + ebegin "Vendoring ${import} ${tarball}" |
160 |
> + rm -fr "${S}/vendor/${import}" || die |
161 |
> + mkdir -p "${S}/vendor/${import}" || die |
162 |
> + tar -C "${S}/vendor/${import}" -x --strip-components 1 \ |
163 |
> + -f "${DISTDIR}/${tarball}" || die |
164 |
> + eend |
165 |
> + done |
166 |
> +} |
167 |
> + |
168 |
> +# @FUNCTION: go-module_live_vendor |
169 |
> +# @DESCRIPTION: |
170 |
> +# This function is used in live ebuilds to vendor the dependencies when |
171 |
> +# upstream doesn't vendor them. |
172 |
> +go-module_live_vendor() { |
173 |
> + debug-print-function ${FUNCNAME} "$@" |
174 |
> + |
175 |
> + has live ${PROPERTIES} || |
176 |
> + die "${FUNCNAME} only allowed in live ebuilds" |
177 |
> + [[ "${EBUILD_PHASE}" == unpack ]] || |
178 |
> + die "${FUNCNAME} only allowed in src_unpack" |
179 |
> + [[ -d "${S}"/vendor ]] || |
180 |
> + die "${FUNCNAME} only allowed when upstream isn't vendoring" |
181 |
> + |
182 |
> + pushd "${S}" >& /dev/null || die |
183 |
> + go mod vendor || die |
184 |
> + popd >& /dev/null || die |
185 |
> +} |
186 |
> + |
187 |
> +# @FUNCTION: go-module_pkg_postinst |
188 |
> +# @DESCRIPTION: |
189 |
> +# Display a warning about security updates for Go programs. |
190 |
> +go-module_pkg_postinst() { |
191 |
> + debug-print-function ${FUNCNAME} "$@" |
192 |
> + ewarn "${PN} is written in the Go programming language." |
193 |
> + ewarn "Since this language is statically linked, security" |
194 |
> + ewarn "updates will be handled in individual packages and will be" |
195 |
> + ewarn "difficult for us to track as a distribution." |
196 |
> + ewarn "For this reason, please update any go packages asap when new" |
197 |
> + ewarn "versions enter the tree or go stable if you are running the" |
198 |
> + ewarn "stable tree." |
199 |
> +} |
200 |
> + |
201 |
> +fi |
202 |
|
203 |
-- |
204 |
Best regards, |
205 |
Michał Górny |