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
80 # Credit for this package build process (GoPackage/Build/Configure and
81 # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
82 # https://salsa.debian.org/go-team/packages/dh-golang
85 # for building packages, not user code
86 GO_PKG_PATH
:=/usr
/share
/gocode
88 GO_PKG_BUILD_PKG?
=$(GO_PKG
)/...
90 GO_PKG_WORK_DIR_NAME
:=.go_work
91 GO_PKG_WORK_DIR
:=$(PKG_BUILD_DIR
)/$(GO_PKG_WORK_DIR_NAME
)
93 GO_PKG_BUILD_DIR
:=$(GO_PKG_WORK_DIR
)/build
94 GO_PKG_CACHE_DIR
:=$(GO_PKG_WORK_DIR
)/cache
95 GO_PKG_TMP_DIR
:=$(GO_PKG_WORK_DIR
)/tmp
97 GO_PKG_BUILD_BIN_DIR
:=$(GO_PKG_BUILD_DIR
)/bin
$(if \
98 $(GO_HOST_TARGET_DIFFERENT
),/$(GO_OS
)_
$(GO_ARCH
))
100 GO_PKG_BUILD_DEPENDS_SRC
:=$(STAGING_DIR
)$(GO_PKG_PATH
)/src
102 # sstrip causes corrupted section header size
103 ifneq ($(CONFIG_USE_SSTRIP
),)
104 ifneq ($(CONFIG_DEBUG
),)
105 GO_PKG_STRIP_ARGS
:=--strip-unneeded
--remove-section
=.comment
--remove-section
=.note
107 GO_PKG_STRIP_ARGS
:=--strip-all
109 STRIP
:=$(TARGET_CROSS
)strip $(GO_PKG_STRIP_ARGS
)
111 export CROSS
="$(TARGET_CROSS)" \
112 $(if
$(PKG_BUILD_ID
),KEEP_BUILD_ID
=1) \
113 $(if
$(CONFIG_KERNEL_KALLSYMS
),NO_RENAME
=1) \
114 $(if
$(CONFIG_KERNEL_PROFILING
),KEEP_SYMBOLS
=1); \
115 NM
="$(TARGET_CROSS)nm" \
117 STRIP_KMOD
="$(SCRIPT_DIR)/strip-kmod.sh" \
118 PATCHELF
="$(STAGING_DIR_HOST)/bin/patchelf" \
119 $(SCRIPT_DIR
)/rstrip.sh
122 define GoPackage
/GoSubMenu
128 define GoPackage
/Environment
134 GOMIPS64
=$(GO_MIPS64
) \
136 CGO_CFLAGS
="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
137 CGO_CPPFLAGS
="$(TARGET_CPPFLAGS)" \
138 CGO_CXXFLAGS
="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
141 # false if directory does not exist
142 GoPackage
/is_dir_not_empty
=$$$$($(FIND
) $(1) -maxdepth
0 -type d \
! -empty
2>/dev
/null
)
144 GoPackage
/has_binaries
=$(call GoPackage
/is_dir_not_empty
,$(GO_PKG_BUILD_BIN_DIR
))
146 define GoPackage
/Build
/Configure
148 cd
$(PKG_BUILD_DIR
) ; \
149 mkdir
-p
$(GO_PKG_BUILD_DIR
)/bin
$(GO_PKG_BUILD_DIR
)/src \
150 $(GO_PKG_CACHE_DIR
) $(GO_PKG_TMP_DIR
) ; \
152 files
=$$$$($(FIND
) .
/ \
153 -type d
-a \
( -path
'./.git' -o
-path
'./$(GO_PKG_WORK_DIR_NAME)' \
) -prune
-o \
154 \
! -type d
-print | \
157 if
[ "$(GO_PKG_INSTALL_ALL)" != 1 ]; then \
158 code
=$$$$(echo
"$$$$files" | grep
'\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
159 testdata
=$$$$(echo
"$$$$files" | grep
'\(^\|/\)testdata/') ; \
160 gomod
=$$$$(echo
"$$$$files" | grep
'\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
162 for pattern in
$(GO_PKG_INSTALL_EXTRA
); do \
163 extra
=$$$$(echo
"$$$$extra"; echo
"$$$$files" | grep
"$$$$pattern") ; \
166 files
=$$$$(echo
"$$$$code"; echo
"$$$$testdata"; echo
"$$$$gomod"; echo
"$$$$extra") ; \
167 files
=$$$$(echo
"$$$$files" | grep
-v
'^[[:space:]]*$$$$' |
sort -u
) ; \
172 echo
"Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)" ; \
173 for file in
$$$$files; do \
175 dest
=$(GO_PKG_BUILD_DIR
)/src
/$(GO_PKG
)/$$$$file ; \
176 mkdir
-p
$$$$(dirname
$$$$dest) ; \
177 $(CP
) $$$$file $$$$dest ; \
184 local dirs
dir base
; \
186 if
[ -n
"$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
187 echo
"$$$$src is already a Go library" ; \
191 dirs
=$$$$($(FIND
) $$$$src -mindepth
1 -maxdepth
1 -type d
) ; \
192 for
dir in
$$$$dirs; do \
193 base
=$$$$(basename $$$$dir) ; \
194 if
[ -d
$$$$dest/$$$$base ]; then \
196 *$(GO_PKG_PATH
)/src
/$(GO_PKG
)) \
197 echo
"$(GO_PKG) is already installed. Please check for circular dependencies." ;; \
199 link_contents
$$$$src/$$$$base $$$$dest/$$$$base ;; \
202 echo
"...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
203 $(LN
) $$$$src/$$$$base $$$$dest/$$$$base ; \
208 if
[ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
209 if
[ -d
$(GO_PKG_BUILD_DEPENDS_SRC
) ]; then \
210 echo
"Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
211 link_contents
$(GO_PKG_BUILD_DEPENDS_SRC
) $(GO_PKG_BUILD_DIR
)/src
; \
213 echo
"$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
216 echo
"Not building binaries, skipping symlinks" ; \
222 # $(1) additional arguments for go command line (optional)
223 define GoPackage
/Build
/Compile
225 cd
$(GO_PKG_BUILD_DIR
) ; \
226 export GOPATH
=$(GO_PKG_BUILD_DIR
) \
227 GOCACHE
=$(GO_PKG_CACHE_DIR
) \
228 GOTMPDIR
=$(GO_PKG_TMP_DIR
) \
229 GOROOT_FINAL
=$(GO_TARGET_ROOT
) \
232 $(call GoPackage
/Environment
) ; \
234 echo
"Finding targets" ; \
235 targets
=$$$$(go list
$(GO_PKG_BUILD_PKG
)) ; \
236 for pattern in
$(GO_PKG_EXCLUDES
); do \
237 targets
=$$$$(echo
"$$$$targets" | grep
-v
"$$$$pattern") ; \
241 if
[ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
242 echo
"Calling go generate" ; \
243 go generate
-v
$(1) $$$$targets ; \
247 if
[ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
248 echo
"Building targets" ; \
250 arm
) installsuffix
="-installsuffix v$(GO_ARM)" ;; \
251 mips|mipsle
) installsuffix
="-installsuffix $(GO_MIPS)" ;; \
252 mips64|mips64le
) installsuffix
="-installsuffix $(GO_MIPS64)" ;; \
254 trimpath
="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
255 ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
258 -gcflags
"$$$$trimpath" \
259 -asmflags
"$$$$trimpath" \
260 -ldflags "$$$$ldflags" \
267 if
[ "$$$$retval" -eq
0 ] && [ -z
"$(call GoPackage/has_binaries)" ]; then \
268 echo
"No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
272 echo
"Cleaning module download cache (golang/go#27455)" ; \
273 go
clean -modcache
; \
280 define GoPackage
/Build
/InstallDev
281 $(call GoPackage
/Package
/Install
/Src
,$(1))
284 define GoPackage
/Package
/Install
/Bin
285 if
[ -n
"$(call GoPackage/has_binaries)" ]; then \
286 $(INSTALL_DIR
) $(1)/usr
/bin
; \
287 $(INSTALL_BIN
) $(GO_PKG_BUILD_BIN_DIR
)/* $(1)/usr
/bin
/ ; \
291 define GoPackage
/Package
/Install
/Src
292 dir=$$$$(dirname
$(GO_PKG
)) ; \
293 $(INSTALL_DIR
) $(1)$(GO_PKG_PATH
)/src
/$$$$dir ; \
294 $(CP
) $(GO_PKG_BUILD_DIR
)/src
/$(GO_PKG
) $(1)$(GO_PKG_PATH
)/src
/$$$$dir/
297 define GoPackage
/Package
/Install
298 $(call GoPackage
/Package
/Install
/Bin
,$(1))
299 $(call GoPackage
/Package
/Install
/Src
,$(1))
304 Build
/Configure
=$(call GoPackage
/Build
/Configure
)
305 Build
/Compile
=$(call GoPackage
/Build
/Compile
)
306 Build
/InstallDev
=$(call GoPackage
/Build
/InstallDev
,$(1))
310 ifndef Package
/$(1)/install
311 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
,$$(1))
316 ifndef Package
/$(1)/install
317 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
/Bin
,$$(1))
322 ifndef Package
/$(1)/install
323 Package
/$(1)/install=$$(call GoPackage
/Package
/Install
/Src
,$$(1))