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 v4 1/1] go-module.eclass: new eclass for go modules
Date: Mon, 30 Sep 2019 17:05:21
Message-Id: 20190930170423.16528-2-williamh@gentoo.org
In Reply to: [gentoo-dev] [PATCH v4 0/1] go modules eclass round 4 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 | 177 ++++++++++++++++++++++++++++++++++++++++
7 1 file changed, 177 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..e6152966911
13 --- /dev/null
14 +++ b/eclass/go-module.eclass
15 @@ -0,0 +1,177 @@
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 as go modules
24 +# @DESCRIPTION:
25 +# This eclass provides basic settings and functions
26 +# needed by all software written in the go programming language that uses
27 +# go modules.
28 +#
29 +# You will know the software you are packaging uses modules because
30 +# it will have files named go.sum and go.mod in its top-level source
31 +# directory. If it does not have these files, use the golang-* eclasses.
32 +#
33 +# If it has these files and a directory named vendor in its top-level
34 +# source directory, you only need to inherit the eclass since upstream
35 +# is vendoring the dependencies.
36 +#
37 +# If it does not have a vendor directory, you should use the EGO_VENDOR
38 +# variable and the go-module_vendor_uris function as shown in the
39 +# example below to handle dependencies.
40 +#
41 +# Since Go programs are statically linked, it is important that your ebuild's
42 +# LICENSE= setting includes the licenses of all statically linked
43 +# dependencies. So please make sure it is accurate.
44 +#
45 +# @EXAMPLE:
46 +#
47 +# @CODE
48 +#
49 +# inherit go-module
50 +#
51 +# EGO_VENDOR=(
52 +# "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
53 +# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
54 +# )
55 +#
56 +# SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
57 +# $(go-module_vendor_uris)"
58 +#
59 +# @CODE
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 +# @FUNCTION: go-module_vendor_uris
100 +# @DESCRIPTION:
101 +# Convert the information in EGO_VENDOR to a format suitable for
102 +# SRC_URI.
103 +# A call to this function should be added to SRC_URI in your ebuild if
104 +# the upstream package does not include vendored dependencies.
105 +go-module_vendor_uris() {
106 + local hash import line repo x
107 + for line in "${EGO_VENDOR[@]}"; do
108 + read -r import hash repo x <<< "${line}"
109 + if [[ -n $x ]]; then
110 + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
111 + eerror "${line}"
112 + eerror "Trailing information is: \"$x\""
113 + die "Invalid EGO_VENDOR format"
114 + fi
115 + : "${repo:=${import}}"
116 + echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz"
117 + done
118 +}
119 +
120 +# @FUNCTION: go-module_src_unpack
121 +# @DESCRIPTION:
122 +# Extract all archives in ${a} which are not nentioned in ${EGO_VENDOR}
123 +# to their usual locations then extract all archives mentioned in
124 +# ${EGO_VENDOR} to ${S}/vendor.
125 +go-module_src_unpack() {
126 + debug-print-function ${FUNCNAME} "$@"
127 + local f hash import line repo tarball vendor_tarballs x
128 + vendor_tarballs=()
129 + for line in "${EGO_VENDOR[@]}"; do
130 + read -r import hash repo x <<< "${line}"
131 + if [[ -n $x ]]; then
132 + eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
133 + eerror "${line}"
134 + die "Invalid EGO_VENDOR format"
135 + fi
136 + : "${repo:=${import}}"
137 + vendor_tarballs+=("${repo//\//-}-${hash}.tar.gz")
138 + done
139 + for f in $A; do
140 + [[ -n ${vendor_tarballs[*]} ]] && has "$f" "${vendor_tarballs[@]}" &&
141 + continue
142 + unpack "$f"
143 + done
144 +
145 + [[ -z ${vendor_tarballs[*]} ]] && return
146 + for line in "${EGO_VENDOR[@]}"; do
147 + read -r import hash repo _ <<< "${line}"
148 + : "${repo:=${import}}"
149 + tarball=${repo//\//-}-${hash}.tar.gz
150 + ebegin "Vendoring ${import} ${tarball}"
151 + rm -fr "${S}/vendor/${import}" || die
152 + mkdir -p "${S}/vendor/${import}" || die
153 + tar -C "${S}/vendor/${import}" -x --strip-components 1 \
154 + -f "${DISTDIR}/${tarball}" || die
155 + eend
156 + done
157 +}
158 +
159 +# @FUNCTION: go-module_live_vendor
160 +# @DESCRIPTION:
161 +# This function is used in live ebuilds to vendor the dependencies when
162 +# upstream doesn't vendor them.
163 +go-module_live_vendor() {
164 + debug-print-function ${FUNCNAME} "$@"
165 +
166 + has live ${PROPERTIES} ||
167 + die "${FUNCNAME} only allowed in live ebuilds"
168 + [[ "${EBUILD_PHASE}" == unpack ]] ||
169 + die "${FUNCNAME} only allowed in src_unpack"
170 + [[ -d "${S}"/vendor ]] ||
171 + die "${FUNCNAME} only allowed when upstream isn't vendoring"
172 +
173 + pushd "${S}" >& /dev/null || die
174 + go mod vendor || die
175 + popd >& /dev/null || die
176 +}
177 +
178 +# @FUNCTION: go-module_pkg_postinst
179 +# @DESCRIPTION:
180 +# Display a warning about security updates for Go programs.
181 +go-module_pkg_postinst() {
182 + debug-print-function ${FUNCNAME} "$@"
183 + ewarn "${PN} is written in the Go programming language."
184 + ewarn "Since this language is statically linked, security"
185 + ewarn "updates will be handled in individual packages and will be"
186 + ewarn "difficult for us to track as a distribution."
187 + ewarn "For this reason, please update any go packages asap when new"
188 + ewarn "versions enter the tree or go stable if you are running the"
189 + ewarn "stable tree."
190 +}
191 +
192 +fi
193 --
194 2.21.0