megacoffee.net Gentoo overlay - legacy mirror
We have moved to Git. This repository is only provided for compatibility with old installations. Learn how to migrate your installation here.
comparison eclass/x-mgcf-golang-common.eclass @ 249:0685ed0fb913
[sync] imported 2c6bf87..18f98a6 from upstream
commit 18f98a6f47cd77c1007b60dbfc7a838ffa99df0c
Author: Daniel Neugebauer <dneuge@energiequant.de>
Date: Mon Mar 27 21:11:43 2023 +0200
app-crypt/acmetool: version bump to 0.2.2
commit ee2df4ec1deab61cd7bb7de2cd13ffdd066cc41a
Author: Daniel Neugebauer <dneuge@energiequant.de>
Date: Mon Mar 27 19:30:58 2023 +0200
app-crypt/acmetool: split 0.2.1 from 9999, changed dependencies
commit f3c9cf1c7a24619446390b0b71bf5bc090dfce77
Author: Daniel Neugebauer <dneuge@energiequant.de>
Date: Mon Mar 27 19:29:33 2023 +0200
removed dependency to go-overlay
commit b2057a9b0ba65917f3840a6b121c1e78397c4723
Author: Daniel Neugebauer <dneuge@energiequant.de>
Date: Mon Mar 27 19:27:28 2023 +0200
golang eclasses: prefixed exported functions x-mgcf-
commit e8d02d1edc997fec9e0b1e5d053a5b82ca4accf2
Author: Daniel Neugebauer <dneuge@energiequant.de>
Date: Mon Mar 27 19:08:52 2023 +0200
renamed golang eclasses, updated maintainer, added disclaimer
commit 748ea4be8ee777bc788cb2a8801b118b41e6f8aa
Author: Daniel Neugebauer <dneuge@energiequant.de>
Date: Mon Mar 27 18:43:56 2023 +0200
copied eclasses for Go from go-overlay
Reason: These eclasses are used to build app-crypt/acmetool.
We previously imported go-overlay as a master repository to use the
eclasses defined there but the overlay has been removed from official
Gentoo overlay index due to inactivity. The removal broke new initial
checkouts for this repository as go-overlay can no longer be found.
Gentoo bug for MegaCoffee: https://bugs.gentoo.org/903167
Gentoo bug that caused removal: https://bugs.gentoo.org/864653
Original repository: https://github.com/Dr-Terrible/go-overlay
author | Migration Sync <gentoo-overlay@megacoffee.net> |
---|---|
date | Mon, 27 Mar 2023 19:15:01 +0000 |
parents | |
children | 32efd9258ce5 |
comparison
equal
deleted
inserted
replaced
248:566e55bdfa9a | 249:0685ed0fb913 |
---|---|
1 # Copyright 1999-2017 Gentoo Foundation | |
2 # Distributed under the terms of the GNU General Public License v2 | |
3 # $Id$ | |
4 | |
5 # @ECLASS: x-mgcf-golang-common.eclass | |
6 # @MAINTAINER: | |
7 # MegaCoffee Overlay <gentoo-overlay@megacoffee.net> | |
8 # @AUTHOR: | |
9 # Mauro Toffanin <toffanin.mauro@gmail.com> | |
10 # @BLURB: Base eclass for GoLang packages | |
11 # @SUPPORTED_EAPIS: 7 | |
12 # @DESCRIPTION: | |
13 # This eclass provides functionalities which are used by golang-single.eclass, | |
14 # golang-live.eclass, and as well as from ebuilds. | |
15 # | |
16 # This eclass should not be inherited directly from an ebuild. | |
17 # Instead, you should inherit golang-single or golang-live for GoLang packages. | |
18 | |
19 inherit eutils multiprocessing | |
20 | |
21 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
22 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
23 # !!! !!! | |
24 # !!! L E G A C Y E C L A S S !!! | |
25 # !!! !!! | |
26 # !!! DO NOT USE OUTSIDE OF MEGACOFFEE OVERLAY !!! | |
27 # !!! !!! | |
28 # !!! DO NOT USE FOR NEW EBUILDS !!! | |
29 # !!! !!! | |
30 # !!! This eclass has only been copied to megacoffee overlay from !!! | |
31 # !!! go-overlay: https://github.com/Dr-Terrible/go-overlay !!! | |
32 # !!! !!! | |
33 # !!! The only purpose is to keep currently depending ebuilds !!! | |
34 # !!! until they could be migrated to standard eclasses for Go, !!! | |
35 # !!! if suitable. !!! | |
36 # !!! !!! | |
37 # !!! go-overlay was previously imported by megacoffee until it !!! | |
38 # !!! got removed from Gentoo overlay index in March 2023, !!! | |
39 # !!! rendering megacoffee overlay unreachable as a result. !!! | |
40 # !!! !!! | |
41 # !!! Related bugs: https://bugs.gentoo.org/864653 !!! | |
42 # !!! https://bugs.gentoo.org/903167 !!! | |
43 # !!! !!! | |
44 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
45 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
46 | |
47 | |
48 if [[ -z ${_GOLANG_BASE_ECLASS} ]]; then | |
49 _GOLANG_BASE_ECLASS=1 | |
50 | |
51 # Silences repoman warnings. | |
52 case "${EAPI:-0}" in | |
53 7) | |
54 case "${GOLANG_PKG_DEPEND_ON_GO_SUBSLOT:-yes}" in | |
55 yes) | |
56 GO_DEPEND="dev-lang/go:0=" | |
57 ;; | |
58 *) | |
59 GO_DEPEND="dev-lang/go:*" | |
60 ;; | |
61 esac | |
62 ;; | |
63 *) | |
64 die "${ECLASS}: EAPI=${EAPI:-0} is not supported" ;; | |
65 esac | |
66 | |
67 DEPEND+=" ${GO_DEPEND}" | |
68 | |
69 RESTRICT+=" mirror strip" | |
70 | |
71 QA_FLAGS_IGNORED="usr/bin/.* | |
72 usr/sbin/.*" | |
73 | |
74 # @ECLASS-VARIABLE: GOLANG_PKG_NAME | |
75 # @DESCRIPTION: | |
76 # Sets the GoLang name for the generated package. | |
77 # GOLANG_PKG_NAME="${PN}" | |
78 GOLANG_PKG_NAME="${GOLANG_PKG_NAME:-${PN}}" | |
79 | |
80 # @ECLASS-VARIABLE: GOLANG_PKG_VERSION | |
81 # @DESCRIPTION: | |
82 # Sets the GoLang version for the generated package. | |
83 # GOLANG_PKG_VERSION="${PV}" | |
84 GOLANG_PKG_VERSION="${GOLANG_PKG_VERSION:-${PV/_pre/.pre}}" | |
85 | |
86 # @ECLASS-VARIABLE: GOLANG_PKG_IMPORTPATH | |
87 # @DESCRIPTION: | |
88 # Sets the remote import path for the generated package. | |
89 # GOLANG_PKG_IMPORTPATH="github.com/captObvious/" | |
90 GOLANG_PKG_IMPORTPATH="${GOLANG_PKG_IMPORTPATH:-}" | |
91 | |
92 # @ECLASS-VARIABLE: GOLANG_PKG_IMPORTPATH_ALIAS | |
93 # @DESCRIPTION: | |
94 # Sets an alias of the remote import path for the generated package. | |
95 # GOLANG_PKG_IMPORTPATH_ALIAS="privaterepo.com/captObvious/" | |
96 GOLANG_PKG_IMPORTPATH_ALIAS="${GOLANG_PKG_IMPORTPATH_ALIAS:=${GOLANG_PKG_IMPORTPATH}}" | |
97 | |
98 # @ECLASS-VARIABLE: GOLANG_PKG_ARCHIVEPREFIX | |
99 # @DESCRIPTION: | |
100 # Sets the archive prefix for the file URI of the package. | |
101 # Most projects hosted on GitHub's mirrors provide archives with prefix as | |
102 # 'v' or 'source-', other hosted services offer different archive formats. | |
103 # This eclass defaults to an empty prefix. | |
104 GOLANG_PKG_ARCHIVEPREFIX="${GOLANG_PKG_ARCHIVEPREFIX:-}" | |
105 | |
106 # @ECLASS-VARIABLE: GOLANG_PKG_ARCHIVESUFFIX | |
107 # @DESCRIPTION: | |
108 # Sets the archive suffix for the file URI of the package. | |
109 # Most projects hosted on GitHub's mirrors provide archives with suffix as | |
110 # '.tar.gz' or '.zip', other hosted services offer different archive formats. | |
111 # This eclass defaults to '.tar.gz'. | |
112 GOLANG_PKG_ARCHIVESUFFIX="${GOLANG_PKG_ARCHIVESUFFIX:=".tar.gz"}" | |
113 | |
114 # @ECLASS-VARIABLE: GOLANG_PKG_OUTPUT_NAME | |
115 # @DESCRIPTION: | |
116 # Specifies the output file name of the package. | |
117 # If not set, it derives from the name of the package, such as $GOLANG_PKG_NAME. | |
118 # This eclass defaults to $PN. | |
119 GOLANG_PKG_OUTPUT_NAME="${GOLANG_PKG_OUTPUT_NAME:=${PN}}" | |
120 | |
121 # @ECLASS-VARIABLE: GOLANG_PKG_BUILDPATH | |
122 # @DESCRIPTION: | |
123 # Specifies a go source file to be compiled as a single main package. | |
124 # This eclass defaults to an empty value. | |
125 # This eclass defaults to "/..." when the user declares GOLANG_PKG_IS_MULTIPLE=1 | |
126 GOLANG_PKG_BUILDPATH="${GOLANG_PKG_BUILDPATH:-}" | |
127 | |
128 # @ECLASS-VARIABLE: GOLANG_PKG_INSTALLPATH | |
129 # @DESCRIPTION: | |
130 # Sets the root path into which a binary, or a list of binaries, will be | |
131 # installed (e.x.: ${GOLANG_PKG_INSTALLPATH}/bin). | |
132 # This eclass defaults to "/usr" | |
133 GOLANG_PKG_INSTALLPATH="${GOLANG_PKG_INSTALLPATH:="/usr"}" | |
134 | |
135 # @ECLASS-VARIABLE: GOLANG_PKG_INSTALLSUFFIX | |
136 # @DESCRIPTION: | |
137 # Sets a suffix to use in the name of the package installation directory. | |
138 # This eclass defaults to an empty install suffix. | |
139 GOLANG_PKG_INSTALLSUFFIX="${GOLANG_PKG_INSTALLSUFFIX:-}" | |
140 | |
141 # @ECLASS-VARIABLE: GOLANG_PKG_IS_MULTIPLE | |
142 # @DESCRIPTION: | |
143 # Set to enable the building of multiple packages from a single import path. | |
144 | |
145 # @ECLASS-VARIABLE: GOLANG_PKG_HAVE_TEST | |
146 # @DEFAULT_UNSET | |
147 # @DESCRIPTION: | |
148 # Set to enable the execution of automated testing. | |
149 | |
150 # @ECLASS-VARIABLE: GOLANG_PKG_HAVE_TEST_RACE | |
151 # @DEFAULT_UNSET | |
152 # @DESCRIPTION: | |
153 # Set to enable the execution of automated testing with support for | |
154 # data race detection. | |
155 | |
156 # @ECLASS-VARIABLE: GOLANG_PKG_USE_CGO | |
157 # @DEFAULT_UNSET | |
158 # @DESCRIPTION: | |
159 # Set to enable the compilation of the package with CGO. | |
160 | |
161 # @ECLASS-VARIABLE: GOLANG_PKG_USE_GENERATE | |
162 # @DEFAULT_UNSET | |
163 # @DESCRIPTION: | |
164 # Set to run commands described by directives within existing golang files. | |
165 | |
166 # @ECLASS-VARIABLE: GOLANG_PKG_DEPEND_ON_GO_SUBSLOT | |
167 # @DESCRIPTION: | |
168 # Set to ensure the package does depend on the dev-lang/go subslot value. | |
169 # Possible values: {yes,no} | |
170 # This eclass defaults to "no" | |
171 GOLANG_PKG_DEPEND_ON_GO_SUBSLOT=${GOLANG_PKG_DEPEND_ON_GO_SUBSLOT:="no"} | |
172 | |
173 # @ECLASS-VARIABLE: GOLANG_PKG_LDFLAGS | |
174 # @DESCRIPTION: | |
175 # Sets the linker arguments to pass to 5l, 6l, or 8l. | |
176 # This eclass defaults to an empty list. | |
177 GOLANG_PKG_LDFLAGS="${GOLANG_PKG_LDFLAGS:-}" | |
178 | |
179 # @ECLASS-VARIABLE: GOLANG_PKG_TAGS | |
180 # @DESCRIPTION: | |
181 # Sets the list of build tags during the build. | |
182 # This eclass defaults to an empty list. | |
183 GOLANG_PKG_TAGS="${GOLANG_PKG_TAGS:-}" | |
184 | |
185 # @ECLASS-VARIABLE: GOLANG_PKG_VENDOR | |
186 # @DESCRIPTION: | |
187 # Sets additional standard Go workspaces to be appended to the environment | |
188 # variable GOPATH, as described in http://golang.org/doc/code.html. | |
189 # This eclass defaults to an empty list. | |
190 GOLANG_PKG_VENDOR=() | |
191 | |
192 # @ECLASS-VARIABLE: GOLANG_PKG_STATIK | |
193 # @DESCRIPTION: | |
194 # Sets the arguments to pass to dev-go/statik. | |
195 # This eclass defaults to an empty list. | |
196 GOLANG_PKG_STATIK="${GOLANG_PKG_STATIK:-}" | |
197 | |
198 # @ECLASS-VARIABLE: GOLANG_PKG_USE_MODULES | |
199 # @DESCRIPTION: | |
200 # Set to enable the compilation of the package with Go modules support. | |
201 | |
202 | |
203 # @ECLASS-VARIABLE: GO | |
204 # @DEFAULT_UNSET | |
205 # @DESCRIPTION: | |
206 # The absolute path to the current GoLang interpreter. | |
207 # | |
208 # This variable is set automatically after calling golang_setup(). | |
209 # | |
210 # Default value: | |
211 # @CODE | |
212 # /usr/bin/go | |
213 # @CODE | |
214 | |
215 # @ECLASS-VARIABLE: EGO | |
216 # @DEFAULT_UNSET | |
217 # @DESCRIPTION: | |
218 # The executable name of the current GoLang interpreter. | |
219 # | |
220 # This variable is set automatically after calling golang_setup(). | |
221 # | |
222 # Default value: | |
223 # @CODE | |
224 # go | |
225 # @CODE | |
226 | |
227 # @ECLASS-VARIABLE: PATCHES | |
228 # @DEFAULT_UNSET | |
229 # @DESCRIPTION: | |
230 # Array variable containing all the patches to be applied. This variable | |
231 # is expected to be defined in the global scope of ebuilds. Make sure to | |
232 # specify the full path. This variable is used in src_prepare phase. | |
233 # | |
234 # Example: | |
235 # @CODE | |
236 # PATCHES=( | |
237 # "${FILESDIR}/mypatch.patch" | |
238 # "${FILESDIR}/mypatch2.patch" | |
239 # ) | |
240 # @CODE | |
241 | |
242 | |
243 | |
244 # Adds gccgo as a compile-time dependency when GOLANG_PKG_USE_CGO is set. | |
245 #[[ -n ${GOLANG_PKG_USE_CGO} ]] && DEPEND+=" >=sys-devel/gcc-4.8.4[go]" | |
246 | |
247 # Adds dev-go/statik as a compile-time dependency when GOLANG_PKG_STATIK is set. | |
248 [[ -n ${GOLANG_PKG_STATIK} ]] && DEPEND+=" dev-go/statik" | |
249 | |
250 # Validates GOLANG_PKG_IMPORTPATH. | |
251 if [[ -z ${GOLANG_PKG_IMPORTPATH} ]]; then | |
252 eerror "The remote import path for this package has not been declared" | |
253 die "Mandatory variable GOLANG_PKG_IMPORTPATH is unset" | |
254 fi | |
255 | |
256 # Forces a multiple package build when user specifies GOLANG_PKG_IS_MULTIPLE=1. | |
257 if [[ -n ${GOLANG_PKG_IS_MULTIPLE} && -z ${GOLANG_PKG_BUILDPATH} ]]; then | |
258 GOLANG_PKG_BUILDPATH="/..." | |
259 fi | |
260 | |
261 # Validates use of GOLANG_PKG_BUILDPATH combined with GOLANG_PKG_IS_MULTIPLE | |
262 # FIX: makes sure user isn't overriding GOLANG_PKG_BUILDPATH with inane values. | |
263 if [[ -n ${GOLANG_PKG_IS_MULTIPLE} && ${GOLANG_PKG_BUILDPATH##*/} != "..." ]]; then | |
264 ewarn "Ebuild ${CATEGORY}/${PF} specifies GOLANG_PKG_IS_MULTIPLE=1," | |
265 ewarn "but then GOLANG_PKG_BUILDPATH is overridden with \"${GOLANG_PKG_BUILDPATH}\"." | |
266 ewarn "Please, fix it by appending \"/...\" to your GOLANG_PKG_BUILDPATH." | |
267 ewarn "If in doubt, remove GOLANG_PKG_BUILDPATH entirely." | |
268 fi | |
269 | |
270 # Even though xz-utils are in @system, they must still be added to DEPEND; see | |
271 # http://archives.gentoo.org/gentoo-dev/msg_a0d4833eb314d1be5d5802a3b710e0a4.xml | |
272 if [[ ${GOLANG_PKG_ARCHIVESUFFIX/.*} == "xz" ]]; then | |
273 DEPEND+=" app-arch/xz-utils" | |
274 fi | |
275 | |
276 # Defines common USE flags | |
277 IUSE="${IUSE} debug pie" | |
278 # Enables USE 'test' when required by GOLANG_PKG_HAVE_TEST. | |
279 if [[ -n ${GOLANG_PKG_HAVE_TEST} ]]; then | |
280 IUSE+=" test" | |
281 fi | |
282 | |
283 # Defines HOMEPAGE. | |
284 [ -z "$HOMEPAGE" ] && HOMEPAGE="https://${GOLANG_PKG_IMPORTPATH}/${PN}" | |
285 | |
286 # Defines SOURCE directory. | |
287 S="${WORKDIR}/gopath/src/${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}" | |
288 | |
289 | |
290 # @FUNCTION: _factorize_dependency_entities | |
291 # @INTERNAL | |
292 # @DESCRIPTION: | |
293 # Factorizes the dependency declaration in specific tokens such as the import | |
294 # path, the import path alias, the host name, the author name, the project name, | |
295 # and the revision tag. | |
296 _factorize_dependency_entities() { | |
297 debug-print-function ${FUNCNAME} "${@}" | |
298 | |
299 local -A dependency=() | |
300 local key_list=(importpathalias importpath host project_name author_name revision) | |
301 | |
302 # Strips all the white spaces from the supplied argument. | |
303 local raw_dependency="${1//\ /}" | |
304 | |
305 # Determines the alias of the import path (if present). | |
306 dependency[importpathalias]="${raw_dependency##*->}" | |
307 | |
308 # Strips the import path alias from the supplied argument. | |
309 raw_dependency="${raw_dependency%%->*}" | |
310 | |
311 # Determines the import path. | |
312 dependency[importpath]="${raw_dependency%:*}" | |
313 | |
314 # When the importpath alias is not specified, then this eclass sets the | |
315 # alias as equal to the import path minus the project name. | |
316 if [[ "${raw_dependency}" == "${dependency[importpathalias]}" ]]; then | |
317 dependency[importpathalias]="${dependency[importpath]%/*}" | |
318 fi | |
319 | |
320 # Determines the host. | |
321 dependency[host]="${dependency[importpath]%%/*}" | |
322 | |
323 # Determines the project name. | |
324 dependency[project_name]="${dependency[importpath]##*/}" | |
325 | |
326 # Determines the author name. | |
327 dependency[author_name]="${dependency[importpath]#*/}" | |
328 dependency[author_name]="${dependency[author_name]%/*}" | |
329 | |
330 # Determines the revision. | |
331 dependency[revision]="${raw_dependency#*:}" | |
332 | |
333 # Exports all the dependency tokens as an associated list. | |
334 for key in ${key_list[@]}; do | |
335 echo "${key} ${dependency[${key}]}" | |
336 done | |
337 } | |
338 | |
339 | |
340 # @FUNCTION: x-mgcf-golang_setup | |
341 # @DESCRIPTION: | |
342 # Determines where is the GoLang implementation and then set-up the | |
343 # GoLang build environment. | |
344 x-mgcf-golang_setup() { | |
345 debug-print-function ${FUNCNAME} "${@}" | |
346 | |
347 # NOTE: Keep /usr/bin/go as index [0] and never overwrite it, | |
348 # always append other binary paths after the index [0] | |
349 local GOLANG_BINS=( | |
350 /usr/bin/go | |
351 /usr/bin/gofmt | |
352 ) | |
353 | |
354 [[ -n ${GOLANG_PKG_STATIK} ]] && GOLANG_BINS+=(/usr/bin/statik) | |
355 | |
356 # Reset GoLang environment variables | |
357 unset EGO | |
358 unset EGOFMT | |
359 unset ESTATIK | |
360 unset GO | |
361 unset GOPATH | |
362 unset GOBIN | |
363 | |
364 # Determine is the GoLang interpreter is working | |
365 local IS_EXECUTABLE=1 | |
366 for binary in "${GOLANG_BINS[@]}"; do | |
367 debug-print "${FUNCNAME}: Checking ... ${binary}" | |
368 | |
369 [[ -x "${EPREFIX}/${binary}" ]] && continue | |
370 IS_EXECUTABLE=0 | |
371 ewarn "It seems that the binary '${binary}' is not executable." | |
372 done | |
373 | |
374 # dev-lang/go isn't installed or one of its binaries aren't executable. | |
375 # Either way, the Gentoo box is screwed; no need to set up the GoLang environment | |
376 [[ ${IS_EXECUTABLE} == 0 ]] && exit | |
377 | |
378 # dev-lang/go is available and working. | |
379 # Exports GO/EGO/EGOFMT global variables. | |
380 export GO="${GOLANG_BINS[0]}" | |
381 export EGO="${GOLANG_BINS[0]##*/}" | |
382 export EGOFMT="${GOLANG_BINS[1]}" | |
383 | |
384 # dev-go/statik is available and working. | |
385 # Exports ESTATIK global variable. | |
386 [[ -n ${GOLANG_PKG_STATIK} ]] && export ESTATIK="${GOLANG_BINS[2]##*/}" | |
387 | |
388 debug-print "${FUNCNAME}: GO = ${GO}" | |
389 debug-print "${FUNCNAME}: EGO = ${EGO}" | |
390 debug-print "${FUNCNAME}: EGOFMT = ${EGOFMT}" | |
391 debug-print "${FUNCNAME}: ESTATIK = ${ESTATIK}" | |
392 | |
393 # Determines go interpreter version. | |
394 GOLANG_VERSION="$( ${GO} version )" | |
395 GOLANG_VERSION="${GOLANG_VERSION/go\ version\ go}" | |
396 export GOLANG_VERSION="${GOLANG_VERSION%\ *}" | |
397 einfo "Found GoLang version: ${GOLANG_VERSION}" | |
398 | |
399 # Determines statik interpreter version. | |
400 # TODO: add version detection when statik will provide a -version option. | |
401 if [[ -n ${GOLANG_PKG_STATIK} ]]; then | |
402 local STATIK_VERSION="" | |
403 einfo "Found statik version: ${STATIK_VERSION}" | |
404 fi | |
405 | |
406 # Enable/Disable frame pointers | |
407 local GOEXPERIMENT="noframepointer" | |
408 use debug && GOEXPERIMENT="framepointer" | |
409 | |
410 # Sets the build environment inside Portage's WORKDIR. | |
411 ebegin "Setting up GoLang build environment" | |
412 | |
413 # Prepares CGO_ENABLED. | |
414 CGO_ENABLED=0 | |
415 [[ -z ${GOLANG_PKG_USE_CGO} ]] || CGO_ENABLED=1 | |
416 use pie && CGO_ENABLED=1 # PIE requires CGO | |
417 | |
418 # Prepares gopath / gobin directories inside WORKDIR. | |
419 local _GOPATH="${WORKDIR}/gopath" | |
420 local _GOBIN="${WORKDIR}/gobin" | |
421 mkdir -p "${_GOBIN}" || die | |
422 mkdir -p "${_GOPATH}"/src || die | |
423 | |
424 # Exports special env variable EGO_SRC. | |
425 export EGO_SRC="${_GOPATH}/src" | |
426 | |
427 # Exports GoLang env variables. | |
428 export GOPATH="$_GOPATH" | |
429 export GOBIN="$_GOBIN" | |
430 export CGO_ENABLED | |
431 #export GOEXPERIMENT | |
432 #export GO15VENDOREXPERIMENT=0 | |
433 | |
434 GO111MODULE="off" | |
435 [[ -z ${GOLANG_PKG_USE_MODULES} ]] || GO111MODULE="on" | |
436 export GO111MODULE | |
437 | |
438 debug-print "${FUNCNAME}: GOPATH = ${GOPATH}" | |
439 debug-print "${FUNCNAME}: GOBIN = ${GOBIN}" | |
440 debug-print "${FUNCNAME}: EGO_SRC = ${EGO_SRC}" | |
441 debug-print "${FUNCNAME}: CGO_ENABLED = ${CGO_ENABLED}" | |
442 eend | |
443 } | |
444 | |
445 | |
446 # @FUNCTION: x-mgcf-golang-common_src_prepare | |
447 # @DESCRIPTION: | |
448 # Prepare source code. | |
449 x-mgcf-golang-common_src_prepare() { | |
450 debug-print-function ${FUNCNAME} "${@}" | |
451 | |
452 pushd "${WORKDIR}" > /dev/null || die | |
453 einfo "Preparing GoLang build environment in ${GOPATH}/src" | |
454 | |
455 # If the ebuild declares an importpath alias, then its path was | |
456 # already created during the src_unpack phase. That means the eclass | |
457 # needs to create the missing original import path (GOLANG_PKG_IMPORTPATH) | |
458 # as a simbolic link pointing to the alias. | |
459 if [[ "${GOLANG_PKG_IMPORTPATH}" != "${GOLANG_PKG_IMPORTPATH_ALIAS}" ]]; then | |
460 | |
461 # If the ebuild declares a GOLANG_PKG_NAME different from PN, then | |
462 # the latter will be used as the simbolic link target. | |
463 local TARGET="${GOLANG_PKG_NAME}" | |
464 [[ "${PN}" != "${GOLANG_PKG_NAME}" ]] && TARGET="${PN}" | |
465 | |
466 x-mgcf-golang_fix_importpath_alias \ | |
467 "${GOLANG_PKG_IMPORTPATH_ALIAS}/${TARGET}" \ | |
468 "${GOLANG_PKG_IMPORTPATH}/${GOLANG_PKG_NAME}" | |
469 fi | |
470 | |
471 # If the ebuild declares some GoLang dependencies, then they need to be | |
472 # correctly installed into the sand-boxed GoLang build environment which | |
473 # was set up automatically during src_unpack) phase. | |
474 if [[ ${#GOLANG_PKG_DEPENDENCIES[@]} -gt 0 ]]; then | |
475 | |
476 for i in ${!GOLANG_PKG_DEPENDENCIES[@]} ; do | |
477 | |
478 # Collects all the tokens of the dependency. | |
479 local -A DEPENDENCY=() | |
480 while read -r -d $'\n' key value; do | |
481 [[ -z ${key} ]] && continue | |
482 DEPENDENCY[$key]="${value}" | |
483 done <<-EOF | |
484 $( _factorize_dependency_entities "${GOLANG_PKG_DEPENDENCIES[$i]}" ) | |
485 EOF | |
486 | |
487 # Debug | |
488 debug-print "${FUNCNAME}: DEPENDENCY = ${GOLANG_PKG_DEPENDENCIES[$i]}" | |
489 debug-print "${FUNCNAME}: importpath = ${DEPENDENCY[importpath]}" | |
490 debug-print "${FUNCNAME}: importpathalias = ${DEPENDENCY[importpathalias]}" | |
491 debug-print "${FUNCNAME}: host = ${DEPENDENCY[host]}" | |
492 debug-print "${FUNCNAME}: author = ${DEPENDENCY[author_name]}" | |
493 debug-print "${FUNCNAME}: project = ${DEPENDENCY[project_name]}" | |
494 debug-print "${FUNCNAME}: revision = ${DEPENDENCY[revision]}" | |
495 | |
496 local message="Importing ${DEPENDENCY[importpath]}" | |
497 local destdir | |
498 | |
499 # Prepares GOPATH structure. | |
500 case ${DEPENDENCY[importpathalias]} in | |
501 gopkg.in*) | |
502 message+=" as ${DEPENDENCY[importpathalias]}" | |
503 destdir="${DEPENDENCY[importpathalias]}" | |
504 | |
505 # Creates the import path in GOPATH. | |
506 mkdir -p "${GOPATH}/src/${DEPENDENCY[importpathalias]%/*}" || die | |
507 #einfo "\n${GOPATH}/src/${DEPENDENCY[importpathalias]%/*}" | |
508 ;; | |
509 *) | |
510 [[ "${DEPENDENCY[importpath]}" != "${DEPENDENCY[importpathalias]}/${DEPENDENCY[project_name]}" ]] && message+=" as ${DEPENDENCY[importpathalias]}/${DEPENDENCY[project_name]}" | |
511 destdir="${DEPENDENCY[importpathalias]}/${DEPENDENCY[project_name]}" | |
512 | |
513 # Creates the import path in GOPATH. | |
514 mkdir -p "${GOPATH}/src/${DEPENDENCY[importpathalias]}" || die | |
515 #einfo "\n${GOPATH}/src/${DEPENDENCY[importpathalias]}" | |
516 ;; | |
517 esac | |
518 | |
519 # Moves sources from WORKDIR into GOPATH. | |
520 case ${DEPENDENCY[host]} in | |
521 github*) | |
522 ebegin "${message}" | |
523 mv ${DEPENDENCY[project_name]}-${DEPENDENCY[revision]}* "${GOPATH}"/src/${destdir} || die | |
524 eend | |
525 | |
526 # FIX: sometimes the source code inside an importpath alias | |
527 # (such as gopkg.in/mylib.v1) invokes imports from | |
528 # the original import path instead of using the alias, | |
529 # thus we need a symbolic link between the alias and | |
530 # the original import path to avoid compilation issues. | |
531 # Example: gopkg.in/Shopify/sarama.v1 erroneously | |
532 # invokes imports from github.com/shopify/sarama | |
533 if [[ ${destdir} != ${DEPENDENCY[importpath]} ]]; then | |
534 x-mgcf-golang_fix_importpath_alias ${destdir} ${DEPENDENCY[importpath]} | |
535 fi | |
536 ;; | |
537 bitbucket*) | |
538 #einfo "path: ${DEPENDENCY[author_name]}-${DEPENDENCY[project_name]}-${DEPENDENCY[revision]}" | |
539 ebegin "${message}" | |
540 mv ${DEPENDENCY[author_name]}-${DEPENDENCY[project_name]}-${DEPENDENCY[revision]}* "${GOPATH}"/src/${destdir} || die | |
541 eend | |
542 ;; | |
543 code.google*) | |
544 ebegin "${message}" | |
545 mv ${DEPENDENCY[project_name]}-${DEPENDENCY[revision]}* "${GOPATH}"/src/${destdir} || die | |
546 eend | |
547 ;; | |
548 *) die "Function 'x-mgcf-golang-common_src_prepare' doesn't support '${DEPENDENCY[importpath]}'" ;; | |
549 esac | |
550 done | |
551 | |
552 fi | |
553 | |
554 popd > /dev/null || die | |
555 | |
556 | |
557 # Auto-detects the presence of Go's vendored | |
558 # dependencies inside $S/vendor. | |
559 local VENDOR="${S}/vendor" | |
560 if [[ -d "${VENDOR}" ]]; then | |
561 x-mgcf-golang_add_vendor "${VENDOR}" | |
562 export GO15VENDOREXPERIMENT=1 | |
563 fi | |
564 | |
565 # Auto-detects the presence of Go's vendored | |
566 # dependencies inside $S/*/vendor | |
567 if [[ -n ${GOLANG_PKG_BUILDPATH} && ${GOLANG_PKG_BUILDPATH##*/} != "..." ]]; then | |
568 while read -r -d $' ' path; do | |
569 # Trims leading slash (if any). | |
570 path="${path/\//}" | |
571 | |
572 # Extracts the root path. | |
573 path="${path%%/*}" | |
574 | |
575 # Ignores $path when it's empty or a string of white spaces. | |
576 [[ -n $path ]] || continue | |
577 | |
578 local vendor="${S}/${path}/vendor" | |
579 if [[ -d "${vendor}" ]]; then | |
580 x-mgcf-golang_add_vendor "${vendor}" | |
581 fi | |
582 done <<< "$( echo ${GOLANG_PKG_BUILDPATH}) " | |
583 fi | |
584 | |
585 | |
586 # Auto-detects the presence of Godep's workspace | |
587 # (see github.com/tools/godep for more infos). | |
588 VENDOR="${S}/Godeps/_workspace" | |
589 if [[ -d "${VENDOR}" ]]; then | |
590 x-mgcf-golang_add_vendor "${VENDOR}" | |
591 fi | |
592 | |
593 # Evaluates PATCHES array. | |
594 default_src_prepare | |
595 } | |
596 | |
597 | |
598 # @FUNCTION: x-mgcf-golang-common_src_configure | |
599 # @DESCRIPTION: | |
600 # Configure the package. | |
601 x-mgcf-golang-common_src_configure() { | |
602 debug-print-function ${FUNCNAME} "${@}" | |
603 | |
604 [[ ${EGO} ]] || die "No GoLang implementation set (x-mgcf-golang_setup not called?)." | |
605 | |
606 # Defines the level of verbosity. | |
607 local EGO_VERBOSE="-v" | |
608 [[ -z ${PORTAGE_VERBOSE} ]] || EGO_VERBOSE+=" -x" | |
609 | |
610 # GoLang doesn't have a configure phase, | |
611 # so instead this eclass prints the output of 'go env'. | |
612 local -a GOLANG_ENV=() | |
613 while read -r line; do | |
614 GOLANG_ENV+=("${line}") | |
615 done <<-EOF | |
616 $( ${GO} env ) | |
617 EOF | |
618 | |
619 # Prints an error when 'go env' output is missing. | |
620 if [[ ${#GOLANG_ENV[@]} -eq 1 ]]; then | |
621 eerror "Your GoLang environment should be more verbose" | |
622 fi | |
623 | |
624 # Prints GoLang environment summary. | |
625 einfo " ${EGO} env" | |
626 for env in "${GOLANG_ENV[@]}"; do | |
627 einfo " - ${env}" | |
628 done | |
629 | |
630 | |
631 # Removes GoLang object files from package source directories (pkg/) | |
632 # and temporary directories (_obj/ _test*/). | |
633 local EGO_SUBPACKAGES="${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}" | |
634 case $( ver_cut 1-2 ${GOLANG_VERSION} ) in | |
635 1.4*) ;; | |
636 *) | |
637 EGO_SUBPACKAGES+="/..." | |
638 ;; | |
639 esac | |
640 # einfo "${EGO} clean -i ${EGO_VERBOSE} ${EGO_SUBPACKAGES}" | |
641 # ${EGO} clean -i \ | |
642 # ${EGO_VERBOSE} \ | |
643 # "${EGO_SUBPACKAGES}" \ | |
644 # || die | |
645 | |
646 # Removes GoLang objects files from all the dependencies too. | |
647 # if [[ ${#GOLANG_PKG_DEPENDENCIES[@]} -gt 0 ]]; then | |
648 # | |
649 # for i in ${!GOLANG_PKG_DEPENDENCIES[@]} ; do | |
650 # | |
651 # # Collects all the tokens of the dependency. | |
652 # local -A DEPENDENCY=() | |
653 # while read -r -d $'\n' key value; do | |
654 # [[ -z ${key} ]] && continue | |
655 # DEPENDENCY[$key]="${value}" | |
656 # done <<-EOF | |
657 # $( _factorize_dependency_entities "${GOLANG_PKG_DEPENDENCIES[$i]}" ) | |
658 # EOF | |
659 # | |
660 # [[ ! -d ${DEPENDENCY[importpath]} ]] && continue | |
661 # | |
662 # # Debug | |
663 # debug-print "${FUNCNAME}: DEPENDENCY = ${GOLANG_PKG_DEPENDENCIES[$i]}" | |
664 # debug-print "${FUNCNAME}: importpath = ${DEPENDENCY[importpath]}" | |
665 # | |
666 # # Cleans object files of the dependency. | |
667 # einfo "${EGO} clean -i ${EGO_VERBOSE} ${DEPENDENCY[importpath]}" | |
668 # ${EGO} clean \ | |
669 # -i ${EGO_VERBOSE} \ | |
670 # "${DEPENDENCY[importpath]}" \ | |
671 # || die | |
672 # done | |
673 # fi | |
674 | |
675 # Before to compile Godep's dependencies it's wise to wipe out | |
676 # all pre-built object files from Godep's package source directories. | |
677 if [[ -d "${S}"/Godeps/_workspace/pkg ]]; then | |
678 ebegin "Cleaning up pre-built object files in Godep workspace" | |
679 rm -r "${S}"/Godeps/_workspace/pkg || die | |
680 eend | |
681 fi | |
682 if [[ -d "${S}"/Godeps/_workspace/bin ]]; then | |
683 ebegin "Cleaning up executables in Godep workspace" | |
684 rm -r "${S}"/Godeps/_workspace/bin || die | |
685 eend | |
686 fi | |
687 | |
688 | |
689 # Executes 'go generate'. | |
690 # NOTE: generate should never run automatically. It must be run explicitly. | |
691 if [[ -n ${GOLANG_PKG_USE_GENERATE} ]]; then | |
692 pushd "${GOPATH}/src/${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}" > /dev/null || die | |
693 einfo "${EGO} generate ${EGO_VERBOSE} ${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}/..." | |
694 ${EGO} generate \ | |
695 ${EGO_VERBOSE} \ | |
696 ./... \ | |
697 || die | |
698 popd > /dev/null || die | |
699 fi | |
700 | |
701 | |
702 # Executes 'statik' when explicitly asked. | |
703 if [[ -n ${GOLANG_PKG_STATIK} ]]; then | |
704 ebegin "${ESTATIK} $GOLANG_PKG_STATIK" | |
705 ${ESTATIK} $GOLANG_PKG_STATIK || die | |
706 eend | |
707 fi | |
708 } | |
709 | |
710 | |
711 # @FUNCTION: x-mgcf-golang-common_src_compile | |
712 # @DESCRIPTION: | |
713 # Compiles the package. | |
714 x-mgcf-golang-common_src_compile() { | |
715 debug-print-function ${FUNCNAME} "${@}" | |
716 | |
717 [[ ${EGO} ]] || die "No GoLang implementation set (x-mgcf-golang_setup not called?)." | |
718 | |
719 # Populates env variable GOPATH with vendored workspaces (if present). | |
720 if [[ -n ${GOLANG_PKG_VENDOR} ]]; then | |
721 einfo "Using vendored dependencies from:" | |
722 | |
723 for path in "${GOLANG_PKG_VENDOR[@]}"; do | |
724 [ -d ${path} ] || continue | |
725 | |
726 if [[ ${path//${S}\//} == "vendor" ]]; then | |
727 einfo "- vendor/ (native vendoring support)" | |
728 continue | |
729 fi | |
730 | |
731 debug-print "$FUNCNAME: GOPATH: Adding vendor path ${path}" | |
732 ebegin "- ${path//${S}\//}" | |
733 GOPATH="${GOPATH}:$( echo ${path} )" | |
734 eend | |
735 done | |
736 | |
737 export GOPATH | |
738 fi | |
739 | |
740 # Enables position-independent executables (PIE) | |
741 local EGO_PIE | |
742 use pie && EGO_PIE="-buildmode=pie" | |
743 | |
744 # Defines the install suffix. | |
745 local EGO_INSTALLSUFFIX | |
746 [[ -z ${GOLANG_PKG_INSTALLSUFFIX} ]] || EGO_INSTALLSUFFIX="-installsuffix=${GOLANG_PKG_INSTALLSUFFIX}" | |
747 | |
748 # Defines the level of verbosity. | |
749 local EGO_VERBOSE="-v" | |
750 [[ -z ${PORTAGE_VERBOSE} ]] || EGO_VERBOSE+=" -x" | |
751 | |
752 # Defines the number of builds that can be run in parallel. | |
753 local EGO_PARALLEL="-p $(makeopts_jobs)" | |
754 | |
755 # Defines extra options. | |
756 local EGO_EXTRA_OPTIONS="-a" | |
757 | |
758 # Prepares build flags for the go toolchain. | |
759 local EGO_BUILD_FLAGS="$( echo ${EGO_VERBOSE} ) $( echo ${EGO_PARALLEL} ) $( echo ${EGO_EXTRA_OPTIONS} ) $( echo ${EGO_PIE} )" | |
760 [[ -n ${EGO_INSTALLSUFFIX} ]] && EGO_BUILD_FLAGS+=" $( echo ${EGO_INSTALLSUFFIX} )" | |
761 | |
762 # Detects the total number of packages. | |
763 local pkgs=0 ifs_save=${IFS} IFS=$' ' | |
764 for path in ${GOLANG_PKG_BUILDPATH[@]} ; do | |
765 pkgs=$(( $pkgs + 1 )) | |
766 done | |
767 [[ ${pkgs} -eq 0 ]] && pkgs=1 # there is always at least 1 package | |
768 IFS=${ifs_save} | |
769 | |
770 # Builds the package | |
771 einfo "Compiling ${pkgs} package(s):" | |
772 if [[ -n ${GOLANG_PKG_BUILDPATH} && ${GOLANG_PKG_BUILDPATH##*/} != "..." && ${pkgs} -gt 1 ]]; then | |
773 | |
774 # NOTE: This eclass trims all leading and trailing white spaces from the | |
775 # input of the following 'while read' loop, then appends an extra | |
776 # trailing space; this is necessary to avoid undefined behaviours | |
777 # within the loop when GOLANG_PKG_BUILDPATH is populated with only | |
778 # a single element. | |
779 while read -r -d $' ' cmd; do | |
780 # Ignores $cmd when it's empty or a string of white spaces | |
781 #einfo "cmd: |$cmd| cmd: |${cmd##*/}|" | |
782 [[ -n $cmd ]] || continue | |
783 | |
784 x-mgcf-golang_do_build \ | |
785 ${EGO_BUILD_FLAGS} \ | |
786 -o "${GOBIN}/${cmd##*/}" \ | |
787 "${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}${cmd}" | |
788 done <<< "$( echo ${GOLANG_PKG_BUILDPATH}) " | |
789 else | |
790 # If the package is a multiple package (/...) | |
791 # then this eclass doesn't specify the output name. | |
792 [[ ${GOLANG_PKG_BUILDPATH##*/} != "..." ]] && EGO_BUILD_FLAGS+=" -o ${GOBIN}/${GOLANG_PKG_OUTPUT_NAME}" | |
793 | |
794 x-mgcf-golang_do_build \ | |
795 ${EGO_BUILD_FLAGS} \ | |
796 "${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}${GOLANG_PKG_BUILDPATH}" | |
797 fi | |
798 } | |
799 | |
800 | |
801 # @FUNCTION: x-mgcf-golang-common_src_install | |
802 # @DESCRIPTION: | |
803 # Installs binaries and documents from DOCS or HTML_DOCS arrays. | |
804 x-mgcf-golang-common_src_install() { | |
805 debug-print-function ${FUNCNAME} "${@}" | |
806 | |
807 [[ ${EGO} ]] || die "No GoLang implementation set (x-mgcf-golang_setup not called?)." | |
808 | |
809 # Enables position-independent executables (PIE) | |
810 local EGO_PIE | |
811 use pie && EGO_PIE="-buildmode=pie" | |
812 | |
813 # Defines the install suffix. | |
814 local EGO_INSTALLSUFFIX | |
815 [[ -z ${GOLANG_PKG_INSTALLSUFFIX} ]] || EGO_INSTALLSUFFIX="-installsuffix=${GOLANG_PKG_INSTALLSUFFIX}" | |
816 | |
817 # Defines the level of verbosity. | |
818 local EGO_VERBOSE="-v" | |
819 [[ -z ${PORTAGE_VERBOSE} ]] || EGO_VERBOSE+=" -x" | |
820 | |
821 # Defines the number of builds that can be run in parallel. | |
822 local EGO_PARALLEL="-p $(makeopts_jobs)" | |
823 | |
824 # Defines extra options. | |
825 local EGO_EXTRA_OPTIONS | |
826 | |
827 # Prepares build flags for the go toolchain. | |
828 local EGO_BUILD_FLAGS="$( echo ${EGO_VERBOSE} ) $( echo ${EGO_PARALLEL} ) $( echo ${EGO_EXTRA_OPTIONS} ) $( echo ${EGO_PIE} )" | |
829 [[ -n ${EGO_INSTALLSUFFIX} ]] && EGO_BUILD_FLAGS+=" $( echo ${EGO_INSTALLSUFFIX} )" | |
830 | |
831 # Defines sub-packages. | |
832 local EGO_SUBPACKAGES="${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}${GOLANG_PKG_BUILDPATH}" | |
833 | |
834 # Executes the pre-install phase (go install). | |
835 if [[ -n ${GOLANG_PKG_IS_MULTIPLE} ]]; then | |
836 einfo "${EGO} install -ldflags '$GOLANG_PKG_LDFLAGS' -tags '$GOLANG_PKG_TAGS' ${EGO_BUILD_FLAGS} ${EGO_SUBPACKAGES}" | |
837 ${EGO} install \ | |
838 -ldflags "${GOLANG_PKG_LDFLAGS}" \ | |
839 -tags "${GOLANG_PKG_TAGS}" \ | |
840 ${EGO_BUILD_FLAGS} \ | |
841 "${EGO_SUBPACKAGES}" \ | |
842 || die | |
843 fi | |
844 | |
845 # Installs binaries. | |
846 into ${GOLANG_PKG_INSTALLPATH} | |
847 for bin in "${GOBIN}"/* ; do | |
848 dobin ${bin} | |
849 done | |
850 | |
851 # Installs documentation. | |
852 einstalldocs | |
853 } | |
854 | |
855 # @FUNCTION: x-mgcf-golang-common_src_test | |
856 # @DESCRIPTION: | |
857 # Runs the unit tests for the main package. | |
858 x-mgcf-golang-common_src_test() { | |
859 debug-print-function ${FUNCNAME} "${@}" | |
860 | |
861 [[ ${EGO} ]] || die "No GoLang implementation set (x-mgcf-golang_setup not called?)." | |
862 | |
863 # Appends S and GOBIN to exported main paths. | |
864 # FIX: this is necessary for unit tests that need to invoke bins from | |
865 # $GOBIN or from within $S/bin. | |
866 export PATH="${S}/bin:${GOBIN}:${PATH}" | |
867 | |
868 # Defines the level of verbosity. | |
869 local EGO_VERBOSE="-v" | |
870 [[ -z ${PORTAGE_VERBOSE} ]] || EGO_VERBOSE+=" -x" | |
871 | |
872 # Defines the number of builds that can be run in parallel. | |
873 local EGO_PARALLEL="-p $(makeopts_jobs)" | |
874 | |
875 # Defines extra options. | |
876 #local EGO_EXTRA_OPTIONS="-a" | |
877 | |
878 # Enables data race detection. | |
879 local EGO_RACE | |
880 [[ -n ${GOLANG_PKG_HAVE_TEST_RACE} ]] && EGO_RACE=" -race" | |
881 | |
882 # Prepares build flags for the go toolchain. | |
883 local EGO_BUILD_FLAGS="$( echo ${EGO_VERBOSE} ) $( echo ${EGO_PARALLEL} ) $( echo ${EGO_EXTRA_OPTIONS} )" | |
884 [[ -n ${EGO_RACE} ]] && EGO_BUILD_FLAGS+=" $( echo ${EGO_RACE} )" | |
885 | |
886 # Sanitizes vars from entra white spaces. | |
887 GOLANG_PKG_LDFLAGS="$( echo ${GOLANG_PKG_LDFLAGS} )" | |
888 GOLANG_PKG_TAGS="$( echo ${GOLANG_PKG_TAGS} )" | |
889 | |
890 # Defines sub-packages. | |
891 local EGO_SUBPACKAGES="${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}${GOLANG_PKG_BUILDPATH}" | |
892 [[ -z ${GOLANG_PKG_IS_MULTIPLE} ]] || EGO_SUBPACKAGES="./..." | |
893 | |
894 # Detects the total number of packages. | |
895 local pkgs=0 ifs_save=${IFS} IFS=$' ' | |
896 for path in ${GOLANG_PKG_BUILDPATH[@]} ; do | |
897 pkgs=$(( $pkgs + 1 )) | |
898 done | |
899 [[ ${pkgs} -eq 0 ]] && pkgs=1 # there is always at least 1 package | |
900 IFS=${ifs_save} | |
901 | |
902 # Runs the Unit Tests | |
903 einfo "Testing ${pkgs} package(s):" | |
904 if [[ -n ${GOLANG_PKG_BUILDPATH} && ${GOLANG_PKG_BUILDPATH##*/} != "..." && ${pkgs} -gt 1 ]]; then | |
905 | |
906 # NOTE: This eclass trims all leading and trailing white spaces from the | |
907 # input of the following 'while read' loop, then appends an extra | |
908 # trailing space; this is necessary to avoid undefined behaviours | |
909 # within the loop when GOLANG_PKG_BUILDPATH is populated with only | |
910 # a single element. | |
911 while read -r -d $' ' cmd; do | |
912 # Ignores $cmd when it's empty or a string of white spaces | |
913 #einfo "cmd: |$cmd| cmd: |${cmd##*/}|" | |
914 [[ -n $cmd ]] || continue | |
915 | |
916 einfo "${EGO} test -ldflags '$GOLANG_PKG_LDFLAGS' -tags '$GOLANG_PKG_TAGS' ${EGO_BUILD_FLAGS} ${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}${cmd}/..." | |
917 ${EGO} test \ | |
918 -ldflags "${GOLANG_PKG_LDFLAGS}" \ | |
919 -tags "${GOLANG_PKG_TAGS}" \ | |
920 ${EGO_BUILD_FLAGS} \ | |
921 "${GOLANG_PKG_IMPORTPATH_ALIAS}/${GOLANG_PKG_NAME}${cmd}/..." \ | |
922 || die | |
923 done <<< "$( echo ${GOLANG_PKG_BUILDPATH}) " | |
924 else | |
925 # It's a single package | |
926 einfo "${EGO} test -ldflags '$GOLANG_PKG_LDFLAGS' -tags '$GOLANG_PKG_TAGS' ${EGO_BUILD_FLAGS} ${EGO_SUBPACKAGES}" | |
927 ${EGO} test \ | |
928 -ldflags "${GOLANG_PKG_LDFLAGS}" \ | |
929 -tags "${GOLANG_PKG_TAGS}" \ | |
930 ${EGO_BUILD_FLAGS} \ | |
931 "${EGO_SUBPACKAGES}" \ | |
932 || die | |
933 fi | |
934 } | |
935 | |
936 | |
937 # @FUNCTION: x-mgcf-golang_do_build | |
938 # @INTERNAL | |
939 # @USAGE: <flags> <buildpath> | |
940 # @DESCRIPTION: | |
941 # | |
942 # @CODE | |
943 # Example: | |
944 # GOLANG_PKG_LDFLAGS="-extldflags=-static" | |
945 # GOLANG_PKG_TAGS="netgo" | |
946 # | |
947 # x-mgcf-golang_do_build ${EGO_BUILD_FLAGS} ${GOLANG_PKG_IMPORTPATH}/${GOLANG_PKG_NAME}${GOLANG_PKG_BUILDPATH} | |
948 # @CODE | |
949 x-mgcf-golang_do_build() { | |
950 debug-print-function ${FUNCNAME} $* | |
951 | |
952 [[ ${GOLANG_VERSION} ]] || die "No GoLang implementation set (x-mgcf-golang_setup not called?)." | |
953 | |
954 # Filters "=" chars from ldflags declaration. | |
955 # NOTE: from go1.5+ linker syntax is no more compatible with <go1.4; | |
956 # this hack ensures that the old behaviour is honoured. | |
957 if [[ $( ver_cut 1-2 ${GOLANG_VERSION} ) == "1.4" ]]; then | |
958 GOLANG_PKG_LDFLAGS="${GOLANG_PKG_LDFLAGS//=/ }" | |
959 fi | |
960 | |
961 # Disables debug symbols (DWARF) when not required. | |
962 if ! use debug; then | |
963 case "${GOLANG_PKG_LDFLAGS}" in | |
964 *-s*|*-w*) | |
965 # Do nothing | |
966 ;; | |
967 *) | |
968 GOLANG_PKG_LDFLAGS+=" -s -w" | |
969 esac | |
970 fi | |
971 | |
972 # Sanitizes vars from entra white spaces. | |
973 GOLANG_PKG_LDFLAGS="$( echo ${GOLANG_PKG_LDFLAGS} )" | |
974 GOLANG_PKG_TAGS="$( echo ${GOLANG_PKG_TAGS} )" | |
975 | |
976 einfo "${EGO} build -ldflags '$GOLANG_PKG_LDFLAGS' -tags '$GOLANG_PKG_TAGS' $*" | |
977 ${EGO} build \ | |
978 -ldflags "${GOLANG_PKG_LDFLAGS}" \ | |
979 -tags "${GOLANG_PKG_TAGS}" \ | |
980 $( echo $* ) \ | |
981 || die | |
982 } | |
983 | |
984 | |
985 # @FUNCTION: x-mgcf-golang_add_vendor | |
986 # @INTERNAL | |
987 # @USAGE: <path> | |
988 # @DESCRIPTION: | |
989 # | |
990 # @CODE | |
991 # Example | |
992 # | |
993 # x-mgcf-golang_add_vendor "${S}"/vendor | |
994 # x-mgcf-golang_add_vendor "${S}"/${PN}/vendor | |
995 # @CODE | |
996 x-mgcf-golang_add_vendor() { | |
997 debug-print-function ${FUNCNAME} $* | |
998 | |
999 [[ ${1} ]] || die "${FUNCNAME}: no paths given" | |
1000 | |
1001 [[ ${GOLANG_VERSION} ]] || die "No Golang implementation set (x-mgcf-golang_setup not called?)." | |
1002 | |
1003 [[ ! -d "${1}" ]] && return | |
1004 | |
1005 # NOTE: this hack is required by Go v1.4 and older versions. | |
1006 #if [[ ! -d "${1}"/src ]]; then | |
1007 # ebegin "Fixing $1" | |
1008 # ln -s "${1}" "${1}"/src || die | |
1009 # eend | |
1010 #fi | |
1011 | |
1012 GOLANG_PKG_VENDOR+=(${1}) | |
1013 } | |
1014 | |
1015 | |
1016 # @FUNCTION: x-mgcf-golang_fix_importpath_alias | |
1017 # @USAGE: <target> <alias> | |
1018 # @DESCRIPTION: | |
1019 # Helper functions for generating a symbolic link for import path <target> as | |
1020 # <alias>. | |
1021 # | |
1022 # WARNING: Use this function only if GOLANG_PKG_DEPENDENCIES declaration of | |
1023 # import path aliases doesn't work (e.g.: the package name differs from both the | |
1024 # import path and the alias, or if the package name is case sensitive but the | |
1025 # import path is not). | |
1026 # | |
1027 # @CODE | |
1028 # Example: | |
1029 # | |
1030 # src_prepare() { | |
1031 # x-mgcf-golang-single_src_prepare | |
1032 # | |
1033 # x-mgcf-golang_fix_importpath_alias \ | |
1034 # "github.com/GoogleCloudPlatform/gcloud-golang" \ | |
1035 # "google.golang.org/cloud" | |
1036 # } | |
1037 # @CODE | |
1038 x-mgcf-golang_fix_importpath_alias() { | |
1039 debug-print-function ${FUNCNAME} "${@}" | |
1040 | |
1041 [[ ${1} ]] || die "${FUNCNAME}: no target specified" | |
1042 [[ ${2} ]] || die "${FUNCNAME}: no alias specified" | |
1043 | |
1044 [[ ${EGO} ]] || die "No GoLang implementation set (x-mgcf-golang_setup not called?)." | |
1045 | |
1046 | |
1047 local TARGET="${1}" | |
1048 local ALIAS="${2}" | |
1049 | |
1050 if [[ ${ALIAS%/*} != ${ALIAS} ]]; then | |
1051 mkdir -p "${GOPATH}/src/${ALIAS%/*}" || die | |
1052 fi | |
1053 ebegin "Linking ${TARGET} as ${ALIAS}" | |
1054 ln -s "${GOPATH}/src/${TARGET}" \ | |
1055 "${GOPATH}/src/${ALIAS}" \ | |
1056 || die | |
1057 eend | |
1058 } | |
1059 | |
1060 | |
1061 fi |