Skip to content

Commit b4d08b5

Browse files
authored
Build bundled library with Cargo directly, drop CMake (#48)
* Build bundled library with Cargo directly, drop CMake CMake was only orchestrating Cargo: it ran `cargo build` (build.rs runs cbindgen to emit the header), applied two textual fix-ups to that header, and merged static archives. The merge folded in C utility objects this package never links, and config.h / enum-string codegen are unused here. Replace the CMake invocation in configure/configure.win with a direct `cargo build --release`, reproduce the two header fix-ups with sed (A_M... -> AM_..., USIZE_ -> pointer size via .Machine$sizeof.pointer), and stage libautomerge_core.a as am-install/lib/libautomerge.a. The install layout is unchanged, so Makevars need no edits. Express the bundled-source changes in the tools workflow: patch-sources.sh now generates Cargo.toml and cbindgen.toml from the upstream cmake/*.in templates (as CMake's configure_file did) and removes CMakeLists.txt and cmake/, instead of patching CMakeLists.txt. The obsolete CMakeLists patches are dropped from patch-rust-msrv.sh and vendor-deps.sh. The generated TOMLs reproduce the committed ones byte-for-byte. Drop CMake >= 3.25 from SystemRequirements; Rust (Cargo + rustc >= 1.85) is now the only build toolchain. Update README, vignette, NEWS, cleanup. * Update news
1 parent 32f3700 commit b4d08b5

20 files changed

Lines changed: 127 additions & 1247 deletions

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ Config/roxygen2/markdown: TRUE
3838
Config/roxygen2/version: 8.0.0
3939
Config/testthat/edition: 3
4040
Encoding: UTF-8
41-
SystemRequirements: 'automerge-c', or Cargo (Rust's package manager),
42-
rustc >= 1.85 and CMake >= 3.25 to build from package sources.
41+
SystemRequirements: 'automerge-c', or Cargo (Rust's package manager) and
42+
rustc >= 1.85 to build from package sources.

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# automerge (development version)
22

3+
* Building the bundled automerge library from source no longer requires CMake.
4+
35
* Updates vendored automerge library to 0.10.0.
46

57
# automerge 0.4.0

README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bob
7070
install.packages("automerge")
7171
```
7272

73-
Building from source requires Rust >= 1.85 ([rustup.rs](https://rustup.rs/)) and CMake >= 3.25 (included in Rtools43+ on Windows).
73+
Building from source requires Rust >= 1.85 ([rustup.rs](https://rustup.rs/); included in Rtools43+ on Windows).
7474

7575
## Documentation
7676

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ install.packages("automerge")
8585
```
8686

8787
Building from source requires Rust \>= 1.85
88-
([rustup.rs](https://rustup.rs/)) and CMake \>= 3.25 (included in
89-
Rtools43+ on Windows).
88+
([rustup.rs](https://rustup.rs/); included in Rtools43+ on Windows).
9089

9190
## Documentation
9291

cleanup

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
rm -f src/Makevars src/Makevars.win
44
rm -f src/*.o src/*.so src/*.dll src/symbols.rds
55
rm -rf am-build am-install
6-
rm -rf src/automerge/rust/automerge-c/build
7-
rm -f src/automerge/rust/automerge-c/cmake_config.log
8-
rm -f src/automerge/rust/automerge-c/cmake_build.log
96
rm -rf src/automerge/rust/target
107
rm -rf src/automerge/rust/vendor

configure

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`
1010
LDFLAGS=`"${R_HOME}/bin/R" CMD config LDFLAGS`
1111
export CC CFLAGS LDFLAGS
1212

13-
# Helper function: detect cmake
14-
detect_cmake() {
15-
if command -v cmake >/dev/null 2>&1; then
16-
return 0
17-
fi
18-
export PATH=$PATH:/Applications/CMake.app/Contents/bin
19-
if command -v cmake >/dev/null 2>&1; then
20-
return 0
21-
fi
22-
echo "ERROR: CMake not found"
23-
exit 1
24-
}
25-
2613
# Helper function: check for UTF-32 indexing in system library
2714
check_utf32_indexing() {
2815
echo "#include <automerge-c/config.h>
@@ -146,9 +133,7 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
146133
exit 1
147134
fi
148135

149-
detect_cmake
150-
151-
echo "Building automerge-c..."
136+
echo "Building automerge-c from bundled source..."
152137
echo "using Rust package manager: '$($CARGO_CMD --version)'"
153138
echo "using Rust compiler: '$($RUSTC_CMD --version)'"
154139

@@ -162,6 +147,13 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
162147
export CARGO_HOME="${PWD}/am-build/.cargo"
163148
mkdir -p "$CARGO_HOME"
164149

150+
# Keep Cargo's build artefacts inside the package build tree
151+
export CARGO_TARGET_DIR="${PWD}/am-build/target"
152+
153+
# automerge-c's build.rs writes the cbindgen-generated header here
154+
export CBINDGEN_TARGET_DIR="${PWD}/am-install/include/automerge-c"
155+
mkdir -p "$CBINDGEN_TARGET_DIR" am-install/lib
156+
165157
# Generate vendor config with absolute path (env vars don't work for source replacement)
166158
cat > "$CARGO_HOME/config.toml" << EOF
167159
[source.crates-io]
@@ -171,36 +163,38 @@ replace-with = "vendored-sources"
171163
directory = "${PWD}/src/automerge/rust/vendor"
172164
EOF
173165

174-
# Extract vendored Rust dependencies (CMakeLists.txt configures Cargo to use them)
166+
# Extract vendored Rust dependencies
175167
if [ -f "src/automerge/rust/vendor.tar.xz" ] && [ ! -d "src/automerge/rust/vendor" ]; then
176168
echo "Extracting vendored Rust dependencies..."
177169
xz -dc src/automerge/rust/vendor.tar.xz | tar -xf - -C src/automerge/rust
178170
fi
179171

180-
cmake -S src/automerge/rust/automerge-c -B am-build \
181-
-DCMAKE_BUILD_TYPE=Release \
182-
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
183-
-DCMAKE_INSTALL_PREFIX="${PWD}/am-install" \
184-
-DCMAKE_INSTALL_LIBDIR=lib \
185-
-DBUILD_TESTING=OFF \
186-
-DCMAKE_COLOR_MAKEFILE=OFF \
187-
-DCMAKE_INSTALL_MESSAGE=NEVER \
188-
-DUTF32_INDEXING=ON \
189-
|| { echo "ERROR: CMake configuration failed"; exit 1; }
190-
191172
echo "Building (this may take several minutes)..."
192-
cmake --build am-build --config Release \
193-
|| { echo "ERROR: Build failed"; exit 1; }
173+
# Default features select UTF-32 indexing; -j2 respects CRAN's core limit.
174+
# build.rs runs cbindgen to emit ${CBINDGEN_TARGET_DIR}/automerge.h.
175+
"$CARGO_CMD" build --release -j2 \
176+
--manifest-path src/automerge/rust/automerge-c/Cargo.toml \
177+
|| { echo "ERROR: cargo build failed"; exit 1; }
178+
179+
HEADER="am-install/include/automerge-c/automerge.h"
180+
if [ ! -f "$HEADER" ]; then
181+
echo "ERROR: cbindgen did not generate ${HEADER}"
182+
exit 1
183+
fi
194184

195-
cmake --build am-build --target install \
196-
|| { echo "ERROR: Install failed"; exit 1; }
185+
# Post-process the cbindgen header (work previously done by CMake):
186+
# 1. cbindgen renders consecutive capitals as "A_M..."; restore "AM_...".
187+
# 2. cbindgen ignores size_of::<usize>(); substitute the pointer size.
188+
PTR_SIZE=`"${R_HOME}/bin/Rscript" -e 'cat(.Machine$sizeof.pointer)'`
189+
sed -e 's/A_M\([^_][^_]*\)_/AM_\1_/g' \
190+
-e "s/USIZE_/+${PTR_SIZE}/g" \
191+
"$HEADER" > "${HEADER}.tmp" && mv "${HEADER}.tmp" "$HEADER"
197192

198-
rm -rf am-build
193+
# Stage the static library where Makevars expects it
194+
cp am-build/target/release/libautomerge_core.a am-install/lib/libautomerge.a \
195+
|| { echo "ERROR: failed to stage static library"; exit 1; }
199196

200-
if [ ! -d "am-install/lib" ]; then
201-
echo "ERROR: Installation did not create expected lib directory"
202-
exit 1
203-
fi
197+
rm -rf am-build
204198

205199
echo "Successfully built automerge-c from source"
206200

configure.win

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`
1010
LDFLAGS=`"${R_HOME}/bin/R" CMD config LDFLAGS`
1111
export CC CFLAGS LDFLAGS
1212

13-
# Helper function: detect cmake
14-
detect_cmake() {
15-
if command -v cmake >/dev/null 2>&1; then
16-
return 0
17-
fi
18-
echo "ERROR: CMake not found"
19-
echo "CMake is included in Rtools43+"
20-
exit 1
21-
}
22-
2313
# Helper function: check for UTF-32 indexing in system library
2414
check_utf32_indexing() {
2515
echo "#include <automerge-c/config.h>
@@ -125,9 +115,7 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
125115
echo "Note: Configuring Rust to use GNU toolchain for Rtools compatibility"
126116
fi
127117

128-
detect_cmake
129-
130-
echo "Building automerge-c..."
118+
echo "Building automerge-c from bundled source..."
131119
echo "using Rust package manager: '$($CARGO_CMD --version)'"
132120
echo "using Rust compiler: '$($RUSTC_CMD --version)'"
133121

@@ -141,6 +129,13 @@ if [ ${AUTOMERGE_FOUND} -eq 0 ]; then
141129
export CARGO_HOME="${PWD}/am-build/.cargo"
142130
mkdir -p "$CARGO_HOME"
143131

132+
# Keep Cargo's build artefacts inside the package build tree
133+
export CARGO_TARGET_DIR="${PWD}/am-build/target"
134+
135+
# automerge-c's build.rs writes the cbindgen-generated header here
136+
export CBINDGEN_TARGET_DIR="${PWD}/am-install/include/automerge-c"
137+
mkdir -p "$CBINDGEN_TARGET_DIR" am-install/lib
138+
144139
# Generate vendor config with absolute path (env vars don't work for source replacement)
145140
# Use pwd -W to get Windows-style path (MSYS2 bash built-in)
146141
WIN_PWD=$(pwd -W)
@@ -152,7 +147,7 @@ replace-with = "vendored-sources"
152147
directory = "${WIN_PWD}/src/automerge/rust/vendor"
153148
EOF
154149

155-
# Extract vendored Rust dependencies (CMakeLists.txt configures Cargo to use them)
150+
# Extract vendored Rust dependencies
156151
if [ -f "src/automerge/rust/vendor.tar.xz" ] && [ ! -d "src/automerge/rust/vendor" ]; then
157152
echo "Extracting vendored Rust dependencies..."
158153
xz -dc src/automerge/rust/vendor.tar.xz | tar -xf - -C src/automerge/rust
@@ -161,31 +156,32 @@ EOF
161156
# Force GNU target to match MinGW toolchain from Rtools
162157
export RUSTUP_TOOLCHAIN="stable-x86_64-pc-windows-gnu"
163158

164-
cmake -G "Unix Makefiles" \
165-
-S src/automerge/rust/automerge-c -B am-build \
166-
-DCMAKE_BUILD_TYPE=Release \
167-
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
168-
-DCMAKE_INSTALL_PREFIX="${PWD}/am-install" \
169-
-DCMAKE_INSTALL_LIBDIR=lib \
170-
-DBUILD_TESTING=OFF \
171-
-DCMAKE_COLOR_MAKEFILE=OFF \
172-
-DCMAKE_INSTALL_MESSAGE=NEVER \
173-
-DUTF32_INDEXING=ON \
174-
|| { echo "ERROR: CMake configuration failed"; exit 1; }
175-
176159
echo "Building (this may take several minutes)..."
177-
cmake --build am-build --config Release \
178-
|| { echo "ERROR: Build failed"; exit 1; }
160+
# Default features select UTF-32 indexing; -j2 respects CRAN's core limit.
161+
# build.rs runs cbindgen to emit ${CBINDGEN_TARGET_DIR}/automerge.h.
162+
"$CARGO_CMD" build --release -j2 \
163+
--manifest-path src/automerge/rust/automerge-c/Cargo.toml \
164+
|| { echo "ERROR: cargo build failed"; exit 1; }
165+
166+
HEADER="am-install/include/automerge-c/automerge.h"
167+
if [ ! -f "$HEADER" ]; then
168+
echo "ERROR: cbindgen did not generate ${HEADER}"
169+
exit 1
170+
fi
179171

180-
cmake --build am-build --target install \
181-
|| { echo "ERROR: Install failed"; exit 1; }
172+
# Post-process the cbindgen header (work previously done by CMake):
173+
# 1. cbindgen renders consecutive capitals as "A_M..."; restore "AM_...".
174+
# 2. cbindgen ignores size_of::<usize>(); substitute the pointer size.
175+
PTR_SIZE=`"${R_HOME}/bin/Rscript" -e 'cat(.Machine$sizeof.pointer)'`
176+
sed -e 's/A_M\([^_][^_]*\)_/AM_\1_/g' \
177+
-e "s/USIZE_/+${PTR_SIZE}/g" \
178+
"$HEADER" > "${HEADER}.tmp" && mv "${HEADER}.tmp" "$HEADER"
182179

183-
rm -rf am-build
180+
# Stage the static library where Makevars expects it
181+
cp am-build/target/release/libautomerge_core.a am-install/lib/libautomerge.a \
182+
|| { echo "ERROR: failed to stage static library"; exit 1; }
184183

185-
if [ ! -d "am-install/lib" ]; then
186-
echo "ERROR: Installation did not create expected lib directory"
187-
exit 1
188-
fi
184+
rm -rf am-build
189185

190186
echo "Successfully built automerge-c from source"
191187

0 commit comments

Comments
 (0)