cmQtAutoGenInitializer.cxx 54 KB

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