Browse Source

Merge topic 'cmake-presets-workflow-arguments'

7ecbe324b0 cmake --workflow: add --fresh option
7d9aa0f00c cmake::Workflow: Refactor to use enum class argument
322193afcd cmake --workflow: print usage and exit on unrecognized argument
70aef29427 cmake --workflow: print usage message

Acked-by: Kitware Robot <[email protected]>
Acked-by: buildbot <[email protected]>
Merge-request: !7825
Brad King 3 years ago
parent
commit
ab88011579

+ 6 - 0
Help/manual/cmake.1.rst

@@ -1286,6 +1286,12 @@ The options are:
   Lists the available workflow presets. The current working directory must
   contain CMake preset files.
 
+.. option:: --fresh
+
+  Perform a fresh configuration of the build tree.
+  This removes any existing ``CMakeCache.txt`` file and associated
+  ``CMakeFiles/`` directory, and recreates them from scratch.
+
 View Help
 =========
 

+ 10 - 6
Source/cmake.cxx

@@ -3742,7 +3742,8 @@ std::function<int()> cmake::BuildWorkflowStep(
 }
 #endif
 
-int cmake::Workflow(const std::string& presetName, bool listPresets)
+int cmake::Workflow(const std::string& presetName,
+                    WorkflowListPresets listPresets, WorkflowFresh fresh)
 {
 #ifndef CMAKE_BOOTSTRAP
   this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
@@ -3757,7 +3758,7 @@ int cmake::Workflow(const std::string& presetName, bool listPresets)
     return 1;
   }
 
-  if (listPresets) {
+  if (listPresets == WorkflowListPresets::Yes) {
     settingsFile.PrintWorkflowPresetList();
     return 0;
   }
@@ -3824,10 +3825,13 @@ int cmake::Workflow(const std::string& presetName, bool listPresets)
         if (!configurePreset) {
           return 1;
         }
-        steps.emplace_back(
-          stepNumber, "configure"_s, step.PresetName,
-          this->BuildWorkflowStep({ cmSystemTools::GetCMakeCommand(),
-                                    "--preset", step.PresetName }));
+        std::vector<std::string> args{ cmSystemTools::GetCMakeCommand(),
+                                       "--preset", step.PresetName };
+        if (fresh == WorkflowFresh::Yes) {
+          args.emplace_back("--fresh");
+        }
+        steps.emplace_back(stepNumber, "configure"_s, step.PresetName,
+                           this->BuildWorkflowStep(args));
       } break;
       case cmCMakePresetsGraph::WorkflowPreset::WorkflowStep::Type::Build: {
         auto const* buildPreset = this->FindPresetForWorkflow(

+ 12 - 1
Source/cmake.h

@@ -611,7 +611,18 @@ public:
   bool Open(const std::string& dir, bool dryRun);
 
   //! run the --workflow option
-  int Workflow(const std::string& presetName, bool listPresets);
+  enum class WorkflowListPresets
+  {
+    No,
+    Yes,
+  };
+  enum class WorkflowFresh
+  {
+    No,
+    Yes,
+  };
+  int Workflow(const std::string& presetName, WorkflowListPresets listPresets,
+               WorkflowFresh fresh);
 
   void UnwatchUnusedCli(const std::string& var);
   void WatchUnusedCli(const std::string& var);

+ 27 - 5
Source/cmakemain.cxx

@@ -917,8 +917,11 @@ int do_workflow(int ac, char const* const* av)
   std::cerr << "This cmake does not support --workflow\n";
   return -1;
 #else
+  using WorkflowListPresets = cmake::WorkflowListPresets;
+  using WorkflowFresh = cmake::WorkflowFresh;
   std::string presetName;
-  bool listPresets = false;
+  auto listPresets = WorkflowListPresets::No;
+  auto fresh = WorkflowFresh::No;
 
   using CommandArgument =
     cmCommandLineArgument<bool(std::string const& value)>;
@@ -927,7 +930,15 @@ int do_workflow(int ac, char const* const* av)
     CommandArgument{ "--preset", CommandArgument::Values::One,
                      CommandArgument::setToValue(presetName) },
     CommandArgument{ "--list-presets", CommandArgument::Values::Zero,
-                     CommandArgument::setToTrue(listPresets) }
+                     [&listPresets](const std::string&) -> bool {
+                       listPresets = WorkflowListPresets::Yes;
+                       return true;
+                     } },
+    CommandArgument{ "--fresh", CommandArgument::Values::Zero,
+                     [&fresh](const std::string&) -> bool {
+                       fresh = WorkflowFresh::Yes;
+                       return true;
+                     } },
   };
 
   std::vector<std::string> inputArgs;
@@ -949,14 +960,25 @@ int do_workflow(int ac, char const* const* av)
     }
     if (!(matched && parsed)) {
       if (!matched) {
+        presetName.clear();
+        listPresets = WorkflowListPresets::No;
         std::cerr << "Unknown argument " << arg << std::endl;
       }
       break;
     }
   }
 
-  if (presetName.empty() && !listPresets) {
-    std::cerr << "TODO: Usage\n";
+  if (presetName.empty() && listPresets == WorkflowListPresets::No) {
+    /* clang-format off */
+    std::cerr <<
+      "Usage: cmake --workflow [options]\n"
+      "Options:\n"
+      "  --preset <preset> = Workflow preset to execute.\n"
+      "  --list-presets    = List available workflow presets.\n"
+      "  --fresh           = Configure a fresh build tree, removing any "
+                            "existing cache file.\n"
+      ;
+    /* clang-format on */
     return 1;
   }
 
@@ -969,7 +991,7 @@ int do_workflow(int ac, char const* const* av)
     cmakemainProgressCallback(msg, prog, &cm);
   });
 
-  return cm.Workflow(presetName, listPresets);
+  return cm.Workflow(presetName, listPresets, fresh);
 #endif
 }
 

+ 4 - 0
Tests/RunCMake/CMakePresetsWorkflow/Fresh.cmake

@@ -0,0 +1,4 @@
+option(FRESH_CONFIGURE "" ON)
+if(NOT FRESH_CONFIGURE)
+  message(FATAL_ERROR "FRESH_CONFIGURE is ${FRESH_CONFIGURE}, should be ON")
+endif()

+ 21 - 0
Tests/RunCMake/CMakePresetsWorkflow/Fresh.json.in

@@ -0,0 +1,21 @@
+{
+  "version": 6,
+  "configurePresets": [
+    {
+      "name": "default",
+      "generator": "@RunCMake_GENERATOR@",
+      "binaryDir": "${sourceDir}/build"
+    }
+  ],
+  "workflowPresets": [
+    {
+      "name": "Fresh",
+      "steps": [
+        {
+          "type": "configure",
+          "name": "default"
+        }
+      ]
+    }
+  ]
+}

+ 1 - 0
Tests/RunCMake/CMakePresetsWorkflow/InvalidOption-result.txt

@@ -0,0 +1 @@
+1

+ 6 - 0
Tests/RunCMake/CMakePresetsWorkflow/InvalidOption-stderr.txt

@@ -0,0 +1,6 @@
+^Unknown argument -DINVALID_OPTION
+Usage: cmake --workflow \[options\]
+Options:
+  --preset <preset> = Workflow preset to execute\.
+  --list-presets    = List available workflow presets\.
+  --fresh           = Configure a fresh build tree, removing any existing cache file\.$

+ 1 - 0
Tests/RunCMake/CMakePresetsWorkflow/InvalidOption-stdout.txt

@@ -0,0 +1 @@
+^$

+ 13 - 3
Tests/RunCMake/CMakePresetsWorkflow/RunCMakeTest.cmake

@@ -10,10 +10,12 @@ function(run_cmake_workflow_presets name)
   set(RunCMake_TEST_BINARY_DIR "${RunCMake_TEST_SOURCE_DIR}/build")
   set(RunCMake_TEST_COMMAND_WORKING_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}")
 
-  set(RunCMake_TEST_NO_CLEAN TRUE)
+  if(NOT RunCMake_TEST_NO_CLEAN)
+    file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
+    file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}")
+  endif()
 
-  file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}")
-  file(MAKE_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}")
+  set(RunCMake_TEST_NO_CLEAN TRUE)
 
   set(CASE_NAME "${name}")
   set(CASE_SOURCE_DIR "${RunCMake_SOURCE_DIR}")
@@ -77,3 +79,11 @@ unset(CMakeUserPresets_FILE)
 unset(CMakePresets_ASSETS)
 
 run_cmake_workflow_presets(ListPresets --list-presets)
+run_cmake_workflow_presets(InvalidOption -DINVALID_OPTION)
+
+set(RunCMake_TEST_NO_CLEAN TRUE)
+file(REMOVE_RECURSE "${RunCMake_BINARY_DIR}/Fresh")
+file(MAKE_DIRECTORY "${RunCMake_BINARY_DIR}/Fresh/build")
+file(WRITE "${RunCMake_BINARY_DIR}/Fresh/build/CMakeCache.txt" "FRESH_CONFIGURE:BOOL=OFF\n")
+run_cmake_workflow_presets(Fresh --fresh)
+unset(RunCMake_TEST_NO_CLEAN)