cmQtAutoGenInitializer.cxx 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290
  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 <limits>
  8. #include <map>
  9. #include <set>
  10. #include <sstream> // for basic_ios, istringstream
  11. #include <string>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/algorithm>
  16. #include <cm/iterator>
  17. #include <cm/memory>
  18. #include <cmext/algorithm>
  19. #include <cmext/string_view>
  20. #include <cm3p/json/value.h>
  21. #include <cm3p/json/writer.h>
  22. #include "cmsys/SystemInformation.hxx"
  23. #include "cmAlgorithms.h"
  24. #include "cmCustomCommand.h"
  25. #include "cmCustomCommandLines.h"
  26. #include "cmEvaluatedTargetProperty.h"
  27. #include "cmGeneratedFileStream.h"
  28. #include "cmGeneratorExpression.h"
  29. #include "cmGeneratorExpressionDAGChecker.h"
  30. #include "cmGeneratorTarget.h"
  31. #include "cmGlobalGenerator.h"
  32. #include "cmLinkItem.h"
  33. #include "cmList.h"
  34. #include "cmListFileCache.h"
  35. #include "cmLocalGenerator.h"
  36. #include "cmMakefile.h"
  37. #include "cmMessageType.h"
  38. #include "cmPolicies.h"
  39. #include "cmQtAutoGen.h"
  40. #include "cmQtAutoGenGlobalInitializer.h"
  41. #include "cmSourceFile.h"
  42. #include "cmSourceFileLocationKind.h"
  43. #include "cmSourceGroup.h"
  44. #include "cmStandardLevelResolver.h"
  45. #include "cmState.h"
  46. #include "cmStateTypes.h"
  47. #include "cmStringAlgorithms.h"
  48. #include "cmSystemTools.h"
  49. #include "cmTarget.h"
  50. #include "cmValue.h"
  51. #include "cmake.h"
  52. namespace {
  53. unsigned int GetParallelCPUCount()
  54. {
  55. static unsigned int count = 0;
  56. // Detect only on the first call
  57. if (count == 0) {
  58. cmsys::SystemInformation info;
  59. info.RunCPUCheck();
  60. count =
  61. cm::clamp(info.GetNumberOfPhysicalCPU(), 1u, cmQtAutoGen::ParallelMax);
  62. }
  63. return count;
  64. }
  65. std::string FileProjectRelativePath(cmMakefile* makefile,
  66. std::string const& fileName)
  67. {
  68. std::string res;
  69. {
  70. std::string pSource = cmSystemTools::RelativePath(
  71. makefile->GetCurrentSourceDirectory(), fileName);
  72. std::string pBinary = cmSystemTools::RelativePath(
  73. makefile->GetCurrentBinaryDirectory(), fileName);
  74. if (pSource.size() < pBinary.size()) {
  75. res = std::move(pSource);
  76. } else if (pBinary.size() < fileName.size()) {
  77. res = std::move(pBinary);
  78. } else {
  79. res = fileName;
  80. }
  81. }
  82. return res;
  83. }
  84. /**
  85. * Tests if targetDepend is a STATIC_LIBRARY and if any of its
  86. * recursive STATIC_LIBRARY dependencies depends on targetOrigin
  87. * (STATIC_LIBRARY cycle).
  88. */
  89. bool StaticLibraryCycle(cmGeneratorTarget const* targetOrigin,
  90. cmGeneratorTarget const* targetDepend,
  91. std::string const& config)
  92. {
  93. bool cycle = false;
  94. if ((targetOrigin->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  95. (targetDepend->GetType() == cmStateEnums::STATIC_LIBRARY)) {
  96. std::set<cmGeneratorTarget const*> knownLibs;
  97. std::deque<cmGeneratorTarget const*> testLibs;
  98. // Insert initial static_library dependency
  99. knownLibs.insert(targetDepend);
  100. testLibs.push_back(targetDepend);
  101. while (!testLibs.empty()) {
  102. cmGeneratorTarget const* testTarget = testLibs.front();
  103. testLibs.pop_front();
  104. // Check if the test target is the origin target (cycle)
  105. if (testTarget == targetOrigin) {
  106. cycle = true;
  107. break;
  108. }
  109. // Collect all static_library dependencies from the test target
  110. cmLinkImplementationLibraries const* libs =
  111. testTarget->GetLinkImplementationLibraries(
  112. config, cmGeneratorTarget::LinkInterfaceFor::Link);
  113. if (libs) {
  114. for (cmLinkItem const& item : libs->Libraries) {
  115. cmGeneratorTarget const* depTarget = item.Target;
  116. if (depTarget &&
  117. (depTarget->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  118. knownLibs.insert(depTarget).second) {
  119. testLibs.push_back(depTarget);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return cycle;
  126. }
  127. /** Sanitizes file search paths. */
  128. class SearchPathSanitizer
  129. {
  130. public:
  131. SearchPathSanitizer(cmMakefile* makefile)
  132. : SourcePath_(makefile->GetCurrentSourceDirectory())
  133. {
  134. }
  135. std::vector<std::string> operator()(
  136. std::vector<std::string> const& paths) const;
  137. private:
  138. std::string SourcePath_;
  139. };
  140. std::vector<std::string> SearchPathSanitizer::operator()(
  141. std::vector<std::string> const& paths) const
  142. {
  143. std::vector<std::string> res;
  144. res.reserve(paths.size());
  145. for (std::string const& srcPath : paths) {
  146. // Collapse relative paths
  147. std::string path =
  148. cmSystemTools::CollapseFullPath(srcPath, this->SourcePath_);
  149. // Remove suffix slashes
  150. while (cmHasSuffix(path, '/')) {
  151. path.pop_back();
  152. }
  153. // Accept only non empty paths
  154. if (!path.empty()) {
  155. res.emplace_back(std::move(path));
  156. }
  157. }
  158. return res;
  159. }
  160. /** @brief Writes a CMake info file. */
  161. class InfoWriter
  162. {
  163. public:
  164. // -- Single value
  165. void Set(std::string const& key, std::string const& value)
  166. {
  167. this->Value_[key] = value;
  168. }
  169. void SetConfig(std::string const& key,
  170. cmQtAutoGenInitializer::ConfigString const& cfgStr);
  171. void SetBool(std::string const& key, bool value)
  172. {
  173. this->Value_[key] = value;
  174. }
  175. void SetUInt(std::string const& key, unsigned int value)
  176. {
  177. this->Value_[key] = value;
  178. }
  179. // -- Array utility
  180. template <typename CONT>
  181. static bool MakeArray(Json::Value& jval, CONT const& container);
  182. template <typename CONT>
  183. static void MakeStringArray(Json::Value& jval, CONT const& container);
  184. // -- Array value
  185. template <typename CONT>
  186. void SetArray(std::string const& key, CONT const& container);
  187. template <typename CONT>
  188. void SetConfigArray(
  189. std::string const& key,
  190. cmQtAutoGenInitializer::ConfigStrings<CONT> const& cfgStr);
  191. // -- Array of arrays
  192. template <typename CONT, typename FUNC>
  193. void SetArrayArray(std::string const& key, CONT const& container, FUNC func);
  194. // -- Save to json file
  195. bool Save(std::string const& filename);
  196. private:
  197. Json::Value Value_;
  198. };
  199. void InfoWriter::SetConfig(std::string const& key,
  200. cmQtAutoGenInitializer::ConfigString const& cfgStr)
  201. {
  202. this->Set(key, cfgStr.Default);
  203. for (auto const& item : cfgStr.Config) {
  204. this->Set(cmStrCat(key, '_', item.first), item.second);
  205. }
  206. }
  207. template <typename CONT>
  208. bool InfoWriter::MakeArray(Json::Value& jval, CONT const& container)
  209. {
  210. jval = Json::arrayValue;
  211. std::size_t const listSize = cm::size(container);
  212. if (listSize == 0) {
  213. return false;
  214. }
  215. jval.resize(static_cast<unsigned int>(listSize));
  216. return true;
  217. }
  218. template <typename CONT>
  219. void InfoWriter::MakeStringArray(Json::Value& jval, CONT const& container)
  220. {
  221. if (MakeArray(jval, container)) {
  222. Json::ArrayIndex ii = 0;
  223. for (std::string const& item : container) {
  224. jval[ii++] = item;
  225. }
  226. }
  227. }
  228. template <typename CONT>
  229. void InfoWriter::SetArray(std::string const& key, CONT const& container)
  230. {
  231. MakeStringArray(this->Value_[key], container);
  232. }
  233. template <typename CONT, typename FUNC>
  234. void InfoWriter::SetArrayArray(std::string const& key, CONT const& container,
  235. FUNC func)
  236. {
  237. Json::Value& jval = this->Value_[key];
  238. if (MakeArray(jval, container)) {
  239. Json::ArrayIndex ii = 0;
  240. for (auto const& citem : container) {
  241. Json::Value& aval = jval[ii++];
  242. aval = Json::arrayValue;
  243. func(aval, citem);
  244. }
  245. }
  246. }
  247. template <typename CONT>
  248. void InfoWriter::SetConfigArray(
  249. std::string const& key,
  250. cmQtAutoGenInitializer::ConfigStrings<CONT> const& cfgStr)
  251. {
  252. this->SetArray(key, cfgStr.Default);
  253. for (auto const& item : cfgStr.Config) {
  254. this->SetArray(cmStrCat(key, '_', item.first), item.second);
  255. }
  256. }
  257. bool InfoWriter::Save(std::string const& filename)
  258. {
  259. cmGeneratedFileStream fileStream;
  260. fileStream.SetCopyIfDifferent(true);
  261. fileStream.Open(filename, false, true);
  262. if (!fileStream) {
  263. return false;
  264. }
  265. Json::StyledStreamWriter jsonWriter;
  266. try {
  267. jsonWriter.write(fileStream, this->Value_);
  268. } catch (...) {
  269. return false;
  270. }
  271. return fileStream.Close();
  272. }
  273. void AddAutogenExecutableToDependencies(
  274. cmQtAutoGenInitializer::GenVarsT const& genVars,
  275. std::vector<std::string>& dependencies)
  276. {
  277. if (genVars.ExecutableTarget != nullptr) {
  278. dependencies.push_back(genVars.ExecutableTarget->Target->GetName());
  279. } else if (!genVars.Executable.empty()) {
  280. dependencies.push_back(genVars.Executable);
  281. }
  282. }
  283. } // End of unnamed namespace
  284. cmQtAutoGenInitializer::cmQtAutoGenInitializer(
  285. cmQtAutoGenGlobalInitializer* globalInitializer,
  286. cmGeneratorTarget* genTarget, IntegerVersion const& qtVersion,
  287. bool mocEnabled, bool uicEnabled, bool rccEnabled, bool globalAutogenTarget,
  288. bool globalAutoRccTarget)
  289. : GlobalInitializer(globalInitializer)
  290. , GenTarget(genTarget)
  291. , GlobalGen(genTarget->GetGlobalGenerator())
  292. , LocalGen(genTarget->GetLocalGenerator())
  293. , Makefile(genTarget->Makefile)
  294. , PathCheckSum(genTarget->Makefile)
  295. , QtVersion(qtVersion)
  296. {
  297. this->AutogenTarget.GlobalTarget = globalAutogenTarget;
  298. this->Moc.Enabled = mocEnabled;
  299. this->Uic.Enabled = uicEnabled;
  300. this->Rcc.Enabled = rccEnabled;
  301. this->Rcc.GlobalTarget = globalAutoRccTarget;
  302. }
  303. bool cmQtAutoGenInitializer::InitCustomTargets()
  304. {
  305. // Configurations
  306. this->MultiConfig = this->GlobalGen->IsMultiConfig();
  307. this->ConfigDefault = this->Makefile->GetDefaultConfiguration();
  308. this->ConfigsList =
  309. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  310. // Verbosity
  311. {
  312. std::string const def =
  313. this->Makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE");
  314. if (!def.empty()) {
  315. unsigned long iVerb = 0;
  316. if (cmStrToULong(def, &iVerb)) {
  317. // Numeric verbosity
  318. this->Verbosity = static_cast<unsigned int>(iVerb);
  319. } else {
  320. // Non numeric verbosity
  321. if (cmIsOn(def)) {
  322. this->Verbosity = 1;
  323. }
  324. }
  325. }
  326. }
  327. // Targets FOLDER
  328. {
  329. cmValue folder =
  330. this->Makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  331. if (!folder) {
  332. folder = this->Makefile->GetState()->GetGlobalProperty(
  333. "AUTOGEN_TARGETS_FOLDER");
  334. }
  335. // Inherit FOLDER property from target (#13688)
  336. if (!folder) {
  337. folder = this->GenTarget->GetProperty("FOLDER");
  338. }
  339. if (folder) {
  340. this->TargetsFolder = *folder;
  341. }
  342. }
  343. // Check status of policy CMP0071 regarding handling of GENERATED files
  344. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0071)) {
  345. case cmPolicies::WARN:
  346. // Ignore GENERATED files but warn
  347. this->CMP0071Warn = true;
  348. CM_FALLTHROUGH;
  349. case cmPolicies::OLD:
  350. // Ignore GENERATED files
  351. break;
  352. case cmPolicies::REQUIRED_IF_USED:
  353. case cmPolicies::REQUIRED_ALWAYS:
  354. case cmPolicies::NEW:
  355. // Process GENERATED files
  356. this->CMP0071Accept = true;
  357. break;
  358. }
  359. // Check status of policy CMP0100 regarding handling of .hh headers
  360. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0100)) {
  361. case cmPolicies::WARN:
  362. // Ignore but .hh files but warn
  363. this->CMP0100Warn = true;
  364. CM_FALLTHROUGH;
  365. case cmPolicies::OLD:
  366. // Ignore .hh files
  367. break;
  368. case cmPolicies::REQUIRED_IF_USED:
  369. case cmPolicies::REQUIRED_ALWAYS:
  370. case cmPolicies::NEW:
  371. // Process .hh file
  372. this->CMP0100Accept = true;
  373. break;
  374. }
  375. // Common directories
  376. std::string relativeBuildDir;
  377. {
  378. // Collapsed current binary directory
  379. std::string const cbd = cmSystemTools::CollapseFullPath(
  380. std::string(), this->Makefile->GetCurrentBinaryDirectory());
  381. // Info directory
  382. this->Dir.Info = cmStrCat(cbd, "/CMakeFiles/", this->GenTarget->GetName(),
  383. "_autogen.dir");
  384. cmSystemTools::ConvertToUnixSlashes(this->Dir.Info);
  385. // Build directory
  386. this->Dir.Build = this->GenTarget->GetSafeProperty("AUTOGEN_BUILD_DIR");
  387. if (this->Dir.Build.empty()) {
  388. this->Dir.Build =
  389. cmStrCat(cbd, '/', this->GenTarget->GetName(), "_autogen");
  390. }
  391. cmSystemTools::ConvertToUnixSlashes(this->Dir.Build);
  392. this->Dir.RelativeBuild =
  393. cmSystemTools::RelativePath(cbd, this->Dir.Build);
  394. // Cleanup build directory
  395. this->AddCleanFile(this->Dir.Build);
  396. // Working directory
  397. this->Dir.Work = cbd;
  398. cmSystemTools::ConvertToUnixSlashes(this->Dir.Work);
  399. // Include directory
  400. this->ConfigFileNamesAndGenex(this->Dir.Include, this->Dir.IncludeGenExp,
  401. cmStrCat(this->Dir.Build, "/include"), "");
  402. }
  403. // Moc, Uic and _autogen target settings
  404. if (this->MocOrUicEnabled()) {
  405. // Init moc specific settings
  406. if (this->Moc.Enabled && !this->InitMoc()) {
  407. return false;
  408. }
  409. // Init uic specific settings
  410. if (this->Uic.Enabled && !this->InitUic()) {
  411. return false;
  412. }
  413. // Autogen target name
  414. this->AutogenTarget.Name =
  415. cmStrCat(this->GenTarget->GetName(), "_autogen");
  416. // Autogen target parallel processing
  417. {
  418. using ParallelType = decltype(this->AutogenTarget.Parallel);
  419. unsigned long propInt = 0;
  420. std::string const& prop =
  421. this->GenTarget->GetSafeProperty("AUTOGEN_PARALLEL");
  422. if (prop.empty() || (prop == "AUTO")) {
  423. // Autodetect number of CPUs
  424. this->AutogenTarget.Parallel = GetParallelCPUCount();
  425. } else if (cmStrToULong(prop, &propInt) && propInt > 0 &&
  426. propInt <= std::numeric_limits<ParallelType>::max()) {
  427. this->AutogenTarget.Parallel = static_cast<ParallelType>(propInt);
  428. } else {
  429. // Warn the project author that AUTOGEN_PARALLEL is not valid.
  430. this->Makefile->IssueMessage(
  431. MessageType::AUTHOR_WARNING,
  432. cmStrCat("AUTOGEN_PARALLEL=\"", prop, "\" for target \"",
  433. this->GenTarget->GetName(),
  434. "\" is not valid. Using AUTOGEN_PARALLEL=1"));
  435. this->AutogenTarget.Parallel = 1;
  436. }
  437. }
  438. // Autogen target info and settings files
  439. {
  440. // Info file
  441. this->AutogenTarget.InfoFile =
  442. cmStrCat(this->Dir.Info, "/AutogenInfo.json");
  443. // Used settings file
  444. this->ConfigFileNames(this->AutogenTarget.SettingsFile,
  445. cmStrCat(this->Dir.Info, "/AutogenUsed"), ".txt");
  446. this->ConfigFileClean(this->AutogenTarget.SettingsFile);
  447. // Parse cache file
  448. this->ConfigFileNames(this->AutogenTarget.ParseCacheFile,
  449. cmStrCat(this->Dir.Info, "/ParseCache"), ".txt");
  450. this->ConfigFileClean(this->AutogenTarget.ParseCacheFile);
  451. }
  452. // Autogen target: Compute user defined dependencies
  453. {
  454. this->AutogenTarget.DependOrigin =
  455. this->GenTarget->GetPropertyAsBool("AUTOGEN_ORIGIN_DEPENDS");
  456. std::string const& deps =
  457. this->GenTarget->GetSafeProperty("AUTOGEN_TARGET_DEPENDS");
  458. if (!deps.empty()) {
  459. for (auto const& depName : cmList{ deps }) {
  460. // Allow target and file dependencies
  461. auto* depTarget = this->Makefile->FindTargetToUse(depName);
  462. if (depTarget) {
  463. this->AutogenTarget.DependTargets.insert(depTarget);
  464. } else {
  465. this->AutogenTarget.DependFiles.insert(depName);
  466. }
  467. }
  468. }
  469. }
  470. if (this->Moc.Enabled) {
  471. // Path prefix
  472. if (cmIsOn(this->GenTarget->GetProperty("AUTOMOC_PATH_PREFIX"))) {
  473. this->Moc.PathPrefix = true;
  474. }
  475. // CMAKE_AUTOMOC_RELAXED_MODE
  476. if (this->Makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE")) {
  477. this->Moc.RelaxedMode = true;
  478. this->Makefile->IssueMessage(
  479. MessageType::AUTHOR_WARNING,
  480. cmStrCat("AUTOMOC: CMAKE_AUTOMOC_RELAXED_MODE is "
  481. "deprecated an will be removed in the future. Consider "
  482. "disabling it and converting the target ",
  483. this->GenTarget->GetName(), " to regular mode."));
  484. }
  485. // Options
  486. cmExpandList(this->GenTarget->GetSafeProperty("AUTOMOC_MOC_OPTIONS"),
  487. this->Moc.Options);
  488. // Filters
  489. cmExpandList(this->GenTarget->GetSafeProperty("AUTOMOC_MACRO_NAMES"),
  490. this->Moc.MacroNames);
  491. this->Moc.MacroNames.erase(cmRemoveDuplicates(this->Moc.MacroNames),
  492. this->Moc.MacroNames.end());
  493. {
  494. cmList const filterList = { this->GenTarget->GetSafeProperty(
  495. "AUTOMOC_DEPEND_FILTERS") };
  496. if ((filterList.size() % 2) != 0) {
  497. cmSystemTools::Error(
  498. cmStrCat("AutoMoc: AUTOMOC_DEPEND_FILTERS predefs size ",
  499. filterList.size(), " is not a multiple of 2."));
  500. return false;
  501. }
  502. this->Moc.DependFilters.reserve(1 + (filterList.size() / 2));
  503. this->Moc.DependFilters.emplace_back(
  504. "Q_PLUGIN_METADATA",
  505. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  506. "[^\\)]*FILE[ \t]*\"([^\"]+)\"");
  507. for (cmList::size_type ii = 0; ii != filterList.size(); ii += 2) {
  508. this->Moc.DependFilters.emplace_back(filterList[ii],
  509. filterList[ii + 1]);
  510. }
  511. }
  512. }
  513. }
  514. // Init rcc specific settings
  515. if (this->Rcc.Enabled && !this->InitRcc()) {
  516. return false;
  517. }
  518. // Add autogen include directory to the origin target INCLUDE_DIRECTORIES
  519. if (this->MocOrUicEnabled() || (this->Rcc.Enabled && this->MultiConfig)) {
  520. auto addBefore = false;
  521. auto const& value =
  522. this->GenTarget->GetProperty("AUTOGEN_USE_SYSTEM_INCLUDE");
  523. if (value.IsSet()) {
  524. if (cmIsOn(value)) {
  525. this->GenTarget->AddSystemIncludeDirectory(this->Dir.IncludeGenExp,
  526. "CXX");
  527. } else {
  528. addBefore = true;
  529. }
  530. } else {
  531. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0151)) {
  532. case cmPolicies::WARN:
  533. case cmPolicies::OLD:
  534. addBefore = true;
  535. break;
  536. case cmPolicies::REQUIRED_IF_USED:
  537. case cmPolicies::REQUIRED_ALWAYS:
  538. case cmPolicies::NEW:
  539. this->GenTarget->AddSystemIncludeDirectory(this->Dir.IncludeGenExp,
  540. "CXX");
  541. break;
  542. }
  543. }
  544. this->GenTarget->AddIncludeDirectory(this->Dir.IncludeGenExp, addBefore);
  545. }
  546. // Scan files
  547. if (!this->InitScanFiles()) {
  548. return false;
  549. }
  550. // Create autogen target
  551. if (this->MocOrUicEnabled() && !this->InitAutogenTarget()) {
  552. return false;
  553. }
  554. // Create rcc targets
  555. if (this->Rcc.Enabled && !this->InitRccTargets()) {
  556. return false;
  557. }
  558. return true;
  559. }
  560. bool cmQtAutoGenInitializer::InitMoc()
  561. {
  562. // Mocs compilation file
  563. if (this->GlobalGen->IsXcode()) {
  564. // XXX(xcode-per-cfg-src): Drop this Xcode-specific code path
  565. // when the Xcode generator supports per-config sources.
  566. this->Moc.CompilationFile.Default =
  567. cmStrCat(this->Dir.Build, "/mocs_compilation.cpp");
  568. this->Moc.CompilationFileGenex = this->Moc.CompilationFile.Default;
  569. } else {
  570. this->ConfigFileNamesAndGenex(
  571. this->Moc.CompilationFile, this->Moc.CompilationFileGenex,
  572. cmStrCat(this->Dir.Build, "/mocs_compilation"_s), ".cpp"_s);
  573. }
  574. // Moc predefs
  575. if (this->GenTarget->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES") &&
  576. (this->QtVersion >= IntegerVersion(5, 8))) {
  577. // Command
  578. cmList::assign(
  579. this->Moc.PredefsCmd,
  580. this->Makefile->GetDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
  581. // Header
  582. if (!this->Moc.PredefsCmd.empty()) {
  583. this->ConfigFileNames(this->Moc.PredefsFile,
  584. cmStrCat(this->Dir.Build, "/moc_predefs"), ".h");
  585. }
  586. }
  587. // Moc includes
  588. {
  589. SearchPathSanitizer const sanitizer(this->Makefile);
  590. auto getDirs =
  591. [this, &sanitizer](std::string const& cfg) -> std::vector<std::string> {
  592. // Get the include dirs for this target, without stripping the implicit
  593. // include dirs off, see issue #13667.
  594. std::vector<std::string> dirs;
  595. bool const appendImplicit = (this->QtVersion.Major >= 5);
  596. this->LocalGen->GetIncludeDirectoriesImplicit(
  597. dirs, this->GenTarget, "CXX", cfg, false, appendImplicit);
  598. return sanitizer(dirs);
  599. };
  600. // Other configuration settings
  601. if (this->MultiConfig) {
  602. for (std::string const& cfg : this->ConfigsList) {
  603. std::vector<std::string> dirs = getDirs(cfg);
  604. if (dirs == this->Moc.Includes.Default) {
  605. continue;
  606. }
  607. this->Moc.Includes.Config[cfg] = std::move(dirs);
  608. }
  609. } else {
  610. // Default configuration include directories
  611. this->Moc.Includes.Default = getDirs(this->ConfigDefault);
  612. }
  613. }
  614. // Moc compile definitions
  615. {
  616. auto getDefs = [this](std::string const& cfg) -> std::set<std::string> {
  617. std::set<std::string> defines;
  618. this->LocalGen->GetTargetDefines(this->GenTarget, cfg, "CXX", defines);
  619. if (this->Moc.PredefsCmd.empty() &&
  620. this->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") ==
  621. "Windows") {
  622. // Add WIN32 definition if we don't have a moc_predefs.h
  623. defines.insert("WIN32");
  624. }
  625. return defines;
  626. };
  627. // Other configuration defines
  628. if (this->MultiConfig) {
  629. for (std::string const& cfg : this->ConfigsList) {
  630. std::set<std::string> defines = getDefs(cfg);
  631. if (defines == this->Moc.Defines.Default) {
  632. continue;
  633. }
  634. this->Moc.Defines.Config[cfg] = std::move(defines);
  635. }
  636. } else {
  637. // Default configuration defines
  638. this->Moc.Defines.Default = getDefs(this->ConfigDefault);
  639. }
  640. }
  641. // Moc executable
  642. {
  643. if (!this->GetQtExecutable(this->Moc, "moc", false)) {
  644. return false;
  645. }
  646. // Let the _autogen target depend on the moc executable
  647. if (this->Moc.ExecutableTarget) {
  648. this->AutogenTarget.DependTargets.insert(
  649. this->Moc.ExecutableTarget->Target);
  650. }
  651. }
  652. return true;
  653. }
  654. bool cmQtAutoGenInitializer::InitUic()
  655. {
  656. // Uic search paths
  657. {
  658. std::string const& usp =
  659. this->GenTarget->GetSafeProperty("AUTOUIC_SEARCH_PATHS");
  660. if (!usp.empty()) {
  661. this->Uic.SearchPaths =
  662. SearchPathSanitizer(this->Makefile)(cmList{ usp });
  663. }
  664. }
  665. // Uic target options
  666. {
  667. auto getOpts = [this](std::string const& cfg) -> std::vector<std::string> {
  668. std::vector<std::string> opts;
  669. this->GenTarget->GetAutoUicOptions(opts, cfg);
  670. return opts;
  671. };
  672. // Default options
  673. this->Uic.Options.Default = getOpts(this->ConfigDefault);
  674. // Configuration specific options
  675. if (this->MultiConfig) {
  676. for (std::string const& cfg : this->ConfigsList) {
  677. std::vector<std::string> options = getOpts(cfg);
  678. if (options == this->Uic.Options.Default) {
  679. continue;
  680. }
  681. this->Uic.Options.Config[cfg] = std::move(options);
  682. }
  683. }
  684. }
  685. // Uic executable
  686. {
  687. if (!this->GetQtExecutable(this->Uic, "uic", true)) {
  688. return false;
  689. }
  690. // Let the _autogen target depend on the uic executable
  691. if (this->Uic.ExecutableTarget) {
  692. this->AutogenTarget.DependTargets.insert(
  693. this->Uic.ExecutableTarget->Target);
  694. }
  695. }
  696. return true;
  697. }
  698. bool cmQtAutoGenInitializer::InitRcc()
  699. {
  700. // Rcc executable
  701. {
  702. if (!this->GetQtExecutable(this->Rcc, "rcc", false)) {
  703. return false;
  704. }
  705. // Evaluate test output on demand
  706. CompilerFeatures& features = *this->Rcc.ExecutableFeatures;
  707. if (!features.Evaluated) {
  708. // Look for list options
  709. if (this->QtVersion.Major == 5 || this->QtVersion.Major == 6) {
  710. if (features.HelpOutput.find("--list") != std::string::npos) {
  711. features.ListOptions.emplace_back("--list");
  712. } else if (features.HelpOutput.find("-list") != std::string::npos) {
  713. features.ListOptions.emplace_back("-list");
  714. }
  715. }
  716. // Evaluation finished
  717. features.Evaluated = true;
  718. }
  719. }
  720. return true;
  721. }
  722. bool cmQtAutoGenInitializer::InitScanFiles()
  723. {
  724. cmake const* cm = this->Makefile->GetCMakeInstance();
  725. auto const& kw = this->GlobalInitializer->kw();
  726. auto makeMUFile = [this, &kw](cmSourceFile* sf, std::string const& fullPath,
  727. std::vector<size_t> const& configs,
  728. bool muIt) -> MUFileHandle {
  729. MUFileHandle muf = cm::make_unique<MUFile>();
  730. muf->FullPath = fullPath;
  731. muf->SF = sf;
  732. if (!configs.empty() && configs.size() != this->ConfigsList.size()) {
  733. muf->Configs = configs;
  734. }
  735. muf->Generated = sf->GetIsGenerated();
  736. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  737. muf->SkipMoc = this->Moc.Enabled &&
  738. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOMOC));
  739. muf->SkipUic = this->Uic.Enabled &&
  740. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  741. if (muIt) {
  742. muf->MocIt = this->Moc.Enabled && !muf->SkipMoc;
  743. muf->UicIt = this->Uic.Enabled && !muf->SkipUic;
  744. }
  745. return muf;
  746. };
  747. auto addMUHeader = [this](MUFileHandle&& muf, cm::string_view extension) {
  748. cmSourceFile* sf = muf->SF;
  749. const bool muIt = (muf->MocIt || muf->UicIt);
  750. if (this->CMP0100Accept || (extension != "hh")) {
  751. // Accept
  752. if (muIt && muf->Generated) {
  753. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  754. }
  755. this->AutogenTarget.Headers.emplace(sf, std::move(muf));
  756. } else if (muIt && this->CMP0100Warn) {
  757. // Store file for warning message
  758. this->AutogenTarget.CMP0100HeadersWarn.push_back(sf);
  759. }
  760. };
  761. auto addMUSource = [this](MUFileHandle&& muf) {
  762. if ((muf->MocIt || muf->UicIt) && muf->Generated) {
  763. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  764. }
  765. this->AutogenTarget.Sources.emplace(muf->SF, std::move(muf));
  766. };
  767. // Scan through target files
  768. {
  769. // Scan through target files
  770. for (cmGeneratorTarget::AllConfigSource const& acs :
  771. this->GenTarget->GetAllConfigSources()) {
  772. std::string const& fullPath = acs.Source->GetFullPath();
  773. std::string const& extLower =
  774. cmSystemTools::LowerCase(acs.Source->GetExtension());
  775. // Register files that will be scanned by moc or uic
  776. if (this->MocOrUicEnabled()) {
  777. if (cm->IsAHeaderExtension(extLower)) {
  778. addMUHeader(makeMUFile(acs.Source, fullPath, acs.Configs, true),
  779. extLower);
  780. } else if (cm->IsACLikeSourceExtension(extLower)) {
  781. addMUSource(makeMUFile(acs.Source, fullPath, acs.Configs, true));
  782. }
  783. }
  784. // Register rcc enabled files
  785. if (this->Rcc.Enabled) {
  786. if ((extLower == kw.qrc) &&
  787. !acs.Source->GetPropertyAsBool(kw.SKIP_AUTOGEN) &&
  788. !acs.Source->GetPropertyAsBool(kw.SKIP_AUTORCC)) {
  789. // Register qrc file
  790. Qrc qrc;
  791. qrc.QrcFile = fullPath;
  792. qrc.QrcName =
  793. cmSystemTools::GetFilenameWithoutLastExtension(qrc.QrcFile);
  794. qrc.Generated = acs.Source->GetIsGenerated();
  795. // RCC options
  796. {
  797. std::string const& opts =
  798. acs.Source->GetSafeProperty(kw.AUTORCC_OPTIONS);
  799. if (!opts.empty()) {
  800. cmExpandList(opts, qrc.Options);
  801. }
  802. }
  803. this->Rcc.Qrcs.push_back(std::move(qrc));
  804. }
  805. }
  806. }
  807. }
  808. // cmGeneratorTarget::GetAllConfigSources computes the target's
  809. // sources meta data cache. Clear it so that OBJECT library targets that
  810. // are AUTOGEN initialized after this target get their added
  811. // mocs_compilation.cpp source acknowledged by this target.
  812. this->GenTarget->ClearSourcesCache();
  813. // For source files find additional headers and private headers
  814. if (this->MocOrUicEnabled()) {
  815. // Header search suffixes and extensions
  816. static std::initializer_list<cm::string_view> const suffixes{ "", "_p" };
  817. auto const& exts = cm->GetHeaderExtensions();
  818. // Scan through sources
  819. for (auto const& pair : this->AutogenTarget.Sources) {
  820. MUFile const& muf = *pair.second;
  821. if (muf.MocIt || muf.UicIt) {
  822. // Search for the default header file and a private header
  823. std::string const& srcFullPath = muf.SF->ResolveFullPath();
  824. std::string const basePath = cmStrCat(
  825. cmQtAutoGen::SubDirPrefix(srcFullPath),
  826. cmSystemTools::GetFilenameWithoutLastExtension(srcFullPath));
  827. for (auto const& suffix : suffixes) {
  828. std::string const suffixedPath = cmStrCat(basePath, suffix);
  829. for (auto const& ext : exts) {
  830. std::string const fullPath = cmStrCat(suffixedPath, '.', ext);
  831. auto constexpr locationKind = cmSourceFileLocationKind::Known;
  832. cmSourceFile* sf =
  833. this->Makefile->GetSource(fullPath, locationKind);
  834. if (sf) {
  835. // Check if we know about this header already
  836. if (cm::contains(this->AutogenTarget.Headers, sf)) {
  837. continue;
  838. }
  839. // We only accept not-GENERATED files that do exist.
  840. if (!sf->GetIsGenerated() &&
  841. !cmSystemTools::FileExists(fullPath)) {
  842. continue;
  843. }
  844. } else if (cmSystemTools::FileExists(fullPath)) {
  845. // Create a new source file for the existing file
  846. sf = this->Makefile->CreateSource(fullPath, false, locationKind);
  847. }
  848. if (sf) {
  849. auto eMuf = makeMUFile(sf, fullPath, muf.Configs, true);
  850. // Only process moc/uic when the parent is processed as well
  851. if (!muf.MocIt) {
  852. eMuf->MocIt = false;
  853. }
  854. if (!muf.UicIt) {
  855. eMuf->UicIt = false;
  856. }
  857. addMUHeader(std::move(eMuf), ext);
  858. }
  859. }
  860. }
  861. }
  862. }
  863. }
  864. // Scan through all source files in the makefile to extract moc and uic
  865. // parameters. Historically we support non target source file parameters.
  866. // The reason is that their file names might be discovered from source files
  867. // at generation time.
  868. if (this->MocOrUicEnabled()) {
  869. for (const auto& sf : this->Makefile->GetSourceFiles()) {
  870. // sf->GetExtension() is only valid after sf->ResolveFullPath() ...
  871. // Since we're iterating over source files that might be not in the
  872. // target we need to check for path errors (not existing files).
  873. std::string pathError;
  874. std::string const& fullPath = sf->ResolveFullPath(&pathError);
  875. if (!pathError.empty() || fullPath.empty()) {
  876. continue;
  877. }
  878. std::string const& extLower =
  879. cmSystemTools::LowerCase(sf->GetExtension());
  880. if (cm->IsAHeaderExtension(extLower)) {
  881. if (!cm::contains(this->AutogenTarget.Headers, sf.get())) {
  882. auto muf = makeMUFile(sf.get(), fullPath, {}, false);
  883. if (muf->SkipMoc || muf->SkipUic) {
  884. addMUHeader(std::move(muf), extLower);
  885. }
  886. }
  887. } else if (cm->IsACLikeSourceExtension(extLower)) {
  888. if (!cm::contains(this->AutogenTarget.Sources, sf.get())) {
  889. auto muf = makeMUFile(sf.get(), fullPath, {}, false);
  890. if (muf->SkipMoc || muf->SkipUic) {
  891. addMUSource(std::move(muf));
  892. }
  893. }
  894. } else if (this->Uic.Enabled && (extLower == kw.ui)) {
  895. // .ui file
  896. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  897. bool const skipUic =
  898. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  899. if (!skipUic) {
  900. // Check if the .ui file has uic options
  901. std::string const uicOpts = sf->GetSafeProperty(kw.AUTOUIC_OPTIONS);
  902. if (uicOpts.empty()) {
  903. this->Uic.UiFilesNoOptions.emplace_back(fullPath);
  904. } else {
  905. this->Uic.UiFilesWithOptions.emplace_back(
  906. fullPath, std::move(cmList{ uicOpts }.data()));
  907. }
  908. auto uiHeaderRelativePath = cmSystemTools::RelativePath(
  909. this->LocalGen->GetCurrentSourceDirectory(),
  910. cmSystemTools::GetFilenamePath(fullPath));
  911. // Avoid creating a path containing adjacent slashes
  912. if (!uiHeaderRelativePath.empty() &&
  913. uiHeaderRelativePath.back() != '/') {
  914. uiHeaderRelativePath += '/';
  915. }
  916. auto uiHeaderFilePath = cmStrCat(
  917. '/', uiHeaderRelativePath, "ui_"_s,
  918. cmSystemTools::GetFilenameWithoutLastExtension(fullPath), ".h"_s);
  919. ConfigString uiHeader;
  920. std::string uiHeaderGenex;
  921. this->ConfigFileNamesAndGenex(
  922. uiHeader, uiHeaderGenex, cmStrCat(this->Dir.Build, "/include"_s),
  923. uiHeaderFilePath);
  924. this->Uic.UiHeaders.emplace_back(uiHeader, uiHeaderGenex);
  925. } else {
  926. // Register skipped .ui file
  927. this->Uic.SkipUi.insert(fullPath);
  928. }
  929. }
  930. }
  931. }
  932. // Process GENERATED sources and headers
  933. if (this->MocOrUicEnabled() && !this->AutogenTarget.FilesGenerated.empty()) {
  934. if (this->CMP0071Accept) {
  935. // Let the autogen target depend on the GENERATED files
  936. if (this->MultiConfig &&
  937. this->Makefile->GetSafeDefinition("CMAKE_CROSS_CONFIGS").empty()) {
  938. for (MUFile const* muf : this->AutogenTarget.FilesGenerated) {
  939. if (muf->Configs.empty()) {
  940. this->AutogenTarget.DependFiles.insert(muf->FullPath);
  941. } else {
  942. for (size_t ci : muf->Configs) {
  943. std::string const& config = this->ConfigsList[ci];
  944. std::string const& pathWithConfig =
  945. cmStrCat("$<$<CONFIG:", config, ">:", muf->FullPath, '>');
  946. this->AutogenTarget.DependFiles.insert(pathWithConfig);
  947. }
  948. }
  949. }
  950. } else {
  951. for (MUFile const* muf : this->AutogenTarget.FilesGenerated) {
  952. this->AutogenTarget.DependFiles.insert(muf->FullPath);
  953. }
  954. }
  955. } else if (this->CMP0071Warn) {
  956. cm::string_view property;
  957. if (this->Moc.Enabled && this->Uic.Enabled) {
  958. property = "SKIP_AUTOGEN";
  959. } else if (this->Moc.Enabled) {
  960. property = "SKIP_AUTOMOC";
  961. } else if (this->Uic.Enabled) {
  962. property = "SKIP_AUTOUIC";
  963. }
  964. std::string files;
  965. for (MUFile const* muf : this->AutogenTarget.FilesGenerated) {
  966. files += cmStrCat(" ", Quoted(muf->FullPath), '\n');
  967. }
  968. this->Makefile->IssueMessage(
  969. MessageType::AUTHOR_WARNING,
  970. cmStrCat(
  971. cmPolicies::GetPolicyWarning(cmPolicies::CMP0071), '\n',
  972. "For compatibility, CMake is excluding the GENERATED source "
  973. "file(s):\n",
  974. files, "from processing by ",
  975. cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false),
  976. ". If any of the files should be processed, set CMP0071 to NEW. "
  977. "If any of the files should not be processed, "
  978. "explicitly exclude them by setting the source file property ",
  979. property, ":\n set_property(SOURCE file.h PROPERTY ", property,
  980. " ON)\n"));
  981. }
  982. }
  983. // Generate CMP0100 warning
  984. if (this->MocOrUicEnabled() &&
  985. !this->AutogenTarget.CMP0100HeadersWarn.empty()) {
  986. cm::string_view property;
  987. if (this->Moc.Enabled && this->Uic.Enabled) {
  988. property = "SKIP_AUTOGEN";
  989. } else if (this->Moc.Enabled) {
  990. property = "SKIP_AUTOMOC";
  991. } else if (this->Uic.Enabled) {
  992. property = "SKIP_AUTOUIC";
  993. }
  994. std::string files;
  995. for (cmSourceFile const* sf : this->AutogenTarget.CMP0100HeadersWarn) {
  996. files += cmStrCat(" ", Quoted(sf->GetFullPath()), '\n');
  997. }
  998. this->Makefile->IssueMessage(
  999. MessageType::AUTHOR_WARNING,
  1000. cmStrCat(
  1001. cmPolicies::GetPolicyWarning(cmPolicies::CMP0100), '\n',
  1002. "For compatibility, CMake is excluding the header file(s):\n", files,
  1003. "from processing by ",
  1004. cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false),
  1005. ". If any of the files should be processed, set CMP0100 to NEW. "
  1006. "If any of the files should not be processed, "
  1007. "explicitly exclude them by setting the source file property ",
  1008. property, ":\n set_property(SOURCE file.hh PROPERTY ", property,
  1009. " ON)\n"));
  1010. }
  1011. // Process qrc files
  1012. if (!this->Rcc.Qrcs.empty()) {
  1013. const bool modernQt = (this->QtVersion.Major >= 5);
  1014. // Target rcc options
  1015. cmList const optionsTarget{ this->GenTarget->GetSafeProperty(
  1016. kw.AUTORCC_OPTIONS) };
  1017. // Check if file name is unique
  1018. for (Qrc& qrc : this->Rcc.Qrcs) {
  1019. qrc.Unique = true;
  1020. for (Qrc const& qrc2 : this->Rcc.Qrcs) {
  1021. if ((&qrc != &qrc2) && (qrc.QrcName == qrc2.QrcName)) {
  1022. qrc.Unique = false;
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. // Path checksum and file names
  1028. for (Qrc& qrc : this->Rcc.Qrcs) {
  1029. // Path checksum
  1030. qrc.QrcPathChecksum = this->PathCheckSum.getPart(qrc.QrcFile);
  1031. // Output file name
  1032. qrc.OutputFile = cmStrCat(this->Dir.Build, '/', qrc.QrcPathChecksum,
  1033. "/qrc_", qrc.QrcName, ".cpp");
  1034. std::string const base = cmStrCat(this->Dir.Info, "/AutoRcc_",
  1035. qrc.QrcName, '_', qrc.QrcPathChecksum);
  1036. qrc.LockFile = cmStrCat(base, "_Lock.lock");
  1037. qrc.InfoFile = cmStrCat(base, "_Info.json");
  1038. this->ConfigFileNames(qrc.SettingsFile, cmStrCat(base, "_Used"), ".txt");
  1039. }
  1040. // rcc options
  1041. for (Qrc& qrc : this->Rcc.Qrcs) {
  1042. // Target options
  1043. std::vector<std::string> opts = optionsTarget;
  1044. // Merge computed "-name XYZ" option
  1045. {
  1046. std::string name = qrc.QrcName;
  1047. // Replace '-' with '_'. The former is not valid for symbol names.
  1048. std::replace(name.begin(), name.end(), '-', '_');
  1049. if (!qrc.Unique) {
  1050. name += cmStrCat('_', qrc.QrcPathChecksum);
  1051. }
  1052. std::vector<std::string> nameOpts;
  1053. nameOpts.emplace_back("-name");
  1054. nameOpts.emplace_back(std::move(name));
  1055. RccMergeOptions(opts, nameOpts, modernQt);
  1056. }
  1057. // Merge file option
  1058. RccMergeOptions(opts, qrc.Options, modernQt);
  1059. qrc.Options = std::move(opts);
  1060. }
  1061. // rcc resources
  1062. for (Qrc& qrc : this->Rcc.Qrcs) {
  1063. if (!qrc.Generated) {
  1064. std::string error;
  1065. RccLister const lister(this->Rcc.Executable,
  1066. this->Rcc.ExecutableFeatures->ListOptions);
  1067. if (!lister.list(qrc.QrcFile, qrc.Resources, error)) {
  1068. cmSystemTools::Error(error);
  1069. return false;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. return true;
  1075. }
  1076. bool cmQtAutoGenInitializer::InitAutogenTarget()
  1077. {
  1078. // Register info file as generated by CMake
  1079. this->Makefile->AddCMakeOutputFile(this->AutogenTarget.InfoFile);
  1080. // Determine whether to use a depfile for the AUTOGEN target.
  1081. bool const useDepfile = [this]() -> bool {
  1082. auto const& gen = this->GlobalGen->GetName();
  1083. return this->QtVersion >= IntegerVersion(5, 15) &&
  1084. (gen.find("Ninja") != std::string::npos ||
  1085. gen.find("Make") != std::string::npos);
  1086. }();
  1087. // Files provided by the autogen target
  1088. std::vector<std::string> autogenByproducts;
  1089. std::vector<std::string> timestampByproducts;
  1090. if (this->Moc.Enabled) {
  1091. this->AddGeneratedSource(this->Moc.CompilationFile, this->Moc, true);
  1092. if (useDepfile) {
  1093. if (this->MultiConfig &&
  1094. !this->Makefile->GetSafeDefinition("CMAKE_CROSS_CONFIGS").empty() &&
  1095. this->GlobalGen->GetName().find("Ninja") != std::string::npos) {
  1096. // Make all mocs_compilation_<CONFIG>.cpp files byproducts of the
  1097. // ${target}_autogen/timestamp custom command.
  1098. // We cannot just use Moc.CompilationFileGenex here, because that
  1099. // custom command runs cmake_autogen for each configuration.
  1100. for (const auto& p : this->Moc.CompilationFile.Config) {
  1101. timestampByproducts.push_back(p.second);
  1102. }
  1103. } else {
  1104. timestampByproducts.push_back(this->Moc.CompilationFileGenex);
  1105. }
  1106. } else {
  1107. autogenByproducts.push_back(this->Moc.CompilationFileGenex);
  1108. }
  1109. }
  1110. if (this->Uic.Enabled) {
  1111. for (const auto& file : this->Uic.UiHeaders) {
  1112. this->AddGeneratedSource(file.first, this->Uic);
  1113. autogenByproducts.push_back(file.second);
  1114. }
  1115. }
  1116. // Compose target comment
  1117. std::string autogenComment;
  1118. {
  1119. std::string tools;
  1120. if (this->Moc.Enabled) {
  1121. tools += "MOC";
  1122. }
  1123. if (this->Uic.Enabled) {
  1124. if (!tools.empty()) {
  1125. tools += " and ";
  1126. }
  1127. tools += "UIC";
  1128. }
  1129. autogenComment = cmStrCat("Automatic ", tools, " for target ",
  1130. this->GenTarget->GetName());
  1131. }
  1132. // Compose command lines
  1133. // FIXME: Take advantage of our per-config mocs_compilation_$<CONFIG>.cpp
  1134. // instead of fiddling with the include directories
  1135. std::vector<std::string> configs;
  1136. this->GlobalGen->GetQtAutoGenConfigs(configs);
  1137. bool constexpr stdPipesUTF8 = true;
  1138. cmCustomCommandLines commandLines;
  1139. if (this->Makefile->GetSafeDefinition("CMAKE_CROSS_CONFIGS").empty()) {
  1140. std::string autogenInfoFileConfig;
  1141. if (this->MultiConfig) {
  1142. autogenInfoFileConfig = "$<CONFIG>";
  1143. } else {
  1144. autogenInfoFileConfig = configs[0];
  1145. }
  1146. commandLines.push_back(cmMakeCommandLine(
  1147. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_autogen",
  1148. this->AutogenTarget.InfoFile, autogenInfoFileConfig }));
  1149. } else {
  1150. for (auto const& config : configs) {
  1151. commandLines.push_back(cmMakeCommandLine(
  1152. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_autogen",
  1153. this->AutogenTarget.InfoFile, config }));
  1154. }
  1155. }
  1156. // Use PRE_BUILD on demand
  1157. bool usePRE_BUILD = false;
  1158. if (this->GlobalGen->GetName().find("Visual Studio") != std::string::npos) {
  1159. // Under VS use a PRE_BUILD event instead of a separate target to
  1160. // reduce the number of targets loaded into the IDE.
  1161. // This also works around a VS 11 bug that may skip updating the target:
  1162. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  1163. usePRE_BUILD = true;
  1164. }
  1165. // Disable PRE_BUILD in some cases
  1166. if (usePRE_BUILD) {
  1167. // Cannot use PRE_BUILD with file depends
  1168. if (!this->AutogenTarget.DependFiles.empty()) {
  1169. usePRE_BUILD = false;
  1170. }
  1171. // Cannot use PRE_BUILD when a global autogen target is in place
  1172. if (this->AutogenTarget.GlobalTarget) {
  1173. usePRE_BUILD = false;
  1174. }
  1175. }
  1176. // Create the autogen target/command
  1177. if (usePRE_BUILD) {
  1178. // Add additional autogen target dependencies to origin target
  1179. for (cmTarget const* depTarget : this->AutogenTarget.DependTargets) {
  1180. this->GenTarget->Target->AddUtility(depTarget->GetName(), false,
  1181. this->Makefile);
  1182. }
  1183. if (!this->Uic.UiFilesNoOptions.empty() ||
  1184. !this->Uic.UiFilesWithOptions.empty()) {
  1185. // Add a generated timestamp file
  1186. ConfigString timestampFile;
  1187. std::string timestampFileGenex;
  1188. ConfigFileNamesAndGenex(timestampFile, timestampFileGenex,
  1189. cmStrCat(this->Dir.Build, "/autouic"_s),
  1190. ".stamp"_s);
  1191. this->AddGeneratedSource(timestampFile, this->Uic);
  1192. // Add a step in the pre-build command to touch the timestamp file
  1193. commandLines.push_back(
  1194. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E", "touch",
  1195. timestampFileGenex }));
  1196. // UIC needs to be re-run if any of the known UI files change or the
  1197. // executable itself has been updated
  1198. auto uicDependencies = this->Uic.UiFilesNoOptions;
  1199. for (auto const& uiFile : this->Uic.UiFilesWithOptions) {
  1200. uicDependencies.push_back(uiFile.first);
  1201. }
  1202. AddAutogenExecutableToDependencies(this->Uic, uicDependencies);
  1203. // Add a rule file to cause the target to build if a dependency has
  1204. // changed, which will trigger the pre-build command to run autogen
  1205. auto cc = cm::make_unique<cmCustomCommand>();
  1206. cc->SetOutputs(timestampFileGenex);
  1207. cc->SetDepends(uicDependencies);
  1208. cc->SetComment("");
  1209. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1210. cc->SetEscapeOldStyle(false);
  1211. cc->SetStdPipesUTF8(stdPipesUTF8);
  1212. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1213. }
  1214. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  1215. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  1216. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  1217. //
  1218. // PRE_BUILD does not support file dependencies!
  1219. cmCustomCommand cc;
  1220. cc.SetByproducts(autogenByproducts);
  1221. cc.SetCommandLines(commandLines);
  1222. cc.SetComment(autogenComment.c_str());
  1223. cc.SetBacktrace(this->Makefile->GetBacktrace());
  1224. cc.SetWorkingDirectory(this->Dir.Work.c_str());
  1225. cc.SetStdPipesUTF8(stdPipesUTF8);
  1226. cc.SetEscapeOldStyle(false);
  1227. cc.SetEscapeAllowMakeVars(true);
  1228. this->GenTarget->Target->AddPreBuildCommand(std::move(cc));
  1229. } else {
  1230. // Add link library target dependencies to the autogen target
  1231. // dependencies
  1232. if (this->AutogenTarget.DependOrigin) {
  1233. // add_dependencies/addUtility do not support generator expressions.
  1234. // We depend only on the libraries found in all configs therefore.
  1235. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  1236. for (std::string const& config : this->ConfigsList) {
  1237. cmLinkImplementationLibraries const* libs =
  1238. this->GenTarget->GetLinkImplementationLibraries(
  1239. config, cmGeneratorTarget::LinkInterfaceFor::Link);
  1240. if (libs) {
  1241. for (cmLinkItem const& item : libs->Libraries) {
  1242. cmGeneratorTarget const* libTarget = item.Target;
  1243. if (libTarget &&
  1244. !StaticLibraryCycle(this->GenTarget, libTarget, config)) {
  1245. // Increment target config count
  1246. commonTargets[libTarget]++;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. for (auto const& item : commonTargets) {
  1252. if (item.second == this->ConfigsList.size()) {
  1253. this->AutogenTarget.DependTargets.insert(item.first->Target);
  1254. }
  1255. }
  1256. }
  1257. std::vector<std::string> dependencies(
  1258. this->AutogenTarget.DependFiles.begin(),
  1259. this->AutogenTarget.DependFiles.end());
  1260. if (useDepfile) {
  1261. // Create a custom command that generates a timestamp file and
  1262. // has a depfile assigned. The depfile is created by JobDepFilesMergeT.
  1263. //
  1264. // Also create an additional '_autogen_timestamp_deps' that the custom
  1265. // command will depend on. It will have no sources or commands to
  1266. // execute, but it will have dependencies that would originally be
  1267. // assigned to the pre-Qt 5.15 'autogen' target. These dependencies will
  1268. // serve as a list of order-only dependencies for the custom command,
  1269. // without forcing the custom command to re-execute.
  1270. //
  1271. // The dependency tree would then look like
  1272. // '_autogen_timestamp_deps (order-only)' <- '/timestamp' file <-
  1273. // '_autogen' target.
  1274. const auto timestampTargetName =
  1275. cmStrCat(this->GenTarget->GetName(), "_autogen_timestamp_deps");
  1276. // Add additional autogen target dependencies to
  1277. // '_autogen_timestamp_deps'.
  1278. for (const cmTarget* t : this->AutogenTarget.DependTargets) {
  1279. std::string depname = t->GetName();
  1280. if (t->IsImported()) {
  1281. auto const ttype = t->GetType();
  1282. if (ttype == cmStateEnums::TargetType::STATIC_LIBRARY ||
  1283. ttype == cmStateEnums::TargetType::SHARED_LIBRARY ||
  1284. ttype == cmStateEnums::TargetType::UNKNOWN_LIBRARY) {
  1285. depname = cmStrCat("$<TARGET_LINKER_FILE:", t->GetName(), ">");
  1286. }
  1287. }
  1288. dependencies.emplace_back(std::move(depname));
  1289. }
  1290. auto cc = cm::make_unique<cmCustomCommand>();
  1291. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1292. cc->SetDepends(dependencies);
  1293. cc->SetEscapeOldStyle(false);
  1294. cmTarget* timestampTarget = this->LocalGen->AddUtilityCommand(
  1295. timestampTargetName, true, std::move(cc));
  1296. this->LocalGen->AddGeneratorTarget(
  1297. cm::make_unique<cmGeneratorTarget>(timestampTarget, this->LocalGen));
  1298. // Set FOLDER property on the timestamp target, so it appears in the
  1299. // appropriate folder in an IDE or in the file api.
  1300. if (!this->TargetsFolder.empty()) {
  1301. timestampTarget->SetProperty("FOLDER", this->TargetsFolder);
  1302. }
  1303. // Make '/timestamp' file depend on '_autogen_timestamp_deps' and on the
  1304. // moc and uic executables (whichever are enabled).
  1305. dependencies.clear();
  1306. dependencies.push_back(timestampTargetName);
  1307. AddAutogenExecutableToDependencies(this->Moc, dependencies);
  1308. AddAutogenExecutableToDependencies(this->Uic, dependencies);
  1309. // Create the custom command that outputs the timestamp file.
  1310. const char timestampFileName[] = "timestamp";
  1311. const std::string outputFile =
  1312. cmStrCat(this->Dir.Build, "/", timestampFileName);
  1313. this->AutogenTarget.DepFile = cmStrCat(this->Dir.Build, "/deps");
  1314. this->AutogenTarget.DepFileRuleName =
  1315. cmStrCat(this->Dir.RelativeBuild, "/", timestampFileName);
  1316. commandLines.push_back(cmMakeCommandLine(
  1317. { cmSystemTools::GetCMakeCommand(), "-E", "touch", outputFile }));
  1318. this->AddGeneratedSource(outputFile, this->Moc);
  1319. cc = cm::make_unique<cmCustomCommand>();
  1320. cc->SetOutputs(outputFile);
  1321. cc->SetByproducts(timestampByproducts);
  1322. cc->SetDepends(dependencies);
  1323. cc->SetCommandLines(commandLines);
  1324. cc->SetComment(autogenComment.c_str());
  1325. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1326. cc->SetEscapeOldStyle(false);
  1327. cc->SetDepfile(this->AutogenTarget.DepFile);
  1328. cc->SetStdPipesUTF8(stdPipesUTF8);
  1329. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1330. // Alter variables for the autogen target which now merely wraps the
  1331. // custom command
  1332. dependencies.clear();
  1333. dependencies.push_back(outputFile);
  1334. commandLines.clear();
  1335. autogenComment.clear();
  1336. }
  1337. // Create autogen target
  1338. auto cc = cm::make_unique<cmCustomCommand>();
  1339. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1340. cc->SetByproducts(autogenByproducts);
  1341. cc->SetDepends(dependencies);
  1342. cc->SetCommandLines(commandLines);
  1343. cc->SetEscapeOldStyle(false);
  1344. cc->SetComment(autogenComment.c_str());
  1345. cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand(
  1346. this->AutogenTarget.Name, true, std::move(cc));
  1347. // Create autogen generator target
  1348. this->LocalGen->AddGeneratorTarget(
  1349. cm::make_unique<cmGeneratorTarget>(autogenTarget, this->LocalGen));
  1350. // Forward origin utilities to autogen target
  1351. if (this->AutogenTarget.DependOrigin) {
  1352. for (BT<std::pair<std::string, bool>> const& depName :
  1353. this->GenTarget->GetUtilities()) {
  1354. autogenTarget->AddUtility(depName.Value.first, false, this->Makefile);
  1355. }
  1356. }
  1357. if (!useDepfile) {
  1358. // Add additional autogen target dependencies to autogen target
  1359. for (cmTarget const* depTarget : this->AutogenTarget.DependTargets) {
  1360. autogenTarget->AddUtility(depTarget->GetName(), false, this->Makefile);
  1361. }
  1362. }
  1363. // Set FOLDER property in autogen target
  1364. if (!this->TargetsFolder.empty()) {
  1365. autogenTarget->SetProperty("FOLDER", this->TargetsFolder);
  1366. }
  1367. // Add autogen target to the origin target dependencies
  1368. this->GenTarget->Target->AddUtility(this->AutogenTarget.Name, false,
  1369. this->Makefile);
  1370. // Add autogen target to the global autogen target dependencies
  1371. if (this->AutogenTarget.GlobalTarget) {
  1372. this->GlobalInitializer->AddToGlobalAutoGen(this->LocalGen,
  1373. this->AutogenTarget.Name);
  1374. }
  1375. }
  1376. return true;
  1377. }
  1378. bool cmQtAutoGenInitializer::InitRccTargets()
  1379. {
  1380. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1381. // Register info file as generated by CMake
  1382. this->Makefile->AddCMakeOutputFile(qrc.InfoFile);
  1383. // Register file at target
  1384. {
  1385. cmSourceFile* sf = this->AddGeneratedSource(qrc.OutputFile, this->Rcc);
  1386. sf->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "On");
  1387. }
  1388. std::vector<std::string> ccOutput;
  1389. ccOutput.push_back(qrc.OutputFile);
  1390. std::vector<std::string> ccDepends;
  1391. // Add the .qrc and info file to the custom command dependencies
  1392. ccDepends.push_back(qrc.QrcFile);
  1393. ccDepends.push_back(qrc.InfoFile);
  1394. cmCustomCommandLines commandLines;
  1395. if (this->MultiConfig) {
  1396. // Build for all configurations
  1397. for (std::string const& config : this->ConfigsList) {
  1398. commandLines.push_back(
  1399. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1400. "cmake_autorcc", qrc.InfoFile, config }));
  1401. }
  1402. } else {
  1403. commandLines.push_back(
  1404. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1405. "cmake_autorcc", qrc.InfoFile, "$<CONFIG>" }));
  1406. }
  1407. std::string const ccComment =
  1408. cmStrCat("Automatic RCC for ",
  1409. FileProjectRelativePath(this->Makefile, qrc.QrcFile));
  1410. auto cc = cm::make_unique<cmCustomCommand>();
  1411. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1412. cc->SetCommandLines(commandLines);
  1413. cc->SetComment(ccComment.c_str());
  1414. cc->SetStdPipesUTF8(true);
  1415. if (qrc.Generated || this->Rcc.GlobalTarget) {
  1416. // Create custom rcc target
  1417. std::string ccName;
  1418. {
  1419. ccName = cmStrCat(this->GenTarget->GetName(), "_arcc_", qrc.QrcName);
  1420. if (!qrc.Unique) {
  1421. ccName += cmStrCat('_', qrc.QrcPathChecksum);
  1422. }
  1423. cc->SetByproducts(ccOutput);
  1424. cc->SetDepends(ccDepends);
  1425. cc->SetEscapeOldStyle(false);
  1426. cmTarget* autoRccTarget =
  1427. this->LocalGen->AddUtilityCommand(ccName, true, std::move(cc));
  1428. // Create autogen generator target
  1429. this->LocalGen->AddGeneratorTarget(
  1430. cm::make_unique<cmGeneratorTarget>(autoRccTarget, this->LocalGen));
  1431. // Set FOLDER property in autogen target
  1432. if (!this->TargetsFolder.empty()) {
  1433. autoRccTarget->SetProperty("FOLDER", this->TargetsFolder);
  1434. }
  1435. if (!this->Rcc.ExecutableTargetName.empty()) {
  1436. autoRccTarget->AddUtility(this->Rcc.ExecutableTargetName, false,
  1437. this->Makefile);
  1438. }
  1439. }
  1440. // Add autogen target to the origin target dependencies
  1441. this->GenTarget->Target->AddUtility(ccName, false, this->Makefile);
  1442. // Add autogen target to the global autogen target dependencies
  1443. if (this->Rcc.GlobalTarget) {
  1444. this->GlobalInitializer->AddToGlobalAutoRcc(this->LocalGen, ccName);
  1445. }
  1446. } else {
  1447. // Create custom rcc command
  1448. {
  1449. std::vector<std::string> ccByproducts;
  1450. // Add the resource files to the dependencies
  1451. for (std::string const& fileName : qrc.Resources) {
  1452. // Add resource file to the custom command dependencies
  1453. ccDepends.push_back(fileName);
  1454. }
  1455. if (!this->Rcc.ExecutableTargetName.empty()) {
  1456. ccDepends.push_back(this->Rcc.ExecutableTargetName);
  1457. }
  1458. cc->SetOutputs(ccOutput);
  1459. cc->SetByproducts(ccByproducts);
  1460. cc->SetDepends(ccDepends);
  1461. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1462. }
  1463. // Reconfigure when .qrc file changes
  1464. this->Makefile->AddCMakeDependFile(qrc.QrcFile);
  1465. }
  1466. }
  1467. return true;
  1468. }
  1469. bool cmQtAutoGenInitializer::SetupCustomTargets()
  1470. {
  1471. // Create info directory on demand
  1472. if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
  1473. cmSystemTools::Error(cmStrCat("AutoGen: Could not create directory: ",
  1474. Quoted(this->Dir.Info)));
  1475. return false;
  1476. }
  1477. // Generate autogen target info file
  1478. if (this->MocOrUicEnabled()) {
  1479. // Write autogen target info files
  1480. if (!this->SetupWriteAutogenInfo()) {
  1481. return false;
  1482. }
  1483. }
  1484. // Write AUTORCC info files
  1485. return !this->Rcc.Enabled || this->SetupWriteRccInfo();
  1486. }
  1487. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  1488. {
  1489. // Utility lambdas
  1490. auto MfDef = [this](std::string const& key) {
  1491. return this->Makefile->GetSafeDefinition(key);
  1492. };
  1493. // Filtered headers and sources
  1494. std::set<std::string> moc_skip;
  1495. std::set<std::string> uic_skip;
  1496. std::vector<MUFile const*> headers;
  1497. std::vector<MUFile const*> sources;
  1498. // Filter headers
  1499. {
  1500. headers.reserve(this->AutogenTarget.Headers.size());
  1501. for (auto const& pair : this->AutogenTarget.Headers) {
  1502. MUFile const* const muf = pair.second.get();
  1503. if (muf->SkipMoc) {
  1504. moc_skip.insert(muf->FullPath);
  1505. }
  1506. if (muf->SkipUic) {
  1507. uic_skip.insert(muf->FullPath);
  1508. }
  1509. if (muf->Generated && !this->CMP0071Accept) {
  1510. continue;
  1511. }
  1512. if (muf->MocIt || muf->UicIt) {
  1513. headers.emplace_back(muf);
  1514. }
  1515. }
  1516. std::sort(headers.begin(), headers.end(),
  1517. [](MUFile const* a, MUFile const* b) {
  1518. return (a->FullPath < b->FullPath);
  1519. });
  1520. }
  1521. // Filter sources
  1522. {
  1523. sources.reserve(this->AutogenTarget.Sources.size());
  1524. for (auto const& pair : this->AutogenTarget.Sources) {
  1525. MUFile const* const muf = pair.second.get();
  1526. if (muf->Generated && !this->CMP0071Accept) {
  1527. continue;
  1528. }
  1529. if (muf->SkipMoc) {
  1530. moc_skip.insert(muf->FullPath);
  1531. }
  1532. if (muf->SkipUic) {
  1533. uic_skip.insert(muf->FullPath);
  1534. }
  1535. if (muf->MocIt || muf->UicIt) {
  1536. sources.emplace_back(muf);
  1537. }
  1538. }
  1539. std::sort(sources.begin(), sources.end(),
  1540. [](MUFile const* a, MUFile const* b) {
  1541. return (a->FullPath < b->FullPath);
  1542. });
  1543. }
  1544. // Info writer
  1545. InfoWriter info;
  1546. // General
  1547. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1548. info.SetUInt("PARALLEL", this->AutogenTarget.Parallel);
  1549. info.SetUInt("VERBOSITY", this->Verbosity);
  1550. // Directories
  1551. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1552. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1553. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1554. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1555. info.Set("BUILD_DIR", this->Dir.Build);
  1556. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1557. info.SetUInt("QT_VERSION_MAJOR", this->QtVersion.Major);
  1558. info.SetUInt("QT_VERSION_MINOR", this->QtVersion.Minor);
  1559. info.Set("QT_MOC_EXECUTABLE", this->Moc.Executable);
  1560. info.Set("QT_UIC_EXECUTABLE", this->Uic.Executable);
  1561. info.Set("CMAKE_EXECUTABLE", cmSystemTools::GetCMakeCommand());
  1562. info.SetConfig("SETTINGS_FILE", this->AutogenTarget.SettingsFile);
  1563. info.SetConfig("PARSE_CACHE_FILE", this->AutogenTarget.ParseCacheFile);
  1564. info.Set("DEP_FILE", this->AutogenTarget.DepFile);
  1565. info.Set("DEP_FILE_RULE_NAME", this->AutogenTarget.DepFileRuleName);
  1566. info.SetArray("CMAKE_LIST_FILES", this->Makefile->GetListFiles());
  1567. info.SetArray("HEADER_EXTENSIONS",
  1568. this->Makefile->GetCMakeInstance()->GetHeaderExtensions());
  1569. auto cfgArray = [this](std::vector<size_t> const& configs) -> Json::Value {
  1570. Json::Value value;
  1571. if (!configs.empty()) {
  1572. value = Json::arrayValue;
  1573. for (size_t ci : configs) {
  1574. value.append(this->ConfigsList[ci]);
  1575. }
  1576. }
  1577. return value;
  1578. };
  1579. info.SetArrayArray("HEADERS", headers,
  1580. [this, &cfgArray](Json::Value& jval, MUFile const* muf) {
  1581. jval.resize(4u);
  1582. jval[0u] = muf->FullPath;
  1583. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm',
  1584. muf->UicIt ? 'U' : 'u');
  1585. jval[2u] = this->GetMocBuildPath(*muf);
  1586. jval[3u] = cfgArray(muf->Configs);
  1587. });
  1588. info.SetArrayArray(
  1589. "SOURCES", sources, [&cfgArray](Json::Value& jval, MUFile const* muf) {
  1590. jval.resize(3u);
  1591. jval[0u] = muf->FullPath;
  1592. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm', muf->UicIt ? 'U' : 'u');
  1593. jval[2u] = cfgArray(muf->Configs);
  1594. });
  1595. // Write moc settings
  1596. if (this->Moc.Enabled) {
  1597. info.SetArray("MOC_SKIP", moc_skip);
  1598. info.SetConfigArray("MOC_DEFINITIONS", this->Moc.Defines);
  1599. info.SetConfigArray("MOC_INCLUDES", this->Moc.Includes);
  1600. info.SetArray("MOC_OPTIONS", this->Moc.Options);
  1601. info.SetBool("MOC_RELAXED_MODE", this->Moc.RelaxedMode);
  1602. info.SetBool("MOC_PATH_PREFIX", this->Moc.PathPrefix);
  1603. cmGeneratorExpressionDAGChecker dagChecker(
  1604. this->GenTarget, "AUTOMOC_MACRO_NAMES", nullptr, nullptr);
  1605. EvaluatedTargetPropertyEntries InterfaceAutoMocMacroNamesEntries;
  1606. if (this->MultiConfig) {
  1607. for (auto const& cfg : this->ConfigsList) {
  1608. if (!cfg.empty()) {
  1609. AddInterfaceEntries(this->GenTarget, cfg,
  1610. "INTERFACE_AUTOMOC_MACRO_NAMES", "CXX",
  1611. &dagChecker, InterfaceAutoMocMacroNamesEntries,
  1612. IncludeRuntimeInterface::Yes);
  1613. }
  1614. }
  1615. } else {
  1616. AddInterfaceEntries(this->GenTarget, this->ConfigDefault,
  1617. "INTERFACE_AUTOMOC_MACRO_NAMES", "CXX", &dagChecker,
  1618. InterfaceAutoMocMacroNamesEntries,
  1619. IncludeRuntimeInterface::Yes);
  1620. }
  1621. for (auto const& entry : InterfaceAutoMocMacroNamesEntries.Entries) {
  1622. this->Moc.MacroNames.insert(this->Moc.MacroNames.end(),
  1623. entry.Values.begin(), entry.Values.end());
  1624. }
  1625. this->Moc.MacroNames.erase(cmRemoveDuplicates(this->Moc.MacroNames),
  1626. this->Moc.MacroNames.end());
  1627. info.SetArray("MOC_MACRO_NAMES", this->Moc.MacroNames);
  1628. info.SetArrayArray(
  1629. "MOC_DEPEND_FILTERS", this->Moc.DependFilters,
  1630. [](Json::Value& jval, std::pair<std::string, std::string> const& pair) {
  1631. jval.resize(2u);
  1632. jval[0u] = pair.first;
  1633. jval[1u] = pair.second;
  1634. });
  1635. info.SetConfig("MOC_COMPILATION_FILE", this->Moc.CompilationFile);
  1636. info.SetConfig("MOC_PREDEFS_FILE", this->Moc.PredefsFile);
  1637. cmStandardLevelResolver const resolver{ this->Makefile };
  1638. auto const CompileOptionFlag =
  1639. resolver.GetCompileOptionDef(this->GenTarget, "CXX", "");
  1640. auto const CompileOptionValue =
  1641. this->GenTarget->Makefile->GetSafeDefinition(CompileOptionFlag);
  1642. if (!CompileOptionValue.empty()) {
  1643. if (this->Moc.PredefsCmd.size() >= 3) {
  1644. this->Moc.PredefsCmd.insert(this->Moc.PredefsCmd.begin() + 1,
  1645. CompileOptionValue);
  1646. }
  1647. }
  1648. info.SetArray("MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  1649. }
  1650. // Write uic settings
  1651. if (this->Uic.Enabled) {
  1652. // Add skipped .ui files
  1653. uic_skip.insert(this->Uic.SkipUi.begin(), this->Uic.SkipUi.end());
  1654. info.SetArray("UIC_SKIP", uic_skip);
  1655. info.SetArrayArray("UIC_UI_FILES", this->Uic.UiFilesWithOptions,
  1656. [](Json::Value& jval, UicT::UiFileT const& uiFile) {
  1657. jval.resize(2u);
  1658. jval[0u] = uiFile.first;
  1659. InfoWriter::MakeStringArray(jval[1u], uiFile.second);
  1660. });
  1661. info.SetConfigArray("UIC_OPTIONS", this->Uic.Options);
  1662. info.SetArray("UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  1663. }
  1664. info.Save(this->AutogenTarget.InfoFile);
  1665. return true;
  1666. }
  1667. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  1668. {
  1669. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1670. // Utility lambdas
  1671. auto MfDef = [this](std::string const& key) {
  1672. return this->Makefile->GetSafeDefinition(key);
  1673. };
  1674. InfoWriter info;
  1675. // General
  1676. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1677. info.SetUInt("VERBOSITY", this->Verbosity);
  1678. // Files
  1679. info.Set("LOCK_FILE", qrc.LockFile);
  1680. info.SetConfig("SETTINGS_FILE", qrc.SettingsFile);
  1681. // Directories
  1682. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1683. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1684. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1685. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1686. info.Set("BUILD_DIR", this->Dir.Build);
  1687. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1688. // rcc executable
  1689. info.Set("RCC_EXECUTABLE", this->Rcc.Executable);
  1690. info.SetArray("RCC_LIST_OPTIONS",
  1691. this->Rcc.ExecutableFeatures->ListOptions);
  1692. // qrc file
  1693. info.Set("SOURCE", qrc.QrcFile);
  1694. info.Set("OUTPUT_CHECKSUM", qrc.QrcPathChecksum);
  1695. info.Set("OUTPUT_NAME", cmSystemTools::GetFilenameName(qrc.OutputFile));
  1696. info.SetArray("OPTIONS", qrc.Options);
  1697. info.SetArray("INPUTS", qrc.Resources);
  1698. info.Save(qrc.InfoFile);
  1699. }
  1700. return true;
  1701. }
  1702. cmSourceFile* cmQtAutoGenInitializer::RegisterGeneratedSource(
  1703. std::string const& filename)
  1704. {
  1705. cmSourceFile* gFile = this->Makefile->GetOrCreateSource(filename, true);
  1706. gFile->MarkAsGenerated();
  1707. gFile->SetProperty("SKIP_AUTOGEN", "1");
  1708. gFile->SetProperty("SKIP_LINTING", "ON");
  1709. return gFile;
  1710. }
  1711. cmSourceFile* cmQtAutoGenInitializer::AddGeneratedSource(
  1712. std::string const& filename, GenVarsT const& genVars, bool prepend)
  1713. {
  1714. // Register source at makefile
  1715. cmSourceFile* gFile = this->RegisterGeneratedSource(filename);
  1716. // Add source file to target
  1717. this->GenTarget->AddSource(filename, prepend);
  1718. // Add source file to source group
  1719. this->AddToSourceGroup(filename, genVars.GenNameUpper);
  1720. return gFile;
  1721. }
  1722. void cmQtAutoGenInitializer::AddGeneratedSource(ConfigString const& filename,
  1723. GenVarsT const& genVars,
  1724. bool prepend)
  1725. {
  1726. // XXX(xcode-per-cfg-src): Drop the Xcode-specific part of the condition
  1727. // when the Xcode generator supports per-config sources.
  1728. if (!this->MultiConfig || this->GlobalGen->IsXcode()) {
  1729. cmSourceFile* sf =
  1730. this->AddGeneratedSource(filename.Default, genVars, prepend);
  1731. handleSkipPch(sf);
  1732. return;
  1733. }
  1734. for (auto const& cfg : this->ConfigsList) {
  1735. std::string const& filenameCfg = filename.Config.at(cfg);
  1736. // Register source at makefile
  1737. cmSourceFile* sf = this->RegisterGeneratedSource(filenameCfg);
  1738. handleSkipPch(sf);
  1739. // Add source file to target for this configuration.
  1740. this->GenTarget->AddSource(
  1741. cmStrCat("$<$<CONFIG:"_s, cfg, ">:"_s, filenameCfg, ">"_s), prepend);
  1742. // Add source file to source group
  1743. this->AddToSourceGroup(filenameCfg, genVars.GenNameUpper);
  1744. }
  1745. }
  1746. void cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName,
  1747. cm::string_view genNameUpper)
  1748. {
  1749. cmSourceGroup* sourceGroup = nullptr;
  1750. // Acquire source group
  1751. {
  1752. std::string property;
  1753. std::string groupName;
  1754. {
  1755. // Prefer generator specific source group name
  1756. std::initializer_list<std::string> const props{
  1757. cmStrCat(genNameUpper, "_SOURCE_GROUP"), "AUTOGEN_SOURCE_GROUP"
  1758. };
  1759. for (std::string const& prop : props) {
  1760. cmValue propName = this->Makefile->GetState()->GetGlobalProperty(prop);
  1761. if (cmNonempty(propName)) {
  1762. groupName = *propName;
  1763. property = prop;
  1764. break;
  1765. }
  1766. }
  1767. }
  1768. // Generate a source group on demand
  1769. if (!groupName.empty()) {
  1770. sourceGroup = this->Makefile->GetOrCreateSourceGroup(groupName);
  1771. if (!sourceGroup) {
  1772. cmSystemTools::Error(
  1773. cmStrCat(genNameUpper, " error in ", property,
  1774. ": Could not find or create the source group ",
  1775. cmQtAutoGen::Quoted(groupName)));
  1776. }
  1777. }
  1778. }
  1779. if (sourceGroup) {
  1780. sourceGroup->AddGroupFile(fileName);
  1781. }
  1782. }
  1783. void cmQtAutoGenInitializer::AddCleanFile(std::string const& fileName)
  1784. {
  1785. this->GenTarget->Target->AppendProperty("ADDITIONAL_CLEAN_FILES", fileName);
  1786. }
  1787. void cmQtAutoGenInitializer::ConfigFileNames(ConfigString& configString,
  1788. cm::string_view prefix,
  1789. cm::string_view suffix)
  1790. {
  1791. configString.Default = cmStrCat(prefix, suffix);
  1792. if (this->MultiConfig) {
  1793. for (auto const& cfg : this->ConfigsList) {
  1794. configString.Config[cfg] = cmStrCat(prefix, '_', cfg, suffix);
  1795. }
  1796. }
  1797. }
  1798. void cmQtAutoGenInitializer::ConfigFileNamesAndGenex(
  1799. ConfigString& configString, std::string& genex, cm::string_view const prefix,
  1800. cm::string_view const suffix)
  1801. {
  1802. this->ConfigFileNames(configString, prefix, suffix);
  1803. if (this->MultiConfig) {
  1804. genex = cmStrCat(prefix, "_$<CONFIG>"_s, suffix);
  1805. } else {
  1806. genex = configString.Default;
  1807. }
  1808. }
  1809. void cmQtAutoGenInitializer::ConfigFileClean(ConfigString& configString)
  1810. {
  1811. this->AddCleanFile(configString.Default);
  1812. if (this->MultiConfig) {
  1813. for (auto const& pair : configString.Config) {
  1814. this->AddCleanFile(pair.second);
  1815. }
  1816. }
  1817. }
  1818. static cmQtAutoGen::IntegerVersion parseMocVersion(std::string str)
  1819. {
  1820. cmQtAutoGen::IntegerVersion result;
  1821. static const std::string prelude = "moc ";
  1822. size_t const pos = str.find(prelude);
  1823. if (pos == std::string::npos) {
  1824. return result;
  1825. }
  1826. str.erase(0, prelude.size() + pos);
  1827. std::istringstream iss(str);
  1828. std::string major;
  1829. std::string minor;
  1830. if (!std::getline(iss, major, '.') || !std::getline(iss, minor, '.')) {
  1831. return result;
  1832. }
  1833. result.Major = static_cast<unsigned int>(std::stoi(major));
  1834. result.Minor = static_cast<unsigned int>(std::stoi(minor));
  1835. return result;
  1836. }
  1837. static cmQtAutoGen::IntegerVersion GetMocVersion(
  1838. const std::string& mocExecutablePath)
  1839. {
  1840. std::string capturedStdOut;
  1841. int exitCode;
  1842. if (!cmSystemTools::RunSingleCommand({ mocExecutablePath, "--version" },
  1843. &capturedStdOut, nullptr, &exitCode,
  1844. nullptr, cmSystemTools::OUTPUT_NONE)) {
  1845. return {};
  1846. }
  1847. if (exitCode != 0) {
  1848. return {};
  1849. }
  1850. return parseMocVersion(capturedStdOut);
  1851. }
  1852. static std::string FindMocExecutableFromMocTarget(cmMakefile const* makefile,
  1853. unsigned int qtMajorVersion)
  1854. {
  1855. std::string result;
  1856. const std::string mocTargetName =
  1857. "Qt" + std::to_string(qtMajorVersion) + "::moc";
  1858. cmTarget const* mocTarget = makefile->FindTargetToUse(mocTargetName);
  1859. if (mocTarget) {
  1860. result = mocTarget->GetSafeProperty("IMPORTED_LOCATION");
  1861. }
  1862. return result;
  1863. }
  1864. std::pair<cmQtAutoGen::IntegerVersion, unsigned int>
  1865. cmQtAutoGenInitializer::GetQtVersion(cmGeneratorTarget const* target,
  1866. std::string mocExecutable)
  1867. {
  1868. // Converts a char ptr to an unsigned int value
  1869. auto toUInt = [](const char* const input) -> unsigned int {
  1870. unsigned long tmp = 0;
  1871. if (input && cmStrToULong(input, &tmp)) {
  1872. return static_cast<unsigned int>(tmp);
  1873. }
  1874. return 0u;
  1875. };
  1876. auto toUInt2 = [](cmValue input) -> unsigned int {
  1877. unsigned long tmp = 0;
  1878. if (input && cmStrToULong(*input, &tmp)) {
  1879. return static_cast<unsigned int>(tmp);
  1880. }
  1881. return 0u;
  1882. };
  1883. // Initialize return value to a default
  1884. std::pair<IntegerVersion, unsigned int> res(
  1885. IntegerVersion(),
  1886. toUInt(target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  1887. "")));
  1888. // Acquire known Qt versions
  1889. std::vector<cmQtAutoGen::IntegerVersion> knownQtVersions;
  1890. {
  1891. // Qt version variable prefixes
  1892. static std::initializer_list<
  1893. std::pair<cm::string_view, cm::string_view>> const keys{
  1894. { "Qt6Core_VERSION_MAJOR", "Qt6Core_VERSION_MINOR" },
  1895. { "Qt5Core_VERSION_MAJOR", "Qt5Core_VERSION_MINOR" },
  1896. { "QT_VERSION_MAJOR", "QT_VERSION_MINOR" },
  1897. };
  1898. knownQtVersions.reserve(keys.size() * 2);
  1899. // Adds a version to the result (nullptr safe)
  1900. auto addVersion = [&knownQtVersions, &toUInt2](cmValue major,
  1901. cmValue minor) {
  1902. cmQtAutoGen::IntegerVersion ver(toUInt2(major), toUInt2(minor));
  1903. if (ver.Major != 0) {
  1904. knownQtVersions.emplace_back(ver);
  1905. }
  1906. };
  1907. // Read versions from variables
  1908. for (auto const& keyPair : keys) {
  1909. addVersion(target->Makefile->GetDefinition(std::string(keyPair.first)),
  1910. target->Makefile->GetDefinition(std::string(keyPair.second)));
  1911. }
  1912. // Read versions from directory properties
  1913. for (auto const& keyPair : keys) {
  1914. addVersion(target->Makefile->GetProperty(std::string(keyPair.first)),
  1915. target->Makefile->GetProperty(std::string(keyPair.second)));
  1916. }
  1917. }
  1918. // Evaluate known Qt versions
  1919. if (!knownQtVersions.empty()) {
  1920. if (res.second == 0) {
  1921. // No specific version was requested by the target:
  1922. // Use highest known Qt version.
  1923. res.first = knownQtVersions.at(0);
  1924. } else {
  1925. // Pick a version from the known versions:
  1926. for (auto const& it : knownQtVersions) {
  1927. if (it.Major == res.second) {
  1928. res.first = it;
  1929. break;
  1930. }
  1931. }
  1932. }
  1933. }
  1934. if (res.first.Major == 0) {
  1935. // We could not get the version number from variables or directory
  1936. // properties. This might happen if the find_package call for Qt is wrapped
  1937. // in a function. Try to find the moc executable path from the available
  1938. // targets and call "moc --version" to get the Qt version.
  1939. if (mocExecutable.empty()) {
  1940. mocExecutable =
  1941. FindMocExecutableFromMocTarget(target->Makefile, res.second);
  1942. }
  1943. if (!mocExecutable.empty()) {
  1944. res.first = GetMocVersion(mocExecutable);
  1945. }
  1946. }
  1947. return res;
  1948. }
  1949. std::string cmQtAutoGenInitializer::GetMocBuildPath(MUFile const& muf)
  1950. {
  1951. std::string res;
  1952. if (!muf.MocIt) {
  1953. return res;
  1954. }
  1955. std::string basePath =
  1956. cmStrCat(this->PathCheckSum.getPart(muf.FullPath), "/moc_",
  1957. FileNameWithoutLastExtension(muf.FullPath));
  1958. res = cmStrCat(basePath, ".cpp");
  1959. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1960. return res;
  1961. }
  1962. // File name already emitted.
  1963. // Try appending the header suffix to the base path.
  1964. basePath = cmStrCat(basePath, '_', muf.SF->GetExtension());
  1965. res = cmStrCat(basePath, ".cpp");
  1966. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1967. return res;
  1968. }
  1969. // File name with header extension already emitted.
  1970. // Try adding a number to the base path.
  1971. constexpr std::size_t number_begin = 2;
  1972. constexpr std::size_t number_end = 256;
  1973. for (std::size_t ii = number_begin; ii != number_end; ++ii) {
  1974. res = cmStrCat(basePath, '_', ii, ".cpp");
  1975. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1976. return res;
  1977. }
  1978. }
  1979. // Output file name conflict (unlikely, but still...)
  1980. cmSystemTools::Error(
  1981. cmStrCat("moc output file name conflict for ", muf.FullPath));
  1982. return res;
  1983. }
  1984. bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
  1985. const std::string& executable,
  1986. bool ignoreMissingTarget) const
  1987. {
  1988. auto print_err = [this, &genVars](std::string const& err) {
  1989. cmSystemTools::Error(cmStrCat(genVars.GenNameUpper, " for target ",
  1990. this->GenTarget->GetName(), ": ", err));
  1991. };
  1992. // Custom executable
  1993. {
  1994. std::string const prop = cmStrCat(genVars.GenNameUpper, "_EXECUTABLE");
  1995. std::string const& val = this->GenTarget->Target->GetSafeProperty(prop);
  1996. if (!val.empty()) {
  1997. // Evaluate generator expression
  1998. {
  1999. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  2000. cmGeneratorExpression ge(*this->Makefile->GetCMakeInstance(), lfbt);
  2001. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(val);
  2002. genVars.Executable = cge->Evaluate(this->LocalGen, "");
  2003. }
  2004. if (genVars.Executable.empty() && !ignoreMissingTarget) {
  2005. print_err(prop + " evaluates to an empty value");
  2006. return false;
  2007. }
  2008. // Create empty compiler features.
  2009. genVars.ExecutableFeatures =
  2010. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  2011. return true;
  2012. }
  2013. }
  2014. // Find executable target
  2015. {
  2016. // Find executable target name
  2017. cm::string_view prefix;
  2018. if (this->QtVersion.Major == 4) {
  2019. prefix = "Qt4::";
  2020. } else if (this->QtVersion.Major == 5) {
  2021. prefix = "Qt5::";
  2022. } else if (this->QtVersion.Major == 6) {
  2023. prefix = "Qt6::";
  2024. }
  2025. std::string const targetName = cmStrCat(prefix, executable);
  2026. // Find target
  2027. cmGeneratorTarget* genTarget =
  2028. this->LocalGen->FindGeneratorTargetToUse(targetName);
  2029. if (genTarget) {
  2030. genVars.ExecutableTargetName = targetName;
  2031. genVars.ExecutableTarget = genTarget;
  2032. if (genTarget->IsImported()) {
  2033. genVars.Executable = genTarget->ImportedGetLocation("");
  2034. } else {
  2035. genVars.Executable = genTarget->GetLocation("");
  2036. }
  2037. } else {
  2038. if (ignoreMissingTarget) {
  2039. // Create empty compiler features.
  2040. genVars.ExecutableFeatures =
  2041. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  2042. return true;
  2043. }
  2044. print_err(cmStrCat("Could not find ", executable, " executable target ",
  2045. targetName));
  2046. return false;
  2047. }
  2048. }
  2049. // Get executable features
  2050. {
  2051. std::string err;
  2052. genVars.ExecutableFeatures = this->GlobalInitializer->GetCompilerFeatures(
  2053. executable, genVars.Executable, err);
  2054. if (!genVars.ExecutableFeatures) {
  2055. print_err(err);
  2056. return false;
  2057. }
  2058. }
  2059. return true;
  2060. }
  2061. void cmQtAutoGenInitializer::handleSkipPch(cmSourceFile* sf)
  2062. {
  2063. bool skipPch = true;
  2064. for (auto const& pair : this->AutogenTarget.Sources) {
  2065. if (!pair.first->GetIsGenerated() &&
  2066. !pair.first->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  2067. skipPch = false;
  2068. }
  2069. }
  2070. if (skipPch) {
  2071. sf->SetProperty("SKIP_PRECOMPILE_HEADERS", "ON");
  2072. }
  2073. }