FindMatlab.cmake 62 KB

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