use std::{
env,
ffi::OsString,
path::{Path, PathBuf},
process::Command,
};
use anyhow::{Context as _, bail};
#[derive(Debug, Clone, Copy)]
pub(crate) enum Platform {
IphoneOs,
IphoneSimulator,
MacCatalyst,
Linux,
MacOs,
Windows,
Gpui,
}
impl Platform {
fn dir(self) -> &'static str {
match self {
Platform::IphoneOs => "ios/iphoneos",
Platform::IphoneSimulator => "ios/iphonesimulator",
Platform::MacCatalyst => "ios/maccatalyst",
Platform::Linux => "linux",
Platform::MacOs => "macos",
Platform::Windows => "windows",
Platform::Gpui => "gpui",
}
}
pub(crate) fn triple(self) -> Option<&'static str> {
match self {
Platform::IphoneOs => Some("aarch64-apple-ios"),
Platform::IphoneSimulator => Some("aarch64-apple-ios-sim"),
Platform::MacCatalyst => Some("aarch64-apple-ios-macabi"),
Platform::Linux | Platform::MacOs | Platform::Windows | Platform::Gpui => None,
}
}
pub(crate) fn binary(self, runner: bool) -> &'static str {
match (self, runner) {
(Platform::Linux, false) => "nsh-linux-host",
(Platform::Linux, true) => "nsh-linux-app",
(Platform::MacOs, false) => "nsh-macos-host",
(Platform::MacOs, true) => "nsh-macos-app",
(Platform::Windows, false) => "nsh-windows-host",
(Platform::Windows, true) => "nsh-windows-app",
(Platform::Gpui, false) => "nsh-gpui-host",
(Platform::Gpui, true) => "nsh-gpui-app",
(_, false) => "nsh-ios-host",
(_, true) => "nsh-ios-app",
}
}
pub(crate) fn file_name(self, runner: bool) -> String {
let suffix = if self.triple().is_none() {
env::consts::EXE_SUFFIX
} else {
""
};
format!("{}{suffix}", self.binary(runner))
}
pub(crate) fn host(self, runner: bool) -> String {
let what = if runner {
"release runner"
} else {
"development host"
};
let platform = match self {
Platform::IphoneOs => "iOS devices",
Platform::IphoneSimulator => "the iOS simulator",
Platform::MacCatalyst => "Mac Catalyst",
Platform::Linux => "Linux",
Platform::MacOs => "macOS",
Platform::Windows => "Windows",
Platform::Gpui => "GPUI",
};
format!("the {what} for {platform}")
}
}
pub(crate) fn cli_exe() -> std::io::Result<PathBuf> {
let exe = env::current_exe()?;
if cfg!(windows) {
return Ok(exe);
}
Ok(exe.canonicalize().unwrap_or(exe))
}
pub(crate) fn android(dev: bool) -> anyhow::Result<PathBuf> {
let prebuilt = prebuilt_android(dev)?;
match crate::rust_app::current() {
Some(app) => app.android(&prebuilt, dev),
None => Ok(prebuilt),
}
}
fn prebuilt_android(dev: bool) -> anyhow::Result<PathBuf> {
let variant = if dev { "dev" } else { "release" };
if let Some(root) = env::var_os("NSH_RUNTIME") {
return android_existing(PathBuf::from(root).join("android").join(variant), dev);
}
let exe = cli_exe()?;
let bin = exe.parent().context("cli has no parent directory")?;
let dist = bin.join("../runtime/android").join(variant);
if dist.join("classes.dex").exists() {
return Ok(dist);
}
let profile = bin
.file_name()
.context("cli profile dir")?
.to_string_lossy();
if let Some(root) = checkout(bin) {
let mut args = vec!["xtask", "android"];
if profile == "release" {
args.push("--release");
}
if dev {
args.push("--dev");
}
let what = if dev {
"the Android runtime with development support"
} else {
"the Android runtime"
};
cargo(&root, &args, what)?;
}
let name = if dev {
format!("{profile}-dev")
} else {
profile.into_owned()
};
let built = bin
.parent()
.context("cli is not in a target directory")?
.join("android")
.join(name);
android_existing(built, dev)
}
fn android_existing(dir: PathBuf, dev: bool) -> anyhow::Result<PathBuf> {
if dir.join("classes.dex").exists() {
return Ok(dir);
}
bail!(
"the Android runtime is not at {}\nin a nsh checkout, build it with: cargo xtask android{}",
dir.display(),
if dev { " --dev" } else { "" }
)
}
pub(crate) fn file(platform: Platform, runner: bool) -> anyhow::Result<PathBuf> {
if let Some(app) = crate::rust_app::current() {
return app.runtime(platform, runner);
}
let name = platform.binary(runner);
let file_name = platform.file_name(runner);
if let Some(root) = env::var_os("NSH_RUNTIME") {
return existing(
PathBuf::from(root).join(platform.dir()).join(&file_name),
platform,
name,
);
}
let exe = cli_exe()?;
let bin = exe.parent().context("cli has no parent directory")?;
let dist = bin.join("../runtime").join(platform.dir()).join(&file_name);
if dist.exists() {
return Ok(dist);
}
let profile = bin.file_name().context("cli profile dir")?;
if let Some(root) = checkout(bin) {
let mut args = vec!["build", "-p", name];
let manifest = root.join("crates/gpui/Cargo.toml");
let manifest = manifest.to_string_lossy();
let target_dir = root.join("target");
let target_dir = target_dir.to_string_lossy();
if matches!(platform, Platform::Gpui) {
args.extend(["--manifest-path", &manifest, "--target-dir", &target_dir]);
}
if let Some(triple) = platform.triple() {
args.extend(["--target", triple]);
}
match profile.to_str() {
Some("debug") => {}
Some("release") => args.push("--release"),
Some(other) => args.extend(["--profile", other]),
None => bail!("cli profile dir is not UTF-8"),
}
let what = match profile.to_str() {
Some("release") => platform.host(runner),
Some(other) => format!("{} ({other} build)", platform.host(runner)),
None => platform.host(runner),
};
cargo(&root, &args, &what)?;
}
let mut built = bin
.parent()
.context("cli is not in a target directory")?
.to_path_buf();
if let Some(triple) = platform.triple() {
built.push(triple);
}
built.push(profile);
built.push(file_name);
existing(built, platform, name)
}
fn checkout(bin: &Path) -> Option<PathBuf> {
let root = bin.parent()?.parent()?;
root.join("crates/nsh-ios-host/Cargo.toml")
.exists()
.then(|| root.to_owned())
}
fn cargo(root: &Path, args: &[&str], what: &str) -> anyhow::Result<()> {
let cargo = env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo"));
crate::ui::cargo(Command::new(&cargo).args(args).current_dir(root), what)
}
fn existing(path: PathBuf, platform: Platform, name: &str) -> anyhow::Result<PathBuf> {
if path.exists() {
return Ok(path);
}
let target = platform
.triple()
.map(|triple| format!(" --target {triple}"))
.unwrap_or_default();
bail!(
"{name} for {} not found at {}\nin a nsh checkout, build it with: cargo build -p {name}{target}",
platform.dir(),
path.display(),
)
}
#[cfg(test)]
mod tests {
use super::Platform;
#[test]
fn gpui_runtimes_are_built_for_this_machine() {
assert_eq!(Platform::Gpui.dir(), "gpui");
assert_eq!(Platform::Gpui.triple(), None);
assert_eq!(Platform::Gpui.binary(false), "nsh-gpui-host");
assert_eq!(Platform::Gpui.binary(true), "nsh-gpui-app");
assert_eq!(
Platform::Gpui.file_name(true),
format!("nsh-gpui-app{}", std::env::consts::EXE_SUFFIX)
);
assert_eq!(Platform::Gpui.host(false), "the development host for GPUI");
}
}