cmQtAutoGeneratorInitializer.cxx 33 KB

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