forked from qt-creator/qt-creator
CMakePM: Handle project FindPackage package variables
For example find_package(ZLIB QUEIT) will result in the package variables ZLIB_LIBRARY ZLIB_INCLUDE_DIR. The variables are available for both code completion and navigation. Change-Id: I4ea6090f44a980dc91632fcabbda16987b0f0285 Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
@@ -1337,13 +1337,58 @@ void CMakeBuildSystem::setupCMakeSymbolsHash()
|
|||||||
m_cmakeSymbolsHash.insert(QString::fromUtf8(arg.Value), link);
|
m_cmakeSymbolsHash.insert(QString::fromUtf8(arg.Value), link);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Gather the exported variables for the Find<Package> CMake packages
|
||||||
|
m_projectFindPackageVariables.clear();
|
||||||
|
|
||||||
|
const std::string fphsFunctionName = "find_package_handle_standard_args";
|
||||||
|
CMakeKeywords keywords;
|
||||||
|
if (auto tool = CMakeKitAspect::cmakeTool(target()->kit()))
|
||||||
|
keywords = tool->keywords();
|
||||||
|
QSet<std::string> fphsFunctionArgs;
|
||||||
|
if (keywords.functionArgs.contains(QString::fromStdString(fphsFunctionName))) {
|
||||||
|
const QList<std::string> args
|
||||||
|
= Utils::transform(keywords.functionArgs.value(QString::fromStdString(fphsFunctionName)),
|
||||||
|
&QString::toStdString);
|
||||||
|
fphsFunctionArgs = Utils::toSet(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto handleFindPackageVariables = [&](const CMakeFileInfo &cmakeFile, const cmListFileFunction &func) {
|
||||||
|
if (func.LowerCaseName() != fphsFunctionName)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (func.Arguments().size() == 0)
|
||||||
|
return;
|
||||||
|
auto firstArgument = func.Arguments()[0];
|
||||||
|
const auto filteredArguments = Utils::filtered(func.Arguments(), [&](const auto &arg) {
|
||||||
|
return !fphsFunctionArgs.contains(arg.Value) && arg != firstArgument;
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const auto &arg : filteredArguments) {
|
||||||
|
const QString value = QString::fromUtf8(arg.Value);
|
||||||
|
if (value.contains("${") || (value.startsWith('"') && value.endsWith('"'))
|
||||||
|
|| (value.startsWith("'") && value.endsWith("'")))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
m_projectFindPackageVariables << value;
|
||||||
|
|
||||||
|
Utils::Link link;
|
||||||
|
link.targetFilePath = cmakeFile.path;
|
||||||
|
link.targetLine = arg.Line;
|
||||||
|
link.targetColumn = arg.Column - 1;
|
||||||
|
m_cmakeSymbolsHash.insert(value, link);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (const auto &cmakeFile : std::as_const(m_cmakeFiles)) {
|
for (const auto &cmakeFile : std::as_const(m_cmakeFiles)) {
|
||||||
for (const auto &func : cmakeFile.cmakeListFile.Functions) {
|
for (const auto &func : cmakeFile.cmakeListFile.Functions) {
|
||||||
handleFunctionMacroOption(cmakeFile, func);
|
handleFunctionMacroOption(cmakeFile, func);
|
||||||
handleImportedTargets(cmakeFile, func);
|
handleImportedTargets(cmakeFile, func);
|
||||||
handleProjectTargets(cmakeFile, func);
|
handleProjectTargets(cmakeFile, func);
|
||||||
|
handleFindPackageVariables(cmakeFile, func);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_projectFindPackageVariables.removeDuplicates();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMakeBuildSystem::ensureBuildDirectory(const BuildDirParameters ¶meters)
|
void CMakeBuildSystem::ensureBuildDirectory(const BuildDirParameters ¶meters)
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ public:
|
|||||||
const QHash<QString, Utils::Link> &cmakeSymbolsHash() const { return m_cmakeSymbolsHash; }
|
const QHash<QString, Utils::Link> &cmakeSymbolsHash() const { return m_cmakeSymbolsHash; }
|
||||||
CMakeKeywords projectKeywords() const { return m_projectKeywords; }
|
CMakeKeywords projectKeywords() const { return m_projectKeywords; }
|
||||||
QStringList projectImportedTargets() const { return m_projectImportedTargets; }
|
QStringList projectImportedTargets() const { return m_projectImportedTargets; }
|
||||||
|
QStringList projectFindPackageVariables() const { return m_projectFindPackageVariables; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void configurationCleared();
|
void configurationCleared();
|
||||||
@@ -229,6 +230,7 @@ private:
|
|||||||
QHash<QString, Utils::Link> m_cmakeSymbolsHash;
|
QHash<QString, Utils::Link> m_cmakeSymbolsHash;
|
||||||
CMakeKeywords m_projectKeywords;
|
CMakeKeywords m_projectKeywords;
|
||||||
QStringList m_projectImportedTargets;
|
QStringList m_projectImportedTargets;
|
||||||
|
QStringList m_projectFindPackageVariables;
|
||||||
|
|
||||||
QHash<QString, ProjectFileArgumentPosition> m_filesToBeRenamed;
|
QHash<QString, ProjectFileArgumentPosition> m_filesToBeRenamed;
|
||||||
|
|
||||||
|
|||||||
@@ -248,6 +248,10 @@ void CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
|
|||||||
|
|
||||||
// Check if the symbols is a user defined function or macro
|
// Check if the symbols is a user defined function or macro
|
||||||
const CMakeBuildSystem *cbs = static_cast<const CMakeBuildSystem *>(bs);
|
const CMakeBuildSystem *cbs = static_cast<const CMakeBuildSystem *>(bs);
|
||||||
|
// Strip variable coating
|
||||||
|
if (buffer.startsWith("${") && buffer.endsWith("}"))
|
||||||
|
buffer = buffer.mid(2, buffer.size() - 3);
|
||||||
|
|
||||||
if (cbs->cmakeSymbolsHash().contains(buffer)) {
|
if (cbs->cmakeSymbolsHash().contains(buffer)) {
|
||||||
link = cbs->cmakeSymbolsHash().value(buffer);
|
link = cbs->cmakeSymbolsHash().value(buffer);
|
||||||
addTextStartEndToLink(link);
|
addTextStartEndToLink(link);
|
||||||
|
|||||||
@@ -254,12 +254,14 @@ IAssistProposal *CMakeFileCompletionAssist::performAsync()
|
|||||||
|
|
||||||
QStringList buildTargets;
|
QStringList buildTargets;
|
||||||
QStringList importedTargets;
|
QStringList importedTargets;
|
||||||
|
QStringList findPackageVariables;
|
||||||
if (auto bs = qobject_cast<CMakeBuildSystem *>(ProjectTree::currentBuildSystem())) {
|
if (auto bs = qobject_cast<CMakeBuildSystem *>(ProjectTree::currentBuildSystem())) {
|
||||||
for (const auto &target : std::as_const(bs->buildTargets()))
|
for (const auto &target : std::as_const(bs->buildTargets()))
|
||||||
if (target.targetType != TargetType::UtilityType)
|
if (target.targetType != TargetType::UtilityType)
|
||||||
buildTargets << target.title;
|
buildTargets << target.title;
|
||||||
projectKeywords = bs->projectKeywords();
|
projectKeywords = bs->projectKeywords();
|
||||||
importedTargets = bs->projectImportedTargets();
|
importedTargets = bs->projectImportedTargets();
|
||||||
|
findPackageVariables = bs->projectFindPackageVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isInComment(interface()))
|
if (isInComment(interface()))
|
||||||
@@ -296,6 +298,7 @@ IAssistProposal *CMakeFileCompletionAssist::performAsync()
|
|||||||
if (varGenexToken == "${") {
|
if (varGenexToken == "${") {
|
||||||
items.append(generateList(keywords.variables, m_variableIcon));
|
items.append(generateList(keywords.variables, m_variableIcon));
|
||||||
items.append(generateList(projectKeywords.variables, m_projectVariableIcon));
|
items.append(generateList(projectKeywords.variables, m_projectVariableIcon));
|
||||||
|
items.append(generateList(findPackageVariables, m_projectVariableIcon));
|
||||||
}
|
}
|
||||||
if (varGenexToken == "$<")
|
if (varGenexToken == "$<")
|
||||||
items.append(generateList(keywords.generatorExpressions, m_genexIcon));
|
items.append(generateList(keywords.generatorExpressions, m_genexIcon));
|
||||||
@@ -311,6 +314,7 @@ IAssistProposal *CMakeFileCompletionAssist::performAsync()
|
|||||||
|| functionName == "cmake_print_variables") {
|
|| functionName == "cmake_print_variables") {
|
||||||
items.append(generateList(keywords.variables, m_variableIcon));
|
items.append(generateList(keywords.variables, m_variableIcon));
|
||||||
items.append(generateList(projectKeywords.variables, m_projectVariableIcon));
|
items.append(generateList(projectKeywords.variables, m_projectVariableIcon));
|
||||||
|
items.append(generateList(findPackageVariables, m_projectVariableIcon));
|
||||||
items.append(generateList(localVariables, m_variableIcon));
|
items.append(generateList(localVariables, m_variableIcon));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,6 +373,7 @@ IAssistProposal *CMakeFileCompletionAssist::performAsync()
|
|||||||
items.append(generateList(keywords.variables, m_variableIcon));
|
items.append(generateList(keywords.variables, m_variableIcon));
|
||||||
items.append(generateList(projectKeywords.variables, m_projectVariableIcon));
|
items.append(generateList(projectKeywords.variables, m_projectVariableIcon));
|
||||||
items.append(generateList(localVariables, m_variableIcon));
|
items.append(generateList(localVariables, m_variableIcon));
|
||||||
|
items.append(generateList(findPackageVariables, m_projectVariableIcon));
|
||||||
|
|
||||||
items.append(generateList(keywords.properties, m_propertyIcon));
|
items.append(generateList(keywords.properties, m_propertyIcon));
|
||||||
items.append(generateList(buildTargets, m_targetsIcon));
|
items.append(generateList(buildTargets, m_targetsIcon));
|
||||||
|
|||||||
Reference in New Issue
Block a user