From 1b6c5333a0b4a56eaf8b010084e18a40e473c2e3 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Mon, 30 Nov 2020 12:34:19 -0500 Subject: cmake: Error out on unknown arguments starting with `-`. Fixes: #21521 --- Source/cmake.cxx | 22 +++++++++++++++++++++- Tests/RunCMake/CommandLine/InvalidArg1-result.txt | 1 + Tests/RunCMake/CommandLine/InvalidArg1-stderr.txt | 2 ++ Tests/RunCMake/CommandLine/InvalidArg2-result.txt | 1 + Tests/RunCMake/CommandLine/InvalidArg2-stderr.txt | 2 ++ Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 2 ++ 6 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Tests/RunCMake/CommandLine/InvalidArg1-result.txt create mode 100644 Tests/RunCMake/CommandLine/InvalidArg1-stderr.txt create mode 100644 Tests/RunCMake/CommandLine/InvalidArg2-result.txt create mode 100644 Tests/RunCMake/CommandLine/InvalidArg2-stderr.txt diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 5524d4e..cfd724b 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -846,6 +846,8 @@ void cmake::SetArgs(const std::vector& args) bool haveToolset = false; bool havePlatform = false; bool haveBArg = false; + bool scriptMode = false; + std::string possibleUnknownArg; #if !defined(CMAKE_BOOTSTRAP) std::string profilingFormat; std::string profilingOutput; @@ -898,7 +900,11 @@ void cmake::SetArgs(const std::vector& args) CommandArgument{ "-B", "No build directory specified for -B", CommandArgument::Values::One, BuildArgLambda }, CommandArgument{ "-P", "-P must be followed by a file name.", - CommandArgument::Values::One, IgnoreAndTrueLambda }, + CommandArgument::Values::One, + [&](std::string const&, cmake*) -> bool { + scriptMode = true; + return true; + } }, CommandArgument{ "-D", "-D must be followed with VAR=VALUE.", CommandArgument::Values::One, IgnoreAndTrueLambda }, CommandArgument{ "-C", "-C must be followed by a file name.", @@ -1145,14 +1151,28 @@ void cmake::SetArgs(const std::vector& args) break; } } + + // We have an issue where arguments to a "-P" script mode + // can be provided before the "-P" argument. This means + // that we need to lazily check this argument after checking + // all args. + // Additionally it can't be the source/binary tree location if (!parsedCorrectly) { cmSystemTools::Error("Run 'cmake --help' for all supported options."); exit(1); + } else if (!matched && cmHasLiteralPrefix(arg, "-")) { + possibleUnknownArg = arg; } else if (!matched) { this->SetDirectoriesFromFile(arg); } } + if (!possibleUnknownArg.empty() && !scriptMode) { + cmSystemTools::Error(cmStrCat("Unknown argument ", possibleUnknownArg)); + cmSystemTools::Error("Run 'cmake --help' for all supported options."); + exit(1); + } + // Empty instance, platform and toolset if only a generator is specified if (this->GlobalGenerator) { this->GeneratorInstance = ""; diff --git a/Tests/RunCMake/CommandLine/InvalidArg1-result.txt b/Tests/RunCMake/CommandLine/InvalidArg1-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/InvalidArg1-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/InvalidArg1-stderr.txt b/Tests/RunCMake/CommandLine/InvalidArg1-stderr.txt new file mode 100644 index 0000000..6b825bb --- /dev/null +++ b/Tests/RunCMake/CommandLine/InvalidArg1-stderr.txt @@ -0,0 +1,2 @@ +^CMake Error: Unknown argument -invalid +CMake Error: Run 'cmake --help' for all supported options.$ diff --git a/Tests/RunCMake/CommandLine/InvalidArg2-result.txt b/Tests/RunCMake/CommandLine/InvalidArg2-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CommandLine/InvalidArg2-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CommandLine/InvalidArg2-stderr.txt b/Tests/RunCMake/CommandLine/InvalidArg2-stderr.txt new file mode 100644 index 0000000..eb1488c --- /dev/null +++ b/Tests/RunCMake/CommandLine/InvalidArg2-stderr.txt @@ -0,0 +1,2 @@ +^CMake Error: Unknown argument --invalid +CMake Error: Run 'cmake --help' for all supported options.$ diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index bb40c52..51754fc 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.1) include(RunCMake) run_cmake_command(NoArgs ${CMAKE_COMMAND}) +run_cmake_command(InvalidArg1 ${CMAKE_COMMAND} -invalid) +run_cmake_command(InvalidArg2 ${CMAKE_COMMAND} --invalid) run_cmake_command(Wizard ${CMAKE_COMMAND} -i) run_cmake_command(C-no-arg ${CMAKE_COMMAND} -B DummyBuildDir -C) run_cmake_command(C-no-file ${CMAKE_COMMAND} -B DummyBuildDir -C nosuchcachefile.txt) -- cgit v0.12