cmQtAutoGeneratorInitializer.cxx 32 KB

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