KWSys 2019-04-17 (c42b92f0)

Code extracted from:

    https://gitlab.kitware.com/utils/kwsys.git

at commit c42b92f0fe01cf588aacc1984501b4fdcc58994e (master).

Upstream Shortlog
-----------------

Ben Boeckel (1):
      84bd7c9e SystemTools: support hiding the translation map

Sebastian Holtermann (8):
      00a3a436 SystemTools: Update buffered env value string only on a change
      d6235796 SystemTools: Optimize iterator lifetime in JoinPath
      9992f69b SystemTools: Optimize range loop in FindName method
      8c9ca162 SystemTools: Optimize range loop in FindProgram method
      c7e3ab12 SystemTools: Optimize range loop in FindProgram method
      ac8d2033 SystemTools: Optimize range loop in FindLibrary method
      815cc593 SystemTools: Optimize range loop in FindProgramPath method
      5d8af0d6 SystemTools: Optimize range loop in RelativePath method
This commit is contained in:
KWSys Upstream
2019-04-17 09:49:30 -04:00
committed by Brad King
parent 8166634958
commit 0225b05afe
4 changed files with 76 additions and 88 deletions

View File

@@ -465,6 +465,14 @@ IF(KWSYS_USE_DynamicLoader)
ENDIF() ENDIF()
IF(KWSYS_USE_SystemTools) IF(KWSYS_USE_SystemTools)
if (NOT DEFINED KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP)
set(KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP 1)
endif ()
if (KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP)
set(KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP 1)
else ()
set(KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP 0)
endif ()
KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_SETENV KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_SETENV
"Checking whether CXX compiler has setenv" DIRECT) "Checking whether CXX compiler has setenv" DIRECT)
KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_UNSETENV KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_UNSETENV

View File

@@ -11,6 +11,9 @@
/* Whether <ext/stdio_filebuf.h> is available. */ /* Whether <ext/stdio_filebuf.h> is available. */
#define @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H \ #define @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H \
@KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H@ @KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H@
/* Whether the translation map is available or not. */
#define @KWSYS_NAMESPACE@_SYSTEMTOOLS_USE_TRANSLATION_MAP \
@KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP@
#if defined(__SUNPRO_CC) && __SUNPRO_CC > 0x5130 && defined(__has_attribute) #if defined(__SUNPRO_CC) && __SUNPRO_CC > 0x5130 && defined(__has_attribute)
# define @KWSYS_NAMESPACE@__has_cpp_attribute(x) __has_attribute(x) # define @KWSYS_NAMESPACE@__has_cpp_attribute(x) __has_attribute(x)
@@ -56,6 +59,8 @@
@KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
# define KWSYS_FALLTHROUGH @KWSYS_NAMESPACE@_FALLTHROUGH # define KWSYS_FALLTHROUGH @KWSYS_NAMESPACE@_FALLTHROUGH
# define KWSYS_NULLPTR @KWSYS_NAMESPACE@_NULLPTR # define KWSYS_NULLPTR @KWSYS_NAMESPACE@_NULLPTR
# define KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP \
@KWSYS_NAMESPACE@_SYSTEMTOOLS_USE_TRANSLATION_MAP
#endif #endif
#endif #endif

View File

@@ -453,11 +453,13 @@ class SystemToolsStatic
{ {
public: public:
typedef std::map<std::string, std::string> StringMap; typedef std::map<std::string, std::string> StringMap;
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP
/** /**
* Path translation table from dir to refdir * Path translation table from dir to refdir
* Each time 'dir' will be found it will be replace by 'refdir' * Each time 'dir' will be found it will be replace by 'refdir'
*/ */
StringMap TranslationMap; StringMap TranslationMap;
#endif
#ifdef _WIN32 #ifdef _WIN32
static std::string GetCasePathName(std::string const& pathIn); static std::string GetCasePathName(std::string const& pathIn);
static std::string GetActualCaseForPathCached(std::string const& path); static std::string GetActualCaseForPathCached(std::string const& path);
@@ -623,7 +625,9 @@ const char* SystemToolsStatic::GetEnvBuffered(const char* key)
std::string env; std::string env;
if (SystemTools::GetEnv(key, env)) { if (SystemTools::GetEnv(key, env)) {
std::string& menv = SystemTools::Statics->EnvMap[key]; std::string& menv = SystemTools::Statics->EnvMap[key];
menv = std::move(env); if (menv != env) {
menv = std::move(env);
}
return menv.c_str(); return menv.c_str();
} }
return KWSYS_NULLPTR; return KWSYS_NULLPTR;
@@ -2810,27 +2814,15 @@ std::string SystemToolsStatic::FindName(
SystemTools::GetPath(path); SystemTools::GetPath(path);
} }
// now add the additional paths // now add the additional paths
{ path.reserve(path.size() + userPaths.size());
for (std::vector<std::string>::const_iterator i = userPaths.begin(); path.insert(path.end(), userPaths.begin(), userPaths.end());
i != userPaths.end(); ++i) {
path.push_back(*i);
}
}
// Add a trailing slash to all paths to aid the search process.
{
for (std::vector<std::string>::iterator i = path.begin(); i != path.end();
++i) {
std::string& p = *i;
if (p.empty() || p.back() != '/') {
p += "/";
}
}
}
// now look for the file // now look for the file
std::string tryPath; std::string tryPath;
for (std::vector<std::string>::const_iterator p = path.begin(); for (std::string const& p : path) {
p != path.end(); ++p) { tryPath = p;
tryPath = *p; if (tryPath.empty() || tryPath.back() != '/') {
tryPath += '/';
}
tryPath += name; tryPath += name;
if (SystemTools::FileExists(tryPath)) { if (SystemTools::FileExists(tryPath)) {
return tryPath; return tryPath;
@@ -2904,14 +2896,13 @@ std::string SystemTools::FindProgram(const std::string& name,
// the end of it // the end of it
// on windows try .com then .exe // on windows try .com then .exe
if (name.size() <= 3 || name[name.size() - 4] != '.') { if (name.size() <= 3 || name[name.size() - 4] != '.') {
extensions.push_back(".com"); extensions.emplace_back(".com");
extensions.push_back(".exe"); extensions.emplace_back(".exe");
// first try with extensions if the os supports them // first try with extensions if the os supports them
for (std::vector<std::string>::iterator i = extensions.begin(); for (std::string const& ext : extensions) {
i != extensions.end(); ++i) {
tryPath = name; tryPath = name;
tryPath += *i; tryPath += ext;
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
@@ -2930,43 +2921,33 @@ std::string SystemTools::FindProgram(const std::string& name,
SystemTools::GetPath(path); SystemTools::GetPath(path);
} }
// now add the additional paths // now add the additional paths
{ path.reserve(path.size() + userPaths.size());
for (std::vector<std::string>::const_iterator i = userPaths.begin(); path.insert(path.end(), userPaths.begin(), userPaths.end());
i != userPaths.end(); ++i) {
path.push_back(*i);
}
}
// Add a trailing slash to all paths to aid the search process. // Add a trailing slash to all paths to aid the search process.
{ for (std::string& p : path) {
for (std::vector<std::string>::iterator i = path.begin(); i != path.end(); if (p.empty() || p.back() != '/') {
++i) { p += '/';
std::string& p = *i;
if (p.empty() || p.back() != '/') {
p += "/";
}
} }
} }
// Try each path // Try each path
for (std::vector<std::string>::iterator p = path.begin(); p != path.end(); for (std::string& p : path) {
++p) {
#ifdef _WIN32 #ifdef _WIN32
// Remove double quotes from the path on windows // Remove double quotes from the path on windows
SystemTools::ReplaceString(*p, "\"", ""); SystemTools::ReplaceString(p, "\"", "");
#endif #endif
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
// first try with extensions // first try with extensions
for (std::vector<std::string>::iterator ext = extensions.begin(); for (std::string const& ext : extensions) {
ext != extensions.end(); ++ext) { tryPath = p;
tryPath = *p;
tryPath += name; tryPath += name;
tryPath += *ext; tryPath += ext;
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
} }
#endif #endif
// now try it without them // now try it without them
tryPath = *p; tryPath = p;
tryPath += name; tryPath += name;
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
@@ -2980,10 +2961,9 @@ std::string SystemTools::FindProgram(const std::vector<std::string>& names,
const std::vector<std::string>& path, const std::vector<std::string>& path,
bool noSystemPath) bool noSystemPath)
{ {
for (std::vector<std::string>::const_iterator it = names.begin(); for (std::string const& name : names) {
it != names.end(); ++it) {
// Try to find the program. // Try to find the program.
std::string result = SystemTools::FindProgram(*it, path, noSystemPath); std::string result = SystemTools::FindProgram(name, path, noSystemPath);
if (!result.empty()) { if (!result.empty()) {
return result; return result;
} }
@@ -3008,27 +2988,18 @@ std::string SystemTools::FindLibrary(const std::string& name,
std::vector<std::string> path; std::vector<std::string> path;
SystemTools::GetPath(path); SystemTools::GetPath(path);
// now add the additional paths // now add the additional paths
{ path.reserve(path.size() + userPaths.size());
for (std::vector<std::string>::const_iterator i = userPaths.begin(); path.insert(path.end(), userPaths.begin(), userPaths.end());
i != userPaths.end(); ++i) {
path.push_back(*i);
}
}
// Add a trailing slash to all paths to aid the search process. // Add a trailing slash to all paths to aid the search process.
{ for (std::string& p : path) {
for (std::vector<std::string>::iterator i = path.begin(); i != path.end(); if (p.empty() || p.back() != '/') {
++i) { p += '/';
std::string& p = *i;
if (p.empty() || p.back() != '/') {
p += "/";
}
} }
} }
std::string tryPath; std::string tryPath;
for (std::vector<std::string>::const_iterator p = path.begin(); for (std::string const& p : path) {
p != path.end(); ++p) {
#if defined(__APPLE__) #if defined(__APPLE__)
tryPath = *p; tryPath = p;
tryPath += name; tryPath += name;
tryPath += ".framework"; tryPath += ".framework";
if (SystemTools::FileIsDirectory(tryPath)) { if (SystemTools::FileIsDirectory(tryPath)) {
@@ -3036,42 +3007,42 @@ std::string SystemTools::FindLibrary(const std::string& name,
} }
#endif #endif
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__) #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
tryPath = *p; tryPath = p;
tryPath += name; tryPath += name;
tryPath += ".lib"; tryPath += ".lib";
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
#else #else
tryPath = *p; tryPath = p;
tryPath += "lib"; tryPath += "lib";
tryPath += name; tryPath += name;
tryPath += ".so"; tryPath += ".so";
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
tryPath = *p; tryPath = p;
tryPath += "lib"; tryPath += "lib";
tryPath += name; tryPath += name;
tryPath += ".a"; tryPath += ".a";
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
tryPath = *p; tryPath = p;
tryPath += "lib"; tryPath += "lib";
tryPath += name; tryPath += name;
tryPath += ".sl"; tryPath += ".sl";
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
tryPath = *p; tryPath = p;
tryPath += "lib"; tryPath += "lib";
tryPath += name; tryPath += name;
tryPath += ".dylib"; tryPath += ".dylib";
if (SystemTools::FileExists(tryPath, true)) { if (SystemTools::FileExists(tryPath, true)) {
return SystemTools::CollapseFullPath(tryPath); return SystemTools::CollapseFullPath(tryPath);
} }
tryPath = *p; tryPath = p;
tryPath += "lib"; tryPath += "lib";
tryPath += name; tryPath += name;
tryPath += ".dll"; tryPath += ".dll";
@@ -3333,9 +3304,8 @@ bool SystemTools::FindProgramPath(const char* argv0, std::string& pathOut,
msg << " argv[0] = \"" << argv0 << "\"\n"; msg << " argv[0] = \"" << argv0 << "\"\n";
} }
msg << " Attempted paths:\n"; msg << " Attempted paths:\n";
std::vector<std::string>::iterator i; for (std::string const& ff : failures) {
for (i = failures.begin(); i != failures.end(); ++i) { msg << " \"" << ff << "\"\n";
msg << " \"" << *i << "\"\n";
} }
errorMsg = msg.str(); errorMsg = msg.str();
return false; return false;
@@ -3349,6 +3319,7 @@ std::string SystemTools::CollapseFullPath(const std::string& in_relative)
return SystemTools::CollapseFullPath(in_relative, KWSYS_NULLPTR); return SystemTools::CollapseFullPath(in_relative, KWSYS_NULLPTR);
} }
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP
void SystemTools::AddTranslationPath(const std::string& a, void SystemTools::AddTranslationPath(const std::string& a,
const std::string& b) const std::string& b)
{ {
@@ -3412,6 +3383,7 @@ void SystemTools::CheckTranslationPath(std::string& path)
// Remove the trailing slash we added before. // Remove the trailing slash we added before.
path.pop_back(); path.pop_back();
} }
#endif
static void SystemToolsAppendComponents( static void SystemToolsAppendComponents(
std::vector<std::string>& out_components, std::vector<std::string>& out_components,
@@ -3482,6 +3454,7 @@ std::string SystemTools::CollapseFullPath(const std::string& in_path,
// Transform the path back to a string. // Transform the path back to a string.
std::string newPath = SystemTools::JoinPath(out_components); std::string newPath = SystemTools::JoinPath(out_components);
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP
// Update the translation table with this potentially new path. I am not // Update the translation table with this potentially new path. I am not
// sure why this line is here, it seems really questionable, but yet I // sure why this line is here, it seems really questionable, but yet I
// would put good money that if I remove it something will break, basically // would put good money that if I remove it something will break, basically
@@ -3497,6 +3470,7 @@ std::string SystemTools::CollapseFullPath(const std::string& in_path,
// SystemTools::AddTranslationPath(newPath, in_path); // SystemTools::AddTranslationPath(newPath, in_path);
SystemTools::CheckTranslationPath(newPath); SystemTools::CheckTranslationPath(newPath);
#endif
#ifdef _WIN32 #ifdef _WIN32
newPath = SystemTools::Statics->GetActualCaseForPathCached(newPath); newPath = SystemTools::Statics->GetActualCaseForPathCached(newPath);
SystemTools::ConvertToUnixSlashes(newPath); SystemTools::ConvertToUnixSlashes(newPath);
@@ -3558,28 +3532,26 @@ std::string SystemTools::RelativePath(const std::string& local,
// for each entry that is not common in the local path // for each entry that is not common in the local path
// add a ../ to the finalpath array, this gets us out of the local // add a ../ to the finalpath array, this gets us out of the local
// path into the remote dir // path into the remote dir
for (unsigned int i = 0; i < localSplit.size(); ++i) { for (std::string const& lp : localSplit) {
if (!localSplit[i].empty()) { if (!lp.empty()) {
finalPath.push_back("../"); finalPath.emplace_back("../");
} }
} }
// for each entry that is not common in the remote path add it // for each entry that is not common in the remote path add it
// to the final path. // to the final path.
for (std::vector<std::string>::iterator vit = remoteSplit.begin(); for (std::string const& rp : remoteSplit) {
vit != remoteSplit.end(); ++vit) { if (!rp.empty()) {
if (!vit->empty()) { finalPath.push_back(rp);
finalPath.push_back(*vit);
} }
} }
std::string relativePath; // result string std::string relativePath; // result string
// now turn the array of directories into a unix path by puttint / // now turn the array of directories into a unix path by puttint /
// between each entry that does not already have one // between each entry that does not already have one
for (std::vector<std::string>::iterator vit1 = finalPath.begin(); for (std::string const& fp : finalPath) {
vit1 != finalPath.end(); ++vit1) {
if (!relativePath.empty() && relativePath.back() != '/') { if (!relativePath.empty() && relativePath.back() != '/') {
relativePath += "/"; relativePath += '/';
} }
relativePath += *vit1; relativePath += fp;
} }
return relativePath; return relativePath;
} }
@@ -3727,8 +3699,7 @@ std::string SystemTools::JoinPath(
// Construct result in a single string. // Construct result in a single string.
std::string result; std::string result;
size_t len = 0; size_t len = 0;
std::vector<std::string>::const_iterator i; for (std::vector<std::string>::const_iterator i = first; i != last; ++i) {
for (i = first; i != last; ++i) {
len += 1 + i->size(); len += 1 + i->size();
} }
result.reserve(len); result.reserve(len);
@@ -4737,10 +4708,11 @@ void SystemTools::ClassInitialize()
// Create statics singleton instance // Create statics singleton instance
SystemTools::Statics = new SystemToolsStatic; SystemTools::Statics = new SystemToolsStatic;
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP
// Add some special translation paths for unix. These are not added // Add some special translation paths for unix. These are not added
// for windows because drive letters need to be maintained. Also, // for windows because drive letters need to be maintained. Also,
// there are not sym-links and mount points on windows anyway. // there are not sym-links and mount points on windows anyway.
#if !defined(_WIN32) || defined(__CYGWIN__) # if !defined(_WIN32) || defined(__CYGWIN__)
// The tmp path is frequently a logical path so always keep it: // The tmp path is frequently a logical path so always keep it:
SystemTools::AddKeepPath("/tmp/"); SystemTools::AddKeepPath("/tmp/");
@@ -4778,6 +4750,7 @@ void SystemTools::ClassInitialize()
} }
} }
} }
# endif
#endif #endif
} }

View File

@@ -891,6 +891,7 @@ public:
*/ */
static int GetTerminalWidth(); static int GetTerminalWidth();
#if @KWSYS_NAMESPACE@_SYSTEMTOOLS_USE_TRANSLATION_MAP
/** /**
* Add an entry in the path translation table. * Add an entry in the path translation table.
*/ */
@@ -907,6 +908,7 @@ public:
* Update path by going through the Path Translation table; * Update path by going through the Path Translation table;
*/ */
static void CheckTranslationPath(std::string& path); static void CheckTranslationPath(std::string& path);
#endif
/** /**
* Delay the execution for a specified amount of time specified * Delay the execution for a specified amount of time specified