Added custom OpenCV mex compiler

This commit is contained in:
hbristow
2013-08-28 12:01:34 +10:00
parent ef2c1e1a24
commit 52dc51a62c
9 changed files with 396 additions and 21 deletions

View File

@@ -19,7 +19,7 @@ def substitute(build, output_dir):
os.mkdir(output_dir)
# populate template
populated = template.render(build=build)
populated = template.render(build=build, time=time)
with open(os.path.join(output_dir, 'buildInformation.m'), 'wb') as f:
f.write(populated)
@@ -46,7 +46,7 @@ if __name__ == "__main__":
"""
# parse the input options
import sys, re, os
import sys, re, os, time
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--os')

View File

@@ -0,0 +1,58 @@
#!/usr/bin/env python
def substitute(cv, output_dir):
# setup the template engine
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jtemplate = Environment(loader=FileSystemLoader(template_dir), trim_blocks=True, lstrip_blocks=True)
# add the filters
jtemplate.filters['cellarray'] = cellarray
jtemplate.filters['split'] = split
jtemplate.filters['csv'] = csv
# load the template
template = jtemplate.get_template('template_cvmex_base.m')
# create the build directory
output_dir = output_dir+'/+cv'
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
# populate template
populated = template.render(cv=cv, time=time)
with open(os.path.join(output_dir, 'mex.m'), 'wb') as f:
f.write(populated)
if __name__ == "__main__":
"""
Usage: python cvmex.py --opts [-list -of -opts]
--include_dirs [-list -of -opencv_include_directories]
--lib_dir opencv_lib_directory
--libs [-lopencv_core -lopencv_imgproc ...]
--flags [-Wall -opencv_build_flags ...]
--outdir /path/to/generated/output
cvmex.py generates a custom mex compiler that automatically links OpenCV
libraries to built sources where appropriate. The calling syntax is the
same as the builtin mex compiler, with added cv qualification:
>> cv.mex(..., ...);
"""
# parse the input options
import sys, re, os, time
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--opts')
parser.add_argument('--include_dirs')
parser.add_argument('--lib_dir')
parser.add_argument('--libs')
parser.add_argument('--flags')
parser.add_argument('--outdir')
cv = parser.parse_args()
from filters import *
from jinja2 import Environment, FileSystemLoader
# populate the mex base template
substitute(cv, cv.outdir)

View File

@@ -139,10 +139,18 @@ def filename(fullpath):
'''
return os.path.splitext(os.path.basename(fullpath))[0]
def split(text, delimiter=' '):
'''Split a text string into a list using the specified delimiter'''
return text.split(delimiter)
def csv(items, sep=', '):
'''format a list with a separator (comma if not specified)'''
return sep.join(item for item in items)
def cellarray(items, escape='\''):
'''format a list of items as a matlab cell array'''
return '{' + ', '.join(escape+item+escape for item in items) + '}'
def stripExtraSpaces(text):
'''Removes superfluous whitespace from a string, including the removal
of all leading and trailing whitespace'''

View File

@@ -6,6 +6,8 @@ function buildInformation()
% run into issues with the Toolbox, it is useful to submit this
% information alongside a bug report to the OpenCV team.
%
% Copyright {{ time.strftime("%Y", time.localtime()) }} The OpenCV Foundation
%
info = {
' ------------------------------------------------------------------------'
' <strong>OpenCV Toolbox</strong>'

View File

@@ -0,0 +1,46 @@
function mex(varargin)
%CV.MEX compile MEX-function with OpenCV linkages
%
% Usage:
% CV.MEX [options ...] file [file file ...]
%
% Description:
% CV.MEX compiles one or more C/C++ source files into a shared-library
% called a mex-file. This function is equivalent to the builtin MEX
% routine, with the notable exception that it automatically resolves
% OpenCV includes, and links in the OpenCV libraries where appropriate.
% It also forwards the flags used to build OpenCV, so architecture-
% specific optimizations can be used.
%
% CV.MEX is designed to be used in situations where the source(s) you
% are compiling contain OpenCV definitions. In such cases, it streamlines
% the finding and including of appropriate OpenCV libraries.
%
% See also: mex
%
% Copyright {{ time.strftime("%Y", time.localtime()) }} The OpenCV Foundation
%
% forward the OpenCV build flags (C++ only)
EXTRA_FLAGS = ['"CXXFLAGS="\$CXXFLAGS '...
'{{ cv.flags | trim | wordwrap(60, false, '\'...\n \'') }}""'];
% add the OpenCV include dirs
INCLUDE_DIRS = {{ cv.include_dirs | split | cellarray | wordwrap(60, false, '...\n ') }};
% add the lib dir (singular in both build tree and install tree)
LIB_DIR = '{{ cv.lib_dir }}';
% add the OpenCV libs. Only the used libs will actually be linked
LIBS = {{ cv.libs | split | cellarray | wordwrap(60, false, '...\n ') }};
% add the mex opts (usually at least -largeArrayDims)
OPTS = {{ cv.opts | split | cellarray | wordwrap(60, false, '...\n ') }};
% merge all of the default options (EXTRA_FLAGS, LIBS, etc) and the options
% and files passed by the user (varargin) into a single cell array
merged = [ {EXTRA_FLAGS}, INCLUDE_DIRS, {LIB_DIR}, LIBS, OPTS, varargin ];
% expand the merged argument list into the builtin mex utility
mex(merged{:});
end