FindMatlab.cmake 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. #.rst:
  2. # FindMatlab
  3. # ----------
  4. #
  5. # Finds Matlab installations and provides Matlab tools and libraries to cmake.
  6. #
  7. # This package first intention is to find the libraries associated with Matlab
  8. # in order to be able to build Matlab extensions (mex files). It can also be
  9. # used:
  10. #
  11. # * run specific commands in Matlab
  12. # * declare Matlab unit test
  13. # * retrieve various information from Matlab (mex extensions, versions and
  14. # release queries, ...)
  15. #
  16. # The module supports the following components:
  17. #
  18. # * ``MX_LIBRARY`` and ``ENG_LIBRARY`` respectively the MX and ENG libraries of
  19. # Matlab
  20. # * ``MAIN_PROGRAM`` the Matlab binary program.
  21. #
  22. # .. note::
  23. #
  24. # The version given to the :command:`find_package` directive is the Matlab
  25. # **version**, which should not be confused with the Matlab *release* name
  26. # (eg. `R2014`).
  27. # The :command:`matlab_get_version_from_release_name` and
  28. # :command:`matlab_get_release_name_from_version` allow a mapping
  29. # from the release name to the version.
  30. #
  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. #
  35. # * Windows: The installed versions of Matlab are retrieved from the
  36. # Windows registry
  37. # * OS X: The installed versions of Matlab are given by the MATLAB
  38. # paths in ``/Application``. If no such application is found, it falls back
  39. # to the one that might be accessible from the PATH.
  40. # * Unix: The desired Matlab should be accessible from the PATH.
  41. #
  42. # Additional information is provided when :variable:`MATLAB_FIND_DEBUG` is set.
  43. # When a Matlab binary is found automatically and the ``MATLAB_VERSION``
  44. # is not given, the version is queried from Matlab directly.
  45. # On Windows, it can make a window running Matlab appear.
  46. #
  47. # The mapping of the release names and the version of Matlab is performed by
  48. # defining pairs (name, version). The variable
  49. # :variable:`MATLAB_ADDITIONAL_VERSIONS` may be provided before the call to
  50. # the :command:`find_package` in order to handle additional versions.
  51. #
  52. # A Matlab scripts can be added to the set of tests using the
  53. # :command:`matlab_add_unit_test`. By default, the Matlab unit test framework
  54. # will be used (>= 2013a) to run this script, but regular ``.m`` files
  55. # returning an exit code can be used as well (0 indicating a success).
  56. #
  57. # Module Input Variables
  58. # ^^^^^^^^^^^^^^^^^^^^^^
  59. #
  60. # Users or projects may set the following variables to configure the module
  61. # behaviour:
  62. #
  63. # :variable:`Matlab_ROOT_DIR`
  64. # the root of the Matlab installation.
  65. # :variable:`MATLAB_FIND_DEBUG`
  66. # outputs debug information
  67. # :variable:`MATLAB_ADDITIONAL_VERSIONS`
  68. # additional versions of Matlab for the automatic retrieval of the installed
  69. # versions.
  70. #
  71. # Variables defined by the module
  72. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  73. #
  74. # Result variables
  75. # """"""""""""""""
  76. #
  77. # ``Matlab_FOUND``
  78. # ``TRUE`` if the Matlab installation is found, ``FALSE``
  79. # otherwise. All variable below are defined if Matlab is found.
  80. # ``Matlab_ROOT_DIR``
  81. # the final root of the Matlab installation determined by the FindMatlab
  82. # module.
  83. # ``Matlab_MAIN_PROGRAM``
  84. # the Matlab binary program. Available only if the component ``MAIN_PROGRAM``
  85. # is given in the :command:`find_package` directive.
  86. # ``Matlab_INCLUDE_DIRS``
  87. # the path of the Matlab libraries headers
  88. # ``Matlab_MEX_LIBRARY``
  89. # library for mex, always available.
  90. # ``Matlab_MX_LIBRARY``
  91. # mx library of Matlab (arrays). Available only if the component
  92. # ``MX_LIBRARY`` has been requested.
  93. # ``Matlab_ENG_LIBRARY``
  94. # Matlab engine library. Available only if the component ``ENG_LIBRARY``
  95. # is requested.
  96. # ``Matlab_LIBRARIES``
  97. # the whole set of libraries of Matlab
  98. # ``Matlab_MEX_COMPILER``
  99. # the mex compiler of Matlab. Currently not used.
  100. # Available only if the component ``MEX_COMPILER`` is asked
  101. #
  102. # Cached variables
  103. # """"""""""""""""
  104. #
  105. # ``Matlab_MEX_EXTENSION``
  106. # the extension of the mex files for the current platform (given by Matlab).
  107. # ``Matlab_ROOT_DIR``
  108. # the location of the root of the Matlab installation found. If this value
  109. # is changed by the user, the result variables are recomputed.
  110. #
  111. # Provided macros
  112. # ^^^^^^^^^^^^^^^
  113. #
  114. # :command:`matlab_get_version_from_release_name`
  115. # returns the version from the release name
  116. # :command:`matlab_get_release_name_from_version`
  117. # returns the release name from the Matlab version
  118. #
  119. # Provided functions
  120. # ^^^^^^^^^^^^^^^^^^
  121. #
  122. # :command:`matlab_add_mex`
  123. # adds a target compiling a MEX file.
  124. # :command:`matlab_add_unit_test`
  125. # adds a Matlab unit test file as a test to the project.
  126. # :command:`matlab_extract_all_installed_versions_from_registry`
  127. # parses the registry for all Matlab versions. Available on Windows only.
  128. # The part of the registry parsed is dependent on the host processor
  129. # :command:`matlab_get_all_valid_matlab_roots_from_registry`
  130. # returns all the possible Matlab paths, according to a previously
  131. # given list. Only the existing/accessible paths are kept. This is mainly
  132. # useful for the searching all possible Matlab installation.
  133. # :command:`matlab_get_mex_suffix`
  134. # returns the suffix to be used for the mex files
  135. # (platform/architecture dependant)
  136. # :command:`matlab_get_version_from_matlab_run`
  137. # returns the version of Matlab, given the full directory of the Matlab
  138. # program.
  139. #
  140. #
  141. # Known issues
  142. # ^^^^^^^^^^^^
  143. #
  144. # **Symbol clash in a MEX target**
  145. # By default, every symbols inside a MEX
  146. # file defined with the command :command:`matlab_add_mex` have hidden
  147. # visibility, except for the entry point. This is the default behaviour of
  148. # the MEX compiler, which lowers the risk of symbol collision between the
  149. # libraries shipped with Matlab, and the libraries to which the MEX file is
  150. # linking to. This is also the default on Windows platforms.
  151. #
  152. # However, this is not sufficient in certain case, where for instance your
  153. # MEX file is linking against libraries that are already loaded by Matlab,
  154. # even if those libraries have different SONAMES.
  155. # A possible solution is to hide the symbols of the libraries to which the
  156. # MEX target is linking to. This can be achieved in GNU GCC compilers with
  157. # the linker option ``-Wl,--exclude-libs,ALL``.
  158. #
  159. # **Tests using GPU resources**
  160. # in case your MEX file is using the GPU and
  161. # in order to be able to run unit tests on this MEX file, the GPU resources
  162. # should be properly released by Matlab. A possible solution is to make
  163. # Matlab aware of the use of the GPU resources in the session, which can be
  164. # performed by a command such as ``D = gpuDevice()`` at the beginning of
  165. # the test script (or via a fixture).
  166. #
  167. #
  168. # Reference
  169. # ^^^^^^^^^
  170. #
  171. # .. variable:: Matlab_ROOT_DIR
  172. #
  173. # The root folder of the Matlab installation. If set before the call to
  174. # :command:`find_package`, the module will look for the components in that
  175. # path. If not set, then an automatic search of Matlab
  176. # will be performed. If set, it should point to a valid version of Matlab.
  177. #
  178. # .. variable:: MATLAB_FIND_DEBUG
  179. #
  180. # If set, the lookup of Matlab and the intermediate configuration steps are
  181. # outputted to the console.
  182. #
  183. # .. variable:: MATLAB_ADDITIONAL_VERSIONS
  184. #
  185. # If set, specifies additional versions of Matlab that may be looked for.
  186. # The variable should be a list of strings, organised by pairs of release
  187. # name and versions, such as follows::
  188. #
  189. # set(MATLAB_ADDITIONAL_VERSIONS
  190. # "release_name1=corresponding_version1"
  191. # "release_name2=corresponding_version2"
  192. # ...
  193. # )
  194. #
  195. # Example::
  196. #
  197. # set(MATLAB_ADDITIONAL_VERSIONS
  198. # "R2013b=8.2"
  199. # "R2013a=8.1"
  200. # "R2012b=8.0")
  201. #
  202. # The order of entries in this list matters when several versions of
  203. # Matlab are installed. The priority is set according to the ordering in
  204. # this list.
  205. #=============================================================================
  206. # Copyright 2014-2015 Raffi Enficiaud, Max Planck Society
  207. #
  208. # Distributed under the OSI-approved BSD License (the "License");
  209. # see accompanying file Copyright.txt for details.
  210. #
  211. # This software is distributed WITHOUT ANY WARRANTY; without even the
  212. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  213. # See the License for more information.
  214. #=============================================================================
  215. # (To distribute this file outside of CMake, substitute the full
  216. # License text for the above reference.)
  217. set(_FindMatlab_SELF_DIR "${CMAKE_CURRENT_LIST_DIR}")
  218. include(FindPackageHandleStandardArgs)
  219. include(CheckCXXCompilerFlag)
  220. # The currently supported versions. Other version can be added by the user by
  221. # providing MATLAB_ADDITIONAL_VERSIONS
  222. if(NOT MATLAB_ADDITIONAL_VERSIONS)
  223. set(MATLAB_ADDITIONAL_VERSIONS)
  224. endif()
  225. set(MATLAB_VERSIONS_MAPPING
  226. "R2015b=8.6"
  227. "R2015a=8.5"
  228. "R2014b=8.4"
  229. "R2014a=8.3"
  230. "R2013b=8.2"
  231. "R2013a=8.1"
  232. "R2012b=8.0"
  233. "R2012a=7.14"
  234. "R2011b=7.13"
  235. "R2011a=7.12"
  236. "R2010b=7.11"
  237. ${MATLAB_ADDITIONAL_VERSIONS}
  238. )
  239. # temporary folder for all Matlab runs
  240. set(_matlab_temporary_folder ${CMAKE_BINARY_DIR}/Matlab)
  241. if(NOT EXISTS "${_matlab_temporary_folder}")
  242. file(MAKE_DIRECTORY "${_matlab_temporary_folder}")
  243. endif()
  244. #.rst:
  245. # .. command:: matlab_get_version_from_release_name
  246. #
  247. # Returns the version of Matlab (17.58) from a release name (R2017k)
  248. macro (matlab_get_version_from_release_name release_name version_name)
  249. string(REGEX MATCHALL "${release_name}=([0-9]+\\.?[0-9]*)" _matched ${MATLAB_VERSIONS_MAPPING})
  250. set(${version_name} "")
  251. if(NOT _matched STREQUAL "")
  252. set(${version_name} ${CMAKE_MATCH_1})
  253. else()
  254. message(WARNING "The release name ${release_name} is not registered")
  255. endif()
  256. unset(_matched)
  257. endmacro()
  258. #.rst:
  259. # .. command:: matlab_get_release_name_from_version
  260. #
  261. # Returns the release name (R2017k) from the version of Matlab (17.58)
  262. macro (matlab_get_release_name_from_version version release_name)
  263. set(${release_name} "")
  264. foreach(_var IN LISTS MATLAB_VERSIONS_MAPPING)
  265. string(REGEX MATCHALL "(.+)=${version}" _matched ${_var})
  266. if(NOT _matched STREQUAL "")
  267. set(${release_name} ${CMAKE_MATCH_1})
  268. break()
  269. endif()
  270. endforeach(_var)
  271. unset(_var)
  272. unset(_matched)
  273. if(${release_name} STREQUAL "")
  274. message(WARNING "The version ${version} is not registered")
  275. endif()
  276. endmacro()
  277. # extracts all the supported release names (R2017k...) of Matlab
  278. # internal use
  279. macro(matlab_get_supported_releases list_releases)
  280. set(${list_releases})
  281. foreach(_var IN LISTS MATLAB_VERSIONS_MAPPING)
  282. string(REGEX MATCHALL "(.+)=([0-9]+\\.?[0-9]*)" _matched ${_var})
  283. if(NOT _matched STREQUAL "")
  284. list(APPEND ${list_releases} ${CMAKE_MATCH_1})
  285. endif()
  286. unset(_matched)
  287. unset(CMAKE_MATCH_1)
  288. endforeach(_var)
  289. unset(_var)
  290. endmacro()
  291. # extracts all the supported versions of Matlab
  292. # internal use
  293. macro(matlab_get_supported_versions list_versions)
  294. set(${list_versions})
  295. foreach(_var IN LISTS MATLAB_VERSIONS_MAPPING)
  296. string(REGEX MATCHALL "(.+)=([0-9]+\\.?[0-9]*)" _matched ${_var})
  297. if(NOT _matched STREQUAL "")
  298. list(APPEND ${list_versions} ${CMAKE_MATCH_2})
  299. endif()
  300. unset(_matched)
  301. unset(CMAKE_MATCH_1)
  302. endforeach(_var)
  303. unset(_var)
  304. endmacro()
  305. #.rst:
  306. # .. command:: matlab_extract_all_installed_versions_from_registry
  307. #
  308. # This function parses the registry and founds the Matlab versions that are
  309. # installed. The found versions are returned in `matlab_versions`.
  310. # Set `win64` to `TRUE` if the 64 bit version of Matlab should be looked for
  311. # The returned list contains all versions under
  312. # ``HKLM\\SOFTWARE\\Mathworks\\MATLAB`` or an empty list in case an error
  313. # occurred (or nothing found).
  314. #
  315. # .. note::
  316. #
  317. # Only the versions are provided. No check is made over the existence of the
  318. # installation referenced in the registry,
  319. #
  320. function(matlab_extract_all_installed_versions_from_registry win64 matlab_versions)
  321. if(NOT CMAKE_HOST_WIN32)
  322. message(FATAL_ERROR "This macro can only be called by a windows host (call to reg.exe")
  323. endif()
  324. if(${win64} AND ${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "64")
  325. set(APPEND_REG "/reg:64")
  326. else()
  327. set(APPEND_REG "/reg:32")
  328. endif()
  329. # /reg:64 should be added on 64 bits capable OSs in order to enable the
  330. # redirection of 64 bits applications
  331. execute_process(
  332. COMMAND reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Mathworks\\MATLAB /f * /k ${APPEND_REG}
  333. RESULT_VARIABLE resultMatlab
  334. OUTPUT_VARIABLE varMatlab
  335. ERROR_VARIABLE errMatlab
  336. INPUT_FILE NUL
  337. )
  338. set(matlabs_from_registry)
  339. if(${resultMatlab} EQUAL 0)
  340. string(
  341. REGEX MATCHALL "MATLAB\\\\([0-9]+(\\.[0-9]+)?)"
  342. matlab_versions_regex ${varMatlab})
  343. foreach(match IN LISTS matlab_versions_regex)
  344. string(
  345. REGEX MATCH "MATLAB\\\\(([0-9]+)(\\.([0-9]+))?)"
  346. current_match ${match})
  347. set(_matlab_current_version ${CMAKE_MATCH_1})
  348. set(current_matlab_version_major ${CMAKE_MATCH_2})
  349. set(current_matlab_version_minor ${CMAKE_MATCH_4})
  350. if(NOT current_matlab_version_minor)
  351. set(current_matlab_version_minor "0")
  352. endif()
  353. list(APPEND matlabs_from_registry ${_matlab_current_version})
  354. unset(_matlab_current_version)
  355. endforeach(match)
  356. endif()
  357. if(matlabs_from_registry)
  358. list(REMOVE_DUPLICATES matlabs_from_registry)
  359. list(SORT matlabs_from_registry)
  360. list(REVERSE matlabs_from_registry)
  361. endif()
  362. set(${matlab_versions} ${matlabs_from_registry} PARENT_SCOPE)
  363. endfunction()
  364. # (internal)
  365. macro(extract_matlab_versions_from_registry_brute_force matlab_versions)
  366. # get the supported versions
  367. set(matlab_supported_versions)
  368. matlab_get_supported_versions(matlab_supported_versions)
  369. # this is a manual population of the versions we want to look for
  370. # this can be done as is, but preferably with the call to
  371. # matlab_get_supported_versions and variable
  372. # populating the versions we want to look for
  373. # set(matlab_supported_versions)
  374. # # Matlab 7
  375. # set(matlab_major 7)
  376. # foreach(current_matlab_minor RANGE 4 20)
  377. # list(APPEND matlab_supported_versions "${matlab_major}.${current_matlab_minor}")
  378. # endforeach(current_matlab_minor)
  379. # # Matlab 8
  380. # set(matlab_major 8)
  381. # foreach(current_matlab_minor RANGE 0 5)
  382. # list(APPEND matlab_supported_versions "${matlab_major}.${current_matlab_minor}")
  383. # endforeach(current_matlab_minor)
  384. # # taking into account the possible additional versions provided by the user
  385. # if(DEFINED MATLAB_ADDITIONAL_VERSIONS)
  386. # list(APPEND matlab_supported_versions MATLAB_ADDITIONAL_VERSIONS)
  387. # endif()
  388. # we order from more recent to older
  389. if(matlab_supported_versions)
  390. list(REMOVE_DUPLICATES matlab_supported_versions)
  391. list(SORT matlab_supported_versions)
  392. list(REVERSE matlab_supported_versions)
  393. endif()
  394. set(${matlab_versions} ${matlab_supported_versions})
  395. endmacro()
  396. #.rst:
  397. # .. command:: matlab_get_all_valid_matlab_roots_from_registry
  398. #
  399. # Populates the Matlab root with valid versions of Matlab.
  400. # The returned matlab_roots is organized in pairs
  401. # ``(version_number,matlab_root_path)``.
  402. #
  403. # ::
  404. #
  405. # matlab_get_all_valid_matlab_roots_from_registry(
  406. # matlab_versions
  407. # matlab_roots)
  408. #
  409. # ``matlab_versions``
  410. # the versions of each of the Matlab installations
  411. # ``matlab_roots``
  412. # the location of each of the Matlab installations
  413. function(matlab_get_all_valid_matlab_roots_from_registry matlab_versions matlab_roots)
  414. # The matlab_versions comes either from
  415. # extract_matlab_versions_from_registry_brute_force or
  416. # matlab_extract_all_installed_versions_from_registry.
  417. set(_matlab_roots_list )
  418. foreach(_matlab_current_version ${matlab_versions})
  419. get_filename_component(
  420. current_MATLAB_ROOT
  421. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\${_matlab_current_version};MATLABROOT]"
  422. ABSOLUTE)
  423. if(EXISTS ${current_MATLAB_ROOT})
  424. list(APPEND _matlab_roots_list ${_matlab_current_version} ${current_MATLAB_ROOT})
  425. endif()
  426. endforeach(_matlab_current_version)
  427. unset(_matlab_current_version)
  428. set(${matlab_roots} ${_matlab_roots_list} PARENT_SCOPE)
  429. unset(_matlab_roots_list)
  430. endfunction()
  431. #.rst:
  432. # .. command:: matlab_get_mex_suffix
  433. #
  434. # Returns the extension of the mex files (the suffixes).
  435. # This function should not be called before the appropriate Matlab root has
  436. # been found.
  437. #
  438. # ::
  439. #
  440. # matlab_get_mex_suffix(
  441. # matlab_root
  442. # mex_suffix)
  443. #
  444. # ``matlab_root``
  445. # the root of the Matlab installation
  446. # ``mex_suffix``
  447. # the variable name in which the suffix will be returned.
  448. function(matlab_get_mex_suffix matlab_root mex_suffix)
  449. # todo setup the extension properly. Currently I do not know if this is
  450. # sufficient for all win32 distributions.
  451. # there is also CMAKE_EXECUTABLE_SUFFIX that could be tweaked
  452. set(mexext_suffix "")
  453. if(WIN32)
  454. list(APPEND mexext_suffix ".bat")
  455. endif()
  456. # we first try without suffix, since cmake does not understand a list with
  457. # one empty string element
  458. find_program(
  459. Matlab_MEXEXTENSIONS_PROG
  460. NAMES mexext
  461. PATHS ${matlab_root}/bin
  462. DOC "Matlab MEX extension provider"
  463. NO_DEFAULT_PATH
  464. )
  465. foreach(current_mexext_suffix IN LISTS mexext_suffix)
  466. if(NOT DEFINED Matlab_MEXEXTENSIONS_PROG OR NOT Matlab_MEXEXTENSIONS_PROG)
  467. # this call should populate the cache automatically
  468. find_program(
  469. Matlab_MEXEXTENSIONS_PROG
  470. "mexext${current_mexext_suffix}"
  471. PATHS ${matlab_root}/bin
  472. DOC "Matlab MEX extension provider"
  473. NO_DEFAULT_PATH
  474. )
  475. endif()
  476. endforeach(current_mexext_suffix)
  477. # the program has been found?
  478. if((NOT Matlab_MEXEXTENSIONS_PROG) OR (NOT EXISTS ${Matlab_MEXEXTENSIONS_PROG}))
  479. if(MATLAB_FIND_DEBUG)
  480. message(WARNING "[MATLAB] Cannot found mexext program. Matlab root is ${matlab_root}")
  481. endif()
  482. unset(Matlab_MEXEXTENSIONS_PROG CACHE)
  483. return()
  484. endif()
  485. set(_matlab_mex_extension)
  486. set(devnull)
  487. if(UNIX)
  488. set(devnull INPUT_FILE /dev/null)
  489. elseif(WIN32)
  490. set(devnull INPUT_FILE NUL)
  491. endif()
  492. execute_process(
  493. COMMAND ${Matlab_MEXEXTENSIONS_PROG}
  494. OUTPUT_VARIABLE _matlab_mex_extension
  495. ERROR_VARIABLE _matlab_mex_extension_error
  496. ${devnull})
  497. string(STRIP ${_matlab_mex_extension} _matlab_mex_extension)
  498. unset(Matlab_MEXEXTENSIONS_PROG CACHE)
  499. set(${mex_suffix} ${_matlab_mex_extension} PARENT_SCOPE)
  500. endfunction()
  501. #.rst:
  502. # .. command:: matlab_get_version_from_matlab_run
  503. #
  504. # This function runs Matlab program specified on arguments and extracts its
  505. # version.
  506. #
  507. # ::
  508. #
  509. # matlab_get_version_from_matlab_run(
  510. # matlab_binary_path
  511. # matlab_list_versions)
  512. #
  513. # ``matlab_binary_path``
  514. # the location of the `matlab` binary executable
  515. # ``matlab_list_versions``
  516. # the version extracted from Matlab
  517. function(matlab_get_version_from_matlab_run matlab_binary_program matlab_list_versions)
  518. set(${matlab_list_versions} "" PARENT_SCOPE)
  519. if(MATLAB_FIND_DEBUG)
  520. message(STATUS "[MATLAB] Determining the version of Matlab from ${matlab_binary_program}")
  521. endif()
  522. if(EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  523. if(MATLAB_FIND_DEBUG)
  524. message(STATUS "[MATLAB] Removing previous ${_matlab_temporary_folder}/matlabVersionLog.cmaketmp file")
  525. endif()
  526. file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  527. endif()
  528. # the log file is needed since on windows the command executes in a new
  529. # window and it is not possible to get back the answer of Matlab
  530. # the -wait command is needed on windows, otherwise the call returns
  531. # immediately after the program launches itself.
  532. if(WIN32)
  533. set(_matlab_additional_commands "-wait")
  534. endif()
  535. set(devnull)
  536. if(UNIX)
  537. set(devnull INPUT_FILE /dev/null)
  538. elseif(WIN32)
  539. set(devnull INPUT_FILE NUL)
  540. endif()
  541. # timeout set to 30 seconds, in case it does not start
  542. # note as said before OUTPUT_VARIABLE cannot be used in a platform
  543. # independent manner however, not setting it would flush the output of Matlab
  544. # in the current console (unix variant)
  545. execute_process(
  546. COMMAND "${matlab_binary_program}" -nosplash -nojvm ${_matlab_additional_commands} -logfile "matlabVersionLog.cmaketmp" -nodesktop -nodisplay -r "version, exit"
  547. OUTPUT_VARIABLE _matlab_version_from_cmd_dummy
  548. RESULT_VARIABLE _matlab_result_version_call
  549. ERROR_VARIABLE _matlab_result_version_call_error
  550. TIMEOUT 30
  551. WORKING_DIRECTORY "${_matlab_temporary_folder}"
  552. ${devnull}
  553. )
  554. if(${_matlab_result_version_call})
  555. if(MATLAB_FIND_DEBUG)
  556. message(WARNING "[MATLAB] Unable to determine the version of Matlab. Matlab call returned with error ${_matlab_result_version_call}.")
  557. endif()
  558. return()
  559. elseif(NOT EXISTS "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  560. if(MATLAB_FIND_DEBUG)
  561. message(WARNING "[MATLAB] Unable to determine the version of Matlab. The log file does not exist.")
  562. endif()
  563. return()
  564. endif()
  565. # if successful, read back the log
  566. file(READ "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp" _matlab_version_from_cmd)
  567. file(REMOVE "${_matlab_temporary_folder}/matlabVersionLog.cmaketmp")
  568. set(index -1)
  569. string(FIND ${_matlab_version_from_cmd} "ans" index)
  570. if(index EQUAL -1)
  571. if(MATLAB_FIND_DEBUG)
  572. message(WARNING "[MATLAB] Cannot find the version of Matlab returned by the run.")
  573. endif()
  574. else()
  575. set(matlab_list_of_all_versions_tmp)
  576. string(SUBSTRING ${_matlab_version_from_cmd} ${index} -1 substring_ans)
  577. string(
  578. REGEX MATCHALL "ans[\r\n\t ]*=[\r\n\t ]*([0-9]+(\\.[0-9]+)?)"
  579. matlab_versions_regex
  580. ${substring_ans})
  581. foreach(match IN LISTS matlab_versions_regex)
  582. string(
  583. REGEX MATCH "ans[\r\n\t ]*=[\r\n\t ]*(([0-9]+)(\\.([0-9]+))?)"
  584. current_match ${match})
  585. list(APPEND matlab_list_of_all_versions_tmp ${CMAKE_MATCH_1})
  586. endforeach()
  587. if(matlab_list_of_all_versions_tmp)
  588. list(REMOVE_DUPLICATES matlab_list_of_all_versions_tmp)
  589. endif()
  590. set(${matlab_list_versions} ${matlab_list_of_all_versions_tmp} PARENT_SCOPE)
  591. endif()
  592. endfunction()
  593. #.rst:
  594. # .. command:: matlab_add_unit_test
  595. #
  596. # Adds a Matlab unit test to the test set of cmake/ctest.
  597. # This command requires the component ``MAIN_PROGRAM``.
  598. # The unit test uses the Matlab unittest framework (default, available
  599. # starting Matlab 2013b+) except if the option ``NO_UNITTEST_FRAMEWORK``
  600. # is given.
  601. #
  602. # The function expects one Matlab test script file to be given.
  603. # In the case ``NO_UNITTEST_FRAMEWORK`` is given, the unittest script file
  604. # should contain the script to be run, plus an exit command with the exit
  605. # value. This exit value will be passed to the ctest framework (0 success,
  606. # non 0 failure). Additional arguments accepted by :command:`add_test` can be
  607. # passed through ``TEST_ARGS`` (eg. ``CONFIGURATION <config> ...``).
  608. #
  609. # ::
  610. #
  611. # matlab_add_unit_test(
  612. # NAME <name>
  613. # UNITTEST_FILE matlab_file_containing_unittest.m
  614. # [UNITTEST_PRECOMMAND matlab_command_to_run]
  615. # [TIMEOUT timeout]
  616. # [ADDITIONAL_PATH path1 [path2 ...]]
  617. # [MATLAB_ADDITIONAL_STARTUP_OPTIONS option1 [option2 ...]]
  618. # [TEST_ARGS arg1 [arg2 ...]]
  619. # [NO_UNITTEST_FRAMEWORK]
  620. # )
  621. #
  622. # The function arguments are:
  623. #
  624. # ``NAME``
  625. # name of the unittest in ctest.
  626. # ``UNITTEST_FILE``
  627. # the matlab unittest file. Its path will be automatically
  628. # added to the Matlab path.
  629. # ``UNITTEST_PRECOMMAND``
  630. # Matlab script command to be ran before the file
  631. # containing the test (eg. GPU device initialisation based on CMake
  632. # variables).
  633. # ``TIMEOUT``
  634. # the test timeout in seconds. Defaults to 180 seconds as the
  635. # Matlab unit test may hang.
  636. # ``ADDITIONAL_PATH``
  637. # a list of paths to add to the Matlab path prior to
  638. # running the unit test.
  639. # ``MATLAB_ADDITIONAL_STARTUP_OPTIONS``
  640. # a list of additional option in order
  641. # to run Matlab from the command line.
  642. # ``TEST_ARGS``
  643. # Additional options provided to the add_test command. These
  644. # options are added to the default options (eg. "CONFIGURATIONS Release")
  645. # ``NO_UNITTEST_FRAMEWORK``
  646. # when set, indicates that the test should not
  647. # use the unittest framework of Matlab (available for versions >= R2013a).
  648. #
  649. function(matlab_add_unit_test)
  650. if(NOT Matlab_MAIN_PROGRAM)
  651. message(FATAL_ERROR "[MATLAB] This functionality needs the MAIN_PROGRAM component (not default)")
  652. endif()
  653. set(options NO_UNITTEST_FRAMEWORK)
  654. set(oneValueArgs NAME UNITTEST_PRECOMMAND UNITTEST_FILE TIMEOUT)
  655. set(multiValueArgs ADDITIONAL_PATH MATLAB_ADDITIONAL_STARTUP_OPTIONS TEST_ARGS)
  656. set(prefix _matlab_unittest_prefix)
  657. cmake_parse_arguments(${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  658. if(NOT ${prefix}_NAME)
  659. message(FATAL_ERROR "[MATLAB] The Matlab test name cannot be empty")
  660. endif()
  661. add_test(NAME ${${prefix}_NAME}
  662. COMMAND ${CMAKE_COMMAND}
  663. -Dtest_name=${${prefix}_NAME}
  664. -Dadditional_paths=${${prefix}_ADDITIONAL_PATH}
  665. -Dtest_timeout=${${prefix}_TIMEOUT}
  666. -Doutput_directory=${_matlab_temporary_folder}
  667. -DMatlab_PROGRAM=${Matlab_MAIN_PROGRAM}
  668. -Dno_unittest_framework=${${prefix}_NO_UNITTEST_FRAMEWORK}
  669. -DMatlab_ADDITIONNAL_STARTUP_OPTIONS=${${prefix}_MATLAB_ADDITIONAL_STARTUP_OPTIONS}
  670. -Dunittest_file_to_run=${${prefix}_UNITTEST_FILE}
  671. -Dcmd_to_run_before_test=${${prefix}_UNITTEST_PRECOMMAND}
  672. -P ${_FindMatlab_SELF_DIR}/MatlabTestsRedirect.cmake
  673. ${${prefix}_TEST_ARGS}
  674. ${${prefix}_UNPARSED_ARGUMENTS}
  675. )
  676. endfunction()
  677. #.rst:
  678. # .. command:: matlab_add_mex
  679. #
  680. # Adds a Matlab MEX target.
  681. # This commands compiles the given sources with the current tool-chain in
  682. # order to produce a MEX file. The final name of the produced output may be
  683. # specified, as well as additional link libraries, and a documentation entry
  684. # for the MEX file. Remaining arguments of the call are passed to the
  685. # :command:`add_library` command.
  686. #
  687. # ::
  688. #
  689. # matlab_add_mex(
  690. # NAME <name>
  691. # SRC src1 [src2 ...]
  692. # [OUTPUT_NAME output_name]
  693. # [DOCUMENTATION file.txt]
  694. # [LINK_TO target1 target2 ...]
  695. # [...]
  696. # )
  697. #
  698. # ``NAME``
  699. # name of the target.
  700. # ``SRC``
  701. # list of tje source files.
  702. # ``LINK_TO``
  703. # a list of additional link dependencies. The target links to ``libmex``
  704. # by default. If ``Matlab_MX_LIBRARY`` is defined, it also
  705. # links to ``libmx``.
  706. # ``OUTPUT_NAME``
  707. # if given, overrides the default name. The default name is
  708. # the name of the target without any prefix and
  709. # with ``Matlab_MEX_EXTENSION`` suffix.
  710. # ``DOCUMENTATION``
  711. # if given, the file ``file.txt`` will be considered as
  712. # being the documentation file for the MEX file. This file is copied into
  713. # the same folder without any processing, with the same name as the final
  714. # mex file, and with extension `.m`. In that case, typing ``help <name>``
  715. # in Matlab prints the documentation contained in this file.
  716. #
  717. # The documentation file is not processed and should be in the following
  718. # format:
  719. #
  720. # ::
  721. #
  722. # % This is the documentation
  723. # function ret = mex_target_output_name(input1)
  724. #
  725. function(matlab_add_mex )
  726. if(NOT WIN32)
  727. # we do not need all this on Windows
  728. # pthread options
  729. check_cxx_compiler_flag(-pthread HAS_MINUS_PTHREAD)
  730. # we should use try_compile instead, the link flags are discarded from
  731. # this compiler_flag function.
  732. #check_cxx_compiler_flag(-Wl,--exclude-libs,ALL HAS_SYMBOL_HIDING_CAPABILITY)
  733. endif()
  734. set(oneValueArgs NAME DOCUMENTATION OUTPUT_NAME)
  735. set(multiValueArgs LINK_TO SRC)
  736. set(prefix _matlab_addmex_prefix)
  737. cmake_parse_arguments(${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  738. if(NOT ${prefix}_NAME)
  739. message(FATAL_ERROR "[MATLAB] The MEX target name cannot be empty")
  740. endif()
  741. if(NOT ${prefix}_OUTPUT_NAME)
  742. set(${prefix}_OUTPUT_NAME ${${prefix}_NAME})
  743. endif()
  744. add_library(${${prefix}_NAME}
  745. SHARED
  746. ${${prefix}_SRC}
  747. ${${prefix}_DOCUMENTATION}
  748. ${${prefix}_UNPARSED_ARGUMENTS})
  749. target_include_directories(${${prefix}_NAME} PRIVATE ${Matlab_INCLUDE_DIRS})
  750. if(DEFINED Matlab_MX_LIBRARY)
  751. target_link_libraries(${${prefix}_NAME} ${Matlab_MX_LIBRARY})
  752. endif()
  753. target_link_libraries(${${prefix}_NAME} ${Matlab_MEX_LIBRARY} ${${prefix}_LINK_TO})
  754. set_target_properties(${${prefix}_NAME}
  755. PROPERTIES
  756. PREFIX ""
  757. OUTPUT_NAME ${${prefix}_OUTPUT_NAME}
  758. SUFFIX ".${Matlab_MEX_EXTENSION}")
  759. # documentation
  760. if(NOT ${${prefix}_DOCUMENTATION} STREQUAL "")
  761. get_target_property(output_name ${${prefix}_NAME} OUTPUT_NAME)
  762. add_custom_command(
  763. TARGET ${${prefix}_NAME}
  764. PRE_BUILD
  765. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${${prefix}_DOCUMENTATION} $<TARGET_FILE_DIR:${${prefix}_NAME}>/${output_name}.m
  766. COMMENT "Copy ${${prefix}_NAME} documentation file into the output folder"
  767. )
  768. endif() # documentation
  769. # entry point in the mex file + taking care of visibility and symbol clashes.
  770. if(WIN32)
  771. set_target_properties(${${prefix}_NAME}
  772. PROPERTIES
  773. DEFINE_SYMBOL "DLL_EXPORT_SYM=__declspec(dllexport)")
  774. else()
  775. if(HAS_MINUS_PTHREAD AND NOT APPLE)
  776. # Apparently, compiling with -pthread generated the proper link flags
  777. # and some defines at compilation
  778. target_compile_options(${${prefix}_NAME} PRIVATE "-pthread")
  779. endif()
  780. # if we do not do that, the symbols linked from eg. boost remain weak and
  781. # then clash with the ones defined in the matlab process. So by default
  782. # the symbols are hidden.
  783. # This also means that for shared libraries (like MEX), the entry point
  784. # should be explicitly declared with default visibility, otherwise Matlab
  785. # cannot find the entry point.
  786. # Note that this is particularly meaningful if the MEX wrapper itself
  787. # contains symbols that are clashing with Matlab (that are compiled in the
  788. # MEX file). In order to propagate the visibility options to the libraries
  789. # to which the MEX file is linked against, the -Wl,--exclude-libs,ALL
  790. # option should also be specified.
  791. set_target_properties(${${prefix}_NAME}
  792. PROPERTIES
  793. CXX_VISIBILITY_PRESET "hidden"
  794. C_VISIBILITY_PRESET "hidden"
  795. VISIBILITY_INLINES_HIDDEN ON
  796. )
  797. # get_target_property(
  798. # _previous_link_flags
  799. # ${${prefix}_NAME}
  800. # LINK_FLAGS)
  801. # if(NOT _previous_link_flags)
  802. # set(_previous_link_flags)
  803. # endif()
  804. # if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  805. # set_target_properties(${${prefix}_NAME}
  806. # PROPERTIES
  807. # LINK_FLAGS "${_previous_link_flags} -Wl,--exclude-libs,ALL"
  808. # # -Wl,--version-script=${_FindMatlab_SELF_DIR}/MatlabLinuxVisibility.map"
  809. # )
  810. # elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  811. # # in this case, all other symbols become hidden.
  812. # set_target_properties(${${prefix}_NAME}
  813. # PROPERTIES
  814. # LINK_FLAGS "${_previous_link_flags} -Wl,-exported_symbol,_mexFunction"
  815. # #-Wl,-exported_symbols_list,${_FindMatlab_SELF_DIR}/MatlabOSXVisilibity.map"
  816. # )
  817. # endif()
  818. set_target_properties(${${prefix}_NAME}
  819. PROPERTIES
  820. DEFINE_SYMBOL "DLL_EXPORT_SYM=__attribute__ ((visibility (\"default\")))"
  821. )
  822. endif()
  823. endfunction()
  824. # (internal)
  825. # Used to get the version of matlab, using caching. This basically transforms the
  826. # output of the root list, with possible unknown version, to a version
  827. #
  828. function(_Matlab_get_version_from_root matlab_root matlab_known_version matlab_final_version)
  829. # if the version is not trivial, we query matlab for that
  830. # we keep track of the location of matlab that induced this version
  831. #if(NOT DEFINED Matlab_PROG_VERSION_STRING_AUTO_DETECT)
  832. # set(Matlab_PROG_VERSION_STRING_AUTO_DETECT "" CACHE INTERNAL "internal matlab location for the discovered version")
  833. #endif()
  834. if(NOT ${matlab_known_version} STREQUAL "NOTFOUND")
  835. # the version is known, we just return it
  836. set(${matlab_final_version} ${matlab_known_version} PARENT_SCOPE)
  837. set(Matlab_VERSION_STRING_INTERNAL ${matlab_known_version} CACHE INTERNAL "Matlab version (automatically determined)" FORCE)
  838. return()
  839. endif()
  840. #
  841. set(_matlab_current_program ${Matlab_MAIN_PROGRAM})
  842. # do we already have a matlab program?
  843. if(NOT _matlab_current_program)
  844. set(_find_matlab_options)
  845. if(matlab_root AND EXISTS ${matlab_root})
  846. set(_find_matlab_options PATHS ${matlab_root} ${matlab_root}/bin NO_DEFAULT_PATH)
  847. endif()
  848. find_program(
  849. _matlab_current_program
  850. matlab
  851. ${_find_matlab_options}
  852. DOC "Matlab main program"
  853. )
  854. endif()
  855. if(NOT _matlab_current_program OR NOT EXISTS ${_matlab_current_program})
  856. # if not found, clear the dependent variables
  857. if(MATLAB_FIND_DEBUG)
  858. message(WARNING "[MATLAB] Cannot find the main matlab program under ${matlab_root}")
  859. endif()
  860. set(Matlab_PROG_VERSION_STRING_AUTO_DETECT "" CACHE INTERNAL "internal matlab location for the discovered version" FORCE)
  861. set(Matlab_VERSION_STRING_INTERNAL "" CACHE INTERNAL "internal matlab location for the discovered version" FORCE)
  862. unset(_matlab_current_program)
  863. unset(_matlab_current_program CACHE)
  864. return()
  865. endif()
  866. # full real path for path comparison
  867. get_filename_component(_matlab_main_real_path_tmp "${_matlab_current_program}" REALPATH)
  868. unset(_matlab_current_program)
  869. unset(_matlab_current_program CACHE)
  870. # is it the same as the previous one?
  871. if(_matlab_main_real_path_tmp STREQUAL Matlab_PROG_VERSION_STRING_AUTO_DETECT)
  872. set(${matlab_final_version} ${Matlab_VERSION_STRING_INTERNAL} PARENT_SCOPE)
  873. return()
  874. endif()
  875. # update the location of the program
  876. set(Matlab_PROG_VERSION_STRING_AUTO_DETECT ${_matlab_main_real_path_tmp} CACHE INTERNAL "internal matlab location for the discovered version" FORCE)
  877. set(matlab_list_of_all_versions)
  878. matlab_get_version_from_matlab_run("${Matlab_PROG_VERSION_STRING_AUTO_DETECT}" matlab_list_of_all_versions)
  879. list(GET matlab_list_of_all_versions 0 _matlab_version_tmp)
  880. # set the version into the cache
  881. set(Matlab_VERSION_STRING_INTERNAL ${_matlab_version_tmp} CACHE INTERNAL "Matlab version (automatically determined)" FORCE)
  882. # warning, just in case several versions found (should not happen)
  883. list(LENGTH matlab_list_of_all_versions list_of_all_versions_length)
  884. if((${list_of_all_versions_length} GREATER 1) AND MATLAB_FIND_DEBUG)
  885. message(WARNING "[MATLAB] Found several versions, taking the first one (versions found ${matlab_list_of_all_versions})")
  886. endif()
  887. # return the updated value
  888. set(${matlab_final_version} ${Matlab_VERSION_STRING_INTERNAL} PARENT_SCOPE)
  889. endfunction()
  890. # ###################################
  891. # Exploring the possible Matlab_ROOTS
  892. # this variable will get all Matlab installations found in the current system.
  893. set(_matlab_possible_roots)
  894. if(Matlab_ROOT_DIR)
  895. # if the user specifies a possible root, we keep this one
  896. if(NOT EXISTS ${Matlab_ROOT_DIR})
  897. # if Matlab_ROOT_DIR specified but erroneous
  898. if(MATLAB_FIND_DEBUG)
  899. message(WARNING "[MATLAB] the specified path for Matlab_ROOT_DIR does not exist (${Matlab_ROOT_DIR})")
  900. endif()
  901. else()
  902. # NOTFOUND indicates the code below to search for the version automatically
  903. if("${Matlab_VERSION_STRING_INTERNAL}" STREQUAL "")
  904. list(APPEND _matlab_possible_roots "NOTFOUND" ${Matlab_ROOT_DIR}) # empty version
  905. else()
  906. list(APPEND _matlab_possible_roots ${Matlab_VERSION_STRING_INTERNAL} ${Matlab_ROOT_DIR}) # cached version
  907. endif()
  908. endif()
  909. else()
  910. # if the user does not specify the possible installation root, we look for
  911. # one installation using the appropriate heuristics
  912. if(WIN32)
  913. # On WIN32, we look for Matlab installation in the registry
  914. # if unsuccessful, we look for all known revision and filter the existing
  915. # ones.
  916. # testing if we are able to extract the needed information from the registry
  917. set(_matlab_versions_from_registry)
  918. matlab_extract_all_installed_versions_from_registry(CMAKE_CL_64 _matlab_versions_from_registry)
  919. # the returned list is empty, doing the search on all known versions
  920. if(NOT _matlab_versions_from_registry)
  921. if(MATLAB_FIND_DEBUG)
  922. message(STATUS "[MATLAB] Search for Matlab from the registry unsuccessful, testing all supported versions")
  923. endif()
  924. extract_matlab_versions_from_registry_brute_force(_matlab_versions_from_registry)
  925. endif()
  926. # filtering the results with the registry keys
  927. matlab_get_all_valid_matlab_roots_from_registry("${_matlab_versions_from_registry}" _matlab_possible_roots)
  928. unset(_matlab_versions_from_registry)
  929. elseif(APPLE)
  930. # on mac, we look for the /Application paths
  931. # this corresponds to the behaviour on Windows. On Linux, we do not have
  932. # any other guess.
  933. matlab_get_supported_releases(_matlab_releases)
  934. if(MATLAB_FIND_DEBUG)
  935. message(STATUS "[MATLAB] Matlab supported versions ${_matlab_releases}. If more version should be supported "
  936. "the variable MATLAB_ADDITIONAL_VERSIONS can be set according to the documentation")
  937. endif()
  938. foreach(_matlab_current_release IN LISTS _matlab_releases)
  939. set(_matlab_full_string "/Applications/MATLAB_${_matlab_current_release}.app")
  940. if(EXISTS ${_matlab_full_string})
  941. set(_matlab_current_version)
  942. matlab_get_version_from_release_name("${_matlab_current_release}" _matlab_current_version)
  943. if(MATLAB_FIND_DEBUG)
  944. message(STATUS "[MATLAB] Found version ${_matlab_current_release} (${_matlab_current_version}) in ${_matlab_full_string}")
  945. endif()
  946. list(APPEND _matlab_possible_roots ${_matlab_current_version} ${_matlab_full_string})
  947. unset(_matlab_current_version)
  948. endif()
  949. unset(_matlab_full_string)
  950. endforeach(_matlab_current_release)
  951. unset(_matlab_current_release)
  952. unset(_matlab_releases)
  953. endif()
  954. endif()
  955. list(LENGTH _matlab_possible_roots _numbers_of_matlab_roots)
  956. if(_numbers_of_matlab_roots EQUAL 0)
  957. # if we have not found anything, we fall back on the PATH
  958. # At this point, we have no other choice than trying to find it from PATH.
  959. # If set by the user, this wont change
  960. find_program(
  961. _matlab_main_tmp
  962. NAMES matlab)
  963. if(_matlab_main_tmp)
  964. # we then populate the list of roots, with empty version
  965. if(MATLAB_FIND_DEBUG)
  966. message(STATUS "[MATLAB] matlab found from PATH: ${_matlab_main_tmp}")
  967. endif()
  968. # resolve symlinks
  969. get_filename_component(_matlab_current_location "${_matlab_main_tmp}" REALPATH)
  970. # get the directory (the command below has to be run twice)
  971. # this will be the matlab root
  972. get_filename_component(_matlab_current_location "${_matlab_current_location}" DIRECTORY)
  973. get_filename_component(_matlab_current_location "${_matlab_current_location}" DIRECTORY) # Matlab should be in bin
  974. list(APPEND _matlab_possible_roots "NOTFOUND" ${_matlab_current_location})
  975. unset(_matlab_current_location)
  976. endif()
  977. unset(_matlab_main_tmp CACHE)
  978. endif()
  979. if(MATLAB_FIND_DEBUG)
  980. message(STATUS "[MATLAB] Matlab root folders are ${_matlab_possible_roots}")
  981. endif()
  982. # take the first possible Matlab root
  983. list(LENGTH _matlab_possible_roots _numbers_of_matlab_roots)
  984. set(Matlab_VERSION_STRING "NOTFOUND")
  985. if(_numbers_of_matlab_roots GREATER 0)
  986. list(GET _matlab_possible_roots 0 Matlab_VERSION_STRING)
  987. list(GET _matlab_possible_roots 1 Matlab_ROOT_DIR)
  988. # adding a warning in case of ambiguity
  989. if(_numbers_of_matlab_roots GREATER 2 AND MATLAB_FIND_DEBUG)
  990. message(WARNING "[MATLAB] Found several distributions of Matlab. Setting the current version to ${Matlab_VERSION_STRING} (located ${Matlab_ROOT_DIR})."
  991. " If this is not the desired behaviour, provide the -DMatlab_ROOT_DIR=... on the command line")
  992. endif()
  993. endif()
  994. # check if the root changed against the previous defined one, if so
  995. # clear all the cached variables
  996. if(DEFINED Matlab_ROOT_DIR_LAST_CACHED)
  997. if(NOT Matlab_ROOT_DIR_LAST_CACHED STREQUAL Matlab_ROOT_DIR)
  998. set(_Matlab_cached_vars
  999. Matlab_INCLUDE_DIRS
  1000. Matlab_MEX_LIBRARY
  1001. Matlab_MEX_COMPILER
  1002. Matlab_MAIN_PROGRAM
  1003. Matlab_MX_LIBRARY
  1004. Matlab_ENG_LIBRARY
  1005. Matlab_MEX_EXTENSION
  1006. # internal
  1007. Matlab_MEXEXTENSIONS_PROG
  1008. Matlab_ROOT_DIR_LAST_CACHED
  1009. #Matlab_PROG_VERSION_STRING_AUTO_DETECT
  1010. Matlab_VERSION_STRING_INTERNAL
  1011. )
  1012. foreach(_var IN LISTS _Matlab_cached_vars)
  1013. if(DEFINED ${_var})
  1014. unset(${_var} CACHE)
  1015. endif()
  1016. endforeach()
  1017. endif()
  1018. endif()
  1019. set(Matlab_ROOT_DIR_LAST_CACHED ${Matlab_ROOT_DIR} CACHE INTERNAL "last Matlab root dir location")
  1020. set(Matlab_ROOT_DIR ${Matlab_ROOT_DIR} CACHE PATH "Matlab installation root path" FORCE)
  1021. # Fix the version, in case this one is NOTFOUND
  1022. _Matlab_get_version_from_root(
  1023. "${Matlab_ROOT_DIR}"
  1024. ${Matlab_VERSION_STRING}
  1025. Matlab_VERSION_STRING
  1026. )
  1027. if(MATLAB_FIND_DEBUG)
  1028. message(STATUS "[MATLAB] Current version is ${Matlab_VERSION_STRING} located ${Matlab_ROOT_DIR}")
  1029. endif()
  1030. if(Matlab_ROOT_DIR)
  1031. file(TO_CMAKE_PATH ${Matlab_ROOT_DIR} Matlab_ROOT_DIR)
  1032. endif()
  1033. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  1034. set(_matlab_64Build FALSE)
  1035. else()
  1036. set(_matlab_64Build TRUE)
  1037. endif()
  1038. if(APPLE)
  1039. set(_matlab_bin_prefix "mac") # i should be for intel
  1040. set(_matlab_bin_suffix_32bits "i")
  1041. set(_matlab_bin_suffix_64bits "i64")
  1042. elseif(UNIX)
  1043. set(_matlab_bin_prefix "gln")
  1044. set(_matlab_bin_suffix_32bits "x86")
  1045. set(_matlab_bin_suffix_64bits "xa64")
  1046. else()
  1047. set(_matlab_bin_prefix "win")
  1048. set(_matlab_bin_suffix_32bits "32")
  1049. set(_matlab_bin_suffix_64bits "64")
  1050. endif()
  1051. set(MATLAB_INCLUDE_DIR_TO_LOOK ${Matlab_ROOT_DIR}/extern/include)
  1052. if(_matlab_64Build)
  1053. set(_matlab_current_suffix ${_matlab_bin_suffix_64bits})
  1054. else()
  1055. set(_matlab_current_suffix ${_matlab_bin_suffix_32bits})
  1056. endif()
  1057. set(Matlab_BINARIES_DIR
  1058. ${Matlab_ROOT_DIR}/bin/${_matlab_bin_prefix}${_matlab_current_suffix})
  1059. set(Matlab_EXTERN_LIBRARY_DIR
  1060. ${Matlab_ROOT_DIR}/extern/lib/${_matlab_bin_prefix}${_matlab_current_suffix})
  1061. if(WIN32)
  1062. set(_matlab_lib_dir_for_search ${Matlab_EXTERN_LIBRARY_DIR}/microsoft)
  1063. set(_matlab_lib_prefix_for_search "lib")
  1064. else()
  1065. set(_matlab_lib_dir_for_search ${Matlab_BINARIES_DIR})
  1066. set(_matlab_lib_prefix_for_search "lib")
  1067. endif()
  1068. unset(_matlab_64Build)
  1069. if(NOT DEFINED Matlab_MEX_EXTENSION)
  1070. set(_matlab_mex_extension "")
  1071. matlab_get_mex_suffix("${Matlab_ROOT_DIR}" _matlab_mex_extension)
  1072. # This variable goes to the cache.
  1073. set(Matlab_MEX_EXTENSION ${_matlab_mex_extension} CACHE STRING "Extensions for the mex targets (automatically given by Matlab)")
  1074. unset(_matlab_mex_extension)
  1075. endif()
  1076. if(MATLAB_FIND_DEBUG)
  1077. message(STATUS "[MATLAB] [DEBUG]_matlab_lib_prefix_for_search = ${_matlab_lib_prefix_for_search} | _matlab_lib_dir_for_search = ${_matlab_lib_dir_for_search}")
  1078. endif()
  1079. # internal
  1080. # This small stub around find_library is to prevent any pollution of CMAKE_FIND_LIBRARY_PREFIXES in the global scope.
  1081. # This is the function to be used below instead of the find_library directives.
  1082. function(_Matlab_find_library _matlab_library_prefix)
  1083. set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} ${_matlab_library_prefix})
  1084. find_library(${ARGN})
  1085. endfunction()
  1086. set(_matlab_required_variables)
  1087. # the MEX library/header are required
  1088. find_path(
  1089. Matlab_INCLUDE_DIRS
  1090. mex.h
  1091. PATHS ${MATLAB_INCLUDE_DIR_TO_LOOK}
  1092. NO_DEFAULT_PATH
  1093. )
  1094. list(APPEND _matlab_required_variables Matlab_INCLUDE_DIRS)
  1095. _Matlab_find_library(
  1096. ${_matlab_lib_prefix_for_search}
  1097. Matlab_MEX_LIBRARY
  1098. mex
  1099. PATHS ${_matlab_lib_dir_for_search}
  1100. NO_DEFAULT_PATH
  1101. )
  1102. list(APPEND _matlab_required_variables Matlab_MEX_LIBRARY)
  1103. # the MEX extension is required
  1104. list(APPEND _matlab_required_variables Matlab_MEX_EXTENSION)
  1105. # the matlab root is required
  1106. list(APPEND _matlab_required_variables Matlab_ROOT_DIR)
  1107. # component Mex Compiler
  1108. list(FIND Matlab_FIND_COMPONENTS MEX_COMPILER _matlab_find_mex_compiler)
  1109. if(_matlab_find_mex_compiler GREATER -1)
  1110. find_program(
  1111. Matlab_MEX_COMPILER
  1112. "mex"
  1113. PATHS ${Matlab_BINARIES_DIR}
  1114. DOC "Matlab MEX compiler"
  1115. NO_DEFAULT_PATH
  1116. )
  1117. if(Matlab_MEX_COMPILER)
  1118. set(Matlab_MEX_COMPILER_FOUND TRUE)
  1119. endif()
  1120. endif()
  1121. unset(_matlab_find_mex_compiler)
  1122. # component Matlab program
  1123. list(FIND Matlab_FIND_COMPONENTS MAIN_PROGRAM _matlab_find_matlab_program)
  1124. if(_matlab_find_matlab_program GREATER -1)
  1125. find_program(
  1126. Matlab_MAIN_PROGRAM
  1127. matlab
  1128. PATHS ${Matlab_ROOT_DIR} ${Matlab_ROOT_DIR}/bin
  1129. DOC "Matlab main program"
  1130. NO_DEFAULT_PATH
  1131. )
  1132. if(Matlab_MAIN_PROGRAM)
  1133. set(Matlab_MAIN_PROGRAM_FOUND TRUE)
  1134. endif()
  1135. endif()
  1136. unset(_matlab_find_matlab_program)
  1137. # Component MX library
  1138. list(FIND Matlab_FIND_COMPONENTS MX_LIBRARY _matlab_find_mx)
  1139. if(_matlab_find_mx GREATER -1)
  1140. _Matlab_find_library(
  1141. ${_matlab_lib_prefix_for_search}
  1142. Matlab_MX_LIBRARY
  1143. mx
  1144. PATHS ${_matlab_lib_dir_for_search}
  1145. NO_DEFAULT_PATH
  1146. )
  1147. if(Matlab_MX_LIBRARY)
  1148. set(Matlab_MX_LIBRARY_FOUND TRUE)
  1149. endif()
  1150. endif()
  1151. unset(_matlab_find_mx)
  1152. # Component ENG library
  1153. list(FIND Matlab_FIND_COMPONENTS ENG_LIBRARY _matlab_find_eng)
  1154. if(_matlab_find_eng GREATER -1)
  1155. _Matlab_find_library(
  1156. ${_matlab_lib_prefix_for_search}
  1157. Matlab_ENG_LIBRARY
  1158. eng
  1159. PATHS ${_matlab_lib_dir_for_search}
  1160. NO_DEFAULT_PATH
  1161. )
  1162. if(Matlab_ENG_LIBRARY)
  1163. set(Matlab_ENG_LIBRARY_FOUND TRUE)
  1164. endif()
  1165. endif()
  1166. unset(_matlab_find_eng)
  1167. unset(_matlab_lib_dir_for_search)
  1168. set(Matlab_LIBRARIES ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY} ${Matlab_ENG_LIBRARY})
  1169. find_package_handle_standard_args(
  1170. Matlab
  1171. FOUND_VAR Matlab_FOUND
  1172. REQUIRED_VARS ${_matlab_required_variables}
  1173. VERSION_VAR Matlab_VERSION_STRING
  1174. HANDLE_COMPONENTS)
  1175. unset(_matlab_required_variables)
  1176. unset(_matlab_bin_prefix)
  1177. unset(_matlab_bin_suffix_32bits)
  1178. unset(_matlab_bin_suffix_64bits)
  1179. unset(_matlab_current_suffix)
  1180. unset(_matlab_lib_dir_for_search)
  1181. unset(_matlab_lib_prefix_for_search)
  1182. if(Matlab_INCLUDE_DIRS AND Matlab_LIBRARIES)
  1183. mark_as_advanced(
  1184. #Matlab_LIBRARIES
  1185. Matlab_MEX_LIBRARY
  1186. Matlab_MX_LIBRARY
  1187. Matlab_ENG_LIBRARY
  1188. Matlab_INCLUDE_DIRS
  1189. Matlab_FOUND
  1190. #Matlab_ROOT_DIR
  1191. #Matlab_VERSION_STRING
  1192. Matlab_MAIN_PROGRAM
  1193. #Matlab_MEX_EXTENSION
  1194. Matlab_MEXEXTENSIONS_PROG
  1195. Matlab_MEX_EXTENSION
  1196. #Matlab_BINARIES_DIR
  1197. )
  1198. endif()