FindMATLAB.cmake
Go to the documentation of this file.
1 ##############################################################################
2 # @file FindMATLAB.cmake
3 # @brief Find MATLAB installation.
4 #
5 # @par Input variables:
6 # <table border="0">
7 # <tr>
8 # @tp @b MATLAB_DIR @endtp
9 # <td>The installation directory of MATLAB.
10 # Can also be set as environment variable.</td>
11 # </tr>
12 # <tr>
13 # @tp @b MATLABDIR @endtp
14 # <td>Alternative environment variable for @p MATLAB_DIR.</td>
15 # </tr>
16 # <tr>
17 # @tp @b MATLAB_FIND_COMPONENTS @endtp
18 # <td>The @c COMPONENTS argument(s) of the find_package() command can
19 # be used to only look for specific MATLAB executables and libraries.
20 # Valid component values are "matlab", "mcc", "mexext", "mex",
21 # "libmex", "mx" or "libmx", "eng" or "libeng",
22 # "libmwmclmcr" or "mwmclmcr", and "libmwmclmcrrt" or "mwmclmcrrt".</td>
23 # </tr>
24 # <tr>
25 # @tp @b MATLAB_FIND_OPTIONAL_COMPONENTS @endtp
26 # <td>The @c OPTIONAL_COMPONENTS argument(s) of the find_package() command.
27 # See @c MATLAB_FIND_COMPONENTS.</td>
28 # </tr>
29 # <tr>
30 # @tp @b MATLAB_PATH_SUFFIXES @endtp
31 # <td>Path suffixes which are used to find the proper MATLAB libraries.
32 # By default, this find module tries to determine the path suffix
33 # from the CMake variables which describe the system. For example,
34 # on 64-bit Unix-based systems, the libraries are searched in
35 # @p MATLAB_DIR/bin/glnxa64. Set this variable before the
36 # find_package() command if this find module fails to
37 # determine the correct location of the MATLAB libraries within
38 # the root directory.</td>
39 # </tr>
40 # </table>
41 #
42 # @par Output variables:
43 # <table border="0">
44 # <tr>
45 # @tp @b MATLAB_FOUND @endtp
46 # <td>Whether the package was found and the following CMake
47 # variables are valid.</td>
48 # </tr>
49 # <tr>
50 # @tp @b MATLAB_EXECUTABLE @endtp
51 # <td>The absolute path of the found matlab executable.</td>
52 # </tr>
53 # <tr>
54 # @tp @b MATLAB_VERSION_STRING @endtp
55 # <td>Version of the found matlab executable (e.g., 7.14.0).</td>
56 # </tr>
57 # <tr>
58 # @tp @b MATLAB_VERSION_MAJOR @endtp
59 # <td>Major version of the found matlab executable (e.g., 7).</td>
60 # </tr>
61 # <tr>
62 # @tp @b MATLAB_VERSION_MINOR @endtp
63 # <td>Minor version of the found matlab executable (e.g., 14).</td>
64 # </tr>
65 # <tr>
66 # @tp @b MATLAB_VERSION_PATCH @endtp
67 # <td>Patch of the found matlab executable (e.g., 0).</td>
68 # </tr>
69 # <tr>
70 # @tp @b MATLAB_RELEASE @endtp
71 # <td>Release version of the found matlab executable (e.g., R2012a).</td>
72 # </tr>
73 # <tr>
74 # @tp @b MATLAB_MCC_EXECUTABLE @endtp
75 # <td>The absolute path of the found MATLAB Compiler (mcc) executable.</td>
76 # </tr>
77 # <tr>
78 # @tp @b MATLAB_MEX_EXECUTABLE @endtp
79 # <td>The absolute path of the found MEX script (mex) executable.</td>
80 # </tr>
81 # <tr>
82 # @tp @b MATLAB_MEXEXT_EXECUTABLE @endtp
83 # <td>The absolute path of the found mexext script executable.</td>
84 # </tr>
85 # <tr>
86 # @tp @b MATLAB_INCLUDE_DIR @endtp
87 # <td>Package include directories.</td>
88 # </tr>
89 # <tr>
90 # @tp @b MATLAB_INCLUDES @endtp
91 # <td>Include directories including prerequisite libraries.</td>
92 # </tr>
93 # <tr>
94 # @tp @b MATLAB_LIBRARY_DIR @endtp
95 # <td>Directory containing the MATLAB libraries.</td>
96 # </tr>
97 # <tr>
98 # @tp @b MATLAB_mex_LIBRARY @endtp
99 # <td>The MEX library of MATLAB.</td>
100 # </tr>
101 # <tr>
102 # @tp @b MATLAB_mx_LIBRARY @endtp
103 # <td>The @c mx library of MATLAB.</td>
104 # </tr>
105 # <tr>
106 # @tp @b MATLAB_eng_LIBRARY @endtp
107 # <td>The MATLAB engine library.</td>
108 # </tr>
109 # <tr>
110 # @tp @b MATLAB_mwmclmcr_LIBRARY @endtp
111 # <td>The MATLAB Compiler library.</td>
112 # </tr>
113 # <tr>
114 # @tp @b MATLAB_mwmclmcrrt_LIBRARY @endtp
115 # <td>The MATLAB Compiler runtime library.</td>
116 # </tr>
117 # <tr>
118 # @tp @b MATLAB_LIBRARY @endtp
119 # <td>All MATLAB libraries excluding @c mwmclmcrrt.</td>
120 # </tr>
121 # <tr>
122 # @tp @b MATLAB_LIBRARIES @endtp
123 # <td>Package libraries and prerequisite libraries.</td>
124 # </tr>
125 # </table>
126 #
127 # @ingroup CMakeFindModules
128 ##############################################################################
129 
130 #=============================================================================
131 # Copyright 2011-2012 University of Pennsylvania
132 # Copyright 2013-2016 Andreas Schuh <andreas.schuh.84@gmail.com>
133 #
134 # Distributed under the OSI-approved BSD License (the "License");
135 # see accompanying file Copyright.txt for details.
136 #
137 # This software is distributed WITHOUT ANY WARRANTY; without even the
138 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
139 # See the License for more information.
140 #=============================================================================
141 # (To distribute this file outside of CMake, substitute the full
142 # License text for the above reference.)
143 
144 # ----------------------------------------------------------------------------
145 # initialize search
146 if (NOT MATLAB_DIR)
147  if (NOT $ENV{MATLABDIR} STREQUAL "")
148  set (MATLAB_DIR "$ENV{MATLABDIR}" CACHE PATH "Installation prefix for MATLAB." FORCE)
149  else ()
150  set (MATLAB_DIR "$ENV{MATLAB_DIR}" CACHE PATH "Installation prefix for MATLAB." FORCE)
151  endif ()
152 endif ()
153 
154 if (NOT MATLAB_PATH_SUFFIXES)
155  if (WIN32)
156  if (CMAKE_GENERATOR MATCHES "Visual Studio 6")
157  set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc60")
158  elseif (CMAKE_GENERATOR MATCHES "Visual Studio 7")
159  # assume people are generally using 7.1,
160  # if using 7.0 need to link to: extern/lib/win32/microsoft/msvc70
161  set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc71")
162  elseif (CMAKE_GENERATOR MATCHES "Visual Studio 8")
163  set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc80")
164  elseif (CMAKE_GENERATOR MATCHES "Visual Studio 9")
165  set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc90")
166  elseif (CMAKE_GENERATOR MATCHES "Borland")
167  # assume people are generally using 5.4
168  # if using 5.0 need to link to: ../extern/lib/win32/microsoft/bcc50
169  # if using 5.1 need to link to: ../extern/lib/win32/microsoft/bcc51
170  set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/bcc54")
171  endif ()
172  elseif (APPLE)
173  if (CMAKE_SIZE_OF_VOID_P EQUAL 4)
174  set (MATLAB_PATH_SUFFIXES "bin/maci" "runtime/maci")
175  else ()
176  set (MATLAB_PATH_SUFFIXES "bin/maci64" "runtime/maci64")
177  endif ()
178  else ()
179  if (CMAKE_SIZE_OF_VOID_P EQUAL 4)
180  set (MATLAB_PATH_SUFFIXES "bin/glnx86" "runtime/glnx86")
181  else ()
182  set (MATLAB_PATH_SUFFIXES "bin/glnxa64" "runtime/glnxa64")
183  endif ()
184  endif ()
185 endif ()
186 
191 
192 if (MATLAB_FIND_COMPONENTS OR MATLAB_FIND_OPTIONAL_COMPONENTS)
193  foreach (_MATLAB_COMPONENT IN LISTS MATLAB_FIND_COMPONENTS)
194  string (TOLOWER "${_MATLAB_COMPONENT}" _MATLAB_COMPONENT)
195  if (_MATLAB_COMPONENT MATCHES "^(matlab|mcc|mexext|mex)$")
196  list (APPEND _MATLAB_EXECUTABLE_NAMES ${_MATLAB_COMPONENT})
197  elseif (_MATLAB_COMPONENT MATCHES "^(lib)?(mex|mx|eng|mwmclmcr|mwmclmcrrt)$")
198  list (APPEND _MATLAB_LIBRARY_NAMES ${CMAKE_MATCH_2})
199  else ()
200  message (FATAL_ERROR "Unknown MATLAB component: ${_MATLAB_COMPONENT}")
201  endif ()
202  endforeach ()
203  foreach (_MATLAB_COMPONENT IN LISTS MATLAB_FIND_OPTIONAL_COMPONENTS)
204  string (TOLOWER "${_MATLAB_COMPONENT}" _MATLAB_COMPONENT)
205  if (_MATLAB_COMPONENT MATCHES "^(matlab|mcc|mexext|mex)$")
206  list (APPEND _MATLAB_OPTIONAL_EXECUTABLE_NAMES ${_MATLAB_COMPONENT})
207  elseif (_MATLAB_COMPONENT MATCHES "^(lib)?(mex|mx|eng|mwmclmcrrt)$")
208  list (APPEND _MATLAB_OPTIONAL_LIBRARY_NAMES ${CMAKE_MATCH_2})
209  else ()
210  message (FATAL_ERROR "Unknown MATLAB component: ${_MATLAB_COMPONENT}")
211  endif ()
212  endforeach ()
213 else ()
214  set (_MATLAB_EXECUTABLE_NAMES matlab)
215  set (_MATLAB_OPTIONAL_EXECUTABLE_NAMES mcc mex mexext)
216  set (_MATLAB_LIBRARY_NAMES mwmclmcrrt)
217  set (_MATLAB_OPTIONAL_LIBRARY_NAMES mex mx eng)
218 endif ()
219 
220 # ----------------------------------------------------------------------------
221 # find MATLAB executables
222 if (_MATLAB_EXECUTABLE_NAMES OR _MATLAB_OPTIONAL_EXECUTABLE_NAMES)
223  if (MATLAB_DIR)
224 
225  foreach (_MATLAB_EXE IN LISTS _MATLAB_EXECUTABLE_NAMES _MATLAB_OPTIONAL_EXECUTABLE_NAMES)
226  if (_MATLAB_EXE MATCHES "matlab")
227  find_program (
228  MATLAB_EXECUTABLE
229  NAMES matlab
230  HINTS "${MATLAB_DIR}/bin"
231  DOC "The MATLAB application (matlab)."
232  )
233  mark_as_advanced (MATLAB_EXECUTABLE)
234  else ()
235  string (TOUPPER "${_MATLAB_EXE}" _MATLAB_EXE_U)
236  if (WIN32 AND _MATLAB_EXE MATCHES "mex")
237  list (APPEND _MATLAB_EXE "${_MATLAB_EXE}.bat")
238  endif ()
239  find_program (
240  MATLAB_${_MATLAB_EXE_U}_EXECUTABLE
241  NAMES ${_MATLAB_EXE}
242  HINTS "${MATLAB_DIR}/bin"
243  DOC "The MATLAB application ${_MATLAB_EXE}."
244  )
245  mark_as_advanced (MATLAB_${_MATLAB_EXE_U}_EXECUTABLE)
246  endif ()
247  endforeach ()
248 
249  else ()
250 
251  foreach (_MATLAB_EXE IN LISTS _MATLAB_EXECUTABLE_NAMES _MATLAB_OPTIONAL_EXECUTABLE_NAMES)
252  if (_MATLAB_EXE MATCHES "matlab")
253  find_program (
254  MATLAB_EXECUTABLE
255  NAMES matlab
256  DOC "The MATLAB application (matlab)."
257  )
258  mark_as_advanced (MATLAB_EXECUTABLE)
259  else ()
260  string (TOUPPER "${_MATLAB_EXE}" _MATLAB_EXE_U)
261  find_program (
262  MATLAB_${_MATLAB_EXE_U}_EXECUTABLE
263  NAMES "${_MATLAB_EXE}"
264  DOC "The MATLAB application ${_MATLAB_EXE}."
265  )
266  mark_as_advanced (MATLAB_${_MATLAB_EXE_U}_EXECUTABLE)
267  endif ()
268  endforeach ()
269 
270  endif ()
271 endif ()
272 
273 # ----------------------------------------------------------------------------
274 # set MATLAB_DIR
275 if (NOT MATLAB_DIR AND MATLAB_EXECUTABLE)
276  string (REGEX REPLACE "/bin(/[a-z0-9]+)?/(matlab|MATLAB)(\\.exe|\\.EXE)?$|/[^/]+\\.app/.*$" "" _MATLAB_PREFIX "${MATLAB_EXECUTABLE}")
277  if (APPLE)
278  string (REGEX REPLACE "^(.+\\.app)/.*$" "\\1" _MATLAB_PREFIX "${MATLAB_EXECUTABLE}")
279  endif ()
280  set (MATLAB_DIR "${_MATLAB_PREFIX}" CACHE PATH "Installation prefix for MATLAB." FORCE)
281 endif ()
282 
283 # ----------------------------------------------------------------------------
284 # determine MATLAB version
285 if (COMMAND basis_get_matlab_version)
286  basis_get_matlab_version ()
287 endif ()
288 
289 # ----------------------------------------------------------------------------
290 # find paths/files
291 if (_MATLAB_LIBRARY_NAMES OR _MATLAB_OPTIONAL_LIBRARY_NAMES)
292  if (MATLAB_DIR)
293 
294  find_path (
295  MATLAB_INCLUDE_DIR
296  NAMES mex.h
297  HINTS "${MATLAB_DIR}/extern/include"
298  DOC "Include directory for MATLAB libraries."
299  NO_DEFAULT_PATH
300  )
301 
302  foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
303  find_library (
304  MATLAB_${_MATLAB_LIB}_LIBRARY
305  NAMES "${_MATLAB_LIB}" "lib${_MATLAB_LIB}"
306  HINTS "${MATLAB_DIR}"
307  PATH_SUFFIXES ${MATLAB_PATH_SUFFIXES}
308  DOC "MATLAB ${_MATLAB_LIB} link library."
309  NO_DEFAULT_PATH
310  )
311  endforeach ()
312 
313  else ()
314 
315  find_path (
316  MATLAB_INCLUDE_DIR
317  NAMES mex.h
318  HINTS ENV C_INCLUDE_PATH ENV CXX_INCLUDE_PATH
319  DOC "Include directory for MATLAB libraries."
320  )
321 
322  foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
323  find_library (
324  MATLAB_${_MATLAB_LIB}_LIBRARY
325  NAMES "${_MATLAB_LIB}"
326  HINTS ENV LD_LIBRARY_PATH
327  DOC "MATLAB ${_MATLAB_LIB} link library."
328  )
329  endforeach ()
330 
331  endif ()
332  # mark variables as advanced
333  mark_as_advanced (MATLAB_INCLUDE_DIR)
334  foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
335  mark_as_advanced (MATLAB_${_MATLAB_LIB}_LIBRARY)
336  endforeach ()
337  # list of all libraries
338  set (MATLAB_LIBRARY)
339  foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
340  if (MATLAB_${_MATLAB_LIB}_LIBRARY)
341  list (APPEND MATLAB_LIBRARY "${MATLAB_${_MATLAB_LIB}_LIBRARY}")
342  endif ()
343  endforeach ()
344  # prerequisite libraries
345  set (MATLAB_INCLUDES "${MATLAB_INCLUDE_DIR}")
346  set (MATLAB_LIBRARIES "${MATLAB_LIBRARY}")
347  # aliases / backwards compatibility
348  set (MATLAB_INCLUDE_DIRS "${MATLAB_INCLUDES}")
349 
350 endif ()
351 
352 # ----------------------------------------------------------------------------
353 # set MATLAB_DIR
354 if (NOT MATLAB_DIR AND MATLAB_INCLUDE_DIR)
355  string (REGEX REPLACE "/extern/include/?" "" _MATLAB_PREFIX "${MATLAB_INCLUDE_DIR}")
356  set (MATLAB_DIR "${_MATLAB_PREFIX}" CACHE PATH "Installation prefix for MATLAB." FORCE)
357 endif ()
358 
359 # ----------------------------------------------------------------------------
360 # set MATLAB_LIBRARY_DIR
361 set (MATLAB_LIBRARY_DIR)
362 foreach (_MATLAB_LIB IN LISTS MATLAB_LIBRARY)
363  get_filename_component (MATLAB_LIBRARY_DIR "${_MATLAB_LIB}" PATH)
364  if (MATLAB_LIBRARY_DIR)
365  break ()
366  endif ()
367 endforeach ()
368 
369 # ----------------------------------------------------------------------------
370 # handle the QUIETLY and REQUIRED arguments and set *_FOUND to TRUE
371 # if all listed variables are found or TRUE
372 include (FindPackageHandleStandardArgs)
373 
374 set (_MATLAB_REQUIRED_VARS)
375 
376 foreach (_MATLAB_EXE IN LISTS _MATLAB_EXECUTABLE_NAMES)
377  if (_MATLAB_EXE MATCHES "matlab")
378  list (APPEND _MATLAB_REQUIRED_VARS MATLAB_EXECUTABLE)
379  else ()
380  string (TOUPPER "${_MATLAB_EXE}" _MATLAB_EXECUTABLE)
381  list (APPEND _MATLAB_REQUIRED_VARS MATLAB_${_MATLAB_EXECUTABLE}_EXECUTABLE)
382  endif ()
383 endforeach ()
384 
385 if (_MATLAB_LIBRARY_NAMES)
386  list (APPEND _MATLAB_REQUIRED_VARS MATLAB_INCLUDE_DIR)
387  foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES)
388  list (APPEND _MATLAB_REQUIRED_VARS MATLAB_${_MATLAB_LIB}_LIBRARY)
389  endforeach ()
390 endif ()
391 
392 if (_MATLAB_REQUIRED_VARS)
393  find_package_handle_standard_args (
394  MATLAB
395  # MESSAGE
396  DEFAULT_MSG
397  # VARIABLES
398  MATLAB_DIR # for status message "Found MATLAB: ..."
399  ${_MATLAB_REQUIRED_VARS}
400  )
401 else ()
402  set (MATLAB_FOUND TRUE)
403 endif ()
404 
405 # ----------------------------------------------------------------------------
406 # unset private variables
407 unset (_MATLAB_REQUIRED_VARS)
408 unset (_MATLAB_EXECUTABLE_NAMES)
409 unset (_MATLAB_LIBRARY_NAMES)
410 unset (_MATLAB_PREFIX)
411 unset (_MATLAB_LIB)
412 unset (_MATLAB_EXE)
cmake MATLAB_DIR
cmake _MATLAB_OPTIONAL_EXECUTABLE_NAMES
cmake MATLAB_PATH_SUFFIXES
cmake _MATLAB_EXECUTABLE_NAMES
cmake _MATLAB_OPTIONAL_LIBRARY_NAMES
cmake _MATLAB_LIBRARY_NAMES
if(oldcoutbuf)
cmake MATLAB_INCLUDES