Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 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 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 19 | |
| 20 | BASE_DIR = ( |
| 21 | subprocess.check_output("git rev-parse --show-toplevel", shell=True) |
| 22 | .strip() |
| 23 | .decode() |
| 24 | ) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 25 | vppapigen_bin = pathlib.Path( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 26 | "%s/src/tools/vppapigen/vppapigen.py" % BASE_DIR |
| 27 | ).as_posix() |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 28 | |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 29 | src_dir_depth = 3 |
| 30 | output_path = pathlib.Path( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 31 | "%s/build-root/install-vpp-native/vpp/share/vpp/api/" % BASE_DIR |
| 32 | ) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 33 | output_path_debug = pathlib.Path( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 34 | "%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/" % BASE_DIR |
| 35 | ) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 36 | |
Vratko Polak | 6307415 | 2019-07-18 15:58:41 +0200 | [diff] [blame] | 37 | output_dir_map = { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | "plugins": "plugins", |
| 39 | "vlibmemory": "core", |
| 40 | "vnet": "core", |
| 41 | "vlib": "core", |
| 42 | "vpp": "core", |
Vratko Polak | 6307415 | 2019-07-18 15:58:41 +0200 | [diff] [blame] | 43 | } |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def api_search_globs(src_dir): |
| 47 | globs = [] |
Vratko Polak | 6307415 | 2019-07-18 15:58:41 +0200 | [diff] [blame] | 48 | for g in output_dir_map: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 49 | globs.extend(list(src_dir.glob("%s/**/*.api" % g))) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 50 | return globs |
| 51 | |
| 52 | |
| 53 | def 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 | |
| 58 | def vppapigen(vppapigen_bin, output_path, src_dir, src_file): |
| 59 | try: |
| 60 | subprocess.check_output( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 61 | [ |
| 62 | vppapigen_bin, |
| 63 | "--includedir", |
| 64 | src_dir.as_posix(), |
| 65 | "--input", |
| 66 | src_file.as_posix(), |
| 67 | "JSON", |
| 68 | "--output", |
| 69 | "%s/%s/%s.json" |
| 70 | % ( |
| 71 | output_path, |
| 72 | output_dir_map[ |
| 73 | src_file.as_posix().split("/")[ |
| 74 | src_dir_depth + BASE_DIR.count("/") - 1 |
| 75 | ] |
| 76 | ], |
| 77 | src_file.name, |
| 78 | ), |
| 79 | ] |
| 80 | ) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 81 | except KeyError: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 82 | print("src_file: %s" % src_file) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 83 | raise |
| 84 | |
| 85 | |
| 86 | def main(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 87 | cliparser = argparse.ArgumentParser(description="VPP API JSON definition generator") |
| 88 | cliparser.add_argument("--srcdir", action="store", default="%s/src" % BASE_DIR), |
| 89 | cliparser.add_argument("--output", action="store", help="directory to store files"), |
| 90 | cliparser.add_argument( |
| 91 | "--debug-target", |
| 92 | action="store_true", |
| 93 | default=False, |
| 94 | help="'True' if -debug target", |
| 95 | ), |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 96 | args = cliparser.parse_args() |
| 97 | |
| 98 | src_dir = pathlib.Path(args.srcdir) |
| 99 | output_target = output_path_debug if args.debug_target else output_path |
| 100 | |
| 101 | if args.output: |
| 102 | output_dir = pathlib.Path(args.output) |
| 103 | else: |
| 104 | output_dir = pathlib.Path(output_target) |
| 105 | |
| 106 | for d in output_dir_map.values(): |
| 107 | output_dir.joinpath(d).mkdir(exist_ok=True, parents=True) |
| 108 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 109 | for f in output_dir.glob("**/*.api.json"): |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 110 | f.unlink() |
| 111 | |
| 112 | for f in api_files(src_dir): |
| 113 | vppapigen(vppapigen_bin, output_dir, src_dir, f) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 114 | print("json files written to: %s/." % output_dir) |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 115 | |
| 116 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 117 | if __name__ == "__main__": |
Paul Vinciguerra | 9529feb | 2019-07-15 15:22:31 -0400 | [diff] [blame] | 118 | main() |