FindMatlab.cmake 48 KB

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