cmQtAutoGeneratorInitializer.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. 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 void SetupSourceFiles(cmGeneratorTarget const* target,
  120. std::vector<std::string>& mocUicSources,
  121. std::vector<std::string>& mocUicHeaders,
  122. std::vector<std::string>& mocSkipList,
  123. std::vector<std::string>& uicSkipList)
  124. {
  125. std::vector<cmSourceFile*> srcFiles;
  126. target->GetConfigCommonSourceFiles(srcFiles);
  127. const bool mocTarget = target->GetPropertyAsBool("AUTOMOC");
  128. const bool uicTarget = target->GetPropertyAsBool("AUTOUIC");
  129. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  130. fileIt != srcFiles.end(); ++fileIt) {
  131. cmSourceFile* sf = *fileIt;
  132. const cmSystemTools::FileFormat fileType =
  133. cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
  134. if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
  135. !(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  136. continue;
  137. }
  138. if (cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  139. continue;
  140. }
  141. const std::string absFile =
  142. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  143. // Skip flags
  144. const bool skipAll =
  145. cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN"));
  146. const bool mocSkip =
  147. skipAll || cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  148. const bool uicSkip =
  149. skipAll || cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOUIC"));
  150. // Add file name to skip lists.
  151. // Do this even when the file is not added to the sources/headers lists
  152. // because the file name may be extracted from an other file when
  153. // processing
  154. if (mocSkip) {
  155. mocSkipList.push_back(absFile);
  156. }
  157. if (uicSkip) {
  158. uicSkipList.push_back(absFile);
  159. }
  160. if ((mocTarget && !mocSkip) || (uicTarget && !uicSkip)) {
  161. // Add file name to sources or headers list
  162. switch (fileType) {
  163. case cmSystemTools::CXX_FILE_FORMAT:
  164. mocUicSources.push_back(absFile);
  165. break;
  166. case cmSystemTools::HEADER_FILE_FORMAT:
  167. mocUicHeaders.push_back(absFile);
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. static void MocSetupAutoTarget(
  176. cmGeneratorTarget const* target, const std::string& autogenTargetName,
  177. const std::string& qtMajorVersion,
  178. std::vector<std::string> const& mocSkipList,
  179. std::map<std::string, std::string>& configIncludes,
  180. std::map<std::string, std::string>& configDefines)
  181. {
  182. cmLocalGenerator* lg = target->GetLocalGenerator();
  183. cmMakefile* makefile = target->Target->GetMakefile();
  184. AddDefinitionEscaped(makefile, "_moc_options",
  185. GetSafeProperty(target, "AUTOMOC_MOC_OPTIONS"));
  186. AddDefinitionEscaped(makefile, "_moc_skip", mocSkipList);
  187. AddDefinitionEscaped(makefile, "_moc_relaxed_mode",
  188. makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE") ? "TRUE"
  189. : "FALSE");
  190. AddDefinitionEscaped(makefile, "_moc_depend_filters",
  191. GetSafeProperty(target, "AUTOMOC_DEPEND_FILTERS"));
  192. // Moc includes and compile definitions
  193. {
  194. std::string _moc_incs;
  195. std::string _moc_compile_defs;
  196. std::vector<std::string> configs;
  197. {
  198. const std::string& config = makefile->GetConfigurations(configs);
  199. GetCompileDefinitionsAndDirectories(target, config, _moc_incs,
  200. _moc_compile_defs);
  201. AddDefinitionEscaped(makefile, "_moc_incs", _moc_incs);
  202. AddDefinitionEscaped(makefile, "_moc_compile_defs", _moc_compile_defs);
  203. }
  204. for (std::vector<std::string>::const_iterator li = configs.begin();
  205. li != configs.end(); ++li) {
  206. std::string config_moc_incs;
  207. std::string config_moc_compile_defs;
  208. GetCompileDefinitionsAndDirectories(target, *li, config_moc_incs,
  209. config_moc_compile_defs);
  210. if (config_moc_incs != _moc_incs) {
  211. configIncludes[*li] =
  212. cmOutputConverter::EscapeForCMake(config_moc_incs);
  213. if (_moc_incs.empty()) {
  214. _moc_incs = config_moc_incs;
  215. }
  216. }
  217. if (config_moc_compile_defs != _moc_compile_defs) {
  218. configDefines[*li] =
  219. cmOutputConverter::EscapeForCMake(config_moc_compile_defs);
  220. if (_moc_compile_defs.empty()) {
  221. _moc_compile_defs = config_moc_compile_defs;
  222. }
  223. }
  224. }
  225. }
  226. // Moc executable
  227. {
  228. std::string err;
  229. const char* mocExec = CM_NULLPTR;
  230. if (qtMajorVersion == "5") {
  231. cmGeneratorTarget* qt5Moc = lg->FindGeneratorTargetToUse("Qt5::moc");
  232. if (qt5Moc != CM_NULLPTR) {
  233. mocExec = qt5Moc->ImportedGetLocation("");
  234. } else {
  235. err = "Qt5::moc target not found " + autogenTargetName;
  236. }
  237. } else if (qtMajorVersion == "4") {
  238. cmGeneratorTarget* qt4Moc = lg->FindGeneratorTargetToUse("Qt4::moc");
  239. if (qt4Moc != CM_NULLPTR) {
  240. mocExec = qt4Moc->ImportedGetLocation("");
  241. } else {
  242. err = "Qt4::moc target not found " + autogenTargetName;
  243. }
  244. } else {
  245. err = "The CMAKE_AUTOMOC feature supports only Qt 4 and Qt 5 ";
  246. err += autogenTargetName;
  247. }
  248. // Add definition or error
  249. if (err.empty()) {
  250. AddDefinitionEscaped(makefile, "_qt_moc_executable",
  251. mocExec ? mocExec : "");
  252. } else {
  253. cmSystemTools::Error(err.c_str());
  254. }
  255. }
  256. }
  257. static void UicGetOpts(cmGeneratorTarget const* target,
  258. const std::string& config, std::string& optString)
  259. {
  260. std::vector<std::string> opts;
  261. target->GetAutoUicOptions(opts, config);
  262. optString = cmJoin(opts, ";");
  263. }
  264. static void UicSetupAutoTarget(
  265. cmGeneratorTarget const* target, const std::string& qtMajorVersion,
  266. std::vector<std::string> const& uicSkipList,
  267. std::map<std::string, std::string>& configUicOptions)
  268. {
  269. cmLocalGenerator* lg = target->GetLocalGenerator();
  270. cmMakefile* makefile = target->Target->GetMakefile();
  271. AddDefinitionEscaped(makefile, "_uic_skip", uicSkipList);
  272. // Uic search paths
  273. {
  274. std::vector<std::string> uicSearchPaths;
  275. cmSystemTools::ExpandListArgument(
  276. GetSafeProperty(target, "AUTOUIC_SEARCH_PATHS"), uicSearchPaths);
  277. const std::string srcDir = makefile->GetCurrentSourceDirectory();
  278. for (std::vector<std::string>::iterator it = uicSearchPaths.begin();
  279. it != uicSearchPaths.end(); ++it) {
  280. *it = cmSystemTools::CollapseFullPath(*it, srcDir);
  281. }
  282. AddDefinitionEscaped(makefile, "_uic_search_paths", uicSearchPaths);
  283. }
  284. // Uic target options
  285. {
  286. std::string _uic_opts;
  287. std::vector<std::string> configs;
  288. UicGetOpts(target, makefile->GetConfigurations(configs), _uic_opts);
  289. AddDefinitionEscaped(makefile, "_uic_target_options", _uic_opts);
  290. for (std::vector<std::string>::const_iterator li = configs.begin();
  291. li != configs.end(); ++li) {
  292. std::string config_uic_opts;
  293. UicGetOpts(target, *li, config_uic_opts);
  294. if (config_uic_opts != _uic_opts) {
  295. configUicOptions[*li] =
  296. cmOutputConverter::EscapeForCMake(config_uic_opts);
  297. if (_uic_opts.empty()) {
  298. _uic_opts = config_uic_opts;
  299. }
  300. }
  301. }
  302. }
  303. // Uic files options
  304. {
  305. std::vector<std::string> uiFileFiles;
  306. std::vector<std::string> uiFileOptions;
  307. {
  308. std::set<std::string> skipped;
  309. skipped.insert(uicSkipList.begin(), uicSkipList.end());
  310. const std::vector<cmSourceFile*> uiFilesWithOptions =
  311. makefile->GetQtUiFilesWithOptions();
  312. for (std::vector<cmSourceFile*>::const_iterator fileIt =
  313. uiFilesWithOptions.begin();
  314. fileIt != uiFilesWithOptions.end(); ++fileIt) {
  315. cmSourceFile* sf = *fileIt;
  316. const std::string absFile =
  317. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  318. if (skipped.insert(absFile).second) {
  319. // The file wasn't skipped
  320. uiFileFiles.push_back(absFile);
  321. {
  322. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  323. cmSystemTools::ReplaceString(opts, ";",
  324. cmQtAutoGeneratorCommon::listSep);
  325. uiFileOptions.push_back(opts);
  326. }
  327. }
  328. }
  329. }
  330. AddDefinitionEscaped(makefile, "_qt_uic_options_files", uiFileFiles);
  331. AddDefinitionEscaped(makefile, "_qt_uic_options_options", uiFileOptions);
  332. }
  333. // Uic executable
  334. {
  335. std::string err;
  336. const char* uicExec = CM_NULLPTR;
  337. if (qtMajorVersion == "5") {
  338. cmGeneratorTarget* qt5Uic = lg->FindGeneratorTargetToUse("Qt5::uic");
  339. if (qt5Uic != CM_NULLPTR) {
  340. uicExec = qt5Uic->ImportedGetLocation("");
  341. } else {
  342. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  343. }
  344. } else if (qtMajorVersion == "4") {
  345. cmGeneratorTarget* qt4Uic = lg->FindGeneratorTargetToUse("Qt4::uic");
  346. if (qt4Uic != CM_NULLPTR) {
  347. uicExec = qt4Uic->ImportedGetLocation("");
  348. } else {
  349. err = "Qt4::uic target not found " + target->GetName();
  350. }
  351. } else {
  352. err = "The CMAKE_AUTOUIC feature supports only Qt 4 and Qt 5 ";
  353. err += target->GetName();
  354. }
  355. // Add definition or error
  356. if (err.empty()) {
  357. AddDefinitionEscaped(makefile, "_qt_uic_executable",
  358. uicExec ? uicExec : "");
  359. } else {
  360. cmSystemTools::Error(err.c_str());
  361. }
  362. }
  363. }
  364. static std::string RccGetExecutable(cmGeneratorTarget const* target,
  365. const std::string& qtMajorVersion)
  366. {
  367. std::string rccExec;
  368. cmLocalGenerator* lg = target->GetLocalGenerator();
  369. if (qtMajorVersion == "5") {
  370. cmGeneratorTarget* qt5Rcc = lg->FindGeneratorTargetToUse("Qt5::rcc");
  371. if (qt5Rcc != CM_NULLPTR) {
  372. rccExec = qt5Rcc->ImportedGetLocation("");
  373. } else {
  374. cmSystemTools::Error("Qt5::rcc target not found ",
  375. target->GetName().c_str());
  376. }
  377. } else if (qtMajorVersion == "4") {
  378. cmGeneratorTarget* qt4Rcc = lg->FindGeneratorTargetToUse("Qt4::rcc");
  379. if (qt4Rcc != CM_NULLPTR) {
  380. rccExec = qt4Rcc->ImportedGetLocation("");
  381. } else {
  382. cmSystemTools::Error("Qt4::rcc target not found ",
  383. target->GetName().c_str());
  384. }
  385. } else {
  386. cmSystemTools::Error(
  387. "The CMAKE_AUTORCC feature supports only Qt 4 and Qt 5 ",
  388. target->GetName().c_str());
  389. }
  390. return rccExec;
  391. }
  392. static void RccMergeOptions(std::vector<std::string>& opts,
  393. const std::vector<std::string>& fileOpts,
  394. bool isQt5)
  395. {
  396. static const char* valueOptions[] = { "name", "root", "compress",
  397. "threshold" };
  398. std::vector<std::string> extraOpts;
  399. for (std::vector<std::string>::const_iterator fit = fileOpts.begin();
  400. fit != fileOpts.end(); ++fit) {
  401. std::vector<std::string>::iterator existingIt =
  402. std::find(opts.begin(), opts.end(), *fit);
  403. if (existingIt != opts.end()) {
  404. const char* optName = fit->c_str();
  405. if (*optName == '-') {
  406. ++optName;
  407. if (isQt5 && *optName == '-') {
  408. ++optName;
  409. }
  410. }
  411. // Test if this is a value option and change the existing value
  412. if ((optName != fit->c_str()) &&
  413. std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  414. cmStrCmp(optName)) != cmArrayEnd(valueOptions)) {
  415. const std::vector<std::string>::iterator existValueIt(existingIt + 1);
  416. const std::vector<std::string>::const_iterator fileValueIt(fit + 1);
  417. if ((existValueIt != opts.end()) && (fileValueIt != fileOpts.end())) {
  418. *existValueIt = *fileValueIt;
  419. ++fit;
  420. }
  421. }
  422. } else {
  423. extraOpts.push_back(*fit);
  424. }
  425. }
  426. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  427. }
  428. static void RccSetupAutoTarget(cmGeneratorTarget const* target,
  429. const std::string& qtMajorVersion)
  430. {
  431. cmMakefile* makefile = target->Target->GetMakefile();
  432. const bool qtMajorVersion5 = (qtMajorVersion == "5");
  433. const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
  434. std::vector<std::string> _rcc_files;
  435. std::vector<std::string> _rcc_inputs;
  436. std::vector<std::string> rccFileFiles;
  437. std::vector<std::string> rccFileOptions;
  438. std::vector<std::string> rccOptionsTarget;
  439. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS")) {
  440. cmSystemTools::ExpandListArgument(opts, rccOptionsTarget);
  441. }
  442. std::vector<cmSourceFile*> srcFiles;
  443. target->GetConfigCommonSourceFiles(srcFiles);
  444. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  445. fileIt != srcFiles.end(); ++fileIt) {
  446. cmSourceFile* sf = *fileIt;
  447. if (sf->GetExtension() == "qrc") {
  448. const bool skip =
  449. cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN")) ||
  450. cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  451. if (!skip) {
  452. const std::string absFile =
  453. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  454. // qrc file
  455. _rcc_files.push_back(absFile);
  456. // qrc file entries
  457. {
  458. std::string entriesList = "{";
  459. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  460. std::vector<std::string> files;
  461. if (cmQtAutoGeneratorCommon::RccListInputs(
  462. qtMajorVersion, rccCommand, absFile, files)) {
  463. entriesList += cmJoin(files, cmQtAutoGeneratorCommon::listSep);
  464. } else {
  465. return;
  466. }
  467. }
  468. entriesList += "}";
  469. _rcc_inputs.push_back(entriesList);
  470. }
  471. // rcc options for this qrc file
  472. {
  473. // Merged target and file options
  474. std::vector<std::string> rccOptions(rccOptionsTarget);
  475. if (const char* prop = sf->GetProperty("AUTORCC_OPTIONS")) {
  476. std::vector<std::string> optsVec;
  477. cmSystemTools::ExpandListArgument(prop, optsVec);
  478. RccMergeOptions(rccOptions, optsVec, qtMajorVersion5);
  479. }
  480. // Only store non empty options lists
  481. if (!rccOptions.empty()) {
  482. rccFileFiles.push_back(absFile);
  483. rccFileOptions.push_back(
  484. cmJoin(rccOptions, cmQtAutoGeneratorCommon::listSep));
  485. }
  486. }
  487. }
  488. }
  489. }
  490. AddDefinitionEscaped(makefile, "_qt_rcc_executable", rccCommand);
  491. AddDefinitionEscaped(makefile, "_rcc_files", _rcc_files);
  492. AddDefinitionEscaped(makefile, "_rcc_inputs", _rcc_inputs);
  493. AddDefinitionEscaped(makefile, "_rcc_options_files", rccFileFiles);
  494. AddDefinitionEscaped(makefile, "_rcc_options_options", rccFileOptions);
  495. }
  496. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  497. cmGeneratorTarget* target)
  498. {
  499. if (target->GetPropertyAsBool("AUTOMOC")) {
  500. cmMakefile* makefile = target->Target->GetMakefile();
  501. const std::string mocCppFile =
  502. GetAutogenTargetBuildDir(target) + "moc_compilation.cpp";
  503. cmSourceFile* gf = makefile->GetOrCreateSource(mocCppFile, true);
  504. gf->SetProperty("SKIP_AUTOGEN", "On");
  505. target->AddSource(mocCppFile);
  506. }
  507. }
  508. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  509. cmLocalGenerator* lg, cmGeneratorTarget* target)
  510. {
  511. cmMakefile* makefile = target->Target->GetMakefile();
  512. // Create a custom target for running generators at buildtime
  513. const std::string autogenTargetName = GetAutogenTargetName(target);
  514. const std::string autogenBuildDir = GetAutogenTargetBuildDir(target);
  515. const std::string workingDirectory =
  516. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  517. const std::string qtMajorVersion = GetQtMajorVersion(target);
  518. const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
  519. std::vector<std::string> autogenOutputFiles;
  520. // Remove old settings on cleanup
  521. {
  522. std::string fname = GetAutogenTargetFilesDir(target);
  523. fname += "/AutogenOldSettings.cmake";
  524. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fname.c_str(),
  525. false);
  526. }
  527. // Create autogen target build directory and add it to the clean files
  528. cmSystemTools::MakeDirectory(autogenBuildDir);
  529. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  530. autogenBuildDir.c_str(), false);
  531. if (target->GetPropertyAsBool("AUTOMOC") ||
  532. target->GetPropertyAsBool("AUTOUIC")) {
  533. // Create autogen target includes directory and
  534. // add it to the origin target INCLUDE_DIRECTORIES
  535. const std::string incsDir = autogenBuildDir + "include";
  536. cmSystemTools::MakeDirectory(incsDir);
  537. target->AddIncludeDirectory(incsDir, true);
  538. }
  539. if (target->GetPropertyAsBool("AUTOMOC")) {
  540. // Register moc compilation file as generated
  541. autogenOutputFiles.push_back(autogenBuildDir + "moc_compilation.cpp");
  542. }
  543. // Initialize autogen target dependencies
  544. std::vector<std::string> depends;
  545. if (const char* autogenDepends =
  546. target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  547. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  548. }
  549. // Compose command lines
  550. cmCustomCommandLines commandLines;
  551. {
  552. cmCustomCommandLine currentLine;
  553. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  554. currentLine.push_back("-E");
  555. currentLine.push_back("cmake_autogen");
  556. currentLine.push_back(GetAutogenTargetFilesDir(target));
  557. currentLine.push_back("$<CONFIGURATION>");
  558. commandLines.push_back(currentLine);
  559. }
  560. // Compose target comment
  561. std::string autogenComment;
  562. {
  563. std::vector<std::string> toolNames;
  564. if (target->GetPropertyAsBool("AUTOMOC")) {
  565. toolNames.push_back("MOC");
  566. }
  567. if (target->GetPropertyAsBool("AUTOUIC")) {
  568. toolNames.push_back("UIC");
  569. }
  570. if (target->GetPropertyAsBool("AUTORCC")) {
  571. toolNames.push_back("RCC");
  572. }
  573. std::string tools = toolNames[0];
  574. toolNames.erase(toolNames.begin());
  575. while (toolNames.size() > 1) {
  576. tools += ", " + toolNames[0];
  577. toolNames.erase(toolNames.begin());
  578. }
  579. if (toolNames.size() == 1) {
  580. tools += " and " + toolNames[0];
  581. }
  582. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  583. }
  584. #if defined(_WIN32) && !defined(__CYGWIN__)
  585. bool usePRE_BUILD = false;
  586. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  587. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  588. cmGlobalVisualStudioGenerator* vsgg =
  589. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  590. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  591. // reduce the number of targets loaded into the IDE.
  592. // This also works around a VS 11 bug that may skip updating the target:
  593. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  594. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  595. if (usePRE_BUILD) {
  596. // If the autogen target depends on an other target
  597. // don't use PRE_BUILD
  598. for (std::vector<std::string>::iterator it = depends.begin();
  599. it != depends.end(); ++it) {
  600. if (!makefile->FindTargetToUse(it->c_str())) {
  601. usePRE_BUILD = false;
  602. break;
  603. }
  604. }
  605. }
  606. }
  607. #endif
  608. if (target->GetPropertyAsBool("AUTORCC")) {
  609. cmFilePathChecksum fpathCheckSum(makefile);
  610. std::vector<cmSourceFile*> srcFiles;
  611. target->GetConfigCommonSourceFiles(srcFiles);
  612. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  613. fileIt != srcFiles.end(); ++fileIt) {
  614. cmSourceFile* sf = *fileIt;
  615. if (sf->GetExtension() == "qrc" &&
  616. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN")) &&
  617. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  618. {
  619. const std::string absFile =
  620. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  621. // Run cmake again when .qrc file changes
  622. makefile->AddCMakeDependFile(absFile);
  623. std::string rccOutputFile = autogenBuildDir;
  624. rccOutputFile += fpathCheckSum.getPart(absFile);
  625. rccOutputFile += "/qrc_";
  626. rccOutputFile +=
  627. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFile);
  628. rccOutputFile += ".cpp";
  629. // Add rcc output file to origin target sources
  630. cmSourceFile* gf = makefile->GetOrCreateSource(rccOutputFile, true);
  631. gf->SetProperty("SKIP_AUTOGEN", "On");
  632. target->AddSource(rccOutputFile);
  633. // Register rcc output file as generated
  634. autogenOutputFiles.push_back(rccOutputFile);
  635. if (lg->GetGlobalGenerator()->GetName() == "Ninja"
  636. #if defined(_WIN32) && !defined(__CYGWIN__)
  637. || usePRE_BUILD
  638. #endif
  639. ) {
  640. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  641. cmQtAutoGeneratorCommon::RccListInputs(
  642. qtMajorVersion, rccCommand, absFile, depends);
  643. #if defined(_WIN32) && !defined(__CYGWIN__)
  644. // Cannot use PRE_BUILD because the resource files themselves
  645. // may not be sources within the target so VS may not know the
  646. // target needs to re-build at all.
  647. usePRE_BUILD = false;
  648. #endif
  649. }
  650. }
  651. }
  652. }
  653. }
  654. }
  655. #if defined(_WIN32) && !defined(__CYGWIN__)
  656. if (usePRE_BUILD) {
  657. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  658. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  659. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  660. std::vector<std::string> no_output;
  661. std::vector<std::string> no_byproducts;
  662. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  663. commandLines, autogenComment.c_str(),
  664. workingDirectory.c_str());
  665. cc.SetEscapeOldStyle(false);
  666. cc.SetEscapeAllowMakeVars(true);
  667. target->Target->AddPreBuildCommand(cc);
  668. } else
  669. #endif
  670. {
  671. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  672. autogenTargetName, true, workingDirectory.c_str(),
  673. /*byproducts=*/autogenOutputFiles, depends, commandLines, false,
  674. autogenComment.c_str());
  675. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  676. lg->AddGeneratorTarget(gt);
  677. // Set target folder
  678. const char* autogenFolder =
  679. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  680. if (!autogenFolder) {
  681. autogenFolder =
  682. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  683. }
  684. if (autogenFolder && *autogenFolder) {
  685. autogenTarget->SetProperty("FOLDER", autogenFolder);
  686. } else {
  687. // inherit FOLDER property from target (#13688)
  688. utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
  689. }
  690. target->Target->AddUtility(autogenTargetName);
  691. }
  692. }
  693. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  694. cmGeneratorTarget const* target)
  695. {
  696. cmMakefile* makefile = target->Target->GetMakefile();
  697. // forget the variables added here afterwards again:
  698. cmMakefile::ScopePushPop varScope(makefile);
  699. static_cast<void>(varScope);
  700. std::map<std::string, std::string> configMocIncludes;
  701. std::map<std::string, std::string> configMocDefines;
  702. std::map<std::string, std::string> configUicOptions;
  703. {
  704. // create a custom target for running generators at buildtime:
  705. const std::string autogenTargetName = GetAutogenTargetName(target);
  706. const std::string qtMajorVersion = GetQtMajorVersion(target);
  707. AddDefinitionEscaped(makefile, "_autogen_target_name", autogenTargetName);
  708. AddDefinitionEscaped(makefile, "_origin_target_name", target->GetName());
  709. AddDefinitionEscaped(makefile, "_qt_version_major", qtMajorVersion);
  710. std::vector<std::string> _sources;
  711. std::vector<std::string> _headers;
  712. std::vector<std::string> mocSkipList;
  713. std::vector<std::string> uicSkipList;
  714. if (target->GetPropertyAsBool("AUTOMOC") ||
  715. target->GetPropertyAsBool("AUTOUIC") ||
  716. target->GetPropertyAsBool("AUTORCC")) {
  717. SetupSourceFiles(target, _sources, _headers, mocSkipList, uicSkipList);
  718. }
  719. AddDefinitionEscaped(makefile, "_sources", _sources);
  720. AddDefinitionEscaped(makefile, "_headers", _headers);
  721. if (target->GetPropertyAsBool("AUTOMOC")) {
  722. MocSetupAutoTarget(target, autogenTargetName, qtMajorVersion,
  723. mocSkipList, configMocIncludes, configMocDefines);
  724. }
  725. if (target->GetPropertyAsBool("AUTOUIC")) {
  726. UicSetupAutoTarget(target, qtMajorVersion, uicSkipList,
  727. configUicOptions);
  728. }
  729. if (target->GetPropertyAsBool("AUTORCC")) {
  730. RccSetupAutoTarget(target, qtMajorVersion);
  731. }
  732. }
  733. // Generate config file
  734. std::string inputFile = cmSystemTools::GetCMakeRoot();
  735. inputFile += "/Modules/AutogenInfo.cmake.in";
  736. std::string outputFile = GetAutogenTargetFilesDir(target);
  737. outputFile += "/AutogenInfo.cmake";
  738. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  739. false);
  740. // Append custom config definitions to info file
  741. if (!configMocDefines.empty() || !configMocIncludes.empty() ||
  742. !configUicOptions.empty()) {
  743. // Ensure we have write permission in case .in was read-only.
  744. mode_t perm = 0;
  745. #if defined(_WIN32) && !defined(__CYGWIN__)
  746. mode_t mode_write = S_IWRITE;
  747. #else
  748. mode_t mode_write = S_IWUSR;
  749. #endif
  750. cmSystemTools::GetPermissions(outputFile, perm);
  751. if (!(perm & mode_write)) {
  752. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  753. }
  754. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  755. if (!infoFile) {
  756. std::string error = "Internal CMake error when trying to open file: ";
  757. error += outputFile;
  758. error += " for writing.";
  759. cmSystemTools::Error(error.c_str());
  760. } else {
  761. infoFile << "# Configuration specific options\n";
  762. if (!configMocDefines.empty()) {
  763. for (std::map<std::string, std::string>::iterator
  764. it = configMocDefines.begin(),
  765. end = configMocDefines.end();
  766. it != end; ++it) {
  767. infoFile << "set(AM_MOC_DEFINITIONS_" << it->first << " "
  768. << it->second << ")\n";
  769. }
  770. }
  771. if (!configMocIncludes.empty()) {
  772. for (std::map<std::string, std::string>::iterator
  773. it = configMocIncludes.begin(),
  774. end = configMocIncludes.end();
  775. it != end; ++it) {
  776. infoFile << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
  777. << ")\n";
  778. }
  779. }
  780. if (!configUicOptions.empty()) {
  781. for (std::map<std::string, std::string>::iterator
  782. it = configUicOptions.begin(),
  783. end = configUicOptions.end();
  784. it != end; ++it) {
  785. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " "
  786. << it->second << ")\n";
  787. }
  788. }
  789. }
  790. }
  791. }