FindMatlab.cmake 49 KB

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