QmlProjectManager: Fix cmake generator update issues

Fixes: QDS-12518
Change-Id: I27d45213100e42117b130bcbbceb5e115ed68445
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Knud Dollereder
2024-04-19 16:17:57 +02:00
committed by Thomas Hartmann
parent 5a49c16694
commit 1b52357d01
2 changed files with 63 additions and 48 deletions

View File

@@ -154,10 +154,10 @@ void CMakeGenerator::initialize(QmlProject *project)
parseNodeTree(m_root, rootProjectNode); parseNodeTree(m_root, rootProjectNode);
parseSourceTree(); parseSourceTree();
compareWithFileSystem(m_root);
createCMakeFiles(m_root); createCMakeFiles(m_root);
createSourceFiles(); createSourceFiles();
compareWithFileSystem(m_root);
} }
void CMakeGenerator::update(const QSet<QString> &added, const QSet<QString> &removed) void CMakeGenerator::update(const QSet<QString> &added, const QSet<QString> &removed)
@@ -170,7 +170,7 @@ void CMakeGenerator::update(const QSet<QString> &added, const QSet<QString> &rem
std::set<NodePtr> dirtyModules; std::set<NodePtr> dirtyModules;
for (const QString &add : added) { for (const QString &add : added) {
const Utils::FilePath path = Utils::FilePath::fromString(add); const Utils::FilePath path = Utils::FilePath::fromString(add);
if (auto node = findOrCreateNode(m_root, path)) { if (auto node = findOrCreateNode(m_root, path.parentDir())) {
insertFile(node, path); insertFile(node, path);
if (auto module = findModuleFor(node)) if (auto module = findModuleFor(node))
dirtyModules.insert(module); dirtyModules.insert(module);
@@ -182,15 +182,15 @@ void CMakeGenerator::update(const QSet<QString> &added, const QSet<QString> &rem
for (const QString &remove : removed) { for (const QString &remove : removed) {
const Utils::FilePath path = Utils::FilePath::fromString(remove); const Utils::FilePath path = Utils::FilePath::fromString(remove);
if (auto node = findNode(m_root, path)) { if (auto node = findNode(m_root, path.parentDir())) {
removeFile(node, path); removeFile(node, path);
if (auto module = findModuleFor(node)) if (auto module = findModuleFor(node))
dirtyModules.insert(module); dirtyModules.insert(module);
} }
} }
for (auto module : dirtyModules) createCMakeFiles(m_root);
m_writer->writeModuleCMakeFile(module, m_root); createSourceFiles();
} }
bool CMakeGenerator::isQml(const Utils::FilePath &path) const bool CMakeGenerator::isQml(const Utils::FilePath &path) const
@@ -282,9 +282,8 @@ NodePtr CMakeGenerator::findModuleFor(const NodePtr &node) const
NodePtr CMakeGenerator::findNode(NodePtr &node, const Utils::FilePath &path) const NodePtr CMakeGenerator::findNode(NodePtr &node, const Utils::FilePath &path) const
{ {
const Utils::FilePath parentDir = path.parentDir();
for (NodePtr &child : node->subdirs) { for (NodePtr &child : node->subdirs) {
if (child->dir == parentDir) if (child->dir == path)
return child; return child;
if (path.isChildOf(child->dir)) if (path.isChildOf(child->dir))
return findNode(child, path); return findNode(child, path);
@@ -300,21 +299,34 @@ NodePtr CMakeGenerator::findOrCreateNode(NodePtr &node, const Utils::FilePath &p
if (!path.isChildOf(node->dir)) if (!path.isChildOf(node->dir))
return nullptr; return nullptr;
const Utils::FilePath parentDir = path.parentDir(); auto findSubDir = [](NodePtr &node, const Utils::FilePath &path) -> NodePtr {
const Utils::FilePath relative = parentDir.relativeChildPath(node->dir); for (NodePtr child : node->subdirs) {
if (child->dir == path)
return child;
}
return nullptr;
};
const Utils::FilePath relative = path.relativeChildPath(node->dir);
const QChar separator = relative.pathComponentSeparator(); const QChar separator = relative.pathComponentSeparator();
const QList<QStringView> components = relative.pathView().split(separator); const QList<QStringView> components = relative.pathView().split(separator);
NodePtr last = node; NodePtr lastNode = node;
for (const auto &comp : components) { for (const auto &comp : components) {
NodePtr newNode = std::make_shared<Node>();
newNode->parent = last; Utils::FilePath subPath = lastNode->dir.pathAppended(comp.toString());
newNode->name = comp.toString(); if (NodePtr sub = findSubDir(lastNode, subPath)) {
newNode->dir = last->dir.pathAppended(comp.toString()); lastNode = sub;
last->subdirs.push_back(newNode); continue;
last = newNode;
} }
return last; NodePtr newNode = std::make_shared<Node>();
newNode->parent = lastNode;
newNode->name = comp.toString();
newNode->dir = subPath;
lastNode->subdirs.push_back(newNode);
lastNode = newNode;
}
return lastNode;
} }
bool findFileWithGetter(const Utils::FilePath &file, const NodePtr &node, const FileGetter &getter) bool findFileWithGetter(const Utils::FilePath &file, const NodePtr &node, const FileGetter &getter)

View File

@@ -57,7 +57,14 @@ void CMakeWriterV1::writeRootCMakeFile(const NodePtr &node) const
writeFile(componentPath, compTemplate); writeFile(componentPath, compTemplate);
} }
const Utils::FilePath sharedFile = node->dir.pathAppended("CMakeLists.txt.shared");
if (!sharedFile.exists()) {
const QString sharedTemplate = readTemplate(":/templates/cmake_shared");
writeFile(sharedFile, sharedTemplate);
}
const Utils::FilePath file = node->dir.pathAppended("CMakeLists.txt"); const Utils::FilePath file = node->dir.pathAppended("CMakeLists.txt");
if (!file.exists()) {
const QString appName = parent()->projectName() + "App"; const QString appName = parent()->projectName() + "App";
QString fileSection = ""; QString fileSection = "";
@@ -68,11 +75,14 @@ void CMakeWriterV1::writeRootCMakeFile(const NodePtr &node) const
const QString fileTemplate = readTemplate(":/templates/cmakeroot_v1"); const QString fileTemplate = readTemplate(":/templates/cmakeroot_v1");
const QString fileContent = fileTemplate.arg(appName, fileSection); const QString fileContent = fileTemplate.arg(appName, fileSection);
writeFile(file, fileContent); writeFile(file, fileContent);
}
}
const Utils::FilePath sharedFile = node->dir.pathAppended("CMakeLists.txt.shared"); void CMakeWriterV1::writeModuleCMakeFile(const NodePtr &node, const NodePtr &) const
const QString sharedTemplate = readTemplate(":/templates/cmake_shared"); {
writeFile(sharedFile, sharedTemplate); QTC_ASSERT(parent(), return);
if (node->type == Node::Type::App) {
const Utils::FilePath userFile = node->dir.pathAppended("qds.cmake"); const Utils::FilePath userFile = node->dir.pathAppended("qds.cmake");
QString userFileContent(DO_NOT_EDIT_FILE); QString userFileContent(DO_NOT_EDIT_FILE);
userFileContent.append(makeSubdirectoriesBlock(node)); userFileContent.append(makeSubdirectoriesBlock(node));
@@ -91,16 +101,9 @@ void CMakeWriterV1::writeRootCMakeFile(const NodePtr &node) const
"%1)"); "%1)");
userFileContent.append(linkLibrariesTemplate.arg(pluginNames)); userFileContent.append(linkLibrariesTemplate.arg(pluginNames));
writeFile(userFile, userFileContent); writeFile(userFile, userFileContent);
}
void CMakeWriterV1::writeModuleCMakeFile(const NodePtr &node, const NodePtr &) const
{
QTC_ASSERT(parent(), return);
if (node->type == Node::Type::App)
return; return;
}
Utils::FilePath writeToFile = node->dir.pathAppended("CMakeLists.txt"); Utils::FilePath writeToFile = node->dir.pathAppended("CMakeLists.txt");
if (node->type == Node::Type::Folder && parent()->hasChildModule(node)) { if (node->type == Node::Type::Folder && parent()->hasChildModule(node)) {