Files
wasm-simulator/check-linuxcnc-switchkins-remap-table-cached.sh
2026-06-02 06:47:10 +08:00

117 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
cache_dir=${CNC_SIM_PREFLIGHT_CACHE_DIR:-}
check_args=("$@")
if [[ "${1:-}" == "--cache-dir" ]]; then
if [[ "$#" -lt 2 ]]; then
echo "missing --cache-dir value" >&2
exit 1
fi
cache_dir=$2
shift 2
check_args=("$@")
fi
linuxcnc_root=${LINUXCNC_ROOT:-../linuxcnc}
manifest=${1:-linuxcnc-kinematics-source-files.txt}
if [[ -z "$cache_dir" ]]; then
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-switchkins-remap-table.sh "${check_args[@]}"
exit 0
fi
if [[ "$cache_dir" =~ [[:space:]] ]]; then
echo "CNC_SIM_PREFLIGHT_CACHE_DIR must not contain whitespace: $cache_dir" >&2
exit 1
fi
mkdir -p "$cache_dir"
cache_dir=$(cd "$cache_dir" && pwd)
if [[ "$cache_dir" == "/" ]]; then
echo "CNC_SIM_PREFLIGHT_CACHE_DIR must not be the filesystem root" >&2
exit 1
fi
exec 8>"$cache_dir/check-linuxcnc-switchkins-remap-table.lock"
flock 8
if [[ ! -d "$linuxcnc_root" || ! -f "$manifest" ]]; then
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-switchkins-remap-table.sh "${check_args[@]}"
exit 0
fi
linuxcnc_root=$(cd "$linuxcnc_root" && pwd)
manifest_path=$(cd "$(dirname "$manifest")" && pwd)/$(basename "$manifest")
manifest_key=$(printf '%s' "$manifest_path" | sha256sum | awk '{ print $1 }')
args_key=$(
{
printf 'ARGS='
printf ' %q' "${check_args[@]}"
printf '\n'
} | sha256sum | awk '{ print $1 }'
)
cache_file="$cache_dir/check-linuxcnc-switchkins-remap-table.${manifest_key}.${args_key}.signature"
next_signature=$(mktemp "$cache_file.next.XXXXXX")
write_manifest_source_stats() {
local source_path
while IFS=: read -r group path note extra; do
case "$group" in
""|\#*)
continue
;;
esac
[[ -z "${path:-}" ]] && continue
source_path="$linuxcnc_root/$path"
if [[ ! -f "$source_path" ]]; then
return 1
fi
stat -c 'SOURCE=%n:%s:%y' "$source_path"
done < "$manifest_path"
}
write_project_output_hashes() {
local project_output
for project_output in \
core/src/cnc_sim_api.cpp \
core/src/linuxcnc_switchkins_remap_table.inc \
core/tests/linuxcnc_switchkins_remap_config_cases.inc \
web/public/linuxcnc_switchkins_remap_config_cases.json \
web/src/index.ts \
web/src/wasm-core.js; do
[[ -f "$project_output" ]] || return 1
sha256sum "$project_output"
done
}
if ! {
printf 'CACHED_SCRIPT='
sha256sum check-linuxcnc-switchkins-remap-table-cached.sh
printf 'CHECK_SCRIPT='
sha256sum check-linuxcnc-switchkins-remap-table.sh
printf 'GENERATOR_SCRIPT='
sha256sum generate-linuxcnc-switchkins-remap-table.sh
printf 'LINUXCNC_ROOT=%s\n' "$linuxcnc_root"
printf 'ARGS='
printf ' %q' "${check_args[@]}"
printf '\n'
printf 'MANIFEST_PATH=%s\n' "$manifest_path"
printf 'MANIFEST='
sha256sum "$manifest_path"
write_manifest_source_stats
printf 'PROJECT_OUTPUTS:\n'
write_project_output_hashes
} > "$next_signature"; then
rm -f "$next_signature"
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-switchkins-remap-table.sh "${check_args[@]}"
exit 0
fi
if [[ -f "$cache_file" ]] && cmp -s "$cache_file" "$next_signature"; then
rm -f "$next_signature"
exit 0
fi
LINUXCNC_ROOT="$linuxcnc_root" ./check-linuxcnc-switchkins-remap-table.sh "${check_args[@]}"
mv "$next_signature" "$cache_file"