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.

354 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?=$(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 \
  109. $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS)_$(GO_ARCH))
  110. GO_PKG_BUILD_DEPENDS_SRC:=$(STAGING_DIR)$(GO_PKG_PATH)/src
  111. # sstrip causes corrupted section header size
  112. ifneq ($(CONFIG_USE_SSTRIP),)
  113. ifneq ($(CONFIG_DEBUG),)
  114. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  115. else
  116. GO_PKG_STRIP_ARGS:=--strip-all
  117. endif
  118. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  119. RSTRIP= \
  120. export CROSS="$(TARGET_CROSS)" \
  121. $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
  122. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  123. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  124. NM="$(TARGET_CROSS)nm" \
  125. STRIP="$(STRIP)" \
  126. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  127. PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
  128. $(SCRIPT_DIR)/rstrip.sh
  129. endif
  130. define GoPackage/GoSubMenu
  131. SUBMENU:=Go
  132. SECTION:=lang
  133. CATEGORY:=Languages
  134. endef
  135. define GoPackage/Environment
  136. GOOS=$(GO_OS) \
  137. GOARCH=$(GO_ARCH) \
  138. GO386=$(GO_386) \
  139. GOARM=$(GO_ARM) \
  140. GOMIPS=$(GO_MIPS) \
  141. GOMIPS64=$(GO_MIPS64) \
  142. CGO_ENABLED=1 \
  143. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  144. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  145. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
  146. endef
  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 [ "$(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/$(GO_PKG)" ; \
  177. for file in $$$$files; do \
  178. echo $$$$file ; \
  179. dest=$(GO_PKG_BUILD_DIR)/src/$(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/$(GO_PKG)) \
  201. echo "$(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 [ "$(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 [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  245. echo "Calling go generate" ; \
  246. go generate -v $(1) $$$$targets ; \
  247. echo ; \
  248. fi ; \
  249. \
  250. if [ "$(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. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  258. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  259. pkg_gcflags="$(GO_PKG_GCFLAGS)" ; \
  260. pkg_ldflags="$(GO_PKG_LDFLAGS)" ; \
  261. for def in $(GO_PKG_LDFLAGS_X); do \
  262. pkg_ldflags="$$$$pkg_ldflags -X $$$$def" ; \
  263. done ; \
  264. go install \
  265. $$$${installsuffix:+-installsuffix $$$$installsuffix} \
  266. -gcflags "$$$$trimpath" \
  267. -asmflags "$$$$trimpath" \
  268. -ldflags "$$$$ldflags" \
  269. -v \
  270. $$$${pkg_gcflags:+-gcflags "$$$$pkg_gcflags"} \
  271. $$$${pkg_ldflags:+-ldflags "$$$$pkg_ldflags"} \
  272. $(1) \
  273. $$$$targets ; \
  274. retval=$$$$? ; \
  275. echo ; \
  276. \
  277. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  278. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  279. echo ; \
  280. fi ; \
  281. \
  282. echo "Cleaning module download cache (golang/go#27455)" ; \
  283. go clean -modcache ; \
  284. echo ; \
  285. fi ; \
  286. exit $$$$retval ; \
  287. )
  288. endef
  289. define GoPackage/Build/InstallDev
  290. $(call GoPackage/Package/Install/Src,$(1))
  291. endef
  292. define GoPackage/Package/Install/Bin
  293. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  294. $(INSTALL_DIR) $(1)/usr/bin ; \
  295. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  296. fi
  297. endef
  298. define GoPackage/Package/Install/Src
  299. dir=$$$$(dirname $(GO_PKG)) ; \
  300. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  301. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  302. endef
  303. define GoPackage/Package/Install
  304. $(call GoPackage/Package/Install/Bin,$(1))
  305. $(call GoPackage/Package/Install/Src,$(1))
  306. endef
  307. ifneq ($(GO_PKG),)
  308. Build/Configure=$(call GoPackage/Build/Configure)
  309. Build/Compile=$(call GoPackage/Build/Compile)
  310. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  311. endif
  312. define GoPackage
  313. ifndef Package/$(1)/install
  314. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  315. endif
  316. endef
  317. define GoBinPackage
  318. ifndef Package/$(1)/install
  319. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  320. endif
  321. endef
  322. define GoSrcPackage
  323. ifndef Package/$(1)/install
  324. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  325. endif
  326. endef