- Feature Name:
- Start Date: 2020-09-27
- RFC PR: rust-lang/rfcs#3239
- Rust Issue: rust-lang/rust#96901
cfg(target = "...") conditional compilation key was previously approved,
but was later withdrawn. For details, see the summary comment.
This proposes a new
target, which matches the entire target triple
arm-unknown-linux-gnueabihf). This also adds a
environment variable for parity with other
In addition, this proposes a shorthand
cfg(target(...)) to match multiple
components of a target string at once.
#[cfg] against a specific target, a
build.rs script is required to emit a
cfg based on the
TARGET environment variable. Adding a build script
increases compile time and makes a crate incompatible with certain build
Otherwise, all available components would need to be specified separately:
This can be very cumbersome. Note that the target ABI cannot currently be
#[cfg]-ed against, so a
build.rs is still necessary to match all target
This would act like existing
target_* configurations (except
but match against all components.
#[cfg(target = "x86_64-apple-ios-macabi")] mod mac_catalyst;
#[cfg_attr(target = "...", attr)].
It would also support to specify each
target_* inside a new
attribute as follows:
// So we can for example rewrite: #[cfg(all(target_os = "linux", target_arch = "arm"))] // as: #[cfg(target(os = "linux", arch = "arm"))]
target is a key-value option set once with the target's Rust triple.
The shorthand form of
#[cfg(target(os = "linux))] is expanded and entirely
#[cfg(target_os = "linux")] (and so on for
arch and the other
Configuring against specific targets can be overly strict and could make certain
#[cfg]s miss similar configurations with small changes.
aarch64-unknown-nonedoes not match
aarch64-unknown-none-softfloat, yet one would likely want to include ABI variants. The same concern applies to the target vendor.
A potential solution would be to allow glob matching (e.g.
aarch64-unknown-none*), but that is not within the scope of this proposal because it is not currently used in other
CARGO_CFG_TARGETenvironment variable is redundant with the existing
TARGET. However, including it would be consistent with other
We can keep the existing work-around of checking the
variable in a
build.rs script. However, that increases compile time and makes
a crate incompatible with certain build systems.
Target component configurations:
CARGO_CFG_TARGET_*environment variables for
- How do we ensure a project does not miss configurations similar to the ones
#[cfg]-ed against with this feature? Perhaps this should be added as a Clippy lint that's off by default.
This would enable
#[cfg]-ing against a specific target ABI (e.g.
eabihf). However, that is not the motivation for this proposal and should be