2 # Copyright (C) 2018 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 and go.sum, 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 arguments, default empty
83 # Additional go tool compile arguments to use when building targets.
85 # e.g. GO_PKG_GCFLAGS:=-N -l
88 # GO_PKG_LDFLAGS - list of arguments, default empty
90 # Additional go tool link arguments to use when building targets.
92 # e.g. GO_PKG_LDFLAGS:=-s -w
95 # GO_PKG_LDFLAGS_X - list of string variable definitions, default empty
97 # Each definition will be passed as the parameter to the -X go tool
98 # link argument, i.e. -ldflags "-X importpath.name=value"
100 # e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH)
102 # Credit for this package build process (GoPackage/Build/Configure and
103 # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
104 # https://salsa.debian.org/go-team/packages/dh-golang
107 # for building packages, not user code
108 GO_PKG_PATH
:=/usr
/share
/gocode
110 GO_PKG_BUILD_PKG?
=$(strip $(GO_PKG
))/...
112 GO_PKG_WORK_DIR_NAME
:=.go_work
113 GO_PKG_WORK_DIR
:=$(PKG_BUILD_DIR
)/$(GO_PKG_WORK_DIR_NAME
)
115 GO_PKG_BUILD_DIR
:=$(GO_PKG_WORK_DIR
)/build
116 GO_PKG_CACHE_DIR
:=$(GO_PKG_WORK_DIR
)/cache
118 GO_PKG_BUILD_BIN_DIR
:=$(GO_PKG_BUILD_DIR
)/bin
$(if
$(GO_HOST_TARGET_DIFFERENT
),/$(GO_OS_ARCH
))
120 GO_PKG_BUILD_DEPENDS_SRC
:=$(STAGING_DIR
)$(GO_PKG_PATH
)/src
122 ifdef CONFIG_PKG_ASLR_PIE_ALL
123 ifeq ($(strip $(PKG_ASLR_PIE
)),1)
124 ifeq ($(GO_TARGET_PIE_SUPPORTED
),1)
127 $(warning PIE buildmode is not supported for
$(GO_OS
)/$(GO_ARCH
))
132 ifdef CONFIG_PKG_ASLR_PIE_REGULAR
133 ifeq ($(strip $(PKG_ASLR_PIE_REGULAR
)),1)
134 ifeq ($(GO_TARGET_PIE_SUPPORTED
),1)
137 $(warning PIE buildmode is not supported for
$(GO_OS
)/$(GO_ARCH
))
142 # sstrip causes corrupted section header size
143 ifneq ($(CONFIG_USE_SSTRIP
),)
144 ifneq ($(CONFIG_DEBUG
),)
145 GO_PKG_STRIP_ARGS
:=--strip-unneeded
--remove-section
=.comment
--remove-section
=.note
147 GO_PKG_STRIP_ARGS
:=--strip-all
149 STRIP
:=$(TARGET_CROSS
)strip $(GO_PKG_STRIP_ARGS
)
151 export CROSS
="$(TARGET_CROSS)" \
152 $(if
$(PKG_BUILD_ID
),KEEP_BUILD_ID
=1) \
153 $(if
$(CONFIG_KERNEL_KALLSYMS
),NO_RENAME
=1) \
154 $(if
$(CONFIG_KERNEL_PROFILING
),KEEP_SYMBOLS
=1); \
155 NM
="$(TARGET_CROSS)nm" \
157 STRIP_KMOD
="$(SCRIPT_DIR)/strip-kmod.sh" \
158 PATCHELF
="$(STAGING_DIR_HOST)/bin/patchelf" \
159 $(SCRIPT_DIR
)/rstrip.sh
162 define GoPackage
/GoSubMenu
168 define GoPackage
/Environment
/Target
174 GOMIPS64
=$(GO_MIPS64
) \
178 CGO_CFLAGS
="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
179 CGO_CPPFLAGS
="$(TARGET_CPPFLAGS)" \
180 CGO_CXXFLAGS
="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \
181 CGO_LDFLAGS
="$(TARGET_LDFLAGS)"
184 define GoPackage
/Environment
/Build
185 GOPATH
=$(GO_PKG_BUILD_DIR
) \
186 GOCACHE
=$(GO_PKG_CACHE_DIR
) \
190 define GoPackage
/Environment
/Default
191 $(call GoPackage
/Environment
/Target
) \
192 $(call GoPackage
/Environment
/Build
)
195 GoPackage
/Environment
=$(call GoPackage
/Environment
/Default
)
197 # false if directory does not exist
198 GoPackage
/is_dir_not_empty
=$$$$($(FIND
) $(1) -maxdepth
0 -type d \
! -empty
2>/dev
/null
)
200 GoPackage
/has_binaries
=$(call GoPackage
/is_dir_not_empty
,$(GO_PKG_BUILD_BIN_DIR
))
202 define GoPackage
/Build
/Configure
204 cd
$(PKG_BUILD_DIR
) ; \
205 mkdir
-p
$(GO_PKG_BUILD_DIR
)/bin
$(GO_PKG_BUILD_DIR
)/src
$(GO_PKG_CACHE_DIR
) ; \
207 files
=$$$$($(FIND
) .
/ \
208 -type d
-a \
( -path
'./.git' -o
-path
'./$(GO_PKG_WORK_DIR_NAME)' \
) -prune
-o \
209 \
! -type d
-print | \
212 if
[ "$(strip $(GO_PKG_INSTALL_ALL))" != 1 ]; then \
213 code
=$$$$(echo
"$$$$files" | grep
'\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
214 testdata
=$$$$(echo
"$$$$files" | grep
'\(^\|/\)testdata/') ; \
215 gomod
=$$$$(echo
"$$$$files" | grep
'\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
217 for pattern in
$(GO_PKG_INSTALL_EXTRA
); do \
218 extra
=$$$$(echo
"$$$$extra"; echo
"$$$$files" | grep
"$$$$pattern") ; \
221 files
=$$$$(echo
"$$$$code"; echo
"$$$$testdata"; echo
"$$$$gomod"; echo
"$$$$extra") ; \
222 files
=$$$$(echo
"$$$$files" | grep
-v
'^[[:space:]]*$$$$' |
sort -u
) ; \
227 echo
"Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))" ; \
228 for file in
$$$$files; do \
230 dest
=$(GO_PKG_BUILD_DIR
)/src
/$(strip $(GO_PKG
))/$$$$file ; \
231 mkdir
-p
$$$$(dirname
$$$$dest) ; \
232 $(CP
) $$$$file $$$$dest ; \
239 local dirs
dir base
; \
241 if
[ -n
"$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
242 echo
"$$$$src is already a Go library" ; \
246 dirs
=$$$$($(FIND
) $$$$src -mindepth
1 -maxdepth
1 -type d
) ; \
247 for
dir in
$$$$dirs; do \
248 base
=$$$$(basename $$$$dir) ; \
249 if
[ -d
$$$$dest/$$$$base ]; then \
251 *$(GO_PKG_PATH
)/src
/$(strip $(GO_PKG
))) \
252 echo
"$(strip $(GO_PKG)) is already installed. Please check for circular dependencies." ;; \
254 link_contents
$$$$src/$$$$base $$$$dest/$$$$base ;; \
257 echo
"...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
258 $(LN
) $$$$src/$$$$base $$$$dest/$$$$base ; \
263 if
[ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
264 if
[ -d
$(GO_PKG_BUILD_DEPENDS_SRC
) ]; then \
265 echo
"Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
266 link_contents
$(GO_PKG_BUILD_DEPENDS_SRC
) $(GO_PKG_BUILD_DIR
)/src
; \
268 echo
"$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
271 echo
"Not building binaries, skipping symlinks" ; \
277 # $(1) additional arguments for go command line (optional)
278 define GoPackage
/Build
/Compile
280 cd
$(GO_PKG_BUILD_DIR
) ; \
281 export $(call GoPackage
/Environment
) ; \
283 echo
"Finding targets" ; \
284 targets
=$$$$(go list
$(GO_PKG_BUILD_PKG
)) ; \
285 for pattern in
$(GO_PKG_EXCLUDES
); do \
286 targets
=$$$$(echo
"$$$$targets" | grep
-v
"$$$$pattern") ; \
290 if
[ "$(strip $(GO_PKG_GO_GENERATE))" = 1 ]; then \
291 echo
"Calling go generate" ; \
292 go generate
-v
$(1) $$$$targets ; \
296 if
[ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
297 echo
"Building targets" ; \
299 arm
) installsuffix
="v$(GO_ARM)" ;; \
300 mips|mipsle
) installsuffix
="$(GO_MIPS)" ;; \
301 mips64|mips64le
) installsuffix
="$(GO_MIPS64)" ;; \
303 ldflags="-linkmode external -extldflags '$(TARGET_LDFLAGS:-z%=-Wl,-z,%)'" ; \
304 pkg_gcflags
="$(strip $(GO_PKG_GCFLAGS))" ; \
305 pkg_ldflags
="$(strip $(GO_PKG_LDFLAGS))" ; \
306 for def in
$(GO_PKG_LDFLAGS_X
); do \
307 pkg_ldflags
="$$$$pkg_ldflags -X $$$$def" ; \
310 $(if
$(GO_PKG_ENABLE_PIE
),-buildmode pie
) \
311 $$$${installsuffix
:+-installsuffix
$$$$installsuffix} \
313 -ldflags "all=$$$$ldflags" \
315 $$$${pkg_gcflags
:+-gcflags
"$$$$pkg_gcflags"} \
316 $$$${pkg_ldflags
:+-ldflags "$$$$pkg_ldflags $$$$ldflags"} \
322 if
[ "$$$$retval" -eq
0 ] && [ -z
"$(call GoPackage/has_binaries)" ]; then \
323 echo
"No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
327 echo
"Cleaning module download cache (golang/go#27455)" ; \
328 go
clean -modcache
; \
335 define GoPackage
/Build
/InstallDev
336 $(call GoPackage
/Package
/Install
/Src
,$(1))
339 define GoPackage
/Package
/Install
/Bin
340 if
[ -n
"$(call GoPackage/has_binaries)" ]; then \
341 $(INSTALL_DIR
) $(1)/usr
/bin
; \
342 $(INSTALL_BIN
) $(GO_PKG_BUILD_BIN_DIR
)/* $(1)/usr
/bin
/ ; \
346 define GoPackage
/Package
/Install
/Src
347 dir=$$$$(dirname
$(GO_PKG
)) ; \
348 $(INSTALL_DIR
) $(1)$(GO_PKG_PATH
)/src
/$$$$dir ; \
349 $(CP
) $(GO_PKG_BUILD_DIR
)/src
/$(strip $(GO_PKG
)) $(1)$(GO_PKG_PATH
)/src
/$$$$dir/
352 define GoPackage
/Package
/Install
353 $(call GoPackage
/Package
/Install
/Bin
,$(1))
354 $(call GoPackage
/Package
/Install
/Src
,$(1))
358 ifneq ($(strip $(GO_PKG
)),)
359 Build
/Configure
=$(call GoPackage
/Build
/Configure
)
360 Build
/Compile
=$(call GoPackage
/Build
/Compile
)
361 Build
/InstallDev
=$(call GoPackage
/Build
/InstallDev
,$(1))
365 ifndef Package
/$(1)/install
366 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
,$$(1))
371 ifndef Package
/$(1)/install
372 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
/Bin
,$$(1))
377 ifndef Package
/$(1)/install
378 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
/Src
,$$(1))