From e4f1b301fecb9006cdb3153fb3575c6506be9ece Mon Sep 17 00:00:00 2001 From: Alex Turbov Date: Mon, 4 May 2020 23:08:40 +0800 Subject: cmake: Allow arbitrary args passed to CMake script --- Help/manual/cmake.1.rst | 6 +++++- Source/cmake.cxx | 8 +++++++- Tests/RunCMake/CommandLine/P_arbitrary_args.cmake | 3 +++ Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/CommandLine/P_arbitrary_args.cmake diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index e3e965c..9becfc6 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -481,13 +481,17 @@ Run a Script .. code-block:: shell - cmake [{-D =}...] -P + cmake [{-D =}...] -P [-- ...] Process the given cmake file as a script written in the CMake language. No configure or generate step is performed and the cache is not modified. If variables are defined using ``-D``, this must be 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 ` variables passed to the +script (including the ``--`` itself). + Run a Command-Line Tool ======================= diff --git a/Source/cmake.cxx b/Source/cmake.cxx index c95cf4f..d6abd18 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -289,7 +289,8 @@ void cmake::CleanupCommandsAndMacros() // Parse the args bool cmake::SetCacheArgs(const std::vector& args) { - bool findPackageMode = false; + auto findPackageMode = false; + auto seenScriptOption = false; for (unsigned int i = 1; i < args.size(); ++i) { std::string const& arg = args[i]; if (cmHasLiteralPrefix(arg, "-D")) { @@ -444,6 +445,11 @@ bool cmake::SetCacheArgs(const std::vector& args) this->SetHomeOutputDirectory( cmSystemTools::GetCurrentWorkingDirectory()); 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")) { findPackageMode = true; } diff --git a/Tests/RunCMake/CommandLine/P_arbitrary_args.cmake b/Tests/RunCMake/CommandLine/P_arbitrary_args.cmake new file mode 100644 index 0000000..29faae3 --- /dev/null +++ b/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() diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 0f806bc..973391d 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/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(P_no-arg ${CMAKE_COMMAND} -P) 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 ${CMAKE_COMMAND} --build) -- cgit v0.12