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:
parent
fc16cfcf41
commit
804d0924c9
@ -847,13 +847,13 @@ if(NOT ANDROID)
|
|||||||
endif()
|
endif()
|
||||||
status(" Java tests:" BUILD_TESTS AND (CAN_BUILD_ANDROID_PROJECTS OR HAVE_opencv_java) THEN YES ELSE NO)
|
status(" Java tests:" BUILD_TESTS AND (CAN_BUILD_ANDROID_PROJECTS OR HAVE_opencv_java) THEN YES ELSE NO)
|
||||||
|
|
||||||
# ========================== java ==========================
|
# ========================= matlab =========================
|
||||||
status("")
|
status("")
|
||||||
status(" Matlab:")
|
status(" Matlab:")
|
||||||
if(NOT MATLAB_FOUND)
|
status(" mex:" MATLAB_MEX_SCRIPT THEN "${MATLAB_MEX_SCRIPT}" ELSE NO)
|
||||||
status(" NOT FOUND")
|
if (MATLAB_FOUND)
|
||||||
else()
|
get_property(MEX_WORKS GLOBAL PROPERTY MEX_WORKS)
|
||||||
status(" MEX:" MATLAB_MEX_SCRIPT THEN "${MATLAB_MEX_SCRIPT}" ELSE NO)
|
status(" Compiler/generator:" MEX_WORKS THEN "Working" ELSE "Not working (bindings will not be generated)")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# ========================== documentation ==========================
|
# ========================== documentation ==========================
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
# CMake file for Matlab/Octave support
|
# 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
|
# 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)
|
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_highgui opencv_ml opencv_calib3d opencv_photo
|
||||||
opencv_nonfree opencv_calib)
|
opencv_nonfree opencv_calib)
|
||||||
|
|
||||||
# Add all of the headers we wish to parse
|
# add the python generator to the python path
|
||||||
string(REPLACE "opencv_" "" OPENCV_MATLAB_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};
|
set(PYPATH_CACHE $ENV{PYTHONPATH})
|
||||||
${OPENCV_MODULE_${the_module}_OPT_DEPS}")
|
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})
|
foreach(module ${OPENCV_MATLAB_MODULES})
|
||||||
if (HAVE_opencv_${module})
|
if (HAVE_opencv_${module})
|
||||||
list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_${module}_LOCATION}/include/opencv2/${module}.hpp")
|
list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_${module}_LOCATION}/include/opencv2/${module}.hpp")
|
||||||
endif()
|
endif()
|
||||||
endforeach()
|
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
|
# synthesise the matlab sources
|
||||||
|
# TODO:These should be build-time (ie add_custom_command)
|
||||||
execute_process(
|
execute_process(
|
||||||
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py
|
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py
|
||||||
${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR})
|
${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
@ -18,10 +18,12 @@ class MatlabWrapperGenerator(object):
|
|||||||
# get the file name
|
# get the file name
|
||||||
name = os.path.splitext(os.path.basename(file))[0]
|
name = os.path.splitext(os.path.basename(file))[0]
|
||||||
ns[name] = parser.parse(file)
|
ns[name] = parser.parse(file)
|
||||||
|
print ns[name]
|
||||||
|
|
||||||
# cleanify the parser output
|
# cleanify the parser output
|
||||||
parse_tree = ParseTree()
|
parse_tree = ParseTree()
|
||||||
parse_tree.build(ns)
|
parse_tree.build(ns)
|
||||||
|
print parse_tree
|
||||||
|
|
||||||
# setup the template engine
|
# setup the template engine
|
||||||
jtemplate = Environment(loader=PackageLoader('templates', ''), trim_blocks=True, lstrip_blocks=True)
|
jtemplate = Environment(loader=PackageLoader('templates', ''), trim_blocks=True, lstrip_blocks=True)
|
||||||
|
@ -31,14 +31,13 @@ void mexFunction(int nlhs, mxArray* plhs[],
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
// parse the inputs and outputs
|
// parse the inputs and outputs
|
||||||
std::vector<Bridge> blhs(plhs, plhs+nlhs);
|
|
||||||
std::vector<Bridge> brhs(prhs, prhs+nrhs);
|
|
||||||
|
|
||||||
{% block postbridge %}
|
{% block postbridge %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
// call the opencv function
|
// call the opencv function
|
||||||
// [out =] namespace.fun(src1, ..., srcn, dst1, ..., dstn, opt1, ..., optn);
|
// [out =] namespace.fun(src1, ..., srcn, dst1, ..., dstn, opt1, ..., optn);
|
||||||
|
{{fun.name}}();
|
||||||
{% block fcall %}
|
{% block fcall %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@ -3,13 +3,4 @@
|
|||||||
|
|
||||||
#include "mex.h"
|
#include "mex.h"
|
||||||
|
|
||||||
namespace mex {
|
|
||||||
class Bridge {
|
|
||||||
private:
|
|
||||||
mxArray* m_;
|
|
||||||
public:
|
|
||||||
bool valid() { return_ m_ != 0; } const
|
|
||||||
mxArray* toMxArray() { return m_; } const
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
15
modules/matlab/test/rand.hpp
Normal file
15
modules/matlab/test/rand.hpp
Normal 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
|
@ -1,5 +0,0 @@
|
|||||||
#include <mex.h>
|
|
||||||
|
|
||||||
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user