From 79a367db167faaaa942baa90553987040fcc6498 Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Mon, 30 Dec 2024 10:28:18 +1100 Subject: [PATCH] Add `no_std` support to `bevy_state` (#17028) # Objective - Contributes to #15460 ## Solution - Added the following features: - `std` (default) - `portable-atomic` - `critical-section` ## Testing - CI ## Notes - `portable-atomic`, and `critical-section` are shortcuts to enable the relevant features in dependencies, making the usage of this crate on atomically challenged platforms possible and simpler. - This PR is blocked until #17027 is merged (as it depends on fixes for the `once!` macro). Once merged, the change-count for this PR should reduce. --- crates/bevy_state/Cargo.toml | 64 ++++++++++++++++--- crates/bevy_state/src/app.rs | 7 +- crates/bevy_state/src/commands.rs | 2 +- crates/bevy_state/src/lib.rs | 4 ++ crates/bevy_state/src/state_scoped_events.rs | 1 + tools/ci/src/commands/compile_check_no_std.rs | 8 +++ 6 files changed, 73 insertions(+), 13 deletions(-) diff --git a/crates/bevy_state/Cargo.toml b/crates/bevy_state/Cargo.toml index 30ddcfd6ed..98f14fe7d6 100644 --- a/crates/bevy_state/Cargo.toml +++ b/crates/bevy_state/Cargo.toml @@ -8,23 +8,67 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT OR Apache-2.0" keywords = ["bevy"] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [features] -default = ["bevy_reflect", "bevy_app", "bevy_hierarchy"] -bevy_reflect = ["dep:bevy_reflect", "bevy_ecs/bevy_reflect"] -bevy_app = ["dep:bevy_app"] +default = ["std", "bevy_reflect", "bevy_app", "bevy_hierarchy"] + +# Functionality + +## Adds runtime reflection support using `bevy_reflect`. +bevy_reflect = [ + "dep:bevy_reflect", + "bevy_ecs/bevy_reflect", + "bevy_hierarchy?/reflect", + "bevy_app?/bevy_reflect", +] + +## Adds integration with the `bevy_app` plugin API. +bevy_app = ["dep:bevy_app", "bevy_hierarchy?/bevy_app"] + +## Adds integration with the `bevy_hierarchy` `Parent` and `Children` API. bevy_hierarchy = ["dep:bevy_hierarchy"] +# Platform Compatibility + +## Allows access to the `std` crate. Enabling this feature will prevent compilation +## on `no_std` targets, but provides access to certain additional features on +## supported platforms. +std = [ + "bevy_ecs/std", + "bevy_utils/std", + "bevy_reflect?/std", + "bevy_app?/std", + "bevy_hierarchy?/std", +] + +## `critical-section` provides the building blocks for synchronization primitives +## on all platforms, including `no_std`. +critical-section = [ + "bevy_ecs/critical-section", + "bevy_utils/critical-section", + "bevy_app?/critical-section", +] + +## `portable-atomic` provides additional platform support for atomic types and +## operations, even on targets without native support. +portable-atomic = [ + "bevy_ecs/portable-atomic", + "bevy_utils/portable-atomic", + "bevy_app?/portable-atomic", +] + [dependencies] -bevy_ecs = { path = "../bevy_ecs", version = "0.15.0-dev" } +# bevy +bevy_ecs = { path = "../bevy_ecs", version = "0.15.0-dev", default-features = false } bevy_state_macros = { path = "macros", version = "0.15.0-dev" } -bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev" } -bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", optional = true } -bevy_app = { path = "../bevy_app", version = "0.15.0-dev", optional = true } -bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.15.0-dev", optional = true } +bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev", default-features = false } +bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", default-features = false, optional = true } +bevy_app = { path = "../bevy_app", version = "0.15.0-dev", default-features = false, optional = true } +bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.15.0-dev", default-features = false, optional = true } variadics_please = "1.1" +# other +log = { version = "0.4", default-features = false } + [lints] workspace = true diff --git a/crates/bevy_state/src/app.rs b/crates/bevy_state/src/app.rs index c1aaf09333..b00c60de53 100644 --- a/crates/bevy_state/src/app.rs +++ b/crates/bevy_state/src/app.rs @@ -1,6 +1,7 @@ use bevy_app::{App, MainScheduleOrder, Plugin, PreStartup, PreUpdate, SubApp}; use bevy_ecs::{event::Events, schedule::IntoSystemConfigs, world::FromWorld}; -use bevy_utils::{tracing::warn, warn_once}; +use bevy_utils::once; +use log::warn; use crate::{ state::{ @@ -87,7 +88,9 @@ pub trait AppExtStates { /// Separate function to only warn once for all state installation methods. fn warn_if_no_states_plugin_installed(app: &SubApp) { if !app.is_plugin_added::() { - warn_once!("States were added to the app, but `StatesPlugin` is not installed."); + once!(warn!( + "States were added to the app, but `StatesPlugin` is not installed." + )); } } diff --git a/crates/bevy_state/src/commands.rs b/crates/bevy_state/src/commands.rs index 036e35d603..d9da362b62 100644 --- a/crates/bevy_state/src/commands.rs +++ b/crates/bevy_state/src/commands.rs @@ -1,5 +1,5 @@ use bevy_ecs::{system::Commands, world::World}; -use bevy_utils::tracing::debug; +use log::debug; use crate::state::{FreelyMutableState, NextState}; diff --git a/crates/bevy_state/src/lib.rs b/crates/bevy_state/src/lib.rs index 796516f5a6..572330de4c 100644 --- a/crates/bevy_state/src/lib.rs +++ b/crates/bevy_state/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + //! In Bevy, states are app-wide interdependent, finite state machines that are generally used to model the large scale structure of your program: whether a game is paused, if the player is in combat, if assets are loaded and so on. //! //! This module provides 3 distinct types of state, all of which implement the [`States`](state::States) trait: @@ -36,6 +38,8 @@ )] #![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))] +extern crate alloc; + #[cfg(feature = "bevy_app")] /// Provides [`App`](bevy_app::App) and [`SubApp`](bevy_app::SubApp) with state installation methods pub mod app; diff --git a/crates/bevy_state/src/state_scoped_events.rs b/crates/bevy_state/src/state_scoped_events.rs index fbeafe5453..1906370ad3 100644 --- a/crates/bevy_state/src/state_scoped_events.rs +++ b/crates/bevy_state/src/state_scoped_events.rs @@ -1,3 +1,4 @@ +use alloc::vec::Vec; use core::marker::PhantomData; use bevy_app::{App, SubApp}; diff --git a/tools/ci/src/commands/compile_check_no_std.rs b/tools/ci/src/commands/compile_check_no_std.rs index 073daacb47..ac8718ac5d 100644 --- a/tools/ci/src/commands/compile_check_no_std.rs +++ b/tools/ci/src/commands/compile_check_no_std.rs @@ -126,6 +126,14 @@ impl Prepare for CompileCheckNoStdCommand { "Please fix compiler errors in output above for bevy_input no_std compatibility.", )); + commands.push(PreparedCommand::new::( + cmd!( + sh, + "cargo check -p bevy_state --no-default-features --features bevy_reflect,bevy_app,bevy_hierarchy --target {target}" + ), + "Please fix compiler errors in output above for bevy_state no_std compatibility.", + )); + commands } }