alexander | f4e2c47 | 2021-05-14 13:14:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 shutil |
| 21 | import multiprocessing |
| 22 | import logging |
| 23 | import sys |
| 24 | from argparse import ArgumentParser |
| 25 | |
| 26 | from set_up_default_resources import set_up_resources |
| 27 | |
| 28 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 29 | def run(toolchain: str, download_resources: bool, run_vela_on_models: bool): |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 30 | """ |
| 31 | Run the helpers scripts. |
| 32 | |
| 33 | Parameters: |
| 34 | ---------- |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 35 | toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used. |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 36 | download_resources (bool): Specifies if 'Download resources' step is performed. |
| 37 | run_vela_on_models (bool): Only if `download_resources` is True, specifies if run vela on downloaded models. |
| 38 | """ |
| 39 | |
| 40 | current_file_dir = os.path.dirname(os.path.abspath(__file__)) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 41 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 42 | # 1. Make sure the toolchain is supported, and set the right one here |
| 43 | supported_toolchain_ids = ["gnu", "arm"] |
| 44 | assert toolchain in supported_toolchain_ids, f"Toolchain must be from {supported_toolchain_ids}" |
| 45 | if toolchain == "arm": |
| 46 | toolchain_file_name = "bare-metal-armclang.cmake" |
| 47 | elif toolchain == "gnu": |
| 48 | toolchain_file_name = "bare-metal-gcc.cmake" |
| 49 | |
| 50 | # 2. Download models if specified |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 51 | if download_resources is True: |
| 52 | logging.info("Downloading resources.") |
| 53 | set_up_resources(run_vela_on_models) |
| 54 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 55 | # 3. Build default configuration |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 56 | logging.info("Building default configuration.") |
| 57 | target_platform = "mps3" |
| 58 | target_subsystem = "sse-300" |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 59 | build_dir = os.path.join(current_file_dir, |
| 60 | f"cmake-build-{target_platform}-{target_subsystem}-{toolchain}-release") |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 61 | try: |
| 62 | os.mkdir(build_dir) |
| 63 | except FileExistsError: |
| 64 | # Directory already exists, clean it |
| 65 | for filename in os.listdir(build_dir): |
| 66 | filepath = os.path.join(build_dir, filename) |
| 67 | try: |
| 68 | if os.path.isfile(filepath) or os.path.islink(filepath): |
| 69 | os.unlink(filepath) |
| 70 | elif os.path.isdir(filepath): |
| 71 | shutil.rmtree(filepath) |
| 72 | except Exception as e: |
| 73 | logging.error('Failed to delete %s. Reason: %s' % (filepath, e)) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 74 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 75 | os.chdir(build_dir) |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 76 | cmake_toolchain_file = os.path.join(current_file_dir, "scripts", "cmake", |
| 77 | "toolchains", toolchain_file_name) |
| 78 | cmake_command = (f"cmake .. -DTARGET_PLATFORM={target_platform} " + |
| 79 | f"-DTARGET_SUBSYSTEM={target_subsystem} " + |
| 80 | f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}") |
| 81 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 82 | logging.info(cmake_command) |
| 83 | state = subprocess.run(cmake_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 84 | logging.info(state.stdout.decode('utf-8')) |
| 85 | |
| 86 | make_command = f"make -j{multiprocessing.cpu_count()}" |
| 87 | logging.info(make_command) |
| 88 | state = subprocess.run(make_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 89 | logging.info(state.stdout.decode('utf-8')) |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | parser = ArgumentParser() |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 94 | parser.add_argument("--toolchain", default="gnu", |
| 95 | help=""" |
| 96 | Specify the toolchain to use (Arm or GNU). |
| 97 | Options are [gnu, arm]; default is gnu. |
| 98 | """) |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 99 | parser.add_argument("--skip-download", |
| 100 | help="Do not download resources: models and test vectors", |
| 101 | action="store_true") |
| 102 | parser.add_argument("--skip-vela", |
| 103 | help="Do not run Vela optimizer on downloaded models.", |
| 104 | action="store_true") |
| 105 | args = parser.parse_args() |
Kshitij Sisodia | b9e9c89 | 2021-05-27 13:57:35 +0100 | [diff] [blame] | 106 | |
| 107 | logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG) |
| 108 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 109 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 110 | run(args.toolchain.lower(), not args.skip_download, not args.skip_vela) |