summaryrefslogtreecommitdiff
path: root/eclass/go-env.eclass
blob: af843a8e81bae40772cebbe6c41c841ad42210b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2023-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: go-env.eclass
# @MAINTAINER:
# Flatcar Linux Maintainers <infra@flatcar-linux.org>
# @AUTHOR:
# Flatcar Linux Maintainers <infra@flatcar-linux.org>
# @SUPPORTED_EAPIS: 7 8 9
# @BLURB: Helper eclass for setting up the Go build environment.
# @DESCRIPTION:
# This eclass includes helper functions for setting up the build environment for
# Go ebuilds. Intended to be called by other Go eclasses in an early build
# stage, e.g. src_configure.

# @ECLASS_VARIABLE: GOMAXPROCS
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# The maximum number of processes for the Go runtime to run in parallel. See
# https://pkg.go.dev/runtime#GOMAXPROCS. If unset, this defaults to the
# configured number of Make jobs. Unfortunately, Go does not currently support
# the GNU Make jobserver, so this may not play nicely alongside other build
# processes. However, Go code is often built without a supporting build system
# or without other non-Go code, so this should be sufficient in most cases.

# @ECLASS_VARIABLE: GOAMD64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for amd64 when building for CHOST. See
# https://golang.org/wiki/MinimumRequirements#amd64.

# @ECLASS_VARIABLE: GOARM64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for arm64 when building for CHOST. See
# https://pkg.go.dev/cmd/go/internal/help#pkg-variables.

# @ECLASS_VARIABLE: GOPPC64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for ppc64 when building for CHOST. See
# https://pkg.go.dev/cmd/go/internal/help#pkg-variables.

# @ECLASS_VARIABLE: GORISCV64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for riscv when building for CHOST. See
# https://pkg.go.dev/cmd/go/internal/help#pkg-variables.

# @ECLASS_VARIABLE: BUILD_GOAMD64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for amd64 when building for CBUILD.

# @ECLASS_VARIABLE: BUILD_GOARM64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for arm64 when building for CBUILD.

# @ECLASS_VARIABLE: BUILD_GOPPC64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for ppc64 when building for CBUILD.

# @ECLASS_VARIABLE: BUILD_GORISCV64
# @USER_VARIABLE
# @DEFAULT_UNSET
# @DESCRIPTION:
# Optimisation setting for riscv when building for CBUILD.

case ${EAPI} in
	7|8|9) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

if [[ -z ${_GO_ENV_ECLASS} ]]; then
_GO_ENV_ECLASS=1

inherit flag-o-matic multiprocessing sysroot toolchain-funcs

# @ECLASS_VARIABLE: CGO_ENABLED
# @DESCRIPTION:
# Whether to enable the cgo tool that lets Go packages call C code. Upstream Go
# enables this by default for native builds when a C compiler is found. Some
# projects will forcibly enable it by necessity. Other projects may disable it
# by default or even forcibly disable it to make the resulting binaries more
# portable. When enabled, certain standard library packages will use the libc
# over their own built-in code for things like DNS resolution, which is
# generally preferable. It makes sense for a distribution to enable it where
# possible, even when cross-compiling. Ebuilds may force this setting one way or
# the other before inheriting this eclass where necessary, although upstream
# projects will likely do this for you. If you believe that a project should not
# force it, then please work with upstream to have the variable respected.
# Ebuilds may also disable it by default if this is more appropriate. See
# https://pkg.go.dev/cmd/cgo.
case ${EAPI} in
	7|8) : ;;
	*) export CGO_ENABLED="${CGO_ENABLED:-1}" ;;
esac

# @FUNCTION: go-env_set_compile_environment
# @DESCRIPTION:
# Sets up the environment to build Go code for CHOST. This includes variables
# required for cross-compiling, cgo-related variables, and architecture-specific
# variables. GO386, GOARM, GOMIPS, and GOMIPS64 are set based on the tuple.
# Variables for other architectures need to be set manually by users. This
# function must be called (implicitly or otherwise) before building any Go code
# whether cross-compiling or not. Make any build flag changes (e.g. CFLAGS)
# before calling this function.
go-env_set_compile_environment() {
	tc-export AR CC CXX FC PKG_CONFIG

	# Go uses all cores by default. Use the configured number of Make jobs, but
	# respect the user value, as described above.
	: "${GOMAXPROCS=$(get_makeopts_jobs)}"
	export GOMAXPROCS

	# The following GOFLAGS should be used for all builds.
	# -x prints commands as they are executed
	# -v prints the names of packages as they are compiled
	# -modcacherw makes the build cache read/write
	# -buildvcs=false omits version control information
	# -buildmode=pie builds position independent executables
	export \
		GOFLAGS="-x -v -modcacherw -buildvcs=false" \
		GOARCH=$(go-env_goarch) \
		GOOS=$(go-env_goos)

	case ${GOARCH} in
		386|amd64|arm*|ppc64le|s390*) GOFLAGS+=" -buildmode=pie" ;;
	esac

	case ${GOARCH} in
		386) export GO386=$(go-env_go386) ;;
		arm|armbe) export GOARM=$(go-env_goarm) ;;
		mips64*) export GOMIPS64=$(go-env_gomips) ;;
		mips*) export GOMIPS=$(go-env_gomips) ;;
	esac

	# Don't modify the non-Go variables outside this function.
	local -I $(all-flag-vars)

	if tc-is-gcc ; then
		# XXX: Hack for checking ICE (bug #912152, gcc PR113204)
		# For either USE=debug or an unreleased compiler, non-default
		# checking will trigger.
		$(tc-getCC) -v 2>&1 | grep -Eqe "--enable-checking=\S*\byes\b" && filter-lto

		# bug #929219
		replace-flags -g3 -g
		replace-flags -ggdb3 -ggdb
	fi

	export \
		CGO_CFLAGS=${CFLAGS} \
		CGO_CPPFLAGS=${CPPFLAGS} \
		CGO_CXXFLAGS=${CXXFLAGS} \
		CGO_LDFLAGS=${LDFLAGS}

	# go run will build binaries for the target system and try to execute them.
	# This will fail when cross-compiling unless you provide a wrapper.
	local script go_exec
	if script=$(sysroot_make_run_prefixed); then
		go_exec="${T}/go-exec"
		PATH="${go_exec}:${PATH}"
		mkdir -p "${go_exec}" || die
		ln -snfr "${script}" "${go_exec}/go_${GOOS}_${GOARCH}_exec" || die
	fi
}

# @FUNCTION: go-env_run
# @DESCRIPTION:
# Runs the given command under a localised environment configured by
# go-env_set_compile_environment. It is not usually necessary to call this, but
# it is useful when combined with tc-env_build.
go-env_run() {
	local -I AR CC CXX FC PKG_CONFIG \
		GO{FLAGS,MAXPROCS,ARCH,OS,386,ARM,MIPS,MIPS64} \
		CGO_{CFLAGS,CPPFLAGS,CXXFLAGS,LDFLAGS}

	go-env_set_compile_environment
	"${@}"
}

# @FUNCTION: go-env_goos
# @USAGE: [toolchain prefix]
# @DESCRIPTION:
# Returns the appropriate GOOS setting for the target operating system.
go-env_goos() {
	local target=${1:-${CHOST}}
	case "${target}" in
		*-linux*) echo linux ;;
		*-darwin*) echo darwin ;;
		*-freebsd*) echo freebsd ;;
		*-netbsd*) echo netbsd ;;
		*-openbsd*) echo openbsd ;;
		*-solaris*) echo solaris ;;
		*-cygwin*|*-interix*|*-winnt*) echo windows ;;
		*-gnu) echo hurd ;;
		*) die "unknown GOOS for ${target}" ;;
	esac
}

# @FUNCTION: go-env_goarch
# @USAGE: [toolchain prefix]
# @DESCRIPTION:
# Returns the appropriate GOARCH setting for the target architecture.
go-env_goarch() {
	local target=${1:-${CHOST}}
	# Some Portage arch names match Go.
	local arch=$(tc-arch "${target}") cpu=${target%%-*}
	case "${arch}" in
		x86)	echo 386 ;;
		loong)	echo loong64 ;;
		*)		case "${cpu}" in
					aarch64*be) echo arm64be ;;
					arm64) echo arm64 ;;
					arm*b*) echo armbe ;;
					mips64*l*) echo mips64le ;;
					mips*l*) echo mipsle ;;
					powerpc64le*) echo ppc64le ;;
					arm64|s390x) echo "${cpu}" ;;
					mips64*|riscv64*|sparc64*) echo "${arch}64" ;;
					*) echo "${arch}" ;;
				esac ;;
	esac
}

# @FUNCTION: go-env_go386
# @DESCRIPTION:
# Returns the appropriate GO386 setting for the CFLAGS in use.
go-env_go386() {
	# Piggy-back off any existing CPU_FLAGS_X86 usage in the ebuild if
	# it's there.
	if in_iuse cpu_flags_x86_sse2 && use cpu_flags_x86_sse2 ; then
		echo 'sse2'
		return
	fi

	if tc-cpp-is-true "defined(__SSE2__)" ${CFLAGS} ${CXXFLAGS} ; then
		echo 'sse2'
		return
	fi

	# Go 1.16 dropped explicit support for 386 FP and relies on software
	# emulation instead in the absence of SSE2.
	echo 'softfloat'
}

# @FUNCTION: go-env_goarm
# @USAGE: [tuple]
# @DESCRIPTION:
# Returns the appropriate GOARM setting for given target or CHOST.
go-env_goarm() {
	local CTARGET=${1:-${CHOST}}

	case ${CTARGET} in
		armv5*)	echo -n 5 ;;
		armv6*)	echo -n 6 ;;
		armv7*)	echo -n 7 ;;
		*) die "unknown GOARM for ${CTARGET}" ;;
	esac

	if [[ $(tc-is-softfloat) == no ]]; then
		echo ,hardfloat
	else
		echo ,softfloat
	fi
}

# @FUNCTION: go-env_gomips
# @USAGE: [tuple]
# @DESCRIPTION:
# Returns the appropriate GOMIPS or GOMIPS64 setting for given target or CHOST.
go-env_gomips() {
	local CTARGET=${1:-${CHOST}}

	if [[ $(tc-is-softfloat) == no ]]; then
		echo hardfloat
	else
		echo softfloat
	fi
}

fi