cmQtAutoGeneratorInitializer.cxx 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 "cmQtAutoGen.h"
  4. #include "cmQtAutoGeneratorInitializer.h"
  5. #include "cmAlgorithms.h"
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmFilePathChecksum.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmOutputConverter.h"
  14. #include "cmPolicies.h"
  15. #include "cmSourceFile.h"
  16. #include "cmSourceGroup.h"
  17. #include "cmState.h"
  18. #include "cmSystemTools.h"
  19. #include "cmTarget.h"
  20. #include "cm_sys_stat.h"
  21. #include "cmake.h"
  22. #include "cmsys/FStream.hxx"
  23. #include <algorithm>
  24. #include <array>
  25. #include <map>
  26. #include <set>
  27. #include <sstream>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. inline static const char* SafeString(const char* value)
  32. {
  33. return (value != nullptr) ? value : "";
  34. }
  35. inline static std::string GetSafeProperty(cmGeneratorTarget const* target,
  36. const char* key)
  37. {
  38. return std::string(SafeString(target->GetProperty(key)));
  39. }
  40. inline static std::string GetSafeProperty(cmSourceFile const* sf,
  41. const char* key)
  42. {
  43. return std::string(SafeString(sf->GetProperty(key)));
  44. }
  45. inline static bool AutogenMultiConfig(cmGlobalGenerator* globalGen)
  46. {
  47. return globalGen->IsMultiConfig();
  48. }
  49. static std::string GetAutogenTargetName(cmGeneratorTarget const* target)
  50. {
  51. std::string autogenTargetName = target->GetName();
  52. autogenTargetName += "_autogen";
  53. return autogenTargetName;
  54. }
  55. static std::string GetAutogenTargetFilesDir(cmGeneratorTarget const* target)
  56. {
  57. cmMakefile* makefile = target->Target->GetMakefile();
  58. std::string targetDir = makefile->GetCurrentBinaryDirectory();
  59. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  60. targetDir += "/";
  61. targetDir += GetAutogenTargetName(target);
  62. targetDir += ".dir";
  63. return targetDir;
  64. }
  65. static std::string GetAutogenTargetBuildDir(cmGeneratorTarget const* target)
  66. {
  67. std::string targetDir = GetSafeProperty(target, "AUTOGEN_BUILD_DIR");
  68. if (targetDir.empty()) {
  69. cmMakefile* makefile = target->Target->GetMakefile();
  70. targetDir = makefile->GetCurrentBinaryDirectory();
  71. targetDir += "/";
  72. targetDir += GetAutogenTargetName(target);
  73. }
  74. return targetDir;
  75. }
  76. std::string cmQtAutoGeneratorInitializer::GetQtMajorVersion(
  77. cmGeneratorTarget const* target)
  78. {
  79. cmMakefile* makefile = target->Target->GetMakefile();
  80. std::string qtMajor = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  81. if (qtMajor.empty()) {
  82. qtMajor = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  83. }
  84. const char* targetQtVersion =
  85. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "");
  86. if (targetQtVersion != nullptr) {
  87. qtMajor = targetQtVersion;
  88. }
  89. return qtMajor;
  90. }
  91. std::string cmQtAutoGeneratorInitializer::GetQtMinorVersion(
  92. cmGeneratorTarget const* target, const std::string& qtVersionMajor)
  93. {
  94. cmMakefile* makefile = target->Target->GetMakefile();
  95. std::string qtMinor;
  96. if (qtVersionMajor == "5") {
  97. qtMinor = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR");
  98. }
  99. if (qtMinor.empty()) {
  100. qtMinor = makefile->GetSafeDefinition("QT_VERSION_MINOR");
  101. }
  102. const char* targetQtVersion =
  103. target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION", "");
  104. if (targetQtVersion != nullptr) {
  105. qtMinor = targetQtVersion;
  106. }
  107. return qtMinor;
  108. }
  109. static bool QtVersionGreaterOrEqual(const std::string& major,
  110. const std::string& minor,
  111. unsigned long requestMajor,
  112. unsigned long requestMinor)
  113. {
  114. unsigned long majorUL(0);
  115. unsigned long minorUL(0);
  116. if (cmSystemTools::StringToULong(major.c_str(), &majorUL) &&
  117. cmSystemTools::StringToULong(minor.c_str(), &minorUL)) {
  118. return (majorUL > requestMajor) ||
  119. (majorUL == requestMajor && minorUL >= requestMinor);
  120. }
  121. return false;
  122. }
  123. static void GetCompileDefinitionsAndDirectories(
  124. cmGeneratorTarget const* target, const std::string& config,
  125. std::string& incs, std::string& defs)
  126. {
  127. cmLocalGenerator* localGen = target->GetLocalGenerator();
  128. {
  129. std::vector<std::string> includeDirs;
  130. // Get the include dirs for this target, without stripping the implicit
  131. // include dirs off, see
  132. // https://gitlab.kitware.com/cmake/cmake/issues/13667
  133. localGen->GetIncludeDirectories(includeDirs, target, "CXX", config, false);
  134. incs = cmJoin(includeDirs, ";");
  135. }
  136. {
  137. std::set<std::string> defines;
  138. localGen->AddCompileDefinitions(defines, target, config, "CXX");
  139. defs += cmJoin(defines, ";");
  140. }
  141. }
  142. static std::vector<std::string> GetConfigurations(
  143. cmMakefile* makefile, std::string* config = nullptr)
  144. {
  145. std::vector<std::string> configs;
  146. {
  147. std::string cfg = makefile->GetConfigurations(configs);
  148. if (config != nullptr) {
  149. *config = cfg;
  150. }
  151. }
  152. // Add empty configuration on demand
  153. if (configs.empty()) {
  154. configs.push_back("");
  155. }
  156. return configs;
  157. }
  158. static std::vector<std::string> GetConfigurationSuffixes(cmMakefile* makefile)
  159. {
  160. std::vector<std::string> suffixes;
  161. if (AutogenMultiConfig(makefile->GetGlobalGenerator())) {
  162. makefile->GetConfigurations(suffixes);
  163. for (std::string& suffix : suffixes) {
  164. suffix.insert(0, "_");
  165. }
  166. }
  167. if (suffixes.empty()) {
  168. suffixes.push_back("");
  169. }
  170. return suffixes;
  171. }
  172. static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
  173. const std::string& value)
  174. {
  175. makefile->AddDefinition(key,
  176. cmOutputConverter::EscapeForCMake(value).c_str());
  177. }
  178. static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
  179. const std::vector<std::string>& values)
  180. {
  181. makefile->AddDefinition(
  182. key, cmOutputConverter::EscapeForCMake(cmJoin(values, ";")).c_str());
  183. }
  184. static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
  185. const std::set<std::string>& values)
  186. {
  187. makefile->AddDefinition(
  188. key, cmOutputConverter::EscapeForCMake(cmJoin(values, ";")).c_str());
  189. }
  190. static bool AddToSourceGroup(cmMakefile* makefile, const std::string& fileName,
  191. cmQtAutoGen::GeneratorType genType)
  192. {
  193. cmSourceGroup* sourceGroup = nullptr;
  194. // Acquire source group
  195. {
  196. std::string property;
  197. std::string groupName;
  198. {
  199. std::array<std::string, 2> props;
  200. // Use generator specific group name
  201. switch (genType) {
  202. case cmQtAutoGen::MOC:
  203. props[0] = "AUTOMOC_SOURCE_GROUP";
  204. break;
  205. case cmQtAutoGen::RCC:
  206. props[0] = "AUTORCC_SOURCE_GROUP";
  207. break;
  208. default:
  209. props[0] = "AUTOGEN_SOURCE_GROUP";
  210. break;
  211. }
  212. props[1] = "AUTOGEN_SOURCE_GROUP";
  213. for (std::string& prop : props) {
  214. const char* propName = makefile->GetState()->GetGlobalProperty(prop);
  215. if ((propName != nullptr) && (*propName != '\0')) {
  216. groupName = propName;
  217. property = std::move(prop);
  218. break;
  219. }
  220. }
  221. }
  222. // Generate a source group on demand
  223. if (!groupName.empty()) {
  224. {
  225. const char* delimiter =
  226. makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
  227. if (delimiter == nullptr) {
  228. delimiter = "\\";
  229. }
  230. std::vector<std::string> folders =
  231. cmSystemTools::tokenize(groupName, delimiter);
  232. sourceGroup = makefile->GetSourceGroup(folders);
  233. if (sourceGroup == nullptr) {
  234. makefile->AddSourceGroup(folders);
  235. sourceGroup = makefile->GetSourceGroup(folders);
  236. }
  237. }
  238. if (sourceGroup == nullptr) {
  239. std::ostringstream ost;
  240. ost << cmQtAutoGen::GeneratorNameUpper(genType);
  241. ost << ": " << property;
  242. ost << ": Could not find or create the source group ";
  243. ost << cmQtAutoGen::Quoted(groupName);
  244. cmSystemTools::Error(ost.str().c_str());
  245. return false;
  246. }
  247. }
  248. }
  249. if (sourceGroup != nullptr) {
  250. sourceGroup->AddGroupFile(fileName);
  251. }
  252. return true;
  253. }
  254. static void AddCleanFile(cmMakefile* makefile, const std::string& fileName)
  255. {
  256. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fileName.c_str(),
  257. false);
  258. }
  259. static void AddGeneratedSource(cmGeneratorTarget* target,
  260. const std::string& filename,
  261. cmQtAutoGen::GeneratorType genType)
  262. {
  263. cmMakefile* makefile = target->Target->GetMakefile();
  264. {
  265. cmSourceFile* gFile = makefile->GetOrCreateSource(filename, true);
  266. gFile->SetProperty("GENERATED", "1");
  267. gFile->SetProperty("SKIP_AUTOGEN", "On");
  268. }
  269. target->AddSource(filename);
  270. AddToSourceGroup(makefile, filename, genType);
  271. }
  272. struct cmQtAutoGenSetup
  273. {
  274. std::set<std::string> MocSkip;
  275. std::set<std::string> UicSkip;
  276. std::map<std::string, std::string> ConfigMocIncludes;
  277. std::map<std::string, std::string> ConfigMocDefines;
  278. std::map<std::string, std::string> ConfigUicOptions;
  279. };
  280. static void SetupAcquireSkipFiles(cmQtAutoGenDigest const& digest,
  281. cmQtAutoGenSetup& setup)
  282. {
  283. // Read skip files from makefile sources
  284. {
  285. const std::vector<cmSourceFile*>& allSources =
  286. digest.Target->Makefile->GetSourceFiles();
  287. for (cmSourceFile* sf : allSources) {
  288. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  289. const std::string& fPath = sf->GetFullPath();
  290. const cmSystemTools::FileFormat fileType =
  291. cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
  292. if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
  293. !(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  294. continue;
  295. }
  296. const bool skipAll = sf->GetPropertyAsBool("SKIP_AUTOGEN");
  297. const bool mocSkip = digest.MocEnabled &&
  298. (skipAll || sf->GetPropertyAsBool("SKIP_AUTOMOC"));
  299. const bool uicSkip = digest.UicEnabled &&
  300. (skipAll || sf->GetPropertyAsBool("SKIP_AUTOUIC"));
  301. if (mocSkip || uicSkip) {
  302. const std::string absFile = cmSystemTools::GetRealPath(fPath);
  303. if (mocSkip) {
  304. setup.MocSkip.insert(absFile);
  305. }
  306. if (uicSkip) {
  307. setup.UicSkip.insert(absFile);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. static void SetupAutoTargetMoc(const cmQtAutoGenDigest& digest,
  314. std::string const& config,
  315. std::vector<std::string> const& configs,
  316. cmQtAutoGenSetup& setup)
  317. {
  318. cmGeneratorTarget const* target = digest.Target;
  319. cmLocalGenerator* localGen = target->GetLocalGenerator();
  320. cmMakefile* makefile = target->Target->GetMakefile();
  321. AddDefinitionEscaped(makefile, "_moc_skip", setup.MocSkip);
  322. AddDefinitionEscaped(makefile, "_moc_options",
  323. GetSafeProperty(target, "AUTOMOC_MOC_OPTIONS"));
  324. AddDefinitionEscaped(makefile, "_moc_relaxed_mode",
  325. makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE") ? "TRUE"
  326. : "FALSE");
  327. AddDefinitionEscaped(makefile, "_moc_macro_names",
  328. GetSafeProperty(target, "AUTOMOC_MACRO_NAMES"));
  329. AddDefinitionEscaped(makefile, "_moc_depend_filters",
  330. GetSafeProperty(target, "AUTOMOC_DEPEND_FILTERS"));
  331. if (QtVersionGreaterOrEqual(digest.QtVersionMajor, digest.QtVersionMinor, 5,
  332. 8)) {
  333. AddDefinitionEscaped(
  334. makefile, "_moc_predefs_cmd",
  335. makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
  336. }
  337. // Moc includes and compile definitions
  338. {
  339. // Default settings
  340. std::string incs;
  341. std::string compileDefs;
  342. GetCompileDefinitionsAndDirectories(target, config, incs, compileDefs);
  343. AddDefinitionEscaped(makefile, "_moc_incs", incs);
  344. AddDefinitionEscaped(makefile, "_moc_compile_defs", compileDefs);
  345. // Configuration specific settings
  346. for (const std::string& cfg : configs) {
  347. std::string configIncs;
  348. std::string configCompileDefs;
  349. GetCompileDefinitionsAndDirectories(target, cfg, configIncs,
  350. configCompileDefs);
  351. if (configIncs != incs) {
  352. setup.ConfigMocIncludes[cfg] = configIncs;
  353. }
  354. if (configCompileDefs != compileDefs) {
  355. setup.ConfigMocDefines[cfg] = configCompileDefs;
  356. }
  357. }
  358. }
  359. // Moc executable
  360. {
  361. std::string mocExec;
  362. std::string err;
  363. if (digest.QtVersionMajor == "5") {
  364. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::moc");
  365. if (tgt != nullptr) {
  366. mocExec = SafeString(tgt->ImportedGetLocation(""));
  367. } else {
  368. err = "AUTOMOC: Qt5::moc target not found";
  369. }
  370. } else if (digest.QtVersionMajor == "4") {
  371. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::moc");
  372. if (tgt != nullptr) {
  373. mocExec = SafeString(tgt->ImportedGetLocation(""));
  374. } else {
  375. err = "AUTOMOC: Qt4::moc target not found";
  376. }
  377. } else {
  378. err = "The AUTOMOC feature supports only Qt 4 and Qt 5";
  379. }
  380. if (err.empty()) {
  381. AddDefinitionEscaped(makefile, "_qt_moc_executable", mocExec);
  382. } else {
  383. err += " (" + target->GetName() + ")";
  384. cmSystemTools::Error(err.c_str());
  385. }
  386. }
  387. }
  388. static void UicGetOpts(cmGeneratorTarget const* target,
  389. const std::string& config, std::string& optString)
  390. {
  391. std::vector<std::string> opts;
  392. target->GetAutoUicOptions(opts, config);
  393. optString = cmJoin(opts, ";");
  394. }
  395. static void SetupAutoTargetUic(const cmQtAutoGenDigest& digest,
  396. std::string const& config,
  397. std::vector<std::string> const& configs,
  398. cmQtAutoGenSetup& setup)
  399. {
  400. cmGeneratorTarget const* target = digest.Target;
  401. cmMakefile* makefile = target->Target->GetMakefile();
  402. AddDefinitionEscaped(makefile, "_uic_skip", setup.UicSkip);
  403. // Uic search paths
  404. {
  405. std::vector<std::string> uicSearchPaths;
  406. {
  407. const std::string usp = GetSafeProperty(target, "AUTOUIC_SEARCH_PATHS");
  408. if (!usp.empty()) {
  409. cmSystemTools::ExpandListArgument(usp, uicSearchPaths);
  410. const std::string srcDir = makefile->GetCurrentSourceDirectory();
  411. for (std::string& path : uicSearchPaths) {
  412. path = cmSystemTools::CollapseFullPath(path, srcDir);
  413. }
  414. }
  415. }
  416. AddDefinitionEscaped(makefile, "_uic_search_paths", uicSearchPaths);
  417. }
  418. // Uic target options
  419. {
  420. // Default settings
  421. std::string uicOpts;
  422. UicGetOpts(target, config, uicOpts);
  423. AddDefinitionEscaped(makefile, "_uic_target_options", uicOpts);
  424. // Configuration specific settings
  425. for (const std::string& cfg : configs) {
  426. std::string configUicOpts;
  427. UicGetOpts(target, cfg, configUicOpts);
  428. if (configUicOpts != uicOpts) {
  429. setup.ConfigUicOptions[cfg] = configUicOpts;
  430. }
  431. }
  432. }
  433. // Uic files options
  434. {
  435. std::vector<std::string> uiFileFiles;
  436. std::vector<std::string> uiFileOptions;
  437. {
  438. const std::string uiExt = "ui";
  439. const std::vector<cmSourceFile*>& srcFiles = makefile->GetSourceFiles();
  440. for (cmSourceFile* sf : srcFiles) {
  441. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  442. const std::string& fPath = sf->GetFullPath();
  443. if (sf->GetExtension() == uiExt) {
  444. // Check if the files has uic options
  445. std::string uicOpts = GetSafeProperty(sf, "AUTOUIC_OPTIONS");
  446. if (!uicOpts.empty()) {
  447. const std::string absFile = cmSystemTools::GetRealPath(fPath);
  448. // Check if file isn't skipped
  449. if (setup.UicSkip.count(absFile) == 0) {
  450. uiFileFiles.push_back(absFile);
  451. cmSystemTools::ReplaceString(uicOpts, ";", cmQtAutoGen::listSep);
  452. uiFileOptions.push_back(uicOpts);
  453. }
  454. }
  455. }
  456. }
  457. }
  458. AddDefinitionEscaped(makefile, "_qt_uic_options_files", uiFileFiles);
  459. AddDefinitionEscaped(makefile, "_qt_uic_options_options", uiFileOptions);
  460. }
  461. // Uic executable
  462. {
  463. std::string err;
  464. std::string uicExec;
  465. cmLocalGenerator* localGen = target->GetLocalGenerator();
  466. if (digest.QtVersionMajor == "5") {
  467. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::uic");
  468. if (tgt != nullptr) {
  469. uicExec = SafeString(tgt->ImportedGetLocation(""));
  470. } else {
  471. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  472. }
  473. } else if (digest.QtVersionMajor == "4") {
  474. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::uic");
  475. if (tgt != nullptr) {
  476. uicExec = SafeString(tgt->ImportedGetLocation(""));
  477. } else {
  478. err = "AUTOUIC: Qt4::uic target not found";
  479. }
  480. } else {
  481. err = "The AUTOUIC feature supports only Qt 4 and Qt 5";
  482. }
  483. if (err.empty()) {
  484. AddDefinitionEscaped(makefile, "_qt_uic_executable", uicExec);
  485. } else {
  486. err += " (" + target->GetName() + ")";
  487. cmSystemTools::Error(err.c_str());
  488. }
  489. }
  490. }
  491. static std::string RccGetExecutable(cmGeneratorTarget const* target,
  492. const std::string& qtMajorVersion)
  493. {
  494. std::string rccExec;
  495. std::string err;
  496. cmLocalGenerator* localGen = target->GetLocalGenerator();
  497. if (qtMajorVersion == "5") {
  498. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::rcc");
  499. if (tgt != nullptr) {
  500. rccExec = SafeString(tgt->ImportedGetLocation(""));
  501. } else {
  502. err = "AUTORCC: Qt5::rcc target not found";
  503. }
  504. } else if (qtMajorVersion == "4") {
  505. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::rcc");
  506. if (tgt != nullptr) {
  507. rccExec = SafeString(tgt->ImportedGetLocation(""));
  508. } else {
  509. err = "AUTORCC: Qt4::rcc target not found";
  510. }
  511. } else {
  512. err = "The AUTORCC feature supports only Qt 4 and Qt 5";
  513. }
  514. if (!err.empty()) {
  515. err += " (" + target->GetName() + ")";
  516. cmSystemTools::Error(err.c_str());
  517. }
  518. return rccExec;
  519. }
  520. static void RccMergeOptions(std::vector<std::string>& opts,
  521. const std::vector<std::string>& fileOpts,
  522. bool isQt5)
  523. {
  524. typedef std::vector<std::string>::iterator Iter;
  525. typedef std::vector<std::string>::const_iterator CIter;
  526. static const char* valueOptions[4] = { "name", "root", "compress",
  527. "threshold" };
  528. std::vector<std::string> extraOpts;
  529. for (CIter fit = fileOpts.begin(), fitEnd = fileOpts.end(); fit != fitEnd;
  530. ++fit) {
  531. Iter existIt = std::find(opts.begin(), opts.end(), *fit);
  532. if (existIt != opts.end()) {
  533. const char* optName = fit->c_str();
  534. if (*optName == '-') {
  535. ++optName;
  536. if (isQt5 && *optName == '-') {
  537. ++optName;
  538. }
  539. }
  540. // Test if this is a value option and change the existing value
  541. if ((optName != fit->c_str()) &&
  542. std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  543. cmStrCmp(optName)) != cmArrayEnd(valueOptions)) {
  544. const Iter existItNext(existIt + 1);
  545. const CIter fitNext(fit + 1);
  546. if ((existItNext != opts.end()) && (fitNext != fitEnd)) {
  547. *existItNext = *fitNext;
  548. ++fit;
  549. }
  550. }
  551. } else {
  552. extraOpts.push_back(*fit);
  553. }
  554. }
  555. // Append options
  556. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  557. }
  558. static void SetupAutoTargetRcc(const cmQtAutoGenDigest& digest)
  559. {
  560. cmGeneratorTarget const* target = digest.Target;
  561. cmMakefile* makefile = target->Target->GetMakefile();
  562. std::vector<std::string> rccFiles;
  563. std::vector<std::string> rccInputs;
  564. std::vector<std::string> rccFileFiles;
  565. std::vector<std::string> rccFileOptions;
  566. for (const cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  567. const std::string absFile = qrcDigest.QrcFile;
  568. // Register file
  569. rccFiles.push_back(absFile);
  570. // Register (known) resource files
  571. {
  572. std::string entriesList = "{";
  573. if (!qrcDigest.Generated) {
  574. entriesList += cmJoin(qrcDigest.Resources, cmQtAutoGen::listSep);
  575. }
  576. entriesList += "}";
  577. rccInputs.push_back(entriesList);
  578. }
  579. // rcc options for this qrc file
  580. if (!qrcDigest.Options.empty()) {
  581. rccFileFiles.push_back(absFile);
  582. rccFileOptions.push_back(
  583. cmJoin(qrcDigest.Options, cmQtAutoGen::listSep));
  584. }
  585. }
  586. AddDefinitionEscaped(makefile, "_qt_rcc_executable",
  587. RccGetExecutable(target, digest.QtVersionMajor));
  588. AddDefinitionEscaped(makefile, "_rcc_files", rccFiles);
  589. AddDefinitionEscaped(makefile, "_rcc_inputs", rccInputs);
  590. AddDefinitionEscaped(makefile, "_rcc_options_files", rccFileFiles);
  591. AddDefinitionEscaped(makefile, "_rcc_options_options", rccFileOptions);
  592. }
  593. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  594. cmQtAutoGenDigest& digest)
  595. {
  596. cmGeneratorTarget* target = digest.Target;
  597. cmMakefile* makefile = target->Target->GetMakefile();
  598. cmLocalGenerator* localGen = target->GetLocalGenerator();
  599. cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
  600. // Create a custom target for running generators at buildtime
  601. const bool multiConfig = AutogenMultiConfig(globalGen);
  602. const std::string autogenTargetName = GetAutogenTargetName(target);
  603. const std::string autogenBuildDir = GetAutogenTargetBuildDir(target);
  604. const std::string workingDirectory =
  605. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  606. const std::vector<std::string> suffixes = GetConfigurationSuffixes(makefile);
  607. std::set<std::string> autogenDepends;
  608. std::vector<std::string> autogenProvides;
  609. // Remove build directories on cleanup
  610. AddCleanFile(makefile, autogenBuildDir);
  611. // Remove old settings on cleanup
  612. {
  613. std::string base = GetAutogenTargetFilesDir(target);
  614. base += "/AutogenOldSettings";
  615. for (const std::string& suffix : suffixes) {
  616. AddCleanFile(makefile, (base + suffix).append(".cmake"));
  617. }
  618. }
  619. // Compose command lines
  620. cmCustomCommandLines commandLines;
  621. {
  622. cmCustomCommandLine currentLine;
  623. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  624. currentLine.push_back("-E");
  625. currentLine.push_back("cmake_autogen");
  626. currentLine.push_back(GetAutogenTargetFilesDir(target));
  627. currentLine.push_back("$<CONFIGURATION>");
  628. commandLines.push_back(currentLine);
  629. }
  630. // Compose target comment
  631. std::string autogenComment;
  632. {
  633. std::vector<std::string> toolNames;
  634. if (digest.MocEnabled) {
  635. toolNames.push_back("MOC");
  636. }
  637. if (digest.UicEnabled) {
  638. toolNames.push_back("UIC");
  639. }
  640. if (digest.RccEnabled) {
  641. toolNames.push_back("RCC");
  642. }
  643. std::string tools = toolNames[0];
  644. toolNames.erase(toolNames.begin());
  645. while (toolNames.size() > 1) {
  646. tools += ", " + toolNames[0];
  647. toolNames.erase(toolNames.begin());
  648. }
  649. if (toolNames.size() == 1) {
  650. tools += " and " + toolNames[0];
  651. }
  652. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  653. }
  654. // Add moc compilation to generated files list
  655. if (digest.MocEnabled) {
  656. const std::string mocsComp = autogenBuildDir + "/mocs_compilation.cpp";
  657. AddGeneratedSource(target, mocsComp, cmQtAutoGen::MOC);
  658. autogenProvides.push_back(mocsComp);
  659. }
  660. // Add autogen includes directory to the origin target INCLUDE_DIRECTORIES
  661. if (digest.MocEnabled || digest.UicEnabled) {
  662. std::string includeDir = autogenBuildDir + "/include";
  663. if (multiConfig) {
  664. includeDir += "_$<CONFIG>";
  665. }
  666. target->AddIncludeDirectory(includeDir, true);
  667. }
  668. // Extract relevant source files
  669. {
  670. const std::string qrcExt = "qrc";
  671. std::vector<cmSourceFile*> srcFiles;
  672. target->GetConfigCommonSourceFiles(srcFiles);
  673. for (cmSourceFile* sf : srcFiles) {
  674. if (sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  675. continue;
  676. }
  677. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  678. const std::string& fPath = sf->GetFullPath();
  679. const std::string& ext = sf->GetExtension();
  680. // Register generated files that will be scanned by moc or uic
  681. if (digest.MocEnabled || digest.UicEnabled) {
  682. const cmSystemTools::FileFormat fileType =
  683. cmSystemTools::GetFileFormat(ext.c_str());
  684. if ((fileType == cmSystemTools::CXX_FILE_FORMAT) ||
  685. (fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  686. const std::string absPath = cmSystemTools::GetRealPath(fPath);
  687. if ((digest.MocEnabled && !sf->GetPropertyAsBool("SKIP_AUTOMOC")) ||
  688. (digest.UicEnabled && !sf->GetPropertyAsBool("SKIP_AUTOUIC"))) {
  689. // Register source
  690. const bool generated = sf->GetPropertyAsBool("GENERATED");
  691. if (fileType == cmSystemTools::HEADER_FILE_FORMAT) {
  692. if (generated) {
  693. digest.HeadersGenerated.push_back(absPath);
  694. } else {
  695. digest.Headers.push_back(absPath);
  696. }
  697. } else {
  698. if (generated) {
  699. digest.SourcesGenerated.push_back(absPath);
  700. } else {
  701. digest.Sources.push_back(absPath);
  702. }
  703. }
  704. }
  705. }
  706. }
  707. // Register rcc enabled files
  708. if (digest.RccEnabled && (ext == qrcExt) &&
  709. !sf->GetPropertyAsBool("SKIP_AUTORCC")) {
  710. // Register qrc file
  711. {
  712. cmQtAutoGenDigestQrc qrcDigest;
  713. qrcDigest.QrcFile = cmSystemTools::GetRealPath(fPath);
  714. qrcDigest.Generated = sf->GetPropertyAsBool("GENERATED");
  715. // RCC options
  716. {
  717. const std::string opts = GetSafeProperty(sf, "AUTORCC_OPTIONS");
  718. if (!opts.empty()) {
  719. cmSystemTools::ExpandListArgument(opts, qrcDigest.Options);
  720. }
  721. }
  722. digest.Qrcs.push_back(std::move(qrcDigest));
  723. }
  724. }
  725. }
  726. // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
  727. // sources meta data cache. Clear it so that OBJECT library targets that
  728. // are AUTOGEN initialized after this target get their added
  729. // mocs_compilation.cpp source acknowledged by this target.
  730. target->ClearSourcesCache();
  731. }
  732. // Process GENERATED sources and headers
  733. if (!digest.SourcesGenerated.empty() || !digest.HeadersGenerated.empty()) {
  734. // Check status of policy CMP0071
  735. bool policyAccept = false;
  736. bool policyWarn = false;
  737. const cmPolicies::PolicyStatus CMP0071_status =
  738. target->Makefile->GetPolicyStatus(cmPolicies::CMP0071);
  739. switch (CMP0071_status) {
  740. case cmPolicies::WARN:
  741. policyWarn = true;
  742. CM_FALLTHROUGH;
  743. case cmPolicies::OLD:
  744. // Ignore GENERATED file
  745. break;
  746. case cmPolicies::REQUIRED_IF_USED:
  747. case cmPolicies::REQUIRED_ALWAYS:
  748. case cmPolicies::NEW:
  749. // Process GENERATED file
  750. policyAccept = true;
  751. break;
  752. }
  753. if (policyAccept) {
  754. // Accept GENERATED sources
  755. for (const std::string& absFile : digest.HeadersGenerated) {
  756. digest.Headers.push_back(absFile);
  757. }
  758. for (const std::string& absFile : digest.SourcesGenerated) {
  759. digest.Sources.push_back(absFile);
  760. }
  761. } else if (policyWarn) {
  762. std::ostringstream ost;
  763. ost << cmPolicies::GetPolicyWarning(cmPolicies::CMP0071) << "\n";
  764. ost << "AUTOMOC,AUTOUIC: Ignoring GENERATED source file(s):\n";
  765. for (const std::string& absFile : digest.HeadersGenerated) {
  766. ost << " " << cmQtAutoGen::Quoted(absFile) << "\n";
  767. }
  768. for (const std::string& absFile : digest.SourcesGenerated) {
  769. ost << " " << cmQtAutoGen::Quoted(absFile) << "\n";
  770. }
  771. makefile->IssueMessage(cmake::AUTHOR_WARNING, ost.str());
  772. }
  773. // Depend on GENERATED sources even when they are not processed by AUTOGEN
  774. for (const std::string& absFile : digest.HeadersGenerated) {
  775. autogenDepends.insert(absFile);
  776. }
  777. for (const std::string& absFile : digest.SourcesGenerated) {
  778. autogenDepends.insert(absFile);
  779. }
  780. }
  781. // Sort headers and sources
  782. std::sort(digest.Headers.begin(), digest.Headers.end());
  783. std::sort(digest.Sources.begin(), digest.Sources.end());
  784. // Process qrc files
  785. if (!digest.Qrcs.empty()) {
  786. const bool QtV5 = (digest.QtVersionMajor == "5");
  787. const cmFilePathChecksum fpathCheckSum(makefile);
  788. const std::string rcc = RccGetExecutable(target, digest.QtVersionMajor);
  789. // Target rcc options
  790. std::vector<std::string> optionsTarget;
  791. cmSystemTools::ExpandListArgument(
  792. GetSafeProperty(target, "AUTORCC_OPTIONS"), optionsTarget);
  793. for (cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  794. // RCC output file name
  795. {
  796. std::string rccFile = autogenBuildDir + "/";
  797. rccFile += fpathCheckSum.getPart(qrcDigest.QrcFile);
  798. rccFile += "/qrc_";
  799. rccFile +=
  800. cmSystemTools::GetFilenameWithoutLastExtension(qrcDigest.QrcFile);
  801. rccFile += ".cpp";
  802. AddGeneratedSource(target, rccFile, cmQtAutoGen::RCC);
  803. autogenProvides.push_back(rccFile);
  804. qrcDigest.RccFile = std::move(rccFile);
  805. }
  806. // RCC options
  807. {
  808. std::vector<std::string> opts = optionsTarget;
  809. if (!qrcDigest.Options.empty()) {
  810. RccMergeOptions(opts, qrcDigest.Options, QtV5);
  811. }
  812. qrcDigest.Options = std::move(opts);
  813. }
  814. // GENERATED or not
  815. if (qrcDigest.Generated) {
  816. // Add GENERATED qrc file to the dependencies
  817. autogenDepends.insert(qrcDigest.QrcFile);
  818. } else {
  819. // Add the resource files to the dependencies
  820. {
  821. std::string error;
  822. if (cmQtAutoGen::RccListInputs(digest.QtVersionMajor, rcc,
  823. qrcDigest.QrcFile,
  824. qrcDigest.Resources, &error)) {
  825. autogenDepends.insert(qrcDigest.Resources.begin(),
  826. qrcDigest.Resources.end());
  827. } else {
  828. cmSystemTools::Error(error.c_str());
  829. }
  830. }
  831. // Run cmake again when .qrc file changes
  832. makefile->AddCMakeDependFile(qrcDigest.QrcFile);
  833. }
  834. }
  835. }
  836. // Add user defined autogen target dependencies
  837. {
  838. const std::string deps = GetSafeProperty(target, "AUTOGEN_TARGET_DEPENDS");
  839. if (!deps.empty()) {
  840. std::vector<std::string> extraDeps;
  841. cmSystemTools::ExpandListArgument(deps, extraDeps);
  842. autogenDepends.insert(extraDeps.begin(), extraDeps.end());
  843. }
  844. }
  845. // Add utility target dependencies to the autogen target dependencies
  846. {
  847. const std::set<std::string>& utils = target->Target->GetUtilities();
  848. for (const std::string& targetName : utils) {
  849. if (makefile->FindTargetToUse(targetName) != nullptr) {
  850. autogenDepends.insert(targetName);
  851. }
  852. }
  853. }
  854. // Add link library target dependencies to the autogen target dependencies
  855. {
  856. const auto& libVec = target->Target->GetOriginalLinkLibraries();
  857. for (const auto& item : libVec) {
  858. if (makefile->FindTargetToUse(item.first) != nullptr) {
  859. autogenDepends.insert(item.first);
  860. }
  861. }
  862. }
  863. // Convert std::set to std::vector
  864. const std::vector<std::string> depends(autogenDepends.begin(),
  865. autogenDepends.end());
  866. autogenDepends.clear();
  867. // Use PRE_BUILD on demand
  868. bool usePRE_BUILD = false;
  869. if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
  870. // Under VS use a PRE_BUILD event instead of a separate target to
  871. // reduce the number of targets loaded into the IDE.
  872. // This also works around a VS 11 bug that may skip updating the target:
  873. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  874. usePRE_BUILD = true;
  875. }
  876. // Disable PRE_BUILD in some cases
  877. if (usePRE_BUILD) {
  878. // - Cannot use PRE_BUILD with GENERATED qrc files because the
  879. // resource files themselves may not be sources within the target
  880. // so VS may not know the target needs to re-build at all.
  881. for (cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  882. if (qrcDigest.Generated) {
  883. usePRE_BUILD = false;
  884. break;
  885. }
  886. }
  887. }
  888. // Create the autogen target/command
  889. if (usePRE_BUILD) {
  890. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  891. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  892. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  893. const std::vector<std::string> no_output;
  894. cmCustomCommand cc(makefile, no_output, autogenProvides, depends,
  895. commandLines, autogenComment.c_str(),
  896. workingDirectory.c_str());
  897. cc.SetEscapeOldStyle(false);
  898. cc.SetEscapeAllowMakeVars(true);
  899. target->Target->AddPreBuildCommand(cc);
  900. } else {
  901. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  902. autogenTargetName, true, workingDirectory.c_str(),
  903. /*byproducts=*/autogenProvides, depends, commandLines, false,
  904. autogenComment.c_str());
  905. localGen->AddGeneratorTarget(
  906. new cmGeneratorTarget(autogenTarget, localGen));
  907. // Set autogen target FOLDER
  908. {
  909. const char* autogenFolder =
  910. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  911. if (autogenFolder == nullptr) {
  912. autogenFolder =
  913. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  914. }
  915. // Inherit FOLDER property from target (#13688)
  916. if (autogenFolder == nullptr) {
  917. autogenFolder = SafeString(target->Target->GetProperty("FOLDER"));
  918. }
  919. if ((autogenFolder != nullptr) && (*autogenFolder != '\0')) {
  920. autogenTarget->SetProperty("FOLDER", autogenFolder);
  921. }
  922. }
  923. // Add autogen target to the origin target dependencies
  924. target->Target->AddUtility(autogenTargetName);
  925. }
  926. }
  927. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  928. const cmQtAutoGenDigest& digest)
  929. {
  930. cmGeneratorTarget const* target = digest.Target;
  931. cmMakefile* makefile = target->Target->GetMakefile();
  932. // forget the variables added here afterwards again:
  933. cmMakefile::ScopePushPop varScope(makefile);
  934. static_cast<void>(varScope);
  935. // Get configurations
  936. std::string config;
  937. const std::vector<std::string> configs(GetConfigurations(makefile, &config));
  938. // Configuration suffixes
  939. std::map<std::string, std::string> configSuffix;
  940. if (AutogenMultiConfig(target->GetGlobalGenerator())) {
  941. for (const std::string& cfg : configs) {
  942. configSuffix[cfg] = "_" + cfg;
  943. }
  944. }
  945. // Configurations settings buffers
  946. cmQtAutoGenSetup setup;
  947. // Basic setup
  948. AddDefinitionEscaped(makefile, "_build_dir",
  949. GetAutogenTargetBuildDir(target));
  950. AddDefinitionEscaped(makefile, "_qt_version_major", digest.QtVersionMajor);
  951. AddDefinitionEscaped(makefile, "_qt_version_minor", digest.QtVersionMinor);
  952. AddDefinitionEscaped(makefile, "_sources", digest.Sources);
  953. AddDefinitionEscaped(makefile, "_headers", digest.Headers);
  954. {
  955. if (digest.MocEnabled || digest.UicEnabled) {
  956. SetupAcquireSkipFiles(digest, setup);
  957. if (digest.MocEnabled) {
  958. SetupAutoTargetMoc(digest, config, configs, setup);
  959. }
  960. if (digest.UicEnabled) {
  961. SetupAutoTargetUic(digest, config, configs, setup);
  962. }
  963. }
  964. if (digest.RccEnabled) {
  965. SetupAutoTargetRcc(digest);
  966. }
  967. }
  968. // Generate info file
  969. std::string infoFile = GetAutogenTargetFilesDir(target);
  970. infoFile += "/AutogenInfo.cmake";
  971. {
  972. std::string inf = cmSystemTools::GetCMakeRoot();
  973. inf += "/Modules/AutogenInfo.cmake.in";
  974. makefile->ConfigureFile(inf.c_str(), infoFile.c_str(), false, true, false);
  975. }
  976. // Append custom definitions to info file on demand
  977. if (!configSuffix.empty() || !setup.ConfigMocDefines.empty() ||
  978. !setup.ConfigMocIncludes.empty() || !setup.ConfigUicOptions.empty()) {
  979. // Ensure we have write permission in case .in was read-only.
  980. mode_t perm = 0;
  981. #if defined(_WIN32) && !defined(__CYGWIN__)
  982. mode_t mode_write = S_IWRITE;
  983. #else
  984. mode_t mode_write = S_IWUSR;
  985. #endif
  986. cmSystemTools::GetPermissions(infoFile, perm);
  987. if (!(perm & mode_write)) {
  988. cmSystemTools::SetPermissions(infoFile, perm | mode_write);
  989. }
  990. // Open and write file
  991. cmsys::ofstream ofs(infoFile.c_str(), std::ios::app);
  992. if (ofs) {
  993. ofs << "# Configuration specific options\n";
  994. for (const auto& item : configSuffix) {
  995. ofs << "set(AM_CONFIG_SUFFIX_" << item.first << " "
  996. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  997. }
  998. for (const auto& item : setup.ConfigMocDefines) {
  999. ofs << "set(AM_MOC_DEFINITIONS_" << item.first << " "
  1000. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  1001. }
  1002. for (const auto& item : setup.ConfigMocIncludes) {
  1003. ofs << "set(AM_MOC_INCLUDES_" << item.first << " "
  1004. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  1005. }
  1006. for (const auto& item : setup.ConfigUicOptions) {
  1007. ofs << "set(AM_UIC_TARGET_OPTIONS_" << item.first << " "
  1008. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  1009. }
  1010. } else {
  1011. // File open error
  1012. std::string error = "Internal CMake error when trying to open file: ";
  1013. error += cmQtAutoGen::Quoted(infoFile);
  1014. error += " for writing.";
  1015. cmSystemTools::Error(error.c_str());
  1016. }
  1017. }
  1018. }