summaryrefslogtreecommitdiff
path: root/src/plugins/cmakeprojectmanager/fileapidataextractor.cpp
blob: 0af8699c74ca5c5c27c82ff414daf28f3da81bf3 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "fileapidataextractor.h"

#include "cmakeprojectnodes.h"
#include "projecttreehelper.h"

#include <projectexplorer/projectnodes.h>

#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>

#include <QDir>

using namespace ProjectExplorer;
using namespace Utils;

namespace {

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
using namespace CMakeProjectManager::Internal::FileApiDetails;

// --------------------------------------------------------------------
// Helpers:
// --------------------------------------------------------------------

class CMakeFileResult
{
public:
    QSet<FilePath> cmakeFiles;

    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeNodesSource;
    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeNodesBuild;
    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeNodesOther;
    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeListNodes;
};

CMakeFileResult extractCMakeFilesData(const std::vector<FileApiDetails::CMakeFileInfo> &cmakefiles,
                                      const FilePath &sourceDirectory,
                                      const FilePath &buildDirectory)
{
    CMakeFileResult result;

    QDir sourceDir(sourceDirectory.toString());
    QDir buildDir(buildDirectory.toString());

    for (const CMakeFileInfo &info : cmakefiles) {
        const FilePath sfn = FilePath::fromString(
            QDir::cleanPath(sourceDir.absoluteFilePath(info.path)));
        const int oldCount = result.cmakeFiles.count();
        result.cmakeFiles.insert(sfn);
        if (oldCount < result.cmakeFiles.count()) {
            if (info.isCMake && !info.isCMakeListsDotTxt) {
                // Skip files that cmake considers to be part of the installation -- but include
                // CMakeLists.txt files. This unbreaks cmake binaries running from their own
                // build directory.
                continue;
            }

            auto node = std::make_unique<FileNode>(sfn, FileType::Project);
            node->setIsGenerated(info.isGenerated
                                 && !info.isCMakeListsDotTxt); // CMakeLists.txt are never
                                                               // generated, independent
                                                               // what cmake thinks:-)

            if (info.isCMakeListsDotTxt) {
                result.cmakeListNodes.emplace_back(std::move(node));
            } else if (sfn.isChildOf(sourceDir)) {
                result.cmakeNodesSource.emplace_back(std::move(node));
            } else if (sfn.isChildOf(buildDir)) {
                result.cmakeNodesBuild.emplace_back(std::move(node));
            } else {
                result.cmakeNodesOther.emplace_back(std::move(node));
            }
        }
    }

    return result;
}

Configuration extractConfiguration(std::vector<Configuration> &codemodel, QString &errorMessage)
{
    if (codemodel.size() == 0) {
        qWarning() << "No configuration found!";
        errorMessage = "No configuration found!";
        return {};
    }
    if (codemodel.size() > 1)
        qWarning() << "Multi-configuration generator found, ignoring all but first configuration";

    Configuration result = std::move(codemodel[0]);
    codemodel.clear();

    return result;
}

class PreprocessedData
{
public:
    CMakeProjectManager::CMakeConfig cache;

    QSet<FilePath> cmakeFiles;

    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeNodesSource;
    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeNodesBuild;
    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeNodesOther;
    std::vector<std::unique_ptr<ProjectExplorer::FileNode>> cmakeListNodes;

    Configuration codemodel;
    std::vector<TargetDetails> targetDetails;
};

PreprocessedData preprocess(FileApiData &data,
                            const FilePath &sourceDirectory,
                            const FilePath &buildDirectory,
                            QString &errorMessage)
{
    PreprocessedData result;

    result.cache = std::move(data.cache); // Make sure this is available, even when nothing else is

    // Simplify to only one configuration:
    result.codemodel = extractConfiguration(data.codemodel, errorMessage);
    if (!errorMessage.isEmpty()) {
        return result;
    }

    CMakeFileResult cmakeFileResult = extractCMakeFilesData(data.cmakeFiles,
                                                            sourceDirectory,
                                                            buildDirectory);

    result.cmakeFiles = std::move(cmakeFileResult.cmakeFiles);
    result.cmakeNodesSource = std::move(cmakeFileResult.cmakeNodesSource);
    result.cmakeNodesBuild = std::move(cmakeFileResult.cmakeNodesBuild);
    result.cmakeNodesOther = std::move(cmakeFileResult.cmakeNodesOther);
    result.cmakeListNodes = std::move(cmakeFileResult.cmakeListNodes);

    result.targetDetails = std::move(data.targetDetails);

    return result;
}

QList<CMakeBuildTarget> generateBuildTargets(const PreprocessedData &input,
                                             const FilePath &sourceDirectory)
{
    QDir sourceDir(sourceDirectory.toString());
    const QList<CMakeBuildTarget> result = transform<
        QList>(input.targetDetails, [&sourceDir](const TargetDetails &t) -> CMakeBuildTarget {
        CMakeBuildTarget ct;
        ct.title = t.name;
        ct.executable = t.artifacts.isEmpty() ? FilePath() : t.artifacts.at(0);
        TargetType type = UtilityType;
        if (t.type == "EXECUTABLE")
            type = ExecutableType;
        else if (t.type == "STATIC_LIBRARY")
            type = StaticLibraryType;
        else if (t.type == "OBJECT_LIBRARY")
            type = ObjectLibraryType;
        else if (t.type == "MODULE_LIBRARY" || t.type == "SHARED_LIBRARY")
            type = DynamicLibraryType;
        else
            type = UtilityType;
        ct.targetType = type;
        if (t.artifacts.isEmpty()) {
            ct.workingDirectory = t.buildDir;
        } else {
            ct.workingDirectory = FilePath::fromString(QDir::cleanPath(
                QDir(t.buildDir.toString()).absoluteFilePath(t.artifacts.at(0).toString() + "/..")));
        }
        ct.sourceDirectory = FilePath::fromString(
            QDir::cleanPath(sourceDir.absoluteFilePath(t.sourceDir.toString())));
        return ct;
    });
    return result;
}

static QStringList splitFragments(const QStringList &fragments)
{
    QStringList result;
    for (const QString &f : fragments) {
        result += QtcProcess::splitArgs(f);
    }
    return result;
}

CppTools::RawProjectParts generateRawProjectParts(const PreprocessedData &input,
                                                  const FilePath &sourceDirectory)
{
    CppTools::RawProjectParts rpps;

    int counter = 0;
    for (const TargetDetails &t : input.targetDetails) {
        QDir sourceDir(sourceDirectory.toString());

        for (const CompileInfo &ci : t.compileGroups) {
            if (ci.language != "C" && ci.language != "CXX" && ci.language != "CUDA")
                continue; // No need to bother the C++ codemodel

            // CMake users worked around Creator's inability of listing header files by creating
            // custom targets with all the header files. This target breaks the code model, so
            // keep quiet about it:-)
            if (ci.defines.empty() && ci.includes.empty() && allOf(ci.sources, [t](const int sid) {
                    const SourceInfo &source = t.sources[static_cast<size_t>(sid)];
                    return Node::fileTypeForFileName(FilePath::fromString(source.path))
                           == FileType::Header;
                })) {
                qWarning() << "Not reporting all-header compilegroup of target" << t.name
                           << "to code model.";
                continue;
            }

            ++counter;
            CppTools::RawProjectPart rpp;
            rpp.setProjectFileLocation(t.sourceDir.pathAppended("CMakeLists.txt").toString());
            rpp.setBuildSystemTarget(t.name);
            rpp.setDisplayName(t.id);
            rpp.setMacros(transform<QVector>(ci.defines, &DefineInfo::define));
            rpp.setHeaderPaths(transform<QVector>(ci.includes, &IncludeInfo::path));

            CppTools::RawProjectPartFlags cProjectFlags;
            cProjectFlags.commandLineFlags = splitFragments(ci.fragments);
            rpp.setFlagsForC(cProjectFlags);

            CppTools::RawProjectPartFlags cxxProjectFlags;
            cxxProjectFlags.commandLineFlags = cProjectFlags.commandLineFlags;
            rpp.setFlagsForCxx(cxxProjectFlags);

            rpp.setFiles(transform<QList>(ci.sources, [&t, &sourceDir](const int si) {
                return sourceDir.absoluteFilePath(t.sources[static_cast<size_t>(si)].path);
            }));

            const bool isExecutable = t.type == "EXECUTABLE";
            rpp.setBuildTargetType(isExecutable ? CppTools::ProjectPart::Executable
                                                : CppTools::ProjectPart::Library);
            rpps.append(rpp);
        }
    }

    return rpps;
}

FilePath directorySourceDir(const Configuration &c, const QDir &sourceDir, int directoryIndex)
{
    const size_t di = static_cast<size_t>(directoryIndex);
    QTC_ASSERT(di < c.directories.size(), return FilePath());

    return FilePath::fromString(
        QDir::cleanPath(sourceDir.absoluteFilePath(c.directories[di].sourcePath)));
}

FilePath directoryBuildDir(const Configuration &c, const QDir &buildDir, int directoryIndex)
{
    const size_t di = static_cast<size_t>(directoryIndex);
    QTC_ASSERT(di < c.directories.size(), return FilePath());

    return FilePath::fromString(
        QDir::cleanPath(buildDir.absoluteFilePath(c.directories[di].buildPath)));
}

void addProjects(const QHash<Utils::FilePath, ProjectNode *> &cmakeListsNodes,
                 const Configuration &config,
                 const QDir &sourceDir)
{
    for (const FileApiDetails::Project &p : config.projects) {
        if (p.parent == -1)
            continue; // Top-level project has already been covered
        FilePath dir = directorySourceDir(config, sourceDir, p.directories[0]);
        createProjectNode(cmakeListsNodes, dir, p.name);
    }
}

void addBacktraceInformation(FolderNode *node,
                             const BacktraceInfo &backtraces,
                             const QDir &sourceDir,
                             int backtraceIndex)
{
    QList<FolderNode::LocationInfo> info;
    // Set up a default target path:
    FilePath targetPath = node->filePath().pathAppended("CMakeLists.txt");
    while (backtraceIndex != -1) {
        const size_t bi = static_cast<size_t>(backtraceIndex);
        QTC_ASSERT(bi < backtraces.nodes.size(), break);
        const BacktraceNode &btNode = backtraces.nodes[bi];
        backtraceIndex = btNode.parent; // advance to next node

        const size_t fileIndex = static_cast<size_t>(btNode.file);
        QTC_ASSERT(fileIndex < backtraces.files.size(), break);
        const FilePath path = FilePath::fromString(
            sourceDir.absoluteFilePath(backtraces.files[fileIndex]));

        if (btNode.command < 0) {
            // No command, skip: The file itself is already covered:-)
            continue;
        }

        const size_t commandIndex = static_cast<size_t>(btNode.command);
        QTC_ASSERT(commandIndex < backtraces.commands.size(), break);

        const QString command = backtraces.commands[commandIndex];

        QString dn;
        if (path == targetPath) {
            if (btNode.line > 0) {
                dn = QCoreApplication::translate("CMakeProjectManager::Internal::FileApiReader",
                                                 "%1 in line %2")
                         .arg(command)
                         .arg(btNode.line);
            } else {
                dn = command;
            }
        } else {
            if (btNode.line > 0) {
                dn = QCoreApplication::translate("CMakeProjectManager::Internal::FileApiReader",
                                                 "%1 in %2:%3")
                         .arg(command)
                         .arg(path.toUserOutput())
                         .arg(btNode.line);
            } else {
                dn = QCoreApplication::translate("CMakeProjectManager::Internal::FileApiReader",
                                                 "%1 in %2")
                         .arg(command)
                         .arg(path.toUserOutput());
            }
        }
        info.append(FolderNode::LocationInfo(dn, path, btNode.line));
    }
    node->setLocationInfo(info);
}

QVector<FolderNode *> addSourceGroups(ProjectNode *targetRoot,
                                      const TargetDetails &td,
                                      const FilePath &sourceDirectory)
{
    QVector<FolderNode *> sourceGroupNodes;
    if (td.sourceGroups.size() == 1) {
        sourceGroupNodes.append(
            targetRoot); // Only one source group, so do not bother to display any:-)
    } else {
        for (const QString &sg : td.sourceGroups) {
            if (sg.isEmpty()) {
                sourceGroupNodes.append(targetRoot);
            } else {
                auto sgNode = createCMakeVFolder(sourceDirectory,
                                                 Node::DefaultFolderPriority + 5,
                                                 sg);
                sgNode->setListInProject(false);

                sourceGroupNodes.append(sgNode.get());
                targetRoot->addNode(std::move(sgNode));
            }
        }
    }

    return sourceGroupNodes;
}

void addCompileGroups(ProjectNode *targetRoot,
                      const Utils::FilePath &topSourceDirectory,
                      const Utils::FilePath &sourceDirectory,
                      const Utils::FilePath &buildDirectory,
                      const TargetDetails &td,
                      QSet<FilePath> &knownHeaderNodes)
{
    const bool inSourceBuild = (sourceDirectory == buildDirectory);
    const QDir currentSourceDir(sourceDirectory.toString());

    std::vector<std::unique_ptr<FileNode>> toList;
    QSet<Utils::FilePath> alreadyListed;

    // Files already added by other configurations:
    targetRoot->forEachGenericNode(
        [&alreadyListed](const Node *n) { alreadyListed.insert(n->filePath()); });

    QVector<FolderNode *> sourceGroupNodes = addSourceGroups(targetRoot, td, sourceDirectory);
    const QDir topSourceDir(topSourceDirectory.toString());

    std::vector<std::unique_ptr<FileNode>> buildFileNodes;
    std::vector<std::unique_ptr<FileNode>> otherFileNodes;

    for (const SourceInfo &si : td.sources) {
        const FilePath sourcePath = FilePath::fromString(
            QDir::cleanPath(topSourceDir.absoluteFilePath(si.path)));

        // Filter out already known files:
        const int count = alreadyListed.count();
        alreadyListed.insert(sourcePath);
        if (count == alreadyListed.count())
            continue;

        // Create FileNodes from the file
        auto node = std::make_unique<FileNode>(sourcePath, Node::fileTypeForFileName(sourcePath));
        node->setIsGenerated(si.isGenerated);

        // Register headers:
        if (node->fileType() == FileType::Header)
            knownHeaderNodes.insert(node->filePath());

        // Where does the file node need to go?
        if (sourcePath.isChildOf(buildDirectory) && !inSourceBuild) {
            buildFileNodes.emplace_back(std::move(node));
        } else if (sourcePath.isChildOf(sourceDirectory)) {
            sourceGroupNodes[si.sourceGroup]->addNode(std::move(node));
        } else {
            otherFileNodes.emplace_back(std::move(node));
        }
    }

    addCMakeVFolder(targetRoot,
                    buildDirectory,
                    100,
                    QCoreApplication::translate("CMakeProjectManager::Internal::FileApi",
                                                "<Build Directory>"),
                    std::move(buildFileNodes));
    addCMakeVFolder(targetRoot,
                    Utils::FilePath(),
                    10,
                    QCoreApplication::translate("CMakeProjectManager::Internal::FileApi",
                                                "<Other Locations>"),
                    std::move(otherFileNodes));
}

void addTargets(const QHash<Utils::FilePath, ProjectExplorer::ProjectNode *> &cmakeListsNodes,
                const Configuration &config,
                const std::vector<TargetDetails> &targetDetails,
                const FilePath &topSourceDir,
                const QDir &sourceDir,
                const QDir &buildDir,
                QSet<FilePath> &knownHeaderNodes)
{
    for (const FileApiDetails::Target &t : config.targets) {
        const TargetDetails &td = Utils::findOrDefault(targetDetails,
                                                       Utils::equal(&TargetDetails::id, t.id));

        const FilePath dir = directorySourceDir(config, sourceDir, t.directory);

        CMakeTargetNode *tNode = createTargetNode(cmakeListsNodes, dir, t.name);
        QTC_ASSERT(tNode, continue);

        tNode->setTargetInformation(td.artifacts, td.type);
        tNode->setBuildDirectory(directoryBuildDir(config, buildDir, t.directory));

        addBacktraceInformation(tNode, td.backtraceGraph, sourceDir, td.backtrace);

        addCompileGroups(tNode, topSourceDir, dir, tNode->buildDirectory(), td, knownHeaderNodes);
    }
}

std::pair<std::unique_ptr<CMakeProjectNode>, QSet<FilePath>> generateRootProjectNode(
    PreprocessedData &data, const FilePath &sourceDirectory, const FilePath &buildDirectory)
{
    std::pair<std::unique_ptr<CMakeProjectNode>, QSet<FilePath>> result;
    result.first = std::make_unique<CMakeProjectNode>(sourceDirectory);

    const QDir sourceDir(sourceDirectory.toString());
    const QDir buildDir(buildDirectory.toString());

    const FileApiDetails::Project topLevelProject
        = findOrDefault(data.codemodel.projects, equal(&FileApiDetails::Project::parent, -1));
    if (!topLevelProject.name.isEmpty())
        result.first->setDisplayName(topLevelProject.name);

    QHash<FilePath, ProjectNode *> cmakeListsNodes = addCMakeLists(result.first.get(),
                                                                   std::move(data.cmakeListNodes));
    data.cmakeListNodes.clear(); // Remove all the nullptr in the vector...

    QSet<FilePath> knownHeaders;
    addProjects(cmakeListsNodes, data.codemodel, sourceDir);

    addTargets(cmakeListsNodes,
               data.codemodel,
               data.targetDetails,
               sourceDirectory,
               sourceDir,
               buildDir,
               knownHeaders);

    // addHeaderNodes(root.get(), knownHeaders, allFiles);

    if (!data.cmakeNodesSource.empty() || !data.cmakeNodesBuild.empty()
        || !data.cmakeNodesOther.empty())
        addCMakeInputs(result.first.get(),
                       sourceDirectory,
                       buildDirectory,
                       std::move(data.cmakeNodesSource),
                       std::move(data.cmakeNodesBuild),
                       std::move(data.cmakeNodesOther));

    data.cmakeNodesSource.clear(); // Remove all the nullptr in the vector...
    data.cmakeNodesBuild.clear();  // Remove all the nullptr in the vector...
    data.cmakeNodesOther.clear();  // Remove all the nullptr in the vector...

    result.second = knownHeaders;

    return result;
}

} // namespace

namespace CMakeProjectManager {
namespace Internal {

using namespace FileApiDetails;

// --------------------------------------------------------------------
// extractData:
// --------------------------------------------------------------------

FileApiQtcData extractData(FileApiData &input,
                           const FilePath &sourceDirectory,
                           const FilePath &buildDirectory)
{
    FileApiQtcData result;

    // Preprocess our input:
    PreprocessedData data = preprocess(input, sourceDirectory, buildDirectory, result.errorMessage);
    result.cache = std::move(data.cache); // Make sure this is available, even when nothing else is
    if (!result.errorMessage.isEmpty()) {
        return {};
    }

    result.buildTargets = generateBuildTargets(data, sourceDirectory);
    result.cmakeFiles = std::move(data.cmakeFiles);
    result.projectParts = generateRawProjectParts(data, sourceDirectory);

    auto pair = generateRootProjectNode(data, sourceDirectory, buildDirectory);
    result.rootProjectNode = std::move(pair.first);
    result.knownHeaders = std::move(pair.second);

    return result;
}

} // namespace Internal
} // namespace CMakeProjectManager