From 87e810f223bad6fb889e7ae4ad08be98461bf6e2 Mon Sep 17 00:00:00 2001 From: Bruno Manganelli Date: Fri, 23 Nov 2018 01:38:29 +0000 Subject: cmOutputConverter: Moved ForceToRelativePath to cmSystem --- Source/cmDependsFortran.cxx | 2 +- Source/cmGlobalXCodeGenerator.cxx | 2 +- Source/cmLinkLineComputer.cxx | 3 +- Source/cmLocalUnixMakefileGenerator3.cxx | 2 +- Source/cmMakefileTargetGenerator.cxx | 2 +- Source/cmOutputConverter.cxx | 76 +------------------------------- Source/cmOutputConverter.h | 8 ---- Source/cmSystemTools.cxx | 75 +++++++++++++++++++++++++++++++ Source/cmSystemTools.h | 8 ++++ 9 files changed, 90 insertions(+), 88 deletions(-) diff --git a/Source/cmDependsFortran.cxx b/Source/cmDependsFortran.cxx index a04cee7..d218b60 100644 --- a/Source/cmDependsFortran.cxx +++ b/Source/cmDependsFortran.cxx @@ -701,5 +701,5 @@ std::string cmDependsFortran::MaybeConvertToRelativePath( base, path, this->LocalGenerator->GetStateSnapshot().GetDirectory())) { return path; } - return cmOutputConverter::ForceToRelativePath(base, path); + return cmSystemTools::ForceToRelativePath(base, path); } diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index ac4f0be..d6ab769 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -3563,7 +3563,7 @@ std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p) { // We force conversion because Xcode breakpoints do not work unless // they are in a file named relative to the source tree. - return cmOutputConverter::ForceToRelativePath( + return cmSystemTools::ForceToRelativePath( cmSystemTools::JoinPath(this->ProjectSourceDirectoryComponents), p); } diff --git a/Source/cmLinkLineComputer.cxx b/Source/cmLinkLineComputer.cxx index 7511fd2..d756564 100644 --- a/Source/cmLinkLineComputer.cxx +++ b/Source/cmLinkLineComputer.cxx @@ -11,6 +11,7 @@ #include "cmOutputConverter.h" #include "cmStateDirectory.h" #include "cmStateTypes.h" +#include "cmSystemTools.h" cmLinkLineComputer::cmLinkLineComputer(cmOutputConverter* outputConverter, cmStateDirectory const& stateDir) @@ -48,7 +49,7 @@ std::string cmLinkLineComputer::ConvertToLinkReference( if (cmOutputConverter::ContainedInDirectory( this->StateDir.GetCurrentBinary(), lib, this->StateDir)) { - relLib = cmOutputConverter::ForceToRelativePath( + relLib = cmSystemTools::ForceToRelativePath( this->StateDir.GetCurrentBinary(), lib); } return relLib; diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 11d3608..ee38cfb 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -2097,5 +2097,5 @@ std::string cmLocalUnixMakefileGenerator3::MaybeConvertToRelativePath( base, path, this->GetStateSnapshot().GetDirectory())) { return path; } - return cmOutputConverter::ForceToRelativePath(base, path); + return cmSystemTools::ForceToRelativePath(base, path); } diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index a79425e..e6f9830 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -1312,7 +1312,7 @@ private: this->StateDir.GetCurrentBinary(), obj, this->StateDir)) { return obj; } - return cmOutputConverter::ForceToRelativePath( + return cmSystemTools::ForceToRelativePath( this->StateDir.GetCurrentBinary(), obj); } diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index 80e6e97..58d31b9 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -111,81 +111,7 @@ std::string cmOutputConverter::ConvertToRelativePath( return remote_path; } - return cmOutputConverter::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.front() != '\"'); - assert(remote_path.front() != '\"'); - - // The local path should never have a trailing slash. - assert(local_path.empty() || local_path.back() != '/'); - - // If the path is already relative then just return the path. - if (!cmSystemTools::FileIsFullPath(remote_path)) { - 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(remote_path, remote); - unsigned int common = 0; - while (common < remote.size() && common < local.size() && - cmSystemTools::ComparePath(remote[common], local[common])) { - ++common; - } - - // If no part of the path is in common then return the full path. - if (common == 0) { - return remote_path; - } - - // If the entire path is in common then just return a ".". - if (common == remote.size() && common == local.size()) { - return "."; - } - - // If the entire path is in common except for a trailing slash then - // just return a "./". - if (common + 1 == remote.size() && remote[common].empty() && - common == local.size()) { - return "./"; - } - - // Construct the relative path. - std::string relative; - - // First add enough ../ to get up to the level of the shared portion - // of the path. Leave off the trailing slash. Note that the last - // component of local will never be empty because local should never - // have a trailing slash. - for (unsigned int i = common; i < local.size(); ++i) { - relative += ".."; - if (i < local.size() - 1) { - relative += "/"; - } - } - - // Now add the portion of the destination path that is not included - // in the shared portion of the path. Add a slash the first time - // only if there was already something in the path. If there was a - // trailing slash in the input then the last iteration of the loop - // will add a slash followed by an empty string which will preserve - // the trailing slash in the output. - - if (!relative.empty() && !remote.empty()) { - relative += "/"; - } - relative += cmJoin(cmMakeRange(remote).advance(common), "/"); - - // Finally return the path. - return relative; + return cmSystemTools::ForceToRelativePath(local_path, remote_path); } static bool cmOutputConverterIsShellOperator(const std::string& str) diff --git a/Source/cmOutputConverter.h b/Source/cmOutputConverter.h index ed7143c..38102e8 100644 --- a/Source/cmOutputConverter.h +++ b/Source/cmOutputConverter.h @@ -106,14 +106,6 @@ public: 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; diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 6fbe482..be65853 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -49,6 +49,7 @@ #include #include #include +#include #if defined(_WIN32) # include @@ -1463,6 +1464,80 @@ std::string cmSystemTools::RelativePath(std::string const& local, return cmsys::SystemTools::RelativePath(local, remote); } +std::string cmSystemTools::ForceToRelativePath(std::string const& local_path, + std::string const& remote_path) +{ + // The paths should never be quoted. + assert(local_path.front() != '\"'); + assert(remote_path.front() != '\"'); + + // The local path should never have a trailing slash. + assert(local_path.empty() || local_path.back() != '/'); + + // If the path is already relative then just return the path. + if (!cmSystemTools::FileIsFullPath(remote_path)) { + 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(remote_path, remote); + unsigned int common = 0; + while (common < remote.size() && common < local.size() && + cmSystemTools::ComparePath(remote[common], local[common])) { + ++common; + } + + // If no part of the path is in common then return the full path. + if (common == 0) { + return remote_path; + } + + // If the entire path is in common then just return a ".". + if (common == remote.size() && common == local.size()) { + return "."; + } + + // If the entire path is in common except for a trailing slash then + // just return a "./". + if (common + 1 == remote.size() && remote[common].empty() && + common == local.size()) { + return "./"; + } + + // Construct the relative path. + std::string relative; + + // First add enough ../ to get up to the level of the shared portion + // of the path. Leave off the trailing slash. Note that the last + // component of local will never be empty because local should never + // have a trailing slash. + for (unsigned int i = common; i < local.size(); ++i) { + relative += ".."; + if (i < local.size() - 1) { + relative += "/"; + } + } + + // Now add the portion of the destination path that is not included + // in the shared portion of the path. Add a slash the first time + // only if there was already something in the path. If there was a + // trailing slash in the input then the last iteration of the loop + // will add a slash followed by an empty string which will preserve + // the trailing slash in the output. + + if (!relative.empty() && !remote.empty()) { + relative += "/"; + } + relative += cmJoin(cmMakeRange(remote).advance(common), "/"); + + // Finally return the path. + return relative; +} + std::string cmSystemTools::CollapseCombinedPath(std::string const& dir, std::string const& file) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 832c1ca..c0999e7 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -375,6 +375,14 @@ public: static std::string RelativePath(std::string const& local, std::string const& remote); + /** + * 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); + /** Joins two paths while collapsing x/../ parts * For example CollapseCombinedPath("a/b/c", "../../d") results in "a/d" */ -- cgit v0.12