FindMATLAB.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # - this module looks for Matlab
  2. # Defines:
  3. # MATLAB_INCLUDE_DIR: include path for mex.h, engine.h
  4. # MATLAB_LIBRARIES: required libraries: libmex, etc
  5. # MATLAB_MEX_LIBRARY: path to libmex.lib
  6. # MATLAB_MX_LIBRARY: path to libmx.lib
  7. # MATLAB_MAT_LIBRARY: path to libmat.lib # added
  8. # MATLAB_ENG_LIBRARY: path to libeng.lib
  9. # MATLAB_ROOT: path to Matlab's root directory
  10. # This file is part of Gerardus
  11. #
  12. # This is a derivative work of file FindMatlab.cmake released with
  13. # CMake v2.8, because the original seems to be a bit outdated and
  14. # doesn't work with my Windows XP and Visual Studio 10 install
  15. #
  16. # (Note that the original file does work for Ubuntu Natty)
  17. #
  18. # Author: Ramon Casero <rcasero@gmail.com>, Tom Doel
  19. # Version: 0.2.3
  20. # $Rev$
  21. # $Date$
  22. #
  23. # The original file was copied from an Ubuntu Linux install
  24. # /usr/share/cmake-2.8/Modules/FindMatlab.cmake
  25. #=============================================================================
  26. # Copyright 2005-2009 Kitware, Inc.
  27. #
  28. # Distributed under the OSI-approved BSD License (the "License");
  29. # see accompanying file Copyright.txt for details.
  30. #
  31. # This software is distributed WITHOUT ANY WARRANTY; without even the
  32. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. # See the License for more information.
  34. #=============================================================================
  35. # (To distribute this file outside of CMake, substitute the full
  36. # License text for the above reference.)
  37. SET(MATLAB_FOUND 0)
  38. IF(WIN32)
  39. # Search for a version of Matlab available, starting from the most modern one to older versions
  40. FOREACH(MATVER "7.14" "7.11" "7.17" "7.10" "7.9" "7.8" "7.7" "7.6" "7.5" "7.4" "8.0" "8.1" "8.2" "8.3" "8.4" "8.5")
  41. IF((NOT DEFINED MATLAB_ROOT)
  42. OR ("${MATLAB_ROOT}" STREQUAL "")
  43. OR ("${MATLAB_ROOT}" STREQUAL "/registry"))
  44. GET_FILENAME_COMPONENT(MATLAB_ROOT
  45. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\${MATVER};MATLABROOT]"
  46. ABSOLUTE)
  47. SET(MATLAB_VERSION ${MATVER})
  48. ENDIF((NOT DEFINED MATLAB_ROOT)
  49. OR ("${MATLAB_ROOT}" STREQUAL "")
  50. OR ("${MATLAB_ROOT}" STREQUAL "/registry"))
  51. ENDFOREACH(MATVER)
  52. # Directory name depending on whether the Windows architecture is 32
  53. # bit or 64 bit
  54. set(CMAKE_SIZEOF_VOID_P 8) # Note: For some weird reason this variable is undefined in my system...
  55. IF(CMAKE_SIZEOF_VOID_P MATCHES "4")
  56. SET(WINDIR "win32")
  57. ELSEIF(CMAKE_SIZEOF_VOID_P MATCHES "8")
  58. SET(WINDIR "win64")
  59. ELSE(CMAKE_SIZEOF_VOID_P MATCHES "4")
  60. MESSAGE(FATAL_ERROR
  61. "CMAKE_SIZEOF_VOID_P (${CMAKE_SIZEOF_VOID_P}) doesn't indicate a valid platform")
  62. ENDIF(CMAKE_SIZEOF_VOID_P MATCHES "4")
  63. # Folder where the MEX libraries are, depending of the Windows compiler
  64. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio 6")
  65. SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/msvc60")
  66. ELSEIF(${CMAKE_GENERATOR} MATCHES "Visual Studio 7")
  67. # Assume people are generally using Visual Studio 7.1,
  68. # if using 7.0 need to link to: ../extern/lib/${WINDIR}/microsoft/msvc70
  69. SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/msvc71")
  70. # SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/msvc70")
  71. ELSEIF(${CMAKE_GENERATOR} MATCHES "Borland")
  72. # Assume people are generally using Borland 5.4,
  73. # if using 7.0 need to link to: ../extern/lib/${WINDIR}/microsoft/msvc70
  74. SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/bcc54")
  75. # SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/bcc50")
  76. # SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/bcc51")
  77. ELSEIF(${CMAKE_GENERATOR} MATCHES "Visual Studio*")
  78. # If the compiler is Visual Studio, but not any of the specific
  79. # versions above, we try our luck with the microsoft directory
  80. SET(MATLAB_LIBRARIES_DIR "${MATLAB_ROOT}/extern/lib/${WINDIR}/microsoft/")
  81. ELSE(${CMAKE_GENERATOR} MATCHES "Visual Studio 6")
  82. MESSAGE(FATAL_ERROR "Generator not compatible: ${CMAKE_GENERATOR}")
  83. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio 6")
  84. # Get paths to the Matlab MEX libraries
  85. FIND_LIBRARY(MATLAB_MEX_LIBRARY
  86. libmex
  87. ${MATLAB_LIBRARIES_DIR}
  88. )
  89. FIND_LIBRARY(MATLAB_MX_LIBRARY
  90. libmx
  91. ${MATLAB_LIBRARIES_DIR}
  92. )
  93. FIND_LIBRARY(MATLAB_MAT_LIBRARY
  94. libmat
  95. ${MATLAB_LIBRARIES_DIR}
  96. )
  97. FIND_LIBRARY(MATLAB_ENG_LIBRARY
  98. libeng
  99. ${MATLAB_LIBRARIES_DIR}
  100. )
  101. # Get path to the include directory
  102. FIND_PATH(MATLAB_INCLUDE_DIR
  103. "mex.h"
  104. "${MATLAB_ROOT}/extern/include"
  105. )
  106. ELSE(WIN32)
  107. IF((NOT DEFINED MATLAB_ROOT)
  108. OR ("${MATLAB_ROOT}" STREQUAL ""))
  109. # get path to the Matlab root directory
  110. EXECUTE_PROCESS(
  111. COMMAND which matlab
  112. COMMAND xargs readlink
  113. COMMAND xargs dirname
  114. COMMAND xargs dirname
  115. COMMAND xargs echo -n
  116. OUTPUT_VARIABLE MATLAB_ROOT
  117. )
  118. ENDIF((NOT DEFINED MATLAB_ROOT)
  119. OR ("${MATLAB_ROOT}" STREQUAL ""))
  120. # Check if this is a Mac
  121. IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  122. SET(LIBRARY_EXTENSION .dylib)
  123. # If this is a Mac and the attempts to find MATLAB_ROOT have so far failed,
  124. # we look in the applications folder
  125. IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
  126. # Search for a version of Matlab available, starting from the most modern one to older versions
  127. FOREACH(MATVER "R2015b" "R2015a" "R2014b" "R2014a" "R2014a" "R2013b" "R2013a" "R2012b" "R2012a" "R2011b" "R2011a" "R2010b" "R2010a" "R2009b" "R2009a" "R2008b")
  128. IF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
  129. IF(EXISTS /Applications/MATLAB_${MATVER}.app)
  130. SET(MATLAB_ROOT /Applications/MATLAB_${MATVER}.app)
  131. ENDIF(EXISTS /Applications/MATLAB_${MATVER}.app)
  132. ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
  133. ENDFOREACH(MATVER)
  134. ENDIF((NOT DEFINED MATLAB_ROOT) OR ("${MATLAB_ROOT}" STREQUAL ""))
  135. ELSE(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  136. SET(LIBRARY_EXTENSION .so)
  137. ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  138. # Get path to the MEX libraries
  139. EXECUTE_PROCESS(
  140. #COMMAND find "${MATLAB_ROOT}/extern/lib" -name libmex${LIBRARY_EXTENSION} # Peter
  141. COMMAND find "${MATLAB_ROOT}/bin" -name libmex${LIBRARY_EXTENSION} # Standard
  142. COMMAND xargs echo -n
  143. OUTPUT_VARIABLE MATLAB_MEX_LIBRARY
  144. )
  145. EXECUTE_PROCESS(
  146. #COMMAND find "${MATLAB_ROOT}/extern/lib" -name libmx${LIBRARY_EXTENSION} # Peter
  147. COMMAND find "${MATLAB_ROOT}/bin" -name libmx${LIBRARY_EXTENSION} # Standard
  148. COMMAND xargs echo -n
  149. OUTPUT_VARIABLE MATLAB_MX_LIBRARY
  150. )
  151. EXECUTE_PROCESS(
  152. #COMMAND find "${MATLAB_ROOT}/extern/lib" -name libmat${LIBRARY_EXTENSION} # Peter
  153. COMMAND find "${MATLAB_ROOT}/bin" -name libmat${LIBRARY_EXTENSION} # Standard
  154. COMMAND xargs echo -n
  155. OUTPUT_VARIABLE MATLAB_MAT_LIBRARY
  156. )
  157. EXECUTE_PROCESS(
  158. #COMMAND find "${MATLAB_ROOT}/extern/lib" -name libeng${LIBRARY_EXTENSION} # Peter
  159. COMMAND find "${MATLAB_ROOT}/bin" -name libeng${LIBRARY_EXTENSION} # Standard
  160. COMMAND xargs echo -n
  161. OUTPUT_VARIABLE MATLAB_ENG_LIBRARY
  162. )
  163. # Get path to the include directory
  164. FIND_PATH(MATLAB_INCLUDE_DIR
  165. "mex.h"
  166. PATHS "${MATLAB_ROOT}/extern/include"
  167. )
  168. ENDIF(WIN32)
  169. if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  170. set(MATLAB_LIBSTDCPP "-lstdc++")
  171. endif ()
  172. # This is common to UNIX and Win32:
  173. SET(MATLAB_LIBRARIES
  174. ${MATLAB_LIBSTDCPP}
  175. ${MATLAB_MAT_LIBRARY}
  176. ${MATLAB_MEX_LIBRARY}
  177. ${MATLAB_MX_LIBRARY}
  178. ${MATLAB_ENG_LIBRARY}
  179. )
  180. IF(MATLAB_INCLUDE_DIR AND MATLAB_LIBRARIES)
  181. SET(MATLAB_FOUND 1)
  182. ENDIF(MATLAB_INCLUDE_DIR AND MATLAB_LIBRARIES)
  183. MARK_AS_ADVANCED(
  184. MATLAB_LIBRARIES
  185. MATLAB_MEX_LIBRARY
  186. MATLAB_MX_LIBRARY
  187. MATLAB_ENG_LIBRARY
  188. MATLAB_INCLUDE_DIR
  189. MATLAB_FOUND
  190. MATLAB_ROOT
  191. )