Commit 84218e18 authored by Stephen Kelly's avatar Stephen Kelly
Browse files

Add automatic uic invocation for Qt.

The source files are already processed by cmQtAutomoc to look for
moc includes, so extend that to also look for ui_ includes and
find corresponding .ui files to process.

This replaces the need to invoke qt4_wrap_ui().

As the ui files are not likely to be part of the SOURCES of the
target, store the options associated with them separately in the
cmMakefile for querying during the autogen run.
parent 94a0ca60
......@@ -73,6 +73,8 @@ Properties on Targets
/prop_tgt/ARCHIVE_OUTPUT_NAME
/prop_tgt/AUTOMOC_MOC_OPTIONS
/prop_tgt/AUTOMOC
/prop_tgt/AUTOUIC
/prop_tgt/AUTOUIC_OPTIONS
/prop_tgt/BUILD_WITH_INSTALL_RPATH
/prop_tgt/BUNDLE_EXTENSION
/prop_tgt/BUNDLE
......@@ -234,6 +236,7 @@ Properties on Source Files
.. toctree::
/prop_sf/ABSTRACT
/prop_sf/AUTOUIC_OPTIONS
/prop_sf/COMPILE_DEFINITIONS_CONFIG
/prop_sf/COMPILE_DEFINITIONS
/prop_sf/COMPILE_FLAGS
......
......@@ -155,6 +155,8 @@ Variables that Control the Build
/variable/CMAKE_ARCHIVE_OUTPUT_DIRECTORY
/variable/CMAKE_AUTOMOC_MOC_OPTIONS
/variable/CMAKE_AUTOMOC
/variable/CMAKE_AUTOUIC
/variable/CMAKE_AUTOUIC_OPTIONS
/variable/CMAKE_BUILD_WITH_INSTALL_RPATH
/variable/CMAKE_CONFIG_POSTFIX
/variable/CMAKE_DEBUG_POSTFIX
......
AUTOUIC_OPTIONS
---------------
Additional options for uic when using autouic (see the :prop_tgt:`AUTOUIC` target property)
This property holds additional command line options
which will be used when uic is executed during the build via autouic,
i.e. it is equivalent to the optional OPTIONS argument of the
qt4_wrap_ui() macro.
By default it is empty.
The options set on the .ui source file may override :prop_tgt:`AUTOUIC_OPTIONS` set
on the target.
AUTOUIC
-------
Should the target be processed with autouic (for Qt projects).
AUTOUIC is a boolean specifying whether CMake will handle
the Qt uic code generator automatically, i.e. without having to use
the QT4_WRAP_UI() or QT5_WRAP_UI() macro. Currently Qt4 and Qt5 are
supported.
When this property is set to TRUE, CMake will scan the source files
at build time and invoke uic accordingly.
If an #include statement like #include "ui_foo.h" is found in
foo.cpp, a foo.ui file is expected next to foo.cpp, and uic is
run on the foo.ui file.
This property is initialized by the value of the :variable:`CMAKE_AUTOUIC`
variable if it is set when a target is created.
Additional command line options for uic can be set via the
:prop_sf:`AUTOUIC_OPTIONS` source file property on the foo.ui file.
The global property :prop_gbl:`AUTOGEN_TARGETS_FOLDER` can be used to group the
autouic targets together in an IDE, e.g. in MSVS.
AUTOUIC_OPTIONS
---------------
Additional options for uic when using autouic (see the :prop_tgt:`AUTOUIC` target property)
This property holds additional command line options
which will be used when uic is executed during the build via autouic,
i.e. it is equivalent to the optional OPTIONS argument of the
qt4_wrap_ui() macro.
By default it is empty.
This property is initialized by the value of the variable
:variable:`CMAKE_AUTOUIC` if it is set when a target is created.
The options set on the target may be overridden by :prop_sf:`AUTOUIC_OPTIONS` set
on the .ui source file.
CMAKE_AUTOUIC
-------------
Whether to handle uic automatically for Qt targets.
This variable is used to initialize the :prop_tgt:`AUTOUIC` property on all the targets.
See that target property for additional information.
CMAKE_AUTOUIC_OPTIONS
---------------------
Whether to handle uic automatically for Qt targets.
This variable is used to initialize the :prop_tgt:`AUTOUIC_OPTIONS` property on
all the targets. See that target property for additional information.
set(AM_SOURCES @_moc_files@ )
set(AM_SKIP_MOC @_skip_moc@ )
set(AM_SKIP_UIC @_skip_uic@ )
set(AM_HEADERS @_moc_headers@ )
set(AM_MOC_COMPILE_DEFINITIONS @_moc_compile_defs@)
set(AM_MOC_INCLUDES @_moc_incs@)
......@@ -8,8 +9,12 @@ set(AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE "@CMAKE_INCLUDE_DIRECTORIES_PROJ
set(AM_CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@/")
set(AM_CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@/")
set(AM_QT_MOC_EXECUTABLE "@_qt_moc_executable@")
set(AM_QT_UIC_EXECUTABLE "@_qt_uic_executable@")
set(AM_CMAKE_CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@/")
set(AM_CMAKE_CURRENT_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@/")
set(AM_QT_VERSION_MAJOR "@_target_qt_version@")
set(AM_TARGET_NAME @_moc_target_name@)
set(AM_RELAXED_MODE "@_moc_relaxed_mode@")
set(AM_UIC_TARGET_OPTIONS @_uic_target_options@)
set(AM_UIC_OPTIONS_FILES @_qt_uic_options_files@)
set(AM_UIC_OPTIONS_OPTIONS @_qt_uic_options_options@)
......@@ -1225,7 +1225,9 @@ void cmGlobalGenerator::CreateQtAutoGeneratorsTargets()
target.GetType() == cmTarget::MODULE_LIBRARY ||
target.GetType() == cmTarget::OBJECT_LIBRARY)
{
if(target.GetPropertyAsBool("AUTOMOC") && !target.IsImported())
if((target.GetPropertyAsBool("AUTOMOC")
|| target.GetPropertyAsBool("AUTOUIC"))
&& !target.IsImported())
{
cmQtAutoGenerators autogen;
if(autogen.InitializeMocSourceFile(&target))
......
......@@ -4239,6 +4239,18 @@ bool cmMakefile::EnforceUniqueDir(const char* srcPath, const char* binPath)
return false;
}
//----------------------------------------------------------------------------
void cmMakefile::AddQtUiFileWithOptions(cmSourceFile *sf)
{
this->QtUiFilesWithOptions.push_back(sf);
}
//----------------------------------------------------------------------------
std::vector<cmSourceFile*> cmMakefile::GetQtUiFilesWithOptions() const
{
return this->QtUiFilesWithOptions;
}
//----------------------------------------------------------------------------
cmPolicies::PolicyStatus
cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id)
......
......@@ -884,6 +884,9 @@ public:
bool IsGeneratingBuildSystem(){ return this->GeneratingBuildSystem; }
void SetGeneratingBuildSystem(){ this->GeneratingBuildSystem = true; }
void AddQtUiFileWithOptions(cmSourceFile *sf);
std::vector<cmSourceFile*> GetQtUiFilesWithOptions() const;
std::set<cmStdString> const & GetSystemIncludeDirectories() const
{ return this->SystemIncludeDirectories; }
......@@ -1061,6 +1064,8 @@ private:
cmSourceFile* source);
void UpdateOutputToSourceMap(std::string const& output,
cmSourceFile* source);
std::vector<cmSourceFile*> QtUiFilesWithOptions;
};
//----------------------------------------------------------------------------
......
......@@ -23,6 +23,7 @@
#include <cmsys/Terminal.h>
#include <cmsys/ios/sstream>
#include <assert.h>
#include <string.h>
#if defined(__APPLE__)
......@@ -117,6 +118,7 @@ cmQtAutoGenerators::cmQtAutoGenerators()
:Verbose(cmsys::SystemTools::GetEnv("VERBOSE") != 0)
,ColorOutput(true)
,RunMocFailed(false)
,RunUicFailed(false)
,GenerateAll(false)
{
......@@ -255,7 +257,22 @@ void cmQtAutoGenerators::SetupAutoGenerateTarget(cmTarget* target)
"", makefile->GetCurrentOutputDirectory());
std::vector<std::string> depends;
std::string tools = "moc";
std::vector<std::string> toolNames;
if (target->GetPropertyAsBool("AUTOMOC"))
{
toolNames.push_back("moc");
}
if (target->GetPropertyAsBool("AUTOUIC"))
{
toolNames.push_back("uic");
}
std::string tools = toolNames[0];
toolNames.erase(toolNames.begin());
if (toolNames.size() == 1)
{
tools += " and " + toolNames[0];
}
std::string autogenComment = "Automatic " + tools + " for target ";
autogenComment += targetName;
......@@ -322,6 +339,10 @@ void cmQtAutoGenerators::SetupAutoGenerateTarget(cmTarget* target)
this->SetupAutoMocTarget(target, autogenTargetName,
configIncludes, configDefines);
}
if (target->GetPropertyAsBool("AUTOUIC"))
{
this->SetupAutoUicTarget(target);
}
const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
std::string inputFile = cmakeRoot;
......@@ -499,6 +520,156 @@ void cmQtAutoGenerators::SetupAutoMocTarget(cmTarget* target,
}
}
void cmQtAutoGenerators::MergeUicOptions(std::vector<std::string> &opts,
const std::vector<std::string> &fileOpts,
bool isQt5)
{
static const char* valueOptions[] = {
"tr",
"translate",
"postfix",
"generator",
"g"
};
std::vector<std::string> extraOpts;
for(std::vector<std::string>::const_iterator it = fileOpts.begin();
it != fileOpts.end(); ++it)
{
std::vector<std::string>::iterator existingIt
= std::find(opts.begin(), opts.end(), *it);
if (existingIt != opts.end())
{
const char *o = it->c_str();
if (*o == '-')
{
++o;
}
if (isQt5 && *o == '-')
{
++o;
}
if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
cmStrCmp(o)) != cmArrayEnd(valueOptions))
{
assert(existingIt + 1 != opts.end());
*(existingIt + 1) = *(it + 1);
++it;
}
}
else
{
extraOpts.push_back(*it);
}
}
opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
}
void cmQtAutoGenerators::SetupAutoUicTarget(cmTarget* target)
{
cmMakefile *makefile = target->GetMakefile();
const char *qtUic = makefile->GetSafeDefinition("QT_UIC_EXECUTABLE");
makefile->AddDefinition("_qt_uic_executable", qtUic);
const std::vector<cmSourceFile*>& srcFiles = target->GetSourceFiles();
std::string skip_uic;
const char *sep = "";
bool skip = target->GetPropertyAsBool("SKIP_AUTOUIC");
std::set<cmStdString> skipped;
for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
fileIt != srcFiles.end();
++fileIt)
{
cmSourceFile* sf = *fileIt;
std::string absFile = cmsys::SystemTools::GetRealPath(
sf->GetFullPath().c_str());
if (!skip)
{
skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOUIC"));
}
if (skip)
{
skip_uic += sep;
skip_uic += absFile;
sep = ";";
skipped.insert(absFile);
}
}
makefile->AddDefinition("_skip_uic",
cmLocalGenerator::EscapeForCMake(skip_uic.c_str()).c_str());
std::vector<cmSourceFile*> uiFilesWithOptions
= makefile->GetQtUiFilesWithOptions();
std::string uiFileFiles;
std::string uiFileOptions;
sep = "";
const char *qtVersion = makefile->GetDefinition("_target_qt_version");
if (const char* opts = target->GetProperty("AUTOUIC_OPTIONS"))
{
makefile->AddDefinition("_uic_target_options",
cmLocalGenerator::EscapeForCMake(opts).c_str());
}
for(std::vector<cmSourceFile*>::const_iterator fileIt =
uiFilesWithOptions.begin();
fileIt != uiFilesWithOptions.end();
++fileIt)
{
cmSourceFile* sf = *fileIt;
std::string absFile = cmsys::SystemTools::GetRealPath(
sf->GetFullPath().c_str());
if (!skipped.insert(absFile).second)
{
continue;
}
uiFileFiles += sep;
uiFileFiles += absFile;
uiFileOptions += sep;
std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
cmSystemTools::ReplaceString(opts, ";", "@list_sep@");
uiFileOptions += opts;
sep = ";";
}
makefile->AddDefinition("_qt_uic_options_files",
cmLocalGenerator::EscapeForCMake(uiFileFiles.c_str()).c_str());
makefile->AddDefinition("_qt_uic_options_options",
cmLocalGenerator::EscapeForCMake(uiFileOptions.c_str()).c_str());
const char* targetName = target->GetName();
if (strcmp(qtVersion, "5") == 0)
{
cmTarget *qt5Uic = makefile->FindTargetToUse("Qt5::uic");
if (!qt5Uic)
{
// Project does not use Qt5Widgets, but has AUTOUIC ON anyway
makefile->RemoveDefinition("_qt_uic_executable");
}
else
{
makefile->AddDefinition("_qt_uic_executable", qt5Uic->GetLocation(0));
}
}
else
{
if (strcmp(qtVersion, "4") != 0)
{
cmSystemTools::Error("The CMAKE_AUTOUIC feature supports only Qt 4 and "
"Qt 5 ", targetName);
}
}
}
bool cmQtAutoGenerators::Run(const char* targetDirectory, const char *config)
{
bool success = true;
......@@ -563,12 +734,15 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(cmMakefile* makefile,
"AM_Qt5Core_VERSION_MAJOR");
}
this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
this->SkipMoc = makefile->GetSafeDefinition("AM_SKIP_MOC");
this->SkipUic = makefile->GetSafeDefinition("AM_SKIP_UIC");
this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
this->IncludeProjectDirsBefore = makefile->IsOn(
"AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
std::string compileDefsPropOrig = "AM_MOC_COMPILE_DEFINITIONS";
std::string compileDefsProp = compileDefsPropOrig;
if(config)
......@@ -594,6 +768,31 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(cmMakefile* makefile,
this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
{
const char *uicOptionsFiles
= makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES");
const char *uicTargetOptions
= makefile->GetSafeDefinition("AM_UIC_TARGET_OPTIONS");
cmSystemTools::ExpandListArgument(uicTargetOptions, this->UicTargetOptions);
const char *uicOptionsOptions
= makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS");
std::vector<std::string> uicFilesVec;
cmSystemTools::ExpandListArgument(uicOptionsFiles, uicFilesVec);
std::vector<std::string> uicOptionsVec;
cmSystemTools::ExpandListArgument(uicOptionsOptions, uicOptionsVec);
if (uicFilesVec.size() != uicOptionsVec.size())
{
return false;
}
for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
optionIt = uicOptionsVec.begin();
fileIt != uicFilesVec.end();
++fileIt, ++optionIt)
{
cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
this->UicOptions[*fileIt] = *optionIt;
}
}
this->CurrentCompileSettingsStr = this->MakeCompileSettingsString(makefile);
this->RelaxedMode = makefile->IsOn("AM_RELAXED_MODE");
......@@ -767,10 +966,18 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
const std::vector<std::string>& headerExtensions =
makefile->GetHeaderExtensions();
std::vector<std::string> includedUis;
std::vector<std::string> skippedUis;
std::vector<std::string> uicSkipped;
cmSystemTools::ExpandListArgument(this->SkipUic, uicSkipped);
for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
it != sourceFiles.end();
++it)
{
const bool skipUic = std::find(uicSkipped.begin(), uicSkipped.end(), *it)
!= uicSkipped.end();
std::vector<std::string>& uiFiles = skipUic ? skippedUis : includedUis;
const std::string &absFilename = *it;
if (this->Verbose)
{
......@@ -778,15 +985,37 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
}
if (this->RelaxedMode)
{
this->ParseCppFile(absFilename, headerExtensions, includedMocs);
this->ParseCppFile(absFilename, headerExtensions, includedMocs,
uiFiles);
}
else
{
this->StrictParseCppFile(absFilename, headerExtensions, includedMocs);
this->StrictParseCppFile(absFilename, headerExtensions, includedMocs,
uiFiles);
}
this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
}
{
std::vector<std::string> mocSkipped;
cmSystemTools::ExpandListArgument(this->SkipMoc, mocSkipped);
for (std::vector<std::string>::const_iterator it = mocSkipped.begin();
it != mocSkipped.end();
++it)
{
if (std::find(uicSkipped.begin(), uicSkipped.end(), *it)
!= uicSkipped.end())
{
const std::string &absFilename = *it;
if (this->Verbose)
{
std::cout << "AUTOGEN: Checking " << absFilename << std::endl;
}
this->ParseForUic(absFilename, includedUis);
}
}
}
std::vector<std::string> headerFilesVec;
cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
for (std::vector<std::string>::const_iterator it = headerFilesVec.begin();
......@@ -798,7 +1027,7 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
// key = moc source filepath, value = moc output filename
std::map<std::string, std::string> notIncludedMocs;
this->ParseHeaders(headerFiles, includedMocs, 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
......@@ -808,6 +1037,12 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
{
this->GenerateMoc(it->first, it->second);
}
for(std::vector<std::string>::const_iterator it = includedUis.begin();
it != includedUis.end();
++it)
{
this->GenerateUi(*it);
}
cmsys_ios::stringstream outStream;
outStream << "/* This file is autogenerated, do not edit*/\n";
......@@ -840,6 +1075,12 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
std::cerr << "moc failed..."<< std::endl;
return false;
}
if (this->RunUicFailed)
{
std::cerr << "uic failed..."<< std::endl;
return false;
}
outStream.flush();
std::string automocSource = outStream.str();
if (!automocCppChanged)
......@@ -866,7 +1107,8 @@ bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
const std::vector<std::string>& headerExtensions,
std::map<std::string, std::string>& includedMocs)
std::map<std::string, std::string>& includedMocs,
std::vector<std::string> &includedUis)
{
cmsys::RegularExpression mocIncludeRegExp(
"[\n][ \t]*#[ \t]*include[ \t]+"
......@@ -1007,6 +1249,7 @@ void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
matchOffset += mocIncludeRegExp.end();
} while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
}
this->ParseForUic(absFilename, contentsString, includedUis);
// In this case, check whether the scanned file itself contains a Q_OBJECT.
// If this is the case, the moc_foo.cpp should probably be generated from
......@@ -1047,7 +1290,8 @@ void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
const std::vector<std::string>& headerExtensions,
std::map<std::string, std::string>& includedMocs)
std::map<std::string, std::string>& includedMocs,
std::vector<std::string>& includedUis)
{
cmsys::RegularExpression mocIncludeRegExp(
"[\n][ \t]*#[ \t]*include[ \t]+"
......@@ -1138,6 +1382,7 @@ void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
matchOffset += mocIncludeRegExp.end();
} while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
}
this->ParseForUic(absFilename, contentsString, includedUis);
// In this case, check whether the scanned file itself contains a Q_OBJECT.
// If this is the case, the moc_foo.cpp should probably be generated from
......@@ -1158,6 +1403,61 @@ void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
}
void cmQtAutoGenerators::ParseForUic(const std::string& absFilename,
std::vector<std::string>& includedUis)
{
if (this->UicExecutable.empty())
{
return;
}
const std::string contentsString = this->ReadAll(absFilename);
if (contentsString.empty())
{
std::cerr << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
<< std::endl;
return;
}
this->ParseForUic(absFilename, contentsString, includedUis);
}
void cmQtAutoGenerators::ParseForUic(const std::string&,
const std::string& contentsString,
std::vector<std::string>& includedUis)
{
if (this->UicExecutable.empty())
{
return;
}
cmsys::RegularExpression uiIncludeRegExp(
"[\n][ \t]*#[ \t]*include[ \t]+"
"[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
std::string::size_type matchOffset = 0;
matchOffset = 0;
if ((strstr(contentsString.c_str(), "ui_") != NULL)
&& (uiIncludeRegExp.find(contentsString)))
{
do
{
const std::string currentUi = uiIncludeRegExp.match(1);
std::string basename = cmsys::SystemTools::
GetFilenameWithoutLastExtension(currentUi);
// basename should be the part of the ui filename used for
// finding the correct header, so we need to remove the ui_ part
basename = basename.substr(3);
includedUis.push_back(basename);
matchOffset += uiIncludeRegExp.end();
} while(uiIncludeRegExp.find(contentsString.c_str() + matchOffset));
}
}
void
cmQtAutoGenerators::SearchHeadersForCppFile(const std::string& absFilename,
const std::vector<std::string>& headerExtensions,
......@@ -1197,13 +1497,15 @@ cmQtAutoGenerators::SearchHeadersForCppFile(const std::string& absFilename,