blob: 34c9c1c654a30791661c0c31803fcf310911edf5 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#
2# Copyright (C) 2018, 2020 Jeffery To
3#
4# This is free software, licensed under the GNU General Public License v2.
5# See /LICENSE for more information.
6#
7
8ifeq ($(origin GO_INCLUDE_DIR),undefined)
9 GO_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
10endif
11
12
13# Unset environment variables
14# There are more magic variables to track down, but ain't nobody got time for that
15
16# From https://pkg.go.dev/cmd/go#hdr-Environment_variables
17
18# General-purpose environment variables:
19unexport \
20 GO111MODULE \
21 GCCGO \
22 GOARCH \
23 GOBIN \
24 GOCACHE \
25 GOMODCACHE \
26 GODEBUG \
27 GOENV \
28 GOFLAGS \
29 GOOS \
30 GOPATH \
31 GOROOT \
32 GOTMPDIR
33# Unmodified:
34# GOINSECURE
35# GOPRIVATE
36# GOPROXY
37# GONOPROXY
38# GOSUMDB
39# GONOSUMDB
40# GOVCS
41
42# Environment variables for use with cgo:
43unexport \
44 AR \
45 CC \
46 CGO_ENABLED \
47 CGO_CFLAGS CGO_CFLAGS_ALLOW CGO_CFLAGS_DISALLOW \
48 CGO_CPPFLAGS CGO_CPPFLAGS_ALLOW CGO_CPPFLAGS_DISALLOW \
49 CGO_CXXFLAGS CGO_CXXFLAGS_ALLOW CGO_CXXFLAGS_DISALLOW \
50 CGO_FFLAGS CGO_FFLAGS_ALLOW CGO_FFLAGS_DISALLOW \
51 CGO_LDFLAGS CGO_LDFLAGS_ALLOW CGO_LDFLAGS_DISALLOW \
52 CXX \
53 FC
54# Unmodified:
55# PKG_CONFIG
56
57# Architecture-specific environment variables:
58unexport \
59 GOARM \
60 GO386 \
61 GOMIPS \
62 GOMIPS64 \
63 GOWASM
64
65# Special-purpose environment variables:
66unexport \
67 GCCGOTOOLDIR \
68 GOEXPERIMENT \
69 GOROOT_FINAL \
70 GO_EXTLINK_ENABLED
71# Unmodified:
72# GIT_ALLOW_PROTOCOL
73
74# From https://pkg.go.dev/runtime#hdr-Environment_Variables
75unexport \
76 GOGC \
77 GOMAXPROCS \
78 GORACE \
79 GOTRACEBACK
80
81# From https://pkg.go.dev/cmd/cgo#hdr-Using_cgo_with_the_go_command
82unexport \
83 CC_FOR_TARGET \
84 CXX_FOR_TARGET
85# Todo:
86# CC_FOR_${GOOS}_${GOARCH}
87# CXX_FOR_${GOOS}_${GOARCH}
88
89# From https://golang.org/doc/install/source#environment
90unexport \
91 GOHOSTOS \
92 GOHOSTARCH \
93 GOPPC64
94
95# From https://golang.org/src/make.bash
96unexport \
97 GO_GCFLAGS \
98 GO_LDFLAGS \
99 GO_LDSO \
100 GO_DISTFLAGS \
101 GOBUILDTIMELOGFILE \
102 GOROOT_BOOTSTRAP
103
104# From https://golang.org/doc/go1.9#parallel-compile
105unexport \
106 GO19CONCURRENTCOMPILATION
107
108# From https://golang.org/src/cmd/dist/build.go
109unexport \
110 BOOT_GO_GCFLAGS \
111 BOOT_GO_LDFLAGS
112
113# From https://golang.org/src/cmd/dist/buildtool.go
114unexport \
115 GOBOOTSTRAP_TOOLEXEC
116
117
118# GOOS / GOARCH
119
120go_arch=$(subst \
121 aarch64,arm64,$(subst \
122 i386,386,$(subst \
123 mipsel,mipsle,$(subst \
124 mips64el,mips64le,$(subst \
125 powerpc64,ppc64,$(subst \
126 x86_64,amd64,$(1)))))))
127
128GO_OS:=linux
129GO_ARCH:=$(call go_arch,$(ARCH))
130GO_OS_ARCH:=$(GO_OS)_$(GO_ARCH)
131
132GO_HOST_OS:=$(call tolower,$(HOST_OS))
133GO_HOST_ARCH:=$(call go_arch,$(subst \
134 armv6l,arm,$(subst \
135 armv7l,arm,$(subst \
136 i686,i386,$(HOST_ARCH)))))
137GO_HOST_OS_ARCH:=$(GO_HOST_OS)_$(GO_HOST_ARCH)
138
139ifeq ($(GO_OS_ARCH),$(GO_HOST_OS_ARCH))
140 GO_HOST_TARGET_SAME:=1
141else
142 GO_HOST_TARGET_DIFFERENT:=1
143endif
144
145ifeq ($(GO_ARCH),386)
146 ifeq ($(CONFIG_TARGET_x86_geode)$(CONFIG_TARGET_x86_legacy),y)
147 GO_386:=softfloat
148 else
149 GO_386:=sse2
150 endif
151
152 # -fno-plt: causes "unexpected GOT reloc for non-dynamic symbol" errors
153 GO_CFLAGS_TO_REMOVE:=-fno-plt
154
155else ifeq ($(GO_ARCH),arm)
156 GO_TARGET_FPU:=$(word 2,$(subst +,$(space),$(call qstrip,$(CONFIG_CPU_TYPE))))
157
158 # FPU names from https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gcc/ARM-Options.html#index-mfpu-1
159 # see also https://github.com/gcc-mirror/gcc/blob/releases/gcc-8.4.0/gcc/config/arm/arm-cpus.in
160
161 ifeq ($(GO_TARGET_FPU),)
162 GO_ARM:=5
163 else ifneq ($(filter $(GO_TARGET_FPU),vfp vfpv2),)
164 GO_ARM:=6
165 else
166 GO_ARM:=7
167 endif
168
169else ifneq ($(filter $(GO_ARCH),mips mipsle),)
170 ifeq ($(CONFIG_HAS_FPU),y)
171 GO_MIPS:=hardfloat
172 else
173 GO_MIPS:=softfloat
174 endif
175
176 # -mips32r2: conflicts with -march=mips32 set by go
177 GO_CFLAGS_TO_REMOVE:=-mips32r2
178
179else ifneq ($(filter $(GO_ARCH),mips64 mips64le),)
180 ifeq ($(CONFIG_HAS_FPU),y)
181 GO_MIPS64:=hardfloat
182 else
183 GO_MIPS64:=softfloat
184 endif
185
186endif
187
188
189# Target Go
190
191GO_ARCH_DEPENDS:=@(aarch64||arm||i386||i686||mips||mips64||mips64el||mipsel||powerpc64||x86_64)
192
193
194# ASLR/PIE
195
196# From https://golang.org/src/cmd/internal/sys/supported.go
197GO_PIE_SUPPORTED_OS_ARCH:= \
198 android_386 android_amd64 android_arm android_arm64 \
199 linux_386 linux_amd64 linux_arm linux_arm64 \
200 \
201 windows_386 windows_amd64 windows_arm \
202 \
203 darwin_amd64 darwin_arm64 \
204 ios_amd64 ios_arm64 \
205 \
206 freebsd_amd64 \
207 \
208 aix_ppc64 \
209 \
210 linux_ppc64le linux_riscv64 linux_s390x
211
212# From https://golang.org/src/cmd/go/internal/work/init.go
213go_pie_install_suffix=$(if $(filter $(1),aix_ppc64 windows_386 windows_amd64 windows_arm),,shared)
214
215ifneq ($(filter $(GO_HOST_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),)
216 GO_HOST_PIE_SUPPORTED:=1
217 GO_HOST_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_HOST_OS_ARCH))
218endif
219
220ifneq ($(filter $(GO_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),)
221 GO_TARGET_PIE_SUPPORTED:=1
222 GO_TARGET_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_OS_ARCH))
223endif
224
225
226# Spectre mitigations
227
228GO_SPECTRE_SUPPORTED_ARCH:=amd64
229
230ifneq ($(filter $(GO_HOST_ARCH),$(GO_SPECTRE_SUPPORTED_ARCH)),)
231 GO_HOST_SPECTRE_SUPPORTED:=1
232endif
233
234ifneq ($(filter $(GO_ARCH),$(GO_SPECTRE_SUPPORTED_ARCH)),)
235 GO_TARGET_SPECTRE_SUPPORTED:=1
236endif
237
238
239# General build info
240
241GO_BUILD_CACHE_DIR:=$(or $(call qstrip,$(CONFIG_GOLANG_BUILD_CACHE_DIR)),$(TMP_DIR)/go-build)
242GO_MOD_CACHE_DIR:=$(DL_DIR)/go-mod-cache
243
244GO_MOD_ARGS= \
245 -modcacherw
246
247GO_GENERAL_BUILD_CONFIG_VARS= \
248 CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE="$(CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE)" \
249 GO_BUILD_CACHE_DIR="$(GO_BUILD_CACHE_DIR)" \
250 GO_MOD_CACHE_DIR="$(GO_MOD_CACHE_DIR)" \
251 GO_MOD_ARGS="$(GO_MOD_ARGS)"
252
253define Go/CacheCleanup
254 $(GO_GENERAL_BUILD_CONFIG_VARS) \
255 $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh cache_cleanup
256endef