cmQtAutoGeneratorInitializer.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 AcquireScanFiles(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. const bool mocTarget = target->GetPropertyAsBool("AUTOMOC");
  126. const bool uicTarget = target->GetPropertyAsBool("AUTOUIC");
  127. std::vector<cmSourceFile*> srcFiles;
  128. target->GetConfigCommonSourceFiles(srcFiles);
  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. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN")) &&
  449. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  450. const std::string absFile =
  451. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  452. // qrc file
  453. _rcc_files.push_back(absFile);
  454. // qrc file entries
  455. {
  456. std::string entriesList = "{";
  457. // Read input file list only for non generated .qrc files.
  458. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  459. std::string error;
  460. std::vector<std::string> files;
  461. if (cmQtAutoGeneratorCommon::RccListInputs(
  462. qtMajorVersion, rccCommand, absFile, files, &error)) {
  463. entriesList += cmJoin(files, cmQtAutoGeneratorCommon::listSep);
  464. } else {
  465. cmSystemTools::Error(error.c_str());
  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. AddDefinitionEscaped(makefile, "_qt_rcc_executable", rccCommand);
  490. AddDefinitionEscaped(makefile, "_rcc_files", _rcc_files);
  491. AddDefinitionEscaped(makefile, "_rcc_inputs", _rcc_inputs);
  492. AddDefinitionEscaped(makefile, "_rcc_options_files", rccFileFiles);
  493. AddDefinitionEscaped(makefile, "_rcc_options_options", rccFileOptions);
  494. }
  495. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  496. cmGeneratorTarget* target)
  497. {
  498. if (target->GetPropertyAsBool("AUTOMOC")) {
  499. cmMakefile* makefile = target->Target->GetMakefile();
  500. const std::string mocCppFile =
  501. GetAutogenTargetBuildDir(target) + "moc_compilation.cpp";
  502. cmSourceFile* gf = makefile->GetOrCreateSource(mocCppFile, true);
  503. gf->SetProperty("SKIP_AUTOGEN", "On");
  504. target->AddSource(mocCppFile);
  505. }
  506. }
  507. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  508. cmLocalGenerator* lg, cmGeneratorTarget* target)
  509. {
  510. cmMakefile* makefile = target->Target->GetMakefile();
  511. // Create a custom target for running generators at buildtime
  512. const bool mocEnabled = target->GetPropertyAsBool("AUTOMOC");
  513. const bool uicEnabled = target->GetPropertyAsBool("AUTOUIC");
  514. const bool rccEnabled = target->GetPropertyAsBool("AUTORCC");
  515. const std::string autogenTargetName = GetAutogenTargetName(target);
  516. const std::string autogenBuildDir = GetAutogenTargetBuildDir(target);
  517. const std::string workingDirectory =
  518. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  519. const std::string qtMajorVersion = GetQtMajorVersion(target);
  520. const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
  521. std::vector<std::string> autogenOutputFiles;
  522. // Remove old settings on cleanup
  523. {
  524. std::string fname = GetAutogenTargetFilesDir(target);
  525. fname += "/AutogenOldSettings.cmake";
  526. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fname.c_str(),
  527. false);
  528. }
  529. // Create autogen target build directory and add it to the clean files
  530. cmSystemTools::MakeDirectory(autogenBuildDir);
  531. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  532. autogenBuildDir.c_str(), false);
  533. if (mocEnabled || uicEnabled) {
  534. // Create autogen target includes directory and
  535. // add it to the origin target INCLUDE_DIRECTORIES
  536. const std::string incsDir = autogenBuildDir + "include";
  537. cmSystemTools::MakeDirectory(incsDir);
  538. target->AddIncludeDirectory(incsDir, true);
  539. }
  540. if (mocEnabled) {
  541. // Register moc compilation file as generated
  542. autogenOutputFiles.push_back(autogenBuildDir + "moc_compilation.cpp");
  543. }
  544. // Initialize autogen target dependencies
  545. std::vector<std::string> depends;
  546. if (const char* autogenDepends =
  547. target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  548. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  549. }
  550. // Compose command lines
  551. cmCustomCommandLines commandLines;
  552. {
  553. cmCustomCommandLine currentLine;
  554. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  555. currentLine.push_back("-E");
  556. currentLine.push_back("cmake_autogen");
  557. currentLine.push_back(GetAutogenTargetFilesDir(target));
  558. currentLine.push_back("$<CONFIGURATION>");
  559. commandLines.push_back(currentLine);
  560. }
  561. // Compose target comment
  562. std::string autogenComment;
  563. {
  564. std::vector<std::string> toolNames;
  565. if (mocEnabled) {
  566. toolNames.push_back("MOC");
  567. }
  568. if (uicEnabled) {
  569. toolNames.push_back("UIC");
  570. }
  571. if (rccEnabled) {
  572. toolNames.push_back("RCC");
  573. }
  574. std::string tools = toolNames[0];
  575. toolNames.erase(toolNames.begin());
  576. while (toolNames.size() > 1) {
  577. tools += ", " + toolNames[0];
  578. toolNames.erase(toolNames.begin());
  579. }
  580. if (toolNames.size() == 1) {
  581. tools += " and " + toolNames[0];
  582. }
  583. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  584. }
  585. #if defined(_WIN32) && !defined(__CYGWIN__)
  586. bool usePRE_BUILD = false;
  587. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  588. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  589. cmGlobalVisualStudioGenerator* vsgg =
  590. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  591. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  592. // reduce the number of targets loaded into the IDE.
  593. // This also works around a VS 11 bug that may skip updating the target:
  594. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  595. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  596. if (usePRE_BUILD) {
  597. // If the autogen target depends on an other target
  598. // don't use PRE_BUILD
  599. for (std::vector<std::string>::iterator it = depends.begin();
  600. it != depends.end(); ++it) {
  601. if (!makefile->FindTargetToUse(it->c_str())) {
  602. usePRE_BUILD = false;
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. #endif
  609. if (rccEnabled) {
  610. cmFilePathChecksum fpathCheckSum(makefile);
  611. std::vector<cmSourceFile*> srcFiles;
  612. target->GetConfigCommonSourceFiles(srcFiles);
  613. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  614. fileIt != srcFiles.end(); ++fileIt) {
  615. cmSourceFile* sf = *fileIt;
  616. if (sf->GetExtension() == "qrc" &&
  617. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN")) &&
  618. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  619. {
  620. const std::string absFile =
  621. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  622. // Run cmake again when .qrc file changes
  623. makefile->AddCMakeDependFile(absFile);
  624. std::string rccOutputFile = autogenBuildDir;
  625. rccOutputFile += fpathCheckSum.getPart(absFile);
  626. rccOutputFile += "/qrc_";
  627. rccOutputFile +=
  628. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFile);
  629. rccOutputFile += ".cpp";
  630. // Add rcc output file to origin target sources
  631. cmSourceFile* gf = makefile->GetOrCreateSource(rccOutputFile, true);
  632. gf->SetProperty("SKIP_AUTOGEN", "On");
  633. target->AddSource(rccOutputFile);
  634. // Register rcc output file as generated
  635. autogenOutputFiles.push_back(rccOutputFile);
  636. if (lg->GetGlobalGenerator()->GetName() == "Ninja"
  637. #if defined(_WIN32) && !defined(__CYGWIN__)
  638. || usePRE_BUILD
  639. #endif
  640. ) {
  641. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  642. {
  643. std::string error;
  644. if (!cmQtAutoGeneratorCommon::RccListInputs(
  645. qtMajorVersion, rccCommand, absFile, depends, &error)) {
  646. cmSystemTools::Error(error.c_str());
  647. }
  648. }
  649. #if defined(_WIN32) && !defined(__CYGWIN__)
  650. // Cannot use PRE_BUILD because the resource files themselves
  651. // may not be sources within the target so VS may not know the
  652. // target needs to re-build at all.
  653. usePRE_BUILD = false;
  654. #endif
  655. }
  656. }
  657. }
  658. }
  659. }
  660. }
  661. #if defined(_WIN32) && !defined(__CYGWIN__)
  662. if (usePRE_BUILD) {
  663. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  664. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  665. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  666. std::vector<std::string> no_output;
  667. std::vector<std::string> no_byproducts;
  668. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  669. commandLines, autogenComment.c_str(),
  670. workingDirectory.c_str());
  671. cc.SetEscapeOldStyle(false);
  672. cc.SetEscapeAllowMakeVars(true);
  673. target->Target->AddPreBuildCommand(cc);
  674. } else
  675. #endif
  676. {
  677. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  678. autogenTargetName, true, workingDirectory.c_str(),
  679. /*byproducts=*/autogenOutputFiles, depends, commandLines, false,
  680. autogenComment.c_str());
  681. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  682. lg->AddGeneratorTarget(gt);
  683. // Set target folder
  684. const char* autogenFolder =
  685. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  686. if (!autogenFolder) {
  687. autogenFolder =
  688. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  689. }
  690. if (autogenFolder && *autogenFolder) {
  691. autogenTarget->SetProperty("FOLDER", autogenFolder);
  692. } else {
  693. // inherit FOLDER property from target (#13688)
  694. utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
  695. }
  696. target->Target->AddUtility(autogenTargetName);
  697. }
  698. }
  699. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  700. cmGeneratorTarget const* target)
  701. {
  702. cmMakefile* makefile = target->Target->GetMakefile();
  703. // forget the variables added here afterwards again:
  704. cmMakefile::ScopePushPop varScope(makefile);
  705. static_cast<void>(varScope);
  706. std::map<std::string, std::string> configMocIncludes;
  707. std::map<std::string, std::string> configMocDefines;
  708. std::map<std::string, std::string> configUicOptions;
  709. {
  710. const bool mocEnabled = target->GetPropertyAsBool("AUTOMOC");
  711. const bool uicEnabled = target->GetPropertyAsBool("AUTOUIC");
  712. const bool rccEnabled = target->GetPropertyAsBool("AUTORCC");
  713. const std::string autogenTargetName = GetAutogenTargetName(target);
  714. const std::string qtMajorVersion = GetQtMajorVersion(target);
  715. std::vector<std::string> _sources;
  716. std::vector<std::string> _headers;
  717. if (mocEnabled || uicEnabled || rccEnabled) {
  718. std::vector<std::string> mocSkipList;
  719. std::vector<std::string> uicSkipList;
  720. AcquireScanFiles(target, _sources, _headers, mocSkipList, uicSkipList);
  721. if (mocEnabled) {
  722. MocSetupAutoTarget(target, autogenTargetName, qtMajorVersion,
  723. mocSkipList, configMocIncludes, configMocDefines);
  724. }
  725. if (uicEnabled) {
  726. UicSetupAutoTarget(target, qtMajorVersion, uicSkipList,
  727. configUicOptions);
  728. }
  729. if (rccEnabled) {
  730. RccSetupAutoTarget(target, qtMajorVersion);
  731. }
  732. }
  733. AddDefinitionEscaped(makefile, "_autogen_target_name", autogenTargetName);
  734. AddDefinitionEscaped(makefile, "_origin_target_name", target->GetName());
  735. AddDefinitionEscaped(makefile, "_qt_version_major", qtMajorVersion);
  736. AddDefinitionEscaped(makefile, "_sources", _sources);
  737. AddDefinitionEscaped(makefile, "_headers", _headers);
  738. }
  739. // Generate config file
  740. std::string inputFile = cmSystemTools::GetCMakeRoot();
  741. inputFile += "/Modules/AutogenInfo.cmake.in";
  742. std::string outputFile = GetAutogenTargetFilesDir(target);
  743. outputFile += "/AutogenInfo.cmake";
  744. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  745. false);
  746. // Append custom config definitions to info file
  747. if (!configMocDefines.empty() || !configMocIncludes.empty() ||
  748. !configUicOptions.empty()) {
  749. // Ensure we have write permission in case .in was read-only.
  750. mode_t perm = 0;
  751. #if defined(_WIN32) && !defined(__CYGWIN__)
  752. mode_t mode_write = S_IWRITE;
  753. #else
  754. mode_t mode_write = S_IWUSR;
  755. #endif
  756. cmSystemTools::GetPermissions(outputFile, perm);
  757. if (!(perm & mode_write)) {
  758. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  759. }
  760. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  761. if (!infoFile) {
  762. std::string error = "Internal CMake error when trying to open file: ";
  763. error += outputFile;
  764. error += " for writing.";
  765. cmSystemTools::Error(error.c_str());
  766. } else {
  767. infoFile << "# Configuration specific options\n";
  768. if (!configMocDefines.empty()) {
  769. for (std::map<std::string, std::string>::iterator
  770. it = configMocDefines.begin(),
  771. end = configMocDefines.end();
  772. it != end; ++it) {
  773. infoFile << "set(AM_MOC_DEFINITIONS_" << it->first << " "
  774. << it->second << ")\n";
  775. }
  776. }
  777. if (!configMocIncludes.empty()) {
  778. for (std::map<std::string, std::string>::iterator
  779. it = configMocIncludes.begin(),
  780. end = configMocIncludes.end();
  781. it != end; ++it) {
  782. infoFile << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
  783. << ")\n";
  784. }
  785. }
  786. if (!configUicOptions.empty()) {
  787. for (std::map<std::string, std::string>::iterator
  788. it = configUicOptions.begin(),
  789. end = configUicOptions.end();
  790. it != end; ++it) {
  791. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " "
  792. << it->second << ")\n";
  793. }
  794. }
  795. }
  796. }
  797. }