diff options
author | Brad King <brad.king@kitware.com> | 2018-12-12 13:19:42 +0000 |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2018-12-12 08:19:49 -0500 |
commit | ed5087e32c517bac158f541fa7ea916c670ec915 (patch) | |
tree | e4c6d0dedc0725afd1c0cd3a0e004ff67fc24db9 /Source | |
parent | c337b2a0b455942eb747d22c0cd71e60db54989d (diff) | |
parent | 33f08eec18b440eac1f24f6f18b971c6203307bd (diff) | |
download | cmake-ed5087e32c517bac158f541fa7ea916c670ec915.tar.gz |
Merge topic 'output-converter-simplify'
33f08eec18 cmOutputConverter: Moved ContainedInDirectory to cmStateDirectory
87e810f223 cmOutputConverter: Moved ForceToRelativePath to cmSystem
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !2665
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmDependsFortran.cxx | 6 | ||||
-rw-r--r-- | Source/cmGlobalXCodeGenerator.cxx | 2 | ||||
-rw-r--r-- | Source/cmLinkLineComputer.cxx | 6 | ||||
-rw-r--r-- | Source/cmLocalUnixMakefileGenerator3.cxx | 5 | ||||
-rw-r--r-- | Source/cmMakefileTargetGenerator.cxx | 5 | ||||
-rw-r--r-- | Source/cmOutputConverter.cxx | 111 | ||||
-rw-r--r-- | Source/cmOutputConverter.h | 13 | ||||
-rw-r--r-- | Source/cmStateDirectory.cxx | 17 | ||||
-rw-r--r-- | Source/cmStateDirectory.h | 3 | ||||
-rw-r--r-- | Source/cmSystemTools.cxx | 75 | ||||
-rw-r--r-- | Source/cmSystemTools.h | 8 |
11 files changed, 117 insertions, 134 deletions
diff --git a/Source/cmDependsFortran.cxx b/Source/cmDependsFortran.cxx index a04cee7902..310af2d5ce 100644 --- a/Source/cmDependsFortran.cxx +++ b/Source/cmDependsFortran.cxx @@ -697,9 +697,9 @@ bool cmDependsFortran::ModulesDiffer(const char* modFile, std::string cmDependsFortran::MaybeConvertToRelativePath( std::string const& base, std::string const& path) { - if (!cmOutputConverter::ContainedInDirectory( - base, path, this->LocalGenerator->GetStateSnapshot().GetDirectory())) { + if (!this->LocalGenerator->GetStateSnapshot().GetDirectory().ContainsBoth( + base, path)) { return path; } - return cmOutputConverter::ForceToRelativePath(base, path); + return cmSystemTools::ForceToRelativePath(base, path); } diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index ac4f0bec60..d6ab76976a 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 7511fd21bb..6643990ac6 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) @@ -46,9 +47,8 @@ std::string cmLinkLineComputer::ConvertToLinkReference( { std::string relLib = lib; - if (cmOutputConverter::ContainedInDirectory( - this->StateDir.GetCurrentBinary(), lib, this->StateDir)) { - relLib = cmOutputConverter::ForceToRelativePath( + if (this->StateDir.ContainsBoth(this->StateDir.GetCurrentBinary(), lib)) { + relLib = cmSystemTools::ForceToRelativePath( this->StateDir.GetCurrentBinary(), lib); } return relLib; diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 11d3608297..707a1b5c9b 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -2093,9 +2093,8 @@ void cmLocalUnixMakefileGenerator3::CreateCDCommand( std::string cmLocalUnixMakefileGenerator3::MaybeConvertToRelativePath( std::string const& base, std::string const& path) { - if (!cmOutputConverter::ContainedInDirectory( - base, path, this->GetStateSnapshot().GetDirectory())) { + if (!this->GetStateSnapshot().GetDirectory().ContainsBoth(base, path)) { return path; } - return cmOutputConverter::ForceToRelativePath(base, path); + return cmSystemTools::ForceToRelativePath(base, path); } diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index a79425e240..cb41c28481 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -1308,11 +1308,10 @@ public: private: std::string MaybeConvertToRelativePath(std::string const& obj) { - if (!cmOutputConverter::ContainedInDirectory( - this->StateDir.GetCurrentBinary(), obj, this->StateDir)) { + if (!this->StateDir.ContainsBoth(this->StateDir.GetCurrentBinary(), obj)) { return obj; } - return cmOutputConverter::ForceToRelativePath( + return cmSystemTools::ForceToRelativePath( this->StateDir.GetCurrentBinary(), obj); } diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index 80e6e9721c..011c7d896b 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -9,7 +9,6 @@ #include <string.h> #include <vector> -#include "cmAlgorithms.h" #include "cmState.h" #include "cmStateDirectory.h" #include "cmSystemTools.h" @@ -73,119 +72,15 @@ std::string cmOutputConverter::ConvertDirectorySeparatorsForShell( return result; } -static bool cmOutputConverterNotAbove(const char* a, const char* b) -{ - return (cmSystemTools::ComparePath(a, b) || - cmSystemTools::IsSubDirectory(a, b)); -} - -bool cmOutputConverter::ContainedInDirectory(std::string const& local_path, - std::string const& remote_path, - cmStateDirectory const& directory) -{ - const std::string& relativePathTopBinary = - directory.GetRelativePathTopBinary(); - const std::string& relativePathTopSource = - directory.GetRelativePathTopSource(); - - const bool bothInBinary = - cmOutputConverterNotAbove(local_path.c_str(), - relativePathTopBinary.c_str()) && - cmOutputConverterNotAbove(remote_path.c_str(), - relativePathTopBinary.c_str()); - - const bool bothInSource = - cmOutputConverterNotAbove(local_path.c_str(), - relativePathTopSource.c_str()) && - cmOutputConverterNotAbove(remote_path.c_str(), - relativePathTopSource.c_str()); - - return bothInSource || bothInBinary; -} - std::string cmOutputConverter::ConvertToRelativePath( std::string const& local_path, std::string const& remote_path) const { - if (!ContainedInDirectory(local_path, remote_path, - this->StateSnapshot.GetDirectory())) { + if (!this->StateSnapshot.GetDirectory().ContainsBoth(local_path, + remote_path)) { 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<std::string> local; - cmSystemTools::SplitPath(local_path, local); - std::vector<std::string> 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 ed7143c435..5a4f879813 100644 --- a/Source/cmOutputConverter.h +++ b/Source/cmOutputConverter.h @@ -10,7 +10,6 @@ #include "cmStateSnapshot.h" class cmState; -class cmStateDirectory; class cmOutputConverter { @@ -92,10 +91,6 @@ public: }; static FortranFormat GetFortranFormat(const char* value); - static bool ContainedInDirectory(std::string const& local_path, - std::string const& remote_path, - cmStateDirectory const& directory); - /** * Convert the given remote path to a relative path with respect to * the given local path. Both paths must use forward slashes and not @@ -106,14 +101,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/cmStateDirectory.cxx b/Source/cmStateDirectory.cxx index f94e7146e3..40f694c0d9 100644 --- a/Source/cmStateDirectory.cxx +++ b/Source/cmStateDirectory.cxx @@ -138,6 +138,23 @@ void cmStateDirectory::SetRelativePathTopBinary(const char* dir) this->DirectoryState->RelativePathTopBinary = dir; } +bool cmStateDirectory::ContainsBoth(std::string const& local_path, + std::string const& remote_path) const +{ + auto PathEqOrSubDir = [](std::string const& a, std::string const& b) { + return (cmSystemTools::ComparePath(a, b) || + cmSystemTools::IsSubDirectory(a, b)); + }; + + bool bothInBinary = PathEqOrSubDir(local_path, GetRelativePathTopBinary()) && + PathEqOrSubDir(remote_path, GetRelativePathTopBinary()); + + bool bothInSource = PathEqOrSubDir(local_path, GetRelativePathTopSource()) && + PathEqOrSubDir(remote_path, GetRelativePathTopSource()); + + return bothInBinary || bothInSource; +} + cmStateDirectory::cmStateDirectory( cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator iter, const cmStateSnapshot& snapshot) diff --git a/Source/cmStateDirectory.h b/Source/cmStateDirectory.h index e5f4d05d16..c4b18adf16 100644 --- a/Source/cmStateDirectory.h +++ b/Source/cmStateDirectory.h @@ -32,6 +32,9 @@ public: void SetRelativePathTopSource(const char* dir); void SetRelativePathTopBinary(const char* dir); + bool ContainsBoth(std::string const& local_path, + std::string const& remote_path) const; + cmStringRange GetIncludeDirectoriesEntries() const; cmBacktraceRange GetIncludeDirectoriesEntryBacktraces() const; void AppendIncludeDirectoriesEntry(std::string const& vec, diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 6fbe48212c..be65853eda 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -49,6 +49,7 @@ #include <string.h> #include <time.h> #include <utility> +#include <vector> #if defined(_WIN32) # include <windows.h> @@ -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<std::string> local; + cmSystemTools::SplitPath(local_path, local); + std::vector<std::string> 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 832c1cab5c..c0999e720d 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" */ |