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.

392 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_INSTALL_ARGS= \
  175. -v \
  176. -trimpath \
  177. -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)"
  178. ifeq ($(GO_PKG_ENABLE_PIE),1)
  179. GO_PKG_INSTALL_ARGS+= -buildmode pie
  180. endif
  181. ifeq ($(GO_ARCH),arm)
  182. GO_PKG_INSTALL_ARGS+= -installsuffix "v$(GO_ARM)"
  183. else ifneq ($(filter $(GO_ARCH),mips mipsle),)
  184. GO_PKG_INSTALL_ARGS+= -installsuffix "$(GO_MIPS)"
  185. else ifneq ($(filter $(GO_ARCH),mips64 mips64le),)
  186. GO_PKG_INSTALL_ARGS+= -installsuffix "$(GO_MIPS64)"
  187. endif
  188. ifneq ($(strip $(GO_PKG_GCFLAGS)),)
  189. GO_PKG_INSTALL_ARGS+= -gcflags "$(GO_PKG_GCFLAGS)"
  190. endif
  191. GO_PKG_CUSTOM_LDFLAGS= \
  192. $(GO_PKG_LDFLAGS) \
  193. $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X))
  194. ifneq ($(strip $(GO_PKG_CUSTOM_LDFLAGS)),)
  195. GO_PKG_INSTALL_ARGS+= -ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)"
  196. endif
  197. # false if directory does not exist
  198. GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null)
  199. GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR))
  200. define GoPackage/Build/Configure
  201. ( \
  202. cd $(PKG_BUILD_DIR) ; \
  203. mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src $(GO_PKG_CACHE_DIR) ; \
  204. \
  205. files=$$$$($(FIND) ./ \
  206. -type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
  207. \! -type d -print | \
  208. sed 's|^\./||') ; \
  209. \
  210. if [ "$(strip $(GO_PKG_INSTALL_ALL))" != 1 ]; then \
  211. code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \
  212. testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \
  213. gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \
  214. \
  215. for pattern in $(GO_PKG_INSTALL_EXTRA); do \
  216. extra=$$$$(echo "$$$$extra"; echo "$$$$files" | grep "$$$$pattern") ; \
  217. done ; \
  218. \
  219. files=$$$$(echo "$$$$code"; echo "$$$$testdata"; echo "$$$$gomod"; echo "$$$$extra") ; \
  220. files=$$$$(echo "$$$$files" | grep -v '^[[:space:]]*$$$$' | sort -u) ; \
  221. fi ; \
  222. \
  223. IFS=$$$$'\n' ; \
  224. \
  225. echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))" ; \
  226. for file in $$$$files; do \
  227. echo $$$$file ; \
  228. dest=$(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))/$$$$file ; \
  229. mkdir -p $$$$(dirname $$$$dest) ; \
  230. $(CP) $$$$file $$$$dest ; \
  231. done ; \
  232. echo ; \
  233. \
  234. link_contents() { \
  235. local src=$$$$1 ; \
  236. local dest=$$$$2 ; \
  237. local dirs dir base ; \
  238. \
  239. if [ -n "$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -name '*.go' \! -type d)" ]; then \
  240. echo "$$$$src is already a Go library" ; \
  241. return 1 ; \
  242. fi ; \
  243. \
  244. dirs=$$$$($(FIND) $$$$src -mindepth 1 -maxdepth 1 -type d) ; \
  245. for dir in $$$$dirs; do \
  246. base=$$$$(basename $$$$dir) ; \
  247. if [ -d $$$$dest/$$$$base ]; then \
  248. case $$$$dir in \
  249. *$(GO_PKG_PATH)/src/$(strip $(GO_PKG))) \
  250. echo "$(strip $(GO_PKG)) is already installed. Please check for circular dependencies." ;; \
  251. *) \
  252. link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \
  253. esac ; \
  254. else \
  255. echo "...$$$${src#$(GO_PKG_BUILD_DEPENDS_SRC)}/$$$$base" ; \
  256. $(LN) $$$$src/$$$$base $$$$dest/$$$$base ; \
  257. fi ; \
  258. done ; \
  259. } ; \
  260. \
  261. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  262. if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \
  263. echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \
  264. link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \
  265. else \
  266. echo "$(GO_PKG_BUILD_DEPENDS_SRC) does not exist, skipping symlinks" ; \
  267. fi ; \
  268. else \
  269. echo "Not building binaries, skipping symlinks" ; \
  270. fi ; \
  271. echo ; \
  272. )
  273. endef
  274. # $(1) additional arguments for go command line (optional)
  275. define GoPackage/Build/Compile
  276. ( \
  277. cd $(GO_PKG_BUILD_DIR) ; \
  278. export $(GO_PKG_VARS) ; \
  279. \
  280. echo "Finding targets" ; \
  281. targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
  282. for pattern in $(GO_PKG_EXCLUDES); do \
  283. targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
  284. done ; \
  285. echo ; \
  286. \
  287. if [ "$(strip $(GO_PKG_GO_GENERATE))" = 1 ]; then \
  288. echo "Calling go generate" ; \
  289. go generate -v $(1) $$$$targets ; \
  290. echo ; \
  291. fi ; \
  292. \
  293. if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
  294. echo "Building targets" ; \
  295. go install $(GO_PKG_INSTALL_ARGS) $(1) $$$$targets ; \
  296. retval=$$$$? ; \
  297. echo ; \
  298. \
  299. if [ "$$$$retval" -eq 0 ] && [ -z "$(call GoPackage/has_binaries)" ]; then \
  300. echo "No binaries were generated, consider adding GO_PKG_SOURCE_ONLY:=1 to Makefile" ; \
  301. echo ; \
  302. fi ; \
  303. \
  304. echo "Cleaning module download cache (golang/go#27455)" ; \
  305. go clean -modcache ; \
  306. echo ; \
  307. fi ; \
  308. exit $$$$retval ; \
  309. )
  310. endef
  311. define GoPackage/Build/InstallDev
  312. $(call GoPackage/Package/Install/Src,$(1))
  313. endef
  314. define GoPackage/Package/Install/Bin
  315. if [ -n "$(call GoPackage/has_binaries)" ]; then \
  316. $(INSTALL_DIR) $(1)/usr/bin ; \
  317. $(INSTALL_BIN) $(GO_PKG_BUILD_BIN_DIR)/* $(1)/usr/bin/ ; \
  318. fi
  319. endef
  320. define GoPackage/Package/Install/Src
  321. dir=$$$$(dirname $(GO_PKG)) ; \
  322. $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \
  323. $(CP) $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG)) $(1)$(GO_PKG_PATH)/src/$$$$dir/
  324. endef
  325. define GoPackage/Package/Install
  326. $(call GoPackage/Package/Install/Bin,$(1))
  327. $(call GoPackage/Package/Install/Src,$(1))
  328. endef
  329. ifneq ($(strip $(GO_PKG)),)
  330. Build/Configure=$(call GoPackage/Build/Configure)
  331. Build/Compile=$(call GoPackage/Build/Compile)
  332. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  333. endif
  334. define GoPackage
  335. ifndef Package/$(1)/install
  336. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  337. endif
  338. endef
  339. define GoBinPackage
  340. ifndef Package/$(1)/install
  341. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  342. endif
  343. endef
  344. define GoSrcPackage
  345. ifndef Package/$(1)/install
  346. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  347. endif
  348. endef