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.

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