Skip to content

Commit 9b8a48f

Browse files
committed
Rewrite and document _include_file
1 parent 0265b57 commit 9b8a48f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

tools/resources/__init__.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,20 +267,24 @@ def add_file_ref(self, file_type, file_name, file_path):
267267
self._file_refs[file_type].add(FileRef(file_name, file_path))
268268

269269
def _include_file(self, ref):
270+
"""Determine if a given file ref should be included in the build
271+
272+
Files may be part of a library if a parent directory contains an
273+
mbed_lib.json. If a file is part of a library, include or exclude
274+
it based on the library it's part of.
275+
If a file is not part of a library, it's included.
276+
"""
270277
_, path = ref
271-
starts_with_excluded = [
272-
len(dirname(e.path)) for e in self._excluded_libs
273-
if path.startswith(dirname(e.path))
274-
]
275-
if not starts_with_excluded:
276-
return True
277-
starts_with_included = [
278-
len(dirname(e.path)) for e in self._libs_filtered
279-
if path.startswith(dirname(e.path))
280-
]
281-
if not starts_with_included:
282-
return False
283-
return max(starts_with_included) > max(starts_with_excluded)
278+
cur_dir = dirname(path)
279+
included_lib_paths = [dirname(e.path) for e in self._libs_filtered]
280+
excluded_lib_paths = [dirname(e.path) for e in self._excluded_libs]
281+
while dirname(cur_dir) != cur_dir:
282+
if cur_dir in included_lib_paths:
283+
return True
284+
elif cur_dir in excluded_lib_paths:
285+
return False
286+
cur_dir = dirname(cur_dir)
287+
return True
284288

285289
def get_file_refs(self, file_type):
286290
"""Return a list of FileRef for every file of the given type"""

0 commit comments

Comments
 (0)