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.

375 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. # Note that the OpenWrt build system has an option to strip binaries
  89. # (enabled by default), so -s (Omit the symbol table and debug
  90. # information) and -w (Omit the DWARF symbol table) flags are not
  91. # necessary.
  92. #
  93. # e.g. GO_PKG_LDFLAGS:=-r dir1:dir2 -u
  94. #
  95. #
  96. # GO_PKG_LDFLAGS_X - list of string variable definitions, default empty
  97. #
  98. # Each definition will be passed as the parameter to the -X go tool
  99. # link argument, i.e. -ldflags "-X importpath.name=value"
  100. #
  101. # e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH)
  102. # Credit for this package build process (GoPackage/Build/Configure and
  103. # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
  104. # https://salsa.debian.org/go-team/packages/dh-golang
  105. # for building packages, not user code
  106. GO_PKG_PATH:=/usr/share/gocode
  107. GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/...
  108. GO_PKG_WORK_DIR_NAME:=.go_work
  109. GO_PKG_WORK_DIR=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)
  110. GO_PKG_BUILD_DIR=$(GO_PKG_WORK_DIR)/build
  111. GO_PKG_CACHE_DIR=$(GO_PKG_WORK_DIR)/cache
  112. GO_PKG_BUILD_BIN_DIR=$(GO_PKG_BUILD_DIR)/bin$(if $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS_ARCH))
  113. GO_PKG_BUILD_DEPENDS_SRC=$(STAGING_DIR)$(GO_PKG_PATH)/src
  114. ifdef CONFIG_PKG_ASLR_PIE_ALL
  115. ifeq ($(strip $(PKG_ASLR_PIE)),1)
  116. ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
  117. GO_PKG_ENABLE_PIE:=1
  118. else
  119. $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
  120. endif
  121. endif
  122. endif
  123. ifdef CONFIG_PKG_ASLR_PIE_REGULAR
  124. ifeq ($(strip $(PKG_ASLR_PIE_REGULAR)),1)
  125. ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
  126. GO_PKG_ENABLE_PIE:=1
  127. else
  128. $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
  129. endif
  130. endif
  131. endif
  132. # sstrip causes corrupted section header size
  133. ifneq ($(CONFIG_USE_SSTRIP),)
  134. ifneq ($(CONFIG_DEBUG),)
  135. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  136. else
  137. GO_PKG_STRIP_ARGS:=--strip-all
  138. endif
  139. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  140. endif
  141. define GoPackage/GoSubMenu
  142. SUBMENU:=Go
  143. SECTION:=lang
  144. CATEGORY:=Languages
  145. endef
  146. GO_PKG_TARGET_VARS= \
  147. GOOS=$(GO_OS) \
  148. GOARCH=$(GO_ARCH) \
  149. GO386=$(GO_386) \
  150. GOARM=$(GO_ARM) \
  151. GOMIPS=$(GO_MIPS) \
  152. GOMIPS64=$(GO_MIPS64) \
  153. CGO_ENABLED=1 \
  154. CC=$(TARGET_CC) \
  155. CXX=$(TARGET_CXX) \
  156. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  157. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  158. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \
  159. CGO_LDFLAGS="$(TARGET_LDFLAGS)"
  160. GO_PKG_BUILD_VARS= \
  161. GOPATH=$(GO_PKG_BUILD_DIR) \
  162. GOCACHE=$(GO_PKG_CACHE_DIR) \
  163. GOENV=off
  164. GO_PKG_DEFAULT_VARS= \
  165. $(GO_PKG_TARGET_VARS) \
  166. $(GO_PKG_BUILD_VARS)
  167. GO_PKG_VARS=$(GO_PKG_DEFAULT_VARS)
  168. # do not use for new code; this will be removed after the next OpenWrt release
  169. GoPackage/Environment=$(GO_PKG_VARS)
  170. GO_PKG_DEFAULT_LDFLAGS= \
  171. -buildid '$(SOURCE_DATE_EPOCH)' \
  172. -linkmode external \
  173. -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))'
  174. GO_PKG_CUSTOM_LDFLAGS= \
  175. $(GO_PKG_LDFLAGS) \
  176. $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X))
  177. GO_PKG_INSTALL_ARGS= \
  178. -v \
  179. -trimpath \
  180. -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \
  181. $(if $(filter $(GO_PKG_ENABLE_PIE),1),-buildmode pie) \
  182. $(if $(filter $(GO_ARCH),arm),-installsuffix "v$(GO_ARM)") \
  183. $(if $(filter $(GO_ARCH),mips mipsle),-installsuffix "$(GO_MIPS)") \
  184. $(if $(filter $(GO_ARCH),mips64 mips64le),-installsuffix "$(GO_MIPS64)") \
  185. $(if $(GO_PKG_GCFLAGS),-gcflags "$(GO_PKG_GCFLAGS)") \
  186. $(if $(GO_PKG_CUSTOM_LDFLAGS),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)")
  187. # false if directory does not exist
  188. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  189. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  190. define GoPackage/Build/Configure
  191. ( \
  192. cd $(PKG_BUILD_DIR) ; \
  193. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src $(GO_PKG_CACHE_DIR) ; \
  194. \
  195. files=$$$$($(FIND) ./ \
  196. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  197. \! -type d -print | \
  198. sed 's|^\./||') ; \
  199. \
  200. if [ "$(strip $(GO_PKG_INSTALL_ALL))" != 1 ]; then \
  201. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
  202. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  203. gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
  204. \
  205. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  206. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  207. done ; \
  208. \
  209. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$gomod"; echo "$$$$extra") ; \
  210. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  211. fi ; \
  212. \
  213. IFS=$$$$'\n' ; \
  214. \
  215. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))" ; \
  216. for file in $$$$files; do \
  217. echo $$$$file ; \
  218. dest=$(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))/$$$$file ; \
  219. mkdir -p $$$$(dirname $$$$dest) ; \
  220. $(CP) $$$$file $$$$dest ; \
  221. done ; \
  222. echo ; \
  223. \
  224. link_contents() { \
  225. local src=$$$$1 ; \
  226. local dest=$$$$2 ; \
  227. local dirs dir base ; \
  228. \
  229. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  230. echo "$$$$src is already a Go library" ; \
  231. return 1 ; \
  232. fi ; \
  233. \
  234. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  235. for dir in $$$$dirs; do \
  236. base=$$$$(basename $$$$dir) ; \
  237. if [ -d $$$$dest/$$$$base ]; then \
  238. case $$$$dir in \
  239. *$(GO_PKG_PATH)/src/$(strip $(GO_PKG))) \
  240. echo "$(strip $(GO_PKG)) is already installed. Please check for circular dependencies." ;; \
  241. *) \
  242. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  243. esac ; \
  244. else \
  245. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  246. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  247. fi ; \
  248. done ; \
  249. } ; \
  250. \
  251. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  252. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  253. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  254. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  255. else \
  256. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  257. fi ; \
  258. else \
  259. echo "Not building binaries, skipping symlinks" ; \
  260. fi ; \
  261. echo ; \
  262. )
  263. endef
  264. # $(1) additional arguments for go command line (optional)
  265. define GoPackage/Build/Compile
  266. ( \
  267. cd $(GO_PKG_BUILD_DIR) ; \
  268. export $(GO_PKG_VARS) ; \
  269. \
  270. echo "Finding targets" ; \
  271. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  272. for pattern in $(GO_PKG_EXCLUDES); do \
  273. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  274. done ; \
  275. echo ; \
  276. \
  277. if [ "$(strip $(GO_PKG_GO_GENERATE))" = 1 ]; then \
  278. echo "Calling go generate" ; \
  279. go generate -v $(1) $$$$targets ; \
  280. echo ; \
  281. fi ; \
  282. \
  283. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  284. echo "Building targets" ; \
  285. go install $(GO_PKG_INSTALL_ARGS) $(1) $$$$targets ; \
  286. retval=$$$$? ; \
  287. echo ; \
  288. \
  289. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  290. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  291. echo ; \
  292. fi ; \
  293. \
  294. echo "Cleaning module download cache (golang/go#27455)" ; \
  295. go clean -modcache ; \
  296. echo ; \
  297. fi ; \
  298. exit $$$$retval ; \
  299. )
  300. endef
  301. define GoPackage/Build/InstallDev
  302. $(call GoPackage/Package/Install/Src,$(1))
  303. endef
  304. define GoPackage/Package/Install/Bin
  305. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  306. $(INSTALL_DIR) $(1)/usr/bin ; \
  307. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  308. fi
  309. endef
  310. define GoPackage/Package/Install/Src
  311. dir=$$$$(dirname $(GO_PKG)) ; \
  312. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  313. $(CP) $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG)) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  314. endef
  315. define GoPackage/Package/Install
  316. $(call GoPackage/Package/Install/Bin,$(1))
  317. $(call GoPackage/Package/Install/Src,$(1))
  318. endef
  319. ifneq ($(strip $(GO_PKG)),)
  320. Build/Configure=$(call GoPackage/Build/Configure)
  321. Build/Compile=$(call GoPackage/Build/Compile)
  322. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  323. endif
  324. define GoPackage
  325. ifndef Package/$(1)/install
  326. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  327. endif
  328. endef
  329. define GoBinPackage
  330. ifndef Package/$(1)/install
  331. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  332. endif
  333. endef
  334. define GoSrcPackage
  335. ifndef Package/$(1)/install
  336. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  337. endif
  338. endef