From 149af87b86362449a99301860af5aac1a3b4b20c Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Thu, 16 Jun 2016 21:37:57 +0200 Subject: [PATCH 1/3] cmOutputConverter: split ConvertToRelativePath Split the ConvertToRelativePath funtion into ConvertToRelativePath and ForceToRelativePath. Both functions take the local path as a string rather than a vector of path segments. Reimplement the old interface on top of the two new functions for interface compatibility. --- Source/cmOutputConverter.cxx | 76 ++++++++++++++++++++++++------------ Source/cmOutputConverter.h | 18 +++++++++ 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index d44fbb7739..e6aaed1bca 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -128,41 +128,67 @@ std::string cmOutputConverter::ConvertToRelativePath( const std::vector& local, const std::string& in_remote, bool force) const { - // The path should never be quoted. - assert(in_remote[0] != '\"'); + std::string local_path = cmSystemTools::JoinPath(local); + return force ? this->ForceToRelativePath(local_path, in_remote) + : this->ConvertToRelativePath(local_path, in_remote); +} + +std::string cmOutputConverter::ConvertToRelativePath( + std::string const& local_path, std::string const& remote_path) const +{ + // The paths should never be quoted. + assert(local_path[0] != '\"'); + assert(remote_path[0] != '\"'); // The local path should never have a trailing slash. - assert(!local.empty() && !(local[local.size() - 1] == "")); + assert(local_path.empty() || local_path[local_path.size() - 1] != '/'); // If the path is already relative then just return the path. - if (!cmSystemTools::FileIsFullPath(in_remote.c_str())) { - return in_remote; + if (!cmSystemTools::FileIsFullPath(remote_path.c_str())) { + return remote_path; } - if (!force) { - // Skip conversion if the path and local are not both in the source - // or both in the binary tree. - std::string local_path = cmSystemTools::JoinPath(local); - if (!((cmOutputConverterNotAbove( - local_path.c_str(), - this->StateSnapshot.GetDirectory().GetRelativePathTopBinary()) && - cmOutputConverterNotAbove( - in_remote.c_str(), - this->StateSnapshot.GetDirectory().GetRelativePathTopBinary())) || - (cmOutputConverterNotAbove( - local_path.c_str(), - this->StateSnapshot.GetDirectory().GetRelativePathTopSource()) && - cmOutputConverterNotAbove(in_remote.c_str(), - this->StateSnapshot.GetDirectory() - .GetRelativePathTopSource())))) { - return in_remote; - } + // Skip conversion if the path and local are not both in the source + // or both in the binary tree. + if (!((cmOutputConverterNotAbove( + local_path.c_str(), + this->StateSnapshot.GetDirectory().GetRelativePathTopBinary()) && + cmOutputConverterNotAbove( + remote_path.c_str(), + this->StateSnapshot.GetDirectory().GetRelativePathTopBinary())) || + (cmOutputConverterNotAbove( + local_path.c_str(), + this->StateSnapshot.GetDirectory().GetRelativePathTopSource()) && + cmOutputConverterNotAbove( + remote_path.c_str(), + this->StateSnapshot.GetDirectory().GetRelativePathTopSource())))) { + return remote_path; + } + + return this->ForceToRelativePath(local_path, remote_path); +} + +std::string cmOutputConverter::ForceToRelativePath( + std::string const& local_path, std::string const& remote_path) +{ + // The paths should never be quoted. + assert(local_path[0] != '\"'); + assert(remote_path[0] != '\"'); + + // The local path should never have a trailing slash. + assert(local_path.empty() || local_path[local_path.size() - 1] != '/'); + + // If the path is already relative then just return the path. + if (!cmSystemTools::FileIsFullPath(remote_path.c_str())) { + return remote_path; } // Identify the longest shared path component between the remote // path and the local path. + std::vector local; + cmSystemTools::SplitPath(local_path, local); std::vector remote; - cmSystemTools::SplitPath(in_remote, remote); + cmSystemTools::SplitPath(remote_path, remote); unsigned int common = 0; while (common < remote.size() && common < local.size() && cmSystemTools::ComparePath(remote[common], local[common])) { @@ -171,7 +197,7 @@ std::string cmOutputConverter::ConvertToRelativePath( // If no part of the path is in common then return the full path. if (common == 0) { - return in_remote; + return remote_path; } // If the entire path is in common then just return a ".". diff --git a/Source/cmOutputConverter.h b/Source/cmOutputConverter.h index ac58ddc32d..1efe109aa4 100644 --- a/Source/cmOutputConverter.h +++ b/Source/cmOutputConverter.h @@ -145,6 +145,24 @@ public: const std::string& in_remote, bool force = false) const; + /** + * Convert the given remote path to a relative path with respect to + * the given local path. Both paths must use forward slashes and not + * already be escaped or quoted. + * The conversion is skipped if the paths are not both in the source + * or both in the binary tree. + */ + std::string ConvertToRelativePath(std::string const& local_path, + std::string const& remote_path) const; + + /** + * Convert the given remote path to a relative path with respect to + * the given local path. Both paths must use forward slashes and not + * already be escaped or quoted. + */ + static std::string ForceToRelativePath(std::string const& local_path, + std::string const& remote_path); + private: cmState* GetState() const; From 8d47a20f131147d4cf38e63df0c382ec5844a833 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Thu, 16 Jun 2016 23:08:49 +0200 Subject: [PATCH 2/3] cmOutputConverter: use new ConvertToRelativePath signature internally --- Source/cmOutputConverter.cxx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index e6aaed1bca..ae7c58307f 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -54,21 +54,19 @@ std::string cmOutputConverter::ConvertToRelativePath( switch (relative) { case HOME: result = this->ConvertToRelativePath( - this->GetState()->GetSourceDirectoryComponents(), source); + this->GetState()->GetSourceDirectory(), source); break; case START: result = this->ConvertToRelativePath( - this->StateSnapshot.GetDirectory().GetCurrentSourceComponents(), - source); + this->StateSnapshot.GetDirectory().GetCurrentSource(), source); break; case HOME_OUTPUT: result = this->ConvertToRelativePath( - this->GetState()->GetBinaryDirectoryComponents(), source); + this->GetState()->GetBinaryDirectory(), source); break; case START_OUTPUT: result = this->ConvertToRelativePath( - this->StateSnapshot.GetDirectory().GetCurrentBinaryComponents(), - source); + this->StateSnapshot.GetDirectory().GetCurrentBinary(), source); break; } return result; From 6afd35b98a2316685a071a43d0e56448e7ab9ed5 Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Thu, 16 Jun 2016 23:15:03 +0200 Subject: [PATCH 3/3] cmState: remove unused code Remove the code that was used by cmOutputConverter exclusively. --- Source/cmState.cxx | 36 ------------------------------------ Source/cmState.h | 8 -------- 2 files changed, 44 deletions(-) diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 073c239a31..ffb104b93a 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -77,8 +77,6 @@ struct cmState::BuildsystemDirectoryStateType std::string Location; std::string OutputLocation; - std::vector CurrentSourceDirectoryComponents; - std::vector CurrentBinaryDirectoryComponents; // The top-most directories for relative path conversion. Both the // source and destination location of a relative path conversion // must be underneath one of these directories (both under source or @@ -591,10 +589,6 @@ void cmState::SetSourceDirectory(std::string const& sourceDirectory) { this->SourceDirectory = sourceDirectory; cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory); - - cmSystemTools::SplitPath( - cmSystemTools::CollapseFullPath(this->SourceDirectory), - this->SourceDirectoryComponents); } const char* cmState::GetSourceDirectory() const @@ -602,19 +596,10 @@ const char* cmState::GetSourceDirectory() const return this->SourceDirectory.c_str(); } -std::vector const& cmState::GetSourceDirectoryComponents() const -{ - return this->SourceDirectoryComponents; -} - void cmState::SetBinaryDirectory(std::string const& binaryDirectory) { this->BinaryDirectory = binaryDirectory; cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory); - - cmSystemTools::SplitPath( - cmSystemTools::CollapseFullPath(this->BinaryDirectory), - this->BinaryDirectoryComponents); } void cmState::SetWindowsShell(bool windowsShell) @@ -692,11 +677,6 @@ const char* cmState::GetBinaryDirectory() const return this->BinaryDirectory.c_str(); } -std::vector const& cmState::GetBinaryDirectoryComponents() const -{ - return this->BinaryDirectoryComponents; -} - void cmState::Directory::ComputeRelativePathTopSource() { // Relative path conversion inside the source tree is not used to @@ -978,8 +958,6 @@ void cmState::Directory::SetCurrentSource(std::string const& dir) cmSystemTools::ConvertToUnixSlashes(loc); loc = cmSystemTools::CollapseFullPath(loc); - cmSystemTools::SplitPath( - loc, this->DirectoryState->CurrentSourceDirectoryComponents); this->ComputeRelativePathTopSource(); this->Snapshot_.SetDefinition("CMAKE_CURRENT_SOURCE_DIR", loc); @@ -997,8 +975,6 @@ void cmState::Directory::SetCurrentBinary(std::string const& dir) cmSystemTools::ConvertToUnixSlashes(loc); loc = cmSystemTools::CollapseFullPath(loc); - cmSystemTools::SplitPath( - loc, this->DirectoryState->CurrentBinaryDirectoryComponents); this->ComputeRelativePathTopBinary(); this->Snapshot_.SetDefinition("CMAKE_CURRENT_BINARY_DIR", loc); @@ -1009,18 +985,6 @@ void cmState::Snapshot::SetListFile(const std::string& listfile) *this->Position->ExecutionListFile = listfile; } -std::vector const& -cmState::Directory::GetCurrentSourceComponents() const -{ - return this->DirectoryState->CurrentSourceDirectoryComponents; -} - -std::vector const& -cmState::Directory::GetCurrentBinaryComponents() const -{ - return this->DirectoryState->CurrentBinaryDirectoryComponents; -} - const char* cmState::Directory::GetRelativePathTopSource() const { return this->DirectoryState->RelativePathTopSource.c_str(); diff --git a/Source/cmState.h b/Source/cmState.h index 9ab421389d..0fac42c570 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -135,9 +135,6 @@ public: const char* GetCurrentBinary() const; void SetCurrentBinary(std::string const& dir); - std::vector const& GetCurrentSourceComponents() const; - std::vector const& GetCurrentBinaryComponents() const; - const char* GetRelativePathTopSource() const; const char* GetRelativePathTopBinary() const; void SetRelativePathTopSource(const char* dir); @@ -312,9 +309,6 @@ public: const char* GetBinaryDirectory() const; void SetBinaryDirectory(std::string const& binaryDirectory); - std::vector const& GetSourceDirectoryComponents() const; - std::vector const& GetBinaryDirectoryComponents() const; - void SetWindowsShell(bool windowsShell); bool UseWindowsShell() const; void SetWindowsVSIDE(bool windowsVSIDE); @@ -350,8 +344,6 @@ private: cmLinkedTree SnapshotData; cmLinkedTree VarTree; - std::vector SourceDirectoryComponents; - std::vector BinaryDirectoryComponents; std::string SourceDirectory; std::string BinaryDirectory; bool IsInTryCompile;