From 874b1133d9cfaef179851aa925b7d6b96e85019b Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Wed, 26 Apr 2023 19:29:37 +0200 Subject: [PATCH] CMakePM: Allow file rename / remove via variables Lookup any variables found in the target definition function in order to find the source files. This works for something like this: set(SOURCE_FILES myfile.cpp) add_executable(myexe ${SOURCE_FILES}) Change-Id: I8a47ea64b4efa467074f03ed5e1d1d05b2b1bf00 Reviewed-by: Reviewed-by: hjk --- .../cmakeprojectmanager/cmakebuildsystem.cpp | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp index d3bfcd720bc..a493062621c 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp @@ -422,6 +422,7 @@ CMakeBuildSystem::projectFileArgumentPosition(const QString &targetName, const Q if (!filePathArgument.Value.empty()) { return ProjectFileArgumentPosition{filePathArgument, targetCMakeFile, fileName}; } else { + // Check if the filename is part of globbing variable result const auto globFunctions = std::get<0>( Utils::partition(cmakeListFile.Functions, [](const auto &f) { return f.LowerCaseName() == "file" && f.Arguments().size() > 2 @@ -440,11 +441,43 @@ CMakeBuildSystem::projectFileArgumentPosition(const QString &targetName, const Q != cmListFileArgument::Comment; }); - if (haveGlobbing) + if (haveGlobbing) { return ProjectFileArgumentPosition{filePathArgument, targetCMakeFile, fileName, true}; + } + + // Check if the filename is part of a variable set by the user + const auto setFunctions = std::get<0>( + Utils::partition(cmakeListFile.Functions, [](const auto &f) { + return f.LowerCaseName() == "set" && f.Arguments().size() > 1; + })); + + for (const auto &arg : func->Arguments()) { + if (arg.Delim == cmListFileArgument::Comment) + continue; + + auto matchedFunctions = Utils::filtered(setFunctions, [arg](const auto &f) { + return arg.Value == std::string("${") + f.Arguments()[0].Value + "}"; + }); + + for (const auto &f : matchedFunctions) { + filePathArgument + = Utils::findOrDefault(f.Arguments(), + [file_name = fileName.toStdString()]( + const auto &arg) { + return arg.Delim != cmListFileArgument::Comment + && arg.Value == file_name; + }); + + if (!filePathArgument.Value.empty()) { + return ProjectFileArgumentPosition{filePathArgument, + targetCMakeFile, + fileName}; + } + } + } } }