FindMatlab.cmake 46 KB

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