use super::*;
pub fn current_host() -> HostOs {
match HOST_OS {
HOST_MACOS => HostOs::Macos,
HOST_LINUX => HostOs::Linux,
HOST_WINDOWS => HostOs::Windows,
_ => HostOs::Unsupported,
}
}
pub fn current_arch() -> String {
HOST_ARCH.to_string()
}
pub fn host_platforms(host: HostOs) -> Vec<NativePlatform> {
match host {
HostOs::Macos => vec![
NativePlatform::Macos,
NativePlatform::Ios,
NativePlatform::IosSimulator,
NativePlatform::Android,
],
HostOs::Linux => vec![NativePlatform::Linux, NativePlatform::Android],
HostOs::Windows => vec![NativePlatform::Windows],
HostOs::Unsupported => Vec::new(),
}
}
pub fn native_task(platform: NativePlatform) -> NativeTask {
match platform {
NativePlatform::Android => NativeTask::Android,
_ => NativeTask::Client,
}
}
pub fn host_supports(host: HostOs, platform: NativePlatform) -> bool {
match platform {
NativePlatform::Android => matches!(host, HostOs::Macos | HostOs::Linux),
NativePlatform::Macos | NativePlatform::Ios | NativePlatform::IosSimulator => {
matches!(host, HostOs::Macos)
}
NativePlatform::Linux => matches!(host, HostOs::Linux),
NativePlatform::Windows => matches!(host, HostOs::Windows),
}
}
pub fn describe_platforms(platforms: &[NativePlatform]) -> String {
platforms
.iter()
.map(|platform: &NativePlatform| platform.platform_name())
.collect::<Vec<&str>>()
.join(", ")
}
pub fn describe_requirements(platforms: &[NativePlatform]) -> String {
platforms
.iter()
.map(|platform: &NativePlatform| {
format!(
"{} needs a {} host",
platform.platform_name(),
platform.required_host()
)
})
.collect::<Vec<String>>()
.join("; ")
}
pub fn resolve_requested_platforms(
all: bool,
platforms: &[NativePlatform],
) -> Result<Vec<NativePlatform>, EuvError> {
if all && !platforms.is_empty() {
return Err(EuvError::Message(format!(
"{ALL_ARG} and {PLATFORMS_ARG} cannot be combined: {ALL_ARG} selects every \
platform this host can build, and {PLATFORMS_ARG} names them explicitly."
)));
}
if all {
return Ok(host_platforms(current_host()));
}
if platforms.is_empty() {
return Ok(Vec::new());
}
let mut unique: Vec<NativePlatform> = Vec::new();
for platform in platforms {
if !unique.contains(platform) {
unique.push(*platform);
}
}
Ok(unique)
}
pub fn resolve_native_plan(
requested: &[NativePlatform],
host: HostOs,
) -> Result<NativePlan, EuvError> {
if host == HostOs::Unsupported || current_arch() == ARCH_UNSUPPORTED {
return Err(EuvError::Message(
"Native packaging is unsupported on this operating system or architecture".to_string(),
));
}
let mut unsupported: Vec<NativePlatform> = Vec::new();
for platform in requested {
if !host_supports(host, *platform) && !unsupported.contains(platform) {
unsupported.push(*platform);
}
}
if !unsupported.is_empty() {
return Err(EuvError::Message(format!(
"{} cannot be built on {}: {}. Build {} on a matching host, or drop it from \
{PLATFORMS_ARG}.",
describe_platforms(&unsupported),
host.host_name(),
describe_requirements(&unsupported),
describe_platforms(&unsupported),
)));
}
let mut targets: Vec<NativeTarget> = Vec::new();
for platform in requested {
targets.push(NativeTarget {
platform: *platform,
task: native_task(*platform),
});
}
Ok(NativePlan {
host,
arch: current_arch(),
targets,
unsupported,
})
}
pub fn print_native_plan(plan: &NativePlan) {
log::info!("Host: {} ({})", plan.host.host_name(), plan.arch);
log::info!(
"Native selection is host-specific; other operating systems require matching build hosts."
);
if plan.targets.is_empty() {
log::info!("Native platforms: none");
} else {
for target in &plan.targets {
log::info!(
"Native platform: {} ({})",
target.platform.platform_name(),
target.task.task_name()
);
}
}
if !plan.unsupported.is_empty() {
log::warn!(
"Not buildable on this host: {} ({})",
describe_platforms(&plan.unsupported),
describe_requirements(&plan.unsupported)
);
}
}
pub fn resolve_native_project(base: &Path, native_path: Option<&PathBuf>) -> PathBuf {
match native_path {
Some(path) if path.is_absolute() => path.clone(),
Some(path) => base.join(path),
None => base.join(NATIVE_PROJECT_RELATIVE_PATH),
}
}
pub fn require_native_project(native_project: &Path) -> Result<(), EuvError> {
if native_project.is_dir() {
return Ok(());
}
Err(EuvError::Message(format!(
"The native client project directory '{}' does not exist. Pass {NATIVE_PATH_ARG} to \
point at it.",
native_project.display()
)))
}
pub async fn resolve_web_dist(args: &ModeArgs) -> PathBuf {
resolve_serving_root(args).await
}
pub fn reject_native_flags_in_run(args: &ModeArgs) -> Result<(), EuvError> {
for argument in args.get_wasm_pack_args() {
let name: &str = argument.split('=').next().unwrap_or(argument.as_str());
if name == ALL_ARG || name == PLATFORMS_ARG || name == NATIVE_PATH_ARG || name == PLAN_ARG {
return Err(EuvError::Message(format!(
"{name} selects a native build, which the run mode cannot perform. Use \
`euv build {name}` instead."
)));
}
}
Ok(())
}
pub async fn dispatch_build_mode(args: BuildArgs) -> Result<(), EuvError> {
if args.try_get_native_path().is_some() && !args.get_all() && args.get_platforms().is_empty() {
return Err(EuvError::Message(format!(
"{NATIVE_PATH_ARG} requires {ALL_ARG} or {PLATFORMS_ARG}"
)));
}
if args.get_all() || !args.get_platforms().is_empty() || args.get_plan() {
build_all_mode(args).await
} else {
build_mode(args.get_mode().clone()).await
}
}
pub async fn build_all_mode(args: BuildArgs) -> Result<(), EuvError> {
let mut mode_args: ModeArgs = args.get_mode().clone();
reconcile_args(&mut mode_args);
mode_args.set_crate_path(sync_canonicalize(mode_args.get_crate_path()).map_err(
|error: io::Error| EuvError::IoPath {
message: ERROR_INVALID_CRATE_PATH.to_string(),
path: mode_args.get_crate_path().clone(),
error,
},
)?);
let crate_path_str: String = mode_args.get_crate_path().to_string_lossy().to_string();
if crate_path_str.starts_with(WINDOWS_UNC_PREFIX) {
mode_args.set_crate_path(PathBuf::from(
crate_path_str
.strip_prefix(WINDOWS_UNC_PREFIX)
.unwrap_or(&crate_path_str),
));
}
let requested: Vec<NativePlatform> =
resolve_requested_platforms(args.get_all(), args.get_platforms())?;
let plan: NativePlan = resolve_native_plan(&requested, current_host())?;
print_banner(Action::Build);
print_native_plan(&plan);
if args.get_plan() {
log::info!("Plan requested. Exiting without building.");
return Ok(());
}
if plan.get_targets().is_empty() {
run_build_only_pipeline(&mode_args).await?;
log::info!("Build completed. Exiting (build-only mode).");
return Ok(());
}
let base: PathBuf = current_dir().map_err(|error: io::Error| EuvError::Io {
message: ERROR_CURRENT_DIRECTORY.to_string(),
error,
})?;
let native_path: Option<&PathBuf> = args.try_get_native_path().as_ref();
let native_project: PathBuf = resolve_native_project(&base, native_path);
require_native_project(&native_project)?;
prepare_native_profile(&mut mode_args)?;
run_build_only_pipeline(&mode_args).await?;
let web_dist: PathBuf = resolve_web_dist(&mode_args).await;
log::info!("Web build output: {}", web_dist.display());
run_native_builds(&plan, &native_project, &web_dist, NATIVE_MODE_RELEASE).await?;
log::info!("Build completed. Exiting (build-all mode).");
Ok(())
}
pub(crate) fn prepare_native_profile(args: &mut ModeArgs) -> Result<(), EuvError> {
if args.get_dev() || args.get_profiling() {
return Err(EuvError::Message(
"Native packages support release builds only; omit --dev and --profiling".to_string(),
));
}
args.set_release(true);
Ok(())
}
pub(crate) async fn run_native_builds(
plan: &NativePlan,
native_project: &Path,
web_dist: &Path,
mode: &str,
) -> Result<(), EuvError> {
for target in plan.get_targets() {
let platform: &str = target.platform.platform_name();
log::info!("Building native client: {platform}");
match target.task {
NativeTask::Android => {
run_android_build(native_project, web_dist, mode).await?;
}
NativeTask::Client => {
run_client_build(native_project, platform, web_dist, mode).await?;
}
}
}
Ok(())
}
async fn run_android_build(
native_project: &Path,
web_dist: &Path,
mode: &str,
) -> Result<(), EuvError> {
let script: PathBuf = native_project.join(ANDROID_BUILD_SCRIPT_PATH);
if !script.is_file() {
return Err(EuvError::Message(format!(
"The Android build script '{}' does not exist.",
script.display()
)));
}
let mut command: Command = Command::new(BASH_COMMAND);
command
.arg(&script)
.arg(PLATFORM_ANDROID)
.arg(mode)
.current_dir(native_project)
.env(ENV_WEB_DIST, web_dist)
.env(ENV_NATIVE_MODE, mode)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
run_command(command, "Android build").await
}
async fn run_client_build(
native_project: &Path,
platform: &str,
web_dist: &Path,
mode: &str,
) -> Result<(), EuvError> {
let script: PathBuf = native_project
.join("scripts")
.join(BUILD_CLIENT_SCRIPT_PATH);
if !script.is_file() {
return Err(EuvError::Message(format!(
"The native client builder '{}' does not exist.",
script.display()
)));
}
let mut command: Command = Command::new(NODE_COMMAND);
command
.arg(&script)
.arg(platform)
.current_dir(native_project)
.env(ENV_WEB_DIST, web_dist)
.env(ENV_NATIVE_MODE, mode)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
run_command(command, "native client build").await
}
async fn run_command(mut command: Command, step: &str) -> Result<(), EuvError> {
let status: ExitStatus = command
.status()
.await
.map_err(|error: io::Error| EuvError::Io {
message: format!("Failed to execute the {step}"),
error,
})?;
if status.success() {
return Ok(());
}
Err(EuvError::Message(format!(
"The {step} failed with exit code {}",
status.code().unwrap_or(-1)
)))
}