1 # ----------------------------------------------------------------------------
2 # CMake file for Matlab/Octave support
4 # Matlab code generation and compilation is broken down into two distinct
5 # stages: configure time and build time. The idea is that we want to give
6 # the user reasonable guarantees that once they type 'make', wrapper
7 # generation is unlikely to fail. Therefore we run a series of tests at
8 # configure time to check the working status of the core components.
11 # During configure time, the script attempts to ascertain whether the
12 # generator and mex compiler are working for a given architecture.
13 # Currently this involves:
14 # 1) Generating a simple CV_EXPORTS_W symbol and checking whether a file
15 # of the symbol name is generated
16 # 2) Compiling a simple mex gateway to check that Bridge.hpp and mex.h
17 # can be found, and that a file with the mexext is produced
20 # If the configure time tests pass, then we assume Matlab wrapper generation
21 # will not fail during build time. We simply glob all of the symbols in
22 # the OpenCV module headers, generate intermediate .cpp files, then compile
24 # ----------------------------------------------------------------------------
27 # Given a list of strings IN and a TOKEN, prepend the token to each string
28 # and append to OUT. This is used for passing command line "-I", "-L" and "-l"
29 # arguments to mex. e.g.
30 # prepend("-I" OUT /path/to/include/dir) --> -I/path/to/include/dir
31 macro(PREPEND TOKEN OUT IN)
33 list(APPEND ${OUT} "${TOKEN}${VAR}")
38 # WARN_MIXED_PRECISION
39 # Formats a warning message if the compiler and Matlab bitness is different
40 macro(WARN_MIXED_PRECISION COMPILER_BITNESS MATLAB_BITNESS)
41 set(MSG "Your compiler is ${COMPILER_BITNESS}-bit")
42 set(MSG "${MSG} but your version of Matlab is ${MATLAB_BITNESS}-bit.")
43 set(MSG "${MSG} To build Matlab bindings, please switch to a ${MATLAB_BITNESS}-bit compiler.")
44 message(WARNING ${MSG})
47 # ----------------------------------------------------------------------------
49 # ----------------------------------------------------------------------------
50 # make sure we're on a supported architecture with Matlab and python installed
51 if (IOS OR ANDROID OR NOT MATLAB_FOUND)
52 ocv_module_disable(matlab)
54 elseif (NOT PYTHONLIBS_FOUND)
55 message(WARNING "A required dependency of the matlab module (PythonLibs) was not found. Disabling Matlab bindings...")
56 ocv_module_disable(matlab)
61 # If the user built OpenCV as X-bit, but they have a Y-bit version of Matlab,
62 # attempting to link to OpenCV during binding generation will fail, since
63 # mixed precision pointers are not allowed. Disable the bindings.
64 math(EXPR ARCH "${CMAKE_SIZEOF_VOID_P} * 8")
65 if (${ARCH} EQUAL 32 AND ${MATLAB_ARCH} MATCHES "64")
66 warn_mixed_precision("32" "64")
67 ocv_module_disable(matlab)
69 elseif (${ARCH} EQUAL 64 AND NOT ${MATLAB_ARCH} MATCHES "64")
70 warn_mixed_precision("64" "32")
71 ocv_module_disable(matlab)
75 # If it's MSVC, warn the user that bindings will only be built in Release mode.
76 # Debug mode seems to cause issues...
78 message(STATUS "Warning: Matlab bindings will only be built in Release configurations")
82 # ----------------------------------------------------------------------------
83 # Configure time components
84 # ----------------------------------------------------------------------------
85 set(the_description "The Matlab/Octave bindings")
86 ocv_add_module(matlab BINDINGS
88 opencv_imgproc opencv_ml opencv_highgui
89 opencv_objdetect opencv_flann opencv_features2d
90 opencv_photo opencv_video opencv_videostab
91 opencv_calib opencv_calib3d
92 opencv_stitching opencv_superres
96 # get the commit information
97 execute_process(COMMAND git log -1 --pretty=%H OUTPUT_VARIABLE GIT_COMMIT ERROR_QUIET)
98 string(REGEX REPLACE "(\r?\n)+$" "" GIT_COMMIT "${GIT_COMMIT}")
100 # set the path to the C++ header and doc parser, and template engine
101 set(JINJA2_PATH ${CMAKE_SOURCE_DIR}/3rdparty)
102 set(HDR_PARSER_PATH ${CMAKE_SOURCE_DIR}/modules/python/src2)
103 set(RST_PARSER_PATH ${CMAKE_SOURCE_DIR}/modules/java/generator)
105 # set mex compiler options
106 prepend("-I" MEX_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include)
107 prepend("-L" MEX_LIB_DIR ${LIBRARY_OUTPUT_PATH}/$<CONFIGURATION>)
108 set(MEX_OPTS "-largeArrayDims")
111 add_subdirectory(test)
113 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
116 # intersection of available modules and optional dependencies
117 # 1. populate the command-line include directories (-I/path/to/module/header, ...)
118 # 2. populate the command-line link libraries (-lopencv_core, ...) for Debug and Release
119 set(MATLAB_DEPS ${OPENCV_MODULE_${the_module}_REQ_DEPS} ${OPENCV_MODULE_${the_module}_OPT_DEPS})
120 foreach(opencv_module ${MATLAB_DEPS})
121 if (HAVE_${opencv_module})
122 string(REPLACE "opencv_" "" module ${opencv_module})
123 list(APPEND opencv_modules ${module})
124 list(APPEND ${the_module}_ACTUAL_DEPS ${opencv_module})
125 prepend("-I" MEX_INCLUDE_DIRS "${OPENCV_MODULE_${opencv_module}_LOCATION}/include")
126 prepend("-l" MEX_LIBS ${opencv_module}${OPENCV_DLLVERSION})
127 prepend("-l" MEX_DEBUG_LIBS ${opencv_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX})
131 # add extra headers by hand
132 list(APPEND opencv_extra_hdrs "core=${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/base.hpp")
133 list(APPEND opencv_extra_hdrs "video=${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp")
135 # pass the OPENCV_CXX_EXTRA_FLAGS through to the mex compiler
136 # remove the visibility modifiers, so the mex gateway is visible
137 # TODO: get mex working without warnings
138 string(REGEX REPLACE "[^\ ]*visibility[^\ ]*" "" MEX_CXXFLAGS "${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
141 # Check to see whether the generator and the mex compiler are working.
142 # The checks currently test:
143 # - whether the python generator can be found
144 # - whether the python generator correctly outputs a file for a definition
145 # - whether the mex compiler can find the required headers
146 # - whether the mex compiler can compile a trivial definition
148 # attempt to generate a gateway for a function
149 message(STATUS "Trying to generate Matlab code")
151 COMMAND ${PYTHON_EXECUTABLE}
152 ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab.py
153 --jinja2 ${JINJA2_PATH}
154 --hdrparser ${HDR_PARSER_PATH}
155 --rstparser ${RST_PARSER_PATH}
156 --extra "test=${CMAKE_CURRENT_SOURCE_DIR}/test/test_generator.hpp"
157 --outdir ${CMAKE_BINARY_DIR}/junk
158 ERROR_VARIABLE GEN_ERROR
163 message(${GEN_ERROR})
164 message(STATUS "Error generating Matlab code. Disabling Matlab bindings...")
165 ocv_module_disable(matlab)
168 message(STATUS "Trying to generate Matlab code - OK")
171 # attempt to compile a gateway using mex
172 message(STATUS "Trying to compile mex file")
174 COMMAND ${MATLAB_MEX_SCRIPT} ${MEX_OPTS} "CXXFLAGS=\$CXXFLAGS ${MEX_CXX_FLAGS}"
175 ${MEX_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_compiler.cpp
176 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/junk
177 ERROR_VARIABLE MEX_ERROR
182 message(${MEX_ERROR})
183 message(STATUS "Error compiling mex file. Disabling Matlab bindings...")
184 ocv_module_disable(matlab)
187 message(STATUS "Trying to compile mex file - OK")
191 # if we make it here, mex works!
192 set(MEX_WORKS True CACHE BOOL ADVANCED)
195 # ----------------------------------------------------------------------------
196 # Build time components
197 # ----------------------------------------------------------------------------
200 # these proxies are used to trigger the add_custom_commands
201 # (which do the real work) only when they're outdated
202 set(GENERATE_PROXY ${CMAKE_CURRENT_BINARY_DIR}/generate.proxy)
203 set(COMPILE_PROXY ${CMAKE_CURRENT_BINARY_DIR}/compile.proxy)
204 # TODO: Remove following line before merging with master
205 file(REMOVE ${GENERATE_PROXY} ${COMPILE_PROXY})
208 # call the python executable to generate the Matlab gateways
210 OUTPUT ${GENERATE_PROXY}
211 COMMAND ${PYTHON_EXECUTABLE}
212 ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab.py
213 --jinja2 ${JINJA2_PATH}
214 --hdrparser ${HDR_PARSER_PATH}
215 --rstparser ${RST_PARSER_PATH}
216 --moduleroot ${CMAKE_SOURCE_DIR}/modules
217 --modules ${opencv_modules}
218 --extra ${opencv_extra_hdrs}
219 --outdir ${CMAKE_CURRENT_BINARY_DIR}
220 COMMAND ${PYTHON_EXECUTABLE}
221 ${CMAKE_CURRENT_SOURCE_DIR}/generator/build_info.py
222 --jinja2 ${JINJA2_PATH}
224 --arch ${ARCH} ${CMAKE_SYSTEM_PROCESSOR}
225 --compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}
226 --mex_arch ${MATLAB_ARCH}
227 --mex_script ${MATLAB_MEX_SCRIPT}
228 --cxx_flags ${MEX_CXXFLAGS}
229 --opencv_version ${OPENCV_VERSION}
230 --commit ${GIT_COMMIT}
231 --modules ${opencv_modules}
232 --configuration $<CONFIGURATION>
233 --outdir ${CMAKE_CURRENT_BINARY_DIR}
234 COMMAND ${PYTHON_EXECUTABLE}
235 ${CMAKE_CURRENT_SOURCE_DIR}/generator/cvmex.py
236 --jinja2 ${JINJA2_PATH}
238 --include_dirs="${MEX_INCLUDE_DIRS}"
239 --lib_dir=${MEX_LIB_DIR}
241 --flags ${MEX_CXXFLAGS}
242 --outdir ${CMAKE_CURRENT_BINARY_DIR}
243 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/test/help.m ${CMAKE_CURRENT_BINARY_DIR}/+cv
244 COMMAND ${CMAKE_COMMAND} -E touch ${GENERATE_PROXY}
245 COMMENT "Generating Matlab source files"
249 # call the mex compiler to compile the gateways
250 # because we don't know the source files at configure-time, this
251 # has to be executed in a separate script in cmake's script processing mode
253 OUTPUT ${COMPILE_PROXY}
254 COMMAND ${CMAKE_COMMAND} -DMATLAB_MEX_SCRIPT=${MATLAB_MEX_SCRIPT}
255 -DMATLAB_MEXEXT=${MATLAB_MEXEXT}
256 -DMEX_OPTS=${MEX_OPTS}
257 -DMEX_CXXFLAGS=${MEX_CXX_FLAGS}
258 -DMEX_INCLUDE_DIRS="${MEX_INCLUDE_DIRS}"
259 -DMEX_LIB_DIR=${MEX_LIB_DIR}
260 -DCONFIGURATION="$<CONFIGURATION>"
261 -DMEX_LIBS="${MEX_LIBS}"
262 -DMEX_DEBUG_LIBS="${MEX_DEBUG_LIBS}"
263 -P ${CMAKE_CURRENT_SOURCE_DIR}/compile.cmake
264 COMMAND ${CMAKE_COMMAND} -E touch ${COMPILE_PROXY}
265 COMMENT "Compiling Matlab source files. This could take a while..."
269 # opencv_matlab_sources --> opencv_matlab
270 add_custom_target(${the_module}_sources ALL DEPENDS ${GENERATE_PROXY})
271 add_custom_target(${the_module} ALL DEPENDS ${COMPILE_PROXY})
272 add_dependencies(${the_module} ${the_module}_sources ${${the_module}_ACTUAL_DEPS})
274 if (ENABLE_SOLUTION_FOLDERS)
275 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
279 # ----------------------------------------------------------------------------
280 # Install time components
281 # ----------------------------------------------------------------------------
282 # NOTE: Trailing slashes on the DIRECTORY paths are important!
283 # TODO: What needs to be done with rpath????
285 # install the +cv directory verbatim
286 install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${OPENCV_INCLUDE_INSTALL_PATH})
287 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/+cv/ DESTINATION matlab/+cv)
288 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cv.m DESTINATION matlab)
290 # update the custom mex compiler to point to the install locations
291 string(REPLACE ";" "\\ " MEX_OPTS "${MEX_OPTS}")
292 string(REPLACE ";" "\\ " MEX_LIBS "${MEX_LIBS}")
293 string(REPLACE " " "\\ " MEX_CXXFLAGS ${MEX_CXXFLAGS})
294 string(REPLACE ";" "\\ " MEX_INCLUDE_DIRS "${MEX_INCLUDE_DIRS}")
297 COMMAND ${PYTHON_EXECUTABLE}
298 ${CMAKE_CURRENT_SOURCE_DIR}/generator/cvmex.py
299 --jinja2 ${JINJA2_PATH}
301 --include_dirs=-I${CMAKE_INSTALL_PREFIX}/${OPENCV_INCLUDE_INSTALL_PATH}
302 --lib_dir=-L${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}
304 --flags=${MEX_CXXFLAGS}
305 --outdir ${CMAKE_INSTALL_PREFIX}/matlab