瀏覽代碼

cmake: Improve acceptance of arbitrary arguments in -P script mode

The fix in commit e4f1b301fe (cmake: Allow arbitrary args passed to
CMake script, 2020-05-04, v3.18.0-rc1~211^2) only applied to "cache"
arguments like `-DFOO`.  Extend the fix to allow arbitrary arguments
that collide with other CMake arguments like `-S` and `-B`.
Brad King 3 年之前
父節點
當前提交
36056ff5a3

+ 6 - 0
Source/cmake.cxx

@@ -1158,6 +1158,12 @@ void cmake::SetArgs(const std::vector<std::string>& args)
     // iterate each argument
     std::string const& arg = args[i];
 
+    if (scriptMode && arg == "--") {
+      // Stop processing CMake args and avoid possible errors
+      // when arbitrary args are given to CMake script.
+      break;
+    }
+
     // Generator flag has special handling for when to print help
     // so it becomes the exception
     if (generatorCommand.matches(arg)) {

+ 8 - 0
Tests/RunCMake/CommandLine/P_arbitrary_args-stdout.txt

@@ -0,0 +1,8 @@
+^-- CMAKE_ARGC='7'
+-- CMAKE_ARGV1='-P'
+-- CMAKE_ARGV2='[^']*/Tests/RunCMake/CommandLine/P_arbitrary_args.cmake'
+-- CMAKE_ARGV3='--'
+-- CMAKE_ARGV4='-DFOO'
+-- CMAKE_ARGV5='-S'
+-- CMAKE_ARGV6='-B'
+-- CMAKE_ARGV7=''$

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

@@ -1,3 +1,8 @@
-if(NOT ("${CMAKE_ARGV3}" STREQUAL "--" AND "${CMAKE_ARGV4}" STREQUAL "-DFOO"))
-    message(FATAL_ERROR "`-DFOO` shouldn't trigger an error after `--`")
-endif()
+message(STATUS "CMAKE_ARGC='${CMAKE_ARGC}'")
+message(STATUS "CMAKE_ARGV1='${CMAKE_ARGV1}'")
+message(STATUS "CMAKE_ARGV2='${CMAKE_ARGV2}'")
+message(STATUS "CMAKE_ARGV3='${CMAKE_ARGV3}'")
+message(STATUS "CMAKE_ARGV4='${CMAKE_ARGV4}'")
+message(STATUS "CMAKE_ARGV5='${CMAKE_ARGV5}'")
+message(STATUS "CMAKE_ARGV6='${CMAKE_ARGV6}'")
+message(STATUS "CMAKE_ARGV7='${CMAKE_ARGV7}'")

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

@@ -52,7 +52,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(P_arbitrary_args ${CMAKE_COMMAND} -P "${RunCMake_SOURCE_DIR}/P_arbitrary_args.cmake" -- -DFOO -S -B)
 
 run_cmake_command(build-no-dir
   ${CMAKE_COMMAND} --build)