From a1adbc724336b0a4185e34b63e18de03a8b119cf Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Sun, 13 Jan 2019 23:26:56 +1100 Subject: cmake: Stop processing if -P option lacks file name While an error message was being logged, processing was continuing nonetheless except with the -P argument omitted. This could have allowed unintended effects if the remaining arguments formed a valid set of command line options. --- Source/cmakemain.cxx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Source') diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index 75dabde222..e52f2b3613 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -285,12 +285,12 @@ int do_cmake(int ac, char const* const* av) } else if (cmHasLiteralPrefix(av[i], "-P")) { if (i == ac - 1) { cmSystemTools::Error("No script specified for argument -P"); - } else { - workingMode = cmake::SCRIPT_MODE; - args.push_back(av[i]); - i++; - args.push_back(av[i]); + return 1; } + workingMode = cmake::SCRIPT_MODE; + args.push_back(av[i]); + i++; + args.push_back(av[i]); } else if (cmHasLiteralPrefix(av[i], "--find-package")) { workingMode = cmake::FIND_PACKAGE_MODE; args.push_back(av[i]); -- cgit v1.2.1 From 27eb7c5bdb5bb8deefe1772675dc4819592bf036 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Sun, 13 Jan 2019 23:44:18 +1100 Subject: cmake: Ensure source and binary dirs are set If only the source dir is provided, the binary dir is assumed to be the working directory. If only the binary dir is provided and it doesn't yet have a CMakeCache.txt to provide the source dir, then the source dir is assumed to be the working directory. This logic was not previously being handled correctly when -S and/or -B options were involved. Furthermore, when both were missing, no suitable error message was provided and an empty string was used for the build directory. Fixes: #18707 --- Source/cmake.cxx | 25 ++++++++++++++++--------- Source/cmake.h | 3 +-- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'Source') diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 1aff5eb372..74542df968 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -609,16 +609,13 @@ bool cmake::FindPackage(const std::vector& args) } // Parse the args -void cmake::SetArgs(const std::vector& args, - bool directoriesSetBefore) +void cmake::SetArgs(const std::vector& args) { - bool directoriesSet = directoriesSetBefore; bool haveToolset = false; bool havePlatform = false; for (unsigned int i = 1; i < args.size(); ++i) { std::string const& arg = args[i]; if (arg.find("-H", 0) == 0 || arg.find("-S", 0) == 0) { - directoriesSet = true; std::string path = arg.substr(2); if (path.empty()) { ++i; @@ -639,7 +636,6 @@ void cmake::SetArgs(const std::vector& args, } else if (arg.find("-O", 0) == 0) { // There is no local generate anymore. Ignore -O option. } else if (arg.find("-B", 0) == 0) { - directoriesSet = true; std::string path = arg.substr(2); if (path.empty()) { ++i; @@ -801,16 +797,27 @@ void cmake::SetArgs(const std::vector& args, this->SetGlobalGenerator(gen); } } - // no option assume it is the path to the source + // no option assume it is the path to the source or an existing build else { - directoriesSet = true; this->SetDirectoriesFromFile(arg.c_str()); } } - if (!directoriesSet) { - this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory()); + + const bool haveSourceDir = !this->GetHomeDirectory().empty(); + const bool haveBinaryDir = !this->GetHomeOutputDirectory().empty(); + + if (this->CurrentWorkingMode == cmake::NORMAL_MODE && !haveSourceDir && + !haveBinaryDir) { + cmSystemTools::Error("No source or binary directory provided"); + return; + } + + if (!haveSourceDir) { this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory()); } + if (!haveBinaryDir) { + this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory()); + } } void cmake::SetDirectoriesFromFile(const char* arg) diff --git a/Source/cmake.h b/Source/cmake.h index d3d0e805d4..5bca306932 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -275,8 +275,7 @@ public: int GetSystemInformation(std::vector&); ///! Parse command line arguments - void SetArgs(const std::vector&, - bool directoriesSetBefore = false); + void SetArgs(const std::vector& args); ///! Is this cmake running as a result of a TRY_COMPILE command bool GetIsInTryCompile() const; -- cgit v1.2.1