summaryrefslogtreecommitdiffstats
path: root/src/app/main.cpp
blob: e614b234513d62bc9d0b70e8a2adc251affafe7e (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
/****************************************************************************
 **
 ** Copyright (C) 2008-2012 Nokia Corporation and/or its subsidiary(-ies).
 ** Contact: Nokia Corporation (info@qt.nokia.com)
 **
 ** This file is part of the jom project on Trolltech Labs.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License version 2.0 or 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 GNU
 ** General Public Licensing requirements will be met:
 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
 ** http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you are unsure which license is appropriate for your use, please
 ** contact the sales department at qt-sales@nokia.com.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/
#include "application.h"
#include <options.h>
#include <parser.h>
#include <preprocessor.h>
#include <targetexecutor.h>
#include <exception.h>
#include <makefilefactory.h>

#include <QDebug>
#include <QDir>
#include <QTextStream>
#include <QProcess>
#include <QTextCodec>

#include <windows.h>
#include <Tlhelp32.h>

#ifndef __in
#define __in
#endif

using namespace NMakeFile;

const int nVersionMajor = 1;
const int nVersionMinor = 0;
const int nVersionPatch = 12;

static void showLogo()
{
    fprintf(stderr, "\njom %d.%d.%d - empower your cores\n\n",
        nVersionMajor, nVersionMinor, nVersionPatch);
    fflush(stderr);
}

static void showUsage()
{
    printf("Usage: jom @commandfile\n"
           "       jom [options] [/f makefile] [macro definitions] [targets]\n\n"
           "nmake compatible options:\n"
           "/A build all targets\n"
           "/D display build information\n"
           "/E override environment variable macros\n"
           "/F <filename> use the specified makefile\n"
           "/G display included makefiles\n"
           "/H show help\n"
           "/I ignore all exit codes\n"
           "/K keep going - build unrelated targets on error\n"
           "/N dry run - just print commands\n"
           "/NOLOGO do not print logo\n"
           "/P print makefile info\n"
           "/R ignore predefined rules and macros\n"
           "/S silent mode\n"
           "/L same as /NOLOGO\n"
           "/W print the working directory before and after other processing\n"
           "/X <filename> write stderr to file.\n"
           "/Y disable batch mode inference rules\n\n"
           "jom only options:\n"
           "/DUMPGRAPH show the generated dependency graph\n"
           "/DUMPGRAPHDOT dump dependency graph in dot format\n"
           "/J <n> use up to n processes in parallel\n"
           "/KEEPTEMPFILES keep all temporary files\n"
           "/VERSION print version and exit\n");
}

static TargetExecutor* g_pTargetExecutor = 0;

BOOL WINAPI ConsoleCtrlHandlerRoutine(__in  DWORD /*dwCtrlType*/)
{
    fprintf(stderr, "jom terminated by user (pid=%u)\n", QCoreApplication::applicationPid());
    fflush(stderr);

    GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
    if (g_pTargetExecutor)
        g_pTargetExecutor->removeTempFiles();

    exit(2);
    return TRUE;
}

QStringList getCommandLineArguments()
{
    QStringList commandLineArguments = qApp->arguments().mid(1);
    QByteArray makeFlags = qgetenv("MAKEFLAGS");
    if (!makeFlags.isEmpty())
        commandLineArguments.prepend(QString::fromLatin1('/' + makeFlags));
    return commandLineArguments;
}

int main(int argc, char* argv[])
{
    SetConsoleCtrlHandler(&ConsoleCtrlHandlerRoutine, TRUE);
    Application app(argc, argv);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("IBM 850"));

    MakefileFactory mf;
    mf.setEnvironment(QProcess::systemEnvironment());
    Options* options = 0;
    if (!mf.apply(getCommandLineArguments(), &options)) {
        switch (mf.errorType()) {
        case MakefileFactory::CommandLineError:
            showUsage();
            return 128;
        case MakefileFactory::ParserError:
        case MakefileFactory::IOError:
            fprintf(stderr, "Error: ");
            fprintf(stderr, qPrintable(mf.errorString()));
            fprintf(stderr, "\n");
            return 2;
        }
    }

    if (options->showUsageAndExit) {
        if (options->showLogo)
            showLogo();
        showUsage();
        return 0;
    } else if (options->showVersionAndExit) {
        printf("jom version %d.%d.%d\n", nVersionMajor, nVersionMinor, nVersionPatch);
        return 0;
    }

    if (options->showLogo && !app.isSubJOM())
        showLogo();

    Makefile* mkfile = mf.makefile();
    if (options->displayMakeInformation) {
        printf("MACROS:\n\n");
        mkfile->macroTable()->dump();
        printf("\nINFERENCE RULES:\n\n");
        mkfile->dumpInferenceRules();
        printf("\nTARGETS:\n\n");
        mkfile->dumpTargets();
    }

    // ### HACK: pass the modified MAKEFLAGS variable to our environment.
    if (g_options.isMaxNumberOfJobsSet) {
        bool found = false;
        QStringList environment = mkfile->macroTable()->environment();
        for (QStringList::iterator it = environment.begin(); it != environment.end(); ++it) {
            QString &str = *it;
            if (str.toUpper().startsWith(QLatin1String("MAKEFLAGS"))) {
                str = QLatin1String("MAKEFLAGS=") + mkfile->macroTable()->macroValue(QLatin1String("MAKEFLAGS"));
                found = true;
                break;
            }
        }
        if (!found)
            environment += QLatin1String("MAKEFLAGS=") + mkfile->macroTable()->macroValue(QLatin1String("MAKEFLAGS"));
        MacroTable *mt = const_cast<MacroTable *>(mkfile->macroTable());
        mt->setEnvironment(environment);
    }

    if (options->printWorkingDir) {
        printf("jom: Entering directory '%s\n", qPrintable(QDir::toNativeSeparators(QDir::currentPath())));
        fflush(stdout);
    }

    TargetExecutor executor(mkfile->macroTable()->environment());
    QObject::connect(&executor, SIGNAL(finished(int)), &app, SLOT(exit(int)));
    g_pTargetExecutor = &executor;
    try {
        executor.apply(mkfile, mf.activeTargets());
    }
    catch (Exception &e) {
        fprintf(stderr, "Error in executor: %s\n", qPrintable(e.message()));
    }

    QMetaObject::invokeMethod(&executor, "startProcesses", Qt::QueuedConnection);
    int result = app.exec();
    g_pTargetExecutor = 0;
    if (options->printWorkingDir) {
        printf("jom: Leaving directory '%s'\n", qPrintable(QDir::toNativeSeparators(QDir::currentPath())));
        fflush(stdout);
    }
    delete mkfile;
    return result;
}