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: new eclass for go modules
Date: Tue, 24 Sep 2019 18:09:56
Message-Id: 20190924180847.17149-2-williamh@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/1] new eclass for go modules (another proposal) by William Hubbs
1 This eclass includes the basic settings and src_unpack/pkg_postinst
2 functions for Go modules.
3
4 Signed-off-by: William Hubbs <williamh@g.o>
5 ---
6 eclass/go-module.eclass | 164 ++++++++++++++++++++++++++++++++++++++++
7 1 file changed, 164 insertions(+)
8 create mode 100644 eclass/go-module.eclass
9
10 diff --git a/eclass/go-module.eclass b/eclass/go-module.eclass
11 new file mode 100644
12 index 00000000000..fcfa5ba643c
13 --- /dev/null
14 +++ b/eclass/go-module.eclass
15 @@ -0,0 +1,164 @@
16 +# Copyright 2019 gentoo authors
17 +# Distributed under the terms of the GNU General Public License v2
18 +
19 +# @ECLASS: go-module.eclass
20 +# @MAINTAINER:
21 +# William Hubbs <williamh@g.o>
22 +# @SUPPORTED_EAPIS: 7
23 +# @BLURB: basic eclass for building software written in the go
24 +# programming language that uses 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 vendor 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 [[ -n ${repo} ]]; then
111 + echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz"
112 + else
113 + echo "https://${import}/archive/${hash}.tar.gz -> ${import//\//-}-${hash}.tar.gz"
114 + fi
115 + done
116 +}
117 +
118 +# @FUNCTION: go-module_src_unpack
119 +# @DESCRIPTION:
120 +# Extract all archives in ${a} which are not nentioned in ${EGO_VENDOR}
121 +# to their usual locations then extract all archives mentioned in
122 +# ${EGO_VENDOR} to ${S}/vendor.
123 +go-module_src_unpack() {
124 + debug-print-function ${FUNCNAME} "$@"
125 + local f hash import line repo tarball vendor_uri
126 + vendor_uri="$(go-module_vendor_uris)"
127 + for f in $A; do
128 + [[ $vendor_uri == *"$f"* ]] && continue
129 + unpack $f
130 + done
131 +
132 + for line in "${EGO_VENDOR[@]}"; do
133 + read -r import hash repo _ <<< "${line}"
134 + if [[ -n ${repo} ]]; then
135 + tarball=${repo//\//-}-${hash}.tar.gz
136 + else
137 + tarball=${import//\//-}-${hash}.tar.gz
138 + fi
139 + einfo "Vendoring ${import} ${tarball}"
140 + rm -fr "${S}/vendor/${import}" || die
141 + mkdir -p "${S}/vendor/${import}" || die
142 + tar -C "${S}/vendor/${import}" -x --strip-components 1\
143 + -f "${DISTDIR}/${tarball}" || die
144 + done
145 +}
146 +
147 +# @FUNCTION: go-module_live_vendor
148 +# @DESCRIPTION:
149 +# This function is used in live ebuilds to vendor the dependencies when
150 +# upstream doesn't vendor them.
151 +go-module_live_vendor() {
152 + debug-print-function ${FUNCNAME} "$@"
153 +
154 + [[ "${PV}" == *9999* ]] ||
155 + die "${FUNCNAME} only allowed in live/9999 ebuilds"
156 + [[ "${EBUILD_PHASE}" == unpack ]] ||
157 + die "${FUNCNAME} only allowed in src_unpack"
158 + [[ -d "${S}"/vendor ]] ||
159 + die "${FUNCNAME} only allowed when upstream isn't vendoring"
160 +
161 + cd "${S}" || die
162 + go mod vendor || die
163 +}
164 +
165 +# @FUNCTION: go-module_pkg_postinst
166 +# @DESCRIPTION:
167 +# Display a warning about security updates for Go programs.
168 +go-module_pkg_postinst() {
169 + debug-print-function ${FUNCNAME} "$@"
170 + ewarn "${PN} is written in the Go programming language."
171 + ewarn "Since this language is statically linked, security"
172 + ewarn "updates will be handled in individual packages and will be"
173 + ewarn "difficult for us to track as a distribution."
174 + ewarn "For this reason, please update any go packages asap when new"
175 + ewarn "versions enter the tree or go stable if you are running the"
176 + ewarn "stable tree."
177 +}
178 +
179 +fi
180 --
181 2.21.0

Replies