cmQtAutoGeneratorInitializer.cxx 33 KB

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