cmQtAutoGeneratorInitializer.cxx 34 KB

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