8
8
# MATLAB_ROOT_DIR: Root of Matlab installation
9
9
# MATLAB_BIN: The main Matlab "executable" (shell script)
10
10
# MATLAB_MEX_SCRIPT: The mex script used to compile mex files
11
- # MATLAB_BIN: The actual Matlab executable
12
- # MATLAB_INCLUDE_DIR: Path to "mex.h"
13
- # MATLAB_LIBRARY_DIR: Path to mex and matrix libraries
14
- # MATLAB_LIBS: The Matlab libs, usually mx, mex, mat
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
15
14
# MATLAB_MEXEXT: The mex library extension. It will be one of:
16
15
# mexwin32, mexwin64, mexglx, mexa64, mexmac,
17
16
# mexmaci, mexmaci64, mexsol, mexs64
18
- # MATLAB_ARCH: The installation architecture. It is simply
19
- # the MEXEXT with the preceding "mex" removed
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
20
#
21
21
# There doesn't appear to be an elegant way to detect all versions of Matlab
22
22
# across different platforms. If you know the matlab path and want to avoid
@@ -57,23 +57,8 @@ endfunction()
57
57
# the absolute path in the variable MATLAB_ROOT_DIR
58
58
function (locate_matlab_root)
59
59
60
- # --- LINUX ---
61
- if (UNIX AND NOT APPLE )
62
- # possible root locations, in order of likelihood
63
- set (SEARCH_DIRS_ /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
- list (SORT MATLAB_ROOT_DIR_)
69
- list (REVERSE MATLAB_ROOT_DIR_)
70
- list (GET MATLAB_ROOT_DIR_ 0 MATLAB_ROOT_DIR_)
71
- break ()
72
- endif ()
73
- endforeach ()
74
-
75
- # --- APPLE ---
76
- elseif (APPLE )
60
+ # --- UNIX/APPLE ---
61
+ if (UNIX )
77
62
# possible root locations, in order of likelihood
78
63
set (SEARCH_DIRS_ /Applications /usr/local /opt/local /usr /opt)
79
64
foreach (DIR_ ${SEARCH_DIRS_} )
@@ -84,21 +69,25 @@ function(locate_matlab_root)
84
69
list (SORT MATLAB_ROOT_DIR_)
85
70
list (REVERSE MATLAB_ROOT_DIR_)
86
71
list (GET MATLAB_ROOT_DIR_ 0 MATLAB_ROOT_DIR_)
87
- break ()
72
+ set (MATLAB_ROOT_DIR ${MATLAB_ROOT_DIR_} PARENT_SCOPE)
73
+ return ()
88
74
endif ()
89
75
endforeach ()
90
76
91
77
# --- WINDOWS ---
92
78
elseif (WIN32 )
93
- # search the path to see if Matlab exists there
94
- # Contrary to EVERY OTHER REGEX IMPLEMENTATION ON EARTH, cmake returns
95
- # the entire input string if no matches for the capture group are found.
96
- string (REGEX REPLACE ".*[;=](.*[Mm][Aa][Tt][Ll][Aa][Bb][^;]*)\\\\ bin.*" "\\ 1" MATLAB_ROOT_DIR_ "$ENV{PATH} " )
97
- if ("${MATLAB_ROOT_DIR_} " STREQUAL "$ENV{PATH} " )
98
- set (MATLAB_ROOT_DIR_)
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 ()
99
88
endif ()
100
89
101
- # registry-hacking
90
+ # 2. search the registry
102
91
# determine the available Matlab versions
103
92
set (REG_EXTENSION_ "SOFTWARE\\ Mathworks\\ MATLAB" )
104
93
set (REG_ROOTS_ "HKEY_LOCAL_MACHINE" "HKEY_CURRENT_USER" )
@@ -119,16 +108,12 @@ function(locate_matlab_root)
119
108
# request the MATLABROOT from the registry
120
109
foreach (REG_ROOT_ ${REG_ROOTS_} )
121
110
get_filename_component (QUERY_RESPONSE_ [${REG_ROOT_} \\${REG_EXTENSION_} \\${VERSION_} ;MATLABROOT] ABSOLUTE )
122
- if ("${MATLAB_ROOT_DIR_} " STREQUAL "" AND NOT ${QUERY_RESPONSE_} MATCHES "registry$" )
123
- set (MATLAB_ROOT_DIR_ ${QUERY_RESPONSE_} )
111
+ if (NOT ${QUERY_RESPONSE_} MATCHES "registry$" )
112
+ set (MATLAB_ROOT_DIR ${QUERY_RESPONSE_} PARENT_SCOPE)
113
+ return ()
124
114
endif ()
125
115
endforeach ()
126
116
endif ()
127
-
128
- # export the root into the parent scope
129
- if (MATLAB_ROOT_DIR_)
130
- set (MATLAB_ROOT_DIR ${MATLAB_ROOT_DIR_} PARENT_SCOPE)
131
- endif ()
132
117
endfunction ()
133
118
134
119
@@ -140,11 +125,7 @@ endfunction()
140
125
# sets the variable MATLAB_FOUND to TRUE
141
126
function (locate_matlab_components MATLAB_ROOT_DIR)
142
127
# get the mex extension
143
- if (UNIX )
144
- execute_process (COMMAND ${MATLAB_ROOT_DIR} /bin/mexext OUTPUT_VARIABLE MATLAB_MEXEXT_)
145
- elseif (WIN32 )
146
- execute_process (COMMAND ${MATLAB_ROOT_DIR} /bin/mexext.bat OUTPUT_VARIABLE MATLAB_MEXEXT_)
147
- endif ()
128
+ execute_process (COMMAND ${MATLAB_ROOT_DIR} /bin/mexext OUTPUT_VARIABLE MATLAB_MEXEXT_)
148
129
if (NOT MATLAB_MEXEXT_)
149
130
return ()
150
131
endif ()
@@ -160,35 +141,34 @@ function(locate_matlab_components MATLAB_ROOT_DIR)
160
141
endforeach ()
161
142
162
143
# get the path to the libraries
163
- set (MATLAB_LIBRARY_DIR_ ${MATLAB_ROOT_DIR} /bin/${MATLAB_ARCH_} )
144
+ set (MATLAB_LIBRARY_DIRS_ ${MATLAB_ROOT_DIR} /bin/${MATLAB_ARCH_} )
164
145
165
146
# get the libraries
166
147
set_libarch_prefix_suffix()
167
- find_library (MATLAB_LIB_MX_ mx PATHS ${MATLAB_LIBRARY_DIR_ } NO_DEFAULT_PATH)
168
- find_library (MATLAB_LIB_MEX_ mex PATHS ${MATLAB_LIBRARY_DIR_ } NO_DEFAULT_PATH)
169
- find_library (MATLAB_LIB_MAT_ mat PATHS ${MATLAB_LIBRARY_DIR_ } NO_DEFAULT_PATH)
170
- set (MATLAB_LIBS_ ${MATLAB_LIB_MX_} ${MATLAB_LIB_MEX_} ${MATLAB_LIB_MAT_} )
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_} )
171
152
172
153
# get the include path
173
- find_path (MATLAB_INCLUDE_DIR_ mex.h ${MATLAB_ROOT_DIR} /extern/include )
154
+ find_path (MATLAB_INCLUDE_DIRS_ mex.h ${MATLAB_ROOT_DIR} /extern/include )
174
155
175
156
# get the mex shell script
176
- find_file (MATLAB_MEX_SCRIPT_ NAMES mex mex.bat PATHS ${MATLAB_ROOT_DIR} /bin NO_DEFAULT_PATH)
157
+ find_program (MATLAB_MEX_SCRIPT_ NAMES mex PATHS ${MATLAB_ROOT_DIR} /bin NO_DEFAULT_PATH)
177
158
178
159
# get the Matlab executable
179
- find_file (MATLAB_BIN_ NAMES matlab matlab.exe PATHS ${MATLAB_ROOT_DIR} /bin NO_DEFAULT_PATH)
160
+ find_program (MATLAB_BIN_ NAMES matlab PATHS ${MATLAB_ROOT_DIR} /bin NO_DEFAULT_PATH)
180
161
181
162
# export into parent scope
182
- if (MATLAB_MEX_SCRIPT_ AND MATLAB_LIBS_ AND MATLAB_INCLUDE_DIR_ )
183
- set (MATLAB_BIN ${MATLAB_BIN_} PARENT_SCOPE)
184
- set (MATLAB_MEX_SCRIPT ${MATLAB_MEX_SCRIPT_} PARENT_SCOPE)
185
- set (MATLAB_INCLUDE_DIR ${MATLAB_INCLUDE_DIR_ } PARENT_SCOPE)
186
- set (MATLAB_LIBS ${MATLAB_LIBS_} PARENT_SCOPE)
187
- set (MATLAB_LIBRARY_DIR ${MATLAB_LIBRARY_DIR_ } PARENT_SCOPE)
188
- set (MATLAB_MEXEXT ${MATLAB_MEXEXT_} PARENT_SCOPE)
189
- set (MATLAB_ARCH ${MATLAB_ARCH_} 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)
190
171
endif ()
191
- return ()
192
172
endfunction ()
193
173
194
174
@@ -198,9 +178,6 @@ endfunction()
198
178
# ----------------------------------------------------------------------------
199
179
if (NOT MATLAB_FOUND)
200
180
201
- # guilty until proven innocent
202
- set (MATLAB_FOUND FALSE )
203
-
204
181
# attempt to find the Matlab root folder
205
182
if (NOT MATLAB_ROOT_DIR)
206
183
locate_matlab_root()
@@ -210,7 +187,9 @@ if (NOT MATLAB_FOUND)
210
187
if (MATLAB_ROOT_DIR)
211
188
locate_matlab_components(${MATLAB_ROOT_DIR} )
212
189
endif ()
213
- find_package_handle_standard_args(Matlab DEFAULT_MSG MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIR
214
- MATLAB_ROOT_DIR MATLAB_LIBS MATLAB_LIBRARY_DIR
215
- MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN)
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)
216
195
endif ()
0 commit comments