Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 2 | # |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 3 | # Copyright (c) 2016,2018 Cisco and/or its affiliates. |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 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 |
Marek Gradzki | 4746a5d | 2017-01-27 08:57:40 +0100 | [diff] [blame] | 9 | # |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 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 | # |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 16 | import argparse |
Marek Gradzki | 568cc60 | 2018-01-26 11:39:27 +0100 | [diff] [blame] | 17 | import logging |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 18 | import os |
| 19 | import sys |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 20 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 21 | from jvppgen.types_gen import generate_types |
Marek Gradzki | 3e2d57d | 2018-06-20 14:49:39 +0200 | [diff] [blame] | 22 | from jvppgen.enums_gen import generate_enums |
Marek Gradzki | 5b3d95b | 2018-06-27 10:18:18 +0200 | [diff] [blame^] | 23 | from jvppgen.unions_gen import generate_unions |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 24 | from jvppgen.dto_gen import generate_dtos |
| 25 | from jvppgen.jvpp_ifc_gen import generate_java_ifc |
| 26 | from jvppgen.jvpp_impl_gen import generate_java_impl |
| 27 | from jvppgen.callback_gen import generate_callbacks |
| 28 | from jvppgen.jni_gen import generate_jni |
| 29 | from jvppgen.notification_gen import generate_notifications |
| 30 | from jvppgen.jvpp_future_facade_gen import generate_future_facade |
| 31 | from jvppgen.jvpp_callback_facade_gen import generate_callback_facade |
| 32 | from jvppgen.jvpp_model import JVppModel |
Marek Gradzki | 568cc60 | 2018-01-26 11:39:27 +0100 | [diff] [blame] | 33 | |
| 34 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 35 | def generate_jvpp(root_dir, model, logger): |
| 36 | base_dir = "%s/target/%s" % (root_dir, model.plugin_package.replace(".", "/")) |
| 37 | generate_types(_work_dir(base_dir, "types"), model, logger) |
Marek Gradzki | 3e2d57d | 2018-06-20 14:49:39 +0200 | [diff] [blame] | 38 | generate_enums(_work_dir(base_dir, "types"), model, logger) |
Marek Gradzki | 5b3d95b | 2018-06-27 10:18:18 +0200 | [diff] [blame^] | 39 | generate_unions(_work_dir(base_dir, "types"), model, logger) |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 40 | generate_dtos(_work_dir(base_dir, "dto"), model, logger) |
| 41 | generate_java_ifc(_work_dir(base_dir), model, logger) |
| 42 | generate_java_impl(_work_dir(base_dir), model, logger) |
| 43 | generate_callbacks(_work_dir(base_dir, "callback"), model, logger) |
| 44 | generate_jni(root_dir, model, logger) |
| 45 | generate_notifications(_work_dir(base_dir, "notification"), model, logger) |
| 46 | generate_future_facade(_work_dir(base_dir, "future"), model, logger) |
| 47 | generate_callback_facade(_work_dir(base_dir, "callfacade"), model, logger) |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 48 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 49 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 50 | def _work_dir(work_dir, sub_dir=None): |
| 51 | if sub_dir: |
| 52 | work_dir = "%s/%s" % (work_dir, sub_dir) |
| 53 | try: |
| 54 | os.makedirs(work_dir) |
| 55 | except OSError: |
| 56 | if not os.path.isdir(work_dir): |
| 57 | raise |
| 58 | return work_dir |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 59 | |
| 60 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 61 | def _init_logger(): |
| 62 | try: |
| 63 | verbose = int(os.getenv("V", 0)) |
| 64 | except: |
| 65 | verbose = 0 |
| 66 | |
| 67 | log_level = logging.WARNING |
| 68 | if verbose == 1: |
| 69 | log_level = logging.INFO |
| 70 | elif verbose >= 2: |
| 71 | log_level = logging.DEBUG |
| 72 | |
| 73 | logging.basicConfig(stream=sys.stdout, level=log_level) |
| 74 | logger = logging.getLogger("JVPP GEN") |
| 75 | logger.setLevel(log_level) |
| 76 | return logger |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 77 | |
| 78 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 79 | if __name__ == '__main__': |
| 80 | logger = _init_logger() |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 81 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 82 | argparser = argparse.ArgumentParser(description="VPP Java API generator") |
| 83 | argparser.add_argument('-i', nargs='+', metavar='api_file.json', help="json vpp api file(s)") |
| 84 | argparser.add_argument('--plugin_name') |
| 85 | argparser.add_argument('--root_dir') |
| 86 | args = argparser.parse_args() |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 87 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 88 | logger.info("Generating Java API for %s" % args.i) |
| 89 | logger.debug("plugin_name: %s" % args.plugin_name) |
| 90 | logger.debug("root_dir: %s" % args.root_dir) |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 91 | |
Marek Gradzki | a51ccb5 | 2018-02-09 13:42:12 +0100 | [diff] [blame] | 92 | model = JVppModel(logger, args.i, args.plugin_name) |
| 93 | generate_jvpp(args.root_dir, model, logger) |