Skip to content

Commit 472ac77

Browse files
authored
Merge pull request #2056 from theotherjimmy/pass-files-to-source
Enable passing individual files to --source options
2 parents dff2c31 + cd9261d commit 472ac77

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

tools/toolchains/__init__.py

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from time import time, sleep
2323
from types import ListType
2424
from shutil import copyfile
25-
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath
25+
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir
2626
from inspect import getmro
2727
from copy import deepcopy
2828
from tools.config import Config
@@ -428,14 +428,31 @@ def is_ignored(self, file_path):
428428
return True
429429
return False
430430

431+
# Create a Resources object from the path pointed to by *path* by either traversing a
432+
# a directory structure, when *path* is a directory, or adding *path* to the resources,
433+
# when *path* is a file.
434+
# The parameter *base_path* is used to set the base_path attribute of the Resources
435+
# object and the parameter *exclude_paths* is used by the directory traversal to
436+
# exclude certain paths from the traversal.
431437
def scan_resources(self, path, exclude_paths=None, base_path=None):
432-
labels = self.get_labels()
433-
434438
resources = Resources(path)
435439
if not base_path:
436-
base_path = path
440+
if isfile(path):
441+
base_path = dirname(path)
442+
else:
443+
base_path = path
437444
resources.base_path = base_path
438445

446+
if isfile(path):
447+
self._add_file(path, resources, base_path, exclude_paths=exclude_paths)
448+
else:
449+
self._add_dir(path, resources, base_path, exclude_paths=exclude_paths)
450+
return resources
451+
452+
# A helper function for scan_resources. _add_dir traverses *path* (assumed to be a
453+
# directory) and heeds the ".mbedignore" files along the way. _add_dir calls _add_file
454+
# on every file it considers adding to the resources object.
455+
def _add_dir(self, path, resources, base_path, exclude_paths=None):
439456
""" os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
440457
When topdown is True, the caller can modify the dirnames list in-place
441458
(perhaps using del or slice assignment), and walk() will only recurse into
@@ -446,6 +463,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
446463
bottom-up mode the directories in dirnames are generated before dirpath
447464
itself is generated.
448465
"""
466+
labels = self.get_labels()
449467
for root, dirs, files in walk(path, followlinks=True):
450468
# Check if folder contains .mbedignore
451469
if ".mbedignore" in files:
@@ -467,7 +485,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
467485
if d == '.hg':
468486
resources.repo_dirs.append(dir_path)
469487
resources.repo_files.extend(self.scan_repository(dir_path))
470-
488+
471489
if ((d.startswith('.') or d in self.legacy_ignore_dirs) or
472490
# Ignore targets that do not match the TARGET in extra_labels list
473491
(d.startswith('TARGET_') and d[7:] not in labels['TARGET']) or
@@ -495,58 +513,61 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
495513

496514
for file in files:
497515
file_path = join(root, file)
516+
self._add_file(file_path, resources, base_path)
498517

499-
resources.file_basepath[file_path] = base_path
518+
# A helper function for both scan_resources and _add_dir. _add_file adds one file
519+
# (*file_path*) to the resources object based on the file type.
520+
def _add_file(self, file_path, resources, base_path, exclude_paths=None):
521+
resources.file_basepath[file_path] = base_path
500522

501-
if self.is_ignored(file_path):
502-
continue
523+
if self.is_ignored(file_path):
524+
return
503525

504-
_, ext = splitext(file)
505-
ext = ext.lower()
526+
_, ext = splitext(file_path)
527+
ext = ext.lower()
506528

507-
if ext == '.s':
508-
resources.s_sources.append(file_path)
529+
if ext == '.s':
530+
resources.s_sources.append(file_path)
509531

510-
elif ext == '.c':
511-
resources.c_sources.append(file_path)
532+
elif ext == '.c':
533+
resources.c_sources.append(file_path)
512534

513-
elif ext == '.cpp':
514-
resources.cpp_sources.append(file_path)
535+
elif ext == '.cpp':
536+
resources.cpp_sources.append(file_path)
515537

516-
elif ext == '.h' or ext == '.hpp':
517-
resources.headers.append(file_path)
538+
elif ext == '.h' or ext == '.hpp':
539+
resources.headers.append(file_path)
518540

519-
elif ext == '.o':
520-
resources.objects.append(file_path)
541+
elif ext == '.o':
542+
resources.objects.append(file_path)
521543

522-
elif ext == self.LIBRARY_EXT:
523-
resources.libraries.append(file_path)
524-
resources.lib_dirs.add(root)
544+
elif ext == self.LIBRARY_EXT:
545+
resources.libraries.append(file_path)
546+
resources.lib_dirs.add(dirname(file_path))
525547

526-
elif ext == self.LINKER_EXT:
527-
if resources.linker_script is not None:
528-
self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
529-
resources.linker_script = file_path
548+
elif ext == self.LINKER_EXT:
549+
if resources.linker_script is not None:
550+
self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
551+
resources.linker_script = file_path
530552

531-
elif ext == '.lib':
532-
resources.lib_refs.append(file_path)
553+
elif ext == '.lib':
554+
resources.lib_refs.append(file_path)
533555

534-
elif ext == '.bld':
535-
resources.lib_builds.append(file_path)
556+
elif ext == '.bld':
557+
resources.lib_builds.append(file_path)
536558

537-
elif file == '.hgignore':
538-
resources.repo_files.append(file_path)
559+
elif file == '.hgignore':
560+
resources.repo_files.append(file_path)
539561

540-
elif ext == '.hex':
541-
resources.hex_files.append(file_path)
562+
elif ext == '.hex':
563+
resources.hex_files.append(file_path)
542564

543-
elif ext == '.bin':
544-
resources.bin_files.append(file_path)
565+
elif ext == '.bin':
566+
resources.bin_files.append(file_path)
545567

546-
elif ext == '.json':
547-
resources.json_files.append(file_path)
568+
elif ext == '.json':
569+
resources.json_files.append(file_path)
548570

549-
return resources
550571

551572
def scan_repository(self, path):
552573
resources = []

0 commit comments

Comments
 (0)