Skip to content

Commit ec9b793

Browse files
committed
Build all of Rust in the build directory and avoid double build on format
1 parent c4f5751 commit ec9b793

9 files changed

Lines changed: 56 additions & 45 deletions

File tree

‎.gitignore‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
build/
33
_buildconfig.py
44

5-
# Cargo
6-
target/
7-
Cargo.lock
85
rules/rustls/rustls.h
96
rules/brotli/brotli/
107

@@ -20,10 +17,3 @@ __pycache__
2017

2118
tmp
2219
*.swp
23-
24-
# Merge/rebase conflict artifacts
25-
*.orig
26-
*.BACKUP.*
27-
*.BASE.*
28-
*.LOCAL.*
29-
*.REMOTE.*

‎CMakeLists.txt‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ add_custom_command(
6767
VERBATIM
6868
)
6969

70+
function(set_cargo_env var crate)
71+
set(_dir "${CMAKE_BINARY_DIR}/${crate}-target")
72+
set(${var} ${CMAKE_COMMAND} -E env
73+
CARGO_TARGET_DIR=${_dir}
74+
CARGO_RESOLVER_LOCKFILE_PATH=${_dir}/Cargo.lock
75+
PARENT_SCOPE)
76+
endfunction()
77+
78+
set_cargo_env(CARGO_ENV_RULES rules)
79+
set_cargo_env(CARGO_ENV_RULE_PREPROCESSOR rule-preprocessor)
80+
set_cargo_env(CARGO_ENV_LIBCC2RS libcc2rs)
81+
set_cargo_env(CARGO_ENV_LIBCC2RS_MACROS libcc2rs-macros)
82+
set_cargo_env(CARGO_ENV_LIBC_DEP libc-dep)
83+
7084
add_subdirectory(cpp2rust)
7185
add_subdirectory(libc-dep)
7286
add_subdirectory(libcc2rs)
@@ -86,13 +100,17 @@ add_custom_target("format"
86100
COMMAND cargo +${RUST_NIGHTLY_VERSION} fmt --manifest-path ${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml
87101
COMMAND cargo +${RUST_STABLE_VERSION} fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs/Cargo.toml
88102
COMMAND cargo +${RUST_STABLE_VERSION} fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs-macros/Cargo.toml
89-
COMMAND cargo +${RUST_STABLE_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
103+
COMMAND ${CARGO_ENV_RULES}
104+
cargo +${RUST_STABLE_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
90105
--manifest-path ${PROJECT_SOURCE_DIR}/rules/Cargo.toml -- -Dwarnings
91-
COMMAND cargo +${RUST_NIGHTLY_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
106+
COMMAND ${CARGO_ENV_RULE_PREPROCESSOR}
107+
cargo +${RUST_NIGHTLY_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
92108
--manifest-path ${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml -- -Dwarnings
93-
COMMAND cargo +${RUST_STABLE_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
109+
COMMAND ${CARGO_ENV_LIBCC2RS}
110+
cargo +${RUST_STABLE_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
94111
--manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs/Cargo.toml -- -Dwarnings
95-
COMMAND cargo +${RUST_STABLE_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
112+
COMMAND ${CARGO_ENV_LIBCC2RS_MACROS}
113+
cargo +${RUST_STABLE_VERSION} clippy --release --fix --allow-dirty --allow-no-vcs --allow-staged --all-targets --all-features
96114
--manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs-macros/Cargo.toml -- -Dwarnings
97115
COMMAND ${RUFF} --silent check --fix ${PYTHON_SOURCES}
98116
COMMAND ${RUFF} --silent format ${PYTHON_SOURCES}
@@ -107,14 +125,16 @@ if (NOT LIT_FLAGS)
107125
endif()
108126

109127
add_custom_target("check-libcc2rs"
110-
COMMAND cargo +${RUST_STABLE_VERSION} test
128+
COMMAND ${CARGO_ENV_LIBCC2RS}
129+
cargo +${RUST_STABLE_VERSION} test
111130
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/libcc2rs"
112131
DEPENDS "${RUST_STAMP_FILE}"
113132
USES_TERMINAL
114133
)
115134

116135
add_custom_target("check-libcc2rs-macros"
117-
COMMAND cargo +${RUST_STABLE_VERSION} test
136+
COMMAND ${CARGO_ENV_LIBCC2RS_MACROS}
137+
cargo +${RUST_STABLE_VERSION} test
118138
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/libcc2rs-macros"
119139
DEPENDS "${RUST_STAMP_FILE}"
120140
USES_TERMINAL
@@ -147,8 +167,8 @@ set(BROTLI_HEADERS
147167
"${PROJECT_SOURCE_DIR}/rules/brotli/brotli/port.h"
148168
"${PROJECT_SOURCE_DIR}/rules/brotli/brotli/types.h")
149169

150-
set(PREPROCESSOR_BIN "${CMAKE_BINARY_DIR}/target_preprocessor/release/rule-preprocessor")
151-
set(RULES_LIB "${PROJECT_SOURCE_DIR}/rules/target/release/librules.rlib")
170+
set(PREPROCESSOR_BIN "${CMAKE_BINARY_DIR}/rule-preprocessor-target/release/rule-preprocessor")
171+
set(RULES_LIB "${CMAKE_BINARY_DIR}/rules-target/release/librules.rlib")
152172

153173
file(GLOB_RECURSE rule_preprocessor_sources
154174
${PROJECT_SOURCE_DIR}/rule-preprocessor/src/*.rs)
@@ -169,20 +189,20 @@ execute_process(
169189

170190
add_custom_command(
171191
OUTPUT ${RUSTLS_HEADER} ${BROTLI_HEADERS} ${RULES_LIB}
172-
COMMAND cargo +${RUST_STABLE_VERSION} build --release
192+
COMMAND ${CARGO_ENV_RULES}
193+
cargo +${RUST_STABLE_VERSION} build --release
173194
--manifest-path "${PROJECT_SOURCE_DIR}/rules/Cargo.toml"
174195
DEPENDS "${PROJECT_SOURCE_DIR}/rules/build.rs"
175196
"${PROJECT_SOURCE_DIR}/rules/Cargo.toml"
176197
${libcc2rs_sources}
177198
"${RUST_STAMP_FILE}"
178-
DEPFILE "${PROJECT_SOURCE_DIR}/rules/target/release/librules.d"
199+
DEPFILE "${CMAKE_BINARY_DIR}/rules-target/release/librules.d"
179200
VERBATIM
180201
)
181202

182203
add_custom_command(
183204
OUTPUT ${PREPROCESSOR_BIN}
184-
COMMAND ${CMAKE_COMMAND} -E env
185-
CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
205+
COMMAND ${CARGO_ENV_RULE_PREPROCESSOR}
186206
cargo +${RUST_NIGHTLY_VERSION} build --release
187207
--manifest-path "${PROJECT_SOURCE_DIR}/rule-preprocessor/Cargo.toml"
188208
DEPENDS ${rule_preprocessor_sources}
@@ -236,8 +256,7 @@ foreach(_rule_dir IN LISTS rule_subdirs)
236256
add_custom_command(
237257
OUTPUT ${_rust_out}
238258
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
239-
COMMAND ${CMAKE_COMMAND} -E env
240-
CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
259+
COMMAND ${CARGO_ENV_RULE_PREPROCESSOR}
241260
RUSTUP_TOOLCHAIN=${RUST_NIGHTLY_VERSION}
242261
LD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
243262
DYLD_LIBRARY_PATH=${NIGHTLY_SYSROOT}/lib
@@ -254,10 +273,9 @@ add_custom_target("preprocess-rust-rules" ALL
254273
DEPENDS ${rust_rules_ir_outputs})
255274

256275
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES
257-
"${PROJECT_SOURCE_DIR}/libcc2rs/target"
258-
"${PROJECT_SOURCE_DIR}/libcc2rs-macros/target"
259-
"${PROJECT_SOURCE_DIR}/libc-dep/target"
260-
"${PROJECT_SOURCE_DIR}/rules/target"
261-
"${PROJECT_SOURCE_DIR}/rule-preprocessor/target"
262-
"${CMAKE_BINARY_DIR}/target_preprocessor"
276+
"${CMAKE_BINARY_DIR}/libcc2rs-target"
277+
"${CMAKE_BINARY_DIR}/libcc2rs-macros-target"
278+
"${CMAKE_BINARY_DIR}/libc-dep-target"
279+
"${CMAKE_BINARY_DIR}/rule-preprocessor-target"
280+
"${CMAKE_BINARY_DIR}/rules-target"
263281
)

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Cpp2Rust's algorithm is described in the paper
88
[Cpp2Rust: Automatic Translation of C++ to Safe Rust](https://web.ist.utl.pt/nuno.lopes/pubs/cpp2rust-pldi26.pdf)
99
published at PLDI 2026.
1010

11+
The [developer's manual](https://cpp2rust.github.io/cpp2rust/) describes how
12+
Cpp2Rust works internally and how to extend it.
13+
1114

1215
## Overview
1316

@@ -92,7 +95,7 @@ fn main_0() -> i32 {
9295
Compile and run with:
9396

9497
```bash
95-
rustc hello.rs -L ../libcc2rs/target/debug
98+
rustc hello.rs -L build/libcc2rs-target/release
9699
./hello
97100
```
98101

‎cmake/generate-buildconfig-py.cmake‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from pathlib import Path
88
99
CXX = Path(r"@CMAKE_CXX_COMPILER@")
1010
CC = Path(r"@CMAKE_C_COMPILER@")
11+
BUILD_DIR = Path(r"@CMAKE_BINARY_DIR@")
1112
]]
1213
ESCAPE_QUOTES
1314
@ONLY

‎docs/src/project/usage.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn main_0() -> i32 {
3838
Compile and run with:
3939

4040
```bash
41-
rustc hello.rs -L ../libcc2rs/target/debug
41+
rustc hello.rs -L build/libcc2rs-target/release
4242
./hello
4343
```
4444

‎docs/src/rules/preprocessors.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ The environment is load-bearing:
8787
CMake drives all of this via the `preprocess-rust-rules` target: it first builds
8888
the `rules` crate with the stable toolchain (which also regenerates
8989
`rules/src/modules.rs`), then runs the preprocessor with
90-
`CARGO_TARGET_DIR=<build>/target_preprocessor`. That initial `cargo build` of
91-
the `rules` crate is what actually gates the build on rule bodies type-checking
92-
(see below). The preprocessor works in two phases.
90+
`CARGO_TARGET_DIR=<build>/rule-preprocessor-target`. That initial `cargo build`
91+
of the `rules` crate is what actually gates the build on rule bodies
92+
type-checking (see below). The preprocessor works in two phases.
9393

9494
**Phase 1, syntactic.** Each `tgt_*.rs` file is parsed with rust-analyzer's
9595
parser, and functions whose `#[cfg]` does not match the host are dropped. Every

‎libc-dep/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
set(LIBC_DEP_DIR ${CMAKE_SOURCE_DIR}/libc-dep)
2-
set(LIBC_DEP_OUTPUT ${LIBC_DEP_DIR}/target/release/libc_dep.rlib)
2+
set(LIBC_DEP_OUTPUT ${CMAKE_BINARY_DIR}/libc-dep-target/release/liblibc_dep.rlib)
33

44
add_custom_command(
55
OUTPUT ${LIBC_DEP_OUTPUT}
6-
COMMAND cargo +${RUST_STABLE_VERSION} build --release
6+
COMMAND ${CARGO_ENV_LIBC_DEP} cargo +${RUST_STABLE_VERSION} build --release
77
WORKING_DIRECTORY ${LIBC_DEP_DIR}
88
DEPENDS "${RUST_STAMP_FILE}" ${LIBC_DEP_DIR}/Cargo.toml
99
)

‎libcc2rs/CMakeLists.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
set(LIBCC2RC_LIB_DIR ${CMAKE_SOURCE_DIR}/libcc2rs)
22
set(LIBCC2RC_MACROS_LIB_DIR ${CMAKE_SOURCE_DIR}/libcc2rs-macros)
3-
set(LIBCC2RS_RLIB ${LIBCC2RC_LIB_DIR}/target/release/liblibcc2rs.rlib)
4-
set(LIBCC2RS_MACROS_RLIB ${LIBCC2RC_LIB_DIR}/target/release/liblibcc2rs-macros.rlib)
3+
set(LIBCC2RS_RLIB ${CMAKE_BINARY_DIR}/libcc2rs-target/release/liblibcc2rs.rlib)
4+
set(LIBCC2RS_MACROS_RLIB ${CMAKE_BINARY_DIR}/libcc2rs-macros-target/release/liblibcc2rs-macros.rlib)
55
file(GLOB_RECURSE LIBCC2RS_SOURCES "${LIBCC2RC_LIB_DIR}/src/*.rs")
66
file(GLOB_RECURSE LIBCC2RS_MACROS_SOURCES "${LIBCC2RC_MACROS_LIB_DIR}/src/*.rs")
77
file(GLOB LIBCC2RS_RULE_SHIMS CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/rules/*/shim.rs")
88

99
add_custom_command(
1010
OUTPUT ${LIBCC2RS_RLIB}
11-
COMMAND cargo +${RUST_STABLE_VERSION} build --release
11+
COMMAND ${CARGO_ENV_LIBCC2RS} cargo +${RUST_STABLE_VERSION} build --release
1212
WORKING_DIRECTORY ${LIBCC2RC_LIB_DIR}
1313
DEPENDS
1414
"${RUST_STAMP_FILE}"
@@ -22,7 +22,7 @@ add_custom_command(
2222

2323
add_custom_command(
2424
OUTPUT ${LIBCC2RS_MACROS_RLIB}
25-
COMMAND cargo +${RUST_STABLE_VERSION} build --release
25+
COMMAND ${CARGO_ENV_LIBCC2RS_MACROS} cargo +${RUST_STABLE_VERSION} build --release
2626
WORKING_DIRECTORY ${LIBCC2RC_MACROS_LIB_DIR}
2727
DEPENDS
2828
"${RUST_STAMP_FILE}"

‎tests/lit/lit/formats/Cpp2RustTest.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import lit.Test
55
import lit.util
6-
from .._buildconfig import CXX, CC
6+
from .._buildconfig import BUILD_DIR, CXX, CC
77
from .base import TestFormat
88
from dataclasses import dataclass
99
from pathlib import Path
@@ -217,9 +217,8 @@ def build_cpp(self):
217217
def build_rust(self):
218218
exp = self.expectations
219219

220-
parent = Path(__file__).resolve().parent.parent.parent.parent.parent
221-
cc2rs_dir = parent / "libcc2rs" / "target" / "release"
222-
libc_dep_deps = parent / "libc-dep" / "target" / "release" / "deps"
220+
cc2rs_dir = BUILD_DIR / "libcc2rs-target" / "release"
221+
libc_dep_deps = BUILD_DIR / "libc-dep-target" / "release" / "deps"
223222
# pick the most recently compiled libc
224223
libc_rlib = max(
225224
libc_dep_deps.glob("liblibc-*.rlib"),

0 commit comments

Comments
 (0)