-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure.win
More file actions
217 lines (180 loc) · 8.05 KB
/
Copy pathconfigure.win
File metadata and controls
217 lines (180 loc) · 8.05 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
#!/bin/sh
AUTOMERGE_FOUND=0
PKG_CFLAGS=""
PKG_LIBS=""
# Find compiler and export flags
CC=`"${R_HOME}/bin/R" CMD config CC`
CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`
LDFLAGS=`"${R_HOME}/bin/R" CMD config LDFLAGS`
export CC CFLAGS LDFLAGS
# Helper function: check for UTF-32 indexing in system library
check_utf32_indexing() {
echo "#include <automerge-c/config.h>
#ifndef AUTOMERGE_C_UTF32
#error \"UTF-32 indexing is not enabled\"
#endif
int main(void) { return 0; }" | \
${CC} -I"$1" -xc - -c -o /dev/null 2>/dev/null
}
if [ "${AUTOMERGE_LIBS}" = "1" ]; then
echo "AUTOMERGE_LIBS=1: forcing bundled source compilation"
else
echo "Checking for system automerge installation..."
if [ -n "${RTOOLS_ROOT}" ]; then
if [ -f "${RTOOLS_ROOT}/usr/include/automerge-c/automerge.h" ] && \
[ -f "${RTOOLS_ROOT}/usr/lib/libautomerge.a" ]; then
echo "Found automerge in Rtools"
# Check if it's built with UTF-32 indexing via compilation test
if check_utf32_indexing "${RTOOLS_ROOT}/usr/include"; then
echo "Verified UTF-32 character indexing enabled"
PKG_CFLAGS="-I${RTOOLS_ROOT}/usr/include"
PKG_LIBS="-L${RTOOLS_ROOT}/usr/lib -lautomerge"
AUTOMERGE_FOUND=1
else
echo "Detected system automerge uses UTF-8 byte indexing"
echo "This package requires UTF-32 character indexing for natural R semantics"
echo "Will build from bundled source instead"
fi
fi
fi
fi
if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
echo "System automerge not found, building from bundled source..."
# Minimum Rust version required (MSRV)
RUST_MSRV="1.85"
# Infer the Rust target from the C toolchain
case `${CC} -dumpmachine 2>/dev/null` in
aarch64*)
CARGO_TARGET="aarch64-pc-windows-gnullvm"
;;
*)
CARGO_TARGET="x86_64-pc-windows-gnu"
;;
esac
echo "Using Rust target: ${CARGO_TARGET}"
# Check for Rust toolchain (cargo and rustc)
# CRAN policy: check both system PATH and ~/.cargo/bin
CARGO_CMD=""
RUSTC_CMD=""
# First check PATH
if command -v cargo >/dev/null 2>&1; then
CARGO_CMD="cargo"
fi
if command -v rustc >/dev/null 2>&1; then
RUSTC_CMD="rustc"
fi
# Also check ~/.cargo/bin (rustup default location, often not on PATH)
# On Windows, this is typically %USERPROFILE%\.cargo\bin
CARGO_HOME="${CARGO_HOME:-$USERPROFILE/.cargo}"
if [ -z "$CARGO_CMD" ] && [ -x "$CARGO_HOME/bin/cargo.exe" ]; then
CARGO_CMD="$CARGO_HOME/bin/cargo"
export PATH="$CARGO_HOME/bin:$PATH"
fi
if [ -z "$RUSTC_CMD" ] && [ -x "$CARGO_HOME/bin/rustc.exe" ]; then
RUSTC_CMD="$CARGO_HOME/bin/rustc"
fi
# Verify both cargo and rustc are found
if [ -z "$CARGO_CMD" ] || [ -z "$RUSTC_CMD" ]; then
echo "-------------------- RUST TOOLCHAIN NOT FOUND --------------------"
echo "This package requires Rust >= $RUST_MSRV to build from source."
echo ""
echo "Install Rust from https://rustup.rs/:"
echo " 1. Download and run rustup-init.exe"
echo " 2. After installation run: rustup target add ${CARGO_TARGET}"
echo ""
echo "Or via winget: winget install Rustlang.Rustup"
echo "Or via scoop: scoop install rustup"
echo ""
echo "After installation, you may need to restart your terminal."
echo "Alternatively, install automerge-c to a standard location."
echo "------------------------------------------------------------------"
exit 1
fi
# Check Rust version meets minimum requirement
RUST_VERSION=$($RUSTC_CMD --version | sed -n 's/rustc \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p')
RUST_MAJOR=$(echo $RUST_VERSION | cut -d. -f1)
RUST_MINOR=$(echo $RUST_VERSION | cut -d. -f2)
MSRV_MAJOR=$(echo $RUST_MSRV | cut -d. -f1)
MSRV_MINOR=$(echo $RUST_MSRV | cut -d. -f2)
if [ "$RUST_MAJOR" -lt "$MSRV_MAJOR" ] || \
{ [ "$RUST_MAJOR" -eq "$MSRV_MAJOR" ] && [ "$RUST_MINOR" -lt "$MSRV_MINOR" ]; }; then
echo "-------------------- RUST VERSION TOO OLD --------------------"
echo "Found Rust $RUST_VERSION but this package requires Rust >= $RUST_MSRV"
echo ""
echo "Update Rust with: rustup update stable"
echo "---------------------------------------------------------------"
exit 1
fi
# Fail before the lengthy build if the target stdlib is absent
TARGET_LIBDIR=$($RUSTC_CMD --print target-libdir --target "${CARGO_TARGET}" 2>/dev/null)
if [ ! -d "$TARGET_LIBDIR" ]; then
echo "-------------------- RUST TARGET NOT INSTALLED --------------------"
echo "The Rust standard library for ${CARGO_TARGET} is not installed."
echo ""
echo "Install it with: rustup target add ${CARGO_TARGET}"
echo "--------------------------------------------------------------------"
exit 1
fi
echo "Building automerge-c from bundled source..."
echo "using Rust package manager: '$($CARGO_CMD --version)'"
echo "using Rust compiler: '$($RUSTC_CMD --version)'"
rm -rf am-build am-install
# Set TMPDIR to avoid rustix test file warning in R CMD check
export TMPDIR="${PWD}/am-build/tmp"
mkdir -p "$TMPDIR"
# Set CARGO_HOME to prevent writes to ~/.cargo (CRAN policy)
export CARGO_HOME="${PWD}/am-build/.cargo"
mkdir -p "$CARGO_HOME"
# Keep Cargo's build artefacts inside the package build tree
export CARGO_TARGET_DIR="${PWD}/am-build/target"
# automerge-c's build.rs writes the cbindgen-generated header here
export CBINDGEN_TARGET_DIR="${PWD}/am-install/include/automerge-c"
mkdir -p "$CBINDGEN_TARGET_DIR" am-install/lib
# Generate vendor config with absolute path (env vars don't work for source replacement)
# Use pwd -W to get Windows-style path (MSYS2 bash built-in)
WIN_PWD=$(pwd -W)
cat > "$CARGO_HOME/config.toml" << EOF
[source.crates-io]
replace-with = "vendored-sources"
[source.vendored-sources]
directory = "${WIN_PWD}/src/automerge/rust/vendor"
EOF
# Extract vendored Rust dependencies
if [ -f "src/automerge/rust/vendor.tar.xz" ] && [ ! -d "src/automerge/rust/vendor" ]; then
echo "Extracting vendored Rust dependencies..."
xz -dc src/automerge/rust/vendor.tar.xz | tar -xf - -C src/automerge/rust
fi
echo "Building (this may take several minutes)..."
# Default features select UTF-32 indexing; -j2 respects CRAN's core limit.
# build.rs runs cbindgen to emit ${CBINDGEN_TARGET_DIR}/automerge.h.
"$CARGO_CMD" build --release -j2 --target "${CARGO_TARGET}" \
--manifest-path src/automerge/rust/automerge-c/Cargo.toml \
|| { echo "ERROR: cargo build failed"; exit 1; }
HEADER="am-install/include/automerge-c/automerge.h"
if [ ! -f "$HEADER" ]; then
echo "ERROR: cbindgen did not generate ${HEADER}"
exit 1
fi
# Post-process the cbindgen header (work previously done by CMake):
# 1. cbindgen renders consecutive capitals as "A_M..."; restore "AM_...".
# 2. cbindgen ignores size_of::<usize>(); substitute the pointer size.
PTR_SIZE=`"${R_HOME}/bin/Rscript" -e 'cat(.Machine$sizeof.pointer)'`
sed -e 's/A_M\([^_][^_]*\)_/AM_\1_/g' \
-e "s/USIZE_/+${PTR_SIZE}/g" \
"$HEADER" > "${HEADER}.tmp" && mv "${HEADER}.tmp" "$HEADER"
# Stage the static library where Makevars expects it
cp "am-build/target/${CARGO_TARGET}/release/libautomerge_core.a" am-install/lib/libautomerge.a \
|| { echo "ERROR: failed to stage static library"; exit 1; }
rm -rf am-build
echo "Successfully built automerge-c from source"
PKG_CFLAGS="-I../am-install/include"
PKG_LIBS="../am-install/lib/libautomerge.a"
fi
PKG_LIBS="${PKG_LIBS} -lws2_32 -luserenv -lbcrypt -lntdll"
echo "Configuration:"
echo " PKG_CFLAGS: ${PKG_CFLAGS}"
echo " PKG_LIBS: ${PKG_LIBS}"
sed -e "s|@PKG_CFLAGS@|${PKG_CFLAGS}|" \
-e "s|@PKG_LIBS@|${PKG_LIBS}|" \
src/Makevars.win.in > src/Makevars.win
exit 0