From 36056ff5a38e72f2edf7f042d594d0356301bb9e Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 17 Mar 2022 13:50:12 -0400 Subject: 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`. --- Source/cmake.cxx | 6 ++++++ Tests/RunCMake/CommandLine/P_arbitrary_args-stdout.txt | 8 ++++++++ Tests/RunCMake/CommandLine/P_arbitrary_args.cmake | 11 ++++++++--- Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 Tests/RunCMake/CommandLine/P_arbitrary_args-stdout.txt diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 81d225d..c41c26f 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1158,6 +1158,12 @@ void cmake::SetArgs(const std::vector& 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)) { diff --git a/Tests/RunCMake/CommandLine/P_arbitrary_args-stdout.txt b/Tests/RunCMake/CommandLine/P_arbitrary_args-stdout.txt new file mode 100644 index 0000000..c0f96f3 --- /dev/null +++ b/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=''$ diff --git a/Tests/RunCMake/CommandLine/P_arbitrary_args.cmake b/Tests/RunCMake/CommandLine/P_arbitrary_args.cmake index 29faae3..d0a4859 100644 --- a/Tests/RunCMake/CommandLine/P_arbitrary_args.cmake +++ b/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}'") diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 5b8c715..38ce9f2 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/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) -- cgit v0.12