gnumakeparser.cpp 18.5 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
Tobias Hunger's avatar
Tobias Hunger committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
Tobias Hunger's avatar
Tobias Hunger committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
Tobias Hunger's avatar
Tobias Hunger committed
30 31 32 33
**
**************************************************************************/

#include "gnumakeparser.h"
34 35

#include "projectexplorerconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
36 37 38 39 40 41 42
#include "taskwindow.h"

#include <QtCore/QDir>
#include <QtCore/QFile>

using namespace ProjectExplorer;

43
namespace {
Friedemann Kleint's avatar
Friedemann Kleint committed
44
    // optional full path, make executable name, optional exe extension, optional number in square brackets, colon space
Tobias Hunger's avatar
Tobias Hunger committed
45
    const char * const MAKE_PATTERN("^(([A-Za-z]:)?[/\\\\][^:]*[/\\\\])?(mingw(32|64)-|g)?make(.exe)?(\\[\\d+\\])?:\\s");
46 47
}

48
GnuMakeParser::GnuMakeParser() :
49 50
    m_suppressIssues(false),
    m_fatalErrorCount(0)
Tobias Hunger's avatar
Tobias Hunger committed
51
{
52
    setObjectName(QLatin1String("GnuMakeParser"));
53 54
    m_makeDir.setPattern(QLatin1String(MAKE_PATTERN) +
                         QLatin1String("(\\w+) directory .(.+).$"));
Tobias Hunger's avatar
Tobias Hunger committed
55
    m_makeDir.setMinimal(true);
Tobias Hunger's avatar
Tobias Hunger committed
56
    m_makeLine.setPattern(QLatin1String(MAKE_PATTERN) + QLatin1String("(\\*\\*\\*\\s)?(.*)$"));
57
    m_makeLine.setMinimal(true);
58 59
    m_makefileError.setPattern(QLatin1String("^(.*):(\\d+):\\s\\*\\*\\*\\s(.*)$"));
    m_makefileError.setMinimal(true);
Tobias Hunger's avatar
Tobias Hunger committed
60 61
}

62
void GnuMakeParser::setWorkingDirectory(const QString &workingDirectory)
63
{
64 65 66 67 68 69 70
    addDirectory(workingDirectory);
    IOutputParser::setWorkingDirectory(workingDirectory);
}

bool GnuMakeParser::hasFatalErrors() const
{
    return (m_fatalErrorCount > 0) || IOutputParser::hasFatalErrors();
71 72
}

Tobias Hunger's avatar
Tobias Hunger committed
73 74 75 76 77
void GnuMakeParser::stdOutput(const QString &line)
{
    QString lne = line.trimmed();

    if (m_makeDir.indexIn(lne) > -1) {
Tobias Hunger's avatar
Tobias Hunger committed
78 79
        if (m_makeDir.cap(7) == "Leaving")
            removeDirectory(m_makeDir.cap(8));
Tobias Hunger's avatar
Tobias Hunger committed
80
        else
Tobias Hunger's avatar
Tobias Hunger committed
81
            addDirectory(m_makeDir.cap(8));
82 83
        return;
    }
Tobias Hunger's avatar
Tobias Hunger committed
84 85 86 87 88 89 90 91

    IOutputParser::stdOutput(line);
}

void GnuMakeParser::stdError(const QString &line)
{
    QString lne = line.trimmed();

92
    if (m_makefileError.indexIn(lne) > -1) {
93
        ++m_fatalErrorCount;
Tobias Hunger's avatar
Tobias Hunger committed
94 95
        if (!m_suppressIssues) {
            m_suppressIssues = true;
96 97
            addTask(Task(Task::Error,
                         m_makefileError.cap(3),
98
                         QDir::fromNativeSeparators(m_makefileError.cap(1)),
99 100
                         m_makefileError.cap(2).toInt(),
                         Constants::TASK_CATEGORY_BUILDSYSTEM));
101
        }
Tobias Hunger's avatar
Tobias Hunger committed
102 103
        return;
    }
Tobias Hunger's avatar
Tobias Hunger committed
104
    if (m_makeLine.indexIn(lne) > -1) {
Tobias Hunger's avatar
Tobias Hunger committed
105 106
        if (!m_makeLine.cap(7).isEmpty())
            ++m_fatalErrorCount;
Tobias Hunger's avatar
Tobias Hunger committed
107 108 109
        if (!m_suppressIssues) {
            m_suppressIssues = true;
            addTask(Task(Task::Error,
Tobias Hunger's avatar
Tobias Hunger committed
110
                         m_makeLine.cap(8),
Tobias Hunger's avatar
Tobias Hunger committed
111 112 113 114 115 116
                         QString() /* filename */,
                         -1, /* line */
                         Constants::TASK_CATEGORY_BUILDSYSTEM));
        }
        return;
    }
Tobias Hunger's avatar
Tobias Hunger committed
117

Tobias Hunger's avatar
Tobias Hunger committed
118
    IOutputParser::stdError(line);
Tobias Hunger's avatar
Tobias Hunger committed
119 120 121 122
}

void GnuMakeParser::addDirectory(const QString &dir)
{
123
    if (dir.isEmpty())
Tobias Hunger's avatar
Tobias Hunger committed
124 125 126 127 128 129
        return;
    m_directories.append(dir);
}

void GnuMakeParser::removeDirectory(const QString &dir)
{
130
    m_directories.removeOne(dir);
Tobias Hunger's avatar
Tobias Hunger committed
131 132
}

133
void GnuMakeParser::taskAdded(const Task &task)
Tobias Hunger's avatar
Tobias Hunger committed
134
{
135
    Task editable(task);
Tobias Hunger's avatar
Tobias Hunger committed
136 137 138 139 140 141

    if (task.type == Task::Error) {
        // assume that all make errors will be follow up errors:
        m_suppressIssues = true;
    }

Tobias Hunger's avatar
Tobias Hunger committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    QString filePath(QDir::cleanPath(task.file.trimmed()));

    if (!filePath.isEmpty() && !QDir::isAbsolutePath(filePath)) {
        QList<QFileInfo> possibleFiles;
        foreach (const QString &dir, m_directories) {
            QFileInfo candidate(dir + QLatin1Char('/') + filePath);
            if (candidate.exists()
                && !possibleFiles.contains(candidate)) {
                possibleFiles << candidate;
            }
        }
        if (possibleFiles.size() == 1)
            editable.file = possibleFiles.first().filePath();
        // Let the Makestep apply additional heuristics (based on
        // files in ther project) if we can not uniquely
        // identify the file!
    }

    IOutputParser::taskAdded(editable);
}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

#if defined WITH_TESTS
QStringList GnuMakeParser::searchDirectories() const
{
    return m_directories;
}
#endif

// Unit tests:

#ifdef WITH_TESTS
#   include <QTest>

#   include <QtCore/QUuid>

#   include "outputparser_test.h"
#   include "projectexplorer.h"
#   include "projectexplorerconstants.h"

#   include "metatypedeclarations.h"

183 184 185 186 187 188 189 190 191 192
GnuMakeParserTester::GnuMakeParserTester(GnuMakeParser *p, QObject *parent) :
    QObject(parent),
    parser(p)
{ }

void GnuMakeParserTester::parserIsAboutToBeDeleted()
{
    directories = parser->searchDirectories();
}

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
void ProjectExplorerPlugin::testGnuMakeParserParsing_data()
{
    QTest::addColumn<QStringList>("extraSearchDirs");
    QTest::addColumn<QString>("input");
    QTest::addColumn<OutputParserTester::Channel>("inputChannel");
    QTest::addColumn<QString>("childStdOutLines");
    QTest::addColumn<QString>("childStdErrLines");
    QTest::addColumn<QList<Task> >("tasks");
    QTest::addColumn<QString>("outputLines");
    QTest::addColumn<QStringList>("additionalSearchDirs");

    QTest::newRow("pass-through stdout")
            << QStringList()
            << QString::fromLatin1("Sometext") << OutputParserTester::STDOUT
            << QString::fromLatin1("Sometext") << QString()
            << QList<Task>()
            << QString()
            << QStringList();
    QTest::newRow("pass-through stderr")
            << QStringList()
            << QString::fromLatin1("Sometext") << OutputParserTester::STDERR
            << QString() << QString::fromLatin1("Sometext")
            << QList<Task>()
            << QString()
            << QStringList();
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    QTest::newRow("pass-through gcc infos")
            << QStringList()
            << QString::fromLatin1("/temp/test/untitled8/main.cpp: In function `int main(int, char**)':\n"
                                   "../../scriptbug/main.cpp: At global scope:\n"
                                   "../../scriptbug/main.cpp: In instantiation of void bar(i) [with i = double]:\n"
                                   "../../scriptbug/main.cpp:8: instantiated from void foo(i) [with i = double]\n"
                                   "../../scriptbug/main.cpp:22: instantiated from here\n")
            << OutputParserTester::STDERR
            << QString()
            << QString::fromLatin1("/temp/test/untitled8/main.cpp: In function `int main(int, char**)':\n"
                                   "../../scriptbug/main.cpp: At global scope:\n"
                                   "../../scriptbug/main.cpp: In instantiation of void bar(i) [with i = double]:\n"
                                   "../../scriptbug/main.cpp:8: instantiated from void foo(i) [with i = double]\n"
                                   "../../scriptbug/main.cpp:22: instantiated from here\n")
            << QList<ProjectExplorer::Task>()
            << QString()
            << QStringList();

236 237 238 239 240 241 242 243 244
    // make sure adding directories works (once;-)
    QTest::newRow("entering directory")
            << (QStringList() << QString::fromLatin1("/test/dir") )
            << QString::fromLatin1("make[4]: Entering directory `/home/code/build/qt/examples/opengl/grabber'\n"
                                   "make[4]: Entering directory `/home/code/build/qt/examples/opengl/grabber'\n")
            << OutputParserTester::STDOUT
            << QString() << QString()
            << QList<Task>()
            << QString()
Tobias Hunger's avatar
Tobias Hunger committed
245 246 247
            << (QStringList() << QString::fromLatin1("/home/code/build/qt/examples/opengl/grabber")
                              << QString::fromLatin1("/home/code/build/qt/examples/opengl/grabber")
                              << QString::fromLatin1("/test/dir"));
248 249 250 251 252 253 254 255 256 257 258
    QTest::newRow("leaving directory")
            << (QStringList()  << QString::fromLatin1("/home/code/build/qt/examples/opengl/grabber") << QString::fromLatin1("/test/dir"))
            << QString::fromLatin1("make[4]: Leaving directory `/home/code/build/qt/examples/opengl/grabber'")
            << OutputParserTester::STDOUT
            << QString() << QString()
            << QList<Task>()
            << QString()
            << (QStringList() << QString::fromLatin1("/test/dir"));
    QTest::newRow("make error")
            << QStringList()
            << QString::fromLatin1("make: *** No rule to make target `hello.c', needed by `hello.o'.  Stop.")
Tobias Hunger's avatar
Tobias Hunger committed
259
            << OutputParserTester::STDERR
260 261 262 263 264 265 266 267
            << QString() << QString()
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("No rule to make target `hello.c', needed by `hello.o'.  Stop."),
                        QString(), -1,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
            << QString()
            << QStringList();
268 269 270 271 272
    QTest::newRow("multiple fatals")
            << QStringList()
            << QString::fromLatin1("make[3]: *** [.obj/debug-shared/gnumakeparser.o] Error 1\n"
                                   "make[3]: *** Waiting for unfinished jobs....\n"
                                   "make[2]: *** [sub-projectexplorer-make_default] Error 2")
Tobias Hunger's avatar
Tobias Hunger committed
273
            << OutputParserTester::STDERR
274 275 276 277 278 279 280 281 282 283 284
            << QString() << QString()
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("[.obj/debug-shared/gnumakeparser.o] Error 1"),
                        QString(), -1,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
            << QString()
            << QStringList();
    QTest::newRow("Makefile error")
            << QStringList()
            << QString::fromLatin1("Makefile:360: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.")
Tobias Hunger's avatar
Tobias Hunger committed
285
            << OutputParserTester::STDERR
286 287 288 289 290 291 292 293
            << QString() << QString()
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("missing separator (did you mean TAB instead of 8 spaces?). Stop."),
                        QString::fromLatin1("Makefile"), 360,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
            << QString()
            << QStringList();
Tobias Hunger's avatar
Tobias Hunger committed
294
    QTest::newRow("mingw32-make error")
295
            << QStringList()
Tobias Hunger's avatar
Tobias Hunger committed
296 297 298
            << QString::fromLatin1("mingw32-make[1]: *** [debug/qplotaxis.o] Error 1\n"
                                   "mingw32-make: *** [debug] Error 2")
            << OutputParserTester::STDERR
299
            << QString() << QString()
Tobias Hunger's avatar
Tobias Hunger committed
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
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("[debug/qplotaxis.o] Error 1"),
                        QString(), -1,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
            << QString()
            << QStringList();
    QTest::newRow("mingw64-make error")
            << QStringList()
            << QString::fromLatin1("mingw64-make.exe[1]: *** [dynlib.inst] Error -1073741819")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("[dynlib.inst] Error -1073741819"),
                        QString(), -1,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
            << QString()
            << QStringList();
    QTest::newRow("pass-trough note")
            << QStringList()
            << QString::fromLatin1("/home/dev/creator/share/qtcreator/gdbmacros/gdbmacros.cpp:1079: note: initialized from here")
            << OutputParserTester::STDERR
            << QString() << QString::fromLatin1("/home/dev/creator/share/qtcreator/gdbmacros/gdbmacros.cpp:1079: note: initialized from here")
            << QList<ProjectExplorer::Task>()
            << QString()
            << QStringList();
    QTest::newRow("Full path make exe")
            << QStringList()
            << QString::fromLatin1("C:\\Qt\\4.6.2-Symbian\\s60sdk\\epoc32\\tools\\make.exe: *** [sis] Error 2")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("[sis] Error 2"),
                        QString(), -1,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
337 338
            << QString()
            << QStringList();
Tobias Hunger's avatar
Tobias Hunger committed
339 340 341 342 343 344 345 346 347 348 349 350
    QTest::newRow("missing g++")
            << QStringList()
            << QString::fromLatin1("make: g++: Command not found")
            << OutputParserTester::STDERR
            << QString() << QString()
            << (QList<Task>()
                << Task(Task::Error,
                        QString::fromLatin1("g++: Command not found"),
                        QString(), -1,
                        Constants::TASK_CATEGORY_BUILDSYSTEM))
            << QString()
            << QStringList();
351 352 353 354 355 356
}

void ProjectExplorerPlugin::testGnuMakeParserParsing()
{
    OutputParserTester testbench;
    GnuMakeParser *childParser = new GnuMakeParser;
357 358 359 360
    GnuMakeParserTester *tester = new GnuMakeParserTester(childParser);
    connect(&testbench, SIGNAL(aboutToDeleteParser()),
            tester, SLOT(parserIsAboutToBeDeleted()));

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    testbench.appendOutputParser(childParser);
    QFETCH(QStringList, extraSearchDirs);
    QFETCH(QString, input);
    QFETCH(OutputParserTester::Channel, inputChannel);
    QFETCH(QList<Task>, tasks);
    QFETCH(QString, childStdOutLines);
    QFETCH(QString, childStdErrLines);
    QFETCH(QString, outputLines);
    QFETCH(QStringList, additionalSearchDirs);

    QStringList searchDirs = childParser->searchDirectories();

    // add extra directories:
    foreach(const QString &dir, extraSearchDirs)
        childParser->addDirectory(dir);

    testbench.testParsing(input, inputChannel,
                          tasks, childStdOutLines, childStdErrLines,
                          outputLines);

    // make sure we still have all the original dirs
382
    QStringList newSearchDirs = tester->directories;
383 384 385 386 387 388 389 390 391 392 393 394
    foreach (const QString &dir, searchDirs) {
        QVERIFY(newSearchDirs.contains(dir));
        newSearchDirs.removeOne(dir);
    }

    // make sure we have all additional dirs:
    foreach (const QString &dir, additionalSearchDirs) {
        QVERIFY(newSearchDirs.contains(dir));
        newSearchDirs.removeOne(dir);
    }
    // make sure we have no extra cruft:
    QVERIFY(newSearchDirs.isEmpty());
395
    delete tester;
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
}

void ProjectExplorerPlugin::testGnuMakeParserTaskMangling_data()
{
    QTest::addColumn<QStringList>("files");
    QTest::addColumn<QStringList>("searchDirectories");
    QTest::addColumn<Task>("inputTask");
    QTest::addColumn<Task>("outputTask");

    QTest::newRow("no filename")
            << QStringList()
            << QStringList()
            << Task(Task::Error,
                    QLatin1String("no filename, no mangling"),
                    QString(),
                    -1,
                    Constants::TASK_CATEGORY_COMPILE)
            << Task(Task::Error,
                    QLatin1String("no filename, no mangling"),
                    QString(),
                    -1,
                    Constants::TASK_CATEGORY_COMPILE);
   QTest::newRow("no mangling")
            << QStringList()
            << QStringList()
            << Task(Task::Error,
                    QLatin1String("unknown filename, no mangling"),
                    QString::fromLatin1("some/path/unknown.cpp"),
                    -1,
                    Constants::TASK_CATEGORY_COMPILE)
            << Task(Task::Error,
                    QLatin1String("unknown filename, no mangling"),
                    QString::fromLatin1("some/path/unknown.cpp"),
                    -1,
                    Constants::TASK_CATEGORY_COMPILE);
    QTest::newRow("find file")
            << (QStringList() << "test/file.cpp")
            << (QStringList() << "test")
            << Task(Task::Error,
                    QLatin1String("mangling"),
                    QString::fromLatin1("file.cpp"),
                    10,
                    Constants::TASK_CATEGORY_COMPILE)
            << Task(Task::Error,
                    QLatin1String("mangling"),
                    QString::fromLatin1("$TMPDIR/test/file.cpp"),
                    10,
                    Constants::TASK_CATEGORY_COMPILE);
}

void ProjectExplorerPlugin::testGnuMakeParserTaskMangling()
{
    OutputParserTester testbench;
    GnuMakeParser *childParser = new GnuMakeParser;
    testbench.appendOutputParser(childParser);

    QFETCH(QStringList, files);
    QFETCH(QStringList, searchDirectories);
    QFETCH(Task, inputTask);
    QFETCH(Task, outputTask);

    // setup files:
458
    QString tempdir = QDir::tempPath();
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
    tempdir.append(QChar('/'));
    tempdir.append(QUuid::createUuid().toString());
    tempdir.append(QChar('/'));

    QDir filedir(tempdir);
    foreach (const QString &file, files) {
        Q_ASSERT(!file.startsWith(QChar('/')));
        Q_ASSERT(!file.contains(QLatin1String("../")));

        filedir.mkpath(file.left(file.lastIndexOf(QChar('/'))));

        QFile tempfile(tempdir + file);
        if (!tempfile.open(QIODevice::WriteOnly))
            continue;
        tempfile.write("Delete me again!");
        tempfile.close();
    }

    // setup search dirs:
    foreach (const QString &dir, searchDirectories) {
        Q_ASSERT(!dir.startsWith(QChar('/')));
        Q_ASSERT(!dir.contains(QLatin1String("../")));
        childParser->addDirectory(tempdir + dir);
    }

    // fix up output task file:
    if (outputTask.file.startsWith(QLatin1String("$TMPDIR/")))
        outputTask.file = outputTask.file.replace(QLatin1String("$TMPDIR/"), tempdir);

    // test mangling:
    testbench.testTaskMangling(inputTask, outputTask);

    // clean up:
    foreach (const QString &file, files)
        filedir.rmpath(tempdir + file);
}
#endif