cmQtAutoGenInitializer.cxx 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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 += cmake::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. #ifdef _WIN32
  476. if (this->Moc.PredefsCmd.empty()) {
  477. // Add WIN32 definition if we don't have a moc_predefs.h
  478. defines.insert("WIN32");
  479. }
  480. #endif
  481. return defines;
  482. };
  483. // Default configuration defines
  484. this->Moc.Defines = GetCompileDefinitions(this->ConfigDefault);
  485. // Other configuration defines
  486. if (this->MultiConfig) {
  487. for (std::string const& cfg : this->ConfigsList) {
  488. std::set<std::string> defines = GetCompileDefinitions(cfg);
  489. if (defines != this->Moc.Defines) {
  490. this->Moc.ConfigDefines[cfg] = std::move(defines);
  491. }
  492. }
  493. }
  494. }
  495. // Moc executable
  496. if (!GetMocExecutable()) {
  497. return false;
  498. }
  499. return true;
  500. }
  501. bool cmQtAutoGenInitializer::InitUic()
  502. {
  503. cmMakefile* makefile = this->Target->Target->GetMakefile();
  504. // Uic search paths
  505. {
  506. std::string const usp =
  507. this->Target->GetSafeProperty("AUTOUIC_SEARCH_PATHS");
  508. if (!usp.empty()) {
  509. cmSystemTools::ExpandListArgument(usp, this->Uic.SearchPaths);
  510. std::string const& srcDir = makefile->GetCurrentSourceDirectory();
  511. for (std::string& path : this->Uic.SearchPaths) {
  512. path = cmSystemTools::CollapseFullPath(path, srcDir);
  513. }
  514. }
  515. }
  516. // Uic target options
  517. {
  518. auto UicGetOpts =
  519. [this](std::string const& cfg) -> std::vector<std::string> {
  520. std::vector<std::string> opts;
  521. this->Target->GetAutoUicOptions(opts, cfg);
  522. return opts;
  523. };
  524. // Default settings
  525. this->Uic.Options = UicGetOpts(this->ConfigDefault);
  526. // Configuration specific settings
  527. if (this->MultiConfig) {
  528. for (std::string const& cfg : this->ConfigsList) {
  529. std::vector<std::string> options = UicGetOpts(cfg);
  530. if (options != this->Uic.Options) {
  531. this->Uic.ConfigOptions[cfg] = std::move(options);
  532. }
  533. }
  534. }
  535. }
  536. // .ui files skip and options
  537. {
  538. std::string const uiExt = "ui";
  539. std::string pathError;
  540. for (cmSourceFile* sf : makefile->GetSourceFiles()) {
  541. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  542. // Since we're iterating over source files that might be not in the
  543. // target we need to check for path errors (not existing files).
  544. std::string const& fPath = sf->GetFullPath(&pathError);
  545. if (!pathError.empty()) {
  546. pathError.clear();
  547. continue;
  548. }
  549. if (sf->GetExtension() == uiExt) {
  550. std::string const absFile = cmSystemTools::GetRealPath(fPath);
  551. // Check if the .ui file should be skipped
  552. if (sf->GetPropertyAsBool("SKIP_AUTOUIC") ||
  553. sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  554. this->Uic.Skip.insert(absFile);
  555. }
  556. // Check if the .ui file has uic options
  557. std::string const uicOpts = sf->GetSafeProperty("AUTOUIC_OPTIONS");
  558. if (!uicOpts.empty()) {
  559. // Check if file isn't skipped
  560. if (this->Uic.Skip.count(absFile) == 0) {
  561. this->Uic.FileFiles.push_back(absFile);
  562. std::vector<std::string> optsVec;
  563. cmSystemTools::ExpandListArgument(uicOpts, optsVec);
  564. this->Uic.FileOptions.push_back(std::move(optsVec));
  565. }
  566. }
  567. }
  568. }
  569. }
  570. // Uic executable
  571. if (!GetUicExecutable()) {
  572. return false;
  573. }
  574. return true;
  575. }
  576. bool cmQtAutoGenInitializer::InitRcc()
  577. {
  578. if (!GetRccExecutable()) {
  579. return false;
  580. }
  581. return true;
  582. }
  583. bool cmQtAutoGenInitializer::InitScanFiles()
  584. {
  585. cmMakefile* makefile = this->Target->Target->GetMakefile();
  586. // Scan through target files
  587. {
  588. std::string const qrcExt = "qrc";
  589. std::vector<cmSourceFile*> srcFiles;
  590. this->Target->GetConfigCommonSourceFiles(srcFiles);
  591. for (cmSourceFile* sf : srcFiles) {
  592. if (sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  593. continue;
  594. }
  595. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  596. std::string const& fPath = sf->GetFullPath();
  597. std::string const& ext = sf->GetExtension();
  598. // Register generated files that will be scanned by moc or uic
  599. if (this->Moc.Enabled || this->Uic.Enabled) {
  600. cmSystemTools::FileFormat const fileType =
  601. cmSystemTools::GetFileFormat(ext.c_str());
  602. if ((fileType == cmSystemTools::CXX_FILE_FORMAT) ||
  603. (fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  604. std::string const absPath = cmSystemTools::GetRealPath(fPath);
  605. if ((this->Moc.Enabled && !sf->GetPropertyAsBool("SKIP_AUTOMOC")) ||
  606. (this->Uic.Enabled && !sf->GetPropertyAsBool("SKIP_AUTOUIC"))) {
  607. // Register source
  608. const bool generated = sf->GetPropertyAsBool("GENERATED");
  609. if (fileType == cmSystemTools::HEADER_FILE_FORMAT) {
  610. if (generated) {
  611. this->AutogenTarget.HeadersGenerated.push_back(absPath);
  612. } else {
  613. this->AutogenTarget.Headers.push_back(absPath);
  614. }
  615. } else {
  616. if (generated) {
  617. this->AutogenTarget.SourcesGenerated.push_back(absPath);
  618. } else {
  619. this->AutogenTarget.Sources.push_back(absPath);
  620. }
  621. }
  622. }
  623. }
  624. }
  625. // Register rcc enabled files
  626. if (this->Rcc.Enabled && (ext == qrcExt) &&
  627. !sf->GetPropertyAsBool("SKIP_AUTORCC")) {
  628. // Register qrc file
  629. {
  630. Qrc qrc;
  631. qrc.QrcFile = cmSystemTools::GetRealPath(fPath);
  632. qrc.QrcName =
  633. cmSystemTools::GetFilenameWithoutLastExtension(qrc.QrcFile);
  634. qrc.Generated = sf->GetPropertyAsBool("GENERATED");
  635. // RCC options
  636. {
  637. std::string const opts = sf->GetSafeProperty("AUTORCC_OPTIONS");
  638. if (!opts.empty()) {
  639. cmSystemTools::ExpandListArgument(opts, qrc.Options);
  640. }
  641. }
  642. this->Rcc.Qrcs.push_back(std::move(qrc));
  643. }
  644. }
  645. }
  646. }
  647. // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
  648. // sources meta data cache. Clear it so that OBJECT library targets that
  649. // are AUTOGEN initialized after this target get their added
  650. // mocs_compilation.cpp source acknowledged by this target.
  651. this->Target->ClearSourcesCache();
  652. if (this->Moc.Enabled || this->Uic.Enabled) {
  653. // Read skip files from makefile sources
  654. {
  655. std::string pathError;
  656. for (cmSourceFile* sf : makefile->GetSourceFiles()) {
  657. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  658. // Since we're iterating over source files that might be not in the
  659. // target we need to check for path errors (not existing files).
  660. std::string const& fPath = sf->GetFullPath(&pathError);
  661. if (!pathError.empty()) {
  662. pathError.clear();
  663. continue;
  664. }
  665. cmSystemTools::FileFormat const fileType =
  666. cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
  667. if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
  668. !(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  669. continue;
  670. }
  671. const bool skipAll = sf->GetPropertyAsBool("SKIP_AUTOGEN");
  672. const bool mocSkip = this->Moc.Enabled &&
  673. (skipAll || sf->GetPropertyAsBool("SKIP_AUTOMOC"));
  674. const bool uicSkip = this->Uic.Enabled &&
  675. (skipAll || sf->GetPropertyAsBool("SKIP_AUTOUIC"));
  676. if (mocSkip || uicSkip) {
  677. std::string const absFile = cmSystemTools::GetRealPath(fPath);
  678. if (mocSkip) {
  679. this->Moc.Skip.insert(absFile);
  680. }
  681. if (uicSkip) {
  682. this->Uic.Skip.insert(absFile);
  683. }
  684. }
  685. }
  686. }
  687. // Process GENERATED sources and headers
  688. if (!this->AutogenTarget.SourcesGenerated.empty() ||
  689. !this->AutogenTarget.HeadersGenerated.empty()) {
  690. // Check status of policy CMP0071
  691. bool policyAccept = false;
  692. bool policyWarn = false;
  693. cmPolicies::PolicyStatus const CMP0071_status =
  694. makefile->GetPolicyStatus(cmPolicies::CMP0071);
  695. switch (CMP0071_status) {
  696. case cmPolicies::WARN:
  697. policyWarn = true;
  698. CM_FALLTHROUGH;
  699. case cmPolicies::OLD:
  700. // Ignore GENERATED file
  701. break;
  702. case cmPolicies::REQUIRED_IF_USED:
  703. case cmPolicies::REQUIRED_ALWAYS:
  704. case cmPolicies::NEW:
  705. // Process GENERATED file
  706. policyAccept = true;
  707. break;
  708. }
  709. if (policyAccept) {
  710. // Accept GENERATED sources
  711. for (std::string const& absFile :
  712. this->AutogenTarget.HeadersGenerated) {
  713. this->AutogenTarget.Headers.push_back(absFile);
  714. this->AutogenTarget.DependFiles.insert(absFile);
  715. }
  716. for (std::string const& absFile :
  717. this->AutogenTarget.SourcesGenerated) {
  718. this->AutogenTarget.Sources.push_back(absFile);
  719. this->AutogenTarget.DependFiles.insert(absFile);
  720. }
  721. } else {
  722. if (policyWarn) {
  723. std::string msg;
  724. msg += cmPolicies::GetPolicyWarning(cmPolicies::CMP0071);
  725. msg += "\n";
  726. std::string tools;
  727. std::string property;
  728. if (this->Moc.Enabled && this->Uic.Enabled) {
  729. tools = "AUTOMOC and AUTOUIC";
  730. property = "SKIP_AUTOGEN";
  731. } else if (this->Moc.Enabled) {
  732. tools = "AUTOMOC";
  733. property = "SKIP_AUTOMOC";
  734. } else if (this->Uic.Enabled) {
  735. tools = "AUTOUIC";
  736. property = "SKIP_AUTOUIC";
  737. }
  738. msg += "For compatibility, CMake is excluding the GENERATED source "
  739. "file(s):\n";
  740. for (const std::string& absFile :
  741. this->AutogenTarget.HeadersGenerated) {
  742. msg.append(" ").append(Quoted(absFile)).append("\n");
  743. }
  744. for (const std::string& absFile :
  745. this->AutogenTarget.SourcesGenerated) {
  746. msg.append(" ").append(Quoted(absFile)).append("\n");
  747. }
  748. msg += "from processing by ";
  749. msg += tools;
  750. msg +=
  751. ". If any of the files should be processed, set CMP0071 to NEW. "
  752. "If any of the files should not be processed, "
  753. "explicitly exclude them by setting the source file property ";
  754. msg += property;
  755. msg += ":\n set_property(SOURCE file.h PROPERTY ";
  756. msg += property;
  757. msg += " ON)\n";
  758. makefile->IssueMessage(cmake::AUTHOR_WARNING, msg);
  759. }
  760. }
  761. }
  762. // Sort headers and sources
  763. if (this->Moc.Enabled || this->Uic.Enabled) {
  764. std::sort(this->AutogenTarget.Headers.begin(),
  765. this->AutogenTarget.Headers.end());
  766. std::sort(this->AutogenTarget.Sources.begin(),
  767. this->AutogenTarget.Sources.end());
  768. }
  769. }
  770. // Process qrc files
  771. if (!this->Rcc.Qrcs.empty()) {
  772. const bool QtV5 = (this->QtVersion.Major == 5);
  773. // Target rcc options
  774. std::vector<std::string> optionsTarget;
  775. cmSystemTools::ExpandListArgument(
  776. this->Target->GetSafeProperty("AUTORCC_OPTIONS"), optionsTarget);
  777. // Check if file name is unique
  778. for (Qrc& qrc : this->Rcc.Qrcs) {
  779. qrc.Unique = true;
  780. for (Qrc const& qrc2 : this->Rcc.Qrcs) {
  781. if ((&qrc != &qrc2) && (qrc.QrcName == qrc2.QrcName)) {
  782. qrc.Unique = false;
  783. break;
  784. }
  785. }
  786. }
  787. // Path checksum and file names
  788. {
  789. cmFilePathChecksum const fpathCheckSum(makefile);
  790. for (Qrc& qrc : this->Rcc.Qrcs) {
  791. qrc.PathChecksum = fpathCheckSum.getPart(qrc.QrcFile);
  792. // RCC output file name
  793. {
  794. std::string rccFile = this->Dir.Build + "/";
  795. rccFile += qrc.PathChecksum;
  796. rccFile += "/qrc_";
  797. rccFile += qrc.QrcName;
  798. rccFile += ".cpp";
  799. qrc.RccFile = std::move(rccFile);
  800. }
  801. {
  802. std::string base = this->Dir.Info;
  803. base += "/RCC";
  804. base += qrc.QrcName;
  805. if (!qrc.Unique) {
  806. base += qrc.PathChecksum;
  807. }
  808. qrc.LockFile = base;
  809. qrc.LockFile += ".lock";
  810. qrc.InfoFile = base;
  811. qrc.InfoFile += "Info.cmake";
  812. qrc.SettingsFile = base;
  813. qrc.SettingsFile += "Settings.txt";
  814. if (this->MultiConfig) {
  815. for (std::string const& cfg : this->ConfigsList) {
  816. qrc.ConfigSettingsFile[cfg] =
  817. AppendFilenameSuffix(qrc.SettingsFile, "_" + cfg);
  818. }
  819. }
  820. }
  821. }
  822. }
  823. // RCC options
  824. for (Qrc& qrc : this->Rcc.Qrcs) {
  825. // Target options
  826. std::vector<std::string> opts = optionsTarget;
  827. // Merge computed "-name XYZ" option
  828. {
  829. std::string name = qrc.QrcName;
  830. // Replace '-' with '_'. The former is not valid for symbol names.
  831. std::replace(name.begin(), name.end(), '-', '_');
  832. if (!qrc.Unique) {
  833. name += "_";
  834. name += qrc.PathChecksum;
  835. }
  836. std::vector<std::string> nameOpts;
  837. nameOpts.emplace_back("-name");
  838. nameOpts.emplace_back(std::move(name));
  839. RccMergeOptions(opts, nameOpts, QtV5);
  840. }
  841. // Merge file option
  842. RccMergeOptions(opts, qrc.Options, QtV5);
  843. qrc.Options = std::move(opts);
  844. }
  845. // RCC resources
  846. for (Qrc& qrc : this->Rcc.Qrcs) {
  847. if (!qrc.Generated) {
  848. std::string error;
  849. if (!RccListInputs(qrc.QrcFile, qrc.Resources, error)) {
  850. cmSystemTools::Error(error.c_str());
  851. return false;
  852. }
  853. }
  854. }
  855. }
  856. return true;
  857. }
  858. bool cmQtAutoGenInitializer::InitAutogenTarget()
  859. {
  860. cmMakefile* makefile = this->Target->Target->GetMakefile();
  861. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  862. cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
  863. // Register info file as generated by CMake
  864. makefile->AddCMakeOutputFile(this->AutogenTarget.InfoFile);
  865. // Files provided by the autogen target
  866. std::vector<std::string> autogenProvides;
  867. if (this->Moc.Enabled) {
  868. this->AddGeneratedSource(this->Moc.MocsCompilation, GeneratorT::MOC);
  869. autogenProvides.push_back(this->Moc.MocsCompilation);
  870. }
  871. // Compose target comment
  872. std::string autogenComment;
  873. {
  874. std::string tools;
  875. if (this->Moc.Enabled) {
  876. tools += "MOC";
  877. }
  878. if (this->Uic.Enabled) {
  879. if (!tools.empty()) {
  880. tools += " and ";
  881. }
  882. tools += "UIC";
  883. }
  884. autogenComment = "Automatic ";
  885. autogenComment += tools;
  886. autogenComment += " for target ";
  887. autogenComment += this->Target->GetName();
  888. }
  889. // Compose command lines
  890. cmCustomCommandLines commandLines;
  891. {
  892. cmCustomCommandLine currentLine;
  893. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  894. currentLine.push_back("-E");
  895. currentLine.push_back("cmake_autogen");
  896. currentLine.push_back(this->AutogenTarget.InfoFile);
  897. currentLine.push_back("$<CONFIGURATION>");
  898. commandLines.push_back(std::move(currentLine));
  899. }
  900. // Use PRE_BUILD on demand
  901. bool usePRE_BUILD = false;
  902. if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
  903. // Under VS use a PRE_BUILD event instead of a separate target to
  904. // reduce the number of targets loaded into the IDE.
  905. // This also works around a VS 11 bug that may skip updating the target:
  906. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  907. usePRE_BUILD = true;
  908. }
  909. // Disable PRE_BUILD in some cases
  910. if (usePRE_BUILD) {
  911. // Cannot use PRE_BUILD with file depends
  912. if (!this->AutogenTarget.DependFiles.empty()) {
  913. usePRE_BUILD = false;
  914. }
  915. // Cannot use PRE_BUILD when a global autogen target is in place
  916. if (AutogenTarget.GlobalTarget) {
  917. usePRE_BUILD = false;
  918. }
  919. }
  920. // Create the autogen target/command
  921. if (usePRE_BUILD) {
  922. // Add additional autogen target dependencies to origin target
  923. for (cmTarget* depTarget : this->AutogenTarget.DependTargets) {
  924. this->Target->Target->AddUtility(depTarget->GetName(), makefile);
  925. }
  926. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  927. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  928. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  929. //
  930. // PRE_BUILD does not support file dependencies!
  931. const std::vector<std::string> no_output;
  932. const std::vector<std::string> no_deps;
  933. cmCustomCommand cc(makefile, no_output, autogenProvides, no_deps,
  934. commandLines, autogenComment.c_str(),
  935. this->Dir.Work.c_str());
  936. cc.SetEscapeOldStyle(false);
  937. cc.SetEscapeAllowMakeVars(true);
  938. this->Target->Target->AddPreBuildCommand(cc);
  939. } else {
  940. // Add link library target dependencies to the autogen target
  941. // dependencies
  942. if (this->AutogenTarget.DependOrigin) {
  943. // add_dependencies/addUtility do not support generator expressions.
  944. // We depend only on the libraries found in all configs therefore.
  945. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  946. for (std::string const& config : this->ConfigsList) {
  947. cmLinkImplementationLibraries const* libs =
  948. this->Target->GetLinkImplementationLibraries(config);
  949. if (libs != nullptr) {
  950. for (cmLinkItem const& item : libs->Libraries) {
  951. cmGeneratorTarget const* libTarget = item.Target;
  952. if ((libTarget != nullptr) &&
  953. !StaticLibraryCycle(this->Target, libTarget, config)) {
  954. // Increment target config count
  955. commonTargets[libTarget]++;
  956. }
  957. }
  958. }
  959. }
  960. for (auto const& item : commonTargets) {
  961. if (item.second == this->ConfigsList.size()) {
  962. this->AutogenTarget.DependTargets.insert(item.first->Target);
  963. }
  964. }
  965. }
  966. // Create autogen target
  967. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  968. this->AutogenTarget.Name, cmMakefile::TargetOrigin::Generator, true,
  969. this->Dir.Work.c_str(), /*byproducts=*/autogenProvides,
  970. std::vector<std::string>(this->AutogenTarget.DependFiles.begin(),
  971. this->AutogenTarget.DependFiles.end()),
  972. commandLines, false, autogenComment.c_str());
  973. // Create autogen generator target
  974. localGen->AddGeneratorTarget(
  975. new cmGeneratorTarget(autogenTarget, localGen));
  976. // Forward origin utilities to autogen target
  977. if (this->AutogenTarget.DependOrigin) {
  978. for (BT<std::string> const& depName : this->Target->GetUtilities()) {
  979. autogenTarget->AddUtility(depName.Value, makefile);
  980. }
  981. }
  982. // Add additional autogen target dependencies to autogen target
  983. for (cmTarget* depTarget : this->AutogenTarget.DependTargets) {
  984. autogenTarget->AddUtility(depTarget->GetName(), makefile);
  985. }
  986. // Set FOLDER property in autogen target
  987. if (!this->TargetsFolder.empty()) {
  988. autogenTarget->SetProperty("FOLDER", this->TargetsFolder.c_str());
  989. }
  990. // Add autogen target to the origin target dependencies
  991. this->Target->Target->AddUtility(this->AutogenTarget.Name, makefile);
  992. // Add autogen target to the global autogen target dependencies
  993. if (this->AutogenTarget.GlobalTarget) {
  994. this->GlobalInitializer->AddToGlobalAutoGen(localGen,
  995. this->AutogenTarget.Name);
  996. }
  997. }
  998. return true;
  999. }
  1000. bool cmQtAutoGenInitializer::InitRccTargets()
  1001. {
  1002. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1003. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1004. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1005. // Register info file as generated by CMake
  1006. makefile->AddCMakeOutputFile(qrc.InfoFile);
  1007. // Register file at target
  1008. this->AddGeneratedSource(qrc.RccFile, GeneratorT::RCC);
  1009. std::vector<std::string> ccOutput;
  1010. ccOutput.push_back(qrc.RccFile);
  1011. cmCustomCommandLines commandLines;
  1012. if (this->MultiConfig) {
  1013. // Build for all configurations
  1014. for (std::string const& config : this->ConfigsList) {
  1015. cmCustomCommandLine currentLine;
  1016. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  1017. currentLine.push_back("-E");
  1018. currentLine.push_back("cmake_autorcc");
  1019. currentLine.push_back(qrc.InfoFile);
  1020. currentLine.push_back(config);
  1021. commandLines.push_back(std::move(currentLine));
  1022. }
  1023. } else {
  1024. cmCustomCommandLine currentLine;
  1025. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  1026. currentLine.push_back("-E");
  1027. currentLine.push_back("cmake_autorcc");
  1028. currentLine.push_back(qrc.InfoFile);
  1029. currentLine.push_back("$<CONFIG>");
  1030. commandLines.push_back(std::move(currentLine));
  1031. }
  1032. std::string ccComment = "Automatic RCC for ";
  1033. ccComment += FileProjectRelativePath(makefile, qrc.QrcFile);
  1034. if (qrc.Generated || this->Rcc.GlobalTarget) {
  1035. // Create custom rcc target
  1036. std::string ccName;
  1037. {
  1038. ccName = this->Target->GetName();
  1039. ccName += "_arcc_";
  1040. ccName += qrc.QrcName;
  1041. if (!qrc.Unique) {
  1042. ccName += "_";
  1043. ccName += qrc.PathChecksum;
  1044. }
  1045. std::vector<std::string> ccDepends;
  1046. // Add the .qrc and info file to the custom target dependencies
  1047. ccDepends.push_back(qrc.QrcFile);
  1048. ccDepends.push_back(qrc.InfoFile);
  1049. cmTarget* autoRccTarget = makefile->AddUtilityCommand(
  1050. ccName, cmMakefile::TargetOrigin::Generator, true,
  1051. this->Dir.Work.c_str(), ccOutput, ccDepends, commandLines, false,
  1052. ccComment.c_str());
  1053. // Create autogen generator target
  1054. localGen->AddGeneratorTarget(
  1055. new cmGeneratorTarget(autoRccTarget, localGen));
  1056. // Set FOLDER property in autogen target
  1057. if (!this->TargetsFolder.empty()) {
  1058. autoRccTarget->SetProperty("FOLDER", this->TargetsFolder.c_str());
  1059. }
  1060. }
  1061. // Add autogen target to the origin target dependencies
  1062. this->Target->Target->AddUtility(ccName, makefile);
  1063. // Add autogen target to the global autogen target dependencies
  1064. if (this->Rcc.GlobalTarget) {
  1065. this->GlobalInitializer->AddToGlobalAutoRcc(localGen, ccName);
  1066. }
  1067. } else {
  1068. // Create custom rcc command
  1069. {
  1070. std::vector<std::string> ccByproducts;
  1071. std::vector<std::string> ccDepends;
  1072. // Add the .qrc and info file to the custom command dependencies
  1073. ccDepends.push_back(qrc.QrcFile);
  1074. ccDepends.push_back(qrc.InfoFile);
  1075. // Add the resource files to the dependencies
  1076. for (std::string const& fileName : qrc.Resources) {
  1077. // Add resource file to the custom command dependencies
  1078. ccDepends.push_back(fileName);
  1079. }
  1080. makefile->AddCustomCommandToOutput(ccOutput, ccByproducts, ccDepends,
  1081. /*main_dependency*/ std::string(),
  1082. commandLines, ccComment.c_str(),
  1083. this->Dir.Work.c_str());
  1084. }
  1085. // Reconfigure when .qrc file changes
  1086. makefile->AddCMakeDependFile(qrc.QrcFile);
  1087. }
  1088. }
  1089. return true;
  1090. }
  1091. bool cmQtAutoGenInitializer::SetupCustomTargets()
  1092. {
  1093. // Create info directory on demand
  1094. if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
  1095. std::string emsg = ("AutoGen: Could not create directory: ");
  1096. emsg += Quoted(this->Dir.Info);
  1097. cmSystemTools::Error(emsg.c_str());
  1098. return false;
  1099. }
  1100. // Generate autogen target info file
  1101. if (this->Moc.Enabled || this->Uic.Enabled) {
  1102. // Write autogen target info files
  1103. if (!this->SetupWriteAutogenInfo()) {
  1104. return false;
  1105. }
  1106. }
  1107. // Write AUTORCC info files
  1108. if (this->Rcc.Enabled && !this->SetupWriteRccInfo()) {
  1109. return false;
  1110. }
  1111. return true;
  1112. }
  1113. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  1114. {
  1115. InfoWriter ofs(this->AutogenTarget.InfoFile);
  1116. if (ofs) {
  1117. // Utility lambdas
  1118. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1119. auto MfDef = [makefile](const char* key) {
  1120. return makefile->GetSafeDefinition(key);
  1121. };
  1122. // Write common settings
  1123. ofs.Write("# Meta\n");
  1124. ofs.Write("AM_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  1125. ofs.Write("AM_PARALLEL", this->AutogenTarget.Parallel);
  1126. ofs.Write("AM_VERBOSITY", this->Verbosity);
  1127. ofs.Write("# Directories\n");
  1128. ofs.Write("AM_CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1129. ofs.Write("AM_CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1130. ofs.Write("AM_CMAKE_CURRENT_SOURCE_DIR",
  1131. MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1132. ofs.Write("AM_CMAKE_CURRENT_BINARY_DIR",
  1133. MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1134. ofs.Write("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE",
  1135. MfDef("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"));
  1136. ofs.Write("AM_BUILD_DIR", this->Dir.Build);
  1137. ofs.Write("AM_INCLUDE_DIR", this->Dir.Include);
  1138. ofs.WriteConfig("AM_INCLUDE_DIR", this->Dir.ConfigInclude);
  1139. ofs.Write("# Files\n");
  1140. ofs.WriteStrings("AM_SOURCES", this->AutogenTarget.Sources);
  1141. ofs.WriteStrings("AM_HEADERS", this->AutogenTarget.Headers);
  1142. ofs.Write("AM_SETTINGS_FILE", this->AutogenTarget.SettingsFile);
  1143. ofs.WriteConfig("AM_SETTINGS_FILE",
  1144. this->AutogenTarget.ConfigSettingsFile);
  1145. ofs.Write("# Qt\n");
  1146. ofs.WriteUInt("AM_QT_VERSION_MAJOR", this->QtVersion.Major);
  1147. ofs.Write("AM_QT_MOC_EXECUTABLE", this->Moc.Executable);
  1148. ofs.Write("AM_QT_UIC_EXECUTABLE", this->Uic.Executable);
  1149. // Write moc settings
  1150. if (this->Moc.Enabled) {
  1151. ofs.Write("# MOC settings\n");
  1152. ofs.WriteStrings("AM_MOC_SKIP", this->Moc.Skip);
  1153. ofs.WriteStrings("AM_MOC_DEFINITIONS", this->Moc.Defines);
  1154. ofs.WriteConfigStrings("AM_MOC_DEFINITIONS", this->Moc.ConfigDefines);
  1155. ofs.WriteStrings("AM_MOC_INCLUDES", this->Moc.Includes);
  1156. ofs.WriteConfigStrings("AM_MOC_INCLUDES", this->Moc.ConfigIncludes);
  1157. ofs.Write("AM_MOC_OPTIONS",
  1158. this->Target->GetSafeProperty("AUTOMOC_MOC_OPTIONS"));
  1159. ofs.Write("AM_MOC_RELAXED_MODE", MfDef("CMAKE_AUTOMOC_RELAXED_MODE"));
  1160. ofs.Write("AM_MOC_MACRO_NAMES",
  1161. this->Target->GetSafeProperty("AUTOMOC_MACRO_NAMES"));
  1162. ofs.Write("AM_MOC_DEPEND_FILTERS",
  1163. this->Target->GetSafeProperty("AUTOMOC_DEPEND_FILTERS"));
  1164. ofs.Write("AM_MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  1165. }
  1166. // Write uic settings
  1167. if (this->Uic.Enabled) {
  1168. ofs.Write("# UIC settings\n");
  1169. ofs.WriteStrings("AM_UIC_SKIP", this->Uic.Skip);
  1170. ofs.WriteStrings("AM_UIC_TARGET_OPTIONS", this->Uic.Options);
  1171. ofs.WriteConfigStrings("AM_UIC_TARGET_OPTIONS", this->Uic.ConfigOptions);
  1172. ofs.WriteStrings("AM_UIC_OPTIONS_FILES", this->Uic.FileFiles);
  1173. ofs.WriteNestedLists("AM_UIC_OPTIONS_OPTIONS", this->Uic.FileOptions);
  1174. ofs.WriteStrings("AM_UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  1175. }
  1176. } else {
  1177. std::string err = "AutoGen: Could not write file ";
  1178. err += this->AutogenTarget.InfoFile;
  1179. cmSystemTools::Error(err.c_str());
  1180. return false;
  1181. }
  1182. return true;
  1183. }
  1184. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  1185. {
  1186. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1187. InfoWriter ofs(qrc.InfoFile);
  1188. if (ofs) {
  1189. // Write
  1190. ofs.Write("# Configurations\n");
  1191. ofs.Write("ARCC_MULTI_CONFIG", this->MultiConfig ? "TRUE" : "FALSE");
  1192. ofs.Write("ARCC_VERBOSITY", this->Verbosity);
  1193. ofs.Write("# Settings file\n");
  1194. ofs.Write("ARCC_SETTINGS_FILE", qrc.SettingsFile);
  1195. ofs.WriteConfig("ARCC_SETTINGS_FILE", qrc.ConfigSettingsFile);
  1196. ofs.Write("# Directories\n");
  1197. ofs.Write("ARCC_BUILD_DIR", this->Dir.Build);
  1198. ofs.Write("ARCC_INCLUDE_DIR", this->Dir.Include);
  1199. ofs.WriteConfig("ARCC_INCLUDE_DIR", this->Dir.ConfigInclude);
  1200. ofs.Write("# Rcc executable\n");
  1201. ofs.Write("ARCC_RCC_EXECUTABLE", this->Rcc.Executable);
  1202. ofs.WriteStrings("ARCC_RCC_LIST_OPTIONS", this->Rcc.ListOptions);
  1203. ofs.Write("# Rcc job\n");
  1204. ofs.Write("ARCC_LOCK_FILE", qrc.LockFile);
  1205. ofs.Write("ARCC_SOURCE", qrc.QrcFile);
  1206. ofs.Write("ARCC_OUTPUT_CHECKSUM", qrc.PathChecksum);
  1207. ofs.Write("ARCC_OUTPUT_NAME",
  1208. cmSystemTools::GetFilenameName(qrc.RccFile));
  1209. ofs.WriteStrings("ARCC_OPTIONS", qrc.Options);
  1210. ofs.WriteStrings("ARCC_INPUTS", qrc.Resources);
  1211. } else {
  1212. std::string err = "AutoRcc: Could not write file ";
  1213. err += qrc.InfoFile;
  1214. cmSystemTools::Error(err.c_str());
  1215. return false;
  1216. }
  1217. }
  1218. return true;
  1219. }
  1220. void cmQtAutoGenInitializer::AddGeneratedSource(std::string const& filename,
  1221. GeneratorT genType)
  1222. {
  1223. // Register source file in makefile
  1224. cmMakefile* makefile = this->Target->Target->GetMakefile();
  1225. {
  1226. cmSourceFile* gFile = makefile->GetOrCreateSource(filename, true);
  1227. gFile->SetProperty("GENERATED", "1");
  1228. gFile->SetProperty("SKIP_AUTOGEN", "On");
  1229. }
  1230. // Add source file to source group
  1231. AddToSourceGroup(makefile, filename, genType);
  1232. // Add source file to target
  1233. this->Target->AddSource(filename);
  1234. }
  1235. cmQtAutoGenInitializer::IntegerVersion cmQtAutoGenInitializer::GetQtVersion(
  1236. cmGeneratorTarget const* target)
  1237. {
  1238. cmQtAutoGenInitializer::IntegerVersion res;
  1239. cmMakefile* makefile = target->Target->GetMakefile();
  1240. // -- Major version
  1241. std::string qtMajor = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  1242. if (qtMajor.empty()) {
  1243. qtMajor = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  1244. }
  1245. {
  1246. const char* targetQtVersion =
  1247. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "");
  1248. if (targetQtVersion != nullptr) {
  1249. qtMajor = targetQtVersion;
  1250. }
  1251. }
  1252. // -- Minor version
  1253. std::string qtMinor;
  1254. if (!qtMajor.empty()) {
  1255. if (qtMajor == "5") {
  1256. qtMinor = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR");
  1257. }
  1258. if (qtMinor.empty()) {
  1259. qtMinor = makefile->GetSafeDefinition("QT_VERSION_MINOR");
  1260. }
  1261. {
  1262. const char* targetQtVersion =
  1263. target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION",
  1264. "");
  1265. if (targetQtVersion != nullptr) {
  1266. qtMinor = targetQtVersion;
  1267. }
  1268. }
  1269. }
  1270. // -- Convert to integer
  1271. if (!qtMajor.empty() && !qtMinor.empty()) {
  1272. unsigned long majorUL(0);
  1273. unsigned long minorUL(0);
  1274. if (cmSystemTools::StringToULong(qtMajor.c_str(), &majorUL) &&
  1275. cmSystemTools::StringToULong(qtMinor.c_str(), &minorUL)) {
  1276. res.Major = static_cast<unsigned int>(majorUL);
  1277. res.Minor = static_cast<unsigned int>(minorUL);
  1278. }
  1279. }
  1280. return res;
  1281. }
  1282. bool cmQtAutoGenInitializer::GetMocExecutable()
  1283. {
  1284. std::string err;
  1285. // Find moc executable
  1286. {
  1287. std::string targetName;
  1288. if (this->QtVersion.Major == 5) {
  1289. targetName = "Qt5::moc";
  1290. } else if (this->QtVersion.Major == 4) {
  1291. targetName = "Qt4::moc";
  1292. } else {
  1293. err = "The AUTOMOC feature supports only Qt 4 and Qt 5";
  1294. }
  1295. if (!targetName.empty()) {
  1296. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1297. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
  1298. if (tgt != nullptr) {
  1299. this->Moc.Executable = tgt->ImportedGetLocation("");
  1300. } else {
  1301. err = "Could not find target " + targetName;
  1302. }
  1303. }
  1304. }
  1305. // Test moc command
  1306. if (err.empty()) {
  1307. if (cmSystemTools::FileExists(this->Moc.Executable, true)) {
  1308. std::vector<std::string> command;
  1309. command.push_back(this->Moc.Executable);
  1310. command.push_back("-h");
  1311. std::string stdOut;
  1312. std::string stdErr;
  1313. int retVal = 0;
  1314. bool result = cmSystemTools::RunSingleCommand(
  1315. command, &stdOut, &stdErr, &retVal, nullptr,
  1316. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1317. if (!result) {
  1318. err = "The moc test command failed: ";
  1319. err += QuotedCommand(command);
  1320. }
  1321. } else {
  1322. err = "The moc executable ";
  1323. err += Quoted(this->Moc.Executable);
  1324. err += " does not exist";
  1325. }
  1326. }
  1327. // Print error
  1328. if (!err.empty()) {
  1329. std::string msg = "AutoMoc (";
  1330. msg += this->Target->GetName();
  1331. msg += "): ";
  1332. msg += err;
  1333. cmSystemTools::Error(msg.c_str());
  1334. return false;
  1335. }
  1336. return true;
  1337. }
  1338. bool cmQtAutoGenInitializer::GetUicExecutable()
  1339. {
  1340. std::string err;
  1341. // Find uic executable
  1342. {
  1343. std::string targetName;
  1344. if (this->QtVersion.Major == 5) {
  1345. targetName = "Qt5::uic";
  1346. } else if (this->QtVersion.Major == 4) {
  1347. targetName = "Qt4::uic";
  1348. } else {
  1349. err = "The AUTOUIC feature supports only Qt 4 and Qt 5";
  1350. }
  1351. if (!targetName.empty()) {
  1352. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1353. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
  1354. if (tgt != nullptr) {
  1355. this->Uic.Executable = tgt->ImportedGetLocation("");
  1356. } else {
  1357. if (this->QtVersion.Major == 5) {
  1358. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  1359. } else {
  1360. err = "Could not find target " + targetName;
  1361. }
  1362. }
  1363. }
  1364. }
  1365. // Test uic command
  1366. if (err.empty() && !this->Uic.Executable.empty()) {
  1367. if (cmSystemTools::FileExists(this->Uic.Executable, true)) {
  1368. std::vector<std::string> command;
  1369. command.push_back(this->Uic.Executable);
  1370. command.push_back("-h");
  1371. std::string stdOut;
  1372. std::string stdErr;
  1373. int retVal = 0;
  1374. bool result = cmSystemTools::RunSingleCommand(
  1375. command, &stdOut, &stdErr, &retVal, nullptr,
  1376. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1377. if (!result) {
  1378. err = "The uic test command failed: ";
  1379. err += QuotedCommand(command);
  1380. }
  1381. } else {
  1382. err = "The uic executable ";
  1383. err += Quoted(this->Uic.Executable);
  1384. err += " does not exist";
  1385. }
  1386. }
  1387. // Print error
  1388. if (!err.empty()) {
  1389. std::string msg = "AutoUic (";
  1390. msg += this->Target->GetName();
  1391. msg += "): ";
  1392. msg += err;
  1393. cmSystemTools::Error(msg.c_str());
  1394. return false;
  1395. }
  1396. return true;
  1397. }
  1398. bool cmQtAutoGenInitializer::GetRccExecutable()
  1399. {
  1400. std::string err;
  1401. // Find rcc executable
  1402. {
  1403. std::string targetName;
  1404. if (this->QtVersion.Major == 5) {
  1405. targetName = "Qt5::rcc";
  1406. } else if (this->QtVersion.Major == 4) {
  1407. targetName = "Qt4::rcc";
  1408. } else {
  1409. err = "The AUTORCC feature supports only Qt 4 and Qt 5";
  1410. }
  1411. if (!targetName.empty()) {
  1412. cmLocalGenerator* localGen = this->Target->GetLocalGenerator();
  1413. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse(targetName);
  1414. if (tgt != nullptr) {
  1415. this->Rcc.Executable = tgt->ImportedGetLocation("");
  1416. } else {
  1417. err = "Could not find target " + targetName;
  1418. }
  1419. }
  1420. }
  1421. // Test rcc command
  1422. if (err.empty()) {
  1423. if (cmSystemTools::FileExists(this->Rcc.Executable, true)) {
  1424. std::vector<std::string> command;
  1425. command.push_back(this->Rcc.Executable);
  1426. command.push_back("-h");
  1427. std::string stdOut;
  1428. std::string stdErr;
  1429. int retVal = 0;
  1430. bool result = cmSystemTools::RunSingleCommand(
  1431. command, &stdOut, &stdErr, &retVal, nullptr,
  1432. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1433. if (result) {
  1434. // Detect if rcc supports (-)-list
  1435. if (this->QtVersion.Major == 5) {
  1436. if (stdOut.find("--list") != std::string::npos) {
  1437. this->Rcc.ListOptions.push_back("--list");
  1438. } else {
  1439. this->Rcc.ListOptions.push_back("-list");
  1440. }
  1441. }
  1442. } else {
  1443. err = "The rcc test command failed: ";
  1444. err += QuotedCommand(command);
  1445. }
  1446. } else {
  1447. err = "The rcc executable ";
  1448. err += Quoted(this->Rcc.Executable);
  1449. err += " does not exist";
  1450. }
  1451. }
  1452. // Print error
  1453. if (!err.empty()) {
  1454. std::string msg = "AutoRcc (";
  1455. msg += this->Target->GetName();
  1456. msg += "): ";
  1457. msg += err;
  1458. cmSystemTools::Error(msg.c_str());
  1459. return false;
  1460. }
  1461. return true;
  1462. }
  1463. /// @brief Reads the resource files list from from a .qrc file
  1464. /// @arg fileName Must be the absolute path of the .qrc file
  1465. /// @return True if the rcc file was successfully read
  1466. bool cmQtAutoGenInitializer::RccListInputs(std::string const& fileName,
  1467. std::vector<std::string>& files,
  1468. std::string& error)
  1469. {
  1470. if (!cmSystemTools::FileExists(fileName)) {
  1471. error = "rcc resource file does not exist:\n ";
  1472. error += Quoted(fileName);
  1473. error += "\n";
  1474. return false;
  1475. }
  1476. if (!this->Rcc.ListOptions.empty()) {
  1477. // Use rcc for file listing
  1478. if (this->Rcc.Executable.empty()) {
  1479. error = "rcc executable not available";
  1480. return false;
  1481. }
  1482. // Run rcc list command in the directory of the qrc file with the
  1483. // pathless
  1484. // qrc file name argument. This way rcc prints relative paths.
  1485. // This avoids issues on Windows when the qrc file is in a path that
  1486. // contains non-ASCII characters.
  1487. std::string const fileDir = cmSystemTools::GetFilenamePath(fileName);
  1488. std::string const fileNameName = cmSystemTools::GetFilenameName(fileName);
  1489. bool result = false;
  1490. int retVal = 0;
  1491. std::string rccStdOut;
  1492. std::string rccStdErr;
  1493. {
  1494. std::vector<std::string> cmd;
  1495. cmd.push_back(this->Rcc.Executable);
  1496. cmd.insert(cmd.end(), this->Rcc.ListOptions.begin(),
  1497. this->Rcc.ListOptions.end());
  1498. cmd.push_back(fileNameName);
  1499. result = cmSystemTools::RunSingleCommand(
  1500. cmd, &rccStdOut, &rccStdErr, &retVal, fileDir.c_str(),
  1501. cmSystemTools::OUTPUT_NONE, cmDuration::zero(), cmProcessOutput::Auto);
  1502. }
  1503. if (!result || retVal) {
  1504. error = "rcc list process failed for:\n ";
  1505. error += Quoted(fileName);
  1506. error += "\n";
  1507. error += rccStdOut;
  1508. error += "\n";
  1509. error += rccStdErr;
  1510. error += "\n";
  1511. return false;
  1512. }
  1513. if (!RccListParseOutput(rccStdOut, rccStdErr, files, error)) {
  1514. return false;
  1515. }
  1516. } else {
  1517. // We can't use rcc for the file listing.
  1518. // Read the qrc file content into string and parse it.
  1519. {
  1520. std::string qrcContents;
  1521. {
  1522. cmsys::ifstream ifs(fileName.c_str());
  1523. if (ifs) {
  1524. std::ostringstream osst;
  1525. osst << ifs.rdbuf();
  1526. qrcContents = osst.str();
  1527. } else {
  1528. error = "rcc file not readable:\n ";
  1529. error += Quoted(fileName);
  1530. error += "\n";
  1531. return false;
  1532. }
  1533. }
  1534. // Parse string content
  1535. RccListParseContent(qrcContents, files);
  1536. }
  1537. }
  1538. // Convert relative paths to absolute paths
  1539. RccListConvertFullPath(cmSystemTools::GetFilenamePath(fileName), files);
  1540. return true;
  1541. }