FindMatlab.cmake 56 KB

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