Skip to content

Commit

Permalink
Add Apple visionOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinPerez committed Apr 19, 2024
1 parent 22c016b commit 761aecc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
51 changes: 46 additions & 5 deletions crate_universe/src/context/platforms.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};

use anyhow::{anyhow, Context, Result};
use cfg_expr::targets::{get_builtin_target_by_triple, TargetInfo};
use cfg_expr::targets::Abi;
use cfg_expr::targets::{
get_builtin_target_by_triple, Arch, Endian, Families, HasAtomics, Os, Panic, TargetInfo,
Triple, Vendor,
};
use cfg_expr::{Expression, Predicate};

// since we use binary_search_by on it, the array must be sorted.
// NOTE: can be removed once, the visionos target is added to cfg_expr
pub const TARGET_INFO: &[TargetInfo] = &[
TargetInfo {
triple: Triple::new_const("aarch64-apple-visionos"),
os: Some(Os(Cow::Borrowed("visionos"))),
abi: None,
arch: Arch::aarch64,
env: None,
vendor: Some(Vendor::apple),
families: Families::unix,
pointer_width: 64,
endian: Endian::little,
has_atomics: HasAtomics::atomic_8_16_32_64_128_ptr,
panic: Panic::unwind,
},
TargetInfo {
triple: Triple::new_const("aarch64-apple-visionos-sim"),
os: Some(Os(Cow::Borrowed("visionos"))),
abi: Some(Abi::new_const("sim")),
arch: Arch::aarch64,
env: None,
vendor: Some(Vendor::apple),
families: Families::unix,
pointer_width: 64,
endian: Endian::little,
has_atomics: HasAtomics::atomic_8_16_32_64_128_ptr,
panic: Panic::unwind,
},
];

use crate::context::CrateContext;
use crate::utils::target_triple::TargetTriple;

Expand Down Expand Up @@ -39,15 +75,20 @@ pub(crate) fn resolve_cfg_platforms(
// Generate target information for each triple string
let target_infos = supported_platform_triples
.iter()
.map(
|target_triple| match get_builtin_target_by_triple(&target_triple.to_cargo()) {
.map(|target_triple| {
match get_builtin_target_by_triple(&target_triple.to_cargo()).or_else(|| {
TARGET_INFO
.binary_search_by(|ti| ti.triple.as_ref().cmp(&target_triple.to_cargo()))
.map(|i| &TARGET_INFO[i])
.ok()
}) {
Some(info) => Ok((target_triple, info)),
None => Err(anyhow!(
"Invalid platform triple in supported platforms: {}",
target_triple
)),
},
)
}
})
.collect::<Result<BTreeMap<&TargetTriple, &'static TargetInfo>>>()?;

// `cfg-expr` does not understand configurations that are simply platform triples
Expand Down
1 change: 1 addition & 0 deletions rust/platform/platform.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _SUPPORTED_SYSTEMS = [
"darwin",
"freebsd",
"ios",
"visionos",
"linux",
"windows",
"nto",
Expand Down
11 changes: 9 additions & 2 deletions rust/platform/triple_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ SUPPORTED_T2_PLATFORM_TRIPLES = [

SUPPORTED_T3_PLATFORM_TRIPLES = [
"aarch64-unknown-nto-qnx710",
"aarch64-apple-visionos-sim",
"aarch64-apple-visionos",
]

SUPPORTED_PLATFORM_TRIPLES = SUPPORTED_T1_PLATFORM_TRIPLES + SUPPORTED_T2_PLATFORM_TRIPLES + SUPPORTED_T3_PLATFORM_TRIPLES
Expand Down Expand Up @@ -97,6 +99,7 @@ _SYSTEM_TO_BUILTIN_SYS_SUFFIX = {
"freebsd": "freebsd",
"fuchsia": "fuchsia",
"ios": "ios",
"visionos": "visionos",
"linux": "linux",
"nacl": None,
"netbsd": None,
Expand All @@ -119,6 +122,7 @@ _SYSTEM_TO_BINARY_EXT = {
"freebsd": "",
"fuchsia": "",
"ios": "",
"visionos": "",
"linux": "",
"nixos": "",
"none": "",
Expand All @@ -140,6 +144,7 @@ _SYSTEM_TO_STATICLIB_EXT = {
"freebsd": ".a",
"fuchsia": ".a",
"ios": ".a",
"visionos": ".a",
"linux": ".a",
"nixos": ".a",
"none": ".a",
Expand All @@ -158,6 +163,7 @@ _SYSTEM_TO_DYLIB_EXT = {
"freebsd": ".so",
"fuchsia": ".so",
"ios": ".dylib",
"visionos": ".dylib",
"linux": ".so",
"nixos": ".so",
"none": ".so",
Expand Down Expand Up @@ -198,6 +204,7 @@ _SYSTEM_TO_STDLIB_LINKFLAGS = {
"fuchsia": ["-lzircon", "-lfdio"],
"illumos": ["-lsocket", "-lposix4", "-lpthread", "-lresolv", "-lnsl", "-lumem"],
"ios": ["-lSystem", "-lobjc", "-Wl,-framework,Security", "-Wl,-framework,Foundation", "-lresolv"],
"visionos": ["-lSystem", "-lobjc", "-Wl,-framework,Security", "-Wl,-framework,Foundation", "-lresolv"],
# TODO: This ignores musl. Longer term what does Bazel think about musl?
"linux": ["-ldl", "-lpthread"],
"nacl": [],
Expand Down Expand Up @@ -263,8 +270,8 @@ def abi_to_constraints(abi, *, arch = None, system = None):
List: A list of labels to constraint values
"""

# add constraints for iOS + watchOS simulator and device triples
if system in ["ios", "watchos"]:
# add constraints for iOS + watchOS + visionOS simulator and device triples
if system in ["ios", "watchos", "visionos"]:
if arch == "x86_64" or abi == "sim":
return ["@build_bazel_apple_support//constraints:simulator"]
else:
Expand Down
2 changes: 1 addition & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate
if toolchain.target_os == "windows":
make_link_flags = _make_link_flags_windows_msvc if toolchain.target_triple.abi == "msvc" else _make_link_flags_windows_gnu
get_lib_name = get_lib_name_for_windows
elif toolchain.target_os.startswith(("mac", "darwin", "ios")):
elif toolchain.target_os.startswith(("mac", "darwin", "ios", "visionos")):
make_link_flags = _make_link_flags_darwin
get_lib_name = get_lib_name_default
else:
Expand Down

0 comments on commit 761aecc

Please sign in to comment.