Skip to content

Enable passing individual files to --source options #2056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 61 additions & 40 deletions tools/toolchains/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from time import time, sleep
from types import ListType
from shutil import copyfile
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir
from inspect import getmro
from copy import deepcopy
from tools.config import Config
Expand Down Expand Up @@ -430,14 +430,31 @@ def is_ignored(self, file_path):
return True
return False

# Create a Resources object from the path pointed to by *path* by either traversing a
# a directory structure, when *path* is a directory, or adding *path* to the resources,
# when *path* is a file.
# The parameter *base_path* is used to set the base_path attribute of the Resources
# object and the parameter *exclude_paths* is used by the directory traversal to
# exclude certain paths from the traversal.
def scan_resources(self, path, exclude_paths=None, base_path=None):
labels = self.get_labels()

resources = Resources(path)
if not base_path:
base_path = path
if isfile(path):
base_path = dirname(path)
else:
base_path = path
resources.base_path = base_path

if isfile(path):
self._add_file(path, resources, base_path, exclude_paths=exclude_paths)
else:
self._add_dir(path, resources, base_path, exclude_paths=exclude_paths)
return resources

# A helper function for scan_resources. _add_dir traverses *path* (assumed to be a
# directory) and heeds the ".mbedignore" files along the way. _add_dir calls _add_file
# on every file it considers adding to the resources object.
def _add_dir(self, path, resources, base_path, exclude_paths=None):
""" os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
When topdown is True, the caller can modify the dirnames list in-place
(perhaps using del or slice assignment), and walk() will only recurse into
Expand All @@ -448,6 +465,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
bottom-up mode the directories in dirnames are generated before dirpath
itself is generated.
"""
labels = self.get_labels()
for root, dirs, files in walk(path, followlinks=True):
# Check if folder contains .mbedignore
if ".mbedignore" in files:
Expand All @@ -469,7 +487,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
if d == '.hg':
resources.repo_dirs.append(dir_path)
resources.repo_files.extend(self.scan_repository(dir_path))

if ((d.startswith('.') or d in self.legacy_ignore_dirs) or
# Ignore targets that do not match the TARGET in extra_labels list
(d.startswith('TARGET_') and d[7:] not in labels['TARGET']) or
Expand Down Expand Up @@ -497,58 +515,61 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):

for file in files:
file_path = join(root, file)
self._add_file(file_path, resources, base_path)

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

if self.is_ignored(file_path):
continue
if self.is_ignored(file_path):
return

_, ext = splitext(file)
ext = ext.lower()
_, ext = splitext(file_path)
ext = ext.lower()

if ext == '.s':
resources.s_sources.append(file_path)
if ext == '.s':
resources.s_sources.append(file_path)

elif ext == '.c':
resources.c_sources.append(file_path)
elif ext == '.c':
resources.c_sources.append(file_path)

elif ext == '.cpp':
resources.cpp_sources.append(file_path)
elif ext == '.cpp':
resources.cpp_sources.append(file_path)

elif ext == '.h' or ext == '.hpp':
resources.headers.append(file_path)
elif ext == '.h' or ext == '.hpp':
resources.headers.append(file_path)

elif ext == '.o':
resources.objects.append(file_path)
elif ext == '.o':
resources.objects.append(file_path)

elif ext == self.LIBRARY_EXT:
resources.libraries.append(file_path)
resources.lib_dirs.add(root)
elif ext == self.LIBRARY_EXT:
resources.libraries.append(file_path)
resources.lib_dirs.add(dirname(file_path))

elif ext == self.LINKER_EXT:
if resources.linker_script is not None:
self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
resources.linker_script = file_path
elif ext == self.LINKER_EXT:
if resources.linker_script is not None:
self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
resources.linker_script = file_path

elif ext == '.lib':
resources.lib_refs.append(file_path)
elif ext == '.lib':
resources.lib_refs.append(file_path)

elif ext == '.bld':
resources.lib_builds.append(file_path)
elif ext == '.bld':
resources.lib_builds.append(file_path)

elif file == '.hgignore':
resources.repo_files.append(file_path)
elif file == '.hgignore':
resources.repo_files.append(file_path)

elif ext == '.hex':
resources.hex_files.append(file_path)
elif ext == '.hex':
resources.hex_files.append(file_path)

elif ext == '.bin':
resources.bin_files.append(file_path)
elif ext == '.bin':
resources.bin_files.append(file_path)

elif ext == '.json':
resources.json_files.append(file_path)
elif ext == '.json':
resources.json_files.append(file_path)

return resources

def scan_repository(self, path):
resources = []
Expand Down