diff --git a/Cargo.lock b/Cargo.lock index 35b873d..4342fb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,7 +75,7 @@ checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" [[package]] name = "cargo-hyperlight" -version = "0.1.9" +version = "0.1.10" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index e8c938c..7152e40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-hyperlight" -version = "0.1.9" +version = "0.1.10" edition = "2024" license = "Apache-2.0" homepage = "https://github.com/hyperlight-dev/cargo-hyperlight" diff --git a/src/toolchain.rs b/src/toolchain.rs index 94d7270..0773935 100644 --- a/src/toolchain.rs +++ b/src/toolchain.rs @@ -1,7 +1,7 @@ use std::ffi::OsString; use std::path::PathBuf; -use anyhow::{Context, Result}; +use anyhow::{Context, Result, bail}; use regex::Regex; use crate::cargo_cmd::{CargoCmd, cargo_cmd}; @@ -21,7 +21,7 @@ struct CargoMetadataPackage { version: semver::Version, } -pub fn prepare(args: &Args) -> Result<()> { +pub fn find_libc_dir(args: &Args) -> Result { let metadata = cargo_cmd()? .env_clear() .envs(args.env.iter()) @@ -37,16 +37,37 @@ pub fn prepare(args: &Args) -> Result<()> { let metadata = serde_json::from_slice::(&metadata.stdout) .context("Failed to parse cargo metadata")?; + let hyperlight_libc = metadata + .packages + .iter() + .find(|pkg| pkg.name == "hyperlight-libc"); + + if let Some(hyperlight_libc) = hyperlight_libc { + let hyperlight_libc_dir = hyperlight_libc + .manifest_path + .parent() + .context("Failed to get directory for hyperlight-libc")?; + return Ok(hyperlight_libc_dir.to_path_buf()); + } + let hyperlight_guest_bin = metadata .packages - .into_iter() - .find(|pkg| pkg.name == "hyperlight-guest-bin") - .context("Could not find hyperlight-guest-bin package in cargo metadata")?; + .iter() + .find(|pkg| pkg.name == "hyperlight-guest-bin"); + + if let Some(hyperlight_guest_bin) = hyperlight_guest_bin { + let hyperlight_guest_bin_dir = hyperlight_guest_bin + .manifest_path + .parent() + .context("Failed to get directory for hyperlight-guest-bin")?; + return Ok(hyperlight_guest_bin_dir.to_path_buf()); + } - let hyperlight_guest_bin_dir = hyperlight_guest_bin - .manifest_path - .parent() - .context("Failed to get directory for hyperlight-guest-bin")?; + bail!("Could not find hyperlight-libc or hyperlight-guest-bin package in cargo metadata"); +} + +pub fn prepare(args: &Args) -> Result<()> { + let libc_dir = find_libc_dir(args)?; let include_dst_dir = args.includes_dir(); @@ -54,27 +75,21 @@ pub fn prepare(args: &Args) -> Result<()> { .context("Failed to create sysroot include directory")?; // Detect which libc variant is present: picolibc or legacy musl - let include_dirs: &[&str] = if hyperlight_guest_bin_dir - .join("third_party/musl/include") - .is_dir() - { - &[ - "third_party/printf/", - "third_party/musl/include", - "third_party/musl/arch/generic", - "third_party/musl/arch/x86_64", - "third_party/musl/src/internal", - ] - } else { - &[ - "third_party/picolibc/libc/include", - "third_party/picolibc/libc/stdio", - "include", - ] - }; + let include_dirs: &[&str] = &[ + // directories for musl + "third_party/printf/", + "third_party/musl/include", + "third_party/musl/arch/generic", + "third_party/musl/arch/x86_64", + "third_party/musl/src/internal", + // directories for picolibc + "third_party/picolibc/libc/include", + "third_party/picolibc/libc/stdio", + "include", + ]; for dir in include_dirs { - let include_src_dir = hyperlight_guest_bin_dir.join(dir); + let include_src_dir = libc_dir.join(dir); let files = glob::glob(&format!("{}/**/*.h", include_src_dir.display())) .context("Failed to read include source directory")?;