Autogen: Split out moc file generation code to dedicated method

This commit is contained in:
Sebastian Holtermann 2016-04-18 13:46:28 +02:00 committed by Brad King
parent 3ea1d09082
commit cf679ea8dc
2 changed files with 107 additions and 54 deletions

View File

@ -410,9 +410,11 @@ cmQtAutoGenerators::WriteOldMocDefinitionsFile(
void cmQtAutoGenerators::Init()
{
this->OutMocCppFilenameRel = this->TargetName;
this->OutMocCppFilenameRel += ".cpp";
this->OutMocCppFilename = this->Builddir;
this->OutMocCppFilename += this->TargetName;
this->OutMocCppFilename += ".cpp";
this->OutMocCppFilename += this->OutMocCppFilenameRel;
std::vector<std::string> cdefList;
cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
@ -589,14 +591,11 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
std::map<std::string, std::string> notIncludedMocs;
this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs, includedUis);
// run moc on all the moc's that are #included in source files
for(std::map<std::string, std::string>::const_iterator
it = includedMocs.begin();
it != includedMocs.end();
++it)
if(!this->MocExecutable.empty())
{
this->GenerateMoc(it->first, it->second);
this->GenerateMocFiles ( includedMocs, notIncludedMocs );
}
for(std::map<std::string, std::vector<std::string> >::const_iterator
it = includedUis.begin();
it != includedUis.end();
@ -615,38 +614,11 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
this->GenerateQrcFiles();
}
std::stringstream outStream;
outStream << "/* This file is autogenerated, do not edit*/\n";
bool automocCppChanged = false;
if (notIncludedMocs.empty())
{
outStream << "enum some_compilers { need_more_than_nothing };\n";
}
else
{
// run moc on the remaining headers and include them in
// the _automoc.cpp file
for(std::map<std::string, std::string>::const_iterator
it = notIncludedMocs.begin();
it != notIncludedMocs.end();
++it)
{
bool mocSuccess = this->GenerateMoc(it->first, it->second);
if (mocSuccess)
{
automocCppChanged = true;
}
outStream << "#include \"" << it->second << "\"\n";
}
}
if (this->RunMocFailed)
{
std::cerr << "moc failed..." << std::endl;
return false;
}
if (this->RunUicFailed)
{
std::cerr << "uic failed..." << std::endl;
@ -657,25 +629,6 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
std::cerr << "rcc failed..." << std::endl;
return false;
}
outStream.flush();
std::string automocSource = outStream.str();
if (!automocCppChanged)
{
// compare contents of the _automoc.cpp file
const std::string oldContents = ReadAll(this->OutMocCppFilename);
if (oldContents == automocSource)
{
// nothing changed: don't touch the _automoc.cpp file
return true;
}
}
// source file that includes all remaining moc files (_automoc.cpp file)
cmsys::ofstream outfile;
outfile.open(this->OutMocCppFilename.c_str(),
std::ios::trunc);
outfile << automocSource;
outfile.close();
return true;
}
@ -1119,6 +1072,102 @@ void cmQtAutoGenerators::ParseHeaders(const std::set<std::string>& absHeaders,
}
}
bool cmQtAutoGenerators::GenerateMocFiles(
const std::map<std::string, std::string>& includedMocs,
const std::map<std::string, std::string>& notIncludedMocs )
{
// generate moc files that are included by source files.
for(std::map<std::string, std::string>::const_iterator
it = includedMocs.begin(); it != includedMocs.end(); ++it)
{
if (!this->GenerateMoc(it->first, it->second))
{
if (this->RunMocFailed)
{
return false;
}
}
}
// generate moc files that are _not_ included by source files.
bool automocCppChanged = false;
for(std::map<std::string, std::string>::const_iterator
it = notIncludedMocs.begin(); it != notIncludedMocs.end(); ++it)
{
if (this->GenerateMoc(it->first, it->second))
{
automocCppChanged = true;
}
else
{
if (this->RunMocFailed)
{
return false;
}
}
}
// compose _automoc.cpp content
std::string automocSource;
{
std::stringstream outStream;
outStream << "/* This file is autogenerated, do not edit*/\n";
if( notIncludedMocs.empty() )
{
outStream << "enum some_compilers { need_more_than_nothing };\n";
}
else
{
for(std::map<std::string, std::string>::const_iterator
it = notIncludedMocs.begin();
it != notIncludedMocs.end();
++it)
{
outStream << "#include \"" << it->second << "\"\n";
}
}
outStream.flush();
automocSource = outStream.str();
}
// check if we even need to update _automoc.cpp
if (!automocCppChanged)
{
// compare contents of the _automoc.cpp file
const std::string oldContents = ReadAll(this->OutMocCppFilename);
if (oldContents == automocSource)
{
// nothing changed: don't touch the _automoc.cpp file
if (this->Verbose)
{
std::cout << "AUTOGEN: " << this->OutMocCppFilenameRel
<< " still up to date" << std::endl;
}
return true;
}
}
// actually write _automoc.cpp
{
std::string msg = "Generating ";
msg += this->OutMocCppFilenameRel;
cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
|cmsysTerminal_Color_ForegroundBold,
msg.c_str(), true, this->ColorOutput);
}
{
cmsys::ofstream outfile;
outfile.open(this->OutMocCppFilename.c_str(),
std::ios::trunc);
outfile << automocSource;
outfile.close();
}
return true;
}
bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
const std::string& mocFileName)
{

View File

@ -39,6 +39,9 @@ private:
std::string MakeCompileSettingsString(cmMakefile* makefile);
bool RunAutogen(cmMakefile* makefile);
bool GenerateMocFiles(
const std::map<std::string, std::string>& includedMocs,
const std::map<std::string, std::string>& notIncludedMocs);
bool GenerateMoc(const std::string& sourceFile,
const std::string& mocFileName);
bool GenerateUi(const std::string& realName, const std::string& uiFileName);
@ -100,6 +103,7 @@ private:
std::string CurrentCompileSettingsStr;
std::string OldCompileSettingsStr;
std::string OutMocCppFilenameRel;
std::string OutMocCppFilename;
std::list<std::string> MocIncludes;
std::list<std::string> MocDefinitions;