Pārlūkot izejas kodu

Merge topic 'script-mode-and-arbitrary-args'

e4f1b301fe cmake: Allow arbitrary args passed to CMake script

Acked-by: Kitware Robot <[email protected]>
Merge-request: !4707
Craig Scott 5 gadi atpakaļ
vecāks
revīzija
b743ffbfa2

+ 5 - 1
Help/manual/cmake.1.rst

@@ -481,13 +481,17 @@ Run a Script
 
 
 .. code-block:: shell
 .. code-block:: shell
 
 
-  cmake [{-D <var>=<value>}...] -P <cmake-script-file>
+  cmake [{-D <var>=<value>}...] -P <cmake-script-file> [-- <unparsed-options>...]
 
 
 Process the given cmake file as a script written in the CMake
 Process the given cmake file as a script written in the CMake
 language.  No configure or generate step is performed and the cache
 language.  No configure or generate step is performed and the cache
 is not modified.  If variables are defined using ``-D``, this must be
 is not modified.  If variables are defined using ``-D``, this must be
 done before the ``-P`` argument.
 done before the ``-P`` argument.
 
 
+Any options after ``--`` are not parsed by CMake, but they are still included
+in the set of :variable:`CMAKE_ARGV<n> <CMAKE_ARGV0>` variables passed to the
+script (including the ``--`` itself).
+
 
 
 Run a Command-Line Tool
 Run a Command-Line Tool
 =======================
 =======================

+ 7 - 1
Source/cmake.cxx

@@ -291,7 +291,8 @@ void cmake::CleanupCommandsAndMacros()
 // Parse the args
 // Parse the args
 bool cmake::SetCacheArgs(const std::vector<std::string>& args)
 bool cmake::SetCacheArgs(const std::vector<std::string>& args)
 {
 {
-  bool findPackageMode = false;
+  auto findPackageMode = false;
+  auto seenScriptOption = false;
   for (unsigned int i = 1; i < args.size(); ++i) {
   for (unsigned int i = 1; i < args.size(); ++i) {
     std::string const& arg = args[i];
     std::string const& arg = args[i];
     if (cmHasLiteralPrefix(arg, "-D")) {
     if (cmHasLiteralPrefix(arg, "-D")) {
@@ -446,6 +447,11 @@ bool cmake::SetCacheArgs(const std::vector<std::string>& args)
       this->SetHomeOutputDirectory(
       this->SetHomeOutputDirectory(
         cmSystemTools::GetCurrentWorkingDirectory());
         cmSystemTools::GetCurrentWorkingDirectory());
       this->ReadListFile(args, path);
       this->ReadListFile(args, path);
+      seenScriptOption = true;
+    } else if (arg == "--" && seenScriptOption) {
+      // Stop processing CMake args and avoid possible errors
+      // when arbitrary args are given to CMake script.
+      break;
     } else if (cmHasLiteralPrefix(arg, "--find-package")) {
     } else if (cmHasLiteralPrefix(arg, "--find-package")) {
       findPackageMode = true;
       findPackageMode = true;
     }
     }

+ 3 - 0
Tests/RunCMake/CommandLine/P_arbitrary_args.cmake

@@ -0,0 +1,3 @@
+if(NOT ("${CMAKE_ARGV3}" STREQUAL "--" AND "${CMAKE_ARGV4}" STREQUAL "-DFOO"))
+    message(FATAL_ERROR "`-DFOO` shouldn't trigger an error after `--`")
+endif()

+ 1 - 0
Tests/RunCMake/CommandLine/RunCMakeTest.cmake

@@ -46,6 +46,7 @@ run_cmake_command(G_no-arg ${CMAKE_COMMAND} -B DummyBuildDir -G)
 run_cmake_command(G_bad-arg ${CMAKE_COMMAND} -B DummyBuildDir -G NoSuchGenerator)
 run_cmake_command(G_bad-arg ${CMAKE_COMMAND} -B DummyBuildDir -G NoSuchGenerator)
 run_cmake_command(P_no-arg ${CMAKE_COMMAND} -P)
 run_cmake_command(P_no-arg ${CMAKE_COMMAND} -P)
 run_cmake_command(P_no-file ${CMAKE_COMMAND} -P nosuchscriptfile.cmake)
 run_cmake_command(P_no-file ${CMAKE_COMMAND} -P nosuchscriptfile.cmake)
+run_cmake_command(P_arbitrary_args ${CMAKE_COMMAND} -P "${RunCMake_SOURCE_DIR}/P_arbitrary_args.cmake" -- -DFOO)
 
 
 run_cmake_command(build-no-dir
 run_cmake_command(build-no-dir
   ${CMAKE_COMMAND} --build)
   ${CMAKE_COMMAND} --build)