cmQtAutoGenInitializer.cxx 56 KB

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