|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright (c) 2017-2018 ARM Limited |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import os |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +import shutil |
| 22 | +from argparse import ArgumentParser |
| 23 | + |
| 24 | +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) |
| 25 | +sys.path.insert(0, ROOT) |
| 26 | +from tools.targets import Target, TARGET_MAP, TARGET_NAMES |
| 27 | + |
| 28 | +MAKE_PY_LOCATTION = os.path.join(ROOT, 'tools', 'make.py') |
| 29 | +TEST_PY_LOCATTION = os.path.join(ROOT, 'tools', 'test.py') |
| 30 | +MBED_PSA_TESTS = '*psa-spm*,*psa-crypto_access_control' |
| 31 | +TFM_MBED_APP = os.path.join(ROOT, 'tools', 'psa', 'tfm', 'mbed_app.json') |
| 32 | +TFM_TESTS = { |
| 33 | + '*psa-spm_smoke': ['USE_PSA_TEST_PARTITIONS', 'USE_SMOKE_TESTS_PART1'], |
| 34 | + '*psa-spm_client': ['USE_PSA_TEST_PARTITIONS', 'USE_CLIENT_TESTS_PART1'], |
| 35 | + '*psa-spm_server': ['USE_PSA_TEST_PARTITIONS', 'USE_SERVER_TESTS_PART1', 'USE_SERVER_TESTS_PART2'], |
| 36 | + '*psa-crypto_access_control': ['USE_PSA_TEST_PARTITIONS', 'USE_CRYPTO_ACL_TEST'] |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def _psa_backend(target_name): |
| 41 | + return 'TFM' if 'TFM' in Target.get_target(target_name).labels else 'MBED_SPM' |
| 42 | + |
| 43 | + |
| 44 | +def get_mbed_official_psa_release(): |
| 45 | + psa_targets_release_list = [] |
| 46 | + psa_secure_targets = [t for t in TARGET_NAMES if Target.get_target(t).is_PSA_secure_target] |
| 47 | + for t in psa_secure_targets: |
| 48 | + psa_targets_release_list.append( |
| 49 | + tuple( |
| 50 | + [ |
| 51 | + TARGET_MAP[t].name, |
| 52 | + TARGET_MAP[t].default_toolchain |
| 53 | + ] |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + return psa_targets_release_list |
| 58 | + |
| 59 | + |
| 60 | +def create_mbed_ignore(build_dir): |
| 61 | + with open(os.path.join(build_dir, '.mbedignore'), 'w') as f: |
| 62 | + f.write('*\n') |
| 63 | + |
| 64 | + |
| 65 | +def build_mbed_spm_platform(target, toolchain): |
| 66 | + subprocess.call([ |
| 67 | + sys.executable, '-u', TEST_PY_LOCATTION, |
| 68 | + '--greentea', |
| 69 | + '--profile', 'debug', |
| 70 | + '-t', toolchain, |
| 71 | + '-m', target, |
| 72 | + '--source', ROOT, |
| 73 | + '--build', os.path.join(ROOT, 'BUILD', 'tests', target), |
| 74 | + '--test-spec', os.path.join(ROOT, 'BUILD', 'tests', target, 'test_spec.json'), |
| 75 | + '--build-data', os.path.join(ROOT, 'BUILD', 'tests', target, 'build_data.json'), |
| 76 | + '-n', MBED_PSA_TESTS |
| 77 | + ]) |
| 78 | + |
| 79 | + subprocess.call([ |
| 80 | + sys.executable, '-u', MAKE_PY_LOCATTION, |
| 81 | + '-t', toolchain, |
| 82 | + '-m', target, |
| 83 | + '--profile', 'release', |
| 84 | + '--source', ROOT, |
| 85 | + '--build', os.path.join(ROOT, 'BUILD', target), |
| 86 | + '--artifact-name', 'psa_release_1.0' |
| 87 | + ]) |
| 88 | + |
| 89 | + |
| 90 | +def _tfm_test_defines(test): |
| 91 | + return ['-D{}'.format(define) for define in TFM_TESTS[test]] |
| 92 | + |
| 93 | + |
| 94 | +def build_tfm_platform(target, toolchain): |
| 95 | + for test in TFM_TESTS.keys(): |
| 96 | + subprocess.call([ |
| 97 | + sys.executable, '-u', TEST_PY_LOCATTION, |
| 98 | + '--greentea', |
| 99 | + '--profile', 'debug', |
| 100 | + '-t', toolchain, |
| 101 | + '-m', target, |
| 102 | + '--source', ROOT, |
| 103 | + '--build', os.path.join(ROOT, 'BUILD', 'tests', target), |
| 104 | + '--test-spec', os.path.join(ROOT, 'BUILD', 'tests', target, 'test_spec.json'), |
| 105 | + '--build-data', os.path.join(ROOT, 'BUILD', 'tests', target, 'build_data.json'), |
| 106 | + '--app-config', TFM_MBED_APP, '-n', test] + _tfm_test_defines(test)) |
| 107 | + |
| 108 | + subprocess.call([ |
| 109 | + sys.executable, '-u', MAKE_PY_LOCATTION, |
| 110 | + '-t', toolchain, |
| 111 | + '-m', target, |
| 112 | + '--profile', 'release', |
| 113 | + '--source', ROOT, |
| 114 | + '--build', os.path.join(ROOT, 'BUILD', target), |
| 115 | + '--app-config', TFM_MBED_APP |
| 116 | + ]) |
| 117 | + |
| 118 | + |
| 119 | +def build_psa_platform(target, toolchain): |
| 120 | + if _psa_backend(options.mcu) is 'TFM': |
| 121 | + build_tfm_platform(target, toolchain) |
| 122 | + else: |
| 123 | + build_mbed_spm_platform(target, toolchain) |
| 124 | + |
| 125 | + |
| 126 | +def get_parser(): |
| 127 | + parser = ArgumentParser() |
| 128 | + parser.add_argument("-m", "--mcu", |
| 129 | + help="build for the given MCU", |
| 130 | + default='*', |
| 131 | + metavar="MCU") |
| 132 | + |
| 133 | + return parser |
| 134 | + |
| 135 | + |
| 136 | +def filter_target(mcu): |
| 137 | + def filter_func(t): |
| 138 | + return t[0] == mcu |
| 139 | + |
| 140 | + return filter_func |
| 141 | + |
| 142 | + |
| 143 | +def main(): |
| 144 | + parser = get_parser() |
| 145 | + options = parser.parse_args() |
| 146 | + build_dir = os.path.join(ROOT, 'BUILD') |
| 147 | + if os.path.exists(build_dir): |
| 148 | + shutil.rmtree(build_dir) |
| 149 | + |
| 150 | + os.makedirs(build_dir) |
| 151 | + create_mbed_ignore(build_dir) |
| 152 | + target_filter_function = None |
| 153 | + psa_platforms_list = get_mbed_official_psa_release() |
| 154 | + |
| 155 | + if options.mcu is not '*': |
| 156 | + target_filter_function = filter_target(options.mcu) |
| 157 | + |
| 158 | + for target, toolchain in filter(target_filter_function, psa_platforms_list): |
| 159 | + build_psa_platform(target, toolchain) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + main() |
0 commit comments