Repository navigation
Expand file tree
/
Copy pathcheck-toolchain-sync.sh
More file actions
executable file
·175 lines (157 loc) · 6.99 KB
/
Copy pathcheck-toolchain-sync.sh
File metadata and controls
executable file
·175 lines (157 loc) · 6.99 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
# =============================================================================
# StellarSettle – Toolchain Synchronization Check (#452)
# =============================================================================
#
# Verifies that rust-toolchain.toml stays in sync with every GitHub Actions
# workflow matrix/pin and every hard-coded wasm target in scripts/docs:
#
# - Workflow `toolchain:` values must equal rust-toolchain.toml `channel`.
# - dtolnay/rust-toolchain@REF (REF not master/main) must equal `channel`.
# - Workflow `target:`/`targets:` values and `--target X` flags must appear
# in rust-toolchain.toml `targets`.
# - Hard-coded `wasm32-*` strings in scripts/docs must appear in `targets`.
#
# Usage:
# bash scripts/check-toolchain-sync.sh
#
# Exit codes:
# 0 everything in sync
# 1 mismatch found (file:line reported on stderr)
# =============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
cd "${REPO_ROOT}"
fail=0
err() { echo "[toolchain-sync] MISMATCH: $*" >&2; fail=1; }
# ---------------------------------------------------------------------------
# Parse rust-toolchain.toml (source of truth)
# ---------------------------------------------------------------------------
toolchain_file="rust-toolchain.toml"
[[ -f "${toolchain_file}" ]] || { err "${toolchain_file} not found"; exit 1; }
channel=$(sed -n 's/^[[:space:]]*channel[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' "${toolchain_file}" | head -n1)
if [[ -z "${channel}" ]]; then
err "could not parse 'channel' from ${toolchain_file}"
exit 1
fi
targets_line=$(sed -n 's/^[[:space:]]*targets[[:space:]]*=[[:space:]]*\[\(.*\)\].*/\1/p' "${toolchain_file}" | head -n1)
toolchain_targets=()
if [[ -n "${targets_line}" ]]; then
while IFS= read -r t; do
[[ -n "${t}" ]] && toolchain_targets+=("${t}")
done < <(echo "${targets_line}" | tr -d '"' | tr ',' '\n' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
fi
if (( ${#toolchain_targets[@]} == 0 )); then
err "could not parse 'targets' from ${toolchain_file}"
exit 1
fi
echo "[toolchain-sync] source of truth: channel=${channel} targets=[${toolchain_targets[*]}]"
target_known() {
local t="$1"
local known
for known in "${toolchain_targets[@]}"; do
[[ "${t}" == "${known}" ]] && return 0
done
return 1
}
# ---------------------------------------------------------------------------
# 1. Workflows: toolchain values, dtolnay REF pins, target:/targets:/--target
# ---------------------------------------------------------------------------
workflow_files=(.github/workflows/*.yml .github/workflows/*.yaml)
for wf in ${workflow_files[@]+"${workflow_files[@]}"}; do
[[ -f "${wf}" ]] || continue
# matrix/static `toolchain:` values (skip expressions like ${{ ... }})
while IFS= read -r line; do
lineno="${line%%:*}"
rest="${line#*:}"
val=$(echo "${rest}" | sed -n 's/^[[:space:]]*toolchain:[[:space:]]*\([^$[:space:]][^#]*\).*/\1/p' | tr -d '"' | tr -d "'" | sed 's/[[:space:]]*$//')
# matrix form: toolchain: [stable] or [a, b]
val="${val#[}"
val="${val%]}"
if [[ -n "${val}" ]]; then
IFS=',' read -ra parts <<< "${val}"
for p in "${parts[@]}"; do
p="$(echo "${p}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
[[ -z "${p}" || "${p}" == '$'* ]] && continue
if [[ "${p}" != "${channel}" ]]; then
err "${wf}:${lineno} toolchain '${p}' != rust-toolchain.toml channel '${channel}'"
fi
done
fi
done < <(grep -n 'toolchain:' "${wf}" || true)
# dtolnay/rust-toolchain@REF where REF is not a floating branch
while IFS= read -r line; do
lineno="${line%%:*}"
ref=$(echo "${line}" | sed -n 's|.*dtolnay/rust-toolchain@\([A-Za-z0-9._/-]*\).*|\1|p')
case "${ref}" in
""|master|main) ;; # floating; channel comes from `with.toolchain` (checked above)
*)
if [[ "${ref}" != "${channel}" ]]; then
err "${wf}:${lineno} dtolnay/rust-toolchain@${ref} != rust-toolchain.toml channel '${channel}'"
fi
;;
esac
done < <(grep -n 'dtolnay/rust-toolchain@' "${wf}" || true)
# target: / targets: values (skip expressions)
while IFS= read -r line; do
lineno="${line%%:*}"
rest="${line#*:}"
val=$(echo "${rest}" | sed -n 's/^[[:space:]]*targets\?:[[:space:]]*\([^$[:space:]][^#]*\).*/\1/p' | tr -d '"' | tr -d "'" | sed 's/[[:space:]]*$//')
# matrix form: target: [wasm32v1-none]
val="${val#[}"
val="${val%]}"
if [[ -n "${val}" ]] && ! target_known "${val}"; then
err "${wf}:${lineno} target '${val}' not in rust-toolchain.toml targets [${toolchain_targets[*]}]"
fi
done < <(grep -nE '^[[:space:]]*targets?:' "${wf}" || true)
# cargo/stellar --target X hard-coded flags (skip shell vars and matrix refs)
while IFS= read -r line; do
lineno="${line%%:*}"
for t in $(echo "${line}" | grep -oE -- '--target[[:space:]]+[A-Za-z0-9_-]+' | awk '{print $2}'); do
case "${t}" in
'$'*) ;; # expression / shell variable
*)
if ! target_known "${t}"; then
err "${wf}:${lineno} --target ${t} not in rust-toolchain.toml targets [${toolchain_targets[*]}]"
fi
;;
esac
done
done < <(grep -n -- '--target' "${wf}" || true)
done
# ---------------------------------------------------------------------------
# 2. Scripts & docs: hard-coded wasm32-* targets
# ---------------------------------------------------------------------------
scan_files=(scripts/*.sh scripts/*.ps1 .env.example DEVELOPMENT.md README.md CONTRIBUTING.md docs/*.md)
for f in ${scan_files[@]+"${scan_files[@]}"}; do
[[ -f "${f}" ]] || continue
while IFS= read -r line; do
lineno="${line%%:*}"
for t in $(echo "${line}" | grep -oE 'wasm32-[A-Za-z0-9_-]+' | sort -u); do
if ! target_known "${t}"; then
err "${f}:${lineno} wasm target '${t}' not in rust-toolchain.toml targets [${toolchain_targets[*]}]"
fi
done
done < <(grep -nE 'wasm32-[A-Za-z0-9_-]+' "${f}" || true)
done
# ---------------------------------------------------------------------------
# 3. Same wasm target must not be mixed with a non-listed alias in package paths
# ---------------------------------------------------------------------------
for f in scripts/*.sh; do
[[ -f "${f}" ]] || continue
while IFS= read -r line; do
lineno="${line%%:*}"
for t in $(echo "${line}" | grep -oE 'target/wasm32-[A-Za-z0-9_-]+/release' | sed 's|target/||;s|/release||' | sort -u); do
if ! target_known "${t}"; then
err "${f}:${lineno} build path uses '${t}' which is not in rust-toolchain.toml targets [${toolchain_targets[*]}]"
fi
done
done < <(grep -nE 'target/wasm32-[A-Za-z0-9_-]+/release' "${f}" || true)
done
if (( fail != 0 )); then
echo "" >&2
echo "[toolchain-sync] FAILED — update rust-toolchain.toml or the listed files so they agree." >&2
exit 1
fi
echo "[toolchain-sync] OK — workflows, scripts, and docs are in sync with rust-toolchain.toml."