FindMatlab.cmake 61 KB

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