CompilationDataBase: Hide build system definition in .cpp

Change-Id: I49fe7eae6bc323c4d618a463d701021591f4d15d
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2024-07-19 15:23:43 +02:00
parent 84cc7208de
commit f2505ee90a
2 changed files with 66 additions and 82 deletions

View File

@@ -27,6 +27,7 @@
#include <projectexplorer/toolchainmanager.h> #include <projectexplorer/toolchainmanager.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/filesystemwatcher.h> #include <utils/filesystemwatcher.h>
@@ -43,22 +44,20 @@ using namespace Utils;
namespace CompilationDatabaseProjectManager::Internal { namespace CompilationDatabaseProjectManager::Internal {
namespace { static bool isGccCompiler(const QString &compilerName)
bool isGccCompiler(const QString &compilerName)
{ {
return compilerName.contains("gcc") return compilerName.contains("gcc")
|| (compilerName.contains("g++") && !compilerName.contains("clang")); || (compilerName.contains("g++") && !compilerName.contains("clang"));
} }
bool isClCompatibleCompiler(const QString &compilerName) static bool isClCompatibleCompiler(const QString &compilerName)
{ {
return compilerName.endsWith("cl"); return compilerName.endsWith("cl");
} }
Utils::Id getCompilerId(QString compilerName) static Id getCompilerId(QString compilerName)
{ {
if (Utils::HostOsInfo::isWindowsHost()) { if (HostOsInfo::isWindowsHost()) {
if (compilerName.endsWith(".exe")) if (compilerName.endsWith(".exe"))
compilerName.chop(4); compilerName.chop(4);
if (isGccCompiler(compilerName)) if (isGccCompiler(compilerName))
@@ -74,7 +73,7 @@ Utils::Id getCompilerId(QString compilerName)
return ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID; return ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID;
} }
Toolchain *toolchainFromCompilerId(const Utils::Id &compilerId, const Utils::Id &language) static Toolchain *toolchainFromCompilerId(const Id &compilerId, const Id &language)
{ {
return ToolchainManager::toolchain([&compilerId, &language](const Toolchain *tc) { return ToolchainManager::toolchain([&compilerId, &language](const Toolchain *tc) {
if (!tc->isValid() || tc->language() != language) if (!tc->isValid() || tc->language() != language)
@@ -83,7 +82,7 @@ Toolchain *toolchainFromCompilerId(const Utils::Id &compilerId, const Utils::Id
}); });
} }
QString compilerPath(QString pathFlag) static QString compilerPath(QString pathFlag)
{ {
if (pathFlag.isEmpty()) if (pathFlag.isEmpty())
return pathFlag; return pathFlag;
@@ -104,7 +103,7 @@ QString compilerPath(QString pathFlag)
return QDir::fromNativeSeparators(pathFlag); return QDir::fromNativeSeparators(pathFlag);
} }
Toolchain *toolchainFromFlags(const Kit *kit, const QStringList &flags, const Utils::Id &language) static Toolchain *toolchainFromFlags(const Kit *kit, const QStringList &flags, const Id &language)
{ {
Toolchain * const kitToolchain = ToolchainKitAspect::toolchain(kit, language); Toolchain * const kitToolchain = ToolchainKitAspect::toolchain(kit, language);
@@ -112,14 +111,14 @@ Toolchain *toolchainFromFlags(const Kit *kit, const QStringList &flags, const Ut
return kitToolchain; return kitToolchain;
// Try exact compiler match. // Try exact compiler match.
const Utils::FilePath compiler = Utils::FilePath::fromUserInput(compilerPath(flags.front())); const FilePath compiler = FilePath::fromUserInput(compilerPath(flags.front()));
Toolchain *toolchain = ToolchainManager::toolchain([&compiler, &language](const Toolchain *tc) { Toolchain *toolchain = ToolchainManager::toolchain([&compiler, &language](const Toolchain *tc) {
return tc->isValid() && tc->language() == language && tc->compilerCommand() == compiler; return tc->isValid() && tc->language() == language && tc->compilerCommand() == compiler;
}); });
if (toolchain) if (toolchain)
return toolchain; return toolchain;
Utils::Id compilerId = getCompilerId(compiler.fileName()); Id compilerId = getCompilerId(compiler.fileName());
if (kitToolchain->isValid() && kitToolchain->typeId() == compilerId) if (kitToolchain->isValid() && kitToolchain->typeId() == compilerId)
return kitToolchain; return kitToolchain;
if ((toolchain = toolchainFromCompilerId(compilerId, language))) if ((toolchain = toolchainFromCompilerId(compilerId, language)))
@@ -127,7 +126,7 @@ Toolchain *toolchainFromFlags(const Kit *kit, const QStringList &flags, const Ut
if (compilerId != ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID && if (compilerId != ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID &&
compilerId != ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID) { compilerId != ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID) {
compilerId = Utils::HostOsInfo::isWindowsHost() compilerId = HostOsInfo::isWindowsHost()
? ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID ? ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID
: ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID; : ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID;
if (kitToolchain->isValid() && kitToolchain->typeId() == compilerId) if (kitToolchain->isValid() && kitToolchain->typeId() == compilerId)
@@ -140,9 +139,9 @@ Toolchain *toolchainFromFlags(const Kit *kit, const QStringList &flags, const Ut
return kitToolchain; return kitToolchain;
} }
void addDriverModeFlagIfNeeded(const Toolchain *toolchain, static void addDriverModeFlagIfNeeded(const Toolchain *toolchain,
QStringList &flags, QStringList &flags,
const QStringList &originalFlags) const QStringList &originalFlags)
{ {
if (toolchain->typeId() == ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID if (toolchain->typeId() == ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID
&& !originalFlags.empty() && !originalFlags.front().endsWith("cl") && !originalFlags.empty() && !originalFlags.front().endsWith("cl")
@@ -151,12 +150,12 @@ void addDriverModeFlagIfNeeded(const Toolchain *toolchain,
} }
} }
RawProjectPart makeRawProjectPart(const Utils::FilePath &projectFile, static RawProjectPart makeRawProjectPart(const FilePath &projectFile,
Kit *kit, Kit *kit,
ProjectExplorer::KitInfo &kitInfo, ProjectExplorer::KitInfo &kitInfo,
const FilePath &workingDir, const FilePath &workingDir,
const FilePath &filePath, const FilePath &filePath,
QStringList flags) QStringList flags)
{ {
HeaderPaths headerPaths; HeaderPaths headerPaths;
Macros macros; Macros macros;
@@ -203,7 +202,7 @@ RawProjectPart makeRawProjectPart(const Utils::FilePath &projectFile,
return rpp; return rpp;
} }
QStringList relativeDirsList(Utils::FilePath currentPath, const Utils::FilePath &rootPath) static QStringList relativeDirsList(FilePath currentPath, const FilePath &rootPath)
{ {
QStringList dirsList; QStringList dirsList;
while (!currentPath.isEmpty() && currentPath != rootPath) { while (!currentPath.isEmpty() && currentPath != rootPath) {
@@ -216,9 +215,9 @@ QStringList relativeDirsList(Utils::FilePath currentPath, const Utils::FilePath
return dirsList; return dirsList;
} }
FolderNode *addChildFolderNode(FolderNode *parent, const QString &childName) static FolderNode *addChildFolderNode(FolderNode *parent, const QString &childName)
{ {
const Utils::FilePath path = parent->filePath().pathAppended(childName); const FilePath path = parent->filePath().pathAppended(childName);
auto node = std::make_unique<FolderNode>(path); auto node = std::make_unique<FolderNode>(path);
FolderNode *childNode = node.get(); FolderNode *childNode = node.get();
childNode->setDisplayName(childName); childNode->setDisplayName(childName);
@@ -227,7 +226,7 @@ FolderNode *addChildFolderNode(FolderNode *parent, const QString &childName)
return childNode; return childNode;
} }
FolderNode *addOrGetChildFolderNode(FolderNode *parent, const QString &childName) static FolderNode *addOrGetChildFolderNode(FolderNode *parent, const QString &childName)
{ {
FolderNode *fn = parent->findChildFolderNode([&](FolderNode *folder) { FolderNode *fn = parent->findChildFolderNode([&](FolderNode *folder) {
return folder->filePath().fileName() == childName; return folder->filePath().fileName() == childName;
@@ -235,8 +234,8 @@ FolderNode *addOrGetChildFolderNode(FolderNode *parent, const QString &childName
return fn ? fn : addChildFolderNode(parent, childName); return fn ? fn : addChildFolderNode(parent, childName);
} }
// Return the node for folderPath. // Return the node for folderPath.
FolderNode *createFoldersIfNeeded(FolderNode *root, const Utils::FilePath &folderPath) static FolderNode *createFoldersIfNeeded(FolderNode *root, const FilePath &folderPath)
{ {
const QStringList dirsList = relativeDirsList(folderPath, root->filePath()); const QStringList dirsList = relativeDirsList(folderPath, root->filePath());
@@ -247,7 +246,7 @@ FolderNode *createFoldersIfNeeded(FolderNode *root, const Utils::FilePath &folde
return parent; return parent;
} }
FileType fileTypeForName(const QString &fileName) static FileType fileTypeForName(const QString &fileName)
{ {
CppEditor::ProjectFile::Kind fileKind = CppEditor::ProjectFile::classify(fileName); CppEditor::ProjectFile::Kind fileKind = CppEditor::ProjectFile::classify(fileName);
if (CppEditor::ProjectFile::isHeader(fileKind)) if (CppEditor::ProjectFile::isHeader(fileKind))
@@ -255,7 +254,7 @@ FileType fileTypeForName(const QString &fileName)
return FileType::Source; return FileType::Source;
} }
void addChild(FolderNode *root, const Utils::FilePath &fileName) static void addChild(FolderNode *root, const FilePath &fileName)
{ {
FolderNode *parentNode = createFoldersIfNeeded(root, fileName.parentDir()); FolderNode *parentNode = createFoldersIfNeeded(root, fileName.parentDir());
if (!parentNode->fileNode(fileName)) { if (!parentNode->fileNode(fileName)) {
@@ -264,8 +263,8 @@ void addChild(FolderNode *root, const Utils::FilePath &fileName)
} }
} }
void createTree(std::unique_ptr<ProjectNode> &root, static void createTree(std::unique_ptr<ProjectNode> &root,
const Utils::FilePath &rootPath, const FilePath &rootPath,
const RawProjectParts &rpps, const RawProjectParts &rpps,
const QList<FileNode *> &scannedFiles = QList<FileNode *>()) const QList<FileNode *> &scannedFiles = QList<FileNode *>())
{ {
@@ -274,12 +273,12 @@ void createTree(std::unique_ptr<ProjectNode> &root,
for (const RawProjectPart &rpp : rpps) { for (const RawProjectPart &rpp : rpps) {
for (const QString &filePath : rpp.files) { for (const QString &filePath : rpp.files) {
Utils::FilePath fileName = Utils::FilePath::fromString(filePath); FilePath fileName = FilePath::fromString(filePath);
if (!fileName.isChildOf(rootPath)) { if (!fileName.isChildOf(rootPath)) {
if (fileName.isChildOf(Utils::FilePath::fromString(rpp.buildSystemTarget))) { if (fileName.isChildOf(FilePath::fromString(rpp.buildSystemTarget))) {
if (!secondRoot) if (!secondRoot)
secondRoot = std::make_unique<ProjectNode>( secondRoot = std::make_unique<ProjectNode>(
Utils::FilePath::fromString(rpp.buildSystemTarget)); FilePath::fromString(rpp.buildSystemTarget));
addChild(secondRoot.get(), fileName); addChild(secondRoot.get(), fileName);
} }
} else { } else {
@@ -292,7 +291,7 @@ void createTree(std::unique_ptr<ProjectNode> &root,
if (node->fileType() != FileType::Header) if (node->fileType() != FileType::Header)
continue; continue;
const Utils::FilePath fileName = node->filePath(); const FilePath fileName = node->filePath();
if (!fileName.isChildOf(rootPath)) if (!fileName.isChildOf(rootPath))
continue; continue;
FolderNode *parentNode = createFoldersIfNeeded(root.get(), fileName.parentDir()); FolderNode *parentNode = createFoldersIfNeeded(root.get(), fileName.parentDir());
@@ -313,8 +312,27 @@ void createTree(std::unique_ptr<ProjectNode> &root,
} }
} }
// CompilationDatabaseBuildSystem
} // anonymous namespace class CompilationDatabaseBuildSystem final : public BuildSystem
{
public:
explicit CompilationDatabaseBuildSystem(Target *target);
~CompilationDatabaseBuildSystem();
void triggerParsing() final;
QString name() const final { return QLatin1String("compilationdb"); }
void reparseProject();
void updateDeploymentData();
void buildTreeAndProjectParts();
std::unique_ptr<ProjectUpdater> m_cppCodeModelUpdater;
MimeBinaryCache m_mimeBinaryCache;
QByteArray m_projectFileHash;
CompilationDbParser *m_parser = nullptr;
FileSystemWatcher * const m_deployFileWatcher;
};
CompilationDatabaseBuildSystem::CompilationDatabaseBuildSystem(Target *target) CompilationDatabaseBuildSystem::CompilationDatabaseBuildSystem(Target *target)
: BuildSystem(target) : BuildSystem(target)
@@ -380,7 +398,7 @@ void CompilationDatabaseBuildSystem::buildTreeAndProjectParts()
} }
if (!dbContents.extras.empty()) { if (!dbContents.extras.empty()) {
const Utils::FilePath baseDir = projectFilePath().parentDir(); const FilePath baseDir = projectFilePath().parentDir();
QStringList extraFiles; QStringList extraFiles;
for (const QString &extra : dbContents.extras) for (const QString &extra : dbContents.extras)
@@ -398,7 +416,7 @@ void CompilationDatabaseBuildSystem::buildTreeAndProjectParts()
root->addNode(std::make_unique<FileNode>(projectFilePath(), FileType::Project)); root->addNode(std::make_unique<FileNode>(projectFilePath(), FileType::Project));
if (QFileInfo::exists(dbContents.extraFileName)) if (QFileInfo::exists(dbContents.extraFileName))
root->addNode(std::make_unique<FileNode>(Utils::FilePath::fromString(dbContents.extraFileName), root->addNode(std::make_unique<FileNode>(FilePath::fromString(dbContents.extraFileName),
FileType::Project)); FileType::Project));
setRootProjectNode(std::move(root)); setRootProjectNode(std::move(root));
@@ -407,7 +425,9 @@ void CompilationDatabaseBuildSystem::buildTreeAndProjectParts()
updateDeploymentData(); updateDeploymentData();
} }
CompilationDatabaseProject::CompilationDatabaseProject(const Utils::FilePath &projectFile) // CompilationDatabaseProject
CompilationDatabaseProject::CompilationDatabaseProject(const FilePath &projectFile)
: Project(Constants::COMPILATIONDATABASEMIMETYPE, projectFile) : Project(Constants::COMPILATIONDATABASEMIMETYPE, projectFile)
{ {
setId(Constants::COMPILATIONDATABASEPROJECT_ID); setId(Constants::COMPILATIONDATABASEPROJECT_ID);
@@ -418,11 +438,11 @@ CompilationDatabaseProject::CompilationDatabaseProject(const Utils::FilePath &pr
{projectFile.stringAppended(Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX)}); {projectFile.stringAppended(Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX)});
} }
Utils::FilePath CompilationDatabaseProject::rootPathFromSettings() const FilePath CompilationDatabaseProject::rootPathFromSettings() const
{ {
Utils::FilePath rootPath; FilePath rootPath;
#ifndef WITH_TESTS #ifndef WITH_TESTS
rootPath = Utils::FilePath::fromString( rootPath = FilePath::fromString(
namedSettings(ProjectExplorer::Constants::PROJECT_ROOT_PATH_KEY).toString()); namedSettings(ProjectExplorer::Constants::PROJECT_ROOT_PATH_KEY).toString());
#endif #endif
if (rootPath.isEmpty()) if (rootPath.isEmpty())
@@ -463,7 +483,7 @@ void CompilationDatabaseBuildSystem::reparseProject()
void CompilationDatabaseBuildSystem::updateDeploymentData() void CompilationDatabaseBuildSystem::updateDeploymentData()
{ {
const Utils::FilePath deploymentFilePath = projectDirectory() const FilePath deploymentFilePath = projectDirectory()
.pathAppended("QtCreatorDeployment.txt"); .pathAppended("QtCreatorDeployment.txt");
DeploymentData deploymentData; DeploymentData deploymentData;
deploymentData.addFilesFromDeploymentFile(deploymentFilePath, projectDirectory()); deploymentData.addFilesFromDeploymentFile(deploymentFilePath, projectDirectory());

View File

@@ -3,28 +3,13 @@
#pragma once #pragma once
#include "compilationdatabaseutils.h"
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildsystem.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <texteditor/texteditor.h> namespace ProjectExplorer { class Kit; }
#include <utils/filesystemwatcher.h>
namespace ProjectExplorer {
class Kit;
class ProjectUpdater;
} // ProjectExplorer
namespace Utils { class FileSystemWatcher; }
namespace CompilationDatabaseProjectManager::Internal { namespace CompilationDatabaseProjectManager::Internal {
class CompilationDbParser; class CompilationDatabaseProject final : public ProjectExplorer::Project
class CompilationDatabaseProject : public ProjectExplorer::Project
{ {
Q_OBJECT Q_OBJECT
@@ -33,31 +18,10 @@ public:
Utils::FilePath rootPathFromSettings() const; Utils::FilePath rootPathFromSettings() const;
private: private:
void configureAsExampleProject(ProjectExplorer::Kit *kit) override; void configureAsExampleProject(ProjectExplorer::Kit *kit) final;
};
class CompilationDatabaseBuildSystem final : public ProjectExplorer::BuildSystem
{
public:
explicit CompilationDatabaseBuildSystem(ProjectExplorer::Target *target);
~CompilationDatabaseBuildSystem();
void triggerParsing() final;
QString name() const final { return QLatin1String("compilationdb"); }
void reparseProject();
void updateDeploymentData();
void buildTreeAndProjectParts();
std::unique_ptr<ProjectExplorer::ProjectUpdater> m_cppCodeModelUpdater;
MimeBinaryCache m_mimeBinaryCache;
QByteArray m_projectFileHash;
CompilationDbParser *m_parser = nullptr;
Utils::FileSystemWatcher * const m_deployFileWatcher;
}; };
void setupCompilationDatabaseEditor(); void setupCompilationDatabaseEditor();
void setupCompilationDatabaseBuildConfiguration(); void setupCompilationDatabaseBuildConfiguration();
} // CompilationDatabaseProjectManager::Internal } // CompilationDatabaseProjectManager::Internal