FindMatlab.cmake 49 KB

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