FindMatlab: Add NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES option to matlab_add_mex
diff --git a/Help/release/dev/FindMatlab-no-implicit-link.txt b/Help/release/dev/FindMatlab-no-implicit-link.txt
new file mode 100644
index 0000000..7ecc17f
--- /dev/null
+++ b/Help/release/dev/FindMatlab-no-implicit-link.txt
@@ -0,0 +1,6 @@
+FindMatlab-no-implicit-link
+---------------------------
+
+* The :module:`FindMatlab` module :command:`matlab_add_mex` function
+  gained a ``NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES`` option to disable
+  automatic linking of MATLAB libraries.
diff --git a/Modules/FindMatlab.cmake b/Modules/FindMatlab.cmake
index 81bc473..48ef5eb 100644
--- a/Modules/FindMatlab.cmake
+++ b/Modules/FindMatlab.cmake
@@ -1020,6 +1020,7 @@
          [LINK_TO target1 target2 ...]
          [R2017b | R2018a]
          [EXCLUDE_FROM_ALL]
+         [NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES]
          [...]
      )
 
@@ -1029,7 +1030,8 @@
     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 the
+    ``NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES`` option is passed.
   ``OUTPUT_NAME``
     if given, overrides the default name. The default name is
     the name of the target without any prefix and
@@ -1065,6 +1067,12 @@
     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``
+    .. versionadded:: 3.24
+
+    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:
@@ -1091,7 +1099,7 @@
 
   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)
 
@@ -1163,16 +1171,19 @@
 
   target_include_directories(${${prefix}_NAME} PRIVATE ${Matlab_INCLUDE_DIRS})
 
-  if(Matlab_HAS_CPP_API)
-    if(Matlab_ENGINE_LIBRARY)
-      target_link_libraries(${${prefix}_NAME} ${Matlab_ENGINE_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()
-    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} ${${prefix}_LINK_TO})
+    target_link_libraries(${${prefix}_NAME} ${Matlab_MEX_LIBRARY} ${Matlab_MX_LIBRARY})
+  endif()
+  target_link_libraries(${${prefix}_NAME} ${${prefix}_LINK_TO})
   set_target_properties(${${prefix}_NAME}
       PROPERTIES
         PREFIX ""
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 3ce9b48..d663a14 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -1558,6 +1558,9 @@
     set(FindMatlab.targets_checks_BUILD_OPTIONS ${FindMatlab_additional_test_options})
     ADD_TEST_MACRO(FindMatlab.targets_checks      ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>)
     set_property(TEST FindMatlab.targets_checks APPEND PROPERTY LABELS "Matlab")
+    set(FindMatlab.no_implicit_link_checks_BUILD_OPTIONS ${FindMatlab_additional_test_options})
+    ADD_TEST_MACRO(FindMatlab.no_implicit_link_checks      ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>)
+    set_property(TEST FindMatlab.no_implicit_link_checks APPEND PROPERTY LABELS "Matlab")
   endif()
 
   set(ExternalProject_BUILD_OPTIONS "")
diff --git a/Tests/FindMatlab/no_implicit_link_checks/CMakeLists.txt b/Tests/FindMatlab/no_implicit_link_checks/CMakeLists.txt
new file mode 100644
index 0000000..bceeba1
--- /dev/null
+++ b/Tests/FindMatlab/no_implicit_link_checks/CMakeLists.txt
@@ -0,0 +1,70 @@
+
+cmake_minimum_required (VERSION 2.8.12)
+enable_testing()
+project(no_implicit_links_checks)
+
+set(MATLAB_FIND_DEBUG TRUE)
+
+if(IS_MCR)
+    set(RUN_UNIT_TESTS FALSE)
+else()
+    set(RUN_UNIT_TESTS TRUE)
+    set(components MAIN_PROGRAM)
+endif()
+
+if(NOT "${MCR_ROOT}" STREQUAL "")
+    set(Matlab_ROOT_DIR "${MCR_ROOT}")
+    if(NOT EXISTS "${MCR_ROOT}")
+        message(FATAL_ERROR "MCR does not exist ${MCR_ROOT}")
+    endif()
+endif()
+
+find_package(Matlab REQUIRED COMPONENTS ${components})
+
+
+matlab_add_mex(
+    # target name
+    NAME cmake_matlab_test_wrapper1
+    # output name
+    OUTPUT_NAME cmake_matlab_mex1
+    SRC ${CMAKE_CURRENT_SOURCE_DIR}/../matlab_wrapper1.cpp
+    DOCUMENTATION ${CMAKE_CURRENT_SOURCE_DIR}/../help_text1.m.txt
+    NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES
+    )
+
+# Inspect the LINK_LIBRARIES properties to check if mex or mx are present
+get_target_property(MATLAB_TEST_WRAPPER1_LINK_LIBRARIES
+                    cmake_matlab_test_wrapper1 LINK_LIBRARIES)
+
+string(FIND "${MATLAB_TEST_WRAPPER1_LINK_LIBRARIES}" "mx" SEARCH_RESULT)
+if(NOT "${SEARCH_RESULT}" EQUAL "-1")
+    message(FATAL_ERROR "Matlab::mx linked even if NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES was passed")
+endif()
+
+string(FIND "${MATLAB_TEST_WRAPPER1_LINK_LIBRARIES}" "mex" SEARCH_RESULT)
+if(NOT "${SEARCH_RESULT}" EQUAL "-1")
+    message(FATAL_ERROR "Matlab::mex linked even if NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES was passed")
+endif()
+
+string(FIND "${MATLAB_TEST_WRAPPER1_LINK_LIBRARIES}" "MatlabEngine" SEARCH_RESULT)
+if(NOT "${SEARCH_RESULT}" EQUAL "-1")
+    message(FATAL_ERROR "Matlab::MatlabEngine linked even if NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES was passed")
+endif()
+
+string(FIND "${MATLAB_TEST_WRAPPER1_LINK_LIBRARIES}" "MatlabDataArray" SEARCH_RESULT)
+if(NOT "${SEARCH_RESULT}" EQUAL "-1")
+    message(FATAL_ERROR "Matlab::MatlabDataArray linked even if NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES was passed")
+endif()
+
+# Link separately with Matlab::mx and Matlab::mex to ensure that compilation
+# and run of the test is successful
+target_link_libraries(cmake_matlab_test_wrapper1 PRIVATE Matlab::mx Matlab::mex)
+
+if(RUN_UNIT_TESTS)
+    matlab_add_unit_test(
+        NAME ${PROJECT_NAME}_matlabtest-1
+        TIMEOUT 300
+        UNITTEST_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../cmake_matlab_unit_tests1.m
+        ADDITIONAL_PATH $<TARGET_FILE_DIR:cmake_matlab_test_wrapper1>
+        )
+endif()