1
0

cmQtAutoGeneratorInitializer.cxx 39 KB

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