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)
32 foreach(VAR ${IN} ${ARGN})
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)
108 prepend("-L" MEX_LIB_DIR ${LIBRARY_OUTPUT_PATH}/${CMAKE_CFG_INTDIR})
110 prepend("-L" MEX_LIB_DIR ${LIBRARY_OUTPUT_PATH})
112 set(MEX_OPTS "-largeArrayDims")
115 add_subdirectory(test)
117 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
120 # intersection of available modules and optional dependencies
121 # 1. populate the command-line include directories (-I/path/to/module/header, ...)
122 # 2. populate the command-line link libraries (-lopencv_core, ...) for Debug and Release
123 set(MATLAB_DEPS ${OPENCV_MODULE_${the_module}_REQ_DEPS} ${OPENCV_MODULE_${the_module}_OPT_DEPS})
124 foreach(opencv_module ${MATLAB_DEPS})
125 if (HAVE_${opencv_module})
126 string(REPLACE "opencv_" "" module ${opencv_module})
127 list(APPEND opencv_modules ${module})
128 list(APPEND ${the_module}_ACTUAL_DEPS ${opencv_module})
129 prepend("-I" MEX_INCLUDE_DIRS "${OPENCV_MODULE_${opencv_module}_LOCATION}/include")
130 prepend("-l" MEX_LIBS ${opencv_module}${OPENCV_DLLVERSION})
131 prepend("-l" MEX_DEBUG_LIBS ${opencv_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX})
135 # add extra headers by hand
136 list(APPEND opencv_extra_hdrs "core=${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/base.hpp")
137 list(APPEND opencv_extra_hdrs "video=${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp")
139 # pass the OPENCV_CXX_EXTRA_FLAGS through to the mex compiler
140 # remove the visibility modifiers, so the mex gateway is visible
141 # TODO: get mex working without warnings
142 string(REGEX REPLACE "[^\ ]*visibility[^\ ]*" "" MEX_CXXFLAGS "${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
145 # Check to see whether the generator and the mex compiler are working.
146 # The checks currently test:
147 # - whether the python generator can be found
148 # - whether the python generator correctly outputs a file for a definition
149 # - whether the mex compiler can find the required headers
150 # - whether the mex compiler can compile a trivial definition
152 # attempt to generate a gateway for a function
153 message(STATUS "Trying to generate Matlab code")
155 COMMAND ${PYTHON_EXECUTABLE}
156 ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab.py
157 --jinja2 ${JINJA2_PATH}
158 --hdrparser ${HDR_PARSER_PATH}
159 --rstparser ${RST_PARSER_PATH}
160 --extra "test=${CMAKE_CURRENT_SOURCE_DIR}/test/test_generator.hpp"
161 --outdir ${CMAKE_BINARY_DIR}/junk
162 ERROR_VARIABLE GEN_ERROR
167 message(${GEN_ERROR})
168 message(STATUS "Error generating Matlab code. Disabling Matlab bindings...")
169 ocv_module_disable(matlab)
172 message(STATUS "Trying to generate Matlab code - OK")
175 # attempt to compile a gateway using mex
176 message(STATUS "Trying to compile mex file")
178 COMMAND ${MATLAB_MEX_SCRIPT} ${MEX_OPTS} "CXXFLAGS=\$CXXFLAGS ${MEX_CXX_FLAGS}"
179 ${MEX_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/test/test_compiler.cpp
180 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/junk
181 ERROR_VARIABLE MEX_ERROR
186 message(${MEX_ERROR})
187 message(STATUS "Error compiling mex file. Disabling Matlab bindings...")
188 ocv_module_disable(matlab)
191 message(STATUS "Trying to compile mex file - OK")
195 # if we make it here, mex works!
196 set(MEX_WORKS True CACHE BOOL ADVANCED)
199 # ----------------------------------------------------------------------------
200 # Build time components
201 # ----------------------------------------------------------------------------
204 # these proxies are used to trigger the add_custom_commands
205 # (which do the real work) only when they're outdated
206 set(GENERATE_PROXY ${CMAKE_CURRENT_BINARY_DIR}/generate.proxy)
207 set(COMPILE_PROXY ${CMAKE_CURRENT_BINARY_DIR}/compile.proxy)
208 # TODO: Remove following line before merging with master
209 file(REMOVE ${GENERATE_PROXY} ${COMPILE_PROXY})
212 # call the python executable to generate the Matlab gateways
214 OUTPUT ${GENERATE_PROXY}
215 COMMAND ${PYTHON_EXECUTABLE}
216 ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab.py
217 --jinja2 ${JINJA2_PATH}
218 --hdrparser ${HDR_PARSER_PATH}
219 --rstparser ${RST_PARSER_PATH}
220 --moduleroot ${CMAKE_SOURCE_DIR}/modules
221 --modules ${opencv_modules}
222 --extra ${opencv_extra_hdrs}
223 --outdir ${CMAKE_CURRENT_BINARY_DIR}
224 COMMAND ${PYTHON_EXECUTABLE}
225 ${CMAKE_CURRENT_SOURCE_DIR}/generator/build_info.py
226 --jinja2 ${JINJA2_PATH}
228 --arch ${ARCH} ${CMAKE_SYSTEM_PROCESSOR}
229 --compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}
230 --mex_arch ${MATLAB_ARCH}
231 --mex_script ${MATLAB_MEX_SCRIPT}
232 --cxx_flags ${MEX_CXXFLAGS}
233 --opencv_version ${OPENCV_VERSION}
234 --commit ${GIT_COMMIT}
235 --modules ${opencv_modules}
236 --configuration $<CONFIGURATION>
237 --outdir ${CMAKE_CURRENT_BINARY_DIR}
238 COMMAND ${PYTHON_EXECUTABLE}
239 ${CMAKE_CURRENT_SOURCE_DIR}/generator/cvmex.py
240 --jinja2 ${JINJA2_PATH}
242 --include_dirs="${MEX_INCLUDE_DIRS}"
243 --lib_dir="${MEX_LIB_DIR}"
245 --flags ${MEX_CXXFLAGS}
246 --outdir ${CMAKE_CURRENT_BINARY_DIR}
247 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/test/help.m ${CMAKE_CURRENT_BINARY_DIR}/+cv
248 COMMAND ${CMAKE_COMMAND} -E touch ${GENERATE_PROXY}
249 COMMENT "Generating Matlab source files"
253 # call the mex compiler to compile the gateways
254 # because we don't know the source files at configure-time, this
255 # has to be executed in a separate script in cmake's script processing mode
257 OUTPUT ${COMPILE_PROXY}
258 COMMAND ${CMAKE_COMMAND} -DMATLAB_MEX_SCRIPT=${MATLAB_MEX_SCRIPT}
259 -DMATLAB_MEXEXT=${MATLAB_MEXEXT}
260 -DMEX_OPTS=${MEX_OPTS}
261 -DMEX_CXXFLAGS=${MEX_CXX_FLAGS}
262 -DMEX_INCLUDE_DIRS="${MEX_INCLUDE_DIRS}"
263 -DMEX_LIB_DIR="${MEX_LIB_DIR}"
264 -DCONFIGURATION="$<CONFIGURATION>"
265 -DMEX_LIBS="${MEX_LIBS}"
266 -DMEX_DEBUG_LIBS="${MEX_DEBUG_LIBS}"
267 -P ${CMAKE_CURRENT_SOURCE_DIR}/compile.cmake
268 COMMAND ${CMAKE_COMMAND} -E touch ${COMPILE_PROXY}
269 COMMENT "Compiling Matlab source files. This could take a while..."
273 # opencv_matlab_sources --> opencv_matlab
274 add_custom_target(${the_module}_sources ALL DEPENDS ${GENERATE_PROXY})
275 add_custom_target(${the_module} ALL DEPENDS ${COMPILE_PROXY})
276 add_dependencies(${the_module} ${the_module}_sources ${${the_module}_ACTUAL_DEPS})
278 if (ENABLE_SOLUTION_FOLDERS)
279 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
283 # ----------------------------------------------------------------------------
284 # Install time components
285 # ----------------------------------------------------------------------------
286 # NOTE: Trailing slashes on the DIRECTORY paths are important!
287 # TODO: What needs to be done with rpath????
289 # install the +cv directory verbatim
290 install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${OPENCV_INCLUDE_INSTALL_PATH})
291 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/+cv/ DESTINATION matlab/+cv)
292 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cv.m DESTINATION matlab)
294 # update the custom mex compiler to point to the install locations
295 string(REPLACE ";" "\\ " MEX_OPTS "${MEX_OPTS}")
296 string(REPLACE ";" "\\ " MEX_LIBS "${MEX_LIBS}")
297 string(REPLACE " " "\\ " MEX_CXXFLAGS ${MEX_CXXFLAGS})
298 string(REPLACE ";" "\\ " MEX_INCLUDE_DIRS "${MEX_INCLUDE_DIRS}")
301 COMMAND ${PYTHON_EXECUTABLE}
302 ${CMAKE_CURRENT_SOURCE_DIR}/generator/cvmex.py
303 --jinja2 ${JINJA2_PATH}
305 --include_dirs=-I${CMAKE_INSTALL_PREFIX}/${OPENCV_INCLUDE_INSTALL_PATH}
306 --lib_dir=-L${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}
308 --flags=${MEX_CXXFLAGS}
309 --outdir ${CMAKE_INSTALL_PREFIX}/matlab