From 99cd14d7e1fccc59c587ca89400f181413293d4f Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 27 Jul 2022 03:58:42 +0200 Subject: [PATCH] cfg-guard things that don't work on older macOS versions --- .github/workflows/ci.yml | 5 +++- objc2-foundation/examples/declaration.rs | 11 ++++++++ objc2-foundation/src/lib.rs | 3 +++ objc2-foundation/src/uuid.rs | 2 ++ objc2/examples/talk_to_me.rs | 33 ++++++++++++++---------- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d1e5acae..942823487 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -206,7 +206,10 @@ jobs: - name: Setup SDK environment if: matrix.sdk # This changes a variable, so is only set when a custom SDK is used - run: echo "SDKROOT=$HOME/extern/sdk" >> $GITHUB_ENV + run: | + echo "SDKROOT=$HOME/extern/sdk" >> $GITHUB_ENV + # Temporary + echo "RUSTFLAGS=$RUSTFLAGS --cfg=macos_10_7" >> $GITHUB_ENV - name: Install Clang & Valgrind if: contains(matrix.os, 'ubuntu') diff --git a/objc2-foundation/examples/declaration.rs b/objc2-foundation/examples/declaration.rs index 9e18ff06b..06d9eb4bb 100644 --- a/objc2-foundation/examples/declaration.rs +++ b/objc2-foundation/examples/declaration.rs @@ -1,18 +1,22 @@ +#[cfg(all(feature = "apple", target_os = "macos"))] use objc2::{ msg_send, msg_send_id, rc::{Id, Shared}, runtime::{Bool, Object}, }; +#[cfg(all(feature = "apple", target_os = "macos"))] use objc2_foundation::{declare_class, extern_class, NSObject}; #[cfg(all(feature = "apple", target_os = "macos"))] #[link(name = "AppKit", kind = "framework")] extern "C" {} +#[cfg(all(feature = "apple", target_os = "macos"))] extern_class! { unsafe struct NSResponder: NSObject; } +#[cfg(all(feature = "apple", target_os = "macos"))] declare_class! { unsafe struct CustomAppDelegate: NSResponder, NSObject { pub ivar: u8, @@ -66,6 +70,7 @@ declare_class! { } } +#[cfg(all(feature = "apple", target_os = "macos"))] impl CustomAppDelegate { pub fn new(ivar: u8, another_ivar: bool) -> Id { let cls = Self::class(); @@ -80,9 +85,15 @@ impl CustomAppDelegate { } } +#[cfg(all(feature = "apple", target_os = "macos"))] fn main() { let delegate = CustomAppDelegate::new(42, true); println!("{}", delegate.ivar); println!("{}", delegate.another_ivar.as_bool()); } + +#[cfg(not(all(feature = "apple", target_os = "macos")))] +fn main() { + panic!("This example uses AppKit, which is only present on macOS"); +} diff --git a/objc2-foundation/src/lib.rs b/objc2-foundation/src/lib.rs index fc0dc0f03..952ae754a 100644 --- a/objc2-foundation/src/lib.rs +++ b/objc2-foundation/src/lib.rs @@ -66,6 +66,7 @@ pub use self::process_info::NSProcessInfo; pub use self::range::NSRange; pub use self::string::NSString; pub use self::thread::{is_main_thread, is_multi_threaded, MainThreadMarker, NSThread}; +#[cfg(not(macos_10_7))] // Temporary pub use self::uuid::NSUUID; pub use self::value::NSValue; pub use self::zone::NSZone; @@ -112,6 +113,8 @@ mod process_info; mod range; mod string; mod thread; +// Temporarily disable testing UUID on macOS 10.7 until +#[cfg(not(macos_10_7))] mod uuid; mod value; mod zone; diff --git a/objc2-foundation/src/uuid.rs b/objc2-foundation/src/uuid.rs index 71f413de1..726ffe641 100644 --- a/objc2-foundation/src/uuid.rs +++ b/objc2-foundation/src/uuid.rs @@ -15,6 +15,8 @@ extern_class! { /// Conversion methods to/from UUIDs from the `uuid` crate can be /// enabled with the `uuid` crate feature. /// + /// macOS: This is only available on 10.8 and above. + /// /// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsuuid?language=objc). #[derive(PartialEq, Eq, Hash)] unsafe pub struct NSUUID: NSObject; diff --git a/objc2/examples/talk_to_me.rs b/objc2/examples/talk_to_me.rs index c2e5ac87a..e3a4b387b 100644 --- a/objc2/examples/talk_to_me.rs +++ b/objc2/examples/talk_to_me.rs @@ -3,22 +3,29 @@ //! **Untested**! //! //! Works on macOS >= 10.15 or iOS > 7.0! -use objc2::ffi::NSUInteger; -use objc2::rc::{Id, Owned, Shared}; -use objc2::runtime::Object; -use objc2::{class, msg_send, msg_send_bool, msg_send_id}; -use std::ffi::c_void; -#[cfg(feature = "apple")] -#[link(name = "AVFoundation", kind = "framework")] -extern "C" {} -#[cfg(feature = "apple")] -#[link(name = "Foundation", kind = "framework")] -extern "C" {} - -const UTF8_ENCODING: NSUInteger = 4; +#[cfg(not(talk_to_me_example))] +fn main() { + panic!("pass the `--cfg talk_to_me_example` flag to run this example!"); +} +#[cfg(talk_to_me_example)] fn main() { + use objc2::ffi::NSUInteger; + use objc2::rc::{Id, Owned, Shared}; + use objc2::runtime::Object; + use objc2::{class, msg_send, msg_send_bool, msg_send_id}; + use std::ffi::c_void; + + #[cfg(feature = "apple")] + #[link(name = "AVFoundation", kind = "framework")] + extern "C" {} + #[cfg(feature = "apple")] + #[link(name = "Foundation", kind = "framework")] + extern "C" {} + + const UTF8_ENCODING: NSUInteger = 4; + let text = "Hello from Rust!"; // Note: objc2-foundation has functionality to do this safely!