FindMatlab.cmake 71 KB

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