From 094c7b5b594190dfef9378003649b1827be9d170 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 28 Apr 2023 22:17:33 +0900 Subject: [PATCH] Add require-cas feature Provide better error message if the end user forgets to use single-core cfg or CS feature. --- Cargo.toml | 3 +++ portable-atomic-util/Cargo.toml | 3 ++- src/lib.rs | 31 +++++++++++++++++++++++++++++-- tools/build.sh | 10 +++++++--- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 72e542d8..1c15e004 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,9 @@ float = [] # Use `std`. std = [] +# Emit compile error if atomic CAS is not available. +require-cas = [] + # Note: serde and critical-section are public dependencies. [dependencies] # Implements serde::{Serialize,Deserialize} for atomic types. diff --git a/portable-atomic-util/Cargo.toml b/portable-atomic-util/Cargo.toml index aff03fa1..fbdfe3cf 100644 --- a/portable-atomic-util/Cargo.toml +++ b/portable-atomic-util/Cargo.toml @@ -39,4 +39,5 @@ alloc = [] # generic = [] [dependencies] -portable-atomic = { version = "1", path = "..", default-features = false } +# TODO: use version 1.3 once portable-atomic with require-cas feature released. +portable-atomic = { version = "1", path = "..", default-features = false, features = ["require-cas"] } diff --git a/src/lib.rs b/src/lib.rs index d390288d..c8d52d91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -365,8 +365,8 @@ compile_error!( )) )] compile_error!( - "cfg(portable_atomic_unsafe_assume_single_core) does not compatible with this target; \ - if you need cfg(portable_atomic_unsafe_assume_single_core) support for this target, \ + "cfg(portable_atomic_unsafe_assume_single_core) does not compatible with this target;\n\ + if you need cfg(portable_atomic_unsafe_assume_single_core) support for this target,\n\ please submit an issue at " ); @@ -402,6 +402,33 @@ compile_error!( "you may not enable feature `critical-section` and cfg(portable_atomic_unsafe_assume_single_core) at the same time" ); +#[cfg(feature = "require-cas")] +#[cfg_attr( + portable_atomic_no_cfg_target_has_atomic, + cfg(not(any( + not(portable_atomic_no_atomic_cas), + portable_atomic_unsafe_assume_single_core, + feature = "critical-section", + target_arch = "avr", + target_arch = "msp430", + ))) +)] +#[cfg_attr( + not(portable_atomic_no_cfg_target_has_atomic), + cfg(not(any( + target_has_atomic = "ptr", + portable_atomic_unsafe_assume_single_core, + feature = "critical-section", + target_arch = "avr", + target_arch = "msp430", + ))) +)] +compile_error!( + "dependents require atomic CAS but not available on this target by default;\n\ + consider using portable_atomic_unsafe_assume_single_core cfg or critical-section feature.\n\ + see for more." +); + #[cfg(any(test, feature = "std"))] extern crate std; diff --git a/tools/build.sh b/tools/build.sh index ff4b362c..2813437f 100755 --- a/tools/build.sh +++ b/tools/build.sh @@ -427,9 +427,13 @@ build() { else echo "target '${target}' requires asm to implement atomic CAS (skipped build with --cfg portable_atomic_unsafe_assume_single_core)" fi - # portable-atomic-util uses atomic CAS, so doesn't work on - # this target without portable_atomic_unsafe_assume_single_core cfg. - args+=(--exclude portable-atomic-util) + # portable-atomic-util crate and portable-atomic's require-cas feature require atomic CAS, + # so doesn't work on this target without portable_atomic_unsafe_assume_single_core cfg + # or critical-section feature. + args+=( + --exclude portable-atomic-util + --exclude-features require-cas + ) fi fi fi