Skip to content

Commit

Permalink
Merge pull request #8 from LittleBoxOfSunshine/chhenk/crosscomp
Browse files Browse the repository at this point in the history
Support cross compilation / CompileKind::target inference
  • Loading branch information
prenwyn committed Dec 19, 2023
2 parents 0eb7fb5 + b9ed41a commit 496afd4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ include = ["src/", "example_files", "LICENSE-*", "README.md", "COPYRIGHT"]
[dependencies]
anyhow = "1.0"
fs_extra = "1.3"
build-target = "0.4"
29 changes: 25 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
use std::result::Result::Ok;
use std::env;
use fs_extra::copy_items;
use fs_extra::dir::CopyOptions;
use std::path::Path;
use std::path::{Path, PathBuf};
use anyhow::*;

pub fn copy_to_output(path: &str, build_type: &str) -> Result<()> {
let mut options = CopyOptions::new();
let mut from_path = Vec::new();
let out_path = format!("target/{}", build_type);
let mut out_path = PathBuf::new();
out_path.push("target");

// This is a hack, ideally we would plug into https://docs.rs/cargo/latest/cargo/core/compiler/enum.CompileKind.html
// However, since the path follows predictable rules https://doc.rust-lang.org/cargo/guide/build-cache.html
// we can just check our parent path for the pattern target/{triple}/{profile}.
// If it is present, we know CompileKind::Target was used, otherwise CompileKind::Host was used.
// Best effort since the existing tests aren't intended to be run in a real build this won't exist.
// Unclear if that also means people in the wild are using the crate similarly, so avoiding any risk of break.
if let Ok(triple) = build_target::target_triple() {
if let Some(out_dir) = env::var_os("OUT_DIR") {
if let Some(out_dir) = out_dir.to_str() {
if out_dir.contains(&format!("target{}{}", std::path::MAIN_SEPARATOR, triple)) {
out_path.push(triple);
}
}
}
}

out_path.push(build_type);

// Overwrite existing files with same name
let mut options = CopyOptions::new();
options.overwrite = true;

let mut from_path = Vec::new();
from_path.push(path);
copy_items(&from_path, &out_path, &options)?;

Expand Down

0 comments on commit 496afd4

Please sign in to comment.