Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / one-cmds / one-codegen
1 #!/usr/bin/env bash
2 ''''export SCRIPT_PATH="$(cd "$(dirname "${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 os
24 import subprocess
25 import sys
26 import tempfile
27
28 import utils as _utils
29
30
31 def _get_backends_list():
32     dir_path = os.path.dirname(os.path.realpath(__file__))
33     files = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))]
34     backends_list = []
35     for cand in files:
36         if cand.endswith('-compile'):
37             # 8 : length of '-compile'
38             backends_list.append(cand[:-8])
39     return backends_list
40
41
42 def _get_parser():
43     parser = argparse.ArgumentParser(description='command line tool for code generation')
44
45     _utils._add_default_arg(parser)
46
47     # get backend list in the directory
48     backends_list = _get_backends_list()
49     if not backends_list:
50         backends_list_message = '(There is no available backend drivers)'
51     else:
52         backends_list_message = '(available backend drivers: ' + '.'.join(
53             backends_list) + ')'
54     backend_help_message = 'backend name to use ' + backends_list_message
55     parser.add_argument('-b', '--backend', type=str, help=backend_help_message)
56
57     return parser
58
59
60 def _verify_arg(parser, args):
61     """verify given arguments"""
62     # check if required arguments is given
63     missing = []
64     if not _utils._is_valid_attr(args, 'backend'):
65         missing.append('-b/--backend')
66     if len(missing):
67         parser.error('the following arguments are required: ' + ' '.join(missing))
68
69
70 def _parse_arg(parser):
71     args, unknown_args = parser.parse_known_args()
72     # print version
73     if args.version:
74         _utils._print_version_and_exit(__file__)
75
76     return args, unknown_args
77
78
79 def main():
80     # parse arguments
81     parser = _get_parser()
82     args, unknown_args = _parse_arg(parser)
83
84     # parse configuration file
85     _utils._parse_cfg(args, 'one-codegen')
86
87     # verify arguments
88     _verify_arg(parser, args)
89
90     # make a command to run given backend driver
91     dir_path = os.path.dirname(os.path.realpath(__file__))
92     codegen_path = os.path.join(dir_path, getattr(args, 'backend') + '-compile')
93     codegen_cmd = [codegen_path] + unknown_args
94     if _utils._is_valid_attr(args, 'command'):
95         codegen_cmd += getattr(args, 'command').split()
96
97     # run backend driver
98     with subprocess.Popen(
99             codegen_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
100             bufsize=1) as p:
101         for line in p.stdout:
102             sys.stdout.buffer.write(line)
103
104
105 if __name__ == '__main__':
106     main()