1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 03:48:02 +08:00

Merge topic 'cmake-presets-path-arg'

b7d7eca66d CMakePresets.json: Rework how --preset argument is handled

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !5416
This commit is contained in:
Craig Scott
2020-10-27 11:33:37 +00:00
committed by Kitware Robot
15 changed files with 65 additions and 32 deletions

View File

@@ -11,7 +11,6 @@ Synopsis
cmake-gui [<options>] cmake-gui [<options>]
cmake-gui [<options>] {<path-to-source> | <path-to-existing-build>} cmake-gui [<options>] {<path-to-source> | <path-to-existing-build>}
cmake-gui [<options>] -S <path-to-source> -B <path-to-build> cmake-gui [<options>] -S <path-to-source> -B <path-to-build>
cmake-gui [<options>] -S <path-to-source> --preset=<preset-name>
Description Description
=========== ===========
@@ -38,8 +37,8 @@ Options
If the directory doesn't already exist CMake will make it. If the directory doesn't already exist CMake will make it.
``--preset=<preset-name>`` ``--preset=<preset-name>``
Name of the preset to use from the project's ``CMakePresets.json`` file, if it Name of the preset to use from the project's
has one. :manual:`presets <cmake-presets(7)>` files, if it has them.
.. include:: OPTIONS_HELP.txt .. include:: OPTIONS_HELP.txt

View File

@@ -12,7 +12,6 @@ Synopsis
cmake [<options>] <path-to-source> cmake [<options>] <path-to-source>
cmake [<options>] <path-to-existing-build> cmake [<options>] <path-to-existing-build>
cmake [<options>] -S <path-to-source> -B <path-to-build> cmake [<options>] -S <path-to-source> -B <path-to-build>
cmake [<options>] -S <path-to-source> --preset=<preset-name>
`Build a Project`_ `Build a Project`_
cmake --build <dir> [<options>] [-- <build-tool-options>] cmake --build <dir> [<options>] [-- <build-tool-options>]
@@ -149,22 +148,6 @@ source and build trees and generate a buildsystem:
$ cmake -S src -B build $ cmake -S src -B build
``cmake [<options>] -S <path-to-source> --preset=<preset-name>``
Uses ``<path-to-source>`` as the source tree and reads a
:manual:`preset <cmake-presets(7)>` from
``<path-to-source>/CMakePresets.json`` and
``<path-to-source>/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 <cmake-gui(1)>` 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 ``<options>`` may be zero or more of the `Options`_ below. In all cases the ``<options>`` may be zero or more of the `Options`_ below.
After generating a buildsystem one may use the corresponding native 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 about:tracing tab of Google Chrome or using a plugin for a tool like Trace
Compass. Compass.
``--preset=<preset>``
Reads a :manual:`preset <cmake-presets(7)>` from
``<path-to-source>/CMakePresets.json`` and
``<path-to-source>/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 <cmake-gui(1)>` 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 Tool Mode`:
Build a Project Build a Project

View File

@@ -32,8 +32,7 @@ static const char* cmDocumentationUsage[][2] = {
" cmake-gui [options]\n" " cmake-gui [options]\n"
" cmake-gui [options] <path-to-source>\n" " cmake-gui [options] <path-to-source>\n"
" cmake-gui [options] <path-to-existing-build>\n" " cmake-gui [options] <path-to-existing-build>\n"
" cmake-gui [options] -S <path-to-source> -B <path-to-build>\n" " cmake-gui [options] -S <path-to-source> -B <path-to-build>\n" },
" cmake-gui [options] -S <path-to-source> --preset=<preset-name>\n" },
{ nullptr, nullptr } { nullptr, nullptr }
}; };

View File

@@ -162,6 +162,7 @@ bool cmCacheManager::LoadCache(const std::string& path, bool internal,
cmSystemTools::Error(message.str()); cmSystemTools::Error(message.str());
} }
} }
this->CacheLoaded = true;
return true; return true;
} }

View File

@@ -66,6 +66,9 @@ public:
//! Print the cache to a stream //! Print the cache to a stream
void PrintCache(std::ostream&) const; 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 //! Get a value from the cache given a key
cmProp GetInitializedCacheValue(const std::string& key) const; cmProp GetInitializedCacheValue(const std::string& key) const;
@@ -204,6 +207,7 @@ private:
const CacheEntry& e, cmMessenger* messenger) const; const CacheEntry& e, cmMessenger* messenger) const;
std::map<std::string, CacheEntry> Cache; std::map<std::string, CacheEntry> Cache;
bool CacheLoaded = false;
// Cache version info // Cache version info
unsigned int CacheMajorVersion = 0; unsigned int CacheMajorVersion = 0;

View File

@@ -135,6 +135,11 @@ bool cmState::DeleteCache(const std::string& path)
return this->CacheManager->DeleteCache(path); return this->CacheManager->DeleteCache(path);
} }
bool cmState::IsCacheLoaded() const
{
return this->CacheManager->IsCacheLoaded();
}
std::vector<std::string> cmState::GetCacheEntryKeys() const std::vector<std::string> cmState::GetCacheEntryKeys() const
{ {
return this->CacheManager->GetCacheEntryKeys(); return this->CacheManager->GetCacheEntryKeys();

View File

@@ -89,6 +89,8 @@ public:
bool DeleteCache(const std::string& path); bool DeleteCache(const std::string& path);
bool IsCacheLoaded() const;
std::vector<std::string> GetCacheEntryKeys() const; std::vector<std::string> GetCacheEntryKeys() const;
cmProp GetCacheEntryValue(std::string const& key) const; cmProp GetCacheEntryValue(std::string const& key) const;
std::string GetSafeCacheEntryValue(std::string const& key) const; std::string GetSafeCacheEntryValue(std::string const& key) const;

View File

@@ -727,6 +727,7 @@ void cmake::SetArgs(const std::vector<std::string>& args)
{ {
bool haveToolset = false; bool haveToolset = false;
bool havePlatform = false; bool havePlatform = false;
bool haveBArg = false;
#if !defined(CMAKE_BOOTSTRAP) #if !defined(CMAKE_BOOTSTRAP)
std::string profilingFormat; std::string profilingFormat;
std::string profilingOutput; std::string profilingOutput;
@@ -775,6 +776,7 @@ void cmake::SetArgs(const std::vector<std::string>& args)
path = cmSystemTools::CollapseFullPath(path); path = cmSystemTools::CollapseFullPath(path);
cmSystemTools::ConvertToUnixSlashes(path); cmSystemTools::ConvertToUnixSlashes(path);
this->SetHomeOutputDirectory(path); this->SetHomeOutputDirectory(path);
haveBArg = true;
} else if ((i < args.size() - 2) && } else if ((i < args.size() - 2) &&
cmHasLiteralPrefix(arg, "--check-build-system")) { cmHasLiteralPrefix(arg, "--check-build-system")) {
this->CheckBuildSystemArgument = args[++i]; this->CheckBuildSystemArgument = args[++i];
@@ -1057,7 +1059,7 @@ void cmake::SetArgs(const std::vector<std::string>& args)
return; return;
} }
if (!haveBinaryDir) { if (!this->State->IsCacheLoaded() && !haveBArg) {
this->SetHomeOutputDirectory(expandedPreset->BinaryDir); this->SetHomeOutputDirectory(expandedPreset->BinaryDir);
} }
if (!this->GlobalGenerator) { if (!this->GlobalGenerator) {

View File

@@ -49,8 +49,7 @@ const char* cmDocumentationUsage[][2] = {
{ nullptr, { nullptr,
" cmake [options] <path-to-source>\n" " cmake [options] <path-to-source>\n"
" cmake [options] <path-to-existing-build>\n" " cmake [options] <path-to-existing-build>\n"
" cmake [options] -S <path-to-source> -B <path-to-build>\n" " cmake [options] -S <path-to-source> -B <path-to-build>" },
" cmake [options] -S <path-to-source> --preset=<preset-name>" },
{ nullptr, { nullptr,
"Specify a source directory to (re-)generate a build system for " "Specify a source directory to (re-)generate a build system for "
"it in the current working directory. Specify an existing build " "it in the current working directory. Specify an existing build "

View File

@@ -159,6 +159,16 @@
"generator": "@RunCMake_GENERATOR@", "generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build" "binaryDir": "${sourceDir}/build"
}, },
{
"name": "GoodNoSCachePrep",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceParentDir}/GoodNoSCachePrep-build"
},
{
"name": "GoodNoSCache",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build"
},
{ {
"name": "GoodInheritanceParentBase", "name": "GoodInheritanceParentBase",
"hidden": true, "hidden": true,

View File

@@ -1,4 +1,3 @@
include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake) include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake)
get_filename_component(_parent "${CMAKE_SOURCE_DIR}" DIRECTORY) test_variable(CMAKE_BINARY_DIR "" "${CMAKE_SOURCE_DIR}/build")
test_variable(CMAKE_BINARY_DIR "" "${_parent}/GoodNoS-build")

View File

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

View File

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

View File

@@ -28,8 +28,8 @@ endfunction()
function(run_cmake_presets name) function(run_cmake_presets name)
set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/${name}") set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/${name}")
set(_source_arg "${RunCMake_TEST_SOURCE_DIR}") set(_source_arg "${RunCMake_TEST_SOURCE_DIR}")
if(CMakePresets_RELATIVE_SOURCE) if(CMakePresets_SOURCE_ARG)
set(_source_arg "../${name}") set(_source_arg "${CMakePresets_SOURCE_ARG}")
endif() endif()
file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}") file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
file(MAKE_DIRECTORY "${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) run_cmake_presets(GoodNoArgs)
file(REMOVE_RECURSE ${RunCMake_BINARY_DIR}/GoodBinaryUp-build) file(REMOVE_RECURSE ${RunCMake_BINARY_DIR}/GoodBinaryUp-build)
run_cmake_presets(GoodBinaryUp) run_cmake_presets(GoodBinaryUp)
set(CMakePresets_RELATIVE_SOURCE TRUE) set(CMakePresets_SOURCE_ARG "../GoodBinaryRelative")
run_cmake_presets(GoodBinaryRelative) run_cmake_presets(GoodBinaryRelative)
unset(CMakePresets_RELATIVE_SOURCE) unset(CMakePresets_SOURCE_ARG)
run_cmake_presets(GoodSpaces "--preset=Good Spaces") run_cmake_presets(GoodSpaces "--preset=Good Spaces")
if(WIN32) if(WIN32)
run_cmake_presets(GoodWindowsBackslash) run_cmake_presets(GoodWindowsBackslash)
@@ -170,6 +170,14 @@ run_cmake_presets(GoodGeneratorCmdLine -G ${RunCMake_GENERATOR})
run_cmake_presets(InvalidGeneratorCmdLine -G "Invalid Generator") run_cmake_presets(InvalidGeneratorCmdLine -G "Invalid Generator")
set(CMakePresets_NO_S_ARG TRUE) set(CMakePresets_NO_S_ARG TRUE)
run_cmake_presets(GoodNoS) 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) unset(CMakePresets_NO_S_ARG)
run_cmake_presets(GoodInheritanceParent) run_cmake_presets(GoodInheritanceParent)
run_cmake_presets(GoodInheritanceChild) run_cmake_presets(GoodInheritanceChild)

View File

@@ -3,7 +3,6 @@
cmake \[options\] <path-to-source> cmake \[options\] <path-to-source>
cmake \[options\] <path-to-existing-build> cmake \[options\] <path-to-existing-build>
cmake \[options\] -S <path-to-source> -B <path-to-build> cmake \[options\] -S <path-to-source> -B <path-to-build>
cmake \[options\] -S <path-to-source> --preset=<preset-name>
Specify a source directory to \(re-\)generate a build system for it in the Specify a source directory to \(re-\)generate a build system for it in the
current working directory. Specify an existing build directory to current working directory. Specify an existing build directory to