golang: Move module cache into DL_DIR
authorJeffery To <jeffery.to@gmail.com>
Tue, 18 Aug 2020 21:39:41 +0000 (05:39 +0800)
committerJeffery To <jeffery.to@gmail.com>
Tue, 6 Oct 2020 08:27:52 +0000 (16:27 +0800)
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 <jeffery.to@gmail.com>
lang/golang/golang-build.sh [new file with mode: 0644]
lang/golang/golang-package.mk
lang/golang/golang-values.mk
lang/golang/golang/Config.in

diff --git a/lang/golang/golang-build.sh b/lang/golang/golang-build.sh
new file mode 100644 (file)
index 0000000..d39949d
--- /dev/null
@@ -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
index 1e279797b27d4f18b5087a1b6359516653c49c2d..266bfeda4a81820b1fd70b1c7ba31f6107d63f79 100644 (file)
@@ -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
 
index 9ea34be8277ffb7fb30ca95d094437c13809aed9..84b6b58920d67158f5980758ec69f42b76ad873a 100644 (file)
@@ -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
index 6bbbf728cc3dc2553ee6db4293a75db62c028dae..cb5fec6d58b516ba52647411a3bb471e3d7f603f 100644 (file)
@@ -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