summaryrefslogtreecommitdiffstats
path: root/src/tools/nacldeployqt/main.cpp
blob: cdc982e8f814bee442efbcfc7ebcf13725a3dfac (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
#include <qdebug.h>
#include <qglobal.h>
#include <qcoreapplication.h>
#include <qcommandlineparser.h>
#include <qlibraryinfo.h>
#include <qdir.h>
#include <qhash.h>
#include <qset.h>

#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

// QtNaclDeployer Usage: set the public options and call deploy();
class QtNaclDeployer
{
public:
    QString tmplate;
    bool quickImports;
    bool isApp;
    QString outDir;
    bool print;
    bool run;
    bool debug;
    bool compress;
    bool testlibMode;
    QString binary;
    QString qtBaseDir;

    int deploy();

private:
    QByteArray mainHtmlFileName;
    QHash<QByteArray, QByteArray> templateReplacements;
    enum DeploymentType { NaCl, PNaCl, Emscripten };
    const char *deploymentTypeNames[3] = { "NaCl", "PNaCl", "Emscripten"};
    DeploymentType deploymentType;

    QString appName;
    QString nmf;
    QSet<QByteArray> permissions;
    QString stdoutCaptureFile;
    QString stderrCaptureFile;


    QString findPNaClTool(const QString &sdkRoot, const QString &toolName);
    QList<QByteArray> quote(const QList<QByteArray> &list);
    void runCommand(const QString &command);
    bool copyRecursively(const QString &srcFilePath, const QString &tgtFilePath);
    QByteArray instantiateTemplate(const QByteArray &tmplate);
    void instantiateWriteTemplate(const QByteArray &tmplate, const QString &filePath);
};

int QtNaclDeployer::deploy()
{
    mainHtmlFileName = "index.html";
    // Create the default Chrome App permission set.
    permissions = { "clipboardRead", "clipboardWrite" };
    stdoutCaptureFile = "qt_stdout";
    stderrCaptureFile = "qt_stderr";

    if (testlibMode && !run) // "--testlib" implies "--run"
        run = true;

    if (run && debug) // "--debug" takes priority over "--run"
        run = false;

    // outDir should end with "/" and exist on disk.
    if (!outDir.endsWith("/"))
        outDir.append("/");
    QDir().mkpath(outDir);

    // Guess deployment type based on filename extension.
    if (binary.endsWith(".bc"))
        deploymentType = PNaCl;
    else if (binary.endsWith(".js"))
        deploymentType = Emscripten;
    else
        deploymentType = NaCl;

    QString appName = binary;
    appName.replace(".nexe", "");
    appName.replace(".bc", "");
    appName.replace(".js", "");
    nmf = appName + ".nmf";

    if (!QFile(binary).exists()) {
        qDebug() << "File not found" << binary;
        return 0;
    }

    if (!quickImports && QFile::exists(binary + "_qml_plugin_import.cpp")) {
        qDebug() << "Note: qml_plugin_import.cpp file found. Auto-enabling --quick option.";
        quickImports = true;
    }

    // Set up template replacements. There is one global map
    // for all templates.
    const char *apptypes[3] = { "application/x-nacl", "application/x-pnacl", "emscripten"};
    const char *extension[3] = { ".nmf", ".nmf", ".js" };

    templateReplacements["%MAINHTML%"] = mainHtmlFileName;
    templateReplacements["%APPNAME%"] = appName.toUtf8();
    templateReplacements["%APPSOURCE%"] = appName.toUtf8() + extension[deploymentType];
    templateReplacements["%APPTYPE%"] = apptypes[deploymentType];
    templateReplacements["%CHROMEAPP%"] = isApp ? "true;" : "false;";
    templateReplacements["%PERMISSIONS%"] = quote(permissions.toList()).join(", ");

    // Get the NaCl SDK root from environment
    QString naclSdkRoot = qgetenv("NACL_SDK_ROOT");
    if (naclSdkRoot.isEmpty()) {
        qDebug() << "Please set NACL_SDK_ROOT";
        qDebug() << "For example: /Users/foo/nacl_sdk/pepper_35";
        return 0;
    }

    // Get the location of the deployment scripts and tools in the nacl sdk:
    QString createNmf = naclSdkRoot + "/tools/create_nmf.py";
    if (!QFile(createNmf).exists()) {
        qDebug() << "create_nmf.py not found at" << createNmf;
        return 0;
    }
    QString createHtml = naclSdkRoot + "/tools/create_html.py";
    if (!QFile(createHtml).exists()) {
        qDebug() << "create_html.py not found at" << createHtml;
        return 0;
    }

    QString pnaclFinalize = findPNaClTool(naclSdkRoot, "pnacl-finalize");
    if (deploymentType == PNaCl && !QFile(pnaclFinalize).exists()) {
        qDebug() << "pnacl-finalize not found at" << pnaclFinalize;
        return 0;
    }

    QString pnaclCompress = findPNaClTool(naclSdkRoot, "pnacl-compress");
    if (deploymentType == PNaCl && compress && !QFile(pnaclCompress).exists()) {
        qDebug() << "pnacl-compress not found at" << pnaclCompress;
        return 0;
    }

    QString qtLibDir = qtBaseDir + "/lib";
    QString qtBinDir = qtBaseDir + "/bin";
    QString qtImportsDir = qtBaseDir + "/qml";
    QString nmfFileName = appName + ".nmf";
    QString nmfFilePath = outDir + nmfFileName;

    // Print info about the environment; except in testlib mode, where we only
    // want testlib output.
    if (!testlibMode) {
        qDebug() << " ";
        qDebug() << "Deploying" << binary;
        qDebug() << "Build Type:" << deploymentTypeNames[deploymentType];
        if (deploymentType != Emscripten) {           // (Emscripten builds are not actually using
            qDebug() << "Using SDK" << naclSdkRoot;   // the SDK at deploy time)
        }
        qDebug() << "Qt libs in" << qtLibDir;
        qDebug() << "Output directory:" << QDir(outDir).canonicalPath();
        qDebug() << " ";
    }

    // Deply Qt Quick imports
    if (quickImports) {
        // TODO: At some pont use qmlimportscanner to select imports in use.
        // For now simply deploy all imports.
        QStringList imports = QDir(qtImportsDir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);

        QString targetBase = outDir + "qml";
        QDir().mkpath(targetBase);

        foreach (const QString &import, imports) {
            QString source = qtImportsDir + "/" + import;
            QString target = targetBase + "/" + import;

            // TODO: Skip the binaries for static builds; they will be built into the main nexe
            copyRecursively(source, target);
        }
    }

    QString finalBinary = binary;

    // On PNaCl, the output from "make" is a bitcode .bc file. Create
    // a .pexe suitable for distribution using "pnacl-finalize".
    if (deploymentType == PNaCl) {
        qDebug() << "pnacl-finalize" << binary;
        finalBinary.replace(".bc", ".pexe");
        QString finalizeCommand = pnaclFinalize + " -o " + outDir + finalBinary + " " + binary;
        runCommand(finalizeCommand);
    } else {
        QFile::copy(binary, outDir + finalBinary);
    }

    const QString finalBinaryPath = outDir + finalBinary;

    // Compress PNaCl binary using panacl-compress.
    if (deploymentType == PNaCl && compress) {
        qDebug() << "pnacl-compress" << finalBinaryPath;
        QString compressCommand = pnaclCompress + " " + finalBinaryPath;
        runCommand(compressCommand.toLatin1().constData());
    }

    // create the .nmf manifest file
    if (deploymentType != Emscripten) {
        QString nmfCommand = QStringLiteral("python ") + createNmf
                + " -o " + nmfFilePath
                + " -L " + qtLibDir           // Add Qt libs search payh
                + " -s  . "                   // copy dependencies
//                + " --debug-libs"
                + " " + finalBinaryPath
                + " " + (debug ? binary : QString("")); // deploy .bc when debugging (adds a "pnacl-debug" section to the nmf)

        runCommand(nmfCommand.toLatin1().constData());
    }

    // select template. See the template_*.cpp files.
    if (tmplate.isEmpty())
        tmplate = "windowed";
    QByteArray html;
    if (tmplate== "fullscreen") {
        extern const char * templateFullscreen;
        html = QByteArray(templateFullscreen);
    } else if (tmplate== "debug") {
        extern const char * templateDebug;
        html = QByteArray(templateDebug);
    } else if (tmplate== "windowed") {
        extern const char * templateWindowed;
        html = QByteArray(templateWindowed);
    }

    // Write the main html file.
    instantiateWriteTemplate(html, outDir + mainHtmlFileName);

    // Write qtloader.js
    extern const char *templateQtLoader;
    instantiateWriteTemplate(QByteArray(templateQtLoader),  outDir + "qtloader.js");

    // Create Chrome App support files.
    if (isApp) {
        // Write manifest.json
        extern const char *templateAppManifest;
        instantiateWriteTemplate(QByteArray(templateAppManifest), outDir + "manifest.json");

        // Write background.js
        extern const char *templateBackgroundJs;
        instantiateWriteTemplate(QByteArray(templateBackgroundJs), outDir + "background.js");
    }

    // NOTE: At this point deployment is done. The following are
    // development aides for running or debugging the app.

    // Clean up testlib mode files from previous runs
    if (testlibMode) {
        QFile::remove(stdoutCaptureFile);
        QFile::remove(stderrCaptureFile);
    }

    // Find the debugger, print startup instructions
    QString gdb = naclSdkRoot + "/toolchain/mac_x86_glibc/bin/i686-nacl-gdb";
    if (!QFile(gdb).exists())
        gdb = naclSdkRoot + "/toolchain/linux_x86_glibc/bin/i686-nacl-gdb";
    QString nmfPath = QDir(nmfFilePath).canonicalPath();
    QString gdbOptions = " -eval-command='nacl-manifest " + nmfPath + "'";

    if (print) {
        qDebug() << "debugger";
        qDebug() << qPrintable("  " + gdb);
        qDebug() << qPrintable("    target remote localhost:4014");
        qDebug() << qPrintable("    nacl-manifest " + nmfFilePath);
        qDebug() << "";
    }

    // Find chrome, print startup instructions
#ifdef Q_OS_LINUX
    // TODO: look up chrome binary location.
    QString chromeExecutable = "/usr/bin/google-chrome";
#else
    QString chromeExecutable = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome";
#endif
    QString chromeNormalOptions = " --disable-cache --no-default-browser-check";
    QString chromeDebugOptions = chromeNormalOptions + " --enable-nacl-debug --no-sandbox";
    QString chromeOpenAppOptions = " --load-and-launch-app=" + outDir;
    QString chromeOpenWindowOptions = " --incognito --new-window \"http://localhost:8000\"";
    QString chromeOpenOptions = isApp ? chromeOpenAppOptions : chromeOpenWindowOptions;
    QString chromeRedirectOptions = testlibMode ?
        QString(" 1>") + stdoutCaptureFile + QString(" 2>") + stderrCaptureFile : QStringLiteral(" ");

    if (print) {
        qDebug() << "chrome:";
        qDebug() << qPrintable("  " + chromeExecutable + chromeNormalOptions + chromeOpenOptions);
        qDebug() << qPrintable("chrome (wait for debugger)");
        qDebug() << qPrintable("  " + chromeExecutable + chromeDebugOptions + chromeOpenOptions);
        qDebug() << "";
    }

    // Start Chrome with correct options
    if (run)
        runCommand(chromeExecutable + chromeNormalOptions + chromeOpenOptions + chromeRedirectOptions + " &");
    else if (debug)
        runCommand(chromeExecutable + chromeDebugOptions + chromeOpenOptions + chromeRedirectOptions + " &");

    // Start the debugger
    if (debug) {
        // print help string, then lanch gdb.
        QString gdbCommand = "echo Connect to Chrome: target remote localhost:4014;\n\n"
                           + gdb + gdbOptions;
        // tell osascript to tell Terminal to tell gdb to debug the App.
        QString terminal = R"STR(osascript -e 'tell application "Terminal" to do script "'"SCRIPT"'" ')STR";
        terminal.replace("SCRIPT", gdbCommand);
        runCommand(terminal);
    }

    // Start a HTTP server and serve the app.
    QString serverRedirectOptions = testlibMode ? "> /dev/null 2>&1 &" : " ";
    if (run || debug) {
        runCommand("python -m SimpleHTTPServer " + serverRedirectOptions);
    }

    if (testlibMode) {
        // testlibMode: At this point the test is starting up / running with output
        // redirected to stdoutCaptureFile. Poll the file at regular intervals to
        // check for new lines. Echo them to standard out. Keep doing this
        // until test completion, which is tested for by looking for a line like
        // ********* Finished testing of tst_Foo *********
        int skipLines = 0;
        while (true) {
            usleep(500 * 1000);
            QFile out(stdoutCaptureFile);
            out.open(QIODevice::ReadOnly);

            // read and discard already printed lines;
            for (int i = 0; i < skipLines; ++i)
                out.readLine();

            // read and print new lines
            QByteArray line;
            do {
                line = out.readLine().trimmed();
                if (!line.isEmpty()) {
                    printf("%s\n", line.constData());
                    ++skipLines;
                }
                if (line.contains("********* Finished testing of")) {
                    // DONE: Kill process group: nacldeployqt, Chrome, and the Python server.
                    QFile::remove(stdoutCaptureFile);
                    QFile::remove(stderrCaptureFile);
                    kill(0, 9);
                }
            } while (!line.isEmpty());

            // check Chrome stderr for Qt process crash/exit.
            QFile err(stderrCaptureFile);
            err.open(QIODevice::ReadOnly);
            QByteArray contents = err.readAll();
            if (contents.contains("NaCl process exited with status")
                || contents.contains("NaCl untrusted code called _exit")) {
                QFile::remove(stdoutCaptureFile);
                QFile::remove(stderrCaptureFile);
                kill(0, 9);
            }
        } // while (true)
    } // testlibMode

    return 0;
}

// Look up executable in the mac and linux bin dirs.
QString QtNaclDeployer::findPNaClTool(const QString &sdkRoot, const QString &toolName)
{
    QString pnaclFinalize = sdkRoot + "/toolchain/mac_pnacl/bin/" + toolName;
    if (!QFile(pnaclFinalize).exists())
        pnaclFinalize = sdkRoot + "/toolchain/linux_pnacl/bin/" + toolName;
    return pnaclFinalize;
}

QList<QByteArray> QtNaclDeployer::quote(const QList<QByteArray> &list)
{
    QList<QByteArray> result;
    for (const QByteArray &item : list)
        result.append('"' + item + '"');
    return result;
}

void QtNaclDeployer::runCommand(const QString &command)
{
    QByteArray c = command.toLatin1();
    // qDebug() << "RUN" << c;

    // This is a host tool, which means no QProcess. Use 'system':
    int r = system(c.constData());
    Q_UNUSED(r);
}

bool QtNaclDeployer::copyRecursively(const QString &srcFilePath,
                                            const QString &tgtFilePath)
{
    QFileInfo srcFileInfo(srcFilePath);
    if (srcFileInfo.isDir()) {
        QDir targetDir(tgtFilePath);
        targetDir.cdUp();
        targetDir.mkdir(QFileInfo(tgtFilePath).fileName());
        if (!targetDir.exists())
            return false;
        QDir sourceDir(srcFilePath);
        QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
        foreach (const QString &fileName, fileNames) {
            const QString newSrcFilePath
                    = srcFilePath + QLatin1Char('/') + fileName;
            const QString newTgtFilePath
                    = tgtFilePath + QLatin1Char('/') + fileName;
            if (!copyRecursively(newSrcFilePath, newTgtFilePath))
                return false;
        }
    } else {
        if (!QFile::copy(srcFilePath, tgtFilePath))
            return false;
    }
    return true;
}


QByteArray QtNaclDeployer::instantiateTemplate(const QByteArray &tmplate)
{
    QByteArray instantiated = tmplate;
    int replacementCount;

    do {
        replacementCount = 0;
        for (auto it : templateReplacements.keys()) {
            if (!instantiated.contains(it))
                continue;
            ++replacementCount;
            instantiated.replace(it, templateReplacements[it]);
        }
    } while (replacementCount > 0);

    return instantiated;
}

void QtNaclDeployer::instantiateWriteTemplate(const QByteArray &tmplate, const QString &filePath)
{
    QByteArray fileContents = instantiateTemplate(tmplate);
    QFile outFile(filePath);
    outFile.open(QIODevice::WriteOnly|QIODevice::Truncate);
    outFile.write(fileContents);
}

int main(int argc, char **argv)
{
    // Command line handling
    QCoreApplication app(argc, argv);
    QCoreApplication::setApplicationName("nacldeployqt");
    QCommandLineParser parser;
    parser.addHelpOption();
    parser.addPositionalArgument("binary", "Application binary file (.nexe or .bc)");

    parser.addOption(QCommandLineOption(QStringList() << "t" << "template",
                        "Selects a html template. Can be either 'fullscreen' or 'debug'. "
                        "Omit this option to use the default nacl_sdk template, as created "
                        "by create_html.py", "template", ""));

    parser.addOption(QCommandLineOption(QStringList() << "q" << "quick", "Deploy Qt Quick imports"));
    parser.addOption(QCommandLineOption(QStringList() << "o" << "out", "Specify an output directory", "outDir", "."));

    parser.addOption(QCommandLineOption(QStringList() << "a" << "app", "Create Chrome App."));

    parser.addOption(QCommandLineOption(QStringList() << "p" << "print", "Print Chrome and debugging help."));
    parser.addOption(QCommandLineOption(QStringList() << "r" << "run", "Run the application in Chrome."));
    parser.addOption(QCommandLineOption(QStringList() << "d" << "debug", "Debug the application in Chrome [OS X only]."));
    parser.addOption(QCommandLineOption(QStringList() << "c" << "compress", "Compress the application binary with pnacl-compress [PNaCl only]."));
    parser.addOption(QCommandLineOption(QStringList() << "e" << "testlib", "Run in QTestLib mode"));

    parser.process(app);
    QStringList args = parser.positionalArguments();

    QtNaclDeployer deployer;
    deployer.tmplate = parser.value("template");
    deployer.quickImports = parser.isSet("quick");
    deployer.isApp = parser.isSet("app");
    deployer.outDir = parser.value("out");
    deployer.print = parser.isSet("print");
    deployer.run = parser.isSet("run");
    deployer.debug = parser.isSet("debug");
    deployer.compress = parser.isSet("compress");
    deployer.testlibMode = parser.isSet("testlib");

#ifdef Q_OS_LINUX
    if (deployer.debug) {
        qDebug() << "Error: --debug is currently supported on OS X only.";
	exit(1);
    }
#endif

    // Get target binary file name from command line, or find one
    // in the currrent directory
    QStringList binaries = QDir().entryList(QStringList() << "*.nexe" << "*.bc" << "*.js", QDir::Files);
    if (args.count() == 0) {
        if (binaries.count() == 0) {
            parser.showHelp(0);
            return 0;
        }
        deployer.binary = binaries.at(0);
    } else {
        deployer.binary = args.at(0);
    }

    // Find the path to the Qt install. Go via argv[0]:
    // path/to/qtbase/bin/nacldeployqt -> path/to/qtbase/
    QString nacldeployqtPath = argv[0];
    nacldeployqtPath.chop(QStringLiteral("nacldeployqt").length());
    deployer.qtBaseDir = QDir(nacldeployqtPath + QStringLiteral("../")).canonicalPath();

    return deployer.deploy();
}