From d27b635768c93704087ca7cbb9074d6619eaf848 Mon Sep 17 00:00:00 2001
From: ptrcnull <git@ptrcnull.me>
Date: Thu, 22 Jun 2023 03:33:59 +0000
Subject: [PATCH] abuild: set cache directories to a temporary directory if
 MOVE_CACHES is set

this allows for isolating cache directories per package
---
 abuild.in         | 17 +++++++++++--
 default.conf      |  3 +++
 tests/abuild_test | 65 +++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/abuild.in b/abuild.in
index 8e575e71..42996d3e 100755
--- a/abuild.in
+++ b/abuild.in
@@ -156,6 +156,7 @@ cleanup() {
 			fi;;
 		pkgdir) msg "Cleaning up pkgdir"; rm -rf "$pkgbasedir";;
 		srcdir) msg "Cleaning up srcdir"; cleanup_srcdir;;
+		tmpdir) msg "Cleaning up tmpdir"; rm -rf "$tmpdir";;
 		deps)
 			if [ -n "$uninstall_after" ]; then
 				msg "Uninstalling dependencies..."
@@ -553,10 +554,11 @@ unpack() {
 	default_unpack
 }
 
-# cleanup source and package dir
+# cleanup source, package and temporary dir
 clean() {
 	cleanup srcdir
 	cleanup pkgdir
+	cleanup tmpdir
 }
 
 # cleanup fetched sources
@@ -690,7 +692,17 @@ runpart() {
 			prepare|build|package|check)
 				# exclude aports from git repo discovery
 				export GIT_CEILING_DIRECTORIES="$startdir"
-				cd "$builddir";;
+
+				# prevent using global cache directories
+				if [ -n "$MOVE_CACHES" ]; then
+					export GOCACHE="${GOCACHE:-"$tmpdir/go"}"
+					export GOMODCACHE="${GOCACHE:-"$tmpdir/gomod"}"
+					export GOTMPDIR="${GOTMPDIR:-"$tmpdir"}"
+					export CARGO_HOME="${CARGO_HOME:-"$tmpdir/cargo"}"
+				fi
+
+				cd "$builddir"
+				;;
 		esac
 	fi
 	$part
@@ -2900,6 +2912,7 @@ APKBUILD=$(readlink -f "$APKBUILD")
 startdir="${APKBUILD%/*}"
 srcdir=${srcdir:-"$startdir/src"}
 pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
+tmpdir=${tmpdir:-"$startdir/tmp"}
 
 repo=${startdir%/*}
 repo=${repo##*/}
diff --git a/default.conf b/default.conf
index bdaef900..04c2540a 100644
--- a/default.conf
+++ b/default.conf
@@ -33,3 +33,6 @@ CLEANUP="srcdir bldroot pkgdir deps"
 
 # what to cleanup after a failed build
 ERROR_CLEANUP="bldroot deps"
+
+# uncomment to prevent using global cache directories for Go and Cargo
+#MOVE_CACHES=1
diff --git a/tests/abuild_test b/tests/abuild_test
index 5edea40c..80264b24 100755
--- a/tests/abuild_test
+++ b/tests/abuild_test
@@ -42,7 +42,9 @@ init_tests \
 	abuild_pyc \
 	abuild_setcap_binary \
 	abuild_setcap_binary_with_option \
-	abuild_command_provides
+	abuild_command_provides \
+	abuild_gocache_dir \
+	abuild_cargo_home_dir
 
 export ABUILD_SHAREDIR=$(atf_get_srcdir)/..
 export ABUILD_CONF=/dev/null
@@ -513,7 +515,6 @@ abuild_git_ceiling_body() {
 		abuild
 }
 
-
 create_fake_du() {
 	mkdir -p bin
 	cat > bin/du <<-EOF
@@ -957,3 +958,63 @@ abuild_command_provides_body() {
 		-o not-match:"provides = cmd:dir=1.0-r0" \
 		grep '^provides = ' pkg/.control.bin-in-path/.PKGINFO
 }
+
+abuild_gocache_dir_body() {
+	init_keys
+	mkdir gocache-dir
+	cd gocache-dir
+	cat >APKBUILD<<-EOF
+		# Maintainer: Joe User <juser@example.com>
+		pkgname="gocache-dir"
+		pkgver="1.0"
+		pkgrel=0
+		pkgdesc="Dummy test package"
+		url="https://gitlab.alpinelinux.org/alpine/aports"
+		arch="noarch"
+		license="MIT"
+		prepare() {
+			mkdir -p "\$builddir"
+		}
+		build() {
+			echo "dir:\$GOCACHE"
+		}
+		package() {
+			:
+		}
+	EOF
+
+	MOVE_CACHES=1 \
+	atf_check -s exit:0 \
+		-o match:"dir:.*/tmp/go" \
+		abuild prepare build
+}
+
+abuild_cargo_home_dir_body() {
+	init_keys
+	mkdir cargo-home-dir
+	cd cargo-home-dir
+	cat >APKBUILD<<-EOF
+		# Maintainer: Joe User <juser@example.com>
+		pkgname="cargo-home-dir"
+		pkgver="1.0"
+		pkgrel=0
+		pkgdesc="Dummy test package"
+		url="https://gitlab.alpinelinux.org/alpine/aports"
+		arch="noarch"
+		license="MIT"
+		prepare() {
+			mkdir -p "\$builddir"
+		}
+		build() {
+			echo "dir:\$CARGO_HOME"
+		}
+		package() {
+			:
+		}
+	EOF
+
+	MOVE_CACHES=1 \
+	atf_check -s exit:0 \
+		-o match:"dir:.*/tmp/cargo" \
+		abuild prepare build
+}
-- 
GitLab