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.

307 lines
8.8 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. \
  157. link_contents() { \
  158. local src=$$$$1 ; \
  159. local dest=$$$$2 ; \
  160. local dirs dir base ; \
  161. \
  162. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  163. echo "$$$$src is already a Go library" ; \
  164. return 1 ; \
  165. fi ; \
  166. \
  167. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  168. for dir in $$$$dirs; do \
  169. base=$$$$(basename $$$$dir) ; \
  170. if [ -d $$$$dest/$$$$base ]; then \
  171. case $$$$dir in \
  172. *$(GO_PKG_PATH)/src/$(GO_PKG)) \
  173. echo "$(GO_PKG) is already installed. Please check for circular dependencies." ;; \
  174. *) \
  175. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  176. esac ; \
  177. else \
  178. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  179. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  180. fi ; \
  181. done ; \
  182. } ; \
  183. \
  184. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  185. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  186. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  187. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  188. else \
  189. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  190. fi ; \
  191. else \
  192. echo "Not building binaries, skipping symlinks" ; \
  193. fi ; \
  194. )
  195. endef
  196. # $(1) additional arguments for go command line (optional)
  197. define GoPackage/Build/Compile
  198. ( \
  199. cd $(GO_PKG_BUILD_DIR) ; \
  200. export GOPATH=$(GO_PKG_BUILD_DIR) \
  201. GOCACHE=$(GO_PKG_CACHE_DIR) \
  202. GOTMPDIR=$(GO_PKG_TMP_DIR) \
  203. GOROOT_FINAL=$(GO_TARGET_ROOT) \
  204. CC=$(TARGET_CC) \
  205. CXX=$(TARGET_CXX) \
  206. $(call GoPackage/Environment) ; \
  207. \
  208. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  209. for pattern in $(GO_PKG_EXCLUDES); do \
  210. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  211. done ; \
  212. \
  213. if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \
  214. go generate -v $(1) $$$$targets ; \
  215. fi ; \
  216. \
  217. if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \
  218. case $(GO_ARCH) in \
  219. arm) installsuffix="-installsuffix v$(GO_ARM)" ;; \
  220. mips|mipsle) installsuffix="-installsuffix $(GO_MIPS)" ;; \
  221. mips64|mips64le) installsuffix="-installsuffix $(GO_MIPS64)" ;; \
  222. esac ; \
  223. trimpath="all=-trimpath=$(GO_PKG_BUILD_DIR)" ; \
  224. ldflags="all=-linkmode external -extldflags '$(TARGET_LDFLAGS)'" ; \
  225. go install \
  226. $$$$installsuffix \
  227. -gcflags "$$$$trimpath" \
  228. -asmflags "$$$$trimpath" \
  229. -ldflags "$$$$ldflags" \
  230. -v \
  231. $(1) \
  232. $$$$targets ; \
  233. retval=$$$$? ; \
  234. \
  235. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  236. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  237. fi ; \
  238. fi ; \
  239. exit $$$$retval ; \
  240. )
  241. endef
  242. define GoPackage/Build/InstallDev
  243. $(call GoPackage/Package/Install/Src,$(1))
  244. endef
  245. define GoPackage/Package/Install/Bin
  246. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  247. $(INSTALL_DIR) $(1)/usr/bin ; \
  248. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  249. fi
  250. endef
  251. define GoPackage/Package/Install/Src
  252. dir=$$$$(dirname $(GO_PKG)) ; \
  253. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  254. $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  255. endef
  256. define GoPackage/Package/Install
  257. $(call GoPackage/Package/Install/Bin,$(1))
  258. $(call GoPackage/Package/Install/Src,$(1))
  259. endef
  260. ifneq ($(GO_PKG),)
  261. Build/Configure=$(call GoPackage/Build/Configure)
  262. Build/Compile=$(call GoPackage/Build/Compile)
  263. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  264. endif
  265. define GoPackage
  266. ifndef Package/$(1)/install
  267. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  268. endif
  269. endef
  270. define GoBinPackage
  271. ifndef Package/$(1)/install
  272. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  273. endif
  274. endef
  275. define GoSrcPackage
  276. ifndef Package/$(1)/install
  277. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  278. endif
  279. endef