Skip to content

Commit 1c79a55

Browse files
authored
Fix cmake Rust concurrency issues (#97)
1 parent 0365227 commit 1c79a55

8 files changed

Lines changed: 28 additions & 56 deletions

File tree

.github/workflows/run-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ jobs:
4444
with:
4545
python-version: '3.x'
4646

47-
- name: Install Python dependencies
48-
run: pip install tomli
49-
5047
- name: Configure
5148
run: |
5249
mkdir build && cd build

CMakeLists.txt

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,12 @@ if (NOT RUSTC_LLVM_VERSION STREQUAL LLVM_PACKAGE_VERSION)
6464
endif()
6565

6666
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/rust-toolchain.cmake)
67-
68-
# rule-preprocessor uses nightly
69-
foreach(_dir libcc2rs rules)
70-
write_rust_toolchain(${CMAKE_SOURCE_DIR}/${_dir})
71-
endforeach()
72-
7367
set(RUST_STAMP_FILE "${CMAKE_BINARY_DIR}/.rust-toolchain.stamp")
7468

7569
add_custom_command(
7670
OUTPUT "${RUST_STAMP_FILE}"
7771
COMMAND rustup toolchain install ${RUST_STABLE_VERSION}
72+
COMMAND rustup toolchain install ${RUST_NIGHTLY_VERSION} --component rustc-dev --component llvm-tools
7873
COMMAND ${CMAKE_COMMAND} -E touch "${RUST_STAMP_FILE}"
7974
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/cmake/rust-toolchain.cmake"
8075
USES_TERMINAL
@@ -95,18 +90,18 @@ set(PYTHON_SOURCES ${PROJECT_SOURCE_DIR}/tests/lit/lit/formats/Cpp2RustTest.py)
9590

9691
add_custom_target("format"
9792
COMMAND ${CLANG_FORMAT} -i ${ALL_CXX_SOURCES}
98-
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/rules/Cargo.toml
99-
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml
100-
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs/Cargo.toml
101-
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs-macros/Cargo.toml
102-
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/rules
103-
cargo clippy --fix --allow-dirty --allow-staged --all-targets --all-features -- -Dwarnings
104-
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/rule-preprocessor
105-
cargo clippy --fix --allow-dirty --allow-staged --all-targets --all-features -- -Dwarnings
106-
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/libcc2rs
107-
cargo clippy --fix --allow-dirty --allow-staged --all-targets --all-features -- -Dwarnings
108-
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/libcc2rs-macros
109-
cargo clippy --fix --allow-dirty --allow-staged --all-targets --all-features -- -Dwarnings
93+
COMMAND cargo +${RUST_STABLE_VERSION} fmt --manifest-path ${PROJECT_SOURCE_DIR}/rules/Cargo.toml
94+
COMMAND cargo +${RUST_STABLE_VERSION} --manifest-path ${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml
95+
COMMAND cargo +${RUST_STABLE_VERSION} fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs/Cargo.toml
96+
COMMAND cargo +${RUST_STABLE_VERSION} fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs-macros/Cargo.toml
97+
COMMAND cargo +${RUST_STABLE_VERSION} clippy --fix --allow-dirty --allow-staged --all-targets --all-features
98+
--manifest-path ${PROJECT_SOURCE_DIR}/rules/Cargo.toml -- -Dwarnings
99+
COMMAND cargo +${RUST_STABLE_VERSION} clippy --fix --allow-dirty --allow-staged --all-targets --all-features
100+
--manifest-path chdir ${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml -- -Dwarnings
101+
COMMAND cargo +${RUST_STABLE_VERSION} clippy --fix --allow-dirty --allow-staged --all-targets --all-features
102+
--manifest-path $${PROJECT_SOURCE_DIR}/libcc2rs/Cargo.toml -- -Dwarnings
103+
COMMAND cargo +${RUST_STABLE_VERSION} clippy --fix --allow-dirty --allow-staged --all-targets --all-features
104+
--manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs-macros/Cargo.toml -- -Dwarnings
110105
COMMAND ${RUFF} --silent check --fix ${PYTHON_SOURCES}
111106
COMMAND ${RUFF} --silent format ${PYTHON_SOURCES}
112107
DEPENDS "${RUST_STAMP_FILE}"
@@ -119,7 +114,7 @@ if (NOT LIT_FLAGS)
119114
endif()
120115

121116
add_custom_target("check-libcc2rs"
122-
COMMAND cargo test
117+
COMMAND cargo +${RUST_STABLE_VERSION} test
123118
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/libcc2rs"
124119
DEPENDS "${RUST_STAMP_FILE}"
125120
USES_TERMINAL
@@ -188,12 +183,10 @@ file(GLOB_RECURSE rule_preprocessor_sources
188183

189184
add_custom_command(
190185
OUTPUT ${rust_rules_ir_outputs}
191-
COMMAND cargo build
192-
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/rule-preprocessor
193-
${CMAKE_COMMAND} -E env
194-
CARGO_TARGET_DIR=${PROJECT_SOURCE_DIR}/rule-preprocessor/target
195-
cargo run
196-
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/rules"
186+
COMMAND cargo +${RUST_STABLE_VERSION} build --release --manifest-path "${PROJECT_SOURCE_DIR}/rules/Cargo.toml"
187+
COMMAND ${CMAKE_COMMAND} -E env
188+
CARGO_TARGET_DIR="${CMAKE_CURRENT_BINARY_DIR}/target_preprocessor"
189+
cargo +${RUST_NIGHTLY_VERSION} run --manifest-path "${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml"
197190
DEPENDS ${rust_rules_inputs} ${rule_preprocessor_sources} "${RUST_STAMP_FILE}"
198191
VERBATIM
199192
)

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ while satisfying Rust's borrow checker through checked run-time operations.
3636
On Ubuntu, install the required dependencies with:
3737

3838
```bash
39-
sudo apt install libclang-22-dev clang++-22 ninja-build cmake python3-tomli
40-
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.95.0
39+
sudo apt install libclang-22-dev clang++-22 ninja-build cmake
4140
curl -LsSf https://astral.sh/ruff/install.sh | sh
4241
```
4342

cmake/rust-toolchain.cmake

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
11
set(RUST_STABLE_VERSION "1.95.0")
2-
3-
function(write_rust_toolchain DIR)
4-
set(_content "[toolchain]\nchannel = \"${RUST_STABLE_VERSION}\"\n")
5-
set(_file ${DIR}/rust-toolchain.toml)
6-
if (EXISTS ${_file})
7-
file(READ ${_file} _existing)
8-
endif()
9-
if (NOT "${_existing}" STREQUAL "${_content}")
10-
file(WRITE ${_file} ${_content})
11-
endif()
12-
endfunction()
2+
set(RUST_NIGHTLY_VERSION "nightly-2026-05-13")

libcc2rs/rust-toolchain.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

rule-preprocessor/rust-toolchain.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

rules/rust-toolchain.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
import re
1010
import shutil
1111
import random
12-
import tomli
1312

1413

1514
def read_rust_version():
1615
toolchain_path = os.path.join(
17-
os.path.dirname(__file__), "../../../../libcc2rs/rust-toolchain.toml"
16+
os.path.dirname(__file__), "../../../../cmake/rust-toolchain.cmake"
1817
)
19-
with open(toolchain_path, "rb") as f:
20-
return tomli.load(f)["toolchain"]["channel"]
18+
with open(toolchain_path, "r") as f:
19+
for line in f:
20+
m = re.match(r'set\(RUST_STABLE_VERSION\s+"([^"]+)', line)
21+
if m:
22+
return m.group(1)
23+
raise Exception("could not find rust version in " + toolchain_path)
2124

2225

2326
def shared_target_dir():
@@ -170,15 +173,12 @@ def fail(str, code=fail_code):
170173
pkg_name = "test_" + re.sub(r"[^a-zA-Z0-9_]", "_", os.path.basename(tmp_dir))
171174

172175
# Check if we can compile the rust file
173-
with open(tmp_dir + "/rust-toolchain.toml", "w") as f:
174-
f.write(f'[toolchain]\nchannel = "{self.rust_version}"\n')
175176
with open(tmp_dir + "/Cargo.toml", "w") as f:
176177
f.write(f"""
177178
[package]
178179
name = "{pkg_name}"
179180
version = "0.1.0"
180181
edition = "2021"
181-
rust-version = "{self.rust_version}"
182182
183183
[[bin]]
184184
name = "{pkg_name}"
@@ -189,7 +189,7 @@ def fail(str, code=fail_code):
189189
libcc2rs = {{ path = "../../../libcc2rs" }}
190190
""")
191191

192-
cmd = ["cargo", "build", "--release", "--quiet"]
192+
cmd = ["cargo", "+" + self.rust_version, "build", "--release", "--quiet"]
193193
_, err, returncode = lit.util.executeCommand(cmd, tmp_dir, env=cargo_env())
194194
if should_not_compile:
195195
if returncode != 0:

0 commit comments

Comments
 (0)