Skip to content

Commit 34904d7

Browse files
authored
Merge pull request #562 from immunant/kkysen/instrument-cargo-add
Run `cargo add` in `c2rust-instrument --set-runtime` to add `c2rust-analysis-rt` automatically
2 parents 55e0e5f + ba996b3 commit 34904d7

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

analysis/test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ edition = "2021"
77

88
[dependencies]
99
libc = "0.2"
10-
c2rust-analysis-rt = { path = "../../analysis/runtime", optional = true }
10+
c2rust-analysis-rt = { path = "../runtime", optional = true, version = "0.1.0" }

dynamic_instrumentation/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ struct Args {
4848
#[clap(long, value_parser)]
4949
metadata: PathBuf,
5050

51+
/// Path to the `c2rust-analysis-rt` crate if you want to use a local version of it (vs. the crates.io one).
52+
/// This is not used unless `--set-runtime` is also passed.
53+
#[clap(long, value_parser)]
54+
runtime_path: Option<PathBuf>,
55+
56+
/// Add the runtime as an optional dependency to the instrumented crate using `cargo add`.
57+
#[clap(long)]
58+
set_runtime: bool,
59+
5160
/// `cargo` args.
5261
cargo_args: Vec<OsString>,
5362
}
@@ -286,6 +295,8 @@ fn set_rust_toolchain() -> anyhow::Result<()> {
286295
fn cargo_wrapper(rustc_wrapper: &Path) -> anyhow::Result<()> {
287296
let Args {
288297
metadata,
298+
runtime_path,
299+
set_runtime,
289300
mut cargo_args,
290301
} = Args::parse();
291302

@@ -312,6 +323,17 @@ fn cargo_wrapper(rustc_wrapper: &Path) -> anyhow::Result<()> {
312323
cmd.args(&["clean", "--package", root_package.name.as_str()]);
313324
})?;
314325

326+
if set_runtime {
327+
cargo.run(|cmd| {
328+
cmd.args(&["add", "--optional", "c2rust-analysis-rt"]);
329+
if let Some(runtime) = runtime_path {
330+
// Since it's a local path, we don't need the internet,
331+
// and running it offline saves a slow index sync.
332+
cmd.args(&["--offline", "--path"]).arg(runtime);
333+
}
334+
})?;
335+
}
336+
315337
// Create and truncate the metadata file for the [`rustc_wrapper`]s to append to.
316338
OpenOptions::new()
317339
.create(true)

scripts/pdg.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ main() {
4141
local c2rust="${CWD}/${profile_dir}/c2rust"
4242
local c2rust_instrument="${CWD}/${profile_dir}/${instrument}"
4343
local metadata="${CWD}/${test_dir}/metadata.bc"
44+
local runtime="${CWD}/analysis/runtime"
4445

4546
(cd "${test_dir}"
4647
unset RUSTFLAGS # transpiled code has tons of warnings; don't allow `-D warnings`
@@ -52,6 +53,8 @@ main() {
5253

5354
time "${c2rust_instrument}" \
5455
--metadata "${metadata}" \
56+
--set-runtime \
57+
--runtime-path "${runtime}" \
5558
-- run "${profile_args[@]}" \
5659
-- "${args[@]}" \
5760
1> instrument.out.log

0 commit comments

Comments
 (0)