Skip to content

Commit c1603b3

Browse files
pillo79kartben
authored andcommitted
gen_edt: use workspace dir as base for relative paths in comments
This commit allows comments to reference files with paths that are relative to the Zephyr workspace root. This is done by adding a new argument '--workspace-dir' to the 'gen_edt.py' script, which is passed to the 'EDT' and 'DT' classes and used instead of the current working directory. The workspace directory is set to WEST_TOPDIR if West is in use, otherwise it is set to the parent directory of ZEPHYR_BASE so that Zephyr files have a 'zephyr/' prefix. Signed-off-by: Luca Burelli <[email protected]>
1 parent a63cb8e commit c1603b3

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

cmake/modules/dts.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,21 @@ set_property(DIRECTORY APPEND PROPERTY
290290
# Run GEN_EDT_SCRIPT.
291291
#
292292

293+
if(WEST_TOPDIR)
294+
set(GEN_EDT_WORKSPACE_DIR ${WEST_TOPDIR})
295+
else()
296+
# If West is not available, define the parent directory of ZEPHYR_BASE as
297+
# the workspace. This will create comments that reference the files in the
298+
# Zephyr tree with a 'zephyr/' prefix.
299+
set(GEN_EDT_WORKSPACE_DIR ${ZEPHYR_BASE}/..)
300+
endif()
301+
293302
string(REPLACE ";" " " EXTRA_DTC_FLAGS_RAW "${EXTRA_DTC_FLAGS}")
294303
set(CMD_GEN_EDT ${PYTHON_EXECUTABLE} ${GEN_EDT_SCRIPT}
295304
--dts ${DTS_POST_CPP}
296305
--dtc-flags '${EXTRA_DTC_FLAGS_RAW}'
297306
--bindings-dirs ${DTS_ROOT_BINDINGS}
307+
--workspace-dir ${GEN_EDT_WORKSPACE_DIR}
298308
--dts-out ${ZEPHYR_DTS}.new # for debugging and dtc
299309
--edt-pickle-out ${EDT_PICKLE}.new
300310
${EXTRA_GEN_EDT_ARGS}

scripts/dts/gen_edt.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def main():
4343

4444
try:
4545
edt = edtlib.EDT(args.dts, args.bindings_dirs,
46+
workspace_dir=args.workspace_dir,
4647
# Suppress this warning if it's suppressed in dtc
4748
warn_reg_unit_address_mismatch=
4849
"-Wno-simple_bus_reg" not in args.dtc_flags,
@@ -71,6 +72,9 @@ def parse_args() -> argparse.Namespace:
7172
parser.add_argument("--bindings-dirs", nargs='+', required=True,
7273
help="directory with bindings in YAML format, "
7374
"we allow multiple")
75+
parser.add_argument("--workspace-dir", default=os.getcwd(),
76+
help="directory to be used as reference for generated "
77+
"relative paths (e.g. WEST_TOPDIR)")
7478
parser.add_argument("--dts-out", required=True,
7579
help="path to write merged DTS source code to (e.g. "
7680
"as a debugging aid)")

scripts/dts/python-devicetree/src/devicetree/dtlib.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def safe_relpath(filename):
207207
# a relative path cannot be established (on Windows with files
208208
# in different drives, for example).
209209
try:
210-
return os.path.relpath(filename, start=os.getcwd())
210+
return os.path.relpath(filename, start=self.dt._base_dir)
211211
except ValueError:
212212
return filename
213213

@@ -787,7 +787,7 @@ class DT:
787787
#
788788

789789
def __init__(self, filename: Optional[str], include_path: Iterable[str] = (),
790-
force: bool = False):
790+
force: bool = False, base_dir: Optional[str] = None):
791791
"""
792792
Parses a DTS file to create a DT instance. Raises OSError if 'filename'
793793
can't be opened, and DTError for any parse errors.
@@ -804,6 +804,11 @@ def __init__(self, filename: Optional[str], include_path: Iterable[str] = (),
804804
force:
805805
Try not to raise DTError even if the input tree has errors.
806806
For experimental use; results not guaranteed.
807+
808+
base_dir:
809+
Path to the directory that is to be used as the reference for
810+
the generated relative paths in comments. When not provided, the
811+
current working directory is used.
807812
"""
808813
# Remember to update __deepcopy__() if you change this.
809814

@@ -817,6 +822,7 @@ def __init__(self, filename: Optional[str], include_path: Iterable[str] = (),
817822
self.filename = filename
818823

819824
self._force = force
825+
self._base_dir = base_dir or os.getcwd()
820826

821827
if filename is not None:
822828
self._parse_file(filename, include_path)
@@ -974,7 +980,7 @@ def __deepcopy__(self, memo):
974980
"""
975981

976982
# We need a new DT, obviously. Make a new, empty one.
977-
ret = DT(None, (), self._force)
983+
ret = DT(None, (), self._force, self._base_dir)
978984

979985
# Now allocate new Node objects for every node in self, to use
980986
# in the new DT. Set their parents to None for now and leave

scripts/dts/python-devicetree/src/devicetree/edtlib.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ class EDT:
19931993
def __init__(self,
19941994
dts: Optional[str],
19951995
bindings_dirs: list[str],
1996+
workspace_dir: Optional[str] = None,
19961997
warn_reg_unit_address_mismatch: bool = True,
19971998
default_prop_types: bool = True,
19981999
support_fixed_partitions_on_any_bus: bool = True,
@@ -2009,6 +2010,10 @@ def __init__(self,
20092010
List of paths to directories containing bindings, in YAML format.
20102011
These directories are recursively searched for .yaml files.
20112012
2013+
workspace_dir:
2014+
Path to the root of the Zephyr workspace. This is used as a base
2015+
directory for relative paths in the generated devicetree comments.
2016+
20122017
warn_reg_unit_address_mismatch (default: True):
20132018
If True, a warning is logged if a node has a 'reg' property where
20142019
the address of the first entry does not match the unit address of the
@@ -2077,7 +2082,7 @@ def __init__(self,
20772082

20782083
if dts is not None:
20792084
try:
2080-
self._dt = DT(dts)
2085+
self._dt = DT(dts, base_dir=workspace_dir)
20812086
except DTError as e:
20822087
raise EDTError(e) from e
20832088
self._finish_init()

0 commit comments

Comments
 (0)