Skip to content

Commit f3ec3c0

Browse files
cartatlv24
andauthored
Cargo Feature Collections (#21472)
## Objective Users of the `bevy` crate currently live one of two lifestyles: 1. Use all of Bevy's default features, potentially compiling many features you don't need or don't want. If there is a feature you _cannot have_ in your app, you must move on to option (2) 2. Disable Bevy's default features, and compose the complete list of features yourself. Living in the world of (2) is an exercise in frustration, as the list of required features to accomplish a given task changes regularly across releases as we add and change features. This is an _expert level_ task that requires intimate knowledge of engine internals to get right. Even I, Bevy's lead developer, would struggle here. To the point that I would never voluntarily choose to disable Bevy's default features. Most games/apps are 2D-only, 3D-only, or UI-only and don't require the full set of features. We cannot currently in good conscience recommend that those developers live in the "no default features" world. That is a fast track to pain, suffering, and perhaps a quick exit from the Bevy ecosystem. The same problem exists for developers that want to build their own Bevy renderer or replace Bevy UI with their own framework. Once again, we _cannot_ recommend that those developers manage every Bevy feature themselves. Fixes: #21369 Alternative to: #20741 #21236 ## Solution Define a "standalone" set of features that enable Bevy developers to disable default features, then opt in to the funtionality they want. The "default" `bevy` feature set is now just: ```toml default = ["2d", "3d", "ui"] ``` This enables developers to select only the features they need. For example, a UI-only app would look like this: ```toml bevy = { version = "0.17", default-features = false, features = [ "ui" ] } ``` "2d", "3d", and "ui" each contain the "full" Bevy experience for that domain. This also includes: - `default_platform`: the default "platform support" features (see docs) - `default_app`: the default "app framework" features (see docs) For developers that do not want the default bevy render / platform / app functionality, this breaks down further into: - `common_api`: common / core backend-less user-facing Bevy render api - `2d_api`: common / core backend-less user-facing Bevy 2d api - `3d_api`: common / core backend-less user-facing Bevy 3d api - `ui_api`: common / core backend-less user-facing Bevy ui api (and many others) I've also added the `dev` feature to this PR, which enables the recommended "dev only" features like dynamic linking, asset watching / hot reloading, and dev / debug tools. Including dynamic linking is a bit controversial here given that it doesn't work everywhere. But I'd like to aggressively push people into the dynamic linking workflow wherever possible, as developing without it is signficantly worse. And removing a single `dev` feature is a much simpler release workflow. --------- Co-authored-by: atlv <[email protected]>
1 parent 0b3a340 commit f3ec3c0

File tree

7 files changed

+326
-124
lines changed

7 files changed

+326
-124
lines changed

Cargo.toml

Lines changed: 126 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -125,65 +125,149 @@ unsafe_op_in_unsafe_fn = "warn"
125125
unused_qualifications = "warn"
126126

127127
[features]
128-
default = [
129-
"std",
128+
default = ["2d", "3d", "ui"]
129+
130+
# PROFILE: The default 2D Bevy experience. This includes the core Bevy framework, 2D functionality, Bevy UI, scenes, audio, and picking.
131+
2d = [
132+
"default_app",
133+
"default_platform",
134+
"2d_api",
135+
"2d_bevy_render",
136+
"ui",
137+
"scene",
138+
"audio",
139+
"picking",
140+
]
141+
142+
# PROFILE: The default 3D Bevy experience. This includes the core Bevy framework, 3D functionality, Bevy UI, scenes, audio, and picking.
143+
3d = [
144+
"default_app",
145+
"default_platform",
146+
"3d_api",
147+
"3d_bevy_render",
148+
"ui",
149+
"scene",
150+
"audio",
151+
"picking",
152+
]
153+
154+
# PROFILE: The default Bevy UI experience. This includes the core Bevy framework, Bevy UI, scenes, audio, and picking.
155+
ui = [
156+
"default_app",
157+
"default_platform",
158+
"ui_api",
159+
"ui_bevy_render",
160+
"scene",
161+
"audio",
162+
"picking",
163+
]
164+
165+
# COLLECTION: Enable this feature during development to improve the development experience. This adds features like asset hot-reloading and debugging tools. This should not be enabled for published apps!
166+
dev = [
167+
"debug", # TODO: rename this to something more specific ... this is a "debug ECS names" feature
168+
"bevy_dev_tools",
169+
"file_watcher",
170+
]
171+
172+
# COLLECTION: Features used to build audio Bevy apps.
173+
audio = ["bevy_audio", "vorbis"]
174+
175+
# COLLECTION: Features used to compose Bevy scenes.
176+
scene = ["bevy_scene"]
177+
178+
# COLLECTION: Enables picking functionality
179+
picking = ["bevy_picking", "mesh_picking", "sprite_picking", "ui_picking"]
180+
181+
# COLLECTION: The core pieces that most apps need. This serves as a baseline feature set for other higher level feature collections (such as "2d" and "3d"). It is also useful as a baseline feature set for scenarios like headless apps that require no rendering (ex: command line tools, servers, etc).
182+
default_app = [
130183
"async_executor",
184+
"bevy_asset",
185+
"bevy_input_focus",
186+
"bevy_log",
187+
"bevy_state",
188+
"bevy_window",
189+
"custom_cursor",
190+
"reflect_auto_register",
191+
]
192+
193+
# COLLECTION: These are platform support features, such as OS support/features, windowing and input backends, etc.
194+
default_platform = [
195+
"std",
131196
"android-game-activity",
132197
"android_shared_stdcxx",
133-
"gltf_animation",
134-
"bevy_asset",
135-
"bevy_audio",
136-
"bevy_color",
137-
"bevy_core_pipeline",
138-
"bevy_post_process",
139-
"bevy_anti_alias",
140198
"bevy_gilrs",
199+
"bevy_winit",
200+
"default_font",
201+
"multi_threaded",
202+
"webgl2",
203+
"x11",
204+
"wayland",
205+
"sysinfo_plugin",
206+
]
207+
208+
# COLLECTION: Default scene definition features. Note that this does not include an actual renderer, such as bevy_render (Bevy's default render backend).
209+
common_api = [
210+
"bevy_animation",
211+
"bevy_camera",
212+
"bevy_color",
141213
"bevy_gizmos",
142-
"bevy_gltf",
143-
"bevy_input_focus",
144-
"bevy_log",
145-
"bevy_pbr",
146-
"bevy_picking",
147-
"bevy_render",
148-
"bevy_scene",
149214
"bevy_image",
150215
"bevy_mesh",
151-
"bevy_mikktspace",
152-
"bevy_camera",
153-
"bevy_light",
154216
"bevy_shader",
155-
"bevy_sprite",
156-
"bevy_sprite_render",
157-
"bevy_state",
158217
"bevy_text",
159-
"bevy_ui",
160-
"bevy_ui_render",
161-
"bevy_window",
162-
"bevy_winit",
163-
"custom_cursor",
164-
"default_font",
165218
"hdr",
219+
"png",
220+
]
221+
222+
# COLLECTION: Features used to build 2D Bevy apps (does not include a render backend). You generally don't need to worry about this unless you are using a custom renderer.
223+
2d_api = ["common_api", "bevy_sprite"]
224+
225+
# COLLECTION: Bevy's built-in 2D renderer, built on top of `bevy_render`
226+
2d_bevy_render = [
227+
"2d_api",
228+
"bevy_render",
229+
"bevy_core_pipeline",
230+
"bevy_post_process",
231+
"bevy_sprite_render",
232+
]
233+
234+
# COLLECTION: Features used to build 3D Bevy apps (does not include a render backend). You generally don't need to worry about this unless you are using a custom renderer.
235+
3d_api = [
236+
"common_api",
237+
"bevy_light",
238+
"bevy_mikktspace",
166239
"ktx2",
167-
"mesh_picking",
168-
"morph",
169240
"morph_animation",
170-
"multi_threaded",
171-
"png",
172-
"reflect_auto_register",
241+
"morph",
173242
"smaa_luts",
174-
"sprite_picking",
175-
"sysinfo_plugin",
176243
"tonemapping_luts",
177-
"ui_picking",
178-
"vorbis",
179-
"webgl2",
180-
"x11",
181-
"wayland",
182-
"debug",
183244
"zstd_rust",
184245
]
185246

186-
# Recommended defaults for no_std applications
247+
# COLLECTION: Bevy's built-in 3D renderer, built on top of `bevy_render`
248+
3d_bevy_render = [
249+
"3d_api",
250+
"bevy_render",
251+
"bevy_core_pipeline",
252+
"bevy_anti_alias",
253+
"bevy_gltf",
254+
"bevy_pbr",
255+
"bevy_post_process",
256+
"gltf_animation",
257+
]
258+
259+
# COLLECTION: Features used to build UI Bevy apps (does not include a render backend). You generally don't need to worry about this unless you are using a custom renderer.
260+
ui_api = ["default_app", "common_api", "bevy_ui"]
261+
262+
# COLLECTION: Bevy's built-in UI renderer, built on top of `bevy_render`
263+
ui_bevy_render = [
264+
"ui_api",
265+
"bevy_render",
266+
"bevy_core_pipeline",
267+
"bevy_ui_render",
268+
]
269+
270+
# COLLECTION: Recommended defaults for no_std applications
187271
default_no_std = ["libm", "critical-section", "bevy_color", "bevy_state"]
188272

189273
# Provides an implementation for picking meshes

crates/bevy_internal/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,10 @@ bevy_picking = ["dep:bevy_picking"]
321321
mesh_picking = ["bevy_picking", "bevy_picking/mesh_picking"]
322322

323323
# Provides a sprite picking backend
324-
sprite_picking = ["bevy_picking", "bevy_sprite/bevy_picking"]
324+
sprite_picking = ["bevy_picking", "bevy_sprite?/bevy_picking"]
325325

326326
# Provides a UI picking backend
327-
ui_picking = ["bevy_picking", "bevy_ui/bevy_picking"]
327+
ui_picking = ["bevy_picking", "bevy_ui?/bevy_picking"]
328328

329329
# Provides a UI debug overlay
330330
bevy_ui_debug = ["bevy_ui_render?/bevy_ui_debug"]

docs-template/features.md.tpl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,42 @@
44

55
## Cargo Features
66

7-
Bevy exposes many features to customize the engine. Enabling them add functionalities but often come at the cost of longer compilation times and extra dependencies.
7+
Bevy exposes many Cargo features to customize the engine. Enabling them adds functionality but may come at the cost of longer compilation times
8+
and extra dependencies.
89

9-
### Default Features
10+
### Profiles
1011

11-
The default feature set enables most of the expected features of a game engine, like rendering in both 2D and 3D, asset loading, audio and UI. To help reduce compilation time, consider disabling default features and enabling only those you need.
12+
"Profiles" are high-level groups of cargo features that provide the full Bevy experience, but scoped to a specific domain.
13+
These exist to be paired with `default-features = false`, enabling compiling only the subset of Bevy that you need.
14+
This can cut down compile times and shrink your final binary size.
1215

13-
|feature name|description|
16+
For example, you can compile only the "2D" Bevy features (without the 3D features) like this:
17+
18+
```toml
19+
bevy = { version = "0.17", default-features = false, features = ["2d"] }
20+
```
21+
22+
|Profile|Description|
23+
|-|-|
24+
{% for feature in features %}{% if feature.is_profile %}|{{ feature.name }}|{{ feature.description }}|
25+
{% endif %}{% endfor %}
26+
By default, the `bevy` crate enables the {% for feature in features %}{% if feature.is_default %}`{{ feature.name }}`{% endif %}{% endfor %} features.
27+
28+
### Collections
29+
30+
"Collections" are mid-level groups of cargo features. These are used to compose the high-level "profiles". If the default profiles don'templated
31+
suit your use case (ex: you want to use a custom renderer, you want to build a "headless" app, you want to target no_std, etc), then you can use these
32+
collections to build your own "profile" equivalent, without needing to manually manage _every single_ feature.
33+
34+
|Collection|Description|
1435
|-|-|
15-
{% for feature in features %}{% if feature.is_default %}|{{ feature.name }}|{{ feature.description }}|
36+
{% for feature in features %}{% if feature.is_collection %}|{{ feature.name }}|{{ feature.description }}|
1637
{% endif %}{% endfor %}
17-
### Optional Features
38+
### Feature List
39+
40+
This is the complete `bevy` cargo feature list, without "profiles" or "collections" (sorted by name):
1841

19-
|feature name|description|
42+
|Feature|Description|
2043
|-|-|
21-
{% for feature in features %}{% if feature.is_default == False %}|{{ feature.name }}|{{ feature.description }}|
44+
{% for feature in sorted_features %}{% if feature.is_collection == False and feature.is_profile == False %}|{{ feature.name }}|{{ feature.description }}|
2245
{% endif %}{% endfor %}

0 commit comments

Comments
 (0)