Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 10 additions & 37 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ description = "cargo subcommand to build hyperlight guest binaries"
anyhow = "1.0"
clap = { version = "4", features = ["derive"] }
console = "0.16"
const_format = "0.2"
glob = "0.3"
libc = "0.2"
object = { version = "0.40", default-features = false, features = ["read", "elf"] }
Expand All @@ -25,4 +24,5 @@ tempfile = "3"
toml = "1.0"
which = { version = "8", features = ["regex"] }
quote = "1.0"
proc-macro2 = "1.0"
proc-macro2 = "1.0"
rustc_version = "0.4.1"
35 changes: 23 additions & 12 deletions examples/guest/Cargo.lock

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

39 changes: 9 additions & 30 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::collections::HashMap;
use std::convert::Infallible;
use std::env;
use std::env::consts::ARCH;
use std::ffi::{OsStr, OsString};
use std::fmt::Debug;
use std::path::PathBuf;

use anyhow::{Context, Result};
use const_format::formatcp;
use os_str_bytes::OsStrBytesExt as _;

use crate::cargo_cmd::{CargoCmd as _, cargo_cmd};
Expand All @@ -16,7 +14,7 @@ use crate::toolchain;
pub struct Args {
pub manifest_path: Option<PathBuf>,
pub target_dir: PathBuf,
pub target: String,
pub target: crate::target_info::TargetInfo,
pub host: String,
pub with_guest_capi: bool,
pub c_sysroot_dir: Option<PathBuf>,
Expand Down Expand Up @@ -151,28 +149,8 @@ impl Args {
},
};

let target = match value.target {
Some(triplet) => triplet,
None => match resolve_target(&value.env, &value.current_dir) {
Ok(triplet) => triplet,
Err(err) => warn.warning(
"could not resolve target triple",
err,
DEFAULT_TARGET.to_string(),
)?,
},
};

let target = if target.ends_with("-hyperlight-none") {
target
} else {
let (arch, _) = target.split_once('-').unwrap_or((&target, ""));
warn.warning(
"requested target is not a hyperlight target",
anyhow::anyhow!("invalid hyperlight target: {target}"),
format!("{arch}-hyperlight-none"),
)?
};
let target =
crate::target_info::resolve(warn, value.target, &value.env, &value.current_dir)?;

let target_dir = value.current_dir.join(target_dir);

Expand All @@ -196,8 +174,6 @@ impl Args {
}
}

const DEFAULT_TARGET: &str = const { formatcp!("{ARCH}-hyperlight-none") };

#[derive(Default)]
//#[command(disable_help_subcommand = true)]
struct ArgsImpl {
Expand Down Expand Up @@ -315,7 +291,10 @@ fn resolve_target_dir(
Ok(metadata.target_directory)
}

fn resolve_target(env: &HashMap<OsString, OsString>, cwd: &PathBuf) -> Result<String> {
pub(crate) fn resolve_target(
env: &HashMap<OsString, OsString>,
cwd: &PathBuf,
) -> Result<Option<String>> {
let output = cargo_cmd(env)?
.current_dir(cwd)
.arg("config")
Expand All @@ -336,8 +315,8 @@ fn resolve_target(env: &HashMap<OsString, OsString>, cwd: &PathBuf) -> Result<St
let target = target.trim_matches(|c| c == '"' || c == '\'');

if target.is_empty() {
Ok(DEFAULT_TARGET.into())
Ok(None)
} else {
Ok(target.into())
Ok(Some(target.into()))
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod cargo_cmd;
mod cli;
mod command;
mod sysroot;
pub mod target_info;
pub mod toolchain;
pub mod toolchain_flags;

Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum SpecialCommand {
New,
BuildCSysroot,
Flags(FlagKind),
PrintHyperlightTarget,
}
impl SpecialCommand {
fn parse(x: &OsStr) -> Option<Self> {
Expand All @@ -51,6 +52,7 @@ impl SpecialCommand {
"perf" => Some(Perf),
"new" => Some(New),
"build-c-sysroot" => Some(BuildCSysroot),
"print-hyperlight-target" => Some(PrintHyperlightTarget),
_ => FlagKind::parse(x).map(Flags),
}
}
Expand Down Expand Up @@ -96,6 +98,10 @@ impl SpecialCommand {
let flags = k.get_flags(&built_args).joined();
println!("{}", flags.to_str().ok_or(anyhow!("flags were not UTF-8"))?);
}
PrintHyperlightTarget => {
let built_args = cargo()?.args(args).build_args();
println!("{}", built_args.target);
}
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/new/host/_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() -> hyperlight_host::Result<()> {
// Create a sandbox from the guest binary. It starts uninitialized so you
// can register host functions before the guest begins executing.
let mut sandbox = UninitializedSandbox::new(
GuestBinary::FilePath("../guest/target/{arch}-hyperlight-none/debug/{guest_name}".into()),
GuestBinary::FilePath("../guest/target/{guest_target}/debug/{guest_name}".into()),
None,
)?;

Expand Down
6 changes: 4 additions & 2 deletions src/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use anyhow::{Context, Result, ensure};
use clap::Parser;

const HYPERLIGHT_VERSION: &str = "0.16";
const GUEST_ARCH: &str = std::env::consts::ARCH;

const GUEST_CARGO_TOML: &str = include_str!("guest/_Cargo.toml");
const GUEST_MAIN_RS: &str = include_str!("guest/_main.rs");
Expand Down Expand Up @@ -102,7 +101,10 @@ fn write_host(dir: &Path, name: &str, guest_name: &str) -> Result<()> {
let main_rs = HOST_MAIN_RS
.replace("{name}", name)
.replace("{guest_name}", guest_name)
.replace("{arch}", GUEST_ARCH);
.replace(
"{guest_target}",
cargo_hyperlight::target_info::default().as_ref(),
);
write_file(dir.join("Cargo.toml"), &cargo_toml)?;
write_file(dir.join("src/main.rs"), &main_rs)?;
Ok(())
Expand Down
Loading
Loading