diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py index 70d89727707..f848ca4941b 100755 --- a/scripts/dts/gen_defines.py +++ b/scripts/dts/gen_defines.py @@ -19,6 +19,8 @@ # edtlib. This will keep this script simple. import argparse +import os +import pathlib import sys import edtlib @@ -40,7 +42,8 @@ def main(): out_comment("Generated by gen_defines.py", blank_before=False) out_comment("DTS input file: " + args.dts, blank_before=False) - out_comment("Directories with bindings: " + ", ".join(args.bindings_dirs), + out_comment("Directories with bindings: " + + ", ".join(map(relativize, args.bindings_dirs)), blank_before=False) active_compats = set() @@ -54,7 +57,8 @@ def main(): out_comment("Devicetree node: " + node.path) out_comment("Binding (compatible = {}): {}".format( - node.matching_compat, node.binding_path), + node.matching_compat, + relativize(node.binding_path)), blank_before=False) out_comment("Binding description: " + node.description, blank_before=False) @@ -111,6 +115,22 @@ def parse_args(): return parser.parse_args() +def relativize(path): + # If 'path' is within $ZEPHYR_BASE, returns it relative to $ZEPHYR_BASE, + # with a "$ZEPHYR_BASE/..." hint at the start of the string. Otherwise, + # returns 'path' unchanged. + + zbase = os.getenv("ZEPHYR_BASE") + if zbase is None: + return path + + try: + return str("$ZEPHYR_BASE" / pathlib.Path(path).relative_to(zbase)) + except ValueError: + # Not within ZEPHYR_BASE + return path + + def write_regs(node): # Writes address/size output for the registers in the node's 'reg' property