FindMATLAB.cmake 49 KB

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