8#include <QtCore/QString>
9#include <QtCore/QDebug>
11#include <QtCore/QFile>
12#include <QtCore/QFileInfo>
13#include <QtCore/QProcess>
14#include <QtCore/QTemporaryFile>
15#include <QtCore/QScopedPointer>
16#include <QtCore/QScopedArrayPointer>
17#include <QtCore/QStandardPaths>
20# include <QtCore/qt_windows.h>
26using namespace Qt::StringLiterals;
32 return (platform.testFlag(Msvc) || platform.testFlag(ClangMsvc))
33 && (dirName ==
"debug"_L1 || dirName ==
"release"_L1);
40 const QString oldDirectory = QDir::currentPath();
41 if (!QDir::setCurrent(source.absolutePath())) {
42 *errorMessage = QStringLiteral(
"Unable to change to directory %1.").arg(QDir::toNativeSeparators(source.absolutePath()));
45 QFile file(source.fileName());
46 const bool success = file.link(target);
47 QDir::setCurrent(oldDirectory);
49 *errorMessage = QString::fromLatin1(
"Failed to create symbolic link %1 -> %2: %3")
50 .arg(QDir::toNativeSeparators(source.absoluteFilePath()),
51 QDir::toNativeSeparators(target), file.errorString());
59 const QFileInfo fi(directory);
63 *errorMessage = QString::fromLatin1(
"%1 already exists and is not a directory.").
64 arg(QDir::toNativeSeparators(directory));
68 std::wcout <<
"Creating " << QDir::toNativeSeparators(directory) <<
"...\n";
71 if (!dir.mkpath(directory)) {
72 *errorMessage = QString::fromLatin1(
"Could not create directory %1.")
73 .arg(QDir::toNativeSeparators(directory));
83 const QString &prefix)
85 QString nameFilter = prefix;
86 if (nameFilter.isEmpty())
88 if (debugMatchMode ==
MatchDebug && platformHasDebugSuffix(platform))
90 nameFilter += sharedLibrarySuffix();
93 const QFileInfoList &dlls = directory.entryInfoList(QStringList(nameFilter), QDir::Files);
94 for (
const QFileInfo &dllFi : dlls) {
95 const QString dllPath = dllFi.absoluteFilePath();
97 if (debugMatchMode != MatchDebugOrRelease && (platform & WindowsBased)) {
98 PeHeaderInfoStruct info;
99 if (readPeExecutableInfo(dllPath, &errorMessage, &info)) {
100 matches = info.isDebug == (debugMatchMode == MatchDebug);
102 std::wcerr <<
"Warning: Unable to read " << QDir::toNativeSeparators(dllPath)
103 <<
": " << errorMessage;
107 result += dllFi.fileName();
115 wchar_t shortBuffer[MAX_PATH];
116 const QString nativeFileName = QDir::toNativeSeparators(name);
117 if (!GetShortPathNameW(
reinterpret_cast<LPCWSTR>(nativeFileName.utf16()), shortBuffer, MAX_PATH))
119 wchar_t result[MAX_PATH];
120 if (!GetLongPathNameW(shortBuffer, result, MAX_PATH))
122 return QDir::fromNativeSeparators(QString::fromWCharArray(result));
127 const bool needsQuote = argument.contains(u' ');
128 if (!commandLine->isEmpty())
129 commandLine->append(u' ');
131 commandLine->append(u'"');
132 commandLine->append(argument);
134 commandLine->append(u'"');
137bool runProcess(
const QString &binary,
const QStringList &args,
138 const QString &workingDirectory,
139 unsigned long *exitCode, QByteArray *stdOut, QByteArray *stdErr,
140 QString *errorMessage,
int timeout)
146 process.setProgram(binary);
147 process.setArguments(args);
148 process.setWorkingDirectory(workingDirectory);
153 appendToCommandLine(binary, &commandLine);
154 for (
const QString &a : args)
155 appendToCommandLine(a, &commandLine);
156 std::wcout <<
"Running: " << commandLine <<
'\n';
160 if (!process.waitForStarted() || !process.waitForFinished(timeout)) {
162 *errorMessage = process.errorString();
167 *stdOut = process.readAllStandardOutput();
169 *stdErr = process.readAllStandardError();
179 if (file.size() < MAX_PATH - 1) {
180 wchar_t buffer[MAX_PATH];
181 file.toWCharArray(buffer);
182 buffer[file.size()] = 0;
183 if (PathFindOnPath(buffer, NULL))
184 return QDir::cleanPath(QString::fromWCharArray(buffer));
188 return QStandardPaths::findExecutable(file);
196 const QString binary = !qtpathsBinary.isEmpty() ? qtpathsBinary : QStringLiteral(
"qtpaths");
197 const QString colonSpace = QStringLiteral(
": ");
200 unsigned long exitCode = 0;
201 if (!runProcess(binary, QStringList(QStringLiteral(
"-query")), QString(), &exitCode, &stdOut,
202 &stdErr, errorMessage)) {
203 *errorMessage = QStringLiteral(
"Error running binary ") + binary + colonSpace + *errorMessage;
204 return QMap<QString, QString>();
207 *errorMessage = binary + QStringLiteral(
" returns ") + QString::number(exitCode)
208 + colonSpace + QString::fromLocal8Bit(stdErr);
209 return QMap<QString, QString>();
211 const QString output = QString::fromLocal8Bit(stdOut).trimmed().remove(u'\r');
212 QMap<QString, QString> result;
213 const qsizetype size = output.size();
214 for (qsizetype pos = 0; pos < size; ) {
215 const qsizetype colonPos = output.indexOf(u':', pos);
218 qsizetype endPos = output.indexOf(u'\n', colonPos + 1);
221 const QString key = output.mid(pos, colonPos - pos);
222 const QString value = output.mid(colonPos + 1, endPos - colonPos - 1);
223 result.insert(key, value);
226 QFile qconfigPriFile(result.value(QStringLiteral(
"QT_HOST_DATA")) + QStringLiteral(
"/mkspecs/qconfig.pri"));
227 if (qconfigPriFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
228 QByteArray lineArray;
229 while (qconfigPriFile.readLineInto(&lineArray)) {
230 QByteArrayView line = QByteArrayView(lineArray);
231 if (line.startsWith(
"QT_LIBINFIX")) {
232 const int pos = line.indexOf(
'=');
234 const QString infix = QString::fromUtf8(line.right(line.size() - pos - 1).trimmed());
235 if (!infix.isEmpty())
242 std::wcerr <<
"Warning: Unable to read " << QDir::toNativeSeparators(qconfigPriFile.fileName())
243 << colonSpace << qconfigPriFile.errorString()<<
'\n';
249bool updateFile(
const QString &sourceFileName,
const QStringList &nameFilters,
250 const QString &targetDirectory,
unsigned flags,
JsonOutput *json, QString *errorMessage)
252 const QFileInfo sourceFileInfo(sourceFileName);
253 const QString targetFileName = targetDirectory + u'/' + sourceFileInfo.fileName();
255 std::wcout <<
"Checking " << sourceFileName <<
", " << targetFileName<<
'\n';
257 if (!sourceFileInfo.exists()) {
258 *errorMessage = QString::fromLatin1(
"%1 does not exist.").arg(QDir::toNativeSeparators(sourceFileName));
262 if (sourceFileInfo.isSymLink()) {
263 *errorMessage = QString::fromLatin1(
"Symbolic links are not supported (%1).")
264 .arg(QDir::toNativeSeparators(sourceFileName));
268 const QFileInfo targetFileInfo(targetFileName);
270 if (sourceFileInfo.isDir()) {
271 if (targetFileInfo.exists()) {
272 if (!targetFileInfo.isDir()) {
273 *errorMessage = QString::fromLatin1(
"%1 already exists and is not a directory.")
274 .arg(QDir::toNativeSeparators(targetFileName));
278 QDir d(targetDirectory);
280 std::wcout <<
"Creating " << QDir::toNativeSeparators(targetFileName) <<
".\n";
281 if (!(flags &
SkipUpdateFile) && !d.mkdir(sourceFileInfo.fileName())) {
282 *errorMessage = QString::fromLatin1(
"Cannot create directory %1 under %2.")
283 .arg(sourceFileInfo.fileName(), QDir::toNativeSeparators(targetDirectory));
288 QDir dir(sourceFileName);
289 const QFileInfoList allEntries = dir.entryInfoList(nameFilters, QDir::Files)
290 + dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
291 for (
const QFileInfo &entryFi : allEntries) {
292 if (!updateFile(entryFi.absoluteFilePath(), nameFilters, targetFileName, flags, json, errorMessage))
298 if (targetFileInfo.exists()) {
300 && targetFileInfo.lastModified() >= sourceFileInfo.lastModified()) {
302 std::wcout << sourceFileInfo.fileName() <<
" is up to date.\n";
304 json->addFile(sourceFileName, targetDirectory);
307 QFile targetFile(targetFileName);
309 *errorMessage = QString::fromLatin1(
"Cannot remove existing file %1: %2")
310 .arg(QDir::toNativeSeparators(targetFileName), targetFile.errorString());
314 QFile file(sourceFileName);
316 std::wcout <<
"Updating " << sourceFileInfo.fileName() <<
".\n";
318 *errorMessage = QString::fromLatin1(
"Cannot copy %1 to %2: %3")
319 .arg(QDir::toNativeSeparators(sourceFileName),
320 QDir::toNativeSeparators(targetFileName),
325 json->addFile(sourceFileName, targetDirectory);
331bool readPeExecutableInfo(
const QString &peExecutableFileName, QString *errorMessage,
332 PeHeaderInfoStruct *headerInfo)
336 *errorMessage = QString::fromLatin1(
"Mandatory parameter missing. Please provide headerInfo");
340 PeHeaderInfo *peHeaderInfo = PeHeaderInfoCache::peHeaderInfo(peExecutableFileName);
341 if (!peHeaderInfo->isValid()) {
342 *errorMessage = peHeaderInfo->errorMessage();
346 headerInfo->wordSize = peHeaderInfo->wordSize();
347 headerInfo->isDebug = peHeaderInfo->isDebug();
348 headerInfo->machineArch = peHeaderInfo->machineArch();
351 if (optVerboseLevel > 1) {
352 std::wcout <<
__FUNCTION__ <<
": " << QDir::toNativeSeparators(peExecutableFileName)
353 <<
' ' << headerInfo->wordSize <<
" bit";
354 std::wcout << (headerInfo->isDebug ?
", debug" :
", release");
363bool readPeExecutableDependencies(
const QString &peExecutableFileName, QString *errorMessage,
364 QStringList *dependentLibraries)
367 if (!dependentLibraries) {
368 *errorMessage = QString::fromLatin1(
"Mandatory parameter missing. Provide dependentLibraries");
372 dependentLibraries->clear();
374 PeHeaderInfo *peHeaderInfo = PeHeaderInfoCache::peHeaderInfo(peExecutableFileName);
375 if (!peHeaderInfo->isValid()) {
376 *errorMessage = peHeaderInfo->errorMessage();
380 *dependentLibraries = peHeaderInfo->dependentLibs();
383 if (optVerboseLevel > 1) {
384 std::wcout <<
__FUNCTION__ <<
": " << QDir::toNativeSeparators(peExecutableFileName);
385 std::wcout <<
", dependent libraries: ";
386 if (optVerboseLevel > 2)
387 std::wcout << dependentLibraries->join(u' ');
389 std::wcout << dependentLibraries->size();
396QString findD3dCompiler(Platform platform,
const QString &qtBinDir,
unsigned wordSize)
398 const QString prefix = QStringLiteral(
"D3Dcompiler_");
399 const QString suffix = QLatin1StringView(windowsSharedLibrarySuffix);
401 const QString kitDir = QString::fromLocal8Bit(qgetenv(
"WindowsSdkDir"));
402 if (!kitDir.isEmpty()) {
403 QString redistDirPath = QDir::cleanPath(kitDir) + QStringLiteral(
"/Redist/D3D/");
404 if (platform.testFlag(ArmBased)) {
405 redistDirPath += QStringLiteral(
"arm");
407 redistDirPath += wordSize == 32 ? QStringLiteral(
"x86") : QStringLiteral(
"x64");
409 QDir redistDir(redistDirPath);
410 if (redistDir.exists()) {
411 const QFileInfoList files = redistDir.entryInfoList(QStringList(prefix + u'*' + suffix), QDir::Files);
412 if (!files.isEmpty())
413 return files.front().absoluteFilePath();
416 QStringList candidateVersions;
417 for (
int i = 47 ; i >= 40 ; --i)
418 candidateVersions.append(prefix + QString::number(i) + suffix);
421 for (
const QString &candidate : std::as_const(candidateVersions)) {
422 const QFileInfo fi(qtBinDir + u'/' + candidate);
424 return fi.absoluteFilePath();
427 if (platform.testFlag(IntelBased)) {
428 QString errorMessage;
429 PeHeaderInfoStruct info;
430 for (
const QString &candidate : std::as_const(candidateVersions)) {
431 const QString dll = findInPath(candidate);
433 && readPeExecutableInfo(dll, &errorMessage, &info)
434 && info.wordSize == wordSize) {
442QStringList findDxc(Platform platform,
const QString &qtBinDir,
unsigned wordSize)
445 const QString kitDir = QString::fromLocal8Bit(qgetenv(
"WindowsSdkDir"));
446 const QString suffix = QLatin1StringView(windowsSharedLibrarySuffix);
447 for (QString prefix : { QStringLiteral(
"dxcompiler"), QStringLiteral(
"dxil") }) {
448 QString name = prefix + suffix;
449 if (!kitDir.isEmpty()) {
450 QString redistDirPath = QDir::cleanPath(kitDir) + QStringLiteral(
"/Redist/D3D/");
451 if (platform.testFlag(ArmBased)) {
452 redistDirPath += wordSize == 32 ? QStringLiteral(
"arm") : QStringLiteral(
"arm64");
454 redistDirPath += wordSize == 32 ? QStringLiteral(
"x86") : QStringLiteral(
"x64");
456 QDir redistDir(redistDirPath);
457 if (redistDir.exists()) {
458 const QFileInfoList files = redistDir.entryInfoList(QStringList(prefix + u'*' + suffix), QDir::Files);
459 if (!files.isEmpty()) {
460 results.append(files.front().absoluteFilePath());
467 const QFileInfo fi(qtBinDir + u'/' + name);
469 results.append(fi.absoluteFilePath());
473 if (platform.testFlag(IntelBased)) {
474 QString errorMessage;
475 PeHeaderInfoStruct info;
476 const QString dll = findInPath(name);
478 && readPeExecutableInfo(dll, &errorMessage, &info)
479 && info.wordSize == wordSize)
492 PeHeaderInfoStruct *)
494 *errorMessage = QStringLiteral(
"Not implemented.");
500 *errorMessage = QStringLiteral(
"Not implemented.");
511 return QStringList();
520 std::wcout <<
"Patching " << QFileInfo(path).fileName() <<
"...\n";
523 if (!file.open(QIODevice::ReadOnly)) {
524 *errorMessage = QString::fromLatin1(
"Unable to patch %1: %2").arg(
525 QDir::toNativeSeparators(path), file.errorString());
528 const QByteArray oldContent = file.readAll();
530 if (oldContent.isEmpty()) {
531 *errorMessage = QString::fromLatin1(
"Unable to patch %1: Could not read file content").arg(
532 QDir::toNativeSeparators(path));
537 QByteArray content = oldContent;
539 QByteArray prfxpath(
"qt_prfxpath=");
540 int startPos = content.indexOf(prfxpath);
541 if (startPos == -1) {
542 *errorMessage = QString::fromLatin1(
543 "Unable to patch %1: Could not locate pattern \"qt_prfxpath=\"").arg(
544 QDir::toNativeSeparators(path));
547 startPos += prfxpath.length();
548 int endPos = content.indexOf(
char(0), startPos);
550 *errorMessage = QString::fromLatin1(
"Unable to patch %1: Internal error").arg(
551 QDir::toNativeSeparators(path));
555 QByteArray replacement = QByteArray(endPos - startPos,
char(0));
556 replacement[0] =
'.';
557 content.replace(startPos, endPos - startPos, replacement);
558 if (content == oldContent)
561 if (!file.open(QIODevice::WriteOnly)
562 || (file.write(content) != content.size())) {
563 *errorMessage = QString::fromLatin1(
"Unable to patch %1: Could not write to file: %2").arg(
564 QDir::toNativeSeparators(path), file.errorString());
571QString getArchString(
unsigned short machineArch)
573 switch (machineArch) {
574 case IMAGE_FILE_MACHINE_I386:
575 return QStringLiteral(
"x86");
576 case IMAGE_FILE_MACHINE_ARM:
577 return QStringLiteral(
"arm");
578 case IMAGE_FILE_MACHINE_AMD64:
579 return QStringLiteral(
"x64");
580 case IMAGE_FILE_MACHINE_ARM64:
581 return QStringLiteral(
"arm64");
static void appendToCommandLine(const QString &argument, QString *commandLine)
QString findD3dCompiler(Platform platform, const QString &qtBinDir, unsigned wordSize)
bool runProcess(const QString &binary, const QStringList &args, const QString &workingDirectory=QString(), unsigned long *exitCode=0, QByteArray *stdOut=0, QByteArray *stdErr=0, QString *errorMessage=0, int timeout=30000)
QStringList findDxc(Platform platform, const QString &qtBinDir, unsigned wordSize)
QStringList findSharedLibraries(const QDir &directory, Platform platform, DebugMatchMode debugMatchMode, const QString &prefix=QString())
const char * qmakeInfixKey
QString findInPath(const QString &file)
bool createSymbolicLink(const QFileInfo &source, const QString &target, QString *errorMessage)
bool readPeExecutableDependencies(const QString &peExecutableFileName, QString *errorMessage, QStringList *dependentLibraries=0)
bool createDirectory(const QString &directory, QString *errorMessage, bool dryRun)
bool updateFile(const QString &sourceFileName, const QStringList &nameFilters, const QString &targetDirectory, unsigned flags, JsonOutput *json, QString *errorMessage)
bool isBuildDirectory(Platform platform, const QString &dirName)
bool patchQtCore(const QString &path, QString *errorMessage)
QMap< QString, QString > queryQtPaths(const QString &qmakeBinary, QString *errorMessage)
bool readPeExecutableInfo(const QString &peExecutableFileName, QString *errorMessage, PeHeaderInfoStruct *headerInfo)
QString normalizeFileName(const QString &name)