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.

320 lines
9.0 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, .h, .hh, .proto, .s
  28. #
  29. # * Files in any 'testdata' directory
  30. #
  31. # e.g. GO_PKG_INSTALL_EXTRA:=example.toml marshal_test.toml
  32. #
  33. #
  34. # GO_PKG_INSTALL_ALL - boolean (0 or 1), default false
  35. #
  36. # If true, install all files regardless of extension or directory.
  37. #
  38. # e.g. GO_PKG_INSTALL_ALL:=1
  39. #
  40. #
  41. # GO_PKG_SOURCE_ONLY - boolean (0 or 1), default false
  42. #
  43. # If true, 'go install' will not be called. If the package does not
  44. # (or should not) build any binaries, then specifying this option will
  45. # save build time.
  46. #
  47. # e.g. GO_PKG_SOURCE_ONLY:=1
  48. #
  49. #
  50. # GO_PKG_BUILD_PKG - list of build targets, default GO_PKG/...
  51. #
  52. # Build targets for compiling this Go package, i.e. arguments passed
  53. # to 'go install'
  54. #
  55. # e.g. GO_PKG_BUILD_PKG:=github.com/debian/ratt/cmd/...
  56. #
  57. #
  58. # GO_PKG_EXCLUDES - list of regular expressions, default empty
  59. #
  60. # Patterns to exclude from the build targets expanded from
  61. # GO_PKG_BUILD_PKG.
  62. #
  63. # e.g. GO_PKG_EXCLUDES:=examples/
  64. #
  65. #
  66. # GO_PKG_GO_GENERATE - boolean (0 or 1), default false
  67. #
  68. # If true, 'go generate' will be called on all build targets (as
  69. # determined by GO_PKG_BUILD_PKG and GO_PKG_EXCLUDES). This is usually
  70. # not necessary.
  71. #
  72. # e.g. GO_PKG_GO_GENERATE:=1
  73. # Credit for this package build process (GoPackage/Build/Configure and
  74. # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
  75. # https://anonscm.debian.org/cgit/pkg-go/packages/dh-golang.git
  76. # for building packages, not user code
  77. GO_PKG_PATH:=/usr/share/gocode
  78. GO_PKG_BUILD_PKG?=$(GO_PKG)/...
  79. GO_PKG_WORK_DIR_NAME:=.go_work
  80. GO_PKG_WORK_DIR:=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)
  81. GO_PKG_BUILD_DIR:=$(GO_PKG_WORK_DIR)/build
  82. GO_PKG_CACHE_DIR:=$(GO_PKG_WORK_DIR)/cache
  83. GO_PKG_TMP_DIR:=$(GO_PKG_WORK_DIR)/tmp
  84. GO_PKG_BUILD_BIN_DIR:=$(GO_PKG_BUILD_DIR)/bin$(if \
  85. $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS)_$(GO_ARCH))
  86. GO_PKG_BUILD_DEPENDS_SRC:=$(STAGING_DIR)$(GO_PKG_PATH)/src
  87. # sstrip causes corrupted section header size
  88. ifneq ($(CONFIG_USE_SSTRIP),)
  89. ifneq ($(CONFIG_DEBUG),)
  90. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  91. else
  92. GO_PKG_STRIP_ARGS:=--strip-all
  93. endif
  94. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  95. RSTRIP= \
  96. export CROSS="$(TARGET_CROSS)" \
  97. $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
  98. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  99. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  100. NM="$(TARGET_CROSS)nm" \
  101. STRIP="$(STRIP)" \
  102. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  103. PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
  104. $(SCRIPT_DIR)/rstrip.sh
  105. endif
  106. define GoPackage/GoSubMenu
  107. SUBMENU:=Go
  108. SECTION:=lang
  109. CATEGORY:=Languages
  110. endef
  111. define GoPackage/Environment
  112. GOOS=$(GO_OS) \
  113. GOARCH=$(GO_ARCH) \
  114. GO386=$(GO_386) \
  115. GOARM=$(GO_ARM) \
  116. GOMIPS=$(GO_MIPS) \
  117. GOMIPS64=$(GO_MIPS64) \
  118. CGO_ENABLED=1 \
  119. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  120. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  121. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
  122. endef
  123. # false if directory does not exist
  124. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  125. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  126. define GoPackage/Build/Configure
  127. ( \
  128. cd $(PKG_BUILD_DIR) ; \
  129. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src \
  130. $(GO_PKG_CACHE_DIR) $(GO_PKG_TMP_DIR) ; \
  131. \
  132. files=$$$$($(FIND) ./ \
  133. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  134. \! -type d -print | \
  135. sed 's|^\./||') ; \
  136. \
  137. if [ "$(GO_PKG_INSTALL_ALL)" != 1 ]; then \
  138. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|go\|h\|hh\|proto\|s\)$$$$') ; \
  139. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  140. \
  141. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  142. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  143. done ; \
  144. \
  145. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$extra") ; \
  146. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  147. fi ; \
  148. \
  149. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)" ; \
  150. for file in $$$$files; do \
  151. echo $$$$file ; \
  152. dest=$(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/$$$$file ; \
  153. mkdir -p $$$$(dirname $$$$dest) ; \
  154. $(CP) $$$$file $$$$dest ; \
  155. done ; \
  156. echo ; \
  157. \
  158. link_contents() { \
  159. local src=$$$$1 ; \
  160. local dest=$$$$2 ; \
  161. local dirs dir base ; \
  162. \
  163. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  164. echo "$$$$src is already a Go library" ; \
  165. return 1 ; \
  166. fi ; \
  167. \
  168. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  169. for dir in $$$$dirs; do \
  170. base=$$$$(basename $$$$dir) ; \
  171. if [ -d $$$$dest/$$$$base ]; then \
  172. case $$$$dir in \
  173. *$(GO_PKG_PATH)/src/$(GO_PKG)) \
  174. echo "$(GO_PKG) is already installed. Please check for circular dependencies." ;; \
  175. *) \
  176. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  177. esac ; \
  178. else \
  179. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  180. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  181. fi ; \
  182. done ; \
  183. } ; \
  184. \
  185. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  186. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  187. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  188. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  189. else \
  190. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  191. fi ; \
  192. else \
  193. echo "Not building binaries, skipping symlinks" ; \
  194. fi ; \
  195. echo ; \
  196. )
  197. endef
  198. # $(1) additional arguments for go command line (optional)
  199. define GoPackage/Build/Compile
  200. ( \
  201. cd $(GO_PKG_BUILD_DIR) ; \
  202. export GOPATH=$(GO_PKG_BUILD_DIR) \
  203. GOCACHE=$(GO_PKG_CACHE_DIR) \
  204. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  205. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  206. CC=$(TARGET_CC) \
  207. CXX=$(TARGET_CXX) \
  208. $(call GoPackage/Environment) ; \
  209. \
  210. echo "Finding targets" ; \
  211. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  212. for pattern in $(GO_PKG_EXCLUDES); do \
  213. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  214. done ; \
  215. echo ; \
  216. \
  217. if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  218. echo "Calling go generate" ; \
  219. go generate -v $(1) $$$$targets ; \
  220. echo ; \
  221. fi ; \
  222. \
  223. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  224. echo "Building targets" ; \
  225. case $(GO_ARCH) in \
  226. arm) installsuffix="-installsuffix v$(GO_ARM)" ;; \
  227. mips|mipsle) installsuffix="-installsuffix $(GO_MIPS)" ;; \
  228. mips64|mips64le) installsuffix="-installsuffix $(GO_MIPS64)" ;; \
  229. esac ; \
  230. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  231. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  232. go install \
  233. $$$$installsuffix \
  234. -gcflags "$$$$trimpath" \
  235. -asmflags "$$$$trimpath" \
  236. -ldflags "$$$$ldflags" \
  237. -v \
  238. $(1) \
  239. $$$$targets ; \
  240. retval=$$$$? ; \
  241. echo ; \
  242. \
  243. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  244. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  245. echo ; \
  246. fi ; \
  247. \
  248. echo "Cleaning module download cache (golang/go#27455)" ; \
  249. go clean -modcache ; \
  250. echo ; \
  251. fi ; \
  252. exit $$$$retval ; \
  253. )
  254. endef
  255. define GoPackage/Build/InstallDev
  256. $(call GoPackage/Package/Install/Src,$(1))
  257. endef
  258. define GoPackage/Package/Install/Bin
  259. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  260. $(INSTALL_DIR) $(1)/usr/bin ; \
  261. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  262. fi
  263. endef
  264. define GoPackage/Package/Install/Src
  265. dir=$$$$(dirname $(GO_PKG)) ; \
  266. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  267. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  268. endef
  269. define GoPackage/Package/Install
  270. $(call GoPackage/Package/Install/Bin,$(1))
  271. $(call GoPackage/Package/Install/Src,$(1))
  272. endef
  273. ifneq ($(GO_PKG),)
  274. Build/Configure=$(call GoPackage/Build/Configure)
  275. Build/Compile=$(call GoPackage/Build/Compile)
  276. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  277. endif
  278. define GoPackage
  279. ifndef Package/$(1)/install
  280. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  281. endif
  282. endef
  283. define GoBinPackage
  284. ifndef Package/$(1)/install
  285. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  286. endif
  287. endef
  288. define GoSrcPackage
  289. ifndef Package/$(1)/install
  290. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  291. endif
  292. endef