Skip to content

Commit afd506f

Browse files
committed
rtic-macros: fix #[cfg] for hardware and software tasks
Disabling hardware and software tasks via `#[cfg]` flags was broken. Added test case to verify, and fixed codegen to output missing cfgs.
1 parent 22ac33a commit afd506f

File tree

7 files changed

+60
-8
lines changed

7 files changed

+60
-8
lines changed

rtic-macros/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top!
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed `#[cfg]` tags on hardware and software tasks.
13+
1014
## [v2.1.0] - 2024-02-27
1115

1216
### Added

rtic-macros/src/codegen/async_dispatchers.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
1616
let interrupts = &analysis.interrupts;
1717

1818
// Generate executor definition and priority in global scope
19-
for (name, _) in app.software_tasks.iter() {
19+
for (name, task) in app.software_tasks.iter() {
2020
let exec_name = util::internal_task_ident(name, "EXEC");
21+
let cfgs = &task.cfgs;
2122

2223
items.push(quote!(
24+
#(#cfgs)*
2325
#[allow(non_upper_case_globals)]
2426
static #exec_name: rtic::export::executor::AsyncTaskExecutorPtr =
2527
rtic::export::executor::AsyncTaskExecutorPtr::new();
@@ -47,15 +49,15 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
4749

4850
for name in channel.tasks.iter() {
4951
let exec_name = util::internal_task_ident(name, "EXEC");
52+
let task = &app.software_tasks[name];
53+
let cfgs = &task.cfgs;
5054
let from_ptr_n_args =
5155
util::from_ptr_n_args_ident(app.software_tasks[name].inputs.len());
5256

53-
// TODO: Fix cfg
54-
// let task = &app.software_tasks[name];
55-
// let cfgs = &task.cfgs;
56-
5757
stmts.push(quote!(
58+
#(#cfgs)*
5859
let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name);
60+
#(#cfgs)*
5961
exec.poll(|| {
6062
let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name);
6163
exec.set_pending();

rtic-macros/src/codegen/hardware_tasks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
7373

7474
if !task.is_extern {
7575
let attrs = &task.attrs;
76+
let cfgs = &task.cfgs;
7677
let context = &task.context;
7778
let stmts = &task.stmts;
7879
user_tasks.push(quote!(
7980
#(#attrs)*
81+
#(#cfgs)*
8082
#[allow(non_snake_case)]
8183
fn #name(#context: #name::Context) {
8284
use rtic::Mutex as _;

rtic-macros/src/codegen/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
2727

2828
let mut executor_allocations = Vec::new();
2929

30-
for (name, _) in app.software_tasks.iter() {
30+
for (name, task) in app.software_tasks.iter() {
3131
let exec_name = util::internal_task_ident(name, "EXEC");
3232
let new_n_args = util::new_n_args_ident(app.software_tasks[name].inputs.len());
33+
let cfgs = &task.cfgs;
3334

3435
executor_allocations.push(quote!(
36+
#(#cfgs)*
3537
let executor = ::core::mem::ManuallyDrop::new(rtic::export::executor::AsyncTaskExecutor::#new_n_args(#name));
36-
executors_size += ::core::mem::size_of_val(&executor);
37-
#exec_name.set_in_main(&executor);
38+
#(#cfgs)*
39+
{
40+
executors_size += ::core::mem::size_of_val(&executor);
41+
#exec_name.set_in_main(&executor);
42+
}
3843
));
3944
}
4045

rtic/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top!
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed `#[cfg]` tags on hardware and software tasks.
13+
1014
## [v2.1.1] - 2024-03-13
1115

1216
### Fixed

rtic/ci/expected/t-cfg-tasks.run

Whitespace-only changes.

rtic/examples/t-cfg-tasks.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! [compile-pass] check that `#[cfg]` attributes applied on tasks work
2+
3+
#![no_main]
4+
#![no_std]
5+
#![deny(warnings)]
6+
#![deny(unsafe_code)]
7+
#![deny(missing_docs)]
8+
9+
use panic_semihosting as _;
10+
11+
#[rtic::app(device = lm3s6965, dispatchers = [SSI0])]
12+
mod app {
13+
use cortex_m_semihosting::debug;
14+
15+
#[shared]
16+
struct Shared {}
17+
18+
#[local]
19+
struct Local {}
20+
21+
#[init]
22+
fn init(_: init::Context) -> (Shared, Local) {
23+
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
24+
25+
(Shared {}, Local {})
26+
}
27+
28+
#[cfg(feature = "feature_x")]
29+
#[task]
30+
async fn opt_sw_task(cx: opt_sw_task::Context) {}
31+
32+
#[cfg(feature = "feature_x")]
33+
#[task(binds = UART0)]
34+
fn opt_hw_task(cx: opt_hw_task::Context) {}
35+
}

0 commit comments

Comments
 (0)