From a9bd81b406ffb55de1074f2a1d86483dbc6672eb Mon Sep 17 00:00:00 2001 From: messense Date: Sun, 26 Nov 2023 15:52:16 +0800 Subject: [PATCH] Add support for cross compiling with `cross` (#1865) --- .gitignore | 1 + Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/compile.rs | 16 +++++++++++++++- src/project_layout.rs | 4 ++++ 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 912a2762e..dd1ec2ca9 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ test-crates/wheels/ test-crates/targets/ test-crates/venvs/ node_modules +.idea diff --git a/Cargo.lock b/Cargo.lock index 3dece1805..40b559c40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -238,9 +238,9 @@ dependencies = [ [[package]] name = "cargo-options" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e06313830a277c39d563be61cea4e008e32a810984e79ee5e46d9cae544018" +checksum = "cad71bf996c8e5b9d28ef3472d7ee41f277edf4e38cd597f51ad0438d05d76ea" dependencies = [ "anstyle", "clap", diff --git a/Cargo.toml b/Cargo.toml index bebdc0c79..426272321 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ base64 = "0.21.0" glob = "0.3.0" cargo-config2 = "0.1.9" cargo_metadata = "0.18.0" -cargo-options = "0.7.1" +cargo-options = "0.7.2" cbindgen = { version = "0.26.0", default-features = false } flate2 = "1.0.18" goblin = "0.7.1" diff --git a/src/compile.rs b/src/compile.rs index 8dc4c41dd..0d9bf3a55 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -447,6 +447,10 @@ fn compile_target( debug!("Running {:?}", build_command); + let using_cross = build_command + .get_program() + .to_string_lossy() + .starts_with("cross"); let mut cargo_build = build_command .spawn() .context("Failed to run `cargo rustc`")?; @@ -494,8 +498,18 @@ fn compile_target( .into_iter() .zip(artifact.filenames); for (crate_type, filename) in tuples { + let path = if using_cross && filename.starts_with("/target") { + // Convert cross target path in docker back to path on host + context + .cargo_metadata + .target_directory + .join(filename.strip_prefix("/target").unwrap()) + .into_std_path_buf() + } else { + filename.into() + }; let artifact = BuildArtifact { - path: filename.into(), + path, linked_paths: Vec::new(), }; artifacts.insert(crate_type, artifact); diff --git a/src/project_layout.rs b/src/project_layout.rs index e794521d8..affb25d80 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -317,6 +317,10 @@ impl ProjectResolver { debug!("Resolving cargo metadata from {:?}", manifest_path); let cargo_metadata_extra_args = extract_cargo_metadata_args(cargo_options)?; let result = MetadataCommand::new() + // Force resolving metadata using cargo instead of instead of $CARGO env var + // to avoid getting wrong file path like target directory, for example `cross` would + // output paths in docker while we want paths on host. + .cargo_path("cargo") .manifest_path(manifest_path) .verbose(true) .other_options(cargo_metadata_extra_args)