-
Notifications
You must be signed in to change notification settings - Fork 86
/
installCargoArtifactsHook.sh
81 lines (65 loc) · 2.49 KB
/
installCargoArtifactsHook.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
compressAndInstallCargoArtifactsDir() {
local dir="${1:?destination directory not defined}"
local cargoTargetDir="${2:?cargoTargetDir not defined}"
mkdir -p "${dir}"
local dest="${dir}/target.tar.zst"
echo "compressing ${cargoTargetDir} to ${dest}"
(
export SOURCE_DATE_EPOCH=1
tar --sort=name \
--mtime="@${SOURCE_DATE_EPOCH}" \
--owner=0 \
--group=0 \
--numeric-owner \
--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \
-c "${cargoTargetDir}" | zstd "-T${NIX_BUILD_CORES:-0}" -o "${dest}"
)
}
dedupAndInstallCargoArtifactsDir() {
local dest="${1:?destination directory not defined}"
local cargoTargetDir="${2:?cargoTargetDir not defined}"
local prevCargoTargetDir="${3:?prevCargoTargetDir not defined}"
mkdir -p "${dest}"
if [ -d "${prevCargoTargetDir}" ]; then
echo "symlinking duplicates in ${cargoTargetDir} to ${prevCargoTargetDir}"
while read -r fullTargetFile; do
# Strip the common prefix of the current target directory
local targetFile="${fullTargetFile#"${cargoTargetDir}"}"
# Join the path and ensure we don't have a duplicate `/` separator
local candidateOrigFile="${prevCargoTargetDir}/${targetFile#/}"
if cmp --silent "${candidateOrigFile}" "${fullTargetFile}"; then
ln --symbolic --force --logical "${candidateOrigFile}" "${fullTargetFile}"
fi
done < <(find "${cargoTargetDir}" -type f)
fi
echo installing "${cargoTargetDir}" to "${dest}"
mv "${cargoTargetDir}" --target-directory="${dest}"
}
prepareAndInstallCargoArtifactsDir() {
# Allow for calling with customized parameters
# or fall back to defaults if none are provided
local dir="${1:-${out}}"
local cargoTargetDir="${2:-${CARGO_TARGET_DIR:-target}}"
local mode="${3:-${installCargoArtifactsMode:-use-symlink}}"
mkdir -p "${dir}"
case "${mode}" in
"use-zstd")
compressAndInstallCargoArtifactsDir "${dir}" "${cargoTargetDir}"
;;
"use-symlink")
# Placeholder if previous artifacts aren't present
local prevCargoTargetDir="/dev/null"
if [ -n "${cargoArtifacts}" ] && [ -d "${cargoArtifacts}/target" ]; then
local prevCargoTargetDir="${cargoArtifacts}/target"
fi
dedupAndInstallCargoArtifactsDir "${dir}" "${cargoTargetDir}" "${prevCargoTargetDir}"
;;
*)
echo "unknown mode: \"${mode}\""
false
;;
esac
}
if [ "1" = "${doInstallCargoArtifacts-}" ]; then
postInstallHooks+=(prepareAndInstallCargoArtifactsDir)
fi