123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include "cmQtAutoGeneratorInitializer.h"
- #include "cmQtAutoGeneratorCommon.h"
- #include "cmAlgorithms.h"
- #include "cmCustomCommandLines.h"
- #include "cmFilePathChecksum.h"
- #include "cmGeneratorTarget.h"
- #include "cmGlobalGenerator.h"
- #include "cmLocalGenerator.h"
- #include "cmMakefile.h"
- #include "cmOutputConverter.h"
- #include "cmPolicies.h"
- #include "cmSourceFile.h"
- #include "cmSourceGroup.h"
- #include "cmState.h"
- #include "cmSystemTools.h"
- #include "cmTarget.h"
- #include "cm_sys_stat.h"
- #include "cmake.h"
- #if defined(_WIN32) && !defined(__CYGWIN__)
- #include "cmGlobalVisualStudioGenerator.h"
- #endif
- #include "cmConfigure.h"
- #include "cmsys/FStream.hxx"
- #include <algorithm>
- #include <map>
- #include <set>
- #include <sstream>
- #include <string>
- #include <utility>
- #include <vector>
- static void utilCopyTargetProperty(cmTarget* destinationTarget,
- cmTarget* sourceTarget,
- const std::string& propertyName)
- {
- const char* propertyValue = sourceTarget->GetProperty(propertyName);
- if (propertyValue) {
- destinationTarget->SetProperty(propertyName, propertyValue);
- }
- }
- inline static bool PropertyEnabled(cmSourceFile* sourceFile, const char* key)
- {
- return cmSystemTools::IsOn(sourceFile->GetPropertyForUser(key));
- }
- static std::string GetSafeProperty(cmGeneratorTarget const* target,
- const char* key)
- {
- const char* tmp = target->GetProperty(key);
- return std::string((tmp != CM_NULLPTR) ? tmp : "");
- }
- 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;
- }
- static std::string GetQtMajorVersion(cmGeneratorTarget const* target)
- {
- cmMakefile* makefile = target->Target->GetMakefile();
- std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
- if (qtMajorVersion.empty()) {
- qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
- }
- const char* targetQtVersion =
- target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "");
- if (targetQtVersion != CM_NULLPTR) {
- qtMajorVersion = targetQtVersion;
- }
- return qtMajorVersion;
- }
- static std::string GetQtMinorVersion(cmGeneratorTarget const* target,
- const std::string& qtMajorVersion)
- {
- cmMakefile* makefile = target->Target->GetMakefile();
- std::string qtMinorVersion;
- if (qtMajorVersion == "5") {
- qtMinorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR");
- }
- if (qtMinorVersion.empty()) {
- qtMinorVersion = makefile->GetSafeDefinition("QT_VERSION_MINOR");
- }
- const char* targetQtVersion =
- target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION", "");
- if (targetQtVersion != CM_NULLPTR) {
- qtMinorVersion = targetQtVersion;
- }
- return qtMinorVersion;
- }
- static bool QtVersionGreaterOrEqual(const std::string& major,
- const std::string& 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 GetCompileDefinitionsAndDirectories(
- cmGeneratorTarget const* target, const std::string& config,
- std::string& incs, std::string& defs)
- {
- cmLocalGenerator* localGen = target->GetLocalGenerator();
- {
- std::vector<std::string> includeDirs;
- // Get the include dirs for this target, without stripping the implicit
- // include dirs off, see
- // https://gitlab.kitware.com/cmake/cmake/issues/13667
- localGen->GetIncludeDirectories(includeDirs, target, "CXX", config, false);
- incs = cmJoin(includeDirs, ";");
- }
- {
- std::set<std::string> defines;
- localGen->AddCompileDefinitions(defines, target, config, "CXX");
- defs += cmJoin(defines, ";");
- }
- }
- static bool IsMultiConfig(cmGlobalGenerator* globalGen)
- {
- return globalGen->IsMultiConfig();
- }
- static std::vector<std::string> GetConfigurations(
- cmMakefile* makefile, std::string* config = CM_NULLPTR)
- {
- std::vector<std::string> configs;
- {
- std::string cfg = makefile->GetConfigurations(configs);
- if (config != CM_NULLPTR) {
- *config = cfg;
- }
- }
- // Add empty configuration on demand
- if (configs.empty()) {
- configs.push_back("");
- }
- return configs;
- }
- static std::vector<std::string> GetConfigurationSuffixes(cmMakefile* makefile)
- {
- std::vector<std::string> suffixes;
- if (IsMultiConfig(makefile->GetGlobalGenerator())) {
- makefile->GetConfigurations(suffixes);
- for (std::vector<std::string>::iterator it = suffixes.begin();
- it != suffixes.end(); ++it) {
- it->insert(0, "_");
- }
- }
- if (suffixes.empty()) {
- suffixes.push_back("");
- }
- return suffixes;
- }
- static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
- const std::string& value)
- {
- makefile->AddDefinition(key,
- cmOutputConverter::EscapeForCMake(value).c_str());
- }
- static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
- const std::vector<std::string>& values)
- {
- makefile->AddDefinition(
- key, cmOutputConverter::EscapeForCMake(cmJoin(values, ";")).c_str());
- }
- static bool AddToSourceGroup(cmMakefile* makefile, const std::string& fileName,
- cmQtAutoGeneratorCommon::GeneratorType genType)
- {
- cmSourceGroup* sourceGroup = CM_NULLPTR;
- // Acquire source group
- {
- const char* groupName = CM_NULLPTR;
- // Use generator specific group name
- switch (genType) {
- case cmQtAutoGeneratorCommon::MOC:
- groupName =
- makefile->GetState()->GetGlobalProperty("AUTOMOC_SOURCE_GROUP");
- break;
- case cmQtAutoGeneratorCommon::RCC:
- groupName =
- makefile->GetState()->GetGlobalProperty("AUTORCC_SOURCE_GROUP");
- break;
- default:
- break;
- }
- // Use default group name on demand
- if ((groupName == CM_NULLPTR) || (*groupName == 0)) {
- groupName =
- makefile->GetState()->GetGlobalProperty("AUTOGEN_SOURCE_GROUP");
- }
- // Generate a source group on demand
- if ((groupName != CM_NULLPTR) && (*groupName != 0)) {
- {
- const char* delimiter =
- makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
- if (delimiter == CM_NULLPTR) {
- delimiter = "\\";
- }
- std::vector<std::string> folders =
- cmSystemTools::tokenize(groupName, delimiter);
- sourceGroup = makefile->GetSourceGroup(folders);
- if (sourceGroup == CM_NULLPTR) {
- makefile->AddSourceGroup(folders);
- sourceGroup = makefile->GetSourceGroup(folders);
- }
- }
- if (sourceGroup == CM_NULLPTR) {
- cmSystemTools::Error(
- "Autogen: Could not create or find source group: ",
- cmQtAutoGeneratorCommon::Quoted(groupName).c_str());
- return false;
- }
- }
- }
- if (sourceGroup != CM_NULLPTR) {
- sourceGroup->AddGroupFile(fileName);
- }
- return true;
- }
- static void AddGeneratedSource(cmMakefile* makefile,
- const std::string& filename,
- cmQtAutoGeneratorCommon::GeneratorType genType)
- {
- cmSourceFile* gFile = makefile->GetOrCreateSource(filename, true);
- gFile->SetProperty("GENERATED", "1");
- gFile->SetProperty("SKIP_AUTOGEN", "On");
- AddToSourceGroup(makefile, filename, genType);
- }
- static void AcquireScanFiles(cmGeneratorTarget const* target,
- std::vector<std::string>& mocUicSources,
- std::vector<std::string>& mocUicHeaders,
- std::vector<std::string>& mocSkipList,
- std::vector<std::string>& uicSkipList)
- {
- const bool mocTarget = target->GetPropertyAsBool("AUTOMOC");
- const bool uicTarget = target->GetPropertyAsBool("AUTOUIC");
- const cmPolicies::PolicyStatus CMP0071_status =
- target->Makefile->GetPolicyStatus(cmPolicies::CMP0071);
- std::vector<cmSourceFile*> srcFiles;
- target->GetConfigCommonSourceFiles(srcFiles);
- for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
- fileIt != srcFiles.end(); ++fileIt) {
- cmSourceFile* sf = *fileIt;
- const cmSystemTools::FileFormat fileType =
- cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
- if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
- !(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
- continue;
- }
- const std::string absFile =
- cmsys::SystemTools::GetRealPath(sf->GetFullPath());
- // Skip flags
- const bool skipAll = PropertyEnabled(sf, "SKIP_AUTOGEN");
- const bool mocSkip = skipAll || PropertyEnabled(sf, "SKIP_AUTOMOC");
- const bool uicSkip = skipAll || PropertyEnabled(sf, "SKIP_AUTOUIC");
- const bool accept = (mocTarget && !mocSkip) || (uicTarget && !uicSkip);
- // For GENERATED files check status of policy CMP0071
- if (accept && PropertyEnabled(sf, "GENERATED")) {
- bool policyAccept = false;
- switch (CMP0071_status) {
- case cmPolicies::WARN: {
- std::ostringstream ost;
- ost << cmPolicies::GetPolicyWarning(cmPolicies::CMP0071) << "\n";
- ost << "AUTOMOC/AUTOUIC: Ignoring GENERATED source file:\n";
- ost << " " << cmQtAutoGeneratorCommon::Quoted(absFile) << "\n";
- target->Makefile->IssueMessage(cmake::AUTHOR_WARNING, ost.str());
- }
- CM_FALLTHROUGH;
- case cmPolicies::OLD:
- // Ignore GENERATED file
- break;
- case cmPolicies::REQUIRED_IF_USED:
- case cmPolicies::REQUIRED_ALWAYS:
- case cmPolicies::NEW:
- // Process GENERATED file
- policyAccept = true;
- break;
- }
- if (!policyAccept) {
- continue;
- }
- }
- // Add file name to skip lists.
- // Do this even when the file is not added to the sources/headers lists
- // because the file name may be extracted from an other file when
- // processing
- if (mocSkip) {
- mocSkipList.push_back(absFile);
- }
- if (uicSkip) {
- uicSkipList.push_back(absFile);
- }
- if (accept) {
- // Add file name to sources or headers list
- switch (fileType) {
- case cmSystemTools::CXX_FILE_FORMAT:
- mocUicSources.push_back(absFile);
- break;
- case cmSystemTools::HEADER_FILE_FORMAT:
- mocUicHeaders.push_back(absFile);
- break;
- default:
- break;
- }
- }
- }
- }
- static void MocSetupAutoTarget(
- cmGeneratorTarget const* target, const std::string& autogenTargetName,
- std::string const& qtMajorVersion, std::string const& config,
- std::vector<std::string> const& configs,
- std::vector<std::string> const& mocSkipList,
- std::map<std::string, std::string>& configMocIncludes,
- std::map<std::string, std::string>& configMocDefines)
- {
- cmLocalGenerator* lg = target->GetLocalGenerator();
- cmMakefile* makefile = target->Target->GetMakefile();
- AddDefinitionEscaped(makefile, "_moc_options",
- GetSafeProperty(target, "AUTOMOC_MOC_OPTIONS"));
- AddDefinitionEscaped(makefile, "_moc_skip", mocSkipList);
- AddDefinitionEscaped(makefile, "_moc_relaxed_mode",
- makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE") ? "TRUE"
- : "FALSE");
- AddDefinitionEscaped(makefile, "_moc_depend_filters",
- GetSafeProperty(target, "AUTOMOC_DEPEND_FILTERS"));
- if (QtVersionGreaterOrEqual(
- qtMajorVersion, GetQtMinorVersion(target, qtMajorVersion), 5, 8)) {
- AddDefinitionEscaped(
- makefile, "_moc_predefs_cmd",
- makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
- }
- // Moc includes and compile definitions
- {
- // Default settings
- std::string incs;
- std::string compileDefs;
- GetCompileDefinitionsAndDirectories(target, config, incs, compileDefs);
- AddDefinitionEscaped(makefile, "_moc_incs", incs);
- AddDefinitionEscaped(makefile, "_moc_compile_defs", compileDefs);
- // Configuration specific settings
- for (std::vector<std::string>::const_iterator li = configs.begin();
- li != configs.end(); ++li) {
- std::string configIncs;
- std::string configCompileDefs;
- GetCompileDefinitionsAndDirectories(target, *li, configIncs,
- configCompileDefs);
- if (configIncs != incs) {
- configMocIncludes[*li] = cmOutputConverter::EscapeForCMake(configIncs);
- }
- if (configCompileDefs != compileDefs) {
- configMocDefines[*li] =
- cmOutputConverter::EscapeForCMake(configCompileDefs);
- }
- }
- }
- // Moc executable
- {
- std::string err;
- const char* mocExec = CM_NULLPTR;
- if (qtMajorVersion == "5") {
- cmGeneratorTarget* qt5Moc = lg->FindGeneratorTargetToUse("Qt5::moc");
- if (qt5Moc != CM_NULLPTR) {
- mocExec = qt5Moc->ImportedGetLocation("");
- } else {
- err = "Qt5::moc target not found " + autogenTargetName;
- }
- } else if (qtMajorVersion == "4") {
- cmGeneratorTarget* qt4Moc = lg->FindGeneratorTargetToUse("Qt4::moc");
- if (qt4Moc != CM_NULLPTR) {
- mocExec = qt4Moc->ImportedGetLocation("");
- } else {
- err = "Qt4::moc target not found " + autogenTargetName;
- }
- } else {
- err = "The CMAKE_AUTOMOC feature supports only Qt 4 and Qt 5 ";
- err += autogenTargetName;
- }
- // Add definition or error
- if (err.empty()) {
- AddDefinitionEscaped(makefile, "_qt_moc_executable",
- mocExec ? mocExec : "");
- } else {
- cmSystemTools::Error(err.c_str());
- }
- }
- }
- static void UicGetOpts(cmGeneratorTarget const* target,
- const std::string& config, std::string& optString)
- {
- std::vector<std::string> opts;
- target->GetAutoUicOptions(opts, config);
- optString = cmJoin(opts, ";");
- }
- static void UicSetupAutoTarget(
- cmGeneratorTarget const* target, std::string const& qtMajorVersion,
- std::string const& config, std::vector<std::string> const& configs,
- std::vector<std::string> const& uicSkipList,
- std::map<std::string, std::string>& configUicOptions)
- {
- cmLocalGenerator* lg = target->GetLocalGenerator();
- cmMakefile* makefile = target->Target->GetMakefile();
- AddDefinitionEscaped(makefile, "_uic_skip", uicSkipList);
- // Uic search paths
- {
- std::vector<std::string> uicSearchPaths;
- cmSystemTools::ExpandListArgument(
- GetSafeProperty(target, "AUTOUIC_SEARCH_PATHS"), uicSearchPaths);
- const std::string srcDir = makefile->GetCurrentSourceDirectory();
- for (std::vector<std::string>::iterator it = uicSearchPaths.begin();
- it != uicSearchPaths.end(); ++it) {
- *it = cmSystemTools::CollapseFullPath(*it, srcDir);
- }
- AddDefinitionEscaped(makefile, "_uic_search_paths", uicSearchPaths);
- }
- // Uic target options
- {
- // Default settings
- std::string uicOpts;
- UicGetOpts(target, config, uicOpts);
- AddDefinitionEscaped(makefile, "_uic_target_options", uicOpts);
- // Configuration specific settings
- for (std::vector<std::string>::const_iterator li = configs.begin();
- li != configs.end(); ++li) {
- std::string configUicOpts;
- UicGetOpts(target, *li, configUicOpts);
- if (configUicOpts != uicOpts) {
- configUicOptions[*li] =
- cmOutputConverter::EscapeForCMake(configUicOpts);
- }
- }
- }
- // Uic files options
- {
- std::vector<std::string> uiFileFiles;
- std::vector<std::string> uiFileOptions;
- {
- std::set<std::string> skipped;
- skipped.insert(uicSkipList.begin(), uicSkipList.end());
- const std::vector<cmSourceFile*> uiFilesWithOptions =
- makefile->GetQtUiFilesWithOptions();
- for (std::vector<cmSourceFile*>::const_iterator fileIt =
- uiFilesWithOptions.begin();
- fileIt != uiFilesWithOptions.end(); ++fileIt) {
- cmSourceFile* sf = *fileIt;
- const std::string absFile =
- cmsys::SystemTools::GetRealPath(sf->GetFullPath());
- if (skipped.insert(absFile).second) {
- // The file wasn't skipped
- uiFileFiles.push_back(absFile);
- {
- std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
- cmSystemTools::ReplaceString(opts, ";",
- cmQtAutoGeneratorCommon::listSep);
- uiFileOptions.push_back(opts);
- }
- }
- }
- }
- AddDefinitionEscaped(makefile, "_qt_uic_options_files", uiFileFiles);
- AddDefinitionEscaped(makefile, "_qt_uic_options_options", uiFileOptions);
- }
- // Uic executable
- {
- std::string err;
- const char* uicExec = CM_NULLPTR;
- if (qtMajorVersion == "5") {
- cmGeneratorTarget* qt5Uic = lg->FindGeneratorTargetToUse("Qt5::uic");
- if (qt5Uic != CM_NULLPTR) {
- uicExec = qt5Uic->ImportedGetLocation("");
- } else {
- // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
- }
- } else if (qtMajorVersion == "4") {
- cmGeneratorTarget* qt4Uic = lg->FindGeneratorTargetToUse("Qt4::uic");
- if (qt4Uic != CM_NULLPTR) {
- uicExec = qt4Uic->ImportedGetLocation("");
- } else {
- err = "Qt4::uic target not found " + target->GetName();
- }
- } else {
- err = "The CMAKE_AUTOUIC feature supports only Qt 4 and Qt 5 ";
- err += target->GetName();
- }
- // Add definition or error
- if (err.empty()) {
- AddDefinitionEscaped(makefile, "_qt_uic_executable",
- uicExec ? uicExec : "");
- } else {
- cmSystemTools::Error(err.c_str());
- }
- }
- }
- static std::string RccGetExecutable(cmGeneratorTarget const* target,
- const std::string& qtMajorVersion)
- {
- std::string rccExec;
- cmLocalGenerator* lg = target->GetLocalGenerator();
- if (qtMajorVersion == "5") {
- cmGeneratorTarget* qt5Rcc = lg->FindGeneratorTargetToUse("Qt5::rcc");
- if (qt5Rcc != CM_NULLPTR) {
- rccExec = qt5Rcc->ImportedGetLocation("");
- } else {
- cmSystemTools::Error("Qt5::rcc target not found ",
- target->GetName().c_str());
- }
- } else if (qtMajorVersion == "4") {
- cmGeneratorTarget* qt4Rcc = lg->FindGeneratorTargetToUse("Qt4::rcc");
- if (qt4Rcc != CM_NULLPTR) {
- rccExec = qt4Rcc->ImportedGetLocation("");
- } else {
- cmSystemTools::Error("Qt4::rcc target not found ",
- target->GetName().c_str());
- }
- } else {
- cmSystemTools::Error(
- "The CMAKE_AUTORCC feature supports only Qt 4 and Qt 5 ",
- target->GetName().c_str());
- }
- return rccExec;
- }
- static void RccMergeOptions(std::vector<std::string>& opts,
- const std::vector<std::string>& fileOpts,
- bool isQt5)
- {
- static const char* valueOptions[] = { "name", "root", "compress",
- "threshold" };
- std::vector<std::string> extraOpts;
- for (std::vector<std::string>::const_iterator fit = fileOpts.begin();
- fit != fileOpts.end(); ++fit) {
- std::vector<std::string>::iterator existingIt =
- std::find(opts.begin(), opts.end(), *fit);
- if (existingIt != opts.end()) {
- const char* optName = fit->c_str();
- if (*optName == '-') {
- ++optName;
- if (isQt5 && *optName == '-') {
- ++optName;
- }
- }
- // Test if this is a value option and change the existing value
- if ((optName != fit->c_str()) &&
- std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
- cmStrCmp(optName)) != cmArrayEnd(valueOptions)) {
- const std::vector<std::string>::iterator existValueIt(existingIt + 1);
- const std::vector<std::string>::const_iterator fileValueIt(fit + 1);
- if ((existValueIt != opts.end()) && (fileValueIt != fileOpts.end())) {
- *existValueIt = *fileValueIt;
- ++fit;
- }
- }
- } else {
- extraOpts.push_back(*fit);
- }
- }
- opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
- }
- static void RccSetupAutoTarget(cmGeneratorTarget const* target,
- const std::string& qtMajorVersion)
- {
- cmMakefile* makefile = target->Target->GetMakefile();
- const bool qtMajorVersion5 = (qtMajorVersion == "5");
- const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
- std::vector<std::string> _rcc_files;
- std::vector<std::string> _rcc_inputs;
- std::vector<std::string> rccFileFiles;
- std::vector<std::string> rccFileOptions;
- std::vector<std::string> rccOptionsTarget;
- if (const char* opts = target->GetProperty("AUTORCC_OPTIONS")) {
- cmSystemTools::ExpandListArgument(opts, rccOptionsTarget);
- }
- std::vector<cmSourceFile*> srcFiles;
- target->GetConfigCommonSourceFiles(srcFiles);
- for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
- fileIt != srcFiles.end(); ++fileIt) {
- cmSourceFile* sf = *fileIt;
- if ((sf->GetExtension() == "qrc") &&
- !PropertyEnabled(sf, "SKIP_AUTOGEN") &&
- !PropertyEnabled(sf, "SKIP_AUTORCC")) {
- const std::string absFile =
- cmsys::SystemTools::GetRealPath(sf->GetFullPath());
- // qrc file
- _rcc_files.push_back(absFile);
- // qrc file entries
- {
- std::string entriesList = "{";
- // Read input file list only for non generated .qrc files.
- if (!PropertyEnabled(sf, "GENERATED")) {
- std::string error;
- std::vector<std::string> files;
- if (cmQtAutoGeneratorCommon::RccListInputs(
- qtMajorVersion, rccCommand, absFile, files, &error)) {
- entriesList += cmJoin(files, cmQtAutoGeneratorCommon::listSep);
- } else {
- cmSystemTools::Error(error.c_str());
- }
- }
- entriesList += "}";
- _rcc_inputs.push_back(entriesList);
- }
- // rcc options for this qrc file
- {
- // Merged target and file options
- std::vector<std::string> rccOptions(rccOptionsTarget);
- if (const char* prop = sf->GetProperty("AUTORCC_OPTIONS")) {
- std::vector<std::string> optsVec;
- cmSystemTools::ExpandListArgument(prop, optsVec);
- RccMergeOptions(rccOptions, optsVec, qtMajorVersion5);
- }
- // Only store non empty options lists
- if (!rccOptions.empty()) {
- rccFileFiles.push_back(absFile);
- rccFileOptions.push_back(
- cmJoin(rccOptions, cmQtAutoGeneratorCommon::listSep));
- }
- }
- }
- }
- AddDefinitionEscaped(makefile, "_qt_rcc_executable", rccCommand);
- AddDefinitionEscaped(makefile, "_rcc_files", _rcc_files);
- AddDefinitionEscaped(makefile, "_rcc_inputs", _rcc_inputs);
- AddDefinitionEscaped(makefile, "_rcc_options_files", rccFileFiles);
- AddDefinitionEscaped(makefile, "_rcc_options_options", rccFileOptions);
- }
- void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
- cmGeneratorTarget* target)
- {
- if (target->GetPropertyAsBool("AUTOMOC")) {
- cmMakefile* makefile = target->Target->GetMakefile();
- // Mocs compilation file
- const std::string mocsComp =
- GetAutogenTargetBuildDir(target) + "/mocs_compilation.cpp";
- AddGeneratedSource(makefile, mocsComp, cmQtAutoGeneratorCommon::MOC);
- target->AddSource(mocsComp);
- }
- }
- void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
- cmLocalGenerator* lg, cmGeneratorTarget* target)
- {
- cmMakefile* makefile = target->Target->GetMakefile();
- // Create a custom target for running generators at buildtime
- const bool mocEnabled = target->GetPropertyAsBool("AUTOMOC");
- const bool uicEnabled = target->GetPropertyAsBool("AUTOUIC");
- const bool rccEnabled = target->GetPropertyAsBool("AUTORCC");
- const bool multiConfig = IsMultiConfig(target->GetGlobalGenerator());
- const std::string autogenTargetName = GetAutogenTargetName(target);
- const std::string autogenBuildDir = GetAutogenTargetBuildDir(target);
- const std::string workingDirectory =
- cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
- const std::string qtMajorVersion = GetQtMajorVersion(target);
- const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
- const std::vector<std::string> suffixes = GetConfigurationSuffixes(makefile);
- std::set<std::string> autogenDependsSet;
- std::vector<std::string> autogenProvides;
- // Remove build directories on cleanup
- makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
- autogenBuildDir.c_str(), false);
- // Remove old settings on cleanup
- {
- std::string base = GetAutogenTargetFilesDir(target);
- for (std::vector<std::string>::const_iterator it = suffixes.begin();
- it != suffixes.end(); ++it) {
- std::string fname = base + "/AutogenOldSettings" + *it + ".cmake";
- makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fname.c_str(),
- false);
- }
- }
- // Compose command lines
- cmCustomCommandLines commandLines;
- {
- cmCustomCommandLine currentLine;
- currentLine.push_back(cmSystemTools::GetCMakeCommand());
- currentLine.push_back("-E");
- currentLine.push_back("cmake_autogen");
- currentLine.push_back(GetAutogenTargetFilesDir(target));
- currentLine.push_back("$<CONFIGURATION>");
- commandLines.push_back(currentLine);
- }
- // Compose target comment
- std::string autogenComment;
- {
- std::vector<std::string> toolNames;
- if (mocEnabled) {
- toolNames.push_back("MOC");
- }
- if (uicEnabled) {
- toolNames.push_back("UIC");
- }
- if (rccEnabled) {
- toolNames.push_back("RCC");
- }
- std::string tools = toolNames[0];
- toolNames.erase(toolNames.begin());
- while (toolNames.size() > 1) {
- tools += ", " + toolNames[0];
- toolNames.erase(toolNames.begin());
- }
- if (toolNames.size() == 1) {
- tools += " and " + toolNames[0];
- }
- autogenComment = "Automatic " + tools + " for target " + target->GetName();
- }
- // Add moc compilation to generated files list
- if (mocEnabled) {
- const std::string mocsComp = autogenBuildDir + "/mocs_compilation.cpp";
- autogenProvides.push_back(mocsComp);
- }
- // Add autogen includes directory to the origin target INCLUDE_DIRECTORIES
- if (mocEnabled || uicEnabled) {
- std::string includeDir = autogenBuildDir + "/include";
- if (multiConfig) {
- includeDir += "_$<CONFIG>";
- }
- target->AddIncludeDirectory(includeDir, true);
- }
- #if defined(_WIN32) && !defined(__CYGWIN__)
- bool usePRE_BUILD = false;
- cmGlobalGenerator* gg = lg->GetGlobalGenerator();
- if (gg->GetName().find("Visual Studio") != std::string::npos) {
- // Under VS use a PRE_BUILD event instead of a separate target to
- // reduce the number of targets loaded into the IDE.
- // This also works around a VS 11 bug that may skip updating the target:
- // https://connect.microsoft.com/VisualStudio/feedback/details/769495
- usePRE_BUILD = true;
- }
- #endif
- // Initialize autogen target dependencies
- if (const char* deps = target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
- std::vector<std::string> extraDepends;
- cmSystemTools::ExpandListArgument(deps, extraDepends);
- autogenDependsSet.insert(extraDepends.begin(), extraDepends.end());
- }
- // Add other target dependencies autogen dependencies
- {
- const std::set<std::string>& utils = target->Target->GetUtilities();
- for (std::set<std::string>::const_iterator it = utils.begin();
- it != utils.end(); ++it) {
- const std::string& targetName = *it;
- if (makefile->FindTargetToUse(targetName) != CM_NULLPTR) {
- autogenDependsSet.insert(targetName);
- }
- }
- }
- // Add link library targets to the autogen dependencies
- {
- const cmTarget::LinkLibraryVectorType& libVec =
- target->Target->GetOriginalLinkLibraries();
- for (cmTarget::LinkLibraryVectorType::const_iterator it = libVec.begin();
- it != libVec.end(); ++it) {
- const std::string& libName = it->first;
- if (makefile->FindTargetToUse(libName) != CM_NULLPTR) {
- autogenDependsSet.insert(libName);
- }
- }
- }
- {
- cmFilePathChecksum fpathCheckSum(makefile);
- // Iterate over all source files
- std::vector<cmSourceFile*> srcFiles;
- target->GetConfigCommonSourceFiles(srcFiles);
- for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
- fileIt != srcFiles.end(); ++fileIt) {
- cmSourceFile* sf = *fileIt;
- if (!PropertyEnabled(sf, "SKIP_AUTOGEN")) {
- std::string const& ext = sf->GetExtension();
- // Add generated file that will be scanned by moc or uic to
- // the dependencies
- if (mocEnabled || uicEnabled) {
- const cmSystemTools::FileFormat fileType =
- cmSystemTools::GetFileFormat(ext.c_str());
- if ((fileType == cmSystemTools::CXX_FILE_FORMAT) ||
- (fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
- if (PropertyEnabled(sf, "GENERATED")) {
- if ((mocEnabled && !PropertyEnabled(sf, "SKIP_AUTOMOC")) ||
- (uicEnabled && !PropertyEnabled(sf, "SKIP_AUTOUIC"))) {
- autogenDependsSet.insert(
- cmsys::SystemTools::GetRealPath(sf->GetFullPath()));
- #if defined(_WIN32) && !defined(__CYGWIN__)
- // Cannot use PRE_BUILD with generated files
- usePRE_BUILD = false;
- #endif
- }
- }
- }
- }
- // Process rcc enabled files
- if (rccEnabled && (ext == "qrc") &&
- !PropertyEnabled(sf, "SKIP_AUTORCC")) {
- const std::string absFile =
- cmsys::SystemTools::GetRealPath(sf->GetFullPath());
- // Compose rcc output file name
- {
- std::string rccBuildFile = autogenBuildDir + "/";
- rccBuildFile += fpathCheckSum.getPart(absFile);
- rccBuildFile += "/qrc_";
- rccBuildFile +=
- cmsys::SystemTools::GetFilenameWithoutLastExtension(absFile);
- rccBuildFile += ".cpp";
- // Register rcc ouput file as generated
- AddGeneratedSource(makefile, rccBuildFile,
- cmQtAutoGeneratorCommon::RCC);
- // Add rcc output file to origin target sources
- target->AddSource(rccBuildFile);
- // Register rcc ouput file as generated by the _autogen target
- autogenProvides.push_back(rccBuildFile);
- }
- if (PropertyEnabled(sf, "GENERATED")) {
- // Add generated qrc file to the dependencies
- autogenDependsSet.insert(absFile);
- } else {
- // Run cmake again when .qrc file changes
- makefile->AddCMakeDependFile(absFile);
- // Add the qrc input files to the dependencies
- {
- std::string error;
- std::vector<std::string> extraDepends;
- if (cmQtAutoGeneratorCommon::RccListInputs(
- qtMajorVersion, rccCommand, absFile, extraDepends,
- &error)) {
- autogenDependsSet.insert(extraDepends.begin(),
- extraDepends.end());
- } else {
- cmSystemTools::Error(error.c_str());
- }
- }
- }
- #if defined(_WIN32) && !defined(__CYGWIN__)
- // Cannot use PRE_BUILD because the resource files themselves
- // may not be sources within the target so VS may not know the
- // target needs to re-build at all.
- usePRE_BUILD = false;
- #endif
- }
- }
- }
- }
- // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
- // sources meta data cache. Clear it so that OBJECT library targets that
- // are AUTOGEN initialized after this target get their added
- // mocs_compilation.cpp source acknowledged by this target.
- target->ClearSourcesCache();
- // Convert std::set to std::vector
- const std::vector<std::string> autogenDepends(autogenDependsSet.begin(),
- autogenDependsSet.end());
- #if defined(_WIN32) && !defined(__CYGWIN__)
- if (usePRE_BUILD) {
- // If the autogen target depends on an other target don't use PRE_BUILD
- for (std::vector<std::string>::const_iterator it = autogenDepends.begin();
- it != autogenDepends.end(); ++it) {
- if (makefile->FindTargetToUse(*it) != CM_NULLPTR) {
- usePRE_BUILD = false;
- break;
- }
- }
- }
- if (usePRE_BUILD) {
- // Add the pre-build command directly to bypass the OBJECT_LIBRARY
- // rejection in cmMakefile::AddCustomCommandToTarget because we know
- // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
- std::vector<std::string> no_output;
- cmCustomCommand cc(makefile, no_output, autogenProvides, autogenDepends,
- commandLines, autogenComment.c_str(),
- workingDirectory.c_str());
- cc.SetEscapeOldStyle(false);
- cc.SetEscapeAllowMakeVars(true);
- target->Target->AddPreBuildCommand(cc);
- } else
- #endif
- {
- cmTarget* autogenTarget = makefile->AddUtilityCommand(
- autogenTargetName, true, workingDirectory.c_str(),
- /*byproducts=*/autogenProvides, autogenDepends, commandLines, false,
- autogenComment.c_str());
- cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
- lg->AddGeneratorTarget(gt);
- // Set target folder
- const char* autogenFolder =
- makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
- if (!autogenFolder) {
- autogenFolder =
- makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
- }
- if (autogenFolder && *autogenFolder) {
- autogenTarget->SetProperty("FOLDER", autogenFolder);
- } else {
- // inherit FOLDER property from target (#13688)
- utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
- }
- target->Target->AddUtility(autogenTargetName);
- }
- }
- void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
- cmGeneratorTarget const* target)
- {
- cmMakefile* makefile = target->Target->GetMakefile();
- // forget the variables added here afterwards again:
- cmMakefile::ScopePushPop varScope(makefile);
- static_cast<void>(varScope);
- // Get configurations
- std::string config;
- const std::vector<std::string> configs(GetConfigurations(makefile, &config));
- // Configurations settings buffers
- std::map<std::string, std::string> configSuffix;
- std::map<std::string, std::string> configMocIncludes;
- std::map<std::string, std::string> configMocDefines;
- std::map<std::string, std::string> configUicOptions;
- // Configuration suffix
- if (IsMultiConfig(target->GetGlobalGenerator())) {
- for (std::vector<std::string>::const_iterator it = configs.begin();
- it != configs.end(); ++it) {
- configSuffix[*it] = cmOutputConverter::EscapeForCMake("_" + *it);
- }
- }
- // Basic setup
- {
- const bool mocEnabled = target->GetPropertyAsBool("AUTOMOC");
- const bool uicEnabled = target->GetPropertyAsBool("AUTOUIC");
- const bool rccEnabled = target->GetPropertyAsBool("AUTORCC");
- const std::string autogenTargetName = GetAutogenTargetName(target);
- const std::string qtMajorVersion = GetQtMajorVersion(target);
- std::vector<std::string> sources;
- std::vector<std::string> headers;
- if (mocEnabled || uicEnabled || rccEnabled) {
- std::vector<std::string> mocSkipList;
- std::vector<std::string> uicSkipList;
- AcquireScanFiles(target, sources, headers, mocSkipList, uicSkipList);
- if (mocEnabled) {
- MocSetupAutoTarget(target, autogenTargetName, qtMajorVersion, config,
- configs, mocSkipList, configMocIncludes,
- configMocDefines);
- }
- if (uicEnabled) {
- UicSetupAutoTarget(target, qtMajorVersion, config, configs,
- uicSkipList, configUicOptions);
- }
- if (rccEnabled) {
- RccSetupAutoTarget(target, qtMajorVersion);
- }
- }
- AddDefinitionEscaped(makefile, "_autogen_build_dir",
- GetAutogenTargetBuildDir(target));
- AddDefinitionEscaped(makefile, "_qt_version_major", qtMajorVersion);
- AddDefinitionEscaped(makefile, "_sources", sources);
- AddDefinitionEscaped(makefile, "_headers", headers);
- }
- // Generate info file
- std::string infoFile = GetAutogenTargetFilesDir(target);
- infoFile += "/AutogenInfo.cmake";
- {
- std::string inf = cmSystemTools::GetCMakeRoot();
- inf += "/Modules/AutogenInfo.cmake.in";
- makefile->ConfigureFile(inf.c_str(), infoFile.c_str(), false, true, false);
- }
- // Append custom definitions to info file on demand
- if (!configSuffix.empty() || !configMocDefines.empty() ||
- !configMocIncludes.empty() || !configUicOptions.empty()) {
- // Ensure we have write permission in case .in was read-only.
- mode_t perm = 0;
- #if defined(_WIN32) && !defined(__CYGWIN__)
- mode_t mode_write = S_IWRITE;
- #else
- mode_t mode_write = S_IWUSR;
- #endif
- cmSystemTools::GetPermissions(infoFile, perm);
- if (!(perm & mode_write)) {
- cmSystemTools::SetPermissions(infoFile, perm | mode_write);
- }
- // Open and write file
- cmsys::ofstream ofs(infoFile.c_str(), std::ios::app);
- if (ofs) {
- ofs << "# Configuration specific options\n";
- for (std::map<std::string, std::string>::iterator
- it = configSuffix.begin(),
- end = configSuffix.end();
- it != end; ++it) {
- ofs << "set(AM_CONFIG_SUFFIX_" << it->first << " " << it->second
- << ")\n";
- }
- for (std::map<std::string, std::string>::iterator
- it = configMocDefines.begin(),
- end = configMocDefines.end();
- it != end; ++it) {
- ofs << "set(AM_MOC_DEFINITIONS_" << it->first << " " << it->second
- << ")\n";
- }
- for (std::map<std::string, std::string>::iterator
- it = configMocIncludes.begin(),
- end = configMocIncludes.end();
- it != end; ++it) {
- ofs << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
- << ")\n";
- }
- for (std::map<std::string, std::string>::iterator
- it = configUicOptions.begin(),
- end = configUicOptions.end();
- it != end; ++it) {
- ofs << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " " << it->second
- << ")\n";
- }
- } else {
- // File open error
- std::string error = "Internal CMake error when trying to open file: ";
- error += cmQtAutoGeneratorCommon::Quoted(infoFile);
- error += " for writing.";
- cmSystemTools::Error(error.c_str());
- }
- }
- }
|