cmQtAutoGenInitializer.cxx 51 KB

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