Files
rust-package-builder/scripts/lib.sh
T
2026-10-19 22:39:09 +02:00

84 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
# Increment when package payload/installation semantics change without a source commit.
PACKAGING_REVISION=4
OPNSENSE_PACKAGING_REVISION=4
FREEBSD_NATIVE_PACKAGING_REVISION=1
log() {
printf '%s %s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$*"
}
fatal() {
log "ERROR: $*" >&2
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || fatal "required command not found: $1"
}
trim_trailing_slash() {
local value=$1
while [[ "$value" == */ ]]; do value=${value%/}; done
printf '%s' "$value"
}
yaml_quote() {
python3 - "$1" <<'PY'
import json, sys
print(json.dumps(sys.argv[1]))
PY
}
read_first_field() {
local file=$1 key=$2
awk -v key="$key" 'BEGIN{IGNORECASE=1} $0 ~ "^" key ":[[:space:]]*" {sub(/^[^:]+:[[:space:]]*/, ""); print; exit}' "$file"
}
atomic_write() {
local path=$1
local tmp="${path}.tmp.$$"
cat > "$tmp"
mv -f "$tmp" "$path"
}
# Keep Cargo's release dependency cache so subsequent builds remain incremental.
# Run only after a branch has successfully published its packages.
cleanup_cargo_target() {
[[ "${CLEAN_TARGET_AFTER_BUILD:-1}" == "1" ]] || return 0
local target="${CARGO_TARGET_DIR:-}"
local expected="${DATA_DIR:-}/cargo/target"
# Never recursively delete an arbitrary path provided via environment.
if [[ -z "$target" || "$target" != "$expected" || ! -d "$target" || -L "$target" ]]; then
log "Cargo target cleanup skipped: unexpected target directory"
return 0
fi
local days="${TARGET_CACHE_MAX_AGE_DAYS:-30}"
if [[ ! "$days" =~ ^[0-9]+$ ]] || (( days < 1 )); then
log "Cargo target cleanup skipped: invalid TARGET_CACHE_MAX_AGE_DAYS=$days"
return 0
fi
local before after
before=$(du -sk "$target" 2>/dev/null | awk '{print $1}')
# Release dependencies, fingerprints and build scripts remain cached.
# Rust incremental data is disposable; debug output is unused by release builds.
find "$target" -type d -name incremental -prune -exec rm -rf -- {} + 2>/dev/null || true
if [[ -d "$target/debug" && ! -L "$target/debug" ]]; then
rm -rf -- "$target/debug" || true
fi
# Expire old compiled dependency files gradually, not the entire target.
# Fresh build outputs stay available for subsequent Cargo invocations.
find "$target/release/deps" "$target/x86_64-unknown-freebsd/release/deps" \
-maxdepth 1 -type f -mtime "+$days" -delete 2>/dev/null || true
after=$(du -sk "$target" 2>/dev/null | awk '{print $1}')
log "Cargo target cleanup: reclaimed $(( ${before:-0} - ${after:-0} )) KiB (cache retained)"
}