cmQtAutoGenInitializer.cxx 52 KB

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