FindMatlab.cmake 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindMatlab
  5. ----------
  6. Finds Matlab or Matlab Compiler Runtime (MCR) and provides Matlab tools,
  7. libraries and compilers to CMake.
  8. This package primary purpose is to find the libraries associated with Matlab
  9. or the MCR in order to be able to build Matlab extensions (mex files). It
  10. can also be used:
  11. * to run specific commands in Matlab in case Matlab is available
  12. * for declaring Matlab unit test
  13. * to retrieve various information from Matlab (mex extensions, versions and
  14. release queries, ...)
  15. .. versionadded:: 3.12
  16. Added Matlab Compiler Runtime (MCR) support.
  17. The module supports the following components:
  18. * ``ENG_LIBRARY`` and ``MAT_LIBRARY``: respectively the ``ENG`` and ``MAT``
  19. libraries of Matlab
  20. * ``MAIN_PROGRAM`` the Matlab binary program. Note that this component is not
  21. available on the MCR version, and will yield an error if the MCR is found
  22. instead of the regular Matlab installation.
  23. * ``MEX_COMPILER`` the MEX compiler.
  24. * ``MCC_COMPILER`` the MCC compiler, included with the Matlab Compiler add-on.
  25. * ``SIMULINK`` the Simulink environment.
  26. .. versionadded:: 3.7
  27. Added the ``MAT_LIBRARY`` component.
  28. .. versionadded:: 3.13
  29. Added the ``ENGINE_LIBRARY``, ``DATAARRAY_LIBRARY`` and ``MCC_COMPILER``
  30. components.
  31. .. versionchanged:: 3.14
  32. Removed the ``MX_LIBRARY``, ``ENGINE_LIBRARY`` and ``DATAARRAY_LIBRARY``
  33. components. These libraries are found unconditionally.
  34. .. versionadded:: 3.30
  35. Added support for specifying a version range to :command:`find_package` and
  36. added support for specifying ``REGISTRY_VIEW`` to :command:`find_package`,
  37. :command:`matlab_extract_all_installed_versions_from_registry` and
  38. :command:`matlab_get_all_valid_matlab_roots_from_registry`. The default
  39. behavior remained unchanged, by using the registry view ``TARGET``.
  40. .. note::
  41. The version given to the :command:`find_package` directive is the Matlab
  42. **version**, which should not be confused with the Matlab *release* name
  43. (e.g. `R2023b`).
  44. The :command:`matlab_get_version_from_release_name` and
  45. :command:`matlab_get_release_name_from_version` provide a mapping
  46. between the release name and the version.
  47. The variable :variable:`Matlab_ROOT_DIR` may be specified in order to give
  48. the path of the desired Matlab version. Otherwise, the behavior is platform
  49. specific:
  50. * Windows: The installed versions of Matlab/MCR are retrieved from the
  51. Windows registry. The ``REGISTRY_VIEW`` argument may optionally be specified
  52. to manually control whether 32bit or 64bit versions shall be searched for.
  53. * macOS: The installed versions of Matlab/MCR are given by the MATLAB
  54. default installation paths in ``/Application``. If no such application is
  55. found, it falls back to the one that might be accessible from the ``PATH``.
  56. * Unix: The desired Matlab should be accessible from the ``PATH``. This does
  57. not work for MCR installation and :variable:`Matlab_ROOT_DIR` should be
  58. specified on this platform.
  59. Additional information is provided when :variable:`MATLAB_FIND_DEBUG` is set.
  60. When a Matlab/MCR installation is found automatically and the ``MATLAB_VERSION``
  61. is not given, the version is queried from Matlab directly (on Windows this
  62. may pop up a Matlab window) or from the MCR installation.
  63. The mapping of the release names and the version of Matlab is performed by
  64. defining pairs (name, version). The variable
  65. :variable:`MATLAB_ADDITIONAL_VERSIONS` may be provided before the call to
  66. the :command:`find_package` in order to handle additional versions.
  67. A Matlab scripts can be added to the set of tests using the
  68. :command:`matlab_add_unit_test`. By default, the Matlab unit test framework
  69. will be used (>= 2013a) to run this script, but regular ``.m`` files
  70. returning an exit code can be used as well (0 indicating a success).
  71. Module Input Variables
  72. ^^^^^^^^^^^^^^^^^^^^^^
  73. Users or projects may set the following variables to configure the module
  74. behavior:
  75. :variable:`Matlab_ROOT <<PackageName>_ROOT>`
  76. .. versionadded:: 3.25
  77. Default value for :variable:`Matlab_ROOT_DIR`, the root of the Matlab
  78. installation.
  79. :variable:`Matlab_ROOT_DIR`
  80. The root of the Matlab installation.
  81. :variable:`MATLAB_FIND_DEBUG`
  82. outputs debug information
  83. :variable:`MATLAB_ADDITIONAL_VERSIONS`
  84. additional versions of Matlab for the automatic retrieval of the installed
  85. versions.
  86. Imported targets
  87. ^^^^^^^^^^^^^^^^
  88. .. versionadded:: 3.22
  89. This module defines the following :prop_tgt:`IMPORTED` targets:
  90. ``Matlab::mex``
  91. The ``mex`` library, always available.
  92. ``Matlab::mx``
  93. The mx library of Matlab (arrays), always available.
  94. ``Matlab::eng``
  95. Matlab engine library. Available only if the ``ENG_LIBRARY`` component
  96. is requested.
  97. ``Matlab::mat``
  98. Matlab matrix library. Available only if the ``MAT_LIBRARY`` component
  99. is requested.
  100. ``Matlab::MatlabEngine``
  101. Matlab C++ engine library, always available for R2018a and newer.
  102. ``Matlab::MatlabDataArray``
  103. Matlab C++ data array library, always available for R2018a and newer.
  104. Variables defined by the module
  105. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  106. Result variables
  107. """"""""""""""""
  108. ``Matlab_FOUND``
  109. ``TRUE`` if the Matlab installation is found, ``FALSE``
  110. otherwise. All variable below are defined if Matlab is found.
  111. ``Matlab_VERSION``
  112. .. versionadded:: 3.27
  113. the numerical version (e.g. 23.2.0) of Matlab found. Not to be confused with
  114. Matlab release name (e.g. R2023b) that can be obtained with
  115. :command:`matlab_get_release_name_from_version`.
  116. ``Matlab_ROOT_DIR``
  117. the final root of the Matlab installation determined by the FindMatlab
  118. module.
  119. ``Matlab_MAIN_PROGRAM``
  120. the Matlab binary program. Available only if the component ``MAIN_PROGRAM``
  121. is given in the :command:`find_package` directive.
  122. ``Matlab_INCLUDE_DIRS``
  123. the path of the Matlab libraries headers
  124. ``Matlab_MEX_LIBRARY``
  125. library for mex, always available.
  126. ``Matlab_MX_LIBRARY``
  127. mx library of Matlab (arrays), always available.
  128. ``Matlab_ENG_LIBRARY``
  129. Matlab engine library. Available only if the component ``ENG_LIBRARY``
  130. is requested.
  131. ``Matlab_MAT_LIBRARY``
  132. Matlab matrix library. Available only if the component ``MAT_LIBRARY``
  133. is requested.
  134. ``Matlab_ENGINE_LIBRARY``
  135. .. versionadded:: 3.13
  136. Matlab C++ engine library, always available for R2018a and newer.
  137. ``Matlab_DATAARRAY_LIBRARY``
  138. .. versionadded:: 3.13
  139. Matlab C++ data array library, always available for R2018a and newer.
  140. ``Matlab_LIBRARIES``
  141. the whole set of libraries of Matlab
  142. ``Matlab_MEX_COMPILER``
  143. the mex compiler of Matlab. Currently not used.
  144. Available only if the component ``MEX_COMPILER`` is requested.
  145. ``Matlab_MCC_COMPILER``
  146. .. versionadded:: 3.13
  147. the mcc compiler of Matlab. Included with the Matlab Compiler add-on.
  148. Available only if the component ``MCC_COMPILER`` is requested.
  149. Cached variables
  150. """"""""""""""""
  151. ``Matlab_MEX_EXTENSION``
  152. the extension of the mex files for the current platform (given by Matlab).
  153. ``Matlab_ROOT_DIR``
  154. the location of the root of the Matlab installation found. If this value
  155. is changed by the user, the result variables are recomputed.
  156. Provided commands
  157. ^^^^^^^^^^^^^^^^^
  158. :command:`matlab_get_version_from_release_name`
  159. returns the version from the Matlab release name
  160. :command:`matlab_get_release_name_from_version`
  161. returns the release name from the Matlab version
  162. :command:`matlab_add_mex`
  163. adds a target compiling a MEX file.
  164. :command:`matlab_add_unit_test`
  165. adds a Matlab unit test file as a test to the project.
  166. :command:`matlab_extract_all_installed_versions_from_registry`
  167. parses the registry for all Matlab versions. Available on Windows only.
  168. The part of the registry parsed is dependent on the host processor
  169. :command:`matlab_get_all_valid_matlab_roots_from_registry`
  170. returns all the possible Matlab or MCR paths, according to a previously
  171. given list. Only the existing/accessible paths are kept. This is mainly
  172. useful for the searching all possible Matlab installation.
  173. :command:`matlab_get_mex_suffix`
  174. returns the suffix to be used for the mex files
  175. (platform/architecture dependent)
  176. :command:`matlab_get_version_from_matlab_run`
  177. returns the version of Matlab/MCR, given the full directory of the Matlab/MCR
  178. installation path.
  179. Known issues
  180. ^^^^^^^^^^^^
  181. **Symbol clash in a MEX target**
  182. By default, every symbols inside a MEX
  183. file defined with the command :command:`matlab_add_mex` have hidden
  184. visibility, except for the entry point. This is the default behavior of
  185. the MEX compiler, which lowers the risk of symbol collision between the
  186. libraries shipped with Matlab, and the libraries to which the MEX file is
  187. linking to. This is also the default on Windows platforms.
  188. However, this is not sufficient in certain case, where for instance your
  189. MEX file is linking against libraries that are already loaded by Matlab,
  190. even if those libraries have different SONAMES.
  191. A possible solution is to hide the symbols of the libraries to which the
  192. MEX target is linking to. This can be achieved in GNU GCC compilers with
  193. the linker option ``-Wl,--exclude-libs,ALL``.
  194. **Tests using GPU resources**
  195. in case your MEX file is using the GPU and
  196. in order to be able to run unit tests on this MEX file, the GPU resources
  197. should be properly released by Matlab. A possible solution is to make
  198. Matlab aware of the use of the GPU resources in the session, which can be
  199. performed by a command such as ``D = gpuDevice()`` at the beginning of
  200. the test script (or via a fixture).
  201. Reference
  202. ^^^^^^^^^
  203. .. variable:: Matlab_ROOT_DIR
  204. The root folder of the Matlab installation. If set before the call to
  205. :command:`find_package`, the module will look for the components in that
  206. path. If not set, then an automatic search of Matlab
  207. will be performed. If set, it should point to a valid version of Matlab.
  208. .. variable:: MATLAB_FIND_DEBUG
  209. If set, the lookup of Matlab and the intermediate configuration steps are
  210. outputted to the console.
  211. .. variable:: MATLAB_ADDITIONAL_VERSIONS
  212. If set, specifies additional versions of Matlab that may be looked for.
  213. The variable should be a list of strings, organized by pairs of release
  214. name and versions, such as follows::
  215. set(MATLAB_ADDITIONAL_VERSIONS
  216. "release_name1=corresponding_version1"
  217. "release_name2=corresponding_version2"
  218. ...
  219. )
  220. Example::
  221. set(MATLAB_ADDITIONAL_VERSIONS
  222. "R2013b=8.2"
  223. "R2013a=8.1"
  224. "R2012b=8.0")
  225. The order of entries in this list matters when several versions of
  226. Matlab are installed. The priority is set according to the ordering in
  227. this list.
  228. #]=======================================================================]
  229. cmake_policy(PUSH)
  230. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  231. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  232. set(_FindMatlab_SELF_DIR "${CMAKE_CURRENT_LIST_DIR}")
  233. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  234. if(NOT WIN32 AND NOT APPLE AND NOT Threads_FOUND
  235. AND (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED))
  236. # MEX files use pthread if available
  237. set(THREADS_PREFER_PTHREAD_FLAG ON)
  238. find_package(Threads)
  239. endif()
  240. # The currently supported versions. Other version can be added by the user by
  241. # providing MATLAB_ADDITIONAL_VERSIONS
  242. if(NOT MATLAB_ADDITIONAL_VERSIONS)
  243. set(MATLAB_ADDITIONAL_VERSIONS)
  244. endif()
  245. set(MATLAB_VERSIONS_MAPPING
  246. "R2024b=24.2"
  247. "R2024a=24.1"
  248. "R2023b=23.2"
  249. "R2023a=9.14"
  250. "R2022b=9.13"
  251. "R2022a=9.12"
  252. "R2021b=9.11"
  253. "R2021a=9.10"
  254. "R2020b=9.9"
  255. "R2020a=9.8"
  256. "R2019b=9.7"
  257. "R2019a=9.6"
  258. "R2018b=9.5"
  259. "R2018a=9.4"
  260. "R2017b=9.3"
  261. "R2017a=9.2"
  262. "R2016b=9.1"
  263. "R2016a=9.0"
  264. "R2015b=8.6"
  265. "R2015a=8.5"
  266. "R2014b=8.4"
  267. "R2014a=8.3"
  268. "R2013b=8.2"
  269. "R2013a=8.1"
  270. "R2012b=8.0"
  271. "R2012a=7.14"
  272. "R2011b=7.13"
  273. "R2011a=7.12"
  274. "R2010b=7.11"
  275. ${MATLAB_ADDITIONAL_VERSIONS}
  276. )
  277. # temporary folder for all Matlab runs
  278. set(_matlab_temporary_folder ${CMAKE_BINARY_DIR}/Matlab)
  279. file(MAKE_DIRECTORY "${_matlab_temporary_folder}")
  280. #[=======================================================================[.rst:
  281. .. command:: matlab_get_version_from_release_name
  282. .. code-block:: cmake
  283. matlab_get_version_from_release_name(release version)
  284. * Input: ``release`` is the release name (e.g. R2023b)
  285. * Output: ``version`` is the version of Matlab (e.g. 23.2.0)
  286. Returns the version of Matlab from a release name
  287. .. note::
  288. This command provides correct versions mappings for Matlab but not MCR.
  289. #]=======================================================================]
  290. macro(matlab_get_version_from_release_name release_name version_name)
  291. string(REGEX MATCHALL "${release_name}=([0-9]+\\.[0-9]+)" _matched ${MATLAB_VERSIONS_MAPPING})
  292. set(${version_name} "")
  293. if(NOT _matched STREQUAL "")
  294. set(${version_name} ${CMAKE_MATCH_1})
  295. else()
  296. message(WARNING "[MATLAB] The release name ${release_name} is not registered")
  297. endif()
  298. unset(_matched)
  299. endmacro()
  300. #[=======================================================================[.rst:
  301. .. command:: matlab_get_release_name_from_version
  302. .. code-block:: cmake
  303. matlab_get_release_name_from_version(version release_name)
  304. * Input: ``version`` is the version of Matlab (e.g. 23.2.0)
  305. * Output: ``release_name`` is the release name (R2023b)
  306. Returns the release name from the version of Matlab
  307. .. note::
  308. This command provides correct version mappings for Matlab but not MCR.
  309. #]=======================================================================]
  310. function(matlab_get_release_name_from_version version release_name)
  311. # only the major.minor version is used
  312. string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" version "${version}")
  313. foreach(_var IN LISTS MATLAB_VERSIONS_MAPPING)
  314. if(_var MATCHES "(.+)=${version}")
  315. set(${release_name} ${CMAKE_MATCH_1} PARENT_SCOPE)
  316. return()
  317. endif()
  318. endforeach()
  319. message(WARNING "[MATLAB] The version ${version} is not registered")
  320. endfunction()
  321. # extracts all the supported release names (R2022b...) of Matlab
  322. # internal use
  323. macro(matlab_get_supported_releases list_releases)
  324. set(${list_releases})
  325. foreach(_var IN LISTS MATLAB_VERSIONS_MAPPING)
  326. string(REGEX MATCHALL "(.+)=([0-9]+\\.[0-9]+)" _matched ${_var})
  327. if(NOT _matched STREQUAL "")
  328. list(APPEND ${list_releases} ${CMAKE_MATCH_1})
  329. endif()
  330. unset(_matched)
  331. unset(CMAKE_MATCH_1)
  332. endforeach()
  333. unset(_var)
  334. endmacro()
  335. # extracts all the supported versions of Matlab
  336. # internal use
  337. macro(matlab_get_supported_versions list_versions)
  338. set(${list_versions})
  339. foreach(_var IN LISTS MATLAB_VERSIONS_MAPPING)
  340. string(REGEX MATCHALL "(.+)=([0-9]+\\.[0-9]+)" _matched ${_var})
  341. if(NOT _matched STREQUAL "")
  342. list(APPEND ${list_versions} ${CMAKE_MATCH_2})
  343. endif()
  344. unset(_matched)
  345. unset(CMAKE_MATCH_1)
  346. endforeach()
  347. unset(_var)
  348. endmacro()
  349. #[=======================================================================[.rst:
  350. .. command:: matlab_extract_all_installed_versions_from_registry
  351. This function parses the Windows registry and finds the Matlab versions that
  352. are installed. The found versions are stored in ``matlab_versions``.
  353. .. signature::
  354. matlab_extract_all_installed_versions_from_registry(matlab_versions
  355. [REGISTRY_VIEW view])
  356. .. versionadded:: 3.30
  357. * Output: ``matlab_versions`` is a list of all the versions of Matlab found
  358. * Input: ``REGISTRY_VIEW`` Optional registry view to use for registry
  359. interaction. The argument is passed (or omitted) to
  360. :command:`cmake_host_system_information` without further checks or
  361. modification.
  362. .. signature::
  363. matlab_extract_all_installed_versions_from_registry(win64 matlab_versions)
  364. * Input: ``win64`` is a boolean to search for the 64 bit version of
  365. Matlab. Set to ``ON`` to use the 64bit registry view or ``OFF`` to use the
  366. 32bit registry view. If finer control is needed, see signature above.
  367. * Output: ``matlab_versions`` is a list of all the versions of Matlab found
  368. The returned list contains all versions under
  369. ``HKLM\SOFTWARE\Mathworks\MATLAB``,
  370. ``HKLM\SOFTWARE\Mathworks\MATLAB Runtime`` and
  371. ``HKLM\SOFTWARE\Mathworks\MATLAB Compiler Runtime`` or an empty list in
  372. case an error occurred (or nothing found).
  373. .. note::
  374. Only the versions are provided. No check is made over the existence of the
  375. installation referenced in the registry,
  376. #]=======================================================================]
  377. function(matlab_extract_all_installed_versions_from_registry win64_or_matlab_versions)
  378. if(NOT CMAKE_HOST_WIN32)
  379. message(FATAL_ERROR "[MATLAB] This function can only be called by a Windows host")
  380. endif()
  381. set(_registry_view_args)
  382. if("${ARGC}" EQUAL "2")
  383. # Old API: <win64> <matlab_versions>
  384. if(${win64_or_matlab_versions})
  385. set(_registry_view_args VIEW 64)
  386. else()
  387. set(_registry_view_args VIEW 32)
  388. endif()
  389. set(matlab_versions ${ARGV1})
  390. else()
  391. # New API: <matlab_versions> [REGISTRY_VIEW <view>]
  392. set(matlab_versions ${win64_or_matlab_versions})
  393. cmake_parse_arguments(_Matlab "" "REGISTRY_VIEW" "" ${ARGN})
  394. if(_Matlab_REGISTRY_VIEW)
  395. set(_registry_view_args VIEW "${_Matlab_REGISTRY_VIEW}")
  396. endif()
  397. endif()
  398. if(MATLAB_FIND_DEBUG)
  399. message(STATUS "[MATLAB] Extracting MATLAB versions with registry view args '${_registry_view_args}'")
  400. endif()
  401. set(matlabs_from_registry)
  402. foreach(_installation_type IN ITEMS "MATLAB" "MATLAB Runtime" "MATLAB Compiler Runtime")
  403. cmake_host_system_information(RESULT _reg
  404. QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Mathworks/${_installation_type}"
  405. SUBKEYS
  406. ${_registry_view_args}
  407. )
  408. string(REGEX MATCHALL "([0-9]+(\\.[0-9]+)+)" _versions_regex "${_reg}")
  409. list(APPEND matlabs_from_registry ${_versions_regex})
  410. endforeach()
  411. if(matlabs_from_registry)
  412. list(REMOVE_DUPLICATES matlabs_from_registry)
  413. list(SORT matlabs_from_registry COMPARE NATURAL ORDER DESCENDING)
  414. endif()
  415. set(${matlab_versions} ${matlabs_from_registry} PARENT_SCOPE)
  416. endfunction()
  417. # (internal)
  418. macro(extract_matlab_versions_from_registry_brute_force matlab_versions)
  419. # get the supported versions
  420. set(matlab_supported_versions)
  421. matlab_get_supported_versions(matlab_supported_versions)
  422. # we order from more recent to older
  423. if(matlab_supported_versions)
  424. list(REMOVE_DUPLICATES matlab_supported_versions)
  425. list(SORT matlab_supported_versions COMPARE NATURAL ORDER DESCENDING)
  426. endif()
  427. set(${matlab_versions} ${matlab_supported_versions})
  428. endmacro()
  429. #[=======================================================================[.rst:
  430. .. command:: matlab_get_all_valid_matlab_roots_from_registry
  431. Populates the Matlab root with valid versions of Matlab or
  432. Matlab Runtime (MCR).
  433. The returned matlab_roots is organized in triplets
  434. ``(type,version_number,matlab_root_path)``, where ``type``
  435. indicates either ``MATLAB`` or ``MCR``.
  436. .. code-block:: cmake
  437. matlab_get_all_valid_matlab_roots_from_registry(matlab_versions matlab_roots [REGISTRY_VIEW view])
  438. * Input: ``matlab_versions`` of each of the Matlab or MCR installations
  439. * Output: ``matlab_roots`` location of each of the Matlab or MCR installations
  440. * Input: ``REGISTRY_VIEW`` Optional registry view to use for registry
  441. interaction. The argument is passed (or omitted) to
  442. :command:`cmake_host_system_information` without further checks or
  443. modification.
  444. .. versionadded:: 3.30
  445. The optional ``REGISTRY_VIEW`` argument was added to provide a more precise
  446. interface on how to interact with the Windows Registry.
  447. #]=======================================================================]
  448. function(matlab_get_all_valid_matlab_roots_from_registry matlab_versions matlab_roots)
  449. # The matlab_versions comes either from
  450. # extract_matlab_versions_from_registry_brute_force or
  451. # matlab_extract_all_installed_versions_from_registry.
  452. cmake_parse_arguments(_Matlab "" "REGISTRY_VIEW" "" ${ARGN})
  453. set(_registry_view_args)
  454. if(_Matlab_REGISTRY_VIEW)
  455. set(_registry_view_args VIEW "${_Matlab_REGISTRY_VIEW}")
  456. endif()
  457. if(MATLAB_FIND_DEBUG)
  458. message(STATUS "[MATLAB] Getting MATLAB roots with registry view args '${_registry_view_args}'")
  459. endif()
  460. # Mostly the major.minor version is used in Mathworks Windows Registry keys.
  461. # If the patch is not zero, major.minor.patch is used.
  462. list(TRANSFORM matlab_versions REPLACE "^([0-9]+\\.[0-9]+(\\.[1-9][0-9]*)?).*" "\\1")
  463. set(_matlab_roots_list )
  464. # check for Matlab installations
  465. foreach(_matlab_current_version IN LISTS matlab_versions)
  466. cmake_host_system_information(RESULT current_MATLAB_ROOT
  467. QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Mathworks/MATLAB/${_matlab_current_version}"
  468. VALUE "MATLABROOT"
  469. ${_registry_view_args}
  470. )
  471. cmake_path(CONVERT "${current_MATLAB_ROOT}" TO_CMAKE_PATH_LIST current_MATLAB_ROOT)
  472. if(IS_DIRECTORY "${current_MATLAB_ROOT}")
  473. _Matlab_VersionInfoXML("${current_MATLAB_ROOT}" _matlab_version_tmp)
  474. if("${_matlab_version_tmp}" STREQUAL "unknown")
  475. set(_matlab_version_tmp ${_matlab_current_version})
  476. endif()
  477. list(APPEND _matlab_roots_list "MATLAB" ${_matlab_version_tmp} ${current_MATLAB_ROOT})
  478. endif()
  479. endforeach()
  480. # Check for MCR installations
  481. foreach(_installation_type IN ITEMS "MATLAB Runtime" "MATLAB Compiler Runtime")
  482. foreach(_matlab_current_version IN LISTS matlab_versions)
  483. cmake_host_system_information(RESULT current_MATLAB_ROOT
  484. QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Mathworks/${_installation_type}/${_matlab_current_version}"
  485. VALUE "MATLABROOT"
  486. ${_registry_view_args}
  487. )
  488. cmake_path(CONVERT "${current_MATLAB_ROOT}" TO_CMAKE_PATH_LIST current_MATLAB_ROOT)
  489. # remove the dot
  490. string(REPLACE "." "" _matlab_current_version_without_dot "${_matlab_current_version}")
  491. if(IS_DIRECTORY "${current_MATLAB_ROOT}")
  492. if(IS_DIRECTORY "${current_MATLAB_ROOT}/v${_matlab_current_version_without_dot}")
  493. cmake_path(APPEND current_MATLAB_ROOT "v${_matlab_current_version_without_dot}")
  494. endif()
  495. _Matlab_VersionInfoXML("${current_MATLAB_ROOT}" _matlab_version_tmp)
  496. if("${_matlab_version_tmp}" STREQUAL "unknown")
  497. set(_matlab_version_tmp ${_matlab_current_version})
  498. endif()
  499. list(APPEND _matlab_roots_list "MCR" ${_matlab_version_tmp} "${current_MATLAB_ROOT}")
  500. endif()
  501. endforeach()
  502. endforeach()
  503. set(${matlab_roots} ${_matlab_roots_list} PARENT_SCOPE)
  504. endfunction()
  505. #[=======================================================================[.rst:
  506. .. command:: matlab_get_mex_suffix
  507. Returns the extension of the mex files (the suffixes).
  508. This function should not be called before the appropriate Matlab root has
  509. been found.
  510. .. code-block:: cmake
  511. matlab_get_mex_suffix(matlab_root mex_suffix)
  512. * Input: ``matlab_root`` root of Matlab/MCR install e.g. ``Matlab_ROOT_DIR``
  513. * Output: ``mex_suffix`` variable name in which the suffix will be returned.
  514. #]=======================================================================]
  515. function(matlab_get_mex_suffix matlab_root mex_suffix)
  516. # find_program does not consider script suffix .bat for Matlab mexext.bat on Windows
  517. set(mexext_suffix "")
  518. if(WIN32)
  519. set(mexext_suffix ".bat")
  520. endif()
  521. find_program(
  522. Matlab_MEXEXTENSIONS_PROG
  523. NAMES mexext mexext${mexext_suffix}
  524. PATHS ${matlab_root}/bin
  525. DOC "Matlab MEX extension provider"
  526. NO_DEFAULT_PATH
  527. )
  528. if(MATLAB_FIND_DEBUG)
  529. message(STATUS "[MATLAB] Determining mex files extensions from '${matlab_root}/bin' with program '${Matlab_MEXEXTENSIONS_PROG}'")
  530. endif()
  531. # the program has been found?
  532. if(NOT Matlab_MEXEXTENSIONS_PROG)
  533. if(MATLAB_FIND_DEBUG)
  534. message(WARNING "[MATLAB] Cannot found mexext program. Matlab root is ${matlab_root}")
  535. endif()
  536. unset(Matlab_MEXEXTENSIONS_PROG CACHE)
  537. return()
  538. endif()
  539. set(_matlab_mex_extension)
  540. set(devnull)
  541. if(UNIX)
  542. set(devnull INPUT_FILE /dev/null)
  543. elseif(WIN32)
  544. set(devnull INPUT_FILE NUL)
  545. endif()
  546. set(_arch)
  547. if(WIN32)
  548. # this environment variable is used to determine the arch on Windows
  549. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  550. set(_arch "MATLAB_ARCH=win64")
  551. else()
  552. set(_arch "MATLAB_ARCH=win32")
  553. endif()
  554. endif()
  555. # this is the preferred way. If this does not work properly (eg. MCR on Windows), then we use our own knowledge
  556. execute_process(
  557. COMMAND ${CMAKE_COMMAND} -E env ${_arch} ${Matlab_MEXEXTENSIONS_PROG}
  558. OUTPUT_VARIABLE _matlab_mex_extension
  559. ERROR_VARIABLE _matlab_mex_extension_error
  560. OUTPUT_STRIP_TRAILING_WHITESPACE
  561. ${devnull})
  562. if(_matlab_mex_extension_error)
  563. if(WIN32)
  564. # this is only for intel architecture
  565. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  566. set(_matlab_mex_extension "mexw64")
  567. else()
  568. set(_matlab_mex_extension "mexw32")
  569. endif()
  570. endif()
  571. endif()
  572. string(STRIP "${_matlab_mex_extension}" _matlab_mex_extension)
  573. if(MATLAB_FIND_DEBUG)
  574. message(STATUS "[MATLAB] '${Matlab_MEXEXTENSIONS_PROG}' : determined extension '${_matlab_mex_extension}' and error string is '${_matlab_mex_extension_error}'")
  575. endif()
  576. set(${mex_suffix} ${_matlab_mex_extension} PARENT_SCOPE)
  577. endfunction()
  578. #[=======================================================================[.rst:
  579. .. command:: matlab_get_version_from_matlab_run
  580. This function runs Matlab program specified on arguments and extracts its
  581. version. If the path provided for the Matlab installation points to an MCR
  582. installation, the version is extracted from the installed files.
  583. .. code-block:: cmake
  584. matlab_get_version_from_matlab_run(matlab_binary_path matlab_list_versions)
  585. * Input: ``matlab_binary_path`` path of the `matlab` binary executable
  586. * Output: ``matlab_list_versions`` the version extracted from Matlab
  587. #]=======================================================================]
  588. function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_versions)
  589. set(${matlab_list_versions} "" PARENT_SCOPE)
  590. if(MATLAB_FIND_DEBUG)
  591. message(STATUS "[MATLAB] Determining the version of Matlab from ${matlab_binary_program}")
  592. endif()
  593. if(EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  594. if(MATLAB_FIND_DEBUG)
  595. message(STATUS "[MATLAB] Removing previous ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp file")
  596. endif()
  597. file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  598. endif()
  599. # the log file is needed since on windows the command executes in a new
  600. # window and it is not possible to get back the answer of Matlab
  601. # the -wait command is needed on windows, otherwise the call returns
  602. # immediately after the program launches itself.
  603. if(WIN32)
  604. set(_matlab_additional_commands "-wait")
  605. endif()
  606. set(devnull)
  607. if(UNIX)
  608. set(devnull INPUT_FILE /dev/null)
  609. elseif(WIN32)
  610. set(devnull INPUT_FILE NUL)
  611. endif()
  612. # we first try to run a simple program using the -r option, and then we use the
  613. # -batch option that is supported and recommended since R2019a
  614. set(_matlab_get_version_failed_with_r_option FALSE)
  615. # timeout set to 120 seconds, in case it does not start
  616. # note as said before OUTPUT_VARIABLE cannot be used in a platform
  617. # independent manner however, not setting it would flush the output of Matlab
  618. # in the current console (unix variant)
  619. execute_process(
  620. COMMAND "${matlab_binary_program}" -nosplash -nojvm ${_matlab_additional_commands} -logfile "matlabVersionLog.cmaketmp" -nodesktop -nodisplay -r "version, exit"
  621. OUTPUT_VARIABLE _matlab_version_from_cmd_dummy
  622. RESULT_VARIABLE _matlab_result_version_call
  623. ERROR_VARIABLE _matlab_result_version_call_error
  624. TIMEOUT 120
  625. WORKING_DIRECTORY "${_matlab_temporary_folder}"
  626. ${devnull}
  627. )
  628. if(_matlab_result_version_call MATCHES "timeout")
  629. if(MATLAB_FIND_DEBUG)
  630. message(WARNING "[MATLAB] Unable to determine the version of Matlab."
  631. " Matlab call with -r option timed out after 120 seconds.")
  632. endif()
  633. set(_matlab_get_version_failed_with_r_option TRUE)
  634. endif()
  635. if(NOT ${_matlab_get_version_failed_with_r_option} AND ${_matlab_result_version_call})
  636. if(MATLAB_FIND_DEBUG)
  637. message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call with -r option returned with error ${_matlab_result_version_call}.")
  638. endif()
  639. set(_matlab_get_version_failed_with_r_option TRUE)
  640. elseif(NOT ${_matlab_get_version_failed_with_r_option} AND NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  641. if(MATLAB_FIND_DEBUG)
  642. message(WARNING "[MATLAB] Unable to determine the version of Matlab. The log file does not exist.")
  643. endif()
  644. set(_matlab_get_version_failed_with_r_option TRUE)
  645. endif()
  646. if(_matlab_get_version_failed_with_r_option)
  647. execute_process(
  648. COMMAND "${matlab_binary_program}" -nosplash -nojvm ${_matlab_additional_commands} -logfile "matlabVersionLog.cmaketmp" -nodesktop -nodisplay -batch "version, exit"
  649. OUTPUT_VARIABLE _matlab_version_from_cmd_dummy_batch
  650. RESULT_VARIABLE _matlab_result_version_call_batch
  651. ERROR_VARIABLE _matlab_result_version_call_error_batch
  652. TIMEOUT 120
  653. WORKING_DIRECTORY "${_matlab_temporary_folder}"
  654. ${devnull}
  655. )
  656. if(_matlab_result_version_call_batch MATCHES "timeout")
  657. if(MATLAB_FIND_DEBUG)
  658. message(WARNING "[MATLAB] Unable to determine the version of Matlab."
  659. " Matlab call with -batch option timed out after 120 seconds.")
  660. endif()
  661. return()
  662. endif()
  663. if(${_matlab_result_version_call_batch})
  664. if(MATLAB_FIND_DEBUG)
  665. message(WARNING "[MATLAB] Command executed \"${matlab_binary_program}\" -nosplash -nojvm ${_matlab_additional_commands} -logfile \"matlabVersionLog.cmaketmp\" -nodesktop -nodisplay -batch \"version, exit\"")
  666. message(WARNING "_matlab_version_from_cmd_dummy_batch (OUTPUT_VARIABLE): ${_matlab_version_from_cmd_dummy_batch}")
  667. message(WARNING "_matlab_result_version_call_batch (RESULT_VARIABLE): ${_matlab_result_version_call_batch}")
  668. message(WARNING "_matlab_result_version_call_error_batch (ERROR_VARIABLE): ${_matlab_result_version_call_error_batch}")
  669. message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call with -batch option returned with error ${_matlab_result_version_call_batch}.")
  670. endif()
  671. return()
  672. elseif(NOT ${_matlab_get_version_failed_with_r_option} AND NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  673. if(MATLAB_FIND_DEBUG)
  674. message(WARNING "[MATLAB] Unable to determine the version of Matlab. The log file does not exist.")
  675. endif()
  676. return()
  677. endif()
  678. endif()
  679. if(NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  680. # last resort check as some HPC with "module load matlab" not enacted fail to catch in earlier checks
  681. # and error CMake configure even if find_package(Matlab) is not REQUIRED
  682. if(MATLAB_FIND_DEBUG)
  683. message(WARNING "[MATLAB] Unable to determine the version of Matlab. The version log file does not exist.")
  684. endif()
  685. return()
  686. endif()
  687. # if successful, read back the log
  688. file(READ "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp" _matlab_version_from_cmd)
  689. file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  690. set(index -1)
  691. string(FIND "${_matlab_version_from_cmd}" "ans" index)
  692. if(index EQUAL -1)
  693. if(MATLAB_FIND_DEBUG)
  694. message(WARNING "[MATLAB] Cannot find the version of Matlab returned by the run.")
  695. endif()
  696. else()
  697. set(matlab_list_of_all_versions_tmp)
  698. string(SUBSTRING "${_matlab_version_from_cmd}" ${index} -1 substring_ans)
  699. string(
  700. REGEX MATCHALL "ans[\r\n\t ]*=[\r\n\t ]*'?([0-9]+(\\.[0-9]+)+)"
  701. matlab_versions_regex
  702. ${substring_ans})
  703. foreach(match IN LISTS matlab_versions_regex)
  704. string(
  705. REGEX MATCH "ans[\r\n\t ]*=[\r\n\t ]*'?(([0-9]+)(\\.([0-9]+))?)"
  706. current_match ${match})
  707. list(APPEND matlab_list_of_all_versions_tmp ${CMAKE_MATCH_1})
  708. endforeach()
  709. if(matlab_list_of_all_versions_tmp)
  710. list(REMOVE_DUPLICATES matlab_list_of_all_versions_tmp)
  711. endif()
  712. set(${matlab_list_versions} ${matlab_list_of_all_versions_tmp} PARENT_SCOPE)
  713. endif()
  714. endfunction()
  715. #[=======================================================================[.rst:
  716. .. command:: matlab_add_unit_test
  717. Adds a Matlab unit test to the test set of cmake/ctest.
  718. This command requires the component ``MAIN_PROGRAM`` and hence is not
  719. available for an MCR installation.
  720. The unit test uses the Matlab unittest framework (default, available
  721. starting Matlab 2013b+) except if the option ``NO_UNITTEST_FRAMEWORK``
  722. is given.
  723. The function expects one Matlab test script file to be given.
  724. In the case ``NO_UNITTEST_FRAMEWORK`` is given, the unittest script file
  725. should contain the script to be run, plus an exit command with the exit
  726. value. This exit value will be passed to the ctest framework (0 success,
  727. non 0 failure). Additional arguments accepted by :command:`add_test` can be
  728. passed through ``TEST_ARGS`` (eg. ``CONFIGURATION <config> ...``).
  729. .. code-block:: cmake
  730. matlab_add_unit_test(
  731. NAME <name>
  732. UNITTEST_FILE matlab_file_containing_unittest.m
  733. [CUSTOM_TEST_COMMAND matlab_command_to_run_as_test]
  734. [UNITTEST_PRECOMMAND matlab_command_to_run]
  735. [TIMEOUT timeout]
  736. [ADDITIONAL_PATH path1 [path2 ...]]
  737. [MATLAB_ADDITIONAL_STARTUP_OPTIONS option1 [option2 ...]]
  738. [TEST_ARGS arg1 [arg2 ...]]
  739. [NO_UNITTEST_FRAMEWORK]
  740. )
  741. Function Parameters:
  742. ``NAME``
  743. name of the unittest in ctest.
  744. ``UNITTEST_FILE``
  745. the matlab unittest file. Its path will be automatically
  746. added to the Matlab path.
  747. ``CUSTOM_TEST_COMMAND``
  748. Matlab script command to run as the test.
  749. If this is not set, then the following is run:
  750. ``runtests('matlab_file_name'), exit(max([ans(1,:).Failed]))``
  751. where ``matlab_file_name`` is the ``UNITTEST_FILE`` without the extension.
  752. ``UNITTEST_PRECOMMAND``
  753. Matlab script command to be ran before the file
  754. containing the test (eg. GPU device initialization based on CMake
  755. variables).
  756. ``TIMEOUT``
  757. the test timeout in seconds. Defaults to 180 seconds as the
  758. Matlab unit test may hang.
  759. ``ADDITIONAL_PATH``
  760. a list of paths to add to the Matlab path prior to
  761. running the unit test.
  762. ``MATLAB_ADDITIONAL_STARTUP_OPTIONS``
  763. a list of additional option in order
  764. to run Matlab from the command line.
  765. ``-nosplash -nodesktop -nodisplay`` are always added.
  766. ``TEST_ARGS``
  767. Additional options provided to the add_test command. These
  768. options are added to the default options (eg. "CONFIGURATIONS Release")
  769. ``NO_UNITTEST_FRAMEWORK``
  770. when set, indicates that the test should not
  771. use the unittest framework of Matlab (available for versions >= R2013a).
  772. ``WORKING_DIRECTORY``
  773. This will be the working directory for the test. If specified it will
  774. also be the output directory used for the log file of the test run.
  775. If not specified the temporary directory ``${CMAKE_BINARY_DIR}/Matlab`` will
  776. be used as the working directory and the log location.
  777. #]=======================================================================]
  778. function(matlab_add_unit_test)
  779. if(NOT Matlab_MAIN_PROGRAM)
  780. message(FATAL_ERROR "[MATLAB] This functionality needs the MAIN_PROGRAM component (not default)")
  781. endif()
  782. set(options NO_UNITTEST_FRAMEWORK)
  783. set(oneValueArgs NAME UNITTEST_FILE TIMEOUT WORKING_DIRECTORY
  784. UNITTEST_PRECOMMAND CUSTOM_TEST_COMMAND)
  785. set(multiValueArgs ADDITIONAL_PATH MATLAB_ADDITIONAL_STARTUP_OPTIONS TEST_ARGS)
  786. set(prefix _matlab_unittest_prefix)
  787. cmake_parse_arguments(PARSE_ARGV 0 ${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" )
  788. if(NOT ${prefix}_NAME)
  789. message(FATAL_ERROR "[MATLAB] The Matlab test name cannot be empty")
  790. endif()
  791. # The option to run a batch program with MATLAB changes depending on the MATLAB version
  792. # For MATLAB before R2019a (9.6), the only supported option is -r, afterwards the suggested option
  793. # is -batch as -r is deprecated
  794. set(maut_BATCH_OPTION "-r")
  795. if(NOT (Matlab_VERSION_STRING STREQUAL ""))
  796. if(Matlab_VERSION_STRING VERSION_GREATER_EQUAL "9.6")
  797. set(maut_BATCH_OPTION "-batch")
  798. endif()
  799. endif()
  800. add_test(NAME ${${prefix}_NAME}
  801. COMMAND ${CMAKE_COMMAND}
  802. "-Dtest_name=${${prefix}_NAME}"
  803. "-Dadditional_paths=${${prefix}_ADDITIONAL_PATH}"
  804. "-Dtest_timeout=${${prefix}_TIMEOUT}"
  805. "-Doutput_directory=${_matlab_temporary_folder}"
  806. "-Dworking_directory=${${prefix}_WORKING_DIRECTORY}"
  807. "-DMatlab_PROGRAM=${Matlab_MAIN_PROGRAM}"
  808. "-Dno_unittest_framework=${${prefix}_NO_UNITTEST_FRAMEWORK}"
  809. "-DMatlab_ADDITIONAL_STARTUP_OPTIONS=${${prefix}_MATLAB_ADDITIONAL_STARTUP_OPTIONS}"
  810. "-Dunittest_file_to_run=${${prefix}_UNITTEST_FILE}"
  811. "-Dcustom_Matlab_test_command=${${prefix}_CUSTOM_TEST_COMMAND}"
  812. "-Dcmd_to_run_before_test=${${prefix}_UNITTEST_PRECOMMAND}"
  813. "-Dmaut_BATCH_OPTION=${maut_BATCH_OPTION}"
  814. -P ${_FindMatlab_SELF_DIR}/MatlabTestsRedirect.cmake
  815. ${${prefix}_TEST_ARGS}
  816. ${${prefix}_UNPARSED_ARGUMENTS}
  817. )
  818. endfunction()
  819. #[=======================================================================[.rst:
  820. .. command:: matlab_add_mex
  821. Adds a Matlab MEX target.
  822. This commands compiles the given sources with the current tool-chain in
  823. order to produce a MEX file. The final name of the produced output may be
  824. specified, as well as additional link libraries, and a documentation entry
  825. for the MEX file. Remaining arguments of the call are passed to the
  826. :command:`add_library` or :command:`add_executable` command.
  827. .. code-block:: cmake
  828. matlab_add_mex(
  829. NAME <name>
  830. [EXECUTABLE | MODULE | SHARED]
  831. SRC src1 [src2 ...]
  832. [OUTPUT_NAME output_name]
  833. [DOCUMENTATION file.txt]
  834. [LINK_TO target1 target2 ...]
  835. [R2017b | R2018a]
  836. [EXCLUDE_FROM_ALL]
  837. [NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES]
  838. [...]
  839. )
  840. Function Parameters:
  841. ``NAME``
  842. name of the target.
  843. ``SRC``
  844. list of source files.
  845. ``LINK_TO``
  846. a list of additional link dependencies. The target links to ``libmex``
  847. and ``libmx`` by default, unless the
  848. ``NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES`` option is passed.
  849. ``OUTPUT_NAME``
  850. if given, overrides the default name. The default name is
  851. the name of the target without any prefix and
  852. with ``Matlab_MEX_EXTENSION`` suffix.
  853. ``DOCUMENTATION``
  854. if given, the file ``file.txt`` will be considered as
  855. being the documentation file for the MEX file. This file is copied into
  856. the same folder without any processing, with the same name as the final
  857. mex file, and with extension `.m`. In that case, typing ``help <name>``
  858. in Matlab prints the documentation contained in this file.
  859. ``R2017b`` or ``R2018a``
  860. .. versionadded:: 3.14
  861. May be given to specify the version of the C API
  862. to use: ``R2017b`` specifies the traditional (separate complex) C API,
  863. and corresponds to the ``-R2017b`` flag for the `mex` command. ``R2018a``
  864. specifies the new interleaved complex C API, and corresponds to the
  865. ``-R2018a`` flag for the `mex` command. Ignored if MATLAB version prior
  866. to R2018a. Defaults to ``R2017b``.
  867. ``MODULE`` or ``SHARED``
  868. .. versionadded:: 3.7
  869. May be given to specify the type of library to be
  870. created.
  871. ``EXECUTABLE``
  872. .. versionadded:: 3.7
  873. May be given to create an executable instead of
  874. a library. If no type is given explicitly, the type is ``SHARED``.
  875. ``EXCLUDE_FROM_ALL``
  876. This option has the same meaning as for :prop_tgt:`EXCLUDE_FROM_ALL` and
  877. is forwarded to :command:`add_library` or :command:`add_executable`
  878. commands.
  879. ``NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES``
  880. .. versionadded:: 3.24
  881. This option permits to disable the automatic linking of MATLAB
  882. libraries, so that only the libraries that are actually required can be
  883. linked via the ``LINK_TO`` option.
  884. The documentation file is not processed and should be in the following
  885. format:
  886. ::
  887. % This is the documentation
  888. function ret = mex_target_output_name(input1)
  889. #]=======================================================================]
  890. function(matlab_add_mex)
  891. set(options EXECUTABLE MODULE SHARED R2017b R2018a EXCLUDE_FROM_ALL NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES)
  892. set(oneValueArgs NAME DOCUMENTATION OUTPUT_NAME)
  893. set(multiValueArgs LINK_TO SRC)
  894. set(prefix _matlab_addmex_prefix)
  895. cmake_parse_arguments(${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  896. if(NOT ${prefix}_NAME)
  897. message(FATAL_ERROR "[MATLAB] The MEX target name cannot be empty")
  898. endif()
  899. if(NOT ${prefix}_OUTPUT_NAME)
  900. set(${prefix}_OUTPUT_NAME ${${prefix}_NAME})
  901. endif()
  902. if(NOT Matlab_VERSION_STRING VERSION_LESS "9.1") # For 9.1 (R2016b) and newer, add version source file
  903. # Compilers officially supported by Matlab 9.1 (R2016b):
  904. # MinGW 4.9, MSVC 2012, Intel C++ 2013, Xcode 6, GCC 4.9
  905. # These compilers definitely support the -w flag to suppress warnings.
  906. # Other compilers (Clang) may support the -w flag and can be added here.
  907. set(_Matlab_silenceable_compilers AppleClang Clang GNU Intel IntelLLVM MSVC)
  908. # Add the correct version file depending on which languages are enabled in the project
  909. if(CMAKE_C_COMPILER_LOADED)
  910. # If C is enabled, use the .c file as it will work fine also with C++
  911. set(MEX_VERSION_FILE "${Matlab_ROOT_DIR}/extern/version/c_mexapi_version.c")
  912. # Silence warnings for version source file
  913. if("${CMAKE_C_COMPILER_ID}" IN_LIST _Matlab_silenceable_compilers)
  914. set_source_files_properties("${MEX_VERSION_FILE}" PROPERTIES COMPILE_OPTIONS -w)
  915. endif()
  916. elseif(CMAKE_CXX_COMPILER_LOADED)
  917. # If C is not enabled, check if CXX is enabled and use the .cpp file
  918. # to avoid that the .c file is silently ignored
  919. set(MEX_VERSION_FILE "${Matlab_ROOT_DIR}/extern/version/cpp_mexapi_version.cpp")
  920. if("${CMAKE_CXX_COMPILER_ID}" IN_LIST _Matlab_silenceable_compilers)
  921. set_source_files_properties("${MEX_VERSION_FILE}" PROPERTIES COMPILE_OPTIONS -w)
  922. endif()
  923. else()
  924. # If neither C or CXX is enabled, warn because we cannot add the source.
  925. # TODO: add support for fortran mex files
  926. message(WARNING "[MATLAB] matlab_add_mex requires that at least C or CXX are enabled languages")
  927. endif()
  928. endif()
  929. # For 9.4 (R2018a) and newer, add API macro.
  930. # Add it for unknown versions too, just in case.
  931. if(NOT Matlab_VERSION_STRING VERSION_LESS "9.4"
  932. OR Matlab_VERSION_STRING STREQUAL "unknown")
  933. if(${${prefix}_R2018a})
  934. set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2018a")
  935. else()
  936. set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2017b")
  937. endif()
  938. endif()
  939. set(_option_EXCLUDE_FROM_ALL)
  940. if(${prefix}_EXCLUDE_FROM_ALL)
  941. set(_option_EXCLUDE_FROM_ALL "EXCLUDE_FROM_ALL")
  942. endif()
  943. if(${prefix}_EXECUTABLE)
  944. add_executable(${${prefix}_NAME}
  945. ${_option_EXCLUDE_FROM_ALL}
  946. ${${prefix}_SRC}
  947. ${MEX_VERSION_FILE}
  948. ${${prefix}_DOCUMENTATION}
  949. ${${prefix}_UNPARSED_ARGUMENTS})
  950. else()
  951. if(${prefix}_MODULE)
  952. set(type MODULE)
  953. else()
  954. set(type SHARED)
  955. endif()
  956. add_library(${${prefix}_NAME}
  957. ${type}
  958. ${_option_EXCLUDE_FROM_ALL}
  959. ${${prefix}_SRC}
  960. ${MEX_VERSION_FILE}
  961. ${${prefix}_DOCUMENTATION}
  962. ${${prefix}_UNPARSED_ARGUMENTS})
  963. endif()
  964. target_include_directories(${${prefix}_NAME} SYSTEM PRIVATE ${Matlab_INCLUDE_DIRS})
  965. if(NOT ${prefix}_NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES)
  966. if(Matlab_HAS_CPP_API)
  967. if(Matlab_ENGINE_LIBRARY)
  968. target_link_libraries(${${prefix}_NAME} ${Matlab_ENGINE_LIBRARY})
  969. endif()
  970. if(Matlab_DATAARRAY_LIBRARY)
  971. target_link_libraries(${${prefix}_NAME} ${Matlab_DATAARRAY_LIBRARY})
  972. endif()
  973. endif()
  974. target_link_libraries(${${prefix}_NAME} ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY})
  975. endif()
  976. target_link_libraries(${${prefix}_NAME} ${${prefix}_LINK_TO})
  977. set_target_properties(${${prefix}_NAME}
  978. PROPERTIES
  979. PREFIX ""
  980. OUTPUT_NAME ${${prefix}_OUTPUT_NAME}
  981. SUFFIX ".${Matlab_MEX_EXTENSION}")
  982. target_compile_definitions(${${prefix}_NAME} PRIVATE ${MEX_API_MACRO} MATLAB_MEX_FILE)
  983. # documentation
  984. if(NOT ${${prefix}_DOCUMENTATION} STREQUAL "")
  985. get_target_property(output_name ${${prefix}_NAME} OUTPUT_NAME)
  986. add_custom_command(
  987. TARGET ${${prefix}_NAME}
  988. PRE_BUILD
  989. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${${prefix}_DOCUMENTATION} $<TARGET_FILE_DIR:${${prefix}_NAME}>/${output_name}.m
  990. COMMENT "[MATLAB] Copy ${${prefix}_NAME} documentation file into the output folder"
  991. )
  992. endif() # documentation
  993. # entry point in the mex file + taking care of visibility and symbol clashes.
  994. if(WIN32)
  995. if (MSVC)
  996. string(APPEND _link_flags " /EXPORT:mexFunction")
  997. if(NOT Matlab_VERSION_STRING VERSION_LESS "9.1") # For 9.1 (R2016b) and newer, export version
  998. string(APPEND _link_flags " /EXPORT:mexfilerequiredapiversion")
  999. endif()
  1000. set_property(TARGET ${${prefix}_NAME} APPEND PROPERTY LINK_FLAGS ${_link_flags})
  1001. endif() # No other compiler currently supported on Windows.
  1002. set_property(TARGET ${${prefix}_NAME} PROPERTY
  1003. DEFINE_SYMBOL "DLL_EXPORT_SYM=__declspec(dllexport)")
  1004. else()
  1005. if(Matlab_VERSION_STRING VERSION_LESS "9.1") # For versions prior to 9.1 (R2016b)
  1006. set(_ver_map_files ${Matlab_EXTERN_LIBRARY_DIR}/mexFunction.map)
  1007. else() # For 9.1 (R2016b) and newer
  1008. set(_ver_map_files ${Matlab_EXTERN_LIBRARY_DIR}/c_exportsmexfileversion.map)
  1009. endif()
  1010. if(NOT Matlab_VERSION_STRING VERSION_LESS "9.5") # For 9.5 (R2018b) (and newer?)
  1011. target_compile_options(${${prefix}_NAME} PRIVATE "-fvisibility=default")
  1012. # This one is weird, it might be a bug in <mex.h> for R2018b. When compiling with
  1013. # -fvisibility=hidden, the symbol `mexFunction` cannot be exported. Reading the
  1014. # source code for <mex.h>, it seems that the preprocessor macro `MW_NEEDS_VERSION_H`
  1015. # needs to be defined for `__attribute__((visibility("default")))` to be added
  1016. # in front of the declaration of `mexFunction`. In previous versions of MATLAB this
  1017. # was not the case, there `DLL_EXPORT_SYM` needed to be defined.
  1018. # Adding `-fvisibility=hidden` to the `mex` command causes the build to fail.
  1019. # TODO: Check that this is still necessary in R2019a when it comes out.
  1020. endif()
  1021. if(APPLE)
  1022. if(Matlab_HAS_CPP_API)
  1023. list(APPEND _ver_map_files ${Matlab_EXTERN_LIBRARY_DIR}/cppMexFunction.map) # This one doesn't exist on Linux
  1024. string(APPEND _link_flags " -Wl,-U,_mexCreateMexFunction -Wl,-U,_mexDestroyMexFunction -Wl,-U,_mexFunctionAdapter")
  1025. # On MacOS, the MEX command adds the above, without it the link breaks
  1026. # because we indiscriminately use "cppMexFunction.map" even for C API MEX-files.
  1027. endif()
  1028. set(_export_flag_name -exported_symbols_list)
  1029. else() # Linux
  1030. if(Threads_FOUND)
  1031. target_link_libraries(${${prefix}_NAME} Threads::Threads)
  1032. endif()
  1033. string(APPEND _link_flags " -Wl,--as-needed")
  1034. set(_export_flag_name --version-script)
  1035. endif()
  1036. foreach(_file IN LISTS _ver_map_files)
  1037. string(APPEND _link_flags " -Wl,${_export_flag_name},${_file}")
  1038. endforeach()
  1039. # The `mex` command doesn't add this define. It is specified here in order
  1040. # to export the symbol in case the client code decides to hide its symbols
  1041. set_target_properties(${${prefix}_NAME}
  1042. PROPERTIES
  1043. DEFINE_SYMBOL "DLL_EXPORT_SYM=__attribute__((visibility(\"default\")))"
  1044. LINK_FLAGS "${_link_flags}"
  1045. )
  1046. endif()
  1047. endfunction()
  1048. # (internal)
  1049. # Used to get the version of matlab, using caching. This basically transforms the
  1050. # output of the root list, with possible unknown version, to a version
  1051. # This can possibly run Matlab for extracting the version.
  1052. function(_Matlab_get_version_from_root matlab_root matlab_or_mcr matlab_known_version matlab_final_version)
  1053. # if the version is not trivial, we query matlab (if not MCR) for that
  1054. # we keep track of the location of matlab that induced this version
  1055. #if(NOT DEFINED Matlab_PROG_VERSION_STRING_AUTO_DETECT)
  1056. # set(Matlab_PROG_VERSION_STRING_AUTO_DETECT "" CACHE INTERNAL "internal matlab location for the discovered version")
  1057. #endif()
  1058. if(NOT matlab_known_version STREQUAL "NOTFOUND")
  1059. # the version is known, we just return it
  1060. set(${matlab_final_version} ${matlab_known_version} PARENT_SCOPE)
  1061. set(Matlab_VERSION_STRING_INTERNAL ${matlab_known_version} CACHE INTERNAL "Matlab version (automatically determined)")
  1062. return()
  1063. endif()
  1064. if(matlab_or_mcr STREQUAL "UNKNOWN")
  1065. if(MATLAB_FIND_DEBUG)
  1066. message(WARNING "[MATLAB] Determining Matlab or MCR")
  1067. endif()
  1068. if(EXISTS "${matlab_root}/appdata/version.xml")
  1069. # we inspect the application version.xml file that contains the product information
  1070. file(READ "${matlab_root}/appdata/version.xml" productinfo_string)
  1071. string(REGEX MATCH "<installedProductData.*displayedString=\"([a-zA-Z ]+)\".*/>"
  1072. product_reg_match
  1073. ${productinfo_string}
  1074. )
  1075. # default fallback to Matlab
  1076. set(matlab_or_mcr "MATLAB")
  1077. if(NOT CMAKE_MATCH_1 STREQUAL "")
  1078. string(TOLOWER "${CMAKE_MATCH_1}" product_reg_match)
  1079. if(product_reg_match STREQUAL "matlab runtime")
  1080. set(matlab_or_mcr "MCR")
  1081. endif()
  1082. endif()
  1083. endif()
  1084. if(MATLAB_FIND_DEBUG)
  1085. message(WARNING "[MATLAB] '${matlab_root}' contains the '${matlab_or_mcr}'")
  1086. endif()
  1087. endif()
  1088. # UNKNOWN is the default behavior in case we
  1089. # - have an erroneous matlab_root
  1090. # - have an initial 'UNKNOWN'
  1091. if(matlab_or_mcr STREQUAL "MATLAB" OR matlab_or_mcr STREQUAL "UNKNOWN")
  1092. # MATLAB versions
  1093. set(_matlab_current_program ${Matlab_MAIN_PROGRAM})
  1094. # do we already have a matlab program?
  1095. if(NOT _matlab_current_program)
  1096. set(_find_matlab_options)
  1097. if(IS_DIRECTORY "${matlab_root}")
  1098. set(_find_matlab_options PATHS ${matlab_root} ${matlab_root}/bin NO_DEFAULT_PATH)
  1099. endif()
  1100. find_program(
  1101. _matlab_current_program
  1102. NAMES matlab
  1103. ${_find_matlab_options}
  1104. DOC "Matlab main program"
  1105. )
  1106. endif()
  1107. if(NOT _matlab_current_program)
  1108. # if not found, clear the dependent variables
  1109. if(MATLAB_FIND_DEBUG)
  1110. message(WARNING "[MATLAB] Cannot find the main matlab program under ${matlab_root}")
  1111. endif()
  1112. set(Matlab_PROG_VERSION_STRING_AUTO_DETECT "" CACHE INTERNAL "internal matlab location for the discovered version")
  1113. set(Matlab_VERSION_STRING_INTERNAL "" CACHE INTERNAL "internal matlab location for the discovered version")
  1114. unset(_matlab_current_program)
  1115. unset(_matlab_current_program CACHE)
  1116. return()
  1117. endif()
  1118. # full real path for path comparison
  1119. get_filename_component(_matlab_main_real_path_tmp "${_matlab_current_program}" REALPATH)
  1120. unset(_matlab_current_program)
  1121. unset(_matlab_current_program CACHE)
  1122. # is it the same as the previous one?
  1123. if(_matlab_main_real_path_tmp STREQUAL Matlab_PROG_VERSION_STRING_AUTO_DETECT)
  1124. set(${matlab_final_version} ${Matlab_VERSION_STRING_INTERNAL} PARENT_SCOPE)
  1125. return()
  1126. endif()
  1127. # update the location of the program
  1128. set(Matlab_PROG_VERSION_STRING_AUTO_DETECT
  1129. ${_matlab_main_real_path_tmp}
  1130. CACHE INTERNAL "internal matlab location for the discovered version")
  1131. _Matlab_VersionInfoXML("${matlab_root}" _matlab_version_tmp)
  1132. if(NOT "${_matlab_version_tmp}" STREQUAL "unknown")
  1133. # at least back to R2016 VersionInfo.xml exists
  1134. set(matlab_list_of_all_versions ${_matlab_version_tmp})
  1135. else()
  1136. # time consuming, less stable way to find Matlab version by running Matlab
  1137. matlab_get_version_from_matlab_run("${Matlab_PROG_VERSION_STRING_AUTO_DETECT}" matlab_list_of_all_versions)
  1138. endif()
  1139. list(LENGTH matlab_list_of_all_versions list_of_all_versions_length)
  1140. if(list_of_all_versions_length GREATER 0)
  1141. list(GET matlab_list_of_all_versions 0 _matlab_version_tmp)
  1142. else()
  1143. set(_matlab_version_tmp "unknown")
  1144. endif()
  1145. # set the version into the cache
  1146. set(Matlab_VERSION_STRING_INTERNAL ${_matlab_version_tmp} CACHE INTERNAL "Matlab version (automatically determined)")
  1147. # warning, just in case several versions found (should not happen)
  1148. if((list_of_all_versions_length GREATER 1) AND MATLAB_FIND_DEBUG)
  1149. message(WARNING "[MATLAB] Found several versions, taking the first one (versions found ${matlab_list_of_all_versions})")
  1150. endif()
  1151. else()
  1152. # MCR
  1153. # we cannot run anything in order to extract the version. We assume that the file
  1154. # VersionInfo.xml exists under the MatlabRoot, we look for it and extract the version from there
  1155. _Matlab_VersionInfoXML("${matlab_root}" _matlab_version_tmp)
  1156. if(NOT "${_matlab_version_tmp}" STREQUAL "unknown")
  1157. set(Matlab_VERSION_STRING_INTERNAL ${_matlab_version_tmp} CACHE INTERNAL "Matlab version (automatically determined)")
  1158. endif()
  1159. endif() # Matlab or MCR
  1160. # return the updated value
  1161. set(${matlab_final_version} ${Matlab_VERSION_STRING_INTERNAL} PARENT_SCOPE)
  1162. endfunction()
  1163. function(_Matlab_VersionInfoXML matlab_root _version)
  1164. set(_ver "unknown")
  1165. set(_XMLfile ${matlab_root}/VersionInfo.xml)
  1166. if(EXISTS ${_XMLfile})
  1167. file(READ ${_XMLfile} versioninfo_string)
  1168. # parses "<version>23.2.0.2365128</version>"
  1169. if(versioninfo_string MATCHES "<version>([0-9]+(\\.[0-9]+)+)</version>")
  1170. set(_ver "${CMAKE_MATCH_1}")
  1171. endif()
  1172. endif()
  1173. set(${_version} ${_ver} PARENT_SCOPE)
  1174. endfunction()
  1175. # Utility function for finding Matlab or MCR on Win32
  1176. function(_Matlab_find_instances_win32 matlab_roots)
  1177. # On WIN32, we look for Matlab installation in the registry
  1178. # if unsuccessful, we look for all known revision and filter the existing
  1179. # ones.
  1180. # testing if we are able to extract the needed information from the registry
  1181. set(_matlab_versions_from_registry)
  1182. matlab_extract_all_installed_versions_from_registry(_matlab_versions_from_registry ${ARGN})
  1183. # the returned list is empty, doing the search on all known versions
  1184. if(NOT _matlab_versions_from_registry)
  1185. if(MATLAB_FIND_DEBUG)
  1186. message(STATUS "[MATLAB] Search for Matlab from the registry unsuccessful, testing all supported versions")
  1187. endif()
  1188. extract_matlab_versions_from_registry_brute_force(_matlab_versions_from_registry)
  1189. endif()
  1190. # filtering the results with the registry keys
  1191. matlab_get_all_valid_matlab_roots_from_registry("${_matlab_versions_from_registry}" _matlab_possible_roots ${ARGN})
  1192. set(${matlab_roots} ${_matlab_possible_roots} PARENT_SCOPE)
  1193. endfunction()
  1194. # Utility function for finding Matlab or MCR on macOS
  1195. function(_Matlab_find_instances_macos matlab_roots)
  1196. set(_matlab_possible_roots)
  1197. # on mac, we look for the /Application paths
  1198. # this corresponds to the behavior on Windows. On Linux, we do not have
  1199. # any other guess.
  1200. matlab_get_supported_releases(_matlab_releases)
  1201. if(MATLAB_FIND_DEBUG)
  1202. message(STATUS "[MATLAB] Matlab supported versions ${_matlab_releases}. If more version should be supported "
  1203. "the variable MATLAB_ADDITIONAL_VERSIONS can be set according to the documentation")
  1204. endif()
  1205. foreach(_matlab_current_release IN LISTS _matlab_releases)
  1206. matlab_get_version_from_release_name("${_matlab_current_release}" _matlab_current_version)
  1207. string(REPLACE "." "" _matlab_current_version_without_dot "${_matlab_current_version}")
  1208. set(_matlab_base_path "/Applications/MATLAB_${_matlab_current_release}.app")
  1209. _Matlab_VersionInfoXML("${_matlab_base_path}" _matlab_version_tmp)
  1210. if(NOT "${_matlab_version_tmp}" STREQUAL "unknown")
  1211. set(_matlab_current_version ${_matlab_version_tmp})
  1212. endif()
  1213. # Check Matlab, has precedence over MCR
  1214. if(IS_DIRECTORY "${_matlab_base_path}")
  1215. if(MATLAB_FIND_DEBUG)
  1216. message(STATUS "[MATLAB] Found version ${_matlab_current_release} (${_matlab_current_version}) in ${_matlab_base_path}")
  1217. endif()
  1218. list(APPEND _matlab_possible_roots "MATLAB" ${_matlab_current_version} ${_matlab_base_path})
  1219. endif()
  1220. # Checks MCR
  1221. set(_mcr_path "/Applications/MATLAB/MATLAB_Runtime/v${_matlab_current_version_without_dot}")
  1222. if(IS_DIRECTORY "${_mcr_path}")
  1223. if(MATLAB_FIND_DEBUG)
  1224. message(STATUS "[MATLAB] Found MCR version ${_matlab_current_release} (${_matlab_current_version}) in ${_mcr_path}")
  1225. endif()
  1226. list(APPEND _matlab_possible_roots "MCR" ${_matlab_current_version} ${_mcr_path})
  1227. endif()
  1228. endforeach()
  1229. set(${matlab_roots} ${_matlab_possible_roots} PARENT_SCOPE)
  1230. endfunction()
  1231. # Utility function for finding Matlab or MCR from the PATH
  1232. function(_Matlab_find_instances_from_path matlab_roots)
  1233. set(_matlab_possible_roots)
  1234. # At this point, we have no other choice than trying to find it from PATH.
  1235. # If set by the user, this won't change.
  1236. find_program(
  1237. _matlab_main_tmp
  1238. NAMES matlab)
  1239. if(_matlab_main_tmp)
  1240. # we then populate the list of roots, with empty version
  1241. if(MATLAB_FIND_DEBUG)
  1242. message(STATUS "[MATLAB] matlab found from PATH: ${_matlab_main_tmp}")
  1243. endif()
  1244. # resolve symlinks
  1245. get_filename_component(_matlab_current_location "${_matlab_main_tmp}" REALPATH)
  1246. # get the directory (the command below has to be run twice)
  1247. # this will be the matlab root
  1248. get_filename_component(_matlab_current_location "${_matlab_current_location}" DIRECTORY)
  1249. get_filename_component(_matlab_current_location "${_matlab_current_location}" DIRECTORY) # Matlab should be in bin
  1250. # We found the Matlab program
  1251. list(APPEND _matlab_possible_roots "MATLAB" "NOTFOUND" ${_matlab_current_location})
  1252. # we remove this from the CACHE
  1253. unset(_matlab_main_tmp CACHE)
  1254. else()
  1255. find_program(
  1256. _matlab_mex_tmp
  1257. NAMES mex)
  1258. if(_matlab_mex_tmp)
  1259. # we then populate the list of roots, with empty version
  1260. if(MATLAB_FIND_DEBUG)
  1261. message(STATUS "[MATLAB] mex compiler found from PATH: ${_matlab_mex_tmp}")
  1262. endif()
  1263. # resolve symlinks
  1264. get_filename_component(_mex_current_location "${_matlab_mex_tmp}" REALPATH)
  1265. # get the directory (the command below has to be run twice)
  1266. # this will be the matlab root
  1267. get_filename_component(_mex_current_location "${_mex_current_location}" DIRECTORY)
  1268. get_filename_component(_mex_current_location "${_mex_current_location}" DIRECTORY) # Matlab Runtime mex compiler should be in bin
  1269. # We found the Matlab program
  1270. list(APPEND _matlab_possible_roots "MCR" "NOTFOUND" ${_mex_current_location})
  1271. unset(_matlab_mex_tmp CACHE)
  1272. else()
  1273. if(MATLAB_FIND_DEBUG)
  1274. message(STATUS "[MATLAB] mex compiler not found")
  1275. endif()
  1276. endif()
  1277. endif()
  1278. set(${matlab_roots} ${_matlab_possible_roots} PARENT_SCOPE)
  1279. endfunction()
  1280. # ###################################
  1281. # Exploring the possible Matlab_ROOTS
  1282. # this variable will get all Matlab installations found in the current system.
  1283. set(_matlab_possible_roots)
  1284. if(NOT DEFINED Matlab_ROOT AND DEFINED ENV{Matlab_ROOT})
  1285. set(Matlab_ROOT $ENV{Matlab_ROOT})
  1286. endif()
  1287. if(DEFINED Matlab_ROOT)
  1288. set(Matlab_ROOT_DIR ${Matlab_ROOT})
  1289. endif()
  1290. if(Matlab_ROOT_DIR)
  1291. # if the user specifies a possible root, we keep this one
  1292. if(NOT IS_DIRECTORY "${Matlab_ROOT_DIR}")
  1293. # if Matlab_ROOT_DIR specified but erroneous
  1294. if(MATLAB_FIND_DEBUG)
  1295. message(WARNING "[MATLAB] the specified path for Matlab_ROOT_DIR does not exist (${Matlab_ROOT_DIR})")
  1296. endif()
  1297. else()
  1298. # NOTFOUND indicates the code below to search for the version automatically
  1299. if("${Matlab_VERSION_STRING_INTERNAL}" STREQUAL "")
  1300. list(APPEND _matlab_possible_roots "UNKNOWN" "NOTFOUND" ${Matlab_ROOT_DIR}) # empty version, empty MCR/Matlab indication
  1301. else()
  1302. list(APPEND _matlab_possible_roots "UNKNOWN" ${Matlab_VERSION_STRING_INTERNAL} ${Matlab_ROOT_DIR}) # cached version
  1303. endif()
  1304. endif()
  1305. else()
  1306. # if the user does not specify the possible installation root, we look for
  1307. # one installation using the appropriate heuristics.
  1308. # There is apparently no standard way on Linux.
  1309. if(CMAKE_HOST_WIN32)
  1310. if(NOT DEFINED Matlab_FIND_REGISTRY_VIEW)
  1311. set(Matlab_FIND_REGISTRY_VIEW TARGET)
  1312. endif()
  1313. _Matlab_find_instances_win32(_matlab_possible_roots_win32 REGISTRY_VIEW ${Matlab_FIND_REGISTRY_VIEW})
  1314. list(APPEND _matlab_possible_roots ${_matlab_possible_roots_win32})
  1315. elseif(APPLE)
  1316. _Matlab_find_instances_macos(_matlab_possible_roots_macos)
  1317. list(APPEND _matlab_possible_roots ${_matlab_possible_roots_macos})
  1318. endif()
  1319. endif()
  1320. list(LENGTH _matlab_possible_roots _numbers_of_matlab_roots)
  1321. if(_numbers_of_matlab_roots EQUAL 0)
  1322. # if we have not found anything, we fall back on the PATH
  1323. _Matlab_find_instances_from_path(_matlab_possible_roots)
  1324. endif()
  1325. if(MATLAB_FIND_DEBUG)
  1326. message(STATUS "[MATLAB] Matlab root folders are ${_matlab_possible_roots}")
  1327. endif()
  1328. # take the first possible Matlab root
  1329. list(LENGTH _matlab_possible_roots _numbers_of_matlab_roots)
  1330. set(Matlab_VERSION_STRING "NOTFOUND")
  1331. set(Matlab_Or_MCR "UNKNOWN")
  1332. if(_numbers_of_matlab_roots GREATER 0)
  1333. set(_list_index -1)
  1334. foreach(_matlab_root_index RANGE 1 ${_numbers_of_matlab_roots} 3)
  1335. list(GET _matlab_possible_roots ${_matlab_root_index} _matlab_root_version)
  1336. find_package_check_version(${_matlab_root_version} _matlab_version_ok HANDLE_VERSION_RANGE)
  1337. if(_matlab_version_ok)
  1338. set(_list_index ${_matlab_root_index})
  1339. break()
  1340. endif()
  1341. endforeach()
  1342. if(_list_index LESS 0)
  1343. set(_list_index 1)
  1344. endif()
  1345. math(EXPR _matlab_or_mcr_index "${_list_index} - 1")
  1346. math(EXPR _matlab_root_dir_index "${_list_index} + 1")
  1347. list(GET _matlab_possible_roots ${_matlab_or_mcr_index} Matlab_Or_MCR)
  1348. list(GET _matlab_possible_roots ${_list_index} Matlab_VERSION_STRING)
  1349. list(GET _matlab_possible_roots ${_matlab_root_dir_index} Matlab_ROOT_DIR)
  1350. # adding a warning in case of ambiguity
  1351. if(_numbers_of_matlab_roots GREATER 3 AND NOT Matlab_FIND_VERSION_EXACT AND MATLAB_FIND_DEBUG)
  1352. message(WARNING "[MATLAB] Found several distributions of Matlab. Setting the current version to ${Matlab_VERSION_STRING} (located ${Matlab_ROOT_DIR})."
  1353. " If this is not the desired behavior, use the EXACT keyword or provide the -DMatlab_ROOT_DIR=... on the command line")
  1354. endif()
  1355. endif()
  1356. # check if the root changed wrt. the previous defined one, if so
  1357. # clear all the cached variables for being able to reconfigure properly
  1358. if(DEFINED Matlab_ROOT_DIR_LAST_CACHED)
  1359. if(NOT Matlab_ROOT_DIR_LAST_CACHED STREQUAL Matlab_ROOT_DIR)
  1360. set(_Matlab_cached_vars
  1361. Matlab_VERSION_STRING
  1362. Matlab_INCLUDE_DIRS
  1363. Matlab_MEX_LIBRARY
  1364. Matlab_MEX_COMPILER
  1365. Matlab_MCC_COMPILER
  1366. Matlab_MAIN_PROGRAM
  1367. Matlab_MX_LIBRARY
  1368. Matlab_ENG_LIBRARY
  1369. Matlab_MAT_LIBRARY
  1370. Matlab_ENGINE_LIBRARY
  1371. Matlab_DATAARRAY_LIBRARY
  1372. Matlab_MEX_EXTENSION
  1373. Matlab_SIMULINK_INCLUDE_DIR
  1374. # internal
  1375. Matlab_MEXEXTENSIONS_PROG
  1376. Matlab_ROOT_DIR_LAST_CACHED
  1377. #Matlab_PROG_VERSION_STRING_AUTO_DETECT
  1378. #Matlab_VERSION_STRING_INTERNAL
  1379. )
  1380. foreach(_var IN LISTS _Matlab_cached_vars)
  1381. if(DEFINED ${_var})
  1382. unset(${_var} CACHE)
  1383. endif()
  1384. endforeach()
  1385. endif()
  1386. endif()
  1387. set(Matlab_ROOT_DIR_LAST_CACHED ${Matlab_ROOT_DIR} CACHE INTERNAL "last Matlab root dir location")
  1388. set(Matlab_ROOT_DIR ${Matlab_ROOT_DIR} CACHE PATH "Matlab installation root path" FORCE)
  1389. # Fix the version, in case this one is NOTFOUND
  1390. _Matlab_get_version_from_root(
  1391. "${Matlab_ROOT_DIR}"
  1392. "${Matlab_Or_MCR}"
  1393. ${Matlab_VERSION_STRING}
  1394. Matlab_VERSION_STRING
  1395. )
  1396. if(MATLAB_FIND_DEBUG)
  1397. message(STATUS "[MATLAB] Current version is ${Matlab_VERSION_STRING} located ${Matlab_ROOT_DIR}")
  1398. endif()
  1399. # MATLAB 9.4 (R2018a) and newer have a new C++ API
  1400. # This API pulls additional required libraries.
  1401. if(NOT ${Matlab_VERSION_STRING} VERSION_LESS "9.4")
  1402. set(Matlab_HAS_CPP_API 1)
  1403. endif()
  1404. if(Matlab_ROOT_DIR)
  1405. file(TO_CMAKE_PATH ${Matlab_ROOT_DIR} Matlab_ROOT_DIR)
  1406. endif()
  1407. if(NOT DEFINED Matlab_MEX_EXTENSION)
  1408. set(_matlab_mex_extension "")
  1409. matlab_get_mex_suffix("${Matlab_ROOT_DIR}" _matlab_mex_extension)
  1410. # This variable goes to the cache.
  1411. set(Matlab_MEX_EXTENSION ${_matlab_mex_extension} CACHE STRING "Extensions for the mex targets (automatically given by Matlab)")
  1412. unset(_matlab_mex_extension)
  1413. endif()
  1414. if(APPLE)
  1415. set(_matlab_bin_prefix "mac") # i should be for intel
  1416. set(_matlab_bin_suffix_32bits "i")
  1417. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" AND Matlab_MEX_EXTENSION MATCHES "a64$")
  1418. # native Apple Silicon Matlab
  1419. set(_matlab_bin_suffix_64bits "a64")
  1420. else()
  1421. # Intel Mac OR Apple Silicon using Rosetta for Matlab
  1422. set(_matlab_bin_suffix_64bits "i64")
  1423. endif()
  1424. elseif(UNIX)
  1425. set(_matlab_bin_prefix "gln")
  1426. set(_matlab_bin_suffix_32bits "x86")
  1427. set(_matlab_bin_suffix_64bits "xa64")
  1428. else()
  1429. set(_matlab_bin_prefix "win")
  1430. set(_matlab_bin_suffix_32bits "32")
  1431. set(_matlab_bin_suffix_64bits "64")
  1432. endif()
  1433. set(MATLAB_INCLUDE_DIR_TO_LOOK ${Matlab_ROOT_DIR}/extern/include)
  1434. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  1435. set(_matlab_current_suffix ${_matlab_bin_suffix_32bits})
  1436. else()
  1437. set(_matlab_current_suffix ${_matlab_bin_suffix_64bits})
  1438. endif()
  1439. set(Matlab_BINARIES_DIR
  1440. ${Matlab_ROOT_DIR}/bin/${_matlab_bin_prefix}${_matlab_current_suffix})
  1441. set(Matlab_EXTERN_LIBRARY_DIR
  1442. ${Matlab_ROOT_DIR}/extern/lib/${_matlab_bin_prefix}${_matlab_current_suffix})
  1443. set(Matlab_EXTERN_BINARIES_DIR
  1444. ${Matlab_ROOT_DIR}/extern/bin/${_matlab_bin_prefix}${_matlab_current_suffix})
  1445. if(WIN32)
  1446. if(MINGW)
  1447. set(_matlab_lib_dir_for_search ${Matlab_EXTERN_LIBRARY_DIR}/mingw64)
  1448. else()
  1449. set(_matlab_lib_dir_for_search ${Matlab_EXTERN_LIBRARY_DIR}/microsoft)
  1450. endif()
  1451. set(_matlab_lib_prefix_for_search "lib")
  1452. else()
  1453. set(_matlab_lib_dir_for_search ${Matlab_BINARIES_DIR} ${Matlab_EXTERN_BINARIES_DIR})
  1454. set(_matlab_lib_prefix_for_search "lib")
  1455. endif()
  1456. if(MATLAB_FIND_DEBUG)
  1457. message(STATUS "[MATLAB] _matlab_lib_prefix_for_search = ${_matlab_lib_prefix_for_search} | _matlab_lib_dir_for_search = ${_matlab_lib_dir_for_search}")
  1458. endif()
  1459. # internal
  1460. # This small stub around find_library is to prevent any pollution of CMAKE_FIND_LIBRARY_PREFIXES in the global scope.
  1461. # This is the function to be used below instead of the find_library directives.
  1462. function(_Matlab_find_library _matlab_library_prefix)
  1463. list(APPEND CMAKE_FIND_LIBRARY_PREFIXES ${_matlab_library_prefix})
  1464. find_library(${ARGN})
  1465. endfunction()
  1466. set(_matlab_required_variables)
  1467. # Order is as follow:
  1468. # - unconditionally required libraries/headers first
  1469. # - then library components
  1470. # - then program components
  1471. # the MEX library/header are required
  1472. find_path(
  1473. Matlab_INCLUDE_DIRS
  1474. NAMES mex.h matrix.h
  1475. PATHS ${MATLAB_INCLUDE_DIR_TO_LOOK}
  1476. NO_DEFAULT_PATH
  1477. )
  1478. list(APPEND _matlab_required_variables Matlab_INCLUDE_DIRS)
  1479. if(Matlab_Or_MCR STREQUAL "MATLAB" OR Matlab_Or_MCR STREQUAL "UNKNOWN")
  1480. _Matlab_find_library(
  1481. ${_matlab_lib_prefix_for_search}
  1482. Matlab_MEX_LIBRARY
  1483. NAMES mex
  1484. PATHS ${_matlab_lib_dir_for_search}
  1485. NO_DEFAULT_PATH
  1486. )
  1487. list(APPEND _matlab_required_variables Matlab_MEX_LIBRARY)
  1488. # the MEX extension is required
  1489. list(APPEND _matlab_required_variables Matlab_MEX_EXTENSION)
  1490. # the matlab root is required
  1491. list(APPEND _matlab_required_variables Matlab_ROOT_DIR)
  1492. # The MX library is required
  1493. _Matlab_find_library(
  1494. ${_matlab_lib_prefix_for_search}
  1495. Matlab_MX_LIBRARY
  1496. NAMES mx
  1497. PATHS ${_matlab_lib_dir_for_search}
  1498. NO_DEFAULT_PATH
  1499. )
  1500. list(APPEND _matlab_required_variables Matlab_MX_LIBRARY)
  1501. if(Matlab_MX_LIBRARY)
  1502. set(Matlab_MX_LIBRARY_FOUND TRUE)
  1503. endif()
  1504. endif()
  1505. if(Matlab_HAS_CPP_API)
  1506. # The MatlabEngine library is required for R2018a+
  1507. _Matlab_find_library(
  1508. ${_matlab_lib_prefix_for_search}
  1509. Matlab_ENGINE_LIBRARY
  1510. NAMES MatlabEngine
  1511. PATHS ${_matlab_lib_dir_for_search}
  1512. DOC "MatlabEngine Library"
  1513. NO_DEFAULT_PATH
  1514. )
  1515. if(Matlab_ENGINE_LIBRARY)
  1516. set(Matlab_ENGINE_LIBRARY_FOUND TRUE)
  1517. endif()
  1518. # The MatlabDataArray library is required for R2018a+
  1519. _Matlab_find_library(
  1520. ${_matlab_lib_prefix_for_search}
  1521. Matlab_DATAARRAY_LIBRARY
  1522. NAMES MatlabDataArray
  1523. PATHS ${_matlab_lib_dir_for_search}
  1524. DOC "MatlabDataArray Library"
  1525. NO_DEFAULT_PATH
  1526. )
  1527. if(Matlab_DATAARRAY_LIBRARY)
  1528. set(Matlab_DATAARRAY_LIBRARY_FOUND TRUE)
  1529. endif()
  1530. endif()
  1531. # Component ENG library
  1532. if("ENG_LIBRARY" IN_LIST Matlab_FIND_COMPONENTS)
  1533. _Matlab_find_library(
  1534. ${_matlab_lib_prefix_for_search}
  1535. Matlab_ENG_LIBRARY
  1536. NAMES eng
  1537. PATHS ${_matlab_lib_dir_for_search}
  1538. NO_DEFAULT_PATH
  1539. )
  1540. if(Matlab_ENG_LIBRARY)
  1541. set(Matlab_ENG_LIBRARY_FOUND TRUE)
  1542. endif()
  1543. endif()
  1544. # Component MAT library
  1545. if("MAT_LIBRARY" IN_LIST Matlab_FIND_COMPONENTS)
  1546. _Matlab_find_library(
  1547. ${_matlab_lib_prefix_for_search}
  1548. Matlab_MAT_LIBRARY
  1549. NAMES mat
  1550. PATHS ${_matlab_lib_dir_for_search}
  1551. NO_DEFAULT_PATH
  1552. )
  1553. if(Matlab_MAT_LIBRARY)
  1554. set(Matlab_MAT_LIBRARY_FOUND TRUE)
  1555. endif()
  1556. endif()
  1557. # Component Simulink
  1558. if("SIMULINK" IN_LIST Matlab_FIND_COMPONENTS)
  1559. find_path(
  1560. Matlab_SIMULINK_INCLUDE_DIR
  1561. NAMES simstruc.h
  1562. PATHS "${Matlab_ROOT_DIR}/simulink/include"
  1563. NO_DEFAULT_PATH
  1564. )
  1565. if(Matlab_SIMULINK_INCLUDE_DIR)
  1566. set(Matlab_SIMULINK_FOUND TRUE)
  1567. list(APPEND Matlab_INCLUDE_DIRS "${Matlab_SIMULINK_INCLUDE_DIR}")
  1568. endif()
  1569. endif()
  1570. # component Matlab program
  1571. if("MAIN_PROGRAM" IN_LIST Matlab_FIND_COMPONENTS)
  1572. find_program(
  1573. Matlab_MAIN_PROGRAM
  1574. NAMES matlab
  1575. PATHS ${Matlab_ROOT_DIR} ${Matlab_ROOT_DIR}/bin
  1576. DOC "Matlab main program"
  1577. NO_DEFAULT_PATH
  1578. )
  1579. if(Matlab_MAIN_PROGRAM)
  1580. set(Matlab_MAIN_PROGRAM_FOUND TRUE)
  1581. endif()
  1582. endif()
  1583. # component Mex Compiler
  1584. if("MEX_COMPILER" IN_LIST Matlab_FIND_COMPONENTS)
  1585. find_program(
  1586. Matlab_MEX_COMPILER
  1587. NAMES "mex"
  1588. PATHS ${Matlab_BINARIES_DIR}
  1589. DOC "Matlab MEX compiler"
  1590. NO_DEFAULT_PATH
  1591. )
  1592. if(Matlab_MEX_COMPILER)
  1593. set(Matlab_MEX_COMPILER_FOUND TRUE)
  1594. endif()
  1595. endif()
  1596. # component MCC Compiler
  1597. if("MCC_COMPILER" IN_LIST Matlab_FIND_COMPONENTS)
  1598. find_program(
  1599. Matlab_MCC_COMPILER
  1600. NAMES "mcc"
  1601. PATHS ${Matlab_BINARIES_DIR}
  1602. DOC "Matlab MCC compiler"
  1603. NO_DEFAULT_PATH
  1604. )
  1605. if(Matlab_MCC_COMPILER)
  1606. set(Matlab_MCC_COMPILER_FOUND TRUE)
  1607. endif()
  1608. endif()
  1609. set(Matlab_LIBRARIES
  1610. ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY}
  1611. ${Matlab_ENG_LIBRARY} ${Matlab_MAT_LIBRARY})
  1612. if(Matlab_ENGINE_LIBRARY)
  1613. list(APPEND Matlab_LIBRARIES ${Matlab_ENGINE_LIBRARY})
  1614. endif()
  1615. if(Matlab_DATAARRAY_LIBRARY)
  1616. list(APPEND Matlab_LIBRARIES ${Matlab_DATAARRAY_LIBRARY})
  1617. endif()
  1618. # internal
  1619. # This small stub permits to add imported targets for the found MATLAB libraries
  1620. function(_Matlab_add_imported_target _matlab_library_variable_name _matlab_library_target_name)
  1621. if(Matlab_${_matlab_library_variable_name}_LIBRARY)
  1622. if(NOT TARGET Matlab::${_matlab_library_target_name})
  1623. add_library(Matlab::${_matlab_library_target_name} UNKNOWN IMPORTED)
  1624. set_target_properties(Matlab::${_matlab_library_target_name} PROPERTIES
  1625. INTERFACE_INCLUDE_DIRECTORIES "${Matlab_INCLUDE_DIRS}"
  1626. IMPORTED_LOCATION "${Matlab_${_matlab_library_variable_name}_LIBRARY}")
  1627. if(_matlab_library_target_name STREQUAL "mex" OR
  1628. _matlab_library_target_name STREQUAL "eng" OR
  1629. _matlab_library_target_name STREQUAL "mat")
  1630. set_target_properties(Matlab::${_matlab_library_target_name} PROPERTIES
  1631. INTERFACE_LINK_LIBRARIES Matlab::mx)
  1632. endif()
  1633. endif()
  1634. endif()
  1635. endfunction()
  1636. _Matlab_add_imported_target(MX mx)
  1637. _Matlab_add_imported_target(MEX mex)
  1638. _Matlab_add_imported_target(ENG eng)
  1639. _Matlab_add_imported_target(MAT mat)
  1640. _Matlab_add_imported_target(ENGINE MatlabEngine)
  1641. _Matlab_add_imported_target(DATAARRAY MatlabDataArray)
  1642. set(Matlab_VERSION ${Matlab_VERSION_STRING})
  1643. find_package_handle_standard_args(
  1644. Matlab
  1645. FOUND_VAR Matlab_FOUND
  1646. REQUIRED_VARS ${_matlab_required_variables}
  1647. VERSION_VAR Matlab_VERSION
  1648. HANDLE_VERSION_RANGE
  1649. HANDLE_COMPONENTS)
  1650. unset(_matlab_required_variables)
  1651. unset(_matlab_bin_prefix)
  1652. unset(_matlab_bin_suffix_32bits)
  1653. unset(_matlab_bin_suffix_64bits)
  1654. unset(_matlab_current_suffix)
  1655. unset(_matlab_lib_dir_for_search)
  1656. unset(_matlab_lib_prefix_for_search)
  1657. if(Matlab_INCLUDE_DIRS AND Matlab_LIBRARIES)
  1658. mark_as_advanced(
  1659. Matlab_MEX_LIBRARY
  1660. Matlab_MX_LIBRARY
  1661. Matlab_ENG_LIBRARY
  1662. Matlab_ENGINE_LIBRARY
  1663. Matlab_DATAARRAY_LIBRARY
  1664. Matlab_MAT_LIBRARY
  1665. Matlab_INCLUDE_DIRS
  1666. Matlab_FOUND
  1667. Matlab_MAIN_PROGRAM
  1668. Matlab_MEXEXTENSIONS_PROG
  1669. Matlab_MEX_EXTENSION
  1670. )
  1671. endif()
  1672. cmake_policy(POP)