FindMatlab.cmake 49 KB

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