-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Profile specific feature selection. #1956
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
- Feature Name: profile_features | ||
- Start Date: 22-03-2017 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
Support profile-specific overrides for cargo features and dependencies. | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
Sometimes, users wish to control the build of their crates in a manner which | ||
is dependent on which profile it is being built for, because of the differences | ||
in use cases between the profiles. Today, this is not easy to achieve directly, | ||
and instead requires hacking with the existing `cfg` options that roughly | ||
correspond to some of the profiles (such as `test` and `debug_assertions`). | ||
|
||
This system is ad hoc and doesn't allow downstream users to effectively control | ||
the way these features are used by their dependencies. To solve this problem, | ||
we allow profiles to play a first class role in feature and dependency | ||
resolution. | ||
|
||
# Detailed design | ||
[design]: #detailed-design | ||
|
||
Each profile gains these additional members: | ||
|
||
* `features` | ||
* `dependencies` | ||
* `dev-dependencies` | ||
* `build-dependencies | ||
* `target` | ||
|
||
Each of these corresponds to the same top-level object. When compiling in | ||
a profile, its members under these objects are merged with the top level | ||
equivalent, overriding any overlapping members. This enables users to configure | ||
thier builds differently in each profile if necessary. | ||
|
||
Specifically: | ||
|
||
* The `features` table for a profile is *merged* with the base `features` | ||
table; each member of the `features` table defined in the profile-specific | ||
table *replaces* the member in the base table (they are not merged together). | ||
* An individual object representing a specific dependency is *merged* with the | ||
base object for that dependency; each member of that object *replaces* the | ||
member in the base table (they are not merged together). | ||
|
||
So from this TOML: | ||
|
||
```toml | ||
[dependencies.foo] | ||
version = "1.0.0" | ||
features = ["foo"] | ||
|
||
[profile.dev.dependencies.foo] | ||
version = ["bar"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you got a mistake here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that should say |
||
``` | ||
|
||
In the `dev` profile, the foo dependency looks like: | ||
|
||
```toml | ||
[dependencies.foo] | ||
version = "1.0.0" | ||
features = ["bar"] # Note that the foo feature has been dropped | ||
``` | ||
|
||
## Use cases | ||
|
||
### Profile-specific default features | ||
|
||
Some features are intended to be used in different profiles - for example, a | ||
feature for testing or for optimizations which is intended for bench and | ||
release. Today, these sorts of things are managed in an ad hoc and imprecise | ||
way through the `test` and `debug_assertions` cfg flags. | ||
|
||
Instead, authors can turn features on by default in only some profiles. For | ||
example: | ||
|
||
```toml | ||
[features] | ||
default = [] | ||
go-fast = [] | ||
|
||
[profile.release.features] | ||
default = ["go-fast"] | ||
|
||
[profile.bench.features] | ||
default = ["go-fast"] | ||
``` | ||
|
||
### Turning on dependency features in specific profiles | ||
|
||
Possibly, the profile-specific feature is not a default feature for this crate, | ||
but a feature the user wants in a dependency, but only in some profiles. This | ||
can be done using by overriding that crate's dependency member in those | ||
profiles. | ||
|
||
```toml | ||
[profile.release.dependencies.another-crate] | ||
features = ["go-fast"] | ||
|
||
[profile.bench.dependencies.another-crate] | ||
features = ["go-fast"] | ||
``` | ||
|
||
# How We Teach This | ||
[how-we-teach-this]: #how-we-teach-this | ||
|
||
This should be documented in the crates.io documentation for the Cargo.toml | ||
manifest format. | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
The primary drawback is that crates which perform conditional compilation on | ||
profiles may not build correctly in one profile. Since most crates are tested | ||
primarily in the `test` profile, this could be a problem. | ||
|
||
However, this is already an issue today, because of the way the `test` and | ||
`debug_assertion` cfgs can already be used to conditionally compile code only | ||
in certain profiles. This RFC gives users greater control over this by tying | ||
the conditional compilation to explicit features which can be turned on or off. | ||
|
||
# Alternatives | ||
[alternatives]: #alternatives | ||
|
||
One alternative would be to add profiles as a unique cfg marker: | ||
|
||
```rust | ||
#[cfg(profile = "release")] | ||
``` | ||
|
||
This would not be as flexible as this RFC - by tying any conditional | ||
compilation to a _feature_, we allow dowstream users to have ultimate control | ||
over which code is compiled in the dependency. In addition to being flexible, | ||
this gives users a way out if a library is broken because of a broken feature | ||
turned on by default in one profile. | ||
|
||
# Unresolved questions | ||
[unresolved]: #unresolved-questions | ||
|
||
Does this feature overlap in purpose with `dev-dependencies` at all, and if so | ||
is there a way to subsume `dev-dependencies` into it & deprecate that feature? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference of this proposal to simply replacing the features table?
Also, the toml standard calls them arrays btw, and calls tables what you call objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging the table means that you don't need to repeat all of its elements. For example:
If we replaced the table, this wouln't be sensical because there would no longer be a
foo
feature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I think I got confused a bit, and thought you meant the features array (the features = ["foo", "bar", "baz"] thing), nvm.