1
0

cmQtAutoGenInitializer.cxx 51 KB

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