cmQtAutoGenInitializer.cxx 52 KB

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