Skip to content

Commit

Permalink
misc: add build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed Oct 18, 2024
1 parent 10f976c commit f3dcd88
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

static PIXI_TRAMPOLINE: &str = "pixi_trampoline";
static TRAMPOLINES_FOLDER: &str = "trampolines";

fn main() {
// Construct trampoline crate path
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let trampoline_crate_path = format!("{}/crates/{}", manifest_dir, PIXI_TRAMPOLINE);
let trampoline_crate = Path::new(&trampoline_crate_path);
let trampolines_dir = trampoline_crate.join(TRAMPOLINES_FOLDER);

// Get the target triplet, which will be used to name the trampoline binary
let target_triplet = env::var("TARGET").expect("TARGET env variable should be set by cargo");

// Build the trampoline binary
// we always use --release profile for this
Command::new("cargo")
.current_dir(&trampoline_crate)

Check failure on line 22 in build.rs

View workflow job for this annotation

GitHub Actions / Cargo Lint

the borrowed expression implements the required traits
.args(&["build", "--release", "--target", target_triplet.as_str()])

Check failure on line 23 in build.rs

View workflow job for this annotation

GitHub Actions / Cargo Lint

the borrowed expression implements the required traits
.status()
.expect("Failed to build trampoline crate");

// Create trampolines directory if it doesn't exist
fs::create_dir_all(&trampolines_dir).expect("Failed to create trampolines directory");

let cargo_target_dir =
env::var("CARGO_TARGET_DIR").expect("CARGO_TARGET_DIR should be set by cargo");
// Construct the path to the built binary
let target_path = PathBuf::from_iter([
cargo_target_dir.as_str(),
target_triplet.as_str(),
"release",
PIXI_TRAMPOLINE,
]);

let built_binary_path = trampoline_crate.join(target_path);
let dest_path = trampolines_dir.join(format!("pixi_trampoline-{}", target_triplet));

// Copy the built binary to the trampolines directory
fs::copy(&built_binary_path, &dest_path).expect("Failed to copy second crate binary");

// Tell cargo to re-run this build script if the trampoline binary code changes
println!("cargo:rerun-if-changed=crates/pixi_trampoline/src");
}
Binary file not shown.

0 comments on commit f3dcd88

Please sign in to comment.