cmQtAutoGenInitializer.cxx 53 KB

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