diff options
author | Sam Freed <safreed@microsoft.com> | 2020-12-14 18:06:08 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2021-02-01 16:59:40 (GMT) |
commit | 676ecf0d37cf5cfda390f52ce60babcb5da78108 (patch) | |
tree | 79129100ed585db7a91b82778166c1e1d3d6f7e5 /Source/cmake.cxx | |
parent | 4f4f2028b8e2c45eb6940d1ec5069caad9b606a2 (diff) | |
download | CMake-676ecf0d37cf5cfda390f52ce60babcb5da78108.zip CMake-676ecf0d37cf5cfda390f52ce60babcb5da78108.tar.gz CMake-676ecf0d37cf5cfda390f52ce60babcb5da78108.tar.bz2 |
cmake-presets: Add build and test presets
Fixes: #21391
Diffstat (limited to 'Source/cmake.cxx')
-rw-r--r-- | Source/cmake.cxx | 224 |
1 files changed, 170 insertions, 54 deletions
diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 48848a7..4b57395 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -726,6 +726,17 @@ void cmake::LoadEnvironmentPresets() readGeneratorVar("CMAKE_GENERATOR_TOOLSET", this->GeneratorToolset); } +namespace { +enum class ListPresets +{ + None, + Configure, + Build, + Test, + All, +}; +} + // Parse the args void cmake::SetArgs(const std::vector<std::string>& args) { @@ -738,7 +749,8 @@ void cmake::SetArgs(const std::vector<std::string>& args) std::string profilingFormat; std::string profilingOutput; std::string presetName; - bool listPresets = false; + + ListPresets listPresets = ListPresets::None; #endif auto SourceArgLambda = [](std::string const& value, cmake* state) -> bool { @@ -995,11 +1007,27 @@ void cmake::SetArgs(const std::vector<std::string>& args) presetName = value; return true; }); - arguments.emplace_back("--list-presets", CommandArgument::Values::Zero, - [&](std::string const&, cmake*) -> bool { - listPresets = true; - return true; - }); + arguments.emplace_back( + "--list-presets", CommandArgument::Values::ZeroOrOne, + [&](std::string const& value, cmake*) -> bool { + if (value.empty() || value == "configure") { + listPresets = ListPresets::Configure; + } else if (value == "build") { + listPresets = ListPresets::Build; + } else if (value == "test") { + listPresets = ListPresets::Test; + } else if (value == "all") { + listPresets = ListPresets::All; + } else { + cmSystemTools::Error( + "Invalid value specified for --list-presets.\n" + "Valid values are configure, build, test, or all. " + "When no value is passed the default is configure."); + return false; + } + + return true; + }); #endif @@ -1119,7 +1147,7 @@ void cmake::SetArgs(const std::vector<std::string>& args) } #if !defined(CMAKE_BOOTSTRAP) - if (listPresets || !presetName.empty()) { + if (listPresets != ListPresets::None || !presetName.empty()) { cmCMakePresetsFile settingsFile; auto result = settingsFile.ReadProjectPresets(this->GetHomeDirectory()); if (result != cmCMakePresetsFile::ReadFileResult::READ_OK) { @@ -1128,12 +1156,24 @@ void cmake::SetArgs(const std::vector<std::string>& args) ": ", cmCMakePresetsFile::ResultToString(result))); return; } - if (listPresets) { - this->PrintPresetList(settingsFile); + + if (listPresets != ListPresets::None) { + if (listPresets == ListPresets::Configure) { + this->PrintPresetList(settingsFile); + } else if (listPresets == ListPresets::Build) { + settingsFile.PrintBuildPresetList(); + } else if (listPresets == ListPresets::Test) { + settingsFile.PrintTestPresetList(); + } else if (listPresets == ListPresets::All) { + settingsFile.PrintAllPresets(); + } + + this->SetWorkingMode(WorkingMode::HELP_MODE); return; } - auto preset = settingsFile.Presets.find(presetName); - if (preset == settingsFile.Presets.end()) { + + auto preset = settingsFile.ConfigurePresets.find(presetName); + if (preset == settingsFile.ConfigurePresets.end()) { cmSystemTools::Error(cmStrCat("No such preset in ", this->GetHomeDirectory(), ": \"", presetName, '"')); @@ -1562,44 +1602,16 @@ void cmake::PrintPresetList(const cmCMakePresetsFile& file) const { std::vector<GeneratorInfo> generators; this->GetRegisteredGenerators(generators, false); + auto filter = + [&generators](const cmCMakePresetsFile::ConfigurePreset& preset) -> bool { + auto condition = [&preset](const GeneratorInfo& info) -> bool { + return info.name == preset.Generator; + }; + auto it = std::find_if(generators.begin(), generators.end(), condition); + return it != generators.end(); + }; - std::vector<cmCMakePresetsFile::UnexpandedPreset> presets; - for (auto const& p : file.PresetOrder) { - auto const& preset = file.Presets.at(p); - if (!preset.Unexpanded.Hidden && preset.Expanded && - std::find_if(generators.begin(), generators.end(), - [&preset](const GeneratorInfo& info) { - return info.name == preset.Unexpanded.Generator; - }) != generators.end()) { - presets.push_back(preset.Unexpanded); - } - } - - if (presets.empty()) { - return; - } - - std::cout << "Available presets:\n\n"; - - auto longestPresetName = - std::max_element(presets.begin(), presets.end(), - [](const cmCMakePresetsFile::UnexpandedPreset& a, - const cmCMakePresetsFile::UnexpandedPreset& b) { - return a.Name.length() < b.Name.length(); - }); - auto longestLength = longestPresetName->Name.length(); - - for (auto const& preset : presets) { - std::cout << " \"" << preset.Name << '"'; - auto const& description = preset.DisplayName; - if (!description.empty()) { - for (std::size_t i = 0; i < longestLength - preset.Name.length(); ++i) { - std::cout << ' '; - } - std::cout << " - " << description; - } - std::cout << '\n'; - } + file.PrintConfigurePresetList(filter); } #endif @@ -3068,15 +3080,119 @@ std::vector<std::string> cmake::GetDebugConfigs() return configs; } -int cmake::Build(int jobs, const std::string& dir, - const std::vector<std::string>& targets, - const std::string& config, - const std::vector<std::string>& nativeOptions, bool clean, - bool verbose) +int cmake::Build(int jobs, std::string dir, std::vector<std::string> targets, + std::string config, std::vector<std::string> nativeOptions, + bool clean, bool verbose, const std::string& presetName, + bool listPresets) { - this->SetHomeDirectory(""); this->SetHomeOutputDirectory(""); + +#if !defined(CMAKE_BOOTSTRAP) + if (!presetName.empty() || listPresets) { + this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory()); + this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory()); + + cmCMakePresetsFile settingsFile; + auto result = settingsFile.ReadProjectPresets(this->GetHomeDirectory()); + if (result != cmCMakePresetsFile::ReadFileResult::READ_OK) { + cmSystemTools::Error( + cmStrCat("Could not read presets from ", this->GetHomeDirectory(), + ": ", cmCMakePresetsFile::ResultToString(result))); + return 1; + } + + if (listPresets) { + settingsFile.PrintBuildPresetList(); + return 0; + } + + auto presetPair = settingsFile.BuildPresets.find(presetName); + if (presetPair == settingsFile.BuildPresets.end()) { + cmSystemTools::Error(cmStrCat("No such build preset in ", + this->GetHomeDirectory(), ": \"", + presetName, '"')); + settingsFile.PrintBuildPresetList(); + return 1; + } + + if (presetPair->second.Unexpanded.Hidden) { + cmSystemTools::Error(cmStrCat("Cannot use hidden build preset in ", + this->GetHomeDirectory(), ": \"", + presetName, '"')); + settingsFile.PrintBuildPresetList(); + return 1; + } + + auto const& expandedPreset = presetPair->second.Expanded; + if (!expandedPreset) { + cmSystemTools::Error(cmStrCat("Could not evaluate build preset \"", + presetName, + "\": Invalid macro expansion")); + settingsFile.PrintBuildPresetList(); + return 1; + } + + auto configurePresetPair = + settingsFile.ConfigurePresets.find(expandedPreset->ConfigurePreset); + if (configurePresetPair == settingsFile.ConfigurePresets.end()) { + cmSystemTools::Error(cmStrCat("No such configure preset in ", + this->GetHomeDirectory(), ": \"", + expandedPreset->ConfigurePreset, '"')); + this->PrintPresetList(settingsFile); + return 1; + } + + if (configurePresetPair->second.Unexpanded.Hidden) { + cmSystemTools::Error(cmStrCat("Cannot use hidden configure preset in ", + this->GetHomeDirectory(), ": \"", + expandedPreset->ConfigurePreset, '"')); + this->PrintPresetList(settingsFile); + return 1; + } + + auto const& expandedConfigurePreset = configurePresetPair->second.Expanded; + if (!expandedConfigurePreset) { + cmSystemTools::Error(cmStrCat("Could not evaluate configure preset \"", + expandedPreset->ConfigurePreset, + "\": Invalid macro expansion")); + return 1; + } + + dir = expandedConfigurePreset->BinaryDir; + + this->UnprocessedPresetEnvironment = expandedPreset->Environment; + this->ProcessPresetEnvironment(); + + if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL && expandedPreset->Jobs) { + jobs = *expandedPreset->Jobs; + } + + if (targets.empty()) { + targets.insert(targets.begin(), expandedPreset->Targets.begin(), + expandedPreset->Targets.end()); + } + + if (config.empty()) { + config = expandedPreset->Configuration; + } + + if (!clean && expandedPreset->CleanFirst) { + clean = *expandedPreset->CleanFirst; + } + + if (!verbose && expandedPreset->Verbose) { + verbose = *expandedPreset->Verbose; + } + + if (nativeOptions.empty()) { + nativeOptions.insert(nativeOptions.begin(), + expandedPreset->NativeToolOptions.begin(), + expandedPreset->NativeToolOptions.end()); + } + } +#endif + if (!cmSystemTools::FileIsDirectory(dir)) { std::cerr << "Error: " << dir << " is not a directory\n"; return 1; |