blob: 3bb91b1c1699afa5f582b436987c6e9a24e80e77 [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__))
41 logging.basicConfig(filename='log_build_default.log', level=logging.DEBUG)
42 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
43
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010044 # 1. Make sure the toolchain is supported, and set the right one here
45 supported_toolchain_ids = ["gnu", "arm"]
46 assert toolchain in supported_toolchain_ids, f"Toolchain must be from {supported_toolchain_ids}"
47 if toolchain == "arm":
48 toolchain_file_name = "bare-metal-armclang.cmake"
49 elif toolchain == "gnu":
50 toolchain_file_name = "bare-metal-gcc.cmake"
51
52 # 2. Download models if specified
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010053 if download_resources is True:
54 logging.info("Downloading resources.")
55 set_up_resources(run_vela_on_models)
56
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010057 # 3. Build default configuration
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010058 logging.info("Building default configuration.")
59 target_platform = "mps3"
60 target_subsystem = "sse-300"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010061 build_dir = os.path.join(current_file_dir,
62 f"cmake-build-{target_platform}-{target_subsystem}-{toolchain}-release")
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010063 try:
64 os.mkdir(build_dir)
65 except FileExistsError:
66 # Directory already exists, clean it
67 for filename in os.listdir(build_dir):
68 filepath = os.path.join(build_dir, filename)
69 try:
70 if os.path.isfile(filepath) or os.path.islink(filepath):
71 os.unlink(filepath)
72 elif os.path.isdir(filepath):
73 shutil.rmtree(filepath)
74 except Exception as e:
75 logging.error('Failed to delete %s. Reason: %s' % (filepath, e))
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010076
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010077 os.chdir(build_dir)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010078 cmake_toolchain_file = os.path.join(current_file_dir, "scripts", "cmake",
79 "toolchains", toolchain_file_name)
80 cmake_command = (f"cmake .. -DTARGET_PLATFORM={target_platform} " +
81 f"-DTARGET_SUBSYSTEM={target_subsystem} " +
82 f" -DCMAKE_TOOLCHAIN_FILE={cmake_toolchain_file}")
83
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010084 logging.info(cmake_command)
85 state = subprocess.run(cmake_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
86 logging.info(state.stdout.decode('utf-8'))
87
88 make_command = f"make -j{multiprocessing.cpu_count()}"
89 logging.info(make_command)
90 state = subprocess.run(make_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
91 logging.info(state.stdout.decode('utf-8'))
92
93
94if __name__ == '__main__':
95 parser = ArgumentParser()
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010096 parser.add_argument("--toolchain", default="gnu",
97 help="""
98 Specify the toolchain to use (Arm or GNU).
99 Options are [gnu, arm]; default is gnu.
100 """)
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100101 parser.add_argument("--skip-download",
102 help="Do not download resources: models and test vectors",
103 action="store_true")
104 parser.add_argument("--skip-vela",
105 help="Do not run Vela optimizer on downloaded models.",
106 action="store_true")
107 args = parser.parse_args()
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100108 run(args.toolchain.lower(), not args.skip_download, not args.skip_vela)