Made changes to OpenCVFindMatlab suggested by SpecLad
[profile/ivi/opencv.git] / cmake / OpenCVFindMatlab.cmake
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
27
28
29 # ----- set_library_presuffix -----
30 #
31 # Matlab tends to use some non-standard prefixes and suffixes on its libraries.
32 # For example, libmx.dll on Windows (Windows does not add prefixes) and
33 # mkl.dylib on OS X (OS X uses "lib" prefixes).
34 # On some versions of Windows the .dll suffix also appears to not be checked.
35 #
36 # This function modifies the library prefixes and suffixes used by
37 # find_library when finding Matlab libraries. It does not affect scopes
38 # outside of this file.
39 function(set_libarch_prefix_suffix)
40   if (UNIX AND NOT APPLE)
41     set(CMAKE_FIND_LIBRARY_PREFIXES "lib" PARENT_SCOPE)
42     set(CMAKE_FIND_LIBRARY_SUFFIXES ".so" ".a" PARENT_SCOPE)
43   elseif (APPLE)
44     set(CMAKE_FIND_LIBRARY_PREFIXES "lib" PARENT_SCOPE)
45     set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".a" PARENT_SCOPE)
46   elseif (WIN32)
47     set(CMAKE_FIND_LIBRARY_PREFIXES "lib" PARENT_SCOPE)
48     set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll" PARENT_SCOPE)
49   endif()
50 endfunction()
51
52
53
54 # ----- locate_matlab_root -----
55 #
56 # Attempt to find the path to the Matlab installation. If successful, sets
57 # the absolute path in the variable MATLAB_ROOT_DIR
58 function(locate_matlab_root)
59
60   # --- UNIX/APPLE ---
61   if (UNIX)
62     # possible root locations, in order of likelihood
63     set(SEARCH_DIRS_ /Applications /usr/local /opt/local /usr /opt)
64     foreach (DIR_ ${SEARCH_DIRS_})
65       file(GLOB MATLAB_ROOT_DIR_ ${DIR_}/*matlab*)
66       if (MATLAB_ROOT_DIR_)
67         # sort in order from highest to lowest
68         # normally it's in the format MATLAB_R[20XX][A/B]
69         list(SORT MATLAB_ROOT_DIR_)
70         list(REVERSE MATLAB_ROOT_DIR_)
71         list(GET MATLAB_ROOT_DIR_ 0 MATLAB_ROOT_DIR_)
72         set(MATLAB_ROOT_DIR ${MATLAB_ROOT_DIR_} PARENT_SCOPE)
73         return()
74       endif()
75     endforeach()
76
77   # --- WINDOWS ---
78   elseif (WIN32)
79     # 1. search the path environment variable
80     find_program(MATLAB_ROOT_DIR_ matlab PATHS ENV PATH)
81     if (MATLAB_ROOT_DIR_)
82       # get the root directory from the full path
83       # /path/to/matlab/rootdir/bin/matlab.exe
84       get_filename_component(MATLAB_ROOT_DIR_ ${MATLAB_ROOT_DIR_} PATH)
85       get_filename_component(MATLAB_ROOT_DIR_ ${MATLAB_ROOT_DIR_} PATH)
86       set(MATLAB_ROOT_DIR ${MATLAB_ROOT_DIR_} PARENT_SCOPE)
87       return()
88     endif()
89
90     # 2. search the registry
91     # determine the available Matlab versions
92     set(REG_EXTENSION_ "SOFTWARE\\Mathworks\\MATLAB")
93     set(REG_ROOTS_ "HKEY_LOCAL_MACHINE" "HKEY_CURRENT_USER")
94     foreach(REG_ROOT_ ${REG_ROOTS_})
95       execute_process(COMMAND reg query "${REG_ROOT_}\\${REG_EXTENSION_}" OUTPUT_VARIABLE QUERY_RESPONSE_)
96       if (QUERY_RESPONSE_)
97         string(REGEX MATCHALL "[0-9]\\.[0-9]" VERSION_STRINGS_ ${QUERY_RESPONSE_})
98         list(APPEND VERSIONS_ ${VERSION_STRINGS_})
99       endif()
100     endforeach()
101
102     # select the highest version
103     list(APPEND VERSIONS_ "0.0")
104     list(SORT VERSIONS_)
105     list(REVERSE VERSIONS_)
106     list(GET VERSIONS_ 0 VERSION_)
107
108     # request the MATLABROOT from the registry
109     foreach(REG_ROOT_ ${REG_ROOTS_})
110       get_filename_component(QUERY_RESPONSE_ [${REG_ROOT_}\\${REG_EXTENSION_}\\${VERSION_};MATLABROOT] ABSOLUTE)
111       if (NOT ${QUERY_RESPONSE_} MATCHES "registry$")
112         set(MATLAB_ROOT_DIR ${QUERY_RESPONSE_} PARENT_SCOPE)
113         return()
114       endif()
115     endforeach()
116   endif()
117 endfunction()
118
119
120
121 # ----- locate_matlab_components -----
122 #
123 # Given a directory MATLAB_ROOT_DIR, attempt to find the Matlab components
124 # (include directory and libraries) under the root. If everything is found,
125 # sets the variable MATLAB_FOUND to TRUE
126 function(locate_matlab_components MATLAB_ROOT_DIR)
127   # get the mex extension
128   execute_process(COMMAND ${MATLAB_ROOT_DIR}/bin/mexext OUTPUT_VARIABLE MATLAB_MEXEXT_)
129   if (NOT MATLAB_MEXEXT_)
130     return()
131   endif()
132   string(STRIP ${MATLAB_MEXEXT_} MATLAB_MEXEXT_)
133
134   # map the mexext to an architecture extension
135   set(ARCHITECTURES_ "maci64" "maci" "glnxa64" "glnx64" "sol64" "sola64" "win32" "win64" )
136   foreach(ARCHITECTURE_ ${ARCHITECTURES_})
137     if(EXISTS ${MATLAB_ROOT_DIR}/bin/${ARCHITECTURE_})
138       set(MATLAB_ARCH_ ${ARCHITECTURE_})
139       break()
140     endif()
141   endforeach()
142
143   # get the path to the libraries
144   set(MATLAB_LIBRARY_DIRS_ ${MATLAB_ROOT_DIR}/bin/${MATLAB_ARCH_})
145
146   # get the libraries
147   set_libarch_prefix_suffix()
148   find_library(MATLAB_LIB_MX_  mx  PATHS ${MATLAB_LIBRARY_DIRS_} NO_DEFAULT_PATH)
149   find_library(MATLAB_LIB_MEX_ mex PATHS ${MATLAB_LIBRARY_DIRS_} NO_DEFAULT_PATH)
150   find_library(MATLAB_LIB_MAT_ mat PATHS ${MATLAB_LIBRARY_DIRS_} NO_DEFAULT_PATH)
151   set(MATLAB_LIBRARIES_ ${MATLAB_LIB_MX_} ${MATLAB_LIB_MEX_} ${MATLAB_LIB_MAT_})
152
153   # get the include path
154   find_path(MATLAB_INCLUDE_DIRS_ mex.h ${MATLAB_ROOT_DIR}/extern/include)
155
156   # get the mex shell script
157   find_program(MATLAB_MEX_SCRIPT_ NAMES mex PATHS ${MATLAB_ROOT_DIR}/bin NO_DEFAULT_PATH)
158
159   # get the Matlab executable
160   find_program(MATLAB_BIN_ NAMES matlab PATHS ${MATLAB_ROOT_DIR}/bin NO_DEFAULT_PATH)
161
162   # export into parent scope
163   if (MATLAB_MEX_SCRIPT_ AND MATLAB_LIBRARIES_ AND MATLAB_INCLUDE_DIRS_)
164     set(MATLAB_BIN          ${MATLAB_BIN_}          PARENT_SCOPE)
165     set(MATLAB_MEX_SCRIPT   ${MATLAB_MEX_SCRIPT_}   PARENT_SCOPE)
166     set(MATLAB_INCLUDE_DIRS ${MATLAB_INCLUDE_DIRS_} PARENT_SCOPE)
167     set(MATLAB_LIBRARIES    ${MATLAB_LIBRARIES_}    PARENT_SCOPE)
168     set(MATLAB_LIBRARY_DIRS ${MATLAB_LIBRARY_DIRS_} PARENT_SCOPE)
169     set(MATLAB_MEXEXT       ${MATLAB_MEXEXT_}       PARENT_SCOPE)
170     set(MATLAB_ARCH         ${MATLAB_ARCH_}         PARENT_SCOPE)
171   endif()
172 endfunction()
173
174
175
176 # ----------------------------------------------------------------------------
177 # FIND MATLAB COMPONENTS
178 # ----------------------------------------------------------------------------
179 if (NOT MATLAB_FOUND)
180
181   # attempt to find the Matlab root folder
182   if (NOT MATLAB_ROOT_DIR)
183     locate_matlab_root()
184   endif()
185
186   # given the matlab root folder, find the library locations
187   if (MATLAB_ROOT_DIR)
188     locate_matlab_components(${MATLAB_ROOT_DIR})
189   endif()
190   find_package_handle_standard_args(Matlab DEFAULT_MSG
191                                            MATLAB_MEX_SCRIPT   MATLAB_INCLUDE_DIRS
192                                            MATLAB_ROOT_DIR     MATLAB_LIBRARIES
193                                            MATLAB_LIBRARY_DIRS MATLAB_MEXEXT
194                                            MATLAB_ARCH         MATLAB_BIN)
195 endif()