cmQtAutoGenInitializer.cxx 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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 "cmAlgorithms.h"
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmDuration.h"
  9. #include "cmFilePathChecksum.h"
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLinkItem.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmOutputConverter.h"
  17. #include "cmPolicies.h"
  18. #include "cmProcessOutput.h"
  19. #include "cmSourceFile.h"
  20. #include "cmSourceGroup.h"
  21. #include "cmState.h"
  22. #include "cmStateTypes.h"
  23. #include "cmSystemTools.h"
  24. #include "cmTarget.h"
  25. #include "cmake.h"
  26. #include "cmsys/FStream.hxx"
  27. #include "cmsys/SystemInformation.hxx"
  28. #include <algorithm>
  29. #include <array>
  30. #include <deque>
  31. #include <map>
  32. #include <set>
  33. #include <sstream>
  34. #include <string>
  35. #include <utility>
  36. #include <vector>
  37. static std::size_t GetParallelCPUCount()
  38. {
  39. static std::size_t count = 0;
  40. // Detect only on the first call
  41. if (count == 0) {
  42. cmsys::SystemInformation info;
  43. info.RunCPUCheck();
  44. count = info.GetNumberOfPhysicalCPU();
  45. count = std::max<std::size_t>(count, 1);
  46. count = std::min<std::size_t>(count, cmQtAutoGen::ParallelMax);
  47. }
  48. return count;
  49. }
  50. static bool AddToSourceGroup(cmMakefile* makefile, std::string const& fileName,
  51. cmQtAutoGen::GeneratorT genType)
  52. {
  53. cmSourceGroup* sourceGroup = nullptr;
  54. // Acquire source group
  55. {
  56. std::string property;
  57. std::string groupName;
  58. {
  59. std::array<std::string, 2> props;
  60. // Use generator specific group name
  61. switch (genType) {
  62. case cmQtAutoGen::GeneratorT::MOC:
  63. props[0] = "AUTOMOC_SOURCE_GROUP";
  64. break;
  65. case cmQtAutoGen::GeneratorT::RCC:
  66. props[0] = "AUTORCC_SOURCE_GROUP";
  67. break;
  68. default:
  69. props[0] = "AUTOGEN_SOURCE_GROUP";
  70. break;
  71. }
  72. props[1] = "AUTOGEN_SOURCE_GROUP";
  73. for (std::string& prop : props) {
  74. const char* propName = makefile->GetState()->GetGlobalProperty(prop);
  75. if ((propName != nullptr) && (*propName != '\0')) {
  76. groupName = propName;
  77. property = std::move(prop);
  78. break;
  79. }
  80. }
  81. }
  82. // Generate a source group on demand
  83. if (!groupName.empty()) {
  84. sourceGroup = makefile->GetOrCreateSourceGroup(groupName);
  85. if (sourceGroup == nullptr) {
  86. std::ostringstream ost;
  87. ost << cmQtAutoGen::GeneratorNameUpper(genType);
  88. ost << ": " << property;
  89. ost << ": Could not find or create the source group ";
  90. ost << cmQtAutoGen::Quoted(groupName);
  91. cmSystemTools::Error(ost.str().c_str());
  92. return false;
  93. }
  94. }
  95. }
  96. if (sourceGroup != nullptr) {
  97. sourceGroup->AddGroupFile(fileName);
  98. }
  99. return true;
  100. }
  101. static void AddCleanFile(cmMakefile* makefile, std::string const& fileName)
  102. {
  103. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fileName.c_str(),
  104. false);
  105. }
  106. static std::string FileProjectRelativePath(cmMakefile* makefile,
  107. std::string const& fileName)
  108. {
  109. std::string res;
  110. {
  111. std::string pSource = cmSystemTools::RelativePath(
  112. makefile->GetCurrentSourceDirectory(), fileName);
  113. std::string pBinary = cmSystemTools::RelativePath(
  114. makefile->GetCurrentBinaryDirectory(), fileName);
  115. if (pSource.size() < pBinary.size()) {
  116. res = std::move(pSource);
  117. } else if (pBinary.size() < fileName.size()) {
  118. res = std::move(pBinary);
  119. } else {
  120. res = fileName;
  121. }
  122. }
  123. return res;
  124. }
  125. /* @brief Tests if targetDepend is a STATIC_LIBRARY and if any of its
  126. * recursive STATIC_LIBRARY dependencies depends on targetOrigin
  127. * (STATIC_LIBRARY cycle).
  128. */
  129. static bool StaticLibraryCycle(cmGeneratorTarget const* targetOrigin,
  130. cmGeneratorTarget const* targetDepend,
  131. std::string const& config)
  132. {
  133. bool cycle = false;
  134. if ((targetOrigin->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  135. (targetDepend->GetType() == cmStateEnums::STATIC_LIBRARY)) {
  136. std::set<cmGeneratorTarget const*> knownLibs;
  137. std::deque<cmGeneratorTarget const*> testLibs;
  138. // Insert initial static_library dependency
  139. knownLibs.insert(targetDepend);
  140. testLibs.push_back(targetDepend);
  141. while (!testLibs.empty()) {
  142. cmGeneratorTarget const* testTarget = testLibs.front();
  143. testLibs.pop_front();
  144. // Check if the test target is the origin target (cycle)
  145. if (testTarget == targetOrigin) {
  146. cycle = true;
  147. break;
  148. }
  149. // Collect all static_library dependencies from the test target
  150. cmLinkImplementationLibraries const* libs =
  151. testTarget->GetLinkImplementationLibraries(config);
  152. if (libs != nullptr) {
  153. for (cmLinkItem const& item : libs->Libraries) {
  154. cmGeneratorTarget const* depTarget = item.Target;
  155. if ((depTarget != nullptr) &&
  156. (depTarget->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  157. knownLibs.insert(depTarget).second) {
  158. testLibs.push_back(depTarget);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. return cycle;
  165. }
  166. cmQtAutoGenInitializer::cmQtAutoGenInitializer(
  167. cmGeneratorTarget* target, bool mocEnabled, bool uicEnabled, bool rccEnabled,
  168. std::string const& qtVersionMajor)
  169. : Target(target)
  170. , MultiConfig(false)
  171. , QtVersionMajor(qtVersionMajor)
  172. {
  173. Moc.Enabled = mocEnabled;
  174. Uic.Enabled = uicEnabled;
  175. Rcc.Enabled = rccEnabled;
  176. this->QtVersionMinor =
  177. cmQtAutoGenInitializer::GetQtMinorVersion(target, this->QtVersionMajor);
  178. }
  179. bool cmQtAutoGenInitializer::InitCustomTargets()
  180. {
  181. cmMakefile* makefile = this->Target->Target->GetMakefile();
  182. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  183. cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
  184. // Verbosity
  185. {
  186. this->Verbosity = makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE");
  187. if (!this->Verbosity.empty()) {
  188. unsigned long iVerb = 0;
  189. if (!cmSystemTools::StringToULong(this->Verbosity.c_str(), &iVerb)) {
  190. // Non numeric verbosity
  191. this->Verbosity =
  192. cmSystemTools::IsOn(this->Verbosity.c_str()) ? "1" : "0";
  193. }
  194. }
  195. }
  196. // Configurations
  197. this->MultiConfig = globalGen->IsMultiConfig();
  198. this->ConfigDefault = makefile->GetConfigurations(this->ConfigsList);
  199. if (this->ConfigsList.empty()) {
  200. this->ConfigsList.push_back(this->ConfigDefault);
  201. }
  202. // Parallel processing
  203. this->Parallel = this->Target->GetSafeProperty("AUTOGEN_PARALLEL");
  204. if (this->Parallel.empty() || (this->Parallel == "AUTO")) {
  205. // Autodetect number of CPUs
  206. this->Parallel = std::to_string(GetParallelCPUCount());
  207. }
  208. // Autogen target name
  209. this->AutogenTargetName = this->Target->GetName();
  210. this->AutogenTargetName += "_autogen";
  211. // Autogen directories
  212. {
  213. // Collapsed current binary directory
  214. std::string const cbd = cmSystemTools::CollapseFullPath(
  215. "", makefile->GetCurrentBinaryDirectory());
  216. // Autogen info dir
  217. this->DirInfo = cbd;
  218. this->DirInfo += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  219. this->DirInfo += '/';
  220. this->DirInfo += this->AutogenTargetName;
  221. this->DirInfo += ".dir";
  222. cmSystemTools::ConvertToUnixSlashes(this->DirInfo);
  223. // Autogen build dir
  224. this->DirBuild = this->Target->GetSafeProperty("AUTOGEN_BUILD_DIR");
  225. if (this->DirBuild.empty()) {
  226. this->DirBuild = cbd;
  227. this->DirBuild += '/';
  228. this->DirBuild += this->AutogenTargetName;
  229. }
  230. cmSystemTools::ConvertToUnixSlashes(this->DirBuild);
  231. // Working directory
  232. this->DirWork = cbd;
  233. cmSystemTools::ConvertToUnixSlashes(this->DirWork);
  234. // Include directory
  235. this->DirInclude = this->DirBuild;
  236. this->DirInclude += "/include";
  237. if (this->MultiConfig) {
  238. this->DirInclude += "_$<CONFIG>";
  239. }
  240. if (this->MultiConfig) {
  241. for (std::string const& cfg : this->ConfigsList) {
  242. std::string& dir = this->DirConfigInclude[cfg];
  243. dir = this->DirBuild;
  244. dir += "/include_";
  245. dir += cfg;
  246. }
  247. }
  248. }
  249. // Autogen files
  250. {
  251. this->AutogenInfoFile = this->DirInfo;
  252. this->AutogenInfoFile += "/AutogenInfo.cmake";
  253. this->AutogenSettingsFile = this->DirInfo;
  254. this->AutogenSettingsFile += "/AutogenOldSettings.txt";
  255. }
  256. // Autogen target FOLDER property
  257. {
  258. const char* folder =
  259. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  260. if (folder == nullptr) {
  261. folder =
  262. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  263. }
  264. // Inherit FOLDER property from target (#13688)
  265. if (folder == nullptr) {
  266. folder = this->Target->GetProperty("FOLDER");
  267. }
  268. if (folder != nullptr) {
  269. this->AutogenFolder = folder;
  270. }
  271. }
  272. std::set<std::string> autogenDependFiles;
  273. std::set<cmTarget*> autogenDependTargets;
  274. std::vector<std::string> autogenProvides;
  275. // Remove build directories on cleanup
  276. AddCleanFile(makefile, this->DirBuild);
  277. // Remove old settings on cleanup
  278. {
  279. std::string base = this->DirInfo;
  280. base += "/AutogenOldSettings";
  281. if (this->MultiConfig) {
  282. for (std::string const& cfg : this->ConfigsList) {
  283. std::string filename = base;
  284. filename += '_';
  285. filename += cfg;
  286. filename += ".cmake";
  287. AddCleanFile(makefile, filename);
  288. }
  289. } else {
  290. AddCleanFile(makefile, base.append(".cmake"));
  291. }
  292. }
  293. // Add moc compilation to generated files list
  294. if (this->Moc.Enabled) {
  295. std::string mocsComp = this->DirBuild + "/mocs_compilation.cpp";
  296. this->AddGeneratedSource(mocsComp, GeneratorT::MOC);
  297. autogenProvides.push_back(std::move(mocsComp));
  298. }
  299. // Add autogen includes directory to the origin target INCLUDE_DIRECTORIES
  300. if (this->Moc.Enabled || this->Uic.Enabled ||
  301. (this->Rcc.Enabled && this->MultiConfig)) {
  302. this->Target->AddIncludeDirectory(this->DirInclude, true);
  303. }
  304. // Acquire rcc executable and features
  305. if (this->Rcc.Enabled) {
  306. if (!GetRccExecutable()) {
  307. return false;
  308. }
  309. }
  310. // Extract relevant source files
  311. std::vector<std::string> generatedSources;
  312. std::vector<std::string> generatedHeaders;
  313. {
  314. std::string const qrcExt = "qrc";
  315. std::vector<cmSourceFile*> srcFiles;
  316. this->Target->GetConfigCommonSourceFiles(srcFiles);
  317. for (cmSourceFile* sf : srcFiles) {
  318. if (sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  319. continue;
  320. }
  321. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  322. std::string const& fPath = sf->GetFullPath();
  323. std::string const& ext = sf->GetExtension();
  324. // Register generated files that will be scanned by moc or uic
  325. if (this->Moc.Enabled || this->Uic.Enabled) {
  326. cmSystemTools::FileFormat const fileType =
  327. cmSystemTools::GetFileFormat(ext.c_str());
  328. if ((fileType == cmSystemTools::CXX_FILE_FORMAT) ||
  329. (fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  330. std::string const absPath = cmSystemTools::GetRealPath(fPath);
  331. if ((this->Moc.Enabled && !sf->GetPropertyAsBool("SKIP_AUTOMOC")) ||
  332. (this->Uic.Enabled && !sf->GetPropertyAsBool("SKIP_AUTOUIC"))) {
  333. // Register source
  334. const bool generated = sf->GetPropertyAsBool("GENERATED");
  335. if (fileType == cmSystemTools::HEADER_FILE_FORMAT) {
  336. if (generated) {
  337. generatedHeaders.push_back(absPath);
  338. } else {
  339. this->Headers.push_back(absPath);
  340. }
  341. } else {
  342. if (generated) {
  343. generatedSources.push_back(absPath);
  344. } else {
  345. this->Sources.push_back(absPath);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. // Register rcc enabled files
  352. if (this->Rcc.Enabled && (ext == qrcExt) &&
  353. !sf->GetPropertyAsBool("SKIP_AUTORCC")) {
  354. // Register qrc file
  355. {
  356. Qrc qrc;
  357. qrc.QrcFile = cmSystemTools::GetRealPath(fPath);
  358. qrc.QrcName =
  359. cmSystemTools::GetFilenameWithoutLastExtension(qrc.QrcFile);
  360. qrc.Generated = sf->GetPropertyAsBool("GENERATED");
  361. // RCC options
  362. {
  363. std::string const opts = sf->GetSafeProperty("AUTORCC_OPTIONS");
  364. if (!opts.empty()) {
  365. cmSystemTools::ExpandListArgument(opts, qrc.Options);
  366. }
  367. }
  368. this->Rcc.Qrcs.push_back(std::move(qrc));
  369. }
  370. }
  371. }
  372. // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
  373. // sources meta data cache. Clear it so that OBJECT library targets that
  374. // are AUTOGEN initialized after this target get their added
  375. // mocs_compilation.cpp source acknowledged by this target.
  376. this->Target->ClearSourcesCache();
  377. }
  378. // Read skip files from makefile sources
  379. if (this->Moc.Enabled || this->Uic.Enabled) {
  380. std::string pathError;
  381. for (cmSourceFile* sf : makefile->GetSourceFiles()) {
  382. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  383. // Since we're iterating over source files that might be not in the
  384. // target we need to check for path errors (not existing files).
  385. std::string const& fPath = sf->GetFullPath(&pathError);
  386. if (!pathError.empty()) {
  387. pathError.clear();
  388. continue;
  389. }
  390. cmSystemTools::FileFormat const fileType =
  391. cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
  392. if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
  393. !(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  394. continue;
  395. }
  396. const bool skipAll = sf->GetPropertyAsBool("SKIP_AUTOGEN");
  397. const bool mocSkip = this->Moc.Enabled &&
  398. (skipAll || sf->GetPropertyAsBool("SKIP_AUTOMOC"));
  399. const bool uicSkip = this->Uic.Enabled &&
  400. (skipAll || sf->GetPropertyAsBool("SKIP_AUTOUIC"));
  401. if (mocSkip || uicSkip) {
  402. std::string const absFile = cmSystemTools::GetRealPath(fPath);
  403. if (mocSkip) {
  404. this->Moc.Skip.insert(absFile);
  405. }
  406. if (uicSkip) {
  407. this->Uic.Skip.insert(absFile);
  408. }
  409. }
  410. }
  411. }
  412. // Process GENERATED sources and headers
  413. if (!generatedSources.empty() || !generatedHeaders.empty()) {
  414. // Check status of policy CMP0071
  415. bool policyAccept = false;
  416. bool policyWarn = false;
  417. cmPolicies::PolicyStatus const CMP0071_status =
  418. makefile->GetPolicyStatus(cmPolicies::CMP0071);
  419. switch (CMP0071_status) {
  420. case cmPolicies::WARN:
  421. policyWarn = true;
  422. CM_FALLTHROUGH;
  423. case cmPolicies::OLD:
  424. // Ignore GENERATED file
  425. break;
  426. case cmPolicies::REQUIRED_IF_USED:
  427. case cmPolicies::REQUIRED_ALWAYS:
  428. case cmPolicies::NEW:
  429. // Process GENERATED file
  430. policyAccept = true;
  431. break;
  432. }
  433. if (policyAccept) {
  434. // Accept GENERATED sources
  435. for (std::string const& absFile : generatedHeaders) {
  436. this->Headers.push_back(absFile);
  437. autogenDependFiles.insert(absFile);
  438. }
  439. for (std::string const& absFile : generatedSources) {
  440. this->Sources.push_back(absFile);
  441. autogenDependFiles.insert(absFile);
  442. }
  443. } else {
  444. if (policyWarn) {
  445. std::string msg;
  446. msg += cmPolicies::GetPolicyWarning(cmPolicies::CMP0071);
  447. msg += "\n";
  448. std::string tools;
  449. std::string property;
  450. if (this->Moc.Enabled && this->Uic.Enabled) {
  451. tools = "AUTOMOC and AUTOUIC";
  452. property = "SKIP_AUTOGEN";
  453. } else if (this->Moc.Enabled) {
  454. tools = "AUTOMOC";
  455. property = "SKIP_AUTOMOC";
  456. } else if (this->Uic.Enabled) {
  457. tools = "AUTOUIC";
  458. property = "SKIP_AUTOUIC";
  459. }
  460. msg += "For compatibility, CMake is excluding the GENERATED source "
  461. "file(s):\n";
  462. for (const std::string& absFile : generatedHeaders) {
  463. msg.append(" ").append(Quoted(absFile)).append("\n");
  464. }
  465. for (const std::string& absFile : generatedSources) {
  466. msg.append(" ").append(Quoted(absFile)).append("\n");
  467. }
  468. msg += "from processing by ";
  469. msg += tools;
  470. msg +=
  471. ". If any of the files should be processed, set CMP0071 to NEW. "
  472. "If any of the files should not be processed, "
  473. "explicitly exclude them by setting the source file property ";
  474. msg += property;
  475. msg += ":\n set_property(SOURCE file.h PROPERTY ";
  476. msg += property;
  477. msg += " ON)\n";
  478. makefile->IssueMessage(cmake::AUTHOR_WARNING, msg);
  479. }
  480. }
  481. // Clear lists
  482. generatedSources.clear();
  483. generatedHeaders.clear();
  484. }
  485. // Sort headers and sources
  486. if (this->Moc.Enabled || this->Uic.Enabled) {
  487. std::sort(this->Headers.begin(), this->Headers.end());
  488. std::sort(this->Sources.begin(), this->Sources.end());
  489. }
  490. // Process qrc files
  491. if (!this->Rcc.Qrcs.empty()) {
  492. const bool QtV5 = (this->QtVersionMajor == "5");
  493. // Target rcc options
  494. std::vector<std::string> optionsTarget;
  495. cmSystemTools::ExpandListArgument(
  496. this->Target->GetSafeProperty("AUTORCC_OPTIONS"), optionsTarget);
  497. // Check if file name is unique
  498. for (Qrc& qrc : this->Rcc.Qrcs) {
  499. qrc.Unique = true;
  500. for (Qrc const& qrc2 : this->Rcc.Qrcs) {
  501. if ((&qrc != &qrc2) && (qrc.QrcName == qrc2.QrcName)) {
  502. qrc.Unique = false;
  503. break;
  504. }
  505. }
  506. }
  507. // Path checksum and file names
  508. {
  509. cmFilePathChecksum const fpathCheckSum(makefile);
  510. for (Qrc& qrc : this->Rcc.Qrcs) {
  511. qrc.PathChecksum = fpathCheckSum.getPart(qrc.QrcFile);
  512. // RCC output file name
  513. {
  514. std::string rccFile = this->DirBuild + "/";
  515. rccFile += qrc.PathChecksum;
  516. rccFile += "/qrc_";
  517. rccFile += qrc.QrcName;
  518. rccFile += ".cpp";
  519. qrc.RccFile = std::move(rccFile);
  520. }
  521. {
  522. std::string base = this->DirInfo;
  523. base += "/RCC";
  524. base += qrc.QrcName;
  525. if (!qrc.Unique) {
  526. base += qrc.PathChecksum;
  527. }
  528. qrc.LockFile = base;
  529. qrc.LockFile += ".lock";
  530. qrc.InfoFile = base;
  531. qrc.InfoFile += "Info.cmake";
  532. qrc.SettingsFile = base;
  533. qrc.SettingsFile += "Settings.txt";
  534. }
  535. }
  536. }
  537. // RCC options
  538. for (Qrc& qrc : this->Rcc.Qrcs) {
  539. // Target options
  540. std::vector<std::string> opts = optionsTarget;
  541. // Merge computed "-name XYZ" option
  542. {
  543. std::string name = qrc.QrcName;
  544. // Replace '-' with '_'. The former is not valid for symbol names.
  545. std::replace(name.begin(), name.end(), '-', '_');
  546. if (!qrc.Unique) {
  547. name += "_";
  548. name += qrc.PathChecksum;
  549. }
  550. std::vector<std::string> nameOpts;
  551. nameOpts.emplace_back("-name");
  552. nameOpts.emplace_back(std::move(name));
  553. RccMergeOptions(opts, nameOpts, QtV5);
  554. }
  555. // Merge file option
  556. RccMergeOptions(opts, qrc.Options, QtV5);
  557. qrc.Options = std::move(opts);
  558. }
  559. for (Qrc& qrc : this->Rcc.Qrcs) {
  560. // Register file at target
  561. this->AddGeneratedSource(qrc.RccFile, GeneratorT::RCC);
  562. std::vector<std::string> ccOutput;
  563. ccOutput.push_back(qrc.RccFile);
  564. cmCustomCommandLines commandLines;
  565. if (this->MultiConfig) {
  566. // Build for all configurations
  567. for (std::string const& config : this->ConfigsList) {
  568. cmCustomCommandLine currentLine;
  569. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  570. currentLine.push_back("-E");
  571. currentLine.push_back("cmake_autorcc");
  572. currentLine.push_back(qrc.InfoFile);
  573. currentLine.push_back(config);
  574. commandLines.push_back(std::move(currentLine));
  575. }
  576. } else {
  577. cmCustomCommandLine currentLine;
  578. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  579. currentLine.push_back("-E");
  580. currentLine.push_back("cmake_autorcc");
  581. currentLine.push_back(qrc.InfoFile);
  582. currentLine.push_back("$<CONFIG>");
  583. commandLines.push_back(std::move(currentLine));
  584. }
  585. std::string ccComment = "Automatic RCC for ";
  586. ccComment += FileProjectRelativePath(makefile, qrc.QrcFile);
  587. if (qrc.Generated) {
  588. // Create custom rcc target
  589. std::string ccName;
  590. {
  591. ccName = this->Target->GetName();
  592. ccName += "_arcc_";
  593. ccName += qrc.QrcName;
  594. if (!qrc.Unique) {
  595. ccName += "_";
  596. ccName += qrc.PathChecksum;
  597. }
  598. std::vector<std::string> ccDepends;
  599. // Add the .qrc and info file to the custom target dependencies
  600. ccDepends.push_back(qrc.QrcFile);
  601. ccDepends.push_back(qrc.InfoFile);
  602. cmTarget* autoRccTarget = makefile->AddUtilityCommand(
  603. ccName, cmMakefile::TargetOrigin::Generator, true,
  604. this->DirWork.c_str(), ccOutput, ccDepends, commandLines, false,
  605. ccComment.c_str());
  606. // Create autogen generator target
  607. localGen->AddGeneratorTarget(
  608. new cmGeneratorTarget(autoRccTarget, localGen));
  609. // Set FOLDER property in autogen target
  610. if (!this->AutogenFolder.empty()) {
  611. autoRccTarget->SetProperty("FOLDER", this->AutogenFolder.c_str());
  612. }
  613. }
  614. // Add autogen target to the origin target dependencies
  615. this->Target->Target->AddUtility(ccName, makefile);
  616. } else {
  617. // Create custom rcc command
  618. {
  619. std::vector<std::string> ccByproducts;
  620. std::vector<std::string> ccDepends;
  621. // Add the .qrc and info file to the custom command dependencies
  622. ccDepends.push_back(qrc.QrcFile);
  623. ccDepends.push_back(qrc.InfoFile);
  624. // Add the resource files to the dependencies
  625. {
  626. std::string error;
  627. if (RccListInputs(qrc.QrcFile, qrc.Resources, error)) {
  628. for (std::string const& fileName : qrc.Resources) {
  629. // Add resource file to the custom command dependencies
  630. ccDepends.push_back(fileName);
  631. }
  632. } else {
  633. cmSystemTools::Error(error.c_str());
  634. return false;
  635. }
  636. }
  637. makefile->AddCustomCommandToOutput(ccOutput, ccByproducts, ccDepends,
  638. /*main_dependency*/ std::string(),
  639. commandLines, ccComment.c_str(),
  640. this->DirWork.c_str());
  641. }
  642. // Reconfigure when .qrc file changes
  643. makefile->AddCMakeDependFile(qrc.QrcFile);
  644. }
  645. }
  646. }
  647. // Create _autogen target
  648. if (this->Moc.Enabled || this->Uic.Enabled) {
  649. // Add user defined autogen target dependencies
  650. {
  651. std::string const deps =
  652. this->Target->GetSafeProperty("AUTOGEN_TARGET_DEPENDS");
  653. if (!deps.empty()) {
  654. std::vector<std::string> extraDeps;
  655. cmSystemTools::ExpandListArgument(deps, extraDeps);
  656. for (std::string const& depName : extraDeps) {
  657. // Allow target and file dependencies
  658. auto* depTarget = makefile->FindTargetToUse(depName);
  659. if (depTarget != nullptr) {
  660. autogenDependTargets.insert(depTarget);
  661. } else {
  662. autogenDependFiles.insert(depName);
  663. }
  664. }
  665. }
  666. }
  667. // Compose target comment
  668. std::string autogenComment;
  669. {
  670. std::string tools;
  671. if (this->Moc.Enabled) {
  672. tools += "MOC";
  673. }
  674. if (this->Uic.Enabled) {
  675. if (!tools.empty()) {
  676. tools += " and ";
  677. }
  678. tools += "UIC";
  679. }
  680. autogenComment = "Automatic ";
  681. autogenComment += tools;
  682. autogenComment += " for target ";
  683. autogenComment += this->Target->GetName();
  684. }
  685. // Compose command lines
  686. cmCustomCommandLines commandLines;
  687. {
  688. cmCustomCommandLine currentLine;
  689. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  690. currentLine.push_back("-E");
  691. currentLine.push_back("cmake_autogen");
  692. currentLine.push_back(this->AutogenInfoFile);
  693. currentLine.push_back("$<CONFIGURATION>");
  694. commandLines.push_back(std::move(currentLine));
  695. }
  696. // Use PRE_BUILD on demand
  697. bool usePRE_BUILD = false;
  698. if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
  699. // Under VS use a PRE_BUILD event instead of a separate target to
  700. // reduce the number of targets loaded into the IDE.
  701. // This also works around a VS 11 bug that may skip updating the target:
  702. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  703. usePRE_BUILD = true;
  704. }
  705. // Disable PRE_BUILD in some cases
  706. if (usePRE_BUILD) {
  707. // Cannot use PRE_BUILD with file depends
  708. if (!autogenDependFiles.empty()) {
  709. usePRE_BUILD = false;
  710. }
  711. }
  712. // Create the autogen target/command
  713. if (usePRE_BUILD) {
  714. // Add additional autogen target dependencies to origin target
  715. for (cmTarget* depTarget : autogenDependTargets) {
  716. this->Target->Target->AddUtility(depTarget->GetName(), makefile);
  717. }
  718. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  719. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  720. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  721. //
  722. // PRE_BUILD does not support file dependencies!
  723. const std::vector<std::string> no_output;
  724. const std::vector<std::string> no_deps;
  725. cmCustomCommand cc(makefile, no_output, autogenProvides, no_deps,
  726. commandLines, autogenComment.c_str(),
  727. this->DirWork.c_str());
  728. cc.SetEscapeOldStyle(false);
  729. cc.SetEscapeAllowMakeVars(true);
  730. this->Target->Target->AddPreBuildCommand(cc);
  731. } else {
  732. // Add link library target dependencies to the autogen target
  733. // dependencies
  734. {
  735. // add_dependencies/addUtility do not support generator expressions.
  736. // We depend only on the libraries found in all configs therefore.
  737. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  738. for (std::string const& config : this->ConfigsList) {
  739. cmLinkImplementationLibraries const* libs =
  740. this->Target->GetLinkImplementationLibraries(config);
  741. if (libs != nullptr) {
  742. for (cmLinkItem const& item : libs->Libraries) {
  743. cmGeneratorTarget const* libTarget = item.Target;
  744. if ((libTarget != nullptr) &&
  745. !StaticLibraryCycle(this->Target, libTarget, config)) {
  746. // Increment target config count
  747. commonTargets[libTarget]++;
  748. }
  749. }
  750. }
  751. }
  752. for (auto const& item : commonTargets) {
  753. if (item.second == this->ConfigsList.size()) {
  754. autogenDependTargets.insert(item.first->Target);
  755. }
  756. }
  757. }
  758. // Create autogen target
  759. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  760. this->AutogenTargetName, cmMakefile::TargetOrigin::Generator, true,
  761. this->DirWork.c_str(), /*byproducts=*/autogenProvides,
  762. std::vector<std::string>(autogenDependFiles.begin(),
  763. autogenDependFiles.end()),
  764. commandLines, false, autogenComment.c_str());
  765. // Create autogen generator target
  766. localGen->AddGeneratorTarget(
  767. new cmGeneratorTarget(autogenTarget, localGen));
  768. // Forward origin utilities to autogen target
  769. for (std::string const& depName : this->Target->Target->GetUtilities()) {
  770. autogenTarget->AddUtility(depName, makefile);
  771. }
  772. // Add additional autogen target dependencies to autogen target
  773. for (cmTarget* depTarget : autogenDependTargets) {
  774. autogenTarget->AddUtility(depTarget->GetName(), makefile);
  775. }
  776. // Set FOLDER property in autogen target
  777. if (!this->AutogenFolder.empty()) {
  778. autogenTarget->SetProperty("FOLDER", this->AutogenFolder.c_str());
  779. }
  780. // Add autogen target to the origin target dependencies
  781. this->Target->Target->AddUtility(this->AutogenTargetName, makefile);
  782. }
  783. }
  784. return true;
  785. }
  786. bool cmQtAutoGenInitializer::SetupCustomTargets()
  787. {
  788. // Create info directory on demand
  789. if (!cmSystemTools::MakeDirectory(this->DirInfo)) {
  790. std::string emsg = ("AutoGen: Could not create directory: ");
  791. emsg += Quoted(this->DirInfo);
  792. cmSystemTools::Error(emsg.c_str());
  793. return false;
  794. }
  795. // Generate autogen target info file
  796. if (this->Moc.Enabled || this->Uic.Enabled) {
  797. if (this->Moc.Enabled) {
  798. this->SetupCustomTargetsMoc();
  799. }
  800. if (this->Uic.Enabled) {
  801. this->SetupCustomTargetsUic();
  802. }
  803. // Write autogen target info files
  804. if (!this->SetupWriteAutogenInfo()) {
  805. return false;
  806. }
  807. }
  808. // Write AUTORCC info files
  809. if (this->Rcc.Enabled && !this->SetupWriteRccInfo()) {
  810. return false;
  811. }
  812. return true;
  813. }
  814. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  815. {
  816. cmMakefile* makefile = this->Target->Target->GetMakefile();
  817. cmGeneratedFileStream ofs;
  818. ofs.SetCopyIfDifferent(true);
  819. ofs.Open(this->AutogenInfoFile.c_str(), false, true);
  820. if (ofs) {
  821. // Utility lambdas
  822. auto CWrite = [&ofs](const char* key, std::string const& value) {
  823. ofs << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
  824. << ")\n";
  825. };
  826. auto CWriteList = [&CWrite](const char* key,
  827. std::vector<std::string> const& list) {
  828. CWrite(key, cmJoin(list, ";"));
  829. };
  830. auto CWriteNestedLists =
  831. [&CWrite](const char* key,
  832. std::vector<std::vector<std::string>> const& lists) {
  833. std::vector<std::string> seplist;
  834. for (const std::vector<std::string>& list : lists) {
  835. std::string blist = "{";
  836. blist += cmJoin(list, ";");
  837. blist += "}";
  838. seplist.push_back(std::move(blist));
  839. }
  840. CWrite(key, cmJoin(seplist, cmQtAutoGen::ListSep));
  841. };
  842. auto CWriteSet = [&CWrite](const char* key,
  843. std::set<std::string> const& list) {
  844. CWrite(key, cmJoin(list, ";"));
  845. };
  846. auto CWriteMap = [&ofs](const char* key,
  847. std::map<std::string, std::string> const& map) {
  848. for (auto const& item : map) {
  849. ofs << "set(" << key << "_" << item.first << " "
  850. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  851. }
  852. };
  853. auto MfDef = [makefile](const char* key) {
  854. return std::string(makefile->GetSafeDefinition(key));
  855. };
  856. // Write
  857. ofs << "# Meta\n";
  858. CWrite("AM_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  859. CWrite("AM_PARALLEL", this->Parallel);
  860. CWrite("AM_VERBOSITY", this->Verbosity);
  861. ofs << "# Directories\n";
  862. CWrite("AM_CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  863. CWrite("AM_CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  864. CWrite("AM_CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  865. CWrite("AM_CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  866. CWrite("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE",
  867. MfDef("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"));
  868. CWrite("AM_BUILD_DIR", this->DirBuild);
  869. if (this->MultiConfig) {
  870. CWriteMap("AM_INCLUDE_DIR", this->DirConfigInclude);
  871. } else {
  872. CWrite("AM_INCLUDE_DIR", this->DirInclude);
  873. }
  874. ofs << "# Files\n";
  875. CWriteList("AM_SOURCES", this->Sources);
  876. CWriteList("AM_HEADERS", this->Headers);
  877. if (this->MultiConfig) {
  878. std::map<std::string, std::string> settingsFiles;
  879. for (std::string const& cfg : this->ConfigsList) {
  880. settingsFiles[cfg] =
  881. AppendFilenameSuffix(this->AutogenSettingsFile, "_" + cfg);
  882. }
  883. CWriteMap("AM_SETTINGS_FILE", settingsFiles);
  884. } else {
  885. CWrite("AM_SETTINGS_FILE", this->AutogenSettingsFile);
  886. }
  887. ofs << "# Qt\n";
  888. CWrite("AM_QT_VERSION_MAJOR", this->QtVersionMajor);
  889. CWrite("AM_QT_MOC_EXECUTABLE", this->Moc.Executable);
  890. CWrite("AM_QT_UIC_EXECUTABLE", this->Uic.Executable);
  891. if (this->Moc.Enabled) {
  892. ofs << "# MOC settings\n";
  893. CWriteSet("AM_MOC_SKIP", this->Moc.Skip);
  894. CWrite("AM_MOC_DEFINITIONS", this->Moc.Defines);
  895. CWriteMap("AM_MOC_DEFINITIONS", this->Moc.ConfigDefines);
  896. CWrite("AM_MOC_INCLUDES", this->Moc.Includes);
  897. CWriteMap("AM_MOC_INCLUDES", this->Moc.ConfigIncludes);
  898. CWrite("AM_MOC_OPTIONS",
  899. this->Target->GetSafeProperty("AUTOMOC_MOC_OPTIONS"));
  900. CWrite("AM_MOC_RELAXED_MODE", MfDef("CMAKE_AUTOMOC_RELAXED_MODE"));
  901. CWrite("AM_MOC_MACRO_NAMES",
  902. this->Target->GetSafeProperty("AUTOMOC_MACRO_NAMES"));
  903. CWrite("AM_MOC_DEPEND_FILTERS",
  904. this->Target->GetSafeProperty("AUTOMOC_DEPEND_FILTERS"));
  905. CWrite("AM_MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  906. }
  907. if (this->Uic.Enabled) {
  908. ofs << "# UIC settings\n";
  909. CWriteSet("AM_UIC_SKIP", this->Uic.Skip);
  910. CWrite("AM_UIC_TARGET_OPTIONS", this->Uic.Options);
  911. CWriteMap("AM_UIC_TARGET_OPTIONS", this->Uic.ConfigOptions);
  912. CWriteList("AM_UIC_OPTIONS_FILES", this->Uic.FileFiles);
  913. CWriteNestedLists("AM_UIC_OPTIONS_OPTIONS", this->Uic.FileOptions);
  914. CWriteList("AM_UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  915. }
  916. } else {
  917. std::string err = "AutoGen: Could not write file ";
  918. err += this->AutogenInfoFile;
  919. cmSystemTools::Error(err.c_str());
  920. return false;
  921. }
  922. return true;
  923. }
  924. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  925. {
  926. cmMakefile* makefile = this->Target->Target->GetMakefile();
  927. for (Qrc const& qrc : this->Rcc.Qrcs) {
  928. // Register rcc info file as generated
  929. makefile->AddCMakeOutputFile(qrc.InfoFile);
  930. cmGeneratedFileStream ofs;
  931. ofs.SetCopyIfDifferent(true);
  932. ofs.Open(qrc.InfoFile.c_str(), false, true);
  933. if (ofs) {
  934. // Utility lambdas
  935. auto CWrite = [&ofs](const char* key, std::string const& value) {
  936. ofs << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
  937. << ")\n";
  938. };
  939. auto CWriteMap = [&ofs](const char* key,
  940. std::map<std::string, std::string> const& map) {
  941. for (auto const& item : map) {
  942. ofs << "set(" << key << "_" << item.first << " "
  943. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  944. }
  945. };
  946. // Write
  947. ofs << "# Configurations\n";
  948. CWrite("ARCC_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  949. CWrite("ARCC_VERBOSITY", this->Verbosity);
  950. ofs << "# Settings file\n";
  951. if (this->MultiConfig) {
  952. std::map<std::string, std::string> settingsFiles;
  953. for (std::string const& cfg : this->ConfigsList) {
  954. settingsFiles[cfg] =
  955. AppendFilenameSuffix(qrc.SettingsFile, "_" + cfg);
  956. }
  957. CWriteMap("ARCC_SETTINGS_FILE", settingsFiles);
  958. } else {
  959. CWrite("ARCC_SETTINGS_FILE", qrc.SettingsFile);
  960. }
  961. ofs << "# Directories\n";
  962. CWrite("ARCC_BUILD_DIR", this->DirBuild);
  963. if (this->MultiConfig) {
  964. CWriteMap("ARCC_INCLUDE_DIR", this->DirConfigInclude);
  965. } else {
  966. CWrite("ARCC_INCLUDE_DIR", this->DirInclude);
  967. }
  968. ofs << "# Rcc executable\n";
  969. CWrite("ARCC_RCC_EXECUTABLE", this->Rcc.Executable);
  970. CWrite("ARCC_RCC_LIST_OPTIONS", cmJoin(this->Rcc.ListOptions, ";"));
  971. ofs << "# Rcc job\n";
  972. CWrite("ARCC_LOCK_FILE", qrc.LockFile);
  973. CWrite("ARCC_SOURCE", qrc.QrcFile);
  974. CWrite("ARCC_OUTPUT_CHECKSUM", qrc.PathChecksum);
  975. CWrite("ARCC_OUTPUT_NAME", cmSystemTools::GetFilenameName(qrc.RccFile));
  976. CWrite("ARCC_OPTIONS", cmJoin(qrc.Options, ";"));
  977. CWrite("ARCC_INPUTS", cmJoin(qrc.Resources, ";"));
  978. } else {
  979. std::string err = "AutoRcc: Could not write file ";
  980. err += qrc.InfoFile;
  981. cmSystemTools::Error(err.c_str());
  982. return false;
  983. }
  984. }
  985. return true;
  986. }
  987. bool cmQtAutoGenInitializer::SetupCustomTargetsMoc()
  988. {
  989. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  990. cmMakefile* makefile = this->Target->Target->GetMakefile();
  991. // Moc predefs command
  992. if (this->Target->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES") &&
  993. this->QtVersionGreaterOrEqual(5, 8)) {
  994. this->Moc.PredefsCmd =
  995. makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND");
  996. }
  997. // Moc includes and compile definitions
  998. {
  999. auto GetIncludeDirs = [this,
  1000. localGen](std::string const& cfg) -> std::string {
  1001. // Get the include dirs for this target, without stripping the implicit
  1002. // include dirs off, see
  1003. // https://gitlab.kitware.com/cmake/cmake/issues/13667
  1004. std::vector<std::string> dirs;
  1005. localGen->GetIncludeDirectories(dirs, this->Target, "CXX", cfg, false);
  1006. return cmJoin(dirs, ";");
  1007. };
  1008. // Default configuration include directories
  1009. this->Moc.Includes = GetIncludeDirs(this->ConfigDefault);
  1010. // Other configuration settings
  1011. for (std::string const& cfg : this->ConfigsList) {
  1012. std::string configIncludeDirs = GetIncludeDirs(cfg);
  1013. if (configIncludeDirs != this->Moc.Includes) {
  1014. this->Moc.ConfigIncludes[cfg] = std::move(configIncludeDirs);
  1015. }
  1016. }
  1017. }
  1018. // Moc compile definitions
  1019. {
  1020. auto GetCompileDefinitions =
  1021. [this, localGen](std::string const& cfg) -> std::string {
  1022. std::set<std::string> defines;
  1023. localGen->AddCompileDefinitions(defines, this->Target, cfg, "CXX");
  1024. return cmJoin(defines, ";");
  1025. };
  1026. // Default configuration defines
  1027. this->Moc.Defines = GetCompileDefinitions(this->ConfigDefault);
  1028. // Other configuration defines
  1029. for (std::string const& cfg : this->ConfigsList) {
  1030. std::string configCompileDefs = GetCompileDefinitions(cfg);
  1031. if (configCompileDefs != this->Moc.Defines) {
  1032. this->Moc.ConfigDefines[cfg] = std::move(configCompileDefs);
  1033. }
  1034. }
  1035. }
  1036. // Moc executable
  1037. if (!GetMocExecutable()) {
  1038. return false;
  1039. }
  1040. return true;
  1041. }
  1042. bool cmQtAutoGenInitializer::SetupCustomTargetsUic()
  1043. {
  1044. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1045. // Uic search paths
  1046. {
  1047. std::string const usp =
  1048. this->Target->GetSafeProperty("AUTOUIC_SEARCH_PATHS");
  1049. if (!usp.empty()) {
  1050. cmSystemTools::ExpandListArgument(usp, this->Uic.SearchPaths);
  1051. std::string const srcDir = makefile->GetCurrentSourceDirectory();
  1052. for (std::string& path : this->Uic.SearchPaths) {
  1053. path = cmSystemTools::CollapseFullPath(path, srcDir);
  1054. }
  1055. }
  1056. }
  1057. // Uic target options
  1058. {
  1059. auto UicGetOpts = [this](std::string const& cfg) -> std::string {
  1060. std::vector<std::string> opts;
  1061. this->Target->GetAutoUicOptions(opts, cfg);
  1062. return cmJoin(opts, ";");
  1063. };
  1064. // Default settings
  1065. this->Uic.Options = UicGetOpts(this->ConfigDefault);
  1066. // Configuration specific settings
  1067. for (std::string const& cfg : this->ConfigsList) {
  1068. std::string const configUicOpts = UicGetOpts(cfg);
  1069. if (configUicOpts != this->Uic.Options) {
  1070. this->Uic.ConfigOptions[cfg] = configUicOpts;
  1071. }
  1072. }
  1073. }
  1074. // .ui files skip and options
  1075. {
  1076. std::string const uiExt = "ui";
  1077. std::string pathError;
  1078. for (cmSourceFile* sf : makefile->GetSourceFiles()) {
  1079. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  1080. // Since we're iterating over source files that might be not in the
  1081. // target we need to check for path errors (not existing files).
  1082. std::string const& fPath = sf->GetFullPath(&pathError);
  1083. if (!pathError.empty()) {
  1084. pathError.clear();
  1085. continue;
  1086. }
  1087. if (sf->GetExtension() == uiExt) {
  1088. std::string const absFile = cmSystemTools::GetRealPath(fPath);
  1089. // Check if the .ui file should be skipped
  1090. if (sf->GetPropertyAsBool("SKIP_AUTOUIC") ||
  1091. sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  1092. this->Uic.Skip.insert(absFile);
  1093. }
  1094. // Check if the .ui file has uic options
  1095. std::string const uicOpts = sf->GetSafeProperty("AUTOUIC_OPTIONS");
  1096. if (!uicOpts.empty()) {
  1097. // Check if file isn't skipped
  1098. if (this->Uic.Skip.count(absFile) == 0) {
  1099. this->Uic.FileFiles.push_back(absFile);
  1100. std::vector<std::string> optsVec;
  1101. cmSystemTools::ExpandListArgument(uicOpts, optsVec);
  1102. this->Uic.FileOptions.push_back(std::move(optsVec));
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. if (!GetUicExecutable()) {
  1109. return false;
  1110. }
  1111. return true;
  1112. }
  1113. void cmQtAutoGenInitializer::AddGeneratedSource(std::string const& filename,
  1114. GeneratorT genType)
  1115. {
  1116. // Register source file in makefile
  1117. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1118. {
  1119. cmSourceFile* gFile = makefile->GetOrCreateSource(filename, true);
  1120. gFile->SetProperty("GENERATED", "1");
  1121. gFile->SetProperty("SKIP_AUTOGEN", "On");
  1122. }
  1123. // Add source file to source group
  1124. AddToSourceGroup(makefile, filename, genType);
  1125. // Add source file to target
  1126. this->Target->AddSource(filename);
  1127. }
  1128. std::string cmQtAutoGenInitializer::GetQtMajorVersion(
  1129. cmGeneratorTarget const* target)
  1130. {
  1131. cmMakefile* makefile = target->Target->GetMakefile();
  1132. std::string qtMajor = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  1133. if (qtMajor.empty()) {
  1134. qtMajor = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  1135. }
  1136. const char* targetQtVersion =
  1137. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "");
  1138. if (targetQtVersion != nullptr) {
  1139. qtMajor = targetQtVersion;
  1140. }
  1141. return qtMajor;
  1142. }
  1143. std::string cmQtAutoGenInitializer::GetQtMinorVersion(
  1144. cmGeneratorTarget const* target, std::string const& qtVersionMajor)
  1145. {
  1146. cmMakefile* makefile = target->Target->GetMakefile();
  1147. std::string qtMinor;
  1148. if (qtVersionMajor == "5") {
  1149. qtMinor = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR");
  1150. }
  1151. if (qtMinor.empty()) {
  1152. qtMinor = makefile->GetSafeDefinition("QT_VERSION_MINOR");
  1153. }
  1154. const char* targetQtVersion =
  1155. target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION", "");
  1156. if (targetQtVersion != nullptr) {
  1157. qtMinor = targetQtVersion;
  1158. }
  1159. return qtMinor;
  1160. }
  1161. bool cmQtAutoGenInitializer::QtVersionGreaterOrEqual(
  1162. unsigned long requestMajor, unsigned long requestMinor) const
  1163. {
  1164. unsigned long majorUL(0);
  1165. unsigned long minorUL(0);
  1166. if (cmSystemTools::StringToULong(this->QtVersionMajor.c_str(), &majorUL) &&
  1167. cmSystemTools::StringToULong(this->QtVersionMinor.c_str(), &minorUL)) {
  1168. return (majorUL > requestMajor) ||
  1169. (majorUL == requestMajor && minorUL >= requestMinor);
  1170. }
  1171. return false;
  1172. }
  1173. bool cmQtAutoGenInitializer::GetMocExecutable()
  1174. {
  1175. std::string err;
  1176. // Find moc executable
  1177. {
  1178. std::string targetName;
  1179. if (this->QtVersionMajor == "5") {
  1180. targetName = "Qt5::moc";
  1181. } else if (QtVersionMajor == "4") {
  1182. targetName = "Qt4::moc";
  1183. } else {
  1184. err = "The AUTOMOC feature supports only Qt 4 and Qt 5";
  1185. }
  1186. if (!targetName.empty()) {
  1187. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1188. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
  1189. if (tgt != nullptr) {
  1190. this->Moc.Executable = tgt->ImportedGetLocation("");
  1191. } else {
  1192. err = "Could not find target " + targetName;
  1193. }
  1194. }
  1195. }
  1196. // Test moc command
  1197. if (err.empty()) {
  1198. if (cmSystemTools::FileExists(this->Moc.Executable, true)) {
  1199. std::vector<std::string> command;
  1200. command.push_back(this->Moc.Executable);
  1201. command.push_back("-h");
  1202. std::string stdOut;
  1203. std::string stdErr;
  1204. int retVal = 0;
  1205. bool result = cmSystemTools::RunSingleCommand(
  1206. command, &stdOut, &stdErr, &retVal, nullptr,
  1207. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1208. if (!result) {
  1209. err = "The moc test command failed: ";
  1210. err += QuotedCommand(command);
  1211. }
  1212. } else {
  1213. err = "The moc executable ";
  1214. err += Quoted(this->Moc.Executable);
  1215. err += " does not exist";
  1216. }
  1217. }
  1218. // Print error
  1219. if (!err.empty()) {
  1220. std::string msg = "AutoMoc (";
  1221. msg += this->Target->GetName();
  1222. msg += "): ";
  1223. msg += err;
  1224. cmSystemTools::Error(msg.c_str());
  1225. return false;
  1226. }
  1227. return true;
  1228. }
  1229. bool cmQtAutoGenInitializer::GetUicExecutable()
  1230. {
  1231. std::string err;
  1232. // Find uic executable
  1233. {
  1234. std::string targetName;
  1235. if (this->QtVersionMajor == "5") {
  1236. targetName = "Qt5::uic";
  1237. } else if (QtVersionMajor == "4") {
  1238. targetName = "Qt4::uic";
  1239. } else {
  1240. err = "The AUTOUIC feature supports only Qt 4 and Qt 5";
  1241. }
  1242. if (!targetName.empty()) {
  1243. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1244. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
  1245. if (tgt != nullptr) {
  1246. this->Uic.Executable = tgt->ImportedGetLocation("");
  1247. } else {
  1248. if (this->QtVersionMajor == "5") {
  1249. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  1250. } else {
  1251. err = "Could not find target " + targetName;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. // Test uic command
  1257. if (err.empty()) {
  1258. if (cmSystemTools::FileExists(this->Uic.Executable, true)) {
  1259. std::vector<std::string> command;
  1260. command.push_back(this->Uic.Executable);
  1261. command.push_back("-h");
  1262. std::string stdOut;
  1263. std::string stdErr;
  1264. int retVal = 0;
  1265. bool result = cmSystemTools::RunSingleCommand(
  1266. command, &stdOut, &stdErr, &retVal, nullptr,
  1267. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1268. if (!result) {
  1269. err = "The uic test command failed: ";
  1270. err += QuotedCommand(command);
  1271. }
  1272. } else {
  1273. err = "The uic executable ";
  1274. err += Quoted(this->Uic.Executable);
  1275. err += " does not exist";
  1276. }
  1277. }
  1278. // Print error
  1279. if (!err.empty()) {
  1280. std::string msg = "AutoUic (";
  1281. msg += this->Target->GetName();
  1282. msg += "): ";
  1283. msg += err;
  1284. cmSystemTools::Error(msg.c_str());
  1285. return false;
  1286. }
  1287. return true;
  1288. }
  1289. bool cmQtAutoGenInitializer::GetRccExecutable()
  1290. {
  1291. std::string err;
  1292. // Find rcc executable
  1293. {
  1294. std::string targetName;
  1295. if (this->QtVersionMajor == "5") {
  1296. targetName = "Qt5::rcc";
  1297. } else if (QtVersionMajor == "4") {
  1298. targetName = "Qt4::rcc";
  1299. } else {
  1300. err = "The AUTORCC feature supports only Qt 4 and Qt 5";
  1301. }
  1302. if (!targetName.empty()) {
  1303. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1304. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
  1305. if (tgt != nullptr) {
  1306. this->Rcc.Executable = tgt->ImportedGetLocation("");
  1307. } else {
  1308. err = "Could not find target " + targetName;
  1309. }
  1310. }
  1311. }
  1312. // Test rcc command
  1313. if (err.empty()) {
  1314. if (cmSystemTools::FileExists(this->Rcc.Executable, true)) {
  1315. std::vector<std::string> command;
  1316. command.push_back(this->Rcc.Executable);
  1317. command.push_back("-h");
  1318. std::string stdOut;
  1319. std::string stdErr;
  1320. int retVal = 0;
  1321. bool result = cmSystemTools::RunSingleCommand(
  1322. command, &stdOut, &stdErr, &retVal, nullptr,
  1323. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1324. if (result) {
  1325. // Detect if rcc supports (-)-list
  1326. if (this->QtVersionMajor == "5") {
  1327. if (stdOut.find("--list") != std::string::npos) {
  1328. this->Rcc.ListOptions.push_back("--list");
  1329. } else {
  1330. this->Rcc.ListOptions.push_back("-list");
  1331. }
  1332. }
  1333. } else {
  1334. err = "The rcc test command failed: ";
  1335. err += QuotedCommand(command);
  1336. }
  1337. } else {
  1338. err = "The rcc executable ";
  1339. err += Quoted(this->Rcc.Executable);
  1340. err += " does not exist";
  1341. }
  1342. }
  1343. // Print error
  1344. if (!err.empty()) {
  1345. std::string msg = "AutoRcc (";
  1346. msg += this->Target->GetName();
  1347. msg += "): ";
  1348. msg += err;
  1349. cmSystemTools::Error(msg.c_str());
  1350. return false;
  1351. }
  1352. return true;
  1353. }
  1354. /// @brief Reads the resource files list from from a .qrc file
  1355. /// @arg fileName Must be the absolute path of the .qrc file
  1356. /// @return True if the rcc file was successfully read
  1357. bool cmQtAutoGenInitializer::RccListInputs(std::string const& fileName,
  1358. std::vector<std::string>& files,
  1359. std::string& error)
  1360. {
  1361. if (!cmSystemTools::FileExists(fileName)) {
  1362. error = "rcc resource file does not exist:\n ";
  1363. error += Quoted(fileName);
  1364. error += "\n";
  1365. return false;
  1366. }
  1367. if (!this->Rcc.ListOptions.empty()) {
  1368. // Use rcc for file listing
  1369. if (this->Rcc.Executable.empty()) {
  1370. error = "rcc executable not available";
  1371. return false;
  1372. }
  1373. // Run rcc list command in the directory of the qrc file with the
  1374. // pathless
  1375. // qrc file name argument. This way rcc prints relative paths.
  1376. // This avoids issues on Windows when the qrc file is in a path that
  1377. // contains non-ASCII characters.
  1378. std::string const fileDir = cmSystemTools::GetFilenamePath(fileName);
  1379. std::string const fileNameName = cmSystemTools::GetFilenameName(fileName);
  1380. bool result = false;
  1381. int retVal = 0;
  1382. std::string rccStdOut;
  1383. std::string rccStdErr;
  1384. {
  1385. std::vector<std::string> cmd;
  1386. cmd.push_back(this->Rcc.Executable);
  1387. cmd.insert(cmd.end(), this->Rcc.ListOptions.begin(),
  1388. this->Rcc.ListOptions.end());
  1389. cmd.push_back(fileNameName);
  1390. result = cmSystemTools::RunSingleCommand(
  1391. cmd, &rccStdOut, &rccStdErr, &retVal, fileDir.c_str(),
  1392. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1393. }
  1394. if (!result || retVal) {
  1395. error = "rcc list process failed for:\n ";
  1396. error += Quoted(fileName);
  1397. error += "\n";
  1398. error += rccStdOut;
  1399. error += "\n";
  1400. error += rccStdErr;
  1401. error += "\n";
  1402. return false;
  1403. }
  1404. if (!RccListParseOutput(rccStdOut, rccStdErr, files, error)) {
  1405. return false;
  1406. }
  1407. } else {
  1408. // We can't use rcc for the file listing.
  1409. // Read the qrc file content into string and parse it.
  1410. {
  1411. std::string qrcContents;
  1412. {
  1413. cmsys::ifstream ifs(fileName.c_str());
  1414. if (ifs) {
  1415. std::ostringstream osst;
  1416. osst << ifs.rdbuf();
  1417. qrcContents = osst.str();
  1418. } else {
  1419. error = "rcc file not readable:\n ";
  1420. error += Quoted(fileName);
  1421. error += "\n";
  1422. return false;
  1423. }
  1424. }
  1425. // Parse string content
  1426. RccListParseContent(qrcContents, files);
  1427. }
  1428. }
  1429. // Convert relative paths to absolute paths
  1430. RccListConvertFullPath(cmSystemTools::GetFilenamePath(fileName), files);
  1431. return true;
  1432. }