From b7d7eca66db97f9dcb6068762733f06941c0c05a Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Fri, 23 Oct 2020 12:37:06 -0400 Subject: CMakePresets.json: Rework how --preset argument is handled If a path argument with no -S or -B leads to a cache directory, use that directory as the binary directory. Otherwise, use the binary directory from the preset. Fixes: #21311 --- Help/manual/cmake-gui.1.rst | 5 ++-- Help/manual/cmake.1.rst | 32 ++++++++++------------ Source/QtDialog/CMakeSetup.cxx | 3 +- Source/cmCacheManager.cxx | 1 + Source/cmCacheManager.h | 4 +++ Source/cmState.cxx | 5 ++++ Source/cmState.h | 2 ++ Source/cmake.cxx | 4 ++- Source/cmakemain.cxx | 3 +- Tests/RunCMake/CMakePresets/CMakePresets.json.in | 10 +++++++ Tests/RunCMake/CMakePresets/GoodNoS.cmake | 3 +- Tests/RunCMake/CMakePresets/GoodNoSCache.cmake | 4 +++ Tests/RunCMake/CMakePresets/GoodNoSCachePrep.cmake | 4 +++ Tests/RunCMake/CMakePresets/RunCMakeTest.cmake | 16 ++++++++--- Tests/RunCMake/CommandLine/NoArgs-stdout.txt | 1 - 15 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 Tests/RunCMake/CMakePresets/GoodNoSCache.cmake create mode 100644 Tests/RunCMake/CMakePresets/GoodNoSCachePrep.cmake diff --git a/Help/manual/cmake-gui.1.rst b/Help/manual/cmake-gui.1.rst index d9c8ed7..97b5731 100644 --- a/Help/manual/cmake-gui.1.rst +++ b/Help/manual/cmake-gui.1.rst @@ -11,7 +11,6 @@ Synopsis cmake-gui [] cmake-gui [] { | } cmake-gui [] -S -B - cmake-gui [] -S --preset= Description =========== @@ -38,8 +37,8 @@ Options If the directory doesn't already exist CMake will make it. ``--preset=`` - Name of the preset to use from the project's ``CMakePresets.json`` file, if it - has one. + Name of the preset to use from the project's + :manual:`presets ` files, if it has them. .. include:: OPTIONS_HELP.txt diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index 5452c79..921f5c4 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -12,7 +12,6 @@ Synopsis cmake [] cmake [] cmake [] -S -B - cmake [] -S --preset= `Build a Project`_ cmake --build [] [-- ] @@ -149,22 +148,6 @@ source and build trees and generate a buildsystem: $ cmake -S src -B build -``cmake [] -S --preset=`` - Uses ```` as the source tree and reads a - :manual:`preset ` from - ``/CMakePresets.json`` and - ``/CMakeUserPresets.json``. The preset specifies the - generator and the build directory, and optionally a list of variables and - other arguments to pass to CMake. The :manual:`CMake GUI ` can - also recognize ``CMakePresets.json`` and ``CMakeUserPresets.json`` files. For - full details on these files, see :manual:`cmake-presets(7)`. - - The presets are read before all other command line options. The options - specified by the preset (variables, generator, etc.) can all be overridden by - manually specifying them on the command line. For example, if the preset sets - a variable called ``MYVAR`` to ``1``, but the user sets it to ``2`` with a - ``-D`` argument, the value ``2`` is preferred. - In all cases the ```` may be zero or more of the `Options`_ below. After generating a buildsystem one may use the corresponding native @@ -394,6 +377,21 @@ Options about:tracing tab of Google Chrome or using a plugin for a tool like Trace Compass. +``--preset=`` + Reads a :manual:`preset ` from + ``/CMakePresets.json`` and + ``/CMakeUserPresets.json``. The preset specifies the + generator and the build directory, and optionally a list of variables and + other arguments to pass to CMake. The :manual:`CMake GUI ` can + also recognize ``CMakePresets.json`` and ``CMakeUserPresets.json`` files. For + full details on these files, see :manual:`cmake-presets(7)`. + + The presets are read before all other command line options. The options + specified by the preset (variables, generator, etc.) can all be overridden by + manually specifying them on the command line. For example, if the preset sets + a variable called ``MYVAR`` to ``1``, but the user sets it to ``2`` with a + ``-D`` argument, the value ``2`` is preferred. + .. _`Build Tool Mode`: Build a Project diff --git a/Source/QtDialog/CMakeSetup.cxx b/Source/QtDialog/CMakeSetup.cxx index a5b2f34..182c23d 100644 --- a/Source/QtDialog/CMakeSetup.cxx +++ b/Source/QtDialog/CMakeSetup.cxx @@ -32,8 +32,7 @@ static const char* cmDocumentationUsage[][2] = { " cmake-gui [options]\n" " cmake-gui [options] \n" " cmake-gui [options] \n" - " cmake-gui [options] -S -B \n" - " cmake-gui [options] -S --preset=\n" }, + " cmake-gui [options] -S -B \n" }, { nullptr, nullptr } }; diff --git a/Source/cmCacheManager.cxx b/Source/cmCacheManager.cxx index 95686ea..8d1a5fd 100644 --- a/Source/cmCacheManager.cxx +++ b/Source/cmCacheManager.cxx @@ -162,6 +162,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal, cmSystemTools::Error(message.str()); } } + this->CacheLoaded = true; return true; } diff --git a/Source/cmCacheManager.h b/Source/cmCacheManager.h index 20d49a8..9aebffc 100644 --- a/Source/cmCacheManager.h +++ b/Source/cmCacheManager.h @@ -66,6 +66,9 @@ public: //! Print the cache to a stream void PrintCache(std::ostream&) const; + //! Get whether or not cache is loaded + bool IsCacheLoaded() const { return this->CacheLoaded; } + //! Get a value from the cache given a key cmProp GetInitializedCacheValue(const std::string& key) const; @@ -204,6 +207,7 @@ private: const CacheEntry& e, cmMessenger* messenger) const; std::map Cache; + bool CacheLoaded = false; // Cache version info unsigned int CacheMajorVersion = 0; diff --git a/Source/cmState.cxx b/Source/cmState.cxx index d5ac9ae..d268e62 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -135,6 +135,11 @@ bool cmState::DeleteCache(const std::string& path) return this->CacheManager->DeleteCache(path); } +bool cmState::IsCacheLoaded() const +{ + return this->CacheManager->IsCacheLoaded(); +} + std::vector cmState::GetCacheEntryKeys() const { return this->CacheManager->GetCacheEntryKeys(); diff --git a/Source/cmState.h b/Source/cmState.h index 2aa57e0..e4c9eb5 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -87,6 +87,8 @@ public: bool DeleteCache(const std::string& path); + bool IsCacheLoaded() const; + std::vector GetCacheEntryKeys() const; cmProp GetCacheEntryValue(std::string const& key) const; std::string GetSafeCacheEntryValue(std::string const& key) const; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 291ce22..8d4cd03 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -727,6 +727,7 @@ void cmake::SetArgs(const std::vector& args) { bool haveToolset = false; bool havePlatform = false; + bool haveBArg = false; #if !defined(CMAKE_BOOTSTRAP) std::string profilingFormat; std::string profilingOutput; @@ -775,6 +776,7 @@ void cmake::SetArgs(const std::vector& args) path = cmSystemTools::CollapseFullPath(path); cmSystemTools::ConvertToUnixSlashes(path); this->SetHomeOutputDirectory(path); + haveBArg = true; } else if ((i < args.size() - 2) && cmHasLiteralPrefix(arg, "--check-build-system")) { this->CheckBuildSystemArgument = args[++i]; @@ -1057,7 +1059,7 @@ void cmake::SetArgs(const std::vector& args) return; } - if (!haveBinaryDir) { + if (!this->State->IsCacheLoaded() && !haveBArg) { this->SetHomeOutputDirectory(expandedPreset->BinaryDir); } if (!this->GlobalGenerator) { diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index f570337..f0963c2 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -49,8 +49,7 @@ const char* cmDocumentationUsage[][2] = { { nullptr, " cmake [options] \n" " cmake [options] \n" - " cmake [options] -S -B \n" - " cmake [options] -S --preset=" }, + " cmake [options] -S -B " }, { nullptr, "Specify a source directory to (re-)generate a build system for " "it in the current working directory. Specify an existing build " diff --git a/Tests/RunCMake/CMakePresets/CMakePresets.json.in b/Tests/RunCMake/CMakePresets/CMakePresets.json.in index a8f89ff..a347120 100644 --- a/Tests/RunCMake/CMakePresets/CMakePresets.json.in +++ b/Tests/RunCMake/CMakePresets/CMakePresets.json.in @@ -160,6 +160,16 @@ "binaryDir": "${sourceDir}/build" }, { + "name": "GoodNoSCachePrep", + "generator": "@RunCMake_GENERATOR@", + "binaryDir": "${sourceParentDir}/GoodNoSCachePrep-build" + }, + { + "name": "GoodNoSCache", + "generator": "@RunCMake_GENERATOR@", + "binaryDir": "${sourceDir}/build" + }, + { "name": "GoodInheritanceParentBase", "hidden": true, "generator": "@RunCMake_GENERATOR@", diff --git a/Tests/RunCMake/CMakePresets/GoodNoS.cmake b/Tests/RunCMake/CMakePresets/GoodNoS.cmake index 1d3b2ff..49e7a25 100644 --- a/Tests/RunCMake/CMakePresets/GoodNoS.cmake +++ b/Tests/RunCMake/CMakePresets/GoodNoS.cmake @@ -1,4 +1,3 @@ include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake) -get_filename_component(_parent "${CMAKE_SOURCE_DIR}" DIRECTORY) -test_variable(CMAKE_BINARY_DIR "" "${_parent}/GoodNoS-build") +test_variable(CMAKE_BINARY_DIR "" "${CMAKE_SOURCE_DIR}/build") diff --git a/Tests/RunCMake/CMakePresets/GoodNoSCache.cmake b/Tests/RunCMake/CMakePresets/GoodNoSCache.cmake new file mode 100644 index 0000000..df58e72 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/GoodNoSCache.cmake @@ -0,0 +1,4 @@ +include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake) + +get_filename_component(_parent "${CMAKE_SOURCE_DIR}" DIRECTORY) +test_variable(CMAKE_BINARY_DIR "" "${_parent}/GoodNoSCachePrep-build") diff --git a/Tests/RunCMake/CMakePresets/GoodNoSCachePrep.cmake b/Tests/RunCMake/CMakePresets/GoodNoSCachePrep.cmake new file mode 100644 index 0000000..df58e72 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/GoodNoSCachePrep.cmake @@ -0,0 +1,4 @@ +include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake) + +get_filename_component(_parent "${CMAKE_SOURCE_DIR}" DIRECTORY) +test_variable(CMAKE_BINARY_DIR "" "${_parent}/GoodNoSCachePrep-build") diff --git a/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake b/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake index bd84510..2caa66a 100644 --- a/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake +++ b/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake @@ -28,8 +28,8 @@ endfunction() function(run_cmake_presets name) set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/${name}") set(_source_arg "${RunCMake_TEST_SOURCE_DIR}") - if(CMakePresets_RELATIVE_SOURCE) - set(_source_arg "../${name}") + if(CMakePresets_SOURCE_ARG) + set(_source_arg "${CMakePresets_SOURCE_ARG}") endif() file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}") file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}") @@ -154,9 +154,9 @@ unset(ENV{TEST_ENV_REF_PENV}) run_cmake_presets(GoodNoArgs) file(REMOVE_RECURSE ${RunCMake_BINARY_DIR}/GoodBinaryUp-build) run_cmake_presets(GoodBinaryUp) -set(CMakePresets_RELATIVE_SOURCE TRUE) +set(CMakePresets_SOURCE_ARG "../GoodBinaryRelative") run_cmake_presets(GoodBinaryRelative) -unset(CMakePresets_RELATIVE_SOURCE) +unset(CMakePresets_SOURCE_ARG) run_cmake_presets(GoodSpaces "--preset=Good Spaces") if(WIN32) run_cmake_presets(GoodWindowsBackslash) @@ -170,6 +170,14 @@ run_cmake_presets(GoodGeneratorCmdLine -G ${RunCMake_GENERATOR}) run_cmake_presets(InvalidGeneratorCmdLine -G "Invalid Generator") set(CMakePresets_NO_S_ARG TRUE) run_cmake_presets(GoodNoS) +set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/GoodNoSCachePrep-build") +run_cmake_presets(GoodNoSCachePrep) +set(CMakePresets_SOURCE_ARG ".") +set(RunCMake_TEST_NO_CLEAN 1) +run_cmake_presets(GoodNoSCache) +unset(RunCMake_TEST_NO_CLEAN) +unset(CMakePresets_SOURCE_ARG) +unset(RunCMake_TEST_BINARY_DIR) unset(CMakePresets_NO_S_ARG) run_cmake_presets(GoodInheritanceParent) run_cmake_presets(GoodInheritanceChild) diff --git a/Tests/RunCMake/CommandLine/NoArgs-stdout.txt b/Tests/RunCMake/CommandLine/NoArgs-stdout.txt index 50f7d9d..f1dafc8 100644 --- a/Tests/RunCMake/CommandLine/NoArgs-stdout.txt +++ b/Tests/RunCMake/CommandLine/NoArgs-stdout.txt @@ -3,7 +3,6 @@ cmake \[options\] cmake \[options\] cmake \[options\] -S -B - cmake \[options\] -S --preset= Specify a source directory to \(re-\)generate a build system for it in the current working directory. Specify an existing build directory to -- cgit v0.12