ソースを参照

cmake-presets: Add build and test presets

Fixes: #21391
Sam Freed 4 年 前
コミット
676ecf0d37

+ 7 - 6
Source/QtDialog/QCMake.cxx

@@ -68,9 +68,9 @@ QCMake::QCMake(QObject* p)
   connect(&this->LoadPresetsTimer, &QTimer::timeout, this, [this]() {
     this->loadPresets();
     if (!this->PresetName.isEmpty() &&
-        this->CMakePresetsFile.Presets.find(
+        this->CMakePresetsFile.ConfigurePresets.find(
           std::string(this->PresetName.toLocal8Bit())) ==
-          this->CMakePresetsFile.Presets.end()) {
+          this->CMakePresetsFile.ConfigurePresets.end()) {
       this->setPreset(QString{});
     }
   });
@@ -158,7 +158,7 @@ void QCMake::setPreset(const QString& name, bool setBinary)
     if (!name.isNull()) {
       std::string presetName(name.toLocal8Bit());
       auto const& expandedPreset =
-        this->CMakePresetsFile.Presets[presetName].Expanded;
+        this->CMakePresetsFile.ConfigurePresets[presetName].Expanded;
       if (expandedPreset) {
         if (setBinary) {
           QString binaryDir =
@@ -420,7 +420,8 @@ QCMakePropertyList QCMake::properties() const
 
   if (!this->PresetName.isNull()) {
     std::string presetName(this->PresetName.toLocal8Bit());
-    auto const& p = this->CMakePresetsFile.Presets.at(presetName).Expanded;
+    auto const& p =
+      this->CMakePresetsFile.ConfigurePresets.at(presetName).Expanded;
     if (p) {
       for (auto const& v : p->CacheVariables) {
         if (!v.second) {
@@ -535,8 +536,8 @@ void QCMake::loadPresets()
   this->LastLoadPresetsResult = result;
 
   QVector<QCMakePreset> presets;
-  for (auto const& name : this->CMakePresetsFile.PresetOrder) {
-    auto const& it = this->CMakePresetsFile.Presets[name];
+  for (auto const& name : this->CMakePresetsFile.ConfigurePresetOrder) {
+    auto const& it = this->CMakePresetsFile.ConfigurePresets[name];
     auto const& p = it.Unexpanded;
     if (p.Hidden) {
       continue;

ファイルの差分が大きいため隠しています
+ 840 - 195
Source/cmCMakePresetsFile.cxx


+ 251 - 56
Source/cmCMakePresetsFile.h

@@ -2,6 +2,7 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #pragma once
 
+#include <functional>
 #include <map>
 #include <string>
 #include <utility>
@@ -12,6 +13,27 @@
 class cmCMakePresetsFile
 {
 public:
+  enum class ReadFileResult
+  {
+    READ_OK,
+    FILE_NOT_FOUND,
+    JSON_PARSE_ERROR,
+    INVALID_ROOT,
+    NO_VERSION,
+    INVALID_VERSION,
+    UNRECOGNIZED_VERSION,
+    INVALID_CMAKE_VERSION,
+    UNRECOGNIZED_CMAKE_VERSION,
+    INVALID_PRESETS,
+    INVALID_PRESET,
+    INVALID_VARIABLE,
+    DUPLICATE_PRESETS,
+    CYCLIC_PRESET_INHERITANCE,
+    USER_PRESET_INHERITANCE,
+    INVALID_MACRO_EXPANSION,
+    BUILD_TEST_PRESETS_UNSUPPORTED,
+  };
+
   enum class ArchToolsetStrategy
   {
     Set,
@@ -29,25 +51,51 @@ public:
   {
   public:
 #if __cplusplus < 201703L && (!defined(_MSVC_LANG) || _MSVC_LANG < 201703L)
-    Preset() = default;
-    Preset(const Preset& /*other*/) = default;
-    Preset(Preset&& /*other*/) = default;
-
-    Preset& operator=(const Preset& /*other*/) = default;
-
     // The move assignment operators for several STL classes did not become
     // noexcept until C++17, which causes some tools to warn about this move
     // assignment operator throwing an exception when it shouldn't. Disable the
     // move assignment operator until C++17 is enabled.
-    Preset& operator=(Preset&& /*other*/) = delete;
+    // Explicitly defining a copy assignment operator prevents the compiler
+    // from automatically generating a move assignment operator.
+    Preset& operator=(const Preset& /*other*/) = default;
 #endif
 
+    virtual ~Preset() = default;
+
     std::string Name;
     std::vector<std::string> Inherits;
     bool Hidden;
     bool User;
     std::string DisplayName;
     std::string Description;
+
+    std::map<std::string, cm::optional<std::string>> Environment;
+
+    virtual ReadFileResult VisitPresetInherit(const Preset& parent) = 0;
+    virtual ReadFileResult VisitPresetBeforeInherit()
+    {
+      return ReadFileResult::READ_OK;
+    }
+
+    virtual ReadFileResult VisitPresetAfterInherit()
+    {
+      return ReadFileResult::READ_OK;
+    }
+  };
+
+  class ConfigurePreset : public Preset
+  {
+  public:
+#if __cplusplus < 201703L && (!defined(_MSVC_LANG) || _MSVC_LANG < 201703L)
+    // The move assignment operators for several STL classes did not become
+    // noexcept until C++17, which causes some tools to warn about this move
+    // assignment operator throwing an exception when it shouldn't. Disable the
+    // move assignment operator until C++17 is enabled.
+    // Explicitly defining a copy assignment operator prevents the compiler
+    // from automatically generating a move assignment operator.
+    ConfigurePreset& operator=(const ConfigurePreset& /*other*/) = default;
+#endif
+
     std::string Generator;
     std::string Architecture;
     cm::optional<ArchToolsetStrategy> ArchitectureStrategy;
@@ -56,7 +104,6 @@ public:
     std::string BinaryDir;
 
     std::map<std::string, cm::optional<CacheVariable>> CacheVariables;
-    std::map<std::string, cm::optional<std::string>> Environment;
 
     cm::optional<bool> WarnDev;
     cm::optional<bool> ErrorDev;
@@ -69,70 +116,183 @@ public:
     cm::optional<bool> DebugOutput;
     cm::optional<bool> DebugTryCompile;
     cm::optional<bool> DebugFind;
+
+    ReadFileResult VisitPresetInherit(const Preset& parent) override;
+    ReadFileResult VisitPresetBeforeInherit() override;
+    ReadFileResult VisitPresetAfterInherit() override;
   };
 
-  class UnexpandedPreset : public Preset
+  class BuildPreset : public Preset
   {
   public:
-    using Preset::Preset;
+#if __cplusplus < 201703L && (!defined(_MSVC_LANG) || _MSVC_LANG < 201703L)
+    // The move assignment operators for several STL classes did not become
+    // noexcept until C++17, which causes some tools to warn about this move
+    // assignment operator throwing an exception when it shouldn't. Disable the
+    // move assignment operator until C++17 is enabled.
+    // Explicitly defining a copy assignment operator prevents the compiler
+    // from automatically generating a move assignment operator.
+    BuildPreset& operator=(const BuildPreset& /*other*/) = default;
+#endif
 
-    UnexpandedPreset() = default;
-    UnexpandedPreset(const Preset& preset)
-      : Preset(preset)
-    {
-    }
-    UnexpandedPreset(Preset&& preset)
-      : Preset(std::move(preset))
-    {
-    }
+    std::string ConfigurePreset;
+    cm::optional<bool> InheritConfigureEnvironment;
+    cm::optional<int> Jobs;
+    std::vector<std::string> Targets;
+    std::string Configuration;
+    cm::optional<bool> CleanFirst;
+    cm::optional<bool> Verbose;
+    std::vector<std::string> NativeToolOptions;
+
+    ReadFileResult VisitPresetInherit(const Preset& parent) override;
+    ReadFileResult VisitPresetAfterInherit() override;
   };
 
-  class ExpandedPreset : public Preset
+  class TestPreset : public Preset
   {
   public:
-    using Preset::Preset;
+#if __cplusplus < 201703L && (!defined(_MSVC_LANG) || _MSVC_LANG < 201703L)
+    // The move assignment operators for several STL classes did not become
+    // noexcept until C++17, which causes some tools to warn about this move
+    // assignment operator throwing an exception when it shouldn't. Disable the
+    // move assignment operator until C++17 is enabled.
+    // Explicitly defining a copy assignment operator prevents the compiler
+    // from automatically generating a move assignment operator.
+    TestPreset& operator=(const TestPreset& /*other*/) = default;
+#endif
 
-    ExpandedPreset() = default;
-    ExpandedPreset(const Preset& preset)
-      : Preset(preset)
+    struct OutputOptions
     {
-    }
-    ExpandedPreset(Preset&& preset)
-      : Preset(std::move(preset))
+      enum class VerbosityEnum
+      {
+        Default,
+        Verbose,
+        Extra
+      };
+
+      cm::optional<bool> ShortProgress;
+      cm::optional<VerbosityEnum> Verbosity;
+      cm::optional<bool> Debug;
+      cm::optional<bool> OutputOnFailure;
+      cm::optional<bool> Quiet;
+      std::string OutputLogFile;
+      cm::optional<bool> LabelSummary;
+      cm::optional<bool> SubprojectSummary;
+      cm::optional<int> MaxPassedTestOutputSize;
+      cm::optional<int> MaxFailedTestOutputSize;
+      cm::optional<int> MaxTestNameWidth;
+    };
+
+    struct IncludeOptions
     {
-    }
+      struct IndexOptions
+      {
+        cm::optional<int> Start;
+        cm::optional<int> End;
+        cm::optional<int> Stride;
+        std::vector<int> SpecificTests;
+
+        std::string IndexFile;
+      };
+
+      std::string Name;
+      std::string Label;
+      cm::optional<IndexOptions> Index;
+      cm::optional<bool> UseUnion;
+    };
+
+    struct ExcludeOptions
+    {
+      struct FixturesOptions
+      {
+        std::string Any;
+        std::string Setup;
+        std::string Cleanup;
+      };
+
+      std::string Name;
+      std::string Label;
+      cm::optional<FixturesOptions> Fixtures;
+    };
+
+    struct FilterOptions
+    {
+      cm::optional<IncludeOptions> Include;
+      cm::optional<ExcludeOptions> Exclude;
+    };
+
+    struct ExecutionOptions
+    {
+      enum class ShowOnlyEnum
+      {
+        Human,
+        JsonV1
+      };
+
+      struct RepeatOptions
+      {
+        enum class ModeEnum
+        {
+          UntilFail,
+          UntilPass,
+          AfterTimeout
+        };
+
+        ModeEnum Mode;
+        int Count;
+      };
+
+      enum class NoTestsActionEnum
+      {
+        Default,
+        Error,
+        Ignore
+      };
+
+      cm::optional<bool> StopOnFailure;
+      cm::optional<bool> EnableFailover;
+      cm::optional<int> Jobs;
+      std::string ResourceSpecFile;
+      cm::optional<int> TestLoad;
+      cm::optional<ShowOnlyEnum> ShowOnly;
+      cm::optional<bool> RerunFailed;
+
+      cm::optional<RepeatOptions> Repeat;
+      cm::optional<bool> InteractiveDebugging;
+      cm::optional<bool> ScheduleRandom;
+      cm::optional<int> Timeout;
+      cm::optional<NoTestsActionEnum> NoTestsAction;
+    };
+
+    std::string ConfigurePreset;
+    cm::optional<bool> InheritConfigureEnvironment;
+    std::string Configuration;
+    std::vector<std::string> OverwriteConfigurationFile;
+    cm::optional<OutputOptions> Output;
+    cm::optional<FilterOptions> Filter;
+    cm::optional<ExecutionOptions> Execution;
+
+    ReadFileResult VisitPresetInherit(const Preset& parent) override;
+    ReadFileResult VisitPresetAfterInherit() override;
   };
 
+  template <class T>
   class PresetPair
   {
   public:
-    UnexpandedPreset Unexpanded;
-    cm::optional<ExpandedPreset> Expanded;
+    T Unexpanded;
+    cm::optional<T> Expanded;
   };
 
-  std::string SourceDir;
-  std::map<std::string, PresetPair> Presets;
-  std::vector<std::string> PresetOrder;
+  std::map<std::string, PresetPair<ConfigurePreset>> ConfigurePresets;
+  std::map<std::string, PresetPair<BuildPreset>> BuildPresets;
+  std::map<std::string, PresetPair<TestPreset>> TestPresets;
 
-  enum class ReadFileResult
-  {
-    READ_OK,
-    FILE_NOT_FOUND,
-    JSON_PARSE_ERROR,
-    INVALID_ROOT,
-    NO_VERSION,
-    INVALID_VERSION,
-    UNRECOGNIZED_VERSION,
-    INVALID_CMAKE_VERSION,
-    UNRECOGNIZED_CMAKE_VERSION,
-    INVALID_PRESETS,
-    INVALID_PRESET,
-    INVALID_VARIABLE,
-    DUPLICATE_PRESETS,
-    CYCLIC_PRESET_INHERITANCE,
-    USER_PRESET_INHERITANCE,
-    INVALID_MACRO_EXPANSION,
-  };
+  std::vector<std::string> ConfigurePresetOrder;
+  std::vector<std::string> BuildPresetOrder;
+  std::vector<std::string> TestPresetOrder;
+
+  std::string SourceDir;
 
   static std::string GetFilename(const std::string& sourceDir);
   static std::string GetUserFilename(const std::string& sourceDir);
@@ -140,9 +300,44 @@ public:
                                     bool allowNoFiles = false);
   static const char* ResultToString(ReadFileResult result);
 
+  std::string GetGeneratorForPreset(const std::string& presetName) const
+  {
+    auto configurePresetName = presetName;
+
+    auto buildPresetIterator = this->BuildPresets.find(presetName);
+    if (buildPresetIterator != this->BuildPresets.end()) {
+      configurePresetName =
+        buildPresetIterator->second.Unexpanded.ConfigurePreset;
+    } else {
+      auto testPresetIterator = this->TestPresets.find(presetName);
+      if (testPresetIterator != this->TestPresets.end()) {
+        configurePresetName =
+          testPresetIterator->second.Unexpanded.ConfigurePreset;
+      }
+    }
+
+    auto configurePresetIterator =
+      this->ConfigurePresets.find(configurePresetName);
+    if (configurePresetIterator != this->ConfigurePresets.end()) {
+      return configurePresetIterator->second.Unexpanded.Generator;
+    }
+
+    // This should only happen if the preset is hidden
+    // or (for build or test presets) if ConfigurePreset is invalid.
+    return "";
+  }
+
+  static void PrintPresets(
+    const std::vector<const cmCMakePresetsFile::Preset*>& presets);
+  void PrintConfigurePresetList() const;
+  void PrintConfigurePresetList(
+    const std::function<bool(const ConfigurePreset&)>& filter) const;
+  void PrintBuildPresetList() const;
+  void PrintTestPresetList() const;
+  void PrintAllPresets() const;
+
 private:
-  ReadFileResult ReadJSONFile(const std::string& filename,
-                              std::vector<std::string>& presetOrder,
-                              std::map<std::string, PresetPair>& presetMap,
-                              bool user);
+  ReadFileResult ReadProjectPresetsInternal(bool allowNoFiles);
+  ReadFileResult ReadJSONFile(const std::string& filename, bool user);
+  void ClearPresets();
 };

+ 339 - 1
Source/cmCTest.cxx

@@ -18,6 +18,7 @@
 #include <vector>
 
 #include <cm/memory>
+#include <cm/optional>
 #include <cm/string_view>
 #include <cmext/algorithm>
 #include <cmext/string_view>
@@ -38,6 +39,7 @@
 #  include <unistd.h> // IWYU pragma: keep
 #endif
 
+#include "cmCMakePresetsFile.h"
 #include "cmCTestBuildAndTestHandler.h"
 #include "cmCTestBuildHandler.h"
 #include "cmCTestConfigureHandler.h"
@@ -2257,6 +2259,311 @@ bool cmCTest::AddVariableDefinition(const std::string& arg)
   return false;
 }
 
+void cmCTest::SetPersistentOptionIfNotEmpty(const std::string& value,
+                                            const std::string& optionName)
+{
+  if (!value.empty()) {
+    this->GetTestHandler()->SetPersistentOption(optionName, value.c_str());
+    this->GetMemCheckHandler()->SetPersistentOption(optionName, value.c_str());
+  }
+}
+
+bool cmCTest::SetArgsFromPreset(const std::string& presetName,
+                                bool listPresets)
+{
+  const auto workingDirectory = cmSystemTools::GetCurrentWorkingDirectory();
+
+  cmCMakePresetsFile settingsFile;
+  auto result = settingsFile.ReadProjectPresets(workingDirectory);
+  if (result != cmCMakePresetsFile::ReadFileResult::READ_OK) {
+    cmSystemTools::Error(cmStrCat("Could not read presets from ",
+                                  workingDirectory, ": ",
+                                  cmCMakePresetsFile::ResultToString(result)));
+    return false;
+  }
+
+  if (listPresets) {
+    settingsFile.PrintTestPresetList();
+    return true;
+  }
+
+  auto presetPair = settingsFile.TestPresets.find(presetName);
+  if (presetPair == settingsFile.TestPresets.end()) {
+    cmSystemTools::Error(cmStrCat("No such test preset in ", workingDirectory,
+                                  ": \"", presetName, '"'));
+    settingsFile.PrintTestPresetList();
+    return false;
+  }
+
+  if (presetPair->second.Unexpanded.Hidden) {
+    cmSystemTools::Error(cmStrCat("Cannot use hidden test preset in ",
+                                  workingDirectory, ": \"", presetName, '"'));
+    settingsFile.PrintTestPresetList();
+    return false;
+  }
+
+  auto const& expandedPreset = presetPair->second.Expanded;
+  if (!expandedPreset) {
+    cmSystemTools::Error(cmStrCat("Could not evaluate test preset \"",
+                                  presetName, "\": Invalid macro expansion"));
+    settingsFile.PrintTestPresetList();
+    return false;
+  }
+
+  auto configurePresetPair =
+    settingsFile.ConfigurePresets.find(expandedPreset->ConfigurePreset);
+  if (configurePresetPair == settingsFile.ConfigurePresets.end()) {
+    cmSystemTools::Error(cmStrCat("No such configure preset in ",
+                                  workingDirectory, ": \"",
+                                  expandedPreset->ConfigurePreset, '"'));
+    settingsFile.PrintConfigurePresetList();
+    return false;
+  }
+
+  if (configurePresetPair->second.Unexpanded.Hidden) {
+    cmSystemTools::Error(cmStrCat("Cannot use hidden configure preset in ",
+                                  workingDirectory, ": \"",
+                                  expandedPreset->ConfigurePreset, '"'));
+    settingsFile.PrintConfigurePresetList();
+    return false;
+  }
+
+  auto const& expandedConfigurePreset = configurePresetPair->second.Expanded;
+  if (!expandedConfigurePreset) {
+    cmSystemTools::Error(cmStrCat("Could not evaluate configure preset \"",
+                                  expandedPreset->ConfigurePreset,
+                                  "\": Invalid macro expansion"));
+    return false;
+  }
+
+  auto presetEnvironment = expandedPreset->Environment;
+  for (auto const& var : presetEnvironment) {
+    if (var.second) {
+      cmSystemTools::PutEnv(cmStrCat(var.first, '=', *var.second));
+    }
+  }
+
+  if (!expandedPreset->Configuration.empty()) {
+    this->SetConfigType(expandedPreset->Configuration);
+  }
+
+  // Set build directory to value specified by the configure preset.
+  this->AddCTestConfigurationOverwrite(
+    cmStrCat("BuildDirectory=", expandedConfigurePreset->BinaryDir));
+  for (const auto& kvp : expandedPreset->OverwriteConfigurationFile) {
+    this->AddCTestConfigurationOverwrite(kvp);
+  }
+
+  if (expandedPreset->Output) {
+    this->Impl->TestProgressOutput =
+      expandedPreset->Output->ShortProgress.value_or(false);
+
+    if (expandedPreset->Output->Verbosity) {
+      const auto& verbosity = *expandedPreset->Output->Verbosity;
+      switch (verbosity) {
+        case cmCMakePresetsFile::TestPreset::OutputOptions::VerbosityEnum::
+          Extra:
+          this->Impl->ExtraVerbose = true;
+          // intentional fallthrough
+        case cmCMakePresetsFile::TestPreset::OutputOptions::VerbosityEnum::
+          Verbose:
+          this->Impl->Verbose = true;
+          break;
+        case cmCMakePresetsFile::TestPreset::OutputOptions::VerbosityEnum::
+          Default:
+        default:
+          // leave default settings
+          break;
+      }
+    }
+
+    this->Impl->Debug = expandedPreset->Output->Debug.value_or(false);
+    this->Impl->ShowLineNumbers =
+      expandedPreset->Output->Debug.value_or(false);
+    this->Impl->OutputTestOutputOnTestFailure =
+      expandedPreset->Output->OutputOnFailure.value_or(false);
+    this->Impl->Quiet = expandedPreset->Output->Quiet.value_or(false);
+
+    if (!expandedPreset->Output->OutputLogFile.empty()) {
+      this->SetOutputLogFileName(expandedPreset->Output->OutputLogFile);
+    }
+
+    this->Impl->LabelSummary =
+      expandedPreset->Output->LabelSummary.value_or(true);
+    this->Impl->SubprojectSummary =
+      expandedPreset->Output->SubprojectSummary.value_or(true);
+
+    if (expandedPreset->Output->MaxPassedTestOutputSize) {
+      this->Impl->TestHandler.SetTestOutputSizePassed(
+        *expandedPreset->Output->MaxPassedTestOutputSize);
+    }
+
+    if (expandedPreset->Output->MaxFailedTestOutputSize) {
+      this->Impl->TestHandler.SetTestOutputSizeFailed(
+        *expandedPreset->Output->MaxFailedTestOutputSize);
+    }
+
+    if (expandedPreset->Output->MaxTestNameWidth) {
+      this->Impl->MaxTestNameWidth = *expandedPreset->Output->MaxTestNameWidth;
+    }
+  }
+
+  if (expandedPreset->Filter) {
+    if (expandedPreset->Filter->Include) {
+      this->SetPersistentOptionIfNotEmpty(
+        expandedPreset->Filter->Include->Name, "IncludeRegularExpression");
+      this->SetPersistentOptionIfNotEmpty(
+        expandedPreset->Filter->Include->Label, "LabelRegularExpression");
+
+      if (expandedPreset->Filter->Include->Index) {
+        if (expandedPreset->Filter->Include->Index->IndexFile.empty()) {
+          const auto& start = expandedPreset->Filter->Include->Index->Start;
+          const auto& end = expandedPreset->Filter->Include->Index->End;
+          const auto& stride = expandedPreset->Filter->Include->Index->Stride;
+          std::string indexOptions;
+          indexOptions += (start ? std::to_string(*start) : "") + ",";
+          indexOptions += (end ? std::to_string(*end) : "") + ",";
+          indexOptions += (stride ? std::to_string(*stride) : "") + ",";
+          indexOptions +=
+            cmJoin(expandedPreset->Filter->Include->Index->SpecificTests, ",");
+
+          this->SetPersistentOptionIfNotEmpty(indexOptions,
+                                              "TestsToRunInformation");
+        } else {
+          this->SetPersistentOptionIfNotEmpty(
+            expandedPreset->Filter->Include->Index->IndexFile,
+            "TestsToRunInformation");
+        }
+      }
+
+      if (expandedPreset->Filter->Include->UseUnion.value_or(false)) {
+        this->GetTestHandler()->SetPersistentOption("UseUnion", "true");
+        this->GetMemCheckHandler()->SetPersistentOption("UseUnion", "true");
+      }
+    }
+
+    if (expandedPreset->Filter->Exclude) {
+      this->SetPersistentOptionIfNotEmpty(
+        expandedPreset->Filter->Exclude->Name, "ExcludeRegularExpression");
+      this->SetPersistentOptionIfNotEmpty(
+        expandedPreset->Filter->Exclude->Label,
+        "ExcludeLabelRegularExpression");
+
+      if (expandedPreset->Filter->Exclude->Fixtures) {
+        this->SetPersistentOptionIfNotEmpty(
+          expandedPreset->Filter->Exclude->Fixtures->Any,
+          "ExcludeFixtureRegularExpression");
+        this->SetPersistentOptionIfNotEmpty(
+          expandedPreset->Filter->Exclude->Fixtures->Setup,
+          "ExcludeFixtureSetupRegularExpression");
+        this->SetPersistentOptionIfNotEmpty(
+          expandedPreset->Filter->Exclude->Fixtures->Cleanup,
+          "ExcludeFixtureCleanupRegularExpression");
+      }
+    }
+  }
+
+  if (expandedPreset->Execution) {
+    this->Impl->StopOnFailure =
+      expandedPreset->Execution->StopOnFailure.value_or(false);
+    this->Impl->Failover =
+      expandedPreset->Execution->EnableFailover.value_or(false);
+
+    if (expandedPreset->Execution->Jobs) {
+      auto jobs = *expandedPreset->Execution->Jobs;
+      this->SetParallelLevel(jobs);
+      this->Impl->ParallelLevelSetInCli = true;
+    }
+
+    this->SetPersistentOptionIfNotEmpty(
+      expandedPreset->Execution->ResourceSpecFile, "ResourceSpecFile");
+
+    if (expandedPreset->Execution->TestLoad) {
+      auto testLoad = *expandedPreset->Execution->TestLoad;
+      this->SetTestLoad(testLoad);
+    }
+
+    if (expandedPreset->Execution->ShowOnly) {
+      this->Impl->ShowOnly = true;
+
+      switch (*expandedPreset->Execution->ShowOnly) {
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::ShowOnlyEnum::
+          JsonV1:
+          this->Impl->Quiet = true;
+          this->Impl->OutputAsJson = true;
+          this->Impl->OutputAsJsonVersion = 1;
+          break;
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::ShowOnlyEnum::
+          Human:
+          // intentional fallthrough (human is the default)
+        default:
+          break;
+      }
+    }
+
+    if (expandedPreset->Execution->RerunFailed.value_or(false)) {
+      this->GetTestHandler()->SetPersistentOption("RerunFailed", "true");
+      this->GetMemCheckHandler()->SetPersistentOption("RerunFailed", "true");
+    }
+
+    if (expandedPreset->Execution->Repeat) {
+      this->Impl->RepeatCount = expandedPreset->Execution->Repeat->Count;
+      switch (expandedPreset->Execution->Repeat->Mode) {
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::RepeatOptions::
+          ModeEnum::UntilFail:
+          this->Impl->RepeatMode = cmCTest::Repeat::UntilFail;
+          break;
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::RepeatOptions::
+          ModeEnum::UntilPass:
+          this->Impl->RepeatMode = cmCTest::Repeat::UntilPass;
+          break;
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::RepeatOptions::
+          ModeEnum::AfterTimeout:
+          this->Impl->RepeatMode = cmCTest::Repeat::AfterTimeout;
+          break;
+        default:
+          // should never default since mode is required
+          return false;
+      }
+    }
+
+    if (expandedPreset->Execution->InteractiveDebugging) {
+      this->Impl->InteractiveDebugMode =
+        *expandedPreset->Execution->InteractiveDebugging;
+    }
+
+    if (expandedPreset->Execution->ScheduleRandom.value_or(false)) {
+      this->Impl->ScheduleType = "Random";
+    }
+
+    if (expandedPreset->Execution->Timeout) {
+      this->Impl->GlobalTimeout =
+        cmDuration(*expandedPreset->Execution->Timeout);
+    }
+
+    if (expandedPreset->Execution->NoTestsAction) {
+      switch (*expandedPreset->Execution->NoTestsAction) {
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::
+          NoTestsActionEnum::Error:
+          this->Impl->NoTestsMode = cmCTest::NoTests::Error;
+          break;
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::
+          NoTestsActionEnum::Ignore:
+          this->Impl->NoTestsMode = cmCTest::NoTests::Ignore;
+          break;
+        case cmCMakePresetsFile::TestPreset::ExecutionOptions::
+          NoTestsActionEnum::Default:
+          break;
+        default:
+          // should never default
+          return false;
+      }
+    }
+  }
+
+  return true;
+}
+
 // the main entry point of ctest, called from main
 int cmCTest::Run(std::vector<std::string>& args, std::string* output)
 {
@@ -2268,6 +2575,37 @@ int cmCTest::Run(std::vector<std::string>& args, std::string* output)
   // copy the command line
   cm::append(this->Impl->InitialCommandLineArguments, args);
 
+  // check if a test preset was specified
+
+  bool listPresets =
+    find(args.begin(), args.end(), "--list-presets") != args.end();
+  auto it = find(args.begin(), args.end(), "--preset");
+  if (listPresets || it != args.end()) {
+    std::string errormsg;
+    bool success;
+
+    if (listPresets) {
+      // If listing presets we don't need a presetName
+      success = this->SetArgsFromPreset("", listPresets);
+    } else {
+      if (++it != args.end()) {
+        auto presetName = *it;
+        success = this->SetArgsFromPreset(presetName, listPresets);
+      } else {
+        cmSystemTools::Error("'--preset' requires an argument");
+        success = false;
+      }
+    }
+
+    if (listPresets) {
+      return success ? 0 : 1;
+    }
+
+    if (!success) {
+      return 1;
+    }
+  }
+
   // process the command line arguments
   for (size_t i = 1; i < args.size(); ++i) {
     // handle the simple commandline arguments
@@ -2339,7 +2677,7 @@ int cmCTest::Run(std::vector<std::string>& args, std::string* output)
       this->Impl->ScheduleType = "Random";
     }
 
-    // pass the argument to all the handlers as well, but i may no longer be
+    // pass the argument to all the handlers as well, but it may no longer be
     // set to what it was originally so I'm not sure this is working as
     // intended
     for (auto& handler : this->Impl->GetTestingHandlers()) {

+ 6 - 0
Source/cmCTest.h

@@ -461,6 +461,9 @@ public:
   void SetRunCurrentScript(bool value);
 
 private:
+  void SetPersistentOptionIfNotEmpty(const std::string& value,
+                                     const std::string& optionName);
+
   int GenerateNotesFile(const std::string& files);
 
   void BlockTestErrorDiagnostics();
@@ -484,6 +487,9 @@ private:
   /** add a variable definition from a command line -D value */
   bool AddVariableDefinition(const std::string& arg);
 
+  /** set command line arguments read from a test preset */
+  bool SetArgsFromPreset(const std::string& presetName, bool listPresets);
+
   /** parse and process most common command line arguments */
   bool HandleCommandLineArguments(size_t& i, std::vector<std::string>& args,
                                   std::string& errormsg);

+ 170 - 54
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;

+ 5 - 5
Source/cmake.h

@@ -238,7 +238,7 @@ public:
   bool CreateAndSetGlobalGenerator(const std::string& name, bool allowArch);
 
 #ifndef CMAKE_BOOTSTRAP
-  //! Print list of presets
+  //! Print list of configure presets
   void PrintPresetList(const cmCMakePresetsFile& file) const;
 #endif
 
@@ -556,10 +556,10 @@ public:
     cmListFileBacktrace const& backtrace = cmListFileBacktrace()) const;
 
   //! run the --build option
-  int 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 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);
 
   //! run the --open option
   bool Open(const std::string& dir, bool dryRun);

+ 41 - 8
Source/cmakemain.cxx

@@ -425,6 +425,8 @@ int do_build(int ac, char const* const* av)
   bool foundClean = false;
   bool foundNonClean = false;
   bool verbose = cmSystemTools::HasEnv("VERBOSE");
+  std::string presetName;
+  bool listPresets = false;
 
   auto jLambda = [&](std::string const& value) -> bool {
     jobs = extract_job_number("-j", value);
@@ -464,6 +466,16 @@ int do_build(int ac, char const* const* av)
     cmCommandLineArgument<bool(std::string const& value)>;
 
   std::vector<CommandArgument> arguments = {
+    CommandArgument{ "--preset", CommandArgument::Values::One,
+                     [&](std::string const& value) -> bool {
+                       presetName = value;
+                       return true;
+                     } },
+    CommandArgument{ "--list-presets", CommandArgument::Values::Zero,
+                     [&](std::string const&) -> bool {
+                       listPresets = true;
+                       return true;
+                     } },
     CommandArgument{ "-j", CommandArgument::Values::ZeroOrOne, jLambda },
     CommandArgument{ "--parallel", CommandArgument::Values::ZeroOrOne,
                      parallelLambda },
@@ -494,11 +506,26 @@ int do_build(int ac, char const* const* av)
   };
 
   if (ac >= 3) {
-    dir = cmSystemTools::CollapseFullPath(av[2]);
-
     std::vector<std::string> inputArgs;
-    inputArgs.reserve(ac - 3);
-    cm::append(inputArgs, av + 3, av + ac);
+
+    bool hasPreset = false;
+    for (int i = 2; i < ac; ++i) {
+      if (strcmp(av[i], "--list-presets") == 0 ||
+          strcmp(av[i], "--preset") == 0) {
+        hasPreset = true;
+        break;
+      }
+    }
+
+    if (hasPreset) {
+      inputArgs.reserve(ac - 2);
+      cm::append(inputArgs, av + 2, av + ac);
+    } else {
+      dir = cmSystemTools::CollapseFullPath(av[2]);
+
+      inputArgs.reserve(ac - 3);
+      cm::append(inputArgs, av + 3, av + ac);
+    }
 
     decltype(inputArgs.size()) i = 0;
     for (; i < inputArgs.size() && !nativeOptionsPassed; ++i) {
@@ -551,12 +578,16 @@ int do_build(int ac, char const* const* av)
     }
   }
 
-  if (dir.empty()) {
+  if (dir.empty() && presetName.empty() && !listPresets) {
     /* clang-format off */
     std::cerr <<
-      "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
+      "Usage: cmake --build [<dir> | --preset <preset>] [options] [-- [native-options]]\n"
       "Options:\n"
       "  <dir>          = Project binary directory to be built.\n"
+      "  --preset <preset>\n"
+      "                 = Specify a build preset.\n"
+      "  --list-presets\n"
+      "                 = List available build presets.\n"
       "  --parallel [<jobs>], -j [<jobs>]\n"
       "                 = Build in parallel using the given number of jobs. \n"
       "                   If <jobs> is omitted the native build tool's \n"
@@ -587,8 +618,10 @@ int do_build(int ac, char const* const* av)
   cm.SetProgressCallback([&cm](const std::string& msg, float prog) {
     cmakemainProgressCallback(msg, prog, &cm);
   });
-  return cm.Build(jobs, dir, targets, config, nativeOptions, cleanFirst,
-                  verbose);
+
+  return cm.Build(jobs, std::move(dir), std::move(targets), std::move(config),
+                  std::move(nativeOptions), cleanFirst, verbose, presetName,
+                  listPresets);
 #endif
 }
 

+ 2 - 0
Source/ctest.cxx

@@ -26,6 +26,8 @@ static const char* cmDocumentationUsage[][2] = { { nullptr,
                                                  { nullptr, nullptr } };
 
 static const char* cmDocumentationOptions[][2] = {
+  { "--preset <preset>", "Read arguments from a test preset." },
+  { "--list-presets", "List available test presets." },
   { "-C <cfg>, --build-config <cfg>", "Choose configuration to test." },
   { "--progress", "Enable short progress output from tests." },
   { "-V,--verbose", "Enable verbose output from tests." },

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません