gen3_julia.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # This file is part of OpenCV project.
  3. # It is subject to the license terms in the LICENSE file found in the top-level directory
  4. # of this distribution and at http://opencv.org/license.html
  5. # Copyright (C) 2020 by Archit Rungta
  6. from __future__ import unicode_literals # Needed for python2
  7. import hdr_parser, sys, re, os
  8. from string import Template
  9. from pprint import pprint
  10. from collections import namedtuple
  11. if sys.version_info[0] >= 3:
  12. from io import StringIO
  13. else:
  14. from cStringIO import StringIO
  15. import os, shutil
  16. from parse_tree import *
  17. submodule_template = Template('')
  18. root_template = Template('')
  19. with open("binding_templates_jl/template_cv2_submodule.jl", "r") as f:
  20. submodule_template = Template(f.read())
  21. with open("binding_templates_jl/template_cv2_root.jl", "r") as f:
  22. root_template = Template(f.read())
  23. class FuncVariant(FuncVariant):
  24. def get_complete_code(self, classname='', isalgo = False, iscons = False, gen_default = True, ns = ''):
  25. return 'const %s = OpenCV.%s_%s' %(self.mapped_name, ns, self.mapped_name)
  26. def gen(srcfiles):
  27. namespaces, _ = gen_tree(srcfiles)
  28. jl_code = StringIO()
  29. for name, ns in namespaces.items():
  30. # cv_types.extend(ns.registered)
  31. jl_code = StringIO()
  32. nsname = '_'.join(name.split('::')[1:])
  33. # Do not duplicate functions. This should prevent overwriting of Mat function by UMat functions
  34. function_signatures = []
  35. if name != 'cv':
  36. for cname, cl in ns.classes.items():
  37. cl.__class__ = ClassInfo
  38. for mname, fs in cl.methods.items():
  39. for f in fs:
  40. f.__class__ = FuncVariant
  41. if f.mapped_name in function_signatures:
  42. print("Skipping entirely: ", f.name)
  43. continue
  44. jl_code.write('\n%s' % f.get_complete_code(isalgo = cl.isalgorithm, ns=nsname))
  45. function_signatures.append(f.mapped_name)
  46. for f in cl.constructors:
  47. f.__class__ = FuncVariant
  48. jl_code.write('\n%s' % f.get_complete_code(classname = cl.mapped_name, isalgo = cl.isalgorithm, iscons = True, ns=nsname))
  49. break
  50. for mname, fs in ns.funcs.items():
  51. for f in fs:
  52. f.__class__ = FuncVariant
  53. if f.mapped_name in function_signatures:
  54. continue
  55. jl_code.write('\n%s' % f.get_complete_code(ns=nsname))
  56. function_signatures.append(f.mapped_name)
  57. jl_code.write('\n')
  58. for mapname, cname in sorted(ns.consts.items()):
  59. jl_code.write(' const %s = OpenCV.%s_%s\n'%(cname, name.replace('::', '_'), cname))
  60. compat_name = re.sub(r"([a-z])([A-Z])", r"\1_\2", cname).upper()
  61. if cname != compat_name:
  62. jl_code.write(' const %s = OpenCV.%s_%s;\n'%(compat_name, name.replace('::', '_'), compat_name))
  63. imports = ''
  64. for namex in namespaces:
  65. if namex.startswith(name) and len(namex.split('::')) == 1 + len(name.split('::')):
  66. imports = imports + '\ninclude("%s_wrap.jl")'%namex.replace('::', '_')
  67. code = ''
  68. if name == 'cv':
  69. code = root_template.substitute(modname = name, code = jl_code.getvalue(), submodule_imports = imports)
  70. else:
  71. code = submodule_template.substitute(modname = name.split('::')[-1], code = jl_code.getvalue(), submodule_imports = imports)
  72. with open ('autogen_jl/%s_wrap.jl' % ns.name.replace('::', '_'), 'w') as fd:
  73. fd.write(code)
  74. srcfiles = hdr_parser.opencv_hdr_list
  75. if len(sys.argv) > 1:
  76. srcfiles = [l.strip() for l in sys.argv[1].split(';')]
  77. gen(srcfiles)