diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py index 28d4a17787d6fa3..f049f7bc2e41da2 100755 --- a/scripts/dts/gen_defines.py +++ b/scripts/dts/gen_defines.py @@ -74,25 +74,6 @@ def main(): with open(args.dts_out, "w", encoding="utf-8") as f: print(edt.dts_source, file=f) - # The raw index into edt.compat2nodes[compat] is used for node - # instance numbering within a compatible. - # - # As a way to satisfy people's intuitions about instance numbers, - # though, we sort this list so enabled instances come first. - # - # This might look like a hack, but it keeps drivers and - # applications which don't use instance numbers carefully working - # as expected, since e.g. instance number 0 is always the - # singleton instance if there's just one enabled node of a - # particular compatible. - # - # This doesn't violate any devicetree.h API guarantees about - # instance ordering, since we make no promises that instance - # numbers are stable across builds. - for compat, nodes in edt.compat2nodes.items(): - edt.compat2nodes[compat] = sorted( - nodes, key=lambda node: 0 if node.status == "okay" else 1) - # Create the generated header. with open(args.header_out, "w", encoding="utf-8") as header_file: write_top_comment(edt) diff --git a/scripts/dts/python-devicetree/src/devicetree/edtlib.py b/scripts/dts/python-devicetree/src/devicetree/edtlib.py index 088ef6e09967a29..38aa537834cc4d5 100644 --- a/scripts/dts/python-devicetree/src/devicetree/edtlib.py +++ b/scripts/dts/python-devicetree/src/devicetree/edtlib.py @@ -2401,6 +2401,25 @@ def _init_luts(self) -> None: f"node '{node.path}' compatible '{compat}' " f"has unknown vendor prefix '{vendor}'") + # The raw index into edt.compat2nodes[compat] is used for node + # instance numbering within a compatible. + # + # As a way to satisfy people's intuitions about instance numbers, + # we sort this list so enabled instances come first. + # + # This might look like a hack, but it keeps drivers and + # applications which don't use instance numbers carefully working + # as expected, since e.g. instance number 0 is always the + # singleton instance if there's just one enabled node of a + # particular compatible. + # + # This doesn't violate any devicetree.h API guarantees about + # instance ordering, since we make no promises that instance + # numbers are stable across builds. + for compat, nodes in self.compat2nodes.items(): + self.compat2nodes[compat] = sorted( + nodes, key=lambda node: 0 if node.status == "okay" else 1 + ) for nodeset in self.scc_order: node = nodeset[0]