Skip to content

Commit e5e18d2

Browse files
committed
deterministic shell script
1 parent e922ade commit e5e18d2

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

examples/ring_example/.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/bazel-*
22
.DS_Store
3-
/proto/.DS_Store
4-
/proto/grpc_server/.DS_Store
5-
/proto/proto_bindings/.DS_Store
3+
deterministic_test_store/*
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
echo "=== Testing Deterministic Build for Ring Crate (.rlib files) ==="
6+
7+
# Define the target to test
8+
TARGET="@crates//:ring"
9+
STORE_DIR="./deterministic_test_store"
10+
BUILD1_DIR="${STORE_DIR}/build1"
11+
BUILD2_DIR="${STORE_DIR}/build2"
12+
13+
# Define the build command with flags (modify this to add/change flags)
14+
# Example: BUILD_CMD="bazel build --noremote_accept_cached --verbose_failures --sandbox_debug"
15+
BUILD_CMD="bazel build --noremote_accept_cached --@rules_rust//rust/settings:lto=thin"
16+
17+
# Clean up any previous test runs
18+
if [ -d "$STORE_DIR" ]; then
19+
echo "Cleaning up previous test store..."
20+
rm -rf "$STORE_DIR"
21+
fi
22+
23+
# Create directories for storing build outputs
24+
mkdir -p "$BUILD1_DIR" "$BUILD2_DIR"
25+
26+
echo "--- First Build ---"
27+
# Build the target
28+
$BUILD_CMD "$TARGET"
29+
30+
# Find and copy the .rlib artifacts
31+
echo "Copying first build .rlib artifacts..."
32+
BAZEL_BIN=$(bazel info bazel-bin)
33+
find "$BAZEL_BIN" -name "*.rlib" -type f -exec cp {} "$BUILD1_DIR/" \;
34+
35+
echo "First build .rlib artifacts stored in: $BUILD1_DIR"
36+
ls -la "$BUILD1_DIR"
37+
38+
echo "--- Cleaning Build ---"
39+
# Clean the build to ensure we're starting fresh
40+
bazel clean
41+
42+
echo "--- Second Build ---"
43+
# Build again
44+
$BUILD_CMD "$TARGET"
45+
46+
# Copy second build .rlib artifacts
47+
echo "Copying second build .rlib artifacts..."
48+
BAZEL_BIN=$(bazel info bazel-bin)
49+
find "$BAZEL_BIN" -name "*.rlib" -type f -exec cp {} "$BUILD2_DIR/" \;
50+
51+
echo "Second build .rlib artifacts stored in: $BUILD2_DIR"
52+
ls -la "$BUILD2_DIR"
53+
54+
echo "--- Comparing Build Outputs ---"
55+
56+
# Compare .rlib files individually
57+
RESULT=0
58+
DIFFERENT_FILES=()
59+
60+
echo "Comparing .rlib files individually..."
61+
for rlib1 in "$BUILD1_DIR"/*.rlib; do
62+
if [ ! -f "$rlib1" ]; then
63+
echo "No .rlib files found in first build"
64+
RESULT=1
65+
break
66+
fi
67+
68+
filename=$(basename "$rlib1")
69+
rlib2="$BUILD2_DIR/$filename"
70+
71+
if [ ! -f "$rlib2" ]; then
72+
echo "❌ File missing in second build: $filename"
73+
RESULT=1
74+
continue
75+
fi
76+
77+
if ! diff "$rlib1" "$rlib2" > /dev/null; then
78+
echo "❌ DIFFERENCE FOUND: $filename"
79+
echo " Hash 1: $(sha256sum "$rlib1" | cut -d' ' -f1)"
80+
echo " Hash 2: $(sha256sum "$rlib2" | cut -d' ' -f1)"
81+
DIFFERENT_FILES+=("$filename")
82+
RESULT=1
83+
else
84+
echo "✅ Identical: $filename"
85+
fi
86+
done
87+
88+
if [ $RESULT -eq 0 ]; then
89+
echo ""
90+
echo "✅ SUCCESS: Builds are deterministic! All .rlib files are identical."
91+
else
92+
echo ""
93+
echo "❌ FAILURE: Builds are NOT deterministic."
94+
echo "Different .rlib files: ${#DIFFERENT_FILES[@]} out of $(ls -1 "$BUILD1_DIR"/*.rlib 2>/dev/null | wc -l)"
95+
96+
if [ ${#DIFFERENT_FILES[@]} -gt 0 ]; then
97+
echo ""
98+
echo "Debug commands for different files:"
99+
for filename in "${DIFFERENT_FILES[@]}"; do
100+
echo " For $filename:"
101+
echo " diffoscope '$BUILD1_DIR/$filename' '$BUILD2_DIR/$filename'"
102+
echo " Or upload to https://try.diffoscope.org/"
103+
done
104+
fi
105+
fi
106+
107+
echo ""
108+
echo "=== Test Complete ==="
109+
echo ".rlib artifacts stored in: $STORE_DIR"
110+
111+
exit $RESULT

0 commit comments

Comments
 (0)