FindMatlab.cmake 75 KB

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