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/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. ldflags="-linkmode external -extldflags '$(TARGET_LDFLAGS:-z%=-Wl,-z,%)'" ; \
  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. -trimpath \
  267. -ldflags "all=$$$$ldflags" \
  268. -v \
  269. $$$${pkg_gcflags:+-gcflags "$$$$pkg_gcflags"} \
  270. $$$${pkg_ldflags:+-ldflags "$$$$pkg_ldflags $$$$ldflags"} \
  271. $(1) \
  272. $$$$targets ; \
  273. retval=$$$$? ; \
  274. echo ; \
  275. \
  276. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  277. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  278. echo ; \
  279. fi ; \
  280. \
  281. echo "Cleaning module download cache (golang/go#27455)" ; \
  282. go clean -modcache ; \
  283. echo ; \
  284. fi ; \
  285. exit $$$$retval ; \
  286. )
  287. endef
  288. define GoPackage/Build/InstallDev
  289. $(call GoPackage/Package/Install/Src,$(1))
  290. endef
  291. define GoPackage/Package/Install/Bin
  292. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  293. $(INSTALL_DIR) $(1)/usr/bin ; \
  294. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  295. fi
  296. endef
  297. define GoPackage/Package/Install/Src
  298. dir=$$$$(dirname $(GO_PKG)) ; \
  299. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  300. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  301. endef
  302. define GoPackage/Package/Install
  303. $(call GoPackage/Package/Install/Bin,$(1))
  304. $(call GoPackage/Package/Install/Src,$(1))
  305. endef
  306. ifneq ($(GO_PKG),)
  307. Build/Configure=$(call GoPackage/Build/Configure)
  308. Build/Compile=$(call GoPackage/Build/Compile)
  309. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  310. endif
  311. define GoPackage
  312. ifndef Package/$(1)/install
  313. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  314. endif
  315. endef
  316. define GoBinPackage
  317. ifndef Package/$(1)/install
  318. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  319. endif
  320. endef
  321. define GoSrcPackage
  322. ifndef Package/$(1)/install
  323. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  324. endif
  325. endef