Skip to content

Commit

Permalink
Compile errors for some invalid feature combinations (atsamd-rs#469)
Browse files Browse the repository at this point in the history
Emit compiler errors if no target chip is specified. Add the `library` Cargo feature to explicitly compile with no specified chip when necessary.

* Compile error if USB is requested but unsupported

* Compile error if no device feature is enabled

* Add `library` feature, reword errors and comments
  • Loading branch information
ianrrees authored Aug 12, 2021
1 parent 62f9dfc commit cdfe4b9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-bsp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ jobs:
run: |
set -ex
cd hal
cargo build
cargo build --features="library"
14 changes: 12 additions & 2 deletions hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,24 @@ optional = true

[features]
default = ["unproven"]
# This section lists our feature name to dependency mapping. This are separated

# This section lists our feature name to dependency mapping. This is separated
# out so that the board support crates can reference a single feature name to
# select the appropriate chip, and to keep that separate from the dependency name
# for the `rt` feature below, which has to list out all possible optional deps.
# If we simply used feature names that matched the dependency names, enabling the
# rt feature would link all possible parts in and cause a linker error due to
# the high degree of similar symbols present in the various parts.
device = [] # Should be included if we are building for a specific target device.

# The `device` feature is a dependency of each specific target device. It
# should not be enabled directly, as that could lead to cryptic build errors.
# The `library` feature can be enabled to use atsamd-hal without selecting a
# specific target device.
device = []

# Bypasses compile-time checks that a specific device is enabled.
library = []

samd11 = ["device"] # Convenience feature for shared configuration of samd11 chips.
samd11c = ["atsamd11c", "samd11"]
samd11c-rt = ["samd11c", "atsamd11c/rt"]
Expand Down
16 changes: 16 additions & 0 deletions hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ pub use paste;

pub mod typelevel;

#[cfg(not(any(feature = "library", feature = "device")))]
compile_error!(
"The HAL is usually built for a specific target device, selected using a \
feature. If atsamd-hal is being built as a library, bypass this check by \
specifying the `library` feature"
);

#[cfg(feature = "samd11c")]
pub use atsamd11c as target_device;

Expand Down Expand Up @@ -98,6 +105,15 @@ pub mod timer_traits;
#[cfg(all(feature = "unproven", feature = "dma"))]
pub mod dmac;

#[cfg(all(feature = "usb", feature = "samd11"))]
compile_error!("'usb' is enabled, but USB isn't supported on SAMD11");

#[cfg(all(
feature = "usb",
not(any(feature = "samd21", feature = "min-samd51g", feature = "library"))
))]
compile_error!("The 'usb' feature is enabled, but not a chip with USB support");

#[cfg(any(feature = "samd11", feature = "samd21"))]
pub mod thumbv6m;
#[cfg(any(feature = "samd11", feature = "samd21"))]
Expand Down

0 comments on commit cdfe4b9

Please sign in to comment.