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

cfg aliases #7260

Open
kvark opened this issue Aug 18, 2019 · 6 comments
Open

cfg aliases #7260

kvark opened this issue Aug 18, 2019 · 6 comments
Labels
A-manifest Area: Cargo.toml issues A-target-dependencies Area: [target.'cfg(foo)'.dependencies] C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@kvark
Copy link

kvark commented Aug 18, 2019

Describe the problem you are trying to solve

As seen in gfx-rs/wgpu#290 (comment), cfg parameters can be quite long:

[target.'cfg(all(target_os = "windows", not(target_arch = "wasm32")))'.dependencies]
gfx-backend-dx12 = { version = "0.3", optional = true }

[target.'cfg(all(any(target_os = "windows", all(unix, not(any(target_os = "macos", target_os = "ios")))), not(target_arch = "wasm32")))'.dependencies]
gfx-backend-vulkan = { version = "0.3", optional = true }

[target.'cfg(any(all(target_os = "macos", not(target_arch = "wasm32"), all(target_arch = "aarch64", target_os = "ios"))))'.dependencies]
gfx-backend-metal = { version = "0.3", optional = true }

If such a dependency is not optional, using it has to be guarded by a similar cfg statement in the code:

#[cfg(any(all(target_os = "macos", not(target_arch = "wasm32"), all(target_arch = "aarch64", target_os = "ios"))))]
gfx_backend_metal::Instance::create("", 1);

Describe the solution you'd like

It is very inconvenient and error prone. It would be great if instead we could define a set of configurations to be used across Cargo config as well as the code. Something like this:

[configurations]
"metal" = 'any(all(target_os = "macos", not(target_arch = "wasm32"), all(target_arch = "aarch64", target_os = "ios")))'

[target.cfg("metal").dependencies]
gfx_backend_metal = "0.3"

And then in Rust code:

#[cfg("metal")]
gfx_backend_metal::Instance::create("", 1);

Notes

Interestingly, configuration aliases are already there, just hard-coded to just "windows" and "unix": https://doc.rust-lang.org/reference/conditional-compilation.html#unix-and-windows

This issue can be seen as making this more generic.

@kvark kvark added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Aug 18, 2019
@kvark
Copy link
Author

kvark commented Sep 8, 2019

A real-world example on how unfortunate for us to not have this feature: gfx-rs/wgpu#333

@tarcieri
Copy link

I'd be a big fan of this as I'm dealing with the following in the polyval crate:

#[cfg(all(
    target_feature = "pclmulqdq",
    target_feature = "sse2",
    target_feature = "sse4.1",
    any(target_arch = "x86", target_arch = "x86_64")
))]

I see there was a pre-RFC for it:

https://internals.rust-lang.org/t/pre-rfc-cfg-attribute-alias/7404

@Manishearth suggested this syntax, which looks great to me:

#![cfg_alias(foo, any(bar, baz))]

#[cfg(foo)] pub mod bar;

@kvark
Copy link
Author

kvark commented Sep 19, 2019

I wonder if there is a way here that would support using these aliases in Cargo.toml as well, but I suppose that's too much to ask :) Having at least cfg_alias would be wonderful.

@alexcrichton
Copy link
Member

It's worth pointing out that you can already do this today with build scripts. You'll need to read the env vars yourself, but you can then print out cargo:rustc-cfg=... and make the aliases yourself.

@tarcieri
Copy link

@alexcrichton neat! Thanks for the tip

@ehuss ehuss added the A-manifest Area: Cargo.toml issues label Sep 21, 2019
@Pzixel
Copy link

Pzixel commented Jan 25, 2023

@alexcrichton wouldn't it only work for just one crate? For instance how can we create an alias for target_feature = "pclmulqdq", target_feature = "sse2" and share it across a dozen crates? It would probably nice to have it as a cargo command-line argument or something like this. I'm not having an good example how could it be implemented right now but I can define the problem: it's quite inconvinient when you have to retype the same cfg in every crate.

@epage epage changed the title Feature: configuration aliases Feature: cfg aliases Nov 1, 2023
@epage epage changed the title Feature: cfg aliases cfg aliases Nov 1, 2023
@epage epage added S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. A-target-dependencies Area: [target.'cfg(foo)'.dependencies] labels Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-manifest Area: Cargo.toml issues A-target-dependencies Area: [target.'cfg(foo)'.dependencies] C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

6 participants