blob: b642b80f27fc60da3b282cbdfa44dfb37c955fce [file] [log] [blame]
alexanderf4e2c472021-05-14 13:14:21 +01001#!/usr/bin/env python3
Isabella Gottardi2181d0a2021-04-07 09:27:38 +01002
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
18import os
19import subprocess
20import shutil
21import multiprocessing
22import logging
23import sys
24from argparse import ArgumentParser
25
Kshitij Sisodia3be26232021-10-29 12:29:06 +010026from set_up_default_resources import set_up_resources, \
27 get_default_npu_config_from_name, \
28 valid_npu_config_names, \
29 default_npu_config_names
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010030
31
Kshitij Sisodia3be26232021-10-29 12:29:06 +010032def run(toolchain: str,
33 download_resources: bool,
34 run_vela_on_models: bool,
35 npu_config_name: str):
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010036 """
37 Run the helpers scripts.
38
39 Parameters:
40 ----------
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010041 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010042 download_resources (bool): Specifies if 'Download resources' step is performed.
43 run_vela_on_models (bool): Only if `download_resources` is True, specifies if run vela on downloaded models.
Kshitij Sisodia3be26232021-10-29 12:29:06 +010044 npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010045 """
46
47 current_file_dir = os.path.dirname(os.path.abspath(__file__))
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010048
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010049 # 1. Make sure the toolchain is supported, and set the right one here
50 supported_toolchain_ids = ["gnu", "arm"]
Kshitij Sisodia3be26232021-10-29 12:29:06 +010051 assert (toolchain in supported_toolchain_ids,
52 f"Toolchain must be from {supported_toolchain_ids}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010053 if toolchain == "arm":
54 toolchain_file_name = "bare-metal-armclang.cmake"
55 elif toolchain == "gnu":
56 toolchain_file_name = "bare-metal-gcc.cmake"
57
58 # 2. Download models if specified
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010059 if download_resources is True:
60 logging.info("Downloading resources.")
Kshitij Sisodia3be26232021-10-29 12:29:06 +010061 set_up_resources(run_vela_on_models=run_vela_on_models,
62 additional_npu_config_names=[npu_config_name])
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010063
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010064 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010065 logging.info("Building default configuration.")
66 target_platform = "mps3"
67 target_subsystem = "sse-300"
Kshitij Sisodia3be26232021-10-29 12:29:06 +010068 ethos_u_cfg = get_default_npu_config_from_name(npu_config_name)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010069 build_dir = os.path.join(current_file_dir,
Kshitij Sisodia3be26232021-10-29 12:29:06 +010070 f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}")
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010071 try:
72 os.mkdir(build_dir)
73 except FileExistsError:
74 # Directory already exists, clean it
75 for filename in os.listdir(build_dir):
76 filepath = os.path.join(build_dir, filename)
77 try:
78 if os.path.isfile(filepath) or os.path.islink(filepath):
79 os.unlink(filepath)
80 elif os.path.isdir(filepath):
81 shutil.rmtree(filepath)
82 except Exception as e:
83 logging.error('Failed to delete %s. Reason: %s' % (filepath, e))
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010084
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010085 os.chdir(build_dir)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010086 cmake_toolchain_file = os.path.join(current_file_dir, "scripts", "cmake",
87 "toolchains", toolchain_file_name)
Kshitij Sisodia3be26232021-10-29 12:29:06 +010088 cmake_command = (f"cmake .. -DTARGET_PLATFORM={target_platform}" +
89 f" -DTARGET_SUBSYSTEM={target_subsystem}" +
90 f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}" +
91 f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}" +
92 f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010094 logging.info(cmake_command)
Kshitij Sisodia3be26232021-10-29 12:29:06 +010095 state = subprocess.run(cmake_command, shell=True, stdout=subprocess.PIPE,
96 stderr=subprocess.STDOUT)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010097 logging.info(state.stdout.decode('utf-8'))
98
99 make_command = f"make -j{multiprocessing.cpu_count()}"
100 logging.info(make_command)
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100101 state = subprocess.run(make_command, shell=True, stdout=subprocess.PIPE,
102 stderr=subprocess.STDOUT)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100103 logging.info(state.stdout.decode('utf-8'))
104
105
106if __name__ == '__main__':
107 parser = ArgumentParser()
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100108 parser.add_argument("--toolchain", default="gnu",
109 help="""
110 Specify the toolchain to use (Arm or GNU).
111 Options are [gnu, arm]; default is gnu.
112 """)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100113 parser.add_argument("--skip-download",
114 help="Do not download resources: models and test vectors",
115 action="store_true")
116 parser.add_argument("--skip-vela",
117 help="Do not run Vela optimizer on downloaded models.",
118 action="store_true")
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100119 parser.add_argument("--npu-config-name",
120 help=f"""Arm Ethos-U configuration to build for. Choose from:
121 {valid_npu_config_names}""",
122 default=default_npu_config_names[0])
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100123 args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100124
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100125 logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG,
126 filemode='w')
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100127 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
128
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100129 run(args.toolchain.lower(),
130 not args.skip_download,
131 not args.skip_vela,
132 args.npu_config_name)