2013-08-11 22:52:27 +08:00
|
|
|
#!/usr/bin/env python
|
2013-03-11 08:19:17 +08:00
|
|
|
|
2013-03-14 15:26:18 +08:00
|
|
|
class MatlabWrapperGenerator(object):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
MatlabWrapperGenerator is a class for generating Matlab mex sources from
|
|
|
|
a set of C++ headers. MatlabWrapperGenerator objects can be default
|
|
|
|
constructed. Given an instance, the gen() method performs the translation.
|
|
|
|
"""
|
2013-03-14 15:26:18 +08:00
|
|
|
|
2013-08-07 14:34:30 +08:00
|
|
|
def gen(self, module_root, modules, extras, output_dir):
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Generate a set of Matlab mex source files by parsing exported symbols
|
|
|
|
in a set of C++ headers. The headers can be input in one (or both) of
|
|
|
|
two methods:
|
|
|
|
1. specify module_root and modules
|
|
|
|
Given a path to the OpenCV module root and a list of module names,
|
|
|
|
the headers to parse are implicitly constructed.
|
|
|
|
2. specifiy header locations explicitly in extras
|
|
|
|
Each element in the list of extras must be of the form:
|
|
|
|
'namespace=/full/path/to/extra/header.hpp' where 'namespace' is
|
|
|
|
the namespace in which the definitions should be added.
|
|
|
|
The output_dir specifies the directory to write the generated sources
|
|
|
|
to.
|
|
|
|
"""
|
2013-03-14 15:26:18 +08:00
|
|
|
# parse each of the files and store in a dictionary
|
|
|
|
# as a separate "namespace"
|
|
|
|
parser = CppHeaderParser()
|
2013-08-07 14:34:30 +08:00
|
|
|
rst = rst_parser.RstParser(parser)
|
|
|
|
rst_parser.verbose = False
|
|
|
|
rst_parser.show_warnings = False
|
|
|
|
rst_parser.show_errors = False
|
|
|
|
rst_parser.show_critical_errors = False
|
|
|
|
|
|
|
|
ns = dict((key, []) for key in modules)
|
|
|
|
doc = dict((key, []) for key in modules)
|
|
|
|
path_template = Template('${module}/include/opencv2/${module}.hpp')
|
|
|
|
|
|
|
|
for module in modules:
|
|
|
|
# construct a header path from the module root and a path template
|
|
|
|
header = os.path.join(module_root, path_template.substitute(module=module))
|
|
|
|
# parse the definitions
|
|
|
|
ns[module] = parser.parse(header)
|
|
|
|
# parse the documentation
|
|
|
|
rst.parse(module, os.path.join(module_root, module))
|
|
|
|
doc[module] = rst.definitions
|
|
|
|
rst.definitions = {}
|
|
|
|
|
|
|
|
for extra in extras:
|
2013-08-08 03:26:56 +08:00
|
|
|
module = extra.split("=")[0]
|
|
|
|
header = extra.split("=")[1]
|
2013-08-07 14:34:30 +08:00
|
|
|
ns[module] = ns[module] + parser.parse(header) if module in ns else parser.parse(header)
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
# cleanify the parser output
|
|
|
|
parse_tree = ParseTree()
|
|
|
|
parse_tree.build(ns)
|
2013-08-07 14:34:30 +08:00
|
|
|
|
2013-03-18 09:47:48 +08:00
|
|
|
# setup the template engine
|
2013-08-05 22:40:57 +08:00
|
|
|
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
|
|
|
|
jtemplate = Environment(loader=FileSystemLoader(template_dir), trim_blocks=True, lstrip_blocks=True)
|
2013-03-18 09:47:48 +08:00
|
|
|
|
|
|
|
# add the custom filters
|
2013-06-28 11:03:38 +08:00
|
|
|
jtemplate.filters['formatMatlabConstant'] = formatMatlabConstant
|
2013-06-27 08:39:08 +08:00
|
|
|
jtemplate.filters['convertibleToInt'] = convertibleToInt
|
2013-03-18 09:47:48 +08:00
|
|
|
jtemplate.filters['toUpperCamelCase'] = toUpperCamelCase
|
|
|
|
jtemplate.filters['toLowerCamelCase'] = toLowerCamelCase
|
|
|
|
jtemplate.filters['toUnderCase'] = toUnderCase
|
2013-08-08 02:46:45 +08:00
|
|
|
jtemplate.filters['matlabURL'] = matlabURL
|
2013-08-07 14:34:30 +08:00
|
|
|
jtemplate.filters['stripTags'] = stripTags
|
2013-08-07 17:28:58 +08:00
|
|
|
jtemplate.filters['filename'] = filename
|
2013-06-22 08:33:24 +08:00
|
|
|
jtemplate.filters['comment'] = comment
|
|
|
|
jtemplate.filters['inputs'] = inputs
|
2013-06-22 13:55:48 +08:00
|
|
|
jtemplate.filters['ninputs'] = ninputs
|
2013-06-22 08:33:24 +08:00
|
|
|
jtemplate.filters['outputs'] = outputs
|
|
|
|
jtemplate.filters['noutputs'] = noutputs
|
2013-08-07 14:34:30 +08:00
|
|
|
jtemplate.filters['qualify'] = qualify
|
2013-08-07 17:28:58 +08:00
|
|
|
jtemplate.filters['slugify'] = slugify
|
2013-06-22 13:55:48 +08:00
|
|
|
jtemplate.filters['only'] = only
|
2013-09-02 22:16:46 +08:00
|
|
|
jtemplate.filters['void'] = void
|
2013-06-22 13:55:48 +08:00
|
|
|
jtemplate.filters['not'] = flip
|
|
|
|
|
2013-03-18 09:47:48 +08:00
|
|
|
# load the templates
|
2013-03-18 10:37:42 +08:00
|
|
|
tfunction = jtemplate.get_template('template_function_base.cpp')
|
|
|
|
tclassm = jtemplate.get_template('template_class_base.m')
|
|
|
|
tclassc = jtemplate.get_template('template_class_base.cpp')
|
|
|
|
tdoc = jtemplate.get_template('template_doc_base.m')
|
2013-06-28 11:03:38 +08:00
|
|
|
tconst = jtemplate.get_template('template_map_base.m')
|
2013-03-18 10:37:42 +08:00
|
|
|
|
|
|
|
# create the build directory
|
2013-03-18 12:48:10 +08:00
|
|
|
output_source_dir = output_dir+'/src'
|
2013-09-02 22:16:46 +08:00
|
|
|
output_private_dir = output_source_dir+'/private'
|
2013-03-18 12:48:10 +08:00
|
|
|
output_class_dir = output_dir+'/+cv'
|
2013-06-27 06:45:05 +08:00
|
|
|
output_map_dir = output_dir+'/map'
|
2013-03-18 12:48:10 +08:00
|
|
|
if not os.path.isdir(output_source_dir):
|
|
|
|
os.mkdir(output_source_dir)
|
|
|
|
if not os.path.isdir(output_private_dir):
|
|
|
|
os.mkdir(output_private_dir)
|
|
|
|
if not os.path.isdir(output_class_dir):
|
|
|
|
os.mkdir(output_class_dir)
|
2013-06-27 06:45:05 +08:00
|
|
|
if not os.path.isdir(output_map_dir):
|
|
|
|
os.mkdir(output_map_dir)
|
2013-03-18 12:48:10 +08:00
|
|
|
|
|
|
|
# populate templates
|
|
|
|
for namespace in parse_tree.namespaces:
|
|
|
|
# functions
|
2013-07-03 07:42:37 +08:00
|
|
|
for method in namespace.methods:
|
|
|
|
populated = tfunction.render(fun=method, time=time, includes=namespace.name)
|
|
|
|
with open(output_source_dir+'/'+method.name+'.cpp', 'wb') as f:
|
2013-03-18 12:48:10 +08:00
|
|
|
f.write(populated)
|
2013-08-07 14:34:30 +08:00
|
|
|
if namespace.name in doc and method.name in doc[namespace.name]:
|
|
|
|
populated = tdoc.render(fun=method, doc=doc[namespace.name][method.name], time=time)
|
|
|
|
with open(output_class_dir+'/'+method.name+'.m', 'wb') as f:
|
|
|
|
f.write(populated)
|
2013-03-18 12:48:10 +08:00
|
|
|
# classes
|
|
|
|
for clss in namespace.classes:
|
|
|
|
# cpp converter
|
|
|
|
populated = tclassc.render(clss=clss, time=time)
|
|
|
|
with open(output_private_dir+'/'+clss.name+'Bridge.cpp', 'wb') as f:
|
|
|
|
f.write(populated)
|
|
|
|
# matlab classdef
|
|
|
|
populated = tclassm.render(clss=clss, time=time)
|
|
|
|
with open(output_class_dir+'/'+clss.name+'.m', 'wb') as f:
|
|
|
|
f.write(populated)
|
2013-06-24 04:34:01 +08:00
|
|
|
|
2013-06-27 06:45:05 +08:00
|
|
|
# create a global constants lookup table
|
|
|
|
const = dict(constants(todict(parse_tree.namespaces)))
|
2013-08-06 23:10:51 +08:00
|
|
|
populated = tconst.render(constants=const, time=time)
|
2013-06-27 08:39:08 +08:00
|
|
|
with open(output_dir+'/cv.m', 'wb') as f:
|
2013-06-28 11:03:38 +08:00
|
|
|
f.write(populated)
|
2013-06-27 06:45:05 +08:00
|
|
|
|
2013-06-24 04:34:01 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-08-11 22:52:27 +08:00
|
|
|
"""
|
|
|
|
Usage: python gen_matlab.py --hdrparser /path/to/hdr_parser/dir
|
|
|
|
--rstparser /path/to/rst_parser/dir
|
|
|
|
--moduleroot /path/to/opencv/modules
|
|
|
|
--modules [core imgproc objdetect etc]
|
|
|
|
--extra namespace=/path/to/extra/header.hpp
|
|
|
|
--outdir /path/to/output/generated/srcs
|
|
|
|
|
|
|
|
gen_matlab.py is the main control script for generating matlab source
|
|
|
|
files from given set of headers. Internally, gen_matlab:
|
|
|
|
1. constructs the headers to parse from the module root and list of modules
|
|
|
|
2. parses the headers using CppHeaderParser
|
|
|
|
3. refactors the definitions using ParseTree
|
2013-09-02 22:16:46 +08:00
|
|
|
4. parses .rst docs using RstParser
|
2013-08-11 22:52:27 +08:00
|
|
|
5. populates the templates for classes, function, enums and docs from the
|
|
|
|
definitions
|
|
|
|
|
|
|
|
gen_matlab.py requires the following inputs:
|
|
|
|
--hdrparser the path to the header parser directory
|
|
|
|
(opencv/modules/python/src2)
|
|
|
|
--rstparser the path to the rst parser directory
|
|
|
|
(opencv/modules/java/generator)
|
|
|
|
--moduleroot (optional) path to the opencv directory containing the modules
|
|
|
|
--modules (optional - required if --moduleroot specified) the modules
|
|
|
|
to produce bindings for. The path to the include directories
|
|
|
|
as well as the namespaces are constructed from the modules
|
|
|
|
and the moduleroot
|
2013-09-02 22:16:46 +08:00
|
|
|
--extra extra headers explicitly defined to parse. This must be in
|
2013-08-11 22:52:27 +08:00
|
|
|
the format "namepsace=/path/to/extra/header.hpp". For example,
|
|
|
|
the core module requires the extra header:
|
|
|
|
"core=/opencv/modules/core/include/opencv2/core/core/base.hpp"
|
|
|
|
--outdir the output directory to put the generated matlab sources. In
|
|
|
|
the OpenCV build this is "${CMAKE_CURRENT_BUILD_DIR}/src"
|
|
|
|
"""
|
2013-09-02 22:16:46 +08:00
|
|
|
|
2013-08-06 23:41:10 +08:00
|
|
|
# parse the input options
|
2013-06-24 04:34:01 +08:00
|
|
|
import sys, re, os, time
|
2013-08-06 23:41:10 +08:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
parser = ArgumentParser()
|
2013-09-15 15:31:34 +08:00
|
|
|
parser.add_argument('--jinja2')
|
2013-08-06 23:41:10 +08:00
|
|
|
parser.add_argument('--hdrparser')
|
|
|
|
parser.add_argument('--rstparser')
|
2013-08-07 14:34:30 +08:00
|
|
|
parser.add_argument('--moduleroot', default='', required=False)
|
|
|
|
parser.add_argument('--modules', nargs='*', default=[], required=False)
|
2013-09-02 22:16:46 +08:00
|
|
|
parser.add_argument('--extra', nargs='*', default=[], required=False)
|
2013-08-06 23:41:10 +08:00
|
|
|
parser.add_argument('--outdir')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# add the hdr_parser and rst_parser modules to the path
|
2013-09-15 15:31:34 +08:00
|
|
|
sys.path.append(args.jinja2)
|
2013-08-06 23:41:10 +08:00
|
|
|
sys.path.append(args.hdrparser)
|
|
|
|
sys.path.append(args.rstparser)
|
|
|
|
|
2013-06-24 04:34:01 +08:00
|
|
|
from string import Template
|
|
|
|
from hdr_parser import CppHeaderParser
|
2013-08-07 14:34:30 +08:00
|
|
|
import rst_parser
|
2013-06-27 06:45:05 +08:00
|
|
|
from parse_tree import ParseTree, todict, constants
|
2013-06-24 04:34:01 +08:00
|
|
|
from filters import *
|
2013-08-05 22:40:57 +08:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2013-06-24 04:34:01 +08:00
|
|
|
|
|
|
|
# create the generator
|
|
|
|
mwg = MatlabWrapperGenerator()
|
2013-08-07 14:34:30 +08:00
|
|
|
mwg.gen(args.moduleroot, args.modules, args.extra, args.outdir)
|