cmQtAutoGenInitializer.cxx 55 KB

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