cmQtAutoGenInitializer.cxx 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoGenInitializer.h"
  4. #include "cmQtAutoGen.h"
  5. #include "cmQtAutoGenGlobalInitializer.h"
  6. #include "cmAlgorithms.h"
  7. #include "cmCustomCommand.h"
  8. #include "cmCustomCommandLines.h"
  9. #include "cmFilePathChecksum.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLinkItem.h"
  14. #include "cmListFileCache.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmOutputConverter.h"
  19. #include "cmPolicies.h"
  20. #include "cmSourceFile.h"
  21. #include "cmSourceFileLocationKind.h"
  22. #include "cmSourceGroup.h"
  23. #include "cmState.h"
  24. #include "cmStateTypes.h"
  25. #include "cmSystemTools.h"
  26. #include "cmTarget.h"
  27. #include "cmake.h"
  28. #include "cmsys/SystemInformation.hxx"
  29. #include <algorithm>
  30. #include <array>
  31. #include <deque>
  32. #include <map>
  33. #include <set>
  34. #include <string>
  35. #include <unordered_set>
  36. #include <utility>
  37. #include <vector>
  38. #include "cm_memory.hxx"
  39. static std::size_t GetParallelCPUCount()
  40. {
  41. static std::size_t count = 0;
  42. // Detect only on the first call
  43. if (count == 0) {
  44. cmsys::SystemInformation info;
  45. info.RunCPUCheck();
  46. count = info.GetNumberOfPhysicalCPU();
  47. count = std::max<std::size_t>(count, 1);
  48. count = std::min<std::size_t>(count, cmQtAutoGen::ParallelMax);
  49. }
  50. return count;
  51. }
  52. static std::string FileProjectRelativePath(cmMakefile* makefile,
  53. std::string const& fileName)
  54. {
  55. std::string res;
  56. {
  57. std::string pSource = cmSystemTools::RelativePath(
  58. makefile->GetCurrentSourceDirectory(), fileName);
  59. std::string pBinary = cmSystemTools::RelativePath(
  60. makefile->GetCurrentBinaryDirectory(), fileName);
  61. if (pSource.size() < pBinary.size()) {
  62. res = std::move(pSource);
  63. } else if (pBinary.size() < fileName.size()) {
  64. res = std::move(pBinary);
  65. } else {
  66. res = fileName;
  67. }
  68. }
  69. return res;
  70. }
  71. /**
  72. * Tests if targetDepend is a STATIC_LIBRARY and if any of its
  73. * recursive STATIC_LIBRARY dependencies depends on targetOrigin
  74. * (STATIC_LIBRARY cycle).
  75. */
  76. static bool StaticLibraryCycle(cmGeneratorTarget const* targetOrigin,
  77. cmGeneratorTarget const* targetDepend,
  78. std::string const& config)
  79. {
  80. bool cycle = false;
  81. if ((targetOrigin->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  82. (targetDepend->GetType() == cmStateEnums::STATIC_LIBRARY)) {
  83. std::set<cmGeneratorTarget const*> knownLibs;
  84. std::deque<cmGeneratorTarget const*> testLibs;
  85. // Insert initial static_library dependency
  86. knownLibs.insert(targetDepend);
  87. testLibs.push_back(targetDepend);
  88. while (!testLibs.empty()) {
  89. cmGeneratorTarget const* testTarget = testLibs.front();
  90. testLibs.pop_front();
  91. // Check if the test target is the origin target (cycle)
  92. if (testTarget == targetOrigin) {
  93. cycle = true;
  94. break;
  95. }
  96. // Collect all static_library dependencies from the test target
  97. cmLinkImplementationLibraries const* libs =
  98. testTarget->GetLinkImplementationLibraries(config);
  99. if (libs != nullptr) {
  100. for (cmLinkItem const& item : libs->Libraries) {
  101. cmGeneratorTarget const* depTarget = item.Target;
  102. if ((depTarget != nullptr) &&
  103. (depTarget->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  104. knownLibs.insert(depTarget).second) {
  105. testLibs.push_back(depTarget);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. return cycle;
  112. }
  113. cmQtAutoGenInitializer::InfoWriter::InfoWriter(std::string const& filename)
  114. {
  115. Ofs_.SetCopyIfDifferent(true);
  116. Ofs_.Open(filename, false, true);
  117. }
  118. template <class IT>
  119. std::string cmQtAutoGenInitializer::InfoWriter::ListJoin(IT it_begin,
  120. IT it_end)
  121. {
  122. std::string res;
  123. for (IT it = it_begin; it != it_end; ++it) {
  124. if (it != it_begin) {
  125. res += ';';
  126. }
  127. for (const char* c = it->c_str(); *c; ++c) {
  128. if (*c == '"') {
  129. // Escape the double quote to avoid ending the argument.
  130. res += "\\\"";
  131. } else if (*c == '$') {
  132. // Escape the dollar to avoid expanding variables.
  133. res += "\\$";
  134. } else if (*c == '\\') {
  135. // Escape the backslash to avoid other escapes.
  136. res += "\\\\";
  137. } else if (*c == ';') {
  138. // Escape the semicolon to avoid list expansion.
  139. res += "\\;";
  140. } else {
  141. // Other characters will be parsed correctly.
  142. res += *c;
  143. }
  144. }
  145. }
  146. return res;
  147. }
  148. std::string cmQtAutoGenInitializer::InfoWriter::ConfigKey(
  149. const char* key, std::string const& config)
  150. {
  151. std::string ckey = key;
  152. ckey += '_';
  153. ckey += config;
  154. return ckey;
  155. }
  156. void cmQtAutoGenInitializer::InfoWriter::Write(const char* key,
  157. std::string const& value)
  158. {
  159. Ofs_ << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
  160. << ")\n";
  161. };
  162. void cmQtAutoGenInitializer::InfoWriter::WriteUInt(const char* key,
  163. unsigned int value)
  164. {
  165. Ofs_ << "set(" << key << " " << value << ")\n";
  166. };
  167. template <class C>
  168. void cmQtAutoGenInitializer::InfoWriter::WriteStrings(const char* key,
  169. C const& container)
  170. {
  171. Ofs_ << "set(" << key << " \""
  172. << ListJoin(container.begin(), container.end()) << "\")\n";
  173. }
  174. void cmQtAutoGenInitializer::InfoWriter::WriteConfig(
  175. const char* key, std::map<std::string, std::string> const& map)
  176. {
  177. for (auto const& item : map) {
  178. Write(ConfigKey(key, item.first).c_str(), item.second);
  179. }
  180. };
  181. template <class C>
  182. void cmQtAutoGenInitializer::InfoWriter::WriteConfigStrings(
  183. const char* key, std::map<std::string, C> const& map)
  184. {
  185. for (auto const& item : map) {
  186. WriteStrings(ConfigKey(key, item.first).c_str(), item.second);
  187. }
  188. }
  189. void cmQtAutoGenInitializer::InfoWriter::WriteNestedLists(
  190. const char* key, std::vector<std::vector<std::string>> const& lists)
  191. {
  192. std::vector<std::string> seplist;
  193. for (const std::vector<std::string>& list : lists) {
  194. std::string blist = "{";
  195. blist += ListJoin(list.begin(), list.end());
  196. blist += "}";
  197. seplist.push_back(std::move(blist));
  198. }
  199. Write(key, cmJoin(seplist, cmQtAutoGen::ListSep));
  200. };
  201. cmQtAutoGenInitializer::cmQtAutoGenInitializer(
  202. cmQtAutoGenGlobalInitializer* globalInitializer, cmGeneratorTarget* target,
  203. IntegerVersion const& qtVersion, bool mocEnabled, bool uicEnabled,
  204. bool rccEnabled, bool globalAutogenTarget, bool globalAutoRccTarget)
  205. : GlobalInitializer(globalInitializer)
  206. , Target(target)
  207. , QtVersion(qtVersion)
  208. {
  209. AutogenTarget.GlobalTarget = globalAutogenTarget;
  210. Moc.Enabled = mocEnabled;
  211. Uic.Enabled = uicEnabled;
  212. Rcc.Enabled = rccEnabled;
  213. Rcc.GlobalTarget = globalAutoRccTarget;
  214. }
  215. bool cmQtAutoGenInitializer::InitCustomTargets()
  216. {
  217. cmMakefile* makefile = this->Target->Target->GetMakefile();
  218. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  219. cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
  220. // Configurations
  221. this->MultiConfig = globalGen->IsMultiConfig();
  222. this->ConfigDefault = makefile->GetConfigurations(this->ConfigsList);
  223. if (this->ConfigsList.empty()) {
  224. this->ConfigsList.push_back(this->ConfigDefault);
  225. }
  226. // Verbosity
  227. this->Verbosity = makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE");
  228. if (!this->Verbosity.empty()) {
  229. unsigned long iVerb = 0;
  230. if (!cmSystemTools::StringToULong(this->Verbosity.c_str(), &iVerb)) {
  231. // Non numeric verbosity
  232. this->Verbosity = cmSystemTools::IsOn(this->Verbosity) ? "1" : "0";
  233. }
  234. }
  235. // Targets FOLDER
  236. {
  237. const char* folder =
  238. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  239. if (folder == nullptr) {
  240. folder =
  241. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  242. }
  243. // Inherit FOLDER property from target (#13688)
  244. if (folder == nullptr) {
  245. folder = this->Target->GetProperty("FOLDER");
  246. }
  247. if (folder != nullptr) {
  248. this->TargetsFolder = folder;
  249. }
  250. }
  251. // Check status of policy CMP0071
  252. {
  253. cmPolicies::PolicyStatus const CMP0071_status =
  254. makefile->GetPolicyStatus(cmPolicies::CMP0071);
  255. switch (CMP0071_status) {
  256. case cmPolicies::WARN:
  257. this->CMP0071Warn = true;
  258. CM_FALLTHROUGH;
  259. case cmPolicies::OLD:
  260. // Ignore GENERATED file
  261. break;
  262. case cmPolicies::REQUIRED_IF_USED:
  263. case cmPolicies::REQUIRED_ALWAYS:
  264. case cmPolicies::NEW:
  265. // Process GENERATED file
  266. this->CMP0071Accept = true;
  267. break;
  268. }
  269. }
  270. // Common directories
  271. {
  272. // Collapsed current binary directory
  273. std::string const cbd = cmSystemTools::CollapseFullPath(
  274. std::string(), makefile->GetCurrentBinaryDirectory());
  275. // Info directory
  276. this->Dir.Info = cbd;
  277. this->Dir.Info += "/CMakeFiles";
  278. this->Dir.Info += '/';
  279. this->Dir.Info += this->Target->GetName();
  280. this->Dir.Info += "_autogen";
  281. this->Dir.Info += ".dir";
  282. cmSystemTools::ConvertToUnixSlashes(this->Dir.Info);
  283. // Build directory
  284. this->Dir.Build = this->Target->GetSafeProperty("AUTOGEN_BUILD_DIR");
  285. if (this->Dir.Build.empty()) {
  286. this->Dir.Build = cbd;
  287. this->Dir.Build += '/';
  288. this->Dir.Build += this->Target->GetName();
  289. this->Dir.Build += "_autogen";
  290. }
  291. cmSystemTools::ConvertToUnixSlashes(this->Dir.Build);
  292. // Cleanup build directory
  293. this->AddCleanFile(this->Dir.Build);
  294. // Working directory
  295. this->Dir.Work = cbd;
  296. cmSystemTools::ConvertToUnixSlashes(this->Dir.Work);
  297. // Include directory
  298. this->Dir.Include = this->Dir.Build;
  299. this->Dir.Include += "/include";
  300. if (this->MultiConfig) {
  301. this->Dir.Include += "_$<CONFIG>";
  302. }
  303. // Per config include directories
  304. if (this->MultiConfig) {
  305. for (std::string const& cfg : this->ConfigsList) {
  306. std::string& dir = this->Dir.ConfigInclude[cfg];
  307. dir = this->Dir.Build;
  308. dir += "/include_";
  309. dir += cfg;
  310. }
  311. }
  312. }
  313. // Moc, Uic and _autogen target settings
  314. if (this->MocOrUicEnabled()) {
  315. // Init moc specific settings
  316. if (this->Moc.Enabled && !InitMoc()) {
  317. return false;
  318. }
  319. // Init uic specific settings
  320. if (this->Uic.Enabled && !InitUic()) {
  321. return false;
  322. }
  323. // Autogen target name
  324. this->AutogenTarget.Name = this->Target->GetName();
  325. this->AutogenTarget.Name += "_autogen";
  326. // Autogen target parallel processing
  327. this->AutogenTarget.Parallel =
  328. this->Target->GetSafeProperty("AUTOGEN_PARALLEL");
  329. if (this->AutogenTarget.Parallel.empty() ||
  330. (this->AutogenTarget.Parallel == "AUTO")) {
  331. // Autodetect number of CPUs
  332. this->AutogenTarget.Parallel = std::to_string(GetParallelCPUCount());
  333. }
  334. // Autogen target info and settings files
  335. {
  336. this->AutogenTarget.InfoFile = this->Dir.Info;
  337. this->AutogenTarget.InfoFile += "/AutogenInfo.cmake";
  338. this->AutogenTarget.SettingsFile = this->Dir.Info;
  339. this->AutogenTarget.SettingsFile += "/AutogenOldSettings.txt";
  340. if (this->MultiConfig) {
  341. for (std::string const& cfg : this->ConfigsList) {
  342. std::string& filename = this->AutogenTarget.ConfigSettingsFile[cfg];
  343. filename =
  344. AppendFilenameSuffix(this->AutogenTarget.SettingsFile, "_" + cfg);
  345. this->AddCleanFile(filename);
  346. }
  347. } else {
  348. this->AddCleanFile(this->AutogenTarget.SettingsFile);
  349. }
  350. this->AutogenTarget.ParseCacheFile = this->Dir.Info;
  351. this->AutogenTarget.ParseCacheFile += "/ParseCache.txt";
  352. this->AddCleanFile(this->AutogenTarget.ParseCacheFile);
  353. }
  354. // Autogen target: Compute user defined dependencies
  355. {
  356. this->AutogenTarget.DependOrigin =
  357. this->Target->GetPropertyAsBool("AUTOGEN_ORIGIN_DEPENDS");
  358. std::string const deps =
  359. this->Target->GetSafeProperty("AUTOGEN_TARGET_DEPENDS");
  360. if (!deps.empty()) {
  361. std::vector<std::string> extraDeps;
  362. cmSystemTools::ExpandListArgument(deps, extraDeps);
  363. for (std::string const& depName : extraDeps) {
  364. // Allow target and file dependencies
  365. auto* depTarget = makefile->FindTargetToUse(depName);
  366. if (depTarget != nullptr) {
  367. this->AutogenTarget.DependTargets.insert(depTarget);
  368. } else {
  369. this->AutogenTarget.DependFiles.insert(depName);
  370. }
  371. }
  372. }
  373. }
  374. // CMAKE_AUTOMOC_RELAXED_MODE deprecation warning
  375. if (this->Moc.Enabled) {
  376. if (cmSystemTools::IsOn(
  377. makefile->GetDefinition("CMAKE_AUTOMOC_RELAXED_MODE"))) {
  378. std::string msg = "AUTOMOC: CMAKE_AUTOMOC_RELAXED_MODE is "
  379. "deprecated an will be removed in the future. ";
  380. msg += "Consider disabling it and converting the target ";
  381. msg += this->Target->GetName();
  382. msg += " to regular mode.";
  383. makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  384. }
  385. }
  386. }
  387. // Init rcc specific settings
  388. if (this->Rcc.Enabled && !InitRcc()) {
  389. return false;
  390. }
  391. // Add autogen include directory to the origin target INCLUDE_DIRECTORIES
  392. if (this->MocOrUicEnabled() || (this->Rcc.Enabled && this->MultiConfig)) {
  393. this->Target->AddIncludeDirectory(this->Dir.Include, true);
  394. }
  395. // Scan files
  396. if (!this->InitScanFiles()) {
  397. return false;
  398. }
  399. // Create autogen target
  400. if (this->MocOrUicEnabled() && !this->InitAutogenTarget()) {
  401. return false;
  402. }
  403. // Create rcc targets
  404. if (this->Rcc.Enabled && !this->InitRccTargets()) {
  405. return false;
  406. }
  407. return true;
  408. }
  409. bool cmQtAutoGenInitializer::InitMoc()
  410. {
  411. cmMakefile* makefile = this->Target->Target->GetMakefile();
  412. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  413. // Mocs compilation file
  414. this->Moc.MocsCompilation = this->Dir.Build;
  415. this->Moc.MocsCompilation += "/mocs_compilation.cpp";
  416. // Moc predefs command
  417. if (this->Target->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES") &&
  418. (this->QtVersion >= IntegerVersion(5, 8))) {
  419. this->Moc.PredefsCmd =
  420. makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND");
  421. }
  422. // Moc includes
  423. {
  424. bool const appendImplicit = (this->QtVersion.Major >= 5);
  425. auto GetIncludeDirs =
  426. [this, localGen,
  427. appendImplicit](std::string const& cfg) -> std::vector<std::string> {
  428. // Get the include dirs for this target, without stripping the implicit
  429. // include dirs off, see
  430. // https://gitlab.kitware.com/cmake/cmake/issues/13667
  431. std::vector<std::string> dirs;
  432. localGen->GetIncludeDirectoriesImplicit(dirs, this->Target, "CXX", cfg,
  433. false, appendImplicit);
  434. return dirs;
  435. };
  436. // Default configuration include directories
  437. this->Moc.Includes = GetIncludeDirs(this->ConfigDefault);
  438. // Other configuration settings
  439. if (this->MultiConfig) {
  440. for (std::string const& cfg : this->ConfigsList) {
  441. std::vector<std::string> dirs = GetIncludeDirs(cfg);
  442. if (dirs != this->Moc.Includes) {
  443. this->Moc.ConfigIncludes[cfg] = std::move(dirs);
  444. }
  445. }
  446. }
  447. }
  448. // Moc compile definitions
  449. {
  450. auto GetCompileDefinitions =
  451. [this, localGen](std::string const& cfg) -> std::set<std::string> {
  452. std::set<std::string> defines;
  453. localGen->GetTargetDefines(this->Target, cfg, "CXX", defines);
  454. #ifdef _WIN32
  455. if (this->Moc.PredefsCmd.empty()) {
  456. // Add WIN32 definition if we don't have a moc_predefs.h
  457. defines.insert("WIN32");
  458. }
  459. #endif
  460. return defines;
  461. };
  462. // Default configuration defines
  463. this->Moc.Defines = GetCompileDefinitions(this->ConfigDefault);
  464. // Other configuration defines
  465. if (this->MultiConfig) {
  466. for (std::string const& cfg : this->ConfigsList) {
  467. std::set<std::string> defines = GetCompileDefinitions(cfg);
  468. if (defines != this->Moc.Defines) {
  469. this->Moc.ConfigDefines[cfg] = std::move(defines);
  470. }
  471. }
  472. }
  473. }
  474. // Moc executable
  475. {
  476. if (!this->GetQtExecutable(this->Moc, "moc", false)) {
  477. return false;
  478. }
  479. // Let the _autogen target depend on the moc executable
  480. if (this->Moc.ExecutableTarget != nullptr) {
  481. this->AutogenTarget.DependTargets.insert(
  482. this->Moc.ExecutableTarget->Target);
  483. }
  484. }
  485. return true;
  486. }
  487. bool cmQtAutoGenInitializer::InitUic()
  488. {
  489. cmMakefile* makefile = this->Target->Target->GetMakefile();
  490. // Uic search paths
  491. {
  492. std::string const usp =
  493. this->Target->GetSafeProperty("AUTOUIC_SEARCH_PATHS");
  494. if (!usp.empty()) {
  495. cmSystemTools::ExpandListArgument(usp, this->Uic.SearchPaths);
  496. std::string const& srcDir = makefile->GetCurrentSourceDirectory();
  497. for (std::string& path : this->Uic.SearchPaths) {
  498. path = cmSystemTools::CollapseFullPath(path, srcDir);
  499. }
  500. }
  501. }
  502. // Uic target options
  503. {
  504. auto UicGetOpts =
  505. [this](std::string const& cfg) -> std::vector<std::string> {
  506. std::vector<std::string> opts;
  507. this->Target->GetAutoUicOptions(opts, cfg);
  508. return opts;
  509. };
  510. // Default settings
  511. this->Uic.Options = UicGetOpts(this->ConfigDefault);
  512. // Configuration specific settings
  513. if (this->MultiConfig) {
  514. for (std::string const& cfg : this->ConfigsList) {
  515. std::vector<std::string> options = UicGetOpts(cfg);
  516. if (options != this->Uic.Options) {
  517. this->Uic.ConfigOptions[cfg] = std::move(options);
  518. }
  519. }
  520. }
  521. }
  522. // Uic executable
  523. {
  524. if (!this->GetQtExecutable(this->Uic, "uic", true)) {
  525. return false;
  526. }
  527. // Let the _autogen target depend on the uic executable
  528. if (this->Uic.ExecutableTarget != nullptr) {
  529. this->AutogenTarget.DependTargets.insert(
  530. this->Uic.ExecutableTarget->Target);
  531. }
  532. }
  533. return true;
  534. }
  535. bool cmQtAutoGenInitializer::InitRcc()
  536. {
  537. // Rcc executable
  538. {
  539. if (!this->GetQtExecutable(this->Rcc, "rcc", false)) {
  540. return false;
  541. }
  542. // Evaluate test output on demand
  543. CompilerFeatures& features = *this->Rcc.ExecutableFeatures;
  544. if (!features.Evaluated) {
  545. // Look for list options
  546. if (this->QtVersion.Major == 5 || this->QtVersion.Major == 6) {
  547. if (features.HelpOutput.find("--list") != std::string::npos) {
  548. features.ListOptions.emplace_back("--list");
  549. } else if (features.HelpOutput.find("-list") != std::string::npos) {
  550. features.ListOptions.emplace_back("-list");
  551. }
  552. }
  553. // Evaluation finished
  554. features.Evaluated = true;
  555. }
  556. }
  557. return true;
  558. }
  559. bool cmQtAutoGenInitializer::InitScanFiles()
  560. {
  561. cmMakefile* makefile = this->Target->Target->GetMakefile();
  562. cmake const* cm = makefile->GetCMakeInstance();
  563. auto const& kw = this->GlobalInitializer->kw();
  564. auto makeMUFile = [this, &kw](cmSourceFile* sf, std::string const& fullPath,
  565. bool muIt) -> MUFileHandle {
  566. MUFileHandle muf = cm::make_unique<MUFile>();
  567. muf->RealPath = cmSystemTools::GetRealPath(fullPath);
  568. muf->SF = sf;
  569. muf->Generated = sf->GetIsGenerated();
  570. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  571. muf->SkipMoc = this->Moc.Enabled &&
  572. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOMOC));
  573. muf->SkipUic = this->Uic.Enabled &&
  574. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  575. if (muIt) {
  576. muf->MocIt = this->Moc.Enabled && !muf->SkipMoc;
  577. muf->UicIt = this->Uic.Enabled && !muf->SkipUic;
  578. }
  579. return muf;
  580. };
  581. auto addMUFile = [&](MUFileHandle&& muf, bool isHeader) {
  582. if ((muf->MocIt || muf->UicIt) && muf->Generated) {
  583. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  584. }
  585. if (isHeader) {
  586. this->AutogenTarget.Headers.emplace(muf->SF, std::move(muf));
  587. } else {
  588. this->AutogenTarget.Sources.emplace(muf->SF, std::move(muf));
  589. }
  590. };
  591. // Scan through target files
  592. {
  593. // Scan through target files
  594. std::vector<cmSourceFile*> srcFiles;
  595. this->Target->GetConfigCommonSourceFiles(srcFiles);
  596. for (cmSourceFile* sf : srcFiles) {
  597. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  598. // Since we're iterating over source files that might be not in the
  599. // target we need to check for path errors (not existing files).
  600. std::string pathError;
  601. std::string const& fullPath = sf->GetFullPath(&pathError);
  602. if (!pathError.empty() || fullPath.empty()) {
  603. continue;
  604. }
  605. std::string const& extLower =
  606. cmSystemTools::LowerCase(sf->GetExtension());
  607. // Register files that will be scanned by moc or uic
  608. if (this->MocOrUicEnabled()) {
  609. if (cm->IsHeaderExtension(extLower)) {
  610. addMUFile(makeMUFile(sf, fullPath, true), true);
  611. } else if (cm->IsSourceExtension(extLower)) {
  612. addMUFile(makeMUFile(sf, fullPath, true), false);
  613. }
  614. }
  615. // Register rcc enabled files
  616. if (this->Rcc.Enabled) {
  617. if ((extLower == kw.qrc) && !sf->GetPropertyAsBool(kw.SKIP_AUTOGEN) &&
  618. !sf->GetPropertyAsBool(kw.SKIP_AUTORCC)) {
  619. // Register qrc file
  620. Qrc qrc;
  621. qrc.QrcFile = cmSystemTools::GetRealPath(fullPath);
  622. qrc.QrcName =
  623. cmSystemTools::GetFilenameWithoutLastExtension(qrc.QrcFile);
  624. qrc.Generated = sf->GetIsGenerated();
  625. // RCC options
  626. {
  627. std::string const opts = sf->GetSafeProperty(kw.AUTORCC_OPTIONS);
  628. if (!opts.empty()) {
  629. cmSystemTools::ExpandListArgument(opts, qrc.Options);
  630. }
  631. }
  632. this->Rcc.Qrcs.push_back(std::move(qrc));
  633. }
  634. }
  635. }
  636. }
  637. // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
  638. // sources meta data cache. Clear it so that OBJECT library targets that
  639. // are AUTOGEN initialized after this target get their added
  640. // mocs_compilation.cpp source acknowledged by this target.
  641. this->Target->ClearSourcesCache();
  642. // For source files find additional headers and private headers
  643. if (this->MocOrUicEnabled()) {
  644. std::vector<MUFileHandle> extraHeaders;
  645. extraHeaders.reserve(this->AutogenTarget.Sources.size() * 2);
  646. // Header search suffixes and extensions
  647. std::array<std::string, 2> const suffixes{ { "", "_p" } };
  648. auto const& exts = cm->GetHeaderExtensions();
  649. // Scan through sources
  650. for (auto const& pair : this->AutogenTarget.Sources) {
  651. MUFile const& muf = *pair.second;
  652. if (muf.MocIt || muf.UicIt) {
  653. // Search for the default header file and a private header
  654. std::string const& srcPath = muf.SF->GetFullPath();
  655. std::string basePath = cmQtAutoGen::SubDirPrefix(srcPath);
  656. basePath += cmSystemTools::GetFilenameWithoutLastExtension(srcPath);
  657. for (auto const& suffix : suffixes) {
  658. std::string const suffixedPath = basePath + suffix;
  659. for (auto const& ext : exts) {
  660. std::string fullPath = suffixedPath;
  661. fullPath += '.';
  662. fullPath += ext;
  663. auto constexpr locationKind = cmSourceFileLocationKind::Known;
  664. cmSourceFile* sf = makefile->GetSource(fullPath, locationKind);
  665. if (sf != nullptr) {
  666. // Check if we know about this header already
  667. if (this->AutogenTarget.Headers.find(sf) !=
  668. this->AutogenTarget.Headers.end()) {
  669. continue;
  670. }
  671. // We only accept not-GENERATED files that do exist.
  672. if (!sf->GetIsGenerated() &&
  673. !cmSystemTools::FileExists(fullPath)) {
  674. continue;
  675. }
  676. } else if (cmSystemTools::FileExists(fullPath)) {
  677. // Create a new source file for the existing file
  678. sf = makefile->CreateSource(fullPath, false, locationKind);
  679. }
  680. if (sf != nullptr) {
  681. auto eMuf = makeMUFile(sf, fullPath, true);
  682. // Ony process moc/uic when the parent is processed as well
  683. if (!muf.MocIt) {
  684. eMuf->MocIt = false;
  685. }
  686. if (!muf.UicIt) {
  687. eMuf->UicIt = false;
  688. }
  689. extraHeaders.emplace_back(std::move(eMuf));
  690. }
  691. }
  692. }
  693. }
  694. }
  695. // Move generated files to main headers list
  696. for (auto& eMuf : extraHeaders) {
  697. addMUFile(std::move(eMuf), true);
  698. }
  699. }
  700. // Scan through all source files in the makefile to extract moc and uic
  701. // parameters. Historically we support non target source file parameters.
  702. // The reason is that their file names might be discovered from source files
  703. // at generation time.
  704. if (this->MocOrUicEnabled()) {
  705. for (cmSourceFile* sf : makefile->GetSourceFiles()) {
  706. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  707. // Since we're iterating over source files that might be not in the
  708. // target we need to check for path errors (not existing files).
  709. std::string pathError;
  710. std::string const& fullPath = sf->GetFullPath(&pathError);
  711. if (!pathError.empty() || fullPath.empty()) {
  712. continue;
  713. }
  714. std::string const& extLower =
  715. cmSystemTools::LowerCase(sf->GetExtension());
  716. if (cm->IsHeaderExtension(extLower)) {
  717. if (this->AutogenTarget.Headers.find(sf) ==
  718. this->AutogenTarget.Headers.end()) {
  719. auto muf = makeMUFile(sf, fullPath, false);
  720. if (muf->SkipMoc || muf->SkipUic) {
  721. this->AutogenTarget.Headers.emplace(sf, std::move(muf));
  722. }
  723. }
  724. } else if (cm->IsSourceExtension(extLower)) {
  725. if (this->AutogenTarget.Sources.find(sf) ==
  726. this->AutogenTarget.Sources.end()) {
  727. auto muf = makeMUFile(sf, fullPath, false);
  728. if (muf->SkipMoc || muf->SkipUic) {
  729. this->AutogenTarget.Sources.emplace(sf, std::move(muf));
  730. }
  731. }
  732. } else if (this->Uic.Enabled && (extLower == kw.ui)) {
  733. // .ui file
  734. std::string realPath = cmSystemTools::GetRealPath(fullPath);
  735. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  736. bool const skipUic =
  737. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  738. if (!skipUic) {
  739. // Check if the .ui file has uic options
  740. std::string const uicOpts = sf->GetSafeProperty(kw.AUTOUIC_OPTIONS);
  741. if (!uicOpts.empty()) {
  742. this->Uic.FileFiles.push_back(std::move(realPath));
  743. std::vector<std::string> optsVec;
  744. cmSystemTools::ExpandListArgument(uicOpts, optsVec);
  745. this->Uic.FileOptions.push_back(std::move(optsVec));
  746. }
  747. } else {
  748. // Register skipped .ui file
  749. this->Uic.SkipUi.insert(std::move(realPath));
  750. }
  751. }
  752. }
  753. }
  754. // Process GENERATED sources and headers
  755. if (this->MocOrUicEnabled() && !this->AutogenTarget.FilesGenerated.empty()) {
  756. if (this->CMP0071Accept) {
  757. // Let the autogen target depend on the GENERATED files
  758. for (MUFile* muf : this->AutogenTarget.FilesGenerated) {
  759. this->AutogenTarget.DependFiles.insert(muf->RealPath);
  760. }
  761. } else if (this->CMP0071Warn) {
  762. std::string msg;
  763. msg += cmPolicies::GetPolicyWarning(cmPolicies::CMP0071);
  764. msg += '\n';
  765. std::string property;
  766. if (this->Moc.Enabled && this->Uic.Enabled) {
  767. property = kw.SKIP_AUTOGEN;
  768. } else if (this->Moc.Enabled) {
  769. property = kw.SKIP_AUTOMOC;
  770. } else if (this->Uic.Enabled) {
  771. property = kw.SKIP_AUTOUIC;
  772. }
  773. msg += "For compatibility, CMake is excluding the GENERATED source "
  774. "file(s):\n";
  775. for (MUFile* muf : this->AutogenTarget.FilesGenerated) {
  776. msg += " ";
  777. msg += Quoted(muf->RealPath);
  778. msg += '\n';
  779. }
  780. msg += "from processing by ";
  781. msg += cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false);
  782. msg += ". If any of the files should be processed, set CMP0071 to NEW. "
  783. "If any of the files should not be processed, "
  784. "explicitly exclude them by setting the source file property ";
  785. msg += property;
  786. msg += ":\n set_property(SOURCE file.h PROPERTY ";
  787. msg += property;
  788. msg += " ON)\n";
  789. makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  790. }
  791. }
  792. // Process qrc files
  793. if (!this->Rcc.Qrcs.empty()) {
  794. const bool modernQt = (this->QtVersion.Major >= 5);
  795. // Target rcc options
  796. std::vector<std::string> optionsTarget;
  797. cmSystemTools::ExpandListArgument(
  798. this->Target->GetSafeProperty(kw.AUTORCC_OPTIONS), optionsTarget);
  799. // Check if file name is unique
  800. for (Qrc& qrc : this->Rcc.Qrcs) {
  801. qrc.Unique = true;
  802. for (Qrc const& qrc2 : this->Rcc.Qrcs) {
  803. if ((&qrc != &qrc2) && (qrc.QrcName == qrc2.QrcName)) {
  804. qrc.Unique = false;
  805. break;
  806. }
  807. }
  808. }
  809. // Path checksum and file names
  810. {
  811. cmFilePathChecksum const fpathCheckSum(makefile);
  812. for (Qrc& qrc : this->Rcc.Qrcs) {
  813. qrc.PathChecksum = fpathCheckSum.getPart(qrc.QrcFile);
  814. // RCC output file name
  815. {
  816. std::string rccFile = this->Dir.Build + "/";
  817. rccFile += qrc.PathChecksum;
  818. rccFile += "/qrc_";
  819. rccFile += qrc.QrcName;
  820. rccFile += ".cpp";
  821. qrc.RccFile = std::move(rccFile);
  822. }
  823. {
  824. std::string base = this->Dir.Info;
  825. base += "/RCC";
  826. base += qrc.QrcName;
  827. if (!qrc.Unique) {
  828. base += qrc.PathChecksum;
  829. }
  830. qrc.LockFile = base;
  831. qrc.LockFile += ".lock";
  832. qrc.InfoFile = base;
  833. qrc.InfoFile += "Info.cmake";
  834. qrc.SettingsFile = base;
  835. qrc.SettingsFile += "Settings.txt";
  836. if (this->MultiConfig) {
  837. for (std::string const& cfg : this->ConfigsList) {
  838. qrc.ConfigSettingsFile[cfg] =
  839. AppendFilenameSuffix(qrc.SettingsFile, "_" + cfg);
  840. }
  841. }
  842. }
  843. }
  844. }
  845. // RCC options
  846. for (Qrc& qrc : this->Rcc.Qrcs) {
  847. // Target options
  848. std::vector<std::string> opts = optionsTarget;
  849. // Merge computed "-name XYZ" option
  850. {
  851. std::string name = qrc.QrcName;
  852. // Replace '-' with '_'. The former is not valid for symbol names.
  853. std::replace(name.begin(), name.end(), '-', '_');
  854. if (!qrc.Unique) {
  855. name += "_";
  856. name += qrc.PathChecksum;
  857. }
  858. std::vector<std::string> nameOpts;
  859. nameOpts.emplace_back("-name");
  860. nameOpts.emplace_back(std::move(name));
  861. RccMergeOptions(opts, nameOpts, modernQt);
  862. }
  863. // Merge file option
  864. RccMergeOptions(opts, qrc.Options, modernQt);
  865. qrc.Options = std::move(opts);
  866. }
  867. // RCC resources
  868. for (Qrc& qrc : this->Rcc.Qrcs) {
  869. if (!qrc.Generated) {
  870. std::string error;
  871. RccLister const lister(this->Rcc.Executable,
  872. this->Rcc.ExecutableFeatures->ListOptions);
  873. if (!lister.list(qrc.QrcFile, qrc.Resources, error)) {
  874. cmSystemTools::Error(error);
  875. return false;
  876. }
  877. }
  878. }
  879. }
  880. return true;
  881. }
  882. bool cmQtAutoGenInitializer::InitAutogenTarget()
  883. {
  884. cmMakefile* makefile = this->Target->Target->GetMakefile();
  885. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  886. cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
  887. // Register info file as generated by CMake
  888. makefile->AddCMakeOutputFile(this->AutogenTarget.InfoFile);
  889. // Files provided by the autogen target
  890. std::vector<std::string> autogenProvides;
  891. if (this->Moc.Enabled) {
  892. this->AddGeneratedSource(this->Moc.MocsCompilation, this->Moc, true);
  893. autogenProvides.push_back(this->Moc.MocsCompilation);
  894. }
  895. // Compose target comment
  896. std::string autogenComment;
  897. {
  898. std::string tools;
  899. if (this->Moc.Enabled) {
  900. tools += "MOC";
  901. }
  902. if (this->Uic.Enabled) {
  903. if (!tools.empty()) {
  904. tools += " and ";
  905. }
  906. tools += "UIC";
  907. }
  908. autogenComment = "Automatic ";
  909. autogenComment += tools;
  910. autogenComment += " for target ";
  911. autogenComment += this->Target->GetName();
  912. }
  913. // Compose command lines
  914. cmCustomCommandLines commandLines;
  915. {
  916. cmCustomCommandLine currentLine;
  917. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  918. currentLine.push_back("-E");
  919. currentLine.push_back("cmake_autogen");
  920. currentLine.push_back(this->AutogenTarget.InfoFile);
  921. currentLine.push_back("$<CONFIGURATION>");
  922. commandLines.push_back(std::move(currentLine));
  923. }
  924. // Use PRE_BUILD on demand
  925. bool usePRE_BUILD = false;
  926. if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
  927. // Under VS use a PRE_BUILD event instead of a separate target to
  928. // reduce the number of targets loaded into the IDE.
  929. // This also works around a VS 11 bug that may skip updating the target:
  930. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  931. usePRE_BUILD = true;
  932. }
  933. // Disable PRE_BUILD in some cases
  934. if (usePRE_BUILD) {
  935. // Cannot use PRE_BUILD with file depends
  936. if (!this->AutogenTarget.DependFiles.empty()) {
  937. usePRE_BUILD = false;
  938. }
  939. // Cannot use PRE_BUILD when a global autogen target is in place
  940. if (AutogenTarget.GlobalTarget) {
  941. usePRE_BUILD = false;
  942. }
  943. }
  944. // Create the autogen target/command
  945. if (usePRE_BUILD) {
  946. // Add additional autogen target dependencies to origin target
  947. for (cmTarget* depTarget : this->AutogenTarget.DependTargets) {
  948. this->Target->Target->AddUtility(depTarget->GetName(), makefile);
  949. }
  950. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  951. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  952. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  953. //
  954. // PRE_BUILD does not support file dependencies!
  955. const std::vector<std::string> no_output;
  956. const std::vector<std::string> no_deps;
  957. cmCustomCommand cc(makefile, no_output, autogenProvides, no_deps,
  958. commandLines, autogenComment.c_str(),
  959. this->Dir.Work.c_str());
  960. cc.SetEscapeOldStyle(false);
  961. cc.SetEscapeAllowMakeVars(true);
  962. this->Target->Target->AddPreBuildCommand(cc);
  963. } else {
  964. // Add link library target dependencies to the autogen target
  965. // dependencies
  966. if (this->AutogenTarget.DependOrigin) {
  967. // add_dependencies/addUtility do not support generator expressions.
  968. // We depend only on the libraries found in all configs therefore.
  969. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  970. for (std::string const& config : this->ConfigsList) {
  971. cmLinkImplementationLibraries const* libs =
  972. this->Target->GetLinkImplementationLibraries(config);
  973. if (libs != nullptr) {
  974. for (cmLinkItem const& item : libs->Libraries) {
  975. cmGeneratorTarget const* libTarget = item.Target;
  976. if ((libTarget != nullptr) &&
  977. !StaticLibraryCycle(this->Target, libTarget, config)) {
  978. // Increment target config count
  979. commonTargets[libTarget]++;
  980. }
  981. }
  982. }
  983. }
  984. for (auto const& item : commonTargets) {
  985. if (item.second == this->ConfigsList.size()) {
  986. this->AutogenTarget.DependTargets.insert(item.first->Target);
  987. }
  988. }
  989. }
  990. // Create autogen target
  991. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  992. this->AutogenTarget.Name, cmMakefile::TargetOrigin::Generator, true,
  993. this->Dir.Work.c_str(), /*byproducts=*/autogenProvides,
  994. std::vector<std::string>(this->AutogenTarget.DependFiles.begin(),
  995. this->AutogenTarget.DependFiles.end()),
  996. commandLines, false, autogenComment.c_str());
  997. // Create autogen generator target
  998. localGen->AddGeneratorTarget(
  999. new cmGeneratorTarget(autogenTarget, localGen));
  1000. // Forward origin utilities to autogen target
  1001. if (this->AutogenTarget.DependOrigin) {
  1002. for (BT<std::string> const& depName : this->Target->GetUtilities()) {
  1003. autogenTarget->AddUtility(depName.Value, makefile);
  1004. }
  1005. }
  1006. // Add additional autogen target dependencies to autogen target
  1007. for (cmTarget* depTarget : this->AutogenTarget.DependTargets) {
  1008. autogenTarget->AddUtility(depTarget->GetName(), makefile);
  1009. }
  1010. // Set FOLDER property in autogen target
  1011. if (!this->TargetsFolder.empty()) {
  1012. autogenTarget->SetProperty("FOLDER", this->TargetsFolder.c_str());
  1013. }
  1014. // Add autogen target to the origin target dependencies
  1015. this->Target->Target->AddUtility(this->AutogenTarget.Name, makefile);
  1016. // Add autogen target to the global autogen target dependencies
  1017. if (this->AutogenTarget.GlobalTarget) {
  1018. this->GlobalInitializer->AddToGlobalAutoGen(localGen,
  1019. this->AutogenTarget.Name);
  1020. }
  1021. }
  1022. return true;
  1023. }
  1024. bool cmQtAutoGenInitializer::InitRccTargets()
  1025. {
  1026. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1027. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1028. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1029. // Register info file as generated by CMake
  1030. makefile->AddCMakeOutputFile(qrc.InfoFile);
  1031. // Register file at target
  1032. this->AddGeneratedSource(qrc.RccFile, this->Rcc);
  1033. std::vector<std::string> ccOutput;
  1034. ccOutput.push_back(qrc.RccFile);
  1035. std::vector<std::string> ccDepends;
  1036. // Add the .qrc and info file to the custom command dependencies
  1037. ccDepends.push_back(qrc.QrcFile);
  1038. ccDepends.push_back(qrc.InfoFile);
  1039. cmCustomCommandLines commandLines;
  1040. if (this->MultiConfig) {
  1041. // Build for all configurations
  1042. for (std::string const& config : this->ConfigsList) {
  1043. cmCustomCommandLine currentLine;
  1044. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  1045. currentLine.push_back("-E");
  1046. currentLine.push_back("cmake_autorcc");
  1047. currentLine.push_back(qrc.InfoFile);
  1048. currentLine.push_back(config);
  1049. commandLines.push_back(std::move(currentLine));
  1050. }
  1051. } else {
  1052. cmCustomCommandLine currentLine;
  1053. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  1054. currentLine.push_back("-E");
  1055. currentLine.push_back("cmake_autorcc");
  1056. currentLine.push_back(qrc.InfoFile);
  1057. currentLine.push_back("$<CONFIG>");
  1058. commandLines.push_back(std::move(currentLine));
  1059. }
  1060. std::string ccComment = "Automatic RCC for ";
  1061. ccComment += FileProjectRelativePath(makefile, qrc.QrcFile);
  1062. if (qrc.Generated || this->Rcc.GlobalTarget) {
  1063. // Create custom rcc target
  1064. std::string ccName;
  1065. {
  1066. ccName = this->Target->GetName();
  1067. ccName += "_arcc_";
  1068. ccName += qrc.QrcName;
  1069. if (!qrc.Unique) {
  1070. ccName += "_";
  1071. ccName += qrc.PathChecksum;
  1072. }
  1073. cmTarget* autoRccTarget = makefile->AddUtilityCommand(
  1074. ccName, cmMakefile::TargetOrigin::Generator, true,
  1075. this->Dir.Work.c_str(), ccOutput, ccDepends, commandLines, false,
  1076. ccComment.c_str());
  1077. // Create autogen generator target
  1078. localGen->AddGeneratorTarget(
  1079. new cmGeneratorTarget(autoRccTarget, localGen));
  1080. // Set FOLDER property in autogen target
  1081. if (!this->TargetsFolder.empty()) {
  1082. autoRccTarget->SetProperty("FOLDER", this->TargetsFolder.c_str());
  1083. }
  1084. if (!this->Rcc.ExecutableTargetName.empty()) {
  1085. autoRccTarget->AddUtility(this->Rcc.ExecutableTargetName, makefile);
  1086. }
  1087. }
  1088. // Add autogen target to the origin target dependencies
  1089. this->Target->Target->AddUtility(ccName, makefile);
  1090. // Add autogen target to the global autogen target dependencies
  1091. if (this->Rcc.GlobalTarget) {
  1092. this->GlobalInitializer->AddToGlobalAutoRcc(localGen, ccName);
  1093. }
  1094. } else {
  1095. // Create custom rcc command
  1096. {
  1097. std::vector<std::string> ccByproducts;
  1098. // Add the resource files to the dependencies
  1099. for (std::string const& fileName : qrc.Resources) {
  1100. // Add resource file to the custom command dependencies
  1101. ccDepends.push_back(fileName);
  1102. }
  1103. if (!this->Rcc.ExecutableTargetName.empty()) {
  1104. ccDepends.push_back(this->Rcc.ExecutableTargetName);
  1105. }
  1106. makefile->AddCustomCommandToOutput(ccOutput, ccByproducts, ccDepends,
  1107. /*main_dependency*/ std::string(),
  1108. commandLines, ccComment.c_str(),
  1109. this->Dir.Work.c_str());
  1110. }
  1111. // Reconfigure when .qrc file changes
  1112. makefile->AddCMakeDependFile(qrc.QrcFile);
  1113. }
  1114. }
  1115. return true;
  1116. }
  1117. bool cmQtAutoGenInitializer::SetupCustomTargets()
  1118. {
  1119. // Create info directory on demand
  1120. if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
  1121. std::string emsg = ("AutoGen: Could not create directory: ");
  1122. emsg += Quoted(this->Dir.Info);
  1123. cmSystemTools::Error(emsg);
  1124. return false;
  1125. }
  1126. // Generate autogen target info file
  1127. if (this->MocOrUicEnabled()) {
  1128. // Write autogen target info files
  1129. if (!this->SetupWriteAutogenInfo()) {
  1130. return false;
  1131. }
  1132. }
  1133. // Write AUTORCC info files
  1134. return !this->Rcc.Enabled || this->SetupWriteRccInfo();
  1135. }
  1136. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  1137. {
  1138. InfoWriter ofs(this->AutogenTarget.InfoFile);
  1139. if (ofs) {
  1140. // Utility lambdas
  1141. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1142. auto MfDef = [makefile](const char* key) {
  1143. return makefile->GetSafeDefinition(key);
  1144. };
  1145. // Write common settings
  1146. ofs.Write("# Meta\n");
  1147. ofs.Write("AM_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  1148. ofs.Write("AM_PARALLEL", this->AutogenTarget.Parallel);
  1149. ofs.Write("AM_VERBOSITY", this->Verbosity);
  1150. ofs.Write("# Directories\n");
  1151. ofs.Write("AM_CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1152. ofs.Write("AM_CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1153. ofs.Write("AM_CMAKE_CURRENT_SOURCE_DIR",
  1154. MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1155. ofs.Write("AM_CMAKE_CURRENT_BINARY_DIR",
  1156. MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1157. ofs.Write("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE",
  1158. MfDef("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"));
  1159. ofs.Write("AM_BUILD_DIR", this->Dir.Build);
  1160. ofs.Write("AM_INCLUDE_DIR", this->Dir.Include);
  1161. ofs.WriteConfig("AM_INCLUDE_DIR", this->Dir.ConfigInclude);
  1162. std::vector<std::string> headers;
  1163. std::vector<std::string> headersFlags;
  1164. std::vector<std::string> headersBuildPaths;
  1165. std::vector<std::string> sources;
  1166. std::vector<std::string> sourcesFlags;
  1167. std::set<std::string> moc_skip;
  1168. std::set<std::string> uic_skip;
  1169. // Filter headers
  1170. {
  1171. auto headerCount = this->AutogenTarget.Headers.size();
  1172. headers.reserve(headerCount);
  1173. headersFlags.reserve(headerCount);
  1174. std::vector<MUFile const*> sortedHeaders;
  1175. {
  1176. sortedHeaders.reserve(headerCount);
  1177. for (auto const& pair : this->AutogenTarget.Headers) {
  1178. sortedHeaders.emplace_back(pair.second.get());
  1179. }
  1180. std::sort(sortedHeaders.begin(), sortedHeaders.end(),
  1181. [](MUFile const* a, MUFile const* b) {
  1182. return (a->RealPath < b->RealPath);
  1183. });
  1184. }
  1185. for (MUFile const* const muf : sortedHeaders) {
  1186. if (muf->Generated && !this->CMP0071Accept) {
  1187. continue;
  1188. }
  1189. if (muf->SkipMoc) {
  1190. moc_skip.insert(muf->RealPath);
  1191. }
  1192. if (muf->SkipUic) {
  1193. uic_skip.insert(muf->RealPath);
  1194. }
  1195. if (muf->MocIt || muf->UicIt) {
  1196. headers.emplace_back(muf->RealPath);
  1197. std::string flags;
  1198. flags += muf->MocIt ? 'M' : 'm';
  1199. flags += muf->UicIt ? 'U' : 'u';
  1200. headersFlags.emplace_back(std::move(flags));
  1201. }
  1202. }
  1203. }
  1204. // Header build paths
  1205. {
  1206. cmFilePathChecksum const fpathCheckSum(makefile);
  1207. std::unordered_set<std::string> emitted;
  1208. for (std::string const& hdr : headers) {
  1209. std::string basePath = fpathCheckSum.getPart(hdr);
  1210. basePath += "/moc_";
  1211. basePath += cmSystemTools::GetFilenameWithoutLastExtension(hdr);
  1212. for (unsigned int ii = 1; ii != 1024; ++ii) {
  1213. std::string path = basePath;
  1214. if (ii > 1) {
  1215. path += '_';
  1216. path += std::to_string(ii);
  1217. }
  1218. path += ".cpp";
  1219. if (emitted.emplace(path).second) {
  1220. headersBuildPaths.emplace_back(std::move(path));
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. // Filter sources
  1227. {
  1228. auto sourcesCount = this->AutogenTarget.Sources.size();
  1229. sources.reserve(sourcesCount);
  1230. sourcesFlags.reserve(sourcesCount);
  1231. std::vector<MUFile const*> sorted;
  1232. sorted.reserve(sourcesCount);
  1233. for (auto const& pair : this->AutogenTarget.Sources) {
  1234. sorted.emplace_back(pair.second.get());
  1235. }
  1236. std::sort(sorted.begin(), sorted.end(),
  1237. [](MUFile const* a, MUFile const* b) {
  1238. return (a->RealPath < b->RealPath);
  1239. });
  1240. for (MUFile const* const muf : sorted) {
  1241. if (muf->Generated && !this->CMP0071Accept) {
  1242. continue;
  1243. }
  1244. if (muf->SkipMoc) {
  1245. moc_skip.insert(muf->RealPath);
  1246. }
  1247. if (muf->SkipUic) {
  1248. uic_skip.insert(muf->RealPath);
  1249. }
  1250. if (muf->MocIt || muf->UicIt) {
  1251. sources.emplace_back(muf->RealPath);
  1252. std::string flags;
  1253. flags += muf->MocIt ? 'M' : 'm';
  1254. flags += muf->UicIt ? 'U' : 'u';
  1255. sourcesFlags.emplace_back(std::move(flags));
  1256. }
  1257. }
  1258. }
  1259. ofs.Write("# Qt\n");
  1260. ofs.WriteUInt("AM_QT_VERSION_MAJOR", this->QtVersion.Major);
  1261. ofs.Write("AM_QT_MOC_EXECUTABLE", this->Moc.Executable);
  1262. ofs.Write("AM_QT_UIC_EXECUTABLE", this->Uic.Executable);
  1263. ofs.Write("# Files\n");
  1264. ofs.Write("AM_CMAKE_EXECUTABLE", cmSystemTools::GetCMakeCommand());
  1265. ofs.Write("AM_SETTINGS_FILE", this->AutogenTarget.SettingsFile);
  1266. ofs.WriteConfig("AM_SETTINGS_FILE",
  1267. this->AutogenTarget.ConfigSettingsFile);
  1268. ofs.Write("AM_PARSE_CACHE_FILE", this->AutogenTarget.ParseCacheFile);
  1269. ofs.WriteStrings("AM_HEADERS", headers);
  1270. ofs.WriteStrings("AM_HEADERS_FLAGS", headersFlags);
  1271. ofs.WriteStrings("AM_HEADERS_BUILD_PATHS", headersBuildPaths);
  1272. ofs.WriteStrings("AM_SOURCES", sources);
  1273. ofs.WriteStrings("AM_SOURCES_FLAGS", sourcesFlags);
  1274. // Write moc settings
  1275. if (this->Moc.Enabled) {
  1276. ofs.Write("# MOC settings\n");
  1277. ofs.WriteStrings("AM_MOC_SKIP", moc_skip);
  1278. ofs.WriteStrings("AM_MOC_DEFINITIONS", this->Moc.Defines);
  1279. ofs.WriteConfigStrings("AM_MOC_DEFINITIONS", this->Moc.ConfigDefines);
  1280. ofs.WriteStrings("AM_MOC_INCLUDES", this->Moc.Includes);
  1281. ofs.WriteConfigStrings("AM_MOC_INCLUDES", this->Moc.ConfigIncludes);
  1282. ofs.Write("AM_MOC_OPTIONS",
  1283. this->Target->GetSafeProperty("AUTOMOC_MOC_OPTIONS"));
  1284. ofs.Write("AM_MOC_RELAXED_MODE", MfDef("CMAKE_AUTOMOC_RELAXED_MODE"));
  1285. ofs.Write("AM_MOC_MACRO_NAMES",
  1286. this->Target->GetSafeProperty("AUTOMOC_MACRO_NAMES"));
  1287. ofs.Write("AM_MOC_DEPEND_FILTERS",
  1288. this->Target->GetSafeProperty("AUTOMOC_DEPEND_FILTERS"));
  1289. ofs.Write("AM_MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  1290. }
  1291. // Write uic settings
  1292. if (this->Uic.Enabled) {
  1293. // Add skipped .ui files
  1294. uic_skip.insert(this->Uic.SkipUi.begin(), this->Uic.SkipUi.end());
  1295. ofs.Write("# UIC settings\n");
  1296. ofs.WriteStrings("AM_UIC_SKIP", uic_skip);
  1297. ofs.WriteStrings("AM_UIC_TARGET_OPTIONS", this->Uic.Options);
  1298. ofs.WriteConfigStrings("AM_UIC_TARGET_OPTIONS", this->Uic.ConfigOptions);
  1299. ofs.WriteStrings("AM_UIC_OPTIONS_FILES", this->Uic.FileFiles);
  1300. ofs.WriteNestedLists("AM_UIC_OPTIONS_OPTIONS", this->Uic.FileOptions);
  1301. ofs.WriteStrings("AM_UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  1302. }
  1303. } else {
  1304. std::string err = "AutoGen: Could not write file ";
  1305. err += this->AutogenTarget.InfoFile;
  1306. cmSystemTools::Error(err);
  1307. return false;
  1308. }
  1309. return true;
  1310. }
  1311. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  1312. {
  1313. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1314. InfoWriter ofs(qrc.InfoFile);
  1315. if (ofs) {
  1316. // Write
  1317. ofs.Write("# Configurations\n");
  1318. ofs.Write("ARCC_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  1319. ofs.Write("ARCC_VERBOSITY", this->Verbosity);
  1320. ofs.Write("# Settings file\n");
  1321. ofs.Write("ARCC_SETTINGS_FILE", qrc.SettingsFile);
  1322. ofs.WriteConfig("ARCC_SETTINGS_FILE", qrc.ConfigSettingsFile);
  1323. ofs.Write("# Directories\n");
  1324. ofs.Write("ARCC_BUILD_DIR", this->Dir.Build);
  1325. ofs.Write("ARCC_INCLUDE_DIR", this->Dir.Include);
  1326. ofs.WriteConfig("ARCC_INCLUDE_DIR", this->Dir.ConfigInclude);
  1327. ofs.Write("# Rcc executable\n");
  1328. ofs.Write("ARCC_RCC_EXECUTABLE", this->Rcc.Executable);
  1329. ofs.WriteStrings("ARCC_RCC_LIST_OPTIONS",
  1330. this->Rcc.ExecutableFeatures->ListOptions);
  1331. ofs.Write("# Rcc job\n");
  1332. ofs.Write("ARCC_LOCK_FILE", qrc.LockFile);
  1333. ofs.Write("ARCC_SOURCE", qrc.QrcFile);
  1334. ofs.Write("ARCC_OUTPUT_CHECKSUM", qrc.PathChecksum);
  1335. ofs.Write("ARCC_OUTPUT_NAME",
  1336. cmSystemTools::GetFilenameName(qrc.RccFile));
  1337. ofs.WriteStrings("ARCC_OPTIONS", qrc.Options);
  1338. ofs.WriteStrings("ARCC_INPUTS", qrc.Resources);
  1339. } else {
  1340. std::string err = "AutoRcc: Could not write file ";
  1341. err += qrc.InfoFile;
  1342. cmSystemTools::Error(err);
  1343. return false;
  1344. }
  1345. }
  1346. return true;
  1347. }
  1348. void cmQtAutoGenInitializer::RegisterGeneratedSource(
  1349. std::string const& filename)
  1350. {
  1351. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1352. cmSourceFile* gFile = makefile->GetOrCreateSource(filename, true);
  1353. gFile->SetProperty("GENERATED", "1");
  1354. gFile->SetProperty("SKIP_AUTOGEN", "1");
  1355. }
  1356. bool cmQtAutoGenInitializer::AddGeneratedSource(std::string const& filename,
  1357. GenVarsT const& genVars,
  1358. bool prepend)
  1359. {
  1360. // Register source at makefile
  1361. this->RegisterGeneratedSource(filename);
  1362. // Add source file to target
  1363. this->Target->AddSource(filename, prepend);
  1364. // Add source file to source group
  1365. return this->AddToSourceGroup(filename, genVars.GenNameUpper);
  1366. }
  1367. bool cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName,
  1368. std::string const& genNameUpper)
  1369. {
  1370. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1371. cmSourceGroup* sourceGroup = nullptr;
  1372. // Acquire source group
  1373. {
  1374. std::string property;
  1375. std::string groupName;
  1376. {
  1377. // Prefer generator specific source group name
  1378. std::array<std::string, 2> props{ { genNameUpper + "_SOURCE_GROUP",
  1379. "AUTOGEN_SOURCE_GROUP" } };
  1380. for (std::string& prop : props) {
  1381. const char* propName = makefile->GetState()->GetGlobalProperty(prop);
  1382. if ((propName != nullptr) && (*propName != '\0')) {
  1383. groupName = propName;
  1384. property = std::move(prop);
  1385. break;
  1386. }
  1387. }
  1388. }
  1389. // Generate a source group on demand
  1390. if (!groupName.empty()) {
  1391. sourceGroup = makefile->GetOrCreateSourceGroup(groupName);
  1392. if (sourceGroup == nullptr) {
  1393. std::string err;
  1394. err += genNameUpper;
  1395. err += " error in ";
  1396. err += property;
  1397. err += ": Could not find or create the source group ";
  1398. err += cmQtAutoGen::Quoted(groupName);
  1399. cmSystemTools::Error(err);
  1400. return false;
  1401. }
  1402. }
  1403. }
  1404. if (sourceGroup != nullptr) {
  1405. sourceGroup->AddGroupFile(fileName);
  1406. }
  1407. return true;
  1408. }
  1409. void cmQtAutoGenInitializer::AddCleanFile(std::string const& fileName)
  1410. {
  1411. Target->Target->AppendProperty("ADDITIONAL_CLEAN_FILES", fileName.c_str(),
  1412. false);
  1413. }
  1414. static unsigned int CharPtrToUInt(const char* const input)
  1415. {
  1416. unsigned long tmp = 0;
  1417. if (input != nullptr && cmSystemTools::StringToULong(input, &tmp)) {
  1418. return static_cast<unsigned int>(tmp);
  1419. }
  1420. return 0;
  1421. }
  1422. static std::vector<cmQtAutoGen::IntegerVersion> GetKnownQtVersions(
  1423. cmGeneratorTarget const* target)
  1424. {
  1425. // Qt version variable prefixes
  1426. static std::array<std::string, 3> const prefixes{ { "Qt6Core", "Qt5Core",
  1427. "QT" } };
  1428. std::vector<cmQtAutoGen::IntegerVersion> result;
  1429. result.reserve(prefixes.size() * 2);
  1430. // Adds a version to the result (nullptr safe)
  1431. auto addVersion = [&result](const char* major, const char* minor) {
  1432. cmQtAutoGen::IntegerVersion ver(CharPtrToUInt(major),
  1433. CharPtrToUInt(minor));
  1434. if (ver.Major != 0) {
  1435. result.emplace_back(ver);
  1436. }
  1437. };
  1438. cmMakefile* makefile = target->Target->GetMakefile();
  1439. // Read versions from variables
  1440. for (const std::string& prefix : prefixes) {
  1441. addVersion(makefile->GetDefinition(prefix + "_VERSION_MAJOR"),
  1442. makefile->GetDefinition(prefix + "_VERSION_MINOR"));
  1443. }
  1444. // Read versions from directory properties
  1445. for (const std::string& prefix : prefixes) {
  1446. addVersion(makefile->GetProperty(prefix + "_VERSION_MAJOR"),
  1447. makefile->GetProperty(prefix + "_VERSION_MINOR"));
  1448. }
  1449. return result;
  1450. }
  1451. std::pair<cmQtAutoGen::IntegerVersion, unsigned int>
  1452. cmQtAutoGenInitializer::GetQtVersion(cmGeneratorTarget const* target)
  1453. {
  1454. std::pair<IntegerVersion, unsigned int> res(
  1455. IntegerVersion(),
  1456. CharPtrToUInt(target->GetLinkInterfaceDependentStringProperty(
  1457. "QT_MAJOR_VERSION", "")));
  1458. auto knownQtVersions = GetKnownQtVersions(target);
  1459. if (!knownQtVersions.empty()) {
  1460. if (res.second == 0) {
  1461. // No specific version was requested by the target:
  1462. // Use highest known Qt version.
  1463. res.first = knownQtVersions.at(0);
  1464. } else {
  1465. // Pick a version from the known versions:
  1466. for (auto it : knownQtVersions) {
  1467. if (it.Major == res.second) {
  1468. res.first = it;
  1469. break;
  1470. }
  1471. }
  1472. }
  1473. }
  1474. return res;
  1475. }
  1476. bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
  1477. const std::string& executable,
  1478. bool ignoreMissingTarget) const
  1479. {
  1480. auto print_err = [this, &genVars](std::string const& err) {
  1481. std::string msg = genVars.GenNameUpper;
  1482. msg += " for target ";
  1483. msg += this->Target->GetName();
  1484. msg += ": ";
  1485. msg += err;
  1486. cmSystemTools::Error(msg);
  1487. };
  1488. // Custom executable
  1489. {
  1490. std::string const prop = genVars.GenNameUpper + "_EXECUTABLE";
  1491. std::string const val = this->Target->Target->GetSafeProperty(prop);
  1492. if (!val.empty()) {
  1493. // Evaluate generator expression
  1494. {
  1495. cmListFileBacktrace lfbt =
  1496. this->Target->Target->GetMakefile()->GetBacktrace();
  1497. cmGeneratorExpression ge(lfbt);
  1498. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(val);
  1499. genVars.Executable =
  1500. cge->Evaluate(this->Target->GetLocalGenerator(), "");
  1501. }
  1502. if (genVars.Executable.empty() && !ignoreMissingTarget) {
  1503. print_err(prop + " evaluates to an empty value");
  1504. return false;
  1505. }
  1506. // Create empty compiler features.
  1507. genVars.ExecutableFeatures =
  1508. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  1509. return true;
  1510. }
  1511. }
  1512. // Find executable target
  1513. {
  1514. // Find executable target name
  1515. std::string targetName;
  1516. if (this->QtVersion.Major == 4) {
  1517. targetName = "Qt4::";
  1518. } else if (this->QtVersion.Major == 5) {
  1519. targetName = "Qt5::";
  1520. } else if (this->QtVersion.Major == 6) {
  1521. targetName = "Qt6::";
  1522. }
  1523. targetName += executable;
  1524. // Find target
  1525. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1526. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(targetName);
  1527. if (target != nullptr) {
  1528. genVars.ExecutableTargetName = targetName;
  1529. genVars.ExecutableTarget = target;
  1530. if (target->IsImported()) {
  1531. genVars.Executable = target->ImportedGetLocation("");
  1532. } else {
  1533. genVars.Executable = target->GetLocation("");
  1534. }
  1535. } else {
  1536. if (ignoreMissingTarget) {
  1537. // Create empty compiler features.
  1538. genVars.ExecutableFeatures =
  1539. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  1540. return true;
  1541. }
  1542. std::string err = "Could not find ";
  1543. err += executable;
  1544. err += " executable target ";
  1545. err += targetName;
  1546. print_err(err);
  1547. return false;
  1548. }
  1549. }
  1550. // Get executable features
  1551. {
  1552. std::string err;
  1553. genVars.ExecutableFeatures = this->GlobalInitializer->GetCompilerFeatures(
  1554. executable, genVars.Executable, err);
  1555. if (!genVars.ExecutableFeatures) {
  1556. print_err(err);
  1557. return false;
  1558. }
  1559. }
  1560. return true;
  1561. }