diff options
author | Robert Maynard <rmaynard@nvidia.com> | 2022-01-20 16:04:57 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2022-01-20 19:38:12 (GMT) |
commit | eacf1f879b0933509efbd4fb4d6d72ce99412aa7 (patch) | |
tree | a869c4da32763c06491413a5c0b6695b6d0628de /Source/cmake.cxx | |
parent | a4f076110d4ceaf92f60a604fbca08f645cb5844 (diff) | |
download | CMake-eacf1f879b0933509efbd4fb4d6d72ce99412aa7.zip CMake-eacf1f879b0933509efbd4fb4d6d72ce99412aa7.tar.gz CMake-eacf1f879b0933509efbd4fb4d6d72ce99412aa7.tar.bz2 |
cmake: Warn about unnecessary paths on command line
We can't make it an error as that would break existing behavior.
Fixes: #23110
Diffstat (limited to 'Source/cmake.cxx')
-rw-r--r-- | Source/cmake.cxx | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 5632c38..72b26e1 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -790,6 +790,7 @@ void cmake::SetArgs(const std::vector<std::string>& args) bool haveBArg = false; bool scriptMode = false; std::string possibleUnknownArg; + std::string extraProvidedPath; #if !defined(CMAKE_BOOTSTRAP) std::string profilingFormat; std::string profilingOutput; @@ -1168,10 +1169,18 @@ void cmake::SetArgs(const std::vector<std::string>& args) } else if (!matched && cmHasLiteralPrefix(arg, "-")) { possibleUnknownArg = arg; } else if (!matched) { - this->SetDirectoriesFromFile(arg); + bool parsedDirectory = this->SetDirectoriesFromFile(arg); + if (!parsedDirectory) { + extraProvidedPath = arg; + } } } + if (!extraProvidedPath.empty() && !scriptMode) { + this->IssueMessage(MessageType::WARNING, + cmStrCat("Ignoring extra path from command line:\n ", + extraProvidedPath)); + } if (!possibleUnknownArg.empty() && !scriptMode) { cmSystemTools::Error(cmStrCat("Unknown argument ", possibleUnknownArg)); cmSystemTools::Error("Run 'cmake --help' for all supported options."); @@ -1450,22 +1459,27 @@ void cmake::PrintTraceFormatVersion() } } -void cmake::SetDirectoriesFromFile(const std::string& arg) +bool cmake::SetDirectoriesFromFile(const std::string& arg) { // Check if the argument refers to a CMakeCache.txt or // CMakeLists.txt file. std::string listPath; std::string cachePath; + bool is_empty_directory = false; if (cmSystemTools::FileIsDirectory(arg)) { std::string path = cmSystemTools::CollapseFullPath(arg); cmSystemTools::ConvertToUnixSlashes(path); std::string cacheFile = cmStrCat(path, "/CMakeCache.txt"); std::string listFile = cmStrCat(path, "/CMakeLists.txt"); + + is_empty_directory = true; if (cmSystemTools::FileExists(cacheFile)) { cachePath = path; + is_empty_directory = false; } if (cmSystemTools::FileExists(listFile)) { listPath = path; + is_empty_directory = false; } } else if (cmSystemTools::FileExists(arg)) { std::string fullPath = cmSystemTools::CollapseFullPath(arg); @@ -1497,7 +1511,7 @@ void cmake::SetDirectoriesFromFile(const std::string& arg) if (existingValue) { this->SetHomeOutputDirectory(cachePath); this->SetHomeDirectory(*existingValue); - return; + return true; } } } @@ -1505,9 +1519,15 @@ void cmake::SetDirectoriesFromFile(const std::string& arg) bool no_source_tree = this->GetHomeDirectory().empty(); bool no_build_tree = this->GetHomeOutputDirectory().empty(); + // When invoked with a path that points to an existing CMakeCache + // This function is called multiple times with the same path + const bool passed_same_path = (listPath == this->GetHomeDirectory()) || + (listPath == this->GetHomeOutputDirectory()); + bool used_provided_path = + (passed_same_path || no_source_tree || no_build_tree); + // If there is a CMakeLists.txt file, use it as the source tree. if (!listPath.empty()) { - // When invoked with a path that points to an existing CMakeCache // This function is called multiple times with the same path if (no_source_tree && no_build_tree) { @@ -1527,13 +1547,20 @@ void cmake::SetDirectoriesFromFile(const std::string& arg) std::string full = cmSystemTools::CollapseFullPath(arg); this->SetHomeDirectory(full); } - if (no_build_tree) { + if (no_build_tree && !no_source_tree && is_empty_directory) { + // passed `-S <path> <build_dir> when build_dir is an empty directory + std::string full = cmSystemTools::CollapseFullPath(arg); + this->SetHomeOutputDirectory(full); + } else if (no_build_tree) { // We didn't find a CMakeCache.txt and it wasn't specified // with -B. Assume the current working directory as the build tree. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory(); this->SetHomeOutputDirectory(cwd); + used_provided_path = false; } } + + return used_provided_path; } // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the |