blob: e4aa59d43c9f5e8cfcdfbdfd3fb3281feeb5768c [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
26from set_up_default_resources import set_up_resources
27
28
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010029def run(toolchain: str, download_resources: bool, run_vela_on_models: bool):
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010030 """
31 Run the helpers scripts.
32
33 Parameters:
34 ----------
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010035 toolchain (str) : Specifies if 'gnu' or 'arm' toolchain needs to be used.
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010036 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 Gottardi2181d0a2021-04-07 09:27:38 +010041
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010042 # 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 Gottardi2181d0a2021-04-07 09:27:38 +010051 if download_resources is True:
52 logging.info("Downloading resources.")
53 set_up_resources(run_vela_on_models)
54
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010055 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010056 logging.info("Building default configuration.")
57 target_platform = "mps3"
58 target_subsystem = "sse-300"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010059 build_dir = os.path.join(current_file_dir,
60 f"cmake-build-{target_platform}-{target_subsystem}-{toolchain}-release")
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010061 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 Sisodiaf9c19ea2021-05-07 16:08:14 +010074
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010075 os.chdir(build_dir)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010076 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 Gottardi2181d0a2021-04-07 09:27:38 +010082 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
92if __name__ == '__main__':
93 parser = ArgumentParser()
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094 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 Gottardi2181d0a2021-04-07 09:27:38 +010099 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 Sisodiab9e9c892021-05-27 13:57:35 +0100106
107 logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG)
108 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
109
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100110 run(args.toolchain.lower(), not args.skip_download, not args.skip_vela)