cmake now attempts to generate and compile a simple mex file at configure time and reports the working status of the generator and compiler, rather than just naively saying Matlab exists

This commit is contained in:
hbristow 2013-06-18 23:00:48 +10:00
parent fc16cfcf41
commit 804d0924c9
7 changed files with 85 additions and 28 deletions

View File

@ -847,13 +847,13 @@ if(NOT ANDROID)
endif()
status(" Java tests:" BUILD_TESTS AND (CAN_BUILD_ANDROID_PROJECTS OR HAVE_opencv_java) THEN YES ELSE NO)
# ========================== java ==========================
# ========================= matlab =========================
status("")
status(" Matlab:")
if(NOT MATLAB_FOUND)
status(" NOT FOUND")
else()
status(" MEX:" MATLAB_MEX_SCRIPT THEN "${MATLAB_MEX_SCRIPT}" ELSE NO)
status(" mex:" MATLAB_MEX_SCRIPT THEN "${MATLAB_MEX_SCRIPT}" ELSE NO)
if (MATLAB_FOUND)
get_property(MEX_WORKS GLOBAL PROPERTY MEX_WORKS)
status(" Compiler/generator:" MEX_WORKS THEN "Working" ELSE "Not working (bindings will not be generated)")
endif()
# ========================== documentation ==========================

View File

@ -1,6 +1,12 @@
# ----------------------------------------------------------------------------
# CMake file for Matlab/Octave support
# ----------------------------------------------------------------------------
macro(PREPEND TOKEN OUT IN)
foreach(VAR ${IN})
string(REGEX REPLACE "^/" "${TOKEN}/" TMP ${VAR})
list(APPEND ${OUT} ${TMP})
endforeach()
endmacro()
# make sure we're on a supported architecture with Matlab and python installed
if (IOS OR ANDROID OR NOT MATLAB_FOUND OR NOT PYTHONLIBS_FOUND)
@ -13,20 +19,69 @@ ocv_add_module(matlab BINDINGS opencv_core opencv_imgproc
opencv_highgui opencv_ml opencv_calib3d opencv_photo
opencv_nonfree opencv_calib)
# Add all of the headers we wish to parse
string(REPLACE "opencv_" "" OPENCV_MATLAB_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};
${OPENCV_MODULE_${the_module}_OPT_DEPS}")
# add the python generator to the python path
set(PYPATH_CACHE $ENV{PYTHONPATH})
set(ENV{PYTHONPATH} ${OPENCV_MODULE_opencv_python_LOCATION}/src2:$ENV{PYTHONPATH})
# get the include path of the Bridge
prepend("-I" MEX_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include)
# ----------------------------------------------------------------------------
# Configure time components
# ----------------------------------------------------------------------------
message("-- Trying to generate Matlab code")
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py
${CMAKE_CURRENT_SOURCE_DIR}/test/rand.hpp ${CMAKE_CURRENT_BINARY_DIR}
ERROR_VARIABLE GEN_ERROR
OUTPUT_QUIET
)
if (GEN_ERROR)
message(${GEN_ERROR})
message("-- Generating Matlab code failed. Disabling Matlab bindings...")
# restore the pythonpath
set(ENV{PYTHONPATH} ${PYPATH_CACHE})
return()
else()
message("-- Trying to generate Matlab code - OK")
endif()
# attempt to compile the file using mex
message("-- Trying to compile mex file")
execute_process(
COMMAND ${MATLAB_MEX_SCRIPT} ${MEX_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/src/rand.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/src
ERROR_VARIABLE MEX_ERROR
OUTPUT_QUIET
)
if (MEX_ERROR)
message(${MEX_ERROR})
message("-- Error compiling mex file. Disabling Matlab bindings...")
# restore the pythonpath
set(ENV{PYTHONPATH} ${PYPATH_CACHE})
return()
else()
message("-- Trying to compile mex file - OK")
endif()
# if we make it here, mex works!
set_property(GLOBAL PROPERTY MEX_WORKS TRUE)
# ----------------------------------------------------------------------------
# Build time components
# ----------------------------------------------------------------------------
#string(REPLACE "opencv_" "" OPENCV_MATLAB_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};
# ${OPENCV_MODULE_${the_module}_OPT_DEPS}")
foreach(module ${OPENCV_MATLAB_MODULES})
if (HAVE_opencv_${module})
list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_${module}_LOCATION}/include/opencv2/${module}.hpp")
endif()
endforeach()
# add the python generator to the python path
set(PYPATH_CACHE $ENV{PYTHONPATH})
set(ENV{PYTHONPATH} ${OPENCV_MODULE_opencv_python_LOCATION}/src2:$ENV{PYTHONPATH})
# synthesise the matlab sources
# TODO:These should be build-time (ie add_custom_command)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py
${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR})

View File

@ -18,10 +18,12 @@ class MatlabWrapperGenerator(object):
# get the file name
name = os.path.splitext(os.path.basename(file))[0]
ns[name] = parser.parse(file)
print ns[name]
# cleanify the parser output
parse_tree = ParseTree()
parse_tree.build(ns)
print parse_tree
# setup the template engine
jtemplate = Environment(loader=PackageLoader('templates', ''), trim_blocks=True, lstrip_blocks=True)

View File

@ -31,14 +31,13 @@ void mexFunction(int nlhs, mxArray* plhs[],
{% endblock %}
// parse the inputs and outputs
std::vector<Bridge> blhs(plhs, plhs+nlhs);
std::vector<Bridge> brhs(prhs, prhs+nrhs);
{% block postbridge %}
{% endblock %}
// call the opencv function
// [out =] namespace.fun(src1, ..., srcn, dst1, ..., dstn, opt1, ..., optn);
{{fun.name}}();
{% block fcall %}
{% endblock %}

View File

@ -3,13 +3,4 @@
#include "mex.h"
namespace mex {
class Bridge {
private:
mxArray* m_;
public:
bool valid() { return_ m_ != 0; } const
mxArray* toMxArray() { return m_; } const
};
#endif

View File

@ -0,0 +1,15 @@
/*
* a rather innocuous-looking function which is actually
* part of <cstdlib>, so we can be reasonably sure its
* definition will be found
*/
#ifndef __OPENCV_MATLAB_TEST_COMPILER_HPP_
#define __OPENCV_MATLAB_TEST_COMPILER_HPP_
namespace cv {
CV_EXPORTS_W int rand( );
};
#endif

View File

@ -1,5 +0,0 @@
#include <mex.h>
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
}