Commit 2a85b5ac authored by Sebastian Holtermann's avatar Sebastian Holtermann
Browse files

Autogen: Make cmQtAutoGeneratorInitializer an instantiable class

Remove the cmQtAutoGenDigest classes and make
cmQtAutoGeneratorInitializer instantiable instead.
parent 75819b86
......@@ -311,7 +311,6 @@ set(SRCS
cmPropertyMap.h
cmQtAutoGen.cxx
cmQtAutoGen.h
cmQtAutoGenDigest.h
cmQtAutoGenerator.cxx
cmQtAutoGenerator.h
cmQtAutoGeneratorInitializer.cxx
......
......@@ -1314,7 +1314,10 @@ bool cmGlobalGenerator::Compute()
#ifdef CMAKE_BUILD_WITH_CMAKE
// Iterate through all targets and set up automoc for those which have
// the AUTOMOC, AUTOUIC or AUTORCC property set
cmQtAutoGenDigestUPV autogenDigests = this->CreateQtAutoGeneratorsTargets();
auto autogenInits = this->CreateQtAutoGenInitializers();
for (auto& autoGen : autogenInits) {
autoGen->InitCustomTargets();
}
#endif
// Add generator specific helper commands
......@@ -1335,10 +1338,11 @@ bool cmGlobalGenerator::Compute()
}
#ifdef CMAKE_BUILD_WITH_CMAKE
for (cmQtAutoGenDigestUP const& digest : autogenDigests) {
cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(*digest);
for (auto& autoGen : autogenInits) {
autoGen->SetupCustomTargets();
autoGen.reset(nullptr);
}
autogenDigests.clear();
autogenInits.clear();
#endif
for (cmLocalGenerator* localGen : this->LocalGenerators) {
......@@ -1469,9 +1473,10 @@ bool cmGlobalGenerator::ComputeTargetDepends()
return true;
}
cmQtAutoGenDigestUPV cmGlobalGenerator::CreateQtAutoGeneratorsTargets()
std::vector<std::unique_ptr<cmQtAutoGeneratorInitializer>>
cmGlobalGenerator::CreateQtAutoGenInitializers()
{
cmQtAutoGenDigestUPV autogenDigests;
std::vector<std::unique_ptr<cmQtAutoGeneratorInitializer>> autogenInits;
#ifdef CMAKE_BUILD_WITH_CMAKE
for (cmLocalGenerator* localGen : this->LocalGenerators) {
......@@ -1507,25 +1512,12 @@ cmQtAutoGenDigestUPV cmGlobalGenerator::CreateQtAutoGeneratorsTargets()
continue;
}
{
cmQtAutoGenDigestUP digest(new cmQtAutoGenDigest(target));
digest->QtVersionMajor = std::move(qtVersionMajor);
digest->QtVersionMinor =
cmQtAutoGeneratorInitializer::GetQtMinorVersion(
target, digest->QtVersionMajor);
digest->MocEnabled = mocEnabled;
digest->UicEnabled = uicEnabled;
digest->RccEnabled = rccEnabled;
autogenDigests.emplace_back(std::move(digest));
}
autogenInits.emplace_back(new cmQtAutoGeneratorInitializer(
target, mocEnabled, uicEnabled, rccEnabled, qtVersionMajor));
}
}
// Initialize autogen targets
for (const cmQtAutoGenDigestUP& digest : autogenDigests) {
cmQtAutoGeneratorInitializer::InitializeAutogenTarget(*digest);
}
#endif
return autogenDigests;
return autogenInits;
}
cmLinkLineComputer* cmGlobalGenerator::CreateLinkLineComputer(
......
......@@ -15,7 +15,6 @@
#include "cmCustomCommandLines.h"
#include "cmExportSetMap.h"
#include "cmQtAutoGenDigest.h"
#include "cmStateSnapshot.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
......@@ -33,6 +32,7 @@ class cmLinkLineComputer;
class cmLocalGenerator;
class cmMakefile;
class cmOutputConverter;
class cmQtAutoGeneratorInitializer;
class cmSourceFile;
class cmStateDirectory;
class cmake;
......@@ -433,7 +433,8 @@ protected:
virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const;
// Qt auto generators
cmQtAutoGenDigestUPV CreateQtAutoGeneratorsTargets();
std::vector<std::unique_ptr<cmQtAutoGeneratorInitializer>>
CreateQtAutoGenInitializers();
std::string SelectMakeProgram(const std::string& makeProgram,
const std::string& makeDefault = "") const;
......
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#ifndef cmQtAutoGenDigest_h
#define cmQtAutoGenDigest_h
#include "cmConfigure.h" // IWYU pragma: keep
#include <memory>
#include <string>
#include <vector>
class cmGeneratorTarget;
class cmQtAutoGenDigestQrc
{
public:
cmQtAutoGenDigestQrc()
: Generated(false)
, Unique(false)
{
}
public:
std::string QrcFile;
std::string QrcName;
std::string PathChecksum;
std::string InfoFile;
std::string SettingsFile;
std::string RccFile;
bool Generated;
bool Unique;
std::vector<std::string> Options;
std::vector<std::string> Resources;
};
/** \class cmQtAutoGenDigest
* \brief Filtered set of QtAutogen variables for a specific target
*/
class cmQtAutoGenDigest
{
public:
cmQtAutoGenDigest(cmGeneratorTarget* target)
: Target(target)
, MocEnabled(false)
, UicEnabled(false)
, RccEnabled(false)
{
}
public:
cmGeneratorTarget* Target;
std::string QtVersionMajor;
std::string QtVersionMinor;
bool MocEnabled;
bool UicEnabled;
bool RccEnabled;
std::vector<std::string> Headers;
std::vector<std::string> Sources;
std::vector<cmQtAutoGenDigestQrc> Qrcs;
};
// Utility types
typedef std::unique_ptr<cmQtAutoGenDigest> cmQtAutoGenDigestUP;
typedef std::vector<cmQtAutoGenDigestUP> cmQtAutoGenDigestUPV;
#endif
......@@ -51,118 +51,6 @@ inline static std::string GetSafeProperty(cmSourceFile const* sf,
return std::string(SafeString(sf->GetProperty(key)));
}
static cmQtAutoGen::MultiConfig AutogenMultiConfig(
cmGlobalGenerator* globalGen)
{
if (!globalGen->IsMultiConfig()) {
return cmQtAutoGen::SINGLE;
}
// FIXME: Xcode does not support per-config sources, yet.
// (EXCLUDED_SOURCE_FILE_NAMES)
// if (globalGen->GetName().find("Xcode") != std::string::npos) {
// return cmQtAutoGen::FULL;
//}
// FIXME: Visual Studio does not support per-config sources, yet.
// (EXCLUDED_SOURCE_FILE_NAMES)
// if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
// return cmQtAutoGen::FULL;
//}
return cmQtAutoGen::WRAP;
}
static std::string GetAutogenTargetName(cmGeneratorTarget const* target)
{
std::string autogenTargetName = target->GetName();
autogenTargetName += "_autogen";
return autogenTargetName;
}
static std::string GetAutogenTargetFilesDir(cmGeneratorTarget const* target)
{
cmMakefile* makefile = target->Target->GetMakefile();
std::string targetDir = makefile->GetCurrentBinaryDirectory();
targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
targetDir += "/";
targetDir += GetAutogenTargetName(target);
targetDir += ".dir";
return targetDir;
}
static std::string GetAutogenTargetBuildDir(cmGeneratorTarget const* target)
{
std::string targetDir = GetSafeProperty(target, "AUTOGEN_BUILD_DIR");
if (targetDir.empty()) {
cmMakefile* makefile = target->Target->GetMakefile();
targetDir = makefile->GetCurrentBinaryDirectory();
targetDir += "/";
targetDir += GetAutogenTargetName(target);
}
return targetDir;
}
std::string cmQtAutoGeneratorInitializer::GetQtMajorVersion(
cmGeneratorTarget const* target)
{
cmMakefile* makefile = target->Target->GetMakefile();
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;
}
return qtMajor;
}
std::string cmQtAutoGeneratorInitializer::GetQtMinorVersion(
cmGeneratorTarget const* target, std::string const& qtVersionMajor)
{
cmMakefile* makefile = target->Target->GetMakefile();
std::string qtMinor;
if (qtVersionMajor == "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 qtMinor;
}
static bool QtVersionGreaterOrEqual(std::string const& major,
std::string const& minor,
unsigned long requestMajor,
unsigned long requestMinor)
{
unsigned long majorUL(0);
unsigned long minorUL(0);
if (cmSystemTools::StringToULong(major.c_str(), &majorUL) &&
cmSystemTools::StringToULong(minor.c_str(), &minorUL)) {
return (majorUL > requestMajor) ||
(majorUL == requestMajor && minorUL >= requestMinor);
}
return false;
}
static void GetConfigs(cmMakefile* makefile, std::string& configDefault,
std::vector<std::string>& configsList)
{
configDefault = makefile->GetConfigurations(configsList);
if (configsList.empty()) {
configsList.push_back(configDefault);
}
}
static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
std::string const& value)
{
......@@ -258,50 +146,6 @@ static void AddCleanFile(cmMakefile* makefile, std::string const& fileName)
false);
}
static std::vector<std::string> AddGeneratedSource(
cmGeneratorTarget* target, std::string const& filename,
cmQtAutoGen::MultiConfig multiConfig,
const std::vector<std::string>& configsList, cmQtAutoGen::Generator genType)
{
std::vector<std::string> genFiles;
// Register source file in makefile and source group
if (multiConfig != cmQtAutoGen::FULL) {
genFiles.push_back(filename);
} else {
for (std::string const& cfg : configsList) {
genFiles.push_back(
cmQtAutoGen::AppendFilenameSuffix(filename, "_" + cfg));
}
}
{
cmMakefile* makefile = target->Target->GetMakefile();
for (std::string const& genFile : genFiles) {
{
cmSourceFile* gFile = makefile->GetOrCreateSource(genFile, true);
gFile->SetProperty("GENERATED", "1");
gFile->SetProperty("SKIP_AUTOGEN", "On");
}
AddToSourceGroup(makefile, genFile, genType);
}
}
// Add source file to target
if (multiConfig != cmQtAutoGen::FULL) {
target->AddSource(filename);
} else {
for (std::string const& cfg : configsList) {
std::string src = "$<$<CONFIG:";
src += cfg;
src += ">:";
src += cmQtAutoGen::AppendFilenameSuffix(filename, "_" + cfg);
src += ">";
target->AddSource(src);
}
}
return genFiles;
}
/* @brief Tests if targetDepend is a STATIC_LIBRARY and if any of its
* recursive STATIC_LIBRARY dependencies depends on targetOrigin
* (STATIC_LIBRARY cycle).
......@@ -346,371 +190,182 @@ static bool StaticLibraryCycle(cmGeneratorTarget const* targetOrigin,
return cycle;
}
struct cmQtAutoGenSetup
static std::string RccGetExecutable(cmGeneratorTarget const* target,
std::string const& qtMajorVersion)
{
std::set<std::string> MocSkip;
std::set<std::string> UicSkip;
std::map<std::string, std::string> ConfigMocIncludes;
std::map<std::string, std::string> ConfigMocDefines;
std::map<std::string, std::string> ConfigUicOptions;
};
std::string rccExec;
std::string err;
static void SetupAcquireSkipFiles(cmQtAutoGenDigest const& digest,
cmQtAutoGenSetup& setup)
{
// Read skip files from makefile sources
{
const std::vector<cmSourceFile*>& allSources =
digest.Target->Makefile->GetSourceFiles();
for (cmSourceFile* sf : allSources) {
// sf->GetExtension() is only valid after sf->GetFullPath() ...
std::string const& fPath = sf->GetFullPath();
cmSystemTools::FileFormat const fileType =
cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
!(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
continue;
}
const bool skipAll = sf->GetPropertyAsBool("SKIP_AUTOGEN");
const bool mocSkip = digest.MocEnabled &&
(skipAll || sf->GetPropertyAsBool("SKIP_AUTOMOC"));
const bool uicSkip = digest.UicEnabled &&
(skipAll || sf->GetPropertyAsBool("SKIP_AUTOUIC"));
if (mocSkip || uicSkip) {
std::string const absFile = cmSystemTools::GetRealPath(fPath);
if (mocSkip) {
setup.MocSkip.insert(absFile);
}
if (uicSkip) {
setup.UicSkip.insert(absFile);
}
}
cmLocalGenerator* localGen = target->GetLocalGenerator();
if (qtMajorVersion == "5") {
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::rcc");
if (tgt != nullptr) {
rccExec = SafeString(tgt->ImportedGetLocation(""));
} else {
err = "AUTORCC: Qt5::rcc target not found";
}
} else if (qtMajorVersion == "4") {
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::rcc");
if (tgt != nullptr) {
rccExec = SafeString(tgt->ImportedGetLocation(""));
} else {
err = "AUTORCC: Qt4::rcc target not found";
}
} else {
err = "The AUTORCC feature supports only Qt 4 and Qt 5";
}
if (!err.empty()) {
err += " (";
err += target->GetName();
err += ")";
cmSystemTools::Error(err.c_str());
}
return rccExec;
}
static void SetupAutoTargetMoc(cmQtAutoGenDigest const& digest,
std::string const& configDefault,
std::vector<std::string> const& configsList,
cmQtAutoGenSetup& setup)
cmQtAutoGeneratorInitializer::cmQtAutoGeneratorInitializer(
cmGeneratorTarget* target, bool mocEnabled, bool uicEnabled, bool rccEnabled,
std::string const& qtVersionMajor)
: Target(target)
, MocEnabled(mocEnabled)
, UicEnabled(uicEnabled)
, RccEnabled(rccEnabled)
, QtVersionMajor(qtVersionMajor)
, MultiConfig(cmQtAutoGen::WRAP)
{
cmGeneratorTarget const* target = digest.Target;
cmLocalGenerator* localGen = target->GetLocalGenerator();
cmMakefile* makefile = target->Target->GetMakefile();
this->QtVersionMinor = cmQtAutoGeneratorInitializer::GetQtMinorVersion(
target, this->QtVersionMajor);
}
AddDefinitionEscaped(makefile, "_moc_skip", setup.MocSkip);
AddDefinitionEscaped(makefile, "_moc_options",
GetSafeProperty(target, "AUTOMOC_MOC_OPTIONS"));
AddDefinitionEscaped(makefile, "_moc_relaxed_mode",
makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE") ? "TRUE"
: "FALSE");
AddDefinitionEscaped(makefile, "_moc_macro_names",
GetSafeProperty(target, "AUTOMOC_MACRO_NAMES"));
AddDefinitionEscaped(makefile, "_moc_depend_filters",
GetSafeProperty(target, "AUTOMOC_DEPEND_FILTERS"));
void cmQtAutoGeneratorInitializer::InitCustomTargets()
{
cmMakefile* makefile = this->Target->Target->GetMakefile();
cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
// Compiler predefines
if (target->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES")) {
if (QtVersionGreaterOrEqual(digest.QtVersionMajor, digest.QtVersionMinor,
5, 8)) {
AddDefinitionEscaped(
makefile, "_moc_predefs_cmd",
makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
}
// Configurations
this->ConfigDefault = makefile->GetConfigurations(this->ConfigsList);
if (this->ConfigsList.empty()) {
this->ConfigsList.push_back(this->ConfigDefault);
}
// Moc includes and compile definitions
// Multi configuration
{
auto GetIncludeDirs = [target,
localGen](std::string const& cfg) -> std::string {
// Get the include dirs for this target, without stripping the implicit
// include dirs off, see
// https://gitlab.kitware.com/cmake/cmake/issues/13667
std::vector<std::string> includeDirs;
localGen->GetIncludeDirectories(includeDirs, target, "CXX", cfg, false);
return cmJoin(includeDirs, ";");
};
auto GetCompileDefinitions =
[target, localGen](std::string const& cfg) -> std::string {
std::set<std::string> defines;
localGen->AddCompileDefinitions(defines, target, cfg, "CXX");
return cmJoin(defines, ";");
};
if (!globalGen->IsMultiConfig()) {
this->MultiConfig = cmQtAutoGen::SINGLE;
}
// Default configuration settings
std::string const includeDirs = GetIncludeDirs(configDefault);
std::string const compileDefs = GetCompileDefinitions(configDefault);
// Other configuration settings
for (std::string const& cfg : configsList) {
{
std::string const configIncludeDirs = GetIncludeDirs(cfg);
if (configIncludeDirs != includeDirs) {
setup.ConfigMocIncludes[cfg] = configIncludeDirs;
}
}
{
std::string const configCompileDefs = GetCompileDefinitions(cfg);
if (configCompileDefs != compileDefs) {
setup.ConfigMocDefines[cfg] = configCompileDefs;
}
}
// FIXME: Xcode does not support per-config sources, yet.
// (EXCLUDED_SOURCE_FILE_NAMES)
// if (globalGen->GetName().find("Xcode") != std::string::npos) {
// return cmQtAutoGen::FULL;
//}
// FIXME: Visual Studio does not support per-config sources, yet.
// (EXCLUDED_SOURCE_FILE_NAMES)
// if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
// return cmQtAutoGen::FULL;
//}
}
// Autogen target name
this->AutogenTargetName = this->Target->GetName();
this->AutogenTargetName += "_autogen";
// Autogen directories
{
// Collapsed current binary directory
std::string const cbd = cmSystemTools::CollapseFullPath(
"", makefile->GetCurrentBinaryDirectory());
// Autogen info dir
this->DirInfo = cbd;
this->DirInfo += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
this->DirInfo += "/";
this->DirInfo += this->AutogenTargetName;
this->DirInfo += ".dir";
cmSystemTools::ConvertToUnixSlashes(this->DirInfo);
// Autogen build dir
this->DirBuild = GetSafeProperty(this->Target, "AUTOGEN_BUILD_DIR");
if (this->DirBuild.empty()) {
this->DirBuild = cbd;
this->DirBuild += "/";
this->DirBuild += this->AutogenTargetName;
}
AddDefinitionEscaped(makefile, "_moc_include_dirs", includeDirs);
AddDefinitionEscaped(makefile, "_moc_compile_defs", compileDefs);
cmSystemTools::ConvertToUnixSlashes(this->DirBuild);
// Working directory
this->DirWork = cbd;
cmSystemTools::ConvertToUnixSlashes(this->DirWork);
}
// Moc executable
// Autogen files
{
std::string mocExec;
std::string err;
this->AutogenInfoFile = this->DirInfo;
this->AutogenInfoFile += "/AutogenInfo.cmake";
if (digest.QtVersionMajor == "5") {
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::moc");
if (tgt != nullptr) {
mocExec = SafeString(tgt->ImportedGetLocation(""));
} else {
err = "AUTOMOC: Qt5::moc target not found";
}
} else if (digest.QtVersionMajor == "4") {
cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::moc");
if (tgt != nullptr) {
mocExec = SafeString(tgt->ImportedGetLocation(""));
} else {
err = "AUTOMOC: Qt4::moc target not found";
}
} else {
err = "The AUTOMOC feature supports only Qt 4 and Qt 5";
}
this->AutogenSettingsFile = this->DirInfo;
this->AutogenSettingsFile += "/AutogenOldSettings.cmake";
}
if (err.empty()) {
AddDefinitionEscaped(makefile, "_qt_moc_executable", mocExec);
} else {
err += " (" + target->GetName() + ")";
cmSystemTools::Error(err.c_str());
// Autogen target FOLDER property
{
const char* folder =
makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
if (folder == nullptr) {
folder =
makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
}
// Inherit FOLDER property from target (#13688)
if (folder == nullptr) {
folder = SafeString(this->Target->Target->GetProperty("FOLDER"));
}
if (folder != nullptr) {