Skip to content

Commit 7e8a847

Browse files
committed
zephyr-build: Use parsed DT for both uses
Instead of trying to hand off data through a file between the build of different crates, which was causing build failures in applications that need to use cfgs based on the presence of DT nodes, instead, just parse the DT again, and recalculate the node tree from it. This should fix build issues with the all nodes txt file missing. Signed-off-by: David Brown <[email protected]>
1 parent 669cae7 commit 7e8a847

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

zephyr-build/src/devicetree/output.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ impl DeviceTree {
2626
self.node_walk(self.root.as_ref(), None, &augments)
2727
}
2828

29-
/// Write to the given file a list of the path names of all of the nodes present in this
30-
/// devicetree.
29+
// Write, to the given writer, CFG lines so that Rust code can conditionalize based on the DT.
3130
pub fn output_node_paths<W: Write>(&self, write: &mut W) -> Result<()> {
3231
self.root.as_ref().output_path_walk(write, None)?;
3332

3433
// Also, output all of the labels. Technically, this depends on the labels augment being
3534
// present.
36-
writeln!(write, "labels")?;
35+
writeln!(write, "cargo:rustc-cfg=dt=\"labels\"")?;
3736
for label in self.labels.keys() {
38-
writeln!(write, "labels::{}", fix_id(label))?;
37+
writeln!(write, "cargo:rustc-cfg=dt=\"labels::{}\"", fix_id(label))?;
3938
}
4039
Ok(())
4140
}
@@ -147,7 +146,7 @@ impl Node {
147146
fixed_name
148147
};
149148

150-
writeln!(write, "{}", child_name)?;
149+
writeln!(write, "cargo:rustc-cfg=dt=\"{}\"", child_name)?;
151150

152151
for prop in &child.properties {
153152
prop.output_path(write, &child_name)?;
@@ -175,7 +174,7 @@ impl Property {
175174
fn output_path<W: Write>(&self, write: &mut W, name: &str) -> Result<()> {
176175
if let Some(value) = self.get_single_value() {
177176
if let Value::Phandle(_) = value {
178-
writeln!(write, "{}::{}", name, self.name)?;
177+
writeln!(write, "cargo:rustc-cfg=dt=\"{}::{}\"", name, fix_id(&self.name))?;
179178
}
180179
}
181180
Ok(())

zephyr-build/src/lib.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,21 @@ pub fn build_kconfig_mod() {
8181
}
8282

8383
/// Parse the finalized DTS file, generating the Rust devicetree file.
84-
pub fn build_dts() {
84+
fn import_dt() -> DeviceTree {
8585
let zephyr_dts = env::var("ZEPHYR_DTS").expect("ZEPHYR_DTS must be set");
86-
let outdir = env::var("OUT_DIR").expect("OUT_DIR must be set");
8786
let gen_include = env::var("BINARY_DIR_INCLUDE_GENERATED")
8887
.expect("BINARY_DIR_INCLUDE_GENERATED");
89-
let builddir = env::var("BUILD_DIR").expect("BUILD_DIR");
88+
89+
let generated = format!("{}/devicetree_generated.h", gen_include);
90+
DeviceTree::new(&zephyr_dts, generated)
91+
}
92+
93+
pub fn build_dts() {
94+
let dt = import_dt();
95+
96+
let outdir = env::var("OUT_DIR").expect("OUT_DIR must be set");
97+
let out_path = Path::new(&outdir).join("devicetree.rs");
98+
let mut out = File::create(&out_path).expect("Unable to create devicetree.rs");
9099

91100
let augments = env::var("DT_AUGMENTS").expect("DT_AUGMENTS must be set");
92101
let augments: Vec<String> = augments.split_whitespace().map(String::from).collect();
@@ -110,40 +119,21 @@ pub fn build_dts() {
110119
.map(|aug| Box::new(aug) as Box<dyn Augment>)
111120
.collect();
112121

113-
let generated = format!("{}/devicetree_generated.h", gen_include);
114-
let dt = DeviceTree::new(&zephyr_dts, generated);
115-
116-
let out_path = Path::new(&outdir).join("devicetree.rs");
117-
let mut out = File::create(&out_path).expect("Unable to create devicetree.rs");
118-
119122
let tokens = dt.to_tokens(&augs);
120123
if has_rustfmt() {
121124
write_formatted(out, tokens);
122125
} else {
123126
writeln!(out, "{}", tokens).unwrap();
124127
};
125-
126-
// Output all of the node names in the discovered tree.
127-
let all_nodes_path = Path::new(&builddir)
128-
.join("rust")
129-
.join("all-dt-nodes.txt");
130-
let mut out = File::create(&all_nodes_path).expect("Unable to create all-dt-nodex.txt");
131-
dt.output_node_paths(&mut out).expect("Unable to write to all-dt-nodes.txt");
132128
}
133129

134130
/// Generate cfg directives for each of the nodes in the generated device tree.
135131
///
136132
/// This assumes that build_dts was already run by the `zephyr` crate, which should happen if this
137133
/// is called from a user application.
138134
pub fn dt_cfgs() {
139-
let builddir = env::var("BUILD_DIR").expect("BUILD_DIR");
140-
let path = Path::new(&builddir)
141-
.join("rust")
142-
.join("all-dt-nodes.txt");
143-
for line in BufReader::new(File::open(&path).expect("Unable to open all-dt-nodes")).lines() {
144-
let line = line.expect("Error reading line from all-dt-nodes");
145-
println!("cargo:rustc-cfg=dt=\"{}\"", line);
146-
}
135+
let dt = import_dt();
136+
dt.output_node_paths(&mut std::io::stdout()).unwrap();
147137
}
148138

149139
/// Determine if `rustfmt` is in the path, and can be excecuted. Returns false on any kind of error.

0 commit comments

Comments
 (0)