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.

319 lines
9.0 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, go.sum and go.work, 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. GOAMD64="$(GO_AMD64)" \
  186. GOARM="$(GO_ARM)" \
  187. GOMIPS="$(GO_MIPS)" \
  188. GOMIPS64="$(GO_MIPS64)" \
  189. GOPPC64="$(GO_PPC64)" \
  190. CGO_ENABLED=1 \
  191. CC="$(TARGET_CC)" \
  192. CXX="$(TARGET_CXX)" \
  193. CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \
  194. CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \
  195. CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \
  196. CGO_LDFLAGS="$(TARGET_LDFLAGS)"
  197. GO_PKG_BUILD_VARS= \
  198. GOPATH="$(GO_PKG_BUILD_DIR)" \
  199. GOCACHE="$(GO_BUILD_CACHE_DIR)" \
  200. GOMODCACHE="$(GO_MOD_CACHE_DIR)" \
  201. GOENV=off
  202. GO_PKG_VARS= \
  203. $(GO_PKG_TARGET_VARS) \
  204. $(GO_PKG_BUILD_VARS)
  205. GO_PKG_DEFAULT_GCFLAGS= \
  206. $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
  207. GO_PKG_DEFAULT_ASMFLAGS= \
  208. $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all)
  209. GO_PKG_DEFAULT_LDFLAGS= \
  210. -buildid '$(SOURCE_DATE_EPOCH)' \
  211. -linkmode external \
  212. -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))'
  213. GO_PKG_CUSTOM_LDFLAGS= \
  214. $(GO_PKG_LDFLAGS) \
  215. $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X))
  216. GO_PKG_INSTALL_ARGS= \
  217. -v \
  218. -buildvcs=false \
  219. -trimpath \
  220. -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \
  221. $(if $(strip $(GO_PKG_DEFAULT_GCFLAGS)),-gcflags "all=$(GO_PKG_DEFAULT_GCFLAGS)") \
  222. $(if $(strip $(GO_PKG_DEFAULT_ASMFLAGS)),-asmflags "all=$(GO_PKG_DEFAULT_ASMFLAGS)") \
  223. $(if $(GO_PKG_ENABLE_PIE),-buildmode pie) \
  224. $(if $(filter $(GO_ARCH),arm),-installsuffix "v$(GO_ARM)") \
  225. $(if $(filter $(GO_ARCH),mips mipsle),-installsuffix "$(GO_MIPS)") \
  226. $(if $(filter $(GO_ARCH),mips64 mips64le),-installsuffix "$(GO_MIPS64)") \
  227. $(if $(strip $(GO_PKG_GCFLAGS)),-gcflags "$(GO_PKG_GCFLAGS) $(GO_PKG_DEFAULT_GCFLAGS)") \
  228. $(if $(strip $(GO_PKG_CUSTOM_LDFLAGS)),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)") \
  229. $(if $(strip $(GO_PKG_TAGS)),-tags "$(GO_PKG_TAGS)")
  230. define GoPackage/Build/Configure
  231. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  232. $(GO_PKG_BUILD_CONFIG_VARS) \
  233. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure
  234. endef
  235. # $(1) additional arguments for go command line (optional)
  236. define GoPackage/Build/Compile
  237. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  238. $(GO_PKG_BUILD_CONFIG_VARS) \
  239. $(GO_PKG_VARS) \
  240. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_PKG_INSTALL_ARGS) $(1)
  241. endef
  242. define GoPackage/Build/InstallDev
  243. $(call GoPackage/Package/Install/Src,$(1))
  244. endef
  245. define GoPackage/Package/Install/Bin
  246. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  247. $(GO_PKG_BUILD_CONFIG_VARS) \
  248. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(1)"
  249. endef
  250. define GoPackage/Package/Install/Src
  251. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  252. $(GO_PKG_BUILD_CONFIG_VARS) \
  253. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(1)"
  254. endef
  255. define GoPackage/Package/Install
  256. $(if $(filter $(GO_PKG_SOURCE_ONLY),1),, \
  257. $(call GoPackage/Package/Install/Bin,$(1)) \
  258. )
  259. $(call GoPackage/Package/Install/Src,$(1))
  260. endef
  261. ifneq ($(strip $(GO_PKG)),)
  262. ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1)
  263. PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE
  264. endif
  265. Build/Configure=$(call GoPackage/Build/Configure)
  266. Build/Compile=$(call GoPackage/Build/Compile)
  267. Hooks/Compile/Post+=Go/CacheCleanup
  268. Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
  269. endif
  270. define GoPackage
  271. ifndef Package/$(1)/install
  272. Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1))
  273. endif
  274. endef
  275. define GoBinPackage
  276. ifndef Package/$(1)/install
  277. Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1))
  278. endif
  279. endef
  280. define GoSrcPackage
  281. ifndef Package/$(1)/install
  282. Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1))
  283. endif
  284. endef