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

WIP: Macro to conditionally emit code depending on deployment target #212

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

madsmtm
Copy link
Owner

@madsmtm madsmtm commented Jul 23, 2022

Fixes #266.

Useful for various optimizations and feature-gating functionality depending on OS version.

The expansion is roughly:

#[cfg_available(macOS(10.9), ...)]
fn my_fn() {}

// MACOSX_DEPLOYMENT_TARGET=10.9
#[cfg(any(target_os = "macos"))]
fn my_fn() {}

// MACOSX_DEPLOYMENT_TARGET=10.7
#[cfg(any(__not_available))]
fn my_fn() {}

// MACOSX_DEPLOYMENT_TARGET not set
// Available on Aarch64, since that has a higher default deployment target
#[cfg(any(not(all(target_os = "macos", not(target_arch = "aarch64")))))]
fn my_fn() {}

Further improvement would be an available! macro that checks the runtime version if the deployment target is not high enough, like this:

if available!(macOS(10.9)) {
    // Do something
} else {
    // Fallback
}

// MACOSX_DEPLOYMENT_TARGET=10.9
if cfg!(any(target_os = "macos")) {
    // Do something
} else {
    // Fallback
}

// MACOSX_DEPLOYMENT_TARGET=10.7
if runtime::check_version(10, 9, 0) {
    // Do something
} else {
    // Fallback
}

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How do we handle availability?
1 participant