cmQtAutoGenInitializer.cxx 52 KB

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