From ea347891b82402e85cb11aa92d21d10753d0598d Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 22 Aug 2023 11:01:33 +0800 Subject: [PATCH] Add some tests --- tests/testsuite/build_script.rs | 37 ++++++++++++++ tests/testsuite/tool_paths.rs | 87 +++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 400d10547c43..89db5351bdbf 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -453,6 +453,43 @@ fn custom_build_env_var_rustc_linker() { p.cargo("build --target").arg(&target).run(); } +// Only run this test on linux, since it's difficult to construct +// a case suitable for all platforms. +// See:https://github.com/rust-lang/cargo/pull/12535#discussion_r1306618264 +#[cargo_test] +#[cfg(target_os = "linux")] +fn custom_build_env_var_rustc_linker_with_target_cfg() { + if cross_compile::disabled() { + return; + } + + let target = cross_compile::alternate(); + let p = project() + .file( + ".cargo/config", + r#" + [target.'cfg(target_arch = "i686")'] + linker = "/path/to/linker" + "#, + ) + .file( + "build.rs", + r#" + use std::env; + + fn main() { + assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker")); + } + "#, + ) + .file("src/lib.rs", "") + .build(); + + // no crate type set => linker never called => build succeeds if and + // only if build.rs succeeds, despite linker binary not existing. + p.cargo("build --target").arg(&target).run(); +} + #[cargo_test] fn custom_build_env_var_rustc_linker_bad_host_target() { let target = rustc_host(); diff --git a/tests/testsuite/tool_paths.rs b/tests/testsuite/tool_paths.rs index 8ddd358c6b11..5428f9d016b7 100644 --- a/tests/testsuite/tool_paths.rs +++ b/tests/testsuite/tool_paths.rs @@ -32,6 +32,93 @@ fn pathless_tools() { .run(); } +// can set a custom linker via `target.'cfg(..)'.linker` +#[cargo_test] +fn custom_linker_cfg() { + let foo = project() + .file("Cargo.toml", &basic_lib_manifest("foo")) + .file("src/lib.rs", "") + .file( + ".cargo/config", + r#" + [target.'cfg(not(target_os = "none"))'] + linker = "nonexistent-linker" + "#, + ) + .build(); + + foo.cargo("build --verbose") + .with_stderr( + "\ +[COMPILING] foo v0.5.0 ([CWD]) +[RUNNING] `rustc [..] -C linker=nonexistent-linker [..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +// custom linker set via `target.$triple.linker` have precede over `target.'cfg(..)'.linker` +#[cargo_test] +fn custom_linker_cfg_precedence() { + let target = rustc_host(); + + let foo = project() + .file("Cargo.toml", &basic_lib_manifest("foo")) + .file("src/lib.rs", "") + .file( + ".cargo/config", + &format!( + r#" + [target.'cfg(not(target_os = "none"))'] + linker = "ignored-linker" + [target.{}] + linker = "nonexistent-linker" + "#, + target + ), + ) + .build(); + + foo.cargo("build --verbose") + .with_stderr( + "\ +[COMPILING] foo v0.5.0 ([CWD]) +[RUNNING] `rustc [..] -C linker=nonexistent-linker [..]` +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +#[cargo_test] +fn custom_linker_cfg_collision() { + let foo = project() + .file("Cargo.toml", &basic_lib_manifest("foo")) + .file("src/lib.rs", "") + .file( + ".cargo/config", + r#" + [target.'cfg(not(target_arch = "avr"))'] + linker = "nonexistent-linker1" + [target.'cfg(not(target_os = "none"))'] + linker = "nonexistent-linker2" + "#, + ) + .build(); + + foo.cargo("build --verbose") + .with_status(101) + .with_stderr(&format!( + "\ +[ERROR] several matching instances of `target.'cfg(..)'.linker` in configurations +first match `cfg(not(target_arch = \"avr\"))` located in [..]/foo/.cargo/config +second match `cfg(not(target_os = \"none\"))` located in [..]/foo/.cargo/config +", + )) + .run(); +} + #[cargo_test] fn absolute_tools() { let target = rustc_host();