From 3dd55b504cb4d7d4ca0251f4ce7b45ec8effeaf8 Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Wed, 19 Aug 2020 05:39:41 +0800 Subject: [PATCH] golang: Move module cache into DL_DIR This also adds a config option GOLANG_MOD_CACHE_WORLD_READABLE; if enabled, chmod is run after a Go package build to make all files/directories in the module cache world-readable. Signed-off-by: Jeffery To --- lang/golang/golang-build.sh | 48 +++++++++++++++++++++++++++++++++++ lang/golang/golang-package.mk | 17 ++++++++----- lang/golang/golang-values.mk | 17 +++++++++++++ lang/golang/golang/Config.in | 4 +++ 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 lang/golang/golang-build.sh diff --git a/lang/golang/golang-build.sh b/lang/golang/golang-build.sh new file mode 100644 index 0000000000..d39949dca9 --- /dev/null +++ b/lang/golang/golang-build.sh @@ -0,0 +1,48 @@ +#!/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 diff --git a/lang/golang/golang-package.mk b/lang/golang/golang-package.mk index 1e279797b2..266bfeda4a 100644 --- a/lang/golang/golang-package.mk +++ b/lang/golang/golang-package.mk @@ -186,6 +186,7 @@ GO_PKG_TARGET_VARS= \ 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= \ @@ -226,7 +227,7 @@ GoPackage/has_binaries=$(call GoPackage/is_dir_not_empty,$(GO_PKG_BUILD_BIN_DIR) 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 \ @@ -303,9 +304,12 @@ define GoPackage/Build/Compile ( \ 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 ; \ @@ -319,7 +323,7 @@ define GoPackage/Build/Compile \ 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 ; \ \ @@ -328,9 +332,9 @@ define GoPackage/Build/Compile 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 ; \ ) @@ -362,6 +366,7 @@ endef 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 diff --git a/lang/golang/golang-values.mk b/lang/golang/golang-values.mk index 9ea34be827..84b6b58920 100644 --- a/lang/golang/golang-values.mk +++ b/lang/golang/golang-values.mk @@ -219,3 +219,20 @@ ifneq ($(filter $(GO_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),) 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 diff --git a/lang/golang/golang/Config.in b/lang/golang/golang/Config.in index 6bbbf728cc..cb5fec6d58 100644 --- a/lang/golang/golang/Config.in +++ b/lang/golang/golang/Config.in @@ -12,4 +12,8 @@ config GOLANG_EXTERNAL_BOOTSTRAP_ROOT Leave blank to compile the default bootstrap Go. +config GOLANG_MOD_CACHE_WORLD_READABLE + bool "Ensure Go module cache is world-readable" + default n + endmenu -- 2.30.2