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.

220 lines
6.6 KiB

  1. #
  2. # Copyright (C) 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. # these variables have the same meanings as in golang-package.mk
  12. GO_HOST_INSTALL_EXTRA?=$(GO_PKG_INSTALL_EXTRA)
  13. GO_HOST_INSTALL_ALL?=$(GO_PKG_INSTALL_ALL)
  14. GO_HOST_SOURCE_ONLY?=$(GO_PKG_SOURCE_ONLY)
  15. GO_HOST_BUILD_PKG?=$(GO_PKG_BUILD_PKG)
  16. GO_HOST_EXCLUDES?=$(GO_PKG_EXCLUDES)
  17. GO_HOST_GO_GENERATE?=$(GO_PKG_GO_GENERATE)
  18. GO_HOST_GCFLAGS?=$(GO_PKG_GCFLAGS)
  19. GO_HOST_LDFLAGS?=$(GO_PKG_LDFLAGS)
  20. GO_HOST_LDFLAGS_X?=$(GO_PKG_LDFLAGS_X)
  21. GO_HOST_TAGS?=$(GO_PKG_TAGS)
  22. GO_HOST_INSTALL_BIN_PATH?=/bin
  23. # need to repeat this here in case golang-package.mk is not included
  24. GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/...
  25. GO_HOST_WORK_DIR_NAME:=.go_work
  26. GO_HOST_BUILD_DIR=$(HOST_BUILD_DIR)/$(GO_HOST_WORK_DIR_NAME)/build
  27. GO_HOST_BUILD_BIN_DIR=$(GO_HOST_BUILD_DIR)/bin
  28. GO_HOST_BUILD_DEPENDS_PATH:=/share/gocode
  29. GO_HOST_BUILD_DEPENDS_SRC=$(STAGING_DIR_HOSTPKG)$(GO_HOST_BUILD_DEPENDS_PATH)/src
  30. GO_HOST_DIR_NAME:=$(lastword $(subst /,$(space),$(CURDIR)))
  31. GO_HOST_STAGING_DIR:=$(TMP_DIR)/host-stage-$(GO_HOST_DIR_NAME)
  32. GO_HOST_STAGING_FILES_LIST_DIR:=$(HOST_BUILD_PREFIX)/stamp
  33. GO_HOST_BIN_STAGING_FILES_LIST:=$(GO_HOST_STAGING_FILES_LIST_DIR)/$(GO_HOST_DIR_NAME)-bin.list
  34. GO_HOST_SRC_STAGING_FILES_LIST:=$(GO_HOST_STAGING_FILES_LIST_DIR)/$(GO_HOST_DIR_NAME)-src.list
  35. ifeq ($(GO_HOST_PIE_SUPPORTED),1)
  36. GO_HOST_ENABLE_PIE:=1
  37. endif
  38. GO_HOST_BUILD_CONFIG_VARS= \
  39. GO_PKG="$(strip $(GO_PKG))" \
  40. GO_INSTALL_EXTRA="$(strip $(GO_HOST_INSTALL_EXTRA))" \
  41. GO_INSTALL_ALL="$(strip $(GO_HOST_INSTALL_ALL))" \
  42. GO_SOURCE_ONLY="$(strip $(GO_HOST_SOURCE_ONLY))" \
  43. GO_BUILD_PKG="$(strip $(GO_HOST_BUILD_PKG))" \
  44. GO_EXCLUDES="$(strip $(GO_HOST_EXCLUDES))" \
  45. GO_GO_GENERATE="$(strip $(GO_HOST_GO_GENERATE))" \
  46. GO_INSTALL_BIN_PATH="$(strip $(GO_HOST_INSTALL_BIN_PATH))" \
  47. BUILD_DIR="$(HOST_BUILD_DIR)" \
  48. GO_BUILD_DIR="$(GO_HOST_BUILD_DIR)" \
  49. GO_BUILD_BIN_DIR="$(GO_HOST_BUILD_BIN_DIR)" \
  50. GO_BUILD_DEPENDS_PATH="$(GO_HOST_BUILD_DEPENDS_PATH)" \
  51. GO_BUILD_DEPENDS_SRC="$(GO_HOST_BUILD_DEPENDS_SRC)"
  52. GO_HOST_MORE_CFLAGS?= \
  53. -Wformat -Werror=format-security \
  54. -fstack-protector-strong \
  55. -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
  56. -Wl,-z,now -Wl,-z,relro \
  57. $(if $(GO_HOST_ENABLE_PIE),$(FPIC))
  58. GO_HOST_MORE_LDFLAGS?= \
  59. -znow -zrelro \
  60. $(if $(GO_HOST_ENABLE_PIE),$(FPIC) -specs=$(INCLUDE_DIR)/hardened-ld-pie.specs)
  61. GO_HOST_TARGET_VARS= \
  62. CGO_ENABLED=1 \
  63. CC=gcc \
  64. CXX=g++ \
  65. PKG_CONFIG=pkg-config \
  66. CGO_CFLAGS="$(HOST_CFLAGS) $(GO_HOST_MORE_CFLAGS)" \
  67. CGO_CPPFLAGS="$(HOST_CPPFLAGS) $(GO_HOST_MORE_CPPFLAGS)" \
  68. CGO_CXXFLAGS="$(HOST_CFLAGS) $(GO_HOST_MORE_CFLAGS)" \
  69. CGO_LDFLAGS="$(HOST_LDFLAGS) $(GO_HOST_MORE_LDFLAGS)" \
  70. GO_GCC_HELPER_CC="$(HOSTCC)" \
  71. GO_GCC_HELPER_CXX="$(HOSTCXX)" \
  72. GO_GCC_HELPER_PATH="$$$$PATH" \
  73. PATH="$(STAGING_DIR_HOSTPKG)/lib/go-cross/openwrt:$$$$PATH"
  74. GO_HOST_BUILD_VARS= \
  75. GOPATH="$(GO_HOST_BUILD_DIR)" \
  76. GOCACHE="$(GO_BUILD_CACHE_DIR)" \
  77. GOMODCACHE="$(GO_MOD_CACHE_DIR)" \
  78. GOENV=off
  79. GO_HOST_VARS= \
  80. $(GO_HOST_TARGET_VARS) \
  81. $(GO_HOST_BUILD_VARS)
  82. GO_HOST_DEFAULT_LDFLAGS= \
  83. -linkmode external \
  84. -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(HOST_LDFLAGS) $(GO_HOST_MORE_LDFLAGS))'
  85. GO_HOST_CUSTOM_LDFLAGS= \
  86. $(GO_HOST_LDFLAGS) \
  87. $(patsubst %,-X %,$(GO_HOST_LDFLAGS_X))
  88. GO_HOST_INSTALL_ARGS= \
  89. -v \
  90. -ldflags "all=$(GO_HOST_DEFAULT_LDFLAGS)" \
  91. $(if $(GO_HOST_ENABLE_PIE),-buildmode pie) \
  92. $(if $(strip $(GO_HOST_GCFLAGS)),-gcflags "$(GO_HOST_GCFLAGS)") \
  93. $(if $(strip $(GO_HOST_CUSTOM_LDFLAGS)),-ldflags "$(GO_HOST_CUSTOM_LDFLAGS) $(GO_HOST_DEFAULT_LDFLAGS)") \
  94. $(if $(strip $(GO_HOST_TAGS)),-tags "$(GO_HOST_TAGS)")
  95. define GoHost/Host/Configure
  96. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  97. $(GO_HOST_BUILD_CONFIG_VARS) \
  98. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure
  99. endef
  100. # $(1) additional arguments for go command line (optional)
  101. define GoHost/Host/Compile
  102. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  103. $(GO_HOST_BUILD_CONFIG_VARS) \
  104. $(GO_HOST_VARS) \
  105. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_HOST_INSTALL_ARGS) $(1)
  106. endef
  107. define GoHost/Host/Install/Bin
  108. rm -rf "$(GO_HOST_STAGING_DIR)"
  109. mkdir -p "$(GO_HOST_STAGING_DIR)" "$(GO_HOST_STAGING_FILES_LIST_DIR)"
  110. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  111. $(GO_HOST_BUILD_CONFIG_VARS) \
  112. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(GO_HOST_STAGING_DIR)"
  113. if [ -f "$(GO_HOST_BIN_STAGING_FILES_LIST)" ]; then \
  114. "$(SCRIPT_DIR)/clean-package.sh" \
  115. "$(GO_HOST_BIN_STAGING_FILES_LIST)" \
  116. "$(1)" ; \
  117. fi
  118. cd "$(GO_HOST_STAGING_DIR)" && find ./ > "$(GO_HOST_STAGING_DIR).files"
  119. $(call locked, \
  120. mv "$(GO_HOST_STAGING_DIR).files" "$(GO_HOST_BIN_STAGING_FILES_LIST)" && \
  121. $(CP) "$(GO_HOST_STAGING_DIR)"/* "$(1)/", \
  122. host-staging-dir \
  123. )
  124. rm -rf "$(GO_HOST_STAGING_DIR)"
  125. endef
  126. define GoHost/Host/Install/Src
  127. rm -rf "$(GO_HOST_STAGING_DIR)"
  128. mkdir -p "$(GO_HOST_STAGING_DIR)" "$(GO_HOST_STAGING_FILES_LIST_DIR)"
  129. $(GO_GENERAL_BUILD_CONFIG_VARS) \
  130. $(GO_HOST_BUILD_CONFIG_VARS) \
  131. $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(GO_HOST_STAGING_DIR)"
  132. if [ -f "$(GO_HOST_SRC_STAGING_FILES_LIST)" ]; then \
  133. "$(SCRIPT_DIR)/clean-package.sh" \
  134. "$(GO_HOST_SRC_STAGING_FILES_LIST)" \
  135. "$(1)" ; \
  136. fi
  137. cd "$(GO_HOST_STAGING_DIR)" && find ./ > "$(GO_HOST_STAGING_DIR).files"
  138. $(call locked, \
  139. mv "$(GO_HOST_STAGING_DIR).files" "$(GO_HOST_SRC_STAGING_FILES_LIST)" && \
  140. $(CP) "$(GO_HOST_STAGING_DIR)"/* "$(1)/", \
  141. host-staging-dir \
  142. )
  143. rm -rf "$(GO_HOST_STAGING_DIR)"
  144. endef
  145. define GoHost/Host/Install
  146. $(if $(filter $(GO_HOST_SOURCE_ONLY),1),, \
  147. $(call GoHost/Host/Install/Bin,$(1)) \
  148. )
  149. $(call GoHost/Host/Install/Src,$(1))
  150. endef
  151. define GoHost/Host/Uninstall
  152. if [ -f "$(GO_HOST_BIN_STAGING_FILES_LIST)" ]; then \
  153. "$(SCRIPT_DIR)/clean-package.sh" \
  154. "$(GO_HOST_BIN_STAGING_FILES_LIST)" \
  155. "$(HOST_BUILD_PREFIX)" ; \
  156. rm -f "$(GO_HOST_BIN_STAGING_FILES_LIST)" ; \
  157. fi
  158. if [ -f "$(GO_HOST_SRC_STAGING_FILES_LIST)" ]; then \
  159. "$(SCRIPT_DIR)/clean-package.sh" \
  160. "$(GO_HOST_SRC_STAGING_FILES_LIST)" \
  161. "$(HOST_BUILD_PREFIX)" ; \
  162. rm -f "$(GO_HOST_SRC_STAGING_FILES_LIST)" ; \
  163. fi
  164. endef
  165. ifneq ($(strip $(GO_PKG)),)
  166. Host/Configure=$(call GoHost/Host/Configure)
  167. Host/Compile=$(call GoHost/Host/Compile)
  168. Hooks/HostCompile/Post+=Go/CacheCleanup
  169. Host/Uninstall=$(call GoHost/Host/Uninstall,$(1))
  170. endif
  171. define GoHostBuild
  172. Host/Install=$$(call GoHost/Host/Install,$$(1))
  173. endef
  174. define GoBinHostBuild
  175. Host/Install=$$(call GoHost/Host/Install/Bin,$$(1))
  176. endef
  177. define GoSrcHostBuild
  178. Host/Install=$$(call GoHost/Host/Install/Src,$$(1))
  179. endef