ad75dd165fe984850e8204b0293022ca774f7367
[feed/packages.git] / lang / golang / golang / Makefile
1 #
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
8 include $(TOPDIR)/rules.mk
9
10 GO_VERSION_MAJOR_MINOR:=1.18
11 GO_VERSION_PATCH:=2
12
13 PKG_NAME:=golang
14 PKG_VERSION:=$(GO_VERSION_MAJOR_MINOR)$(if $(GO_VERSION_PATCH),.$(GO_VERSION_PATCH))
15 PKG_RELEASE:=1
16
17 GO_SOURCE_URLS:=https://dl.google.com/go/ \
18 https://mirrors.ustc.edu.cn/golang/ \
19 https://mirrors.nju.edu.cn/golang/
20
21 PKG_SOURCE:=go$(PKG_VERSION).src.tar.gz
22 PKG_SOURCE_URL:=$(GO_SOURCE_URLS)
23 PKG_HASH:=2c44d03ea2c34092137ab919ba602f2c261a038d08eb468528a3f3a28e5667e2
24
25 PKG_MAINTAINER:=Jeffery To <jeffery.to@gmail.com>
26 PKG_LICENSE:=BSD-3-Clause
27 PKG_LICENSE_FILES:=LICENSE
28 PKG_CPE_ID:=cpe:/a:golang:go
29
30 PKG_BUILD_DEPENDS:=golang/host
31 PKG_BUILD_DIR:=$(BUILD_DIR)/go-$(PKG_VERSION)
32 PKG_BUILD_PARALLEL:=1
33 PKG_USE_MIPS16:=0
34
35 PKG_GO_PREFIX:=/usr
36 PKG_GO_VERSION_ID:=$(GO_VERSION_MAJOR_MINOR)
37 PKG_GO_ROOT:=$(PKG_GO_PREFIX)/lib/go-$(PKG_GO_VERSION_ID)
38
39 HOST_BUILD_DIR:=$(BUILD_DIR_HOST)/go-$(PKG_VERSION)
40 HOST_BUILD_PARALLEL:=1
41
42 HOST_GO_PREFIX:=$(STAGING_DIR_HOSTPKG)
43 HOST_GO_VERSION_ID:=cross
44 HOST_GO_ROOT:=$(HOST_GO_PREFIX)/lib/go-$(HOST_GO_VERSION_ID)
45
46 HOST_GO_VALID_OS_ARCH:= \
47 android_386 android_amd64 android_arm android_arm64 \
48 freebsd_386 freebsd_amd64 freebsd_arm freebsd_arm64 \
49 linux_386 linux_amd64 linux_arm linux_arm64 \
50 openbsd_386 openbsd_amd64 openbsd_arm openbsd_arm64 \
51 netbsd_386 netbsd_amd64 netbsd_arm netbsd_arm64 \
52 windows_386 windows_amd64 windows_arm windows_arm64 \
53 \
54 plan9_386 plan9_amd64 plan9_arm \
55 \
56 darwin_amd64 darwin_arm64 \
57 ios_amd64 ios_arm64 \
58 \
59 dragonfly_amd64 \
60 illumos_amd64 \
61 solaris_amd64 \
62 \
63 aix_ppc64 \
64 js_wasm \
65 \
66 linux_ppc64 linux_ppc64le \
67 linux_mips linux_mipsle linux_mips64 linux_mips64le \
68 linux_riscv64 linux_s390x \
69 \
70 openbsd_mips64
71
72 BOOTSTRAP_SOURCE:=go1.4-bootstrap-20171003.tar.gz
73 BOOTSTRAP_SOURCE_URL:=$(GO_SOURCE_URLS)
74 BOOTSTRAP_HASH:=f4ff5b5eb3a3cae1c993723f3eab519c5bae18866b5e5f96fe1102f0cb5c3e52
75
76 BOOTSTRAP_BUILD_DIR:=$(HOST_BUILD_DIR)/.go_bootstrap
77
78 BOOTSTRAP_GO_VALID_OS_ARCH:= \
79 darwin_386 darwin_amd64 \
80 dragonfly_386 dragonfly_amd64 \
81 freebsd_386 freebsd_amd64 freebsd_arm \
82 linux_386 linux_amd64 linux_arm \
83 netbsd_386 netbsd_amd64 netbsd_arm \
84 openbsd_386 openbsd_amd64 \
85 plan9_386 plan9_amd64 \
86 solaris_amd64 \
87 windows_386 windows_amd64
88
89 include $(INCLUDE_DIR)/host-build.mk
90 include $(INCLUDE_DIR)/package.mk
91 include ../golang-compiler.mk
92 include ../golang-package.mk
93
94 PKG_UNPACK:=$(HOST_TAR) -C "$(PKG_BUILD_DIR)" --strip-components=1 -xzf "$(DL_DIR)/$(PKG_SOURCE)"
95 HOST_UNPACK:=$(HOST_TAR) -C "$(HOST_BUILD_DIR)" --strip-components=1 -xzf "$(DL_DIR)/$(PKG_SOURCE)"
96 BOOTSTRAP_UNPACK:=$(HOST_TAR) -C "$(BOOTSTRAP_BUILD_DIR)" --strip-components=1 -xzf "$(DL_DIR)/$(BOOTSTRAP_SOURCE)"
97
98 # don't strip ELF executables in test data
99 RSTRIP:=:
100 STRIP:=:
101
102 ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1)
103 PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE
104 endif
105
106 define Package/golang/Default
107 $(call GoPackage/GoSubMenu)
108 TITLE:=Go programming language
109 URL:=https://go.dev/
110 DEPENDS:=$(GO_ARCH_DEPENDS)
111 endef
112
113 define Package/golang/Default/description
114 The Go programming language is an open source project to make
115 programmers more productive.
116
117 Go is expressive, concise, clean, and efficient. Its concurrency
118 mechanisms make it easy to write programs that get the most out of
119 multicore and networked machines, while its novel type system enables
120 flexible and modular program construction. Go compiles quickly to
121 machine code yet has the convenience of garbage collection and the power
122 of run-time reflection. It's a fast, statically typed, compiled language
123 that feels like a dynamically typed, interpreted language.
124 endef
125
126 # go tool requires source present:
127 # https://github.com/golang/go/issues/4635
128 define Package/golang
129 $(call Package/golang/Default)
130 TITLE+= (compiler)
131 DEPENDS+= +golang-src
132 endef
133
134 define Package/golang/description
135 $(call Package/golang/Default/description)
136
137 This package provides an assembler, compiler, linker, and compiled
138 libraries for the Go programming language.
139 endef
140
141 define Package/golang/config
142 source "$(SOURCE)/Config.in"
143 endef
144
145 define Package/golang-doc
146 $(call Package/golang/Default)
147 TITLE+= (documentation)
148 endef
149
150 define Package/golang-doc/description
151 $(call Package/golang/Default/description)
152
153 This package provides the documentation for the Go programming language.
154 endef
155
156 define Package/golang-src
157 $(call Package/golang/Default)
158 TITLE+= (source files)
159 endef
160
161 define Package/golang-src/description
162 $(call Package/golang/Default/description)
163
164 This package provides the Go programming language source files needed
165 for cross-compilation.
166 endef
167
168
169 # Bootstrap
170
171 BOOTSTRAP_ROOT_DIR:=$(call qstrip,$(CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT))
172
173 ifeq ($(BOOTSTRAP_ROOT_DIR),)
174 BOOTSTRAP_ROOT_DIR:=$(BOOTSTRAP_BUILD_DIR)
175
176 define Download/golang-bootstrap
177 FILE:=$(BOOTSTRAP_SOURCE)
178 URL:=$(BOOTSTRAP_SOURCE_URL)
179 HASH:=$(BOOTSTRAP_HASH)
180 endef
181 $(eval $(call Download,golang-bootstrap))
182
183 define Bootstrap/Prepare
184 mkdir -p "$(BOOTSTRAP_BUILD_DIR)"
185 $(BOOTSTRAP_UNPACK)
186 endef
187 Hooks/HostPrepare/Post+=Bootstrap/Prepare
188
189 $(eval $(call GoCompiler/AddProfile,Bootstrap,$(BOOTSTRAP_BUILD_DIR),,bootstrap,$(GO_HOST_OS_ARCH)))
190 endif
191
192
193 # Host
194
195 ifeq ($(GO_HOST_PIE_SUPPORTED),1)
196 HOST_GO_ENABLE_PIE:=1
197 endif
198
199 # when using GO_LDFLAGS to set buildmode=pie, the PIE install suffix
200 # does not apply (we also delete the std lib during Host/Install)
201
202 $(eval $(call GoCompiler/AddProfile,Host,$(HOST_BUILD_DIR),$(HOST_GO_PREFIX),$(HOST_GO_VERSION_ID),$(GO_HOST_OS_ARCH),$(HOST_GO_INSTALL_SUFFIX)))
203
204 HOST_GO_VARS= \
205 GOHOSTARCH="$(GO_HOST_ARCH)" \
206 GOCACHE="$(GO_BUILD_CACHE_DIR)" \
207 GOENV=off \
208 CC="$(HOSTCC_NOCACHE)" \
209 CXX="$(HOSTCXX_NOCACHE)"
210
211 define Host/Configure
212 $(call GoCompiler/Bootstrap/CheckHost,$(BOOTSTRAP_GO_VALID_OS_ARCH))
213 $(call GoCompiler/Host/CheckHost,$(HOST_GO_VALID_OS_ARCH))
214
215 mkdir -p "$(GO_BUILD_CACHE_DIR)"
216 endef
217
218 define Host/Compile
219 $(call GoCompiler/Bootstrap/Make, \
220 $(HOST_GO_VARS) \
221 )
222
223 $(call GoCompiler/Host/Make, \
224 GOROOT_BOOTSTRAP="$(BOOTSTRAP_ROOT_DIR)" \
225 $(if $(HOST_GO_ENABLE_PIE),GO_LDFLAGS="-buildmode pie") \
226 $(HOST_GO_VARS) \
227 )
228 endef
229
230 # if host and target os/arch are the same,
231 # when go compiles a program, it will use the host std lib
232 # so remove it now and force go to rebuild std for target later
233 define Host/Install
234 $(call Host/Uninstall)
235
236 $(call GoCompiler/Host/Install/Bin,)
237 $(call GoCompiler/Host/Install/Src,)
238
239 $(call GoCompiler/Host/Install/BinLinks,)
240
241 rm -rf "$(HOST_GO_ROOT)/pkg/$(GO_HOST_OS_ARCH)$(if $(HOST_GO_INSTALL_SUFFIX),_$(HOST_GO_INSTALL_SUFFIX))"
242
243 $(INSTALL_DIR) "$(HOST_GO_ROOT)/openwrt"
244 $(INSTALL_BIN) ./files/go-gcc-helper "$(HOST_GO_ROOT)/openwrt/"
245 $(LN) go-gcc-helper "$(HOST_GO_ROOT)/openwrt/gcc"
246 $(LN) go-gcc-helper "$(HOST_GO_ROOT)/openwrt/g++"
247 endef
248
249 define Host/Uninstall
250 rm -rf "$(HOST_GO_ROOT)/openwrt"
251
252 $(call GoCompiler/Host/Uninstall/BinLinks,)
253
254 $(call GoCompiler/Host/Uninstall,)
255 endef
256
257
258 # Target
259
260 ifeq ($(GO_PKG_ENABLE_PIE),1)
261 PKG_GO_INSTALL_SUFFIX:=$(GO_TARGET_PIE_INSTALL_SUFFIX)
262 endif
263
264 $(eval $(call GoCompiler/AddProfile,Package,$(PKG_BUILD_DIR),$(PKG_GO_PREFIX),$(PKG_GO_VERSION_ID),$(GO_OS_ARCH),$(PKG_GO_INSTALL_SUFFIX)))
265
266 PKG_GO_ZBOOTSTRAP_MODS:= \
267 s/defaultGO386 = `[^`]*`/defaultGO386 = `$(or $(GO_386),sse2)`/; \
268 s/defaultGOAMD64 = `[^`]*`/defaultGOAMD64 = `$(or $(GO_AMD64),v1)`/; \
269 s/defaultGOARM = `[^`]*`/defaultGOARM = `$(or $(GO_ARM),5)`/; \
270 s/defaultGOMIPS = `[^`]*`/defaultGOMIPS = `$(or $(GO_MIPS),hardfloat)`/; \
271 s/defaultGOMIPS64 = `[^`]*`/defaultGOMIPS64 = `$(or $(GO_MIPS64),hardfloat)`/; \
272 s/defaultGOPPC64 = `[^`]*`/defaultGOPPC64 = `$(or $(GO_PPC64),power8)`/;
273
274 PKG_GO_ZBOOTSTRAP_PATH:=$(PKG_BUILD_DIR)/src/internal/buildcfg/zbootstrap.go
275
276 PKG_GO_VARS= \
277 GOHOSTARCH="$(GO_HOST_ARCH)" \
278 GOCACHE="$(GO_BUILD_CACHE_DIR)" \
279 GOENV=off \
280 GO_GCC_HELPER_PATH="$$$$PATH" \
281 CC=gcc \
282 CXX=g++ \
283 PKG_CONFIG=pkg-config \
284 PATH="$(HOST_GO_ROOT)/openwrt:$$$$PATH"
285
286 PKG_GO_GCFLAGS= \
287 $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
288
289 PKG_GO_ASMFLAGS= \
290 $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
291
292 PKG_GO_LDFLAGS= \
293 -buildid '$(SOURCE_DATE_EPOCH)' \
294 -linkmode external \
295 -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))' \
296 $(if $(CONFIG_NO_STRIP)$(CONFIG_DEBUG),,-s -w)
297
298 # setting -trimpath is not necessary here because the paths inside the
299 # compiler binary are relative to GOROOT_FINAL (PKG_GO_ROOT), which is
300 # static / not dependent on the build environment
301 PKG_GO_INSTALL_ARGS= \
302 -ldflags "all=$(PKG_GO_LDFLAGS)" \
303 $(if $(PKG_GO_GCFLAGS),-gcflags "all=$(PKG_GO_GCFLAGS)") \
304 $(if $(PKG_GO_ASMFLAGS),-asmflags "all=$(PKG_GO_ASMFLAGS)") \
305 $(if $(filter $(GO_PKG_ENABLE_PIE),1),-buildmode pie)
306
307 define Build/Configure
308 mkdir -p "$(GO_BUILD_CACHE_DIR)"
309 endef
310
311 define Build/Compile
312 @echo "Building target Go first stage"
313
314 $(call GoCompiler/Package/Make, \
315 GOROOT_BOOTSTRAP="$(HOST_GO_ROOT)" \
316 GO_GCC_HELPER_CC="$(HOSTCC)" \
317 GO_GCC_HELPER_CXX="$(HOSTCXX)" \
318 $(PKG_GO_VARS) \
319 )
320
321 $(SED) '$(PKG_GO_ZBOOTSTRAP_MODS)' "$(PKG_GO_ZBOOTSTRAP_PATH)"
322
323 ( \
324 if echo 'int main() { return 0; }' | $(TARGET_CC) -o $(PKG_BUILD_DIR)/test-ldso -x c - > /dev/null 2>&1; then \
325 LDSO=$$$$( \
326 readelf -l $(PKG_BUILD_DIR)/test-ldso | \
327 sed -n -e 's/^.*interpreter: \(.*\)[]]/\1/p' \
328 ) ; \
329 fi ; \
330 $(SED) "s,defaultGO_LDSO = \`[^\`]*\`,defaultGO_LDSO = \`$$$$LDSO\`," "$(PKG_GO_ZBOOTSTRAP_PATH)" ; \
331 )
332
333 @echo "Building target Go second stage"
334
335 ( \
336 cd "$(PKG_BUILD_DIR)/bin" ; \
337 export $(GO_PKG_TARGET_VARS) ; \
338 $(CP) go go-host ; \
339 GOROOT_FINAL="$(PKG_GO_ROOT)" \
340 GO_GCC_HELPER_CC="$(TARGET_CC)" \
341 GO_GCC_HELPER_CXX="$(TARGET_CXX)" \
342 $(PKG_GO_VARS) \
343 ./go-host install -a $(PKG_GO_INSTALL_ARGS) std cmd ; \
344 retval="$$$$?" ; \
345 rm -f go-host ; \
346 exit "$$$$retval" ; \
347 )
348 endef
349
350 define Package/golang/install
351 $(call GoCompiler/Package/Install/Bin,$(1)$(PKG_GO_PREFIX))
352 $(call GoCompiler/Package/Install/BinLinks,$(1)$(PKG_GO_PREFIX))
353 endef
354
355 define Package/golang-doc/install
356 $(call GoCompiler/Package/Install/Doc,$(1)$(PKG_GO_PREFIX))
357 endef
358
359 define Package/golang-src/install
360 $(call GoCompiler/Package/Install/Src,$(1)$(PKG_GO_PREFIX))
361 endef
362
363 # src/debug contains ELF executables as test data
364 # and they reference these libraries
365 # we need to call this in Package/$(1)/extra_provides
366 # to pass CheckDependencies in include/package-ipkg.mk
367 define Package/golang-src/extra_provides
368 echo 'libc.so.6'
369 endef
370
371
372 $(eval $(call HostBuild))
373 $(eval $(call BuildPackage,golang))
374 $(eval $(call BuildPackage,golang-doc))
375 $(eval $(call BuildPackage,golang-src))