Skip to content

Commit

Permalink
Don't automatically enable std and alloc features of dependencies
Browse files Browse the repository at this point in the history
Unless it's currently required.

Part of #656.
  • Loading branch information
madsmtm committed Dec 25, 2024
1 parent 321d4cb commit cfe3c3e
Show file tree
Hide file tree
Showing 107 changed files with 477 additions and 642 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ env:
--package=objc2-exception-helper
# BEGIN AUTOMATICALLY GENERATED
FRAMEWORKS_MACOS_10_12: --package=objc2-accounts --package=objc2-audio-toolbox --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-multipeer-connectivity --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-store-kit --package=objc2-web-kit
FRAMEWORKS_MACOS_10_12: --package=objc2-accounts --package=objc2-audio-toolbox --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-location --package=objc2-core-media --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-multipeer-connectivity --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-store-kit --package=objc2-web-kit
FRAMEWORKS_MACOS_10_13: --package=objc2-accounts --package=objc2-audio-toolbox --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-ml --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-external-accessory --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-metal-performance-shaders --package=objc2-multipeer-connectivity --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-store-kit --package=objc2-vision --package=objc2-web-kit
FRAMEWORKS_MACOS_11: --package=objc2-accessibility --package=objc2-accounts --package=objc2-ad-support --package=objc2-app-kit --package=objc2-app-tracking-transparency --package=objc2-audio-toolbox --package=objc2-authentication-services --package=objc2-automatic-assessment-configuration --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-business-chat --package=objc2-class-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-audio-types --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-ml --package=objc2-core-motion --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-device-check --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-external-accessory --package=objc2-file-provider --package=objc2-file-provider-ui --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-identity-lookup --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-link-presentation --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-metal-performance-shaders --package=objc2-metal-performance-shaders-graph --package=objc2-ml-compute --package=objc2-multipeer-connectivity --package=objc2-natural-language --package=objc2-nearby-interaction --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-sound-analysis --package=objc2-speech --package=objc2-store-kit --package=objc2-system-extensions --package=objc2-uniform-type-identifiers --package=objc2-user-notifications --package=objc2-virtualization --package=objc2-vision --package=objc2-web-kit
FRAMEWORKS_MACOS_12: --package=objc2-accessibility --package=objc2-accounts --package=objc2-ad-services --package=objc2-ad-support --package=objc2-app-kit --package=objc2-app-tracking-transparency --package=objc2-audio-toolbox --package=objc2-authentication-services --package=objc2-automatic-assessment-configuration --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-avf-audio --package=objc2-business-chat --package=objc2-class-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-audio-types --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-ml --package=objc2-core-motion --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-data-detection --package=objc2-device-check --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-external-accessory --package=objc2-file-provider --package=objc2-file-provider-ui --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-identity-lookup --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-link-presentation --package=objc2-local-authentication --package=objc2-local-authentication-embedded-ui --package=objc2-mail-kit --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-metal-performance-shaders --package=objc2-metal-performance-shaders-graph --package=objc2-metric-kit --package=objc2-ml-compute --package=objc2-multipeer-connectivity --package=objc2-natural-language --package=objc2-nearby-interaction --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-sound-analysis --package=objc2-speech --package=objc2-store-kit --package=objc2-system-extensions --package=objc2-uniform-type-identifiers --package=objc2-user-notifications --package=objc2-virtualization --package=objc2-vision --package=objc2-web-kit
Expand Down
8 changes: 4 additions & 4 deletions crates/block2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ workspace = true
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc", "objc2/std"]
alloc = ["objc2/alloc"]
std = ["alloc"]
alloc = []

# Deprecated; this is the default on Apple platforms, and not applicable on other platforms.
apple = []
Expand All @@ -48,12 +48,12 @@ unstable-objfw = []
unstable-private = []

[dependencies]
objc2 = { path = "../objc2", version = "0.5.2", default-features = false }
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, features = ["std"] }

[dev-dependencies.objc2-foundation]
path = "../../framework-crates/objc2-foundation"
default-features = false
features = ["NSError"]
features = ["std", "NSError"]

[package.metadata.docs.rs]
default-target = "aarch64-apple-darwin"
Expand Down
7 changes: 4 additions & 3 deletions crates/block2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,13 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide))]
#![cfg_attr(docsrs, doc(cfg_hide(doc)))]

#[cfg(not(feature = "alloc"))]
compile_error!("The `alloc` feature currently must be enabled.");

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(not(feature = "std"))]
compile_error!("The `std` feature currently must be enabled.");

#[cfg(all(
not(docsrs),
not(any(
Expand Down
4 changes: 2 additions & 2 deletions crates/block2/src/rc_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<F: ?Sized> RcBlock<F> {
/// # Example
///
/// ```
/// # use std::ffi::CStr;
/// # use core::ffi::CStr;
/// # use block2::{Block, ManualBlockEncoding, RcBlock};
/// # use objc2_foundation::NSError;
/// #
Expand All @@ -173,7 +173,7 @@ impl<F: ?Sized> RcBlock<F> {
/// let my_block = RcBlock::with_encoding::<_, _, _, MyBlockEncoding>(|_err: *mut NSError| {
/// 42i32
/// });
/// assert_eq!(my_block.call((std::ptr::null_mut(),)), 42);
/// assert_eq!(my_block.call((core::ptr::null_mut(),)), 42);
/// ```
#[inline]
pub fn with_encoding<'f, A, R, Closure, E>(closure: Closure) -> Self
Expand Down
4 changes: 2 additions & 2 deletions crates/block2/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ where
/// # Example
///
/// ```
/// # use std::ffi::CStr;
/// # use core::ffi::CStr;
/// # use block2::{Block, ManualBlockEncoding, StackBlock};
/// # use objc2_foundation::NSError;
/// #
Expand All @@ -224,7 +224,7 @@ where
/// let my_block = StackBlock::with_encoding::<MyBlockEncoding>(|_err: *mut NSError| {
/// 42i32
/// });
/// assert_eq!(my_block.call((std::ptr::null_mut(),)), 42);
/// assert_eq!(my_block.call((core::ptr::null_mut(),)), 42);
/// ```
#[inline]
pub fn with_encoding<E>(closure: Closure) -> Self
Expand Down
15 changes: 7 additions & 8 deletions crates/block2/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::ffi::CStr;
use core::marker::PhantomData;
use core::mem;
use core::ptr;
// TODO: use `core` when the MSRV is at least 1.64.
use std::ffi::CStr;

use objc2::encode::EncodeArguments;
use objc2::encode::{EncodeArgument, EncodeReturn};
Expand Down Expand Up @@ -347,7 +346,7 @@ mod tests {
unsafe { CStr::from_bytes_with_nul_unchecked(b"C12@?0i4f8\0") };
}
// HACK: use `identity` in order to circumvent a Clippy warning.
assert!(!std::convert::identity(UserSpecified::<Enc1>::IS_NONE));
assert!(!core::convert::identity(UserSpecified::<Enc1>::IS_NONE));

// No input + no output case.
struct Enc2;
Expand All @@ -361,7 +360,7 @@ mod tests {
const ENCODING_CSTR: &'static CStr =
unsafe { CStr::from_bytes_with_nul_unchecked(b"v4@?0\0") };
}
assert!(!std::convert::identity(UserSpecified::<Enc2>::IS_NONE));
assert!(!core::convert::identity(UserSpecified::<Enc2>::IS_NONE));

// Ensure we don't rely on the encoding string's emptiness.
struct Enc3;
Expand All @@ -371,14 +370,14 @@ mod tests {
const ENCODING_CSTR: &'static CStr =
unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
}
assert!(!std::convert::identity(UserSpecified::<Enc3>::IS_NONE));
assert!(!core::convert::identity(UserSpecified::<Enc3>::IS_NONE));

// Only `NoBlockEncoding` should be `IS_NONE`.
assert!(std::convert::identity(NoBlockEncoding::<(), ()>::IS_NONE));
assert!(std::convert::identity(
assert!(core::convert::identity(NoBlockEncoding::<(), ()>::IS_NONE));
assert!(core::convert::identity(
NoBlockEncoding::<(i32, f32), u8>::IS_NONE
));
assert!(std::convert::identity(
assert!(core::convert::identity(
NoBlockEncoding::<(*const u8,), *const c_char>::IS_NONE
));
}
Expand Down
1 change: 1 addition & 0 deletions crates/dispatch2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
crate.
- Added `Once`, a wrapper over `dispatch_once_f` which works similarly to
`std::sync::Once`.
- Added `#![no_std]` support

### Changed
- Moved to the `objc2` project.
Expand Down
10 changes: 4 additions & 6 deletions crates/dispatch2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ workspace = true

[dependencies]
bitflags = { version = "2.5.0", default-features = false }
block2 = { path = "../block2", version = "0.5.1", default-features = false, optional = true }
block2 = { path = "../block2", version = "0.5.1", default-features = false, optional = true, features = ["alloc"] }
libc = { version = "0.2.80", default-features = false, optional = true }
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, optional = true }
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, optional = true, features = ["std"] }

[package.metadata.docs.rs]
default-target = "aarch64-apple-darwin"
Expand All @@ -39,10 +39,8 @@ targets = [

[features]
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc", "bitflags/std", "block2?/std", "libc?/std", "objc2?/std"]
alloc = ["block2?/alloc", "objc2?/alloc"]
std = ["alloc", "bitflags/std"]
alloc = []
block2 = ["dep:block2"]
libc = ["dep:libc"]
objc2 = ["dep:objc2"]
Expand Down
2 changes: 0 additions & 2 deletions crates/header-translator/src/default_cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ targets = [

[features]
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc"]
alloc = []
31 changes: 21 additions & 10 deletions crates/header-translator/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ impl Library {
}
}

for (krate, (_, _, krate_features)) in &mut dependencies {
// std is currently required for objc2
if *krate == "objc2" {
krate_features.insert("std".into());
}
// alloc is currently required for these crates
if matches!(*krate, "block2" | "dispatch2" | "objc2-foundation") {
krate_features.insert("alloc".into());
}
}

dependencies
}

Expand Down Expand Up @@ -273,6 +284,10 @@ see that for related crates.", self.data.krate)?;
(false, false) => format!("../{krate}"),
};
let mut table = match *krate {
"dispatch2" => InlineTable::from_iter([
("path", Value::from(path)),
("version", Value::from("0.1.0")),
]),
"objc2" => InlineTable::from_iter([
("path", Value::from(path)),
("version", Value::from("0.5.2")),
Expand Down Expand Up @@ -327,19 +342,15 @@ see that for related crates.", self.data.krate)?;
.entry(krate)
.or_insert(Item::Value(Value::InlineTable(table)));

cargo_toml["features"]["std"]
.as_array_mut()
.unwrap()
.push(Value::from(format!(
"{krate}{}/std",
if *required { "" } else { "?" },
)));
if *krate != "bitflags" && *krate != "libc" {
cargo_toml["features"]["alloc"]
// Bitflags is more of an "internal" dependency, just like
// objc2-encode is mostly an internal dependency to objc2, so it's
// fine to special-case std for that here.
if *krate == "bitflags" {
cargo_toml["features"]["std"]
.as_array_mut()
.unwrap()
.push(Value::from(format!(
"{krate}{}/alloc",
"{krate}{}/std",
if *required { "" } else { "?" },
)));
}
Expand Down
7 changes: 4 additions & 3 deletions crates/header-translator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,10 @@ fn update_ci(workspace_dir: &Path, config: &Config) -> io::Result<()> {
lib.macos
.as_ref()
.is_some_and(|v| VersionReq::parse("<=10.12").unwrap().matches(v))
// HACK: These depend on `objc2-uniform-type-identifiers`, which
// is not available on macOS 10.12, but will be enabled by `"all"`
&& !["objc2-app-kit", "objc2-file-provider", "objc2-health-kit", "objc2-photos"].contains(&&*lib.krate)
// HACK: These depend on `objc2-uniform-type-identifiers` or
// `objc2-core-ml`, which is not available on macOS 10.12, but
// will be enabled by `"all"`.
&& !["objc2-app-kit", "objc2-file-provider", "objc2-health-kit", "objc2-photos", "objc2-core-image"].contains(&&*lib.krate)
})?;
writer(&mut ci, config, "FRAMEWORKS_MACOS_10_13", |lib| {
lib.macos
Expand Down
4 changes: 1 addition & 3 deletions crates/objc2-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@
#[cfg(not(feature = "alloc"))]
compile_error!("the `alloc` feature currently must be enabled");

extern crate alloc;
#[cfg(any(feature = "std", doc))]
extern crate std;

#[cfg(any(feature = "alloc", test))]
extern crate alloc;

mod encoding;
mod encoding_box;
mod helper;
Expand Down
7 changes: 4 additions & 3 deletions crates/objc2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ workspace = true
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc", "objc2-encode/std", "block2/std", "objc2-foundation/std", "objc2-exception-helper?/std"]
alloc = ["objc2-encode/alloc", "block2/alloc", "objc2-foundation/alloc", "objc2-exception-helper?/alloc"]
std = ["alloc", "objc2-encode/std"]
alloc = ["objc2-encode/alloc"]

# Enables `objc2::exception::throw` and `objc2::exception::catch`
exception = ["dep:objc2-exception-helper"]
Expand Down Expand Up @@ -125,11 +125,12 @@ objc2-exception-helper = { path = "../objc2-exception-helper", version = "0.1.0"
iai = { version = "0.1", git = "https://github.com/madsmtm/iai", branch = "callgrind" }
static_assertions = "1.1.0"
memoffset = "0.9.0"
block2 = { path = "../block2", default-features = false }
block2 = { path = "../block2" }
objc2-core-foundation = { path = "../../framework-crates/objc2-core-foundation", default-features = false, features = [
"CFCGTypes",
] }
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = [
"std",
"NSArray",
"NSDate",
"NSDictionary",
Expand Down
3 changes: 3 additions & 0 deletions crates/objc2/src/topics/about_generated/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* **BREAKING**: The feature flag guarding `SCSensitivityAnalysis` changed.
* **BREAKING**: `-[NSSavePanel beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo:]`
now takes an optional value as the file path.
* **BREAKING**: No longer automatically enable `std` and `alloc` features of
dependencies. If you want a certain framework crate to use `std` or `alloc`
features, you cannot rely on a higher-level crate to enable that for you.

### Deprecated
* Moved `MainThreadMarker` from `objc2-foundation` to `objc2`.
Expand Down
Loading

0 comments on commit cfe3c3e

Please sign in to comment.