FindMatlab.cmake 46 KB

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