Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): use stderr for detection mode #469

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions cli/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ fn setup_logging() {
tracing::subscriber::set_global_default(subscriber).unwrap();
}

const HAX_VANILLA_RUSTC: &str = "HAX_VANILLA_RUSTC";

fn main() {
setup_logging();

Expand All @@ -93,22 +95,32 @@ fn main() {
};

// When `HAX_FEATURES_DETECTION_MODE` is set, we just detect
// features for the current crate, output them in JSON on stdout
// features for the current crate, output them in JSON on stderr
// and exit immediately
if std::env::var("HAX_FEATURES_DETECTION_MODE").is_ok() {
use std::io::BufWriter;
return serde_json::to_writer(
BufWriter::new(std::io::stdout()),
BufWriter::new(std::io::stderr()),
&Features::detect(&options, &rustc_args),
)
.unwrap();
}

let (vanilla_rustc, vanilla_rustc_never) = {
let vanilla_rustc = std::env::var(HAX_VANILLA_RUSTC);
let vanilla_rustc_never = vanilla_rustc == Ok("never".into());
(
!vanilla_rustc_never && vanilla_rustc.is_ok(),
vanilla_rustc_never,
)
};

// fetch the correct callback structure given the command, and
// coerce options
let is_primary_package = std::env::var("CARGO_PRIMARY_PACKAGE").is_ok();
let is_build_script = std::env::var("CARGO_CRATE_NAME") == Ok("build_script_build".to_string()); // FIXME: is there a more robust way to do this?
let translate_package = !is_build_script && (options.deps || is_primary_package);
let translate_package =
!vanilla_rustc && !is_build_script && (options.deps || is_primary_package);
let mut callbacks: Box<dyn Callbacks + Send> = if translate_package {
match &options.command {
Some(Command::ExporterCommand(command)) => Box::new(exporter::ExtractionCallbacks {
Expand Down Expand Up @@ -170,7 +182,43 @@ fn main() {
},
};

std::process::exit(rustc_driver::catch_with_exit_code(move || {
rustc_driver::RunCompiler::new(&rustc_args, &mut callbacks).run()
}))
let exit_code = rustc_driver::catch_with_exit_code({
let rustc_args = rustc_args.clone();
move || rustc_driver::RunCompiler::new(&rustc_args, &mut callbacks).run()
});

std::process::exit(
if !vanilla_rustc_never && translate_package && exit_code == 0 {
// When the hax translation is successful, we need to re-run
// rustc. Indeed, hax translation doesn't actually build a
// package: no `rlib` will be written on disk.
self::vanilla_rustc()
} else {
exit_code
},
)
}

/// Re-run rustc without doing any hax translation. This ensures a
/// `rlib` is produced (when the crate compiles correctly).
fn vanilla_rustc() -> i32 {
use std::process::{Command, Stdio};
let output = Command::new(std::env::args().next().unwrap())
.args(std::env::args().skip(1))
.env(HAX_VANILLA_RUSTC, "1")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
if output.status.success() {
0
} else {
let stdout = &std::str::from_utf8(&output.stdout).unwrap();
let stderr = &std::str::from_utf8(&output.stderr).unwrap();
println!("{stdout}");
eprintln!("{stderr}");
output.status.code().unwrap_or(1)
}
}
14 changes: 10 additions & 4 deletions cli/driver/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,22 @@ impl Features {
/// Rustc messages twice.
pub fn detect_forking() -> Self {
use std::process::{Command, Stdio};
let stdout = Command::new(std::env::args().next().unwrap())
let output = Command::new(std::env::args().next().unwrap())
.args(std::env::args().skip(1))
.env("HAX_FEATURES_DETECTION_MODE", "1")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap()
.stdout;
serde_json::from_str(&std::str::from_utf8(&stdout).unwrap()).unwrap()
.unwrap();
let stderr = &std::str::from_utf8(&output.stderr).unwrap();
serde_json::from_str(stderr).unwrap_or_else(|e| {
let stdout = &std::str::from_utf8(&output.stdout).unwrap();
panic!(
"[detect_forking] could not parse `stdout`, got error `{e}`\n\n### STDERR{}\n\n### STDOUT{}",
stderr, stdout
);
})
}
}
2 changes: 2 additions & 0 deletions examples/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ in
export HINT_DIR=$(mktemp -d)
export SHELL=${stdenv.shell}
make clean # Should be a no-op (see `filter` above)
# Need to inject `HAX_VANILLA_RUSTC=never` because of #472
sed -i "s/make -C limited-order-book/HAX_VANILLA_RUSTC=never make -C limited-order-book/g" Makefile
make
'';
buildInputs = [hax fstar];
Expand Down
2 changes: 1 addition & 1 deletion tests/mut-ref-functionalization/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct S {
b: [u8; 5],
}

pub fn foo(mut lhs: S, rhs: &S) -> S {
fn foo(mut lhs: S, rhs: &S) -> S {
for i in 0..1 {
lhs.b[i] += rhs.b[i];
}
Expand Down
Loading