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.

297 lines
8.6 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. CGO_ENABLED=1 \
  118. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  119. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  120. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))"
  121. endef
  122. # false if directory does not exist
  123. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  124. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  125. define GoPackage/Build/Configure
  126. ( \
  127. cd $(PKG_BUILD_DIR) ; \
  128. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src \
  129. $(GO_PKG_CACHE_DIR) $(GO_PKG_TMP_DIR) ; \
  130. \
  131. files=$$$$($(FIND) ./ \
  132. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  133. \! -type d -print | \
  134. sed 's|^\./||') ; \
  135. \
  136. if [ "$(GO_PKG_INSTALL_ALL)" != 1 ]; then \
  137. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|go\|h\|hh\|proto\|s\)$$$$') ; \
  138. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  139. \
  140. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  141. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  142. done ; \
  143. \
  144. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$extra") ; \
  145. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  146. fi ; \
  147. \
  148. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)" ; \
  149. for file in $$$$files; do \
  150. echo $$$$file ; \
  151. dest=$(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/$$$$file ; \
  152. mkdir -p $$$$(dirname $$$$dest) ; \
  153. $(CP) $$$$file $$$$dest ; \
  154. done ; \
  155. \
  156. link_contents() { \
  157. local src=$$$$1 ; \
  158. local dest=$$$$2 ; \
  159. local dirs dir base ; \
  160. \
  161. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  162. echo "$$$$src is already a Go library" ; \
  163. return 1 ; \
  164. fi ; \
  165. \
  166. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  167. for dir in $$$$dirs; do \
  168. base=$$$$(basename $$$$dir) ; \
  169. if [ -d $$$$dest/$$$$base ]; then \
  170. case $$$$dir in \
  171. *$(GO_PKG_PATH)/src/$(GO_PKG)) \
  172. echo "$(GO_PKG) is already installed. Please check for circular dependencies." ;; \
  173. *) \
  174. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  175. esac ; \
  176. else \
  177. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  178. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  179. fi ; \
  180. done ; \
  181. } ; \
  182. \
  183. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  184. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  185. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  186. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  187. else \
  188. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  189. fi ; \
  190. else \
  191. echo "Not building binaries, skipping symlinks" ; \
  192. fi ; \
  193. )
  194. endef
  195. define GoPackage/Build/Compile
  196. ( \
  197. cd $(GO_PKG_BUILD_DIR) ; \
  198. export GOPATH=$(GO_PKG_BUILD_DIR) \
  199. GOCACHE=$(GO_PKG_CACHE_DIR) \
  200. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  201. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  202. CC=$(TARGET_CC) \
  203. CXX=$(TARGET_CXX) \
  204. $(call GoPackage/Environment) ; \
  205. \
  206. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  207. for pattern in $(GO_PKG_EXCLUDES); do \
  208. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  209. done ; \
  210. \
  211. if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  212. go generate -v $$$$targets ; \
  213. fi ; \
  214. \
  215. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  216. case $(GO_ARCH) in \
  217. arm) installsuffix="-installsuffix v$(GO_ARM)" ;; \
  218. mips|mipsle) installsuffix="-installsuffix $(GO_MIPS)" ;; \
  219. esac ; \
  220. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  221. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  222. go install $$$$installsuffix -gcflags "$$$$trimpath" -asmflags "$$$$trimpath" -ldflags "$$$$ldflags" -v $$$$targets ; \
  223. retval=$$$$? ; \
  224. \
  225. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  226. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  227. fi ; \
  228. fi ; \
  229. exit $$$$retval ; \
  230. )
  231. endef
  232. define GoPackage/Build/InstallDev
  233. $(call GoPackage/Package/Install/Src,$(1))
  234. endef
  235. define GoPackage/Package/Install/Bin
  236. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  237. $(INSTALL_DIR) $(1)/usr/bin ; \
  238. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  239. fi
  240. endef
  241. define GoPackage/Package/Install/Src
  242. dir=$$$$(dirname $(GO_PKG)) ; \
  243. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  244. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  245. endef
  246. define GoPackage/Package/Install
  247. $(call GoPackage/Package/Install/Bin,$(1))
  248. $(call GoPackage/Package/Install/Src,$(1))
  249. endef
  250. ifneq ($(GO_PKG),)
  251. Build/Configure=$(call GoPackage/Build/Configure)
  252. Build/Compile=$(call GoPackage/Build/Compile)
  253. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  254. endif
  255. define GoPackage
  256. ifndef Package/$(1)/install
  257. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  258. endif
  259. endef
  260. define GoBinPackage
  261. ifndef Package/$(1)/install
  262. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  263. endif
  264. endef
  265. define GoSrcPackage
  266. ifndef Package/$(1)/install
  267. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  268. endif
  269. endef