libcanard_dsdlc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. #
  3. # UAVCAN DSDL compiler for libcanard
  4. # Supported Python versions: 3.2+, 2.7.
  5. #
  6. # This code is written by Pavel Kirienko for libuavcan DSDL generator
  7. # copied and modified for the libcanard use
  8. #
  9. # Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
  10. # Copyright (C) 2018 Intel Corporation
  11. #
  12. from __future__ import division, absolute_import, print_function, unicode_literals
  13. import os, sys, logging, argparse
  14. # This trickery allows to use the compiler even if pyuavcan is not installed in the system.
  15. # This is extremely important, as it makes the compiler (and therefore libcanard in general)
  16. # totally dependency-free, except for the Python interpreter itself.
  17. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  18. LOCAL_PYUAVCAN_DIR = os.path.join(SCRIPT_DIR, 'pyuavcan')
  19. RUNNING_FROM_SRC_DIR = os.path.isdir(LOCAL_PYUAVCAN_DIR)
  20. if RUNNING_FROM_SRC_DIR:
  21. sys.path.insert(0, SCRIPT_DIR)
  22. sys.path.insert(0, LOCAL_PYUAVCAN_DIR)
  23. def configure_logging(verbosity):
  24. fmt = '%(message)s'
  25. level = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG }.get(verbosity or 0, logging.DEBUG)
  26. logging.basicConfig(stream=sys.stderr, level=level, format=fmt)
  27. def die(text):
  28. print(text, file=sys.stderr)
  29. exit(1)
  30. DEFAULT_OUTDIR = 'dsdlc_generated'
  31. DESCRIPTION = '''UAVCAN DSDL compiler for the libcanard.
  32. Takes an input directory/directories that contains an hierarchy of DSDL
  33. definitions and converts it into C header and source files for the libcanard.
  34. This script can be used directly from the source directory, no installation required!
  35. Supported Python versions: 3.2+, 2.7.
  36. '''
  37. argparser = argparse.ArgumentParser(description=DESCRIPTION)
  38. argparser.add_argument('source_dir', nargs='+', help='source directory with DSDL definitions')
  39. argparser.add_argument('--header_only', '-ho', action='store_true', help='Generate as header only library')
  40. argparser.add_argument('--verbose', '-v', action='count', help='verbosity level (-v, -vv)')
  41. argparser.add_argument('--outdir', '-O', default=DEFAULT_OUTDIR, help='output directory, default %s' % DEFAULT_OUTDIR)
  42. argparser.add_argument('--incdir', '-I', default=[], action='append', help=
  43. '''nested type namespaces, one path per argument. Can be also specified through the environment variable
  44. UAVCAN_DSDL_INCLUDE_PATH, where the path entries are separated by colons ":"''')
  45. args = argparser.parse_args()
  46. configure_logging(args.verbose)
  47. try:
  48. extra_incdir = os.environ['UAVCAN_DSDL_INCLUDE_PATH'].split(':')
  49. logging.info('Additional include directories: %s', extra_incdir)
  50. args.incdir += extra_incdir
  51. except KeyError:
  52. pass
  53. from libcanard_dsdl_compiler import run as dsdlc_run
  54. try:
  55. dsdlc_run(args.source_dir, args.incdir, args.outdir, args.header_only)
  56. except Exception as ex:
  57. logging.error('Compiler failure', exc_info=True)
  58. die(str(ex))