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.

325 lines
9.2 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. # Credit for this package build process (GoPackage/Build/Configure and
  76. # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
  77. # https://salsa.debian.org/go-team/packages/dh-golang
  78. # for building packages, not user code
  79. GO_PKG_PATH:=/usr/share/gocode
  80. GO_PKG_BUILD_PKG?=$(GO_PKG)/...
  81. GO_PKG_WORK_DIR_NAME:=.go_work
  82. GO_PKG_WORK_DIR:=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)
  83. GO_PKG_BUILD_DIR:=$(GO_PKG_WORK_DIR)/build
  84. GO_PKG_CACHE_DIR:=$(GO_PKG_WORK_DIR)/cache
  85. GO_PKG_TMP_DIR:=$(GO_PKG_WORK_DIR)/tmp
  86. GO_PKG_BUILD_BIN_DIR:=$(GO_PKG_BUILD_DIR)/bin$(if \
  87. $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS)_$(GO_ARCH))
  88. GO_PKG_BUILD_DEPENDS_SRC:=$(STAGING_DIR)$(GO_PKG_PATH)/src
  89. # sstrip causes corrupted section header size
  90. ifneq ($(CONFIG_USE_SSTRIP),)
  91. ifneq ($(CONFIG_DEBUG),)
  92. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  93. else
  94. GO_PKG_STRIP_ARGS:=--strip-all
  95. endif
  96. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  97. RSTRIP= \
  98. export CROSS="$(TARGET_CROSS)" \
  99. $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
  100. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  101. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  102. NM="$(TARGET_CROSS)nm" \
  103. STRIP="$(STRIP)" \
  104. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  105. PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
  106. $(SCRIPT_DIR)/rstrip.sh
  107. endif
  108. define GoPackage/GoSubMenu
  109. SUBMENU:=Go
  110. SECTION:=lang
  111. CATEGORY:=Languages
  112. endef
  113. define GoPackage/Environment
  114. GOOS=$(GO_OS) \
  115. GOARCH=$(GO_ARCH) \
  116. GO386=$(GO_386) \
  117. GOARM=$(GO_ARM) \
  118. GOMIPS=$(GO_MIPS) \
  119. GOMIPS64=$(GO_MIPS64) \
  120. CGO_ENABLED=1 \
  121. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  122. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  123. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
  124. endef
  125. # false if directory does not exist
  126. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  127. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  128. define GoPackage/Build/Configure
  129. ( \
  130. cd $(PKG_BUILD_DIR) ; \
  131. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src \
  132. $(GO_PKG_CACHE_DIR) $(GO_PKG_TMP_DIR) ; \
  133. \
  134. files=$$$$($(FIND) ./ \
  135. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  136. \! -type d -print | \
  137. sed 's|^\./||') ; \
  138. \
  139. if [ "$(GO_PKG_INSTALL_ALL)" != 1 ]; then \
  140. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
  141. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  142. gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
  143. \
  144. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  145. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  146. done ; \
  147. \
  148. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$gomod"; echo "$$$$extra") ; \
  149. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  150. fi ; \
  151. \
  152. IFS=$$$$'\n' ; \
  153. \
  154. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)" ; \
  155. for file in $$$$files; do \
  156. echo $$$$file ; \
  157. dest=$(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/$$$$file ; \
  158. mkdir -p $$$$(dirname $$$$dest) ; \
  159. $(CP) $$$$file $$$$dest ; \
  160. done ; \
  161. echo ; \
  162. \
  163. link_contents() { \
  164. local src=$$$$1 ; \
  165. local dest=$$$$2 ; \
  166. local dirs dir base ; \
  167. \
  168. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  169. echo "$$$$src is already a Go library" ; \
  170. return 1 ; \
  171. fi ; \
  172. \
  173. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  174. for dir in $$$$dirs; do \
  175. base=$$$$(basename $$$$dir) ; \
  176. if [ -d $$$$dest/$$$$base ]; then \
  177. case $$$$dir in \
  178. *$(GO_PKG_PATH)/src/$(GO_PKG)) \
  179. echo "$(GO_PKG) is already installed. Please check for circular dependencies." ;; \
  180. *) \
  181. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  182. esac ; \
  183. else \
  184. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  185. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  186. fi ; \
  187. done ; \
  188. } ; \
  189. \
  190. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  191. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  192. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  193. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  194. else \
  195. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  196. fi ; \
  197. else \
  198. echo "Not building binaries, skipping symlinks" ; \
  199. fi ; \
  200. echo ; \
  201. )
  202. endef
  203. # $(1) additional arguments for go command line (optional)
  204. define GoPackage/Build/Compile
  205. ( \
  206. cd $(GO_PKG_BUILD_DIR) ; \
  207. export GOPATH=$(GO_PKG_BUILD_DIR) \
  208. GOCACHE=$(GO_PKG_CACHE_DIR) \
  209. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  210. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  211. CC=$(TARGET_CC) \
  212. CXX=$(TARGET_CXX) \
  213. $(call GoPackage/Environment) ; \
  214. \
  215. echo "Finding targets" ; \
  216. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  217. for pattern in $(GO_PKG_EXCLUDES); do \
  218. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  219. done ; \
  220. echo ; \
  221. \
  222. if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  223. echo "Calling go generate" ; \
  224. go generate -v $(1) $$$$targets ; \
  225. echo ; \
  226. fi ; \
  227. \
  228. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  229. echo "Building targets" ; \
  230. case $(GO_ARCH) in \
  231. arm) installsuffix="-installsuffix v$(GO_ARM)" ;; \
  232. mips|mipsle) installsuffix="-installsuffix $(GO_MIPS)" ;; \
  233. mips64|mips64le) installsuffix="-installsuffix $(GO_MIPS64)" ;; \
  234. esac ; \
  235. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  236. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  237. go install \
  238. $$$$installsuffix \
  239. -gcflags "$$$$trimpath" \
  240. -asmflags "$$$$trimpath" \
  241. -ldflags "$$$$ldflags" \
  242. -v \
  243. $(1) \
  244. $$$$targets ; \
  245. retval=$$$$? ; \
  246. echo ; \
  247. \
  248. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  249. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  250. echo ; \
  251. fi ; \
  252. \
  253. echo "Cleaning module download cache (golang/go#27455)" ; \
  254. go clean -modcache ; \
  255. echo ; \
  256. fi ; \
  257. exit $$$$retval ; \
  258. )
  259. endef
  260. define GoPackage/Build/InstallDev
  261. $(call GoPackage/Package/Install/Src,$(1))
  262. endef
  263. define GoPackage/Package/Install/Bin
  264. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  265. $(INSTALL_DIR) $(1)/usr/bin ; \
  266. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  267. fi
  268. endef
  269. define GoPackage/Package/Install/Src
  270. dir=$$$$(dirname $(GO_PKG)) ; \
  271. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  272. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  273. endef
  274. define GoPackage/Package/Install
  275. $(call GoPackage/Package/Install/Bin,$(1))
  276. $(call GoPackage/Package/Install/Src,$(1))
  277. endef
  278. ifneq ($(GO_PKG),)
  279. Build/Configure=$(call GoPackage/Build/Configure)
  280. Build/Compile=$(call GoPackage/Build/Compile)
  281. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  282. endif
  283. define GoPackage
  284. ifndef Package/$(1)/install
  285. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  286. endif
  287. endef
  288. define GoBinPackage
  289. ifndef Package/$(1)/install
  290. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  291. endif
  292. endef
  293. define GoSrcPackage
  294. ifndef Package/$(1)/install
  295. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  296. endif
  297. endef