cmQtAutoGeneratorInitializer.cxx 34 KB

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