Skip to content

Commit 2e081dc

Browse files
author
Cruz Monrreal
authored
Merge pull request #7644 from theotherjimmy/components
Tools: Scan for "components"
2 parents 4089d4e + e695a34 commit 2e081dc

File tree

5 files changed

+108
-42
lines changed

5 files changed

+108
-42
lines changed

tools/resources/__init__.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
from os.path import (join, splitext, dirname, relpath, basename, split, normcase,
4242
abspath, exists)
4343

44+
from .ignore import MbedIgnoreSet, IGNORE_FILENAME
45+
4446
# Support legacy build conventions: the original mbed build system did not have
4547
# standard labels for the "TARGET_" and "TOOLCHAIN_" specific directories, but
4648
# had the knowledge of a list of these directories to be ignored.
@@ -135,16 +137,15 @@ def __init__(self, notify, collect_ignores=False):
135137

136138
# Incremental scan related
137139
self._label_paths = []
138-
self._labels = {"TARGET": [], "TOOLCHAIN": [], "FEATURE": []}
140+
self._labels = {
141+
"TARGET": [], "TOOLCHAIN": [], "FEATURE": [], "COMPONENT": []
142+
}
139143
self._prefixed_labels = set()
140144

141145
# Path seperator style (defaults to OS-specific seperator)
142146
self._sep = sep
143147

144-
# Ignore patterns from .mbedignore files and add_ignore_patters
145-
self._ignore_patterns = []
146-
self._ignore_regex = re.compile("$^")
147-
148+
self._ignoreset = MbedIgnoreSet()
148149

149150
def ignore_dir(self, directory):
150151
if self._collect_ignores:
@@ -227,6 +228,8 @@ def _add_labels(self, prefix, labels):
227228

228229
def add_target_labels(self, target):
229230
self._add_labels("TARGET", target.labels)
231+
self._add_labels("COMPONENT", target.components)
232+
self.add_features(target.features)
230233

231234
def add_features(self, features):
232235
self._add_labels("FEATURE", features)
@@ -237,27 +240,9 @@ def add_toolchain_labels(self, toolchain):
237240
self._legacy_ignore_dirs -= set(
238241
[toolchain.target.name, LEGACY_TOOLCHAIN_NAMES[toolchain.name]])
239242

240-
def is_ignored(self, file_path):
241-
"""Check if file path is ignored by any .mbedignore thus far"""
242-
return self._ignore_regex.match(normcase(file_path))
243-
244243
def add_ignore_patterns(self, root, base_path, patterns):
245-
"""Add a series of patterns to the ignored paths
246-
247-
Positional arguments:
248-
root - the directory containing the ignore file
249-
base_path - the location that the scan started from
250-
patterns - the list of patterns we will ignore in the future
251-
"""
252244
real_base = relpath(root, base_path)
253-
if real_base == ".":
254-
self._ignore_patterns.extend(normcase(p) for p in patterns)
255-
else:
256-
self._ignore_patterns.extend(
257-
normcase(join(real_base, pat)) for pat in patterns)
258-
if self._ignore_patterns:
259-
self._ignore_regex = re.compile("|".join(
260-
fnmatch.translate(p) for p in self._ignore_patterns))
245+
self._ignoreset.add_ignore_patterns(real_base, patterns)
261246

262247
def _not_current_label(self, dirname, label_type):
263248
return (dirname.startswith(label_type + "_") and
@@ -401,15 +386,13 @@ def add_directory(
401386

402387
for root, dirs, files in walk(path, followlinks=True):
403388
# Check if folder contains .mbedignore
404-
if ".mbedignore" in files:
405-
with open (join(root,".mbedignore"), "r") as f:
406-
lines=f.readlines()
407-
lines = [l.strip() for l in lines
408-
if l.strip() != "" and not l.startswith("#")]
409-
self.add_ignore_patterns(root, base_path, lines)
389+
if IGNORE_FILENAME in files:
390+
real_base = relpath(root, base_path)
391+
self._ignoreset.add_mbedignore(
392+
real_base, join(root, IGNORE_FILENAME))
410393

411394
root_path =join(relpath(root, base_path))
412-
if self.is_ignored(join(root_path,"")):
395+
if self._ignoreset.is_ignored(join(root_path,"")):
413396
self.ignore_dir(root_path)
414397
dirs[:] = []
415398
continue
@@ -421,12 +404,12 @@ def add_directory(
421404
self.add_file_ref(FileType.REPO_DIR, fake_path, dir_path)
422405

423406
if (any(self._not_current_label(d, t) for t
424-
in ['TARGET', 'TOOLCHAIN', 'FEATURE'])):
407+
in self._labels.keys())):
425408
self._label_paths.append((dir_path, base_path, into_path))
426409
self.ignore_dir(dir_path)
427410
dirs.remove(d)
428411
elif (d.startswith('.') or d in self._legacy_ignore_dirs or
429-
self.is_ignored(join(root_path, d, ""))):
412+
self._ignoreset.is_ignored(join(root_path, d, ""))):
430413
self.ignore_dir(dir_path)
431414
dirs.remove(d)
432415

@@ -468,7 +451,7 @@ def _add_file(self, file_path, base_path, into_path):
468451
scanning starting as base_path
469452
"""
470453

471-
if (self.is_ignored(relpath(file_path, base_path)) or
454+
if (self._ignoreset.is_ignored(relpath(file_path, base_path)) or
472455
basename(file_path).startswith(".")):
473456
self.ignore_dir(relpath(file_path, base_path))
474457
return
@@ -546,3 +529,4 @@ def scan_with_config(self, src_paths, config):
546529
self.add_directory(path)
547530
config.load_resources(self)
548531
return self
532+

tools/resources/ignore.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# mbed SDK
2+
# Copyright (c) 2011-2013 ARM Limited
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import fnmatch
17+
import re
18+
from os.path import normcase, join
19+
20+
21+
IGNORE_FILENAME = ".mbedignore"
22+
23+
24+
class MbedIgnoreSet(object):
25+
"""
26+
# The mbedignore rules as an object
27+
28+
A project in Mbed OS contains metadata files that exclude files from a build.
29+
These rules are stored as `fnmatch` patterns in text in a files named `.mbedignore`.
30+
"""
31+
32+
def __init__(self):
33+
self._ignore_patterns = []
34+
self._ignore_regex = re.compile("$^")
35+
36+
def is_ignored(self, file_path):
37+
"""Check if file path is ignored by any .mbedignore thus far"""
38+
return self._ignore_regex.match(normcase(file_path))
39+
40+
def add_ignore_patterns(self, in_name, patterns):
41+
"""Ignore all files and directories matching the paterns in
42+
directories named by in_name.
43+
44+
Positional arguments:
45+
in_name - the filename prefix that this ignore will apply to
46+
patterns - the list of patterns we will ignore in the future
47+
"""
48+
if in_name == ".":
49+
self._ignore_patterns.extend(normcase(p) for p in patterns)
50+
else:
51+
self._ignore_patterns.extend(
52+
normcase(join(in_name, pat)) for pat in patterns)
53+
if self._ignore_patterns:
54+
self._ignore_regex = re.compile("|".join(
55+
fnmatch.translate(p) for p in self._ignore_patterns))
56+
57+
def add_mbedignore(self, in_name, filepath):
58+
"""Add a series of patterns to the ignored paths
59+
60+
Positional arguments:
61+
in_name - the filename prefix that this ignore will apply to
62+
patterns - the list of patterns we will ignore in the future
63+
"""
64+
with open (filepath) as f:
65+
patterns = [l.strip() for l in f
66+
if l.strip() != "" and not l.startswith("#")]
67+
self.add_ignore_patterns(in_name, patterns)
68+
69+

tools/targets/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def wrapper(*args, **kwargs):
9191

9292
# Cumulative attributes can have values appended to them, so they
9393
# need to be computed differently than regular attributes
94-
CUMULATIVE_ATTRIBUTES = ['extra_labels', 'macros', 'device_has', 'features']
94+
CUMULATIVE_ATTRIBUTES = ['extra_labels', 'macros', 'device_has', 'features', 'components']
9595

9696

9797
def get_resolution_order(json_data, target_name, order, level=0):
@@ -222,8 +222,7 @@ def __getattr_cumulative(self, attrname):
222222
def_idx = idx
223223
break
224224
else:
225-
raise AttributeError("Attribute '%s' not found in target '%s'"
226-
% (attrname, self.name))
225+
return []
227226
# Get the starting value of the attribute
228227
starting_value = (tdata[self.resolution_order[def_idx][0]][attrname]
229228
or [])[:]

tools/test_api.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from Queue import Queue, Empty
4141
except ImportError:
4242
from queue import Queue, Empty
43-
from os.path import join, exists, basename, relpath, isdir
43+
from os.path import join, exists, basename, relpath, isdir, isfile
4444
from threading import Thread, Lock
4545
from multiprocessing import Pool, cpu_count
4646
from subprocess import Popen, PIPE
@@ -55,6 +55,7 @@
5555
from tools.utils import construct_enum
5656
from tools.memap import MemapParser
5757
from tools.targets import TARGET_MAP, Target
58+
from tools.config import Config
5859
import tools.test_configs as TestConfig
5960
from tools.test_db import BaseDBAccess
6061
from tools.build_api import build_project, build_mbed_libs, build_lib
@@ -66,7 +67,7 @@
6667
from tools.build_api import add_result_to_report
6768
from tools.build_api import prepare_toolchain
6869
from tools.build_api import get_config
69-
from tools.resources import Resources
70+
from tools.resources import Resources, MbedIgnoreSet, IGNORE_FILENAME
7071
from tools.libraries import LIBRARIES, LIBRARY_MAP
7172
from tools.options import extract_profile
7273
from tools.toolchains import TOOLCHAIN_PATHS
@@ -2082,19 +2083,29 @@ def find_tests(base_dir, target_name, toolchain_name, app_config=None):
20822083
# List of common folders: (predicate function, path) tuple
20832084
commons = []
20842085

2086+
config = Config(target_name, base_dir, app_config)
2087+
20852088
# Scan the directory for paths to probe for 'TESTS' folders
20862089
base_resources = Resources(MockNotifier(), collect_ignores=True)
2087-
base_resources.add_directory(base_dir)
2090+
base_resources.scan_with_config(base_dir, config)
20882091

20892092
dirs = [d for d in base_resources.ignored_dirs if basename(d) == 'TESTS']
2093+
ignoreset = MbedIgnoreSet()
2094+
20902095
for directory in dirs:
2096+
ignorefile = join(directory, IGNORE_FILENAME)
2097+
if isfile(ignorefile):
2098+
ignoreset.add_mbedignore(directory, ignorefile)
20912099
for test_group_directory in os.listdir(directory):
20922100
grp_dir = join(directory, test_group_directory)
2093-
if not isdir(grp_dir):
2101+
if not isdir(grp_dir) or ignoreset.is_ignored(grp_dir):
20942102
continue
2103+
grpignorefile = join(grp_dir, IGNORE_FILENAME)
2104+
if isfile(grpignorefile):
2105+
ignoreset.add_mbedignore(grp_dir, grpignorefile)
20952106
for test_case_directory in os.listdir(grp_dir):
20962107
d = join(directory, test_group_directory, test_case_directory)
2097-
if not isdir(d):
2108+
if not isdir(d) or ignoreset.is_ignored(d):
20982109
continue
20992110
special_dirs = ['host_tests', 'COMMON']
21002111
if test_group_directory not in special_dirs and test_case_directory not in special_dirs:

tools/toolchains/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ def get_symbols(self, for_asm=False):
202202
self.cxx_symbols += ["DEVICE_" + data + "=1" for data in self.target.device_has]
203203
# Add target's features
204204
self.cxx_symbols += ["FEATURE_" + data + "=1" for data in self.target.features]
205+
# Add target's components
206+
self.cxx_symbols += ["COMPONENT_" + data + "=1" for data in self.target.components]
205207
# Add extra symbols passed via 'macros' parameter
206208
self.cxx_symbols += self.macros
207209

@@ -221,6 +223,7 @@ def get_labels(self):
221223
self.labels = {
222224
'TARGET': self.target.labels,
223225
'FEATURE': self.target.features,
226+
'COMPONENT': self.target.components,
224227
'TOOLCHAIN': toolchain_labels
225228
}
226229

0 commit comments

Comments
 (0)