cmQtAutoGenInitializer.cxx 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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.LockFile = base;
  559. qrc.LockFile += ".lock";
  560. qrc.InfoFile = base;
  561. qrc.InfoFile += "Info.cmake";
  562. qrc.SettingsFile = base;
  563. qrc.SettingsFile += "Settings.txt";
  564. }
  565. }
  566. }
  567. // RCC options
  568. for (Qrc& qrc : this->Qrcs) {
  569. // Target options
  570. std::vector<std::string> opts = optionsTarget;
  571. // Merge computed "-name XYZ" option
  572. {
  573. std::string name = qrc.QrcName;
  574. // Replace '-' with '_'. The former is not valid for symbol names.
  575. std::replace(name.begin(), name.end(), '-', '_');
  576. if (!qrc.Unique) {
  577. name += "_";
  578. name += qrc.PathChecksum;
  579. }
  580. std::vector<std::string> nameOpts;
  581. nameOpts.emplace_back("-name");
  582. nameOpts.emplace_back(std::move(name));
  583. RccMergeOptions(opts, nameOpts, QtV5);
  584. }
  585. // Merge file option
  586. RccMergeOptions(opts, qrc.Options, QtV5);
  587. qrc.Options = std::move(opts);
  588. }
  589. for (Qrc& qrc : this->Qrcs) {
  590. // Register file at target
  591. this->AddGeneratedSource(qrc.RccFile, GeneratorT::RCC);
  592. std::vector<std::string> ccOutput;
  593. ccOutput.push_back(qrc.RccFile);
  594. cmCustomCommandLines commandLines;
  595. if (this->MultiConfig) {
  596. // Build for all configurations
  597. for (std::string const& config : this->ConfigsList) {
  598. cmCustomCommandLine currentLine;
  599. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  600. currentLine.push_back("-E");
  601. currentLine.push_back("cmake_autorcc");
  602. currentLine.push_back(qrc.InfoFile);
  603. currentLine.push_back(config);
  604. commandLines.push_back(std::move(currentLine));
  605. }
  606. } else {
  607. cmCustomCommandLine currentLine;
  608. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  609. currentLine.push_back("-E");
  610. currentLine.push_back("cmake_autorcc");
  611. currentLine.push_back(qrc.InfoFile);
  612. currentLine.push_back("$<CONFIG>");
  613. commandLines.push_back(std::move(currentLine));
  614. }
  615. std::string ccComment = "Automatic RCC for ";
  616. ccComment += FileProjectRelativePath(makefile, qrc.QrcFile);
  617. if (qrc.Generated) {
  618. // Create custom rcc target
  619. std::string ccName;
  620. {
  621. ccName = this->Target->GetName();
  622. ccName += "_arcc_";
  623. ccName += qrc.QrcName;
  624. if (!qrc.Unique) {
  625. ccName += "_";
  626. ccName += qrc.PathChecksum;
  627. }
  628. std::vector<std::string> ccDepends;
  629. // Add the .qrc and info file to the custom target dependencies
  630. ccDepends.push_back(qrc.QrcFile);
  631. ccDepends.push_back(qrc.InfoFile);
  632. cmTarget* autoRccTarget = makefile->AddUtilityCommand(
  633. ccName, cmMakefile::TargetOrigin::Generator, true,
  634. this->DirWork.c_str(), ccOutput, ccDepends, commandLines, false,
  635. ccComment.c_str());
  636. // Create autogen generator target
  637. localGen->AddGeneratorTarget(
  638. new cmGeneratorTarget(autoRccTarget, localGen));
  639. // Set FOLDER property in autogen target
  640. if (!this->AutogenFolder.empty()) {
  641. autoRccTarget->SetProperty("FOLDER", this->AutogenFolder.c_str());
  642. }
  643. }
  644. // Add autogen target to the origin target dependencies
  645. this->Target->Target->AddUtility(ccName, makefile);
  646. } else {
  647. // Create custom rcc command
  648. {
  649. std::vector<std::string> ccByproducts;
  650. std::vector<std::string> ccDepends;
  651. // Add the .qrc and info file to the custom command dependencies
  652. ccDepends.push_back(qrc.QrcFile);
  653. ccDepends.push_back(qrc.InfoFile);
  654. // Add the resource files to the dependencies
  655. {
  656. std::string error;
  657. if (RccListInputs(qrc.QrcFile, qrc.Resources, error)) {
  658. for (std::string const& fileName : qrc.Resources) {
  659. // Add resource file to the custom command dependencies
  660. ccDepends.push_back(fileName);
  661. }
  662. } else {
  663. cmSystemTools::Error(error.c_str());
  664. }
  665. }
  666. makefile->AddCustomCommandToOutput(ccOutput, ccByproducts, ccDepends,
  667. /*main_dependency*/ std::string(),
  668. commandLines, ccComment.c_str(),
  669. this->DirWork.c_str());
  670. }
  671. // Reconfigure when .qrc file changes
  672. makefile->AddCMakeDependFile(qrc.QrcFile);
  673. }
  674. }
  675. }
  676. // Create _autogen target
  677. if (this->MocEnabled || this->UicEnabled) {
  678. // Add user defined autogen target dependencies
  679. {
  680. std::string const deps =
  681. GetSafeProperty(this->Target, "AUTOGEN_TARGET_DEPENDS");
  682. if (!deps.empty()) {
  683. std::vector<std::string> extraDeps;
  684. cmSystemTools::ExpandListArgument(deps, extraDeps);
  685. for (std::string const& depName : extraDeps) {
  686. // Allow target and file dependencies
  687. auto* depTarget = makefile->FindTargetToUse(depName);
  688. if (depTarget != nullptr) {
  689. autogenDependTargets.insert(depTarget);
  690. } else {
  691. autogenDependFiles.insert(depName);
  692. }
  693. }
  694. }
  695. }
  696. // Compose target comment
  697. std::string autogenComment;
  698. {
  699. std::string tools;
  700. if (this->MocEnabled) {
  701. tools += "MOC";
  702. }
  703. if (this->UicEnabled) {
  704. if (!tools.empty()) {
  705. tools += " and ";
  706. }
  707. tools += "UIC";
  708. }
  709. autogenComment = "Automatic ";
  710. autogenComment += tools;
  711. autogenComment += " for target ";
  712. autogenComment += this->Target->GetName();
  713. }
  714. // Compose command lines
  715. cmCustomCommandLines commandLines;
  716. {
  717. cmCustomCommandLine currentLine;
  718. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  719. currentLine.push_back("-E");
  720. currentLine.push_back("cmake_autogen");
  721. currentLine.push_back(this->AutogenInfoFile);
  722. currentLine.push_back("$<CONFIGURATION>");
  723. commandLines.push_back(std::move(currentLine));
  724. }
  725. // Use PRE_BUILD on demand
  726. bool usePRE_BUILD = false;
  727. if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
  728. // Under VS use a PRE_BUILD event instead of a separate target to
  729. // reduce the number of targets loaded into the IDE.
  730. // This also works around a VS 11 bug that may skip updating the target:
  731. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  732. usePRE_BUILD = true;
  733. }
  734. // Disable PRE_BUILD in some cases
  735. if (usePRE_BUILD) {
  736. // Cannot use PRE_BUILD with file depends
  737. if (!autogenDependFiles.empty()) {
  738. usePRE_BUILD = false;
  739. }
  740. }
  741. // Create the autogen target/command
  742. if (usePRE_BUILD) {
  743. // Add additional autogen target dependencies to origin target
  744. for (cmTarget* depTarget : autogenDependTargets) {
  745. this->Target->Target->AddUtility(depTarget->GetName(), makefile);
  746. }
  747. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  748. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  749. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  750. //
  751. // PRE_BUILD does not support file dependencies!
  752. const std::vector<std::string> no_output;
  753. const std::vector<std::string> no_deps;
  754. cmCustomCommand cc(makefile, no_output, autogenProvides, no_deps,
  755. commandLines, autogenComment.c_str(),
  756. this->DirWork.c_str());
  757. cc.SetEscapeOldStyle(false);
  758. cc.SetEscapeAllowMakeVars(true);
  759. this->Target->Target->AddPreBuildCommand(cc);
  760. } else {
  761. // Add link library target dependencies to the autogen target
  762. // dependencies
  763. {
  764. // add_dependencies/addUtility do not support generator expressions.
  765. // We depend only on the libraries found in all configs therefore.
  766. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  767. for (std::string const& config : this->ConfigsList) {
  768. cmLinkImplementationLibraries const* libs =
  769. this->Target->GetLinkImplementationLibraries(config);
  770. if (libs != nullptr) {
  771. for (cmLinkItem const& item : libs->Libraries) {
  772. cmGeneratorTarget const* libTarget = item.Target;
  773. if ((libTarget != nullptr) &&
  774. !StaticLibraryCycle(this->Target, libTarget, config)) {
  775. // Increment target config count
  776. commonTargets[libTarget]++;
  777. }
  778. }
  779. }
  780. }
  781. for (auto const& item : commonTargets) {
  782. if (item.second == this->ConfigsList.size()) {
  783. autogenDependTargets.insert(item.first->Target);
  784. }
  785. }
  786. }
  787. // Create autogen target
  788. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  789. this->AutogenTargetName, cmMakefile::TargetOrigin::Generator, true,
  790. this->DirWork.c_str(), /*byproducts=*/autogenProvides,
  791. std::vector<std::string>(autogenDependFiles.begin(),
  792. autogenDependFiles.end()),
  793. commandLines, false, autogenComment.c_str());
  794. // Create autogen generator target
  795. localGen->AddGeneratorTarget(
  796. new cmGeneratorTarget(autogenTarget, localGen));
  797. // Forward origin utilities to autogen target
  798. for (std::string const& depName : this->Target->Target->GetUtilities()) {
  799. autogenTarget->AddUtility(depName, makefile);
  800. }
  801. // Add additional autogen target dependencies to autogen target
  802. for (cmTarget* depTarget : autogenDependTargets) {
  803. autogenTarget->AddUtility(depTarget->GetName(), makefile);
  804. }
  805. // Set FOLDER property in autogen target
  806. if (!this->AutogenFolder.empty()) {
  807. autogenTarget->SetProperty("FOLDER", this->AutogenFolder.c_str());
  808. }
  809. // Add autogen target to the origin target dependencies
  810. this->Target->Target->AddUtility(this->AutogenTargetName, makefile);
  811. }
  812. }
  813. }
  814. void cmQtAutoGenInitializer::SetupCustomTargets()
  815. {
  816. cmMakefile* makefile = this->Target->Target->GetMakefile();
  817. // Create info directory on demand
  818. if (!cmSystemTools::MakeDirectory(this->DirInfo)) {
  819. std::string emsg = ("Could not create directory: ");
  820. emsg += Quoted(this->DirInfo);
  821. cmSystemTools::Error(emsg.c_str());
  822. }
  823. // Configuration include directories
  824. std::string includeDir = "include";
  825. std::map<std::string, std::string> includeDirs;
  826. for (std::string const& cfg : this->ConfigsList) {
  827. std::string& dir = includeDirs[cfg];
  828. dir = "include_";
  829. dir += cfg;
  830. }
  831. // Generate autogen target info file
  832. if (this->MocEnabled || this->UicEnabled) {
  833. if (this->MocEnabled) {
  834. this->SetupCustomTargetsMoc();
  835. }
  836. if (this->UicEnabled) {
  837. this->SetupCustomTargetsUic();
  838. }
  839. // Parallel processing
  840. this->Parallel = GetSafeProperty(this->Target, "AUTOGEN_PARALLEL");
  841. if (this->Parallel.empty() || (this->Parallel == "AUTO")) {
  842. // Autodetect number of CPUs
  843. this->Parallel = std::to_string(GetParallelCPUCount());
  844. }
  845. cmGeneratedFileStream ofs;
  846. ofs.SetCopyIfDifferent(true);
  847. ofs.Open(this->AutogenInfoFile.c_str(), false, true);
  848. if (ofs) {
  849. // Utility lambdas
  850. auto CWrite = [&ofs](const char* key, std::string const& value) {
  851. ofs << "set(" << key << " " << cmOutputConverter::EscapeForCMake(value)
  852. << ")\n";
  853. };
  854. auto CWriteList = [&CWrite](const char* key,
  855. std::vector<std::string> const& list) {
  856. CWrite(key, cmJoin(list, ";"));
  857. };
  858. auto CWriteNestedLists = [&CWrite](
  859. const char* key, std::vector<std::vector<std::string>> const& lists) {
  860. std::vector<std::string> seplist;
  861. for (const std::vector<std::string>& list : lists) {
  862. std::string blist = "{";
  863. blist += cmJoin(list, ";");
  864. blist += "}";
  865. seplist.push_back(std::move(blist));
  866. }
  867. CWrite(key, cmJoin(seplist, cmQtAutoGen::ListSep));
  868. };
  869. auto CWriteSet = [&CWrite](const char* key,
  870. std::set<std::string> const& list) {
  871. CWrite(key, cmJoin(list, ";"));
  872. };
  873. auto CWriteMap = [&ofs](const char* key,
  874. std::map<std::string, std::string> const& map) {
  875. for (auto const& item : map) {
  876. ofs << "set(" << key << "_" << item.first << " "
  877. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  878. }
  879. };
  880. auto MfDef = [makefile](const char* key) {
  881. return std::string(makefile->GetSafeDefinition(key));
  882. };
  883. // Write
  884. ofs << "# Meta\n";
  885. CWrite("AM_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  886. CWrite("AM_PARALLEL", this->Parallel);
  887. ofs << "# Directories\n";
  888. CWrite("AM_CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  889. CWrite("AM_CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  890. CWrite("AM_CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  891. CWrite("AM_CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  892. CWrite("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE",
  893. MfDef("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"));
  894. CWrite("AM_BUILD_DIR", this->DirBuild);
  895. if (this->MultiConfig) {
  896. CWriteMap("AM_INCLUDE_DIR", includeDirs);
  897. } else {
  898. CWrite("AM_INCLUDE_DIR", includeDir);
  899. }
  900. ofs << "# Files\n";
  901. CWriteList("AM_SOURCES", this->Sources);
  902. CWriteList("AM_HEADERS", this->Headers);
  903. if (this->MultiConfig) {
  904. std::map<std::string, std::string> settingsFiles;
  905. for (std::string const& cfg : this->ConfigsList) {
  906. settingsFiles[cfg] =
  907. AppendFilenameSuffix(this->AutogenSettingsFile, "_" + cfg);
  908. }
  909. CWriteMap("AM_SETTINGS_FILE", settingsFiles);
  910. } else {
  911. CWrite("AM_SETTINGS_FILE", this->AutogenSettingsFile);
  912. }
  913. ofs << "# Qt\n";
  914. CWrite("AM_QT_VERSION_MAJOR", this->QtVersionMajor);
  915. CWrite("AM_QT_MOC_EXECUTABLE", this->MocExecutable);
  916. CWrite("AM_QT_UIC_EXECUTABLE", this->UicExecutable);
  917. if (this->MocEnabled) {
  918. ofs << "# MOC settings\n";
  919. CWriteSet("AM_MOC_SKIP", this->MocSkip);
  920. CWrite("AM_MOC_DEFINITIONS", this->MocDefines);
  921. CWriteMap("AM_MOC_DEFINITIONS", this->MocDefinesConfig);
  922. CWrite("AM_MOC_INCLUDES", this->MocIncludes);
  923. CWriteMap("AM_MOC_INCLUDES", this->MocIncludesConfig);
  924. CWrite("AM_MOC_OPTIONS",
  925. GetSafeProperty(this->Target, "AUTOMOC_MOC_OPTIONS"));
  926. CWrite("AM_MOC_RELAXED_MODE", MfDef("CMAKE_AUTOMOC_RELAXED_MODE"));
  927. CWrite("AM_MOC_MACRO_NAMES",
  928. GetSafeProperty(this->Target, "AUTOMOC_MACRO_NAMES"));
  929. CWrite("AM_MOC_DEPEND_FILTERS",
  930. GetSafeProperty(this->Target, "AUTOMOC_DEPEND_FILTERS"));
  931. CWrite("AM_MOC_PREDEFS_CMD", this->MocPredefsCmd);
  932. }
  933. if (this->UicEnabled) {
  934. ofs << "# UIC settings\n";
  935. CWriteSet("AM_UIC_SKIP", this->UicSkip);
  936. CWrite("AM_UIC_TARGET_OPTIONS", this->UicOptions);
  937. CWriteMap("AM_UIC_TARGET_OPTIONS", this->UicOptionsConfig);
  938. CWriteList("AM_UIC_OPTIONS_FILES", this->UicFileFiles);
  939. CWriteNestedLists("AM_UIC_OPTIONS_OPTIONS", this->UicFileOptions);
  940. CWriteList("AM_UIC_SEARCH_PATHS", this->UicSearchPaths);
  941. }
  942. } else {
  943. return;
  944. }
  945. }
  946. // Generate auto RCC info files
  947. if (this->RccEnabled) {
  948. for (Qrc const& qrc : this->Qrcs) {
  949. // Register rcc info file as generated
  950. makefile->AddCMakeOutputFile(qrc.InfoFile);
  951. cmGeneratedFileStream ofs;
  952. ofs.SetCopyIfDifferent(true);
  953. ofs.Open(qrc.InfoFile.c_str(), false, true);
  954. if (ofs) {
  955. // Utility lambdas
  956. auto CWrite = [&ofs](const char* key, std::string const& value) {
  957. ofs << "set(" << key << " "
  958. << cmOutputConverter::EscapeForCMake(value) << ")\n";
  959. };
  960. auto CWriteMap = [&ofs](
  961. const char* key, std::map<std::string, std::string> const& map) {
  962. for (auto const& item : map) {
  963. ofs << "set(" << key << "_" << item.first << " "
  964. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  965. }
  966. };
  967. // Write
  968. ofs << "# Configurations\n";
  969. CWrite("ARCC_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  970. ofs << "# Settings file\n";
  971. if (this->MultiConfig) {
  972. std::map<std::string, std::string> settingsFiles;
  973. for (std::string const& cfg : this->ConfigsList) {
  974. settingsFiles[cfg] =
  975. AppendFilenameSuffix(qrc.SettingsFile, "_" + cfg);
  976. }
  977. CWriteMap("ARCC_SETTINGS_FILE", settingsFiles);
  978. } else {
  979. CWrite("ARCC_SETTINGS_FILE", qrc.SettingsFile);
  980. }
  981. ofs << "# Directories\n";
  982. CWrite("ARCC_BUILD_DIR", this->DirBuild);
  983. if (this->MultiConfig) {
  984. CWriteMap("ARCC_INCLUDE_DIR", includeDirs);
  985. } else {
  986. CWrite("ARCC_INCLUDE_DIR", includeDir);
  987. }
  988. ofs << "# Rcc executable\n";
  989. CWrite("ARCC_RCC_EXECUTABLE", this->RccExecutable);
  990. CWrite("ARCC_RCC_LIST_OPTIONS", cmJoin(this->RccListOptions, ";"));
  991. ofs << "# Rcc job\n";
  992. CWrite("ARCC_LOCK_FILE", qrc.LockFile);
  993. CWrite("ARCC_SOURCE", qrc.QrcFile);
  994. CWrite("ARCC_OUTPUT_CHECKSUM", qrc.PathChecksum);
  995. CWrite("ARCC_OUTPUT_NAME",
  996. cmSystemTools::GetFilenameName(qrc.RccFile));
  997. CWrite("ARCC_OPTIONS", cmJoin(qrc.Options, ";"));
  998. CWrite("ARCC_INPUTS", cmJoin(qrc.Resources, ";"));
  999. } else {
  1000. return;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. void cmQtAutoGenInitializer::SetupCustomTargetsMoc()
  1006. {
  1007. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1008. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1009. // Moc predefs command
  1010. if (this->Target->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES") &&
  1011. this->QtVersionGreaterOrEqual(5, 8)) {
  1012. this->MocPredefsCmd =
  1013. makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND");
  1014. }
  1015. // Moc includes and compile definitions
  1016. {
  1017. auto GetIncludeDirs = [this,
  1018. localGen](std::string const& cfg) -> std::string {
  1019. // Get the include dirs for this target, without stripping the implicit
  1020. // include dirs off, see
  1021. // https://gitlab.kitware.com/cmake/cmake/issues/13667
  1022. std::vector<std::string> includeDirs;
  1023. localGen->GetIncludeDirectories(includeDirs, this->Target, "CXX", cfg,
  1024. false);
  1025. return cmJoin(includeDirs, ";");
  1026. };
  1027. auto GetCompileDefinitions =
  1028. [this, localGen](std::string const& cfg) -> std::string {
  1029. std::set<std::string> defines;
  1030. localGen->AddCompileDefinitions(defines, this->Target, cfg, "CXX");
  1031. return cmJoin(defines, ";");
  1032. };
  1033. // Default configuration settings
  1034. this->MocIncludes = GetIncludeDirs(this->ConfigDefault);
  1035. this->MocDefines = GetCompileDefinitions(this->ConfigDefault);
  1036. // Other configuration settings
  1037. for (std::string const& cfg : this->ConfigsList) {
  1038. {
  1039. std::string const configIncludeDirs = GetIncludeDirs(cfg);
  1040. if (configIncludeDirs != this->MocIncludes) {
  1041. this->MocIncludesConfig[cfg] = configIncludeDirs;
  1042. }
  1043. }
  1044. {
  1045. std::string const configCompileDefs = GetCompileDefinitions(cfg);
  1046. if (configCompileDefs != this->MocDefines) {
  1047. this->MocDefinesConfig[cfg] = configCompileDefs;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. // Moc executable
  1053. {
  1054. std::string mocExec;
  1055. std::string err;
  1056. if (this->QtVersionMajor == "5") {
  1057. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::moc");
  1058. if (tgt != nullptr) {
  1059. mocExec = SafeString(tgt->ImportedGetLocation(""));
  1060. } else {
  1061. err = "AUTOMOC: Qt5::moc target not found";
  1062. }
  1063. } else if (this->QtVersionMajor == "4") {
  1064. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::moc");
  1065. if (tgt != nullptr) {
  1066. mocExec = SafeString(tgt->ImportedGetLocation(""));
  1067. } else {
  1068. err = "AUTOMOC: Qt4::moc target not found";
  1069. }
  1070. } else {
  1071. err = "The AUTOMOC feature supports only Qt 4 and Qt 5";
  1072. }
  1073. if (err.empty()) {
  1074. this->MocExecutable = mocExec;
  1075. } else {
  1076. err += " (";
  1077. err += this->Target->GetName();
  1078. err += ")";
  1079. cmSystemTools::Error(err.c_str());
  1080. }
  1081. }
  1082. }
  1083. void cmQtAutoGenInitializer::SetupCustomTargetsUic()
  1084. {
  1085. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1086. // Uic search paths
  1087. {
  1088. std::string const usp =
  1089. GetSafeProperty(this->Target, "AUTOUIC_SEARCH_PATHS");
  1090. if (!usp.empty()) {
  1091. cmSystemTools::ExpandListArgument(usp, this->UicSearchPaths);
  1092. std::string const srcDir = makefile->GetCurrentSourceDirectory();
  1093. for (std::string& path : this->UicSearchPaths) {
  1094. path = cmSystemTools::CollapseFullPath(path, srcDir);
  1095. }
  1096. }
  1097. }
  1098. // Uic target options
  1099. {
  1100. auto UicGetOpts = [this](std::string const& cfg) -> std::string {
  1101. std::vector<std::string> opts;
  1102. this->Target->GetAutoUicOptions(opts, cfg);
  1103. return cmJoin(opts, ";");
  1104. };
  1105. // Default settings
  1106. this->UicOptions = UicGetOpts(this->ConfigDefault);
  1107. // Configuration specific settings
  1108. for (std::string const& cfg : this->ConfigsList) {
  1109. std::string const configUicOpts = UicGetOpts(cfg);
  1110. if (configUicOpts != this->UicOptions) {
  1111. this->UicOptionsConfig[cfg] = configUicOpts;
  1112. }
  1113. }
  1114. }
  1115. // .ui files skip and options
  1116. {
  1117. std::string const uiExt = "ui";
  1118. std::string pathError;
  1119. for (cmSourceFile* sf : makefile->GetSourceFiles()) {
  1120. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  1121. // Since we're iterating over source files that might be not in the
  1122. // target we need to check for path errors (not existing files).
  1123. std::string const& fPath = sf->GetFullPath(&pathError);
  1124. if (!pathError.empty()) {
  1125. pathError.clear();
  1126. continue;
  1127. }
  1128. if (sf->GetExtension() == uiExt) {
  1129. std::string const absFile = cmSystemTools::GetRealPath(fPath);
  1130. // Check if the .ui file should be skipped
  1131. if (sf->GetPropertyAsBool("SKIP_AUTOUIC") ||
  1132. sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  1133. this->UicSkip.insert(absFile);
  1134. }
  1135. // Check if the .ui file has uic options
  1136. std::string const uicOpts = GetSafeProperty(sf, "AUTOUIC_OPTIONS");
  1137. if (!uicOpts.empty()) {
  1138. // Check if file isn't skipped
  1139. if (this->UicSkip.count(absFile) == 0) {
  1140. this->UicFileFiles.push_back(absFile);
  1141. std::vector<std::string> optsVec;
  1142. cmSystemTools::ExpandListArgument(uicOpts, optsVec);
  1143. this->UicFileOptions.push_back(std::move(optsVec));
  1144. }
  1145. }
  1146. }
  1147. }
  1148. }
  1149. // Uic executable
  1150. {
  1151. std::string err;
  1152. std::string uicExec;
  1153. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1154. if (this->QtVersionMajor == "5") {
  1155. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::uic");
  1156. if (tgt != nullptr) {
  1157. uicExec = SafeString(tgt->ImportedGetLocation(""));
  1158. } else {
  1159. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  1160. }
  1161. } else if (this->QtVersionMajor == "4") {
  1162. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::uic");
  1163. if (tgt != nullptr) {
  1164. uicExec = SafeString(tgt->ImportedGetLocation(""));
  1165. } else {
  1166. err = "AUTOUIC: Qt4::uic target not found";
  1167. }
  1168. } else {
  1169. err = "The AUTOUIC feature supports only Qt 4 and Qt 5";
  1170. }
  1171. if (err.empty()) {
  1172. this->UicExecutable = uicExec;
  1173. } else {
  1174. err += " (";
  1175. err += this->Target->GetName();
  1176. err += ")";
  1177. cmSystemTools::Error(err.c_str());
  1178. }
  1179. }
  1180. }
  1181. void cmQtAutoGenInitializer::AddGeneratedSource(std::string const& filename,
  1182. GeneratorT genType)
  1183. {
  1184. // Register source file in makefile
  1185. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1186. {
  1187. cmSourceFile* gFile = makefile->GetOrCreateSource(filename, true);
  1188. gFile->SetProperty("GENERATED", "1");
  1189. gFile->SetProperty("SKIP_AUTOGEN", "On");
  1190. }
  1191. // Add source file to source group
  1192. AddToSourceGroup(makefile, filename, genType);
  1193. // Add source file to target
  1194. this->Target->AddSource(filename);
  1195. }
  1196. std::string cmQtAutoGenInitializer::GetQtMajorVersion(
  1197. cmGeneratorTarget const* target)
  1198. {
  1199. cmMakefile* makefile = target->Target->GetMakefile();
  1200. std::string qtMajor = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  1201. if (qtMajor.empty()) {
  1202. qtMajor = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  1203. }
  1204. const char* targetQtVersion =
  1205. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "");
  1206. if (targetQtVersion != nullptr) {
  1207. qtMajor = targetQtVersion;
  1208. }
  1209. return qtMajor;
  1210. }
  1211. std::string cmQtAutoGenInitializer::GetQtMinorVersion(
  1212. cmGeneratorTarget const* target, std::string const& qtVersionMajor)
  1213. {
  1214. cmMakefile* makefile = target->Target->GetMakefile();
  1215. std::string qtMinor;
  1216. if (qtVersionMajor == "5") {
  1217. qtMinor = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR");
  1218. }
  1219. if (qtMinor.empty()) {
  1220. qtMinor = makefile->GetSafeDefinition("QT_VERSION_MINOR");
  1221. }
  1222. const char* targetQtVersion =
  1223. target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION", "");
  1224. if (targetQtVersion != nullptr) {
  1225. qtMinor = targetQtVersion;
  1226. }
  1227. return qtMinor;
  1228. }
  1229. bool cmQtAutoGenInitializer::QtVersionGreaterOrEqual(
  1230. unsigned long requestMajor, unsigned long requestMinor) const
  1231. {
  1232. unsigned long majorUL(0);
  1233. unsigned long minorUL(0);
  1234. if (cmSystemTools::StringToULong(this->QtVersionMajor.c_str(), &majorUL) &&
  1235. cmSystemTools::StringToULong(this->QtVersionMinor.c_str(), &minorUL)) {
  1236. return (majorUL > requestMajor) ||
  1237. (majorUL == requestMajor && minorUL >= requestMinor);
  1238. }
  1239. return false;
  1240. }
  1241. /// @brief Reads the resource files list from from a .qrc file
  1242. /// @arg fileName Must be the absolute path of the .qrc file
  1243. /// @return True if the rcc file was successfully read
  1244. bool cmQtAutoGenInitializer::RccListInputs(std::string const& fileName,
  1245. std::vector<std::string>& files,
  1246. std::string& error)
  1247. {
  1248. if (!cmSystemTools::FileExists(fileName)) {
  1249. error = "rcc resource file does not exist:\n ";
  1250. error += Quoted(fileName);
  1251. error += "\n";
  1252. return false;
  1253. }
  1254. if (!RccListOptions.empty()) {
  1255. // Use rcc for file listing
  1256. if (RccExecutable.empty()) {
  1257. error = "rcc executable not available";
  1258. return false;
  1259. }
  1260. // Run rcc list command in the directory of the qrc file with the
  1261. // pathless
  1262. // qrc file name argument. This way rcc prints relative paths.
  1263. // This avoids issues on Windows when the qrc file is in a path that
  1264. // contains non-ASCII characters.
  1265. std::string const fileDir = cmSystemTools::GetFilenamePath(fileName);
  1266. std::string const fileNameName = cmSystemTools::GetFilenameName(fileName);
  1267. bool result = false;
  1268. int retVal = 0;
  1269. std::string rccStdOut;
  1270. std::string rccStdErr;
  1271. {
  1272. std::vector<std::string> cmd;
  1273. cmd.push_back(RccExecutable);
  1274. cmd.insert(cmd.end(), RccListOptions.begin(), RccListOptions.end());
  1275. cmd.push_back(fileNameName);
  1276. result = cmSystemTools::RunSingleCommand(
  1277. cmd, &rccStdOut, &rccStdErr, &retVal, fileDir.c_str(),
  1278. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1279. }
  1280. if (!result || retVal) {
  1281. error = "rcc list process failed for:\n ";
  1282. error += Quoted(fileName);
  1283. error += "\n";
  1284. error += rccStdOut;
  1285. error += "\n";
  1286. error += rccStdErr;
  1287. error += "\n";
  1288. return false;
  1289. }
  1290. if (!RccListParseOutput(rccStdOut, rccStdErr, files, error)) {
  1291. return false;
  1292. }
  1293. } else {
  1294. // We can't use rcc for the file listing.
  1295. // Read the qrc file content into string and parse it.
  1296. {
  1297. std::string qrcContents;
  1298. {
  1299. cmsys::ifstream ifs(fileName.c_str());
  1300. if (ifs) {
  1301. std::ostringstream osst;
  1302. osst << ifs.rdbuf();
  1303. qrcContents = osst.str();
  1304. } else {
  1305. error = "rcc file not readable:\n ";
  1306. error += Quoted(fileName);
  1307. error += "\n";
  1308. return false;
  1309. }
  1310. }
  1311. // Parse string content
  1312. RccListParseContent(qrcContents, files);
  1313. }
  1314. }
  1315. // Convert relative paths to absolute paths
  1316. RccListConvertFullPath(cmSystemTools::GetFilenamePath(fileName), files);
  1317. return true;
  1318. }