BASIS  r3148
FindMATLAB.cmake
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  FindMATLAB.cmake
00003 # @brief Find MATLAB installation.
00004 #
00005 # @par Input variables:
00006 # <table border="0">
00007 #   <tr>
00008 #     @tp @b MATLAB_DIR @endtp
00009 #     <td>The installation directory of MATLAB.
00010 #         Can also be set as environment variable.</td>
00011 #   </tr>
00012 #   <tr>
00013 #     @tp @b MATLABDIR @endtp
00014 #     <td>Alternative environment variable for @p MATLAB_DIR.</td>
00015 #   </tr>
00016 #   <tr>
00017 #     @tp @b MATLAB_FIND_COMPONENTS @endtp
00018 #     <td>The @c COMPONENTS argument(s) of the find_package() command can
00019 #         be used to only look for specific MATLAB executables and libraries.
00020 #         Valid component values are "matlab", "mcc", "mexext", "mex",
00021 #         "libmex", "mx" or "libmx", and "eng" or "libeng".</td>
00022 #   </tr>
00023 #   <tr>
00024 #     @tp @b MATLAB_FIND_OPTIONAL_COMPONENTS @endtp
00025 #     <td>The @c OPTIONAL_COMPONENTS argument(s) of the find_package() command.
00026 #         See @c MATLAB_FIND_COMPONENTS.</td>
00027 #   </tr>
00028 #   <tr>
00029 #     @tp @b MATLAB_PATH_SUFFIXES @endtp
00030 #     <td>Path suffixes which are used to find the proper MATLAB libraries.
00031 #         By default, this find module tries to determine the path suffix
00032 #         from the CMake variables which describe the system. For example,
00033 #         on 64-bit Unix-based systems, the libraries are searched in
00034 #         @p MATLAB_DIR/bin/glnxa64. Set this variable before the
00035 #         find_package() command if this find module fails to
00036 #         determine the correct location of the MATLAB libraries within
00037 #         the root directory.</td>
00038 #   </tr>
00039 # </table>
00040 #
00041 # @par Output variables:
00042 # <table border="0">
00043 #   <tr>
00044 #     @tp @b MATLAB_FOUND @endtp
00045 #     <td>Whether the package was found and the following CMake
00046 #         variables are valid.</td>
00047 #   </tr>
00048 #   <tr>
00049 #     @tp @b MATLAB_EXECUTABLE @endtp
00050 #     <td>The absolute path of the found matlab executable.</td>
00051 #   </tr>
00052 #   <tr>
00053 #     @tp @b MATLAB_VERSION_STRING @endtp
00054 #     <td>Version of the found matlab executable (e.g., 7.14.0).</td>
00055 #   </tr>
00056 #   <tr>
00057 #     @tp @b MATLAB_VERSION_MAJOR @endtp
00058 #     <td>Major version of the found matlab executable (e.g., 7).</td>
00059 #   </tr>
00060 #   <tr>
00061 #     @tp @b MATLAB_VERSION_MINOR @endtp
00062 #     <td>Minor version of the found matlab executable (e.g., 14).</td>
00063 #   </tr>
00064 #   <tr>
00065 #     @tp @b MATLAB_VERSION_PATCH @endtp
00066 #     <td>Patch of the found matlab executable (e.g., 0).</td>
00067 #   </tr>
00068 #   <tr>
00069 #     @tp @b MATLAB_RELEASE @endtp
00070 #     <td>Release version of the found matlab executable (e.g., R2012a).</td>
00071 #   </tr>
00072 #   <tr>
00073 #     @tp @b MATLAB_MCC_EXECUTABLE @endtp
00074 #     <td>The absolute path of the found MATLAB Compiler (mcc) executable.</td>
00075 #   </tr>
00076 #   <tr>
00077 #     @tp @b MATLAB_MEX_EXECUTABLE @endtp
00078 #     <td>The absolute path of the found MEX script (mex) executable.</td>
00079 #   </tr>
00080 #   <tr>
00081 #     @tp @b MATLAB_MEXEXT_EXECUTABLE @endtp
00082 #     <td>The absolute path of the found mexext script executable.</td>
00083 #   </tr>
00084 #   <tr>
00085 #     @tp @b MATLAB_INCLUDE_DIR @endtp
00086 #     <td>Package include directories.</td>
00087 #   </tr>
00088 #   <tr>
00089 #     @tp @b MATLAB_INCLUDES @endtp
00090 #     <td>Include directories including prerequisite libraries.</td>
00091 #   </tr>
00092 #   <tr>
00093 #     @tp @b MATLAB_mex_LIBRARY @endtp
00094 #     <td>The MEX library of MATLAB.</td>
00095 #   </tr>
00096 #   <tr>
00097 #     @tp @b MATLAB_mx_LIBRARY @endtp
00098 #     <td>The @c mx library of MATLAB.</td>
00099 #   </tr>
00100 #   <tr>
00101 #     @tp @b MATLAB_eng_LIBRARY @endtp
00102 #     <td>The MATLAB engine library.</td>
00103 #   </tr>
00104 #   <tr>
00105 #     @tp @b MATLAB_LIBRARY @endtp
00106 #     <td>All MATLAB libraries.</td>
00107 #   </tr>
00108 #   <tr>
00109 #     @tp @b MATLAB_LIBRARIES @endtp
00110 #     <td>Package libraries and prerequisite libraries.</td>
00111 #   </tr>
00112 # </table>
00113 #
00114 # Copyright (c) 2011, 2012, 2013 University of Pennsylvania. All rights reserved.<br />
00115 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00116 #
00117 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00118 #
00119 # @ingroup CMakeFindModules
00120 ##############################################################################
00121 
00122 # ----------------------------------------------------------------------------
00123 # initialize search
00124 if (NOT MATLAB_DIR)
00125   if (NOT $ENV{MATLABDIR} STREQUAL "")
00126     set (MATLAB_DIR "$ENV{MATLABDIR}"  CACHE PATH "Installation prefix for MATLAB." FORCE)
00127   else ()
00128     set (MATLAB_DIR "$ENV{MATLAB_DIR}" CACHE PATH "Installation prefix for MATLAB." FORCE)
00129   endif ()
00130 endif ()
00131 
00132 if (NOT MATLAB_PATH_SUFFIXES)
00133   if (WIN32)
00134     if (CMAKE_GENERATOR MATCHES "Visual Studio 6")
00135       set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc60")
00136     elseif (CMAKE_GENERATOR MATCHES "Visual Studio 7")
00137       # assume people are generally using 7.1,
00138       # if using 7.0 need to link to: extern/lib/win32/microsoft/msvc70
00139       set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc71")
00140     elseif (CMAKE_GENERATOR MATCHES "Visual Studio 8")
00141       set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc80")
00142     elseif (CMAKE_GENERATOR MATCHES "Visual Studio 9")
00143       set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/msvc90")
00144     elseif (CMAKE_GENERATOR MATCHES "Borland")
00145       # assume people are generally using 5.4
00146       # if using 5.0 need to link to: ../extern/lib/win32/microsoft/bcc50
00147       # if using 5.1 need to link to: ../extern/lib/win32/microsoft/bcc51
00148       set (MATLAB_PATH_SUFFIXES "extern/lib/win32/microsoft/bcc54")
00149     endif ()
00150   elseif (APPLE)
00151     if (CMAKE_SIZE_OF_VOID_P EQUAL 4)
00152       set (MATLAB_PATH_SUFFIXES "bin/maci")
00153     else ()
00154       set (MATLAB_PATH_SUFFIXES "bin/maci64")
00155     endif ()
00156   else ()
00157     if (CMAKE_SIZE_OF_VOID_P EQUAL 4)
00158       set (MATLAB_PATH_SUFFIXES "bin/glnx86")
00159     else ()
00160       set (MATLAB_PATH_SUFFIXES "bin/glnxa64")
00161     endif ()
00162   endif ()
00163 endif ()
00164 
00165 set (_MATLAB_EXECUTABLE_NAMES)
00166 set (_MATLAB_LIBRARY_NAMES)
00167 set (_MATLAB_OPTIONAL_EXECUTABLE_NAMES)
00168 set (_MATLAB_OPTIONAL_LIBRARY_NAMES)
00169 
00170 if (MATLAB_FIND_COMPONENTS OR MATLAB_FIND_OPTIONAL_COMPONENTS)
00171   foreach (_MATLAB_COMPONENT IN LISTS MATLAB_FIND_COMPONENTS)
00172     string (TOLOWER "${_MATLAB_COMPONENT}" _MATLAB_COMPONENT)
00173     if (_MATLAB_COMPONENT MATCHES "^(matlab|mcc|mexext|mex)$")
00174       list (APPEND _MATLAB_EXECUTABLE_NAMES ${_MATLAB_COMPONENT})
00175     elseif (_MATLAB_COMPONENT MATCHES "^(lib)?(mex|mx|eng)$")
00176       list (APPEND _MATLAB_LIBRARY_NAMES ${CMAKE_MATCH_2})
00177     else ()
00178       message (FATAL_ERROR "Unknown MATLAB component: ${_MATLAB_COMPONENT}")
00179     endif ()
00180   endforeach ()
00181   foreach (_MATLAB_COMPONENT IN LISTS MATLAB_FIND_OPTIONAL_COMPONENTS)
00182     string (TOLOWER "${_MATLAB_COMPONENT}" _MATLAB_COMPONENT)
00183     if (_MATLAB_COMPONENT MATCHES "^(matlab|mcc|mexext|mex)$")
00184       list (APPEND _MATLAB_OPTIONAL_EXECUTABLE_NAMES ${_MATLAB_COMPONENT})
00185     elseif (_MATLAB_COMPONENT MATCHES "^(lib)?(mex|mx|eng)$")
00186       list (APPEND _MATLAB_OPTIONAL_LIBRARY_NAMES ${CMAKE_MATCH_2})
00187     else ()
00188       message (FATAL_ERROR "Unknown MATLAB component: ${_MATLAB_COMPONENT}")
00189     endif ()
00190   endforeach ()
00191 else ()
00192   set (_MATLAB_EXECUTABLE_NAMES          matlab)
00193   set (_MATLAB_OPTIONAL_EXECUTABLE_NAMES mcc mex mexext)
00194   set (_MATLAB_LIBRARY_NAMES             mex mx eng)
00195   set (_MATLAB_OPTIONAL_LIBRARY_NAMES)
00196 endif ()
00197 
00198 # ----------------------------------------------------------------------------
00199 # find MATLAB executables
00200 if (_MATLAB_EXECUTABLE_NAMES OR _MATLAB_OPTIONAL_EXECUTABLE_NAMES)
00201   if (MATLAB_DIR)
00202 
00203     foreach (_MATLAB_EXE IN LISTS _MATLAB_EXECUTABLE_NAMES _MATLAB_OPTIONAL_EXECUTABLE_NAMES)
00204       if (_MATLAB_EXE MATCHES "matlab")
00205         find_program (
00206           MATLAB_EXECUTABLE
00207             NAMES matlab
00208             HINTS "${MATLAB_DIR}/bin"
00209             DOC   "The MATLAB application (matlab)."
00210         )
00211         mark_as_advanced (MATLAB_EXECUTABLE)
00212       else ()
00213         string (TOUPPER "${_MATLAB_EXE}" _MATLAB_EXE_U)
00214         if (WIN32 AND _MATLAB_EXE MATCHES "mex")
00215           list (APPEND _MATLAB_EXE "${_MATLAB_EXE}.bat")
00216         endif ()
00217         find_program (
00218           MATLAB_${_MATLAB_EXE_U}_EXECUTABLE
00219             NAMES ${_MATLAB_EXE}
00220             HINTS "${MATLAB_DIR}/bin"
00221             DOC   "The MATLAB application ${_MATLAB_EXE}."
00222         )
00223         mark_as_advanced (MATLAB_${_MATLAB_EXE_U}_EXECUTABLE)
00224       endif ()
00225     endforeach ()
00226 
00227   else ()
00228 
00229     foreach (_MATLAB_EXE IN LISTS _MATLAB_EXECUTABLE_NAMES _MATLAB_OPTIONAL_EXECUTABLE_NAMES)
00230       if (_MATLAB_EXE MATCHES "matlab")
00231         find_program (
00232           MATLAB_EXECUTABLE
00233             NAMES matlab
00234             DOC   "The MATLAB application (matlab)."
00235         )
00236         mark_as_advanced (MATLAB_EXECUTABLE)
00237       else ()
00238         string (TOUPPER "${_MATLAB_EXE}" _MATLAB_EXE_U)
00239         find_program (
00240           MATLAB_${_MATLAB_EXE_U}_EXECUTABLE
00241             NAMES "${_MATLAB_EXE}"
00242             DOC   "The MATLAB application ${_MATLAB_EXE}."
00243         )
00244         mark_as_advanced (MATLAB_${_MATLAB_EXE_U}_EXECUTABLE)
00245       endif ()
00246     endforeach ()
00247 
00248   endif ()
00249 endif ()
00250 
00251 # ----------------------------------------------------------------------------
00252 # set MATLAB_DIR
00253 if (NOT MATLAB_DIR AND MATLAB_EXECUTABLE)
00254   string (REGEX REPLACE "/bin(/[a-z0-9]+)?/(matlab|MATLAB)(\\.exe|\\.EXE)?$|/[^/]+\\.app/.*$" "" _MATLAB_PREFIX "${MATLAB_EXECUTABLE}")
00255   if (APPLE)
00256     string (REGEX REPLACE "^(.+\\.app)/.*$" "\\1" _MATLAB_PREFIX "${MATLAB_EXECUTABLE}")
00257   endif ()
00258   set (MATLAB_DIR "${_MATLAB_PREFIX}" CACHE PATH "Installation prefix for MATLAB." FORCE)
00259 endif ()
00260 
00261 # ----------------------------------------------------------------------------
00262 # determine MATLAB version
00263 if (COMMAND basis_get_matlab_version)
00264   basis_get_matlab_version ()
00265 endif ()
00266 
00267 # ----------------------------------------------------------------------------
00268 # find paths/files
00269 if (_MATLAB_LIBRARY_NAMES OR _MATLAB_OPTIONAL_LIBRARY_NAMES)
00270   if (MATLAB_DIR)
00271 
00272     find_path (
00273       MATLAB_INCLUDE_DIR
00274         NAMES mex.h
00275         HINTS "${MATLAB_DIR}/extern/include"
00276         DOC   "Include directory for MATLAB libraries."
00277         NO_DEFAULT_PATH
00278     )
00279 
00280     foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
00281       find_library (
00282         MATLAB_${_MATLAB_LIB}_LIBRARY
00283           NAMES         "${_MATLAB_LIB}" "lib${_MATLAB_LIB}"
00284           HINTS         "${MATLAB_DIR}"
00285           PATH_SUFFIXES ${MATLAB_PATH_SUFFIXES}
00286           DOC           "MATLAB ${_MATLAB_LIB} link library."
00287           NO_DEFAULT_PATH
00288       )
00289     endforeach ()
00290 
00291   else ()
00292 
00293     find_path (
00294       MATLAB_INCLUDE_DIR
00295         NAMES mex.h
00296         HINTS ENV C_INCLUDE_PATH ENV CXX_INCLUDE_PATH
00297         DOC   "Include directory for MATLAB libraries."
00298     )
00299 
00300     foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
00301       find_library (
00302         MATLAB_${_MATLAB_LIB}_LIBRARY
00303           NAMES "${_MATLAB_LIB}"
00304           HINTS ENV LD_LIBRARY_PATH
00305           DOC   "MATLAB ${_MATLAB_LIB} link library."
00306       )
00307     endforeach ()
00308 
00309   endif ()
00310   # mark variables as advanced
00311   mark_as_advanced (MATLAB_INCLUDE_DIR)
00312   foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
00313     mark_as_advanced (MATLAB_${_MATLAB_LIB}_LIBRARY)
00314   endforeach ()
00315   # list of all libraries
00316   set (MATLAB_LIBRARY)
00317   foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES _MATLAB_OPTIONAL_LIBRARY_NAMES)
00318     if (MATLAB_${_MATLAB_LIB}_LIBRARY)
00319       list (APPEND MATLAB_LIBRARY "${MATLAB_${_MATLAB_LIB}_LIBRARY}")
00320     endif ()
00321   endforeach ()
00322   # prerequisite libraries
00323   set (MATLAB_INCLUDES  "${MATLAB_INCLUDE_DIR}")
00324   set (MATLAB_LIBRARIES "${MATLAB_LIBRARY}")
00325   # aliases / backwards compatibility
00326   set (MATLAB_INCLUDE_DIRS "${MATLAB_INCLUDES}")
00327 
00328 endif ()
00329 
00330 # ----------------------------------------------------------------------------
00331 # handle the QUIETLY and REQUIRED arguments and set *_FOUND to TRUE
00332 # if all listed variables are found or TRUE
00333 include (FindPackageHandleStandardArgs)
00334 
00335 set (_MATLAB_REQUIRED_VARS)
00336 
00337 foreach (_MATLAB_EXE IN LISTS _MATLAB_EXECUTABLE_NAMES)
00338   if (_MATLAB_EXE MATCHES "matlab")
00339     list (APPEND _MATLAB_REQUIRED_VARS MATLAB_EXECUTABLE)
00340   else ()
00341     string (TOUPPER "${_MATLAB_EXE}" _MATLAB_EXECUTABLE)
00342     list (APPEND _MATLAB_REQUIRED_VARS MATLAB_${_MATLAB_EXECUTABLE}_EXECUTABLE)
00343   endif ()
00344 endforeach ()
00345 
00346 if (_MATLAB_LIBRARY_NAMES)
00347   list (APPEND _MATLAB_REQUIRED_VARS MATLAB_INCLUDE_DIR)
00348   foreach (_MATLAB_LIB IN LISTS _MATLAB_LIBRARY_NAMES)
00349     list (APPEND _MATLAB_REQUIRED_VARS MATLAB_${_MATLAB_LIB}_LIBRARY)
00350   endforeach ()
00351 endif ()
00352 
00353 if (_MATLAB_REQUIRED_VARS)
00354   find_package_handle_standard_args (
00355     MATLAB
00356   # MESSAGE
00357       DEFAULT_MSG
00358   # VARIABLES
00359       ${_MATLAB_REQUIRED_VARS}
00360   )
00361 else ()
00362   set (MATLAB_FOUND TRUE)
00363 endif ()
00364 
00365 # ----------------------------------------------------------------------------
00366 # set MATLAB_DIR
00367 if (NOT MATLAB_DIR AND MATLAB_INCLUDE_DIR)
00368   string (REGEX REPLACE "/extern/include/?" "" _MATLAB_PREFIX "${MATLAB_INCLUDE_DIR}")
00369   set (MATLAB_DIR "${_MATLAB_PREFIX}" CACHE PATH "Installation prefix for MATLAB." FORCE)
00370 endif ()
00371 
00372 # ----------------------------------------------------------------------------
00373 # unset private variables
00374 unset (_MATLAB_REQUIRED_VARS)
00375 unset (_MATLAB_EXECUTABLE_NAMES)
00376 unset (_MATLAB_LIBRARY_NAMES)
00377 unset (_MATLAB_PREFIX)
00378 unset (_MATLAB_LIB)
00379 unset (_MATLAB_EXE)