Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / one-cmds / one-build
1 #!/usr/bin/env bash
2 ''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" # '''
3 ''''export PY_PATH=${SCRIPT_PATH}/venv/bin/python                                       # '''
4 ''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@"                                     # '''
5 ''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # '''
6 ''''exit 255                                                                            # '''
7
8 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
9 #
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at
13 #
14 #    http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21
22 import argparse
23 import configparser
24 import os
25 import subprocess
26 import sys
27
28 import utils as _utils
29
30 # TODO Find better way to suppress trackback on error
31 # This suppression is applied only to `one-build`
32 sys.tracebacklimit = 0
33
34
35 def _get_parser():
36     parser = argparse.ArgumentParser(
37         description='command line tool to run ONE drivers in customized order')
38
39     _utils._add_default_arg(parser)
40
41     return parser
42
43
44 def _parse_arg(parser):
45     args = parser.parse_args()
46     # print version
47     if args.version:
48         _utils._print_version_and_exit(__file__)
49
50     return args
51
52
53 def _verify_arg(parser, args):
54     """verify given arguments"""
55     # check if required arguments is given
56     if not _utils._is_valid_attr(args, 'config'):
57         parser.error('-C/--config argument is required')
58
59
60 def _get_driver_name(driver_name):
61     return {
62         'one-import-bcq': 'one-import-bcq',
63         'one-import-tf': 'one-import-tf',
64         'one-import-tflite': 'one-import-tflite',
65         'one-import-onnx': 'one-import-onnx',
66         'one-optimize': 'one-optimize',
67         'one-quantize': 'one-quantize',
68         'one-pack': 'one-pack',
69         'one-codegen': 'one-codegen'
70     }[driver_name]
71
72
73 def _parse_cfg(args):
74     config = configparser.ConfigParser()
75     config.optionxform = str
76     parsed = config.read(os.path.expanduser(getattr(args, 'config')))
77     if not parsed:
78         raise FileNotFoundError('Not found given configuration file')
79     return config
80
81
82 def _is_available_driver(config, driver_name):
83     return config.has_option('one-build', driver_name) and config.getboolean(
84         'one-build', driver_name)
85
86
87 def _verify_cfg(driver_list, config):
88     if not config.has_section('one-build'):
89         raise ImportError('[one-build] section is required in configuraion file')
90
91     import_driver_cnt = 0
92     if _is_available_driver(config, 'one-import-tf'):
93         import_driver_cnt += 1
94     if _is_available_driver(config, 'one-import-tflite'):
95         import_driver_cnt += 1
96     if _is_available_driver(config, 'one-import-bcq'):
97         import_driver_cnt += 1
98     if _is_available_driver(config, 'one-import-onnx'):
99         import_driver_cnt += 1
100     if import_driver_cnt > 1:
101         raise AssertionError('Only one import-* driver can be executed')
102
103
104 def main():
105     # parse arguments
106     # since the configuration file path is required first,
107     # parsing of the configuration file proceeds after this.
108     parser = _get_parser()
109     args = _parse_arg(parser)
110
111     # verify arguments
112     _verify_arg(parser, args)
113
114     # parse configuration file
115     config = _parse_cfg(args)
116
117     # verify configuration file
118     drivers = [
119         'one-import-tf', 'one-import-tflite', 'one-import-bcq', 'one-import-onnx',
120         'one-optimize', 'one-quantize', 'one-pack', 'one-codegen'
121     ]
122     _verify_cfg(drivers, config)
123
124     # get sections to run
125     section_to_run = []
126     for d in drivers:
127         if _is_available_driver(config, d):
128             section_to_run.append(d)
129
130     # run
131     dir_path = os.path.dirname(os.path.realpath(__file__))
132     for section in section_to_run:
133         driver_path = os.path.join(dir_path, _get_driver_name(section))
134         cmd = [driver_path, '--config', getattr(args, 'config'), '--section', section]
135         _utils._run(cmd)
136
137
138 if __name__ == '__main__':
139     _utils._safemain(main, __file__)