Skip to content

Commit

Permalink
Allow crate users to opt out of some platform support
Browse files Browse the repository at this point in the history
We have gotten several inbound tickets with users expressing concern
over the inclusion of `cxx` to support the haiku platform. Additionally,
for my personal use, I would prefer to be able to prune things like
`wasm-bindgen` from my lockfile when not expressly enabling Wasm
platform support, similar to the approach the `getrandom` crate takes.

This commit adds cargo features which allow disabling platform support
for each platform that requires a third party dependency. Currently,
that set of platforms is:

- android
- all apple targets
- haiku
- wasm
- windows

This commit adds a new default feature, `platform-all`, which retains
the existing behavior of all platforms being enabled by default.
`platform-all` is a meta feature which enables all of the other platform
support features.

For example, to disable haiku support, users would add the following to
their `Cargo.toml`:

```toml
iana-time-zone = { version = "0.2", default-features = false, features = ["platform-android", "platform-apple", "platform-wasm", "platform-windows"] }
```

The addition of a new default feature is a breaking change because it
may break the build for folks currently depending on this crate with
`default-features = false`.

I'm not sure if this is the direction we want to go, but it would
address the ask from these tickets:

- #89
- #88

I know that cargo won't build deps unless compiling for a particular
platform, but practically, most users don't want or need Haiku or Wasm
support. These targets have heavy dependencies which end up in the lock
file. Deps in the lock file have a carrying cost in terms of supply
chain risk and need for patching/updating.
  • Loading branch information
lopopolo committed May 10, 2023
1 parent 822dd4c commit 14a40cb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
31 changes: 24 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "iana-time-zone"
description = "get the IANA time zone for the current system"
version = "0.1.56"
version = "0.2.0"
authors = [
"Andrew Straw <strawman@astraw.com>",
"René Kijewski <rene.kijewski@fu-berlin.de>",
Expand All @@ -15,27 +15,44 @@ readme = "README.md"
edition = "2018"

[features]
default = ["platform-all"]
# Allow users to opt out of platform support for platforms that require third
# party dependencies. Disabling any of these platforms (i.e. with
# `default-features = false`) will prevent the platform dependencies from
# appearing in downstream applications' `Cargo.lock`.
platform-all = [
"platform-android",
"platform-apple",
"platform-haiku",
"platform-wasm",
"platform-windows",
]
platform-android = ["android_system_properties"]
platform-apple = ["core-foundation-sys"]
platform-haiku = ["iana-time-zone-haiku"]
platform-wasm = ["js-sys", "wasm-bindgen"]
platform-windows = ["windows"]
# When enabled, the library will succeed to compile for unknown target platforms, and return an `Err(GetTimezoneError::OsError)` at runtime.
fallback = []

[target.'cfg(target_os = "android")'.dependencies]
android_system_properties = "0.1.5"
android_system_properties = { version = "0.1.5", optional = true }

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation-sys = "0.8.3"
core-foundation-sys = { version = "0.8.3", optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.48.0", features = [ "Globalization" ] }
windows = { version = "0.48.0", features = [ "Globalization" ], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.50"
wasm-bindgen = "0.2.70"
js-sys = { version = "0.3.50", optional = true }
wasm-bindgen = { version = "0.2.70", optional = true }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3"

[target.'cfg(target_os = "haiku")'.dependencies]
iana-time-zone-haiku = { version = "0.1.1", path = "haiku" }
iana-time-zone-haiku = { version = "0.1.1", path = "haiku", optional = true }

[workspace]
members = [".", "haiku"]
Expand Down
29 changes: 24 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,23 @@
mod ffi_utils;

#[cfg_attr(target_os = "linux", path = "tz_linux.rs")]
#[cfg_attr(target_os = "windows", path = "tz_windows.rs")]
#[cfg_attr(any(target_os = "macos", target_os = "ios"), path = "tz_macos.rs")]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
all(feature = "platform-windows", target_os = "windows"),
path = "tz_windows.rs"
)]
#[cfg_attr(
all(
feature = "platform-apple",
any(target_os = "macos", target_os = "ios")
),
path = "tz_macos.rs"
)]
#[cfg_attr(
all(
feature = "platform-wasm",
target_arch = "wasm32",
not(target_os = "wasi")
),
path = "tz_wasm32.rs"
)]
#[cfg_attr(
Expand All @@ -52,8 +65,14 @@ mod ffi_utils;
any(target_os = "illumos", target_os = "solaris"),
path = "tz_illumos.rs"
)]
#[cfg_attr(target_os = "android", path = "tz_android.rs")]
#[cfg_attr(target_os = "haiku", path = "tz_haiku.rs")]
#[cfg_attr(
all(feature = "platform-android", target_os = "android"),
path = "tz_android.rs"
)]
#[cfg_attr(
all(feature = "platform-haiku", target_os = "haiku"),
path = "tz_haiku.rs"
)]
mod platform;

/// Error types
Expand Down
5 changes: 4 additions & 1 deletion src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ pub fn get_timezone_inner() -> std::result::Result<String, crate::GetTimezoneErr
#[cfg(not(feature = "fallback"))]
compile_error!(
"iana-time-zone is currently implemented for Linux, Window, MacOS, FreeBSD, NetBSD, \
OpenBSD, Dragonfly, WebAssembly (browser), iOS, Illumos, Android, Solaris and Haiku.",
OpenBSD, Dragonfly, WebAssembly (browser), iOS, Illumos, Android, Solaris and Haiku. \
If you are seeing this error on a supported platform, ensure the platform support \
Cargo feature is enabled, e.g. platform-android, platform-apple, platform-haiku, \
platform-wasm, platform-windows, or platform-all.",
);

0 comments on commit 14a40cb

Please sign in to comment.