1 # Copyright 2019 Gentoo authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: go-module.eclass
6 # William Hubbs <williamh@gentoo.org>
8 # @BLURB: basic eclass for building software written as go modules
10 # This eclass provides basic settings and functions
11 # needed by all software written in the go programming language that uses
14 # You will know the software you are packaging uses modules because
15 # it will have files named go.sum and go.mod in its top-level source
16 # directory. If it does not have these files, use the golang-* eclasses.
18 # If it has these files and a directory named vendor in its top-level
19 # source directory, you only need to inherit the eclass since upstream
20 # is vendoring the dependencies.
22 # If it does not have a vendor directory, you should use the EGO_VENDOR
23 # variable and the go-module_vendor_uris function as shown in the
24 # example below to handle dependencies.
26 # Since Go programs are statically linked, it is important that your ebuild's
27 # LICENSE= setting includes the licenses of all statically linked
28 # dependencies. So please make sure it is accurate.
37 # "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
38 # "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
41 # SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
42 # $(go-module_vendor_uris)"
48 *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
51 if [[ -z ${_GO_MODULE} ]]; then
55 BDEPEND=">=dev-lang/go-1.12"
57 # Force go to build in module mode.
58 # In this mode the GOPATH environment variable is ignored.
59 # this will become the default in the future.
62 # The following go flags should be used for all builds.
63 # -mod=vendor stopps downloading of dependencies from the internet.
64 # -v prints the names of packages as they are compiled
65 # -x prints commands as they are executed
66 export GOFLAGS="-mod=vendor -v -x"
68 # Do not complain about CFLAGS etc since go projects do not use them.
71 # Go packages should not be stripped with strip(1).
74 EXPORT_FUNCTIONS src_unpack pkg_postinst
76 # @ECLASS-VARIABLE: EGO_VENDOR
78 # This variable contains a list of vendored packages.
79 # The items of this array are strings that contain the
80 # import path and the git commit hash for a vendored package.
81 # If the import path does not start with github.com, the third argument
82 # can be used to point to a github repository.
84 # @FUNCTION: go-module_vendor_uris
86 # Convert the information in EGO_VENDOR to a format suitable for
88 # A call to this function should be added to SRC_URI in your ebuild if
89 # the upstream package does not include vendored dependencies.
90 go-module_vendor_uris() {
91 local hash import line repo x
92 for line in "${EGO_VENDOR[@]}"; do
93 read -r import hash repo x <<< "${line}"
95 eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
97 eerror "Trailing information is: \"$x\""
98 die "Invalid EGO_VENDOR format"
100 : "${repo:=${import}}"
101 echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz"
105 # @FUNCTION: go-module_src_unpack
107 # Extract all archives in ${a} which are not nentioned in ${EGO_VENDOR}
108 # to their usual locations then extract all archives mentioned in
109 # ${EGO_VENDOR} to ${S}/vendor.
110 go-module_src_unpack() {
111 debug-print-function ${FUNCNAME} "$@"
112 local f hash import line repo tarball vendor_tarballs x
114 for line in "${EGO_VENDOR[@]}"; do
115 read -r import hash repo x <<< "${line}"
117 eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
119 die "Invalid EGO_VENDOR format"
121 : "${repo:=${import}}"
122 vendor_tarballs+=("${repo//\//-}-${hash}.tar.gz")
125 [[ -n ${vendor_tarballs[*]} ]] && has "$f" "${vendor_tarballs[@]}" &&
130 [[ -z ${vendor_tarballs[*]} ]] && return
131 for line in "${EGO_VENDOR[@]}"; do
132 read -r import hash repo _ <<< "${line}"
133 : "${repo:=${import}}"
134 tarball=${repo//\//-}-${hash}.tar.gz
135 ebegin "Vendoring ${import} ${tarball}"
136 rm -fr "${S}/vendor/${import}" || die
137 mkdir -p "${S}/vendor/${import}" || die
138 tar -C "${S}/vendor/${import}" -x --strip-components 1 \
139 -f "${DISTDIR}/${tarball}" || die
144 # @FUNCTION: go-module_live_vendor
146 # This function is used in live ebuilds to vendor the dependencies when
147 # upstream doesn't vendor them.
148 go-module_live_vendor() {
149 debug-print-function ${FUNCNAME} "$@"
151 has live ${PROPERTIES} ||
152 die "${FUNCNAME} only allowed in live ebuilds"
153 [[ "${EBUILD_PHASE}" == unpack ]] ||
154 die "${FUNCNAME} only allowed in src_unpack"
155 [[ -d "${S}"/vendor ]] ||
156 die "${FUNCNAME} only allowed when upstream isn't vendoring"
158 pushd "${S}" >& /dev/null || die
160 popd >& /dev/null || die
163 # @FUNCTION: go-module_pkg_postinst
165 # Display a warning about security updates for Go programs.
166 go-module_pkg_postinst() {
167 debug-print-function ${FUNCNAME} "$@"
168 [[ -n ${REPLACING_VERSIONS} ]] && return 0
169 ewarn "${PN} is written in the Go programming language."
170 ewarn "Since this language is statically linked, security"
171 ewarn "updates will be handled in individual packages and will be"
172 ewarn "difficult for us to track as a distribution."
173 ewarn "For this reason, please update any go packages asap when new"
174 ewarn "versions enter the tree or go stable if you are running the"