Gentoo Archives: gentoo-commits

From: William Hubbs <williamh@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
Date: Mon, 30 Sep 2019 22:20:22
Message-Id: 1569881944.d6385200b27bc5fcce3826fc31aecc51bcb1e19c.williamh@gentoo
1 commit: d6385200b27bc5fcce3826fc31aecc51bcb1e19c
2 Author: William Hubbs <williamh <AT> gentoo <DOT> org>
3 AuthorDate: Mon Sep 30 22:17:26 2019 +0000
4 Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
5 CommitDate: Mon Sep 30 22:19:04 2019 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=d6385200
7
8 go-module.eclass: new eclass for go modules
9
10 This eclass includes the basic settings and src_unpack/pkg_postinst
11 functions for software written in the Go programming language that uses
12 Go modules.
13
14 Signed-off-by: William Hubbs <williamh <AT> gentoo.org>
15
16 eclass/go-module.eclass | 177 ++++++++++++++++++++++++++++++++++++++++++++++++
17 1 file changed, 177 insertions(+)
18
19 diff --git a/eclass/go-module.eclass b/eclass/go-module.eclass
20 new file mode 100644
21 index 00000000000..e6152966911
22 --- /dev/null
23 +++ b/eclass/go-module.eclass
24 @@ -0,0 +1,177 @@
25 +# Copyright 2019 Gentoo authors
26 +# Distributed under the terms of the GNU General Public License v2
27 +
28 +# @ECLASS: go-module.eclass
29 +# @MAINTAINER:
30 +# William Hubbs <williamh@g.o>
31 +# @SUPPORTED_EAPIS: 7
32 +# @BLURB: basic eclass for building software written as go modules
33 +# @DESCRIPTION:
34 +# This eclass provides basic settings and functions
35 +# needed by all software written in the go programming language that uses
36 +# go modules.
37 +#
38 +# You will know the software you are packaging uses modules because
39 +# it will have files named go.sum and go.mod in its top-level source
40 +# directory. If it does not have these files, use the golang-* eclasses.
41 +#
42 +# If it has these files and a directory named vendor in its top-level
43 +# source directory, you only need to inherit the eclass since upstream
44 +# is vendoring the dependencies.
45 +#
46 +# If it does not have a vendor directory, you should use the EGO_VENDOR
47 +# variable and the go-module_vendor_uris function as shown in the
48 +# example below to handle dependencies.
49 +#
50 +# Since Go programs are statically linked, it is important that your ebuild's
51 +# LICENSE= setting includes the licenses of all statically linked
52 +# dependencies. So please make sure it is accurate.
53 +#
54 +# @EXAMPLE:
55 +#
56 +# @CODE
57 +#
58 +# inherit go-module
59 +#
60 +# EGO_VENDOR=(
61 +# "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
62 +# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
63 +# )
64 +#
65 +# SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
66 +# $(go-module_vendor_uris)"
67 +#
68 +# @CODE
69 +
70 +case ${EAPI:-0} in
71 + 7) ;;
72 + *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
73 +esac
74 +
75 +if [[ -z ${_GO_MODULE} ]]; then
76 +
77 +_GO_MODULE=1
78 +
79 +BDEPEND=">=dev-lang/go-1.12"
80 +
81 +# Force go to build in module mode.
82 +# In this mode the GOPATH environment variable is ignored.
83 +# this will become the default in the future.
84 +export GO111MODULE=on
85 +
86 +# The following go flags should be used for all builds.
87 +# -mod=vendor stopps downloading of dependencies from the internet.
88 +# -v prints the names of packages as they are compiled
89 +# -x prints commands as they are executed
90 +export GOFLAGS="-mod=vendor -v -x"
91 +
92 +# Do not complain about CFLAGS etc since go projects do not use them.
93 +QA_FLAGS_IGNORED='.*'
94 +
95 +# Go packages should not be stripped with strip(1).
96 +RESTRICT="strip"
97 +
98 +EXPORT_FUNCTIONS src_unpack pkg_postinst
99 +
100 +# @ECLASS-VARIABLE: EGO_VENDOR
101 +# @DESCRIPTION:
102 +# This variable contains a list of vendored packages.
103 +# The items of this array are strings that contain the
104 +# import path and the git commit hash for a vendored package.
105 +# If the import path does not start with github.com, the third argument
106 +# can be used to point to a github repository.
107 +
108 +# @FUNCTION: go-module_vendor_uris
109 +# @DESCRIPTION:
110 +# Convert the information in EGO_VENDOR to a format suitable for
111 +# SRC_URI.
112 +# A call to this function should be added to SRC_URI in your ebuild if
113 +# the upstream package does not include vendored dependencies.
114 +go-module_vendor_uris() {
115 + local hash import line repo x
116 + for line in "${EGO_VENDOR[@]}"; do
117 + read -r import hash repo x <<< "${line}"
118 + if [[ -n $x ]]; then
119 + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
120 + eerror "${line}"
121 + eerror "Trailing information is: \"$x\""
122 + die "Invalid EGO_VENDOR format"
123 + fi
124 + : "${repo:=${import}}"
125 + echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz"
126 + done
127 +}
128 +
129 +# @FUNCTION: go-module_src_unpack
130 +# @DESCRIPTION:
131 +# Extract all archives in ${a} which are not nentioned in ${EGO_VENDOR}
132 +# to their usual locations then extract all archives mentioned in
133 +# ${EGO_VENDOR} to ${S}/vendor.
134 +go-module_src_unpack() {
135 + debug-print-function ${FUNCNAME} "$@"
136 + local f hash import line repo tarball vendor_tarballs x
137 + vendor_tarballs=()
138 + for line in "${EGO_VENDOR[@]}"; do
139 + read -r import hash repo x <<< "${line}"
140 + if [[ -n $x ]]; then
141 + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
142 + eerror "${line}"
143 + die "Invalid EGO_VENDOR format"
144 + fi
145 + : "${repo:=${import}}"
146 + vendor_tarballs+=("${repo//\//-}-${hash}.tar.gz")
147 + done
148 + for f in $A; do
149 + [[ -n ${vendor_tarballs[*]} ]] && has "$f" "${vendor_tarballs[@]}" &&
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