cmQtAutoGeneratorInitializer.cxx 40 KB

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