Commit 7630eb19 authored by Tobias Hunger's avatar Tobias Hunger Committed by Brad King
Browse files

cmQtAutoGenInitializer: Reduce code duplication

Do not repeat the code used to find uic/rcc and moc exectuables.
parent a956ab7f
...@@ -35,9 +35,22 @@ ...@@ -35,9 +35,22 @@
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <type_traits>
#include <utility> #include <utility>
#include <vector> #include <vector>
std::string GetQtExecutableTargetName(
const cmQtAutoGen::IntegerVersion& qtVersion, std::string const& executable)
{
if (qtVersion.Major == 5) {
return ("Qt5::" + executable);
}
if (qtVersion.Major == 4) {
return ("Qt4::" + executable);
}
return ("");
}
static std::size_t GetParallelCPUCount() static std::size_t GetParallelCPUCount()
{ {
static std::size_t count = 0; static std::size_t count = 0;
...@@ -544,11 +557,7 @@ bool cmQtAutoGenInitializer::InitMoc() ...@@ -544,11 +557,7 @@ bool cmQtAutoGenInitializer::InitMoc()
} }
// Moc executable // Moc executable
if (!GetMocExecutable()) { return GetMocExecutable();
return false;
}
return true;
} }
bool cmQtAutoGenInitializer::InitUic() bool cmQtAutoGenInitializer::InitUic()
...@@ -625,19 +634,12 @@ bool cmQtAutoGenInitializer::InitUic() ...@@ -625,19 +634,12 @@ bool cmQtAutoGenInitializer::InitUic()
} }
// Uic executable // Uic executable
if (!GetUicExecutable()) { return GetUicExecutable();
return false;
}
return true;
} }
bool cmQtAutoGenInitializer::InitRcc() bool cmQtAutoGenInitializer::InitRcc()
{ {
if (!GetRccExecutable()) { return GetRccExecutable();
return false;
}
return true;
} }
bool cmQtAutoGenInitializer::InitScanFiles() bool cmQtAutoGenInitializer::InitScanFiles()
...@@ -1409,199 +1411,109 @@ cmQtAutoGenInitializer::IntegerVersion cmQtAutoGenInitializer::GetQtVersion( ...@@ -1409,199 +1411,109 @@ cmQtAutoGenInitializer::IntegerVersion cmQtAutoGenInitializer::GetQtVersion(
return cmQtAutoGenInitializer::IntegerVersion(); return cmQtAutoGenInitializer::IntegerVersion();
} }
bool cmQtAutoGenInitializer::GetMocExecutable() std::pair<bool, std::string> GetQtExecutable(
const cmQtAutoGen::IntegerVersion& qtVersion, cmGeneratorTarget* target,
const std::string& executable, bool ignoreMissingTarget, std::string* output)
{ {
std::string err; std::string err;
std::string result;
// Find moc executable // Find executable
{ {
std::string targetName; const std::string targetName =
if (this->QtVersion.Major == 5) { GetQtExecutableTargetName(qtVersion, executable);
targetName = "Qt5::moc"; if (targetName.empty()) {
} else if (this->QtVersion.Major == 4) { err = "The AUTOMOC, AUTOUIC and AUTORCC feature ";
targetName = "Qt4::moc"; err += "supports only Qt 4 and Qt 5";
} else { } else {
err = "The AUTOMOC feature supports only Qt 4 and Qt 5"; cmLocalGenerator* localGen = target->GetLocalGenerator();
}
if (!targetName.empty()) {
cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName); cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
if (tgt != nullptr) { if (tgt != nullptr) {
this->Moc.Executable = tgt->ImportedGetLocation(""); result = tgt->ImportedGetLocation("");
} else { } else {
if (ignoreMissingTarget) {
return std::make_pair(true, "");
}
err = "Could not find target " + targetName; err = "Could not find target " + targetName;
} }
} }
} }
// Test moc command // Test executable
if (err.empty()) { if (err.empty()) {
if (cmSystemTools::FileExists(this->Moc.Executable, true)) { if (cmSystemTools::FileExists(result, true)) {
std::vector<std::string> command; std::vector<std::string> command;
command.push_back(this->Moc.Executable); command.push_back(result);
command.push_back("-h"); command.push_back("-h");
std::string stdOut; std::string stdOut;
std::string stdErr; std::string stdErr;
int retVal = 0; int retVal = 0;
bool result = cmSystemTools::RunSingleCommand( const bool runResult = cmSystemTools::RunSingleCommand(
command, &stdOut, &stdErr, &retVal, nullptr, command, &stdOut, &stdErr, &retVal, nullptr,
cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto); cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
if (!result) { if (!runResult) {
err = "The moc test command failed: "; err = "Test of \"" + executable + "\" binary ";
err += QuotedCommand(command); err += cmQtAutoGen::Quoted(result) + " failed: ";
err += cmQtAutoGen::QuotedCommand(command);
} else {
if (output != nullptr) {
*output = stdOut;
}
} }
} else { } else {
err = "The moc executable "; err = "The \"" + executable + "\" binary ";
err += Quoted(this->Moc.Executable); err += cmQtAutoGen::Quoted(result);
err += " does not exist"; err += " does not exist";
} }
} }
// Print error // Print error
if (!err.empty()) { if (!err.empty()) {
std::string msg = "AutoMoc ("; std::string msg = "AutoGen (";
msg += this->Target->GetName(); msg += target->GetName();
msg += "): "; msg += "): ";
msg += err; msg += err;
cmSystemTools::Error(msg.c_str()); cmSystemTools::Error(msg.c_str());
return false; return std::make_pair(false, "");
} }
return true; return std::make_pair(true, result);
} }
bool cmQtAutoGenInitializer::GetUicExecutable() bool cmQtAutoGenInitializer::GetMocExecutable()
{ {
std::string err; const auto result =
GetQtExecutable(this->QtVersion, this->Target, "moc", false, nullptr);
// Find uic executable this->Moc.Executable = result.second;
{ return result.first;
std::string targetName; }
if (this->QtVersion.Major == 5) {
targetName = "Qt5::uic";
} else if (this->QtVersion.Major == 4) {
targetName = "Qt4::uic";
} else {
err = "The AUTOUIC feature supports only Qt 4 and Qt 5";
}
if (!targetName.empty()) {
cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
if (tgt != nullptr) {
this->Uic.Executable = tgt->ImportedGetLocation("");
} else {
if (this->QtVersion.Major == 5) {
// Project does not use Qt5Widgets, but has AUTOUIC ON anyway
} else {
err = "Could not find target " + targetName;
}
}
}
}
// Test uic command
if (err.empty() && !this->Uic.Executable.empty()) {
if (cmSystemTools::FileExists(this->Uic.Executable, true)) {
std::vector<std::string> command;
command.push_back(this->Uic.Executable);
command.push_back("-h");
std::string stdOut;
std::string stdErr;
int retVal = 0;
bool result = cmSystemTools::RunSingleCommand(
command, &stdOut, &stdErr, &retVal, nullptr,
cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
if (!result) {
err = "The uic test command failed: ";
err += QuotedCommand(command);
}
} else {
err = "The uic executable ";
err += Quoted(this->Uic.Executable);
err += " does not exist";
}
}
// Print error
if (!err.empty()) {
std::string msg = "AutoUic (";
msg += this->Target->GetName();
msg += "): ";
msg += err;
cmSystemTools::Error(msg.c_str());
return false;
}
return true; bool cmQtAutoGenInitializer::GetUicExecutable()
{
const auto result =
GetQtExecutable(this->QtVersion, this->Target, "uic", true, nullptr);
this->Uic.Executable = result.second;
return result.first;
} }
bool cmQtAutoGenInitializer::GetRccExecutable() bool cmQtAutoGenInitializer::GetRccExecutable()
{ {
std::string err; std::string stdOut;
const auto result =
// Find rcc executable GetQtExecutable(this->QtVersion, this->Target, "rcc", false, &stdOut);
{ this->Rcc.Executable = result.second;
std::string targetName; if (!result.first) {
if (this->QtVersion.Major == 5) { return false;
targetName = "Qt5::rcc";
} else if (this->QtVersion.Major == 4) {
targetName = "Qt4::rcc";
} else {
err = "The AUTORCC feature supports only Qt 4 and Qt 5";
}
if (!targetName.empty()) {
cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
if (tgt != nullptr) {
this->Rcc.Executable = tgt->ImportedGetLocation("");
} else {
err = "Could not find target " + targetName;
}
}
} }
// Test rcc command if (this->QtVersion.Major == 5) {
if (err.empty()) { if (stdOut.find("--list") != std::string::npos) {
if (cmSystemTools::FileExists(this->Rcc.Executable, true)) { this->Rcc.ListOptions.push_back("--list");
std::vector<std::string> command;
command.push_back(this->Rcc.Executable);
command.push_back("-h");
std::string stdOut;
std::string stdErr;
int retVal = 0;
bool result = cmSystemTools::RunSingleCommand(
command, &stdOut, &stdErr, &retVal, nullptr,
cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
if (result) {
// Detect if rcc supports (-)-list
if (this->QtVersion.Major == 5) {
if (stdOut.find("--list") != std::string::npos) {
this->Rcc.ListOptions.push_back("--list");
} else {
this->Rcc.ListOptions.push_back("-list");
}
}
} else {
err = "The rcc test command failed: ";
err += QuotedCommand(command);
}
} else { } else {
err = "The rcc executable "; this->Rcc.ListOptions.push_back("-list");
err += Quoted(this->Rcc.Executable);
err += " does not exist";
} }
} }
// Print error
if (!err.empty()) {
std::string msg = "AutoRcc (";
msg += this->Target->GetName();
msg += "): ";
msg += err;
cmSystemTools::Error(msg.c_str());
return false;
}
return true; return true;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment