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) {
Utils::FilePath subPath = lastNode->dir.pathAppended(comp.toString());
if (NodePtr sub = findSubDir(lastNode, subPath)) {
lastNode = sub;
continue;
}
NodePtr newNode = std::make_shared<Node>(); NodePtr newNode = std::make_shared<Node>();
newNode->parent = last; newNode->parent = lastNode;
newNode->name = comp.toString(); newNode->name = comp.toString();
newNode->dir = last->dir.pathAppended(comp.toString()); newNode->dir = subPath;
last->subdirs.push_back(newNode); lastNode->subdirs.push_back(newNode);
last = newNode; lastNode = newNode;
} }
return last; 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,50 +57,53 @@ void CMakeWriterV1::writeRootCMakeFile(const NodePtr &node) const
writeFile(componentPath, compTemplate); writeFile(componentPath, compTemplate);
} }
const Utils::FilePath file = node->dir.pathAppended("CMakeLists.txt");
const QString appName = parent()->projectName() + "App";
QString fileSection = "";
const QString configFile = getEnvironmentVariable(ENV_VARIABLE_CONTROLCONF);
if (!configFile.isEmpty())
fileSection = QString("\t\t%1").arg(configFile);
const QString fileTemplate = readTemplate(":/templates/cmakeroot_v1");
const QString fileContent = fileTemplate.arg(appName, fileSection);
writeFile(file, fileContent);
const Utils::FilePath sharedFile = node->dir.pathAppended("CMakeLists.txt.shared"); const Utils::FilePath sharedFile = node->dir.pathAppended("CMakeLists.txt.shared");
const QString sharedTemplate = readTemplate(":/templates/cmake_shared"); if (!sharedFile.exists()) {
writeFile(sharedFile, sharedTemplate); const QString sharedTemplate = readTemplate(":/templates/cmake_shared");
writeFile(sharedFile, sharedTemplate);
const Utils::FilePath userFile = node->dir.pathAppended("qds.cmake");
QString userFileContent(DO_NOT_EDIT_FILE);
userFileContent.append(makeSubdirectoriesBlock(node));
userFileContent.append("\n");
QString pluginNames;
std::vector<QString> plugs = plugins(node);
for (size_t i = 0; i < plugs.size(); ++i) {
pluginNames.append("\t" + plugs[i] + "plugin");
if (i != plugs.size() - 1)
pluginNames.append("\n");
} }
QString linkLibrariesTemplate( const Utils::FilePath file = node->dir.pathAppended("CMakeLists.txt");
"target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE\n" if (!file.exists()) {
"%1)"); const QString appName = parent()->projectName() + "App";
userFileContent.append(linkLibrariesTemplate.arg(pluginNames)); QString fileSection = "";
const QString configFile = getEnvironmentVariable(ENV_VARIABLE_CONTROLCONF);
if (!configFile.isEmpty())
fileSection = QString("\t\t%1").arg(configFile);
writeFile(userFile, userFileContent); const QString fileTemplate = readTemplate(":/templates/cmakeroot_v1");
const QString fileContent = fileTemplate.arg(appName, fileSection);
writeFile(file, fileContent);
}
} }
void CMakeWriterV1::writeModuleCMakeFile(const NodePtr &node, const NodePtr &) const void CMakeWriterV1::writeModuleCMakeFile(const NodePtr &node, const NodePtr &) const
{ {
QTC_ASSERT(parent(), return); QTC_ASSERT(parent(), return);
if (node->type == Node::Type::App) if (node->type == Node::Type::App) {
const Utils::FilePath userFile = node->dir.pathAppended("qds.cmake");
QString userFileContent(DO_NOT_EDIT_FILE);
userFileContent.append(makeSubdirectoriesBlock(node));
userFileContent.append("\n");
QString pluginNames;
std::vector<QString> plugs = plugins(node);
for (size_t i = 0; i < plugs.size(); ++i) {
pluginNames.append("\t" + plugs[i] + "plugin");
if (i != plugs.size() - 1)
pluginNames.append("\n");
}
QString linkLibrariesTemplate(
"target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE\n"
"%1)");
userFileContent.append(linkLibrariesTemplate.arg(pluginNames));
writeFile(userFile, userFileContent);
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)) {