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.

356 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/Default
  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. GoPackage/Environment=$(call GoPackage/Environment/Default,)
  148. # false if directory does not exist
  149. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  150. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  151. define GoPackage/Build/Configure
  152. ( \
  153. cd $(PKG_BUILD_DIR) ; \
  154. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src \
  155. $(GO_PKG_CACHE_DIR) $(GO_PKG_TMP_DIR) ; \
  156. \
  157. files=$$$$($(FIND) ./ \
  158. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  159. \! -type d -print | \
  160. sed 's|^\./||') ; \
  161. \
  162. if [ "$(GO_PKG_INSTALL_ALL)" != 1 ]; then \
  163. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
  164. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  165. gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
  166. \
  167. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  168. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  169. done ; \
  170. \
  171. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$gomod"; echo "$$$$extra") ; \
  172. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  173. fi ; \
  174. \
  175. IFS=$$$$'\n' ; \
  176. \
  177. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)" ; \
  178. for file in $$$$files; do \
  179. echo $$$$file ; \
  180. dest=$(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/$$$$file ; \
  181. mkdir -p $$$$(dirname $$$$dest) ; \
  182. $(CP) $$$$file $$$$dest ; \
  183. done ; \
  184. echo ; \
  185. \
  186. link_contents() { \
  187. local src=$$$$1 ; \
  188. local dest=$$$$2 ; \
  189. local dirs dir base ; \
  190. \
  191. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  192. echo "$$$$src is already a Go library" ; \
  193. return 1 ; \
  194. fi ; \
  195. \
  196. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  197. for dir in $$$$dirs; do \
  198. base=$$$$(basename $$$$dir) ; \
  199. if [ -d $$$$dest/$$$$base ]; then \
  200. case $$$$dir in \
  201. *$(GO_PKG_PATH)/src/$(GO_PKG)) \
  202. echo "$(GO_PKG) is already installed. Please check for circular dependencies." ;; \
  203. *) \
  204. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  205. esac ; \
  206. else \
  207. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  208. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  209. fi ; \
  210. done ; \
  211. } ; \
  212. \
  213. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  214. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  215. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  216. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  217. else \
  218. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  219. fi ; \
  220. else \
  221. echo "Not building binaries, skipping symlinks" ; \
  222. fi ; \
  223. echo ; \
  224. )
  225. endef
  226. # $(1) additional arguments for go command line (optional)
  227. define GoPackage/Build/Compile
  228. ( \
  229. cd $(GO_PKG_BUILD_DIR) ; \
  230. export GOPATH=$(GO_PKG_BUILD_DIR) \
  231. GOCACHE=$(GO_PKG_CACHE_DIR) \
  232. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  233. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  234. CC=$(TARGET_CC) \
  235. CXX=$(TARGET_CXX) \
  236. $(call GoPackage/Environment) ; \
  237. \
  238. echo "Finding targets" ; \
  239. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  240. for pattern in $(GO_PKG_EXCLUDES); do \
  241. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  242. done ; \
  243. echo ; \
  244. \
  245. if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  246. echo "Calling go generate" ; \
  247. go generate -v $(1) $$$$targets ; \
  248. echo ; \
  249. fi ; \
  250. \
  251. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  252. echo "Building targets" ; \
  253. case $(GO_ARCH) in \
  254. arm) installsuffix="v$(GO_ARM)" ;; \
  255. mips|mipsle) installsuffix="$(GO_MIPS)" ;; \
  256. mips64|mips64le) installsuffix="$(GO_MIPS64)" ;; \
  257. esac ; \
  258. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  259. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  260. pkg_gcflags="$(GO_PKG_GCFLAGS)" ; \
  261. pkg_ldflags="$(GO_PKG_LDFLAGS)" ; \
  262. for def in $(GO_PKG_LDFLAGS_X); do \
  263. pkg_ldflags="$$$$pkg_ldflags -X $$$$def" ; \
  264. done ; \
  265. go install \
  266. $$$${installsuffix:+-installsuffix $$$$installsuffix} \
  267. -gcflags "$$$$trimpath" \
  268. -asmflags "$$$$trimpath" \
  269. -ldflags "$$$$ldflags" \
  270. -v \
  271. $$$${pkg_gcflags:+-gcflags "$$$$pkg_gcflags"} \
  272. $$$${pkg_ldflags:+-ldflags "$$$$pkg_ldflags"} \
  273. $(1) \
  274. $$$$targets ; \
  275. retval=$$$$? ; \
  276. echo ; \
  277. \
  278. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  279. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  280. echo ; \
  281. fi ; \
  282. \
  283. echo "Cleaning module download cache (golang/go#27455)" ; \
  284. go clean -modcache ; \
  285. echo ; \
  286. fi ; \
  287. exit $$$$retval ; \
  288. )
  289. endef
  290. define GoPackage/Build/InstallDev
  291. $(call GoPackage/Package/Install/Src,$(1))
  292. endef
  293. define GoPackage/Package/Install/Bin
  294. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  295. $(INSTALL_DIR) $(1)/usr/bin ; \
  296. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  297. fi
  298. endef
  299. define GoPackage/Package/Install/Src
  300. dir=$$$$(dirname $(GO_PKG)) ; \
  301. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  302. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  303. endef
  304. define GoPackage/Package/Install
  305. $(call GoPackage/Package/Install/Bin,$(1))
  306. $(call GoPackage/Package/Install/Src,$(1))
  307. endef
  308. ifneq ($(GO_PKG),)
  309. Build/Configure=$(call GoPackage/Build/Configure)
  310. Build/Compile=$(call GoPackage/Build/Compile)
  311. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  312. endif
  313. define GoPackage
  314. ifndef Package/$(1)/install
  315. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  316. endif
  317. endef
  318. define GoBinPackage
  319. ifndef Package/$(1)/install
  320. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  321. endif
  322. endef
  323. define GoSrcPackage
  324. ifndef Package/$(1)/install
  325. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  326. endif
  327. endef