From 5191b74524e6e2ccc3484989910da8ca46ebb100 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Wed, 28 Nov 2018 14:45:33 +0100 Subject: Autogen: Qt version detection cleanup Split the Qt version detection code up a bit so that it is a bit easier to follow. --- Source/cmQtAutoGenInitializer.cxx | 85 ++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index 49236ed..730bdb7 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -1342,56 +1342,65 @@ void cmQtAutoGenInitializer::AddGeneratedSource(std::string const& filename, this->Target->AddSource(filename); } -cmQtAutoGenInitializer::IntegerVersion cmQtAutoGenInitializer::GetQtVersion( +static unsigned int CharPtrToInt(const char* const input) +{ + unsigned long tmp = 0; + if (input != nullptr && cmSystemTools::StringToULong(input, &tmp)) { + return static_cast(tmp); + } + return 0; +} + +static unsigned int StringToInt(const std::string& input) +{ + return input.empty() ? 0 : CharPtrToInt(input.c_str()); +} + +static std::vector GetKnownQtVersions( cmGeneratorTarget const* target) { - cmQtAutoGenInitializer::IntegerVersion res; cmMakefile* makefile = target->Target->GetMakefile(); - // -- Major version - std::string qtMajor = makefile->GetSafeDefinition("QT_VERSION_MAJOR"); - if (qtMajor.empty()) { - qtMajor = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR"); - } - { - const char* targetQtVersion = - target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""); - if (targetQtVersion != nullptr) { - qtMajor = targetQtVersion; + std::vector result; + for (const std::string& prefix : + std::vector({ "Qt5Core", "QT" })) { + auto tmp = cmQtAutoGenInitializer::IntegerVersion( + StringToInt(makefile->GetSafeDefinition(prefix + "_VERSION_MAJOR")), + StringToInt(makefile->GetSafeDefinition(prefix + "_VERSION_MINOR"))); + if (tmp.Major != 0) { + result.push_back(tmp); } } - // -- Minor version - std::string qtMinor; - if (!qtMajor.empty()) { - if (qtMajor == "5") { - qtMinor = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR"); - } - if (qtMinor.empty()) { - qtMinor = makefile->GetSafeDefinition("QT_VERSION_MINOR"); - } - { - const char* targetQtVersion = - target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION", - ""); - if (targetQtVersion != nullptr) { - qtMinor = targetQtVersion; - } - } + return result; +} + +cmQtAutoGenInitializer::IntegerVersion cmQtAutoGenInitializer::GetQtVersion( + cmGeneratorTarget const* target) +{ + auto knownQtVersions = GetKnownQtVersions(target); + if (knownQtVersions.empty()) { + return cmQtAutoGenInitializer::IntegerVersion(); // No Qt + } + + // Pick a version from the known versions: + auto targetVersion = CharPtrToInt( + target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "")); + + if (targetVersion == 0) { + // No specific version was requested by the target: + // Use highest known Qt version. + return knownQtVersions.at(0); } - // -- Convert to integer - if (!qtMajor.empty() && !qtMinor.empty()) { - unsigned long majorUL(0); - unsigned long minorUL(0); - if (cmSystemTools::StringToULong(qtMajor.c_str(), &majorUL) && - cmSystemTools::StringToULong(qtMinor.c_str(), &minorUL)) { - res.Major = static_cast(majorUL); - res.Minor = static_cast(minorUL); + for (auto it : knownQtVersions) { + if (it.Major == targetVersion) { + return it; } } - return res; + // Requested version was not found + return cmQtAutoGenInitializer::IntegerVersion(); } bool cmQtAutoGenInitializer::GetMocExecutable() -- cgit v0.12