|
| 1 | +""" import and bulid a bunch of example programs """ |
| 2 | + |
| 3 | +from argparse import ArgumentParser |
| 4 | +import os |
| 5 | +from os.path import dirname, abspath, basename |
| 6 | +import os.path |
| 7 | +import sys |
| 8 | +import subprocess |
| 9 | +import json |
| 10 | + |
| 11 | +ROOT = abspath(dirname(dirname(dirname(dirname(__file__))))) |
| 12 | +sys.path.insert(0, ROOT) |
| 13 | + |
| 14 | +from tools.build_api import get_mbed_official_release |
| 15 | +from tools.targets import TARGET_MAP |
| 16 | +from tools.utils import argparse_force_uppercase_type |
| 17 | + |
| 18 | + |
| 19 | +EXAMPLES = json.load(open(os.path.join(os.path.dirname(__file__), |
| 20 | + "examples.json"))) |
| 21 | + |
| 22 | +def print_stuff(name, lst): |
| 23 | + if lst: |
| 24 | + print("#"*80) |
| 25 | + print("# {} example combinations".format(name)) |
| 26 | + print("#") |
| 27 | + for thing in lst: |
| 28 | + print(thing) |
| 29 | + |
| 30 | + |
| 31 | +SUPPORTED_TOOLCHAINS = ["ARM", "IAR", "GCC_ARM"] |
| 32 | + |
| 33 | + |
| 34 | +def target_cross_toolchain(allowed_toolchains, |
| 35 | + features=[], targets=TARGET_MAP.keys()): |
| 36 | + """Generate pairs of target and toolchains |
| 37 | +
|
| 38 | + Args: |
| 39 | + allowed_toolchains - a list of all possible toolchains |
| 40 | +
|
| 41 | + Kwargs: |
| 42 | + features - the features that must be in the features array of a |
| 43 | + target |
| 44 | + targets - a list of available targets |
| 45 | + """ |
| 46 | + for target, toolchains in get_mbed_official_release("5"): |
| 47 | + for toolchain in toolchains: |
| 48 | + if (toolchain in allowed_toolchains and |
| 49 | + target in targets and |
| 50 | + all(feature in TARGET_MAP[target].features |
| 51 | + for feature in features)): |
| 52 | + yield target, toolchain |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + """Entry point""" |
| 57 | + parser = ArgumentParser() |
| 58 | + subparsers = parser.add_subparsers() |
| 59 | + import_cmd = subparsers.add_parser("import") |
| 60 | + import_cmd.set_defaults(fn=do_import) |
| 61 | + compile_cmd = subparsers.add_parser("compile") |
| 62 | + compile_cmd.set_defaults(fn=do_compile) |
| 63 | + compile_cmd.add_argument( |
| 64 | + "toolchains", nargs="*", default=SUPPORTED_TOOLCHAINS, |
| 65 | + type=argparse_force_uppercase_type(SUPPORTED_TOOLCHAINS, |
| 66 | + "toolchain")) |
| 67 | + args = parser.parse_args() |
| 68 | + return args.fn(args) |
| 69 | + |
| 70 | + |
| 71 | +def do_import(_): |
| 72 | + """Do the import step of this process""" |
| 73 | + for example, _ in EXAMPLES.iteritems(): |
| 74 | + subprocess.call(["mbed-cli", "import", example]) |
| 75 | + return 0 |
| 76 | + |
| 77 | + |
| 78 | +def do_compile(args): |
| 79 | + """Do the compile step""" |
| 80 | + failures = [] |
| 81 | + sucesses = [] |
| 82 | + for example, requirements in EXAMPLES.iteritems(): |
| 83 | + os.chdir(basename(example)) |
| 84 | + for target, toolchain in target_cross_toolchain(args.toolchains, |
| 85 | + **requirements): |
| 86 | + proc = subprocess.Popen(["mbed-cli", "compile", "-t", toolchain, |
| 87 | + "-m", target, "--silent"]) |
| 88 | + proc.wait() |
| 89 | + example_name = "{} {} {}".format(basename(example), target, |
| 90 | + toolchain) |
| 91 | + if proc.returncode: |
| 92 | + failures.append(example_name) |
| 93 | + else: |
| 94 | + sucesses.append(example_name) |
| 95 | + os.chdir("..") |
| 96 | + |
| 97 | + print_stuff("Passed", sucesses) |
| 98 | + print_stuff("Failed", failures) |
| 99 | + return len(failures) |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + sys.exit(main()) |
0 commit comments