diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e83e5bf8d..cbe77bbc47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ========================== diff --git a/modules/matlab/CMakeLists.txt b/modules/matlab/CMakeLists.txt index 5790aed442..38eed60a39 100644 --- a/modules/matlab/CMakeLists.txt +++ b/modules/matlab/CMakeLists.txt @@ -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}) diff --git a/modules/matlab/generator/gen_matlab.py b/modules/matlab/generator/gen_matlab.py index 3e4b60c626..1b2e4d9b36 100644 --- a/modules/matlab/generator/gen_matlab.py +++ b/modules/matlab/generator/gen_matlab.py @@ -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) diff --git a/modules/matlab/generator/templates/template_function_base.cpp b/modules/matlab/generator/templates/template_function_base.cpp index 417b610fc6..7286c8f669 100644 --- a/modules/matlab/generator/templates/template_function_base.cpp +++ b/modules/matlab/generator/templates/template_function_base.cpp @@ -31,14 +31,13 @@ void mexFunction(int nlhs, mxArray* plhs[], {% endblock %} // parse the inputs and outputs - std::vector blhs(plhs, plhs+nlhs); - std::vector 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 %} diff --git a/modules/matlab/include/bridge.hpp b/modules/matlab/include/bridge.hpp index 3df8d121a2..467eb1ff45 100644 --- a/modules/matlab/include/bridge.hpp +++ b/modules/matlab/include/bridge.hpp @@ -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 diff --git a/modules/matlab/test/rand.hpp b/modules/matlab/test/rand.hpp new file mode 100644 index 0000000000..78e3118945 --- /dev/null +++ b/modules/matlab/test/rand.hpp @@ -0,0 +1,15 @@ +/* + * a rather innocuous-looking function which is actually + * part of , 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 diff --git a/modules/matlab/test/test_compiler.cpp b/modules/matlab/test/test_compiler.cpp deleted file mode 100644 index 367b97cc70..0000000000 --- a/modules/matlab/test/test_compiler.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include - -void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { - -}