OpenCVFindMatlab.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # ----- Find Matlab/Octave -----
  2. #
  3. # OpenCVFindMatlab.cmake attempts to locate the install path of Matlab in order
  4. # to extract the mex headers, libraries and shell scripts. If found
  5. # successfully, the following variables will be defined
  6. #
  7. # MATLAB_FOUND: true/false
  8. # MATLAB_ROOT_DIR: Root of Matlab installation
  9. # MATLAB_BIN: The main Matlab "executable" (shell script)
  10. # MATLAB_MEX_SCRIPT: The mex script used to compile mex files
  11. # MATLAB_INCLUDE_DIRS:Path to "mex.h"
  12. # MATLAB_LIBRARY_DIRS:Path to mex and matrix libraries
  13. # MATLAB_LIBRARIES: The Matlab libs, usually mx, mex, mat
  14. # MATLAB_MEXEXT: The mex library extension. It will be one of:
  15. # mexwin32, mexwin64, mexglx, mexa64, mexmac,
  16. # mexmaci, mexmaci64, mexsol, mexs64
  17. # MATLAB_ARCH: The installation architecture. It is **usually**
  18. # the MEXEXT with the preceding "mex" removed,
  19. # though it's different for linux distros.
  20. #
  21. # There doesn't appear to be an elegant way to detect all versions of Matlab
  22. # across different platforms. If you know the matlab path and want to avoid
  23. # the search, you can define the path to the Matlab root when invoking cmake:
  24. #
  25. # cmake -DMATLAB_ROOT_DIR='/PATH/TO/ROOT_DIR' ..
  26. # ----- set_library_presuffix -----
  27. #
  28. # Matlab tends to use some non-standard prefixes and suffixes on its libraries.
  29. # For example, libmx.dll on Windows (Windows does not add prefixes) and
  30. # mkl.dylib on OS X (OS X uses "lib" prefixes).
  31. # On some versions of Windows the .dll suffix also appears to not be checked.
  32. #
  33. # This function modifies the library prefixes and suffixes used by
  34. # find_library when finding Matlab libraries. It does not affect scopes
  35. # outside of this file.
  36. function(set_libarch_prefix_suffix)
  37. if (UNIX AND NOT APPLE)
  38. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" PARENT_SCOPE)
  39. set(CMAKE_FIND_LIBRARY_SUFFIXES ".so" ".a" PARENT_SCOPE)
  40. elseif (APPLE)
  41. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" PARENT_SCOPE)
  42. set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".a" PARENT_SCOPE)
  43. elseif (WIN32)
  44. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" PARENT_SCOPE)
  45. set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll" PARENT_SCOPE)
  46. endif()
  47. endfunction()
  48. # ----- locate_matlab_root -----
  49. #
  50. # Attempt to find the path to the Matlab installation. If successful, sets
  51. # the absolute path in the variable MATLAB_ROOT_DIR
  52. function(locate_matlab_root)
  53. # --- UNIX/APPLE ---
  54. if (UNIX)
  55. # possible root locations, in order of likelihood
  56. set(SEARCH_DIRS_ /Applications /usr/local /opt/local /usr /opt)
  57. foreach (DIR_ ${SEARCH_DIRS_})
  58. file(GLOB MATLAB_ROOT_DIR_ ${DIR_}/MATLAB/R* ${DIR_}/MATLAB_R*)
  59. if (MATLAB_ROOT_DIR_)
  60. # sort in order from highest to lowest
  61. # normally it's in the format MATLAB_R[20XX][A/B]
  62. # TODO: numerical rather than lexicographic sort. However,
  63. # CMake does not support floating-point MATH(EXPR ...) at this time.
  64. list(SORT MATLAB_ROOT_DIR_)
  65. list(REVERSE MATLAB_ROOT_DIR_)
  66. list(GET MATLAB_ROOT_DIR_ 0 MATLAB_ROOT_DIR_)
  67. set(MATLAB_ROOT_DIR ${MATLAB_ROOT_DIR_} PARENT_SCOPE)
  68. return()
  69. endif()
  70. endforeach()
  71. # --- WINDOWS ---
  72. elseif (WIN32)
  73. # 1. search the path environment variable
  74. find_program(MATLAB_ROOT_DIR_ matlab PATHS ENV PATH)
  75. if (MATLAB_ROOT_DIR_)
  76. # get the root directory from the full path
  77. # /path/to/matlab/rootdir/bin/matlab.exe
  78. get_filename_component(MATLAB_ROOT_DIR_ ${MATLAB_ROOT_DIR_} PATH)
  79. get_filename_component(MATLAB_ROOT_DIR_ ${MATLAB_ROOT_DIR_} PATH)
  80. set(MATLAB_ROOT_DIR ${MATLAB_ROOT_DIR_} PARENT_SCOPE)
  81. return()
  82. endif()
  83. # 2. search the registry
  84. # determine the available Matlab versions
  85. set(REG_EXTENSION_ "SOFTWARE\\Mathworks\\MATLAB")
  86. set(REG_ROOTS_ "HKEY_LOCAL_MACHINE" "HKEY_CURRENT_USER")
  87. foreach(REG_ROOT_ ${REG_ROOTS_})
  88. execute_process(COMMAND reg query "${REG_ROOT_}\\${REG_EXTENSION_}" OUTPUT_VARIABLE QUERY_RESPONSE_ ERROR_VARIABLE UNUSED_)
  89. if (QUERY_RESPONSE_)
  90. string(REGEX MATCHALL "[0-9]\\.[0-9]" VERSION_STRINGS_ ${QUERY_RESPONSE_})
  91. list(APPEND VERSIONS_ ${VERSION_STRINGS_})
  92. endif()
  93. endforeach()
  94. # select the highest version
  95. list(APPEND VERSIONS_ "0.0")
  96. list(SORT VERSIONS_)
  97. list(REVERSE VERSIONS_)
  98. list(GET VERSIONS_ 0 VERSION_)
  99. # request the MATLABROOT from the registry
  100. foreach(REG_ROOT_ ${REG_ROOTS_})
  101. get_filename_component(QUERY_RESPONSE_ [${REG_ROOT_}\\${REG_EXTENSION_}\\${VERSION_};MATLABROOT] ABSOLUTE)
  102. if (NOT ${QUERY_RESPONSE_} MATCHES "registry$")
  103. set(MATLAB_ROOT_DIR ${QUERY_RESPONSE_} PARENT_SCOPE)
  104. return()
  105. endif()
  106. endforeach()
  107. endif()
  108. endfunction()
  109. # ----- locate_matlab_components -----
  110. #
  111. # Given a directory MATLAB_ROOT_DIR, attempt to find the Matlab components
  112. # (include directory and libraries) under the root. If everything is found,
  113. # sets the variable MATLAB_FOUND to TRUE
  114. function(locate_matlab_components MATLAB_ROOT_DIR)
  115. # get the mex extension
  116. find_file(MATLAB_MEXEXT_SCRIPT_ NAMES mexext mexext.bat PATHS ${MATLAB_ROOT_DIR}/bin NO_DEFAULT_PATH)
  117. execute_process(COMMAND ${MATLAB_MEXEXT_SCRIPT_}
  118. OUTPUT_VARIABLE MATLAB_MEXEXT_
  119. OUTPUT_STRIP_TRAILING_WHITESPACE)
  120. if (NOT MATLAB_MEXEXT_)
  121. return()
  122. endif()
  123. # map the mexext to an architecture extension
  124. set(ARCHITECTURES_ "maci64" "maci" "glnxa64" "glnx64" "sol64" "sola64" "win32" "win64" )
  125. foreach(ARCHITECTURE_ ${ARCHITECTURES_})
  126. if(EXISTS ${MATLAB_ROOT_DIR}/bin/${ARCHITECTURE_})
  127. set(MATLAB_ARCH_ ${ARCHITECTURE_})
  128. break()
  129. endif()
  130. endforeach()
  131. # get the path to the libraries
  132. set(MATLAB_LIBRARY_DIRS_ ${MATLAB_ROOT_DIR}/bin/${MATLAB_ARCH_})
  133. # get the libraries
  134. set_libarch_prefix_suffix()
  135. find_library(MATLAB_LIB_MX_ mx PATHS ${MATLAB_LIBRARY_DIRS_} NO_DEFAULT_PATH)
  136. find_library(MATLAB_LIB_MEX_ mex PATHS ${MATLAB_LIBRARY_DIRS_} NO_DEFAULT_PATH)
  137. find_library(MATLAB_LIB_MAT_ mat PATHS ${MATLAB_LIBRARY_DIRS_} NO_DEFAULT_PATH)
  138. set(MATLAB_LIBRARIES_ ${MATLAB_LIB_MX_} ${MATLAB_LIB_MEX_} ${MATLAB_LIB_MAT_})
  139. # get the include path
  140. find_path(MATLAB_INCLUDE_DIRS_ mex.h ${MATLAB_ROOT_DIR}/extern/include)
  141. # get the mex shell script
  142. find_program(MATLAB_MEX_SCRIPT_ NAMES mex mex.bat PATHS ${MATLAB_ROOT_DIR}/bin NO_DEFAULT_PATH)
  143. # get the Matlab executable
  144. find_program(MATLAB_BIN_ NAMES matlab PATHS ${MATLAB_ROOT_DIR}/bin NO_DEFAULT_PATH)
  145. # export into parent scope
  146. if (MATLAB_MEX_SCRIPT_ AND MATLAB_LIBRARIES_ AND MATLAB_INCLUDE_DIRS_)
  147. set(MATLAB_BIN ${MATLAB_BIN_} PARENT_SCOPE)
  148. set(MATLAB_MEX_SCRIPT ${MATLAB_MEX_SCRIPT_} PARENT_SCOPE)
  149. set(MATLAB_INCLUDE_DIRS ${MATLAB_INCLUDE_DIRS_} PARENT_SCOPE)
  150. set(MATLAB_LIBRARIES ${MATLAB_LIBRARIES_} PARENT_SCOPE)
  151. set(MATLAB_LIBRARY_DIRS ${MATLAB_LIBRARY_DIRS_} PARENT_SCOPE)
  152. set(MATLAB_MEXEXT ${MATLAB_MEXEXT_} PARENT_SCOPE)
  153. set(MATLAB_ARCH ${MATLAB_ARCH_} PARENT_SCOPE)
  154. endif()
  155. endfunction()
  156. # ----------------------------------------------------------------------------
  157. # FIND MATLAB COMPONENTS
  158. # ----------------------------------------------------------------------------
  159. if (NOT MATLAB_FOUND)
  160. # attempt to find the Matlab root folder
  161. if (NOT MATLAB_ROOT_DIR)
  162. locate_matlab_root()
  163. endif()
  164. # given the matlab root folder, find the library locations
  165. if (MATLAB_ROOT_DIR)
  166. locate_matlab_components(${MATLAB_ROOT_DIR})
  167. endif()
  168. find_package_handle_standard_args(Matlab DEFAULT_MSG
  169. MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS
  170. MATLAB_ROOT_DIR MATLAB_LIBRARIES
  171. MATLAB_LIBRARY_DIRS MATLAB_MEXEXT
  172. MATLAB_ARCH MATLAB_BIN)
  173. endif()