Skip to content

Commit d793249

Browse files
committed
Implement thrust-macros that expands requires and ensures attrs
1 parent 9bce62e commit d793249

7 files changed

Lines changed: 363 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/target
1+
target/

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[workspace]
2+
members = [".", "thrust-macros"]
3+
resolver = "2"
4+
15
[package]
26
name = "thrust"
37
version = "0.1.0"
@@ -18,6 +22,11 @@ name = "ui"
1822
harness = false
1923

2024
[dependencies]
25+
# Workaround until thrust supports invocation via cargo: adding thrust-macros as
26+
# a dependency ensures cargo builds (and rebuilds) it alongside thrust. It is not
27+
# used as a proc-macro here; the dylib is loaded at runtime via --extern.
28+
thrust-macros = { path = "thrust-macros" }
29+
2130
anyhow = "1.0.102"
2231
pretty = { version = "0.12.5", features = ["termcolor"] }
2332
tempfile = "3.27.0"

src/main.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,36 @@ impl Callbacks for CompilerCalls {
6666
}
6767
}
6868

69+
fn thrust_macros_path() -> Option<std::path::PathBuf> {
70+
let exe = std::env::current_exe().ok()?;
71+
let dir = exe.parent()?;
72+
// When thrust-macros is a cargo dependency it lands in deps/ with a hash
73+
// suffix (e.g. libthrust_macros-<hash>.so), so check both locations.
74+
let search_dirs = [dir.to_path_buf(), dir.join("deps")];
75+
for search_dir in &search_dirs {
76+
for ext in ["so", "dylib", "dll"] {
77+
// First try the exact name (e.g. when built standalone).
78+
let candidate = search_dir.join(format!("libthrust_macros.{ext}"));
79+
if candidate.exists() {
80+
return Some(candidate);
81+
}
82+
// Then scan for libthrust_macros-<hash>.<ext>.
83+
if let Ok(entries) = std::fs::read_dir(search_dir) {
84+
for entry in entries.flatten() {
85+
let name = entry.file_name();
86+
let name = name.to_string_lossy();
87+
if name.starts_with("libthrust_macros-") && name.ends_with(ext) {
88+
return Some(entry.path());
89+
}
90+
}
91+
}
92+
}
93+
}
94+
None
95+
}
96+
6997
pub fn main() {
70-
let args = std::env::args().collect::<Vec<_>>();
98+
let mut args = std::env::args().collect::<Vec<_>>();
7199

72100
use tracing_subscriber::{filter::EnvFilter, prelude::*};
73101
tracing_subscriber::registry()
@@ -80,6 +108,14 @@ pub fn main() {
80108
)
81109
.init();
82110

111+
if let Some(path) = thrust_macros_path() {
112+
args.push("--extern".to_owned());
113+
args.push(format!("thrust_macros={}", path.display()));
114+
tracing::debug!("linking thrust_macros from {}", path.display());
115+
} else {
116+
tracing::warn!("could not locate thrust_macros library");
117+
}
118+
83119
let code =
84120
rustc_driver::catch_with_exit_code(|| RunCompiler::new(&args, &mut CompilerCalls {}).run());
85121
std::process::exit(code);

thrust-macros/Cargo.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

thrust-macros/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "thrust-macros"
3+
version = "0.1.0"
4+
authors = ["coord_e <me@coord-e.com>"]
5+
edition = "2021"
6+
7+
[lib]
8+
proc-macro = true
9+
10+
[dependencies]
11+
proc-macro2 = "1"
12+
quote = "=1.0.36"
13+
syn = { version = "2", features = ["full"] }

0 commit comments

Comments
 (0)