b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 | |
| 8 | ifeq ($(origin GO_INCLUDE_DIR),undefined) |
| 9 | GO_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST))) |
| 10 | endif |
| 11 | |
| 12 | include $(GO_INCLUDE_DIR)/golang-values.mk |
| 13 | |
| 14 | |
| 15 | # Variables (all optional, except GO_PKG) to be set in package |
| 16 | # Makefiles: |
| 17 | # |
| 18 | # GO_PKG (required) - name of Go package |
| 19 | # |
| 20 | # Go name of the package. |
| 21 | # |
| 22 | # e.g. GO_PKG:=golang.org/x/text |
| 23 | # |
| 24 | # |
| 25 | # GO_PKG_INSTALL_EXTRA - list of regular expressions, default empty |
| 26 | # |
| 27 | # Additional files/directories to install. By default, only these |
| 28 | # files are installed: |
| 29 | # |
| 30 | # * Files with one of these extensions: |
| 31 | # .go, .c, .cc, .cpp, .h, .hh, .hpp, .proto, .s |
| 32 | # |
| 33 | # * Files in any 'testdata' directory |
| 34 | # |
| 35 | # * go.mod and go.sum, in any directory |
| 36 | # |
| 37 | # e.g. GO_PKG_INSTALL_EXTRA:=example.toml marshal_test.toml |
| 38 | # |
| 39 | # |
| 40 | # GO_PKG_INSTALL_ALL - boolean (0 or 1), default false |
| 41 | # |
| 42 | # If true, install all files regardless of extension or directory. |
| 43 | # |
| 44 | # e.g. GO_PKG_INSTALL_ALL:=1 |
| 45 | # |
| 46 | # |
| 47 | # GO_PKG_SOURCE_ONLY - boolean (0 or 1), default false |
| 48 | # |
| 49 | # If true, 'go install' will not be called. If the package does not |
| 50 | # (or should not) build any binaries, then specifying this option will |
| 51 | # save build time. |
| 52 | # |
| 53 | # e.g. GO_PKG_SOURCE_ONLY:=1 |
| 54 | # |
| 55 | # |
| 56 | # GO_PKG_BUILD_PKG - list of build targets, default GO_PKG/... |
| 57 | # |
| 58 | # Build targets for compiling this Go package, i.e. arguments passed |
| 59 | # to 'go install'. |
| 60 | # |
| 61 | # e.g. GO_PKG_BUILD_PKG:=github.com/debian/ratt/cmd/... |
| 62 | # |
| 63 | # |
| 64 | # GO_PKG_EXCLUDES - list of regular expressions, default empty |
| 65 | # |
| 66 | # Patterns to exclude from the build targets expanded from |
| 67 | # GO_PKG_BUILD_PKG. |
| 68 | # |
| 69 | # e.g. GO_PKG_EXCLUDES:=examples/ |
| 70 | # |
| 71 | # |
| 72 | # GO_PKG_GO_GENERATE - boolean (0 or 1), default false |
| 73 | # |
| 74 | # If true, 'go generate' will be called on all build targets (as |
| 75 | # determined by GO_PKG_BUILD_PKG and GO_PKG_EXCLUDES). This is usually |
| 76 | # not necessary. |
| 77 | # |
| 78 | # e.g. GO_PKG_GO_GENERATE:=1 |
| 79 | # |
| 80 | # |
| 81 | # GO_PKG_GCFLAGS - list of options, default empty |
| 82 | # |
| 83 | # Additional go tool compile options to use when building targets. |
| 84 | # |
| 85 | # e.g. GO_PKG_GCFLAGS:=-N -l |
| 86 | # |
| 87 | # |
| 88 | # GO_PKG_LDFLAGS - list of options, default empty |
| 89 | # |
| 90 | # Additional go tool link options to use when building targets. |
| 91 | # |
| 92 | # Note that the OpenWrt build system has an option to strip binaries |
| 93 | # (enabled by default), so -s (Omit the symbol table and debug |
| 94 | # information) and -w (Omit the DWARF symbol table) flags are not |
| 95 | # necessary. |
| 96 | # |
| 97 | # e.g. GO_PKG_LDFLAGS:=-r dir1:dir2 -u |
| 98 | # |
| 99 | # |
| 100 | # GO_PKG_LDFLAGS_X - list of string variable definitions, default empty |
| 101 | # |
| 102 | # Each definition will be passed as the parameter to the -X go tool |
| 103 | # link option, i.e. -ldflags "-X importpath.name=value". |
| 104 | # |
| 105 | # e.g. GO_PKG_LDFLAGS_X:=main.Version=$(PKG_VERSION) main.BuildStamp=$(SOURCE_DATE_EPOCH) |
| 106 | # |
| 107 | # |
| 108 | # GO_PKG_TAGS - list of build tags, default empty |
| 109 | # |
| 110 | # Build tags to consider satisfied during the build, passed as the |
| 111 | # parameter to the -tags option for 'go install'. |
| 112 | # |
| 113 | # e.g. GO_PKG_TAGS:=release,noupgrade |
| 114 | # |
| 115 | # |
| 116 | # GO_PKG_INSTALL_BIN_PATH - target directory path, default /usr/bin |
| 117 | # |
| 118 | # Directory path under "dest_dir" where binaries will be installed by |
| 119 | # '$(call GoPackage/Package/Install/Bin,dest_dir)'. |
| 120 | # |
| 121 | # e.g. GO_PKG_INSTALL_BIN_PATH:=/sbin |
| 122 | |
| 123 | # Credit for this package build process (GoPackage/Build/Configure and |
| 124 | # GoPackage/Build/Compile) belong to Debian's dh-golang completely. |
| 125 | # https://salsa.debian.org/go-team/packages/dh-golang |
| 126 | |
| 127 | |
| 128 | GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/... |
| 129 | GO_PKG_INSTALL_BIN_PATH?=/usr/bin |
| 130 | |
| 131 | GO_PKG_WORK_DIR_NAME:=.go_work |
| 132 | GO_PKG_BUILD_DIR=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME)/build |
| 133 | GO_PKG_BUILD_BIN_DIR=$(GO_PKG_BUILD_DIR)/bin$(if $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS_ARCH)) |
| 134 | |
| 135 | GO_PKG_BUILD_DEPENDS_PATH:=/usr/share/gocode |
| 136 | GO_PKG_BUILD_DEPENDS_SRC=$(STAGING_DIR)$(GO_PKG_BUILD_DEPENDS_PATH)/src |
| 137 | |
| 138 | ifdef CONFIG_PKG_ASLR_PIE_ALL |
| 139 | ifeq ($(strip $(PKG_ASLR_PIE)),1) |
| 140 | ifeq ($(GO_TARGET_PIE_SUPPORTED),1) |
| 141 | GO_PKG_ENABLE_PIE:=1 |
| 142 | else |
| 143 | $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH)) |
| 144 | endif |
| 145 | endif |
| 146 | endif |
| 147 | |
| 148 | ifdef CONFIG_PKG_ASLR_PIE_REGULAR |
| 149 | ifeq ($(strip $(PKG_ASLR_PIE_REGULAR)),1) |
| 150 | ifeq ($(GO_TARGET_PIE_SUPPORTED),1) |
| 151 | GO_PKG_ENABLE_PIE:=1 |
| 152 | else |
| 153 | $(warning PIE buildmode is not supported for $(GO_OS)/$(GO_ARCH)) |
| 154 | endif |
| 155 | endif |
| 156 | endif |
| 157 | |
| 158 | ifdef CONFIG_GOLANG_SPECTRE |
| 159 | ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1) |
| 160 | GO_PKG_ENABLE_SPECTRE:=1 |
| 161 | else |
| 162 | $(warning Spectre mitigations are not supported for $(GO_ARCH)) |
| 163 | endif |
| 164 | endif |
| 165 | |
| 166 | # sstrip causes corrupted section header size |
| 167 | ifneq ($(CONFIG_USE_SSTRIP),) |
| 168 | ifneq ($(CONFIG_DEBUG),) |
| 169 | GO_PKG_STRIP_ARGS:=--strip-unneeded --remove-section=.comment --remove-section=.note |
| 170 | else |
| 171 | GO_PKG_STRIP_ARGS:=--strip-all |
| 172 | endif |
| 173 | STRIP:=$(TARGET_CROSS)strip $(GO_PKG_STRIP_ARGS) |
| 174 | endif |
| 175 | |
| 176 | define GoPackage/GoSubMenu |
| 177 | SUBMENU:=Go |
| 178 | SECTION:=lang |
| 179 | CATEGORY:=Languages |
| 180 | endef |
| 181 | |
| 182 | GO_PKG_BUILD_CONFIG_VARS= \ |
| 183 | GO_PKG="$(strip $(GO_PKG))" \ |
| 184 | GO_INSTALL_EXTRA="$(strip $(GO_PKG_INSTALL_EXTRA))" \ |
| 185 | GO_INSTALL_ALL="$(strip $(GO_PKG_INSTALL_ALL))" \ |
| 186 | GO_SOURCE_ONLY="$(strip $(GO_PKG_SOURCE_ONLY))" \ |
| 187 | GO_BUILD_PKG="$(strip $(GO_PKG_BUILD_PKG))" \ |
| 188 | GO_EXCLUDES="$(strip $(GO_PKG_EXCLUDES))" \ |
| 189 | GO_GO_GENERATE="$(strip $(GO_PKG_GO_GENERATE))" \ |
| 190 | GO_INSTALL_BIN_PATH="$(strip $(GO_PKG_INSTALL_BIN_PATH))" \ |
| 191 | BUILD_DIR="$(PKG_BUILD_DIR)" \ |
| 192 | GO_BUILD_DIR="$(GO_PKG_BUILD_DIR)" \ |
| 193 | GO_BUILD_BIN_DIR="$(GO_PKG_BUILD_BIN_DIR)" \ |
| 194 | GO_BUILD_DEPENDS_PATH="$(GO_PKG_BUILD_DEPENDS_PATH)" \ |
| 195 | GO_BUILD_DEPENDS_SRC="$(GO_PKG_BUILD_DEPENDS_SRC)" |
| 196 | |
| 197 | GO_PKG_TARGET_VARS= \ |
| 198 | GOOS="$(GO_OS)" \ |
| 199 | GOARCH="$(GO_ARCH)" \ |
| 200 | GO386="$(GO_386)" \ |
| 201 | GOARM="$(GO_ARM)" \ |
| 202 | GOMIPS="$(GO_MIPS)" \ |
| 203 | GOMIPS64="$(GO_MIPS64)" \ |
| 204 | CGO_ENABLED=1 \ |
| 205 | CC="$(TARGET_CC)" \ |
| 206 | CXX="$(TARGET_CXX)" \ |
| 207 | CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \ |
| 208 | CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \ |
| 209 | CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \ |
| 210 | CGO_LDFLAGS="$(TARGET_LDFLAGS)" |
| 211 | |
| 212 | GO_PKG_BUILD_VARS= \ |
| 213 | GOPATH="$(GO_PKG_BUILD_DIR)" \ |
| 214 | GOCACHE="$(GO_BUILD_CACHE_DIR)" \ |
| 215 | GOMODCACHE="$(GO_MOD_CACHE_DIR)" \ |
| 216 | GOENV=off |
| 217 | |
| 218 | GO_PKG_VARS= \ |
| 219 | $(GO_PKG_TARGET_VARS) \ |
| 220 | $(GO_PKG_BUILD_VARS) |
| 221 | |
| 222 | GO_PKG_DEFAULT_GCFLAGS= \ |
| 223 | $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all) |
| 224 | |
| 225 | GO_PKG_DEFAULT_ASMFLAGS= \ |
| 226 | $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all) |
| 227 | |
| 228 | GO_PKG_DEFAULT_LDFLAGS= \ |
| 229 | -buildid '$(SOURCE_DATE_EPOCH)' \ |
| 230 | -linkmode external \ |
| 231 | -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))' |
| 232 | |
| 233 | GO_PKG_CUSTOM_LDFLAGS= \ |
| 234 | $(GO_PKG_LDFLAGS) \ |
| 235 | $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X)) |
| 236 | |
| 237 | GO_PKG_INSTALL_ARGS= \ |
| 238 | -v \ |
| 239 | -trimpath \ |
| 240 | -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \ |
| 241 | $(if $(GO_PKG_DEFAULT_GCFLAGS),-gcflags "all=$(GO_PKG_DEFAULT_GCFLAGS)") \ |
| 242 | $(if $(GO_PKG_DEFAULT_ASMFLAGS),-asmflags "all=$(GO_PKG_DEFAULT_ASMFLAGS)") \ |
| 243 | $(if $(filter $(GO_PKG_ENABLE_PIE),1),-buildmode pie) \ |
| 244 | $(if $(filter $(GO_ARCH),arm),-installsuffix "v$(GO_ARM)") \ |
| 245 | $(if $(filter $(GO_ARCH),mips mipsle),-installsuffix "$(GO_MIPS)") \ |
| 246 | $(if $(filter $(GO_ARCH),mips64 mips64le),-installsuffix "$(GO_MIPS64)") \ |
| 247 | $(if $(GO_PKG_GCFLAGS),-gcflags "$(GO_PKG_GCFLAGS) $(GO_PKG_DEFAULT_GCFLAGS)") \ |
| 248 | $(if $(GO_PKG_CUSTOM_LDFLAGS),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)") \ |
| 249 | $(if $(GO_PKG_TAGS),-tags "$(GO_PKG_TAGS)") |
| 250 | |
| 251 | define GoPackage/Build/Configure |
| 252 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ |
| 253 | $(GO_PKG_BUILD_CONFIG_VARS) \ |
| 254 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure |
| 255 | endef |
| 256 | |
| 257 | # $(1) additional arguments for go command line (optional) |
| 258 | define GoPackage/Build/Compile |
| 259 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ |
| 260 | $(GO_PKG_BUILD_CONFIG_VARS) \ |
| 261 | $(GO_PKG_VARS) \ |
| 262 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_PKG_INSTALL_ARGS) $(1) |
| 263 | endef |
| 264 | |
| 265 | define GoPackage/Build/InstallDev |
| 266 | $(call GoPackage/Package/Install/Src,$(1)) |
| 267 | endef |
| 268 | |
| 269 | define GoPackage/Package/Install/Bin |
| 270 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ |
| 271 | $(GO_PKG_BUILD_CONFIG_VARS) \ |
| 272 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(1)" |
| 273 | endef |
| 274 | |
| 275 | define GoPackage/Package/Install/Src |
| 276 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ |
| 277 | $(GO_PKG_BUILD_CONFIG_VARS) \ |
| 278 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(1)" |
| 279 | endef |
| 280 | |
| 281 | define GoPackage/Package/Install |
| 282 | $(if $(filter $(GO_PKG_SOURCE_ONLY),1),, \ |
| 283 | $(call GoPackage/Package/Install/Bin,$(1)) \ |
| 284 | ) |
| 285 | $(call GoPackage/Package/Install/Src,$(1)) |
| 286 | endef |
| 287 | |
| 288 | |
| 289 | ifneq ($(strip $(GO_PKG)),) |
| 290 | ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1) |
| 291 | PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE |
| 292 | endif |
| 293 | |
| 294 | Build/Configure=$(call GoPackage/Build/Configure) |
| 295 | Build/Compile=$(call GoPackage/Build/Compile) |
| 296 | Hooks/Compile/Post+=Go/CacheCleanup |
| 297 | Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1)) |
| 298 | endif |
| 299 | |
| 300 | define GoPackage |
| 301 | ifndef Package/$(1)/install |
| 302 | Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1)) |
| 303 | endif |
| 304 | endef |
| 305 | |
| 306 | define GoBinPackage |
| 307 | ifndef Package/$(1)/install |
| 308 | Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1)) |
| 309 | endif |
| 310 | endef |
| 311 | |
| 312 | define GoSrcPackage |
| 313 | ifndef Package/$(1)/install |
| 314 | Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1)) |
| 315 | endif |
| 316 | endef |
| 317 | |
| 318 | |
| 319 | # Deprecated variables - these will be removed after the next OpenWrt release |
| 320 | GO_PKG_PATH=$(GO_PKG_BUILD_DEPENDS_PATH) |
| 321 | GO_PKG_WORK_DIR=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME) |
| 322 | GO_PKG_CACHE_DIR=$(GO_BUILD_CACHE_DIR) |
| 323 | GO_PKG_DEFAULT_VARS=$(GO_PKG_VARS) |
| 324 | GoPackage/Environment=$(GO_PKG_VARS) |
| 325 | GoPackage/is_dir_not_empty=$$$$($(FIND) "$(1)" -maxdepth 0 -type d \! -empty 2>/dev/null) |
| 326 | GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR)) |
| 327 | # End of deprecated variables |