Skip to content

Commit

Permalink
analyze: add C2RUST_ANALYZE_NO_CARGO env var to disable cargo integra…
Browse files Browse the repository at this point in the history
…tion (#1070)

Adding cargo integration made it a bit tricky to access the pre-cargo
behavior, in which `c2rust-analyze` would behave like `rustc`. This
behavior was useful for experimenting on small single-file example
programs (e.g. `c2rust-analyze foo.rs`). This commit adds a new env var,
`C2RUST_ANALYZE_NO_CARGO`, which can be set to restore the old behavior.
  • Loading branch information
spernsteiner authored Apr 11, 2024
2 parents 0e732f6 + b0d0aed commit 982bafb
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions c2rust-analyze/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,24 @@ fn is_build_script(at_args: &[String]) -> anyhow::Result<bool> {
Ok(bin_crate_name().is_none() && is_bin_crate(at_args)?)
}

/// Check whether "no Cargo" mode is enabled. In this mode, `c2rust-analyze` behaves like `rustc`
/// instead of like `cargo`. For example, `C2RUST_ANALYZE_NO_CARGO=1 c2rust-analyze foo.rs` will
/// process `foo.rs` much like `rustc foo.rs` does.
fn in_no_cargo_mode() -> bool {
env::var_os("C2RUST_ANALYZE_NO_CARGO").is_some()
}

/// Run as a `rustc` wrapper (a la `$RUSTC_WRAPPER`/[`RUSTC_WRAPPER_VAR`]).
fn rustc_wrapper() -> anyhow::Result<()> {
let mut at_args = env::args().skip(1).collect::<Vec<_>>();
let no_cargo = in_no_cargo_mode();
let mut at_args = if no_cargo {
env::args().collect::<Vec<_>>()
} else {
env::args().skip(1).collect::<Vec<_>>()
};
// We also want to avoid proc-macro crates,
// but those must be separate crates, so we should be okay.
let is_primary_compilation = is_primary_package() && !is_build_script(&at_args)?;
let is_primary_compilation = (is_primary_package() && !is_build_script(&at_args)?) || no_cargo;

let sysroot = env_path_from_wrapper(RUST_SYSROOT_VAR).or_else(|_| resolve_sysroot())?;
let sysroot = sysroot
Expand Down Expand Up @@ -419,7 +431,8 @@ fn main() -> anyhow::Result<()> {

let own_exe = env::current_exe()?;

let wrapping_rustc = env::var_os(RUSTC_WRAPPER_VAR).as_deref() == Some(own_exe.as_os_str());
let wrapping_rustc = env::var_os(RUSTC_WRAPPER_VAR).as_deref() == Some(own_exe.as_os_str())
|| in_no_cargo_mode();
if wrapping_rustc {
rustc_wrapper()
} else {
Expand Down

0 comments on commit 982bafb

Please sign in to comment.