4be4f69f591124dee23920105f03b962e7edd03b
[vpp.git] / src / tools / vppapigen / generate_json.py
1 #!/usr/bin/env python3
2 #  Copyright (c) 2019. Vinci Consulting Corp. All Rights Reserved.
3 #
4 #  Licensed under the Apache License, Version 2.0 (the "License");
5 #  you may not use this file except in compliance with the License.
6 #  You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS,
12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #  See the License for the specific language governing permissions and
14 #  limitations under the License.
15
16 import argparse
17 import pathlib
18 import subprocess
19 BASE_DIR = subprocess.check_output('git rev-parse --show-toplevel',
20                                    shell=True).strip().decode()
21 vppapigen_bin = pathlib.Path(
22     '%s/src/tools/vppapigen/vppapigen.py' % BASE_DIR).as_posix()
23
24 src_dir_depth = 3
25 output_path = pathlib.Path(
26     '%s/build-root/install-vpp-native/vpp/share/vpp/api/' % BASE_DIR)
27 output_path_debug = pathlib.Path(
28     '%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/' % BASE_DIR)
29
30 output_dir_map = {
31     'plugins': 'plugins',
32     'vlibmemory': 'core',
33     'vnet': 'core',
34     'vpp': 'core',
35 }
36
37
38 def api_search_globs(src_dir):
39     globs = []
40     for g in output_dir_map:
41         globs.extend(list(src_dir.glob('%s/**/*.api' % g)))
42     return globs
43
44
45 def api_files(src_dir):
46     print("Searching '%s' for .api files." % src_dir.as_posix())
47     return [x for x in api_search_globs(src_dir)]
48
49
50 def vppapigen(vppapigen_bin, output_path, src_dir, src_file):
51     try:
52         subprocess.check_output(
53             [vppapigen_bin, '--includedir', src_dir.as_posix(),
54              '--input', src_file.as_posix(), 'JSON',
55              '--output', '%s/%s/%s.json' % (
56                  output_path,
57                  output_dir_map[src_file.as_posix().split('/')[
58                      src_dir_depth + BASE_DIR.count('/') - 1]],
59                  src_file.name)])
60     except KeyError:
61         print('src_file: %s' % src_file)
62         raise
63
64
65 def main():
66     cliparser = argparse.ArgumentParser(
67         description='VPP API JSON definition generator')
68     cliparser.add_argument('--srcdir', action='store',
69                            default='%s/src' % BASE_DIR),
70     cliparser.add_argument('--output', action='store',
71                            help='directory to store files'),
72     cliparser.add_argument('--debug-target', action='store_true',
73                            default=False,
74                            help="'True' if -debug target"),
75     args = cliparser.parse_args()
76
77     src_dir = pathlib.Path(args.srcdir)
78     output_target = output_path_debug if args.debug_target else output_path
79
80     if args.output:
81         output_dir = pathlib.Path(args.output)
82     else:
83         output_dir = pathlib.Path(output_target)
84
85     for d in output_dir_map.values():
86         output_dir.joinpath(d).mkdir(exist_ok=True, parents=True)
87
88     for f in output_dir.glob('**/*.api.json'):
89         f.unlink()
90
91     for f in api_files(src_dir):
92         vppapigen(vppapigen_bin, output_dir, src_dir, f)
93     print('json files written to: %s/.' % output_dir)
94
95
96 if __name__ == '__main__':
97     main()