Skip to content

Commit 989f690

Browse files
jeroenshikokuchuo
andauthored
Fix build on Windows ARM64 (#52)
* Fix build on Windows ARM64 * Make Rust install guidance arch-aware and fail fast on missing target Infer CARGO_TARGET before the toolchain checks so the install instructions can name the correct target for the architecture, and verify the target stdlib is present via rustc --print target-libdir before starting the lengthy vendor extraction and build. --------- Co-authored-by: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com>
1 parent c0ee1c5 commit 989f690

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

.github/workflows/R-CMD-check.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
config:
2222
- {os: macos-latest, r: 'release'}
2323
- {os: windows-latest, r: 'release'}
24+
- {os: windows-11-arm, r: 'release'}
2425
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
2526
- {os: ubuntu-latest, r: 'release'}
2627
- {os: ubuntu-latest, r: 'oldrel-1'}
@@ -32,6 +33,9 @@ jobs:
3233
steps:
3334
- uses: actions/checkout@v6
3435

36+
- if: ${{ runner.os == 'windows' && runner.arch == 'ARM64' }}
37+
run: rustup target add aarch64-pc-windows-gnullvm
38+
3539
- uses: r-lib/actions/setup-pandoc@v2
3640

3741
- uses: r-lib/actions/setup-r@v2

configure.win

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
5151
# Minimum Rust version required (MSRV)
5252
RUST_MSRV="1.85"
5353

54+
# Infer the Rust target from the C toolchain
55+
case `${CC} -dumpmachine 2>/dev/null` in
56+
aarch64*)
57+
CARGO_TARGET="aarch64-pc-windows-gnullvm"
58+
;;
59+
*)
60+
CARGO_TARGET="x86_64-pc-windows-gnu"
61+
;;
62+
esac
63+
echo "Using Rust target: ${CARGO_TARGET}"
64+
5465
# Check for Rust toolchain (cargo and rustc)
5566
# CRAN policy: check both system PATH and ~/.cargo/bin
5667
CARGO_CMD=""
@@ -82,7 +93,7 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
8293
echo ""
8394
echo "Install Rust from https://rustup.rs/:"
8495
echo " 1. Download and run rustup-init.exe"
85-
echo " 2. Select: x86_64-pc-windows-gnu toolchain"
96+
echo " 2. After installation run: rustup target add ${CARGO_TARGET}"
8697
echo ""
8798
echo "Or via winget: winget install Rustlang.Rustup"
8899
echo "Or via scoop: scoop install rustup"
@@ -110,9 +121,15 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
110121
exit 1
111122
fi
112123

113-
# Ensure GNU toolchain is used (required for Rtools compatibility)
114-
if ! $RUSTC_CMD --version --verbose 2>/dev/null | grep -q "host:.*windows-gnu"; then
115-
echo "Note: Configuring Rust to use GNU toolchain for Rtools compatibility"
124+
# Fail before the lengthy build if the target stdlib is absent
125+
TARGET_LIBDIR=$($RUSTC_CMD --print target-libdir --target "${CARGO_TARGET}" 2>/dev/null)
126+
if [ ! -d "$TARGET_LIBDIR" ]; then
127+
echo "-------------------- RUST TARGET NOT INSTALLED --------------------"
128+
echo "The Rust standard library for ${CARGO_TARGET} is not installed."
129+
echo ""
130+
echo "Install it with: rustup target add ${CARGO_TARGET}"
131+
echo "--------------------------------------------------------------------"
132+
exit 1
116133
fi
117134

118135
echo "Building automerge-c from bundled source..."
@@ -153,13 +170,10 @@ EOF
153170
xz -dc src/automerge/rust/vendor.tar.xz | tar -xf - -C src/automerge/rust
154171
fi
155172

156-
# Force GNU target to match MinGW toolchain from Rtools
157-
export RUSTUP_TOOLCHAIN="stable-x86_64-pc-windows-gnu"
158-
159173
echo "Building (this may take several minutes)..."
160174
# Default features select UTF-32 indexing; -j2 respects CRAN's core limit.
161175
# build.rs runs cbindgen to emit ${CBINDGEN_TARGET_DIR}/automerge.h.
162-
"$CARGO_CMD" build --release -j2 \
176+
"$CARGO_CMD" build --release -j2 --target "${CARGO_TARGET}" \
163177
--manifest-path src/automerge/rust/automerge-c/Cargo.toml \
164178
|| { echo "ERROR: cargo build failed"; exit 1; }
165179

@@ -178,7 +192,7 @@ EOF
178192
"$HEADER" > "${HEADER}.tmp" && mv "${HEADER}.tmp" "$HEADER"
179193

180194
# Stage the static library where Makevars expects it
181-
cp am-build/target/release/libautomerge_core.a am-install/lib/libautomerge.a \
195+
cp "am-build/target/${CARGO_TARGET}/release/libautomerge_core.a" am-install/lib/libautomerge.a \
182196
|| { echo "ERROR: failed to stage static library"; exit 1; }
183197

184198
rm -rf am-build

0 commit comments

Comments
 (0)