FindMatlab.cmake 72 KB

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