Skip to content

Platform target-specific features #1197

Open

Description

@alexcrichton

See also https://internals.rust-lang.org/t/pre-rfc-mutually-excusive-global-features/19618


It would be nice to support target-specific features for cases such as when certain functionality is only available on one platform or the default set of functionality should vary per-platform.

Activity

  1. added
    A-features
    on Jan 20, 2015
  2. alexcrichton commented on Jan 20, 2015

    @alexcrichton
    MemberAuthor
  3. nox commented on Jul 10, 2016

    @nox
    Contributor

    Shouldn't this work?

    [target.'cfg(not(any(target_os = "android", target_os = "windows")))'.dependencies]
    ipc-channel = {git = "https://github.com/servo/ipc-channel"}
    
    [target.'cfg(any(target_os = "android", target_os = "windows"))'.dependencies]
    ipc-channel = {git = "https://github.com/servo/ipc-channel", features = ["inprocess"]}
  4. alexcrichton commented on Jul 11, 2016

    @alexcrichton
    MemberAuthor

    @nox in theory yeah that seems like it should work, but today due to the structure of Resolve it doesn't. The entire target-independent resolution graph is created and then only later filtered down to the relevant set of packages. As an artifact of creating Resolve at the beginning it resolves all features, basically with a map from package to list of features. That means that all instances of ipc-channel in your example would have the inprocess feature enabled, regardless of what platform it was on.

    I think that's a fixable bug, however, although likely quite invasive as it would require deferring calculation of features to the compilation phase rather than the resolution phase.

  5. added a commit that references this issue on Jul 12, 2016
  6. added a commit that references this issue on Jul 12, 2016
  7. jethrogb commented on Nov 5, 2016

    @jethrogb
    Contributor

    Ran into this extremely confusing bug today...

  8. gyscos commented on Dec 20, 2016

    @gyscos

    Would this also allow specifying a target-specific features.default array?
    Something like:

    [target.'cfg(not(target_os = "windows"))'.features]
    default = ["general_feature"]
    
    [target.'cfg(target_os = "windows")'.features]
    default = ["winonly_replacement"]

    I suppose a workaround to get this would be an intermediate crate which depends on the actual crate with target-dependent features, but it doesn't sound very convenient.

  9. alexcrichton commented on Dec 20, 2016

    @alexcrichton
    MemberAuthor

    @gyscos that seems like a nifty and natural way to encode this information! I haven't though too much about the implications of target-specific features, but I don't particularly see any immediate reason to block anything.

  10. nox commented on Mar 4, 2017

    @nox
    Contributor

    This isn't enough, I don't want my crate to stop working because someone used no-default-features.

    I guess one could use @retep998's trick of enabling a feature programatically from a build.rs script.

  11. retep998 commented on Mar 4, 2017

    @retep998
    Contributor

    @nox That enables features in code programmatically. It doesn't let me control dependencies, but for some people just controlling their code might be enough. The reason I even wrote that trick was due to the lack of support for cyclic features. https://github.com/retep998/winapi-rs/blob/dev/build.rs#L193

  12. philn commented on Oct 18, 2017

    @philn

    What's the status of this issue? It would be interesting to use target-specific features in gecko-media (enable pulseaudio only for linux for instance).

  13. alexcrichton commented on Oct 18, 2017

    @alexcrichton
    MemberAuthor

    @philn AFAIK no progress has been made

  14. praetp commented on May 3, 2018

    @praetp

    Hope we can see progress on this issue...

  15. huangjj27 commented on May 12, 2018

    @huangjj27

    This will help a lot with rust-random/rand#442

  16. 105 remaining items

  17. daxpedda commented on Feb 14, 2024

    @daxpedda
    Contributor

    Why is that? Backends are a top-level concern so it seems like something that should be set using globals, rather than features which are for direct dependents and unified.

    This is true and would also be a good fit for Wgpu but addresses a different problem.

    Taking from #1197 (comment) Wgpu needs:

    • To enable some dependencies (and features) by default depending on the target.
    • Expose crate features that do nothing depending on the target.

    Specifically the proposal mentions but leaves out:

    • Target-specific set-globals, which would have to play well with default as well.
    • Multi-valued globals, which Wgpu could probably live without, but would still introduce some complexity, having to play around with multiple globals that do the same thing depending on the target or if using the default.

    Maybe I didn't understand the proposal well enough or missed something?


    On second thought: maybe I misunderstood your earlier comment and you meant to say that this problem should rather be solved by "global, mutually exclusive features" because these use-cases should move away from crate features anyway?

  18. epage commented on Feb 14, 2024

    @epage
    Contributor

    Taking from #1197 (comment) Wgpu needs:

    Are those for cases that should be controlled by the direct dependent or the final artifact?

    • If its for the direct dependent, this is the appropriate issue but an explanation why this is for direct dependents would be helpful.
    • If its for the final artifact, that would be feedback on the global, mutually exclusive features proposal.
  19. daxpedda commented on Feb 14, 2024

    @daxpedda
    Contributor

    Taking from #1197 (comment) Wgpu needs:

    Are those for cases that should be controlled by the direct dependent or the final artifact?

    • If its for the direct dependent, this is the appropriate issue but an explanation why this is for direct dependents would be helpful.
    • If its for the final artifact, that would be feedback on the global, mutually exclusive features proposal.

    They should be controlled by the final artifact.
    I will make a post there, thanks!

  20. fwcd commented on Apr 23, 2024

    @fwcd

    Haven't read the whole thread, but this is my use case:

    I am building a cross platform GUI library (nuit) and would like to provide both a smooth experience for library consumers that "just want sensible defaults" (e.g. SwiftUI on macOS, GTK+ on Linux, ...), while simultaneously offering the flexibility to select a custom backend. Since backends may be supported on multiple platforms (e.g. GTK+ runs on macOS too), I don't think this can cleanly be modeled with the winit workaround of enabling all backends by default and making them a no-op on unsupported platforms, since that would e.g. always result in GTK+ libraries being pulled in on macOS.

    What I would really like to write would be something along the lines of

    [target.'cfg(target_os = "macos")'.features]
    default = ["swiftui"]
    
    [target.'cfg(target_os = "linux")'.features]
    default = ["gtk"]

    which is very close to the existing suggestions from this thread. The other workaround would be introducing wrapper crates for each backend and using the existing target-specific dependency mechanism, but that still feels like an artificial restriction over something that could be expressed a lot more cleanly and concisely.

  21. added
    S-needs-design
    and removed
    S-needs-team-input
    on Mar 19, 2025
  22. zoechi commented on Apr 30, 2025

    @zoechi

    I just run into this with [build-dependencies].
    tonic-build/transport is required for non-WASM but must not be enabled for WASM or it generates code that doesn't compile

  23. teohhanhui commented on Jul 24, 2025

    @teohhanhui

    The workarounds mentioned so far don't cover the use case of:

    • A feature on a non-target specific (optional) dependency, required by one of the current crate's features in a target-specific way.

    Whew... What a mouthful.

    Example (current):

    [dependencies]
    rustix = { workspace = true, features = [], optional = true }
    tokio = { workspace = true, features = ["sync"] }
    tokio-util = { workspace = true, features = [], optional = true }
    
    [features]
    default = ["terminal"]
    terminal = ["rustix/fs", "tokio/io-std", "tokio/net", "tokio-util/codec"]

    Example (wanted):

    [dependencies]
    rustix = { workspace = true, features = [], optional = true }
    tokio = { workspace = true, features = ["sync"] }
    tokio-util = { workspace = true, features = [], optional = true }
    
    [features]
    default = ["terminal"]
    terminal = ["rustix/fs", "tokio/io-std", "tokio-util/codec"]
    
    [target.'cfg(unix)'.features]
    terminal = ["tokio/net"] # Expected feature unification

    The net feature on tokio is only being used when the terminal feature of the current crate is selected, and only on unix targets.

  24. tmccombs commented on Nov 4, 2025

    @tmccombs
    Contributor

    My usecase (and it looks like the use case in several issues on various projects that link to this), is I would like to enable using jemalloc by default on platforms where it is easy to build, but optional on other platforms, so that it can still be compiled with jemalloc if the user has jemalloc set up, but won't try by default, when it is likely to fail.

    The current options I see are:

    • Make the tikv-jemallocator dep always optional, and users must opt-in to using it, regardless of platform
    • Use target.cfg(...).dependencies to only add the dependency for some platforms. But then platforms that don't match can't use jemalloc, even if they want to. (this is what fd currently does, but see https://github.com/sharkdp/fd/issues/1822}.
    • Always include jemalloc in the default features, and on platforms where building jemalloc is more difficult, the user must opt out by not using the default features.

    None of these options are very good.

  25. epage commented on Nov 4, 2025

    @epage
    Contributor

    I feel like features aren't quite the right solution to this problem and that it should likely be solved as part of https://internals.rust-lang.org/t/pre-rfc-mutually-excusive-global-features/19618 or as a follow up afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-featuresE-hardS-needs-design

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions