cmQtAutoGeneratorInitializer.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 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 += ".dir/";
  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. cmFilePathUuid fpathUuid(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 rcc_output_file = GetAutogenTargetBuildDir(target);
  103. // Create output directory
  104. cmSystemTools::MakeDirectory(rcc_output_file.c_str());
  105. rcc_output_file += fpathUuid.get(absFile, "qrc_", ".cpp");
  106. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  107. rcc_output_file.c_str(), false);
  108. makefile->GetOrCreateSource(rcc_output_file, true);
  109. newRccFiles.push_back(rcc_output_file);
  110. }
  111. }
  112. if (!generated) {
  113. if (skipFileForMoc) {
  114. skipMoc.push_back(absFile);
  115. } else {
  116. cmSystemTools::FileFormat fileType =
  117. cmSystemTools::GetFileFormat(ext.c_str());
  118. if (fileType == cmSystemTools::CXX_FILE_FORMAT) {
  119. mocSources.push_back(absFile);
  120. } else if (fileType == cmSystemTools::HEADER_FILE_FORMAT) {
  121. mocHeaders.push_back(absFile);
  122. }
  123. }
  124. }
  125. }
  126. for (std::vector<std::string>::const_iterator fileIt = newRccFiles.begin();
  127. fileIt != newRccFiles.end(); ++fileIt) {
  128. const_cast<cmGeneratorTarget*>(target)->AddSource(*fileIt);
  129. }
  130. }
  131. static void GetCompileDefinitionsAndDirectories(
  132. cmGeneratorTarget const* target, const std::string& config,
  133. std::string& incs, std::string& defs)
  134. {
  135. std::vector<std::string> includeDirs;
  136. cmLocalGenerator* localGen = target->GetLocalGenerator();
  137. // Get the include dirs for this target, without stripping the implicit
  138. // include dirs off, see https://gitlab.kitware.com/cmake/cmake/issues/13667
  139. localGen->GetIncludeDirectories(includeDirs, target, "CXX", config, false);
  140. incs = cmJoin(includeDirs, ";");
  141. std::set<std::string> defines;
  142. localGen->AddCompileDefinitions(defines, target, config, "CXX");
  143. defs += cmJoin(defines, ";");
  144. }
  145. static void MocSetupAutoTarget(
  146. cmGeneratorTarget const* target, const std::string& autogenTargetName,
  147. std::vector<std::string> const& skipMoc,
  148. std::vector<std::string> const& mocHeaders,
  149. std::map<std::string, std::string>& configIncludes,
  150. std::map<std::string, std::string>& configDefines)
  151. {
  152. cmLocalGenerator* lg = target->GetLocalGenerator();
  153. cmMakefile* makefile = target->Target->GetMakefile();
  154. const char* tmp = target->GetProperty("AUTOMOC_MOC_OPTIONS");
  155. std::string _moc_options = (tmp != CM_NULLPTR ? tmp : "");
  156. makefile->AddDefinition(
  157. "_moc_options", cmOutputConverter::EscapeForCMake(_moc_options).c_str());
  158. makefile->AddDefinition(
  159. "_skip_moc",
  160. cmOutputConverter::EscapeForCMake(cmJoin(skipMoc, ";")).c_str());
  161. makefile->AddDefinition(
  162. "_moc_headers",
  163. cmOutputConverter::EscapeForCMake(cmJoin(mocHeaders, ";")).c_str());
  164. bool relaxedMode = makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE");
  165. makefile->AddDefinition("_moc_relaxed_mode", relaxedMode ? "TRUE" : "FALSE");
  166. std::string _moc_incs;
  167. std::string _moc_compile_defs;
  168. std::vector<std::string> configs;
  169. const std::string& config = makefile->GetConfigurations(configs);
  170. GetCompileDefinitionsAndDirectories(target, config, _moc_incs,
  171. _moc_compile_defs);
  172. makefile->AddDefinition(
  173. "_moc_incs", cmOutputConverter::EscapeForCMake(_moc_incs).c_str());
  174. makefile->AddDefinition(
  175. "_moc_compile_defs",
  176. cmOutputConverter::EscapeForCMake(_moc_compile_defs).c_str());
  177. for (std::vector<std::string>::const_iterator li = configs.begin();
  178. li != configs.end(); ++li) {
  179. std::string config_moc_incs;
  180. std::string config_moc_compile_defs;
  181. GetCompileDefinitionsAndDirectories(target, *li, config_moc_incs,
  182. config_moc_compile_defs);
  183. if (config_moc_incs != _moc_incs) {
  184. configIncludes[*li] = cmOutputConverter::EscapeForCMake(config_moc_incs);
  185. if (_moc_incs.empty()) {
  186. _moc_incs = config_moc_incs;
  187. }
  188. }
  189. if (config_moc_compile_defs != _moc_compile_defs) {
  190. configDefines[*li] =
  191. cmOutputConverter::EscapeForCMake(config_moc_compile_defs);
  192. if (_moc_compile_defs.empty()) {
  193. _moc_compile_defs = config_moc_compile_defs;
  194. }
  195. }
  196. }
  197. const char* qtVersion = makefile->GetDefinition("_target_qt_version");
  198. if (strcmp(qtVersion, "5") == 0) {
  199. cmGeneratorTarget* qt5Moc = lg->FindGeneratorTargetToUse("Qt5::moc");
  200. if (!qt5Moc) {
  201. cmSystemTools::Error("Qt5::moc target not found ",
  202. autogenTargetName.c_str());
  203. return;
  204. }
  205. makefile->AddDefinition("_qt_moc_executable",
  206. qt5Moc->ImportedGetLocation(""));
  207. } else if (strcmp(qtVersion, "4") == 0) {
  208. cmGeneratorTarget* qt4Moc = lg->FindGeneratorTargetToUse("Qt4::moc");
  209. if (!qt4Moc) {
  210. cmSystemTools::Error("Qt4::moc target not found ",
  211. autogenTargetName.c_str());
  212. return;
  213. }
  214. makefile->AddDefinition("_qt_moc_executable",
  215. qt4Moc->ImportedGetLocation(""));
  216. } else {
  217. cmSystemTools::Error("The CMAKE_AUTOMOC feature supports only Qt 4 and "
  218. "Qt 5 ",
  219. autogenTargetName.c_str());
  220. }
  221. }
  222. static void UicGetOpts(cmGeneratorTarget const* target,
  223. const std::string& config, std::string& optString)
  224. {
  225. std::vector<std::string> opts;
  226. target->GetAutoUicOptions(opts, config);
  227. optString = cmJoin(opts, ";");
  228. }
  229. static void UicSetupAutoTarget(
  230. cmGeneratorTarget const* target, std::vector<std::string> const& skipUic,
  231. std::map<std::string, std::string>& configUicOptions)
  232. {
  233. cmLocalGenerator* lg = target->GetLocalGenerator();
  234. cmMakefile* makefile = target->Target->GetMakefile();
  235. std::set<std::string> skipped;
  236. skipped.insert(skipUic.begin(), skipUic.end());
  237. makefile->AddDefinition(
  238. "_skip_uic",
  239. cmOutputConverter::EscapeForCMake(cmJoin(skipUic, ";")).c_str());
  240. std::vector<cmSourceFile*> uiFilesWithOptions =
  241. makefile->GetQtUiFilesWithOptions();
  242. const char* qtVersion = makefile->GetDefinition("_target_qt_version");
  243. std::string _uic_opts;
  244. std::vector<std::string> configs;
  245. const std::string& config = makefile->GetConfigurations(configs);
  246. UicGetOpts(target, config, _uic_opts);
  247. if (!_uic_opts.empty()) {
  248. _uic_opts = cmOutputConverter::EscapeForCMake(_uic_opts);
  249. makefile->AddDefinition("_uic_target_options", _uic_opts.c_str());
  250. }
  251. for (std::vector<std::string>::const_iterator li = configs.begin();
  252. li != configs.end(); ++li) {
  253. std::string config_uic_opts;
  254. UicGetOpts(target, *li, config_uic_opts);
  255. if (config_uic_opts != _uic_opts) {
  256. configUicOptions[*li] =
  257. cmOutputConverter::EscapeForCMake(config_uic_opts);
  258. if (_uic_opts.empty()) {
  259. _uic_opts = config_uic_opts;
  260. }
  261. }
  262. }
  263. std::string uiFileFiles;
  264. std::string uiFileOptions;
  265. const char* sep = "";
  266. for (std::vector<cmSourceFile*>::const_iterator fileIt =
  267. uiFilesWithOptions.begin();
  268. fileIt != uiFilesWithOptions.end(); ++fileIt) {
  269. cmSourceFile* sf = *fileIt;
  270. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  271. if (!skipped.insert(absFile).second) {
  272. continue;
  273. }
  274. uiFileFiles += sep;
  275. uiFileFiles += absFile;
  276. uiFileOptions += sep;
  277. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  278. cmSystemTools::ReplaceString(opts, ";", "@list_sep@");
  279. uiFileOptions += opts;
  280. sep = ";";
  281. }
  282. makefile->AddDefinition(
  283. "_qt_uic_options_files",
  284. cmOutputConverter::EscapeForCMake(uiFileFiles).c_str());
  285. makefile->AddDefinition(
  286. "_qt_uic_options_options",
  287. cmOutputConverter::EscapeForCMake(uiFileOptions).c_str());
  288. std::string targetName = target->GetName();
  289. if (strcmp(qtVersion, "5") == 0) {
  290. cmGeneratorTarget* qt5Uic = lg->FindGeneratorTargetToUse("Qt5::uic");
  291. if (!qt5Uic) {
  292. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  293. } else {
  294. makefile->AddDefinition("_qt_uic_executable",
  295. qt5Uic->ImportedGetLocation(""));
  296. }
  297. } else if (strcmp(qtVersion, "4") == 0) {
  298. cmGeneratorTarget* qt4Uic = lg->FindGeneratorTargetToUse("Qt4::uic");
  299. if (!qt4Uic) {
  300. cmSystemTools::Error("Qt4::uic target not found ", targetName.c_str());
  301. return;
  302. }
  303. makefile->AddDefinition("_qt_uic_executable",
  304. qt4Uic->ImportedGetLocation(""));
  305. } else {
  306. cmSystemTools::Error("The CMAKE_AUTOUIC feature supports only Qt 4 and "
  307. "Qt 5 ",
  308. targetName.c_str());
  309. }
  310. }
  311. static std::string RccGetExecutable(cmGeneratorTarget const* target)
  312. {
  313. cmLocalGenerator* lg = target->GetLocalGenerator();
  314. cmMakefile* makefile = target->Target->GetMakefile();
  315. const char* qtVersion = makefile->GetDefinition("_target_qt_version");
  316. if (!qtVersion) {
  317. qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  318. if (!qtVersion) {
  319. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  320. }
  321. if (const char* targetQtVersion =
  322. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  323. "")) {
  324. qtVersion = targetQtVersion;
  325. }
  326. }
  327. std::string targetName = target->GetName();
  328. if (strcmp(qtVersion, "5") == 0) {
  329. cmGeneratorTarget* qt5Rcc = lg->FindGeneratorTargetToUse("Qt5::rcc");
  330. if (!qt5Rcc) {
  331. cmSystemTools::Error("Qt5::rcc target not found ", targetName.c_str());
  332. return std::string();
  333. }
  334. return qt5Rcc->ImportedGetLocation("");
  335. }
  336. if (strcmp(qtVersion, "4") == 0) {
  337. cmGeneratorTarget* qt4Rcc = lg->FindGeneratorTargetToUse("Qt4::rcc");
  338. if (!qt4Rcc) {
  339. cmSystemTools::Error("Qt4::rcc target not found ", targetName.c_str());
  340. return std::string();
  341. }
  342. return qt4Rcc->ImportedGetLocation("");
  343. }
  344. cmSystemTools::Error("The CMAKE_AUTORCC feature supports only Qt 4 and "
  345. "Qt 5 ",
  346. targetName.c_str());
  347. return std::string();
  348. }
  349. static void RccMergeOptions(std::vector<std::string>& opts,
  350. const std::vector<std::string>& fileOpts,
  351. bool isQt5)
  352. {
  353. static const char* valueOptions[] = { "name", "root", "compress",
  354. "threshold" };
  355. std::vector<std::string> extraOpts;
  356. for (std::vector<std::string>::const_iterator it = fileOpts.begin();
  357. it != fileOpts.end(); ++it) {
  358. std::vector<std::string>::iterator existingIt =
  359. std::find(opts.begin(), opts.end(), *it);
  360. if (existingIt != opts.end()) {
  361. const char* o = it->c_str();
  362. if (*o == '-') {
  363. ++o;
  364. }
  365. if (isQt5 && *o == '-') {
  366. ++o;
  367. }
  368. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  369. cmStrCmp(*it)) != cmArrayEnd(valueOptions)) {
  370. assert(existingIt + 1 != opts.end());
  371. *(existingIt + 1) = *(it + 1);
  372. ++it;
  373. }
  374. } else {
  375. extraOpts.push_back(*it);
  376. }
  377. }
  378. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  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. const std::string rccCommand = RccGetExecutable(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. RccMergeOptions(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. "_rcc_inputs", cmOutputConverter::EscapeForCMake(qrcInputs).c_str());
  564. makefile->AddDefinition(
  565. "_rcc_files", cmOutputConverter::EscapeForCMake(_rcc_files).c_str());
  566. makefile->AddDefinition(
  567. "_rcc_options_files",
  568. cmOutputConverter::EscapeForCMake(rccFileFiles).c_str());
  569. makefile->AddDefinition(
  570. "_rcc_options_options",
  571. cmOutputConverter::EscapeForCMake(rccFileOptions).c_str());
  572. makefile->AddDefinition("_qt_rcc_executable",
  573. RccGetExecutable(target).c_str());
  574. }
  575. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  576. cmGeneratorTarget* target)
  577. {
  578. cmMakefile* makefile = target->Target->GetMakefile();
  579. if (target->GetPropertyAsBool("AUTOMOC")) {
  580. std::string automocTargetName = GetAutogenTargetName(target);
  581. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  582. mocCppFile += "/";
  583. mocCppFile += automocTargetName;
  584. mocCppFile += ".cpp";
  585. makefile->GetOrCreateSource(mocCppFile, true);
  586. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", mocCppFile.c_str(),
  587. false);
  588. target->AddSource(mocCppFile);
  589. }
  590. }
  591. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  592. cmLocalGenerator* lg, cmGeneratorTarget* target)
  593. {
  594. cmMakefile* makefile = target->Target->GetMakefile();
  595. // Create a custom target for running generators at buildtime
  596. const std::string autogenTargetName = GetAutogenTargetName(target);
  597. const std::string workingDirectory =
  598. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  599. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  600. if (qtMajorVersion == "") {
  601. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  602. }
  603. std::vector<std::string> depends;
  604. if (const char* autogenDepends =
  605. target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  606. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  607. }
  608. // Compose command lines
  609. cmCustomCommandLines commandLines;
  610. {
  611. cmCustomCommandLine currentLine;
  612. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  613. currentLine.push_back("-E");
  614. currentLine.push_back("cmake_autogen");
  615. currentLine.push_back(GetAutogenTargetFilesDir(target));
  616. currentLine.push_back("$<CONFIGURATION>");
  617. commandLines.push_back(currentLine);
  618. }
  619. // Compose target comment
  620. std::string autogenComment;
  621. {
  622. std::vector<std::string> toolNames;
  623. if (target->GetPropertyAsBool("AUTOMOC")) {
  624. toolNames.push_back("moc");
  625. }
  626. if (target->GetPropertyAsBool("AUTOUIC")) {
  627. toolNames.push_back("uic");
  628. }
  629. if (target->GetPropertyAsBool("AUTORCC")) {
  630. toolNames.push_back("rcc");
  631. }
  632. std::string tools = toolNames[0];
  633. toolNames.erase(toolNames.begin());
  634. while (toolNames.size() > 1) {
  635. tools += ", " + toolNames[0];
  636. toolNames.erase(toolNames.begin());
  637. }
  638. if (toolNames.size() == 1) {
  639. tools += " and " + toolNames[0];
  640. }
  641. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  642. }
  643. #if defined(_WIN32) && !defined(__CYGWIN__)
  644. bool usePRE_BUILD = false;
  645. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  646. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  647. cmGlobalVisualStudioGenerator* vsgg =
  648. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  649. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  650. // reduce the number of targets loaded into the IDE.
  651. // This also works around a VS 11 bug that may skip updating the target:
  652. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  653. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  654. if (usePRE_BUILD) {
  655. for (std::vector<std::string>::iterator it = depends.begin();
  656. it != depends.end(); ++it) {
  657. if (!makefile->FindTargetToUse(it->c_str())) {
  658. usePRE_BUILD = false;
  659. break;
  660. }
  661. }
  662. }
  663. }
  664. #endif
  665. std::vector<std::string> rcc_output;
  666. bool const isNinja = lg->GetGlobalGenerator()->GetName() == "Ninja";
  667. if (isNinja
  668. #if defined(_WIN32) && !defined(__CYGWIN__)
  669. || usePRE_BUILD
  670. #endif
  671. ) {
  672. std::vector<cmSourceFile*> srcFiles;
  673. target->GetConfigCommonSourceFiles(srcFiles);
  674. cmFilePathUuid fpathUuid(makefile);
  675. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  676. fileIt != srcFiles.end(); ++fileIt) {
  677. cmSourceFile* sf = *fileIt;
  678. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  679. std::string ext = sf->GetExtension();
  680. if (target->GetPropertyAsBool("AUTORCC")) {
  681. if (ext == "qrc" &&
  682. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  683. {
  684. std::string rcc_output_file = GetAutogenTargetBuildDir(target);
  685. // Create output directory
  686. cmSystemTools::MakeDirectory(rcc_output_file.c_str());
  687. rcc_output_file += fpathUuid.get(absFile, "qrc_", ".cpp");
  688. rcc_output.push_back(rcc_output_file);
  689. }
  690. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  691. RccListInputs(qtMajorVersion, sf, target, depends);
  692. #if defined(_WIN32) && !defined(__CYGWIN__)
  693. // Cannot use PRE_BUILD because the resource files themselves
  694. // may not be sources within the target so VS may not know the
  695. // target needs to re-build at all.
  696. usePRE_BUILD = false;
  697. #endif
  698. }
  699. }
  700. }
  701. }
  702. }
  703. #if defined(_WIN32) && !defined(__CYGWIN__)
  704. if (usePRE_BUILD) {
  705. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  706. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  707. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  708. std::vector<std::string> no_output;
  709. std::vector<std::string> no_byproducts;
  710. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  711. commandLines, autogenComment.c_str(),
  712. workingDirectory.c_str());
  713. cc.SetEscapeOldStyle(false);
  714. cc.SetEscapeAllowMakeVars(true);
  715. target->Target->AddPreBuildCommand(cc);
  716. } else
  717. #endif
  718. {
  719. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  720. autogenTargetName, true, workingDirectory.c_str(),
  721. /*byproducts=*/rcc_output, depends, commandLines, false,
  722. autogenComment.c_str());
  723. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  724. lg->AddGeneratorTarget(gt);
  725. // Set target folder
  726. const char* autogenFolder =
  727. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  728. if (!autogenFolder) {
  729. autogenFolder =
  730. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  731. }
  732. if (autogenFolder && *autogenFolder) {
  733. autogenTarget->SetProperty("FOLDER", autogenFolder);
  734. } else {
  735. // inherit FOLDER property from target (#13688)
  736. utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
  737. }
  738. target->Target->AddUtility(autogenTargetName);
  739. }
  740. }
  741. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  742. cmGeneratorTarget const* target)
  743. {
  744. cmMakefile* makefile = target->Target->GetMakefile();
  745. // forget the variables added here afterwards again:
  746. cmMakefile::ScopePushPop varScope(makefile);
  747. static_cast<void>(varScope);
  748. // create a custom target for running generators at buildtime:
  749. const std::string autogenTargetName = GetAutogenTargetName(target);
  750. makefile->AddDefinition(
  751. "_moc_target_name",
  752. cmOutputConverter::EscapeForCMake(autogenTargetName).c_str());
  753. makefile->AddDefinition(
  754. "_origin_target_name",
  755. cmOutputConverter::EscapeForCMake(target->GetName()).c_str());
  756. const char* qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  757. if (!qtVersion) {
  758. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  759. }
  760. if (const char* targetQtVersion =
  761. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  762. "")) {
  763. qtVersion = targetQtVersion;
  764. }
  765. if (qtVersion) {
  766. makefile->AddDefinition("_target_qt_version", qtVersion);
  767. }
  768. std::vector<std::string> skipUic;
  769. std::vector<std::string> skipMoc;
  770. std::vector<std::string> mocSources;
  771. std::vector<std::string> mocHeaders;
  772. std::map<std::string, std::string> configMocIncludes;
  773. std::map<std::string, std::string> configMocDefines;
  774. std::map<std::string, std::string> configUicOptions;
  775. if (target->GetPropertyAsBool("AUTOMOC") ||
  776. target->GetPropertyAsBool("AUTOUIC") ||
  777. target->GetPropertyAsBool("AUTORCC")) {
  778. SetupSourceFiles(target, skipMoc, mocSources, mocHeaders, skipUic);
  779. }
  780. makefile->AddDefinition(
  781. "_cpp_files",
  782. cmOutputConverter::EscapeForCMake(cmJoin(mocSources, ";")).c_str());
  783. if (target->GetPropertyAsBool("AUTOMOC")) {
  784. MocSetupAutoTarget(target, autogenTargetName, skipMoc, mocHeaders,
  785. configMocIncludes, configMocDefines);
  786. }
  787. if (target->GetPropertyAsBool("AUTOUIC")) {
  788. UicSetupAutoTarget(target, skipUic, configUicOptions);
  789. }
  790. if (target->GetPropertyAsBool("AUTORCC")) {
  791. RccSetupAutoTarget(target);
  792. }
  793. // Generate config file
  794. std::string inputFile = cmSystemTools::GetCMakeRoot();
  795. inputFile += "/Modules/AutogenInfo.cmake.in";
  796. std::string outputFile = GetAutogenTargetFilesDir(target);
  797. outputFile += "/AutogenInfo.cmake";
  798. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  799. false);
  800. // Append custom definitions to config file
  801. if (!configMocDefines.empty() || !configMocIncludes.empty() ||
  802. !configUicOptions.empty()) {
  803. // Ensure we have write permission in case .in was read-only.
  804. mode_t perm = 0;
  805. #if defined(_WIN32) && !defined(__CYGWIN__)
  806. mode_t mode_write = S_IWRITE;
  807. #else
  808. mode_t mode_write = S_IWUSR;
  809. #endif
  810. cmSystemTools::GetPermissions(outputFile, perm);
  811. if (!(perm & mode_write)) {
  812. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  813. }
  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 (!configMocDefines.empty()) {
  823. for (std::map<std::string, std::string>::iterator
  824. it = configMocDefines.begin(),
  825. end = configMocDefines.end();
  826. it != end; ++it) {
  827. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first << " "
  828. << it->second << ")\n";
  829. }
  830. }
  831. if (!configMocIncludes.empty()) {
  832. for (std::map<std::string, std::string>::iterator
  833. it = configMocIncludes.begin(),
  834. end = configMocIncludes.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. }