Skip to content

Commit

Permalink
Random cleanups in non-checked packages (#2034)
Browse files Browse the repository at this point in the history
* Deduplicate feature check macros

* Re-enable rust-analyzer for most of the workspace

* Cargo fix

* Turn off defmt

* Only build xtask

* Clippy pls

* Fix CI

* Fix paths

* Always create doc directory first

* Revert r-a

* Update esp-hal-procmacros/src/lp_core.rs

Co-authored-by: Dominic Fischer <14130965+Dominaezzz@users.noreply.github.com>

---------

Co-authored-by: Dominic Fischer <14130965+Dominaezzz@users.noreply.github.com>
  • Loading branch information
bugadani and Dominaezzz authored Aug 30, 2024
1 parent fce510f commit 8e6411b
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/hil.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
run: cargo install cross

- name: Build xtasks
run: cross build --release --target ${{ matrix.host.rust-target }}
run: cross build --release --target ${{ matrix.host.rust-target }} -p xtask

- name: Upload artifact
uses: actions/upload-artifact@v4
Expand Down
67 changes: 37 additions & 30 deletions esp-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::{io::Write as _, process};

use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{parse_macro_input, punctuated::Punctuated, LitStr, Token};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

Expand Down Expand Up @@ -58,22 +59,10 @@ pub fn assert_unique_features(input: TokenStream) -> TokenStream {
.into_iter()
.collect::<Vec<_>>();

let pairs = unique_pairs(&features);
let unique_cfgs = pairs
.iter()
.map(|(a, b)| quote::quote! { all(feature = #a, feature = #b) });

let message = format!(
r#"
ERROR: expected exactly zero or one enabled feature from feature group:
{:?}
"#,
features.iter().map(|lit| lit.value()).collect::<Vec<_>>(),
);
let unique = impl_unique_features(&features, "exactly zero or one");

quote::quote! {
#[cfg(any(#(#unique_cfgs),*))]
::esp_build::error! { #message }
#unique
}
.into()
}
Expand All @@ -91,17 +80,10 @@ pub fn assert_used_features(input: TokenStream) -> TokenStream {
.into_iter()
.collect::<Vec<_>>();

let message = format!(
r#"
ERROR: expected at least one enabled feature from feature group:
{:?}
"#,
features.iter().map(|lit| lit.value()).collect::<Vec<_>>()
);
let used = impl_used_features(&features, "at least one");

quote::quote! {
#[cfg(not(any(#(feature = #features),*)))]
::esp_build::error! { #message }
#used
}
.into()
}
Expand All @@ -118,29 +100,54 @@ pub fn assert_unique_used_features(input: TokenStream) -> TokenStream {
.into_iter()
.collect::<Vec<_>>();

let pairs = unique_pairs(&features);
let unique = impl_unique_features(&features, "exactly one");
let used = impl_used_features(&features, "exactly one");

quote::quote! {
#unique
#used
}
.into()
}

// ----------------------------------------------------------------------------
// Helper Functions

fn impl_unique_features(features: &[LitStr], expectation: &str) -> impl ToTokens {
let pairs = unique_pairs(features);
let unique_cfgs = pairs
.iter()
.map(|(a, b)| quote::quote! { all(feature = #a, feature = #b) });

let message = format!(
r#"
ERROR: expected exactly one enabled feature from feature group:
ERROR: expected {expectation} enabled feature from feature group:
{:?}
"#,
features.iter().map(|lit| lit.value()).collect::<Vec<_>>(),
);

quote::quote! {
#[cfg(any(#(#unique_cfgs),*))]
::esp_build::error! { #message }
}
}

fn impl_used_features(features: &[LitStr], expectation: &str) -> impl ToTokens {
let message = format!(
r#"
ERROR: expected {expectation} enabled feature from feature group:
{:?}
"#,
features.iter().map(|lit| lit.value()).collect::<Vec<_>>()
);

quote::quote! {
#[cfg(any(any(#(#unique_cfgs),*), not(any(#(feature = #features),*))))]
#[cfg(not(any(#(feature = #features),*)))]
::esp_build::error! { #message }
}
.into()
}

// ----------------------------------------------------------------------------
// Helper Functions

// Adapted from:
// https://github.com/dtolnay/build-alert/blob/49d060e/src/lib.rs#L54-L93
fn do_alert(color: Color, input: TokenStream) -> TokenStream {
Expand Down
6 changes: 3 additions & 3 deletions esp-hal-procmacros/src/embassy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ pub(crate) mod main {
if !f.sig.generics.params.is_empty() {
ctxt.error_spanned_by(&f.sig, "main function must not be generic");
}
if !f.sig.generics.where_clause.is_none() {
if f.sig.generics.where_clause.is_some() {
ctxt.error_spanned_by(&f.sig, "main function must not have `where` clauses");
}
if !f.sig.abi.is_none() {
if f.sig.abi.is_some() {
ctxt.error_spanned_by(&f.sig, "main function must not have an ABI qualifier");
}
if !f.sig.variadic.is_none() {
if f.sig.variadic.is_some() {
ctxt.error_spanned_by(&f.sig, "main function must not be variadic");
}
match &f.sig.output {
Expand Down
25 changes: 11 additions & 14 deletions esp-hal-procmacros/src/enum_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@ impl Parse for MakeGpioEnumDispatchMacro {

let mut elements = vec![];

let mut stream = input.parse::<Group>()?.stream().into_iter();
let stream = input.parse::<Group>()?.stream().into_iter();
let mut element_name = String::new();
loop {
match stream.next() {
Some(v) => match v {
TokenTree::Ident(ident) => {
element_name = ident.to_string();
}
TokenTree::Literal(lit) => {
let index = lit.to_string().parse().unwrap();
elements.push((element_name.clone(), index));
}
_ => (),
},
None => break,
for v in stream {
match v {
TokenTree::Ident(ident) => {
element_name = ident.to_string();
}
TokenTree::Literal(lit) => {
let index = lit.to_string().parse().unwrap();
elements.push((element_name.clone(), index));
}
_ => (),
}
}

Expand Down
4 changes: 2 additions & 2 deletions esp-hal-procmacros/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn check_attr_whitelist(

'o: for attr in attrs {
for val in whitelist {
if eq(&attr, &val) {
if eq(attr, val) {
continue 'o;
}
}
Expand All @@ -35,7 +35,7 @@ pub(crate) fn check_attr_whitelist(
}
};

return Err(Error::new(attr.span(), &err_str).to_compile_error().into());
return Err(Error::new(attr.span(), err_str).to_compile_error().into());
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions esp-hal-procmacros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn ram(args: TokenStream, input: TokenStream) -> TokenStream {

let hal = proc_macro2::Ident::new(
if let Ok(FoundCrate::Name(ref name)) = crate_name("esp-hal") {
&name
name
} else {
"crate"
},
Expand Down Expand Up @@ -278,7 +278,7 @@ pub fn handler(args: TokenStream, input: TokenStream) -> TokenStream {

let root = Ident::new(
if let Ok(FoundCrate::Name(ref name)) = crate_name("esp-hal") {
&name
name
} else {
"crate"
},
Expand Down
48 changes: 24 additions & 24 deletions esp-hal-procmacros/src/lp_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
let mut res = String::from("__ULP_MAGIC_");
for &a in args {
let t = &a.ty;
let quoted = to_string(&t);
let quoted = to_string(t);
res.push_str(&quoted);
res.push_str("$");
res.push('$');
}

res
}

pub(crate) fn get_simplename(t: &Type) -> String {
String::from(match t {
Type::Path(p) => String::from(&p.path.segments.last().unwrap().ident.to_string()),
match t {
Type::Path(p) => p.path.segments.last().unwrap().ident.to_string(),
_ => String::new(),
})
}
}

pub(crate) fn extract_pin(ty: &Type) -> u8 {
Expand All @@ -49,7 +49,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
res = extract_pin(t);
}
GenericArgument::Const(c) => {
res = (&quote! { #c }.to_string()).parse().unwrap();
res = quote! { #c }.to_string().parse().unwrap();
}
_ => (),
}
Expand All @@ -68,11 +68,11 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
res.push_str(&segment.ident.to_string());

if let PathArguments::AngleBracketed(g) = &segment.arguments {
res.push_str("<");
res.push('<');
let mut pushed = false;
for arg in &g.args {
if pushed {
res.push_str(",");
res.push(',');
}

match arg {
Expand All @@ -87,7 +87,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
_ => (),
}
}
res.push_str(">");
res.push('>');
}
}

Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {
};

let hal_crate = if let Ok(FoundCrate::Name(ref name)) = hal_crate {
let ident = Ident::new(&name, Span::call_site().into());
let ident = Ident::new(name, Span::call_site().into());
quote!( #ident )
} else {
quote!(crate)
Expand Down Expand Up @@ -261,12 +261,14 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {

let mut sections: Vec<Section> = sections
.into_iter()
.filter(|section| match section.kind() {
SectionKind::Text
| SectionKind::ReadOnlyData
| SectionKind::Data
| SectionKind::UninitializedData => true,
_ => false,
.filter(|section| {
matches!(
section.kind(),
SectionKind::Text
| SectionKind::ReadOnlyData
| SectionKind::Data
| SectionKind::UninitializedData
)
})
.collect();
sections.sort_by(|a, b| a.address().partial_cmp(&b.address()).unwrap());
Expand All @@ -280,9 +282,8 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {

for section in sections {
if section.address() > last_address {
for _ in 0..(section.address() - last_address) {
binary.push(0);
}
let fill = section.address() - last_address;
binary.extend(std::iter::repeat(0).take(fill as usize));
}

binary.extend_from_slice(section.data().unwrap());
Expand All @@ -293,21 +294,20 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {
.symbols()
.find(|s| s.name().unwrap().starts_with("__ULP_MAGIC_"));

if let None = magic_symbol {
let magic_symbol = if let Some(magic_symbol) = magic_symbol {
magic_symbol.name().unwrap()
} else {
return Error::new(
Span::call_site().into(),
"Given file doesn't seem to be an LP/ULP core application.",
)
.to_compile_error()
.into();
}

let magic_symbol = magic_symbol.unwrap().name().unwrap();
};

let magic_symbol = magic_symbol.trim_start_matches("__ULP_MAGIC_");
let args: Vec<proc_macro2::TokenStream> = magic_symbol
.split("$")
.into_iter()
.map(|t| {
let t = if t.contains("OutputOpenDrain") {
t.replace("OutputOpenDrain", "LowPowerOutputOpenDrain")
Expand Down
8 changes: 4 additions & 4 deletions esp-lp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ description = "HAL for low-power RISC-V coprocessors found in ESP32 devices"
repository = "https://github.com/esp-rs/esp-hal"
license = "MIT OR Apache-2.0"

[lib]
bench = false
test = false

keywords = [
"embedded",
"embedded-hal",
Expand All @@ -24,6 +20,10 @@ categories = [
"no-std",
]

[lib]
bench = false
test = false

[dependencies]
cfg-if = "1.0.0"
document-features = "0.2.10"
Expand Down
Loading

0 comments on commit 8e6411b

Please sign in to comment.