FindMatlab.cmake 49 KB

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