Skip to content

Commit

Permalink
Add require-cas feature
Browse files Browse the repository at this point in the history
Provide better error message if the end user forgets to use single-core
cfg or CS feature.
  • Loading branch information
taiki-e committed Apr 28, 2023
1 parent be5f5c3 commit 2d2027a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion portable-atomic-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
31 changes: 29 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/taiki-e/portable-atomic>"
);

Expand Down Expand Up @@ -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 <https://docs.rs/portable-atomic/latest/portable_atomic/#optional-cfg> for more."
);

#[cfg(any(test, feature = "std"))]
extern crate std;

Expand Down
10 changes: 7 additions & 3 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d2027a

Please sign in to comment.