FindMatlab.cmake 62 KB

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