2 # Copyright (C) 2018-2022 Jeffery To
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
8 ifeq ($(origin GO_INCLUDE_DIR
),undefined
)
9 GO_INCLUDE_DIR
:=$(dir $(lastword
$(MAKEFILE_LIST
)))
12 include $(GO_INCLUDE_DIR
)/golang-values.mk
15 # Variables (all optional, except GO_PKG) to be set in package
18 # GO_PKG (required) - name of Go package
20 # Go name of the package.
22 # e.g. GO_PKG:=golang.org/x/text
25 # GO_PKG_INSTALL_EXTRA - list of regular expressions, default empty
27 # Additional files/directories to install. By default, only these
28 # files are installed:
30 # * Files with one of these extensions:
31 # .go, .c, .cc, .cpp, .h, .hh, .hpp, .proto, .s
33 # * Files in any 'testdata' directory
35 # * go.mod, go.sum and go.work, in any directory
37 # e.g. GO_PKG_INSTALL_EXTRA:=example.toml marshal_test.toml
40 # GO_PKG_INSTALL_ALL - boolean (0 or 1), default false
42 # If true, install all files regardless of extension or directory.
44 # e.g. GO_PKG_INSTALL_ALL:=1
47 # GO_PKG_SOURCE_ONLY - boolean (0 or 1), default false
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
53 # e.g. GO_PKG_SOURCE_ONLY:=1
56 # GO_PKG_BUILD_PKG - list of build targets, default GO_PKG/...
58 # Build targets for compiling this Go package, i.e. arguments passed
61 # e.g. GO_PKG_BUILD_PKG:=github.com/debian/ratt/cmd/...
64 # GO_PKG_EXCLUDES - list of regular expressions, default empty
66 # Patterns to exclude from the build targets expanded from
69 # e.g. GO_PKG_EXCLUDES:=examples/
72 # GO_PKG_GO_GENERATE - boolean (0 or 1), default false
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
78 # e.g. GO_PKG_GO_GENERATE:=1
81 # GO_PKG_GCFLAGS - list of options, default empty
83 # Additional go tool compile options to use when building targets.
85 # e.g. GO_PKG_GCFLAGS:=-N -l
88 # GO_PKG_LDFLAGS - list of options, default empty
90 # Additional go tool link options to use when building targets.
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
97 # e.g. GO_PKG_LDFLAGS:=-r dir1:dir2 -u
100 # GO_PKG_LDFLAGS_X - list of string variable definitions, default empty
102 # Each definition will be passed as the parameter to the -X go tool
103 # link option, i.e. -ldflags "-X importpath.name=value".
105 # e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH)
108 # GO_PKG_TAGS - list of build tags, default empty
110 # Build tags to consider satisfied during the build, passed as the
111 # parameter to the -tags option for 'go install'.
113 # e.g. GO_PKG_TAGS:=release,noupgrade
116 # GO_PKG_INSTALL_BIN_PATH - target directory path, default /usr/bin
118 # Directory path under "dest_dir" where binaries will be installed by
119 # '$(call GoPackage/Package/Install/Bin,dest_dir)'.
121 # e.g. GO_PKG_INSTALL_BIN_PATH:=/sbin
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
128 GO_PKG_BUILD_PKG?
=$(strip $(GO_PKG
))/...
129 GO_PKG_INSTALL_BIN_PATH?
=/usr
/bin
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
))
135 GO_PKG_BUILD_DEPENDS_PATH
:=/usr
/share
/gocode
136 GO_PKG_BUILD_DEPENDS_SRC
=$(STAGING_DIR
)$(GO_PKG_BUILD_DEPENDS_PATH
)/src
138 ifdef CONFIG_PKG_ASLR_PIE_ALL
139 ifeq ($(strip $(PKG_ASLR_PIE
)),1)
140 ifeq ($(GO_TARGET_PIE_SUPPORTED
),1)
143 $(warning PIE buildmode is not supported for
$(GO_OS
)/$(GO_ARCH
))
148 ifdef CONFIG_PKG_ASLR_PIE_REGULAR
149 ifeq ($(strip $(PKG_ASLR_PIE_REGULAR
)),1)
150 ifeq ($(GO_TARGET_PIE_SUPPORTED
),1)
153 $(warning PIE buildmode is not supported for
$(GO_OS
)/$(GO_ARCH
))
158 ifdef CONFIG_GOLANG_SPECTRE
159 ifeq ($(GO_TARGET_SPECTRE_SUPPORTED
),1)
160 GO_PKG_ENABLE_SPECTRE
:=1
162 $(warning Spectre mitigations are not supported for
$(GO_ARCH
))
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
171 GO_PKG_STRIP_ARGS
:=--strip-all
173 STRIP
:=$(TARGET_CROSS
)strip $(GO_PKG_STRIP_ARGS
)
176 define GoPackage
/GoSubMenu
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)"
197 GO_PKG_TARGET_VARS
= \
199 GOARCH
="$(GO_ARCH)" \
201 GOAMD64
="$(GO_AMD64)" \
203 GOMIPS
="$(GO_MIPS)" \
204 GOMIPS64
="$(GO_MIPS64)" \
205 GOPPC64
="$(GO_PPC64)" \
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)"
215 GOPATH
="$(GO_PKG_BUILD_DIR)" \
216 GOCACHE
="$(GO_BUILD_CACHE_DIR)" \
217 GOMODCACHE
="$(GO_MOD_CACHE_DIR)" \
222 $(GO_PKG_TARGET_VARS
) \
225 GO_PKG_DEFAULT_GCFLAGS
= \
226 $(if
$(GO_PKG_ENABLE_SPECTRE
),-spectre
all)
228 GO_PKG_DEFAULT_ASMFLAGS
= \
229 $(if
$(GO_PKG_ENABLE_SPECTRE
),-spectre
all)
231 GO_PKG_DEFAULT_LDFLAGS
= \
232 -buildid
'$(SOURCE_DATE_EPOCH)' \
234 -extldflags
'$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))'
236 GO_PKG_CUSTOM_LDFLAGS
= \
238 $(patsubst %,-X
%,$(GO_PKG_LDFLAGS_X
))
240 GO_PKG_INSTALL_ARGS
= \
244 -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \
245 $(if
$(strip $(GO_PKG_DEFAULT_GCFLAGS
)),-gcflags
"all=$(GO_PKG_DEFAULT_GCFLAGS)") \
246 $(if
$(strip $(GO_PKG_DEFAULT_ASMFLAGS
)),-asmflags
"all=$(GO_PKG_DEFAULT_ASMFLAGS)") \
247 $(if
$(GO_PKG_ENABLE_PIE
),-buildmode pie
) \
248 $(if
$(filter $(GO_ARCH
),arm
),-installsuffix
"v$(GO_ARM)") \
249 $(if
$(filter $(GO_ARCH
),mips mipsle
),-installsuffix
"$(GO_MIPS)") \
250 $(if
$(filter $(GO_ARCH
),mips64 mips64le
),-installsuffix
"$(GO_MIPS64)") \
251 $(if
$(strip $(GO_PKG_GCFLAGS
)),-gcflags
"$(GO_PKG_GCFLAGS) $(GO_PKG_DEFAULT_GCFLAGS)") \
252 $(if
$(strip $(GO_PKG_CUSTOM_LDFLAGS
)),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)") \
253 $(if
$(strip $(GO_PKG_TAGS
)),-tags "$(GO_PKG_TAGS)")
255 define GoPackage
/Build
/Configure
256 $(GO_GENERAL_BUILD_CONFIG_VARS
) \
257 $(GO_PKG_BUILD_CONFIG_VARS
) \
258 $(SHELL
) $(GO_INCLUDE_DIR
)/golang-build.sh configure
261 # $(1) additional arguments for go command line (optional)
262 define GoPackage
/Build
/Compile
263 $(GO_GENERAL_BUILD_CONFIG_VARS
) \
264 $(GO_PKG_BUILD_CONFIG_VARS
) \
266 $(SHELL
) $(GO_INCLUDE_DIR
)/golang-build.sh build
$(GO_PKG_INSTALL_ARGS
) $(1)
269 define GoPackage
/Build
/InstallDev
270 $(call GoPackage
/Package
/Install
/Src
,$(1))
273 define GoPackage
/Package
/Install
/Bin
274 $(GO_GENERAL_BUILD_CONFIG_VARS
) \
275 $(GO_PKG_BUILD_CONFIG_VARS
) \
276 $(SHELL
) $(GO_INCLUDE_DIR
)/golang-build.sh install_bin
"$(1)"
279 define GoPackage
/Package
/Install
/Src
280 $(GO_GENERAL_BUILD_CONFIG_VARS
) \
281 $(GO_PKG_BUILD_CONFIG_VARS
) \
282 $(SHELL
) $(GO_INCLUDE_DIR
)/golang-build.sh install_src
"$(1)"
285 define GoPackage
/Package
/Install
286 $(if
$(filter $(GO_PKG_SOURCE_ONLY
),1),, \
287 $(call GoPackage
/Package
/Install
/Bin
,$(1)) \
289 $(call GoPackage
/Package
/Install
/Src
,$(1))
293 ifneq ($(strip $(GO_PKG
)),)
294 ifeq ($(GO_TARGET_SPECTRE_SUPPORTED
),1)
295 PKG_CONFIG_DEPENDS
+=CONFIG_GOLANG_SPECTRE
298 Build
/Configure
=$(call GoPackage
/Build
/Configure
)
299 Build
/Compile
=$(call GoPackage
/Build
/Compile
)
300 Hooks
/Compile
/Post
+=Go
/CacheCleanup
301 Build
/InstallDev
=$(call GoPackage
/Build
/InstallDev
,$(1))
305 ifndef Package
/$(1)/install
306 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
,$$(1))
311 ifndef Package
/$(1)/install
312 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
/Bin
,$$(1))
317 ifndef Package
/$(1)/install
318 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
/Src
,$$(1))