summaryrefslogtreecommitdiff
path: root/src/libs/utils/environment.cpp
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@nokia.com>2012-08-23 15:53:58 +0200
committerhjk <qthjk@ovi.com>2012-08-27 17:00:03 +0200
commite669f054069c3a1214a31c4c1d7d9269659c31c4 (patch)
treed611e865215dc8ee705a640fdf3e35660542632f /src/libs/utils/environment.cpp
parentb674b59b3d652fa9c5d74dc4e16380e3a5d23882 (diff)
downloadqt-creator-e669f054069c3a1214a31c4c1d7d9269659c31c4.tar.gz
Utils: Introduce HostOsInfo class.
The class' member functions are intended to be used instead of the Q_OS_* macros in all contexts where the latter are not syntactically required. This lowers the likelihood of changes made on one platform breaking the build on another, e.g. due to the code model missing symbols in #ifdef'ed out code when refactoring. Change-Id: I4a54788591b4c8f8d589b8368a6c683d4155c9fa Reviewed-by: hjk <qthjk@ovi.com>
Diffstat (limited to 'src/libs/utils/environment.cpp')
-rw-r--r--src/libs/utils/environment.cpp241
1 files changed, 110 insertions, 131 deletions
diff --git a/src/libs/utils/environment.cpp b/src/libs/utils/environment.cpp
index 97650cb2bb..03eb5c72a7 100644
--- a/src/libs/utils/environment.cpp
+++ b/src/libs/utils/environment.cpp
@@ -30,6 +30,8 @@
#include "environment.h"
+#include "hostosinfo.h"
+
#include <QDir>
#include <QProcess>
#include <QString>
@@ -41,18 +43,18 @@ public:
SystemEnvironment()
: Environment(QProcess::systemEnvironment())
{
-#ifdef Q_OS_LINUX
- QString ldLibraryPath = value(QLatin1String("LD_LIBRARY_PATH"));
- QDir lib(QCoreApplication::applicationDirPath());
- lib.cd("../lib");
- QString toReplace = lib.path();
- lib.cd("qtcreator");
- toReplace.append(QLatin1String(":"));
- toReplace.append(lib.path());
-
- if (ldLibraryPath.startsWith(toReplace))
- set(QLatin1String("LD_LIBRARY_PATH"), ldLibraryPath.remove(0, toReplace.length()));
-#endif
+ if (Utils::HostOsInfo::isLinuxHost()) {
+ QString ldLibraryPath = value(QLatin1String("LD_LIBRARY_PATH"));
+ QDir lib(QCoreApplication::applicationDirPath());
+ lib.cd("../lib");
+ QString toReplace = lib.path();
+ lib.cd("qtcreator");
+ toReplace.append(QLatin1String(":"));
+ toReplace.append(lib.path());
+
+ if (ldLibraryPath.startsWith(toReplace))
+ set(QLatin1String("LD_LIBRARY_PATH"), ldLibraryPath.remove(0, toReplace.length()));
+ }
}
};
@@ -60,6 +62,11 @@ Q_GLOBAL_STATIC(SystemEnvironment, staticSystemEnvironment)
namespace Utils {
+static QChar varSeparator()
+{
+ return HostOsInfo::isWindowsHost() ? QLatin1Char(';') : QLatin1Char(':');
+}
+
static bool sortEnvironmentItem(const EnvironmentItem &a, const EnvironmentItem &b)
{
return a.name < b.name;
@@ -104,11 +111,10 @@ Environment::Environment(const QStringList &env)
foreach (const QString &s, env) {
int i = s.indexOf(QLatin1Char('='));
if (i >= 0) {
-#ifdef Q_OS_WIN
- m_values.insert(s.left(i).toUpper(), s.mid(i+1));
-#else
- m_values.insert(s.left(i), s.mid(i+1));
-#endif
+ if (HostOsInfo::isWindowsHost())
+ m_values.insert(s.left(i).toUpper(), s.mid(i+1));
+ else
+ m_values.insert(s.left(i), s.mid(i+1));
}
}
}
@@ -128,32 +134,18 @@ QStringList Environment::toStringList() const
void Environment::set(const QString &key, const QString &value)
{
-#ifdef Q_OS_WIN
- QString _key = key.toUpper();
-#else
- const QString &_key = key;
-#endif
- m_values.insert(_key, value);
+ m_values.insert(HostOsInfo::isWindowsHost() ? key.toUpper() : key, value);
}
void Environment::unset(const QString &key)
{
-#ifdef Q_OS_WIN
- QString _key = key.toUpper();
-#else
- const QString &_key = key;
-#endif
- m_values.remove(_key);
+ m_values.remove(HostOsInfo::isWindowsHost() ? key.toUpper() : key);
}
void Environment::appendOrSet(const QString &key, const QString &value, const QString &sep)
{
-#ifdef Q_OS_WIN
- QString _key = key.toUpper();
-#else
- const QString &_key = key;
-#endif
- QMap<QString, QString>::iterator it = m_values.find(key);
+ const QString &_key = HostOsInfo::isWindowsHost() ? key.toUpper() : key;
+ QMap<QString, QString>::iterator it = m_values.find(_key);
if (it == m_values.end()) {
m_values.insert(_key, value);
} else {
@@ -166,12 +158,8 @@ void Environment::appendOrSet(const QString &key, const QString &value, const QS
void Environment::prependOrSet(const QString&key, const QString &value, const QString &sep)
{
-#ifdef Q_OS_WIN
- QString _key = key.toUpper();
-#else
- const QString &_key = key;
-#endif
- QMap<QString, QString>::iterator it = m_values.find(key);
+ const QString &_key = HostOsInfo::isWindowsHost() ? key.toUpper() : key;
+ QMap<QString, QString>::iterator it = m_values.find(_key);
if (it == m_values.end()) {
m_values.insert(_key, value);
} else {
@@ -184,38 +172,33 @@ void Environment::prependOrSet(const QString&key, const QString &value, const QS
void Environment::appendOrSetPath(const QString &value)
{
-#ifdef Q_OS_WIN
- const QChar sep = QLatin1Char(';');
-#else
- const QChar sep = QLatin1Char(':');
-#endif
- appendOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(sep));
+ appendOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(varSeparator()));
}
void Environment::prependOrSetPath(const QString &value)
{
-#ifdef Q_OS_WIN
- const QChar sep = QLatin1Char(';');
-#else
- const QChar sep = QLatin1Char(':');
-#endif
- prependOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(sep));
+ prependOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value), QString(varSeparator()));
}
void Environment::prependOrSetLibrarySearchPath(const QString &value)
{
-#ifdef Q_OS_MAC
- Q_UNUSED(value);
- // we could set DYLD_LIBRARY_PATH on Mac but it is unnecessary in practice
-#elif defined(Q_OS_WIN)
- const QChar sep = QLatin1Char(';');
- const QLatin1String path("PATH");
- prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
-#elif defined(Q_OS_UNIX)
- const QChar sep = QLatin1Char(':');
- const QLatin1String path("LD_LIBRARY_PATH");
- prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
-#endif
+ switch (HostOsInfo::hostOs()) {
+ case HostOsInfo::HostOsWindows: {
+ const QChar sep = QLatin1Char(';');
+ const QLatin1String path("PATH");
+ prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
+ break;
+ }
+ case HostOsInfo::HostOsLinux:
+ case HostOsInfo::HostOsOtherUnix: {
+ const QChar sep = QLatin1Char(':');
+ const QLatin1String path("LD_LIBRARY_PATH");
+ prependOrSet(path, QDir::toNativeSeparators(value), QString(sep));
+ break;
+ }
+ default: // we could set DYLD_LIBRARY_PATH on Mac but it is unnecessary in practice
+ break;
+ }
}
Environment Environment::systemEnvironment()
@@ -233,14 +216,15 @@ QString Environment::searchInPath(const QString &executable,
{
QStringList execs;
execs << executable;
-#ifdef Q_OS_WIN
- // Check all the executable extensions on windows:
- QStringList extensions = value(QLatin1String("PATHEXT")).split(QLatin1Char(';'));
-
- // .exe.bat is legal (and run when starting new.exe), so always go through the complete list once:
- foreach (const QString &ext, extensions)
- execs << executable + ext.toLower();
-#endif
+ if (HostOsInfo::isWindowsHost()) {
+ // Check all the executable extensions on windows:
+ QStringList extensions = value(QLatin1String("PATHEXT")).split(QLatin1Char(';'));
+
+ // .exe.bat is legal (and run when starting new.exe), so always go through the
+ // complete list once:
+ foreach (const QString &ext, extensions)
+ execs << executable + ext.toLower();
+ }
return searchInPath(execs, additionalDirs);
}
@@ -287,12 +271,7 @@ QString Environment::searchInPath(const QStringList &executables,
QStringList Environment::path() const
{
-#ifdef Q_OS_WIN
- const QChar sep = QLatin1Char(';');
-#else
- const QChar sep = QLatin1Char(':');
-#endif
- return m_values.value(QLatin1String("PATH")).split(sep, QString::SkipEmptyParts);
+ return m_values.value(QLatin1String("PATH")).split(varSeparator(), QString::SkipEmptyParts);
}
QString Environment::value(const QString &key) const
@@ -425,68 +404,68 @@ QString Environment::expandVariables(const QString &input) const
{
QString result = input;
-#ifdef Q_OS_WIN
- for (int vStart = -1, i = 0; i < result.length(); ) {
- if (result.at(i++) == QLatin1Char('%')) {
- if (vStart > 0) {
- const_iterator it = m_values.constFind(result.mid(vStart, i - vStart - 1).toUpper());
- if (it != m_values.constEnd()) {
- result.replace(vStart - 1, i - vStart + 1, *it);
- i = vStart - 1 + it->length();
- vStart = -1;
+ if (HostOsInfo::isWindowsHost()) {
+ for (int vStart = -1, i = 0; i < result.length(); ) {
+ if (result.at(i++) == QLatin1Char('%')) {
+ if (vStart > 0) {
+ const_iterator it = m_values.constFind(result.mid(vStart, i - vStart - 1).toUpper());
+ if (it != m_values.constEnd()) {
+ result.replace(vStart - 1, i - vStart + 1, *it);
+ i = vStart - 1 + it->length();
+ vStart = -1;
+ } else {
+ vStart = i;
+ }
} else {
vStart = i;
}
- } else {
- vStart = i;
}
}
- }
-#else
- enum { BASE, OPTIONALVARIABLEBRACE, VARIABLE, BRACEDVARIABLE } state = BASE;
- int vStart = -1;
-
- for (int i = 0; i < result.length();) {
- QChar c = result.at(i++);
- if (state == BASE) {
- if (c == QLatin1Char('$'))
- state = OPTIONALVARIABLEBRACE;
- } else if (state == OPTIONALVARIABLEBRACE) {
- if (c == QLatin1Char('{')) {
- state = BRACEDVARIABLE;
- vStart = i;
- } else if (c.isLetterOrNumber() || c == QLatin1Char('_')) {
- state = VARIABLE;
- vStart = i - 1;
- } else {
- state = BASE;
- }
- } else if (state == BRACEDVARIABLE) {
- if (c == QLatin1Char('}')) {
- const_iterator it = m_values.constFind(result.mid(vStart, i - 1 - vStart));
- if (it != constEnd()) {
- result.replace(vStart - 2, i - vStart + 2, *it);
- i = vStart - 2 + it->length();
+ } else {
+ enum { BASE, OPTIONALVARIABLEBRACE, VARIABLE, BRACEDVARIABLE } state = BASE;
+ int vStart = -1;
+
+ for (int i = 0; i < result.length();) {
+ QChar c = result.at(i++);
+ if (state == BASE) {
+ if (c == QLatin1Char('$'))
+ state = OPTIONALVARIABLEBRACE;
+ } else if (state == OPTIONALVARIABLEBRACE) {
+ if (c == QLatin1Char('{')) {
+ state = BRACEDVARIABLE;
+ vStart = i;
+ } else if (c.isLetterOrNumber() || c == QLatin1Char('_')) {
+ state = VARIABLE;
+ vStart = i - 1;
+ } else {
+ state = BASE;
}
- state = BASE;
- }
- } else if (state == VARIABLE) {
- if (!c.isLetterOrNumber() && c != QLatin1Char('_')) {
- const_iterator it = m_values.constFind(result.mid(vStart, i - vStart - 1));
- if (it != constEnd()) {
- result.replace(vStart - 1, i - vStart, *it);
- i = vStart - 1 + it->length();
+ } else if (state == BRACEDVARIABLE) {
+ if (c == QLatin1Char('}')) {
+ const_iterator it = m_values.constFind(result.mid(vStart, i - 1 - vStart));
+ if (it != constEnd()) {
+ result.replace(vStart - 2, i - vStart + 2, *it);
+ i = vStart - 2 + it->length();
+ }
+ state = BASE;
+ }
+ } else if (state == VARIABLE) {
+ if (!c.isLetterOrNumber() && c != QLatin1Char('_')) {
+ const_iterator it = m_values.constFind(result.mid(vStart, i - vStart - 1));
+ if (it != constEnd()) {
+ result.replace(vStart - 1, i - vStart, *it);
+ i = vStart - 1 + it->length();
+ }
+ state = BASE;
}
- state = BASE;
}
}
+ if (state == VARIABLE) {
+ const_iterator it = m_values.constFind(result.mid(vStart));
+ if (it != constEnd())
+ result.replace(vStart - 1, result.length() - vStart + 1, *it);
+ }
}
- if (state == VARIABLE) {
- const_iterator it = m_values.constFind(result.mid(vStart));
- if (it != constEnd())
- result.replace(vStart - 1, result.length() - vStart + 1, *it);
- }
-#endif
return result;
}