Skip to content

Commit 6a97ed6

Browse files
authored
Merge pull request #196 from daxpedda/pass-target
Fix cross-compilation
2 parents b874810 + 50fcf47 commit 6a97ed6

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixed
1313

14+
* Passing `--target` to build command when cross-compiling.
15+
1416
### Changed
1517

1618
### Removed

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ impl Config {
258258
Ok(())
259259
}
260260

261+
/// Check whether the host is the specified string
262+
pub fn host_matches(&self, target: &str) -> bool {
263+
self.host.as_ref().expect("host should have been filled in") == target
264+
}
265+
261266
pub(crate) fn has_asm_support(&self) -> bool {
262267
static ASM_SUPPORTED_ARCHS: &[&str] = &[
263268
"x86", "x86_64", "arm", "aarch64", "riscv32",

src/lib.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,44 @@ pub fn default_any_file_filter(path: &Path, config: &Config) -> bool {
170170

171171
/// The default per-file config used by `run_tests`.
172172
pub fn default_per_file_config(config: &mut Config, _path: &Path, file_contents: &[u8]) {
173-
// Heuristic:
174-
// * if the file contains `#[test]`, automatically pass `--cfg test`.
175-
// * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
176-
// This avoids having to spam `fn main() {}` in almost every test.
173+
config.program.args.push(
174+
match crate_type(file_contents) {
175+
CrateType::ProcMacro => "--crate-type=proc-macro",
176+
CrateType::Test => "--test",
177+
CrateType::Bin => return,
178+
CrateType::Lib => "--crate-type=lib",
179+
}
180+
.into(),
181+
)
182+
}
183+
184+
/// The kind of crate we're building here. Corresponds to `--crate-type` flags of rustc
185+
pub enum CrateType {
186+
/// A proc macro
187+
ProcMacro,
188+
/// A file containing unit tests
189+
Test,
190+
/// A binary file containing a main function or start function
191+
Bin,
192+
/// A library crate
193+
Lib,
194+
}
195+
196+
/// Heuristic:
197+
/// * if the file contains `#[test]`, automatically pass `--cfg test`.
198+
/// * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
199+
/// This avoids having to spam `fn main() {}` in almost every test.
200+
pub fn crate_type(file_contents: &[u8]) -> CrateType {
177201
if file_contents.find(b"#[proc_macro").is_some() {
178-
config.program.args.push("--crate-type=proc-macro".into())
202+
CrateType::ProcMacro
179203
} else if file_contents.find(b"#[test]").is_some() {
180-
config.program.args.push("--test".into());
204+
CrateType::Test
181205
} else if file_contents.find(b"fn main()").is_none()
182206
&& file_contents.find(b"#[start]").is_none()
183207
{
184-
config.program.args.push("--crate-type=lib".into());
208+
CrateType::Lib
209+
} else {
210+
CrateType::Bin
185211
}
186212
}
187213

@@ -520,6 +546,15 @@ fn build_command(
520546
cmd.arg("--edition").arg(&*edition);
521547
}
522548

549+
if let Some(target) = &config.target {
550+
// Adding a `--target` arg to calls to Cargo will cause target folders
551+
// to create a target-specific sub-folder. We can avoid that by just
552+
// not passing a `--target` arg if its the same as the host.
553+
if !config.host_matches(target) {
554+
cmd.arg("--target").arg(target);
555+
}
556+
}
557+
523558
// False positive in miri, our `map` uses a ref pattern to get the references to the tuple fields instead
524559
// of a reference to a tuple
525560
#[allow(clippy::map_identity)]
@@ -574,6 +609,12 @@ fn build_aux(
574609

575610
default_per_file_config(&mut config, aux_file, &file_contents);
576611

612+
match crate_type(&file_contents) {
613+
// Proc macros must be run on the host
614+
CrateType::ProcMacro => config.target = config.host.clone(),
615+
CrateType::Test | CrateType::Bin | CrateType::Lib => {}
616+
}
617+
577618
// Put aux builds into a separate directory per path so that multiple aux files
578619
// from different directories (but with the same file name) don't collide.
579620
let relative = strip_path_prefix(aux_file.parent().unwrap(), &config.out_dir);
@@ -1281,7 +1322,7 @@ fn test_condition(condition: &Condition, config: &Config) -> bool {
12811322
Condition::Bitwidth(bits) => get_pointer_width(target) == *bits,
12821323
Condition::Target(t) => target.contains(t),
12831324
Condition::Host(t) => config.host.as_ref().unwrap().contains(t),
1284-
Condition::OnHost => target == config.host.as_ref().unwrap(),
1325+
Condition::OnHost => config.host_matches(target),
12851326
}
12861327
}
12871328

tests/integration.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ fn main() -> Result<()> {
8282
config.filter("program not found", "No such file or directory");
8383
config.filter(" \\(os error [0-9]+\\)", "");
8484
config.filter("note: rustc 1\\..*", "");
85+
// Cross compilation paths contain an additional target directory name
86+
config.stderr_filter(
87+
"(/target/ui/tests/integrations/[^/]+).*debug/deps",
88+
"$1/debug/deps",
89+
);
8590

8691
let text = ui_test::status_emitter::Text::from(args.format);
8792

tests/integrations/basic/tests/ui_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ fn main() -> ui_test::color_eyre::Result<()> {
1717
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
1818
config.path_stderr_filter(&std::path::Path::new(path), "$DIR");
1919

20+
if let Ok(target) = std::env::var("UITEST_TEST_TARGET") {
21+
config.target = Some(target);
22+
config.output_conflict_handling = OutputConflictHandling::Ignore;
23+
}
24+
2025
// hide binaries generated for successfully passing tests
2126
let tmp_dir = tempfile::tempdir_in(path)?;
2227
let tmp_dir = tmp_dir.path();

0 commit comments

Comments
 (0)