Commit dbda5906 authored by Sebastian Holtermann's avatar Sebastian Holtermann
Browse files

Autogen: Per-config file suffixes. New AUTOGEN_BUILD_DIR target property.

Closes #14760
Closes #14313
parent 0965002e
......@@ -119,6 +119,7 @@ Properties on Targets
/prop_tgt/ARCHIVE_OUTPUT_DIRECTORY
/prop_tgt/ARCHIVE_OUTPUT_NAME_CONFIG
/prop_tgt/ARCHIVE_OUTPUT_NAME
/prop_tgt/AUTOGEN_BUILD_DIR
/prop_tgt/AUTOGEN_TARGET_DEPENDS
/prop_tgt/AUTOMOC_DEPEND_FILTERS
/prop_tgt/AUTOMOC_MOC_OPTIONS
......
......@@ -68,14 +68,24 @@ be included by the user in the C++ implementation file with a preprocessor
``#include``.
Included ``moc_*.cpp`` and ``*.moc`` files will be generated in the
``<CMAKE_CURRENT_BINARY_DIR>/<TARGETNAME>_autogen/include`` directory which is
``<AUTOGEN_BUILD_DIR>/include`` directory which is
automatically added to the target's :prop_tgt:`INCLUDE_DIRECTORIES`.
(This differs from CMake 3.7 and below; see their documentation for details.)
* For multi configuration generators, except Xcode, the include directory is
``<AUTOGEN_BUILD_DIR>/include_<CONFIG>``.
* See :prop_tgt:`AUTOGEN_BUILD_DIR`.
Not included ``moc_<basename>.cpp`` files will be generated in custom
folders to avoid name collisions and included in a separate
``<CMAKE_CURRENT_BINARY_DIR>/<TARGETNAME>_autogen/moc_compilation.cpp``
file which is compiled into the target.
``<AUTOGEN_BUILD_DIR>/mocs_compilation.cpp`` file which is compiled
into the target.
* For multi configuration generators, except Xcode, the file is
``<AUTOGEN_BUILD_DIR>/mocs_compilation_<CONFIG>.cpp``.
* See :prop_tgt:`AUTOGEN_BUILD_DIR`.
The ``moc`` command line will consume the :prop_tgt:`COMPILE_DEFINITIONS` and
:prop_tgt:`INCLUDE_DIRECTORIES` target properties from the target it is being
......@@ -109,10 +119,15 @@ searched for first in the vicinity of including file and afterwards in the
optional :prop_tgt:`AUTOUIC_SEARCH_PATHS` of the target.
The generated generated ``ui_*.h`` files are placed in the
``<CMAKE_CURRENT_BINARY_DIR>/<TARGETNAME>_autogen/include`` directory which is
``<AUTOGEN_BUILD_DIR>/include`` directory which is
automatically added to the target's :prop_tgt:`INCLUDE_DIRECTORIES`.
(This differs from CMake 3.7 and below; see their documentation for details.)
* For multi configuration generators, except Xcode, the include directory is
``<AUTOGEN_BUILD_DIR>/include_<CONFIG>``.
* See :prop_tgt:`AUTOGEN_BUILD_DIR`.
The :prop_tgt:`AUTOUIC` target property may be pre-set for all following
targets by setting the :variable:`CMAKE_AUTOUIC` variable. The
:prop_tgt:`AUTOUIC_OPTIONS` target property may be populated to set options
......
AUTOGEN_BUILD_DIR
-----------------
Directory where :prop_tgt:`AUTOMOC`, :prop_tgt:`AUTOUIC` and :prop_tgt:`AUTORCC`
generate files for the target.
The directory is created on demand and automatically added to the
:prop_dir:`ADDITIONAL_MAKE_CLEAN_FILES`.
When unset or empty the directory ``<dir>/<target-name>_autogen`` is used where
``<dir>`` is :variable:`CMAKE_CURRENT_BINARY_DIR` and ``<target-name>``
is :prop_tgt:`NAME`.
By default :prop_tgt:`AUTOGEN_BUILD_DIR` is unset.
See the :manual:`cmake-qt(7)` manual for more information on using CMake
with Qt.
......@@ -13,13 +13,17 @@ source files at build time and invoke moc accordingly.
* If an ``#include`` statement like ``#include "moc_<basename>.cpp"`` is found,
the ``Q_OBJECT`` or ``Q_GADGET`` macros are expected in an otherwise empty
line of the ``<basename>.h(xx)`` header file. ``moc`` is run on the header file to
generate ``moc_<basename>.cpp`` in the
``<CMAKE_CURRENT_BINARY_DIR>/<TARGETNAME>_autogen/include`` directory
which is automatically added to the target's
:prop_tgt:`INCLUDE_DIRECTORIES`. This allows the compiler to find the
included ``moc_<basename>.cpp`` file regardless of the location the
original source.
line of the ``<basename>.h(xx)`` header file. ``moc`` is run on the header
file to generate ``moc_<basename>.cpp`` in the
``<AUTOGEN_BUILD_DIR>/include`` directory which is automatically added
to the target's :prop_tgt:`INCLUDE_DIRECTORIES`.
This allows the compiler to find the included ``moc_<basename>.cpp`` file
regardless of the location the original source.
* For multi configuration generators, except Xcode, the include directory is
``<AUTOGEN_BUILD_DIR>/include_<CONFIG>``.
* See :prop_tgt:`AUTOGEN_BUILD_DIR`.
* If an ``#include`` statement like ``#include "<basename>.moc"`` is found,
then ``Q_OBJECT`` or ``Q_GADGET`` macros are expected in the current source
......@@ -28,10 +32,19 @@ source files at build time and invoke moc accordingly.
* Header files that are not included by an ``#include "moc_<basename>.cpp"``
statement are nonetheless scanned for ``Q_OBJECT`` or ``Q_GADGET`` macros.
The resulting ``moc_<basename>.cpp`` files are generated in custom
directories and automatically included in the generated
``<CMAKE_CURRENT_BINARY_DIR>/<TARGETNAME>_autogen/moc_compilation.cpp`` file,
which is compiled as part of the target. The custom directories help to
avoid name collisions for moc files with the same ``<basename>``.
directories and automatically included in a generated
``<AUTOGEN_BUILD_DIR>/mocs_compilation.cpp`` file,
which is compiled as part of the target.
* For multi configuration generators, except Xcode, the file names are
``moc_<basename>_<CONFIG>.cpp`` and
``<AUTOGEN_BUILD_DIR>/mocs_compilation_<CONFIG>.cpp``.
* The custom directories with checksum
based names help to avoid name collisions for moc files with the same
``<basename>``.
* See :prop_tgt:`AUTOGEN_BUILD_DIR`.
* Additionally, header files with the same base name as a source file,
(like ``<basename>.h``) or ``_p`` appended to the base name (like
......
......@@ -14,8 +14,13 @@ and invoke ``uic`` accordingly. If an ``#include`` statement like
searched for first in the vicinity of ``source.cpp`` and afterwards in the
optional :prop_tgt:`AUTOUIC_SEARCH_PATHS` of the target.
``uic`` is run on the ``foo.ui`` file to generate ``ui_foo.h`` in the directory
``<CMAKE_CURRENT_BINARY_DIR>/<TARGETNAME>_autogen/include``,
which is added to the target's :prop_tgt:`INCLUDE_DIRECTORIES` automatically.
``<AUTOGEN_BUILD_DIR>/include``,
which is automatically added to the target's :prop_tgt:`INCLUDE_DIRECTORIES`.
* For multi configuration generators, except Xcode, the include directory is
``<AUTOGEN_BUILD_DIR>/include_<CONFIG>``.
* See :prop_tgt:`AUTOGEN_BUILD_DIR`.
This property is initialized by the value of the :variable:`CMAKE_AUTOUIC`
variable if it is set when a target is created.
......
AutoGen build dir
-----------------
* The new target property :prop_tgt:`AUTOGEN_BUILD_DIR` was introduced which
allows to set a custom output directory for
:prop_tgt:`AUTOMOC`, :prop_tgt:`AUTOUIC` and :prop_tgt:`AUTORCC`.
# Target names
set(AM_TARGET_NAME @_autogen_target_name@)
set(AM_ORIGIN_TARGET_NAME @_origin_target_name@)
# Directories and files
set(AM_CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@/")
set(AM_CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@/")
......
This diff is collapsed.
......@@ -78,8 +78,8 @@ static void InfoGet(cmMakefile* makefile, const char* key,
cmSystemTools::ExpandListArgument(makefile->GetSafeDefinition(key), list);
}
static void InfoGet(cmMakefile* makefile, const char* key,
const std::string& config, std::vector<std::string>& list)
static void InfoGetConfig(cmMakefile* makefile, const char* key,
const std::string& config, std::string& value)
{
const char* valueConf = CM_NULLPTR;
{
......@@ -93,7 +93,16 @@ static void InfoGet(cmMakefile* makefile, const char* key,
if (valueConf == CM_NULLPTR) {
valueConf = makefile->GetSafeDefinition(key);
}
cmSystemTools::ExpandListArgument(valueConf, list);
value = valueConf;
}
static void InfoGetConfig(cmMakefile* makefile, const char* key,
const std::string& config,
std::vector<std::string>& list)
{
std::string value;
InfoGetConfig(makefile, key, config, value);
cmSystemTools::ExpandListArgument(value, list);
}
inline static bool SettingsMatch(cmMakefile* makefile, const char* key,
......@@ -270,6 +279,7 @@ cmQtAutoGenerators::cmQtAutoGenerators()
}
}
// Moc macro filters
this->MocMacroFilters[0].first = "Q_OBJECT";
this->MocMacroFilters[0].second.compile("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
this->MocMacroFilters[1].first = "Q_GADGET";
......@@ -358,12 +368,13 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(
{
this->SettingsFile = cmSystemTools::CollapseFullPath(targetDirectory);
cmSystemTools::ConvertToUnixSlashes(this->SettingsFile);
this->SettingsFile += "/AutogenOldSettings.cmake";
this->SettingsFile += "/AutogenOldSettings";
this->SettingsFile += this->ConfigSuffix;
this->SettingsFile += ".cmake";
}
// - Target names
InfoGet(makefile, "AM_TARGET_NAME", this->AutogenTargetName);
InfoGet(makefile, "AM_ORIGIN_TARGET_NAME", this->OriginTargetName);
// -- Meta
InfoGetConfig(makefile, "AM_CONFIG_SUFFIX", config, this->ConfigSuffix);
// - Files and directories
InfoGet(makefile, "AM_CMAKE_SOURCE_DIR", this->ProjectSourceDir);
......@@ -373,6 +384,10 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(
InfoGet(makefile, "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE",
this->IncludeProjectDirsBefore);
InfoGet(makefile, "AM_BUILD_DIR", this->AutogenBuildDir);
if (this->AutogenBuildDir.empty()) {
this->LogError("AutoGen: Error: Missing autogen build directory ");
return false;
}
InfoGet(makefile, "AM_SOURCES", this->Sources);
InfoGet(makefile, "AM_HEADERS", this->Headers);
......@@ -396,7 +411,8 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(
// - Moc
if (this->MocEnabled()) {
InfoGet(makefile, "AM_MOC_SKIP", this->MocSkipList);
InfoGet(makefile, "AM_MOC_DEFINITIONS", config, this->MocDefinitions);
InfoGetConfig(makefile, "AM_MOC_DEFINITIONS", config,
this->MocDefinitions);
#ifdef _WIN32
{
const std::string win32("WIN32");
......@@ -405,7 +421,7 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(
}
}
#endif
InfoGet(makefile, "AM_MOC_INCLUDES", config, this->MocIncludePaths);
InfoGetConfig(makefile, "AM_MOC_INCLUDES", config, this->MocIncludePaths);
InfoGet(makefile, "AM_MOC_OPTIONS", this->MocOptions);
InfoGet(makefile, "AM_MOC_RELAXED_MODE", this->MocRelaxedMode);
{
......@@ -440,7 +456,8 @@ bool cmQtAutoGenerators::ReadAutogenInfoFile(
if (this->UicEnabled()) {
InfoGet(makefile, "AM_UIC_SKIP", this->UicSkipList);
InfoGet(makefile, "AM_UIC_SEARCH_PATHS", this->UicSearchPaths);
InfoGet(makefile, "AM_UIC_TARGET_OPTIONS", config, this->UicTargetOptions);
InfoGetConfig(makefile, "AM_UIC_TARGET_OPTIONS", config,
this->UicTargetOptions);
{
std::vector<std::string> uicFilesVec;
std::vector<std::string> uicOptionsVec;
......@@ -616,9 +633,17 @@ bool cmQtAutoGenerators::SettingsFileWrite()
void cmQtAutoGenerators::Init(cmMakefile* makefile)
{
this->MocCppFilenameRel = "moc_compilation.cpp";
this->MocCppFilenameAbs = cmSystemTools::CollapseCombinedPath(
this->AutogenBuildDir, this->MocCppFilenameRel);
// Mocs compilation file
this->MocCompFileRel = "mocs_compilation";
this->MocCompFileRel += this->ConfigSuffix;
this->MocCompFileRel += ".cpp";
this->MocCompFileAbs = cmSystemTools::CollapseCombinedPath(
this->AutogenBuildDir, this->MocCompFileRel);
// Mocs include directory
this->AutogenIncludeDir = "include";
this->AutogenIncludeDir += this->ConfigSuffix;
this->AutogenIncludeDir += "/";
// Moc predefs file
if (!this->MocPredefsCmd.empty()) {
......@@ -697,10 +722,10 @@ bool cmQtAutoGenerators::RunAutogen()
// the program goes through all .cpp files to see which moc files are
// included. It is not really interesting how the moc file is named, but
// what file the moc is created from. Once a moc is included the same moc
// may not be included in the moc_compilation.cpp file anymore. OTOH if
// there's a header containing Q_OBJECT where no corresponding moc file
// is included anywhere a moc_<filename>.cpp file is created and included
// in the moc_compilation.cpp file.
// may not be included in the mocs_compilation_$<CONFIG>.cpp file anymore.
// OTOH if there's a header containing Q_OBJECT where no corresponding
// moc file is included anywhere a moc_<filename>.cpp file is created and
// included in the mocs_compilation_$<CONFIG>.cpp file.
// key = moc source filepath, value = moc output filepath
std::map<std::string, std::string> mocsIncluded;
......@@ -1105,7 +1130,7 @@ void cmQtAutoGenerators::MocParseHeaderContent(
if (this->MocRequired(contentText)) {
// Register moc job
mocsNotIncluded[absFilename] =
this->ChecksumedPath(absFilename, "moc_", ".cpp");
this->ChecksumedPath(absFilename, "moc_", this->ConfigSuffix + ".cpp");
this->MocFindDepends(absFilename, contentText, mocDepends);
}
}
......@@ -1255,37 +1280,31 @@ bool cmQtAutoGenerators::MocGenerateAll(
}
// Generate moc files that are included by source files.
{
const std::string subDir = "include/";
for (std::map<std::string, std::string>::const_iterator it =
mocsIncluded.begin();
it != mocsIncluded.end(); ++it) {
if (!this->MocGenerateFile(it->first, it->second, subDir, mocDepends)) {
if (this->MocRunFailed) {
return false;
}
for (std::map<std::string, std::string>::const_iterator it =
mocsIncluded.begin();
it != mocsIncluded.end(); ++it) {
if (!this->MocGenerateFile(it->first, it->second, mocDepends, true)) {
if (this->MocRunFailed) {
return false;
}
}
}
// Generate moc files that are _not_ included by source files.
bool mocCompFileGenerated = false;
{
const std::string subDir;
for (std::map<std::string, std::string>::const_iterator it =
mocsNotIncluded.begin();
it != mocsNotIncluded.end(); ++it) {
if (this->MocGenerateFile(it->first, it->second, subDir, mocDepends)) {
mocCompFileGenerated = true;
} else {
if (this->MocRunFailed) {
return false;
}
for (std::map<std::string, std::string>::const_iterator it =
mocsNotIncluded.begin();
it != mocsNotIncluded.end(); ++it) {
if (this->MocGenerateFile(it->first, it->second, mocDepends, false)) {
mocCompFileGenerated = true;
} else {
if (this->MocRunFailed) {
return false;
}
}
}
// Compose moc_compilation.cpp content
// Compose mocs compilation file content
std::string automocSource;
{
std::ostringstream ost;
......@@ -1304,18 +1323,18 @@ bool cmQtAutoGenerators::MocGenerateAll(
automocSource = ost.str();
}
if (this->FileDiffers(this->MocCppFilenameAbs, automocSource)) {
// Actually write moc_compilation.cpp
this->LogBold("Generating MOC compilation " + this->MocCppFilenameRel);
if (!this->FileWrite("AutoMoc", this->MocCppFilenameAbs, automocSource)) {
if (this->FileDiffers(this->MocCompFileAbs, automocSource)) {
// Actually write mocs compilation file
this->LogBold("Generating MOC compilation " + this->MocCompFileRel);
if (!this->FileWrite("AutoMoc", this->MocCompFileAbs, automocSource)) {
return false;
}
} else if (mocCompFileGenerated) {
// Only touch moc_compilation.cpp
// Only touch mocs compilation file
if (this->Verbose) {
this->LogInfo("Touching MOC compilation " + this->MocCppFilenameRel);
this->LogInfo("Touching MOC compilation " + this->MocCompFileRel);
}
cmSystemTools::Touch(this->MocCppFilenameAbs, false);
cmSystemTools::Touch(this->MocCompFileAbs, false);
}
return true;
......@@ -1326,14 +1345,14 @@ bool cmQtAutoGenerators::MocGenerateAll(
*/
bool cmQtAutoGenerators::MocGenerateFile(
const std::string& sourceFile, const std::string& mocFileName,
const std::string& subDir,
const std::map<std::string, std::set<std::string> >& mocDepends)
const std::map<std::string, std::set<std::string> >& mocDepends,
bool included)
{
bool mocGenerated = false;
bool generateMoc = this->MocSettingsChanged || this->MocPredefsChanged;
const std::string mocFileRel =
cmSystemTools::CollapseCombinedPath(subDir, mocFileName);
included ? (this->AutogenIncludeDir + mocFileName) : mocFileName;
const std::string mocFileAbs =
cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, mocFileRel);
......@@ -1523,8 +1542,7 @@ bool cmQtAutoGenerators::UicGenerateFile(const std::string& realName,
bool uicGenerated = false;
bool generateUic = this->UicSettingsChanged;
const std::string uicFileRel =
cmSystemTools::CollapseCombinedPath("include", uiOutputFile);
const std::string uicFileRel = this->AutogenIncludeDir + uiOutputFile;
const std::string uicFileAbs =
cmSystemTools::CollapseCombinedPath(this->AutogenBuildDir, uicFileRel);
......@@ -1590,11 +1608,17 @@ bool cmQtAutoGenerators::RccGenerateAll()
// generate single map with input / output names
std::map<std::string, std::string> qrcGenMap;
for (std::vector<std::string>::const_iterator si = this->RccSources.begin();
si != this->RccSources.end(); ++si) {
const std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
if (ext == ".qrc") {
qrcGenMap[*si] = this->ChecksumedPath(*si, "qrc_", ".cpp");
{
const std::string qrcPrefix = "qrc_";
const std::string qrcSuffix = this->ConfigSuffix + ".cpp";
for (std::vector<std::string>::const_iterator si =
this->RccSources.begin();
si != this->RccSources.end(); ++si) {
const std::string ext =
cmsys::SystemTools::GetFilenameLastExtension(*si);
if (ext == ".qrc") {
qrcGenMap[*si] = this->ChecksumedPath(*si, qrcPrefix, qrcSuffix);
}
}
}
......@@ -1827,9 +1851,9 @@ bool cmQtAutoGenerators::NameCollisionTest(
* @brief Generates a file path based on the checksum of the source file path
* @return The path
*/
std::string cmQtAutoGenerators::ChecksumedPath(const std::string& sourceFile,
const char* basePrefix,
const char* baseSuffix) const
std::string cmQtAutoGenerators::ChecksumedPath(
const std::string& sourceFile, const std::string& basePrefix,
const std::string& baseSuffix) const
{
std::string res = FPathChecksum.getPart(sourceFile);
res += "/";
......
......@@ -107,8 +107,8 @@ private:
const std::map<std::string, std::set<std::string> >& mocDepends);
bool MocGenerateFile(
const std::string& sourceFile, const std::string& mocFileName,
const std::string& subDir,
const std::map<std::string, std::set<std::string> >& mocDepends);
const std::map<std::string, std::set<std::string> >& mocDepends,
bool included);
// -- Uic file generation
bool UicFindIncludedFile(std::string& absFile, const std::string& sourceFile,
......@@ -139,8 +139,8 @@ private:
const std::map<std::string, std::string>& genFiles,
std::multimap<std::string, std::string>& collisions) const;
std::string ChecksumedPath(const std::string& sourceFile,
const char* basePrefix,
const char* baseSuffix) const;
const std::string& basePrefix,
const std::string& baseSuffix) const;
bool MakeParentDirectory(const char* logPrefix,
const std::string& filename) const;
bool FileDiffers(const std::string& filename, const std::string& content);
......@@ -157,15 +157,15 @@ private:
bool MocFindIncludedFile(std::string& absFile, const std::string& sourceFile,
const std::string& includeString) const;
// -- Target names
std::string OriginTargetName;
std::string AutogenTargetName;
// -- Meta
std::string ConfigSuffix;
// -- Directories
std::string ProjectSourceDir;
std::string ProjectBinaryDir;
std::string CurrentSourceDir;
std::string CurrentBinaryDir;
std::string AutogenBuildDir;
std::string AutogenIncludeDir;
// -- Qt environment
std::string QtMajorVersion;
std::string MocExecutable;
......@@ -189,8 +189,8 @@ private:
bool MocPredefsChanged;
bool MocRelaxedMode;
bool MocRunFailed;
std::string MocCppFilenameRel;
std::string MocCppFilenameAbs;
std::string MocCompFileRel;
std::string MocCompFileAbs;
std::string MocPredefsFileRel;
std::string MocPredefsFileAbs;
std::vector<std::string> MocSkipList;
......
......@@ -64,6 +64,6 @@ target_link_libraries(MyWidget KI18n ${QT_GUI_TARGET})
add_executable(QtAutoUicInterface main.cpp)
target_compile_definitions(QtAutoUicInterface
PRIVATE
UI_LIBWIDGET_H="${CMAKE_CURRENT_BINARY_DIR}/LibWidget_autogen/include/ui_libwidget.h"
UI_MYWIDGET_H="${CMAKE_CURRENT_BINARY_DIR}/MyWidget_autogen/include/ui_mywidget.h"
UI_LIBWIDGET_H="${CMAKE_CURRENT_BINARY_DIR}/LibWidget_autogen/include_${CMAKE_BUILD_TYPE}/ui_libwidget.h"
UI_MYWIDGET_H="${CMAKE_CURRENT_BINARY_DIR}/MyWidget_autogen/include_${CMAKE_BUILD_TYPE}/ui_mywidget.h"
)
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