cmQtAutoGeneratorInitializer.cxx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. // Compiler predefines
  335. if (target->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES")) {
  336. if (QtVersionGreaterOrEqual(digest.QtVersionMajor, digest.QtVersionMinor,
  337. 5, 8)) {
  338. AddDefinitionEscaped(
  339. makefile, "_moc_predefs_cmd",
  340. makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
  341. }
  342. }
  343. // Moc includes and compile definitions
  344. {
  345. auto GetIncludeDirs = [target,
  346. localGen](std::string const& cfg) -> std::string {
  347. // Get the include dirs for this target, without stripping the implicit
  348. // include dirs off, see
  349. // https://gitlab.kitware.com/cmake/cmake/issues/13667
  350. std::vector<std::string> includeDirs;
  351. localGen->GetIncludeDirectories(includeDirs, target, "CXX", cfg, false);
  352. return cmJoin(includeDirs, ";");
  353. };
  354. auto GetCompileDefinitions =
  355. [target, localGen](std::string const& cfg) -> std::string {
  356. std::set<std::string> defines;
  357. localGen->AddCompileDefinitions(defines, target, cfg, "CXX");
  358. return cmJoin(defines, ";");
  359. };
  360. // Default configuration settings
  361. std::string const includeDirs = GetIncludeDirs(configDefault);
  362. std::string const compileDefs = GetCompileDefinitions(configDefault);
  363. // Other configuration settings
  364. for (std::string const& cfg : configsList) {
  365. {
  366. std::string const configIncludeDirs = GetIncludeDirs(cfg);
  367. if (configIncludeDirs != includeDirs) {
  368. setup.ConfigMocIncludes[cfg] = configIncludeDirs;
  369. }
  370. }
  371. {
  372. std::string const configCompileDefs = GetCompileDefinitions(cfg);
  373. if (configCompileDefs != compileDefs) {
  374. setup.ConfigMocDefines[cfg] = configCompileDefs;
  375. }
  376. }
  377. }
  378. AddDefinitionEscaped(makefile, "_moc_include_dirs", includeDirs);
  379. AddDefinitionEscaped(makefile, "_moc_compile_defs", compileDefs);
  380. }
  381. // Moc executable
  382. {
  383. std::string mocExec;
  384. std::string err;
  385. if (digest.QtVersionMajor == "5") {
  386. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::moc");
  387. if (tgt != nullptr) {
  388. mocExec = SafeString(tgt->ImportedGetLocation(""));
  389. } else {
  390. err = "AUTOMOC: Qt5::moc target not found";
  391. }
  392. } else if (digest.QtVersionMajor == "4") {
  393. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::moc");
  394. if (tgt != nullptr) {
  395. mocExec = SafeString(tgt->ImportedGetLocation(""));
  396. } else {
  397. err = "AUTOMOC: Qt4::moc target not found";
  398. }
  399. } else {
  400. err = "The AUTOMOC feature supports only Qt 4 and Qt 5";
  401. }
  402. if (err.empty()) {
  403. AddDefinitionEscaped(makefile, "_qt_moc_executable", mocExec);
  404. } else {
  405. err += " (" + target->GetName() + ")";
  406. cmSystemTools::Error(err.c_str());
  407. }
  408. }
  409. }
  410. static void SetupAutoTargetUic(cmQtAutoGenDigest const& digest,
  411. std::string const& config,
  412. std::vector<std::string> const& configs,
  413. cmQtAutoGenSetup& setup)
  414. {
  415. cmGeneratorTarget const* target = digest.Target;
  416. cmMakefile* makefile = target->Target->GetMakefile();
  417. AddDefinitionEscaped(makefile, "_uic_skip", setup.UicSkip);
  418. // Uic search paths
  419. {
  420. std::vector<std::string> uicSearchPaths;
  421. {
  422. std::string const usp = GetSafeProperty(target, "AUTOUIC_SEARCH_PATHS");
  423. if (!usp.empty()) {
  424. cmSystemTools::ExpandListArgument(usp, uicSearchPaths);
  425. std::string const srcDir = makefile->GetCurrentSourceDirectory();
  426. for (std::string& path : uicSearchPaths) {
  427. path = cmSystemTools::CollapseFullPath(path, srcDir);
  428. }
  429. }
  430. }
  431. AddDefinitionEscaped(makefile, "_uic_search_paths", uicSearchPaths);
  432. }
  433. // Uic target options
  434. {
  435. auto UicGetOpts = [target](std::string const& cfg) -> std::string {
  436. std::vector<std::string> opts;
  437. target->GetAutoUicOptions(opts, cfg);
  438. return cmJoin(opts, ";");
  439. };
  440. // Default settings
  441. std::string const uicOpts = UicGetOpts(config);
  442. AddDefinitionEscaped(makefile, "_uic_target_options", uicOpts);
  443. // Configuration specific settings
  444. for (std::string const& cfg : configs) {
  445. std::string const configUicOpts = UicGetOpts(cfg);
  446. if (configUicOpts != uicOpts) {
  447. setup.ConfigUicOptions[cfg] = configUicOpts;
  448. }
  449. }
  450. }
  451. // Uic files options
  452. {
  453. std::vector<std::string> uiFileFiles;
  454. std::vector<std::vector<std::string>> uiFileOptions;
  455. {
  456. std::string const uiExt = "ui";
  457. const std::vector<cmSourceFile*>& srcFiles = makefile->GetSourceFiles();
  458. for (cmSourceFile* sf : srcFiles) {
  459. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  460. std::string const& fPath = sf->GetFullPath();
  461. if (sf->GetExtension() == uiExt) {
  462. // Check if the files has uic options
  463. std::string const uicOpts = GetSafeProperty(sf, "AUTOUIC_OPTIONS");
  464. if (!uicOpts.empty()) {
  465. std::string const absFile = cmSystemTools::GetRealPath(fPath);
  466. // Check if file isn't skipped
  467. if (setup.UicSkip.count(absFile) == 0) {
  468. uiFileFiles.push_back(absFile);
  469. std::vector<std::string> optsVec;
  470. cmSystemTools::ExpandListArgument(uicOpts, optsVec);
  471. uiFileOptions.push_back(std::move(optsVec));
  472. }
  473. }
  474. }
  475. }
  476. }
  477. AddDefinitionEscaped(makefile, "_qt_uic_options_files", uiFileFiles);
  478. AddDefinitionEscaped(makefile, "_qt_uic_options_options", uiFileOptions);
  479. }
  480. // Uic executable
  481. {
  482. std::string err;
  483. std::string uicExec;
  484. cmLocalGenerator* localGen = target->GetLocalGenerator();
  485. if (digest.QtVersionMajor == "5") {
  486. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::uic");
  487. if (tgt != nullptr) {
  488. uicExec = SafeString(tgt->ImportedGetLocation(""));
  489. } else {
  490. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  491. }
  492. } else if (digest.QtVersionMajor == "4") {
  493. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::uic");
  494. if (tgt != nullptr) {
  495. uicExec = SafeString(tgt->ImportedGetLocation(""));
  496. } else {
  497. err = "AUTOUIC: Qt4::uic target not found";
  498. }
  499. } else {
  500. err = "The AUTOUIC feature supports only Qt 4 and Qt 5";
  501. }
  502. if (err.empty()) {
  503. AddDefinitionEscaped(makefile, "_qt_uic_executable", uicExec);
  504. } else {
  505. err += " (" + target->GetName() + ")";
  506. cmSystemTools::Error(err.c_str());
  507. }
  508. }
  509. }
  510. static std::string RccGetExecutable(cmGeneratorTarget const* target,
  511. std::string const& qtMajorVersion)
  512. {
  513. std::string rccExec;
  514. std::string err;
  515. cmLocalGenerator* localGen = target->GetLocalGenerator();
  516. if (qtMajorVersion == "5") {
  517. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt5::rcc");
  518. if (tgt != nullptr) {
  519. rccExec = SafeString(tgt->ImportedGetLocation(""));
  520. } else {
  521. err = "AUTORCC: Qt5::rcc target not found";
  522. }
  523. } else if (qtMajorVersion == "4") {
  524. cmGeneratorTarget* tgt = localGen->FindGeneratorTargetToUse("Qt4::rcc");
  525. if (tgt != nullptr) {
  526. rccExec = SafeString(tgt->ImportedGetLocation(""));
  527. } else {
  528. err = "AUTORCC: Qt4::rcc target not found";
  529. }
  530. } else {
  531. err = "The AUTORCC feature supports only Qt 4 and Qt 5";
  532. }
  533. if (!err.empty()) {
  534. err += " (" + target->GetName() + ")";
  535. cmSystemTools::Error(err.c_str());
  536. }
  537. return rccExec;
  538. }
  539. static void SetupAutoTargetRcc(cmQtAutoGenDigest const& digest)
  540. {
  541. std::vector<std::string> rccFiles;
  542. std::vector<std::string> rccBuilds;
  543. std::vector<std::vector<std::string>> rccOptions;
  544. std::vector<std::vector<std::string>> rccInputs;
  545. for (cmQtAutoGenDigestQrc const& qrcDigest : digest.Qrcs) {
  546. rccFiles.push_back(qrcDigest.QrcFile);
  547. rccBuilds.push_back(qrcDigest.RccFile);
  548. rccOptions.push_back(qrcDigest.Options);
  549. rccInputs.push_back(qrcDigest.Resources);
  550. }
  551. cmMakefile* makefile = digest.Target->Target->GetMakefile();
  552. AddDefinitionEscaped(makefile, "_qt_rcc_executable",
  553. RccGetExecutable(digest.Target, digest.QtVersionMajor));
  554. AddDefinitionEscaped(makefile, "_rcc_files", rccFiles);
  555. AddDefinitionEscaped(makefile, "_rcc_builds", rccBuilds);
  556. AddDefinitionEscaped(makefile, "_rcc_options", rccOptions);
  557. AddDefinitionEscaped(makefile, "_rcc_inputs", rccInputs);
  558. }
  559. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  560. cmQtAutoGenDigest& digest)
  561. {
  562. cmGeneratorTarget* target = digest.Target;
  563. cmMakefile* makefile = target->Target->GetMakefile();
  564. cmLocalGenerator* localGen = target->GetLocalGenerator();
  565. cmGlobalGenerator* globalGen = localGen->GetGlobalGenerator();
  566. std::string const autogenTargetName = GetAutogenTargetName(target);
  567. std::string const autogenInfoDir = GetAutogenTargetFilesDir(target);
  568. std::string const autogenBuildDir = GetAutogenTargetBuildDir(target);
  569. std::string const workingDirectory =
  570. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  571. cmQtAutoGen::MultiConfig const multiConfig = AutogenMultiConfig(globalGen);
  572. std::string configDefault;
  573. std::vector<std::string> configsList;
  574. GetConfigs(makefile, configDefault, configsList);
  575. std::set<std::string> autogenDependFiles;
  576. std::set<std::string> autogenDependTargets;
  577. std::vector<std::string> autogenProvides;
  578. // Remove build directories on cleanup
  579. AddCleanFile(makefile, autogenBuildDir);
  580. // Remove old settings on cleanup
  581. {
  582. std::string base = autogenInfoDir + "/AutogenOldSettings";
  583. if (multiConfig == cmQtAutoGen::SINGLE) {
  584. AddCleanFile(makefile, base.append(".cmake"));
  585. } else {
  586. for (std::string const& cfg : configsList) {
  587. std::string filename = base;
  588. filename += "_";
  589. filename += cfg;
  590. filename += ".cmake";
  591. AddCleanFile(makefile, filename);
  592. }
  593. }
  594. }
  595. // Compose command lines
  596. cmCustomCommandLines commandLines;
  597. {
  598. cmCustomCommandLine currentLine;
  599. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  600. currentLine.push_back("-E");
  601. currentLine.push_back("cmake_autogen");
  602. currentLine.push_back(autogenInfoDir);
  603. currentLine.push_back("$<CONFIGURATION>");
  604. commandLines.push_back(currentLine);
  605. }
  606. // Compose target comment
  607. std::string autogenComment;
  608. {
  609. std::vector<std::string> toolNames;
  610. if (digest.MocEnabled) {
  611. toolNames.emplace_back("MOC");
  612. }
  613. if (digest.UicEnabled) {
  614. toolNames.emplace_back("UIC");
  615. }
  616. if (digest.RccEnabled) {
  617. toolNames.emplace_back("RCC");
  618. }
  619. std::string tools = toolNames.front();
  620. toolNames.erase(toolNames.begin());
  621. if (!toolNames.empty()) {
  622. while (toolNames.size() > 1) {
  623. tools += ", ";
  624. tools += toolNames.front();
  625. toolNames.erase(toolNames.begin());
  626. }
  627. tools += " and " + toolNames.front();
  628. }
  629. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  630. }
  631. // Add moc compilation to generated files list
  632. if (digest.MocEnabled) {
  633. std::string const mocsComp = autogenBuildDir + "/mocs_compilation.cpp";
  634. auto files = AddGeneratedSource(target, mocsComp, multiConfig, configsList,
  635. cmQtAutoGen::MOC);
  636. for (std::string& file : files) {
  637. autogenProvides.push_back(std::move(file));
  638. }
  639. }
  640. // Add autogen includes directory to the origin target INCLUDE_DIRECTORIES
  641. if (digest.MocEnabled || digest.UicEnabled) {
  642. std::string includeDir = autogenBuildDir + "/include";
  643. if (multiConfig != cmQtAutoGen::SINGLE) {
  644. includeDir += "_$<CONFIG>";
  645. }
  646. target->AddIncludeDirectory(includeDir, true);
  647. }
  648. // Extract relevant source files
  649. std::vector<std::string> generatedSources;
  650. std::vector<std::string> generatedHeaders;
  651. {
  652. std::string const qrcExt = "qrc";
  653. std::vector<cmSourceFile*> srcFiles;
  654. target->GetConfigCommonSourceFiles(srcFiles);
  655. for (cmSourceFile* sf : srcFiles) {
  656. if (sf->GetPropertyAsBool("SKIP_AUTOGEN")) {
  657. continue;
  658. }
  659. // sf->GetExtension() is only valid after sf->GetFullPath() ...
  660. std::string const& fPath = sf->GetFullPath();
  661. std::string const& ext = sf->GetExtension();
  662. // Register generated files that will be scanned by moc or uic
  663. if (digest.MocEnabled || digest.UicEnabled) {
  664. cmSystemTools::FileFormat const fileType =
  665. cmSystemTools::GetFileFormat(ext.c_str());
  666. if ((fileType == cmSystemTools::CXX_FILE_FORMAT) ||
  667. (fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  668. std::string const absPath = cmSystemTools::GetRealPath(fPath);
  669. if ((digest.MocEnabled && !sf->GetPropertyAsBool("SKIP_AUTOMOC")) ||
  670. (digest.UicEnabled && !sf->GetPropertyAsBool("SKIP_AUTOUIC"))) {
  671. // Register source
  672. const bool generated = sf->GetPropertyAsBool("GENERATED");
  673. if (fileType == cmSystemTools::HEADER_FILE_FORMAT) {
  674. if (generated) {
  675. generatedHeaders.push_back(absPath);
  676. } else {
  677. digest.Headers.push_back(absPath);
  678. }
  679. } else {
  680. if (generated) {
  681. generatedSources.push_back(absPath);
  682. } else {
  683. digest.Sources.push_back(absPath);
  684. }
  685. }
  686. }
  687. }
  688. }
  689. // Register rcc enabled files
  690. if (digest.RccEnabled && (ext == qrcExt) &&
  691. !sf->GetPropertyAsBool("SKIP_AUTORCC")) {
  692. // Register qrc file
  693. {
  694. cmQtAutoGenDigestQrc qrcDigest;
  695. qrcDigest.QrcFile = cmSystemTools::GetRealPath(fPath);
  696. qrcDigest.QrcName =
  697. cmSystemTools::GetFilenameWithoutLastExtension(qrcDigest.QrcFile);
  698. qrcDigest.Generated = sf->GetPropertyAsBool("GENERATED");
  699. // RCC options
  700. {
  701. std::string const opts = GetSafeProperty(sf, "AUTORCC_OPTIONS");
  702. if (!opts.empty()) {
  703. cmSystemTools::ExpandListArgument(opts, qrcDigest.Options);
  704. }
  705. }
  706. digest.Qrcs.push_back(std::move(qrcDigest));
  707. }
  708. }
  709. }
  710. // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
  711. // sources meta data cache. Clear it so that OBJECT library targets that
  712. // are AUTOGEN initialized after this target get their added
  713. // mocs_compilation.cpp source acknowledged by this target.
  714. target->ClearSourcesCache();
  715. }
  716. // Process GENERATED sources and headers
  717. if (!generatedSources.empty() || !generatedHeaders.empty()) {
  718. // Check status of policy CMP0071
  719. bool policyAccept = false;
  720. bool policyWarn = false;
  721. cmPolicies::PolicyStatus const CMP0071_status =
  722. target->Makefile->GetPolicyStatus(cmPolicies::CMP0071);
  723. switch (CMP0071_status) {
  724. case cmPolicies::WARN:
  725. policyWarn = true;
  726. CM_FALLTHROUGH;
  727. case cmPolicies::OLD:
  728. // Ignore GENERATED file
  729. break;
  730. case cmPolicies::REQUIRED_IF_USED:
  731. case cmPolicies::REQUIRED_ALWAYS:
  732. case cmPolicies::NEW:
  733. // Process GENERATED file
  734. policyAccept = true;
  735. break;
  736. }
  737. if (policyAccept) {
  738. // Accept GENERATED sources
  739. for (std::string const& absFile : generatedHeaders) {
  740. digest.Headers.push_back(absFile);
  741. autogenDependFiles.insert(absFile);
  742. }
  743. for (std::string const& absFile : generatedSources) {
  744. digest.Sources.push_back(absFile);
  745. autogenDependFiles.insert(absFile);
  746. }
  747. } else {
  748. if (policyWarn) {
  749. std::string msg;
  750. msg += cmPolicies::GetPolicyWarning(cmPolicies::CMP0071);
  751. msg += "\n";
  752. std::string tools;
  753. std::string property;
  754. if (digest.MocEnabled && digest.UicEnabled) {
  755. tools = "AUTOMOC and AUTOUIC";
  756. property = "SKIP_AUTOGEN";
  757. } else if (digest.MocEnabled) {
  758. tools = "AUTOMOC";
  759. property = "SKIP_AUTOMOC";
  760. } else if (digest.UicEnabled) {
  761. tools = "AUTOUIC";
  762. property = "SKIP_AUTOUIC";
  763. }
  764. msg += "For compatibility, CMake is excluding the GENERATED source "
  765. "file(s):\n";
  766. for (const std::string& absFile : generatedHeaders) {
  767. msg.append(" ").append(cmQtAutoGen::Quoted(absFile)).append("\n");
  768. }
  769. for (const std::string& absFile : generatedSources) {
  770. msg.append(" ").append(cmQtAutoGen::Quoted(absFile)).append("\n");
  771. }
  772. msg += "from processing by ";
  773. msg += tools;
  774. msg +=
  775. ". If any of the files should be processed, set CMP0071 to NEW. "
  776. "If any of the files should not be processed, "
  777. "explicitly exclude them by setting the source file property ";
  778. msg += property;
  779. msg += ":\n set_property(SOURCE file.h PROPERTY ";
  780. msg += property;
  781. msg += " ON)\n";
  782. makefile->IssueMessage(cmake::AUTHOR_WARNING, msg);
  783. }
  784. }
  785. }
  786. // Sort headers and sources
  787. std::sort(digest.Headers.begin(), digest.Headers.end());
  788. std::sort(digest.Sources.begin(), digest.Sources.end());
  789. // Process qrc files
  790. if (!digest.Qrcs.empty()) {
  791. const bool QtV5 = (digest.QtVersionMajor == "5");
  792. std::string const rcc = RccGetExecutable(target, digest.QtVersionMajor);
  793. // Target rcc options
  794. std::vector<std::string> optionsTarget;
  795. cmSystemTools::ExpandListArgument(
  796. GetSafeProperty(target, "AUTORCC_OPTIONS"), optionsTarget);
  797. // Check if file name is unique
  798. for (cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  799. qrcDigest.Unique = true;
  800. for (cmQtAutoGenDigestQrc const& qrcDig2 : digest.Qrcs) {
  801. if ((&qrcDigest != &qrcDig2) &&
  802. (qrcDigest.QrcName == qrcDig2.QrcName)) {
  803. qrcDigest.Unique = false;
  804. break;
  805. }
  806. }
  807. }
  808. // Path checksum
  809. {
  810. cmFilePathChecksum const fpathCheckSum(makefile);
  811. for (cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  812. qrcDigest.PathChecksum = fpathCheckSum.getPart(qrcDigest.QrcFile);
  813. // RCC output file name
  814. std::string rccFile = autogenBuildDir + "/";
  815. rccFile += qrcDigest.PathChecksum;
  816. rccFile += "/qrc_";
  817. rccFile += qrcDigest.QrcName;
  818. rccFile += ".cpp";
  819. qrcDigest.RccFile = std::move(rccFile);
  820. }
  821. }
  822. // RCC options
  823. for (cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  824. // Target options
  825. std::vector<std::string> opts = optionsTarget;
  826. // Merge computed "-name XYZ" option
  827. {
  828. std::string name = qrcDigest.QrcName;
  829. // Replace '-' with '_'. The former is not valid for symbol names.
  830. std::replace(name.begin(), name.end(), '-', '_');
  831. if (!qrcDigest.Unique) {
  832. name += "_";
  833. name += qrcDigest.PathChecksum;
  834. }
  835. std::vector<std::string> nameOpts;
  836. nameOpts.emplace_back("-name");
  837. nameOpts.emplace_back(std::move(name));
  838. cmQtAutoGen::RccMergeOptions(opts, nameOpts, QtV5);
  839. }
  840. // Merge file option
  841. cmQtAutoGen::RccMergeOptions(opts, qrcDigest.Options, QtV5);
  842. qrcDigest.Options = std::move(opts);
  843. }
  844. for (cmQtAutoGenDigestQrc& qrcDigest : digest.Qrcs) {
  845. // Register file at target
  846. {
  847. auto files = AddGeneratedSource(target, qrcDigest.RccFile, multiConfig,
  848. configsList, cmQtAutoGen::RCC);
  849. for (std::string& file : files) {
  850. autogenProvides.push_back(std::move(file));
  851. }
  852. }
  853. // Dependencies
  854. if (qrcDigest.Generated) {
  855. // Add the GENERATED .qrc file to the dependencies
  856. autogenDependFiles.insert(qrcDigest.QrcFile);
  857. } else {
  858. // Add the resource files to the dependencies
  859. {
  860. std::string error;
  861. if (cmQtAutoGen::RccListInputs(digest.QtVersionMajor, rcc,
  862. qrcDigest.QrcFile,
  863. qrcDigest.Resources, &error)) {
  864. for (std::string const& fileName : qrcDigest.Resources) {
  865. autogenDependFiles.insert(fileName);
  866. }
  867. } else {
  868. cmSystemTools::Error(error.c_str());
  869. }
  870. }
  871. // Run cmake again when .qrc file changes
  872. makefile->AddCMakeDependFile(qrcDigest.QrcFile);
  873. }
  874. }
  875. }
  876. // Add user defined autogen target dependencies
  877. {
  878. std::string const deps = GetSafeProperty(target, "AUTOGEN_TARGET_DEPENDS");
  879. if (!deps.empty()) {
  880. std::vector<std::string> extraDeps;
  881. cmSystemTools::ExpandListArgument(deps, extraDeps);
  882. for (std::string const& depName : extraDeps) {
  883. // Allow target and file dependencies
  884. auto* depTarget = makefile->FindTargetToUse(depName);
  885. if (depTarget != nullptr) {
  886. autogenDependTargets.insert(depTarget->GetName());
  887. } else {
  888. autogenDependFiles.insert(depName);
  889. }
  890. }
  891. }
  892. }
  893. // Use PRE_BUILD on demand
  894. bool usePRE_BUILD = false;
  895. if (globalGen->GetName().find("Visual Studio") != std::string::npos) {
  896. // Under VS use a PRE_BUILD event instead of a separate target to
  897. // reduce the number of targets loaded into the IDE.
  898. // This also works around a VS 11 bug that may skip updating the target:
  899. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  900. usePRE_BUILD = true;
  901. }
  902. // Disable PRE_BUILD in some cases
  903. if (usePRE_BUILD) {
  904. // Cannot use PRE_BUILD with file depends
  905. if (!autogenDependFiles.empty()) {
  906. usePRE_BUILD = false;
  907. }
  908. }
  909. // Create the autogen target/command
  910. if (usePRE_BUILD) {
  911. // Add additional autogen target dependencies to origin target
  912. for (std::string const& depTarget : autogenDependTargets) {
  913. target->Target->AddUtility(depTarget, makefile);
  914. }
  915. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  916. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  917. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  918. //
  919. // PRE_BUILD does not support file dependencies!
  920. const std::vector<std::string> no_output;
  921. const std::vector<std::string> no_deps;
  922. cmCustomCommand cc(makefile, no_output, autogenProvides, no_deps,
  923. commandLines, autogenComment.c_str(),
  924. workingDirectory.c_str());
  925. cc.SetEscapeOldStyle(false);
  926. cc.SetEscapeAllowMakeVars(true);
  927. target->Target->AddPreBuildCommand(cc);
  928. } else {
  929. // Add utility target dependencies to the autogen target dependencies
  930. for (std::string const& depTarget : target->Target->GetUtilities()) {
  931. autogenDependTargets.insert(depTarget);
  932. }
  933. // Add link library target dependencies to the autogen target dependencies
  934. for (const auto& item : target->Target->GetOriginalLinkLibraries()) {
  935. if (makefile->FindTargetToUse(item.first) != nullptr) {
  936. autogenDependTargets.insert(item.first);
  937. }
  938. }
  939. // Convert file dependencies std::set to std::vector
  940. const std::vector<std::string> autogenDepends(autogenDependFiles.begin(),
  941. autogenDependFiles.end());
  942. // Create autogen target
  943. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  944. autogenTargetName, true, workingDirectory.c_str(),
  945. /*byproducts=*/autogenProvides, autogenDepends, commandLines, false,
  946. autogenComment.c_str());
  947. // Create autogen generator target
  948. localGen->AddGeneratorTarget(
  949. new cmGeneratorTarget(autogenTarget, localGen));
  950. // Add additional autogen target dependencies to autogen target
  951. for (std::string const& depTarget : autogenDependTargets) {
  952. autogenTarget->AddUtility(depTarget, makefile);
  953. }
  954. // Set FOLDER property in autogen target
  955. {
  956. const char* autogenFolder =
  957. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  958. if (autogenFolder == nullptr) {
  959. autogenFolder =
  960. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  961. }
  962. // Inherit FOLDER property from target (#13688)
  963. if (autogenFolder == nullptr) {
  964. autogenFolder = SafeString(target->Target->GetProperty("FOLDER"));
  965. }
  966. if ((autogenFolder != nullptr) && (*autogenFolder != '\0')) {
  967. autogenTarget->SetProperty("FOLDER", autogenFolder);
  968. }
  969. }
  970. // Add autogen target to the origin target dependencies
  971. target->Target->AddUtility(autogenTargetName, makefile);
  972. }
  973. }
  974. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  975. cmQtAutoGenDigest const& digest)
  976. {
  977. cmGeneratorTarget const* target = digest.Target;
  978. cmMakefile* makefile = target->Target->GetMakefile();
  979. cmQtAutoGen::MultiConfig const multiConfig =
  980. AutogenMultiConfig(target->GetGlobalGenerator());
  981. // forget the variables added here afterwards again:
  982. cmMakefile::ScopePushPop varScope(makefile);
  983. static_cast<void>(varScope);
  984. // Configurations
  985. std::string configDefault;
  986. std::vector<std::string> configsList;
  987. std::map<std::string, std::string> configSuffixes;
  988. {
  989. configDefault = makefile->GetConfigurations(configsList);
  990. if (configsList.empty()) {
  991. configsList.push_back("");
  992. }
  993. }
  994. for (std::string const& cfg : configsList) {
  995. configSuffixes[cfg] = "_" + cfg;
  996. }
  997. // Configurations settings buffers
  998. cmQtAutoGenSetup setup;
  999. // Basic setup
  1000. AddDefinitionEscaped(makefile, "_multi_config",
  1001. cmQtAutoGen::MultiConfigName(multiConfig));
  1002. AddDefinitionEscaped(makefile, "_build_dir",
  1003. GetAutogenTargetBuildDir(target));
  1004. AddDefinitionEscaped(makefile, "_sources", digest.Sources);
  1005. AddDefinitionEscaped(makefile, "_headers", digest.Headers);
  1006. AddDefinitionEscaped(makefile, "_qt_version_major", digest.QtVersionMajor);
  1007. AddDefinitionEscaped(makefile, "_qt_version_minor", digest.QtVersionMinor);
  1008. {
  1009. if (digest.MocEnabled || digest.UicEnabled) {
  1010. SetupAcquireSkipFiles(digest, setup);
  1011. if (digest.MocEnabled) {
  1012. SetupAutoTargetMoc(digest, configDefault, configsList, setup);
  1013. }
  1014. if (digest.UicEnabled) {
  1015. SetupAutoTargetUic(digest, configDefault, configsList, setup);
  1016. }
  1017. }
  1018. if (digest.RccEnabled) {
  1019. SetupAutoTargetRcc(digest);
  1020. }
  1021. }
  1022. // Generate info file
  1023. {
  1024. std::string const infoDir = GetAutogenTargetFilesDir(target);
  1025. if (!cmSystemTools::MakeDirectory(infoDir)) {
  1026. std::string emsg = ("Could not create directory: ");
  1027. emsg += cmQtAutoGen::Quoted(infoDir);
  1028. cmSystemTools::Error(emsg.c_str());
  1029. }
  1030. std::string const infoFile = infoDir + "/AutogenInfo.cmake";
  1031. {
  1032. std::string infoFileIn = cmSystemTools::GetCMakeRoot();
  1033. infoFileIn += "/Modules/AutogenInfo.cmake.in";
  1034. makefile->ConfigureFile(infoFileIn.c_str(), infoFile.c_str(), false,
  1035. true, false);
  1036. }
  1037. // Append custom definitions to info file
  1038. // --------------------------------------
  1039. // Ensure we have write permission in case .in was read-only.
  1040. mode_t perm = 0;
  1041. #if defined(_WIN32) && !defined(__CYGWIN__)
  1042. mode_t mode_write = S_IWRITE;
  1043. #else
  1044. mode_t mode_write = S_IWUSR;
  1045. #endif
  1046. cmSystemTools::GetPermissions(infoFile, perm);
  1047. if (!(perm & mode_write)) {
  1048. cmSystemTools::SetPermissions(infoFile, perm | mode_write);
  1049. }
  1050. // Open and write file
  1051. cmsys::ofstream ofs(infoFile.c_str(), std::ios::app);
  1052. if (ofs) {
  1053. auto OfsWriteMap = [&ofs](
  1054. const char* key, std::map<std::string, std::string> const& map) {
  1055. for (auto const& item : map) {
  1056. ofs << "set(" << key << "_" << item.first << " "
  1057. << cmOutputConverter::EscapeForCMake(item.second) << ")\n";
  1058. }
  1059. };
  1060. ofs << "# Configurations options\n";
  1061. OfsWriteMap("AM_CONFIG_SUFFIX", configSuffixes);
  1062. OfsWriteMap("AM_MOC_DEFINITIONS", setup.ConfigMocDefines);
  1063. OfsWriteMap("AM_MOC_INCLUDES", setup.ConfigMocIncludes);
  1064. OfsWriteMap("AM_UIC_TARGET_OPTIONS", setup.ConfigUicOptions);
  1065. } else {
  1066. // File open error
  1067. std::string error = "Internal CMake error when trying to open file: ";
  1068. error += cmQtAutoGen::Quoted(infoFile);
  1069. error += " for writing.";
  1070. cmSystemTools::Error(error.c_str());
  1071. }
  1072. }
  1073. }