From bb03fb71bd910ca86667d197d36c23e7dabe4436 Mon Sep 17 00:00:00 2001
From: David Brown <david.brown@linaro.org>
Date: Fri, 18 Oct 2024 13:43:49 -0600
Subject: [PATCH] samples: blinky: Domonstrate conditional DT compilation

Show how an application can be conditional based on the presence of a
node in the devicetree.

Signed-off-by: David Brown <david.brown@linaro.org>
---
 samples/blinky/Cargo.toml |  3 +++
 samples/blinky/build.rs   |  3 +++
 samples/blinky/src/lib.rs | 14 ++++++++++++++
 3 files changed, 20 insertions(+)
 create mode 100644 samples/blinky/build.rs

diff --git a/samples/blinky/Cargo.toml b/samples/blinky/Cargo.toml
index cc62c8b..e812ba1 100644
--- a/samples/blinky/Cargo.toml
+++ b/samples/blinky/Cargo.toml
@@ -15,3 +15,6 @@ crate-type = ["staticlib"]
 [dependencies]
 zephyr = "3.7.0"
 log = "0.4.22"
+
+[build-dependencies]
+zephyr-build = "3.7.0"
diff --git a/samples/blinky/build.rs b/samples/blinky/build.rs
new file mode 100644
index 0000000..eea8aad
--- /dev/null
+++ b/samples/blinky/build.rs
@@ -0,0 +1,3 @@
+fn main() {
+    zephyr_build::dt_cfgs();
+}
diff --git a/samples/blinky/src/lib.rs b/samples/blinky/src/lib.rs
index b889551..716cd2c 100644
--- a/samples/blinky/src/lib.rs
+++ b/samples/blinky/src/lib.rs
@@ -3,6 +3,12 @@
 
 #![no_std]
 
+// Sigh. The check config system requires that the compiler be told what possible config values
+// there might be.  This is completely impossible with both Kconfig and the DT configs, since the
+// whole point is that we likely need to check for configs that aren't otherwise present in the
+// build.  So, this is just always necessary.
+#![allow(unexpected_cfgs)]
+
 use log::warn;
 
 use core::ffi::c_void;
@@ -41,6 +47,7 @@ unsafe extern "C" fn blink(_p1: *mut c_void, _p2: *mut c_void, _p3: *mut c_void)
     do_blink();
 }
 
+#[cfg(dt = "aliases::led0")]
 fn do_blink() {
     warn!("Inside of blinky");
 
@@ -61,3 +68,10 @@ fn do_blink() {
         sleep(duration);
     }
 }
+
+#[cfg(not(dt = "aliases::led0"))]
+fn do_blink() {
+    warn!("No leds configured");
+    loop {
+    }
+}