cmQtAutoGeneratorInitializer.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. static std::string ReadAll(const std::string& filename)
  381. {
  382. cmsys::ifstream file(filename.c_str());
  383. std::ostringstream stream;
  384. stream << file.rdbuf();
  385. file.close();
  386. return stream.str();
  387. }
  388. /// @brief Reads the resource files list from from a .qrc file - Qt5 version
  389. /// @return True if the .qrc file was successfully parsed
  390. static bool ListQt5RccInputs(cmSourceFile* sf, cmGeneratorTarget const* target,
  391. std::vector<std::string>& depends)
  392. {
  393. std::string rccCommand = GetRccExecutable(target);
  394. bool hasDashDashList = false;
  395. // Read rcc features
  396. {
  397. std::vector<std::string> command;
  398. command.push_back(rccCommand);
  399. command.push_back("--help");
  400. std::string rccStdOut;
  401. std::string rccStdErr;
  402. int retVal = 0;
  403. bool result =
  404. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  405. CM_NULLPTR, cmSystemTools::OUTPUT_NONE);
  406. if (result && retVal == 0 &&
  407. rccStdOut.find("--list") != std::string::npos) {
  408. hasDashDashList = true;
  409. }
  410. }
  411. // Run rcc list command
  412. std::vector<std::string> command;
  413. command.push_back(rccCommand);
  414. command.push_back(hasDashDashList ? "--list" : "-list");
  415. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  416. command.push_back(absFile);
  417. std::string rccStdOut;
  418. std::string rccStdErr;
  419. int retVal = 0;
  420. bool result =
  421. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  422. CM_NULLPTR, cmSystemTools::OUTPUT_NONE);
  423. if (!result || retVal) {
  424. std::ostringstream err;
  425. err << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  426. << " failed:\n"
  427. << rccStdOut << "\n"
  428. << rccStdErr << std::endl;
  429. cmSystemTools::Error(err.str().c_str());
  430. return false;
  431. }
  432. // Parse rcc list output
  433. {
  434. std::istringstream ostr(rccStdOut);
  435. std::string oline;
  436. while (std::getline(ostr, oline)) {
  437. oline = utilStripCR(oline);
  438. if (!oline.empty()) {
  439. depends.push_back(oline);
  440. }
  441. }
  442. }
  443. {
  444. std::istringstream estr(rccStdErr);
  445. std::string eline;
  446. while (std::getline(estr, eline)) {
  447. eline = utilStripCR(eline);
  448. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  449. static std::string searchString = "Cannot find file '";
  450. std::string::size_type pos = eline.find(searchString);
  451. if (pos == std::string::npos) {
  452. std::ostringstream err;
  453. err << "AUTOGEN: error: Rcc lists unparsable output " << eline
  454. << std::endl;
  455. cmSystemTools::Error(err.str().c_str());
  456. return false;
  457. }
  458. pos += searchString.length();
  459. std::string::size_type sz = eline.size() - pos - 1;
  460. depends.push_back(eline.substr(pos, sz));
  461. }
  462. }
  463. }
  464. return true;
  465. }
  466. /// @brief Reads the resource files list from from a .qrc file - Qt4 version
  467. /// @return True if the .qrc file was successfully parsed
  468. static bool ListQt4RccInputs(cmSourceFile* sf,
  469. std::vector<std::string>& depends)
  470. {
  471. const std::string qrcContents = ReadAll(sf->GetFullPath());
  472. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  473. size_t offset = 0;
  474. while (fileMatchRegex.find(qrcContents.c_str() + offset)) {
  475. std::string qrcEntry = fileMatchRegex.match(1);
  476. offset += qrcEntry.size();
  477. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  478. fileReplaceRegex.find(qrcEntry);
  479. std::string tag = fileReplaceRegex.match(1);
  480. qrcEntry = qrcEntry.substr(tag.size());
  481. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str())) {
  482. qrcEntry = sf->GetLocation().GetDirectory() + "/" + qrcEntry;
  483. }
  484. depends.push_back(qrcEntry);
  485. }
  486. return true;
  487. }
  488. /// @brief Reads the resource files list from from a .qrc file
  489. /// @return True if the rcc file was successfully parsed
  490. static bool ListQtRccInputs(const std::string& qtMajorVersion,
  491. cmSourceFile* sf, cmGeneratorTarget const* target,
  492. std::vector<std::string>& depends)
  493. {
  494. if (qtMajorVersion == "5") {
  495. return ListQt5RccInputs(sf, target, depends);
  496. }
  497. return ListQt4RccInputs(sf, depends);
  498. }
  499. static void SetupAutoRccTarget(cmGeneratorTarget const* target)
  500. {
  501. std::string _rcc_files;
  502. const char* sepRccFiles = "";
  503. cmMakefile* makefile = target->Target->GetMakefile();
  504. std::vector<cmSourceFile*> srcFiles;
  505. target->GetConfigCommonSourceFiles(srcFiles);
  506. std::string qrcInputs;
  507. const char* qrcInputsSep = "";
  508. std::string rccFileFiles;
  509. std::string rccFileOptions;
  510. const char* optionSep = "";
  511. const char* qtVersion = makefile->GetDefinition("_target_qt_version");
  512. std::vector<std::string> rccOptions;
  513. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS")) {
  514. cmSystemTools::ExpandListArgument(opts, rccOptions);
  515. }
  516. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  517. if (qtMajorVersion == "") {
  518. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  519. }
  520. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  521. fileIt != srcFiles.end(); ++fileIt) {
  522. cmSourceFile* sf = *fileIt;
  523. std::string ext = sf->GetExtension();
  524. if (ext == "qrc") {
  525. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  526. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  527. if (!skip) {
  528. _rcc_files += sepRccFiles;
  529. _rcc_files += absFile;
  530. sepRccFiles = ";";
  531. if (const char* prop = sf->GetProperty("AUTORCC_OPTIONS")) {
  532. std::vector<std::string> optsVec;
  533. cmSystemTools::ExpandListArgument(prop, optsVec);
  534. MergeRccOptions(rccOptions, optsVec, strcmp(qtVersion, "5") == 0);
  535. }
  536. if (!rccOptions.empty()) {
  537. rccFileFiles += optionSep;
  538. rccFileFiles += absFile;
  539. rccFileOptions += optionSep;
  540. }
  541. const char* listSep = "";
  542. for (std::vector<std::string>::const_iterator it = rccOptions.begin();
  543. it != rccOptions.end(); ++it) {
  544. rccFileOptions += listSep;
  545. rccFileOptions += *it;
  546. listSep = "@list_sep@";
  547. }
  548. optionSep = ";";
  549. std::string entriesList;
  550. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  551. std::vector<std::string> depends;
  552. if (ListQtRccInputs(qtMajorVersion, sf, target, depends)) {
  553. entriesList = cmJoin(depends, "@list_sep@");
  554. } else {
  555. return;
  556. }
  557. }
  558. qrcInputs += qrcInputsSep;
  559. qrcInputs += entriesList;
  560. qrcInputsSep = ";";
  561. }
  562. }
  563. }
  564. makefile->AddDefinition(
  565. "_qt_rcc_inputs_" + target->GetName(),
  566. cmOutputConverter::EscapeForCMake(qrcInputs).c_str());
  567. makefile->AddDefinition(
  568. "_rcc_files", cmOutputConverter::EscapeForCMake(_rcc_files).c_str());
  569. makefile->AddDefinition(
  570. "_qt_rcc_options_files",
  571. cmOutputConverter::EscapeForCMake(rccFileFiles).c_str());
  572. makefile->AddDefinition(
  573. "_qt_rcc_options_options",
  574. cmOutputConverter::EscapeForCMake(rccFileOptions).c_str());
  575. makefile->AddDefinition("_qt_rcc_executable",
  576. GetRccExecutable(target).c_str());
  577. }
  578. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  579. cmGeneratorTarget* target)
  580. {
  581. cmMakefile* makefile = target->Target->GetMakefile();
  582. if (target->GetPropertyAsBool("AUTOMOC")) {
  583. std::string automocTargetName = GetAutogenTargetName(target);
  584. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  585. mocCppFile += "/";
  586. mocCppFile += automocTargetName;
  587. mocCppFile += ".cpp";
  588. makefile->GetOrCreateSource(mocCppFile, true);
  589. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", mocCppFile.c_str(),
  590. false);
  591. target->AddSource(mocCppFile);
  592. }
  593. }
  594. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  595. cmLocalGenerator* lg, cmGeneratorTarget* target)
  596. {
  597. cmMakefile* makefile = target->Target->GetMakefile();
  598. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  599. if (qtMajorVersion == "") {
  600. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  601. }
  602. // create a custom target for running generators at buildtime:
  603. std::string autogenTargetName = GetAutogenTargetName(target);
  604. std::string targetDir = GetAutogenTargetDir(target);
  605. cmCustomCommandLine currentLine;
  606. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  607. currentLine.push_back("-E");
  608. currentLine.push_back("cmake_autogen");
  609. currentLine.push_back(targetDir);
  610. currentLine.push_back("$<CONFIGURATION>");
  611. cmCustomCommandLines commandLines;
  612. commandLines.push_back(currentLine);
  613. std::string workingDirectory =
  614. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  615. std::vector<std::string> depends;
  616. if (const char* autogenDepends =
  617. target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  618. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  619. }
  620. std::vector<std::string> toolNames;
  621. if (target->GetPropertyAsBool("AUTOMOC")) {
  622. toolNames.push_back("moc");
  623. }
  624. if (target->GetPropertyAsBool("AUTOUIC")) {
  625. toolNames.push_back("uic");
  626. }
  627. if (target->GetPropertyAsBool("AUTORCC")) {
  628. toolNames.push_back("rcc");
  629. }
  630. std::string tools = toolNames[0];
  631. toolNames.erase(toolNames.begin());
  632. while (toolNames.size() > 1) {
  633. tools += ", " + toolNames[0];
  634. toolNames.erase(toolNames.begin());
  635. }
  636. if (toolNames.size() == 1) {
  637. tools += " and " + toolNames[0];
  638. }
  639. std::string autogenComment = "Automatic " + tools + " for target ";
  640. autogenComment += target->GetName();
  641. #if defined(_WIN32) && !defined(__CYGWIN__)
  642. bool usePRE_BUILD = false;
  643. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  644. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  645. cmGlobalVisualStudioGenerator* vsgg =
  646. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  647. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  648. // reduce the number of targets loaded into the IDE.
  649. // This also works around a VS 11 bug that may skip updating the target:
  650. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  651. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  652. if (usePRE_BUILD) {
  653. for (std::vector<std::string>::iterator it = depends.begin();
  654. it != depends.end(); ++it) {
  655. if (!makefile->FindTargetToUse(it->c_str())) {
  656. usePRE_BUILD = false;
  657. break;
  658. }
  659. }
  660. }
  661. }
  662. #endif
  663. std::vector<std::string> rcc_output;
  664. bool const isNinja = lg->GetGlobalGenerator()->GetName() == "Ninja";
  665. if (isNinja
  666. #if defined(_WIN32) && !defined(__CYGWIN__)
  667. || usePRE_BUILD
  668. #endif
  669. ) {
  670. std::vector<cmSourceFile*> srcFiles;
  671. target->GetConfigCommonSourceFiles(srcFiles);
  672. cmFilePathUuid fpathUuid(makefile);
  673. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  674. fileIt != srcFiles.end(); ++fileIt) {
  675. cmSourceFile* sf = *fileIt;
  676. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  677. std::string ext = sf->GetExtension();
  678. if (target->GetPropertyAsBool("AUTORCC")) {
  679. if (ext == "qrc" &&
  680. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  681. {
  682. std::string rcc_output_file = GetAutogenTargetBuildDir(target);
  683. // Create output directory
  684. cmSystemTools::MakeDirectory(rcc_output_file.c_str());
  685. rcc_output_file += fpathUuid.get(absFile, "qrc_", ".cpp");
  686. rcc_output.push_back(rcc_output_file);
  687. }
  688. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  689. ListQtRccInputs(qtMajorVersion, sf, target, depends);
  690. #if defined(_WIN32) && !defined(__CYGWIN__)
  691. // Cannot use PRE_BUILD because the resource files themselves
  692. // may not be sources within the target so VS may not know the
  693. // target needs to re-build at all.
  694. usePRE_BUILD = false;
  695. #endif
  696. }
  697. }
  698. }
  699. }
  700. }
  701. #if defined(_WIN32) && !defined(__CYGWIN__)
  702. if (usePRE_BUILD) {
  703. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  704. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  705. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  706. std::vector<std::string> no_output;
  707. std::vector<std::string> no_byproducts;
  708. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  709. commandLines, autogenComment.c_str(),
  710. workingDirectory.c_str());
  711. cc.SetEscapeOldStyle(false);
  712. cc.SetEscapeAllowMakeVars(true);
  713. target->Target->AddPreBuildCommand(cc);
  714. } else
  715. #endif
  716. {
  717. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  718. autogenTargetName, true, workingDirectory.c_str(),
  719. /*byproducts=*/rcc_output, depends, commandLines, false,
  720. autogenComment.c_str());
  721. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  722. lg->AddGeneratorTarget(gt);
  723. // Set target folder
  724. const char* autogenFolder =
  725. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  726. if (!autogenFolder) {
  727. autogenFolder =
  728. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  729. }
  730. if (autogenFolder && *autogenFolder) {
  731. autogenTarget->SetProperty("FOLDER", autogenFolder);
  732. } else {
  733. // inherit FOLDER property from target (#13688)
  734. utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
  735. }
  736. target->Target->AddUtility(autogenTargetName);
  737. }
  738. }
  739. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  740. cmGeneratorTarget const* target)
  741. {
  742. cmMakefile* makefile = target->Target->GetMakefile();
  743. // forget the variables added here afterwards again:
  744. cmMakefile::ScopePushPop varScope(makefile);
  745. static_cast<void>(varScope);
  746. // create a custom target for running generators at buildtime:
  747. std::string autogenTargetName = GetAutogenTargetName(target);
  748. makefile->AddDefinition(
  749. "_moc_target_name",
  750. cmOutputConverter::EscapeForCMake(autogenTargetName).c_str());
  751. makefile->AddDefinition(
  752. "_origin_target_name",
  753. cmOutputConverter::EscapeForCMake(target->GetName()).c_str());
  754. std::string targetDir = GetAutogenTargetDir(target);
  755. const char* qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  756. if (!qtVersion) {
  757. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  758. }
  759. if (const char* targetQtVersion =
  760. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  761. "")) {
  762. qtVersion = targetQtVersion;
  763. }
  764. if (qtVersion) {
  765. makefile->AddDefinition("_target_qt_version", qtVersion);
  766. }
  767. std::vector<std::string> skipUic;
  768. std::vector<std::string> skipMoc;
  769. std::vector<std::string> mocSources;
  770. std::vector<std::string> mocHeaders;
  771. std::map<std::string, std::string> configIncludes;
  772. std::map<std::string, std::string> configDefines;
  773. std::map<std::string, std::string> configUicOptions;
  774. if (target->GetPropertyAsBool("AUTOMOC") ||
  775. target->GetPropertyAsBool("AUTOUIC") ||
  776. target->GetPropertyAsBool("AUTORCC")) {
  777. SetupSourceFiles(target, skipMoc, mocSources, mocHeaders, skipUic);
  778. }
  779. makefile->AddDefinition(
  780. "_cpp_files",
  781. cmOutputConverter::EscapeForCMake(cmJoin(mocSources, ";")).c_str());
  782. if (target->GetPropertyAsBool("AUTOMOC")) {
  783. SetupAutoMocTarget(target, autogenTargetName, skipMoc, mocHeaders,
  784. configIncludes, configDefines);
  785. }
  786. if (target->GetPropertyAsBool("AUTOUIC")) {
  787. SetupAutoUicTarget(target, skipUic, configUicOptions);
  788. }
  789. if (target->GetPropertyAsBool("AUTORCC")) {
  790. SetupAutoRccTarget(target);
  791. }
  792. std::string inputFile = cmSystemTools::GetCMakeRoot();
  793. inputFile += "/Modules/AutogenInfo.cmake.in";
  794. std::string outputFile = targetDir;
  795. outputFile += "/AutogenInfo.cmake";
  796. makefile->AddDefinition(
  797. "_qt_rcc_inputs",
  798. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  799. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  800. false);
  801. // Ensure we have write permission in case .in was read-only.
  802. mode_t perm = 0;
  803. #if defined(_WIN32) && !defined(__CYGWIN__)
  804. mode_t mode_write = S_IWRITE;
  805. #else
  806. mode_t mode_write = S_IWUSR;
  807. #endif
  808. cmSystemTools::GetPermissions(outputFile, perm);
  809. if (!(perm & mode_write)) {
  810. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  811. }
  812. if (!configDefines.empty() || !configIncludes.empty() ||
  813. !configUicOptions.empty()) {
  814. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  815. if (!infoFile) {
  816. std::string error = "Internal CMake error when trying to open file: ";
  817. error += outputFile;
  818. error += " for writing.";
  819. cmSystemTools::Error(error.c_str());
  820. return;
  821. }
  822. if (!configDefines.empty()) {
  823. for (std::map<std::string, std::string>::iterator
  824. it = configDefines.begin(),
  825. end = configDefines.end();
  826. it != end; ++it) {
  827. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first << " "
  828. << it->second << ")\n";
  829. }
  830. }
  831. if (!configIncludes.empty()) {
  832. for (std::map<std::string, std::string>::iterator
  833. it = configIncludes.begin(),
  834. end = configIncludes.end();
  835. it != end; ++it) {
  836. infoFile << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
  837. << ")\n";
  838. }
  839. }
  840. if (!configUicOptions.empty()) {
  841. for (std::map<std::string, std::string>::iterator
  842. it = configUicOptions.begin(),
  843. end = configUicOptions.end();
  844. it != end; ++it) {
  845. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " "
  846. << it->second << ")\n";
  847. }
  848. }
  849. }
  850. }