FindMatlab.cmake 64 KB

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