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.

374 lines
11 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. ifdef CONFIG_PKG_ASLR_PIE_ALL
  111. ifeq ($(strip $(PKG_ASLR_PIE)),1)
  112. ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
  113. GO_PKG_ENABLE_PIE:=1
  114. else
  115. $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
  116. endif
  117. endif
  118. endif
  119. ifdef CONFIG_PKG_ASLR_PIE_REGULAR
  120. ifeq ($(strip $(PKG_ASLR_PIE_REGULAR)),1)
  121. ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
  122. GO_PKG_ENABLE_PIE:=1
  123. else
  124. $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
  125. endif
  126. endif
  127. endif
  128. # sstrip causes corrupted section header size
  129. ifneq ($(CONFIG_USE_SSTRIP),)
  130. ifneq ($(CONFIG_DEBUG),)
  131. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  132. else
  133. GO_PKG_STRIP_ARGS:=--strip-all
  134. endif
  135. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  136. RSTRIP= \
  137. export CROSS="$(TARGET_CROSS)" \
  138. $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
  139. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  140. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  141. NM="$(TARGET_CROSS)nm" \
  142. STRIP="$(STRIP)" \
  143. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  144. PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
  145. $(SCRIPT_DIR)/rstrip.sh
  146. endif
  147. define GoPackage/GoSubMenu
  148. SUBMENU:=Go
  149. SECTION:=lang
  150. CATEGORY:=Languages
  151. endef
  152. define GoPackage/Environment/Default
  153. GOOS=$(GO_OS) \
  154. GOARCH=$(GO_ARCH) \
  155. GO386=$(GO_386) \
  156. GOARM=$(GO_ARM) \
  157. GOMIPS=$(GO_MIPS) \
  158. GOMIPS64=$(GO_MIPS64) \
  159. CGO_ENABLED=1 \
  160. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  161. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  162. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
  163. endef
  164. GoPackage/Environment=$(call GoPackage/Environment/Default)
  165. # false if directory does not exist
  166. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  167. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  168. define GoPackage/Build/Configure
  169. ( \
  170. cd $(PKG_BUILD_DIR) ; \
  171. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src \
  172. $(GO_PKG_CACHE_DIR) $(GO_PKG_TMP_DIR) ; \
  173. \
  174. files=$$$$($(FIND) ./ \
  175. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  176. \! -type d -print | \
  177. sed 's|^\./||') ; \
  178. \
  179. if [ "$(strip $(GO_PKG_INSTALL_ALL))" != 1 ]; then \
  180. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
  181. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  182. gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
  183. \
  184. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  185. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  186. done ; \
  187. \
  188. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$gomod"; echo "$$$$extra") ; \
  189. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  190. fi ; \
  191. \
  192. IFS=$$$$'\n' ; \
  193. \
  194. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))" ; \
  195. for file in $$$$files; do \
  196. echo $$$$file ; \
  197. dest=$(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))/$$$$file ; \
  198. mkdir -p $$$$(dirname $$$$dest) ; \
  199. $(CP) $$$$file $$$$dest ; \
  200. done ; \
  201. echo ; \
  202. \
  203. link_contents() { \
  204. local src=$$$$1 ; \
  205. local dest=$$$$2 ; \
  206. local dirs dir base ; \
  207. \
  208. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  209. echo "$$$$src is already a Go library" ; \
  210. return 1 ; \
  211. fi ; \
  212. \
  213. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  214. for dir in $$$$dirs; do \
  215. base=$$$$(basename $$$$dir) ; \
  216. if [ -d $$$$dest/$$$$base ]; then \
  217. case $$$$dir in \
  218. *$(GO_PKG_PATH)/src/$(strip $(GO_PKG))) \
  219. echo "$(strip $(GO_PKG)) is already installed. Please check for circular dependencies." ;; \
  220. *) \
  221. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  222. esac ; \
  223. else \
  224. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  225. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  226. fi ; \
  227. done ; \
  228. } ; \
  229. \
  230. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  231. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  232. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  233. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  234. else \
  235. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  236. fi ; \
  237. else \
  238. echo "Not building binaries, skipping symlinks" ; \
  239. fi ; \
  240. echo ; \
  241. )
  242. endef
  243. # $(1) additional arguments for go command line (optional)
  244. define GoPackage/Build/Compile
  245. ( \
  246. cd $(GO_PKG_BUILD_DIR) ; \
  247. export GOPATH=$(GO_PKG_BUILD_DIR) \
  248. GOCACHE=$(GO_PKG_CACHE_DIR) \
  249. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  250. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  251. CC=$(TARGET_CC) \
  252. CXX=$(TARGET_CXX) \
  253. $(call GoPackage/Environment) ; \
  254. \
  255. echo "Finding targets" ; \
  256. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  257. for pattern in $(GO_PKG_EXCLUDES); do \
  258. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  259. done ; \
  260. echo ; \
  261. \
  262. if [ "$(strip $(GO_PKG_GO_GENERATE))" = 1 ]; then \
  263. echo "Calling go generate" ; \
  264. go generate -v $(1) $$$$targets ; \
  265. echo ; \
  266. fi ; \
  267. \
  268. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  269. echo "Building targets" ; \
  270. case $(GO_ARCH) in \
  271. arm) installsuffix="v$(GO_ARM)" ;; \
  272. mips|mipsle) installsuffix="$(GO_MIPS)" ;; \
  273. mips64|mips64le) installsuffix="$(GO_MIPS64)" ;; \
  274. esac ; \
  275. ldflags="-linkmode external -extldflags '$(TARGET_LDFLAGS:-z%=-Wl,-z,%)'" ; \
  276. pkg_gcflags="$(strip $(GO_PKG_GCFLAGS))" ; \
  277. pkg_ldflags="$(strip $(GO_PKG_LDFLAGS))" ; \
  278. for def in $(GO_PKG_LDFLAGS_X); do \
  279. pkg_ldflags="$$$$pkg_ldflags -X $$$$def" ; \
  280. done ; \
  281. go install \
  282. $(if $(GO_PKG_ENABLE_PIE),-buildmode pie) \
  283. $$$${installsuffix:+-installsuffix $$$$installsuffix} \
  284. -trimpath \
  285. -ldflags "all=$$$$ldflags" \
  286. -v \
  287. $$$${pkg_gcflags:+-gcflags "$$$$pkg_gcflags"} \
  288. $$$${pkg_ldflags:+-ldflags "$$$$pkg_ldflags $$$$ldflags"} \
  289. $(1) \
  290. $$$$targets ; \
  291. retval=$$$$? ; \
  292. echo ; \
  293. \
  294. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  295. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  296. echo ; \
  297. fi ; \
  298. \
  299. echo "Cleaning module download cache (golang/go#27455)" ; \
  300. go clean -modcache ; \
  301. echo ; \
  302. fi ; \
  303. exit $$$$retval ; \
  304. )
  305. endef
  306. define GoPackage/Build/InstallDev
  307. $(call GoPackage/Package/Install/Src,$(1))
  308. endef
  309. define GoPackage/Package/Install/Bin
  310. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  311. $(INSTALL_DIR) $(1)/usr/bin ; \
  312. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  313. fi
  314. endef
  315. define GoPackage/Package/Install/Src
  316. dir=$$$$(dirname $(GO_PKG)) ; \
  317. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  318. $(CP) $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG)) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  319. endef
  320. define GoPackage/Package/Install
  321. $(call GoPackage/Package/Install/Bin,$(1))
  322. $(call GoPackage/Package/Install/Src,$(1))
  323. endef
  324. ifneq ($(strip $(GO_PKG)),)
  325. Build/Configure=$(call GoPackage/Build/Configure)
  326. Build/Compile=$(call GoPackage/Build/Compile)
  327. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  328. endif
  329. define GoPackage
  330. ifndef Package/$(1)/install
  331. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  332. endif
  333. endef
  334. define GoBinPackage
  335. ifndef Package/$(1)/install
  336. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  337. endif
  338. endef
  339. define GoSrcPackage
  340. ifndef Package/$(1)/install
  341. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  342. endif
  343. endef