Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added visionOS support for Apple Vision Pro #340

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changes/visionos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-mobile2": minor
---

Added visionOS support for Apple Vision Pro based on https://github.com/rust-lang/rust/pull/121419
15 changes: 15 additions & 0 deletions src/apple/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use thiserror::Error;
static DEFAULT_PROJECT_DIR: &str = "gen/apple";
const DEFAULT_BUNDLE_VERSION: VersionNumber = VersionNumber::new(VersionTriple::new(1, 0, 0), None);
const DEFAULT_IOS_VERSION: VersionDouble = VersionDouble::new(13, 0);
const DEFAULT_VISIONOS_VERSION: VersionDouble = VersionDouble::new(1, 0);
const DEFAULT_MACOS_VERSION: VersionDouble = VersionDouble::new(11, 0);

#[derive(Debug, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -151,6 +152,8 @@ pub struct Metadata {
#[serde(default)]
pub ios: Platform,
#[serde(default)]
pub visionos: Platform,
#[serde(default)]
pub macos: Platform,
}

Expand All @@ -159,6 +162,7 @@ impl Default for Metadata {
Self {
supported: true,
ios: Default::default(),
visionos: Default::default(),
macos: Default::default(),
}
}
Expand All @@ -173,6 +177,10 @@ impl Metadata {
&self.ios
}

pub fn visionos(&self) -> &Platform {
&self.visionos
}

pub fn macos(&self) -> &Platform {
&self.macos
}
Expand Down Expand Up @@ -286,6 +294,7 @@ pub struct Config {
bundle_version: VersionNumber,
bundle_version_short: VersionTriple,
ios_version: VersionDouble,
visionos_version: VersionDouble,
macos_version: VersionDouble,
use_legacy_build_system: bool,
plist_pairs: Vec<PListPair>,
Expand Down Expand Up @@ -353,6 +362,12 @@ impl Config {
.transpose()
.map_err(Error::IosVersionInvalid)?
.unwrap_or(DEFAULT_IOS_VERSION),
visionos_version: raw
.visionos_version
.map(|str| VersionDouble::from_str(&str))
.transpose()
.map_err(Error::IosVersionInvalid)?
.unwrap_or(DEFAULT_VISIONOS_VERSION),
macos_version: raw
.macos_version
.map(|str| VersionDouble::from_str(&str))
Expand Down
3 changes: 3 additions & 0 deletions src/apple/config/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub struct Raw {
pub bundle_version: Option<String>,
pub bundle_version_short: Option<String>,
pub ios_version: Option<String>,
pub visionos_version: Option<String>,
pub macos_version: Option<String>,
pub use_legacy_build_system: Option<bool>,
pub plist_pairs: Option<Vec<PListPair>>,
Expand All @@ -145,6 +146,7 @@ impl Raw {
bundle_version: None,
bundle_version_short: None,
ios_version: None,
visionos_version: None,
macos_version: None,
use_legacy_build_system: None,
plist_pairs: None,
Expand Down Expand Up @@ -227,6 +229,7 @@ impl Raw {
bundle_version: None,
bundle_version_short: None,
ios_version: None,
visionos_version: None,
macos_version: None,
use_legacy_build_system: None,
plist_pairs: None,
Expand Down
5 changes: 4 additions & 1 deletion src/apple/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Reportable for RunError {
pub enum DeviceKind {
Simulator,
IosDeployDevice,
VisionOsDeployDevice,
DeviceCtlDevice,
}

Expand Down Expand Up @@ -133,7 +134,9 @@ impl<'a> Device<'a> {
match self.kind {
DeviceKind::Simulator => simctl::run(config, env, non_interactive, &self.id)
.map_err(|e| RunError::DeployFailed(e.to_string())),
DeviceKind::IosDeployDevice | DeviceKind::DeviceCtlDevice => {
DeviceKind::IosDeployDevice
| DeviceKind::VisionOsDeployDevice
| DeviceKind::DeviceCtlDevice => {
println!("Exporting app...");
self.target
.export(config, env, noise_level)
Expand Down
20 changes: 14 additions & 6 deletions src/apple/device/simctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ impl Display for Device {

impl<'a> From<Device> for AppleDevice<'a> {
fn from(device: Device) -> AppleDevice<'a> {
let name = device.name.clone();
let is_visionos = name.contains("Apple Vision Pro");
let target = Target::for_triple(if cfg!(target_arch = "aarch64") {
match is_visionos {
true => "aarch64-apple-visionos",
_ => "aarch64-apple-ios", // TODO: figure out how to check for sim here, or probably do this elsewhere, and just add -sim to the triple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the simctl module, if we're using this then it's always a simulator

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, need to update this, we've done more stuff in our internal pipeline, and it's time to update it for 2.0.0 🥳

// true => "aarch64-apple-visionos-sim",
// _ => "aarch64-apple-ios-sim
}
} else {
"x86_64-apple-ios"
});

AppleDevice::new(
device.udid,
device.name,
"".into(),
Target::for_arch(if cfg!(target_arch = "aarch64") {
"arm64-sim"
} else {
"x86_64"
})
.unwrap(),
target.unwrap(),
DeviceKind::Simulator,
)
}
Expand Down
75 changes: 65 additions & 10 deletions src/apple/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,24 @@ pub struct Target<'a> {
}

impl<'a> TargetTrait<'a> for Target<'a> {
const DEFAULT_KEY: &'static str = "aarch64";
const DEFAULT_KEY: &'static str = "aarch64-apple-ios";

fn all() -> &'a BTreeMap<&'a str, Self> {
static TARGETS: OnceCell<BTreeMap<&'static str, Target<'static>>> = OnceCell::new();
TARGETS.get_or_init(|| {
let mut targets = BTreeMap::new();
targets.insert(
"aarch64",
"aarch64-apple-ios",
Target {
triple: "aarch64-apple-ios",
arch: "arm64",
sdk: "iphoneos",
alias: Some("arm64e"),
alias: Some("arm64e-ios"),
min_xcode_version: None,
},
);
targets.insert(
"x86_64",
"x86_64-apple-ios",
Target {
triple: "x86_64-apple-ios",
arch: "x86_64",
Expand All @@ -175,12 +175,32 @@ impl<'a> TargetTrait<'a> for Target<'a> {
},
);
targets.insert(
"aarch64-sim",
"aarch64-apple-ios-sim",
Target {
triple: "aarch64-apple-ios-sim",
arch: "arm64-sim",
sdk: "iphonesimulator",
alias: Some("arm64e-sim"),
alias: Some("arm64e-ios-sim"),
min_xcode_version: None,
},
);
targets.insert(
"aarch64-apple-visionos",
Target {
triple: "aarch64-apple-visionos",
arch: "arm64",
sdk: "xros",
alias: Some("arm64e-xros"),
min_xcode_version: None,
},
);
targets.insert(
"aarch64-apple-visionos-sim",
Target {
triple: "aarch64-apple-visionos-sim",
arch: "arm64-sim",
sdk: "xrsimulator",
alias: Some("arm64e-xros-sim"),
min_xcode_version: None,
},
);
Expand All @@ -201,6 +221,13 @@ impl<'a> TargetTrait<'a> for Target<'a> {
}
}

pub enum TargetPlatform {
MacOS,
#[allow(non_camel_case_types)]
iOS,
VisionOS,
}

impl<'a> Target<'a> {
// TODO: Make this cleaner
pub fn macos() -> Self {
Expand All @@ -213,16 +240,44 @@ impl<'a> Target<'a> {
}
}

pub fn get_platform(&self) -> TargetPlatform {
let platform = if self.is_macos() {
TargetPlatform::MacOS
} else if self.is_ios() {
TargetPlatform::iOS
} else if self.is_visionos() {
TargetPlatform::VisionOS
} else {
TargetPlatform::MacOS
};

platform
}

pub fn is_macos(&self) -> bool {
*self == Self::macos()
}

pub fn is_ios(&self) -> bool {
self.triple.contains("apple-ios")
}

pub fn is_visionos(&self) -> bool {
self.triple.contains("apple-visionos")
}

pub fn for_arch(arch: &str) -> Option<&'a Self> {
Self::all()
.values()
.find(|target| target.arch == arch || target.alias == Some(arch))
}

pub fn for_triple(triple: &str) -> Option<&'a Self> {
Self::all()
.values()
.find(|target| target.triple == triple || target.alias == Some(triple))
}

fn min_xcode_version_satisfied(&self) -> Result<(), VersionCheckError> {
self.min_xcode_version
.map(|(min_version, msg)| {
Expand All @@ -247,10 +302,10 @@ impl<'a> Target<'a> {
metadata: &'a Metadata,
subcommand: &'a str,
) -> Result<CargoCommand<'a>, VersionCheckError> {
let metadata = if self.is_macos() {
metadata.macos()
} else {
metadata.ios()
let metadata = match self.get_platform() {
TargetPlatform::MacOS => metadata.macos(),
TargetPlatform::iOS => metadata.ios(),
TargetPlatform::VisionOS => metadata.visionos(),
};
self.min_xcode_version_satisfied().map(|()| {
CargoCommand::new(subcommand)
Expand Down