--- /dev/null
+#!/bin/sh
+
+log() {
+ # shellcheck disable=SC2039
+ local IFS=" "
+ printf '%s\n' "$*"
+}
+
+log_error() {
+ # shellcheck disable=SC2039
+ local IFS=" "
+ printf 'Error: %s\n' "$*" >&2
+}
+
+cache_cleanup() {
+ if ! [ -d "$GO_MOD_CACHE_DIR" ]; then
+ return 0
+ fi
+
+ # in case go is called without -modcacherw
+ find "$GO_MOD_CACHE_DIR" -type d -not -perm -u+w -exec chmod u+w '{}' +
+
+ if [ -n "$CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE" ]; then
+ find "$GO_MOD_CACHE_DIR" -type d -not -perm -go+rx -exec chmod go+rx '{}' +
+ find "$GO_MOD_CACHE_DIR" -not -type d -not -perm -go+r -exec chmod go+r '{}' +
+ fi
+
+ return 0
+}
+
+
+if [ "$#" -lt 1 ]; then
+ log_error "Missing command"
+ exit 1
+fi
+
+command="$1"
+shift 1
+
+case "$command" in
+ cache_cleanup)
+ cache_cleanup
+ ;;
+ *)
+ log_error "Invalid command \"$command\""
+ exit 1
+ ;;
+esac
GO_PKG_BUILD_VARS= \
GOPATH=$(GO_PKG_BUILD_DIR) \
GOCACHE=$(GO_PKG_CACHE_DIR) \
+ GOMODCACHE=$(GO_MOD_CACHE_DIR) \
GOENV=off
GO_PKG_DEFAULT_VARS= \
define GoPackage/Build/Configure
( \
cd $(PKG_BUILD_DIR) ; \
- mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src $(GO_PKG_CACHE_DIR) ; \
+ mkdir -p $(GO_PKG_BUILD_DIR)/bin $(GO_PKG_BUILD_DIR)/src $(GO_PKG_CACHE_DIR) $(GO_MOD_CACHE_DIR) ; \
\
files=$$$$($(FIND) ./ \
-type d -a \( -path './.git' -o -path './$(GO_PKG_WORK_DIR_NAME)' \) -prune -o \
( \
cd $(GO_PKG_BUILD_DIR) ; \
export $(GO_PKG_VARS) ; \
+ if [ -f "$(PKG_BUILD_DIR)/go.mod" ] ; then \
+ modargs="$(GO_MOD_ARGS)" ; \
+ fi ; \
\
echo "Finding targets" ; \
- targets=$$$$(go list $(GO_PKG_BUILD_PKG)) ; \
+ targets=$$$$(go list $$$$modargs $(GO_PKG_BUILD_PKG)) ; \
for pattern in $(GO_PKG_EXCLUDES); do \
targets=$$$$(echo "$$$$targets" | grep -v "$$$$pattern") ; \
done ; \
\
if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \
echo "Building targets" ; \
- go install $(GO_PKG_INSTALL_ARGS) $(1) $$$$targets ; \
+ go install $(GO_PKG_INSTALL_ARGS) $$$$modargs $(1) $$$$targets ; \
retval=$$$$? ; \
echo ; \
\
echo ; \
fi ; \
\
- echo "Cleaning module download cache (golang/go#27455)" ; \
- go clean -modcache ; \
- echo ; \
+ if [ "$$$$retval" -ne 0 ]; then \
+ $(call Go/CacheCleanup) ; \
+ fi ; \
fi ; \
exit $$$$retval ; \
)
ifneq ($(strip $(GO_PKG)),)
Build/Configure=$(call GoPackage/Build/Configure)
Build/Compile=$(call GoPackage/Build/Compile)
+ Hooks/Compile/Post+=Go/CacheCleanup
Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1))
endif
GO_TARGET_PIE_SUPPORTED:=1
GO_TARGET_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_OS_ARCH))
endif
+
+
+# General build info
+
+GO_MOD_CACHE_DIR:=$(DL_DIR)/go-mod-cache
+
+GO_MOD_ARGS= \
+ -modcacherw
+
+GO_GENERAL_BUILD_CONFIG_VARS= \
+ CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE="$(CONFIG_GOLANG_MOD_CACHE_WORLD_READABLE)" \
+ GO_MOD_CACHE_DIR="$(GO_MOD_CACHE_DIR)"
+
+define Go/CacheCleanup
+ $(GENERAL_BUILD_CONFIG_VARS) \
+ $(SHELL) $(GO_INCLUDE_DIR)/golang-build.sh cache_cleanup
+endef