cmQtAutoGeneratorInitializer.cxx 34 KB

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