# Ability to detect profiles

**URL:** <https://internals.rust-lang.org/t/ability-to-detect-profiles/19794>\
**Category:** compiler\
**Created:** [November 1, 2023, 11:10am UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794 "2023-11-01T11:10:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pzixel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pzixel/32/4349_2.png) [@Pzixel](https://internals.rust-lang.org/u/Pzixel)\
**Post date:** [November 1, 2023, 11:10am UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/1 "2023-11-01T11:10:39Z")

</div>

cargo currently supports profiles but it's not possbile to access this information from the code. Example :

```toml
[profile.mycoolprofile]
inherits = "release"

[profile.mycoolotherprofile]
inherits = "release"

```

Now I want to make cfg `if cfg!(profile = "mycoolotherprofile") { ... }`

Another way to do this is to implement feature manually but again there is no way currently to declare a feature for the profile only (or at least not to my knowledge)

What is the proper way to do this? I was told to 'use make and be with it' but I don't want to use tooling outside cargo. I think that this could be a valuable feature in cargo, but the onl proposal on this got closed ( [Allow profile to be used in `cfg`s · Issue #3803 · rust-lang/cargo · GitHub](https://github.com/rust-lang/cargo/issues/3803) ) with no direct answer (they were focused on `bench` question but this is just one example of what profile could be. We have cfg test in the end).

---

<div class="post-metadata">

**Author:** ![epage](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/epage/32/3171_2.png) [@epage](https://internals.rust-lang.org/u/epage)\
**Post date:** [November 1, 2023, 12:28pm UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/2 "2023-11-01T12:28:56Z")

</div>

Could you expand on why making it conditional on `profile` is the right way to go as compared to `--cfg` or features?

---

<div class="post-metadata">

**Author:** ![Pzixel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pzixel/32/4349_2.png) [@Pzixel](https://internals.rust-lang.org/u/Pzixel)\
**Post date:** [November 1, 2023, 12:57pm UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/3 "2023-11-01T12:57:04Z")

</div>

Because profile can enable something else (for example, profile can enable lto). For example I can have `profile.highperf` that enables lto=true and say feature `allow_unsafe_indexing`. In my case I would like it to make it always enabled for this profile.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 1, 2023, 9:31pm UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/4 "2023-11-01T21:31:59Z")

</div>

Maybe a design the other way would be better: allow profiles to enable features, so your `[profile.highperf]` could have `required-features = ["reckless_indexing"]`

---

<div class="post-metadata">

**Author:** ![epage](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/epage/32/3171_2.png) [@epage](https://internals.rust-lang.org/u/epage)\
**Post date:** [November 2, 2023, 1:44am UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/5 "2023-11-02T01:44:36Z")

</div>

Since profiles are at the workspace level, it would be very limited what features they can enable.

This also, to me, is a reason against making `cfg`s for profiles like this as it very much limits where the profiles can be detected. This would also likely not play well with the `--check-cfg` feature.

Now, [global features](https://internals.rust-lang.org/t/pre-rfc-mutually-excusive-global-features/19618) might do the trick 😉

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [November 3, 2023, 3:09am UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/6 "2023-11-03T03:09:48Z")

</div>

To ensure the information known, build scripts have `$PROFILE` set to `debug` or `release` (_only_) depending on which profile is transitively inherited from, but using this is not recommended; using other more granular indicators set by the profile (e.g. `OPT_LEVEL`) is preferred.

I would extend this to cfging based on profiles — instead of matching the profile itself, which is more fragile (even if we set a multivalued cfg for the whole inheritance chain, so e.g. `profile = "release"` and `profile = "bench"` were both set), key off of something the profile sets.

Setting features from a profile doesn't work well, but I do think setting custom cfgs from profiles could work well. It doesn't break use of `--cfg lib_unstable` root package opt-in requirement tricks (only the root manifest can define profiles) and it makes it easier to use conventional cfgs than needing to mess with `$RUSTFLAGS` (e.g. cargo-careful establishes `cfg(careful)` to turn on extra safety checks over just `cfg(debug_assertions)`).

Setting custom cfgs are, in a way, a form of semiglobal features, since enabling them via RUSTFLAGS does so globally, and there's no real targeted way to enable them just for certain crates in a compilation graph (except for environment variable plus buildscript shenanigans).

---

<div class="post-metadata">

**Author:** ![Pzixel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pzixel/32/4349_2.png) [@Pzixel](https://internals.rust-lang.org/u/Pzixel)\
**Post date:** [November 3, 2023, 10:39am UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/7 "2023-11-03T10:39:05Z")

</div>

Hmm, this sounds like a great idea, but I was unable to enable `required-features` just for one profile. Could you please elaborate a little bit more?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [November 3, 2023, 4:43pm UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/8 "2023-11-03T16:43:23Z")

</div>

This is not a real syntax, but a hypothetical alternative proposal.

---

<div class="post-metadata">

**Author:** ![Pzixel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pzixel/32/4349_2.png) [@Pzixel](https://internals.rust-lang.org/u/Pzixel)\
**Post date:** [November 3, 2023, 5:13pm UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/9 "2023-11-03T17:13:57Z")

</div>

Ah, my bad. But it would really a big help in this, not automatically generating features but at least requiring them would be really nice.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [February 1, 2024, 5:14pm UTC](https://internals.rust-lang.org/t/ability-to-detect-profiles/19794/10 "2024-02-01T17:14:51Z")

</div>

Closed on February 1, 2024, 5:14pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
