cmQtAutoGenInitializer.cxx 52 KB

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