CMake: -S paths preferred over other provided paths

When cmake parses `cmake -S <path> <path>` treat the second
path as the build directory. Likwise when invoked with
`<path> -S <path>` treat the first path as the build path
instead of using the current working directory.
This commit is contained in:
Robert Maynard
2022-01-18 10:51:14 -05:00
parent 2e1b7e5b9a
commit b2bc3364f0

View File

@@ -1456,7 +1456,6 @@ void cmake::SetDirectoriesFromFile(const std::string& arg)
// CMakeLists.txt file. // CMakeLists.txt file.
std::string listPath; std::string listPath;
std::string cachePath; std::string cachePath;
bool argIsFile = false;
if (cmSystemTools::FileIsDirectory(arg)) { if (cmSystemTools::FileIsDirectory(arg)) {
std::string path = cmSystemTools::CollapseFullPath(arg); std::string path = cmSystemTools::CollapseFullPath(arg);
cmSystemTools::ConvertToUnixSlashes(path); cmSystemTools::ConvertToUnixSlashes(path);
@@ -1469,7 +1468,6 @@ void cmake::SetDirectoriesFromFile(const std::string& arg)
listPath = path; listPath = path;
} }
} else if (cmSystemTools::FileExists(arg)) { } else if (cmSystemTools::FileExists(arg)) {
argIsFile = true;
std::string fullPath = cmSystemTools::CollapseFullPath(arg); std::string fullPath = cmSystemTools::CollapseFullPath(arg);
std::string name = cmSystemTools::GetFilenameName(fullPath); std::string name = cmSystemTools::GetFilenameName(fullPath);
name = cmSystemTools::LowerCase(name); name = cmSystemTools::LowerCase(name);
@@ -1485,7 +1483,6 @@ void cmake::SetDirectoriesFromFile(const std::string& arg)
std::string name = cmSystemTools::GetFilenameName(fullPath); std::string name = cmSystemTools::GetFilenameName(fullPath);
name = cmSystemTools::LowerCase(name); name = cmSystemTools::LowerCase(name);
if (name == "cmakecache.txt"_s || name == "cmakelists.txt"_s) { if (name == "cmakecache.txt"_s || name == "cmakelists.txt"_s) {
argIsFile = true;
listPath = cmSystemTools::GetFilenamePath(fullPath); listPath = cmSystemTools::GetFilenamePath(fullPath);
} else { } else {
listPath = fullPath; listPath = fullPath;
@@ -1505,36 +1502,37 @@ void cmake::SetDirectoriesFromFile(const std::string& arg)
} }
} }
bool no_source_tree = this->GetHomeDirectory().empty();
bool no_build_tree = this->GetHomeOutputDirectory().empty();
// If there is a CMakeLists.txt file, use it as the source tree. // If there is a CMakeLists.txt file, use it as the source tree.
if (!listPath.empty()) { if (!listPath.empty()) {
this->SetHomeDirectory(listPath);
if (argIsFile) { // When invoked with a path that points to an existing CMakeCache
// Source CMakeLists.txt file given. It was probably dropped // This function is called multiple times with the same path
// onto the executable in a GUI. Default to an in-source build. if (no_source_tree && no_build_tree) {
this->SetHomeDirectory(listPath);
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
this->SetHomeOutputDirectory(cwd);
} else if (no_source_tree) {
this->SetHomeDirectory(listPath);
} else if (no_build_tree) {
this->SetHomeOutputDirectory(listPath); this->SetHomeOutputDirectory(listPath);
} else {
// Source directory given on command line. Use current working
// directory as build tree if -B hasn't been given already
if (this->GetHomeOutputDirectory().empty()) {
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
this->SetHomeOutputDirectory(cwd);
}
} }
return; } else {
} if (no_source_tree) {
// We didn't find a CMakeLists.txt and it wasn't specified
if (this->GetHomeDirectory().empty()) { // with -S. Assume it is the path to the source tree
// We didn't find a CMakeLists.txt and it wasn't specified std::string full = cmSystemTools::CollapseFullPath(arg);
// with -S. Assume it is the path to the source tree this->SetHomeDirectory(full);
std::string full = cmSystemTools::CollapseFullPath(arg); }
this->SetHomeDirectory(full); if (no_build_tree) {
} // We didn't find a CMakeCache.txt and it wasn't specified
if (this->GetHomeOutputDirectory().empty()) { // with -B. Assume the current working directory as the build tree.
// We didn't find a CMakeCache.txt and it wasn't specified std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
// with -B. Assume the current working directory as the build tree. this->SetHomeOutputDirectory(cwd);
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory(); }
this->SetHomeOutputDirectory(cwd);
} }
} }