summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2022-02-25 13:35:46 (GMT)
committerBrad King <brad.king@kitware.com>2022-02-28 15:26:04 (GMT)
commitd33b12d84b59fd3310c31de7d9c3f83b28b681e2 (patch)
tree4a43c7f6e68f1f8770b42288799ecb2433ade03c
parent43416c48ed6bb7d3b8063ac4ea1ba02a4aab828e (diff)
downloadCMake-d33b12d84b59fd3310c31de7d9c3f83b28b681e2.zip
CMake-d33b12d84b59fd3310c31de7d9c3f83b28b681e2.tar.gz
CMake-d33b12d84b59fd3310c31de7d9c3f83b28b681e2.tar.bz2
Add support for build tree symlink inside source tree
Since commit c564a3e3ff (Ninja: Always compile sources using absolute paths, 2021-05-19, v3.21.0-rc1~129^2), both the Ninja and Makefile generators pass source files and include directories to the compiler as absolute paths. However, in some other contexts within generated build systems, we generate paths that may be relative or absolute. In these contexts, we prefer relative paths, but avoid them when they contain a `../` sequence that leaves both the build tree and the source tree: * When the build tree is outside of the source tree, all paths to the source tree are absolute. * When the build tree is inside the source tree, we previously assumed that it is a real directory such that exiting the build tree with `../` enters the source tree. This allowed paths to the source tree to be relative to the build tree. In the latter case, we previously did not support using a symbolic link inside the source tree to point at the build tree. This is because relative paths to the source tree would be generated with `../` sequences leaving the build tree, but they would jump to the parent of the real build tree, which is not the source tree. Fix this by requiring that `../` sequences stay inside the build tree even if its path appears to be inside the source tree. When the build tree is inside the source tree, all paths to the source tree are now absolute. For consistency, this applies regardless of whether the path to the build tree contains a symbolic link. Fixes: #21819
-rw-r--r--Source/cmOutputConverter.cxx46
-rw-r--r--Source/cmOutputConverter.h9
-rw-r--r--Tests/RunCMake/SymlinkTrees/RunCMakeTest.cmake6
3 files changed, 51 insertions, 10 deletions
diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx
index 3d61743..e62e0cd 100644
--- a/Source/cmOutputConverter.cxx
+++ b/Source/cmOutputConverter.cxx
@@ -34,6 +34,7 @@ cmOutputConverter::cmOutputConverter(cmStateSnapshot const& snapshot)
assert(this->StateSnapshot.IsValid());
this->ComputeRelativePathTopSource();
this->ComputeRelativePathTopBinary();
+ this->ComputeRelativePathTopRelation();
}
void cmOutputConverter::ComputeRelativePathTopSource()
@@ -69,6 +70,22 @@ void cmOutputConverter::ComputeRelativePathTopBinary()
this->RelativePathTopBinary = snapshot.GetDirectory().GetCurrentBinary();
}
+void cmOutputConverter::ComputeRelativePathTopRelation()
+{
+ if (cmSystemTools::ComparePath(this->RelativePathTopSource,
+ this->RelativePathTopBinary)) {
+ this->RelativePathTopRelation = TopRelation::InSource;
+ } else if (cmSystemTools::IsSubDirectory(this->RelativePathTopBinary,
+ this->RelativePathTopSource)) {
+ this->RelativePathTopRelation = TopRelation::BinInSrc;
+ } else if (cmSystemTools::IsSubDirectory(this->RelativePathTopSource,
+ this->RelativePathTopBinary)) {
+ this->RelativePathTopRelation = TopRelation::SrcInBin;
+ } else {
+ this->RelativePathTopRelation = TopRelation::Separate;
+ }
+}
+
std::string const& cmOutputConverter::GetRelativePathTopSource() const
{
return this->RelativePathTopSource;
@@ -84,19 +101,40 @@ void cmOutputConverter::SetRelativePathTop(std::string const& topSource,
{
this->RelativePathTopSource = topSource;
this->RelativePathTopBinary = topBinary;
+ this->ComputeRelativePathTopRelation();
}
std::string cmOutputConverter::MaybeRelativeTo(
std::string const& local_path, std::string const& remote_path) const
{
- bool bothInBinary =
- PathEqOrSubDir(local_path, this->RelativePathTopBinary) &&
+ bool localInBinary = PathEqOrSubDir(local_path, this->RelativePathTopBinary);
+ bool remoteInBinary =
PathEqOrSubDir(remote_path, this->RelativePathTopBinary);
- bool bothInSource =
- PathEqOrSubDir(local_path, this->RelativePathTopSource) &&
+ bool localInSource = PathEqOrSubDir(local_path, this->RelativePathTopSource);
+ bool remoteInSource =
PathEqOrSubDir(remote_path, this->RelativePathTopSource);
+ switch (this->RelativePathTopRelation) {
+ case TopRelation::Separate:
+ // Checks are independent.
+ break;
+ case TopRelation::BinInSrc:
+ localInSource = localInSource && !localInBinary;
+ remoteInSource = remoteInSource && !remoteInBinary;
+ break;
+ case TopRelation::SrcInBin:
+ localInBinary = localInBinary && !localInSource;
+ remoteInBinary = remoteInBinary && !remoteInSource;
+ break;
+ case TopRelation::InSource:
+ // Checks are identical.
+ break;
+ };
+
+ bool const bothInBinary = localInBinary && remoteInBinary;
+ bool const bothInSource = localInSource && remoteInSource;
+
if (bothInBinary || bothInSource) {
return cmSystemTools::ForceToRelativePath(local_path, remote_path);
}
diff --git a/Source/cmOutputConverter.h b/Source/cmOutputConverter.h
index 290809f..d19bccc 100644
--- a/Source/cmOutputConverter.h
+++ b/Source/cmOutputConverter.h
@@ -147,8 +147,17 @@ private:
// safely by the build tools.
std::string RelativePathTopSource;
std::string RelativePathTopBinary;
+ enum class TopRelation
+ {
+ Separate,
+ BinInSrc,
+ SrcInBin,
+ InSource,
+ };
+ TopRelation RelativePathTopRelation = TopRelation::Separate;
void ComputeRelativePathTopSource();
void ComputeRelativePathTopBinary();
+ void ComputeRelativePathTopRelation();
std::string MaybeRelativeTo(std::string const& local_path,
std::string const& remote_path) const;
};
diff --git a/Tests/RunCMake/SymlinkTrees/RunCMakeTest.cmake b/Tests/RunCMake/SymlinkTrees/RunCMakeTest.cmake
index 3f3aafe..e5bfac4 100644
--- a/Tests/RunCMake/SymlinkTrees/RunCMakeTest.cmake
+++ b/Tests/RunCMake/SymlinkTrees/RunCMakeTest.cmake
@@ -76,12 +76,6 @@ function (run_symlink_test case src bin src_from_bin bin_from_src)
run_symlink_test_case("${case}" -S "../${name}/${src}" -B "../${name}/${bin}")
# Verify paths passed to compiler.
- if(case MATCHES "^(different|asymmetric)-bin_in_src$")
- # FIXME: Some generators compute incorrect relative paths.
- message(STATUS "${case}-exe - SKIPPED")
- message(STATUS "${case}-exe-build - SKIPPED")
- return()
- endif()
unset(RunCMake_TEST_VARIANT_DESCRIPTION)
run_symlink_test_case("${case}-exe" -S "${src}" -B "${bin}")
if (RunCMake_GENERATOR MATCHES "Xcode")