-
Notifications
You must be signed in to change notification settings - Fork 2
311 lines (264 loc) · 11.8 KB
/
simulation.yml
File metadata and controls
311 lines (264 loc) · 11.8 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
name: simulation
run-name: ${{ github.actor }} running HDL simulations
on:
[push]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
detect-changes:
runs-on: self-hosted
outputs:
vunit_tests: ${{ steps.parse.outputs.vunit_tests }}
bsv_tests: ${{ steps.parse.outputs.bsv_tests }}
has_vunit_tests: ${{ steps.parse.outputs.has_vunit_tests }}
has_bsv_tests: ${{ steps.parse.outputs.has_bsv_tests }}
has_changes: ${{ steps.parse.outputs.has_changes }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: 'true'
fetch-depth: 0 # Need full history
clean: false # Preserve buck-out/ for caching
- name: Clean stale files (preserve buck-out/)
run: git clean -ffdx --exclude=buck-out --exclude=vunit_out
- name: Setup environment
run: |
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
python3 -m pip install --upgrade -r tools/requirements.txt --break-system-packages
- name: Get changed files and base commit
id: changed-files
run: |
# On main (post-merge), compare against the previous commit;
# on branches, compare against origin/main to detect all changes.
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
BASE_COMMIT="HEAD~1"
else
BASE_COMMIT="origin/main"
fi
# Generate list of changed files in BTD format: "M path/to/file"
# BTD expects Mercurial-like format with status code (M/A/D) and space separator
# Filter out .github/ (CI-only changes don't affect build targets)
# but keep other dotfiles like .buckconfig which are build inputs
git diff --name-status "$BASE_COMMIT" HEAD | sed 's/\t/ /' | grep -v ' \.github/' > /tmp/changes.txt || touch /tmp/changes.txt
echo "Changed files ($(wc -l < /tmp/changes.txt)):"
cat /tmp/changes.txt
echo "--- End of changes.txt ---"
# Debug: Show hex dump of first line to check for hidden characters
if [ -s /tmp/changes.txt ]; then
echo "Hex dump of first line:"
head -1 /tmp/changes.txt | od -c
fi
echo "base_commit=$BASE_COMMIT" >> "$GITHUB_OUTPUT"
- name: Generate base snapshot
run: |
git checkout ${{ steps.changed-files.outputs.base_commit }}
supertd targets //... 2>/dev/null > /tmp/base.jsonl
echo "Base snapshot: $(wc -l < /tmp/base.jsonl) targets"
git checkout ${{ github.sha }}
- name: Generate diff snapshot
run: |
supertd targets //... 2>/dev/null > /tmp/diff.jsonl
echo "Diff snapshot: $(wc -l < /tmp/diff.jsonl) targets"
- name: Run buck2-change-detector
id: btd
continue-on-error: true
run: |
# Note: Requires supertd to be installed
# Install with: cargo install --git https://github.com/facebookincubator/buck2-change-detector.git supertd
echo "Checking for supertd installation..."
which supertd || echo "supertd not found in PATH"
command -v supertd && supertd --version || echo "supertd command failed"
if command -v supertd &> /dev/null; then
echo "Running BTD analysis..."
echo "Command: supertd btd --json --changes /tmp/changes.txt --base /tmp/base.jsonl --diff /tmp/diff.jsonl root//..."
# Run BTD with --json flag and root//... universe pattern
if supertd btd --json \
--changes /tmp/changes.txt \
--base /tmp/base.jsonl \
--diff /tmp/diff.jsonl \
root//... > /tmp/btd_output.json 2> /tmp/btd_stderr.txt; then
echo "BTD completed successfully"
# Count targets (BTD outputs an array of target objects)
TARGETS=$(python3 -c "import json; data = json.load(open('/tmp/btd_output.json')); print(len(data))" 2>/dev/null || echo "unknown")
echo "Found $TARGETS affected targets"
echo "BTD output preview:"
head -20 /tmp/btd_output.json || true
echo "succeeded=true" >> "$GITHUB_OUTPUT"
else
EXIT_CODE=$?
echo "::error::BTD command failed with exit code $EXIT_CODE"
echo "BTD stdout:"
cat /tmp/btd_output.json || echo "No output file generated"
echo "BTD stderr:"
cat /tmp/btd_stderr.txt || echo "No stderr file generated"
echo "::warning::BTD failed, will run all targets"
echo '[]' > /tmp/btd_output.json
echo "succeeded=false" >> "$GITHUB_OUTPUT"
fi
else
echo "::warning::supertd not installed, will run all targets"
echo "PATH: $PATH"
echo '[]' > /tmp/btd_output.json
echo "succeeded=false" >> "$GITHUB_OUTPUT"
fi
- name: Parse affected targets
id: parse
run: |
mkdir -p /tmp/btd_results
# Run parser script; --fallback means BTD failed so run everything
FALLBACK_FLAG=""
if [ "${{ steps.btd.outputs.succeeded }}" != "true" ]; then
FALLBACK_FLAG="--fallback"
fi
python3 .github/scripts/parse-affected-targets.py \
/tmp/btd_output.json \
/tmp/btd_results $FALLBACK_FLAG || {
echo "::warning::Parser failed, creating empty matrices"
# Create empty matrices on failure
for matrix in vunit_matrix bsv_test_matrix ice40_matrix ecp5_matrix vivado_matrix; do
echo '{"target": []}' > /tmp/btd_results/$matrix.json
done
echo '{"total_affected": 0, "vunit_tests": 0, "bsv_tests": 0, "ice40_bitstreams": 0, "ecp5_bitstreams": 0, "vivado_bitstreams": 0}' > /tmp/btd_results/summary.json
}
# Load target lists (simple JSON arrays for sims)
echo "vunit_tests=$(cat /tmp/btd_results/vunit_tests.json)" >> "$GITHUB_OUTPUT"
echo "bsv_tests=$(cat /tmp/btd_results/bsv_tests.json)" >> "$GITHUB_OUTPUT"
# Set has_* flags based on array length
VUNIT_COUNT=$(python3 -c "import json; print(len(json.load(open('/tmp/btd_results/vunit_tests.json'))))" 2>/dev/null || echo "0")
BSV_COUNT=$(python3 -c "import json; print(len(json.load(open('/tmp/btd_results/bsv_tests.json'))))" 2>/dev/null || echo "0")
if [ "$VUNIT_COUNT" -gt 0 ]; then
echo "has_vunit_tests=true" >> "$GITHUB_OUTPUT"
else
echo "has_vunit_tests=false" >> "$GITHUB_OUTPUT"
fi
if [ "$BSV_COUNT" -gt 0 ]; then
echo "has_bsv_tests=true" >> "$GITHUB_OUTPUT"
else
echo "has_bsv_tests=false" >> "$GITHUB_OUTPUT"
fi
# Set has_changes flag
TOTAL=$(python3 -c "import json; print(json.load(open('/tmp/btd_results/summary.json'))['total_affected'])" 2>/dev/null || echo "0")
if [ "$TOTAL" -gt 0 ]; then
echo "has_changes=true" >> "$GITHUB_OUTPUT"
else
echo "has_changes=false" >> "$GITHUB_OUTPUT"
fi
# Display summary
cat /tmp/btd_results/summary.json
vunit-sim:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.has_vunit_tests == 'true' }}
runs-on: self-hosted
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
submodules: 'true'
clean: false # Preserve buck-out/ for caching
- name: Clean stale files (preserve buck-out/)
run: git clean -ffdx --exclude=buck-out --exclude=vunit_out
- name: Update pip reqs
run: python3 -m pip install --upgrade -r tools/requirements.txt --break-system-packages
- name: Setup PATH
run: echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
- name: Run VUnit tests
run: |
TARGETS='${{ needs.detect-changes.outputs.vunit_tests }}'
echo "Running $(python3 -c "import json; print(len(json.loads('$TARGETS')))") VUnit tests"
FAILED=0
for target in $(python3 -c "import json; print('\n'.join(json.loads('$TARGETS')))"); do
echo "::group::Running $target"
TEST_NAME=$(echo "$target" | sed 's|.*:||')
if ! buck2 run "$target" -- --clean -x "${TEST_NAME}.xml"; then
echo "::error::Test $target failed"
FAILED=1
fi
echo "::endgroup::"
done
if [ $FAILED -eq 1 ]; then
exit 1
fi
- name: Cleanup
run: bash .github/cleanup.sh
bsv-sim-buck2:
needs: detect-changes
if: ${{ needs.detect-changes.outputs.has_bsv_tests == 'true' }}
runs-on: self-hosted
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
submodules: 'true'
clean: false # Preserve buck-out/ for caching
- name: Clean stale files (preserve buck-out/)
run: git clean -ffdx --exclude=buck-out --exclude=vunit_out
- name: Update pip reqs
run: python3 -m pip install --upgrade -r tools/requirements.txt --break-system-packages
- name: Setup PATH
run: |
echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"
echo "/opt/bsc-2022.01/bin" >> "$GITHUB_PATH"
- name: Run BSV tests
env:
BSV_LIB_DIR: /opt/bsc-2022.01/lib
run: |
TARGETS='${{ needs.detect-changes.outputs.bsv_tests }}'
# Convert JSON array to space-separated list for buck2
TARGET_LIST=$(python3 -c "import json; print(' '.join(json.loads('$TARGETS')))")
echo "Building $(echo $TARGET_LIST | wc -w) BSV tests in parallel"
# Build all tests in parallel first
buck2 build --preemptible=ondifferentstate $TARGET_LIST
# Run each test (buck2 run sets up BSV environment)
FAILED=0
for target in $TARGET_LIST; do
echo "::group::Running $target"
if ! buck2 run "$target"; then
echo "::error::Test $target failed"
FAILED=1
fi
echo "::endgroup::"
done
if [ $FAILED -eq 1 ]; then
exit 1
fi
- name: Cleanup
run: bash .github/cleanup.sh
# Summary job that consolidates all simulation results for required status checks
simulation-summary:
name: Simulation Summary
runs-on: ubuntu-latest
needs: [detect-changes, vunit-sim, bsv-sim-buck2]
if: always()
steps:
- name: Check simulation results
run: |
echo "Checking results of all simulation jobs..."
# Check if we had changes to process
if [ "${{ needs.detect-changes.outputs.has_changes }}" != "true" ]; then
echo "✓ No changes detected - skipping simulations"
exit 0
fi
# Check each matrix job result
VUNIT_RESULT="${{ needs.vunit-sim.result }}"
BSV_RESULT="${{ needs.bsv-sim-buck2.result }}"
echo "VUnit simulations: $VUNIT_RESULT"
echo "BSV simulations: $BSV_RESULT"
# Job results can be: success, failure, cancelled, or skipped
# We pass if all jobs are success or skipped (skipped means no targets in matrix)
FAILED=0
if [ "$VUNIT_RESULT" = "failure" ] || [ "$VUNIT_RESULT" = "cancelled" ]; then
echo "✗ VUnit simulations failed"
FAILED=1
fi
if [ "$BSV_RESULT" = "failure" ] || [ "$BSV_RESULT" = "cancelled" ]; then
echo "✗ BSV simulations failed"
FAILED=1
fi
if [ $FAILED -eq 1 ]; then
echo "❌ Simulations failed"
exit 1
fi
echo "✅ All simulations passed or were skipped"
exit 0