blob: 610f84f5533bddde1a9cdca0f28cf5fb0f041680 [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
Nathan Skrzypczak1b299fa2022-06-16 17:00:02 +020019import vppapigen
20import os
21from multiprocessing import Pool
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020022
23BASE_DIR = (
24 subprocess.check_output("git rev-parse --show-toplevel", shell=True)
25 .strip()
26 .decode()
27)
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040028
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040029src_dir_depth = 3
30output_path = pathlib.Path(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020031 "%s/build-root/install-vpp-native/vpp/share/vpp/api/" % BASE_DIR
32)
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040033output_path_debug = pathlib.Path(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034 "%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/" % BASE_DIR
35)
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040036
Vratko Polak63074152019-07-18 15:58:41 +020037output_dir_map = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 "plugins": "plugins",
39 "vlibmemory": "core",
40 "vnet": "core",
41 "vlib": "core",
42 "vpp": "core",
Vratko Polak63074152019-07-18 15:58:41 +020043}
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040044
45
46def api_search_globs(src_dir):
47 globs = []
Vratko Polak63074152019-07-18 15:58:41 +020048 for g in output_dir_map:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020049 globs.extend(list(src_dir.glob("%s/**/*.api" % g)))
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040050 return globs
51
52
53def api_files(src_dir):
54 print("Searching '%s' for .api files." % src_dir.as_posix())
55 return [x for x in api_search_globs(src_dir)]
56
57
Nathan Skrzypczak1b299fa2022-06-16 17:00:02 +020058def get_n_parallel(n_parallel):
59 if n_parallel == 0:
60 n_parallel = os.environ.get("MAKE_PARALLEL_JOBS", os.cpu_count())
61 try:
62 n_parallel = int(n_parallel)
63 except ValueError:
64 return os.cpu_count()
65 return n_parallel or os.cpu_count()
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040066
67
68def main():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020069 cliparser = argparse.ArgumentParser(description="VPP API JSON definition generator")
70 cliparser.add_argument("--srcdir", action="store", default="%s/src" % BASE_DIR),
71 cliparser.add_argument("--output", action="store", help="directory to store files"),
72 cliparser.add_argument(
Nathan Skrzypczak1b299fa2022-06-16 17:00:02 +020073 "--parallel", type=int, default=0, help="Number of parallel processes"
74 ),
75 cliparser.add_argument(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 "--debug-target",
77 action="store_true",
78 default=False,
79 help="'True' if -debug target",
80 ),
Nathan Skrzypczak1b299fa2022-06-16 17:00:02 +020081
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040082 args = cliparser.parse_args()
83
84 src_dir = pathlib.Path(args.srcdir)
85 output_target = output_path_debug if args.debug_target else output_path
86
87 if args.output:
88 output_dir = pathlib.Path(args.output)
89 else:
90 output_dir = pathlib.Path(output_target)
91
92 for d in output_dir_map.values():
93 output_dir.joinpath(d).mkdir(exist_ok=True, parents=True)
94
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 for f in output_dir.glob("**/*.api.json"):
Paul Vinciguerra9529feb2019-07-15 15:22:31 -040096 f.unlink()
97
Nathan Skrzypczak1b299fa2022-06-16 17:00:02 +020098 with Pool(get_n_parallel(args.parallel)) as p:
99 p.map(
100 vppapigen.run_kw_vppapigen,
101 [
102 {
103 "output": "%s/%s/%s.json"
104 % (
105 output_path,
106 output_dir_map[
107 f.as_posix().split("/")[
108 src_dir_depth + BASE_DIR.count("/") - 1
109 ]
110 ],
111 f.name,
112 ),
113 "input_file": f.as_posix(),
114 "includedir": [src_dir.as_posix()],
115 "output_module": "JSON",
116 }
117 for f in api_files(src_dir)
118 ],
119 )
120
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200121 print("json files written to: %s/." % output_dir)
Paul Vinciguerra9529feb2019-07-15 15:22:31 -0400122
123
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124if __name__ == "__main__":
Paul Vinciguerra9529feb2019-07-15 15:22:31 -0400125 main()