cmQtAutoGenInitializer.cxx 55 KB

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