FindMatlab.cmake 49 KB

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