cmQtAutoGeneratorInitializer.cxx 32 KB

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