├── README.md ├── golang-build.sh ├── golang-compiler.mk ├── golang-host-build.mk ├── golang-package.mk ├── golang-values.mk └── golang ├── Config.in ├── Makefile └── files └── go-gcc-helper /README.md: -------------------------------------------------------------------------------- 1 | # OpenWrt golang latest version 2 | 3 | ## How to use? 4 | 5 | After the `./scripts/feeds install -a` operation is completed, execute the following command: 6 | 7 | ```shell 8 | rm -rf feeds/packages/lang/golang 9 | git clone https://github.com/sbwml/packages_lang_golang -b 24.x feeds/packages/lang/golang 10 | ``` 11 | -------------------------------------------------------------------------------- /golang-build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (C) 2020, 2022 Jeffery To 4 | # 5 | # This is free software, licensed under the GNU General Public License v2. 6 | # See /LICENSE for more information. 7 | # 8 | 9 | nl=" 10 | " 11 | 12 | log() { 13 | # shellcheck disable=SC2039 14 | local IFS=" " 15 | printf '%s\n' "$*" 16 | } 17 | 18 | log_error() { 19 | # shellcheck disable=SC2039 20 | local IFS=" " 21 | printf 'Error: %s\n' "$*" >&2 22 | } 23 | 24 | link_contents() { 25 | # shellcheck disable=SC2039 26 | local src="$1" dest="$2" IFS="$nl" dirs dir base 27 | 28 | if [ -n "$(find "$src" -mindepth 1 -maxdepth 1 -name "*.go" -not -type d)" ]; then 29 | log_error "$src is already a Go library" 30 | return 1 31 | fi 32 | 33 | dirs="$(find "$src" -mindepth 1 -maxdepth 1 -type d)" 34 | for dir in $dirs; do 35 | base="${dir##*/}" 36 | if [ -d "$dest/$base" ]; then 37 | case "$dir" in 38 | *$GO_BUILD_DEPENDS_SRC/$GO_PKG) 39 | log "$GO_PKG is already installed. Please check for circular dependencies." 40 | ;; 41 | *) 42 | link_contents "$src/$base" "$dest/$base" 43 | ;; 44 | esac 45 | else 46 | log "...${src#$GO_BUILD_DEPENDS_SRC}/$base" 47 | ln -sf "$src/$base" "$dest/$base" 48 | fi 49 | done 50 | 51 | return 0 52 | } 53 | 54 | configure() { 55 | # shellcheck disable=SC2039 56 | local files code testdata gomod pattern extra IFS file dest 57 | 58 | cd "$BUILD_DIR" || return 1 59 | 60 | files="$(find ./ -path "*/.*" -prune -o -not -type d -print)" 61 | 62 | if [ "$GO_INSTALL_ALL" != 1 ]; then 63 | code="$(printf '%s\n' "$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$')" 64 | testdata="$(printf '%s\n' "$files" | grep '/testdata/')" 65 | gomod="$(printf '%s\n' "$files" | grep '/go\.\(mod\|sum\|work\)$')" 66 | 67 | for pattern in $GO_INSTALL_EXTRA; do 68 | extra="$(printf '%s\n' "$extra"; printf '%s\n' "$files" | grep -e "$pattern")" 69 | done 70 | 71 | files="$(printf '%s\n%s\n%s\n%s\n' "$code" "$testdata" "$gomod" "$extra" | grep -v '^[[:space:]]*$' | sort -u)" 72 | fi 73 | 74 | IFS="$nl" 75 | 76 | log "Copying files from $BUILD_DIR into $GO_BUILD_DIR/src/$GO_PKG" 77 | mkdir -p "$GO_BUILD_DIR/src" 78 | for file in $files; do 79 | log "${file#./}" 80 | dest="$GO_BUILD_DIR/src/$GO_PKG/${file#./}" 81 | mkdir -p "${dest%/*}" 82 | cp -fpR "$file" "$dest" 83 | done 84 | log 85 | 86 | if [ "$GO_SOURCE_ONLY" != 1 ]; then 87 | if [ -d "$GO_BUILD_DEPENDS_SRC" ]; then 88 | log "Symlinking directories from $GO_BUILD_DEPENDS_SRC into $GO_BUILD_DIR/src" 89 | link_contents "$GO_BUILD_DEPENDS_SRC" "$GO_BUILD_DIR/src" 90 | else 91 | log "$GO_BUILD_DEPENDS_SRC does not exist, skipping symlinks" 92 | fi 93 | else 94 | log "Not building binaries, skipping symlinks" 95 | fi 96 | log 97 | 98 | return 0 99 | } 100 | 101 | build() { 102 | # shellcheck disable=SC2039 103 | local modargs pattern targets retval 104 | 105 | cd "$GO_BUILD_DIR" || return 1 106 | 107 | if [ -f "$BUILD_DIR/go.mod" ] ; then 108 | mkdir -p "$GO_MOD_CACHE_DIR" 109 | modargs="$GO_MOD_ARGS" 110 | fi 111 | 112 | log "Finding targets" 113 | # shellcheck disable=SC2086 114 | targets="$(go list $modargs $GO_BUILD_PKG)" 115 | for pattern in $GO_EXCLUDES; do 116 | targets="$(printf '%s\n' "$targets" | grep -v "$pattern")" 117 | done 118 | log 119 | 120 | if [ "$GO_GO_GENERATE" = 1 ]; then 121 | log "Calling go generate" 122 | # shellcheck disable=SC2086 123 | GOOS='' GOARCH='' GO386='' GOARM='' GOARM64='' GOMIPS='' GOMIPS64='' GORISCV64=''\ 124 | go generate -v $targets 125 | log 126 | fi 127 | 128 | if [ "$GO_SOURCE_ONLY" = 1 ]; then 129 | return 0 130 | fi 131 | 132 | log "Building targets" 133 | mkdir -p "$GO_BUILD_DIR/bin" "$GO_BUILD_CACHE_DIR" 134 | # shellcheck disable=SC2086 135 | go install $modargs "$@" $targets 136 | retval="$?" 137 | log 138 | 139 | if [ "$retval" -eq 0 ] && [ -z "$(find "$GO_BUILD_BIN_DIR" -maxdepth 0 -type d -not -empty 2>/dev/null)" ]; then 140 | log_error "No binaries were built" 141 | retval=1 142 | fi 143 | 144 | if [ "$retval" -ne 0 ]; then 145 | cache_cleanup 146 | fi 147 | 148 | return "$retval" 149 | } 150 | 151 | install_bin() { 152 | # shellcheck disable=SC2039 153 | local dest="$1" 154 | install -d -m0755 "$dest/$GO_INSTALL_BIN_PATH" 155 | install -m0755 "$GO_BUILD_BIN_DIR"/* "$dest/$GO_INSTALL_BIN_PATH/" 156 | } 157 | 158 | install_src() { 159 | # shellcheck disable=SC2039 160 | local dest="$1" dir="${GO_PKG%/*}" 161 | install -d -m0755 "$dest/$GO_BUILD_DEPENDS_PATH/src/$dir" 162 | cp -fpR "$GO_BUILD_DIR/src/$GO_PKG" "$dest/$GO_BUILD_DEPENDS_PATH/src/$dir/" 163 | } 164 | 165 | cache_cleanup() { 166 | if ! [ -d "$GO_MOD_CACHE_DIR" ]; then 167 | return 0 168 | fi 169 | 170 | # in case go is called without -modcacherw 171 | find "$GO_MOD_CACHE_DIR" -type d -not -perm -u+w -exec chmod u+w '{}' + 172 | 173 | if [ -n "$CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE" ]; then 174 | find "$GO_MOD_CACHE_DIR" -type d -not -perm -go+rx -exec chmod go+rx '{}' + 175 | find "$GO_MOD_CACHE_DIR" -not -type d -not -perm -go+r -exec chmod go+r '{}' + 176 | fi 177 | 178 | return 0 179 | } 180 | 181 | 182 | if [ "$#" -lt 1 ]; then 183 | log_error "Missing command" 184 | exit 1 185 | fi 186 | 187 | command="$1" 188 | shift 1 189 | 190 | case "$command" in 191 | configure) 192 | configure 193 | ;; 194 | build) 195 | build "$@" 196 | ;; 197 | install_bin) 198 | install_bin "$@" 199 | ;; 200 | install_src) 201 | install_src "$@" 202 | ;; 203 | cache_cleanup) 204 | cache_cleanup 205 | ;; 206 | *) 207 | log_error "Invalid command \"$command\"" 208 | exit 1 209 | ;; 210 | esac 211 | -------------------------------------------------------------------------------- /golang-compiler.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2018, 2020-2021, 2023 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 | # $(1) valid GOOS_GOARCH combinations 16 | # $(2) go version id 17 | define GoCompiler/Default/CheckHost 18 | $(if $(filter $(GO_HOST_OS_ARCH),$(1)),,$(error go-$(2) cannot be installed on $(GO_HOST_OS)/$(GO_HOST_ARCH))) 19 | endef 20 | 21 | # $(1) source go root 22 | # $(2) additional environment variables (optional) 23 | define GoCompiler/Default/Make 24 | ( \ 25 | cd "$(1)/src" ; \ 26 | $(2) \ 27 | $(BASH) make.bash \ 28 | $(if $(findstring s,$(OPENWRT_VERBOSE)),-v) \ 29 | --no-banner \ 30 | ; \ 31 | ) 32 | endef 33 | 34 | # $(1) destination prefix 35 | # $(2) go version id 36 | define GoCompiler/Default/Install/make-dirs 37 | $(INSTALL_DIR) "$(1)/lib/go-$(2)" 38 | $(INSTALL_DIR) "$(1)/share/go-$(2)" 39 | endef 40 | 41 | # $(1) source go root 42 | # $(2) destination prefix 43 | # $(3) go version id 44 | # $(4) file/directory name 45 | define GoCompiler/Default/Install/install-share-data 46 | $(CP) "$(1)/$(4)" "$(2)/share/go-$(3)/" 47 | $(LN) "../../share/go-$(3)/$(4)" "$(2)/lib/go-$(3)/" 48 | endef 49 | 50 | # $(1) source go root 51 | # $(2) destination prefix 52 | # $(3) go version id 53 | # $(4) GOOS_GOARCH 54 | # $(5) install suffix (optional) 55 | define GoCompiler/Default/Install/Bin 56 | $(call GoCompiler/Default/Install/make-dirs,$(2),$(3)) 57 | 58 | $(call GoCompiler/Default/Install/install-share-data,$(1),$(2),$(3),api) 59 | 60 | $(INSTALL_DATA) -p "$(1)/go.env" "$(2)/lib/go-$(3)/" 61 | $(INSTALL_DATA) -p "$(1)/VERSION" "$(2)/lib/go-$(3)/" 62 | 63 | for file in CONTRIBUTING.md LICENSE PATENTS README.md SECURITY.md; do \ 64 | if [ -f "$(1)/$$$$file" ]; then \ 65 | $(INSTALL_DATA) -p "$(1)/$$$$file" "$(2)/share/go-$(3)/" ; \ 66 | fi ; \ 67 | done 68 | 69 | $(INSTALL_DIR) "$(2)/lib/go-$(3)/bin" 70 | 71 | ifeq ($(4),$(GO_HOST_OS_ARCH)) 72 | $(INSTALL_BIN) -p "$(1)/bin"/* "$(2)/lib/go-$(3)/bin/" 73 | else 74 | $(INSTALL_BIN) -p "$(1)/bin/$(4)"/* "$(2)/lib/go-$(3)/bin/" 75 | endif 76 | 77 | if [ -d "$(1)/pkg/$(4)$(if $(5),_$(5))" ]; then \ 78 | $(INSTALL_DIR) "$(2)/lib/go-$(3)/pkg" ; \ 79 | $(CP) "$(1)/pkg/$(4)$(if $(5),_$(5))" "$(2)/lib/go-$(3)/pkg/" ; \ 80 | fi 81 | 82 | $(INSTALL_DIR) "$(2)/lib/go-$(3)/pkg/tool/$(4)" 83 | $(INSTALL_BIN) -p "$(1)/pkg/tool/$(4)"/* "$(2)/lib/go-$(3)/pkg/tool/$(4)/" 84 | endef 85 | 86 | # $(1) destination prefix 87 | # $(2) go version id 88 | define GoCompiler/Default/Install/BinLinks 89 | $(INSTALL_DIR) "$(1)/bin" 90 | $(LN) "../lib/go-$(2)/bin/go" "$(1)/bin/go" 91 | $(LN) "../lib/go-$(2)/bin/gofmt" "$(1)/bin/gofmt" 92 | endef 93 | 94 | # $(1) source go root 95 | # $(2) destination prefix 96 | # $(3) go version id 97 | define GoCompiler/Default/Install/Doc 98 | $(call GoCompiler/Default/Install/make-dirs,$(2),$(3)) 99 | 100 | $(call GoCompiler/Default/Install/install-share-data,$(1),$(2),$(3),doc) 101 | endef 102 | 103 | # $(1) source go root 104 | # $(2) destination prefix 105 | # $(3) go version id 106 | define GoCompiler/Default/Install/Src 107 | $(call GoCompiler/Default/Install/make-dirs,$(2),$(3)) 108 | 109 | $(call GoCompiler/Default/Install/install-share-data,$(1),$(2),$(3),lib) 110 | $(call GoCompiler/Default/Install/install-share-data,$(1),$(2),$(3),misc) 111 | $(call GoCompiler/Default/Install/install-share-data,$(1),$(2),$(3),src) 112 | $(call GoCompiler/Default/Install/install-share-data,$(1),$(2),$(3),test) 113 | 114 | $(FIND) \ 115 | "$(2)/share/go-$(3)/src/" \ 116 | \! -type d -a \( -name "*.bat" -o -name "*.rc" \) \ 117 | -delete 118 | 119 | if [ -d "$(1)/pkg/include" ]; then \ 120 | $(INSTALL_DIR) "$(2)/lib/go-$(3)/pkg" ; \ 121 | $(INSTALL_DIR) "$(2)/share/go-$(3)/pkg" ; \ 122 | $(CP) "$(1)/pkg/include" "$(2)/share/go-$(3)/pkg/" ; \ 123 | $(LN) "../../../share/go-$(3)/pkg/include" "$(2)/lib/go-$(3)/pkg/" ; \ 124 | fi 125 | endef 126 | 127 | # $(1) destination prefix 128 | # $(2) go version id 129 | define GoCompiler/Default/Uninstall 130 | rm -rf "$(1)/lib/go-$(2)" 131 | rm -rf "$(1)/share/go-$(2)" 132 | endef 133 | 134 | # $(1) destination prefix 135 | define GoCompiler/Default/Uninstall/BinLinks 136 | rm -f "$(1)/bin/go" 137 | rm -f "$(1)/bin/gofmt" 138 | endef 139 | 140 | 141 | # $(1) profile name 142 | # $(2) source go root 143 | # $(3) destination prefix 144 | # $(4) go version id 145 | # $(5) GOOS_GOARCH 146 | # $(6) install suffix (optional) 147 | define GoCompiler/AddProfile 148 | 149 | # $$(1) valid GOOS_GOARCH combinations 150 | define GoCompiler/$(1)/CheckHost 151 | $$(call GoCompiler/Default/CheckHost,$$(1),$(4)) 152 | endef 153 | 154 | # $$(1) additional environment variables (optional) 155 | define GoCompiler/$(1)/Make 156 | $$(call GoCompiler/Default/Make,$(2),$$(1)) 157 | endef 158 | 159 | # $$(1) override install prefix (optional) 160 | define GoCompiler/$(1)/Install/Bin 161 | $$(call GoCompiler/Default/Install/Bin,$(2),$$(or $$(1),$(3)),$(4),$(5),$(6)) 162 | endef 163 | 164 | # $$(1) override install prefix (optional) 165 | define GoCompiler/$(1)/Install/BinLinks 166 | $$(call GoCompiler/Default/Install/BinLinks,$$(or $$(1),$(3)),$(4)) 167 | endef 168 | 169 | # $$(1) override install prefix (optional) 170 | define GoCompiler/$(1)/Install/Doc 171 | $$(call GoCompiler/Default/Install/Doc,$(2),$$(or $$(1),$(3)),$(4)) 172 | endef 173 | 174 | # $$(1) override install prefix (optional) 175 | define GoCompiler/$(1)/Install/Src 176 | $$(call GoCompiler/Default/Install/Src,$(2),$$(or $$(1),$(3)),$(4)) 177 | endef 178 | 179 | # $$(1) override install prefix (optional) 180 | define GoCompiler/$(1)/Uninstall 181 | $$(call GoCompiler/Default/Uninstall,$$(or $$(1),$(3)),$(4)) 182 | endef 183 | 184 | # $$(1) override install prefix (optional) 185 | define GoCompiler/$(1)/Uninstall/BinLinks 186 | $$(call GoCompiler/Default/Uninstall/BinLinks,$$(or $$(1),$(3))) 187 | endef 188 | 189 | endef 190 | -------------------------------------------------------------------------------- /golang-host-build.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2020, 2022 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 | # these variables have the same meanings as in golang-package.mk 16 | GO_HOST_INSTALL_EXTRA?=$(GO_PKG_INSTALL_EXTRA) 17 | GO_HOST_INSTALL_ALL?=$(GO_PKG_INSTALL_ALL) 18 | GO_HOST_SOURCE_ONLY?=$(GO_PKG_SOURCE_ONLY) 19 | GO_HOST_BUILD_PKG?=$(GO_PKG_BUILD_PKG) 20 | GO_HOST_EXCLUDES?=$(GO_PKG_EXCLUDES) 21 | GO_HOST_GO_GENERATE?=$(GO_PKG_GO_GENERATE) 22 | GO_HOST_GCFLAGS?=$(GO_PKG_GCFLAGS) 23 | GO_HOST_LDFLAGS?=$(GO_PKG_LDFLAGS) 24 | GO_HOST_LDFLAGS_X?=$(GO_PKG_LDFLAGS_X) 25 | GO_HOST_TAGS?=$(GO_PKG_TAGS) 26 | GO_HOST_INSTALL_BIN_PATH?=/bin 27 | 28 | 29 | # need to repeat this here in case golang-package.mk is not included 30 | GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/... 31 | 32 | GO_HOST_WORK_DIR_NAME:=.go_work 33 | GO_HOST_BUILD_DIR=$(HOST_BUILD_DIR)/$(GO_HOST_WORK_DIR_NAME)/build 34 | GO_HOST_BUILD_BIN_DIR=$(GO_HOST_BUILD_DIR)/bin 35 | 36 | GO_HOST_BUILD_DEPENDS_PATH:=/share/gocode 37 | GO_HOST_BUILD_DEPENDS_SRC=$(STAGING_DIR_HOSTPKG)$(GO_HOST_BUILD_DEPENDS_PATH)/src 38 | 39 | GO_HOST_DIR_NAME:=$(lastword $(subst /,$(space),$(CURDIR))) 40 | GO_HOST_STAGING_DIR:=$(TMP_DIR)/host-stage-$(GO_HOST_DIR_NAME) 41 | GO_HOST_STAGING_FILES_LIST_DIR:=$(HOST_BUILD_PREFIX)/stamp 42 | GO_HOST_BIN_STAGING_FILES_LIST:=$(GO_HOST_STAGING_FILES_LIST_DIR)/$(GO_HOST_DIR_NAME)-bin.list 43 | GO_HOST_SRC_STAGING_FILES_LIST:=$(GO_HOST_STAGING_FILES_LIST_DIR)/$(GO_HOST_DIR_NAME)-src.list 44 | 45 | ifeq ($(GO_HOST_PIE_SUPPORTED),1) 46 | GO_HOST_ENABLE_PIE:=1 47 | endif 48 | 49 | GO_HOST_BUILD_CONFIG_VARS= \ 50 | GO_PKG="$(strip $(GO_PKG))" \ 51 | GO_INSTALL_EXTRA="$(strip $(GO_HOST_INSTALL_EXTRA))" \ 52 | GO_INSTALL_ALL="$(strip $(GO_HOST_INSTALL_ALL))" \ 53 | GO_SOURCE_ONLY="$(strip $(GO_HOST_SOURCE_ONLY))" \ 54 | GO_BUILD_PKG="$(strip $(GO_HOST_BUILD_PKG))" \ 55 | GO_EXCLUDES="$(strip $(GO_HOST_EXCLUDES))" \ 56 | GO_GO_GENERATE="$(strip $(GO_HOST_GO_GENERATE))" \ 57 | GO_INSTALL_BIN_PATH="$(strip $(GO_HOST_INSTALL_BIN_PATH))" \ 58 | BUILD_DIR="$(HOST_BUILD_DIR)" \ 59 | GO_BUILD_DIR="$(GO_HOST_BUILD_DIR)" \ 60 | GO_BUILD_BIN_DIR="$(GO_HOST_BUILD_BIN_DIR)" \ 61 | GO_BUILD_DEPENDS_PATH="$(GO_HOST_BUILD_DEPENDS_PATH)" \ 62 | GO_BUILD_DEPENDS_SRC="$(GO_HOST_BUILD_DEPENDS_SRC)" 63 | 64 | GO_HOST_MORE_CFLAGS?= \ 65 | -Wformat -Werror=format-security \ 66 | -fstack-protector-strong \ 67 | -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \ 68 | -Wl,-z,now -Wl,-z,relro \ 69 | $(if $(GO_HOST_ENABLE_PIE),$(FPIC)) 70 | 71 | GO_HOST_MORE_LDFLAGS?= \ 72 | -znow -zrelro \ 73 | $(if $(GO_HOST_ENABLE_PIE),$(FPIC) -specs=$(INCLUDE_DIR)/hardened-ld-pie.specs) 74 | 75 | GO_HOST_TARGET_VARS= \ 76 | CGO_ENABLED=1 \ 77 | CC=gcc \ 78 | CXX=g++ \ 79 | PKG_CONFIG=pkg-config \ 80 | CGO_CFLAGS="$(HOST_CFLAGS) $(GO_HOST_MORE_CFLAGS)" \ 81 | CGO_CPPFLAGS="$(HOST_CPPFLAGS) $(GO_HOST_MORE_CPPFLAGS)" \ 82 | CGO_CXXFLAGS="$(HOST_CFLAGS) $(GO_HOST_MORE_CFLAGS)" \ 83 | CGO_LDFLAGS="$(HOST_LDFLAGS) $(GO_HOST_MORE_LDFLAGS)" \ 84 | GO_GCC_HELPER_CC="$(HOSTCC)" \ 85 | GO_GCC_HELPER_CXX="$(HOSTCXX)" \ 86 | GO_GCC_HELPER_PATH="$$$$PATH" \ 87 | PATH="$(STAGING_DIR_HOSTPKG)/lib/go-cross/openwrt:$$$$PATH" 88 | 89 | GO_HOST_BUILD_VARS= \ 90 | GOPATH="$(GO_HOST_BUILD_DIR)" \ 91 | GOCACHE="$(GO_BUILD_CACHE_DIR)" \ 92 | GOMODCACHE="$(GO_MOD_CACHE_DIR)" \ 93 | GOENV=off 94 | 95 | GO_HOST_VARS= \ 96 | $(GO_HOST_TARGET_VARS) \ 97 | $(GO_HOST_BUILD_VARS) 98 | 99 | GO_HOST_DEFAULT_LDFLAGS= \ 100 | -linkmode external \ 101 | -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(HOST_LDFLAGS) $(GO_HOST_MORE_LDFLAGS))' 102 | 103 | GO_HOST_CUSTOM_LDFLAGS= \ 104 | $(GO_HOST_LDFLAGS) \ 105 | $(patsubst %,-X %,$(GO_HOST_LDFLAGS_X)) 106 | 107 | GO_HOST_INSTALL_ARGS= \ 108 | -v \ 109 | -ldflags "all=$(GO_HOST_DEFAULT_LDFLAGS)" \ 110 | $(if $(GO_HOST_ENABLE_PIE),-buildmode pie) \ 111 | $(if $(strip $(GO_HOST_GCFLAGS)),-gcflags "$(GO_HOST_GCFLAGS)") \ 112 | $(if $(strip $(GO_HOST_CUSTOM_LDFLAGS)),-ldflags "$(GO_HOST_CUSTOM_LDFLAGS) $(GO_HOST_DEFAULT_LDFLAGS)") \ 113 | $(if $(strip $(GO_HOST_TAGS)),-tags "$(GO_HOST_TAGS)") 114 | 115 | define GoHost/Host/Configure 116 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 117 | $(GO_HOST_BUILD_CONFIG_VARS) \ 118 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure 119 | endef 120 | 121 | # $(1) additional arguments for go command line (optional) 122 | define GoHost/Host/Compile 123 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 124 | $(GO_HOST_BUILD_CONFIG_VARS) \ 125 | $(GO_HOST_VARS) \ 126 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_HOST_INSTALL_ARGS) $(1) 127 | endef 128 | 129 | define GoHost/Host/Install/Bin 130 | rm -rf "$(GO_HOST_STAGING_DIR)" 131 | mkdir -p "$(GO_HOST_STAGING_DIR)" "$(GO_HOST_STAGING_FILES_LIST_DIR)" 132 | 133 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 134 | $(GO_HOST_BUILD_CONFIG_VARS) \ 135 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(GO_HOST_STAGING_DIR)" 136 | 137 | if [ -f "$(GO_HOST_BIN_STAGING_FILES_LIST)" ]; then \ 138 | "$(SCRIPT_DIR)/clean-package.sh" \ 139 | "$(GO_HOST_BIN_STAGING_FILES_LIST)" \ 140 | "$(1)" ; \ 141 | fi 142 | 143 | cd "$(GO_HOST_STAGING_DIR)" && find ./ > "$(GO_HOST_STAGING_DIR).files" 144 | 145 | $(call locked, \ 146 | mv "$(GO_HOST_STAGING_DIR).files" "$(GO_HOST_BIN_STAGING_FILES_LIST)" && \ 147 | $(CP) "$(GO_HOST_STAGING_DIR)"/* "$(1)/", \ 148 | host-staging-dir \ 149 | ) 150 | 151 | rm -rf "$(GO_HOST_STAGING_DIR)" 152 | endef 153 | 154 | define GoHost/Host/Install/Src 155 | rm -rf "$(GO_HOST_STAGING_DIR)" 156 | mkdir -p "$(GO_HOST_STAGING_DIR)" "$(GO_HOST_STAGING_FILES_LIST_DIR)" 157 | 158 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 159 | $(GO_HOST_BUILD_CONFIG_VARS) \ 160 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(GO_HOST_STAGING_DIR)" 161 | 162 | if [ -f "$(GO_HOST_SRC_STAGING_FILES_LIST)" ]; then \ 163 | "$(SCRIPT_DIR)/clean-package.sh" \ 164 | "$(GO_HOST_SRC_STAGING_FILES_LIST)" \ 165 | "$(1)" ; \ 166 | fi 167 | 168 | cd "$(GO_HOST_STAGING_DIR)" && find ./ > "$(GO_HOST_STAGING_DIR).files" 169 | 170 | $(call locked, \ 171 | mv "$(GO_HOST_STAGING_DIR).files" "$(GO_HOST_SRC_STAGING_FILES_LIST)" && \ 172 | $(CP) "$(GO_HOST_STAGING_DIR)"/* "$(1)/", \ 173 | host-staging-dir \ 174 | ) 175 | 176 | rm -rf "$(GO_HOST_STAGING_DIR)" 177 | endef 178 | 179 | define GoHost/Host/Install 180 | $(if $(filter $(GO_HOST_SOURCE_ONLY),1),, \ 181 | $(call GoHost/Host/Install/Bin,$(1)) \ 182 | ) 183 | $(call GoHost/Host/Install/Src,$(1)) 184 | endef 185 | 186 | define GoHost/Host/Uninstall 187 | if [ -f "$(GO_HOST_BIN_STAGING_FILES_LIST)" ]; then \ 188 | "$(SCRIPT_DIR)/clean-package.sh" \ 189 | "$(GO_HOST_BIN_STAGING_FILES_LIST)" \ 190 | "$(HOST_BUILD_PREFIX)" ; \ 191 | rm -f "$(GO_HOST_BIN_STAGING_FILES_LIST)" ; \ 192 | fi 193 | 194 | if [ -f "$(GO_HOST_SRC_STAGING_FILES_LIST)" ]; then \ 195 | "$(SCRIPT_DIR)/clean-package.sh" \ 196 | "$(GO_HOST_SRC_STAGING_FILES_LIST)" \ 197 | "$(HOST_BUILD_PREFIX)" ; \ 198 | rm -f "$(GO_HOST_SRC_STAGING_FILES_LIST)" ; \ 199 | fi 200 | endef 201 | 202 | 203 | ifneq ($(strip $(GO_PKG)),) 204 | Host/Configure=$(call GoHost/Host/Configure) 205 | Host/Compile=$(call GoHost/Host/Compile) 206 | Hooks/HostCompile/Post+=Go/CacheCleanup 207 | Host/Uninstall=$(call GoHost/Host/Uninstall,$(1)) 208 | endif 209 | 210 | define GoHostBuild 211 | Host/Install=$$(call GoHost/Host/Install,$$(1)) 212 | endef 213 | 214 | define GoBinHostBuild 215 | Host/Install=$$(call GoHost/Host/Install/Bin,$$(1)) 216 | endef 217 | 218 | define GoSrcHostBuild 219 | Host/Install=$$(call GoHost/Host/Install/Src,$$(1)) 220 | endef 221 | -------------------------------------------------------------------------------- /golang-package.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2018-2022 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, go.sum and go.work, 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 | GOAMD64="$(GO_AMD64)" \ 202 | GOARM="$(GO_ARM)" \ 203 | GOMIPS="$(GO_MIPS)" \ 204 | GOMIPS64="$(GO_MIPS64)" \ 205 | GOPPC64="$(GO_PPC64)" \ 206 | CGO_ENABLED=1 \ 207 | CC="$(TARGET_CC)" \ 208 | CXX="$(TARGET_CXX)" \ 209 | CGO_CFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CFLAGS))" \ 210 | CGO_CPPFLAGS="$(TARGET_CPPFLAGS)" \ 211 | CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" \ 212 | CGO_LDFLAGS="$(TARGET_LDFLAGS)" 213 | 214 | GO_PKG_BUILD_VARS= \ 215 | GOPATH="$(GO_PKG_BUILD_DIR)" \ 216 | GOCACHE="$(GO_BUILD_CACHE_DIR)" \ 217 | GOMODCACHE="$(GO_MOD_CACHE_DIR)" \ 218 | GOENV=off \ 219 | GOTOOLCHAIN=local 220 | 221 | GO_PKG_VARS= \ 222 | $(GO_PKG_TARGET_VARS) \ 223 | $(GO_PKG_BUILD_VARS) 224 | 225 | GO_PKG_DEFAULT_GCFLAGS= \ 226 | $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all) 227 | 228 | GO_PKG_DEFAULT_ASMFLAGS= \ 229 | $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all) 230 | 231 | GO_PKG_DEFAULT_LDFLAGS= \ 232 | -buildid '$(SOURCE_DATE_EPOCH)' \ 233 | -linkmode external \ 234 | -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))' 235 | 236 | GO_PKG_CUSTOM_LDFLAGS= \ 237 | $(GO_PKG_LDFLAGS) \ 238 | $(patsubst %,-X %,$(GO_PKG_LDFLAGS_X)) 239 | 240 | GO_PKG_INSTALL_ARGS= \ 241 | -v \ 242 | -buildvcs=false \ 243 | -trimpath \ 244 | -ldflags "all=$(GO_PKG_DEFAULT_LDFLAGS)" \ 245 | $(if $(strip $(GO_PKG_DEFAULT_GCFLAGS)),-gcflags "all=$(GO_PKG_DEFAULT_GCFLAGS)") \ 246 | $(if $(strip $(GO_PKG_DEFAULT_ASMFLAGS)),-asmflags "all=$(GO_PKG_DEFAULT_ASMFLAGS)") \ 247 | $(if $(GO_PKG_ENABLE_PIE),-buildmode pie) \ 248 | $(if $(filter $(GO_ARCH),arm),-installsuffix "v$(GO_ARM)") \ 249 | $(if $(filter $(GO_ARCH),mips mipsle),-installsuffix "$(GO_MIPS)") \ 250 | $(if $(filter $(GO_ARCH),mips64 mips64le),-installsuffix "$(GO_MIPS64)") \ 251 | $(if $(strip $(GO_PKG_GCFLAGS)),-gcflags "$(GO_PKG_GCFLAGS) $(GO_PKG_DEFAULT_GCFLAGS)") \ 252 | $(if $(strip $(GO_PKG_CUSTOM_LDFLAGS)),-ldflags "$(GO_PKG_CUSTOM_LDFLAGS) $(GO_PKG_DEFAULT_LDFLAGS)") \ 253 | $(if $(strip $(GO_PKG_TAGS)),-tags "$(GO_PKG_TAGS)") 254 | 255 | define GoPackage/Build/Configure 256 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 257 | $(GO_PKG_BUILD_CONFIG_VARS) \ 258 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh configure 259 | endef 260 | 261 | # $(1) additional arguments for go command line (optional) 262 | define GoPackage/Build/Compile 263 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 264 | $(GO_PKG_BUILD_CONFIG_VARS) \ 265 | $(GO_PKG_VARS) \ 266 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh build $(GO_PKG_INSTALL_ARGS) $(1) 267 | endef 268 | 269 | define GoPackage/Build/InstallDev 270 | $(call GoPackage/Package/Install/Src,$(1)) 271 | endef 272 | 273 | define GoPackage/Package/Install/Bin 274 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 275 | $(GO_PKG_BUILD_CONFIG_VARS) \ 276 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_bin "$(1)" 277 | endef 278 | 279 | define GoPackage/Package/Install/Src 280 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 281 | $(GO_PKG_BUILD_CONFIG_VARS) \ 282 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh install_src "$(1)" 283 | endef 284 | 285 | define GoPackage/Package/Install 286 | $(if $(filter $(GO_PKG_SOURCE_ONLY),1),, \ 287 | $(call GoPackage/Package/Install/Bin,$(1)) \ 288 | ) 289 | $(call GoPackage/Package/Install/Src,$(1)) 290 | endef 291 | 292 | 293 | ifneq ($(strip $(GO_PKG)),) 294 | ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1) 295 | PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE 296 | endif 297 | 298 | Build/Configure=$(call GoPackage/Build/Configure) 299 | Build/Compile=$(call GoPackage/Build/Compile) 300 | Hooks/Compile/Post+=Go/CacheCleanup 301 | Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1)) 302 | endif 303 | 304 | define GoPackage 305 | ifndef Package/$(1)/install 306 | Package/$(1)/install=$$(call GoPackage/Package/Install,$$(1)) 307 | endif 308 | endef 309 | 310 | define GoBinPackage 311 | ifndef Package/$(1)/install 312 | Package/$(1)/install=$$(call GoPackage/Package/Install/Bin,$$(1)) 313 | endif 314 | endef 315 | 316 | define GoSrcPackage 317 | ifndef Package/$(1)/install 318 | Package/$(1)/install=$$(call GoPackage/Package/Install/Src,$$(1)) 319 | endif 320 | endef 321 | -------------------------------------------------------------------------------- /golang-values.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2018-2023 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 | 13 | # Unset environment variables 14 | # There are more magic variables to track down, but ain't nobody got time for that 15 | 16 | # From https://pkg.go.dev/cmd/go#hdr-Environment_variables 17 | 18 | # General-purpose environment variables: 19 | unexport \ 20 | GO111MODULE \ 21 | GCCGO \ 22 | GOARCH \ 23 | GOBIN \ 24 | GOCACHE \ 25 | GOMODCACHE \ 26 | GODEBUG \ 27 | GOENV \ 28 | GOFLAGS \ 29 | GOOS \ 30 | GOPATH \ 31 | GOROOT \ 32 | GOTOOLCHAIN \ 33 | GOTMPDIR \ 34 | GOWORK 35 | # Unmodified: 36 | # GOINSECURE 37 | # GOPRIVATE 38 | # GOPROXY 39 | # GONOPROXY 40 | # GOSUMDB 41 | # GONOSUMDB 42 | # GOVCS 43 | 44 | # Environment variables for use with cgo: 45 | unexport \ 46 | AR \ 47 | CC \ 48 | CGO_ENABLED \ 49 | CGO_CFLAGS CGO_CFLAGS_ALLOW CGO_CFLAGS_DISALLOW \ 50 | CGO_CPPFLAGS CGO_CPPFLAGS_ALLOW CGO_CPPFLAGS_DISALLOW \ 51 | CGO_CXXFLAGS CGO_CXXFLAGS_ALLOW CGO_CXXFLAGS_DISALLOW \ 52 | CGO_FFLAGS CGO_FFLAGS_ALLOW CGO_FFLAGS_DISALLOW \ 53 | CGO_LDFLAGS CGO_LDFLAGS_ALLOW CGO_LDFLAGS_DISALLOW \ 54 | CXX \ 55 | FC 56 | # Unmodified: 57 | # PKG_CONFIG 58 | 59 | # Architecture-specific environment variables: 60 | unexport \ 61 | GOARM \ 62 | GOARM64 \ 63 | GO386 \ 64 | GOAMD64 \ 65 | GOMIPS \ 66 | GOMIPS64 \ 67 | GOPPC64 \ 68 | GORISCV64 \ 69 | GOWASM 70 | 71 | # Environment variables for use with code coverage: 72 | unexport \ 73 | GOCOVERDIR 74 | 75 | # Special-purpose environment variables: 76 | unexport \ 77 | GCCGOTOOLDIR \ 78 | GOEXPERIMENT \ 79 | GOROOT_FINAL \ 80 | GO_EXTLINK_ENABLED 81 | # Unmodified: 82 | # GIT_ALLOW_PROTOCOL 83 | 84 | # From https://pkg.go.dev/runtime#hdr-Environment_Variables 85 | unexport \ 86 | GOGC \ 87 | GOMEMLIMIT \ 88 | GOMAXPROCS \ 89 | GORACE \ 90 | GOTRACEBACK 91 | 92 | # From https://pkg.go.dev/cmd/cgo#hdr-Using_cgo_with_the_go_command 93 | unexport \ 94 | CC_FOR_TARGET \ 95 | CXX_FOR_TARGET 96 | # Todo: 97 | # CC_FOR_${GOOS}_${GOARCH} 98 | # CXX_FOR_${GOOS}_${GOARCH} 99 | 100 | # From https://go.dev/doc/install/source#environment 101 | unexport \ 102 | GOHOSTOS \ 103 | GOHOSTARCH 104 | 105 | # From https://go.dev/src/make.bash 106 | unexport \ 107 | GO_GCFLAGS \ 108 | GO_LDFLAGS \ 109 | GO_LDSO \ 110 | GO_DISTFLAGS \ 111 | GOBUILDTIMELOGFILE \ 112 | GOROOT_BOOTSTRAP 113 | 114 | # From https://go.dev/doc/go1.9#parallel-compile 115 | unexport \ 116 | GO19CONCURRENTCOMPILATION 117 | 118 | # From https://go.dev/src/cmd/dist/build.go 119 | unexport \ 120 | BOOT_GO_GCFLAGS \ 121 | BOOT_GO_LDFLAGS 122 | 123 | # From https://go.dev/src/cmd/dist/buildtool.go 124 | unexport \ 125 | GOBOOTSTRAP_TOOLEXEC 126 | 127 | 128 | # GOOS / GOARCH 129 | 130 | go_arch=$(subst \ 131 | aarch64,arm64,$(subst \ 132 | i386,386,$(subst \ 133 | loongarch64,loong64,$(subst \ 134 | mipsel,mipsle,$(subst \ 135 | mips64el,mips64le,$(subst \ 136 | powerpc64,ppc64,$(subst \ 137 | x86_64,amd64,$(1)))))))) 138 | 139 | GO_OS:=linux 140 | GO_ARCH:=$(call go_arch,$(ARCH)) 141 | GO_OS_ARCH:=$(GO_OS)_$(GO_ARCH) 142 | 143 | GO_HOST_OS:=$(call tolower,$(HOST_OS)) 144 | GO_HOST_ARCH:=$(call go_arch,$(subst \ 145 | armv6l,arm,$(subst \ 146 | armv7l,arm,$(subst \ 147 | i686,i386,$(HOST_ARCH))))) 148 | GO_HOST_OS_ARCH:=$(GO_HOST_OS)_$(GO_HOST_ARCH) 149 | 150 | ifeq ($(GO_OS_ARCH),$(GO_HOST_OS_ARCH)) 151 | GO_HOST_TARGET_SAME:=1 152 | else 153 | GO_HOST_TARGET_DIFFERENT:=1 154 | endif 155 | 156 | ifeq ($(GO_ARCH),386) 157 | ifeq ($(CONFIG_TARGET_x86_geode)$(CONFIG_TARGET_x86_legacy),y) 158 | GO_386:=softfloat 159 | else 160 | GO_386:=sse2 161 | endif 162 | 163 | # -fno-plt: causes "unexpected GOT reloc for non-dynamic symbol" errors 164 | GO_CFLAGS_TO_REMOVE:=-fno-plt 165 | 166 | else ifeq ($(GO_ARCH),amd64) 167 | GO_AMD64:=v1 168 | 169 | else ifeq ($(GO_ARCH),arm) 170 | GO_TARGET_FPU:=$(word 2,$(subst +,$(space),$(call qstrip,$(CONFIG_CPU_TYPE)))) 171 | 172 | # FPU names from https://gcc.gnu.org/onlinedocs/gcc-8.4.0/gcc/ARM-Options.html#index-mfpu-1 173 | # see also https://github.com/gcc-mirror/gcc/blob/releases/gcc-8.4.0/gcc/config/arm/arm-cpus.in 174 | 175 | ifeq ($(GO_TARGET_FPU),) 176 | GO_ARM:=5 177 | else ifneq ($(filter $(GO_TARGET_FPU),vfp vfpv2),) 178 | GO_ARM:=6 179 | else 180 | GO_ARM:=7 181 | endif 182 | 183 | else ifneq ($(filter $(GO_ARCH),mips mipsle),) 184 | ifeq ($(CONFIG_HAS_FPU),y) 185 | GO_MIPS:=hardfloat 186 | else 187 | GO_MIPS:=softfloat 188 | endif 189 | 190 | # -mips32r2: conflicts with -march=mips32 set by go 191 | GO_CFLAGS_TO_REMOVE:=-mips32r2 192 | 193 | else ifneq ($(filter $(GO_ARCH),mips64 mips64le),) 194 | ifeq ($(CONFIG_HAS_FPU),y) 195 | GO_MIPS64:=hardfloat 196 | else 197 | GO_MIPS64:=softfloat 198 | endif 199 | 200 | else ifeq ($(GO_ARCH),ppc64) 201 | GO_PPC64:=power8 202 | 203 | endif 204 | 205 | 206 | # Target Go 207 | 208 | GO_ARCH_DEPENDS:=@(aarch64||arm||i386||i686||loongarch64||mips||mips64||mips64el||mipsel||powerpc64||riscv64||x86_64) 209 | 210 | 211 | # ASLR/PIE 212 | 213 | # From https://go.dev/src/internal/platform/supported.go 214 | GO_PIE_SUPPORTED_OS_ARCH:= \ 215 | android_386 android_amd64 android_arm android_arm64 \ 216 | linux_386 linux_amd64 linux_arm linux_arm64 \ 217 | windows_386 windows_amd64 windows_arm windows_arm64 \ 218 | \ 219 | darwin_amd64 darwin_arm64 \ 220 | ios_amd64 ios_arm64 \ 221 | \ 222 | freebsd_amd64 \ 223 | \ 224 | aix_ppc64 \ 225 | \ 226 | linux_loong64 linux_ppc64le linux_riscv64 linux_s390x 227 | 228 | # From https://go.dev/src/cmd/go/internal/work/init.go 229 | go_pie_install_suffix=$(if $(filter $(1),aix_ppc64 windows_386 windows_amd64 windows_arm windows_arm64),,shared) 230 | 231 | ifneq ($(filter $(GO_HOST_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),) 232 | GO_HOST_PIE_SUPPORTED:=1 233 | GO_HOST_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_HOST_OS_ARCH)) 234 | endif 235 | 236 | ifneq ($(filter $(GO_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),) 237 | GO_TARGET_PIE_SUPPORTED:=1 238 | GO_TARGET_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_OS_ARCH)) 239 | endif 240 | 241 | 242 | # Spectre mitigations 243 | 244 | GO_SPECTRE_SUPPORTED_ARCH:=amd64 245 | 246 | ifneq ($(filter $(GO_HOST_ARCH),$(GO_SPECTRE_SUPPORTED_ARCH)),) 247 | GO_HOST_SPECTRE_SUPPORTED:=1 248 | endif 249 | 250 | ifneq ($(filter $(GO_ARCH),$(GO_SPECTRE_SUPPORTED_ARCH)),) 251 | GO_TARGET_SPECTRE_SUPPORTED:=1 252 | endif 253 | 254 | 255 | # General build info 256 | 257 | GO_BUILD_CACHE_DIR:=$(or $(call qstrip,$(CONFIG_GOLANG_BUILD_CACHE_DIR)),$(TMP_DIR)/go-build) 258 | GO_MOD_CACHE_DIR:=$(DL_DIR)/go-mod-cache 259 | 260 | GO_MOD_ARGS= \ 261 | -modcacherw 262 | 263 | GO_GENERAL_BUILD_CONFIG_VARS= \ 264 | CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE="$(CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE)" \ 265 | GO_BUILD_CACHE_DIR="$(GO_BUILD_CACHE_DIR)" \ 266 | GO_MOD_CACHE_DIR="$(GO_MOD_CACHE_DIR)" \ 267 | GO_MOD_ARGS="$(GO_MOD_ARGS)" 268 | 269 | define Go/CacheCleanup 270 | $(GO_GENERAL_BUILD_CONFIG_VARS) \ 271 | $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh cache_cleanup 272 | endef 273 | -------------------------------------------------------------------------------- /golang/Config.in: -------------------------------------------------------------------------------- 1 | menu "Configuration" 2 | 3 | config GOLANG_EXTERNAL_BOOTSTRAP_ROOT 4 | string "External bootstrap Go root directory" 5 | default "" 6 | help 7 | Path to a working Go tree (>= Go 1.4), with bin, pkg, and src 8 | subdirectories and the Go compiler at bin/go. 9 | 10 | If specified, the existing Go installation will be used to 11 | compile host (buildroot) Go. 12 | 13 | Leave blank to compile the default bootstrap Go. 14 | 15 | config GOLANG_BUILD_CACHE_DIR 16 | string "Go build cache directory" 17 | default "" 18 | help 19 | Store the Go build cache in this directory. 20 | If not set, uses '$(TMP_DIR)/go-build'. 21 | 22 | config GOLANG_MOD_CACHE_WORLD_READABLE 23 | bool "Ensure Go module cache is world-readable" 24 | default n 25 | 26 | config GOLANG_SPECTRE 27 | bool "Enable Spectre mitigations" 28 | default n 29 | depends on x86_64 30 | help 31 | Currently only available for x86-64 (amd64). 32 | 33 | endmenu 34 | -------------------------------------------------------------------------------- /golang/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2018-2023 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 | include $(TOPDIR)/rules.mk 9 | 10 | GO_VERSION_MAJOR_MINOR:=1.24 11 | GO_VERSION_PATCH:=3 12 | 13 | PKG_NAME:=golang 14 | PKG_VERSION:=$(GO_VERSION_MAJOR_MINOR)$(if $(GO_VERSION_PATCH),.$(GO_VERSION_PATCH)) 15 | PKG_RELEASE:=1 16 | 17 | GO_SOURCE_URLS:=https://dl.google.com/go/ \ 18 | https://mirrors.ustc.edu.cn/golang/ \ 19 | https://mirrors.nju.edu.cn/golang/ 20 | 21 | PKG_SOURCE:=go$(PKG_VERSION).src.tar.gz 22 | PKG_SOURCE_URL:=$(GO_SOURCE_URLS) 23 | PKG_HASH:=229c08b600b1446798109fae1f569228102c8473caba8104b6418cb5bc032878 24 | 25 | PKG_MAINTAINER:=Jeffery To 26 | PKG_LICENSE:=BSD-3-Clause 27 | PKG_LICENSE_FILES:=LICENSE 28 | PKG_CPE_ID:=cpe:/a:golang:go 29 | 30 | PKG_BUILD_DEPENDS:=golang/host 31 | PKG_BUILD_DIR:=$(BUILD_DIR)/go-$(PKG_VERSION) 32 | PKG_BUILD_PARALLEL:=1 33 | PKG_BUILD_FLAGS:=no-mips16 34 | 35 | PKG_GO_PREFIX:=/usr 36 | PKG_GO_VERSION_ID:=$(GO_VERSION_MAJOR_MINOR) 37 | 38 | HOST_BUILD_DIR:=$(BUILD_DIR_HOST)/go-$(PKG_VERSION) 39 | HOST_BUILD_PARALLEL:=1 40 | 41 | HOST_GO_PREFIX:=$(STAGING_DIR_HOSTPKG) 42 | HOST_GO_VERSION_ID:=cross 43 | HOST_GO_ROOT:=$(HOST_GO_PREFIX)/lib/go-$(HOST_GO_VERSION_ID) 44 | 45 | HOST_GO_VALID_OS_ARCH:= \ 46 | android_386 android_amd64 android_arm android_arm64 \ 47 | freebsd_386 freebsd_amd64 freebsd_arm freebsd_arm64 \ 48 | linux_386 linux_amd64 linux_arm linux_arm64 \ 49 | openbsd_386 openbsd_amd64 openbsd_arm openbsd_arm64 \ 50 | netbsd_386 netbsd_amd64 netbsd_arm netbsd_arm64 \ 51 | windows_386 windows_amd64 windows_arm windows_arm64 \ 52 | \ 53 | plan9_386 plan9_amd64 plan9_arm \ 54 | \ 55 | darwin_amd64 darwin_arm64 \ 56 | ios_amd64 ios_arm64 \ 57 | \ 58 | dragonfly_amd64 \ 59 | illumos_amd64 \ 60 | solaris_amd64 \ 61 | \ 62 | aix_ppc64 \ 63 | js_wasm \ 64 | wasip1_wasm \ 65 | \ 66 | freebsd_riscv64 \ 67 | openbsd_riscv64 \ 68 | \ 69 | linux_ppc64 linux_ppc64le \ 70 | linux_mips linux_mipsle linux_mips64 linux_mips64le \ 71 | linux_loong64 linux_riscv64 linux_s390x \ 72 | \ 73 | openbsd_mips64 74 | 75 | ifeq ($(HOST_ARCH),x86_64) 76 | PKG_ARCH:=amd64 77 | BOOTSTRAP_HASH:=999805bed7d9039ec3da1a53bfbcafc13e367da52aa823cb60b68ba22d44c616 78 | endif 79 | 80 | ifeq ($(HOST_ARCH),aarch64) 81 | PKG_ARCH:=arm64 82 | BOOTSTRAP_HASH:=c15fa895341b8eaf7f219fada25c36a610eb042985dc1a912410c1c90098eaf2 83 | endif 84 | 85 | BOOTSTRAP_SOURCE:=go1.22.6.linux-$(PKG_ARCH).tar.gz 86 | BOOTSTRAP_SOURCE_URL:=$(GO_SOURCE_URLS) 87 | 88 | BOOTSTRAP_BUILD_DIR:=$(HOST_BUILD_DIR)/.go_bootstrap 89 | 90 | BOOTSTRAP_GO_VALID_OS_ARCH:= \ 91 | darwin_386 darwin_amd64 \ 92 | dragonfly_386 dragonfly_amd64 \ 93 | freebsd_386 freebsd_amd64 freebsd_arm \ 94 | linux_386 linux_amd64 linux_arm \ 95 | netbsd_386 netbsd_amd64 netbsd_arm \ 96 | openbsd_386 openbsd_amd64 \ 97 | plan9_386 plan9_amd64 \ 98 | solaris_amd64 \ 99 | windows_386 windows_amd64 100 | 101 | include $(INCLUDE_DIR)/host-build.mk 102 | include $(INCLUDE_DIR)/package.mk 103 | include ../golang-compiler.mk 104 | include ../golang-package.mk 105 | 106 | PKG_UNPACK:=$(HOST_TAR) -C "$(PKG_BUILD_DIR)" --strip-components=1 -xzf "$(DL_DIR)/$(PKG_SOURCE)" 107 | HOST_UNPACK:=$(HOST_TAR) -C "$(HOST_BUILD_DIR)" --strip-components=1 -xzf "$(DL_DIR)/$(PKG_SOURCE)" 108 | BOOTSTRAP_UNPACK:=$(HOST_TAR) -C "$(BOOTSTRAP_BUILD_DIR)" --strip-components=1 -xzf "$(DL_DIR)/$(BOOTSTRAP_SOURCE)" 109 | 110 | # don't strip ELF executables in test data 111 | RSTRIP:=: 112 | STRIP:=: 113 | 114 | ifeq ($(GO_TARGET_SPECTRE_SUPPORTED),1) 115 | PKG_CONFIG_DEPENDS+=CONFIG_GOLANG_SPECTRE 116 | endif 117 | 118 | define Package/golang/Default 119 | $(call GoPackage/GoSubMenu) 120 | TITLE:=Go programming language 121 | URL:=https://go.dev/ 122 | DEPENDS:=$(GO_ARCH_DEPENDS) 123 | endef 124 | 125 | define Package/golang/Default/description 126 | The Go programming language is an open source project to make 127 | programmers more productive. 128 | 129 | Go is expressive, concise, clean, and efficient. Its concurrency 130 | mechanisms make it easy to write programs that get the most out of 131 | multicore and networked machines, while its novel type system enables 132 | flexible and modular program construction. Go compiles quickly to 133 | machine code yet has the convenience of garbage collection and the power 134 | of run-time reflection. It's a fast, statically typed, compiled language 135 | that feels like a dynamically typed, interpreted language. 136 | endef 137 | 138 | # go tool requires source present: 139 | # https://github.com/golang/go/issues/4635 140 | define Package/golang 141 | $(call Package/golang/Default) 142 | TITLE+= (compiler) 143 | DEPENDS+= +golang-src 144 | endef 145 | 146 | define Package/golang/description 147 | $(call Package/golang/Default/description) 148 | 149 | This package provides an assembler, compiler, linker, and compiled 150 | libraries for the Go programming language. 151 | endef 152 | 153 | define Package/golang/config 154 | source "$(SOURCE)/Config.in" 155 | endef 156 | 157 | define Package/golang-doc 158 | $(call Package/golang/Default) 159 | TITLE+= (documentation) 160 | endef 161 | 162 | define Package/golang-doc/description 163 | $(call Package/golang/Default/description) 164 | 165 | This package provides the documentation for the Go programming language. 166 | endef 167 | 168 | define Package/golang-src 169 | $(call Package/golang/Default) 170 | TITLE+= (source files) 171 | endef 172 | 173 | define Package/golang-src/description 174 | $(call Package/golang/Default/description) 175 | 176 | This package provides the Go programming language source files needed 177 | for cross-compilation. 178 | endef 179 | 180 | 181 | # Bootstrap 182 | 183 | BOOTSTRAP_ROOT_DIR:=$(call qstrip,$(CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT)) 184 | 185 | ifeq ($(BOOTSTRAP_ROOT_DIR),) 186 | BOOTSTRAP_ROOT_DIR:=$(BOOTSTRAP_BUILD_DIR) 187 | 188 | define Download/golang-bootstrap 189 | FILE:=$(BOOTSTRAP_SOURCE) 190 | URL:=$(BOOTSTRAP_SOURCE_URL) 191 | HASH:=$(BOOTSTRAP_HASH) 192 | endef 193 | $(eval $(call Download,golang-bootstrap)) 194 | 195 | define Bootstrap/Prepare 196 | mkdir -p "$(BOOTSTRAP_BUILD_DIR)" && $(BOOTSTRAP_UNPACK) ; 197 | endef 198 | Hooks/HostPrepare/Post+=Bootstrap/Prepare 199 | 200 | $(eval $(call GoCompiler/AddProfile,Bootstrap,$(BOOTSTRAP_BUILD_DIR),,bootstrap,$(GO_HOST_OS_ARCH))) 201 | endif 202 | 203 | 204 | # Host 205 | 206 | ifeq ($(GO_HOST_PIE_SUPPORTED),1) 207 | HOST_GO_ENABLE_PIE:=1 208 | endif 209 | 210 | # when using GO_LDFLAGS to set buildmode=pie, the PIE install suffix 211 | # does not apply (we also delete the std lib during Host/Install) 212 | 213 | $(eval $(call GoCompiler/AddProfile,Host,$(HOST_BUILD_DIR),$(HOST_GO_PREFIX),$(HOST_GO_VERSION_ID),$(GO_HOST_OS_ARCH),$(HOST_GO_INSTALL_SUFFIX))) 214 | 215 | HOST_GO_VARS= \ 216 | GOHOSTARCH="$(GO_HOST_ARCH)" \ 217 | GOCACHE="$(GO_BUILD_CACHE_DIR)" \ 218 | GOENV=off \ 219 | CC="$(HOSTCC_NOCACHE)" \ 220 | CXX="$(HOSTCXX_NOCACHE)" 221 | 222 | define Host/Compile 223 | $(call GoCompiler/Bootstrap/CheckHost,$(BOOTSTRAP_GO_VALID_OS_ARCH)) 224 | $(call GoCompiler/Host/CheckHost,$(HOST_GO_VALID_OS_ARCH)) 225 | 226 | mkdir -p "$(GO_BUILD_CACHE_DIR)" 227 | 228 | $(call GoCompiler/Host/Make, \ 229 | GOROOT_BOOTSTRAP="$(BOOTSTRAP_ROOT_DIR)" \ 230 | $(if $(HOST_GO_ENABLE_PIE),GO_LDFLAGS="-buildmode pie") \ 231 | $(HOST_GO_VARS) \ 232 | ) 233 | endef 234 | 235 | # if host and target os/arch are the same, 236 | # when go compiles a program, it will use the host std lib 237 | # so remove it now and force go to rebuild std for target later 238 | define Host/Install 239 | $(call Host/Uninstall) 240 | 241 | $(call GoCompiler/Host/Install/Bin,) 242 | $(call GoCompiler/Host/Install/Src,) 243 | 244 | $(call GoCompiler/Host/Install/BinLinks,) 245 | 246 | rm -rf "$(HOST_GO_ROOT)/pkg/$(GO_HOST_OS_ARCH)$(if $(HOST_GO_INSTALL_SUFFIX),_$(HOST_GO_INSTALL_SUFFIX))" 247 | 248 | $(INSTALL_DIR) "$(HOST_GO_ROOT)/openwrt" 249 | $(INSTALL_BIN) ./files/go-gcc-helper "$(HOST_GO_ROOT)/openwrt/" 250 | $(LN) go-gcc-helper "$(HOST_GO_ROOT)/openwrt/gcc" 251 | $(LN) go-gcc-helper "$(HOST_GO_ROOT)/openwrt/g++" 252 | endef 253 | 254 | define Host/Uninstall 255 | rm -rf "$(HOST_GO_ROOT)/openwrt" 256 | 257 | $(call GoCompiler/Host/Uninstall/BinLinks,) 258 | 259 | $(call GoCompiler/Host/Uninstall,) 260 | endef 261 | 262 | 263 | # Target 264 | 265 | ifeq ($(GO_PKG_ENABLE_PIE),1) 266 | PKG_GO_INSTALL_SUFFIX:=$(GO_TARGET_PIE_INSTALL_SUFFIX) 267 | endif 268 | 269 | $(eval $(call GoCompiler/AddProfile,Package,$(PKG_BUILD_DIR),$(PKG_GO_PREFIX),$(PKG_GO_VERSION_ID),$(GO_OS_ARCH),$(PKG_GO_INSTALL_SUFFIX))) 270 | 271 | PKG_GO_ZBOOTSTRAP_MODS:= \ 272 | s/defaultGO386 = `[^`]*`/defaultGO386 = `$(or $(GO_386),sse2)`/; \ 273 | s/defaultGOAMD64 = `[^`]*`/defaultGOAMD64 = `$(or $(GO_AMD64),v1)`/; \ 274 | s/defaultGOARM = `[^`]*`/defaultGOARM = `$(or $(GO_ARM),7)`/; \ 275 | s/defaultGOMIPS = `[^`]*`/defaultGOMIPS = `$(or $(GO_MIPS),hardfloat)`/; \ 276 | s/defaultGOMIPS64 = `[^`]*`/defaultGOMIPS64 = `$(or $(GO_MIPS64),hardfloat)`/; \ 277 | s/defaultGOPPC64 = `[^`]*`/defaultGOPPC64 = `$(or $(GO_PPC64),power8)`/; 278 | 279 | PKG_GO_ZBOOTSTRAP_PATH:=$(PKG_BUILD_DIR)/src/internal/buildcfg/zbootstrap.go 280 | 281 | PKG_GO_VARS= \ 282 | GOHOSTARCH="$(GO_HOST_ARCH)" \ 283 | GOCACHE="$(GO_BUILD_CACHE_DIR)" \ 284 | GOENV=off \ 285 | GO_GCC_HELPER_PATH="$$$$PATH" \ 286 | CC=gcc \ 287 | CXX=g++ \ 288 | PKG_CONFIG=pkg-config \ 289 | PATH="$(HOST_GO_ROOT)/openwrt:$$$$PATH" 290 | 291 | PKG_GO_GCFLAGS= \ 292 | $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all) 293 | 294 | PKG_GO_ASMFLAGS= \ 295 | $(if $(GO_PKG_ENABLE_SPECTRE),-spectre all) 296 | 297 | PKG_GO_LDFLAGS= \ 298 | -buildid '$(SOURCE_DATE_EPOCH)' \ 299 | -linkmode external \ 300 | -extldflags '$(patsubst -z%,-Wl$(comma)-z$(comma)%,$(TARGET_LDFLAGS))' \ 301 | $(if $(CONFIG_NO_STRIP)$(CONFIG_DEBUG),,-s -w) 302 | 303 | # setting -trimpath is not necessary here because the paths inside the 304 | # compiler binary are relative to GOROOT_FINAL (PKG_GO_ROOT), which is 305 | # static / not dependent on the build environment 306 | PKG_GO_INSTALL_ARGS= \ 307 | -ldflags "all=$(PKG_GO_LDFLAGS)" \ 308 | $(if $(PKG_GO_GCFLAGS),-gcflags "all=$(PKG_GO_GCFLAGS)") \ 309 | $(if $(PKG_GO_ASMFLAGS),-asmflags "all=$(PKG_GO_ASMFLAGS)") \ 310 | $(if $(filter $(GO_PKG_ENABLE_PIE),1),-buildmode pie) 311 | 312 | define Build/Compile 313 | mkdir -p "$(GO_BUILD_CACHE_DIR)" 314 | 315 | @echo "Building target Go first stage" 316 | 317 | $(call GoCompiler/Package/Make, \ 318 | GOROOT_BOOTSTRAP="$(HOST_GO_ROOT)" \ 319 | GO_GCC_HELPER_CC="$(HOSTCC)" \ 320 | GO_GCC_HELPER_CXX="$(HOSTCXX)" \ 321 | $(PKG_GO_VARS) \ 322 | ) 323 | 324 | $(SED) '$(PKG_GO_ZBOOTSTRAP_MODS)' "$(PKG_GO_ZBOOTSTRAP_PATH)" 325 | 326 | ( \ 327 | if echo 'int main() { return 0; }' | $(TARGET_CC) -o $(PKG_BUILD_DIR)/test-ldso -x c - > /dev/null 2>&1; then \ 328 | LDSO=$$$$( \ 329 | readelf -l $(PKG_BUILD_DIR)/test-ldso | \ 330 | sed -n -e 's/^.*interpreter: \(.*\)[]]/\1/p' \ 331 | ) ; \ 332 | fi ; \ 333 | $(SED) "s,defaultGO_LDSO = \`[^\`]*\`,defaultGO_LDSO = \`$$$$LDSO\`," "$(PKG_GO_ZBOOTSTRAP_PATH)" ; \ 334 | ) 335 | 336 | @echo "Building target Go second stage" 337 | 338 | ( \ 339 | cd "$(PKG_BUILD_DIR)/bin" ; \ 340 | export $(GO_PKG_TARGET_VARS) ; \ 341 | $(CP) go go-host ; \ 342 | GO_GCC_HELPER_CC="$(TARGET_CC)" \ 343 | GO_GCC_HELPER_CXX="$(TARGET_CXX)" \ 344 | $(PKG_GO_VARS) \ 345 | ./go-host install -a $(PKG_GO_INSTALL_ARGS) std cmd ; \ 346 | retval="$$$$?" ; \ 347 | rm -f go-host ; \ 348 | exit "$$$$retval" ; \ 349 | ) 350 | endef 351 | 352 | define Package/golang/install 353 | $(call GoCompiler/Package/Install/Bin,$(1)$(PKG_GO_PREFIX)) 354 | $(call GoCompiler/Package/Install/BinLinks,$(1)$(PKG_GO_PREFIX)) 355 | endef 356 | 357 | define Package/golang-doc/install 358 | $(call GoCompiler/Package/Install/Doc,$(1)$(PKG_GO_PREFIX)) 359 | endef 360 | 361 | define Package/golang-src/install 362 | $(call GoCompiler/Package/Install/Src,$(1)$(PKG_GO_PREFIX)) 363 | endef 364 | 365 | # src/debug contains ELF executables as test data 366 | # and they reference these libraries 367 | # we need to call this in Package/$(1)/extra_provides 368 | # to pass CheckDependencies in include/package-ipkg.mk 369 | define Package/golang-src/extra_provides 370 | echo 'libc.so.6' 371 | endef 372 | 373 | 374 | $(eval $(call HostBuild)) 375 | $(eval $(call BuildPackage,golang)) 376 | $(eval $(call BuildPackage,golang-doc)) 377 | $(eval $(call BuildPackage,golang-src)) 378 | -------------------------------------------------------------------------------- /golang/files/go-gcc-helper: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | me=go-gcc-helper 4 | name="${0##*/}" 5 | 6 | log() { 7 | # shellcheck disable=SC2039 8 | local IFS=" " 9 | printf '%s\n' "$me: $*" 10 | } 11 | 12 | case "$name" in 13 | gcc) 14 | if [ -z "$GO_GCC_HELPER_CC" ]; then 15 | log "missing GO_GCC_HELPER_CC" 16 | exit 1 17 | fi 18 | cmd="$GO_GCC_HELPER_CC" 19 | ;; 20 | g++) 21 | if [ -z "$GO_GCC_HELPER_CXX" ]; then 22 | log "missing GO_GCC_HELPER_CXX" 23 | exit 1 24 | fi 25 | cmd="$GO_GCC_HELPER_CXX" 26 | ;; 27 | *) 28 | log "unknown command \"$name\"" 29 | exit 1 30 | ;; 31 | esac 32 | 33 | if [ -n "$GO_GCC_HELPER_PATH" ]; then 34 | export PATH="$GO_GCC_HELPER_PATH" 35 | else 36 | log "missing GO_GCC_HELPER_PATH" 37 | fi 38 | 39 | log "running $cmd $*" 40 | 41 | $cmd "$@" 42 | --------------------------------------------------------------------------------