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