@@ -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.
138134pub 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