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.

305 lines
8.7 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. # $(1) additional arguments for go command line (optional)
  196. define GoPackage/Build/Compile
  197. ( \
  198. cd $(GO_PKG_BUILD_DIR) ; \
  199. export GOPATH=$(GO_PKG_BUILD_DIR) \
  200. GOCACHE=$(GO_PKG_CACHE_DIR) \
  201. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  202. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  203. CC=$(TARGET_CC) \
  204. CXX=$(TARGET_CXX) \
  205. $(call GoPackage/Environment) ; \
  206. \
  207. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  208. for pattern in $(GO_PKG_EXCLUDES); do \
  209. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  210. done ; \
  211. \
  212. if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  213. go generate -v $(1) $$$$targets ; \
  214. fi ; \
  215. \
  216. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  217. case $(GO_ARCH) in \
  218. arm) installsuffix="-installsuffix v$(GO_ARM)" ;; \
  219. mips|mipsle) installsuffix="-installsuffix $(GO_MIPS)" ;; \
  220. esac ; \
  221. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  222. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  223. go install \
  224. $$$$installsuffix \
  225. -gcflags "$$$$trimpath" \
  226. -asmflags "$$$$trimpath" \
  227. -ldflags "$$$$ldflags" \
  228. -v \
  229. $(1) \
  230. $$$$targets ; \
  231. retval=$$$$? ; \
  232. \
  233. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  234. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  235. fi ; \
  236. fi ; \
  237. exit $$$$retval ; \
  238. )
  239. endef
  240. define GoPackage/Build/InstallDev
  241. $(call GoPackage/Package/Install/Src,$(1))
  242. endef
  243. define GoPackage/Package/Install/Bin
  244. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  245. $(INSTALL_DIR) $(1)/usr/bin ; \
  246. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  247. fi
  248. endef
  249. define GoPackage/Package/Install/Src
  250. dir=$$$$(dirname $(GO_PKG)) ; \
  251. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  252. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  253. endef
  254. define GoPackage/Package/Install
  255. $(call GoPackage/Package/Install/Bin,$(1))
  256. $(call GoPackage/Package/Install/Src,$(1))
  257. endef
  258. ifneq ($(GO_PKG),)
  259. Build/Configure=$(call GoPackage/Build/Configure)
  260. Build/Compile=$(call GoPackage/Build/Compile)
  261. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  262. endif
  263. define GoPackage
  264. ifndef Package/$(1)/install
  265. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  266. endif
  267. endef
  268. define GoBinPackage
  269. ifndef Package/$(1)/install
  270. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  271. endif
  272. endef
  273. define GoSrcPackage
  274. ifndef Package/$(1)/install
  275. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  276. endif
  277. endef