cmQtAutoGeneratorInitializer.cxx 31 KB

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