You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
10 KiB

  1. #
  2. # Copyright (C) 2018 Jeffery To
  3. #
  4. # This is free software, licensed under the GNU General Public License v2.
  5. # See /LICENSE for more information.
  6. #
  7. ifeq ($(origin GO_INCLUDE_DIR),undefined)
  8. GO_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
  9. endif
  10. include $(GO_INCLUDE_DIR)/golang-values.mk
  11. # Variables (all optional, except GO_PKG) to be set in package
  12. # Makefiles:
  13. #
  14. # GO_PKG (required) - name of Go package
  15. #
  16. # Go name of the package.
  17. #
  18. # e.g. GO_PKG:=golang.org/x/text
  19. #
  20. #
  21. # GO_PKG_INSTALL_EXTRA - list of regular expressions, default empty
  22. #
  23. # Additional files/directories to install. By default, only these
  24. # files are installed:
  25. #
  26. # * Files with one of these extensions:
  27. # .go, .c, .cc, .cpp, .h, .hh, .hpp, .proto, .s
  28. #
  29. # * Files in any 'testdata' directory
  30. #
  31. # * go.mod and go.sum, in any directory
  32. #
  33. # e.g. GO_PKG_INSTALL_EXTRA:=example.toml marshal_test.toml
  34. #
  35. #
  36. # GO_PKG_INSTALL_ALL - boolean (0 or 1), default false
  37. #
  38. # If true, install all files regardless of extension or directory.
  39. #
  40. # e.g. GO_PKG_INSTALL_ALL:=1
  41. #
  42. #
  43. # GO_PKG_SOURCE_ONLY - boolean (0 or 1), default false
  44. #
  45. # If true, 'go install' will not be called. If the package does not
  46. # (or should not) build any binaries, then specifying this option will
  47. # save build time.
  48. #
  49. # e.g. GO_PKG_SOURCE_ONLY:=1
  50. #
  51. #
  52. # GO_PKG_BUILD_PKG - list of build targets, default GO_PKG/...
  53. #
  54. # Build targets for compiling this Go package, i.e. arguments passed
  55. # to 'go install'
  56. #
  57. # e.g. GO_PKG_BUILD_PKG:=github.com/debian/ratt/cmd/...
  58. #
  59. #
  60. # GO_PKG_EXCLUDES - list of regular expressions, default empty
  61. #
  62. # Patterns to exclude from the build targets expanded from
  63. # GO_PKG_BUILD_PKG.
  64. #
  65. # e.g. GO_PKG_EXCLUDES:=examples/
  66. #
  67. #
  68. # GO_PKG_GO_GENERATE - boolean (0 or 1), default false
  69. #
  70. # If true, 'go generate' will be called on all build targets (as
  71. # determined by GO_PKG_BUILD_PKG and GO_PKG_EXCLUDES). This is usually
  72. # not necessary.
  73. #
  74. # e.g. GO_PKG_GO_GENERATE:=1
  75. #
  76. #
  77. # GO_PKG_GCFLAGS - list of arguments, default empty
  78. #
  79. # Additional go tool compile arguments to use when building targets.
  80. #
  81. # e.g. GO_PKG_GCFLAGS:=-N -l
  82. #
  83. #
  84. # GO_PKG_LDFLAGS - list of arguments, default empty
  85. #
  86. # Additional go tool link arguments to use when building targets.
  87. #
  88. # e.g. GO_PKG_LDFLAGS:=-s -w
  89. #
  90. #
  91. # GO_PKG_LDFLAGS_X - list of string variable definitions, default empty
  92. #
  93. # Each definition will be passed as the parameter to the -X go tool
  94. # link argument, i.e. -ldflags "-X importpath.name=value"
  95. #
  96. # e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH)
  97. # Credit for this package build process (GoPackage/Build/Configure and
  98. # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
  99. # https://salsa.debian.org/go-team/packages/dh-golang
  100. # for building packages, not user code
  101. GO_PKG_PATH:=/usr/share/gocode
  102. GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/...
  103. GO_PKG_WORK_DIR_NAME:=.go_work
  104. GO_PKG_WORK_DIR:=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)
  105. GO_PKG_BUILD_DIR:=$(GO_PKG_WORK_DIR)/build
  106. GO_PKG_CACHE_DIR:=$(GO_PKG_WORK_DIR)/cache
  107. GO_PKG_TMP_DIR:=$(GO_PKG_WORK_DIR)/tmp
  108. GO_PKG_BUILD_BIN_DIR:=$(GO_PKG_BUILD_DIR)/bin$(if $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS_ARCH))
  109. GO_PKG_BUILD_DEPENDS_SRC:=$(STAGING_DIR)$(GO_PKG_PATH)/src
  110. # sstrip causes corrupted section header size
  111. ifneq ($(CONFIG_USE_SSTRIP),)
  112. ifneq ($(CONFIG_DEBUG),)
  113. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  114. else
  115. GO_PKG_STRIP_ARGS:=--strip-all
  116. endif
  117. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  118. RSTRIP= \
  119. export CROSS="$(TARGET_CROSS)" \
  120. $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
  121. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  122. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  123. NM="$(TARGET_CROSS)nm" \
  124. STRIP="$(STRIP)" \
  125. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  126. PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
  127. $(SCRIPT_DIR)/rstrip.sh
  128. endif
  129. define GoPackage/GoSubMenu
  130. SUBMENU:=Go
  131. SECTION:=lang
  132. CATEGORY:=Languages
  133. endef
  134. define GoPackage/Environment/Default
  135. GOOS=$(GO_OS) \
  136. GOARCH=$(GO_ARCH) \
  137. GO386=$(GO_386) \
  138. GOARM=$(GO_ARM) \
  139. GOMIPS=$(GO_MIPS) \
  140. GOMIPS64=$(GO_MIPS64) \
  141. CGO_ENABLED=1 \
  142. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  143. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  144. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
  145. endef
  146. GoPackage/Environment=$(call GoPackage/Environment/Default)
  147. # false if directory does not exist
  148. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  149. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  150. define GoPackage/Build/Configure
  151. ( \
  152. cd $(PKG_BUILD_DIR) ; \
  153. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src \
  154. $(GO_PKG_CACHE_DIR) $(GO_PKG_TMP_DIR) ; \
  155. \
  156. files=$$$$($(FIND) ./ \
  157. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  158. \! -type d -print | \
  159. sed 's|^\./||') ; \
  160. \
  161. if [ "$(strip $(GO_PKG_INSTALL_ALL))" != 1 ]; then \
  162. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
  163. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  164. gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
  165. \
  166. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  167. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  168. done ; \
  169. \
  170. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$gomod"; echo "$$$$extra") ; \
  171. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  172. fi ; \
  173. \
  174. IFS=$$$$'\n' ; \
  175. \
  176. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))" ; \
  177. for file in $$$$files; do \
  178. echo $$$$file ; \
  179. dest=$(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))/$$$$file ; \
  180. mkdir -p $$$$(dirname $$$$dest) ; \
  181. $(CP) $$$$file $$$$dest ; \
  182. done ; \
  183. echo ; \
  184. \
  185. link_contents() { \
  186. local src=$$$$1 ; \
  187. local dest=$$$$2 ; \
  188. local dirs dir base ; \
  189. \
  190. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  191. echo "$$$$src is already a Go library" ; \
  192. return 1 ; \
  193. fi ; \
  194. \
  195. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  196. for dir in $$$$dirs; do \
  197. base=$$$$(basename $$$$dir) ; \
  198. if [ -d $$$$dest/$$$$base ]; then \
  199. case $$$$dir in \
  200. *$(GO_PKG_PATH)/src/$(strip $(GO_PKG))) \
  201. echo "$(strip $(GO_PKG)) is already installed. Please check for circular dependencies." ;; \
  202. *) \
  203. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  204. esac ; \
  205. else \
  206. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  207. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  208. fi ; \
  209. done ; \
  210. } ; \
  211. \
  212. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  213. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  214. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  215. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  216. else \
  217. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  218. fi ; \
  219. else \
  220. echo "Not building binaries, skipping symlinks" ; \
  221. fi ; \
  222. echo ; \
  223. )
  224. endef
  225. # $(1) additional arguments for go command line (optional)
  226. define GoPackage/Build/Compile
  227. ( \
  228. cd $(GO_PKG_BUILD_DIR) ; \
  229. export GOPATH=$(GO_PKG_BUILD_DIR) \
  230. GOCACHE=$(GO_PKG_CACHE_DIR) \
  231. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  232. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  233. CC=$(TARGET_CC) \
  234. CXX=$(TARGET_CXX) \
  235. $(call GoPackage/Environment) ; \
  236. \
  237. echo "Finding targets" ; \
  238. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  239. for pattern in $(GO_PKG_EXCLUDES); do \
  240. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  241. done ; \
  242. echo ; \
  243. \
  244. if [ "$(strip $(GO_PKG_GO_GENERATE))" = 1 ]; then \
  245. echo "Calling go generate" ; \
  246. go generate -v $(1) $$$$targets ; \
  247. echo ; \
  248. fi ; \
  249. \
  250. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  251. echo "Building targets" ; \
  252. case $(GO_ARCH) in \
  253. arm) installsuffix="v$(GO_ARM)" ;; \
  254. mips|mipsle) installsuffix="$(GO_MIPS)" ;; \
  255. mips64|mips64le) installsuffix="$(GO_MIPS64)" ;; \
  256. esac ; \
  257. ldflags="-linkmode external -extldflags '$(TARGET_LDFLAGS:-z%=-Wl,-z,%)'" ; \
  258. pkg_gcflags="$(strip $(GO_PKG_GCFLAGS))" ; \
  259. pkg_ldflags="$(strip $(GO_PKG_LDFLAGS))" ; \
  260. for def in $(GO_PKG_LDFLAGS_X); do \
  261. pkg_ldflags="$$$$pkg_ldflags -X $$$$def" ; \
  262. done ; \
  263. go install \
  264. $$$${installsuffix:+-installsuffix $$$$installsuffix} \
  265. -trimpath \
  266. -ldflags "all=$$$$ldflags" \
  267. -v \
  268. $$$${pkg_gcflags:+-gcflags "$$$$pkg_gcflags"} \
  269. $$$${pkg_ldflags:+-ldflags "$$$$pkg_ldflags $$$$ldflags"} \
  270. $(1) \
  271. $$$$targets ; \
  272. retval=$$$$? ; \
  273. echo ; \
  274. \
  275. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  276. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  277. echo ; \
  278. fi ; \
  279. \
  280. echo "Cleaning module download cache (golang/go#27455)" ; \
  281. go clean -modcache ; \
  282. echo ; \
  283. fi ; \
  284. exit $$$$retval ; \
  285. )
  286. endef
  287. define GoPackage/Build/InstallDev
  288. $(call GoPackage/Package/Install/Src,$(1))
  289. endef
  290. define GoPackage/Package/Install/Bin
  291. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  292. $(INSTALL_DIR) $(1)/usr/bin ; \
  293. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  294. fi
  295. endef
  296. define GoPackage/Package/Install/Src
  297. dir=$$$$(dirname $(GO_PKG)) ; \
  298. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  299. $(CP) $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG)) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  300. endef
  301. define GoPackage/Package/Install
  302. $(call GoPackage/Package/Install/Bin,$(1))
  303. $(call GoPackage/Package/Install/Src,$(1))
  304. endef
  305. ifneq ($(strip $(GO_PKG)),)
  306. Build/Configure=$(call GoPackage/Build/Configure)
  307. Build/Compile=$(call GoPackage/Build/Compile)
  308. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  309. endif
  310. define GoPackage
  311. ifndef Package/$(1)/install
  312. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  313. endif
  314. endef
  315. define GoBinPackage
  316. ifndef Package/$(1)/install
  317. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  318. endif
  319. endef
  320. define GoSrcPackage
  321. ifndef Package/$(1)/install
  322. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  323. endif
  324. endef