Merge topic 'cmake-initial-cache-dirs'

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

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3709
This commit is contained in:
Brad King 2019-08-27 15:23:03 +00:00 committed by Kitware Robot
commit 843ab7544e
10 changed files with 51 additions and 10 deletions

View File

@ -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.

View File

@ -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.

View File

@ -431,6 +431,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;
@ -460,15 +465,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) {
@ -481,8 +480,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

View File

@ -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

View File

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

View File

@ -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}")

View File

@ -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}")

View File

@ -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")

View File

@ -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")