Browse Source

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 <jeffery.to@gmail.com>
lilik-openwrt-22.03
Jeffery To 4 years ago
parent
commit
3dd55b504c
No known key found for this signature in database GPG Key ID: C616D9E719E868E4
4 changed files with 80 additions and 6 deletions
  1. +48
    -0
      lang/golang/golang-build.sh
  2. +11
    -6
      lang/golang/golang-package.mk
  3. +17
    -0
      lang/golang/golang-values.mk
  4. +4
    -0
      lang/golang/golang/Config.in

+ 48
- 0
lang/golang/golang-build.sh View File

@ -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

+ 11
- 6
lang/golang/golang-package.mk View 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


+ 17
- 0
lang/golang/golang-values.mk View 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

+ 4
- 0
lang/golang/golang/Config.in View 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

Loading…
Cancel
Save