Browse Source

Optionally make `test` target depend on `all`

Fixes: #8774
William Sciaroni 2 years ago
parent
commit
5e0c1777a3

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

@@ -1656,6 +1656,7 @@ syn keyword cmakeVariable contained
             \ CMAKE_SKIP_INSTALL_RPATH
             \ CMAKE_SKIP_INSTALL_RULES
             \ CMAKE_SKIP_RPATH
+            \ CMAKE_SKIP_TEST_ALL_DEPENDENCY
             \ CMAKE_SOURCE_DIR
             \ CMAKE_STAGING_PREFIX
             \ CMAKE_STATIC_LIBRARY_PREFIX

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

@@ -255,6 +255,7 @@ Variables that Change Behavior
    /variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES
    /variable/CMAKE_REQUIRE_FIND_PACKAGE_PackageName
    /variable/CMAKE_SKIP_INSTALL_ALL_DEPENDENCY
+   /variable/CMAKE_SKIP_TEST_ALL_DEPENDENCY
    /variable/CMAKE_STAGING_PREFIX
    /variable/CMAKE_SUBLIME_TEXT_2_ENV_SETTINGS
    /variable/CMAKE_SUBLIME_TEXT_2_EXCLUDE_BUILD_TREE

+ 6 - 0
Help/release/dev/make-test-depend-on-all.rst

@@ -0,0 +1,6 @@
+make-test-depend-on-all
+-----------------------
+
+* The :variable:`CMAKE_SKIP_TEST_ALL_DEPENDENCY` variable was added
+  to control whether the ``test`` (or ``RUN_TESTS``) buildsystem
+  target depends on the ``all`` (or ``ALL_BUILD``) target.

+ 2 - 0
Help/variable/CMAKE_SKIP_INSTALL_ALL_DEPENDENCY.rst

@@ -9,3 +9,5 @@ built, first the ``all`` target is built, then the installation starts.
 If ``CMAKE_SKIP_INSTALL_ALL_DEPENDENCY`` is set to ``TRUE``, this
 dependency is not created, so the installation process will start immediately,
 independent from whether the project has been completely built or not.
+
+See also :variable:`CMAKE_SKIP_TEST_ALL_DEPENDENCY`.

+ 19 - 0
Help/variable/CMAKE_SKIP_TEST_ALL_DEPENDENCY.rst

@@ -0,0 +1,19 @@
+CMAKE_SKIP_TEST_ALL_DEPENDENCY
+------------------------------
+
+.. versionadded:: 3.29
+
+Control whether the ``test`` target depends on the ``all`` target.
+
+If this variable is not defined, or is set to ``TRUE``, then the
+``test`` (or ``RUN_TESTS``) target does not depend on the
+``all`` (or ``ALL_BUILD``) target.  When the ``test`` target is built,
+e.g., via ``make test``, the test process will start immediately,
+regardless of whether the project has been completely built or not.
+
+If ``CMAKE_SKIP_TEST_ALL_DEPENDENCY`` is explicitly set to ``FALSE``,
+then the ``test`` target will depend on the ``all`` target.  When the
+``test`` target is built, e.g., via ``make test``, the ``all`` target
+will be built first, and then the tests will run.
+
+See also :variable:`CMAKE_SKIP_INSTALL_ALL_DEPENDENCY`.

+ 8 - 0
Source/cmGlobalGenerator.cxx

@@ -2865,6 +2865,14 @@ void cmGlobalGenerator::AddGlobalTarget_Test(
   gti.Name = this->GetTestTargetName();
   gti.Message = "Running tests...";
   gti.UsesTerminal = true;
+  // Unlike the 'install' target, the 'test' target does not depend on 'all'
+  // by default.  Enable it only if CMAKE_SKIP_TEST_ALL_DEPENDENCY is
+  // explicitly set to OFF.
+  if (cmValue noall = mf->GetDefinition("CMAKE_SKIP_TEST_ALL_DEPENDENCY")) {
+    if (cmIsOff(noall)) {
+      gti.Depends.emplace_back(this->GetAllTargetName());
+    }
+  }
   cmCustomCommandLine singleLine;
   singleLine.push_back(cmSystemTools::GetCTestCommand());
   singleLine.push_back("--force-new-ctest-process");

+ 2 - 0
Tests/RunCMake/BuiltinTargets/RunCMakeTest.cmake

@@ -15,3 +15,5 @@ function(run_BuiltinTarget case target)
 endfunction()
 
 run_BuiltinTarget(TestDependsAll-Default test)
+run_BuiltinTarget(TestDependsAll-No test)
+run_BuiltinTarget(TestDependsAll-Yes test)

+ 2 - 0
Tests/RunCMake/BuiltinTargets/TestDependsAll-No.cmake

@@ -0,0 +1,2 @@
+include(TestDependsAll-common.cmake)
+set(CMAKE_SKIP_TEST_ALL_DEPENDENCY ON)

+ 3 - 0
Tests/RunCMake/BuiltinTargets/TestDependsAll-Yes-build-check.cmake

@@ -0,0 +1,3 @@
+if(NOT EXISTS ${RunCMake_TEST_BINARY_DIR}/custom-output.txt)
+  set(RunCMake_TEST_FAILED "Building 'test' target did not build 'all' target:\n ${RunCMake_TEST_BINARY_DIR}/custom-output.txt")
+endif()

+ 2 - 0
Tests/RunCMake/BuiltinTargets/TestDependsAll-Yes.cmake

@@ -0,0 +1,2 @@
+include(TestDependsAll-common.cmake)
+set(CMAKE_SKIP_TEST_ALL_DEPENDENCY OFF)