Skip to content

Commit

Permalink
Add support for cross compiling with cross
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Nov 26, 2023
1 parent d0b9f80 commit 9830a52
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test-crates/wheels/
test-crates/targets/
test-crates/venvs/
node_modules
.idea
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 15 additions & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`")?;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9830a52

Please sign in to comment.