1
0

cmQtAutoGeneratorInitializer.cxx 33 KB

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