symdefgen.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. # Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  3. # All rights reserved.
  4. #
  5. # This software is supplied "AS IS" without any warranties.
  6. # RDA assumes no responsibility or liability for the use of the software,
  7. # conveys no license or title under any patent, copyright, or mask work
  8. # right to the product. RDA reserves the right to make changes in the
  9. # software without notification. RDA also make no representation or
  10. # warranty that such application will be suitable for the specified use
  11. # without further testing or modification.
  12. import argparse
  13. import sys
  14. import io
  15. import os
  16. DESCRIPTION = """
  17. generate C defines for symbols.
  18. To void name conflict, it may be needed to change the symbol names
  19. in some modules. This script will parse a symbol list, and generate
  20. C header file, to define each symbol with a prefix.
  21. """
  22. def main(argv):
  23. parser = argparse.ArgumentParser(description=DESCRIPTION)
  24. parser.add_argument("sym", help="symbol list file")
  25. parser.add_argument("prefix", help="prefix to be added")
  26. parser.add_argument("header", help="output header file")
  27. args = parser.parse_args(argv)
  28. symbs = []
  29. with open(args.sym, 'r') as fh:
  30. for line in fh.readlines():
  31. if line.startswith('#'):
  32. continue
  33. line = line.strip()
  34. if not line:
  35. continue
  36. if line not in symbs:
  37. symbs.append(line)
  38. prot = '_{}_'.format(os.path.basename(args.header).upper().replace('.', '_'))
  39. with open(args.header, 'w') as fh:
  40. fh.write('#ifndef {}\n'.format(prot))
  41. fh.write('#define {}\n'.format(prot))
  42. fh.write('// Auto generated. Don\'t edit it manually!\n')
  43. for sym in symbs:
  44. fh.write('#define {} {}{}\n'.format(sym, args.prefix, sym))
  45. fh.write('#endif\n')
  46. return 0
  47. if __name__ == '__main__':
  48. sys.exit(main(sys.argv[1:]))