blob: 288e519edd25b0d0eb6f92a67cae85b7b5b4f88e [file] [log] [blame]
Paul Vinciguerra9529feb2019-07-15 15:22:31 -04001#!/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
16import argparse
17import pathlib
18import subprocess
19BASE_DIR = subprocess.check_output('git rev-parse --show-toplevel',
20 shell=True).strip().decode()
21vppapigen_bin = pathlib.Path(
22 '%s/src/tools/vppapigen/vppapigen.py' % BASE_DIR).as_posix()
23
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040024src_dir_depth = 3
25output_path = pathlib.Path(
26 '%s/build-root/install-vpp-native/vpp/share/vpp/api/' % BASE_DIR)
27output_path_debug = pathlib.Path(
28 '%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/' % BASE_DIR)
29
Vratko Polak63074152019-07-18 15:58:41 +020030output_dir_map = {
31 'plugins': 'plugins',
32 'vlibmemory': 'core',
33 'vnet': 'core',
Nathan Skrzypczak9ff0cd72020-08-21 11:16:44 +020034 'vlib': 'core',
Vratko Polak63074152019-07-18 15:58:41 +020035 'vpp': 'core',
36}
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040037
38
39def api_search_globs(src_dir):
40 globs = []
Vratko Polak63074152019-07-18 15:58:41 +020041 for g in output_dir_map:
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040042 globs.extend(list(src_dir.glob('%s/**/*.api' % g)))
43 return globs
44
45
46def api_files(src_dir):
47 print("Searching '%s' for .api files." % src_dir.as_posix())
48 return [x for x in api_search_globs(src_dir)]
49
50
51def vppapigen(vppapigen_bin, output_path, src_dir, src_file):
52 try:
53 subprocess.check_output(
54 [vppapigen_bin, '--includedir', src_dir.as_posix(),
55 '--input', src_file.as_posix(), 'JSON',
56 '--output', '%s/%s/%s.json' % (
57 output_path,
58 output_dir_map[src_file.as_posix().split('/')[
59 src_dir_depth + BASE_DIR.count('/') - 1]],
60 src_file.name)])
61 except KeyError:
62 print('src_file: %s' % src_file)
63 raise
64
65
66def main():
67 cliparser = argparse.ArgumentParser(
68 description='VPP API JSON definition generator')
69 cliparser.add_argument('--srcdir', action='store',
70 default='%s/src' % BASE_DIR),
71 cliparser.add_argument('--output', action='store',
72 help='directory to store files'),
73 cliparser.add_argument('--debug-target', action='store_true',
74 default=False,
75 help="'True' if -debug target"),
76 args = cliparser.parse_args()
77
78 src_dir = pathlib.Path(args.srcdir)
79 output_target = output_path_debug if args.debug_target else output_path
80
81 if args.output:
82 output_dir = pathlib.Path(args.output)
83 else:
84 output_dir = pathlib.Path(output_target)
85
86 for d in output_dir_map.values():
87 output_dir.joinpath(d).mkdir(exist_ok=True, parents=True)
88
89 for f in output_dir.glob('**/*.api.json'):
90 f.unlink()
91
92 for f in api_files(src_dir):
93 vppapigen(vppapigen_bin, output_dir, src_dir, f)
94 print('json files written to: %s/.' % output_dir)
95
96
97if __name__ == '__main__':
98 main()