use super::*;
#[must_use]
pub fn render_ios_info_plist(version: &str, build_number: &str, bundle_id: &str) -> String {
format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n\
<dict>\n\
<key>CFBundleDevelopmentRegion</key><string>en</string>\n\
<key>CFBundleDisplayName</key><string>Euv Studio</string>\n\
<key>CFBundleExecutable</key><string>EuvShowcase</string>\n\
<key>CFBundleIconFiles</key>\n\
<array>\n\
<string>AppIcon60x60@2x.png</string>\n\
<string>AppIcon60x60@3x.png</string>\n\
<string>AppIcon.png</string>\n\
</array>\n\
<key>CFBundleIcons</key>\n\
<dict>\n\
<key>CFBundlePrimaryIcon</key>\n\
<dict>\n\
<key>CFBundleIconFiles</key>\n\
<array>\n\
<string>AppIcon60x60</string>\n\
</array>\n\
</dict>\n\
</dict>\n\
<key>CFBundleIdentifier</key><string>{bundle_id}</string>\n\
<key>CFBundleInfoDictionaryVersion</key><string>6.0</string>\n\
<key>CFBundleName</key><string>Euv Studio</string>\n\
<key>CFBundlePackageType</key><string>APPL</string>\n\
<key>CFBundleShortVersionString</key><string>{version}</string>\n\
<key>CFBundleVersion</key><string>{build_number}</string>\n\
<key>CFBundleURLTypes</key>\n\
<array>\n\
<dict>\n\
<key>CFBundleURLName</key><string>{bundle_id}</string>\n\
<key>CFBundleURLSchemes</key>\n\
<array>\n\
<string>euv</string>\n\
</array>\n\
</dict>\n\
</array>\n\
<key>CADisableMinimumFrameDurationOnPhone</key><true/>\n\
<key>LSRequiresIPhoneOS</key><true/>\n\
<key>NSAppTransportSecurity</key>\n\
<dict>\n\
<key>NSAllowsArbitraryLoads</key><true/>\n\
<key>NSAllowsLocalNetworking</key><true/>\n\
</dict>\n\
<key>UIApplicationSceneManifest</key>\n\
<dict>\n\
<key>UIApplicationSupportsMultipleScenes</key><true/>\n\
<key>UISceneConfigurations</key>\n\
<dict>\n\
<key>UIWindowSceneSessionRoleApplication</key>\n\
<array>\n\
<dict>\n\
<key>UISceneConfigurationName</key><string>Default Configuration</string>\n\
<key>UISceneDelegateClassName</key><string>EuvSceneDelegate</string>\n\
</dict>\n\
</array>\n\
</dict>\n\
</dict>\n\
<key>UIApplicationSupportsIndirectInputEvents</key><true/>\n\
<key>NSMotionUsageDescription</key><string>Motion sensors drive physically accurate liquid water gravity and sloshing simulation.</string>\n\
<key>UIDeviceFamily</key>\n\
<array>\n\
<integer>1</integer>\n\
<integer>2</integer>\n\
</array>\n\
<key>UIRequiresFullScreen</key><false/>\n\
<key>UILaunchScreen</key><dict/>\n\
<key>UISupportedInterfaceOrientations</key>\n\
<array>\n\
<string>UIInterfaceOrientationPortrait</string>\n\
<string>UIInterfaceOrientationLandscapeLeft</string>\n\
<string>UIInterfaceOrientationLandscapeRight</string>\n\
</array>\n\
<key>UISupportedInterfaceOrientations~ipad</key>\n\
<array>\n\
<string>UIInterfaceOrientationPortrait</string>\n\
<string>UIInterfaceOrientationPortraitUpsideDown</string>\n\
<string>UIInterfaceOrientationLandscapeLeft</string>\n\
<string>UIInterfaceOrientationLandscapeRight</string>\n\
</array>\n\
</dict>\n\
</plist>\n"
)
}
#[must_use]
pub fn render_watchos_info_plist(version: &str, build_number: &str, bundle_id: &str) -> String {
format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n\
<dict>\n\
<key>CFBundleExecutable</key>\n\
<string>EuvWatch</string>\n\
<key>CFBundleIdentifier</key>\n\
<string>{bundle_id}</string>\n\
<key>CFBundleDisplayName</key>\n\
<string>Euv Watch</string>\n\
<key>CFBundleShortVersionString</key>\n\
<string>{version}</string>\n\
<key>CFBundleVersion</key>\n\
<string>{build_number}</string>\n\
<key>WKApplication</key>\n\
<true/>\n\
<key>WKRunsIndependentlyOfCompanionApp</key>\n\
<true/>\n\
</dict>\n\
</plist>\n"
)
}
pub fn generate_apple_host_project(
config: &AppleBuildConfig,
icon_writer: Option<&MobileIconWriter<'_>>,
) -> io::Result<AppleBuildReport> {
let workspace_root: PathBuf = resolve_absolute_path(config.get_workspace_root());
let target_kind: AppleTargetKind = config.get_target_kind();
let version: String = config
.try_get_app_version()
.clone()
.unwrap_or_else(|| resolve_workspace_semver(&workspace_root));
let build_num: u32 = config
.try_get_build_number()
.unwrap_or_else(|| compute_semver_version_code(&version));
let build_number: String = build_num.to_string();
let bundle_id: String = config
.try_get_bundle_id()
.clone()
.unwrap_or_else(|| target_kind.default_bundle_id().to_string());
let build_dir: PathBuf = config.try_get_build_dir().clone().unwrap_or_else(|| {
workspace_root
.join(MOBILE_HOSTS_SUBDIR)
.join(target_kind.host_dir_name())
});
let out_dir: PathBuf = config.try_get_out_dir().clone().unwrap_or_else(|| {
workspace_root
.join(MOBILE_DIST_DIR_NAME)
.join(target_kind.default_out_subdir())
});
let app_bundle: PathBuf = out_dir.join(target_kind.default_bundle_name());
fs::create_dir_all(build_dir.join(MOBILE_DIR_SRC))?;
fs::create_dir_all(&out_dir)?;
let report: AppleBuildReport = AppleBuildReport {
target_kind: target_kind.as_label().to_string(),
rust_triple: target_kind.rust_triple().to_string(),
version,
build_number,
build_dir,
app_bundle_path: app_bundle,
archive_path: config.try_get_archive_path().clone(),
binary_bytes: 0,
ai_schema_clean: true,
launched: false,
};
if target_kind.is_watchos() {
generate_watchos_host_files(&workspace_root, &out_dir, &bundle_id, &report, icon_writer)?;
} else {
generate_ios_host_files(
&workspace_root,
config.get_render_mode(),
&bundle_id,
&report,
icon_writer,
)?;
}
Ok(report)
}
pub fn build_apple_bundle(
config: &AppleBuildConfig,
icon_writer: Option<&MobileIconWriter<'_>>,
) -> io::Result<AppleBuildReport> {
let generated: AppleBuildReport = generate_apple_host_project(config, icon_writer)?;
if config.get_shell_only() {
return Ok(generated);
}
if consts::OS != MOBILE_HOST_OS_MACOS {
return Err(Error::new(ErrorKind::Unsupported, ERR_APPLE_REQUIRES_MACOS));
}
let target_kind: AppleTargetKind = config.get_target_kind();
let sdk_path: String = resolve_xcode_sdk_path(target_kind.xcode_sdk())?;
let build_dir: &Path = generated.get_build_dir();
let app_bundle: &Path = generated.get_app_bundle_path();
let exec_path: PathBuf = app_bundle.join(target_kind.default_executable_name());
if target_kind.is_watchos() {
compile_and_link_watchos(config, &sdk_path, build_dir, app_bundle, &exec_path)?;
} else {
compile_and_link_ios(config, &sdk_path, build_dir, app_bundle, &exec_path)?;
}
if config.get_release() {
assert_release_binary_ai_schema_clean(&exec_path)?;
}
let binary_bytes: u64 = fs::metadata(&exec_path)?.len();
let archive_path: Option<PathBuf> = config.try_get_archive_path().clone();
if let Some(archive_dst) = archive_path.as_deref() {
archive_apple_bundle_tar_gz(app_bundle, archive_dst)?;
}
let skip_env: bool = env::var(ENV_EUV_IOS_SKIP_LAUNCH)
.map(|val: String| val.trim() == "1")
.unwrap_or(false);
let launched: bool = if !config.get_no_launch()
&& !skip_env
&& target_kind.is_simulator()
&& !target_kind.is_watchos()
{
let bundle_id: String = config
.try_get_bundle_id()
.clone()
.unwrap_or_else(|| target_kind.default_bundle_id().to_string());
launch_ios_simulator_bundle(config, app_bundle, &bundle_id)?;
true
} else {
false
};
Ok(AppleBuildReport {
target_kind: generated.get_target_kind().clone(),
rust_triple: generated.get_rust_triple().clone(),
version: generated.get_version().clone(),
build_number: generated.get_build_number().clone(),
build_dir: generated.get_build_dir().clone(),
app_bundle_path: app_bundle.to_path_buf(),
archive_path,
binary_bytes,
ai_schema_clean: true,
launched,
})
}
fn generate_ios_host_files(
workspace_root: &Path,
render_mode: RenderArg,
bundle_id: &str,
report: &AppleBuildReport,
icon_writer: Option<&MobileIconWriter<'_>>,
) -> io::Result<()> {
let build_dir: &Path = report.get_build_dir();
let app_bundle: &Path = report.get_app_bundle_path();
let version: &str = report.get_version();
let build_number: &str = report.get_build_number();
let render_expr: &str = render_mode_const_expr(render_mode);
let host_config: String = format!(
"use euv::RenderMode;\n\n\
pub const HOST_RENDER_MODE: RenderMode = {render_expr};\n"
);
let root_escaped: String = escape_toml_path(workspace_root);
let example_escaped: String = escape_toml_path(&workspace_root.join(MOBILE_DIR_EXAMPLE));
let cargo_toml: String = format!(
"[workspace]\n\n\
[package]\n\
name = \"euv-ios-host\"\n\
version = \"{version}\"\n\
edition = \"2024\"\n\n\
[lib]\n\
crate-type = [\"staticlib\"]\n\n\
[dependencies]\n\
euv = {{ path = \"{root_escaped}\" }}\n\
euv-example = {{ path = \"{example_escaped}\" }}\n\n\
[profile.release]\n\
opt-level = 3\n\
lto = \"thin\"\n\
codegen-units = 1\n\
panic = \"abort\"\n"
);
let info_plist: String = render_ios_info_plist(version, build_number, bundle_id);
fs::write(
build_dir
.join(MOBILE_DIR_SRC)
.join(MOBILE_FILE_HOST_CONFIG_RS),
host_config,
)?;
fs::write(build_dir.join(MOBILE_FILE_CARGO_TOML), cargo_toml)?;
fs::write(
build_dir.join(MOBILE_DIR_SRC).join(MOBILE_FILE_LIB_RS),
APPLE_IOS_HOST_LIB_RS_TEMPLATE,
)?;
fs::write(
build_dir.join(APPLE_IOS_MAIN_M_FILE),
APPLE_IOS_MAIN_M_TEMPLATE,
)?;
fs::write(build_dir.join(APPLE_INFO_PLIST_FILE), &info_plist)?;
if app_bundle.exists() {
fs::remove_dir_all(app_bundle)?;
}
fs::create_dir_all(app_bundle)?;
fs::write(app_bundle.join(APPLE_INFO_PLIST_FILE), info_plist)?;
for (icon_name, size_px) in APPLE_IOS_ICON_FILES.iter().copied() {
let icon_path: PathBuf = app_bundle.join(icon_name);
write_icon_or_fallback(icon_writer, &icon_path, size_px)?;
}
Ok(())
}
fn generate_watchos_host_files(
workspace_root: &Path,
out_dir: &Path,
bundle_id: &str,
report: &AppleBuildReport,
icon_writer: Option<&MobileIconWriter<'_>>,
) -> io::Result<()> {
let build_dir: &Path = report.get_build_dir();
let version: &str = report.get_version();
let build_number: &str = report.get_build_number();
let app_src_dir: PathBuf = out_dir.join(APPLE_WATCHOS_APP_SRC_DIR);
let iconset_dir: PathBuf = out_dir.join(APPLE_WATCHOS_ICONSET_REL);
fs::create_dir_all(&app_src_dir)?;
fs::create_dir_all(&iconset_dir)?;
fs::create_dir_all(out_dir.join(APPLE_LIBS_DIR_NAME))?;
let root_escaped: String = escape_toml_path(workspace_root);
let example_escaped: String = escape_toml_path(&workspace_root.join(MOBILE_DIR_EXAMPLE));
let cargo_toml: String = format!(
"[package]\n\
name = \"euv-watchos-host\"\n\
version = \"{version}\"\n\
edition = \"2024\"\n\n\
[workspace]\n\n\
[lib]\n\
crate-type = [\"staticlib\"]\n\n\
[dependencies]\n\
euv = {{ path = \"{root_escaped}\" }}\n\
euv-example = {{ path = \"{example_escaped}\" }}\n\n\
[profile.release]\n\
opt-level = \"s\"\n\
lto = \"thin\"\n\
codegen-units = 1\n\
panic = \"abort\"\n"
);
let info_plist: String = render_watchos_info_plist(version, build_number, bundle_id);
fs::write(build_dir.join(MOBILE_FILE_CARGO_TOML), cargo_toml)?;
fs::write(
build_dir.join(MOBILE_DIR_SRC).join(MOBILE_FILE_LIB_RS),
APPLE_WATCHOS_HOST_LIB_RS_TEMPLATE,
)?;
fs::write(
app_src_dir.join(APPLE_WATCHOS_BRIDGE_H_FILE),
APPLE_WATCHOS_BRIDGE_H_TEMPLATE,
)?;
fs::write(
app_src_dir.join(APPLE_WATCHOS_SWIFT_FILE),
APPLE_WATCHOS_APP_SWIFT_TEMPLATE,
)?;
fs::write(app_src_dir.join(APPLE_INFO_PLIST_FILE), info_plist)?;
fs::write(
iconset_dir.join(APPLE_CONTENTS_JSON_FILE),
APPLE_WATCHOS_ICONSET_JSON,
)?;
write_icon_or_fallback(
icon_writer,
&iconset_dir.join(APPLE_APP_ICON_PNG_FILE),
APPLE_WATCHOS_ICON_SIZE,
)
}
fn compile_and_link_ios(
config: &AppleBuildConfig,
sdk_path: &str,
build_dir: &Path,
app_bundle: &Path,
exec_path: &Path,
) -> io::Result<()> {
let target_kind: AppleTargetKind = config.get_target_kind();
let triple: &str = target_kind.rust_triple();
let mut cargo_args: Vec<&OsStr> = vec![OsStr::new(MOBILE_ARG_BUILD)];
if config.get_release() {
cargo_args.push(OsStr::new(MOBILE_ARG_RELEASE));
}
cargo_args.push(OsStr::new(MOBILE_ARG_TARGET));
cargo_args.push(OsStr::new(triple));
run_checked_command(
Path::new(MOBILE_CMD_CARGO),
&cargo_args,
Some(build_dir),
&[],
)?;
let profile_dir: &str = if config.get_release() {
MOBILE_DIR_RELEASE
} else {
MOBILE_DIR_DEBUG
};
let static_lib: PathBuf = build_dir
.join(MOBILE_DIR_TARGET)
.join(triple)
.join(profile_dir)
.join(APPLE_IOS_STATICLIB_NAME);
let clang_out: Output = run_checked_command(
Path::new(CMD_XCRUN),
&[
OsStr::new(ARG_XCRUN_SDK),
OsStr::new(target_kind.xcode_sdk()),
OsStr::new(ARG_XCRUN_FIND),
OsStr::new(TOOL_CLANG),
],
None,
&[],
)?;
let clang_bin: PathBuf = PathBuf::from(String::from_utf8_lossy(&clang_out.stdout).trim());
let min_ver_flag: &str = if target_kind.is_simulator() {
ARG_MIN_IOS_SIM
} else {
ARG_MIN_IOS_DEVICE
};
let main_m: PathBuf = build_dir.join(APPLE_IOS_MAIN_M_FILE);
let mut link_args: Vec<&OsStr> = vec![
OsStr::new(ARG_ARCH),
OsStr::new(ARG_ARM64),
OsStr::new(ARG_ISYSROOT),
OsStr::new(sdk_path),
OsStr::new(min_ver_flag),
OsStr::new(ARG_FOBJC_ARC),
main_m.as_os_str(),
static_lib.as_os_str(),
];
for framework in APPLE_IOS_FRAMEWORKS.iter().copied() {
link_args.push(OsStr::new(ARG_FRAMEWORK));
link_args.push(OsStr::new(framework));
}
link_args.push(OsStr::new("-o"));
link_args.push(exec_path.as_os_str());
run_checked_command(&clang_bin, &link_args, None, &[])?;
run_checked_command(
Path::new(CMD_STRIP),
&[OsStr::new("-x"), exec_path.as_os_str()],
None,
&[],
)?;
let identity: &str = config.try_get_codesign_identity().as_deref().unwrap_or("-");
run_checked_command(
Path::new(CMD_CODESIGN),
&[
OsStr::new(ARG_CODESIGN_FORCE),
OsStr::new(ARG_CODESIGN_SIGN),
OsStr::new(identity),
app_bundle.as_os_str(),
],
None,
&[],
)?;
Ok(())
}
fn compile_and_link_watchos(
config: &AppleBuildConfig,
sdk_path: &str,
build_dir: &Path,
app_bundle: &Path,
exec_path: &Path,
) -> io::Result<()> {
let target_kind: AppleTargetKind = config.get_target_kind();
let triple: &str = target_kind.rust_triple();
let mut cargo_args: Vec<&OsStr> = vec![OsStr::new(MOBILE_ARG_BUILD)];
if config.get_release() {
cargo_args.push(OsStr::new(MOBILE_ARG_RELEASE));
}
cargo_args.push(OsStr::new(MOBILE_ARG_TARGET));
cargo_args.push(OsStr::new(triple));
let envs: [(&str, &OsStr); 1] = [(ENV_SDKROOT, OsStr::new(sdk_path))];
run_checked_command(
Path::new(MOBILE_CMD_CARGO),
&cargo_args,
Some(build_dir),
&envs,
)?;
let profile_dir: &str = if config.get_release() {
MOBILE_DIR_RELEASE
} else {
MOBILE_DIR_DEBUG
};
let static_lib: PathBuf = build_dir
.join(MOBILE_DIR_TARGET)
.join(triple)
.join(profile_dir)
.join(APPLE_WATCHOS_STATICLIB_NAME);
let out_dir: PathBuf = app_bundle
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| PathBuf::from("."));
let lib_dest_dir: PathBuf = out_dir.join(APPLE_LIBS_DIR_NAME).join(triple);
fs::create_dir_all(&lib_dest_dir)?;
fs::create_dir_all(app_bundle)?;
let _: u64 = fs::copy(&static_lib, lib_dest_dir.join(APPLE_WATCHOS_STATICLIB_NAME))?;
let app_src_dir: PathBuf = out_dir.join(APPLE_WATCHOS_APP_SRC_DIR);
let _: u64 = fs::copy(
app_src_dir.join(APPLE_INFO_PLIST_FILE),
app_bundle.join(APPLE_INFO_PLIST_FILE),
)?;
let swift_target: &str = if target_kind.is_simulator() {
ARG_WATCHOS_SIM_TRIPLE
} else {
ARG_WATCHOS_DEVICE_TRIPLE
};
let bridge_h: PathBuf = app_src_dir.join(APPLE_WATCHOS_BRIDGE_H_FILE);
let swift_src: PathBuf = app_src_dir.join(APPLE_WATCHOS_SWIFT_FILE);
let mut swift_args: Vec<&OsStr> = vec![
OsStr::new(ARG_XCRUN_SDK),
OsStr::new(target_kind.xcode_sdk()),
OsStr::new(TOOL_SWIFTC),
OsStr::new(ARG_PARSE_AS_LIBRARY),
OsStr::new(ARG_SWIFTC_TARGET),
OsStr::new(swift_target),
OsStr::new(ARG_SWIFTC_SDK),
OsStr::new(sdk_path),
OsStr::new("-O"),
OsStr::new(ARG_IMPORT_OBJC_HEADER),
bridge_h.as_os_str(),
swift_src.as_os_str(),
static_lib.as_os_str(),
];
for framework in APPLE_WATCHOS_FRAMEWORKS.iter().copied() {
swift_args.push(OsStr::new(ARG_FRAMEWORK));
swift_args.push(OsStr::new(framework));
}
swift_args.push(OsStr::new("-o"));
swift_args.push(exec_path.as_os_str());
run_checked_command(Path::new(CMD_XCRUN), &swift_args, None, &[])?;
let identity: &str = config.try_get_codesign_identity().as_deref().unwrap_or("-");
run_checked_command(
Path::new(CMD_CODESIGN),
&[
OsStr::new(ARG_CODESIGN_FORCE),
OsStr::new(ARG_CODESIGN_SIGN),
OsStr::new(identity),
app_bundle.as_os_str(),
],
None,
&[],
)?;
Ok(())
}
fn resolve_xcode_sdk_path(sdk_name: &str) -> io::Result<String> {
let output: Output = run_checked_command(
Path::new(CMD_XCRUN),
&[
OsStr::new(ARG_XCRUN_SDK),
OsStr::new(sdk_name),
OsStr::new(ARG_XCRUN_SHOW_SDK_PATH),
],
None,
&[],
)?;
let path_str: String = String::from_utf8_lossy(&output.stdout).trim().to_string();
if path_str.is_empty() {
return Err(Error::new(
ErrorKind::NotFound,
format!("xcrun returned empty SDK path for '{sdk_name}'"),
));
}
Ok(path_str)
}
fn archive_apple_bundle_tar_gz(app_bundle: &Path, archive_dst: &Path) -> io::Result<()> {
if let Some(archive_parent) = archive_dst.parent() {
fs::create_dir_all(archive_parent)?;
}
let bundle_parent: &Path = app_bundle.parent().unwrap_or_else(|| Path::new("."));
let bundle_name: &OsStr = app_bundle
.file_name()
.unwrap_or_else(|| OsStr::new(APPLE_IOS_BUNDLE_NAME));
run_checked_command(
Path::new(APPLE_CMD_TAR),
&[
OsStr::new(ARG_TAR_CZF),
archive_dst.as_os_str(),
OsStr::new("-C"),
bundle_parent.as_os_str(),
bundle_name,
],
None,
&[],
)?;
Ok(())
}
fn launch_ios_simulator_bundle(
config: &AppleBuildConfig,
app_bundle: &Path,
bundle_id: &str,
) -> io::Result<()> {
let sim_udid: String = resolve_ios_simulator_udid(config)?;
let _: io::Result<Output> = Command::new(CMD_XCRUN)
.args([TOOL_SIMCTL, ARG_SIMCTL_BOOT, &sim_udid])
.output();
let _: io::Result<Output> = Command::new(CMD_XCRUN)
.args([TOOL_SIMCTL, ARG_SIMCTL_TERMINATE, &sim_udid, bundle_id])
.output();
run_checked_command(
Path::new(CMD_XCRUN),
&[
OsStr::new(TOOL_SIMCTL),
OsStr::new(ARG_SIMCTL_INSTALL),
OsStr::new(&sim_udid),
app_bundle.as_os_str(),
],
None,
&[],
)?;
let mut forwarded_owned: Vec<(String, OsString)> = Vec::new();
for env_key in [
ENV_EUV_UPDATE_URL,
ENV_EUV_PROXY_URL,
ENV_EUV_UPDATE_CHANNEL,
] {
if let Ok(env_val) = env::var(env_key)
&& !env_val.trim().is_empty()
{
forwarded_owned.push((
format!("{ENV_SIMCTL_CHILD_PREFIX}{env_key}"),
OsString::from(env_val),
));
}
}
let forwarded_refs: Vec<(&str, &OsStr)> = forwarded_owned
.iter()
.map(|(env_k, env_v): &(String, OsString)| (env_k.as_str(), env_v.as_os_str()))
.collect();
run_checked_command(
Path::new(CMD_XCRUN),
&[
OsStr::new(TOOL_SIMCTL),
OsStr::new(ARG_SIMCTL_LAUNCH),
OsStr::new(&sim_udid),
OsStr::new(bundle_id),
],
None,
&forwarded_refs,
)?;
Ok(())
}
fn resolve_ios_simulator_udid(config: &AppleBuildConfig) -> io::Result<String> {
if let Some(custom) = config.try_get_sim_udid() {
return Ok(custom.clone());
}
if let Ok(env_udid) = env::var(ENV_EUV_IOS_SIM_UDID) {
let trimmed: &str = env_udid.trim();
if !trimmed.is_empty() {
return Ok(trimmed.to_string());
}
}
let booted_out: Output = Command::new(CMD_XCRUN)
.args([
TOOL_SIMCTL,
ARG_SIMCTL_LIST,
ARG_SIMCTL_DEVICES,
ARG_SIMCTL_BOOTED,
])
.output()?;
let booted_text: Cow<'_, str> = String::from_utf8_lossy(&booted_out.stdout);
if let Some(udid) = extract_first_sim_uuid(&booted_text, None) {
return Ok(udid);
}
let avail_out: Output = Command::new(CMD_XCRUN)
.args([
TOOL_SIMCTL,
ARG_SIMCTL_LIST,
ARG_SIMCTL_DEVICES,
ARG_SIMCTL_AVAILABLE,
])
.output()?;
let avail_text: Cow<'_, str> = String::from_utf8_lossy(&avail_out.stdout);
let device_filter: &str = if config.get_target_kind().is_ipad() {
SIM_DEVICE_IPAD
} else {
SIM_DEVICE_IPHONE
};
if let Some(udid) = extract_first_sim_uuid(&avail_text, Some(device_filter)) {
return Ok(udid);
}
Err(Error::new(ErrorKind::NotFound, ERR_APPLE_NO_SIMULATOR))
}
fn extract_first_sim_uuid(simctl_output: &str, line_filter: Option<&str>) -> Option<String> {
for raw_line in simctl_output.lines() {
if let Some(filter) = line_filter
&& !raw_line.contains(filter)
{
continue;
}
let mut search_from: usize = 0;
while let Some(open_rel) = raw_line[search_from..].find('(') {
let start_idx: usize = search_from + open_rel + 1;
let Some(close_rel) = raw_line[start_idx..].find(')') else {
break;
};
let candidate: &str = &raw_line[start_idx..start_idx + close_rel];
if is_sim_uuid(candidate) {
return Some(candidate.to_string());
}
search_from = start_idx + close_rel + 1;
}
}
None
}
fn is_sim_uuid(candidate: &str) -> bool {
if candidate.len() != APPLE_UUID_LEN {
return false;
}
for (idx, byte_val) in candidate.bytes().enumerate() {
let expect_dash: bool = idx == 8 || idx == 13 || idx == 18 || idx == 23;
if expect_dash {
if byte_val != b'-' {
return false;
}
} else if !byte_val.is_ascii_hexdigit() {
return false;
}
}
true
}