summaryrefslogtreecommitdiffstats
path: root/src/windeployqt/main.cpp
blob: 40ad5c329bf67d2d8ccf45597aad4a3c54fe7c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "utils.h"

#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QCoreApplication>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>

#include <cstdio>

enum QtModule {
    QtCoreModule = 0x1,
    QtGuiModule = 0x2,
    QtSqlModule = 0x4,
    QtNetworkModule = 0x8,
    QtMultimediaModule = 0x10,
    QtMultimediaWidgetsModule = 0x20,
    QtOpenGlModule = 0x40,
    QtPrintSupportModule = 0x80,
    QtDeclarativeModule = 0x100,
    QtQmlModule = 0x200,
    QtQuickModule = 0x400,
    QtQuickParticlesModule = 0x800,
    QtScriptModule = 0x1000,
    QtSvgModule = 0x2000,
    QtXmlModule = 0x4000,
    QtXmlPatternsModule = 0x8000,
    QtHelpModule = 0x10000,
    QtSensorsModule = 0x20000,
    QtV8Module = 0x40000,
    QtWidgetsModule = 0x80000,
    QtWebKitModule = 0x100000,
    QtWebKitWidgetsModule = 0x200000
};

struct QtModuleEntry {
    unsigned module;
    const char *option;
    const char *libraryName;
    const char *translation;
};

QtModuleEntry qtModuleEntries[] = {
    { QtCoreModule, "core", "Qt5Core", "qtbase" },
    { QtGuiModule, "gui", "Qt5Gui", "qtbase" },
    { QtSqlModule, "sql", "Qt5Sql", "qtbase" },
    { QtNetworkModule, "network", "Qt5Network", "qtbase" },
    { QtMultimediaModule, "multimedia", "Qt5Multimedia", "qtmultimedia" },
    { QtMultimediaWidgetsModule, "multimediawidgets", "Qt5MultimediaWidgets", "qtmultimedia" },
    { QtOpenGlModule, "opengl", "Qt5OpenGL", 0 },
    { QtPrintSupportModule, "printsupport", "Qt5PrintSupport", 0 },
    { QtDeclarativeModule, "declarative", "Qt5Declarative", "qtquick1" },
    { QtQmlModule, "qml", "Qt5Qml", "qtdeclarative" },
    { QtQuickModule, "quick", "Qt5Quick", "qtdeclarative" },
    { QtQuickParticlesModule, "quickparticles", "Qt5QuickParticles", 0 },
    { QtScriptModule, "script", "Qt5Script", "qtscript" },
    { QtXmlModule, "xml", "Qt5Xml", 0 },
    { QtXmlPatternsModule, "xmlpatterns", "Qt5XmlPatterns", "qtxmlpatterns" },
    { QtHelpModule, "help", "Qt5Help", "qt_help" },
    { QtSensorsModule, "sensors", "Qt5Sensors", 0 },
    { QtSvgModule, "svg", "Qt5Svg", 0 },
    { QtV8Module, "v8", "Qt5V8", 0 },
    { QtWebKitModule, "webkit", "Qt5WebKit", 0 },
    { QtWebKitWidgetsModule, "webkitwidgets", "Qt5WebKitWidgets", 0 },
    { QtWidgetsModule, "widgets", "Qt5Widgets", "qtbase" }
};

static QByteArray formatQtModules(unsigned mask, bool option = false)
{
    QByteArray result;
    const size_t qtModulesCount = sizeof(qtModuleEntries)/sizeof(QtModuleEntry);
    for (size_t i = 0; i < qtModulesCount; ++i) {
        if (mask & qtModuleEntries[i].module) {
            if (!result.isEmpty())
                result.append(' ');
            result.append(option ? qtModuleEntries[i].option : qtModuleEntries[i].libraryName);
        }
    }
    return result;
}

static const char webProcessC[] = "QtWebProcess";

static inline QString webProcessBinary(Platform p)
{
    const QString webProcess = QLatin1String(webProcessC);
    return p == Windows || p == WinRt ? webProcess + QStringLiteral(".exe") : webProcess;
}

static unsigned qtModuleByOption(const QStringRef &r)
{
    const size_t qtModulesCount = sizeof(qtModuleEntries)/sizeof(QtModuleEntry);
    for (size_t i = 0; i < qtModulesCount; ++i)
        if (r == QLatin1String(qtModuleEntries[i].option))
            return qtModuleEntries[i].module;
    return 0;
}

static Platform platformFromMkSpec(const QString &xSpec)
{
    if (xSpec == QLatin1String("linux-g++"))
        return Unix;
    if (xSpec.startsWith(QLatin1String("win32-")))
        return Windows;
    if (xSpec.startsWith(QLatin1String("winrt")) || xSpec.startsWith(QLatin1String("winphone")))
        return WinRt;
    return UnknownPlatform;
}

bool optHelp = false;
int optWebKit2 = 0;

// Container class for JSON output
class JsonOutput {
public:
    void addFile(const QString &source, const QString &target)
    {
        QJsonObject object;
        object.insert(QStringLiteral("source"), QDir::toNativeSeparators(source));
        object.insert(QStringLiteral("target"), QDir::toNativeSeparators(target));
        m_files.append(object);
    }
    QByteArray toJson() const
    {
        QJsonObject document;
        document.insert(QStringLiteral("files"), m_files);
        return QJsonDocument(document).toJson();
    }
private:
    QJsonArray m_files;
};

struct Options {
    Options() : plugins(true), libraries(true), quickImports(true), translations(true)
              , platform(Windows), additionalLibraries(0), disabledLibraries(0), json(0) {}

    bool plugins;
    bool libraries;
    bool quickImports;
    bool translations;
    Platform platform;
    unsigned additionalLibraries;
    unsigned disabledLibraries;
    QString directory;
    QString libraryDirectory;
    QString binary;
    JsonOutput *json;
};

static QByteArray usage()
{
    QByteArray result = QByteArrayLiteral("Usage: windeployqt file [options]\n\n"
"Copies/updates the dependent Qt libraries and plugins required for\n"
"a Windows/WinRT application to the build-directory.\n\n"
"<file> is an executable file or build directory.\n\n"
"Options:\n"
"         -libdir <path>     : Copy libraries to <path>\n"
"         -no-plugins        : Skip plugin deployment\n"
"         -no-libraries      : Skip library deployment\n"
"         -no-quick-imports  : Skip deployment of Qt Quick imports\n"
"         -no-translations   : Skip deployment of the translations\n"
"         -webkit2           : Deployment of WebKit2 (web process)\n"
"         -no-webkit2        : Skip deployment of WebKit2\n"
"         -h                 : Display help\n"
"         -json              : Print to stdout in JSON format\n"
"         -verbose=<0-3>     : 0 = no output, 1 = progress (default),\n"
"                              2 = normal, 3 = debug\n"
"\nLibraries can be added by passing their name (-xml) or disabled by passing\n"
"the name prepended by -no- (-no-xml).\nAvailable libraries: ");
    result += formatQtModules(0xFFFFFFFF, true);
    return result;
}

static inline bool parseOption(QStringList::ConstIterator &it,
                               const QStringList::ConstIterator &end,
                               Options *options)
{
    const QString &option = *it;
    if (option == QLatin1String("-libdir")) {
        if (++it == end)
            return false;
        options->libraryDirectory = *it;
        return true;
    }
    if (option == QLatin1String("-no-plugins")) {
        options->plugins = false;
        return true;
    }
    if (option == QLatin1String("-no-libraries")) {
       options->libraries = false;
       return true;
    }
    if (option == QLatin1String("-no-quick-imports")) {
       options->quickImports = false;
       return true;
    }
    if (option == QLatin1String("-no-translations")) {
        options->translations = false;
        return true;
    }
    if (option == QLatin1String("-h")) {
        optHelp = true;
        return true;
    }
    if (option == QLatin1String("-h")) {
        optHelp = true;
        return true;
    }
    if (option == QLatin1String("-webkit2")) {
        optWebKit2 = 1;
        return true;
    }
    if (option == QLatin1String("-no-webkit2")) {
        optWebKit2 = -1;
        return true;
    }
    if (option == QLatin1String("-json")) {
        if (optVerboseLevel > 1)
            std::fprintf(stderr, "-json was passed, -verbose ignored.\n");
        optVerboseLevel = 0;
        options->json = new JsonOutput;
        return true;
    }
    if (option.startsWith(QLatin1String("-verbose"))) {
        if (options->json) {
            std::fprintf(stderr, "-json was passed, -verbose ignored.\n");
            return true;
        }
        const int index = option.indexOf(QLatin1Char('='));
        bool ok = false;
        optVerboseLevel = option.mid(index + 1).toInt(&ok);
        if (!ok) {
            std::fprintf(stderr, "Could not parse verbose level.\n");
            return false;
        }
        return true;
    }
    // Enabled, disabled module
    if (option.startsWith(QLatin1String("-no-"))) {
        if (const unsigned qtModule = qtModuleByOption(option.rightRef(it->size() - 4))) {
            options->disabledLibraries |= qtModule;
            return true;
        }
    }
    if (const unsigned qtModule = qtModuleByOption(option.rightRef(option.size() - 1))) {
        options->additionalLibraries |= qtModule;
        return true;
    }
    std::fprintf(stderr, "Unhandled option '%s'.\n", qPrintable(option));
    return false;
}

// Return binary from folder
static inline QString findBinary(const QString &directory, Platform platform)
{
    QDir dir(QDir::cleanPath(directory));

    const QStringList nameFilters = platform == Windows || platform == WinRt ?
        QStringList(QStringLiteral("*.exe")) : QStringList();
    foreach (const QString &binary, dir.entryList(nameFilters, QDir::Files | QDir::Executable))
        if (!binary.contains(QLatin1String(webProcessC), Qt::CaseInsensitive))
            return dir.filePath(binary);
    return QString();
}

static inline bool parseArguments(const QStringList &arguments, Options *options)
{
    const QStringList::ConstIterator end = arguments.end();
    QStringList::ConstIterator it = arguments.begin();
    for (++it ; it != end ; ++it) {
        if (it->startsWith(QLatin1Char('-'))) {
            if (!parseOption(it, end, options))
                return false;
        } else {
            if (!options->directory.isEmpty())
                return false;
            const QFileInfo fi(QDir::cleanPath(*it));
            if (!fi.exists()) {
                std::fprintf(stderr, "'%s' does not exist.\n", qPrintable(*it));
                return false;
            }
            if (fi.isFile()) {
                options->binary = fi.absoluteFilePath();
                options->directory = fi.absolutePath();
            } else {
                options->binary = findBinary(fi.absoluteFilePath(), options->platform);
                if (options->binary.isEmpty()) {
                    std::fprintf(stderr, "Unable to find binary in %s.\n", qPrintable(*it));
                    return false;
                }
                options->directory = fi.absoluteFilePath();
            } // directory.
        }     // argument
    }         // for
    return !options->binary.isEmpty();
}

// Find the latest D3D compiler DLL in path
static inline QString findD3dCompiler()
{
    for (int i = 46 ; i >= 40 ; --i) {
        const QString dll = findInPath(QStringLiteral("D3Dcompiler_") + QString::number(i) + QStringLiteral(".dll"));
        if (!dll.isEmpty())
            return dll;
    }
    return QString();
}

// Helper for recursively finding all dependent Qt libraries.
static bool findDependentQtLibraries(const QString &qtBinDir, const QString &binary, Platform platform,
                                     QString *errorMessage, QStringList *result,
                                     unsigned *wordSize = 0, bool *isDebug = 0,
                                     int *directDependencyCount = 0, int recursionDepth = 0)
{
    QStringList dependentLibs;
    if (directDependencyCount)
        *directDependencyCount = 0;
    if (!readExecutable(binary, platform, errorMessage, &dependentLibs, wordSize, isDebug)) {
        errorMessage->prepend(QLatin1String("Unable to find dependent libraries of ") +
                              QDir::toNativeSeparators(binary) + QLatin1String(" :"));
        return false;
    }
    // Filter out the Qt libraries. Note that depends.exe finds libs from optDirectory if we
    // are run the 2nd time (updating). We want to check against the Qt bin dir libraries
    const int start = result->size();
    const QRegExp filterRegExp(QStringLiteral("Qt5"), Qt::CaseInsensitive, QRegExp::FixedString);
    foreach (const QString &qtLib, dependentLibs.filter(filterRegExp)) {
        const QString path = normalizeFileName(qtBinDir + QLatin1Char('/') + QFileInfo(qtLib).fileName());
        if (!result->contains(path))
            result->append(path);
    }
    const int end = result->size();
    if (directDependencyCount)
        *directDependencyCount = end - start;
    // Recurse
    for (int i = start; i < end; ++i)
        if (!findDependentQtLibraries(qtBinDir, result->at(i), platform, errorMessage, result, 0, 0, 0, recursionDepth + 1))
            return false;
    return true;
}

// Base class to filter debug/release Windows DLLs for functions to be passed to updateFile().
// Tries to pre-filter by namefilter and does check via PE.
class DllDirectoryFileEntryFunction {
public:
    explicit DllDirectoryFileEntryFunction(bool debug, const QString &prefix = QStringLiteral("*")) :
        m_nameFilter(QStringList(prefix  + (debug ? QStringLiteral("d.dll") : QStringLiteral(".dll")))),
        m_dllDebug(debug) {}

    QStringList operator()(const QDir &dir) const
    {
        QStringList result;
        QString errorMessage;
        foreach (const QString &dll, m_nameFilter(dir)) {
            const QString dllPath = dir.absoluteFilePath(dll);
            bool debugDll;
            if (readPeExecutable(dllPath, &errorMessage, 0, 0, &debugDll)) {
                if (debugDll == m_dllDebug) {
                    result.push_back(dll);
                }
            } else {
                std::fprintf(stderr, "Warning: Unable to read %s: %s",
                             qPrintable(QDir::toNativeSeparators(dllPath)), qPrintable(errorMessage));
            }
        }
        return result;
    }

private:
    const NameFilterFileEntryFunction m_nameFilter;
    const bool m_dllDebug;
};

// File entry filter function for updateFile() that returns a list of files for
// QML import trees: DLLs (matching debgug) and .qml/,js, etc.
class QmlDirectoryFileEntryFunction {
public:
    explicit QmlDirectoryFileEntryFunction(bool debug)
        : m_qmlNameFilter(QStringList() << QStringLiteral("*.js") << QStringLiteral("qmldir") << QStringLiteral("*.qmltypes") << QStringLiteral("*.png"))
        , m_dllFilter(debug)
    {}

    QStringList operator()(const QDir &dir) const { return m_dllFilter(dir) + m_qmlNameFilter(dir);  }

private:
    NameFilterFileEntryFunction m_qmlNameFilter;
    DllDirectoryFileEntryFunction m_dllFilter;
};

static inline unsigned qtModuleForPlugin(const QString &subDirName)
{
    if (subDirName == QLatin1String("accessible") || subDirName == QLatin1String("iconengines")
        || subDirName == QLatin1String("imageformats") || subDirName == QLatin1String("platforms")) {
        return QtGuiModule;
    }
    if (subDirName == QLatin1String("bearer"))
        return QtNetworkModule;
    if (subDirName == QLatin1String("sqldrivers"))
        return QtSqlModule;
    if (subDirName == QLatin1String("mediaservice") || subDirName == QLatin1String("playlistformats"))
        return QtMultimediaModule;
    if (subDirName == QLatin1String("printsupport"))
        return QtPrintSupportModule;
    if (subDirName == QLatin1String("qmltooling"))
        return QtDeclarativeModule | QtQuickModule;
    return 0; // "designer"
}

QStringList findQtPlugins(unsigned usedQtModules,
                          const QString qtPluginsDirName,
                          bool debug, Platform platform,
                          QString *platformPlugin)
{
    if (qtPluginsDirName.isEmpty())
        return QStringList();
    QDir pluginsDir(qtPluginsDirName);
    QStringList result;
    foreach (const QString &subDirName, pluginsDir.entryList(QStringList(QLatin1String("*")), QDir::Dirs | QDir::NoDotAndDotDot)) {
        const unsigned module = qtModuleForPlugin(subDirName);
        if (module & usedQtModules) {
            const QString subDirPath = qtPluginsDirName + QLatin1Char('/') + subDirName;
            QDir subDir(subDirPath);
            // Filter for platform or any.
            QString filter;
            const bool isPlatformPlugin = subDirName == QLatin1String("platforms");
            if (isPlatformPlugin) {
                switch (platform) {
                case Windows:
                    filter = QStringLiteral("qwindows");
                    break;
                case WinRt:
                    filter = QStringLiteral("qwinrt");
                    break;
                case Unix:
                    filter = QStringLiteral("libqxcb");
                    break;
                case UnknownPlatform:
                    break;
                }
            } else {
                filter  = QLatin1String("*");
            }
            const QStringList plugins = platform == Unix ?
                        NameFilterFileEntryFunction(QStringList(filter + QStringLiteral(".so")))(subDir) :
                        DllDirectoryFileEntryFunction(debug, filter)(subDir);
            foreach (const QString &plugin, plugins) {
                const QString pluginPath = subDir.absoluteFilePath(plugin);
                if (isPlatformPlugin)
                    *platformPlugin = pluginPath;
                result.push_back(pluginPath);
            } // for filter
        } // type matches
    } // for plugin folder
    return result;
}

static unsigned qtModule(const QString &module)
{
    const size_t qtModulesCount = sizeof(qtModuleEntries)/sizeof(QtModuleEntry);
    for (size_t i = 0; i < qtModulesCount; ++i)
        if (module.contains(QLatin1String(qtModuleEntries[i].libraryName), Qt::CaseInsensitive))
            return qtModuleEntries[i].module;
    return 0;
}

static QStringList translationNameFilters(unsigned modules, const QString &prefix)
{
    QStringList result;
    const size_t qtModulesCount = sizeof(qtModuleEntries)/sizeof(QtModuleEntry);
    for (size_t i = 0; i < qtModulesCount; ++i) {
        if ((qtModuleEntries[i].module & modules) && qtModuleEntries[i].translation) {
            const QString name = QLatin1String(qtModuleEntries[i].translation) +
                                 QLatin1Char('_') +  prefix + QStringLiteral(".qm");
            if (!result.contains(name))
                result.push_back(name);
        }
    }
    return result;
}

static bool deployTranslations(const QString &sourcePath, unsigned usedQtModules,
                               const QString &target, QString *errorMessage)
{
    // Find available languages prefixes by checking on qtbase.
    QStringList prefixes;
    QDir sourceDir(sourcePath);
    const QStringList qmFilter = QStringList(QStringLiteral("qtbase_*.qm"));
    foreach (QString qmFile, sourceDir.entryList(qmFilter)) {
       qmFile.chop(3);
       qmFile.remove(0, 7);
       prefixes.push_back(qmFile);
    }
    if (prefixes.isEmpty()) {
        fprintf(stderr, "Warning: Could not find any translations in %s (developer build?)\n.",
                qPrintable(QDir::toNativeSeparators(sourcePath)));
        return true;
    }
    // Run lconvert to concatenate all files into a single named "qt_<prefix>.qm" in the application folder
    // Use QT_INSTALL_TRANSLATIONS as working directory to keep the command line short.
    const QString absoluteTarget = QFileInfo(target).absoluteFilePath();
    const QString binary = QStringLiteral("lconvert");
    QStringList arguments;
    foreach (const QString &prefix, prefixes) {
        const QString targetFile = QStringLiteral("qt_") + prefix + QStringLiteral(".qm");
        arguments.append(QStringLiteral("-o"));
        arguments.append(QDir::toNativeSeparators(absoluteTarget + QLatin1Char('/') + targetFile));
        foreach (const QString &qmFile, sourceDir.entryList(translationNameFilters(usedQtModules, prefix)))
            arguments.append(qmFile);
        if (optVerboseLevel)
            std::printf("Creating %s...\n", qPrintable(targetFile));
        unsigned long exitCode;
        if (!runProcess(binary, arguments, sourcePath, &exitCode, 0, 0, errorMessage) || exitCode)
            return false;
    } // for prefixes.
    return true;
}

struct DeployResult
{
    DeployResult() : success(false), directlyUsedQtLibraries(0), usedQtLibraries(0), deployedQtLibraries(0) {}
    operator bool() const { return success; }

    bool success;
    unsigned directlyUsedQtLibraries;
    unsigned usedQtLibraries;
    unsigned deployedQtLibraries;
};

static QString libraryPath(const QString &libraryLocation, const char *name, Platform platform, bool debug)
{
    QString result = libraryLocation + QLatin1Char('/');
    switch (platform) {
    case Windows:
    case WinRt:
        result += QLatin1String(name);
        if (debug)
            result += QLatin1Char('d');
        result += QStringLiteral(".dll");
        break;
    case Unix:
        result += QStringLiteral("lib");
        result += QLatin1String(name);
        result += QStringLiteral(".so");
        break;
    default:
        break;
    }
    return result;
}

static DeployResult deploy(const Options &options,
                           const QMap<QString, QString> &qmakeVariables,
                           QString *errorMessage)
{
    DeployResult result;

    const QChar slash = QLatin1Char('/');

    const QString qtBinDir = qmakeVariables.value(QStringLiteral("QT_INSTALL_BINS"));
    const QString libraryLocation = options.platform == Unix ? qmakeVariables.value(QStringLiteral("QT_INSTALL_LIBS")) : qtBinDir;

    if (optVerboseLevel > 1)
        std::printf("Qt binaries in %s\n", qPrintable(QDir::toNativeSeparators(qtBinDir)));

    QStringList dependentQtLibs;
    bool isDebug;
    unsigned wordSize;
    int directDependencyCount;
    if (!findDependentQtLibraries(libraryLocation, options.binary, options.platform, errorMessage, &dependentQtLibs, &wordSize, &isDebug, &directDependencyCount))
        return result;

    if (optVerboseLevel) {
        std::printf("%s: %ubit, %s executable.\n", qPrintable(QDir::toNativeSeparators(options.binary)),
                    wordSize, isDebug ? "debug" : "release");
    }

    if (dependentQtLibs.isEmpty()) {
        *errorMessage = QDir::toNativeSeparators(options.binary) +  QStringLiteral(" does not seem to be a Qt executable.");
        return result;
    }

    // Some Windows-specific checks in QtCore: ICU
    if (options.platform  == Windows || options.platform == WinRt)  {
        const QStringList qt5Core = dependentQtLibs.filter(QStringLiteral("Qt5Core"), Qt::CaseInsensitive);
        if (!qt5Core.isEmpty()) {
            QStringList icuLibs = findDependentLibraries(qt5Core.front(), options.platform, errorMessage).filter(QStringLiteral("ICU"), Qt::CaseInsensitive);
            if (!icuLibs.isEmpty()) {
                // Find out the ICU version to add the data library icudtXX.dll, which does not show
                // as a dependency.
                QRegExp numberExpression(QStringLiteral("\\d+"));
                Q_ASSERT(numberExpression.isValid());
                const int index = numberExpression.indexIn(icuLibs.front());
                if (index >= 0)  {
                    const QString icuVersion = icuLibs.front().mid(index, numberExpression.matchedLength());
                    if (optVerboseLevel > 1)
                        std::printf("Adding ICU version %s\n", qPrintable(icuVersion));
                    icuLibs.push_back(QStringLiteral("icudt") + icuVersion + QStringLiteral(".dll"));
                }
                foreach (const QString &icuLib, icuLibs) {
                    const QString icuPath = findInPath(icuLib);
                    if (icuPath.isEmpty()) {
                        *errorMessage = QStringLiteral("Unable to locate ICU library ") + icuLib;
                        return result;
                    }
                    dependentQtLibs.push_back(icuPath);
                } // foreach icuLib
            } // !icuLibs.isEmpty()
        } // Qt5Core
    } // Windows

    // Find the plugins and check whether ANGLE, D3D are required on the platform plugin.
    QString platformPlugin;
    // Sort apart Qt 5 libraries in the ones that are represented by the
    // QtModule enumeration (and thus controlled by flags) and others.
    QStringList deployedQtLibraries;
    for (int i = 0 ; i < dependentQtLibs.size(); ++i)  {
        if (const unsigned qtm = qtModule(dependentQtLibs.at(i))) {
            result.usedQtLibraries |= qtm;
            if (i < directDependencyCount)
                result.directlyUsedQtLibraries |= qtm;
        } else {
            deployedQtLibraries.push_back(dependentQtLibs.at(i)); // Not represented by flag.
        }
    }
    result.deployedQtLibraries = (result.usedQtLibraries | options.additionalLibraries) & ~options.disabledLibraries;
    // Apply options flags and re-add library names.
    const size_t qtModulesCount = sizeof(qtModuleEntries)/sizeof(QtModuleEntry);
    for (size_t i = 0; i < qtModulesCount; ++i)
        if (result.deployedQtLibraries & qtModuleEntries[i].module)
            deployedQtLibraries.push_back(libraryPath(libraryLocation, qtModuleEntries[i].libraryName, options.platform, isDebug));

    if (optVerboseLevel >= 1) {
        std::printf("Direct dependencies: %s\nAll dependencies   : %s\nTo be deployed     : %s\n",
                    formatQtModules(result.directlyUsedQtLibraries).constData(),
                    formatQtModules(result.usedQtLibraries).constData(),
                    formatQtModules(result.deployedQtLibraries).constData());
    }

    const QStringList plugins = findQtPlugins(result.deployedQtLibraries, qmakeVariables.value(QStringLiteral("QT_INSTALL_PLUGINS")),
                                              isDebug, options.platform, &platformPlugin);
    if (optVerboseLevel > 1)
        std::printf("Plugins: %s\n", qPrintable(plugins.join(QLatin1Char(','))));

    if (plugins.isEmpty())
        return result;

    if (platformPlugin.isEmpty()) {
        *errorMessage =QStringLiteral("Unable to find the platform plugin.");
        return result;
    }

    // Check for ANGLE on the platform plugin.
    if (options.platform  == Windows || options.platform == WinRt)  {
        const QStringList platformPluginLibraries = findDependentLibraries(platformPlugin, options.platform, errorMessage);
        const QStringList libEgl = platformPluginLibraries.filter(QStringLiteral("libegl"), Qt::CaseInsensitive);
        if (!libEgl.isEmpty()) {
            const QString libEglFullPath = qtBinDir + slash + QFileInfo(libEgl.front()).fileName();
            deployedQtLibraries.push_back(libEglFullPath);
            const QStringList libGLESv2 = findDependentLibraries(libEglFullPath, options.platform, errorMessage).filter(QStringLiteral("libGLESv2"), Qt::CaseInsensitive);
            if (!libGLESv2.isEmpty()) {
                const QString libGLESv2FullPath = qtBinDir + slash + QFileInfo(libGLESv2.front()).fileName();
                deployedQtLibraries.push_back(libGLESv2FullPath);
            }
            // Find the D3d Compiler matching the D3D library.
            const QString d3dCompiler = findD3dCompiler();
            if (d3dCompiler.isEmpty()) {
                std::fprintf(stderr, "Warning: Cannot find any version of the d3dcompiler DLL.\n");
            } else {
                deployedQtLibraries.push_back(d3dCompiler);
            }
        } // !libEgl.isEmpty()
    } // Windows

    // Update libraries
    if (options.libraries) {
        QString targetPath = options.directory;
        if (!options.libraryDirectory.isEmpty()) {
            if (!createDirectory(options.libraryDirectory, errorMessage))
                return result;
            targetPath = options.libraryDirectory;
        }
        foreach (const QString &qtLib, deployedQtLibraries) {
            if (!updateFile(qtLib, targetPath, errorMessage))
                return result;
            if (options.json)
                options.json->addFile(qtLib, targetPath);
        }
    } // optLibraries

    // Update plugins
    if (options.plugins) {
        QDir dir(options.directory);
        foreach (const QString &plugin, plugins) {
            const QString targetDirName = plugin.section(slash, -2, -2);
            if (!dir.exists(targetDirName)) {
                if (optVerboseLevel)
                    std::printf("Creating directory %s.\n", qPrintable(targetDirName));
                if (!dir.mkdir(targetDirName)) {
                    std::fprintf(stderr, "Cannot create %s.\n",  qPrintable(targetDirName));
                    *errorMessage = QStringLiteral("Cannot create ") + targetDirName +  QLatin1Char('.');
                    return result;
                }
            }
            const QString targetPath = options.directory + slash + targetDirName;
            if (!updateFile(plugin, targetPath, errorMessage))
                return result;
            if (options.json)
                options.json->addFile(plugin, targetPath);
        }
    } // optPlugins

    // Update Quick imports
    const bool usesQuick1 = result.deployedQtLibraries & QtDeclarativeModule;
    // Do not be fooled by QtWebKit.dll depending on Quick into always installing Quick imports
    // for WebKit1-applications. Check direct dependency only.
    const bool usesQuick2 = (result.directlyUsedQtLibraries & QtQuickModule)
                            || (options.additionalLibraries & QtQuickModule);
    if (options.quickImports && (usesQuick1 || usesQuick2)) {
        const QmlDirectoryFileEntryFunction qmlFileEntryFunction(isDebug);
        if (usesQuick2) {
            const QString quick2ImportPath = qmakeVariables.value(QStringLiteral("QT_INSTALL_QML"));
            QStringList quick2Imports;
            quick2Imports << QStringLiteral("QtQml") << QStringLiteral("QtQuick") << QStringLiteral("QtQuick.2");
            if (result.deployedQtLibraries & QtMultimediaModule)
                quick2Imports << QStringLiteral("QtMultimedia");
            if (result.deployedQtLibraries & QtSensorsModule)
                quick2Imports << QStringLiteral("QtSensors");
            if (result.deployedQtLibraries & QtWebKitModule)
                quick2Imports << QStringLiteral("QtWebKit");
            foreach (const QString &quick2Import, quick2Imports) {
                const QString sourceFile = quick2ImportPath + slash + quick2Import;
                if (!updateFile(sourceFile, qmlFileEntryFunction, options.directory, errorMessage))
                    return result;
                if (options.json)
                    options.json->addFile(sourceFile, options.directory);
            }
        } // Quick 2
        if (usesQuick1) {
            const QString quick1ImportPath = qmakeVariables.value(QStringLiteral("QT_INSTALL_IMPORTS"));
            QStringList quick1Imports(QStringLiteral("Qt"));
            if (result.deployedQtLibraries & QtWebKitModule)
                quick1Imports << QStringLiteral("QtWebKit");
            foreach (const QString &quick1Import, quick1Imports) {
                const QString sourceFile = quick1ImportPath + slash + quick1Import;
                if (!updateFile(sourceFile, qmlFileEntryFunction, options.directory, errorMessage))
                    return result;
                if (options.json)
                    options.json->addFile(sourceFile, options.directory);
            }
        } // Quick 1
    } // optQuickImports

    if (options.translations
        && !deployTranslations(qmakeVariables.value(QStringLiteral("QT_INSTALL_TRANSLATIONS")),
                               result.deployedQtLibraries, options.directory, errorMessage)) {
        return result;
    }

    result.success = true;
    return result;
}

static bool deployWebKit2(const QMap<QString, QString> &qmakeVariables,
                          const Options &sourceOptions, QString *errorMessage)
{
    // Copy the web process and its dependencies
    const QString webProcess = webProcessBinary(sourceOptions.platform);
    const QString webProcessSource = qmakeVariables.value(QStringLiteral("QT_INSTALL_LIBEXECS")) +
                                     QLatin1Char('/') + webProcess;
    if (!updateFile(webProcessSource, sourceOptions.directory, errorMessage))
        return false;
    if (sourceOptions.json)
        sourceOptions.json->addFile(webProcessSource, sourceOptions.directory);
    Options options(sourceOptions);
    options.binary = options.directory + QLatin1Char('/') + webProcess;
    options.quickImports = false;
    options.translations = false;
    return deploy(options, qmakeVariables, errorMessage);
}

int main(int argc, char **argv)
{
    QCoreApplication a(argc, argv);


    Options options;
    QString errorMessage;
    const QMap<QString, QString> qmakeVariables = queryQMakeAll(&errorMessage);
    const QString xSpec = qmakeVariables.value(QStringLiteral("QMAKE_XSPEC"));
    options.platform = platformFromMkSpec(xSpec);

    if (!parseArguments(QCoreApplication::arguments(), &options) || optHelp) {
        std::fprintf(stderr, "\nwindeployqt based on Qt %s\n\n%s", QT_VERSION_STR, usage().constData());
        return optHelp ? 0 : 1;
    }

    if (qmakeVariables.isEmpty() || xSpec.isEmpty() || !qmakeVariables.contains(QStringLiteral("QT_INSTALL_BINS"))) {
        std::fprintf(stderr, "Unable to query qmake: %s\n", qPrintable(errorMessage));
        return 1;
    }

    if (options.platform == UnknownPlatform) {
        std::fprintf(stderr, "Unsupported platform %s\n", qPrintable(xSpec));
        return 1;
    }

    if (optWebKit2)
        options.additionalLibraries |= QtWebKitModule;


    const DeployResult result = deploy(options, qmakeVariables, &errorMessage);
    if (!result) {
        std::fprintf(stderr, "%s\n", qPrintable(errorMessage));
        return 1;
    }

    if ((optWebKit2 != -1)
        && (optWebKit2 == 1
            || ((result.deployedQtLibraries & QtWebKitModule)
                && (result.directlyUsedQtLibraries & QtQuickModule)))) {
        if (optVerboseLevel)
            std::printf("Deploying: %s...\n", webProcessC);
        if (!deployWebKit2(qmakeVariables, options, &errorMessage)) {
            std::fprintf(stderr, "%s\n", qPrintable(errorMessage));
            return 1;
        }
    }

    if (options.json) {
        std::fputs(options.json->toJson().constData(), stdout);
        delete options.json;
        options.json = 0;
    }

    return 0;
}