blob: 3971a0534298bd282f73580238bce94f79796ebe [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
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000023import threading
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010024import sys
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000025from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010026
Kshitij Sisodia3be26232021-10-29 12:29:06 +010027from set_up_default_resources import set_up_resources, \
28 get_default_npu_config_from_name, \
29 valid_npu_config_names, \
30 default_npu_config_names
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010031
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000032class PipeLogging(threading.Thread):
33
34 def __init__(self, log_level):
35 threading.Thread.__init__(self)
36 self.logLevel = log_level
37 self.fileRead, self.fileWrite = os.pipe()
38 self.pipeIn = os.fdopen(self.fileRead)
39 self.daemon = False
40 self.start()
41
42 def fileno(self):
43 return self.fileWrite
44
45 def run(self):
46 for line in iter(self.pipeIn.readline, ''):
47 logging.log(self.logLevel, line.strip('\n'))
48
49 self.pipeIn.close()
50
51 def close(self):
52 os.close(self.fileWrite)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010053
Kshitij Sisodia3be26232021-10-29 12:29:06 +010054def run(toolchain: str,
55 download_resources: bool,
56 run_vela_on_models: bool,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +000057 npu_config_name: str,
58 make_jobs: int,
59 make_verbose: bool):
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010060 """
61 Run the helpers scripts.
62
63 Parameters:
64 ----------
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010065 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010066 download_resources (bool): Specifies if 'Download resources' step is performed.
67 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 +010068 npu_config_name(str) : Ethos-U NPU configuration name. See "valid_npu_config_names"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010069 """
70
71 current_file_dir = os.path.dirname(os.path.abspath(__file__))
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010072
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010073 # 1. Make sure the toolchain is supported, and set the right one here
74 supported_toolchain_ids = ["gnu", "arm"]
Kshitij Sisodia3be26232021-10-29 12:29:06 +010075 assert (toolchain in supported_toolchain_ids,
76 f"Toolchain must be from {supported_toolchain_ids}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010077 if toolchain == "arm":
78 toolchain_file_name = "bare-metal-armclang.cmake"
79 elif toolchain == "gnu":
80 toolchain_file_name = "bare-metal-gcc.cmake"
81
82 # 2. Download models if specified
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010083 if download_resources is True:
84 logging.info("Downloading resources.")
Kshitij Sisodia3be26232021-10-29 12:29:06 +010085 set_up_resources(run_vela_on_models=run_vela_on_models,
86 additional_npu_config_names=[npu_config_name])
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010087
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010088 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010089 logging.info("Building default configuration.")
90 target_platform = "mps3"
91 target_subsystem = "sse-300"
Kshitij Sisodia3be26232021-10-29 12:29:06 +010092 ethos_u_cfg = get_default_npu_config_from_name(npu_config_name)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093 build_dir = os.path.join(current_file_dir,
Kshitij Sisodia3be26232021-10-29 12:29:06 +010094 f"cmake-build-{target_platform}-{target_subsystem}-{npu_config_name}-{toolchain}")
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010095 try:
96 os.mkdir(build_dir)
97 except FileExistsError:
98 # Directory already exists, clean it
99 for filename in os.listdir(build_dir):
100 filepath = os.path.join(build_dir, filename)
101 try:
102 if os.path.isfile(filepath) or os.path.islink(filepath):
103 os.unlink(filepath)
104 elif os.path.isdir(filepath):
105 shutil.rmtree(filepath)
106 except Exception as e:
107 logging.error('Failed to delete %s. Reason: %s' % (filepath, e))
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100108
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000109 logpipe = PipeLogging(logging.INFO)
110
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100111 os.chdir(build_dir)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100112 cmake_toolchain_file = os.path.join(current_file_dir, "scripts", "cmake",
113 "toolchains", toolchain_file_name)
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100114 cmake_command = (f"cmake .. -DTARGET_PLATFORM={target_platform}" +
115 f" -DTARGET_SUBSYSTEM={target_subsystem}" +
116 f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}" +
117 f" -DETHOS_U_NPU_ID={ethos_u_cfg.ethos_u_npu_id}" +
118 f" -DETHOS_U_NPU_CONFIG_ID={ethos_u_cfg.ethos_u_config_id}")
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100119
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100120 logging.info(cmake_command)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000121 state = subprocess.run(cmake_command, shell=True, stdout=logpipe,
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100122 stderr=subprocess.STDOUT)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100123
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000124 make_command = f"make -j{make_jobs}"
125 if make_verbose :
126 make_command += " VERBOSE=1"
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100127 logging.info(make_command)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000128 state = subprocess.run(make_command, shell=True, stdout=logpipe,
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100129 stderr=subprocess.STDOUT)
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000130
131 logpipe.close()
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100132
133
134if __name__ == '__main__':
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000135 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100136 parser.add_argument("--toolchain", default="gnu",
137 help="""
138 Specify the toolchain to use (Arm or GNU).
139 Options are [gnu, arm]; default is gnu.
140 """)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100141 parser.add_argument("--skip-download",
142 help="Do not download resources: models and test vectors",
143 action="store_true")
144 parser.add_argument("--skip-vela",
145 help="Do not run Vela optimizer on downloaded models.",
146 action="store_true")
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100147 parser.add_argument("--npu-config-name",
148 help=f"""Arm Ethos-U configuration to build for. Choose from:
149 {valid_npu_config_names}""",
150 default=default_npu_config_names[0])
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000151 parser.add_argument("--make-jobs",
152 help="Number of jobs to run with make",
153 default=multiprocessing.cpu_count())
154 parser.add_argument("--make-verbose",
155 help="Make runs with VERBOSE=1",
156 action='store_true')
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100157 args = parser.parse_args()
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100158
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100159 logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG,
160 filemode='w')
Kshitij Sisodiab9e9c892021-05-27 13:57:35 +0100161 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
162
Kshitij Sisodia3be26232021-10-29 12:29:06 +0100163 run(args.toolchain.lower(),
164 not args.skip_download,
165 not args.skip_vela,
Cisco Cervellera6ef76bd2021-11-25 18:32:27 +0000166 args.npu_config_name,
167 args.make_jobs,
168 args.make_verbose)