From b3f5e052f6a5aa9806c55887b7257ddb1647f8b0 Mon Sep 17 00:00:00 2001 From: zdivelbiss <8019570+zdivelbiss@users.noreply.github.com> Date: Thu, 14 Aug 2025 18:57:12 -0500 Subject: [PATCH] tests for `host` target specifier --- tests/testsuite/cross_compile.rs | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/tests/testsuite/cross_compile.rs b/tests/testsuite/cross_compile.rs index c4ad40930..fd0c46b2b 100644 --- a/tests/testsuite/cross_compile.rs +++ b/tests/testsuite/cross_compile.rs @@ -125,6 +125,119 @@ fn simple_cross_config() { } } +#[cargo_test] +fn target_host_arg() { + if cross_compile_disabled() { + return; + } + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.0" + edition = "2015" + authors = [] + build = "build.rs" + "#, + ) + .file( + "build.rs", + &format!( + r#" + fn main() {{ + assert_eq!(std::env::var("TARGET").unwrap(), "{}"); + }} + "#, + rustc_host() + ), + ) + .file( + "src/main.rs", + &format!( + r#" + use std::env; + fn main() {{ + assert_eq!(env::consts::ARCH, "{}"); + }} + "#, + cross_compile::native_arch() + ), + ) + .build(); + + let target = rustc_host(); + p.cargo("build -v --target host").run(); + assert!(p.target_bin(target, "foo").is_file()); + + if cross_compile_can_run_on_host() { + p.process(&p.target_bin(target, "foo")).run(); + } +} + +#[cargo_test] +fn target_host_config() { + if cross_compile_disabled() { + return; + } + + let p = project() + .file( + ".cargo/config.toml", + &format!( + r#" + [build] + target = "host" + "#, + ), + ) + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.0" + edition = "2015" + authors = [] + build = "build.rs" + "#, + ) + .file( + "build.rs", + &format!( + r#" + fn main() {{ + assert_eq!(std::env::var("TARGET").unwrap(), "{}"); + }} + "#, + rustc_host() + ), + ) + .file( + "src/main.rs", + &format!( + r#" + use std::env; + fn main() {{ + assert_eq!(env::consts::ARCH, "{}"); + }} + "#, + cross_compile::native_arch() + ), + ) + .build(); + + let target = rustc_host(); + p.cargo("build -v").run(); + assert!(p.target_bin(target, "foo").is_file()); + + if cross_compile_can_run_on_host() { + p.process(&p.target_bin(target, "foo")).run(); + } +} + #[cargo_test] fn simple_deps() { if cross_compile_disabled() {