cmQtAutoGenInitializer.cxx 52 KB

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