2022-02-12 00:11:15 +01:00
|
|
|
use darling::FromMeta;
|
|
|
|
use proc_macro2::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
|
|
|
|
use crate::util::ctxt::Ctxt;
|
|
|
|
|
|
|
|
#[derive(Debug, FromMeta)]
|
2022-08-17 18:49:55 +02:00
|
|
|
struct Args {}
|
2022-02-12 00:11:15 +01:00
|
|
|
|
|
|
|
pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> {
|
2022-08-17 15:03:12 +02:00
|
|
|
#[allow(unused_variables)]
|
2022-02-12 00:11:15 +01:00
|
|
|
let args = Args::from_list(&args).map_err(|e| e.write_errors())?;
|
|
|
|
|
|
|
|
let fargs = f.sig.inputs.clone();
|
|
|
|
|
|
|
|
let ctxt = Ctxt::new();
|
|
|
|
|
|
|
|
if f.sig.asyncness.is_none() {
|
2022-08-17 18:49:55 +02:00
|
|
|
ctxt.error_spanned_by(&f.sig, "main function must be async");
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
if !f.sig.generics.params.is_empty() {
|
2022-08-17 18:49:55 +02:00
|
|
|
ctxt.error_spanned_by(&f.sig, "main function must not be generic");
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:49:55 +02:00
|
|
|
if fargs.len() != 1 {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "main function must have 1 argument: the spawner.");
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctxt.check()?;
|
|
|
|
|
|
|
|
let f_body = f.block;
|
|
|
|
|
|
|
|
#[cfg(feature = "wasm")]
|
|
|
|
let main = quote! {
|
|
|
|
#[wasm_bindgen::prelude::wasm_bindgen(start)]
|
|
|
|
pub fn main() -> Result<(), wasm_bindgen::JsValue> {
|
2022-08-22 15:51:44 +02:00
|
|
|
static EXECUTOR: ::embassy_executor::_export::StaticCell<::embassy_executor::Executor> = ::embassy_executor::_export::StaticCell::new();
|
|
|
|
let executor = EXECUTOR.init(::embassy_executor::Executor::new());
|
2022-02-12 00:11:15 +01:00
|
|
|
|
|
|
|
executor.start(|spawner| {
|
|
|
|
spawner.spawn(__embassy_main(spawner)).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-25 20:10:11 +02:00
|
|
|
#[cfg(all(feature = "std", not(feature = "wasm"), not(feature = "riscv")))]
|
2022-02-12 00:11:15 +01:00
|
|
|
let main = quote! {
|
|
|
|
fn main() -> ! {
|
2022-08-17 23:40:16 +02:00
|
|
|
let mut executor = ::embassy_executor::Executor::new();
|
2022-02-12 00:11:15 +01:00
|
|
|
let executor = unsafe { __make_static(&mut executor) };
|
|
|
|
|
|
|
|
executor.run(|spawner| {
|
|
|
|
spawner.must_spawn(__embassy_main(spawner));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-25 20:10:11 +02:00
|
|
|
#[cfg(all(not(feature = "std"), not(feature = "wasm"), not(feature = "riscv")))]
|
2022-08-17 18:49:55 +02:00
|
|
|
let main = quote! {
|
|
|
|
#[cortex_m_rt::entry]
|
|
|
|
fn main() -> ! {
|
2022-08-17 23:40:16 +02:00
|
|
|
let mut executor = ::embassy_executor::Executor::new();
|
2022-08-17 18:49:55 +02:00
|
|
|
let executor = unsafe { __make_static(&mut executor) };
|
2022-09-25 20:10:11 +02:00
|
|
|
executor.run(|spawner| {
|
|
|
|
spawner.must_spawn(__embassy_main(spawner));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2022-08-17 18:49:55 +02:00
|
|
|
|
2022-09-25 20:10:11 +02:00
|
|
|
#[cfg(all(not(feature = "std"), not(feature = "wasm"), feature = "riscv"))]
|
|
|
|
let main = quote! {
|
|
|
|
#[riscv_rt::entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
let mut executor = ::embassy_executor::Executor::new();
|
|
|
|
let executor = unsafe { __make_static(&mut executor) };
|
2022-08-17 18:49:55 +02:00
|
|
|
executor.run(|spawner| {
|
|
|
|
spawner.must_spawn(__embassy_main(spawner));
|
2022-06-12 22:15:44 +02:00
|
|
|
})
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = quote! {
|
2022-08-17 15:03:12 +02:00
|
|
|
#[::embassy_executor::task()]
|
2022-02-12 00:11:15 +01:00
|
|
|
async fn __embassy_main(#fargs) {
|
|
|
|
#f_body
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn __make_static<T>(t: &mut T) -> &'static mut T {
|
|
|
|
::core::mem::transmute(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
#main
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|