use std::collections::BTreeMap;
use std::path::Path;
use nativeview_cli::manifest::{AndroidShell, AppPresenceConfig, NvManifest};
use super::android::bundle::{
android_apk_output_path, android_app_cdylib_spec, android_cmake_configure_args,
android_debug_keystore, android_image_cabi_rustc_args, android_image_cabi_spec_for_target,
android_runtime_spec, android_shell_cache_dir, ANDROID_RUST_TARGET,
};
use super::android::compose::compose_project_dir;
use super::android::run::android_launch_component;
use super::android::tools::NinjaLocation;
use super::apple::ios_bundle::{
ios_sim_app_cdylib_spec, ios_sim_cmake_configure_args, ios_sim_host_cache_dir,
ios_sim_runtime_spec, IOS_SIM_CMAKE_TARGET, IOS_SIM_RUST_TARGET,
};
use super::apple::macos_bundle::{macos_bundle_id, should_direct_exec_macos};
use super::host_cache::{
cmake_build_args, cmake_configure_args, host_cache_dir, modules_cache_key,
prebuild_runtime_spec, prebuild_win_a11y_spec, HostShell,
};
use super::pipeline::{
app_cdylib_spec, app_wasm_spec, BuildTarget, BuiltAppArtifact, ResolvedBuildPlan,
};
pub(crate) fn append_manifest_shell_args(args: &mut Vec<String>, manifest: &NvManifest) {
if manifest.app.presence != AppPresenceConfig::Standard {
args.push("--presence".to_owned());
args.push(manifest.app.presence.as_str().to_owned());
}
if let Some(w) = manifest.app.width {
args.push("--width".to_owned());
args.push(w.to_string());
}
if let Some(h) = manifest.app.height {
args.push("--height".to_owned());
args.push(h.to_string());
}
}
pub(crate) fn desktop_shell_launch_args(
shell: HostShell,
dylib_path: &Path,
manifest: &NvManifest,
extra_args: &[String],
) -> Vec<String> {
let mut args = vec!["--dylib".to_owned(), dylib_path.display().to_string()];
append_manifest_shell_args(&mut args, manifest);
if shell == HostShell::Skia {
args.push("--no-ws".to_owned());
}
args.extend(extra_args.iter().cloned());
args
}
use crate::cargo_env::{AppContext, NvSdkRoot};
#[derive(Debug, Clone, Copy)]
pub(crate) struct PlanRunOptions<'a> {
pub http_port: u16,
pub direct: bool,
pub device: Option<&'a str>,
pub headless: bool,
pub pair_only: bool,
pub pair_uri: Option<&'a str>,
pub shell_args: &'a [String],
}
pub(crate) fn format_app_plan(
app: &AppContext,
sdk_root: Option<&NvSdkRoot>,
manifest: &NvManifest,
plan: &ResolvedBuildPlan,
run_opts: Option<PlanRunOptions<'_>>,
) -> String {
let profile = if plan.release { "release" } else { "debug" };
let info = app.manifest_app_info();
let sdk_display = sdk_root
.map(|s| s.path().display().to_string())
.unwrap_or_else(|| "<none (standalone web)>".to_owned());
let mut out = format!(
"Native View Engine — {} Plan\n app: {} ({}, lib={})\n manifest: {}\n sdk_root: {}\n target: {}{}\n profile: {profile}\n modules: {}\n out_dir: {}\n\n",
if run_opts.is_some() { "Run" } else { "Build" },
manifest.resolved_name(&info),
app.package_name,
app.lib_name,
app.manifest_path.display(),
sdk_display,
plan.target.as_str(),
plan.shell.map(|s| format!(" (shell={})", s.as_str())).unwrap_or_default(),
modules_cache_key(&app.host_modules),
plan.out_dir.display(),
);
match plan.target {
BuildTarget::Macos | BuildTarget::Linux | BuildTarget::Windows => {
if let Some(sdk) = sdk_root {
format_desktop_plan(&mut out, app, sdk, manifest, plan, profile, run_opts);
}
}
BuildTarget::Web => {
let wasm_cmd = app_wasm_spec(app, plan.release).cargo_args().join(" ");
let pwa_dir = plan.out_dir.join("pwa");
out.push_str(&format!("[1/3] WASM cdylib\n cmd cargo {wasm_cmd}\n\n"));
out.push_str(&format!(
"[2/3] wasm-bindgen\n cmd wasm-bindgen --target web --no-typescript --out-dir {}/pkg <wasm>\n\n",
pwa_dir.display()
));
out.push_str(&format!("[3/3] Web bundle\n pwa {}\n", pwa_dir.display()));
if manifest.web.extension.is_some() {
out.push_str(&format!(" ext {}\n", plan.out_dir.join("extension").display()));
}
if let Some(run) = run_opts {
out.push_str(&format!(
"\n[run]\n serve {} at http://localhost:{}/\n",
pwa_dir.display(),
run.http_port
));
}
}
BuildTarget::IosSim => {
if let Some(sdk) = sdk_root {
format_ios_sim_plan(&mut out, app, sdk, manifest, plan, profile, run_opts);
}
}
BuildTarget::Android => {
if let Some(sdk) = sdk_root {
format_android_plan(&mut out, app, sdk, manifest, plan, profile, run_opts);
}
}
BuildTarget::WebPkg | BuildTarget::RnPkg | BuildTarget::NitroUiPkg => {
out.push_str("[export] handled by export_pkg::format_export_plan.\n");
}
}
out
}
fn format_ios_sim_plan(
out: &mut String,
app: &AppContext,
sdk_root: &NvSdkRoot,
manifest: &NvManifest,
plan: &ResolvedBuildPlan,
profile: &str,
run_opts: Option<PlanRunOptions<'_>>,
) {
let info = app.manifest_app_info();
let min_ios = manifest.apple.min_ios.as_str();
let pair_only = run_opts.is_some_and(|r| r.pair_only || r.pair_uri.is_some());
if pair_only {
out.push_str("[1/3] App cdylib\n skip --pair-only (no embedded iOS app cdylib)\n\n");
} else {
let app_cmd =
ios_sim_app_cdylib_spec(app, plan.release, None, min_ios).cargo_args().join(" ");
out.push_str(&format!(
"[1/3] App cdylib ({IOS_SIM_RUST_TARGET})\n cmd cargo {app_cmd}\n\n"
));
}
let build_dir =
ios_sim_host_cache_dir(&app.target_directory, plan.release, min_ios, &app.host_modules);
let rt_lib = if sdk_root.is_packaged_sdk() {
sdk_root
.path()
.join("lib")
.join(IOS_SIM_RUST_TARGET)
.join("libnv_runtime.a")
} else {
app.target_directory
.join(IOS_SIM_RUST_TARGET)
.join(profile)
.join("libnv_runtime.a")
};
let cfg = ios_sim_cmake_configure_args(
&sdk_root.path().join("hosts").join("ios-shell"),
&build_dir,
plan.release,
&app.host_modules,
&rt_lib,
min_ios,
true,
);
let bld = cmake_build_args(&build_dir, IOS_SIM_CMAKE_TARGET, plan.release, false);
if sdk_root.is_packaged_sdk() {
out.push_str(&format!(
"[2/3] iOS Simulator host shell (packaged SDK prebuilt runtime)\n prebuilt libnv_runtime.a ({IOS_SIM_RUST_TARGET})\n cache {}\n cmd cmake {}\n cmd cmake {}\n\n",
build_dir.display(),
cfg.join(" "),
bld.join(" "),
));
} else {
let rt_spec =
ios_sim_runtime_spec(sdk_root.path(), &app.target_directory, plan.release, None, min_ios);
out.push_str(&format!(
"[2/3] iOS Simulator host shell\n cmd cargo {}\n cache {}\n cmd cmake {}\n cmd cmake {}\n\n",
rt_spec.cargo_args().join(" "),
build_dir.display(),
cfg.join(" "),
bld.join(" "),
));
}
let exe = manifest.resolved_executable(&info);
let bundle_id = manifest.resolved_id(&info);
let bundle = plan.out_dir.join(format!("{exe}.app"));
let binary = bundle.join(&exe);
out.push_str(&format!(
"[3/3] iOS Simulator .app bundle\n bundle {}\n bundle_id {bundle_id}\n binary {}\n",
bundle.display(),
binary.display(),
));
if !pair_only {
out.push_str(&format!(" dylib {}/libnv_app.dylib\n", bundle.display()));
}
if let Some(run) = run_opts {
let dev = run.device.unwrap_or("<auto-booted-or-newest-iPhone>");
let extra = if run.shell_args.is_empty() {
String::new()
} else {
format!(" {}", run.shell_args.join(" "))
};
let env_prefix = match run.pair_uri {
Some(uri) => format!("SIMCTL_CHILD_NV_PAIR_CONNECT={uri} "),
None => String::new(),
};
out.push_str(&format!(
"\n[run]\n device {dev} (headless={})\n cmd xcrun simctl install {dev} {}\n cmd {env_prefix}xcrun simctl launch {dev} {bundle_id}{extra}\n",
run.headless,
bundle.display(),
));
}
}
fn format_android_plan(
out: &mut String,
app: &AppContext,
sdk_root: &NvSdkRoot,
manifest: &NvManifest,
plan: &ResolvedBuildPlan,
profile: &str,
run_opts: Option<PlanRunOptions<'_>>,
) {
let info = app.manifest_app_info();
let app_id = manifest
.resolved_android_id(&info)
.unwrap_or_else(|_| manifest.resolved_id(&info));
let shell = match plan.shell {
Some(HostShell::Compose) => AndroidShell::Compose,
_ => AndroidShell::View,
};
let min_sdk = manifest.android.min_sdk;
let target_sdk = manifest.android.target_sdk;
let rt_cmd = android_runtime_spec(sdk_root.path(), &app.target_directory, plan.release, BTreeMap::new())
.cargo_args()
.join(" ");
let img_cmd = android_image_cabi_spec_for_target(
sdk_root.path(),
&app.target_directory,
ANDROID_RUST_TARGET,
plan.release,
BTreeMap::new(),
)
.cargo_args_with_rustc(&android_image_cabi_rustc_args())
.join(" ");
let app_cmd = android_app_cdylib_spec(app, plan.release, BTreeMap::new()).cargo_args().join(" ");
let cache_dir = android_shell_cache_dir(&app.target_directory, min_sdk, plan.release);
let rt_lib = app.target_directory.join(ANDROID_RUST_TARGET).join(profile).join("libnv_runtime.a");
let img_lib = app.target_directory.join(ANDROID_RUST_TARGET).join(profile).join("libnativeview_image.so");
let cfg = android_cmake_configure_args(
&sdk_root.path().join("hosts/android-shell"),
&cache_dir,
Path::new("<ndk>/build/cmake/android.toolchain.cmake"),
min_sdk,
plan.release,
&rt_lib,
&img_lib,
&NinjaLocation::OnPath,
);
let apk_path = android_apk_output_path(&plan.out_dir, &app.lib_name, plan.release);
if sdk_root.is_packaged_sdk() {
out.push_str(&format!(
"[1/3] Native arm64-v8a libraries ({ANDROID_RUST_TARGET}, min_sdk={min_sdk}, packaged SDK prebuilt)\n\
\x20 abi prebuilt SDK headers (read-only)\n\
\x20 prebuilt libnv_android_shell.so + libnativeview_image.so (runtime[\"android-all\"])\n\
\x20 cmd cargo {app_cmd}\n\n",
));
} else {
out.push_str(&format!(
"[1/3] Native arm64-v8a libraries ({ANDROID_RUST_TARGET}, min_sdk={min_sdk})\n\
\x20 abi cargo xtask codegen abi (workspace checkout)\n\
\x20 cmd cargo {rt_cmd}\n\
\x20 cmd cargo {img_cmd}\n\
\x20 cmd cargo {app_cmd}\n\
\x20 cache {}\n\
\x20 cmd cmake {}\n\
\x20 cmd cmake --build {} --target nv_android_shell --parallel\n\n",
cache_dir.display(),
cfg.join(" "),
cache_dir.display(),
));
}
match shell {
AndroidShell::View => {
let ks = android_debug_keystore(&app.target_directory);
out.push_str(&format!(
"[2/3] Android View host Java + DEX (HotSpot JDK)\n\
\x20 cmd javac --release 11 -Xlint:all -Werror -cp <android.jar> <host-java,backends/android,canvas,image,linking,media-fit,svg,android-app>\n\
\x20 cmd d8 --min-api {min_sdk} --lib <android.jar> <classes>\n\n\
[3/3] Signed zero-Gradle APK\n\
\x20 cmd aapt2 link --min-sdk-version {min_sdk} --target-sdk-version {target_sdk}\n\
\x20 libs lib/arm64-v8a/{{libnv_android_shell.so,libnativeview_image.so,libnv_app.so,libc++_shared.so}} (uncompressed)\n\
\x20 ks {}\n\
\x20 apk {}\n\
\x20 id {app_id}\n",
ks.display(),
apk_path.display(),
));
}
AndroidShell::Compose => {
let proj = compose_project_dir(&app.target_directory, &app.lib_name);
let gradlew = sdk_root.path().join("packages/backends/compose/gradlew");
let task = if plan.release { "assembleRelease" } else { "assembleDebug" };
let has_camera = app.host_modules.iter().any(|m| m == "camera");
let deps = if has_camera {
"io.github.nativeview:nativeview-standard-android, io.github.nativeview:camera"
} else {
"io.github.nativeview:nativeview-standard-android"
};
out.push_str(&format!(
"[2/3] Synthesize cached Android Compose project\n\
\x20 dir {}\n\
\x20 deps {deps}\n\
\x20 libs src/main/jniLibs/arm64-v8a/{{libnv_android_shell.so,libnv_app.so,libc++_shared.so}}\n\n\
[3/3] Assemble Compose APK (HotSpot JDK 17–23)\n\
\x20 cmd {} -p {} {task} --no-daemon\n\
\x20 apk {}\n\
\x20 id {app_id}\n",
proj.display(),
gradlew.display(),
proj.display(),
apk_path.display(),
));
}
}
if let Some(run) = run_opts {
let serial = run.device.unwrap_or("<first-online-adb-device>");
let component = android_launch_component(&app_id, shell);
out.push_str(&format!(
"\n[run]\n cmd adb -s {serial} install -r {}\n cmd adb -s {serial} shell am start -W -n {component}\n",
apk_path.display(),
));
}
}
fn format_desktop_plan(
out: &mut String,
app: &AppContext,
sdk_root: &NvSdkRoot,
manifest: &NvManifest,
plan: &ResolvedBuildPlan,
profile: &str,
run_opts: Option<PlanRunOptions<'_>>,
) {
let info = app.manifest_app_info();
let shell = plan.shell.unwrap_or(HostShell::Skia);
let dll_suffix = match plan.target {
BuildTarget::Macos => ".dylib",
BuildTarget::Linux => ".so",
_ => ".dll",
};
let app_cmd = app_cdylib_spec(app, plan.release, dll_suffix).cargo_args().join(" ");
out.push_str(&format!("[1/3] App cdylib\n cmd cargo {app_cmd}\n\n"));
out.push_str("[2/3] Host shell\n");
if shell == HostShell::Skia {
if sdk_root.is_packaged_sdk() {
out.push_str(" prebuilt nv-skia-shell (from packaged SDK)\n\n");
} else {
let rel = if plan.release { " --release" } else { "" };
out.push_str(&format!(
" cmd cargo build -p nativeview-skia-shell --bin nativeview-skia-shell{rel}\n\n"
));
}
} else if let Some(hdir) = shell.cmake_host_dir()
&& let Some(target_name) = shell.cmake_target_name()
{
let is_win = plan.target == BuildTarget::Windows;
let build_dir =
host_cache_dir(&app.target_directory, shell, plan.release, &app.host_modules);
if sdk_root.is_packaged_sdk() {
out.push_str(" prebuilt libnv_runtime (from packaged SDK)\n");
if shell == HostShell::Windows {
out.push_str(" prebuilt nv_accessibility_windows (optional, from packaged SDK)\n");
}
} else {
let rt_spec =
prebuild_runtime_spec(sdk_root.path(), &app.target_directory, plan.release, is_win);
out.push_str(&format!(" cmd cargo {}\n", rt_spec.cargo_args().join(" ")));
if shell == HostShell::Windows {
let a11y_spec =
prebuild_win_a11y_spec(sdk_root.path(), &app.target_directory, plan.release);
out.push_str(&format!(" cmd cargo {}\n", a11y_spec.cargo_args().join(" ")));
}
}
let rt_lib = app.target_directory.join(profile).join(format!(
"{}nv_runtime{}",
if is_win { "" } else { "lib" },
if is_win { ".lib" } else { ".a" }
));
let win_a11y = (shell == HostShell::Windows).then(|| {
(
app.target_directory.join(profile).join("nativeview_accessibility_windows.dll"),
app.target_directory.join(profile).join("nativeview_accessibility_windows.dll.lib"),
)
});
let use_ninja = !is_win;
let cfg = cmake_configure_args(
&sdk_root.path().join("hosts").join(hdir),
&build_dir,
shell,
plan.release,
&app.host_modules,
&rt_lib,
win_a11y.as_ref().map(|(d, i)| (d.as_path(), i.as_path())),
use_ninja,
plan.target == BuildTarget::Macos,
);
let bld = cmake_build_args(&build_dir, target_name, plan.release, is_win && !use_ninja);
out.push_str(&format!(" cache {}\n", build_dir.display()));
out.push_str(&format!(" cmd cmake {}\n", cfg.join(" ")));
out.push_str(&format!(" cmd cmake {}\n\n", bld.join(" ")));
}
if plan.target == BuildTarget::Macos {
let exe = manifest.resolved_executable(&info);
let bundle_id = macos_bundle_id(manifest, &info, shell);
let bundle = plan.out_dir.join(format!("{exe}.app"));
let binary = bundle.join("Contents").join("MacOS").join(&exe);
out.push_str(&format!(
"[3/3] macOS .app bundle\n bundle {}\n bundle_id {bundle_id}\n binary {}\n dylib {}/Contents/Frameworks/libnv_app.dylib\n",
bundle.display(),
binary.display(),
bundle.display(),
));
if let Some(run) = run_opts {
if should_direct_exec_macos(run.direct, run.shell_args) {
let extra = if run.shell_args.is_empty() {
String::new()
} else {
format!(" {}", run.shell_args.join(" "))
};
out.push_str(&format!("\n[run]\n cmd {}{extra}\n", binary.display()));
} else {
let extra = if run.shell_args.is_empty() {
String::new()
} else {
format!(" --args {}", run.shell_args.join(" "))
};
out.push_str(&format!("\n[run]\n cmd open -W -n {}{extra}\n", bundle.display()));
}
}
} else {
let exe_name = shell.cmake_target_name().unwrap_or("nativeview-skia-shell");
let exe_suffix = if plan.target == BuildTarget::Windows { ".exe" } else { "" };
let staged_exe = plan.out_dir.join(format!("{exe_name}{exe_suffix}"));
let staged_lib = plan.out_dir.join(format!(
"{}{}{}",
if plan.target == BuildTarget::Windows { "" } else { "lib" },
app.lib_name,
dll_suffix
));
out.push_str(&format!(
"[3/3] Stage desktop bundle\n exe {}\n dylib {}\n",
staged_exe.display(),
staged_lib.display()
));
if let Some(run) = run_opts {
let args = desktop_shell_launch_args(shell, &staged_lib, manifest, run.shell_args);
out.push_str(&format!(
"\n[run]\n cmd {} {}\n",
staged_exe.display(),
args.join(" ")
));
}
}
}
pub(crate) fn format_built_app_summary(built: &BuiltAppArtifact) -> String {
let mut summary = String::from("==> Build complete:\n");
match built {
BuiltAppArtifact::MacosApp { app_bundle, executable, dylib, bundle_id } => {
summary.push_str(&format!(
" bundle: {}\n bundle_id: {bundle_id}\n binary: {}\n dylib: {}\n",
app_bundle.display(),
executable.display(),
dylib.display(),
));
}
BuiltAppArtifact::IosSimApp { app_bundle, executable, dylib, bundle_id } => {
summary.push_str(&format!(
" bundle: {}\n bundle_id: {bundle_id}\n binary: {}\n",
app_bundle.display(),
executable.display(),
));
if let Some(d) = dylib {
summary.push_str(&format!(" dylib: {}\n", d.display()));
}
}
BuiltAppArtifact::DesktopStaged { shell_binary, app_dylib, .. } => {
summary.push_str(&format!(
" shell: {}\n dylib: {}\n",
shell_binary.display(),
app_dylib.display(),
));
}
BuiltAppArtifact::WebBundle { pwa_dir, extension_dir } => {
summary.push_str(&format!(" pwa: {}\n", pwa_dir.display()));
if let Some(ext) = extension_dir {
summary.push_str(&format!(" ext: {}\n", ext.display()));
}
}
BuiltAppArtifact::AndroidApk { apk_path, app_id, shell } => {
summary.push_str(&format!(
" apk: {}\n app_id: {app_id}\n shell: {}\n",
apk_path.display(),
shell.as_str(),
));
}
}
summary
}