Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2 | """Zephyr Sanity Tests |
| 3 | |
| 4 | This script scans for the set of unit test applications in the git |
| 5 | repository and attempts to execute them. By default, it tries to |
| 6 | build each test case on one platform per architecture, using a precedence |
| 7 | list defined in an archtecture configuration file, and if possible |
| 8 | run the tests in the QEMU emulator. |
| 9 | |
| 10 | Test cases are detected by the presence of a 'testcase.ini' file in |
| 11 | the application's project directory. This file may contain one or |
| 12 | more blocks, each identifying a test scenario. The title of the block |
| 13 | is a name for the test case, which only needs to be unique for the |
| 14 | test cases specified in that testcase.ini file. The full canonical |
| 15 | name for each test case is <path to test case under samples/>/<block>. |
| 16 | |
| 17 | Each testcase.ini block can define the following key/value pairs: |
| 18 | |
| 19 | tags = <list of tags> (required) |
| 20 | A set of string tags for the testcase. Usually pertains to |
| 21 | functional domains but can be anything. Command line invocations |
| 22 | of this script can filter the set of tests to run based on tag. |
| 23 | |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 24 | skip = <True|False> (default False) |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 25 | skip testcase unconditionally. This can be used for broken tests. |
| 26 | |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 27 | slow = <True|False> (default False) |
| 28 | Don't run this test case unless --enable-slow was passed in on the |
| 29 | command line. Intended for time-consuming test cases that are only |
| 30 | run under certain circumstances, like daily builds. These test cases |
| 31 | are still compiled. |
| 32 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 33 | extra_args = <list of extra arguments> |
| 34 | Extra arguments to pass to Make when building or running the |
| 35 | test case. |
| 36 | |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 37 | build_only = <True|False> (default False) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 38 | If true, don't try to run the test under QEMU even if the |
| 39 | selected platform supports it. |
| 40 | |
| 41 | timeout = <number of seconds> |
| 42 | Length of time to run test in QEMU before automatically killing it. |
| 43 | Default to 60 seconds. |
| 44 | |
| 45 | arch_whitelist = <list of arches, such as x86, arm, arc> |
| 46 | Set of architectures that this test case should only be run for. |
| 47 | |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 48 | arch_exclude = <list of arches, such as x86, arm, arc> |
| 49 | Set of architectures that this test case should not run on. |
| 50 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 51 | platform_whitelist = <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 52 | Set of platforms that this test case should only be run for. |
| 53 | |
| 54 | platform_exclude = <list of platforms> |
| 55 | Set of platforms that this test case should not run on. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 56 | |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 57 | extra_sections = <list of extra binary sections> |
| 58 | When computing sizes, sanitycheck will report errors if it finds |
| 59 | extra, unexpected sections in the Zephyr binary unless they are named |
| 60 | here. They will not be included in the size calculation. |
| 61 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 62 | filter = <expression> |
| 63 | Filter whether the testcase should be run by evaluating an expression |
| 64 | against an environment containing the following values: |
| 65 | |
| 66 | { ARCH : <architecture>, |
| 67 | PLATFORM : <platform>, |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 68 | <all CONFIG_* key/value pairs in the test's generated defconfig>, |
| 69 | *<env>: any environment variable available |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | The grammar for the expression language is as follows: |
| 73 | |
| 74 | expression ::= expression "and" expression |
| 75 | | expression "or" expression |
| 76 | | "not" expression |
| 77 | | "(" expression ")" |
| 78 | | symbol "==" constant |
| 79 | | symbol "!=" constant |
| 80 | | symbol "<" number |
| 81 | | symbol ">" number |
| 82 | | symbol ">=" number |
| 83 | | symbol "<=" number |
| 84 | | symbol "in" list |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 85 | | symbol ":" string |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 86 | | symbol |
| 87 | |
| 88 | list ::= "[" list_contents "]" |
| 89 | |
| 90 | list_contents ::= constant |
| 91 | | list_contents "," constant |
| 92 | |
| 93 | constant ::= number |
| 94 | | string |
| 95 | |
| 96 | |
| 97 | For the case where expression ::= symbol, it evaluates to true |
| 98 | if the symbol is defined to a non-empty string. |
| 99 | |
| 100 | Operator precedence, starting from lowest to highest: |
| 101 | |
| 102 | or (left associative) |
| 103 | and (left associative) |
| 104 | not (right associative) |
| 105 | all comparison operators (non-associative) |
| 106 | |
| 107 | arch_whitelist, arch_exclude, platform_whitelist, platform_exclude |
| 108 | are all syntactic sugar for these expressions. For instance |
| 109 | |
| 110 | arch_exclude = x86 arc |
| 111 | |
| 112 | Is the same as: |
| 113 | |
| 114 | filter = not ARCH in ["x86", "arc"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 115 | |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 116 | The ':' operator compiles the string argument as a regular expression, |
| 117 | and then returns a true value only if the symbol's value in the environment |
| 118 | matches. For example, if CONFIG_SOC="quark_se" then |
| 119 | |
| 120 | filter = CONFIG_SOC : "quark.*" |
| 121 | |
| 122 | Would match it. |
| 123 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 124 | Architectures and platforms are defined in an archtecture configuration |
| 125 | file which are stored by default in scripts/sanity_chk/arches/. These |
| 126 | each define an [arch] block with the following key/value pairs: |
| 127 | |
| 128 | name = <arch name> |
| 129 | The name of the arch. Example: x86 |
| 130 | |
| 131 | platforms = <list of supported platforms in order of precedence> |
| 132 | List of supported platforms for this arch. The ordering here |
| 133 | is used to select a default platform to build for that arch. |
| 134 | |
| 135 | For every platform defined, there must be a corresponding block for it |
| 136 | in the arch configuration file. This block can be empty if there are |
| 137 | no special definitions for that arch. Options are: |
| 138 | |
| 139 | qemu_support = <True|False> (default False) |
| 140 | Indicates whether binaries for this platform can run under QEMU |
| 141 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 142 | The set of test cases that actually run depends on directives in the |
Paul Sokolovsky | cb2ae52 | 2017-03-13 18:05:11 +0300 | [diff] [blame] | 143 | testcase and architecture .ini file and options passed in on the command |
| 144 | line. If there is any confusion, running with -v or --discard-report |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 145 | can help show why particular test cases were skipped. |
| 146 | |
| 147 | Metrics (such as pass/fail state and binary size) for the last code |
| 148 | release are stored in scripts/sanity_chk/sanity_last_release.csv. |
| 149 | To update this, pass the --all --release options. |
| 150 | |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 151 | To load arguments from a file, write '+' before the file name, e.g., |
| 152 | +file_name. File content must be one or more valid arguments separated by |
| 153 | line break instead of white spaces. |
| 154 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 155 | Most everyday users will run with no arguments. |
| 156 | """ |
| 157 | |
| 158 | import argparse |
| 159 | import os |
| 160 | import sys |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 161 | import configparser |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 162 | import re |
| 163 | import tempfile |
| 164 | import subprocess |
| 165 | import multiprocessing |
| 166 | import select |
| 167 | import shutil |
| 168 | import signal |
| 169 | import threading |
| 170 | import time |
| 171 | import csv |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 172 | import glob |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 173 | import concurrent |
| 174 | import concurrent.futures |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame^] | 175 | import xml.etree.ElementTree as ET |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 176 | |
| 177 | if "ZEPHYR_BASE" not in os.environ: |
Anas Nashif | 427cdd3 | 2015-08-06 07:25:42 -0400 | [diff] [blame] | 178 | sys.stderr.write("$ZEPHYR_BASE environment variable undefined.\n") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 179 | exit(1) |
| 180 | ZEPHYR_BASE = os.environ["ZEPHYR_BASE"] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 181 | |
| 182 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/")) |
| 183 | |
| 184 | import expr_parser |
| 185 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 186 | VERBOSE = 0 |
| 187 | LAST_SANITY = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 188 | "last_sanity.csv") |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame^] | 189 | LAST_SANITY_XUNIT = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 190 | "last_sanity.xml") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 191 | RELEASE_DATA = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 192 | "sanity_last_release.csv") |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 193 | CPU_COUNTS = multiprocessing.cpu_count() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 194 | |
| 195 | if os.isatty(sys.stdout.fileno()): |
| 196 | TERMINAL = True |
| 197 | COLOR_NORMAL = '\033[0m' |
| 198 | COLOR_RED = '\033[91m' |
| 199 | COLOR_GREEN = '\033[92m' |
| 200 | COLOR_YELLOW = '\033[93m' |
| 201 | else: |
| 202 | TERMINAL = False |
| 203 | COLOR_NORMAL = "" |
| 204 | COLOR_RED = "" |
| 205 | COLOR_GREEN = "" |
| 206 | COLOR_YELLOW = "" |
| 207 | |
| 208 | class SanityCheckException(Exception): |
| 209 | pass |
| 210 | |
| 211 | class SanityRuntimeError(SanityCheckException): |
| 212 | pass |
| 213 | |
| 214 | class ConfigurationError(SanityCheckException): |
| 215 | def __init__(self, cfile, message): |
| 216 | self.cfile = cfile |
| 217 | self.message = message |
| 218 | |
| 219 | def __str__(self): |
| 220 | return repr(self.cfile + ": " + self.message) |
| 221 | |
| 222 | class MakeError(SanityCheckException): |
| 223 | pass |
| 224 | |
| 225 | class BuildError(MakeError): |
| 226 | pass |
| 227 | |
| 228 | class ExecutionError(MakeError): |
| 229 | pass |
| 230 | |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 231 | log_file = None |
| 232 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 233 | # Debug Functions |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 234 | def info(what): |
| 235 | sys.stdout.write(what + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 236 | if log_file: |
| 237 | log_file.write(what + "\n") |
| 238 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 239 | |
| 240 | def error(what): |
| 241 | sys.stderr.write(COLOR_RED + what + COLOR_NORMAL + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 242 | if log_file: |
| 243 | log_file(what + "\n") |
| 244 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 245 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 246 | def debug(what): |
| 247 | if VERBOSE >= 1: |
| 248 | info(what) |
| 249 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 250 | def verbose(what): |
| 251 | if VERBOSE >= 2: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 252 | info(what) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 253 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 254 | class Handler: |
| 255 | RUN_PASSED = "PROJECT EXECUTION SUCCESSFUL" |
| 256 | RUN_FAILED = "PROJECT EXECUTION FAILED" |
| 257 | def __init__(self, name, outdir, log_fn, timeout, unit=False): |
| 258 | """Constructor |
| 259 | |
| 260 | @param name Arbitrary name of the created thread |
| 261 | @param outdir Working directory, should be where qemu.pid gets created |
| 262 | by kbuild |
| 263 | @param log_fn Absolute path to write out QEMU's log data |
| 264 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 265 | the given number of seconds |
| 266 | """ |
| 267 | self.lock = threading.Lock() |
| 268 | self.state = "waiting" |
| 269 | self.metrics = {} |
| 270 | self.metrics["qemu_time"] = 0 |
| 271 | self.metrics["ram_size"] = 0 |
| 272 | self.metrics["rom_size"] = 0 |
| 273 | self.unit = unit |
| 274 | |
| 275 | def set_state(self, state, metrics): |
| 276 | self.lock.acquire() |
| 277 | self.state = state |
| 278 | self.metrics.update(metrics) |
| 279 | self.lock.release() |
| 280 | |
| 281 | def get_state(self): |
| 282 | self.lock.acquire() |
| 283 | ret = (self.state, self.metrics) |
| 284 | self.lock.release() |
| 285 | return ret |
| 286 | |
| 287 | class UnitHandler(Handler): |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 288 | def __init__(self, name, sourcedir, outdir, run_log, valgrind_log, timeout): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 289 | """Constructor |
| 290 | |
| 291 | @param name Arbitrary name of the created thread |
| 292 | @param outdir Working directory containing the test binary |
| 293 | @param run_log Absolute path to runtime logs |
| 294 | @param valgrind Absolute path to valgrind's log |
| 295 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 296 | the given number of seconds |
| 297 | """ |
| 298 | super().__init__(name, outdir, run_log, timeout, True) |
| 299 | |
| 300 | self.timeout = timeout |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 301 | self.sourcedir = sourcedir |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 302 | self.outdir = outdir |
| 303 | self.run_log = run_log |
| 304 | self.valgrind_log = valgrind_log |
| 305 | self.returncode = 0 |
| 306 | self.set_state("running", {}) |
| 307 | |
| 308 | def handle(self): |
| 309 | out_state = "failed" |
| 310 | |
| 311 | with open(self.run_log, "wt") as rl, open(self.valgrind_log, "wt") as vl: |
| 312 | try: |
| 313 | binary = os.path.join(self.outdir, "testbinary") |
| 314 | command = [binary] |
| 315 | if shutil.which("valgrind"): |
| 316 | command = ["valgrind", "--error-exitcode=2", |
| 317 | "--leak-check=full"] + command |
| 318 | returncode = subprocess.call(command, timeout=self.timeout, |
| 319 | stdout=rl, stderr=vl) |
| 320 | self.returncode = returncode |
| 321 | if returncode != 0: |
| 322 | if self.returncode == 1: |
| 323 | out_state = "failed" |
| 324 | else: |
| 325 | out_state = "failed valgrind" |
| 326 | else: |
| 327 | out_state = "passed" |
| 328 | except subprocess.TimeoutExpired: |
| 329 | out_state = "timeout" |
| 330 | self.returncode = 1 |
| 331 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 332 | returncode = subprocess.call(["GCOV_PREFIX=" + self.outdir, "gcov", self.sourcedir, "-s", self.outdir], shell=True) |
| 333 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 334 | self.set_state(out_state, {}) |
| 335 | |
| 336 | class QEMUHandler(Handler): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 337 | """Spawns a thread to monitor QEMU output from pipes |
| 338 | |
| 339 | We pass QEMU_PIPE to 'make qemu' and monitor the pipes for output. |
| 340 | We need to do this as once qemu starts, it runs forever until killed. |
| 341 | Test cases emit special messages to the console as they run, we check |
| 342 | for these to collect whether the test passed or failed. |
| 343 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 344 | |
| 345 | @staticmethod |
| 346 | def _thread(handler, timeout, outdir, logfile, fifo_fn, pid_fn, results): |
| 347 | fifo_in = fifo_fn + ".in" |
| 348 | fifo_out = fifo_fn + ".out" |
| 349 | |
| 350 | # These in/out nodes are named from QEMU's perspective, not ours |
| 351 | if os.path.exists(fifo_in): |
| 352 | os.unlink(fifo_in) |
| 353 | os.mkfifo(fifo_in) |
| 354 | if os.path.exists(fifo_out): |
| 355 | os.unlink(fifo_out) |
| 356 | os.mkfifo(fifo_out) |
| 357 | |
| 358 | # We don't do anything with out_fp but we need to open it for |
| 359 | # writing so that QEMU doesn't block, due to the way pipes work |
| 360 | out_fp = open(fifo_in, "wb") |
| 361 | # Disable internal buffering, we don't |
| 362 | # want read() or poll() to ever block if there is data in there |
| 363 | in_fp = open(fifo_out, "rb", buffering=0) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 364 | log_out_fp = open(logfile, "wt") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 365 | |
| 366 | start_time = time.time() |
| 367 | timeout_time = start_time + timeout |
| 368 | p = select.poll() |
| 369 | p.register(in_fp, select.POLLIN) |
| 370 | |
| 371 | metrics = {} |
| 372 | line = "" |
| 373 | while True: |
| 374 | this_timeout = int((timeout_time - time.time()) * 1000) |
| 375 | if this_timeout < 0 or not p.poll(this_timeout): |
| 376 | out_state = "timeout" |
| 377 | break |
| 378 | |
Genaro Saucedo Tejada | 2968b36 | 2016-08-09 15:11:53 -0500 | [diff] [blame] | 379 | try: |
| 380 | c = in_fp.read(1).decode("utf-8") |
| 381 | except UnicodeDecodeError: |
| 382 | # Test is writing something weird, fail |
| 383 | out_state = "unexpected byte" |
| 384 | break |
| 385 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 386 | if c == "": |
| 387 | # EOF, this shouldn't happen unless QEMU crashes |
| 388 | out_state = "unexpected eof" |
| 389 | break |
| 390 | line = line + c |
| 391 | if c != "\n": |
| 392 | continue |
| 393 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 394 | # line contains a full line of data output from QEMU |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 395 | log_out_fp.write(line) |
| 396 | log_out_fp.flush() |
| 397 | line = line.strip() |
| 398 | verbose("QEMU: %s" % line) |
| 399 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 400 | if line == handler.RUN_PASSED: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 401 | out_state = "passed" |
| 402 | break |
| 403 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 404 | if line == handler.RUN_FAILED: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 405 | out_state = "failed" |
| 406 | break |
| 407 | |
| 408 | # TODO: Add support for getting numerical performance data |
| 409 | # from test cases. Will involve extending test case reporting |
| 410 | # APIs. Add whatever gets reported to the metrics dictionary |
| 411 | line = "" |
| 412 | |
| 413 | metrics["qemu_time"] = time.time() - start_time |
| 414 | verbose("QEMU complete (%s) after %f seconds" % |
| 415 | (out_state, metrics["qemu_time"])) |
| 416 | handler.set_state(out_state, metrics) |
| 417 | |
| 418 | log_out_fp.close() |
| 419 | out_fp.close() |
| 420 | in_fp.close() |
| 421 | |
| 422 | pid = int(open(pid_fn).read()) |
| 423 | os.unlink(pid_fn) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 424 | try: |
| 425 | os.kill(pid, signal.SIGTERM) |
| 426 | except ProcessLookupError: |
| 427 | # Oh well, as long as it's dead! User probably sent Ctrl-C |
| 428 | pass |
| 429 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 430 | os.unlink(fifo_in) |
| 431 | os.unlink(fifo_out) |
| 432 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 433 | def __init__(self, name, outdir, log_fn, timeout): |
| 434 | """Constructor |
| 435 | |
| 436 | @param name Arbitrary name of the created thread |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 437 | @param outdir Working directory, should be where qemu.pid gets created |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 438 | by kbuild |
| 439 | @param log_fn Absolute path to write out QEMU's log data |
| 440 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 441 | the given number of seconds |
| 442 | """ |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 443 | super().__init__(name, outdir, log_fn, timeout) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 444 | self.results = {} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 445 | |
| 446 | # We pass this to QEMU which looks for fifos with .in and .out |
| 447 | # suffixes. |
| 448 | self.fifo_fn = os.path.join(outdir, "qemu-fifo") |
| 449 | |
| 450 | self.pid_fn = os.path.join(outdir, "qemu.pid") |
| 451 | if os.path.exists(self.pid_fn): |
| 452 | os.unlink(self.pid_fn) |
| 453 | |
| 454 | self.log_fn = log_fn |
| 455 | self.thread = threading.Thread(name=name, target=QEMUHandler._thread, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 456 | args=(self, timeout, outdir, |
| 457 | self.log_fn, self.fifo_fn, |
| 458 | self.pid_fn, self.results)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 459 | self.thread.daemon = True |
| 460 | verbose("Spawning QEMU process for %s" % name) |
| 461 | self.thread.start() |
| 462 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 463 | def get_fifo(self): |
| 464 | return self.fifo_fn |
| 465 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 466 | class SizeCalculator: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 467 | |
| 468 | alloc_sections = ["bss", "noinit"] |
Andrew Boie | 18ba153 | 2017-01-17 13:47:06 -0800 | [diff] [blame] | 469 | rw_sections = ["datas", "initlevel", "_k_task_list", "_k_event_list", |
Allan Stephens | 3f5c74c | 2016-11-01 14:37:15 -0500 | [diff] [blame] | 470 | "_k_memory_pool", "exceptions", "initshell", |
| 471 | "_static_thread_area", "_k_timer_area", |
| 472 | "_k_mem_slab_area", "_k_mem_pool_area", |
| 473 | "_k_sem_area", "_k_mutex_area", "_k_alert_area", |
| 474 | "_k_fifo_area", "_k_lifo_area", "_k_stack_area", |
Tomasz Bursztyka | b56c4df | 2016-08-18 14:07:22 +0200 | [diff] [blame] | 475 | "_k_msgq_area", "_k_mbox_area", "_k_pipe_area", |
Tomasz Bursztyka | e38a9e8 | 2017-03-08 09:30:03 +0100 | [diff] [blame] | 476 | "net_if", "net_if_event", "net_stack", "net_l2_data", |
Anas Nashif | 6d72e63 | 2017-02-27 19:49:03 -0500 | [diff] [blame] | 477 | "_k_queue_area"] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 478 | # These get copied into RAM only on non-XIP |
Andrew Boie | 3b93021 | 2016-06-07 13:11:45 -0700 | [diff] [blame] | 479 | ro_sections = ["text", "ctors", "init_array", "reset", |
Anas Nashif | bfcdfaf | 2016-12-15 10:02:14 -0500 | [diff] [blame] | 480 | "rodata", "devconfig", "net_l2", "vector"] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 481 | |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 482 | def __init__(self, filename, extra_sections): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 483 | """Constructor |
| 484 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 485 | @param filename Path to the output binary |
| 486 | The <filename> is parsed by objdump to determine section sizes |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 487 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 488 | # Make sure this is an ELF binary |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 489 | with open(filename, "rb") as f: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 490 | magic = f.read(4) |
| 491 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 492 | if (magic != b'\x7fELF'): |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 493 | raise SanityRuntimeError("%s is not an ELF binary" % filename) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 494 | |
| 495 | # Search for CONFIG_XIP in the ELF's list of symbols using NM and AWK. |
| 496 | # GREP can not be used as it returns an error if the symbol is not found. |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 497 | is_xip_command = "nm " + filename + " | awk '/CONFIG_XIP/ { print $3 }'" |
Andrew Boie | 8f0211d | 2016-03-02 20:40:29 -0800 | [diff] [blame] | 498 | is_xip_output = subprocess.check_output(is_xip_command, shell=True, |
| 499 | stderr=subprocess.STDOUT).decode("utf-8").strip() |
| 500 | if is_xip_output.endswith("no symbols"): |
| 501 | raise SanityRuntimeError("%s has no symbol information" % filename) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 502 | self.is_xip = (len(is_xip_output) != 0) |
| 503 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 504 | self.filename = filename |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 505 | self.sections = [] |
| 506 | self.rom_size = 0 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 507 | self.ram_size = 0 |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 508 | self.extra_sections = extra_sections |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 509 | |
| 510 | self._calculate_sizes() |
| 511 | |
| 512 | def get_ram_size(self): |
| 513 | """Get the amount of RAM the application will use up on the device |
| 514 | |
| 515 | @return amount of RAM, in bytes |
| 516 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 517 | return self.ram_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 518 | |
| 519 | def get_rom_size(self): |
| 520 | """Get the size of the data that this application uses on device's flash |
| 521 | |
| 522 | @return amount of ROM, in bytes |
| 523 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 524 | return self.rom_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 525 | |
| 526 | def unrecognized_sections(self): |
| 527 | """Get a list of sections inside the binary that weren't recognized |
| 528 | |
| 529 | @return list of unrecogized section names |
| 530 | """ |
| 531 | slist = [] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 532 | for v in self.sections: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 533 | if not v["recognized"]: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 534 | slist.append(v["name"]) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 535 | return slist |
| 536 | |
| 537 | def _calculate_sizes(self): |
| 538 | """ Calculate RAM and ROM usage by section """ |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 539 | objdump_command = "objdump -h " + self.filename |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 540 | objdump_output = subprocess.check_output(objdump_command, |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 541 | shell=True).decode("utf-8").splitlines() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 542 | |
| 543 | for line in objdump_output: |
| 544 | words = line.split() |
| 545 | |
| 546 | if (len(words) == 0): # Skip lines that are too short |
| 547 | continue |
| 548 | |
| 549 | index = words[0] |
| 550 | if (not index[0].isdigit()): # Skip lines that do not start |
| 551 | continue # with a digit |
| 552 | |
| 553 | name = words[1] # Skip lines with section names |
| 554 | if (name[0] == '.'): # starting with '.' |
| 555 | continue |
| 556 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 557 | # TODO this doesn't actually reflect the size in flash or RAM as |
| 558 | # it doesn't include linker-imposed padding between sections. |
| 559 | # It is close though. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 560 | size = int(words[2], 16) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 561 | if size == 0: |
| 562 | continue |
| 563 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 564 | load_addr = int(words[4], 16) |
| 565 | virt_addr = int(words[3], 16) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 566 | |
| 567 | # Add section to memory use totals (for both non-XIP and XIP scenarios) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 568 | # Unrecognized section names are not included in the calculations. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 569 | recognized = True |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 570 | if name in SizeCalculator.alloc_sections: |
| 571 | self.ram_size += size |
| 572 | stype = "alloc" |
| 573 | elif name in SizeCalculator.rw_sections: |
| 574 | self.ram_size += size |
| 575 | self.rom_size += size |
| 576 | stype = "rw" |
| 577 | elif name in SizeCalculator.ro_sections: |
| 578 | self.rom_size += size |
| 579 | if not self.is_xip: |
| 580 | self.ram_size += size |
| 581 | stype = "ro" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 582 | else: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 583 | stype = "unknown" |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 584 | if name not in self.extra_sections: |
| 585 | recognized = False |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 586 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 587 | self.sections.append({"name" : name, "load_addr" : load_addr, |
| 588 | "size" : size, "virt_addr" : virt_addr, |
Andy Ross | 8d801a5 | 2016-10-04 11:48:21 -0700 | [diff] [blame] | 589 | "type" : stype, "recognized" : recognized}) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 590 | |
| 591 | |
| 592 | class MakeGoal: |
| 593 | """Metadata class representing one of the sub-makes called by MakeGenerator |
| 594 | |
| 595 | MakeGenerator returns a dictionary of these which can then be associdated |
| 596 | with TestInstances to get a complete picture of what happened during a test. |
| 597 | MakeGenerator is used for tasks outside of building tests (such as |
| 598 | defconfigs) which is why MakeGoal is a separate class from TestInstance. |
| 599 | """ |
| 600 | def __init__(self, name, text, qemu, make_log, build_log, run_log, |
| 601 | qemu_log): |
| 602 | self.name = name |
| 603 | self.text = text |
| 604 | self.qemu = qemu |
| 605 | self.make_log = make_log |
| 606 | self.build_log = build_log |
| 607 | self.run_log = run_log |
| 608 | self.qemu_log = qemu_log |
| 609 | self.make_state = "waiting" |
| 610 | self.failed = False |
| 611 | self.finished = False |
| 612 | self.reason = None |
| 613 | self.metrics = {} |
| 614 | |
| 615 | def get_error_log(self): |
| 616 | if self.make_state == "waiting": |
| 617 | # Shouldn't ever see this; breakage in the main Makefile itself. |
| 618 | return self.make_log |
| 619 | elif self.make_state == "building": |
| 620 | # Failure when calling the sub-make to build the code |
| 621 | return self.build_log |
| 622 | elif self.make_state == "running": |
| 623 | # Failure in sub-make for "make qemu", qemu probably failed to start |
| 624 | return self.run_log |
| 625 | elif self.make_state == "finished": |
| 626 | # QEMU finished, but timed out or otherwise wasn't successful |
| 627 | return self.qemu_log |
| 628 | |
| 629 | def fail(self, reason): |
| 630 | self.failed = True |
| 631 | self.finished = True |
| 632 | self.reason = reason |
| 633 | |
| 634 | def success(self): |
| 635 | self.finished = True |
| 636 | |
| 637 | def __str__(self): |
| 638 | if self.finished: |
| 639 | if self.failed: |
| 640 | return "[%s] failed (%s: see %s)" % (self.name, self.reason, |
| 641 | self.get_error_log()) |
| 642 | else: |
| 643 | return "[%s] passed" % self.name |
| 644 | else: |
| 645 | return "[%s] in progress (%s)" % (self.name, self.make_state) |
| 646 | |
| 647 | |
| 648 | class MakeGenerator: |
| 649 | """Generates a Makefile which just calls a bunch of sub-make sessions |
| 650 | |
| 651 | In any given test suite we may need to build dozens if not hundreds of |
| 652 | test cases. The cleanest way to parallelize this is to just let Make |
| 653 | do the parallelization, sharing the jobserver among all the different |
| 654 | sub-make targets. |
| 655 | """ |
| 656 | |
| 657 | GOAL_HEADER_TMPL = """.PHONY: {goal} |
| 658 | {goal}: |
| 659 | """ |
| 660 | |
| 661 | MAKE_RULE_TMPL = """\t@echo sanity_test_{phase} {goal} >&2 |
Andrew Boie | f7a6e28 | 2017-02-02 13:04:57 -0800 | [diff] [blame] | 662 | \t$(MAKE) -C {directory} O={outdir} V={verb} EXTRA_CFLAGS="-Werror {cflags}" EXTRA_ASMFLAGS=-Wa,--fatal-warnings EXTRA_LDFLAGS=--fatal-warnings {args} >{logfile} 2>&1 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 663 | """ |
| 664 | |
| 665 | GOAL_FOOTER_TMPL = "\t@echo sanity_test_finished {goal} >&2\n\n" |
| 666 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 667 | re_make = re.compile("sanity_test_([A-Za-z0-9]+) (.+)|$|make[:] \*\*\* \[(.+:.+: )?(.+)\] Error.+$") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 668 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 669 | def __init__(self, base_outdir, asserts=False, deprecations=False, ccache=0): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 670 | """MakeGenerator constructor |
| 671 | |
| 672 | @param base_outdir Intended to be the base out directory. A make.log |
| 673 | file will be created here which contains the output of the |
| 674 | top-level Make session, as well as the dynamic control Makefile |
| 675 | @param verbose If true, pass V=1 to all the sub-makes which greatly |
| 676 | increases their verbosity |
| 677 | """ |
| 678 | self.goals = {} |
| 679 | if not os.path.exists(base_outdir): |
| 680 | os.makedirs(base_outdir) |
| 681 | self.logfile = os.path.join(base_outdir, "make.log") |
| 682 | self.makefile = os.path.join(base_outdir, "Makefile") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 683 | self.asserts = asserts |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 684 | self.deprecations = deprecations |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 685 | self.ccache = ccache |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 686 | |
| 687 | def _get_rule_header(self, name): |
| 688 | return MakeGenerator.GOAL_HEADER_TMPL.format(goal=name) |
| 689 | |
| 690 | def _get_sub_make(self, name, phase, workdir, outdir, logfile, args): |
| 691 | verb = "1" if VERBOSE else "0" |
| 692 | args = " ".join(args) |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 693 | |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 694 | if self.asserts: |
| 695 | cflags="-DCONFIG_ASSERT=1 -D__ASSERT_ON=2" |
| 696 | else: |
| 697 | cflags="" |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 698 | |
| 699 | if self.deprecations: |
| 700 | cflags = cflags + " -Wno-deprecated-declarations" |
Andrew Boie | f7a6e28 | 2017-02-02 13:04:57 -0800 | [diff] [blame] | 701 | |
| 702 | if self.ccache: |
| 703 | args = args + " USE_CCACHE=1" |
| 704 | |
| 705 | return MakeGenerator.MAKE_RULE_TMPL.format(phase=phase, goal=name, |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 706 | outdir=outdir, cflags=cflags, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 707 | directory=workdir, verb=verb, |
| 708 | args=args, logfile=logfile) |
| 709 | |
| 710 | def _get_rule_footer(self, name): |
| 711 | return MakeGenerator.GOAL_FOOTER_TMPL.format(goal=name) |
| 712 | |
| 713 | def _add_goal(self, outdir): |
| 714 | if not os.path.exists(outdir): |
| 715 | os.makedirs(outdir) |
| 716 | |
| 717 | def add_build_goal(self, name, directory, outdir, args): |
| 718 | """Add a goal to invoke a Kbuild session |
| 719 | |
| 720 | @param name A unique string name for this build goal. The results |
| 721 | dictionary returned by execute() will be keyed by this name. |
| 722 | @param directory Absolute path to working directory, will be passed |
| 723 | to make -C |
| 724 | @param outdir Absolute path to output directory, will be passed to |
| 725 | Kbuild via -O=<path> |
| 726 | @param args Extra command line arguments to pass to 'make', typically |
| 727 | environment variables or specific Make goals |
| 728 | """ |
| 729 | self._add_goal(outdir) |
| 730 | build_logfile = os.path.join(outdir, "build.log") |
| 731 | text = (self._get_rule_header(name) + |
| 732 | self._get_sub_make(name, "building", directory, |
| 733 | outdir, build_logfile, args) + |
| 734 | self._get_rule_footer(name)) |
| 735 | self.goals[name] = MakeGoal(name, text, None, self.logfile, build_logfile, |
| 736 | None, None) |
| 737 | |
| 738 | def add_qemu_goal(self, name, directory, outdir, args, timeout=30): |
| 739 | """Add a goal to build a Zephyr project and then run it under QEMU |
| 740 | |
| 741 | The generated make goal invokes Make twice, the first time it will |
| 742 | build the default goal, and the second will invoke the 'qemu' goal. |
| 743 | The output of the QEMU session will be monitored, and terminated |
| 744 | either upon pass/fail result of the test program, or the timeout |
| 745 | is reached. |
| 746 | |
| 747 | @param name A unique string name for this build goal. The results |
| 748 | dictionary returned by execute() will be keyed by this name. |
| 749 | @param directory Absolute path to working directory, will be passed |
| 750 | to make -C |
| 751 | @param outdir Absolute path to output directory, will be passed to |
| 752 | Kbuild via -O=<path> |
| 753 | @param args Extra command line arguments to pass to 'make', typically |
| 754 | environment variables. Do not pass specific Make goals here. |
| 755 | @param timeout Maximum length of time QEMU session should be allowed |
| 756 | to run before automatically killing it. Default is 30 seconds. |
| 757 | """ |
| 758 | |
| 759 | self._add_goal(outdir) |
| 760 | build_logfile = os.path.join(outdir, "build.log") |
| 761 | run_logfile = os.path.join(outdir, "run.log") |
| 762 | qemu_logfile = os.path.join(outdir, "qemu.log") |
| 763 | |
| 764 | q = QEMUHandler(name, outdir, qemu_logfile, timeout) |
| 765 | args.append("QEMU_PIPE=%s" % q.get_fifo()) |
| 766 | text = (self._get_rule_header(name) + |
| 767 | self._get_sub_make(name, "building", directory, |
| 768 | outdir, build_logfile, args) + |
| 769 | self._get_sub_make(name, "running", directory, |
| 770 | outdir, run_logfile, |
Mazen NEIFER | 7f04637 | 2017-01-31 12:41:33 +0100 | [diff] [blame] | 771 | args + ["run"]) + |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 772 | self._get_rule_footer(name)) |
| 773 | self.goals[name] = MakeGoal(name, text, q, self.logfile, build_logfile, |
| 774 | run_logfile, qemu_logfile) |
| 775 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 776 | def add_unit_goal(self, name, directory, outdir, args, timeout=30, coverage=False): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 777 | self._add_goal(outdir) |
| 778 | build_logfile = os.path.join(outdir, "build.log") |
| 779 | run_logfile = os.path.join(outdir, "run.log") |
| 780 | qemu_logfile = os.path.join(outdir, "qemu.log") |
| 781 | valgrind_logfile = os.path.join(outdir, "valgrind.log") |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 782 | if coverage: |
| 783 | args += ["COVERAGE=1"] |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 784 | |
| 785 | # we handle running in the UnitHandler class |
| 786 | text = (self._get_rule_header(name) + |
| 787 | self._get_sub_make(name, "building", directory, |
| 788 | outdir, build_logfile, args) + |
| 789 | self._get_rule_footer(name)) |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 790 | q = UnitHandler(name, directory, outdir, run_logfile, valgrind_logfile, timeout) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 791 | self.goals[name] = MakeGoal(name, text, q, self.logfile, build_logfile, |
| 792 | run_logfile, valgrind_logfile) |
| 793 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 794 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 795 | def add_test_instance(self, ti, build_only=False, enable_slow=False, coverage=False, |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 796 | extra_args=[]): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 797 | """Add a goal to build/test a TestInstance object |
| 798 | |
| 799 | @param ti TestInstance object to build. The status dictionary returned |
| 800 | by execute() will be keyed by its .name field. |
| 801 | """ |
| 802 | args = ti.test.extra_args[:] |
| 803 | args.extend(["ARCH=%s" % ti.platform.arch.name, |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 804 | "BOARD=%s" % ti.platform.name]) |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 805 | args.extend(extra_args) |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 806 | if (ti.platform.qemu_support and (not ti.build_only) and |
| 807 | (not build_only) and (enable_slow or not ti.test.slow)): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 808 | self.add_qemu_goal(ti.name, ti.test.code_location, ti.outdir, |
| 809 | args, ti.test.timeout) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 810 | elif ti.test.type == "unit": |
| 811 | self.add_unit_goal(ti.name, ti.test.code_location, ti.outdir, |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 812 | args, ti.test.timeout, coverage) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 813 | else: |
| 814 | self.add_build_goal(ti.name, ti.test.code_location, ti.outdir, args) |
| 815 | |
| 816 | def execute(self, callback_fn=None, context=None): |
| 817 | """Execute all the registered build goals |
| 818 | |
| 819 | @param callback_fn If not None, a callback function will be called |
| 820 | as individual goals transition between states. This function |
| 821 | should accept two parameters: a string state and an arbitrary |
| 822 | context object, supplied here |
| 823 | @param context Context object to pass to the callback function. |
| 824 | Type and semantics are specific to that callback function. |
| 825 | @return A dictionary mapping goal names to final status. |
| 826 | """ |
| 827 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 828 | with open(self.makefile, "wt") as tf, \ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 829 | open(os.devnull, "wb") as devnull, \ |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 830 | open(self.logfile, "wt") as make_log: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 831 | # Create our dynamic Makefile and execute it. |
| 832 | # Watch stderr output which is where we will keep |
| 833 | # track of build state |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 834 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 835 | tf.write(goal.text) |
| 836 | tf.write("all: %s\n" % (" ".join(self.goals.keys()))) |
| 837 | tf.flush() |
| 838 | |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 839 | cmd = ["make", "-k", "-j", str(CPU_COUNTS * 2), "-f", tf.name, "all"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 840 | p = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
| 841 | stdout=devnull) |
| 842 | |
| 843 | for line in iter(p.stderr.readline, b''): |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 844 | line = line.decode("utf-8") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 845 | make_log.write(line) |
| 846 | verbose("MAKE: " + repr(line.strip())) |
| 847 | m = MakeGenerator.re_make.match(line) |
| 848 | if not m: |
| 849 | continue |
| 850 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 851 | state, name, _, error = m.groups() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 852 | if error: |
| 853 | goal = self.goals[error] |
| 854 | else: |
| 855 | goal = self.goals[name] |
| 856 | goal.make_state = state |
| 857 | |
| 858 | |
| 859 | if error: |
Andrew Boie | 822b087 | 2017-01-10 13:32:40 -0800 | [diff] [blame] | 860 | # Sometimes QEMU will run an image and then crash out, which |
| 861 | # will cause the 'make qemu' invocation to exit with |
| 862 | # nonzero status. |
| 863 | # Need to distinguish this case from a compilation failure. |
| 864 | if goal.qemu: |
| 865 | goal.fail("qemu_crash") |
| 866 | else: |
| 867 | goal.fail("build_error") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 868 | else: |
| 869 | if state == "finished": |
| 870 | if goal.qemu: |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 871 | if goal.qemu.unit: |
| 872 | # We can't run unit tests with Make |
| 873 | goal.qemu.handle() |
| 874 | if goal.qemu.returncode == 2: |
| 875 | goal.qemu_log = goal.qemu.valgrind_log |
| 876 | elif goal.qemu.returncode: |
| 877 | goal.qemu_log = goal.qemu.run_log |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 878 | thread_status, metrics = goal.qemu.get_state() |
| 879 | goal.metrics.update(metrics) |
| 880 | if thread_status == "passed": |
| 881 | goal.success() |
| 882 | else: |
| 883 | goal.fail(thread_status) |
| 884 | else: |
| 885 | goal.success() |
| 886 | |
| 887 | if callback_fn: |
| 888 | callback_fn(context, self.goals, goal) |
| 889 | |
| 890 | p.wait() |
| 891 | return self.goals |
| 892 | |
| 893 | |
| 894 | # "list" - List of strings |
| 895 | # "list:<type>" - List of <type> |
| 896 | # "set" - Set of unordered, unique strings |
| 897 | # "set:<type>" - Set of <type> |
| 898 | # "float" - Floating point |
| 899 | # "int" - Integer |
| 900 | # "bool" - Boolean |
| 901 | # "str" - String |
| 902 | |
| 903 | # XXX Be sure to update __doc__ if you change any of this!! |
| 904 | |
| 905 | arch_valid_keys = {"name" : {"type" : "str", "required" : True}, |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 906 | "platforms" : {"type" : "list", "required" : True}, |
| 907 | "supported_toolchains" : {"type" : "list", "required" : True}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 908 | |
| 909 | platform_valid_keys = {"qemu_support" : {"type" : "bool", "default" : False}, |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 910 | "supported_toolchains" : {"type" : "list", "default" : []}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 911 | |
| 912 | testcase_valid_keys = {"tags" : {"type" : "set", "required" : True}, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 913 | "type" : {"type" : "str", "default": "integration"}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 914 | "extra_args" : {"type" : "list"}, |
| 915 | "build_only" : {"type" : "bool", "default" : False}, |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 916 | "skip" : {"type" : "bool", "default" : False}, |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 917 | "slow" : {"type" : "bool", "default" : False}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 918 | "timeout" : {"type" : "int", "default" : 60}, |
| 919 | "arch_whitelist" : {"type" : "set"}, |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 920 | "arch_exclude" : {"type" : "set"}, |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 921 | "extra_sections" : {"type" : "list", "default" : []}, |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 922 | "platform_exclude" : {"type" : "set"}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 923 | "platform_whitelist" : {"type" : "set"}, |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 924 | "filter" : {"type" : "str"}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 925 | |
| 926 | |
| 927 | class SanityConfigParser: |
| 928 | """Class to read architecture and test case .ini files with semantic checking |
| 929 | """ |
| 930 | def __init__(self, filename): |
| 931 | """Instantiate a new SanityConfigParser object |
| 932 | |
| 933 | @param filename Source .ini file to read |
| 934 | """ |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 935 | cp = configparser.SafeConfigParser() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 936 | cp.readfp(open(filename)) |
| 937 | self.filename = filename |
| 938 | self.cp = cp |
| 939 | |
| 940 | def _cast_value(self, value, typestr): |
| 941 | v = value.strip() |
| 942 | if typestr == "str": |
| 943 | return v |
| 944 | |
| 945 | elif typestr == "float": |
| 946 | return float(v) |
| 947 | |
| 948 | elif typestr == "int": |
| 949 | return int(v) |
| 950 | |
| 951 | elif typestr == "bool": |
| 952 | v = v.lower() |
| 953 | if v == "true" or v == "1": |
| 954 | return True |
| 955 | elif v == "" or v == "false" or v == "0": |
| 956 | return False |
| 957 | raise ConfigurationError(self.filename, |
| 958 | "bad value for boolean: '%s'" % value) |
| 959 | |
| 960 | elif typestr.startswith("list"): |
| 961 | vs = v.split() |
| 962 | if len(typestr) > 4 and typestr[4] == ":": |
| 963 | return [self._cast_value(vsi, typestr[5:]) for vsi in vs] |
| 964 | else: |
| 965 | return vs |
| 966 | |
| 967 | elif typestr.startswith("set"): |
| 968 | vs = v.split() |
| 969 | if len(typestr) > 3 and typestr[3] == ":": |
| 970 | return set([self._cast_value(vsi, typestr[4:]) for vsi in vs]) |
| 971 | else: |
| 972 | return set(vs) |
| 973 | |
| 974 | else: |
| 975 | raise ConfigurationError(self.filename, "unknown type '%s'" % value) |
| 976 | |
| 977 | |
| 978 | def sections(self): |
| 979 | """Get the set of sections within the .ini file |
| 980 | |
| 981 | @return a list of string section names""" |
| 982 | return self.cp.sections() |
| 983 | |
| 984 | def get_section(self, section, valid_keys): |
| 985 | """Get a dictionary representing the keys/values within a section |
| 986 | |
| 987 | @param section The section in the .ini file to retrieve data from |
| 988 | @param valid_keys A dictionary representing the intended semantics |
| 989 | for this section. Each key in this dictionary is a key that could |
| 990 | be specified, if a key is given in the .ini file which isn't in |
| 991 | here, it will generate an error. Each value in this dictionary |
| 992 | is another dictionary containing metadata: |
| 993 | |
| 994 | "default" - Default value if not given |
| 995 | "type" - Data type to convert the text value to. Simple types |
| 996 | supported are "str", "float", "int", "bool" which will get |
| 997 | converted to respective Python data types. "set" and "list" |
| 998 | may also be specified which will split the value by |
| 999 | whitespace (but keep the elements as strings). finally, |
| 1000 | "list:<type>" and "set:<type>" may be given which will |
| 1001 | perform a type conversion after splitting the value up. |
| 1002 | "required" - If true, raise an error if not defined. If false |
| 1003 | and "default" isn't specified, a type conversion will be |
| 1004 | done on an empty string |
| 1005 | @return A dictionary containing the section key-value pairs with |
| 1006 | type conversion and default values filled in per valid_keys |
| 1007 | """ |
| 1008 | |
| 1009 | d = {} |
| 1010 | cp = self.cp |
| 1011 | |
| 1012 | if not cp.has_section(section): |
Andrew Boie | e57a1e5 | 2016-03-22 10:29:14 -0700 | [diff] [blame] | 1013 | # Just fill it with defaults |
| 1014 | cp.add_section(section) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1015 | |
| 1016 | for k, v in cp.items(section): |
| 1017 | if k not in valid_keys: |
| 1018 | raise ConfigurationError(self.filename, |
| 1019 | "Unknown config key '%s' in defintiion for '%s'" |
| 1020 | % (k, section)) |
| 1021 | d[k] = v |
| 1022 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1023 | for k, kinfo in valid_keys.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1024 | if k not in d: |
| 1025 | if "required" in kinfo: |
| 1026 | required = kinfo["required"] |
| 1027 | else: |
| 1028 | required = False |
| 1029 | |
| 1030 | if required: |
| 1031 | raise ConfigurationError(self.filename, |
| 1032 | "missing required value for '%s' in section '%s'" |
| 1033 | % (k, section)) |
| 1034 | else: |
| 1035 | if "default" in kinfo: |
| 1036 | default = kinfo["default"] |
| 1037 | else: |
| 1038 | default = self._cast_value("", kinfo["type"]) |
| 1039 | d[k] = default |
| 1040 | else: |
| 1041 | try: |
| 1042 | d[k] = self._cast_value(d[k], kinfo["type"]) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1043 | except ValueError as ve: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1044 | raise ConfigurationError(self.filename, |
| 1045 | "bad %s value '%s' for key '%s' in section '%s'" |
| 1046 | % (kinfo["type"], d[k], k, section)) |
| 1047 | |
| 1048 | return d |
| 1049 | |
| 1050 | |
| 1051 | class Platform: |
| 1052 | """Class representing metadata for a particular platform |
| 1053 | |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1054 | Maps directly to BOARD when building""" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1055 | def __init__(self, arch, name, plat_dict): |
| 1056 | """Constructor. |
| 1057 | |
| 1058 | @param arch Architecture object for this platform |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1059 | @param name String name for this platform, same as BOARD |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1060 | @param plat_dict SanityConfigParser output on the relevant section |
| 1061 | in the architecture configuration file which has lots of metadata. |
| 1062 | See the Architecture class. |
| 1063 | """ |
| 1064 | self.name = name |
| 1065 | self.qemu_support = plat_dict["qemu_support"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1066 | self.arch = arch |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1067 | self.supported_toolchains = arch.supported_toolchains |
| 1068 | if plat_dict["supported_toolchains"]: |
| 1069 | self.supported_toolchains = plat_dict["supported_toolchains"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1070 | # Gets populated in a separate step |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1071 | self.defconfig = None |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1072 | pass |
| 1073 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1074 | def __repr__(self): |
| 1075 | return "<%s on %s>" % (self.name, self.arch.name) |
| 1076 | |
| 1077 | |
| 1078 | class Architecture: |
| 1079 | """Class representing metadata for a particular architecture |
| 1080 | """ |
| 1081 | def __init__(self, cfile): |
| 1082 | """Architecture constructor |
| 1083 | |
| 1084 | @param cfile Path to Architecture configuration file, which gives |
| 1085 | info about the arch and all the platforms for it |
| 1086 | """ |
| 1087 | cp = SanityConfigParser(cfile) |
| 1088 | self.platforms = [] |
| 1089 | |
| 1090 | arch = cp.get_section("arch", arch_valid_keys) |
| 1091 | |
| 1092 | self.name = arch["name"] |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1093 | self.supported_toolchains = arch["supported_toolchains"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1094 | |
| 1095 | for plat_name in arch["platforms"]: |
| 1096 | verbose("Platform: %s" % plat_name) |
| 1097 | plat_dict = cp.get_section(plat_name, platform_valid_keys) |
| 1098 | self.platforms.append(Platform(self, plat_name, plat_dict)) |
| 1099 | |
| 1100 | def __repr__(self): |
| 1101 | return "<arch %s>" % self.name |
| 1102 | |
| 1103 | |
| 1104 | class TestCase: |
| 1105 | """Class representing a test application |
| 1106 | """ |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1107 | def __init__(self, testcase_root, workdir, name, tc_dict, inifile): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1108 | """TestCase constructor. |
| 1109 | |
| 1110 | This gets called by TestSuite as it finds and reads testcase.ini files. |
| 1111 | Multiple TestCase instances may be generated from a single testcase.ini, |
| 1112 | each one corresponds to a section within that file. |
| 1113 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1114 | We need to have a unique name for every single test case. Since |
| 1115 | a testcase.ini can define multiple tests, the canonical name for |
| 1116 | the test case is <workdir>/<name>. |
| 1117 | |
| 1118 | @param testcase_root Absolute path to the root directory where |
| 1119 | all the test cases live |
| 1120 | @param workdir Relative path to the project directory for this |
| 1121 | test application from the test_case root. |
| 1122 | @param name Name of this test case, corresponding to the section name |
| 1123 | in the test case configuration file. For many test cases that just |
| 1124 | define one test, can be anything and is usually "test". This is |
| 1125 | really only used to distinguish between different cases when |
| 1126 | the testcase.ini defines multiple tests |
| 1127 | @param tc_dict Dictionary with section values for this test case |
| 1128 | from the testcase.ini file |
| 1129 | """ |
| 1130 | self.code_location = os.path.join(testcase_root, workdir) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1131 | self.type = tc_dict["type"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1132 | self.tags = tc_dict["tags"] |
| 1133 | self.extra_args = tc_dict["extra_args"] |
| 1134 | self.arch_whitelist = tc_dict["arch_whitelist"] |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1135 | self.arch_exclude = tc_dict["arch_exclude"] |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1136 | self.skip = tc_dict["skip"] |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1137 | self.platform_exclude = tc_dict["platform_exclude"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1138 | self.platform_whitelist = tc_dict["platform_whitelist"] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1139 | self.tc_filter = tc_dict["filter"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1140 | self.timeout = tc_dict["timeout"] |
| 1141 | self.build_only = tc_dict["build_only"] |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1142 | self.slow = tc_dict["slow"] |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1143 | self.extra_sections = tc_dict["extra_sections"] |
Andrew Boie | 14ac902 | 2016-04-08 13:31:53 -0700 | [diff] [blame] | 1144 | self.path = os.path.join(os.path.basename(os.path.abspath(testcase_root)), |
| 1145 | workdir, name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1146 | self.name = self.path # for now |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1147 | self.defconfig = {} |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1148 | self.inifile = inifile |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1149 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1150 | def __repr__(self): |
| 1151 | return self.name |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | class TestInstance: |
| 1156 | """Class representing the execution of a particular TestCase on a platform |
| 1157 | |
| 1158 | @param test The TestCase object we want to build/execute |
| 1159 | @param platform Platform object that we want to build and run against |
| 1160 | @param base_outdir Base directory for all test results. The actual |
| 1161 | out directory used is <outdir>/<platform>/<test case name> |
| 1162 | """ |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1163 | def __init__(self, test, platform, base_outdir, build_only=False, |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1164 | slow=False, coverage=False): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1165 | self.test = test |
| 1166 | self.platform = platform |
| 1167 | self.name = os.path.join(platform.name, test.path) |
| 1168 | self.outdir = os.path.join(base_outdir, platform.name, test.path) |
| 1169 | self.build_only = build_only or test.build_only |
| 1170 | |
| 1171 | def calculate_sizes(self): |
| 1172 | """Get the RAM/ROM sizes of a test case. |
| 1173 | |
| 1174 | This can only be run after the instance has been executed by |
| 1175 | MakeGenerator, otherwise there won't be any binaries to measure. |
| 1176 | |
| 1177 | @return A SizeCalculator object |
| 1178 | """ |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1179 | fns = glob.glob(os.path.join(self.outdir, "*.elf")) |
Anas Nashif | f8dcad4 | 2016-10-27 18:10:08 -0400 | [diff] [blame] | 1180 | fns = [x for x in fns if not x.endswith('_prebuilt.elf')] |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1181 | if (len(fns) != 1): |
| 1182 | raise BuildError("Missing/multiple output ELF binary") |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1183 | return SizeCalculator(fns[0], self.test.extra_sections) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1184 | |
| 1185 | def __repr__(self): |
| 1186 | return "<TestCase %s on %s>" % (self.test.name, self.platform.name) |
| 1187 | |
| 1188 | |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1189 | def defconfig_cb(context, goals, goal): |
| 1190 | if not goal.failed: |
| 1191 | return |
| 1192 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1193 | |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1194 | info("%sCould not build defconfig for %s%s" % |
| 1195 | (COLOR_RED, goal.name, COLOR_NORMAL)); |
| 1196 | if INLINE_LOGS: |
| 1197 | with open(goal.get_error_log()) as fp: |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1198 | data = fp.read() |
| 1199 | sys.stdout.write(data) |
| 1200 | if log_file: |
| 1201 | log_file.write(data) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1202 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1203 | info("\tsee: " + COLOR_YELLOW + goal.get_error_log() + COLOR_NORMAL) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1204 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1205 | |
| 1206 | class TestSuite: |
Andrew Boie | 60823c2 | 2017-02-10 09:43:07 -0800 | [diff] [blame] | 1207 | config_re = re.compile('(CONFIG_[A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1208 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1209 | def __init__(self, arch_root, testcase_roots, outdir, coverage): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1210 | # Keep track of which test cases we've filtered out and why |
| 1211 | discards = {} |
| 1212 | self.arches = {} |
| 1213 | self.testcases = {} |
| 1214 | self.platforms = [] |
Andrew Boie | b391e66 | 2015-08-31 15:25:45 -0700 | [diff] [blame] | 1215 | self.outdir = os.path.abspath(outdir) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1216 | self.instances = {} |
| 1217 | self.goals = None |
| 1218 | self.discards = None |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1219 | self.coverage = coverage |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1220 | |
| 1221 | arch_root = os.path.abspath(arch_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1222 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1223 | for testcase_root in testcase_roots: |
| 1224 | testcase_root = os.path.abspath(testcase_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1225 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1226 | debug("Reading test case configuration files under %s..." % |
| 1227 | testcase_root) |
| 1228 | for dirpath, dirnames, filenames in os.walk(testcase_root, |
| 1229 | topdown=True): |
| 1230 | verbose("scanning %s" % dirpath) |
| 1231 | if "testcase.ini" in filenames: |
| 1232 | verbose("Found test case in " + dirpath) |
| 1233 | dirnames[:] = [] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1234 | ini_path = os.path.join(dirpath, "testcase.ini") |
| 1235 | cp = SanityConfigParser(ini_path) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1236 | workdir = os.path.relpath(dirpath, testcase_root) |
| 1237 | |
| 1238 | for section in cp.sections(): |
| 1239 | tc_dict = cp.get_section(section, testcase_valid_keys) |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1240 | tc = TestCase(testcase_root, workdir, section, tc_dict, |
| 1241 | ini_path) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1242 | self.testcases[tc.name] = tc |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1243 | |
| 1244 | debug("Reading architecture configuration files under %s..." % arch_root) |
| 1245 | for dirpath, dirnames, filenames in os.walk(arch_root): |
| 1246 | for filename in filenames: |
| 1247 | if filename.endswith(".ini"): |
| 1248 | fn = os.path.join(dirpath, filename) |
| 1249 | verbose("Found arch configuration " + fn) |
| 1250 | arch = Architecture(fn) |
| 1251 | self.arches[arch.name] = arch |
| 1252 | self.platforms.extend(arch.platforms) |
| 1253 | |
Andrew Boie | 05e3d7f | 2016-08-09 11:29:34 -0700 | [diff] [blame] | 1254 | # Build up a list of boards based on the presence of |
| 1255 | # boards/*/*_defconfig files. We want to make sure that the arch.ini |
| 1256 | # files are not missing any boards |
| 1257 | all_plats = [plat.name for plat in self.platforms] |
| 1258 | for dirpath, dirnames, filenames in os.walk(os.path.join(ZEPHYR_BASE, |
| 1259 | "boards")): |
| 1260 | for filename in filenames: |
| 1261 | if filename.endswith("_defconfig"): |
| 1262 | board_name = filename.replace("_defconfig", "") |
| 1263 | if board_name not in all_plats: |
| 1264 | error("Platform '%s' not specified in any arch .ini file and will not be tested" |
| 1265 | % board_name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1266 | self.instances = {} |
| 1267 | |
| 1268 | def get_last_failed(self): |
| 1269 | if not os.path.exists(LAST_SANITY): |
Anas Nashif | d9616b9 | 2016-11-29 13:34:53 -0500 | [diff] [blame] | 1270 | raise SanityRuntimeError("Couldn't find last sanity run.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1271 | result = [] |
| 1272 | with open(LAST_SANITY, "r") as fp: |
| 1273 | cr = csv.DictReader(fp) |
| 1274 | for row in cr: |
| 1275 | if row["passed"] == "True": |
| 1276 | continue |
| 1277 | test = row["test"] |
| 1278 | platform = row["platform"] |
| 1279 | result.append((test, platform)) |
| 1280 | return result |
| 1281 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1282 | def apply_filters(self, platform_filter, arch_filter, tag_filter, exclude_tag, |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1283 | config_filter, testcase_filter, last_failed, all_plats, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1284 | platform_limit, toolchain, extra_args, enable_ccache): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1285 | instances = [] |
| 1286 | discards = {} |
| 1287 | verbose("platform filter: " + str(platform_filter)) |
| 1288 | verbose(" arch_filter: " + str(arch_filter)) |
| 1289 | verbose(" tag_filter: " + str(tag_filter)) |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1290 | verbose(" exclude_tag: " + str(exclude_tag)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1291 | verbose(" config_filter: " + str(config_filter)) |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1292 | verbose(" enable_ccache: " + str(enable_ccache)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1293 | |
| 1294 | if last_failed: |
| 1295 | failed_tests = self.get_last_failed() |
| 1296 | |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1297 | default_platforms = False |
| 1298 | |
| 1299 | if all_plats: |
| 1300 | info("Selecting all possible platforms per test case") |
| 1301 | # When --all used, any --platform arguments ignored |
| 1302 | platform_filter = [] |
| 1303 | elif not platform_filter: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1304 | info("Selecting default platforms per test case") |
| 1305 | default_platforms = True |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1306 | |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1307 | mg = MakeGenerator(self.outdir, ccache=enable_ccache) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1308 | dlist = {} |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1309 | for tc_name, tc in self.testcases.items(): |
| 1310 | for arch_name, arch in self.arches.items(): |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1311 | instance_list = [] |
| 1312 | for plat in arch.platforms: |
| 1313 | instance = TestInstance(tc, plat, self.outdir) |
| 1314 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1315 | if (arch_name == "unit") != (tc.type == "unit"): |
| 1316 | continue |
| 1317 | |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1318 | if tc.skip: |
| 1319 | continue |
| 1320 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1321 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 1322 | continue |
| 1323 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1324 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 1325 | continue |
| 1326 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1327 | if testcase_filter and tc_name not in testcase_filter: |
| 1328 | continue |
| 1329 | |
| 1330 | if last_failed and (tc.name, plat.name) not in failed_tests: |
| 1331 | continue |
| 1332 | |
| 1333 | if arch_filter and arch_name not in arch_filter: |
| 1334 | continue |
| 1335 | |
| 1336 | if tc.arch_whitelist and arch.name not in tc.arch_whitelist: |
| 1337 | continue |
| 1338 | |
| 1339 | if tc.arch_exclude and arch.name in tc.arch_exclude: |
| 1340 | continue |
| 1341 | |
| 1342 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 1343 | continue |
| 1344 | |
| 1345 | if platform_filter and plat.name not in platform_filter: |
| 1346 | continue |
| 1347 | |
| 1348 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 1349 | continue |
| 1350 | |
Anas Nashif | 6d4ff23 | 2016-12-21 13:05:28 -0500 | [diff] [blame] | 1351 | if tc.tc_filter and (plat in arch.platforms[:platform_limit] or all_plats or platform_filter): |
Anas Nashif | 8ea9d02 | 2015-11-10 12:24:20 -0500 | [diff] [blame] | 1352 | args = tc.extra_args[:] |
| 1353 | args.extend(["ARCH=" + plat.arch.name, |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1354 | "BOARD=" + plat.name, "initconfig"]) |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 1355 | args.extend(extra_args) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1356 | # FIXME would be nice to use a common outdir for this so that |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1357 | # conf, gen_idt, etc aren't rebuilt for every combination, |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1358 | # need a way to avoid different Make processe from clobbering |
| 1359 | # each other since they all try to build them simultaneously |
| 1360 | |
| 1361 | o = os.path.join(self.outdir, plat.name, tc.path) |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1362 | dlist[tc, plat, tc.name.split("/")[-1]] = os.path.join(o,".config") |
| 1363 | goal = "_".join([plat.name, "_".join(tc.name.split("/")), "initconfig"]) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1364 | mg.add_build_goal(goal, os.path.join(ZEPHYR_BASE, tc.code_location), o, args) |
| 1365 | |
| 1366 | info("Building testcase defconfigs...") |
| 1367 | results = mg.execute(defconfig_cb) |
| 1368 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1369 | for name, goal in results.items(): |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1370 | if goal.failed: |
| 1371 | raise SanityRuntimeError("Couldn't build some defconfigs") |
| 1372 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1373 | for k, out_config in dlist.items(): |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1374 | test, plat, name = k |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1375 | defconfig = {} |
| 1376 | with open(out_config, "r") as fp: |
| 1377 | for line in fp.readlines(): |
| 1378 | m = TestSuite.config_re.match(line) |
| 1379 | if not m: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1380 | if line.strip() and not line.startswith("#"): |
| 1381 | sys.stderr.write("Unrecognized line %s\n" % line) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1382 | continue |
| 1383 | defconfig[m.group(1)] = m.group(2).strip() |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1384 | test.defconfig[plat] = defconfig |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1385 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1386 | for tc_name, tc in self.testcases.items(): |
| 1387 | for arch_name, arch in self.arches.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1388 | instance_list = [] |
| 1389 | for plat in arch.platforms: |
| 1390 | instance = TestInstance(tc, plat, self.outdir) |
| 1391 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1392 | if (arch_name == "unit") != (tc.type == "unit"): |
| 1393 | # Discard silently |
| 1394 | continue |
| 1395 | |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1396 | if tc.skip: |
| 1397 | discards[instance] = "Skip filter" |
| 1398 | continue |
| 1399 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1400 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 1401 | discards[instance] = "Command line testcase tag filter" |
| 1402 | continue |
| 1403 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1404 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 1405 | discards[instance] = "Command line testcase exclude filter" |
| 1406 | continue |
| 1407 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1408 | if testcase_filter and tc_name not in testcase_filter: |
| 1409 | discards[instance] = "Testcase name filter" |
| 1410 | continue |
| 1411 | |
| 1412 | if last_failed and (tc.name, plat.name) not in failed_tests: |
| 1413 | discards[instance] = "Passed or skipped during last run" |
| 1414 | continue |
| 1415 | |
| 1416 | if arch_filter and arch_name not in arch_filter: |
| 1417 | discards[instance] = "Command line testcase arch filter" |
| 1418 | continue |
| 1419 | |
| 1420 | if tc.arch_whitelist and arch.name not in tc.arch_whitelist: |
| 1421 | discards[instance] = "Not in test case arch whitelist" |
| 1422 | continue |
| 1423 | |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1424 | if tc.arch_exclude and arch.name in tc.arch_exclude: |
| 1425 | discards[instance] = "In test case arch exclude" |
| 1426 | continue |
| 1427 | |
| 1428 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 1429 | discards[instance] = "In test case platform exclude" |
| 1430 | continue |
| 1431 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1432 | if platform_filter and plat.name not in platform_filter: |
| 1433 | discards[instance] = "Command line platform filter" |
| 1434 | continue |
| 1435 | |
| 1436 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 1437 | discards[instance] = "Not in testcase platform whitelist" |
| 1438 | continue |
| 1439 | |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1440 | if toolchain and toolchain not in plat.supported_toolchains: |
| 1441 | discards[instance] = "Not supported by the toolchain" |
| 1442 | continue |
| 1443 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1444 | defconfig = {"ARCH" : arch.name, "PLATFORM" : plat.name} |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 1445 | defconfig.update(os.environ) |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1446 | for p, tdefconfig in tc.defconfig.items(): |
| 1447 | if p == plat: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1448 | defconfig.update(tdefconfig) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1449 | break |
| 1450 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1451 | if tc.tc_filter: |
| 1452 | try: |
| 1453 | res = expr_parser.parse(tc.tc_filter, defconfig) |
Andrew Boie | c09b4b8 | 2017-04-18 11:46:07 -0700 | [diff] [blame] | 1454 | except (ValueError, SyntaxError) as se: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1455 | sys.stderr.write("Failed processing %s\n" % tc.inifile) |
| 1456 | raise se |
| 1457 | if not res: |
| 1458 | discards[instance] = ("defconfig doesn't satisfy expression '%s'" % |
| 1459 | tc.tc_filter) |
| 1460 | continue |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1461 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1462 | instance_list.append(instance) |
| 1463 | |
| 1464 | if not instance_list: |
| 1465 | # Every platform in this arch was rejected already |
| 1466 | continue |
| 1467 | |
| 1468 | if default_platforms: |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1469 | self.add_instances(instance_list[:platform_limit]) |
| 1470 | for instance in instance_list[platform_limit:]: |
| 1471 | discards[instance] = "Not in first %d platform(s) for arch" % platform_limit |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1472 | else: |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1473 | self.add_instances(instance_list) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1474 | self.discards = discards |
| 1475 | return discards |
| 1476 | |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1477 | def add_instances(self, ti_list): |
| 1478 | for ti in ti_list: |
| 1479 | self.instances[ti.name] = ti |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1480 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1481 | def execute(self, cb, cb_context, build_only, enable_slow, enable_asserts, enable_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1482 | extra_args, enable_ccache): |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1483 | |
| 1484 | def calc_one_elf_size(name, goal): |
| 1485 | if not goal.failed: |
| 1486 | i = self.instances[name] |
| 1487 | sc = i.calculate_sizes() |
| 1488 | goal.metrics["ram_size"] = sc.get_ram_size() |
| 1489 | goal.metrics["rom_size"] = sc.get_rom_size() |
| 1490 | goal.metrics["unrecognized"] = sc.unrecognized_sections() |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1491 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1492 | mg = MakeGenerator(self.outdir, asserts=enable_asserts, deprecations=enable_deprecations, |
| 1493 | ccache=enable_ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1494 | for i in self.instances.values(): |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1495 | mg.add_test_instance(i, build_only, enable_slow, self.coverage, extra_args) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1496 | self.goals = mg.execute(cb, cb_context) |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1497 | |
| 1498 | # Parallelize size calculation |
| 1499 | executor = concurrent.futures.ThreadPoolExecutor(CPU_COUNTS) |
| 1500 | futures = [executor.submit(calc_one_elf_size, name, goal) \ |
| 1501 | for name, goal in self.goals.items()] |
| 1502 | concurrent.futures.wait(futures) |
| 1503 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1504 | return self.goals |
| 1505 | |
| 1506 | def discard_report(self, filename): |
| 1507 | if self.discards == None: |
| 1508 | raise SanityRuntimeException("apply_filters() hasn't been run!") |
| 1509 | |
| 1510 | with open(filename, "wb") as csvfile: |
| 1511 | fieldnames = ["test", "arch", "platform", "reason"] |
| 1512 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 1513 | cw.writeheader() |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1514 | for instance, reason in self.discards.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1515 | rowdict = {"test" : i.test.name, |
| 1516 | "arch" : i.platform.arch.name, |
| 1517 | "platform" : i.platform.name, |
| 1518 | "reason" : reason} |
| 1519 | cw.writerow(rowdict) |
| 1520 | |
| 1521 | def compare_metrics(self, filename): |
| 1522 | # name, datatype, lower results better |
| 1523 | interesting_metrics = [("ram_size", int, True), |
| 1524 | ("rom_size", int, True)] |
| 1525 | |
| 1526 | if self.goals == None: |
| 1527 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1528 | |
| 1529 | if not os.path.exists(filename): |
| 1530 | info("Cannot compare metrics, %s not found" % filename) |
| 1531 | return [] |
| 1532 | |
| 1533 | results = [] |
| 1534 | saved_metrics = {} |
| 1535 | with open(filename) as fp: |
| 1536 | cr = csv.DictReader(fp) |
| 1537 | for row in cr: |
| 1538 | d = {} |
| 1539 | for m, _, _ in interesting_metrics: |
| 1540 | d[m] = row[m] |
| 1541 | saved_metrics[(row["test"], row["platform"])] = d |
| 1542 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1543 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1544 | i = self.instances[name] |
| 1545 | mkey = (i.test.name, i.platform.name) |
| 1546 | if mkey not in saved_metrics: |
| 1547 | continue |
| 1548 | sm = saved_metrics[mkey] |
| 1549 | for metric, mtype, lower_better in interesting_metrics: |
| 1550 | if metric not in goal.metrics: |
| 1551 | continue |
| 1552 | if sm[metric] == "": |
| 1553 | continue |
| 1554 | delta = goal.metrics[metric] - mtype(sm[metric]) |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1555 | if delta == 0: |
| 1556 | continue |
| 1557 | results.append((i, metric, goal.metrics[metric], delta, |
| 1558 | lower_better)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1559 | return results |
| 1560 | |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame^] | 1561 | def testcase_xunit_report(self, filename, args): |
| 1562 | if self.goals == None: |
| 1563 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1564 | |
| 1565 | fails = 0 |
| 1566 | passes = 0 |
| 1567 | errors = 0 |
| 1568 | |
| 1569 | for name, goal in self.goals.items(): |
| 1570 | if goal.failed: |
| 1571 | if goal.reason in ['build_error', 'qemu_crash']: |
| 1572 | errors += 1 |
| 1573 | else: |
| 1574 | fails += 1 |
| 1575 | else: |
| 1576 | passes += 1 |
| 1577 | |
| 1578 | run = "Sanitycheck" |
| 1579 | eleTestsuite = None |
| 1580 | append = args.only_failed |
| 1581 | |
| 1582 | if os.path.exists(filename) or append: |
| 1583 | tree = ET.parse(filename) |
| 1584 | eleTestsuites = tree.getroot() |
| 1585 | eleTestsuite = tree.findall('testsuite')[0]; |
| 1586 | else: |
| 1587 | eleTestsuites = ET.Element('testsuites') |
| 1588 | eleTestsuite = ET.SubElement(eleTestsuites, 'testsuite', name=run, |
| 1589 | tests="%d" %(errors + passes + fails), failures="%d" %fails, errors="%d" %errors, skip="0") |
| 1590 | |
| 1591 | qemu_time = "0" |
| 1592 | for name, goal in self.goals.items(): |
| 1593 | |
| 1594 | i = self.instances[name] |
| 1595 | if append: |
| 1596 | for tc in eleTestsuite.findall('testcase'): |
| 1597 | if tc.get('name') == "%s:%s" %(i.platform.name, i.test.name): |
| 1598 | eleTestsuite.remove(tc) |
| 1599 | |
| 1600 | if not goal.failed and goal.qemu: |
| 1601 | qemu_time = "%s" %(goal.metrics["qemu_time"]) |
| 1602 | |
| 1603 | eleTestcase = ET.SubElement(eleTestsuite, 'testcase', name="%s:%s" %(i.platform.name, i.test.name), time=qemu_time) |
| 1604 | if goal.failed: |
| 1605 | failure = ET.SubElement(eleTestcase, 'failure', type="failure", message=goal.reason) |
| 1606 | p = ("%s/%s/%s" %(args.outdir, i.platform.name, i.test.name)) |
| 1607 | bl = os.path.join(p, "build.log") |
| 1608 | if os.path.exists(bl): |
| 1609 | with open(bl, "r") as f: |
| 1610 | log = f.read() |
| 1611 | failure.text = (str(log)) |
| 1612 | |
| 1613 | result = ET.tostring(eleTestsuites) |
| 1614 | f = open(filename, 'wb') |
| 1615 | f.write(result) |
| 1616 | f.close() |
| 1617 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1618 | def testcase_report(self, filename): |
| 1619 | if self.goals == None: |
| 1620 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1621 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1622 | with open(filename, "wt") as csvfile: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1623 | fieldnames = ["test", "arch", "platform", "passed", "status", |
| 1624 | "extra_args", "qemu", "qemu_time", "ram_size", |
| 1625 | "rom_size"] |
| 1626 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 1627 | cw.writeheader() |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1628 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1629 | i = self.instances[name] |
| 1630 | rowdict = {"test" : i.test.name, |
| 1631 | "arch" : i.platform.arch.name, |
| 1632 | "platform" : i.platform.name, |
| 1633 | "extra_args" : " ".join(i.test.extra_args), |
| 1634 | "qemu" : i.platform.qemu_support} |
| 1635 | if goal.failed: |
| 1636 | rowdict["passed"] = False |
| 1637 | rowdict["status"] = goal.reason |
| 1638 | else: |
| 1639 | rowdict["passed"] = True |
| 1640 | if goal.qemu: |
| 1641 | rowdict["qemu_time"] = goal.metrics["qemu_time"] |
| 1642 | rowdict["ram_size"] = goal.metrics["ram_size"] |
| 1643 | rowdict["rom_size"] = goal.metrics["rom_size"] |
| 1644 | cw.writerow(rowdict) |
| 1645 | |
| 1646 | |
| 1647 | def parse_arguments(): |
| 1648 | |
| 1649 | parser = argparse.ArgumentParser(description = __doc__, |
| 1650 | formatter_class = argparse.RawDescriptionHelpFormatter) |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 1651 | parser.fromfile_prefix_chars = "+" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1652 | |
| 1653 | parser.add_argument("-p", "--platform", action="append", |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1654 | help="Platform filter for testing. This option may be used multiple " |
| 1655 | "times. Testcases will only be built/run on the platforms " |
| 1656 | "specified. If this option is not used, then N platforms will " |
| 1657 | "automatically be chosen from each arch to build and test, " |
| 1658 | "where N is provided by the --platform-limit option.") |
| 1659 | parser.add_argument("-L", "--platform-limit", action="store", type=int, |
| 1660 | metavar="N", default=1, |
| 1661 | help="Controls what platforms are tested if --platform or " |
| 1662 | "--all are not used. For each architecture specified by " |
| 1663 | "--arch (defaults to all of them), choose the first " |
| 1664 | "N platforms to test in the arch-specific .ini file " |
| 1665 | "'platforms' list. Defaults to 1.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1666 | parser.add_argument("-a", "--arch", action="append", |
| 1667 | help="Arch filter for testing. Takes precedence over --platform. " |
| 1668 | "If unspecified, test all arches. Multiple invocations " |
| 1669 | "are treated as a logical 'or' relationship") |
| 1670 | parser.add_argument("-t", "--tag", action="append", |
| 1671 | help="Specify tags to restrict which tests to run by tag value. " |
| 1672 | "Default is to not do any tag filtering. Multiple invocations " |
| 1673 | "are treated as a logical 'or' relationship") |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1674 | parser.add_argument("-e", "--exclude-tag", action="append", |
| 1675 | help="Specify tags of tests that should not run." |
| 1676 | "Default is to run all tests with all tags.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1677 | parser.add_argument("-f", "--only-failed", action="store_true", |
| 1678 | help="Run only those tests that failed the previous sanity check " |
| 1679 | "invocation.") |
| 1680 | parser.add_argument("-c", "--config", action="append", |
| 1681 | help="Specify platform configuration values filtering. This can be " |
| 1682 | "specified two ways: <config>=<value> or just <config>. The " |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1683 | "defconfig for all platforms will be " |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1684 | "checked. For the <config>=<value> case, only match defconfig " |
| 1685 | "that have that value defined. For the <config> case, match " |
| 1686 | "defconfig that have that value assigned to any value. " |
| 1687 | "Prepend a '!' to invert the match.") |
| 1688 | parser.add_argument("-s", "--test", action="append", |
| 1689 | help="Run only the specified test cases. These are named by " |
| 1690 | "<path to test project relative to " |
| 1691 | "--testcase-root>/<testcase.ini section name>") |
| 1692 | parser.add_argument("-l", "--all", action="store_true", |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1693 | help="Build/test on all platforms. Any --platform arguments " |
| 1694 | "ignored.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1695 | |
| 1696 | parser.add_argument("-o", "--testcase-report", |
| 1697 | help="Output a CSV spreadsheet containing results of the test run") |
| 1698 | parser.add_argument("-d", "--discard-report", |
| 1699 | help="Output a CSV spreadhseet showing tests that were skipped " |
| 1700 | "and why") |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 1701 | parser.add_argument("--compare-report", |
| 1702 | help="Use this report file for size comparision") |
| 1703 | |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1704 | parser.add_argument("--ccache", action="store_const", const=1, default=0, |
| 1705 | help="Enable the use of ccache when building") |
| 1706 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1707 | parser.add_argument("-y", "--dry-run", action="store_true", |
| 1708 | help="Create the filtered list of test cases, but don't actually " |
| 1709 | "run them. Useful if you're just interested in " |
| 1710 | "--discard-report") |
| 1711 | |
| 1712 | parser.add_argument("-r", "--release", action="store_true", |
| 1713 | help="Update the benchmark database with the results of this test " |
| 1714 | "run. Intended to be run by CI when tagging an official " |
| 1715 | "release. This database is used as a basis for comparison " |
| 1716 | "when looking for deltas in metrics such as footprint") |
| 1717 | parser.add_argument("-w", "--warnings-as-errors", action="store_true", |
| 1718 | help="Treat warning conditions as errors") |
| 1719 | parser.add_argument("-v", "--verbose", action="count", default=0, |
| 1720 | help="Emit debugging information, call multiple times to increase " |
| 1721 | "verbosity") |
| 1722 | parser.add_argument("-i", "--inline-logs", action="store_true", |
| 1723 | help="Upon test failure, print relevant log data to stdout " |
| 1724 | "instead of just a path to it") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1725 | parser.add_argument("--log-file", metavar="FILENAME", action="store", |
| 1726 | help="log also to file") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1727 | parser.add_argument("-m", "--last-metrics", action="store_true", |
| 1728 | help="Instead of comparing metrics from the last --release, " |
| 1729 | "compare with the results of the previous sanity check " |
| 1730 | "invocation") |
| 1731 | parser.add_argument("-u", "--no-update", action="store_true", |
| 1732 | help="do not update the results of the last run of the sanity " |
| 1733 | "checks") |
| 1734 | parser.add_argument("-b", "--build-only", action="store_true", |
| 1735 | help="Only build the code, do not execute any of it in QEMU") |
| 1736 | parser.add_argument("-j", "--jobs", type=int, |
| 1737 | help="Number of cores to use when building, defaults to " |
| 1738 | "number of CPUs * 2") |
| 1739 | parser.add_argument("-H", "--footprint-threshold", type=float, default=5, |
| 1740 | help="When checking test case footprint sizes, warn the user if " |
| 1741 | "the new app size is greater then the specified percentage " |
| 1742 | "from the last release. Default is 5. 0 to warn on any " |
| 1743 | "increase on app size") |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1744 | parser.add_argument("-D", "--all-deltas", action="store_true", |
| 1745 | help="Show all footprint deltas, positive or negative. Implies " |
| 1746 | "--footprint-threshold=0") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1747 | parser.add_argument("-O", "--outdir", |
| 1748 | default="%s/sanity-out" % ZEPHYR_BASE, |
| 1749 | help="Output directory for logs and binaries.") |
Andrew Boie | ae9e7f7b | 2015-07-31 12:26:12 -0700 | [diff] [blame] | 1750 | parser.add_argument("-n", "--no-clean", action="store_true", |
| 1751 | help="Do not delete the outdir before building. Will result in " |
| 1752 | "faster compilation since builds will be incremental") |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1753 | parser.add_argument("-T", "--testcase-root", action="append", default=[], |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1754 | help="Base directory to recursively search for test cases. All " |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1755 | "testcase.ini files under here will be processed. May be " |
| 1756 | "called multiple times. Defaults to the 'samples' and " |
| 1757 | "'tests' directories in the Zephyr tree.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1758 | parser.add_argument("-A", "--arch-root", |
| 1759 | default="%s/scripts/sanity_chk/arches" % ZEPHYR_BASE, |
| 1760 | help="Directory to search for arch configuration files. All .ini " |
| 1761 | "files in the directory will be processed.") |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1762 | parser.add_argument("-z", "--size", action="append", |
| 1763 | help="Don't run sanity checks. Instead, produce a report to " |
| 1764 | "stdout detailing RAM/ROM sizes on the specified filenames. " |
| 1765 | "All other command line arguments ignored.") |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1766 | parser.add_argument("-S", "--enable-slow", action="store_true", |
| 1767 | help="Execute time-consuming test cases that have been marked " |
| 1768 | "as 'slow' in testcase.ini. Normally these are only built.") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 1769 | parser.add_argument("-R", "--enable-asserts", action="store_true", |
| 1770 | help="Build all test cases with assertions enabled.") |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1771 | parser.add_argument("-Q", "--error-on-deprecations", action="store_false", |
| 1772 | help="Error on deprecation warnings.") |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 1773 | parser.add_argument("-x", "--extra-args", action="append", default=[], |
| 1774 | help="Extra arguments to pass to the build when compiling test " |
| 1775 | "cases. May be called multiple times. These will be passed " |
| 1776 | "in after any sanitycheck-supplied options.") |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1777 | parser.add_argument("-C", "--coverage", action="store_true", |
| 1778 | help="Scan for unit test coverage with gcov + lcov.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1779 | |
| 1780 | return parser.parse_args() |
| 1781 | |
| 1782 | def log_info(filename): |
Mazen NEIFER | 8f1dd3a | 2017-02-04 14:32:04 +0100 | [diff] [blame] | 1783 | filename = os.path.relpath(os.path.realpath(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1784 | if INLINE_LOGS: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1785 | info("{:-^100}".format(filename)) |
Andrew Boie | d01535c | 2017-01-10 13:15:02 -0800 | [diff] [blame] | 1786 | |
| 1787 | try: |
| 1788 | with open(filename) as fp: |
| 1789 | data = fp.read() |
| 1790 | except Exception as e: |
| 1791 | data = "Unable to read log data (%s)\n" % (str(e)) |
| 1792 | |
| 1793 | sys.stdout.write(data) |
| 1794 | if log_file: |
| 1795 | log_file.write(data) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1796 | info("{:-^100}".format(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1797 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1798 | info("\tsee: " + COLOR_YELLOW + filename + COLOR_NORMAL) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1799 | |
| 1800 | def terse_test_cb(instances, goals, goal): |
| 1801 | total_tests = len(goals) |
| 1802 | total_done = 0 |
| 1803 | total_failed = 0 |
| 1804 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1805 | for k, g in goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1806 | if g.finished: |
| 1807 | total_done += 1 |
| 1808 | if g.failed: |
| 1809 | total_failed += 1 |
| 1810 | |
| 1811 | if goal.failed: |
| 1812 | i = instances[goal.name] |
| 1813 | info("\n\n{:<25} {:<50} {}FAILED{}: {}".format(i.platform.name, |
| 1814 | i.test.name, COLOR_RED, COLOR_NORMAL, goal.reason)) |
| 1815 | log_info(goal.get_error_log()) |
| 1816 | info("") |
| 1817 | |
Andrew Boie | 7a992ae | 2017-01-17 10:40:02 -0800 | [diff] [blame] | 1818 | sys.stdout.write("\rtotal complete: %s%4d/%4d%s %2d%% failed: %s%4d%s" % ( |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1819 | COLOR_GREEN, total_done, total_tests, COLOR_NORMAL, |
Andrew Boie | 7a992ae | 2017-01-17 10:40:02 -0800 | [diff] [blame] | 1820 | int((float(total_done) / total_tests) * 100), |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1821 | COLOR_RED if total_failed > 0 else COLOR_NORMAL, |
| 1822 | total_failed, COLOR_NORMAL)) |
| 1823 | sys.stdout.flush() |
| 1824 | |
| 1825 | def chatty_test_cb(instances, goals, goal): |
| 1826 | i = instances[goal.name] |
| 1827 | |
| 1828 | if VERBOSE < 2 and not goal.finished: |
| 1829 | return |
| 1830 | |
| 1831 | if goal.failed: |
| 1832 | status = COLOR_RED + "FAILED" + COLOR_NORMAL + ": " + goal.reason |
| 1833 | elif goal.finished: |
| 1834 | status = COLOR_GREEN + "PASSED" + COLOR_NORMAL |
| 1835 | else: |
| 1836 | status = goal.make_state |
| 1837 | |
| 1838 | info("{:<25} {:<50} {}".format(i.platform.name, i.test.name, status)) |
| 1839 | if goal.failed: |
| 1840 | log_info(goal.get_error_log()) |
| 1841 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1842 | |
| 1843 | def size_report(sc): |
| 1844 | info(sc.filename) |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1845 | info("SECTION NAME VMA LMA SIZE HEX SZ TYPE") |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1846 | for i in range(len(sc.sections)): |
| 1847 | v = sc.sections[i] |
| 1848 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1849 | info("%-17s 0x%08x 0x%08x %8d 0x%05x %-7s" % |
| 1850 | (v["name"], v["virt_addr"], v["load_addr"], v["size"], v["size"], |
| 1851 | v["type"])) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1852 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1853 | info("Totals: %d bytes (ROM), %d bytes (RAM)" % |
| 1854 | (sc.rom_size, sc.ram_size)) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1855 | info("") |
| 1856 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1857 | def generate_coverage(outdir, ignores): |
| 1858 | with open(os.path.join(outdir, "coverage.log"), "a") as coveragelog: |
| 1859 | coveragefile = os.path.join(outdir, "coverage.info") |
| 1860 | ztestfile = os.path.join(outdir, "ztest.info") |
| 1861 | subprocess.call(["lcov", "--capture", "--directory", outdir, |
| 1862 | "--output-file", coveragefile], stdout=coveragelog) |
| 1863 | # We want to remove tests/* and tests/ztest/test/* but save tests/ztest |
| 1864 | subprocess.call(["lcov", "--extract", coveragefile, |
| 1865 | os.path.join(ZEPHYR_BASE, "tests", "ztest", "*"), |
| 1866 | "--output-file", ztestfile], stdout=coveragelog) |
| 1867 | subprocess.call(["lcov", "--remove", ztestfile, |
| 1868 | os.path.join(ZEPHYR_BASE, "tests/ztest/test/*"), |
| 1869 | "--output-file", ztestfile], stdout=coveragelog) |
| 1870 | for i in ignores: |
| 1871 | subprocess.call(["lcov", "--remove", coveragefile, i, |
| 1872 | "--output-file", coveragefile], stdout=coveragelog) |
| 1873 | subprocess.call(["genhtml", "-output-directory", |
| 1874 | os.path.join(outdir, "coverage"), |
| 1875 | coveragefile, ztestfile], stdout=coveragelog) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1876 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1877 | def main(): |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1878 | start_time = time.time() |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1879 | global VERBOSE, INLINE_LOGS, CPU_COUNTS, log_file |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1880 | args = parse_arguments() |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1881 | toolchain = os.environ.get("ZEPHYR_GCC_VARIANT", None) |
Andrew Boie | 1e4e68b | 2017-02-10 11:40:54 -0800 | [diff] [blame] | 1882 | if toolchain == "zephyr": |
| 1883 | os.environ["DISABLE_TRYRUN"] = "1" |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1884 | |
| 1885 | if args.size: |
| 1886 | for fn in args.size: |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1887 | size_report(SizeCalculator(fn, [])) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1888 | sys.exit(0) |
| 1889 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1890 | VERBOSE += args.verbose |
| 1891 | INLINE_LOGS = args.inline_logs |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1892 | if args.log_file: |
| 1893 | log_file = open(args.log_file, "w") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1894 | if args.jobs: |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1895 | CPU_COUNTS = args.jobs |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1896 | |
Andrew Boie | ae9e7f7b | 2015-07-31 12:26:12 -0700 | [diff] [blame] | 1897 | if os.path.exists(args.outdir) and not args.no_clean: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1898 | info("Cleaning output directory " + args.outdir) |
| 1899 | shutil.rmtree(args.outdir) |
| 1900 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1901 | if not args.testcase_root: |
| 1902 | args.testcase_root = [os.path.join(ZEPHYR_BASE, "tests"), |
| 1903 | os.path.join(ZEPHYR_BASE, "samples")] |
| 1904 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1905 | ts = TestSuite(args.arch_root, args.testcase_root, args.outdir, args.coverage) |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1906 | discards = ts.apply_filters(args.platform, args.arch, args.tag, args.exclude_tag, args.config, |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1907 | args.test, args.only_failed, args.all, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1908 | args.platform_limit, toolchain, args.extra_args, args.ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1909 | |
| 1910 | if args.discard_report: |
| 1911 | ts.discard_report(args.discard_report) |
| 1912 | |
| 1913 | if VERBOSE: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1914 | for i, reason in discards.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1915 | debug("{:<25} {:<50} {}SKIPPED{}: {}".format(i.platform.name, |
| 1916 | i.test.name, COLOR_YELLOW, COLOR_NORMAL, reason)) |
| 1917 | |
| 1918 | info("%d tests selected, %d tests discarded due to filters" % |
| 1919 | (len(ts.instances), len(discards))) |
| 1920 | |
| 1921 | if args.dry_run: |
| 1922 | return |
| 1923 | |
| 1924 | if VERBOSE or not TERMINAL: |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1925 | goals = ts.execute(chatty_test_cb, ts.instances, args.build_only, |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1926 | args.enable_slow, args.enable_asserts, args.error_on_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1927 | args.extra_args, args.ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1928 | else: |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1929 | goals = ts.execute(terse_test_cb, ts.instances, args.build_only, |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1930 | args.enable_slow, args.enable_asserts, args.error_on_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1931 | args.extra_args, args.ccache) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1932 | info("") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1933 | |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 1934 | # figure out which report to use for size comparison |
| 1935 | if args.compare_report: |
| 1936 | report_to_use = args.compare_report |
| 1937 | elif args.last_metrics: |
| 1938 | report_to_use = LAST_SANITY |
| 1939 | else: |
| 1940 | report_to_use = RELEASE_DATA |
| 1941 | |
| 1942 | deltas = ts.compare_metrics(report_to_use) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1943 | warnings = 0 |
| 1944 | if deltas: |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1945 | for i, metric, value, delta, lower_better in deltas: |
| 1946 | if not args.all_deltas and ((delta < 0 and lower_better) or |
| 1947 | (delta > 0 and not lower_better)): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1948 | continue |
| 1949 | |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1950 | percentage = (float(delta) / float(value - delta)) |
| 1951 | if not args.all_deltas and (percentage < |
| 1952 | (args.footprint_threshold / 100.0)): |
| 1953 | continue |
| 1954 | |
Daniel Leung | 00525c2 | 2016-04-11 10:27:56 -0700 | [diff] [blame] | 1955 | info("{:<25} {:<60} {}{}{}: {} {:<+4}, is now {:6} {:+.2%}".format( |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1956 | i.platform.name, i.test.name, COLOR_YELLOW, |
| 1957 | "INFO" if args.all_deltas else "WARNING", COLOR_NORMAL, |
Andrew Boie | 829c056 | 2015-10-13 09:44:19 -0700 | [diff] [blame] | 1958 | metric, delta, value, percentage)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1959 | warnings += 1 |
| 1960 | |
| 1961 | if warnings: |
| 1962 | info("Deltas based on metrics from last %s" % |
| 1963 | ("release" if not args.last_metrics else "run")) |
| 1964 | |
| 1965 | failed = 0 |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1966 | for name, goal in goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1967 | if goal.failed: |
| 1968 | failed += 1 |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1969 | elif goal.metrics.get("unrecognized"): |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1970 | info("%sFAILED%s: %s has unrecognized binary sections: %s" % |
| 1971 | (COLOR_RED, COLOR_NORMAL, goal.name, |
| 1972 | str(goal.metrics["unrecognized"]))) |
| 1973 | failed += 1 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1974 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1975 | if args.coverage: |
| 1976 | info("Generating coverage files...") |
| 1977 | generate_coverage(args.outdir, ["tests/*", "samples/*"]) |
| 1978 | |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1979 | info("%s%d of %d%s tests passed with %s%d%s warnings in %d seconds" % |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1980 | (COLOR_RED if failed else COLOR_GREEN, len(goals) - failed, |
| 1981 | len(goals), COLOR_NORMAL, COLOR_YELLOW if warnings else COLOR_NORMAL, |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1982 | warnings, COLOR_NORMAL, time.time() - start_time)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1983 | |
| 1984 | if args.testcase_report: |
| 1985 | ts.testcase_report(args.testcase_report) |
| 1986 | if not args.no_update: |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame^] | 1987 | ts.testcase_xunit_report(LAST_SANITY_XUNIT, args) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1988 | ts.testcase_report(LAST_SANITY) |
| 1989 | if args.release: |
| 1990 | ts.testcase_report(RELEASE_DATA) |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1991 | if log_file: |
| 1992 | log_file.close() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1993 | if failed or (warnings and args.warnings_as_errors): |
| 1994 | sys.exit(1) |
| 1995 | |
| 1996 | if __name__ == "__main__": |
| 1997 | main() |