1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef UTILS_H
30 #define UTILS_H
31 
32 #include <QStringList>
33 #include <QMap>
34 #include <QtCore/QFile>
35 #include <QtCore/QDir>
36 #include <QtCore/QDateTime>
37 #include <QtCore/QJsonArray>
38 #include <QtCore/QJsonObject>
39 #include <QtCore/QJsonDocument>
40 
41 #include <iostream>
42 
43 QT_BEGIN_NAMESPACE
44 
45 enum PlatformFlag {
46     // OS
47     WindowsBased = 0x00001,
48     UnixBased    = 0x00002,
49     WinRt        = 0x00004,
50     // CPU
51     IntelBased   = 0x00010,
52     ArmBased     = 0x00020,
53     // Compiler
54     Msvc         = 0x00100,
55     MinGW        = 0x00200,
56     ClangMsvc    = 0x00400,
57     ClangMinGW   = 0x00800,
58     // Platforms
59     WindowsDesktopMsvc = WindowsBased + IntelBased + Msvc,
60     WindowsDesktopMinGW = WindowsBased + IntelBased + MinGW,
61     WindowsDesktopClangMsvc = WindowsBased + IntelBased + ClangMsvc,
62     WindowsDesktopClangMinGW = WindowsBased + IntelBased + ClangMinGW,
63     WinRtIntelMsvc = WindowsBased + WinRt + IntelBased + Msvc,
64     WinRtArmMsvc = WindowsBased + WinRt + ArmBased + Msvc,
65     Unix = UnixBased,
66     UnknownPlatform
67 };
68 
Q_DECLARE_FLAGS(Platform,PlatformFlag)69 Q_DECLARE_FLAGS(Platform, PlatformFlag)
70 
71 Q_DECLARE_OPERATORS_FOR_FLAGS(Platform)
72 
73 inline bool platformHasDebugSuffix(Platform p) // Uses 'd' debug suffix
74 {
75     return p.testFlag(Msvc) || p.testFlag(ClangMsvc);
76 }
77 
78 enum ListOption {
79     ListNone = 0,
80     ListSource,
81     ListTarget,
82     ListRelative,
83     ListMapping
84 };
85 
86 inline std::wostream &operator<<(std::wostream &str, const QString &s)
87 {
88 #ifdef Q_OS_WIN
89     str << reinterpret_cast<const wchar_t *>(s.utf16());
90 #else
91     str << s.toStdWString();
92 #endif
93     return str;
94 }
95 
96 // Container class for JSON output
97 class JsonOutput
98 {
99     using SourceTargetMapping = QPair<QString, QString>;
100     using SourceTargetMappings = QList<SourceTargetMapping>;
101 
102 public:
addFile(const QString & source,const QString & target)103     void addFile(const QString &source, const QString &target)
104     {
105         m_files.append(SourceTargetMapping(source, target));
106     }
107 
removeTargetDirectory(const QString & targetDirectory)108     void removeTargetDirectory(const QString &targetDirectory)
109     {
110         for (int i = m_files.size() - 1; i >= 0; --i) {
111             if (m_files.at(i).second == targetDirectory)
112                 m_files.removeAt(i);
113         }
114     }
115 
toJson()116     QByteArray toJson() const
117     {
118         QJsonObject document;
119         QJsonArray files;
120         for (const SourceTargetMapping &mapping : m_files) {
121             QJsonObject object;
122             object.insert(QStringLiteral("source"), QDir::toNativeSeparators(mapping.first));
123             object.insert(QStringLiteral("target"), QDir::toNativeSeparators(mapping.second));
124             files.append(object);
125         }
126         document.insert(QStringLiteral("files"), files);
127         return QJsonDocument(document).toJson();
128     }
toList(ListOption option,const QDir & base)129     QByteArray toList(ListOption option, const QDir &base) const
130     {
131         QByteArray list;
132         for (const SourceTargetMapping &mapping : m_files) {
133             const QString source = QDir::toNativeSeparators(mapping.first);
134             const QString fileName = QFileInfo(mapping.first).fileName();
135             const QString target = QDir::toNativeSeparators(mapping.second) + QDir::separator() + fileName;
136             switch (option) {
137             case ListNone:
138                 break;
139             case ListSource:
140                 list += source.toUtf8() + '\n';
141                 break;
142             case ListTarget:
143                 list += target.toUtf8() + '\n';
144                 break;
145             case ListRelative:
146                 list += QDir::toNativeSeparators(base.relativeFilePath(target)).toUtf8() + '\n';
147                 break;
148             case ListMapping:
149                 list += '"' + source.toUtf8() + "\" \"" + QDir::toNativeSeparators(base.relativeFilePath(target)).toUtf8() + "\"\n";
150                 break;
151             }
152         }
153         return list;
154     }
155 private:
156     SourceTargetMappings m_files;
157 };
158 
159 #ifdef Q_OS_WIN
160 QString normalizeFileName(const QString &name);
161 QString winErrorMessage(unsigned long error);
162 QString findSdkTool(const QString &tool);
163 #else // !Q_OS_WIN
normalizeFileName(const QString & name)164 inline QString normalizeFileName(const QString &name) { return name; }
165 #endif // !Q_OS_WIN
166 
167 static const char windowsSharedLibrarySuffix[] = ".dll";
168 static const char unixSharedLibrarySuffix[] = ".so";
169 
sharedLibrarySuffix(Platform platform)170 inline QString sharedLibrarySuffix(Platform platform) { return QLatin1String((platform & WindowsBased) ? windowsSharedLibrarySuffix : unixSharedLibrarySuffix); }
171 bool isBuildDirectory(Platform platform, const QString &dirName);
172 
173 bool createSymbolicLink(const QFileInfo &source, const QString &target, QString *errorMessage);
174 bool createDirectory(const QString &directory, QString *errorMessage);
175 QString findInPath(const QString &file);
176 
177 extern const char *qmakeInfixKey; // Fake key containing the libinfix
178 
179 QMap<QString, QString> queryQMakeAll(QString *errorMessage);
180 QString queryQMake(const QString &variable, QString *errorMessage);
181 
182 enum DebugMatchMode {
183     MatchDebug,
184     MatchRelease,
185     MatchDebugOrRelease
186 };
187 
188 QStringList findSharedLibraries(const QDir &directory, Platform platform,
189                                 DebugMatchMode debugMatchMode,
190                                 const QString &prefix = QString());
191 
192 bool updateFile(const QString &sourceFileName, const QStringList &nameFilters,
193                 const QString &targetDirectory, unsigned flags, JsonOutput *json, QString *errorMessage);
194 bool runProcess(const QString &binary, const QStringList &args,
195                 const QString &workingDirectory = QString(),
196                 unsigned long *exitCode = 0, QByteArray *stdOut = 0, QByteArray *stdErr = 0,
197                 QString *errorMessage = 0);
198 bool runElevatedBackgroundProcess(const QString &binary, const QStringList &args, Qt::HANDLE *processHandle);
199 
200 bool readPeExecutable(const QString &peExecutableFileName, QString *errorMessage,
201                       QStringList *dependentLibraries = 0, unsigned *wordSize = 0,
202                       bool *isDebug = 0, bool isMinGW = false, unsigned short *machineArch = nullptr);
203 bool readElfExecutable(const QString &elfExecutableFileName, QString *errorMessage,
204                       QStringList *dependentLibraries = 0, unsigned *wordSize = 0,
205                       bool *isDebug = 0);
206 
207 inline bool readExecutable(const QString &executableFileName, Platform platform,
208                            QString *errorMessage, QStringList *dependentLibraries = 0,
209                            unsigned *wordSize = 0, bool *isDebug = 0, unsigned short *machineArch = nullptr)
210 {
211     return platform == Unix ?
212         readElfExecutable(executableFileName, errorMessage, dependentLibraries, wordSize, isDebug) :
213         readPeExecutable(executableFileName, errorMessage, dependentLibraries, wordSize, isDebug,
214                          (platform == WindowsDesktopMinGW), machineArch);
215 }
216 
217 #ifdef Q_OS_WIN
218 #  if !defined(IMAGE_FILE_MACHINE_ARM64)
219 #    define IMAGE_FILE_MACHINE_ARM64 0xAA64
220 #  endif
221 QString getArchString (unsigned short machineArch);
222 #endif // Q_OS_WIN
223 
224 // Return dependent modules of executable files.
225 
findDependentLibraries(const QString & executableFileName,Platform platform,QString * errorMessage)226 inline QStringList findDependentLibraries(const QString &executableFileName, Platform platform, QString *errorMessage)
227 {
228     QStringList result;
229     readExecutable(executableFileName, platform, errorMessage, &result);
230     return result;
231 }
232 
233 QString findD3dCompiler(Platform platform, const QString &qtBinDir, unsigned wordSize);
234 
235 bool patchQtCore(const QString &path, QString *errorMessage);
236 
237 extern int optVerboseLevel;
238 
239 // Recursively update a file or directory, matching DirectoryFileEntryFunction against the QDir
240 // to obtain the files.
241 enum UpdateFileFlag  {
242     ForceUpdateFile = 0x1,
243     SkipUpdateFile = 0x2,
244     RemoveEmptyQmlDirectories = 0x4,
245     SkipQmlDesignerSpecificsDirectories = 0x8
246 };
247 
248 template <class DirectoryFileEntryFunction>
updateFile(const QString & sourceFileName,DirectoryFileEntryFunction directoryFileEntryFunction,const QString & targetDirectory,unsigned flags,JsonOutput * json,QString * errorMessage)249 bool updateFile(const QString &sourceFileName,
250                 DirectoryFileEntryFunction directoryFileEntryFunction,
251                 const QString &targetDirectory,
252                 unsigned flags,
253                 JsonOutput *json,
254                 QString *errorMessage)
255 {
256     const QFileInfo sourceFileInfo(sourceFileName);
257     const QString targetFileName = targetDirectory + QLatin1Char('/') + sourceFileInfo.fileName();
258     if (optVerboseLevel > 1)
259         std::wcout << "Checking " << sourceFileName << ", " << targetFileName << '\n';
260 
261     if (!sourceFileInfo.exists()) {
262         *errorMessage = QString::fromLatin1("%1 does not exist.").arg(QDir::toNativeSeparators(sourceFileName));
263         return false;
264     }
265 
266     const QFileInfo targetFileInfo(targetFileName);
267 
268     if (sourceFileInfo.isSymLink()) {
269         const QString sourcePath = sourceFileInfo.symLinkTarget();
270         const QString relativeSource = QDir(sourceFileInfo.absolutePath()).relativeFilePath(sourcePath);
271         if (relativeSource.contains(QLatin1Char('/'))) {
272             *errorMessage = QString::fromLatin1("Symbolic links across directories are not supported (%1).")
273                             .arg(QDir::toNativeSeparators(sourceFileName));
274             return false;
275         }
276 
277         // Update the linked-to file
278         if (!updateFile(sourcePath, directoryFileEntryFunction, targetDirectory, flags, json, errorMessage))
279             return false;
280 
281         if (targetFileInfo.exists()) {
282             if (!targetFileInfo.isSymLink()) {
283                 *errorMessage = QString::fromLatin1("%1 already exists and is not a symbolic link.")
284                                 .arg(QDir::toNativeSeparators(targetFileName));
285                 return false;
286             } // Not a symlink
287             const QString relativeTarget = QDir(targetFileInfo.absolutePath()).relativeFilePath(targetFileInfo.symLinkTarget());
288             if (relativeSource == relativeTarget) // Exists and points to same entry: happy.
289                 return true;
290             QFile existingTargetFile(targetFileName);
291             if (!(flags & SkipUpdateFile) && !existingTargetFile.remove()) {
292                 *errorMessage = QString::fromLatin1("Cannot remove existing symbolic link %1: %2")
293                                 .arg(QDir::toNativeSeparators(targetFileName), existingTargetFile.errorString());
294                 return false;
295             }
296         } // target symbolic link exists
297         return createSymbolicLink(QFileInfo(targetDirectory + QLatin1Char('/') + relativeSource), sourceFileInfo.fileName(), errorMessage);
298     } // Source is symbolic link
299 
300     if (sourceFileInfo.isDir()) {
301         if ((flags & SkipQmlDesignerSpecificsDirectories) && sourceFileInfo.fileName() == QLatin1String("designer")) {
302             if (optVerboseLevel)
303                 std::wcout << "Skipping " << QDir::toNativeSeparators(sourceFileName) << ".\n";
304             return true;
305         }
306         bool created = false;
307         if (targetFileInfo.exists()) {
308             if (!targetFileInfo.isDir()) {
309                 *errorMessage = QString::fromLatin1("%1 already exists and is not a directory.")
310                                 .arg(QDir::toNativeSeparators(targetFileName));
311                 return false;
312             } // Not a directory.
313         } else { // exists.
314             QDir d(targetDirectory);
315             if (optVerboseLevel)
316                 std::wcout << "Creating " << targetFileName << ".\n";
317             if (!(flags & SkipUpdateFile)) {
318                 created = d.mkdir(sourceFileInfo.fileName());
319                 if (!created) {
320                     *errorMessage = QString::fromLatin1("Cannot create directory %1 under %2.")
321                             .arg(sourceFileInfo.fileName(), QDir::toNativeSeparators(targetDirectory));
322                     return false;
323                 }
324             }
325         }
326         // Recurse into directory
327         QDir dir(sourceFileName);
328 
329         const QStringList allEntries = directoryFileEntryFunction(dir) + dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
330         for (const QString &entry : allEntries)
331             if (!updateFile(sourceFileName + QLatin1Char('/') + entry, directoryFileEntryFunction, targetFileName, flags, json, errorMessage))
332                 return false;
333         // Remove empty directories, for example QML import folders for which the filter did not match.
334         if (created && (flags & RemoveEmptyQmlDirectories)) {
335             QDir d(targetFileName);
336             const QStringList entries = d.entryList(QStringList(), QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
337             if (entries.isEmpty() || (entries.size() == 1 && entries.first() == QLatin1String("qmldir"))) {
338                 if (!d.removeRecursively()) {
339                     *errorMessage = QString::fromLatin1("Cannot remove empty directory %1.")
340                             .arg(QDir::toNativeSeparators(targetFileName));
341                     return false;
342                 }
343                 if (json)
344                     json->removeTargetDirectory(targetFileName);
345             }
346         }
347         return true;
348     } // Source is directory.
349 
350     if (targetFileInfo.exists()) {
351         if (!(flags & ForceUpdateFile)
352             && targetFileInfo.lastModified() >= sourceFileInfo.lastModified()) {
353             if (optVerboseLevel)
354                 std::wcout << sourceFileInfo.fileName() << " is up to date.\n";
355             if (json)
356                 json->addFile(sourceFileName, targetDirectory);
357             return true;
358         }
359         QFile targetFile(targetFileName);
360         if (!(flags & SkipUpdateFile) && !targetFile.remove()) {
361             *errorMessage = QString::fromLatin1("Cannot remove existing file %1: %2")
362                             .arg(QDir::toNativeSeparators(targetFileName), targetFile.errorString());
363             return false;
364         }
365     } // target exists
366     QFile file(sourceFileName);
367     if (optVerboseLevel)
368         std::wcout << "Updating " << sourceFileInfo.fileName() << ".\n";
369     if (!(flags & SkipUpdateFile)) {
370         if (!file.copy(targetFileName)) {
371             *errorMessage = QString::fromLatin1("Cannot copy %1 to %2: %3")
372                 .arg(QDir::toNativeSeparators(sourceFileName),
373                      QDir::toNativeSeparators(targetFileName),
374                      file.errorString());
375             return false;
376         }
377         if (!(file.permissions() & QFile::WriteUser)) { // QTBUG-40152, clear inherited read-only attribute
378             QFile targetFile(targetFileName);
379             if (!targetFile.setPermissions(targetFile.permissions() | QFile::WriteUser)) {
380                 *errorMessage = QString::fromLatin1("Cannot set write permission on %1: %2")
381                     .arg(QDir::toNativeSeparators(targetFileName), file.errorString());
382                 return false;
383             }
384         } // Check permissions
385     } // !SkipUpdateFile
386     if (json)
387         json->addFile(sourceFileName, targetDirectory);
388     return true;
389 }
390 
391 // Base class to filter files by name filters functions to be passed to updateFile().
392 class NameFilterFileEntryFunction {
393 public:
NameFilterFileEntryFunction(const QStringList & nameFilters)394     explicit NameFilterFileEntryFunction(const QStringList &nameFilters) : m_nameFilters(nameFilters) {}
operator()395     QStringList operator()(const QDir &dir) const { return dir.entryList(m_nameFilters, QDir::Files); }
396 
397 private:
398     const QStringList m_nameFilters;
399 };
400 
401 // Convenience for all files.
updateFile(const QString & sourceFileName,const QString & targetDirectory,unsigned flags,JsonOutput * json,QString * errorMessage)402 inline bool updateFile(const QString &sourceFileName, const QString &targetDirectory, unsigned flags, JsonOutput *json, QString *errorMessage)
403 {
404     return updateFile(sourceFileName, NameFilterFileEntryFunction(QStringList()), targetDirectory, flags, json, errorMessage);
405 }
406 
407 QT_END_NAMESPACE
408 
409 #endif // UTILS_H
410