Skip to content

Commit 47312c4

Browse files
committed
chore: Moved tools and some cross compiling logic into testsuite
1 parent 409fed7 commit 47312c4

28 files changed

+332
-287
lines changed

crates/cargo-test-support/src/cross_compile.rs

Lines changed: 1 addition & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -9,189 +9,7 @@
99
//!
1010
//! These tests are all disabled on rust-lang/rust's CI, but run in Cargo's CI.
1111
12-
use crate::{basic_manifest, main_file, project};
13-
use cargo_util::ProcessError;
1412
use std::env;
15-
use std::fmt::Write;
16-
use std::process::{Command, Output};
17-
use std::sync::atomic::{AtomicBool, Ordering};
18-
use std::sync::Once;
19-
20-
/// Whether or not the resulting cross binaries can run on the host.
21-
static CAN_RUN_ON_HOST: AtomicBool = AtomicBool::new(false);
22-
23-
pub fn disabled() -> bool {
24-
// First, disable if requested.
25-
match env::var("CFG_DISABLE_CROSS_TESTS") {
26-
Ok(ref s) if *s == "1" => return true,
27-
_ => {}
28-
}
29-
30-
// It requires setting `target.linker` for cross-compilation to work on aarch64,
31-
// so not going to bother now.
32-
if cfg!(all(target_arch = "aarch64", target_os = "linux")) {
33-
return true;
34-
}
35-
36-
// Cross tests are only tested to work on macos, linux, and MSVC windows.
37-
if !(cfg!(target_os = "macos") || cfg!(target_os = "linux") || cfg!(target_env = "msvc")) {
38-
return true;
39-
}
40-
41-
// It's not particularly common to have a cross-compilation setup, so
42-
// try to detect that before we fail a bunch of tests through no fault
43-
// of the user.
44-
static CAN_BUILD_CROSS_TESTS: AtomicBool = AtomicBool::new(false);
45-
static CHECK: Once = Once::new();
46-
47-
let cross_target = alternate();
48-
49-
let run_cross_test = || -> anyhow::Result<Output> {
50-
let p = project()
51-
.at("cross_test")
52-
.file("Cargo.toml", &basic_manifest("cross_test", "1.0.0"))
53-
.file("src/main.rs", &main_file(r#""testing!""#, &[]))
54-
.build();
55-
56-
let build_result = p
57-
.cargo("build --target")
58-
.arg(&cross_target)
59-
.exec_with_output();
60-
61-
if build_result.is_ok() {
62-
CAN_BUILD_CROSS_TESTS.store(true, Ordering::SeqCst);
63-
}
64-
65-
let result = p
66-
.cargo("run --target")
67-
.arg(&cross_target)
68-
.exec_with_output();
69-
70-
if result.is_ok() {
71-
CAN_RUN_ON_HOST.store(true, Ordering::SeqCst);
72-
}
73-
build_result
74-
};
75-
76-
CHECK.call_once(|| {
77-
drop(run_cross_test());
78-
});
79-
80-
if CAN_BUILD_CROSS_TESTS.load(Ordering::SeqCst) {
81-
// We were able to compile a simple project, so the user has the
82-
// necessary `std::` bits installed. Therefore, tests should not
83-
// be disabled.
84-
return false;
85-
}
86-
87-
// We can't compile a simple cross project. We want to warn the user
88-
// by failing a single test and having the remainder of the cross tests
89-
// pass. We don't use `std::sync::Once` here because panicking inside its
90-
// `call_once` method would poison the `Once` instance, which is not what
91-
// we want.
92-
static HAVE_WARNED: AtomicBool = AtomicBool::new(false);
93-
94-
if HAVE_WARNED.swap(true, Ordering::SeqCst) {
95-
// We are some other test and somebody else is handling the warning.
96-
// Just disable the current test.
97-
return true;
98-
}
99-
100-
// We are responsible for warning the user, which we do by panicking.
101-
let mut message = format!(
102-
"
103-
Cannot cross compile to {}.
104-
105-
This failure can be safely ignored. If you would prefer to not see this
106-
failure, you can set the environment variable CFG_DISABLE_CROSS_TESTS to \"1\".
107-
108-
Alternatively, you can install the necessary libraries to enable cross
109-
compilation tests. Cross compilation tests depend on your host platform.
110-
",
111-
cross_target
112-
);
113-
114-
if cfg!(target_os = "linux") {
115-
message.push_str(
116-
"
117-
Linux cross tests target i686-unknown-linux-gnu, which requires the ability to
118-
build and run 32-bit targets. This requires the 32-bit libraries to be
119-
installed. For example, on Ubuntu, run `sudo apt install gcc-multilib` to
120-
install the necessary libraries.
121-
",
122-
);
123-
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
124-
message.push_str(
125-
"
126-
macOS on aarch64 cross tests to target x86_64-apple-darwin.
127-
This should be natively supported via Xcode, nothing additional besides the
128-
rustup target should be needed.
129-
",
130-
);
131-
} else if cfg!(target_os = "macos") {
132-
message.push_str(
133-
"
134-
macOS on x86_64 cross tests to target x86_64-apple-ios, which requires the iOS
135-
SDK to be installed. This should be included with Xcode automatically. If you
136-
are using the Xcode command line tools, you'll need to install the full Xcode
137-
app (from the Apple App Store), and switch to it with this command:
138-
139-
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
140-
141-
Some cross-tests want to *run* the executables on the host. These tests will
142-
be ignored if this is not possible. On macOS, this means you need an iOS
143-
simulator installed to run these tests. To install a simulator, open Xcode, go
144-
to preferences > Components, and download the latest iOS simulator.
145-
",
146-
);
147-
} else if cfg!(target_os = "windows") {
148-
message.push_str(
149-
"
150-
Windows cross tests target i686-pc-windows-msvc, which requires the ability
151-
to build and run 32-bit targets. This should work automatically if you have
152-
properly installed Visual Studio build tools.
153-
",
154-
);
155-
} else {
156-
// The check at the top should prevent this.
157-
panic!("platform should have been skipped");
158-
}
159-
160-
let rustup_available = Command::new("rustup").output().is_ok();
161-
if rustup_available {
162-
write!(
163-
message,
164-
"
165-
Make sure that the appropriate `rustc` target is installed with rustup:
166-
167-
rustup target add {}
168-
",
169-
cross_target
170-
)
171-
.unwrap();
172-
} else {
173-
write!(
174-
message,
175-
"
176-
rustup does not appear to be installed. Make sure that the appropriate
177-
`rustc` target is installed for the target `{}`.
178-
",
179-
cross_target
180-
)
181-
.unwrap();
182-
}
183-
184-
// Show the actual error message.
185-
match run_cross_test() {
186-
Ok(_) => message.push_str("\nUh oh, second run succeeded?\n"),
187-
Err(err) => match err.downcast_ref::<ProcessError>() {
188-
Some(proc_err) => write!(message, "\nTest error: {}\n", proc_err).unwrap(),
189-
None => write!(message, "\nUnexpected non-process error: {}\n", err).unwrap(),
190-
},
191-
}
192-
193-
panic!("{}", message);
194-
}
19513

19614
/// The arch triple of the test-running host.
19715
pub fn native() -> &'static str {
@@ -252,31 +70,9 @@ pub fn unused() -> &'static str {
25270
"wasm32-unknown-unknown"
25371
}
25472

255-
/// Whether or not the host can run cross-compiled executables.
256-
pub fn can_run_on_host() -> bool {
257-
if disabled() {
258-
return false;
259-
}
260-
// macos is currently configured to cross compile to x86_64-apple-ios
261-
// which requires a simulator to run. Azure's CI image appears to have the
262-
// SDK installed, but are not configured to launch iOS images with a
263-
// simulator.
264-
if cfg!(target_os = "macos") {
265-
if CAN_RUN_ON_HOST.load(Ordering::SeqCst) {
266-
return true;
267-
} else {
268-
println!("Note: Cannot run on host, skipping.");
269-
return false;
270-
}
271-
} else {
272-
assert!(CAN_RUN_ON_HOST.load(Ordering::SeqCst));
273-
return true;
274-
}
275-
}
276-
27773
/// Check if the given target has been installed.
27874
///
279-
/// Generally [`disabled`] should be used to check if cross-compilation is allowed.
75+
/// Generally `testsuite::utils::cross_compile::disabled` should be used to check if cross-compilation is allowed.
28076
/// And [`alternate`] to get the cross target.
28177
///
28278
/// You should only use this as a last resort to skip tests,

crates/cargo-test-support/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ pub mod install;
110110
pub mod paths;
111111
pub mod publish;
112112
pub mod registry;
113-
pub mod tools;
114113

115114
pub mod prelude {
116115
pub use crate::cargo_test;
@@ -643,8 +642,8 @@ pub struct RawOutput {
643642
///
644643
/// Construct with
645644
/// - [`execs`]
646-
/// - [`cargo_process`]
647645
/// - [`Project`] methods
646+
/// - `cargo_process` in testsuite
648647
#[must_use]
649648
#[derive(Clone)]
650649
pub struct Execs {

tests/testsuite/artifact_dep.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Tests specific to artifact dependencies, designated using
22
//! the new `dep = { artifact = "bin", … }` syntax in manifests.
33
4+
use crate::utils::cross_compile::{
5+
can_run_on_host as cross_compile_can_run_on_host, disabled as cross_compile_disabled,
6+
};
47
use cargo_test_support::compare::assert_e2e;
58
use cargo_test_support::prelude::*;
69
use cargo_test_support::registry::{Package, RegistryBuilder};
@@ -340,7 +343,7 @@ fn features_are_unified_among_lib_and_bin_dep_of_same_target() {
340343

341344
#[cargo_test]
342345
fn features_are_not_unified_among_lib_and_bin_dep_of_different_target() {
343-
if cross_compile::disabled() {
346+
if cross_compile_disabled() {
344347
return;
345348
}
346349
let target = cross_compile::alternate();
@@ -455,7 +458,7 @@ For more information about this error, try `rustc --explain E0425`.
455458

456459
#[cargo_test]
457460
fn feature_resolution_works_for_cfg_target_specification() {
458-
if cross_compile::disabled() {
461+
if cross_compile_disabled() {
459462
return;
460463
}
461464
let target = cross_compile::alternate();
@@ -1079,7 +1082,7 @@ fn allow_artifact_and_non_artifact_dependency_to_same_crate() {
10791082

10801083
#[cargo_test]
10811084
fn build_script_deps_adopt_specified_target_unconditionally() {
1082-
if cross_compile::disabled() {
1085+
if cross_compile_disabled() {
10831086
return;
10841087
}
10851088

@@ -1137,7 +1140,7 @@ fn build_script_deps_adopt_specified_target_unconditionally() {
11371140
/// inverse RFC-3176
11381141
#[cargo_test]
11391142
fn build_script_deps_adopt_do_not_allow_multiple_targets_under_different_name_and_same_version() {
1140-
if cross_compile::disabled() {
1143+
if cross_compile_disabled() {
11411144
return;
11421145
}
11431146

@@ -1196,7 +1199,7 @@ fn build_script_deps_adopt_do_not_allow_multiple_targets_under_different_name_an
11961199

11971200
#[cargo_test]
11981201
fn non_build_script_deps_adopt_specified_target_unconditionally() {
1199-
if cross_compile::disabled() {
1202+
if cross_compile_disabled() {
12001203
return;
12011204
}
12021205

@@ -1247,7 +1250,7 @@ fn non_build_script_deps_adopt_specified_target_unconditionally() {
12471250

12481251
#[cargo_test]
12491252
fn cross_doctests_works_with_artifacts() {
1250-
if cross_compile::disabled() {
1253+
if cross_compile_disabled() {
12511254
return;
12521255
}
12531256

@@ -1302,7 +1305,7 @@ fn cross_doctests_works_with_artifacts() {
13021305
println!("c");
13031306
let target = cross_compile::alternate();
13041307

1305-
if !cross_compile::can_run_on_host() {
1308+
if !cross_compile_can_run_on_host() {
13061309
return;
13071310
}
13081311

@@ -1327,7 +1330,7 @@ fn cross_doctests_works_with_artifacts() {
13271330

13281331
#[cargo_test]
13291332
fn build_script_deps_adopts_target_platform_if_target_equals_target() {
1330-
if cross_compile::disabled() {
1333+
if cross_compile_disabled() {
13311334
return;
13321335
}
13331336

@@ -1498,7 +1501,7 @@ foo v0.0.0 ([ROOT]/foo)
14981501

14991502
#[cargo_test]
15001503
fn artifact_dep_target_specified() {
1501-
if cross_compile::disabled() {
1504+
if cross_compile_disabled() {
15021505
return;
15031506
}
15041507
let target = cross_compile::alternate();
@@ -1554,7 +1557,7 @@ foo v0.0.0 ([ROOT]/foo)
15541557
/// * the target is not activated.
15551558
#[cargo_test]
15561559
fn dep_of_artifact_dep_same_target_specified() {
1557-
if cross_compile::disabled() {
1560+
if cross_compile_disabled() {
15581561
return;
15591562
}
15601563
let target = cross_compile::alternate();
@@ -1629,7 +1632,7 @@ foo v0.1.0 ([ROOT]/foo)
16291632

16301633
#[cargo_test]
16311634
fn targets_are_picked_up_from_non_workspace_artifact_deps() {
1632-
if cross_compile::disabled() {
1635+
if cross_compile_disabled() {
16331636
return;
16341637
}
16351638
let target = cross_compile::alternate();
@@ -1675,7 +1678,7 @@ fn targets_are_picked_up_from_non_workspace_artifact_deps() {
16751678

16761679
#[cargo_test]
16771680
fn index_version_filtering() {
1678-
if cross_compile::disabled() {
1681+
if cross_compile_disabled() {
16791682
return;
16801683
}
16811684
let target = cross_compile::alternate();
@@ -1748,7 +1751,7 @@ required by package `foo v0.1.0 ([ROOT]/foo)`
17481751
fn proc_macro_in_artifact_dep() {
17491752
// Forcing FeatureResolver to check a proc-macro for a dependency behind a
17501753
// target dependency.
1751-
if cross_compile::disabled() {
1754+
if cross_compile_disabled() {
17521755
return;
17531756
}
17541757
Package::new("pm", "1.0.0")
@@ -2563,7 +2566,7 @@ fn build_script_features_for_shared_dependency() {
25632566
//
25642567
// When common is built as a dependency of foo, it should have features
25652568
// `f1` (for the library and the build script).
2566-
if cross_compile::disabled() {
2569+
if cross_compile_disabled() {
25672570
return;
25682571
}
25692572
let target = cross_compile::alternate();
@@ -2728,7 +2731,7 @@ fn calc_bin_artifact_fingerprint() {
27282731
#[cargo_test]
27292732
fn with_target_and_optional() {
27302733
// See rust-lang/cargo#10526
2731-
if cross_compile::disabled() {
2734+
if cross_compile_disabled() {
27322735
return;
27332736
}
27342737
let target = cross_compile::alternate();

0 commit comments

Comments
 (0)