FindMATLAB.cmake 49 KB

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