ecm.eclass: Set correct KFMIN default for kde-frameworks/*
[gentoo.git] / eclass / go-module.eclass
1 # Copyright 2019 Gentoo authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: go-module.eclass
5 # @MAINTAINER:
6 # William Hubbs <williamh@gentoo.org>
7 # @SUPPORTED_EAPIS: 7
8 # @BLURB: basic eclass for building software written as go modules
9 # @DESCRIPTION:
10 # This eclass provides basic settings and functions
11 # needed by all software written in the go programming language that uses
12 # go modules.
13 #
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.
17 #
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.
21 #
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.
25 #
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.
29 #
30 # @EXAMPLE:
31 #
32 # @CODE
33 #
34 # inherit go-module
35 #
36 # EGO_VENDOR=(
37 #       "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
38 # "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
39 # )
40 #
41 # SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
42 # $(go-module_vendor_uris)"
43 #
44 # @CODE
45
46 case ${EAPI:-0} in
47         7) ;;
48         *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
49 esac
50
51 if [[ -z ${_GO_MODULE} ]]; then
52
53 _GO_MODULE=1
54
55 BDEPEND=">=dev-lang/go-1.12"
56
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.
60 export GO111MODULE=on
61
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"
67
68 # Do not complain about CFLAGS etc since go projects do not use them.
69 QA_FLAGS_IGNORED='.*'
70
71 # Go packages should not be stripped with strip(1).
72 RESTRICT="strip"
73
74 EXPORT_FUNCTIONS src_unpack pkg_postinst
75
76 # @ECLASS-VARIABLE: EGO_VENDOR
77 # @DESCRIPTION:
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.
83
84 # @FUNCTION: go-module_vendor_uris
85 # @DESCRIPTION:
86 # Convert the information in EGO_VENDOR to a format suitable for
87 # SRC_URI.
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}"
94                 if [[ -n $x ]]; then
95                         eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
96                         eerror "${line}"
97                         eerror "Trailing information is: \"$x\""
98                         die "Invalid EGO_VENDOR format"
99                 fi
100                 : "${repo:=${import}}"
101                 echo "https://${repo}/archive/${hash}.tar.gz -> ${repo//\//-}-${hash}.tar.gz"
102         done
103 }
104
105 # @FUNCTION: go-module_src_unpack
106 # @DESCRIPTION:
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
113         vendor_tarballs=()
114         for line in "${EGO_VENDOR[@]}"; do
115                 read -r import hash repo x <<< "${line}"
116                 if [[ -n $x ]]; then
117                         eerror "Trailing information in EGO_VENDOR in ${P}.ebuild"
118                         eerror "${line}"
119                         die "Invalid EGO_VENDOR format"
120                 fi
121                 : "${repo:=${import}}"
122                 vendor_tarballs+=("${repo//\//-}-${hash}.tar.gz")
123         done
124         for f in $A; do
125                 [[ -n ${vendor_tarballs[*]} ]] && has "$f" "${vendor_tarballs[@]}" &&
126                         continue
127                 unpack "$f"
128         done
129
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
140                 eend
141         done
142 }
143
144 # @FUNCTION: go-module_live_vendor
145 # @DESCRIPTION:
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} "$@"
150
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"
157
158         pushd "${S}" >& /dev/null || die
159         go mod vendor || die
160         popd >& /dev/null || die
161 }
162
163 # @FUNCTION: go-module_pkg_postinst
164 # @DESCRIPTION:
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"
175         ewarn "stable tree."
176 }
177
178 fi