Преглед на файлове

cmake: Pass -S and -B into PreLoad.cmake and -C scripts

Before this, it was not possible to see what the source or build directories
were set to on the command line.

Fixes: #19619
Peter Waller преди 6 години
родител
ревизия
4ca0526f8a

+ 3 - 0
Help/manual/OPTIONS_BUILD.txt

@@ -18,6 +18,9 @@
  containing :command:`set` commands that use the ``CACHE`` option, not a
  cache-format file.
 
+ References to :variable:`CMAKE_SOURCE_DIR` and :variable:`CMAKE_BINARY_DIR`
+ within the script evaluate to the top-level source and build tree.
+
 ``-D <var>:<type>=<value>, -D <var>=<value>``
  Create or update a CMake ``CACHE`` entry.
 

+ 6 - 0
Help/release/dev/cmake-initial-cache-dirs.rst

@@ -0,0 +1,6 @@
+cmake-initial-cache-dirs
+------------------------
+
+* The :manual:`cmake(1)` ``-C <initial-cache>`` option now evaluates the
+  initial cache script with :variable:`CMAKE_SOURCE_DIR` and
+  :variable:`CMAKE_BINARY_DIR` set to the top-level source and build trees.

+ 7 - 10
Source/cmake.cxx

@@ -430,6 +430,11 @@ bool cmake::SetCacheArgs(const std::vector<std::string>& args)
       }
       // Register fake project commands that hint misuse in script mode.
       GetProjectCommandsInScriptMode(this->GetState());
+      // Documented behaviour of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be
+      // set to $PWD for -P mode.
+      this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
+      this->SetHomeOutputDirectory(
+        cmSystemTools::GetCurrentWorkingDirectory());
       this->ReadListFile(args, path);
     } else if (arg.find("--find-package", 0) == 0) {
       findPackageMode = true;
@@ -459,15 +464,9 @@ void cmake::ReadListFile(const std::vector<std::string>& args,
   // read in the list file to fill the cache
   if (!path.empty()) {
     this->CurrentSnapshot = this->State->Reset();
-    std::string homeDir = this->GetHomeDirectory();
-    std::string homeOutputDir = this->GetHomeOutputDirectory();
-    this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
-    this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
     cmStateSnapshot snapshot = this->GetCurrentSnapshot();
-    snapshot.GetDirectory().SetCurrentBinary(
-      cmSystemTools::GetCurrentWorkingDirectory());
-    snapshot.GetDirectory().SetCurrentSource(
-      cmSystemTools::GetCurrentWorkingDirectory());
+    snapshot.GetDirectory().SetCurrentBinary(this->GetHomeOutputDirectory());
+    snapshot.GetDirectory().SetCurrentSource(this->GetHomeDirectory());
     snapshot.SetDefaultDefinitions();
     cmMakefile mf(gg, snapshot);
     if (this->GetWorkingMode() != NORMAL_MODE) {
@@ -480,8 +479,6 @@ void cmake::ReadListFile(const std::vector<std::string>& args,
     if (!mf.ReadListFile(path)) {
       cmSystemTools::Error("Error processing file: " + path);
     }
-    this->SetHomeDirectory(homeDir);
-    this->SetHomeOutputDirectory(homeOutputDir);
   }
 
   // free generic one if generated

+ 8 - 0
Tests/RunCMake/CommandLine/C_buildsrcdir-stderr.txt

@@ -0,0 +1,8 @@
+initial-cache.txt: CMAKE_SOURCE_DIR: .*/C_buildsrcdir/src
+initial-cache.txt: CMAKE_BINARY_DIR: .*/C_buildsrcdir-build/DummyBuildDir
+PreLoad.cmake: CMAKE_SOURCE_DIR: .*/C_buildsrcdir/src
+PreLoad.cmake: CMAKE_BINARY_DIR: .*/C_buildsrcdir-build/DummyBuildDir
+CMakeLists.txt: INITIAL_SOURCE_DIR: .*/C_buildsrcdir/src
+CMakeLists.txt: INITIAL_BINARY_DIR: .*/C_buildsrcdir-build/DummyBuildDir
+CMakeLists.txt: PRELOAD_SOURCE_DIR: .*/C_buildsrcdir/src
+CMakeLists.txt: PRELOAD_BINARY_DIR: .*/C_buildsrcdir-build/DummyBuildDir

+ 2 - 0
Tests/RunCMake/CommandLine/C_buildsrcdir-stdout.txt

@@ -0,0 +1,2 @@
+loading initial cache file .*/C_buildsrcdir/initial-cache.txt
+.*

+ 0 - 0
Tests/RunCMake/CommandLine/C_buildsrcdir.cmake


+ 6 - 0
Tests/RunCMake/CommandLine/C_buildsrcdir/initial-cache.txt

@@ -0,0 +1,6 @@
+# Used to verify that the values match what is passed via -S and -B, and are retained in cache.
+set(INITIAL_SOURCE_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "defined in initial.cmake")
+set(INITIAL_BINARY_DIR "${CMAKE_BINARY_DIR}" CACHE PATH "defined in initial.cmake")
+
+message("initial-cache.txt: CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
+message("initial-cache.txt: CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")

+ 6 - 0
Tests/RunCMake/CommandLine/C_buildsrcdir/src/CMakeLists.txt

@@ -0,0 +1,6 @@
+project(C_buildsrcdir)
+
+message("CMakeLists.txt: INITIAL_SOURCE_DIR: ${INITIAL_SOURCE_DIR}")
+message("CMakeLists.txt: INITIAL_BINARY_DIR: ${INITIAL_BINARY_DIR}")
+message("CMakeLists.txt: PRELOAD_SOURCE_DIR: ${PRELOAD_SOURCE_DIR}")
+message("CMakeLists.txt: PRELOAD_BINARY_DIR: ${PRELOAD_BINARY_DIR}")

+ 6 - 0
Tests/RunCMake/CommandLine/C_buildsrcdir/src/PreLoad.cmake

@@ -0,0 +1,6 @@
+# Used to verify that the values match what is passed via -S and -B, and are retained in cache.
+message("PreLoad.cmake: CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
+message("PreLoad.cmake: CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
+
+set(PRELOAD_BINARY_DIR "${CMAKE_BINARY_DIR}" CACHE PATH "value of cmake_binary_dir during preload")
+set(PRELOAD_SOURCE_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "value of cmake_source_dir during preload")

+ 7 - 0
Tests/RunCMake/CommandLine/RunCMakeTest.cmake

@@ -391,6 +391,13 @@ run_cmake_command(E_sleep-one-tenth ${CMAKE_COMMAND} -E sleep 0.1)
 
 run_cmake_command(P_directory ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR})
 run_cmake_command(P_working-dir ${CMAKE_COMMAND} -DEXPECTED_WORKING_DIR=${RunCMake_BINARY_DIR}/P_working-dir-build -P ${RunCMake_SOURCE_DIR}/P_working-dir.cmake)
+# Documented to return the same result as above even if -S and -B are set to something else.
+# Tests the values of CMAKE_BINARY_DIR CMAKE_CURRENT_BINARY_DIR CMAKE_SOURCE_DIR CMAKE_CURRENT_SOURCE_DIR.
+run_cmake_command(P_working-dir ${CMAKE_COMMAND} -DEXPECTED_WORKING_DIR=${RunCMake_BINARY_DIR}/P_working-dir-build -P ${RunCMake_SOURCE_DIR}/P_working-dir.cmake -S something_else -B something_else_1)
+
+# CMAKE_BINARY_DIR should be determined by -B if specified, and CMAKE_SOURCE_DIR determined by -S if specified.
+run_cmake_with_options(C_buildsrcdir -B DummyBuildDir -S ${RunCMake_SOURCE_DIR}/C_buildsrcdir/src -C ${RunCMake_SOURCE_DIR}/C_buildsrcdir/initial-cache.txt)
+
 
 set(RunCMake_TEST_OPTIONS
   "-DFOO=-DBAR:BOOL=BAZ")