FindMatlab.cmake 75 KB

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