1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qbs.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QBS_HOSTOSINFO_H
41 #define QBS_HOSTOSINFO_H
42 
43 #include "qbs_export.h"
44 #include "stlutils.h"
45 #include "version.h"
46 
47 #include <QtCore/qglobal.h>
48 #include <QtCore/qmap.h>
49 #include <QtCore/qsettings.h>
50 #include <QtCore/qstring.h>
51 #include <QtCore/qstringlist.h>
52 
53 #if defined(Q_OS_WIN)
54 #define QBS_HOST_EXE_SUFFIX ".exe"
55 #define QBS_HOST_DYNAMICLIB_PREFIX ""
56 #define QBS_HOST_DYNAMICLIB_SUFFIX ".dll"
57 #elif defined(Q_OS_DARWIN)
58 #define QBS_HOST_EXE_SUFFIX ""
59 #define QBS_HOST_DYNAMICLIB_PREFIX "lib"
60 #define QBS_HOST_DYNAMICLIB_SUFFIX ".dylib"
61 #else
62 #define QBS_HOST_EXE_SUFFIX ""
63 #define QBS_HOST_DYNAMICLIB_PREFIX "lib"
64 #define QBS_HOST_DYNAMICLIB_SUFFIX ".so"
65 #endif // Q_OS_WIN
66 
67 namespace qbs {
68 namespace Internal {
69 
70 class HostOsInfo
71 {
72 public:
73     // Add more as needed.
74     enum HostOs { HostOsWindows, HostOsLinux, HostOsMacos, HostOsOtherUnix, HostOsOther };
75 
76     static inline std::string hostOSIdentifier();
77     static inline std::string hostOSArchitecture();
78     static inline std::vector<std::string> hostOSIdentifiers();
79     static inline std::vector<std::string> canonicalOSIdentifiers(const std::string &os);
80     static inline HostOs hostOs();
81 
hostOsVersion()82     static inline Version hostOsVersion() {
83         Version v;
84         if (HostOsInfo::isWindowsHost()) {
85             QSettings settings(QStringLiteral("HKEY_LOCAL_MACHINE\\Software\\"
86                                                    "Microsoft\\Windows NT\\CurrentVersion"),
87                                QSettings::NativeFormat);
88             v = v.fromString(settings.value(QStringLiteral("CurrentVersion")).toString() +
89                              QLatin1Char('.') +
90                              settings.value(QStringLiteral("CurrentBuildNumber")).toString());
91             Q_ASSERT(v.isValid());
92         } else if (HostOsInfo::isMacosHost()) {
93             QSettings settings(QStringLiteral("/System/Library/CoreServices/SystemVersion.plist"),
94                                QSettings::NativeFormat);
95             v = v.fromString(settings.value(QStringLiteral("ProductVersion")).toString());
96             Q_ASSERT(v.isValid());
97         }
98         return v;
99     }
100 
isWindowsHost()101     static bool isWindowsHost() { return hostOs() == HostOsWindows; }
isLinuxHost()102     static bool isLinuxHost() { return hostOs() == HostOsLinux; }
isMacosHost()103     static bool isMacosHost() { return hostOs() == HostOsMacos; }
104     static inline bool isAnyUnixHost();
105     static inline QString rfc1034Identifier(const QString &str);
106 
appendExecutableSuffix(const QString & executable)107     static QString appendExecutableSuffix(const QString &executable)
108     {
109         QString finalName = executable;
110         if (isWindowsHost())
111             finalName += QLatin1String(QBS_HOST_EXE_SUFFIX);
112         return finalName;
113     }
114 
stripExecutableSuffix(const QString & executable)115     static QString stripExecutableSuffix(const QString &executable)
116     {
117         constexpr QLatin1String suffix(QBS_HOST_EXE_SUFFIX, sizeof(QBS_HOST_EXE_SUFFIX) - 1);
118         return !suffix.isEmpty() && executable.endsWith(suffix)
119             ? executable.chopped(suffix.size()) : executable;
120     }
121 
dynamicLibraryName(const QString & libraryBaseName)122     static QString dynamicLibraryName(const QString &libraryBaseName)
123     {
124         return QLatin1String(QBS_HOST_DYNAMICLIB_PREFIX) + libraryBaseName
125                 + QLatin1String(QBS_HOST_DYNAMICLIB_SUFFIX);
126     }
127 
fileNameCaseSensitivity()128     static Qt::CaseSensitivity fileNameCaseSensitivity()
129     {
130         return isWindowsHost() ? Qt::CaseInsensitive: Qt::CaseSensitive;
131     }
132 
libraryPathEnvironmentVariable()133     static QString libraryPathEnvironmentVariable()
134     {
135         if (isWindowsHost())
136             return QStringLiteral("PATH");
137         if (isMacosHost())
138             return QStringLiteral("DYLD_LIBRARY_PATH");
139         return QStringLiteral("LD_LIBRARY_PATH");
140     }
141 
142     static QChar pathListSeparator(HostOsInfo::HostOs hostOs = HostOsInfo::hostOs())
143     {
144         return hostOs == HostOsWindows ? QLatin1Char(';') : QLatin1Char(':');
145     }
146 
147     static QChar pathSeparator(HostOsInfo::HostOs hostOs = HostOsInfo::hostOs())
148     {
149         return hostOs == HostOsWindows ? QLatin1Char('\\') : QLatin1Char('/');
150     }
151 
controlModifier()152     static Qt::KeyboardModifier controlModifier()
153     {
154         return isMacosHost() ? Qt::MetaModifier : Qt::ControlModifier;
155     }
156 };
157 
hostOSIdentifier()158 std::string HostOsInfo::hostOSIdentifier()
159 {
160 #if defined(__APPLE__)
161     return "macos";
162 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
163     return "windows";
164 #elif defined(_AIX)
165     return "aix";
166 #elif defined(hpux) || defined(__hpux)
167     return "hpux";
168 #elif defined(__sun) || defined(sun)
169     return "solaris";
170 #elif defined(__linux__) || defined(__linux)
171     return "linux";
172 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
173     return "freebsd";
174 #elif defined(__NetBSD__)
175     return "netbsd";
176 #elif defined(__OpenBSD__)
177     return "openbsd";
178 #elif defined(__GNU__)
179     return "hurd";
180 #elif defined(__HAIKU__)
181     return "haiku";
182 #else
183     #warning "Qbs has not been ported to this OS - see http://qbs.io/"
184     return "";
185 #endif
186 }
187 
hostOSArchitecture()188 std::string HostOsInfo::hostOSArchitecture()
189 {
190     const auto cpuArch = QSysInfo::currentCpuArchitecture();
191     if (cpuArch == QLatin1String("i386"))
192         return "x86";
193     return cpuArch.toStdString();
194 }
195 
hostOSIdentifiers()196 std::vector<std::string> HostOsInfo::hostOSIdentifiers()
197 {
198     return canonicalOSIdentifiers(hostOSIdentifier());
199 }
200 
canonicalOSIdentifiers(const std::string & name)201 std::vector<std::string> HostOsInfo::canonicalOSIdentifiers(const std::string &name)
202 {
203     std::vector<std::string> list { name };
204     if (contains({"ios-simulator"}, name))
205         list << canonicalOSIdentifiers("ios");
206     if (contains({"tvos-simulator"}, name))
207         list << canonicalOSIdentifiers("tvos");
208     if (contains({"watchos-simulator"}, name))
209         list << canonicalOSIdentifiers("watchos");
210     if (contains({"macos", "ios", "tvos", "watchos"}, name))
211         list << canonicalOSIdentifiers("darwin");
212     if (contains({"darwin", "freebsd", "netbsd", "openbsd"}, name))
213         list << canonicalOSIdentifiers("bsd");
214     if (contains({"android"}, name))
215         list << canonicalOSIdentifiers("linux");
216 
217     // Note: recognized non-Unix platforms include: windows, haiku, vxworks
218     if (contains({"bsd", "aix", "hpux", "solaris", "linux", "hurd", "qnx", "integrity"}, name))
219         list << canonicalOSIdentifiers("unix");
220 
221     return list;
222 }
223 
hostOs()224 HostOsInfo::HostOs HostOsInfo::hostOs()
225 {
226 #if defined(Q_OS_WIN)
227     return HostOsWindows;
228 #elif defined(Q_OS_LINUX)
229     return HostOsLinux;
230 #elif defined(Q_OS_DARWIN)
231     return HostOsMacos;
232 #elif defined(Q_OS_UNIX)
233     return HostOsOtherUnix;
234 #else
235     return HostOsOther;
236 #endif
237 }
238 
isAnyUnixHost()239 bool HostOsInfo::isAnyUnixHost()
240 {
241 #ifdef Q_OS_UNIX
242     return true;
243 #else
244     return false;
245 #endif
246 }
247 
rfc1034Identifier(const QString & str)248 QString HostOsInfo::rfc1034Identifier(const QString &str)
249 {
250     QString s = str;
251     for (QChar &ch : s) {
252         const char c = ch.toLatin1();
253 
254         const bool okChar = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')
255                 || (c >= 'a' && c <= 'z') || c == '-' || c == '.';
256         if (!okChar)
257             ch = QChar::fromLatin1('-');
258     }
259     return s;
260 }
261 
262 } // namespace Internal
263 } // namespace qbs
264 
265 #endif // QBS_HOSTOSINFO_H
266