golang: Update to 1.18, update patch
[feed/packages.git] / lang / golang / golang-package.mk
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 ifeq ($(origin GO_INCLUDE_DIR),undefined)
9 GO_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
10 endif
11
12 include $(GO_INCLUDE_DIR)/golang-values.mk
13
14
15 # Variables (all optional, except GO_PKG) to be set in package
16 # Makefiles:
17 #
18 # GO_PKG (required) - name of Go package
19 #
20 # Go name of the package.
21 #
22 # e.g. GO_PKG:=golang.org/x/text
23 #
24 #
25 # GO_PKG_INSTALL_EXTRA - list of regular expressions, default empty
26 #
27 # Additional files/directories to install. By default, only these
28 # files are installed:
29 #
30 # * Files with one of these extensions:
31 # .go, .c, .cc, .cpp, .h, .hh, .hpp, .proto, .s
32 #
33 # * Files in any 'testdata' directory
34 #
35 # * go.mod, go.sum and go.work, in any directory
36 #
37 # e.g. GO_PKG_INSTALL_EXTRA:=example.toml marshal_test.toml
38 #
39 #
40 # GO_PKG_INSTALL_ALL - boolean (0 or 1), default false
41 #
42 # If true, install all files regardless of extension or directory.
43 #
44 # e.g. GO_PKG_INSTALL_ALL:=1
45 #
46 #
47 # GO_PKG_SOURCE_ONLY - boolean (0 or 1), default false
48 #
49 # If true, 'go install' will not be called. If the package does not
50 # (or should not) build any binaries, then specifying this option will
51 # save build time.
52 #
53 # e.g. GO_PKG_SOURCE_ONLY:=1
54 #
55 #
56 # GO_PKG_BUILD_PKG - list of build targets, default GO_PKG/...
57 #
58 # Build targets for compiling this Go package, i.e. arguments passed
59 # to 'go install'.
60 #
61 # e.g. GO_PKG_BUILD_PKG:=github.com/debian/ratt/cmd/...
62 #
63 #
64 # GO_PKG_EXCLUDES - list of regular expressions, default empty
65 #
66 # Patterns to exclude from the build targets expanded from
67 # GO_PKG_BUILD_PKG.
68 #
69 # e.g. GO_PKG_EXCLUDES:=examples/
70 #
71 #
72 # GO_PKG_GO_GENERATE - boolean (0 or 1), default false
73 #
74 # If true, 'go generate' will be called on all build targets (as
75 # determined by GO_PKG_BUILD_PKG and GO_PKG_EXCLUDES). This is usually
76 # not necessary.
77 #
78 # e.g. GO_PKG_GO_GENERATE:=1
79 #
80 #
81 # GO_PKG_GCFLAGS - list of options, default empty
82 #
83 # Additional go tool compile options to use when building targets.
84 #
85 # e.g. GO_PKG_GCFLAGS:=-N -l
86 #
87 #
88 # GO_PKG_LDFLAGS - list of options, default empty
89 #
90 # Additional go tool link options to use when building targets.
91 #
92 # Note that the OpenWrt build system has an option to strip binaries
93 # (enabled by default), so -s (Omit the symbol table and debug
94 # information) and -w (Omit the DWARF symbol table) flags are not
95 # necessary.
96 #
97 # e.g. GO_PKG_LDFLAGS:=-r dir1:dir2 -u
98 #
99 #
100 # GO_PKG_LDFLAGS_X - list of string variable definitions, default empty
101 #
102 # Each definition will be passed as the parameter to the -X go tool
103 # link option, i.e. -ldflags "-X importpath.name=value".
104 #
105 # e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH)
106 #
107 #
108 # GO_PKG_TAGS - list of build tags, default empty
109 #
110 # Build tags to consider satisfied during the build, passed as the
111 # parameter to the -tags option for 'go install'.
112 #
113 # e.g. GO_PKG_TAGS:=release,noupgrade
114 #
115 #
116 # GO_PKG_INSTALL_BIN_PATH - target directory path, default /usr/bin
117 #
118 # Directory path under "dest_dir" where binaries will be installed by
119 # '$(call GoPackage/Package/Install/Bin,dest_dir)'.
120 #
121 # e.g. GO_PKG_INSTALL_BIN_PATH:=/sbin
122
123 # Credit for this package build process (GoPackage/Build/Configure and
124 # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
125 # https://salsa.debian.org/go-team/packages/dh-golang
126
127
128 GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/...
129 GO_PKG_INSTALL_BIN_PATH?=/usr/bin
130
131 GO_PKG_WORK_DIR_NAME:=.go_work
132 GO_PKG_BUILD_DIR=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)/build
133 GO_PKG_BUILD_BIN_DIR=$(GO_PKG_BUILD_DIR)/bin$(if $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS_ARCH))
134
135 GO_PKG_BUILD_DEPENDS_PATH:=/usr/share/gocode
136 GO_PKG_BUILD_DEPENDS_SRC=$(STAGING_DIR)$(GO_PKG_BUILD_DEPENDS_PATH)/src
137
138 ifdef CONFIG_PKG_ASLR_PIE_ALL
139 ifeq ($(strip $(PKG_ASLR_PIE)),1)
140 ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
141 GO_PKG_ENABLE_PIE:=1
142 else
143 $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
144 endif
145 endif
146 endif
147
148 ifdef CONFIG_PKG_ASLR_PIE_REGULAR
149 ifeq ($(strip $(PKG_ASLR_PIE_REGULAR)),1)
150 ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
151 GO_PKG_ENABLE_PIE:=1
152 else
153 $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
154 endif
155 endif
156 endif
157
158 ifdef CONFIG_GOLANG_SPECTRE
159 ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1)
160 GO_PKG_ENABLE_SPECTRE:=1
161 else
162 $(warning Spectre mitigations are not supported for $(GO_ARCH))
163 endif
164 endif
165
166 # sstrip causes corrupted section header size
167 ifneq ($(CONFIG_USE_SSTRIP),)
168 ifneq ($(CONFIG_DEBUG),)
169 GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
170 else
171 GO_PKG_STRIP_ARGS:=--strip-all
172 endif
173 STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
174 endif
175
176 define GoPackage/GoSubMenu
177 SUBMENU:=Go
178 SECTION:=lang
179 CATEGORY:=Languages
180 endef
181
182 GO_PKG_BUILD_CONFIG_VARS= \
183 GO_PKG="$(strip $(GO_PKG))" \
184 GO_INSTALL_EXTRA="$(strip $(GO_PKG_INSTALL_EXTRA))" \
185 GO_INSTALL_ALL="$(strip $(GO_PKG_INSTALL_ALL))" \
186 GO_SOURCE_ONLY="$(strip $(GO_PKG_SOURCE_ONLY))" \
187 GO_BUILD_PKG="$(strip $(GO_PKG_BUILD_PKG))" \
188 GO_EXCLUDES="$(strip $(GO_PKG_EXCLUDES))" \
189 GO_GO_GENERATE="$(strip $(GO_PKG_GO_GENERATE))" \
190 GO_INSTALL_BIN_PATH="$(strip $(GO_PKG_INSTALL_BIN_PATH))" \
191 BUILD_DIR="$(PKG_BUILD_DIR)" \
192 GO_BUILD_DIR="$(GO_PKG_BUILD_DIR)" \
193 GO_BUILD_BIN_DIR="$(GO_PKG_BUILD_BIN_DIR)" \
194 GO_BUILD_DEPENDS_PATH="$(GO_PKG_BUILD_DEPENDS_PATH)" \
195 GO_BUILD_DEPENDS_SRC="$(GO_PKG_BUILD_DEPENDS_SRC)"
196
197 GO_PKG_TARGET_VARS= \
198 GOOS="$(GO_OS)" \
199 GOARCH="$(GO_ARCH)" \
200 GO386="$(GO_386)" \
201 GOAMD64="$(GO_AMD64)" \
202 GOARM="$(GO_ARM)" \
203 GOMIPS="$(GO_MIPS)" \
204 GOMIPS64="$(GO_MIPS64)" \
205 GOPPC64="$(GO_PPC64)" \
206 CGO_ENABLED=1 \
207 CC="$(TARGET_CC)" \
208 CXX="$(TARGET_CXX)" \
209 CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
210 CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
211 CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \
212 CGO_LDFLAGS="$(TARGET_LDFLAGS)"
213
214 GO_PKG_BUILD_VARS= \
215 GOPATH="$(GO_PKG_BUILD_DIR)" \
216 GOCACHE="$(GO_BUILD_CACHE_DIR)" \
217 GOMODCACHE="$(GO_MOD_CACHE_DIR)" \
218 GOENV=off
219
220 GO_PKG_VARS= \
221 $(GO_PKG_TARGET_VARS) \
222 $(GO_PKG_BUILD_VARS)
223
224 GO_PKG_DEFAULT_GCFLAGS= \
225 $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
226
227 GO_PKG_DEFAULT_ASMFLAGS= \
228 $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
229
230 GO_PKG_DEFAULT_LDFLAGS= \
231 -buildid '$(SOURCE_DATE_EPOCH)' \
232 -linkmode external \
233 -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))'
234
235 GO_PKG_CUSTOM_LDFLAGS= \
236 $(GO_PKG_LDFLAGS) \
237 $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X))
238
239 GO_PKG_INSTALL_ARGS= \
240 -v \
241 -trimpath \
242 -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \
243 $(if $(GO_PKG_DEFAULT_GCFLAGS),-gcflags "all=$(GO_PKG_DEFAULT_GCFLAGS)") \
244 $(if $(GO_PKG_DEFAULT_ASMFLAGS),-asmflags "all=$(GO_PKG_DEFAULT_ASMFLAGS)") \
245 $(if $(filter $(GO_PKG_ENABLE_PIE),1),-buildmode pie) \
246 $(if $(filter $(GO_ARCH),arm),-installsuffix "v$(GO_ARM)") \
247 $(if $(filter $(GO_ARCH),mips mipsle),-installsuffix "$(GO_MIPS)") \
248 $(if $(filter $(GO_ARCH),mips64 mips64le),-installsuffix "$(GO_MIPS64)") \
249 $(if $(GO_PKG_GCFLAGS),-gcflags "$(GO_PKG_GCFLAGS) $(GO_PKG_DEFAULT_GCFLAGS)") \
250 $(if $(GO_PKG_CUSTOM_LDFLAGS),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)") \
251 $(if $(GO_PKG_TAGS),-tags "$(GO_PKG_TAGS)")
252
253 define GoPackage/Build/Configure
254 $(GO_GENERAL_BUILD_CONFIG_VARS) \
255 $(GO_PKG_BUILD_CONFIG_VARS) \
256 $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure
257 endef
258
259 # $(1) additional arguments for go command line (optional)
260 define GoPackage/Build/Compile
261 $(GO_GENERAL_BUILD_CONFIG_VARS) \
262 $(GO_PKG_BUILD_CONFIG_VARS) \
263 $(GO_PKG_VARS) \
264 $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_PKG_INSTALL_ARGS) $(1)
265 endef
266
267 define GoPackage/Build/InstallDev
268 $(call GoPackage/Package/Install/Src,$(1))
269 endef
270
271 define GoPackage/Package/Install/Bin
272 $(GO_GENERAL_BUILD_CONFIG_VARS) \
273 $(GO_PKG_BUILD_CONFIG_VARS) \
274 $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(1)"
275 endef
276
277 define GoPackage/Package/Install/Src
278 $(GO_GENERAL_BUILD_CONFIG_VARS) \
279 $(GO_PKG_BUILD_CONFIG_VARS) \
280 $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(1)"
281 endef
282
283 define GoPackage/Package/Install
284 $(if $(filter $(GO_PKG_SOURCE_ONLY),1),, \
285 $(call GoPackage/Package/Install/Bin,$(1)) \
286 )
287 $(call GoPackage/Package/Install/Src,$(1))
288 endef
289
290
291 ifneq ($(strip $(GO_PKG)),)
292 ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1)
293 PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE
294 endif
295
296 Build/Configure=$(call GoPackage/Build/Configure)
297 Build/Compile=$(call GoPackage/Build/Compile)
298 Hooks/Compile/Post+=Go/CacheCleanup
299 Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
300 endif
301
302 define GoPackage
303 ifndef Package/$(1)/install
304 Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
305 endif
306 endef
307
308 define GoBinPackage
309 ifndef Package/$(1)/install
310 Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
311 endif
312 endef
313
314 define GoSrcPackage
315 ifndef Package/$(1)/install
316 Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
317 endif
318 endef