FindMatlab.cmake 49 KB

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