Skip to content

Commit

Permalink
Add Apple visionOS support (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinPerez authored Apr 16, 2024
1 parent 42ccbce commit 969e1a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
16 changes: 8 additions & 8 deletions ctor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ macro_rules! ctor_attributes {
#[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
#[cfg_attr(target_os = "illumos", link_section = ".init_array")]
#[cfg_attr(target_os = "haiku", link_section = ".init_array")]
#[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), link_section = "__DATA,__mod_init_func")]
#[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos"), link_section = "__DATA,__mod_init_func")]
#[cfg_attr(windows, link_section = ".CRT$XCU")]
)
};
Expand Down Expand Up @@ -171,7 +171,7 @@ pub fn ctor(_attribute: TokenStream, function: TokenStream) -> TokenStream {

let tokens = ctor_attributes!();
let output = quote!(
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", target_os = "illumos", target_os = "haiku", target_os = "macos", target_os = "ios", target_os = "tvos", windows)))]
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", target_os = "illumos", target_os = "haiku", target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos", windows)))]
compile_error!("#[ctor] is not supported on the current target");

#(#attrs)*
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn ctor(_attribute: TokenStream, function: TokenStream) -> TokenStream {

let tokens = ctor_attributes!();
let output = quote!(
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", target_os = "illumos", target_os = "haiku", target_os = "macos", target_os = "ios", target_os = "tvos", windows)))]
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", target_os = "illumos", target_os = "haiku", target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos", windows)))]
compile_error!("#[ctor] is not supported on the current target");

// This is mutable, but only by this macro code!
Expand Down Expand Up @@ -328,7 +328,7 @@ pub fn dtor(_attribute: TokenStream, function: TokenStream) -> TokenStream {

let tokens = ctor_attributes!();
let output = quote!(
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", target_os = "illumos", target_os = "haiku", target_os = "macos", target_os = "ios", target_os = "tvos", windows)))]
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly", target_os = "illumos", target_os = "haiku", target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos", windows)))]
compile_error!("#[dtor] is not supported on the current target");

#(#attrs)*
Expand All @@ -339,7 +339,7 @@ pub fn dtor(_attribute: TokenStream, function: TokenStream) -> TokenStream {

// Note that we avoid a dep on the libc crate by linking directly to atexit functions

#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos")))]
#[inline(always)]
unsafe fn do_atexit(cb: unsafe extern fn()) {
extern "C" {
Expand All @@ -349,7 +349,7 @@ pub fn dtor(_attribute: TokenStream, function: TokenStream) -> TokenStream {
}

// For platforms that have __cxa_atexit, we register the dtor as scoped to dso_handle
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos"))]
#[inline(always)]
unsafe fn do_atexit(cb: unsafe extern fn(_: *const u8)) {
extern "C" {
Expand All @@ -366,10 +366,10 @@ pub fn dtor(_attribute: TokenStream, function: TokenStream) -> TokenStream {
:
unsafe extern "C" fn() =
{
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos")))]
#[cfg_attr(any(target_os = "linux", target_os = "android"), link_section = ".text.exit")]
unsafe extern "C" fn #dtor_ident() { #ident() };
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "visionos", target_os = "tvos"))]
unsafe extern "C" fn #dtor_ident(_: *const u8) { #ident() };
#[cfg_attr(any(target_os = "linux", target_os = "android"), link_section = ".text.startup")]
unsafe extern fn __dtor_atexit() {
Expand Down
7 changes: 6 additions & 1 deletion tests/src/dylib_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ unsafe fn dtor() {
libc_ewriteln!("- dtor bin");
}

#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "visionos",
target_os = "tvos"
))]
fn lib_extension() -> &'static str {
"dylib"
}
Expand Down

0 comments on commit 969e1a0

Please sign in to comment.