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.

316 lines
8.9 KiB

  1. #
  2. # Copyright (C) 2018-2020 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 options, default empty
  78. #
  79. # Additional go tool compile options to use when building targets.
  80. #
  81. # e.g. GO_PKG_GCFLAGS:=-N -l
  82. #
  83. #
  84. # GO_PKG_LDFLAGS - list of options, default empty
  85. #
  86. # Additional go tool link options 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 option, 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. #
  103. #
  104. # GO_PKG_TAGS - list of build tags, default empty
  105. #
  106. # Build tags to consider satisfied during the build, passed as the
  107. # parameter to the -tags option for 'go install'.
  108. #
  109. # e.g. GO_PKG_TAGS:=release,noupgrade
  110. #
  111. #
  112. # GO_PKG_INSTALL_BIN_PATH - target directory path, default /usr/bin
  113. #
  114. # Directory path under "dest_dir" where binaries will be installed by
  115. # '$(call GoPackage/Package/Install/Bin,dest_dir)'.
  116. #
  117. # e.g. GO_PKG_INSTALL_BIN_PATH:=/sbin
  118. # Credit for this package build process (GoPackage/Build/Configure and
  119. # GoPackage/Build/Compile) belong to Debian's dh-golang completely.
  120. # https://salsa.debian.org/go-team/packages/dh-golang
  121. GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/...
  122. GO_PKG_INSTALL_BIN_PATH?=/usr/bin
  123. GO_PKG_WORK_DIR_NAME:=.go_work
  124. GO_PKG_BUILD_DIR=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)/build
  125. GO_PKG_BUILD_BIN_DIR=$(GO_PKG_BUILD_DIR)/bin$(if $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS_ARCH))
  126. GO_PKG_BUILD_DEPENDS_PATH:=/usr/share/gocode
  127. GO_PKG_BUILD_DEPENDS_SRC=$(STAGING_DIR)$(GO_PKG_BUILD_DEPENDS_PATH)/src
  128. ifdef CONFIG_PKG_ASLR_PIE_ALL
  129. ifeq ($(strip $(PKG_ASLR_PIE)),1)
  130. ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
  131. GO_PKG_ENABLE_PIE:=1
  132. else
  133. $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
  134. endif
  135. endif
  136. endif
  137. ifdef CONFIG_PKG_ASLR_PIE_REGULAR
  138. ifeq ($(strip $(PKG_ASLR_PIE_REGULAR)),1)
  139. ifeq ($(GO_TARGET_PIE_SUPPORTED),1)
  140. GO_PKG_ENABLE_PIE:=1
  141. else
  142. $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH))
  143. endif
  144. endif
  145. endif
  146. ifdef CONFIG_GOLANG_SPECTRE
  147. ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1)
  148. GO_PKG_ENABLE_SPECTRE:=1
  149. else
  150. $(warning Spectre mitigations are not supported for $(GO_ARCH))
  151. endif
  152. endif
  153. # sstrip causes corrupted section header size
  154. ifneq ($(CONFIG_USE_SSTRIP),)
  155. ifneq ($(CONFIG_DEBUG),)
  156. GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note
  157. else
  158. GO_PKG_STRIP_ARGS:=--strip-all
  159. endif
  160. STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS)
  161. endif
  162. define GoPackage/GoSubMenu
  163. SUBMENU:=Go
  164. SECTION:=lang
  165. CATEGORY:=Languages
  166. endef
  167. GO_PKG_BUILD_CONFIG_VARS= \
  168. GO_PKG="$(strip $(GO_PKG))" \
  169. GO_INSTALL_EXTRA="$(strip $(GO_PKG_INSTALL_EXTRA))" \
  170. GO_INSTALL_ALL="$(strip $(GO_PKG_INSTALL_ALL))" \
  171. GO_SOURCE_ONLY="$(strip $(GO_PKG_SOURCE_ONLY))" \
  172. GO_BUILD_PKG="$(strip $(GO_PKG_BUILD_PKG))" \
  173. GO_EXCLUDES="$(strip $(GO_PKG_EXCLUDES))" \
  174. GO_GO_GENERATE="$(strip $(GO_PKG_GO_GENERATE))" \
  175. GO_INSTALL_BIN_PATH="$(strip $(GO_PKG_INSTALL_BIN_PATH))" \
  176. BUILD_DIR="$(PKG_BUILD_DIR)" \
  177. GO_BUILD_DIR="$(GO_PKG_BUILD_DIR)" \
  178. GO_BUILD_BIN_DIR="$(GO_PKG_BUILD_BIN_DIR)" \
  179. GO_BUILD_DEPENDS_PATH="$(GO_PKG_BUILD_DEPENDS_PATH)" \
  180. GO_BUILD_DEPENDS_SRC="$(GO_PKG_BUILD_DEPENDS_SRC)"
  181. GO_PKG_TARGET_VARS= \
  182. GOOS="$(GO_OS)" \
  183. GOARCH="$(GO_ARCH)" \
  184. GO386="$(GO_386)" \
  185. GOARM="$(GO_ARM)" \
  186. GOMIPS="$(GO_MIPS)" \
  187. GOMIPS64="$(GO_MIPS64)" \
  188. CGO_ENABLED=1 \
  189. CC="$(TARGET_CC)" \
  190. CXX="$(TARGET_CXX)" \
  191. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  192. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  193. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \
  194. CGO_LDFLAGS="$(TARGET_LDFLAGS)"
  195. GO_PKG_BUILD_VARS= \
  196. GOPATH="$(GO_PKG_BUILD_DIR)" \
  197. GOCACHE="$(GO_BUILD_CACHE_DIR)" \
  198. GOMODCACHE="$(GO_MOD_CACHE_DIR)" \
  199. GOENV=off
  200. GO_PKG_VARS= \
  201. $(GO_PKG_TARGET_VARS) \
  202. $(GO_PKG_BUILD_VARS)
  203. GO_PKG_DEFAULT_GCFLAGS= \
  204. $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
  205. GO_PKG_DEFAULT_ASMFLAGS= \
  206. $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
  207. GO_PKG_DEFAULT_LDFLAGS= \
  208. -buildid '$(SOURCE_DATE_EPOCH)' \
  209. -linkmode external \
  210. -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))'
  211. GO_PKG_CUSTOM_LDFLAGS= \
  212. $(GO_PKG_LDFLAGS) \
  213. $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X))
  214. GO_PKG_INSTALL_ARGS= \
  215. -v \
  216. -trimpath \
  217. -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \
  218. $(if $(GO_PKG_DEFAULT_GCFLAGS),-gcflags "all=$(GO_PKG_DEFAULT_GCFLAGS)") \
  219. $(if $(GO_PKG_DEFAULT_ASMFLAGS),-asmflags "all=$(GO_PKG_DEFAULT_ASMFLAGS)") \
  220. $(if $(filter $(GO_PKG_ENABLE_PIE),1),-buildmode pie) \
  221. $(if $(filter $(GO_ARCH),arm),-installsuffix "v$(GO_ARM)") \
  222. $(if $(filter $(GO_ARCH),mips mipsle),-installsuffix "$(GO_MIPS)") \
  223. $(if $(filter $(GO_ARCH),mips64 mips64le),-installsuffix "$(GO_MIPS64)") \
  224. $(if $(GO_PKG_GCFLAGS),-gcflags "$(GO_PKG_GCFLAGS) $(GO_PKG_DEFAULT_GCFLAGS)") \
  225. $(if $(GO_PKG_CUSTOM_LDFLAGS),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)") \
  226. $(if $(GO_PKG_TAGS),-tags "$(GO_PKG_TAGS)")
  227. define GoPackage/Build/Configure
  228. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  229. $(GO_PKG_BUILD_CONFIG_VARS) \
  230. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure
  231. endef
  232. # $(1) additional arguments for go command line (optional)
  233. define GoPackage/Build/Compile
  234. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  235. $(GO_PKG_BUILD_CONFIG_VARS) \
  236. $(GO_PKG_VARS) \
  237. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_PKG_INSTALL_ARGS) $(1)
  238. endef
  239. define GoPackage/Build/InstallDev
  240. $(call GoPackage/Package/Install/Src,$(1))
  241. endef
  242. define GoPackage/Package/Install/Bin
  243. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  244. $(GO_PKG_BUILD_CONFIG_VARS) \
  245. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(1)"
  246. endef
  247. define GoPackage/Package/Install/Src
  248. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  249. $(GO_PKG_BUILD_CONFIG_VARS) \
  250. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(1)"
  251. endef
  252. define GoPackage/Package/Install
  253. $(if $(filter $(GO_PKG_SOURCE_ONLY),1),, \
  254. $(call GoPackage/Package/Install/Bin,$(1)) \
  255. )
  256. $(call GoPackage/Package/Install/Src,$(1))
  257. endef
  258. ifneq ($(strip $(GO_PKG)),)
  259. ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1)
  260. PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE
  261. endif
  262. Build/Configure=$(call GoPackage/Build/Configure)
  263. Build/Compile=$(call GoPackage/Build/Compile)
  264. Hooks/Compile/Post+=Go/CacheCleanup
  265. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  266. endif
  267. define GoPackage
  268. ifndef Package/$(1)/install
  269. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  270. endif
  271. endef
  272. define GoBinPackage
  273. ifndef Package/$(1)/install
  274. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  275. endif
  276. endef
  277. define GoSrcPackage
  278. ifndef Package/$(1)/install
  279. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  280. endif
  281. endef