FindMatlab.cmake 48 KB

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