cmQtAutoGenInitializer.cxx 47 KB

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