From b0d0aed69277034bcd4f9395e07cd03298c80fb4 Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Wed, 3 Apr 2024 15:57:14 -0700 Subject: [PATCH] analyze: refactor and document handling of C2RUST_ANALYZE_NO_CARGO --- c2rust-analyze/src/main.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/c2rust-analyze/src/main.rs b/c2rust-analyze/src/main.rs index 88c9e1e62d..afd5d28430 100644 --- a/c2rust-analyze/src/main.rs +++ b/c2rust-analyze/src/main.rs @@ -202,8 +202,6 @@ impl Cargo { const RUSTC_WRAPPER_VAR: &str = "RUSTC_WRAPPER"; const RUST_SYSROOT_VAR: &str = "RUST_SYSROOT"; -/// If this env var is set, we skip all cargo integration and just behave like rustc. -const NO_CARGO_VAR: &str = "C2RUST_ANALYZE_NO_CARGO"; /// Read a [`PathBuf`] from the [`mod@env`]ironment that should've been set by the [`cargo_wrapper`]. fn env_path_from_wrapper(var: &str) -> anyhow::Result { @@ -274,9 +272,16 @@ fn is_build_script(at_args: &[String]) -> anyhow::Result { 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 no_cargo = env::var_os(NO_CARGO_VAR).is_some(); + let no_cargo = in_no_cargo_mode(); let mut at_args = if no_cargo { env::args().collect::>() } else { @@ -427,7 +432,7 @@ 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()) - || env::var_os(NO_CARGO_VAR).is_some(); + || in_no_cargo_mode(); if wrapping_rustc { rustc_wrapper() } else {