Skip to content

Commit

Permalink
Avoid to link mex file to too many not used libraries and fix use in …
Browse files Browse the repository at this point in the history
…MATLAB Online (#62)
  • Loading branch information
traversaro authored Mar 12, 2021
1 parent d5b5640 commit 26e5f3d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 44 deletions.
154 changes: 111 additions & 43 deletions cmake/FindMatlab.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
# Vendored from https://gitlab.kitware.com/cmake/cmake/-/raw/v3.20.0-rc3/Modules/FindMatlab.cmake
# With local modification to implement https://gitlab.kitware.com/cmake/cmake/-/issues/21912
# Distributed under the OSI-approved BSD 3-Clause License. See
# https://cmake.org/licensing for details.

#[=======================================================================[.rst:
FindMatlab
Expand All @@ -17,6 +19,9 @@ can also be used:
* to retrieve various information from Matlab (mex extensions, versions and
release queries, ...)

.. versionadded:: 3.12
Added Matlab Compiler Runtime (MCR) support.

The module supports the following components:

* ``ENG_LIBRARY`` and ``MAT_LIBRARY``: respectively the ``ENG`` and ``MAT``
Expand All @@ -28,6 +33,17 @@ The module supports the following components:
* ``MCC_COMPILER`` the MCC compiler, included with the Matlab Compiler add-on.
* ``SIMULINK`` the Simulink environment.

.. versionadded:: 3.7
Added the ``MAT_LIBRARY`` component.

.. versionadded:: 3.13
Added the ``ENGINE_LIBRARY``, ``DATAARRAY_LIBRARY`` and ``MCC_COMPILER``
components.

.. versionchanged:: 3.14
Removed the ``MX_LIBRARY``, ``ENGINE_LIBRARY`` and ``DATAARRAY_LIBRARY``
components. These libraries are found unconditionally.

.. note::

The version given to the :command:`find_package` directive is the Matlab
Expand Down Expand Up @@ -107,15 +123,21 @@ Result variables
Matlab matrix library. Available only if the component ``MAT_LIBRARY``
is requested.
``Matlab_ENGINE_LIBRARY``
.. versionadded:: 3.13

Matlab C++ engine library, always available for R2018a and newer.
``Matlab_DATAARRAY_LIBRARY``
.. versionadded:: 3.13

Matlab C++ data array library, always available for R2018a and newer.
``Matlab_LIBRARIES``
the whole set of libraries of Matlab
``Matlab_MEX_COMPILER``
the mex compiler of Matlab. Currently not used.
Available only if the component ``MEX_COMPILER`` is requested.
``Matlab_MCC_COMPILER``
.. versionadded:: 3.13

the mcc compiler of Matlab. Included with the Matlab Compiler add-on.
Available only if the component ``MCC_COMPILER`` is requested.

Expand Down Expand Up @@ -241,6 +263,8 @@ if(NOT MATLAB_ADDITIONAL_VERSIONS)
endif()

set(MATLAB_VERSIONS_MAPPING
"R2020b=9.9"
"R2020a=9.8"
"R2019b=9.7"
"R2019a=9.6"
"R2018b=9.5"
Expand Down Expand Up @@ -387,12 +411,12 @@ function(matlab_extract_all_installed_versions_from_registry win64 matlab_versio

set(matlabs_from_registry)

foreach(_installation_type IN ITEMS "MATLAB" "MATLAB Runtime")
foreach(_installation_type IN ITEMS "MATLAB" "MATLAB Runtime" "MATLAB Compiler Runtime")

# /reg:64 should be added on 64 bits capable OSs in order to enable the
# redirection of 64 bits applications
execute_process(
COMMAND reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Mathworks\\${_installation_type} /f * /k ${APPEND_REG}
COMMAND reg query "HKEY_LOCAL_MACHINE\\SOFTWARE\\Mathworks\\${_installation_type}" /f * /k ${APPEND_REG}
RESULT_VARIABLE resultMatlab
OUTPUT_VARIABLE varMatlab
ERROR_VARIABLE errMatlab
Expand All @@ -403,12 +427,12 @@ function(matlab_extract_all_installed_versions_from_registry win64 matlab_versio
if(resultMatlab EQUAL 0)

string(
REGEX MATCHALL "MATLAB\\\\([0-9]+(\\.[0-9]+)?)"
REGEX MATCHALL "${_installation_type}\\\\([0-9]+(\\.[0-9]+)?)"
matlab_versions_regex ${varMatlab})

foreach(match IN LISTS matlab_versions_regex)
string(
REGEX MATCH "MATLAB\\\\(([0-9]+)(\\.([0-9]+))?)"
REGEX MATCH "${_installation_type}\\\\(([0-9]+)(\\.([0-9]+))?)"
current_match ${match})

set(_matlab_current_version ${CMAKE_MATCH_1})
Expand Down Expand Up @@ -515,7 +539,7 @@ function(matlab_get_all_valid_matlab_roots_from_registry matlab_versions matlab_
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\${_matlab_current_version};MATLABROOT]"
ABSOLUTE)

if(EXISTS ${current_MATLAB_ROOT})
if(EXISTS "${current_MATLAB_ROOT}")
list(APPEND _matlab_roots_list "MATLAB" ${_matlab_current_version} ${current_MATLAB_ROOT})
endif()

Expand All @@ -531,7 +555,23 @@ function(matlab_get_all_valid_matlab_roots_from_registry matlab_versions matlab_
# remove the dot
string(REPLACE "." "" _matlab_current_version_without_dot "${_matlab_current_version}")

if(EXISTS ${current_MATLAB_ROOT})
if(EXISTS "${current_MATLAB_ROOT}")
list(APPEND _matlab_roots_list "MCR" ${_matlab_current_version} "${current_MATLAB_ROOT}/v${_matlab_current_version_without_dot}")
endif()

endforeach()

# Check for old MCR installations
foreach(_matlab_current_version ${matlab_versions})
get_filename_component(
current_MATLAB_ROOT
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB Compiler Runtime\\${_matlab_current_version};MATLABROOT]"
ABSOLUTE)

# remove the dot
string(REPLACE "." "" _matlab_current_version_without_dot "${_matlab_current_version}")

if(EXISTS "${current_MATLAB_ROOT}")
list(APPEND _matlab_roots_list "MCR" ${_matlab_current_version} "${current_MATLAB_ROOT}/v${_matlab_current_version_without_dot}")
endif()

Expand Down Expand Up @@ -901,6 +941,7 @@ endfunction()
[LINK_TO target1 target2 ...]
[R2017b | R2018a]
[EXCLUDE_FROM_ALL]
[NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES]
[...]
)

Expand All @@ -910,7 +951,7 @@ endfunction()
list of source files.
``LINK_TO``
a list of additional link dependencies. The target links to ``libmex``
and ``libmx`` by default.
and ``libmx`` by default, unless NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES is passed.
``OUTPUT_NAME``
if given, overrides the default name. The default name is
the name of the target without any prefix and
Expand All @@ -921,19 +962,35 @@ endfunction()
the same folder without any processing, with the same name as the final
mex file, and with extension `.m`. In that case, typing ``help <name>``
in Matlab prints the documentation contained in this file.
``R2017b`` or ``R2018a`` may be given to specify the version of the C API
``R2017b`` or ``R2018a``
.. versionadded:: 3.14

May be given to specify the version of the C API
to use: ``R2017b`` specifies the traditional (separate complex) C API,
and corresponds to the ``-R2017b`` flag for the `mex` command. ``R2018a``
specifies the new interleaved complex C API, and corresponds to the
``-R2018a`` flag for the `mex` command. Ignored if MATLAB version prior
to R2018a. Defaults to ``R2017b``.
``MODULE`` or ``SHARED`` may be given to specify the type of library to be
created. ``EXECUTABLE`` may be given to create an executable instead of

``MODULE`` or ``SHARED``
.. versionadded:: 3.7

May be given to specify the type of library to be
created.

``EXECUTABLE``
.. versionadded:: 3.7

May be given to create an executable instead of
a library. If no type is given explicitly, the type is ``SHARED``.
``EXCLUDE_FROM_ALL``
This option has the same meaning as for :prop_tgt:`EXCLUDE_FROM_ALL` and
is forwarded to :command:`add_library` or :command:`add_executable`
commands.
``NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES``
This option permits to disable the automatic linking of MATLAB libraries,
so that only the libraries that are actually required can be linked via the
``LINK_TO`` option.

The documentation file is not processed and should be in the following
format:
Expand All @@ -960,7 +1017,7 @@ function(matlab_add_mex)

endif()

set(options EXECUTABLE MODULE SHARED R2017b R2018a EXCLUDE_FROM_ALL)
set(options EXECUTABLE MODULE SHARED R2017b R2018a EXCLUDE_FROM_ALL NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES)
set(oneValueArgs NAME DOCUMENTATION OUTPUT_NAME)
set(multiValueArgs LINK_TO SRC)

Expand Down Expand Up @@ -991,7 +1048,10 @@ function(matlab_add_mex)
endif()
endif()

if(NOT Matlab_VERSION_STRING VERSION_LESS "9.4") # For 9.4 (R2018a) and newer, add API macro
# For 9.4 (R2018a) and newer, add API macro.
# Add it for unknown versions too, just in case.
if(NOT Matlab_VERSION_STRING VERSION_LESS "9.4"
OR Matlab_VERSION_STRING STREQUAL "unknown")
if(${${prefix}_R2018a})
set(MEX_API_MACRO "MATLAB_DEFAULT_RELEASE=R2018a")
else()
Expand Down Expand Up @@ -1029,11 +1089,19 @@ function(matlab_add_mex)

target_include_directories(${${prefix}_NAME} PRIVATE ${Matlab_INCLUDE_DIRS})

if(Matlab_HAS_CPP_API)
target_link_libraries(${${prefix}_NAME} ${Matlab_ENGINE_LIBRARY} ${Matlab_DATAARRAY_LIBRARY})
if(NOT ${prefix}_NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES)
if(Matlab_HAS_CPP_API)
if(Matlab_ENGINE_LIBRARY)
target_link_libraries(${${prefix}_NAME} ${Matlab_ENGINE_LIBRARY})
endif()
if(Matlab_DATAARRAY_LIBRARY)
target_link_libraries(${${prefix}_NAME} ${Matlab_DATAARRAY_LIBRARY})
endif()
endif()

target_link_libraries(${${prefix}_NAME} ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY})
endif()

target_link_libraries(${${prefix}_NAME} ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY} ${${prefix}_LINK_TO})
target_link_libraries(${${prefix}_NAME} ${${prefix}_LINK_TO})
set_target_properties(${${prefix}_NAME}
PROPERTIES
PREFIX ""
Expand Down Expand Up @@ -1650,32 +1718,34 @@ find_path(
)
list(APPEND _matlab_required_variables Matlab_INCLUDE_DIRS)

_Matlab_find_library(
${_matlab_lib_prefix_for_search}
Matlab_MEX_LIBRARY
mex
PATHS ${_matlab_lib_dir_for_search}
NO_DEFAULT_PATH
)
list(APPEND _matlab_required_variables Matlab_MEX_LIBRARY)
if(Matlab_Or_MCR STREQUAL "MATLAB" OR Matlab_Or_MCR STREQUAL "UNKNOWN")
_Matlab_find_library(
${_matlab_lib_prefix_for_search}
Matlab_MEX_LIBRARY
mex
PATHS ${_matlab_lib_dir_for_search}
NO_DEFAULT_PATH
)
list(APPEND _matlab_required_variables Matlab_MEX_LIBRARY)

# the MEX extension is required
list(APPEND _matlab_required_variables Matlab_MEX_EXTENSION)
# the MEX extension is required
list(APPEND _matlab_required_variables Matlab_MEX_EXTENSION)

# the matlab root is required
list(APPEND _matlab_required_variables Matlab_ROOT_DIR)
# the matlab root is required
list(APPEND _matlab_required_variables Matlab_ROOT_DIR)

# The MX library is required
_Matlab_find_library(
${_matlab_lib_prefix_for_search}
Matlab_MX_LIBRARY
mx
PATHS ${_matlab_lib_dir_for_search}
NO_DEFAULT_PATH
)
list(APPEND _matlab_required_variables Matlab_MX_LIBRARY)
if(Matlab_MX_LIBRARY)
set(Matlab_MX_LIBRARY_FOUND TRUE)
# The MX library is required
_Matlab_find_library(
${_matlab_lib_prefix_for_search}
Matlab_MX_LIBRARY
mx
PATHS ${_matlab_lib_dir_for_search}
NO_DEFAULT_PATH
)
list(APPEND _matlab_required_variables Matlab_MX_LIBRARY)
if(Matlab_MX_LIBRARY)
set(Matlab_MX_LIBRARY_FOUND TRUE)
endif()
endif()

if(Matlab_HAS_CPP_API)
Expand All @@ -1689,7 +1759,6 @@ if(Matlab_HAS_CPP_API)
DOC "MatlabEngine Library"
NO_DEFAULT_PATH
)
list(APPEND _matlab_required_variables Matlab_ENGINE_LIBRARY)
if(Matlab_ENGINE_LIBRARY)
set(Matlab_ENGINE_LIBRARY_FOUND TRUE)
endif()
Expand All @@ -1703,7 +1772,6 @@ if(Matlab_HAS_CPP_API)
DOC "MatlabDataArray Library"
NO_DEFAULT_PATH
)
list(APPEND _matlab_required_variables Matlab_DATAARRAY_LIBRARY)
if(Matlab_DATAARRAY_LIBRARY)
set(Matlab_DATAARRAY_LIBRARY_FOUND TRUE)
endif()
Expand Down
3 changes: 2 additions & 1 deletion sources/Simulink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ matlab_add_mex(
src/SimulinkBlockInformationImpl.cpp
include/BlockFactory/Simulink/Private/SimulinkBlockInformationImpl.h
src/BlockFactory.cpp
LINK_TO BlockFactory::Core shlibpp::shlibpp mxpp)
NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES
LINK_TO ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY} BlockFactory::Core shlibpp::shlibpp mxpp)
add_library(BlockFactory::Simulink ALIAS Simulink)

target_compile_warnings(Simulink
Expand Down

0 comments on commit 26e5f3d

Please sign in to comment.