Browse Source

Add variable CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY

Add CMake variable `CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY` to enable
option `DEPENDS_EXPLICIT_ONLY` on all uses of `add_custom_command`.

Fixes: #17097
Abdelmaged Khalifa 2 years ago
parent
commit
ea2a05f402

+ 2 - 0
Auxiliary/vim/syntax/cmake.vim

@@ -459,6 +459,7 @@ syn keyword cmakeVariable contained
             \ BUILD_SHARED_LIBS
             \ CACHE
             \ CMAKE_ABSOLUTE_DESTINATION_FILES
+            \ CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY
             \ CMAKE_AIX_EXPORT_ALL_SYMBOLS
             \ CMAKE_ANDROID_ANT_ADDITIONAL_OPTIONS
             \ CMAKE_ANDROID_API
@@ -2095,6 +2096,7 @@ syn keyword cmakeKWadd_custom_command contained
             \ COMMENT
             \ CROSSCOMPILING_EMULATOR
             \ DEPENDS
+            \ DEPENDS_EXPLICIT_ONLY
             \ DEPFILE
             \ GENERATED
             \ IMPLICIT_DEPENDS

+ 3 - 0
Help/command/add_custom_command.rst

@@ -370,6 +370,9 @@ The options are:
   the custom command in case this custom command requires files implicitly
   created by those targets.
 
+  This option can be enabled on all custom commands by setting
+  :variable:`CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY` to ``ON``.
+
   Only the :ref:`Ninja Generators` actually use this information to remove
   unnecessary implicit dependencies.
 

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -166,6 +166,7 @@ Variables that Change Behavior
 
    /variable/BUILD_SHARED_LIBS
    /variable/CMAKE_ABSOLUTE_DESTINATION_FILES
+   /variable/CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY
    /variable/CMAKE_APPBUNDLE_PATH
    /variable/CMAKE_AUTOMOC_RELAXED_MODE
    /variable/CMAKE_BACKWARDS_COMPATIBILITY

+ 4 - 0
Help/release/dev/ninja-custom-command-depends.rst

@@ -5,3 +5,7 @@ ninja-custom-command-depends
   ``DEPENDS_EXPLICIT_ONLY`` option to tell the :ref:`Ninja Generators`
   not to add any dependencies implied by the target to which it is
   attached.
+
+* The :variable:`CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY` variable can
+  be set to enable ``DEPENDS_EXPLICIT_ONLY`` in all uses of
+  :command:`add_custom_command` command.

+ 11 - 0
Help/variable/CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY.rst

@@ -0,0 +1,11 @@
+CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY
+----------------------------------------------
+
+.. versionadded:: 3.27
+
+Whether to enable DEPENDS_EXPLICIT_ONLY option by default in
+:command:`add_custom_command`.
+
+This variable affects the default behavior of the :command:`add_custom_command`
+command.  Setting this variable to ``ON`` is equivalent to using the ``DEPENDS_EXPLICIT_ONLY``
+option in all uses of that command.

+ 2 - 1
Source/cmAddCustomCommandCommand.cxx

@@ -49,7 +49,8 @@ bool cmAddCustomCommandCommand(std::vector<std::string> const& args,
   bool append = false;
   bool uses_terminal = false;
   bool command_expand_lists = false;
-  bool depends_explicit_only = false;
+  bool depends_explicit_only =
+    mf.IsOn("CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY");
   std::string implicit_depends_lang;
   cmImplicitDependsList implicit_depends;
 

+ 24 - 6
Tests/RunCMake/Ninja/CustomCommandExplicitDepends.cmake

@@ -2,17 +2,35 @@ cmake_minimum_required(VERSION 3.26)
 project(CustomCommandExplicitDepends C)
 
 add_custom_command(
-  OUTPUT  "${CMAKE_CURRENT_BINARY_DIR}/command.h"
+  OUTPUT  "${CMAKE_CURRENT_BINARY_DIR}/command-option.h"
   COMMAND "${CMAKE_COMMAND}" -E touch
-          "${CMAKE_CURRENT_BINARY_DIR}/command.h"
-  COMMENT "Creating command.h"
+          "${CMAKE_CURRENT_BINARY_DIR}/command-option.h"
+  COMMENT "Creating command-option.h"
   DEPENDS_EXPLICIT_ONLY
 )
 
-add_library(dep STATIC dep.c)
+set(CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY ON)
+add_custom_command(
+  OUTPUT  "${CMAKE_CURRENT_BINARY_DIR}/command-variable-on.h"
+  COMMAND "${CMAKE_COMMAND}" -E touch
+          "${CMAKE_CURRENT_BINARY_DIR}/command-variable-on.h"
+  COMMENT "Creating command-variable-on.h"
+)
+
+set(CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY OFF)
+add_custom_command(
+  OUTPUT  "${CMAKE_CURRENT_BINARY_DIR}/command-variable-off.h"
+  COMMAND "${CMAKE_COMMAND}" -E touch
+          "${CMAKE_CURRENT_BINARY_DIR}/command-variable-off.h"
+  COMMENT "Creating command-variable-off.h"
+)
+
+add_library(dep SHARED dep.c)
 
-add_library(top STATIC
+add_library(top SHARED
   top.c
-  "${CMAKE_CURRENT_BINARY_DIR}/command.h"
+  "${CMAKE_CURRENT_BINARY_DIR}/command-option.h"
+  "${CMAKE_CURRENT_BINARY_DIR}/command-variable-on.h"
+  "${CMAKE_CURRENT_BINARY_DIR}/command-variable-off.h"
 )
 target_link_libraries(top PRIVATE dep)

+ 24 - 4
Tests/RunCMake/Ninja/RunCMakeTest.cmake

@@ -193,11 +193,31 @@ run_LooseObjectDepends()
 function (run_CustomCommandExplictDepends)
   set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CustomCommandExplicitDepends-build)
   run_cmake(CustomCommandExplicitDepends)
-  run_ninja("${RunCMake_TEST_BINARY_DIR}" "command.h")
-  if (EXISTS "${RunCMake_TEST_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}dep${CMAKE_STATIC_LIBRARY_SUFFIX}")
+
+  set(DEP_LIB "${RunCMake_TEST_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}dep${CMAKE_SHARED_LIBRARY_SUFFIX}")
+
+  run_ninja("${RunCMake_TEST_BINARY_DIR}" "command-option.h")
+  if (EXISTS "${DEP_LIB}")
+    message(FATAL_ERROR
+      "The `dep` library was created when requesting a custom command to be "
+      "generated; this should no longer be necessary when passing "
+      "DEPENDS_EXPLICIT_ONLY option.")
+  endif ()
+
+  run_ninja("${RunCMake_TEST_BINARY_DIR}" "command-variable-on.h")
+  if (EXISTS "${DEP_LIB}")
+    message(FATAL_ERROR
+      "The `dep` library was created when requesting a custom command to be "
+      "generated; this should no longer be necessary when setting "
+      "CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY variable to ON.")
+  endif ()
+
+  run_ninja("${RunCMake_TEST_BINARY_DIR}" "command-variable-off.h")
+  if (NOT EXISTS "${DEP_LIB}")
     message(FATAL_ERROR
-      "The `dep` library was created when requesting an custom command to be "
-      "generated; this should no longer be necessary when passing DEPENDS_EXPLICIT_ONLY keyword.")
+      "The `dep` library was not created when requesting a custom command to be "
+      "generated; this should be necessary when setting "
+      "CMAKE_ADD_CUSTOM_COMMAND_DEPENDS_EXPLICIT_ONLY variable to OFF.")
   endif ()
 endfunction ()
 run_CustomCommandExplictDepends()