Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check device selected in build.rs #502

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Replace UB code by a legitimate pointer access [#480]
- Fix flash error flag clearing [#489]
- Clarify README for windows users [#496]
- Check "device selected" in `build.rs`

### Added

Expand Down
13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,12 @@ usb-device = "0.2.8"
usbd-serial = "0.1.1"

[features]
device-selected = []
doc = []
# deprecated feature
rt = []
stm32f100 = ["stm32f1/stm32f100", "device-selected"]
stm32f101 = ["stm32f1/stm32f101", "device-selected"]
stm32f103 = ["stm32f1/stm32f103", "device-selected", "has-can", "stm32-usbd"]
stm32f105 = ["stm32f1/stm32f107", "device-selected", "connectivity"]
stm32f107 = ["stm32f1/stm32f107", "device-selected", "connectivity"]
stm32f100 = ["stm32f1/stm32f100"]
stm32f101 = ["stm32f1/stm32f101"]
stm32f103 = ["stm32f1/stm32f103", "has-can", "stm32-usbd"]
stm32f105 = ["stm32f1/stm32f107", "connectivity"]
stm32f107 = ["stm32f1/stm32f107", "connectivity"]

# Devices with 64 or 128 Kb ROM
medium = []
Expand Down
36 changes: 36 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::env;

#[derive(Clone, Copy, Debug)]
enum GetOneError {
None,
Multiple,
}

trait IteratorExt: Iterator {
fn get_one(self) -> Result<Self::Item, GetOneError>;
}

impl<T: Iterator> IteratorExt for T {
fn get_one(mut self) -> Result<Self::Item, GetOneError> {
match (self.next(), self.next()) {
(Some(res), None) => Ok(res),
(None, _) => Err(GetOneError::None),
_ => Err(GetOneError::Multiple),
}
}
}

fn main() {
let _chip_name = match env::vars()
.map(|(a, _)| a)
.filter(|x| x.starts_with("CARGO_FEATURE_STM32F1"))
.get_one()
{
Ok(x) => x,
Err(GetOneError::None) => panic!("No stm32xx Cargo feature enabled"),
Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"),
}
.strip_prefix("CARGO_FEATURE_")
.unwrap()
.to_ascii_lowercase();
}
28 changes: 3 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ compile_error!(
"Multiple targets specified. Only a single `--features <target-name>` can be specified."
);

#[cfg(feature = "device-selected")]
pub use embedded_hal as hal;
#[cfg(feature = "device-selected")]
pub use embedded_hal_02 as hal_02;

#[cfg(feature = "stm32f100")]
Expand All @@ -130,52 +128,32 @@ pub use stm32f1::stm32f103 as pac;
#[cfg(any(feature = "stm32f105", feature = "stm32f107"))]
pub use stm32f1::stm32f107 as pac;

#[cfg(feature = "device-selected")]
pub mod adc;
#[cfg(feature = "device-selected")]
pub mod afio;
#[cfg(feature = "device-selected")]
pub mod backup_domain;
#[cfg(feature = "device-selected")]
pub mod bb;
#[cfg(all(feature = "device-selected", feature = "has-can"))]
#[cfg(feature = "has-can")]
pub mod can;
#[cfg(feature = "device-selected")]
pub mod crc;
#[cfg(all(feature = "device-selected", feature = "has-dac"))]
#[cfg(feature = "has-dac")]
pub mod dac;
#[cfg(feature = "device-selected")]
pub mod dma;
#[cfg(feature = "device-selected")]
pub mod flash;
#[cfg(feature = "device-selected")]
pub mod gpio;
#[cfg(feature = "device-selected")]
pub mod i2c;
#[cfg(feature = "device-selected")]
pub mod prelude;
#[cfg(feature = "device-selected")]
pub mod qei;
#[cfg(feature = "device-selected")]
pub mod rcc;
#[cfg(feature = "device-selected")]
pub mod rtc;
#[cfg(feature = "device-selected")]
pub mod serial;
#[cfg(feature = "device-selected")]
pub mod spi;
#[cfg(feature = "device-selected")]
pub mod time;
#[cfg(feature = "device-selected")]
pub mod timer;
#[cfg(all(feature = "device-selected", feature = "stm32-usbd"))]
#[cfg(feature = "stm32-usbd")]
pub mod usb;
#[cfg(feature = "device-selected")]
pub mod watchdog;

#[cfg(feature = "device-selected")]
mod sealed {
pub trait Sealed {}
}
#[cfg(feature = "device-selected")]
use sealed::Sealed;
Loading