6987cd21f252690bfb10a8f91a9ec047a5050124
[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 api_types = ['vnet', 'plugins']
25 src_dir_depth = 3
26 output_path = pathlib.Path(
27     '%s/build-root/install-vpp-native/vpp/share/vpp/api/' % BASE_DIR)
28 output_path_debug = pathlib.Path(
29     '%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/' % BASE_DIR)
30
31 output_dir_map = {'vnet': 'core',
32                   'plugins': 'plugins'}
33
34
35 def api_search_globs(src_dir):
36     globs = []
37     for g in api_types:
38         globs.extend(list(src_dir.glob('%s/**/*.api' % g)))
39     return globs
40
41
42 def api_files(src_dir):
43     print("Searching '%s' for .api files." % src_dir.as_posix())
44     return [x for x in api_search_globs(src_dir)]
45
46
47 def vppapigen(vppapigen_bin, output_path, src_dir, src_file):
48     try:
49         subprocess.check_output(
50             [vppapigen_bin, '--includedir', src_dir.as_posix(),
51              '--input', src_file.as_posix(), 'JSON',
52              '--output', '%s/%s/%s.json' % (
53                  output_path,
54                  output_dir_map[src_file.as_posix().split('/')[
55                      src_dir_depth + BASE_DIR.count('/') - 1]],
56                  src_file.name)])
57     except KeyError:
58         print('src_file: %s' % src_file)
59         raise
60
61
62 def main():
63     cliparser = argparse.ArgumentParser(
64         description='VPP API JSON definition generator')
65     cliparser.add_argument('--srcdir', action='store',
66                            default='%s/src' % BASE_DIR),
67     cliparser.add_argument('--output', action='store',
68                            help='directory to store files'),
69     cliparser.add_argument('--debug-target', action='store_true',
70                            default=False,
71                            help="'True' if -debug target"),
72     args = cliparser.parse_args()
73
74     src_dir = pathlib.Path(args.srcdir)
75     output_target = output_path_debug if args.debug_target else output_path
76
77     if args.output:
78         output_dir = pathlib.Path(args.output)
79     else:
80         output_dir = pathlib.Path(output_target)
81
82     for d in output_dir_map.values():
83         output_dir.joinpath(d).mkdir(exist_ok=True, parents=True)
84
85     for f in output_dir.glob('**/*.api.json'):
86         f.unlink()
87
88     for f in api_files(src_dir):
89         vppapigen(vppapigen_bin, output_dir, src_dir, f)
90     print('json files written to: %s/.' % output_dir)
91
92
93 if __name__ == '__main__':
94     main()