cmQtAutoGeneratorInitializer.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 search paths
  283. {
  284. std::vector<std::string> uicSearchPaths;
  285. cmSystemTools::ExpandListArgument(
  286. GetSafeProperty(target, "AUTOUIC_SEARCH_PATHS"), uicSearchPaths);
  287. const std::string srcDir = makefile->GetCurrentSourceDirectory();
  288. for (std::vector<std::string>::iterator it = uicSearchPaths.begin();
  289. it != uicSearchPaths.end(); ++it) {
  290. *it = cmSystemTools::CollapseFullPath(*it, srcDir);
  291. }
  292. AddDefinitionEscaped(makefile, "_uic_search_paths", uicSearchPaths);
  293. }
  294. // Uic target options
  295. {
  296. std::string _uic_opts;
  297. std::vector<std::string> configs;
  298. UicGetOpts(target, makefile->GetConfigurations(configs), _uic_opts);
  299. AddDefinitionEscaped(makefile, "_uic_target_options", _uic_opts);
  300. for (std::vector<std::string>::const_iterator li = configs.begin();
  301. li != configs.end(); ++li) {
  302. std::string config_uic_opts;
  303. UicGetOpts(target, *li, config_uic_opts);
  304. if (config_uic_opts != _uic_opts) {
  305. configUicOptions[*li] =
  306. cmOutputConverter::EscapeForCMake(config_uic_opts);
  307. if (_uic_opts.empty()) {
  308. _uic_opts = config_uic_opts;
  309. }
  310. }
  311. }
  312. }
  313. // Uic files options
  314. {
  315. std::vector<std::string> uiFileFiles;
  316. std::vector<std::string> uiFileOptions;
  317. {
  318. std::set<std::string> skipped;
  319. skipped.insert(uicSkipList.begin(), uicSkipList.end());
  320. const std::vector<cmSourceFile*> uiFilesWithOptions =
  321. makefile->GetQtUiFilesWithOptions();
  322. for (std::vector<cmSourceFile*>::const_iterator fileIt =
  323. uiFilesWithOptions.begin();
  324. fileIt != uiFilesWithOptions.end(); ++fileIt) {
  325. cmSourceFile* sf = *fileIt;
  326. const std::string absFile =
  327. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  328. if (skipped.insert(absFile).second) {
  329. // The file wasn't skipped
  330. uiFileFiles.push_back(absFile);
  331. {
  332. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  333. cmSystemTools::ReplaceString(opts, ";", "@list_sep@");
  334. uiFileOptions.push_back(opts);
  335. }
  336. }
  337. }
  338. }
  339. AddDefinitionEscaped(makefile, "_qt_uic_options_files", uiFileFiles);
  340. AddDefinitionEscaped(makefile, "_qt_uic_options_options", uiFileOptions);
  341. }
  342. // Uic executable
  343. {
  344. std::string err;
  345. const char* uicExec = CM_NULLPTR;
  346. if (qtMajorVersion == "5") {
  347. cmGeneratorTarget* qt5Uic = lg->FindGeneratorTargetToUse("Qt5::uic");
  348. if (qt5Uic != CM_NULLPTR) {
  349. uicExec = qt5Uic->ImportedGetLocation("");
  350. } else {
  351. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  352. }
  353. } else if (qtMajorVersion == "4") {
  354. cmGeneratorTarget* qt4Uic = lg->FindGeneratorTargetToUse("Qt4::uic");
  355. if (qt4Uic != CM_NULLPTR) {
  356. uicExec = qt4Uic->ImportedGetLocation("");
  357. } else {
  358. err = "Qt4::uic target not found " + target->GetName();
  359. }
  360. } else {
  361. err = "The CMAKE_AUTOUIC feature supports only Qt 4 and Qt 5 ";
  362. err += target->GetName();
  363. }
  364. // Add definition or error
  365. if (err.empty()) {
  366. AddDefinitionEscaped(makefile, "_qt_uic_executable",
  367. uicExec ? uicExec : "");
  368. } else {
  369. cmSystemTools::Error(err.c_str());
  370. }
  371. }
  372. }
  373. static std::string RccGetExecutable(cmGeneratorTarget const* target,
  374. const std::string& qtMajorVersion)
  375. {
  376. std::string rccExec;
  377. cmLocalGenerator* lg = target->GetLocalGenerator();
  378. if (qtMajorVersion == "5") {
  379. cmGeneratorTarget* qt5Rcc = lg->FindGeneratorTargetToUse("Qt5::rcc");
  380. if (qt5Rcc != CM_NULLPTR) {
  381. rccExec = qt5Rcc->ImportedGetLocation("");
  382. } else {
  383. cmSystemTools::Error("Qt5::rcc target not found ",
  384. target->GetName().c_str());
  385. }
  386. } else if (qtMajorVersion == "4") {
  387. cmGeneratorTarget* qt4Rcc = lg->FindGeneratorTargetToUse("Qt4::rcc");
  388. if (qt4Rcc != CM_NULLPTR) {
  389. rccExec = qt4Rcc->ImportedGetLocation("");
  390. } else {
  391. cmSystemTools::Error("Qt4::rcc target not found ",
  392. target->GetName().c_str());
  393. }
  394. } else {
  395. cmSystemTools::Error(
  396. "The CMAKE_AUTORCC feature supports only Qt 4 and Qt 5 ",
  397. target->GetName().c_str());
  398. }
  399. return rccExec;
  400. }
  401. static void RccMergeOptions(std::vector<std::string>& opts,
  402. const std::vector<std::string>& fileOpts,
  403. bool isQt5)
  404. {
  405. static const char* valueOptions[] = { "name", "root", "compress",
  406. "threshold" };
  407. std::vector<std::string> extraOpts;
  408. for (std::vector<std::string>::const_iterator fit = fileOpts.begin();
  409. fit != fileOpts.end(); ++fit) {
  410. std::vector<std::string>::iterator existingIt =
  411. std::find(opts.begin(), opts.end(), *fit);
  412. if (existingIt != opts.end()) {
  413. const char* optName = fit->c_str();
  414. if (*optName == '-') {
  415. ++optName;
  416. if (isQt5 && *optName == '-') {
  417. ++optName;
  418. }
  419. }
  420. // Test if this is a value option and change the existing value
  421. if ((optName != fit->c_str()) &&
  422. std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  423. cmStrCmp(optName)) != cmArrayEnd(valueOptions)) {
  424. const std::vector<std::string>::iterator existValueIt(existingIt + 1);
  425. const std::vector<std::string>::const_iterator fileValueIt(fit + 1);
  426. if ((existValueIt != opts.end()) && (fileValueIt != fileOpts.end())) {
  427. *existValueIt = *fileValueIt;
  428. ++fit;
  429. }
  430. }
  431. } else {
  432. extraOpts.push_back(*fit);
  433. }
  434. }
  435. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  436. }
  437. /// @brief Reads the resource files list from from a .qrc file - Qt5 version
  438. /// @return True if the .qrc file was successfully parsed
  439. static bool RccListInputsQt5(cmSourceFile* sf, cmGeneratorTarget const* target,
  440. std::vector<std::string>& depends)
  441. {
  442. const std::string rccCommand = RccGetExecutable(target, "5");
  443. if (rccCommand.empty()) {
  444. cmSystemTools::Error("AUTOGEN: error: rcc executable not available\n");
  445. return false;
  446. }
  447. bool hasDashDashList = false;
  448. // Read rcc features
  449. {
  450. std::vector<std::string> command;
  451. command.push_back(rccCommand);
  452. command.push_back("--help");
  453. std::string rccStdOut;
  454. std::string rccStdErr;
  455. int retVal = 0;
  456. bool result =
  457. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  458. CM_NULLPTR, cmSystemTools::OUTPUT_NONE);
  459. if (result && retVal == 0 &&
  460. rccStdOut.find("--list") != std::string::npos) {
  461. hasDashDashList = true;
  462. }
  463. }
  464. // Run rcc list command
  465. std::vector<std::string> command;
  466. command.push_back(rccCommand);
  467. command.push_back(hasDashDashList ? "--list" : "-list");
  468. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  469. command.push_back(absFile);
  470. std::string rccStdOut;
  471. std::string rccStdErr;
  472. int retVal = 0;
  473. bool result =
  474. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  475. CM_NULLPTR, cmSystemTools::OUTPUT_NONE);
  476. if (!result || retVal) {
  477. std::ostringstream err;
  478. err << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  479. << " failed:\n"
  480. << rccStdOut << "\n"
  481. << rccStdErr << std::endl;
  482. cmSystemTools::Error(err.str().c_str());
  483. return false;
  484. }
  485. // Parse rcc list output
  486. {
  487. std::istringstream ostr(rccStdOut);
  488. std::string oline;
  489. while (std::getline(ostr, oline)) {
  490. oline = utilStripCR(oline);
  491. if (!oline.empty()) {
  492. depends.push_back(oline);
  493. }
  494. }
  495. }
  496. {
  497. std::istringstream estr(rccStdErr);
  498. std::string eline;
  499. while (std::getline(estr, eline)) {
  500. eline = utilStripCR(eline);
  501. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  502. static std::string searchString = "Cannot find file '";
  503. std::string::size_type pos = eline.find(searchString);
  504. if (pos == std::string::npos) {
  505. std::ostringstream err;
  506. err << "AUTOGEN: error: Rcc lists unparsable output " << eline
  507. << std::endl;
  508. cmSystemTools::Error(err.str().c_str());
  509. return false;
  510. }
  511. pos += searchString.length();
  512. std::string::size_type sz = eline.size() - pos - 1;
  513. depends.push_back(eline.substr(pos, sz));
  514. }
  515. }
  516. }
  517. return true;
  518. }
  519. /// @brief Reads the resource files list from from a .qrc file - Qt4 version
  520. /// @return True if the .qrc file was successfully parsed
  521. static bool RccListInputsQt4(cmSourceFile* sf,
  522. std::vector<std::string>& depends)
  523. {
  524. // Read file into string
  525. std::string qrcContents;
  526. {
  527. std::ostringstream stream;
  528. stream << cmsys::ifstream(sf->GetFullPath().c_str()).rdbuf();
  529. qrcContents = stream.str();
  530. }
  531. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  532. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  533. size_t offset = 0;
  534. while (fileMatchRegex.find(qrcContents.c_str() + offset)) {
  535. std::string qrcEntry = fileMatchRegex.match(1);
  536. offset += qrcEntry.size();
  537. {
  538. fileReplaceRegex.find(qrcEntry);
  539. std::string tag = fileReplaceRegex.match(1);
  540. qrcEntry = qrcEntry.substr(tag.size());
  541. }
  542. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str())) {
  543. qrcEntry = sf->GetLocation().GetDirectory() + "/" + qrcEntry;
  544. }
  545. depends.push_back(qrcEntry);
  546. }
  547. return true;
  548. }
  549. /// @brief Reads the resource files list from from a .qrc file
  550. /// @return True if the rcc file was successfully parsed
  551. static bool RccListInputs(const std::string& qtMajorVersion, cmSourceFile* sf,
  552. cmGeneratorTarget const* target,
  553. std::vector<std::string>& depends)
  554. {
  555. if (qtMajorVersion == "5") {
  556. return RccListInputsQt5(sf, target, depends);
  557. }
  558. return RccListInputsQt4(sf, depends);
  559. }
  560. static void RccSetupAutoTarget(cmGeneratorTarget const* target,
  561. const std::string& qtMajorVersion)
  562. {
  563. cmMakefile* makefile = target->Target->GetMakefile();
  564. const bool qtMajorVersion5 = (qtMajorVersion == "5");
  565. std::vector<std::string> _rcc_files;
  566. std::vector<std::string> _rcc_inputs;
  567. std::vector<std::string> rccFileFiles;
  568. std::vector<std::string> rccFileOptions;
  569. std::vector<std::string> rccOptionsTarget;
  570. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS")) {
  571. cmSystemTools::ExpandListArgument(opts, rccOptionsTarget);
  572. }
  573. std::vector<cmSourceFile*> srcFiles;
  574. target->GetConfigCommonSourceFiles(srcFiles);
  575. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  576. fileIt != srcFiles.end(); ++fileIt) {
  577. cmSourceFile* sf = *fileIt;
  578. if (sf->GetExtension() == "qrc") {
  579. const bool skip =
  580. cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN")) ||
  581. cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  582. if (!skip) {
  583. const std::string absFile =
  584. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  585. // qrc file
  586. _rcc_files.push_back(absFile);
  587. // qrc file entries
  588. {
  589. std::string entriesList;
  590. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  591. std::vector<std::string> depends;
  592. if (RccListInputs(qtMajorVersion, sf, target, depends)) {
  593. entriesList = cmJoin(depends, "@list_sep@");
  594. } else {
  595. return;
  596. }
  597. }
  598. _rcc_inputs.push_back(entriesList);
  599. }
  600. // rcc options for this qrc file
  601. {
  602. // Merged target and file options
  603. std::vector<std::string> rccOptions(rccOptionsTarget);
  604. if (const char* prop = sf->GetProperty("AUTORCC_OPTIONS")) {
  605. std::vector<std::string> optsVec;
  606. cmSystemTools::ExpandListArgument(prop, optsVec);
  607. RccMergeOptions(rccOptions, optsVec, qtMajorVersion5);
  608. }
  609. // Only store non empty options lists
  610. if (!rccOptions.empty()) {
  611. rccFileFiles.push_back(absFile);
  612. rccFileOptions.push_back(cmJoin(rccOptions, "@list_sep@"));
  613. }
  614. }
  615. }
  616. }
  617. }
  618. AddDefinitionEscaped(makefile, "_rcc_files", _rcc_files);
  619. AddDefinitionEscaped(makefile, "_rcc_inputs", _rcc_inputs);
  620. AddDefinitionEscaped(makefile, "_rcc_options_files", rccFileFiles);
  621. AddDefinitionEscaped(makefile, "_rcc_options_options", rccFileOptions);
  622. AddDefinitionEscaped(makefile, "_qt_rcc_executable",
  623. RccGetExecutable(target, qtMajorVersion));
  624. }
  625. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  626. cmGeneratorTarget* target)
  627. {
  628. if (target->GetPropertyAsBool("AUTOMOC")) {
  629. cmMakefile* makefile = target->Target->GetMakefile();
  630. const std::string mocCppFile =
  631. GetAutogenTargetBuildDir(target) + "moc_compilation.cpp";
  632. cmSourceFile* gf = makefile->GetOrCreateSource(mocCppFile, true);
  633. gf->SetProperty("SKIP_AUTOGEN", "On");
  634. target->AddSource(mocCppFile);
  635. }
  636. }
  637. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  638. cmLocalGenerator* lg, cmGeneratorTarget* target)
  639. {
  640. cmMakefile* makefile = target->Target->GetMakefile();
  641. // Create a custom target for running generators at buildtime
  642. const std::string autogenTargetName = GetAutogenTargetName(target);
  643. const std::string autogenBuildDir = GetAutogenTargetBuildDir(target);
  644. const std::string workingDirectory =
  645. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  646. const std::string qtMajorVersion = GetQtMajorVersion(target);
  647. std::vector<std::string> autogenOutputFiles;
  648. // Remove old settings on cleanup
  649. {
  650. std::string fname = GetAutogenTargetFilesDir(target);
  651. fname += "/AutogenOldSettings.cmake";
  652. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fname.c_str(),
  653. false);
  654. }
  655. // Create autogen target build directory and add it to the clean files
  656. cmSystemTools::MakeDirectory(autogenBuildDir);
  657. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  658. autogenBuildDir.c_str(), false);
  659. if (target->GetPropertyAsBool("AUTOMOC") ||
  660. target->GetPropertyAsBool("AUTOUIC")) {
  661. // Create autogen target includes directory and
  662. // add it to the origin target INCLUDE_DIRECTORIES
  663. const std::string incsDir = autogenBuildDir + "include";
  664. cmSystemTools::MakeDirectory(incsDir);
  665. target->AddIncludeDirectory(incsDir, true);
  666. }
  667. if (target->GetPropertyAsBool("AUTOMOC")) {
  668. // Register moc compilation file as generated
  669. autogenOutputFiles.push_back(autogenBuildDir + "moc_compilation.cpp");
  670. }
  671. // Initialize autogen target dependencies
  672. std::vector<std::string> depends;
  673. if (const char* autogenDepends =
  674. target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  675. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  676. }
  677. // Compose command lines
  678. cmCustomCommandLines commandLines;
  679. {
  680. cmCustomCommandLine currentLine;
  681. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  682. currentLine.push_back("-E");
  683. currentLine.push_back("cmake_autogen");
  684. currentLine.push_back(GetAutogenTargetFilesDir(target));
  685. currentLine.push_back("$<CONFIGURATION>");
  686. commandLines.push_back(currentLine);
  687. }
  688. // Compose target comment
  689. std::string autogenComment;
  690. {
  691. std::vector<std::string> toolNames;
  692. if (target->GetPropertyAsBool("AUTOMOC")) {
  693. toolNames.push_back("MOC");
  694. }
  695. if (target->GetPropertyAsBool("AUTOUIC")) {
  696. toolNames.push_back("UIC");
  697. }
  698. if (target->GetPropertyAsBool("AUTORCC")) {
  699. toolNames.push_back("RCC");
  700. }
  701. std::string tools = toolNames[0];
  702. toolNames.erase(toolNames.begin());
  703. while (toolNames.size() > 1) {
  704. tools += ", " + toolNames[0];
  705. toolNames.erase(toolNames.begin());
  706. }
  707. if (toolNames.size() == 1) {
  708. tools += " and " + toolNames[0];
  709. }
  710. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  711. }
  712. #if defined(_WIN32) && !defined(__CYGWIN__)
  713. bool usePRE_BUILD = false;
  714. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  715. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  716. cmGlobalVisualStudioGenerator* vsgg =
  717. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  718. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  719. // reduce the number of targets loaded into the IDE.
  720. // This also works around a VS 11 bug that may skip updating the target:
  721. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  722. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  723. if (usePRE_BUILD) {
  724. // If the autogen target depends on an other target
  725. // don't use PRE_BUILD
  726. for (std::vector<std::string>::iterator it = depends.begin();
  727. it != depends.end(); ++it) {
  728. if (!makefile->FindTargetToUse(it->c_str())) {
  729. usePRE_BUILD = false;
  730. break;
  731. }
  732. }
  733. }
  734. }
  735. #endif
  736. if (target->GetPropertyAsBool("AUTORCC")) {
  737. cmFilePathChecksum fpathCheckSum(makefile);
  738. std::vector<cmSourceFile*> srcFiles;
  739. target->GetConfigCommonSourceFiles(srcFiles);
  740. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  741. fileIt != srcFiles.end(); ++fileIt) {
  742. cmSourceFile* sf = *fileIt;
  743. if (sf->GetExtension() == "qrc" &&
  744. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOGEN")) &&
  745. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  746. {
  747. const std::string absFile =
  748. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  749. // Run cmake again when .qrc file changes
  750. makefile->AddCMakeDependFile(absFile);
  751. std::string rccOutputFile = autogenBuildDir;
  752. rccOutputFile += fpathCheckSum.getPart(absFile);
  753. rccOutputFile += "/qrc_";
  754. rccOutputFile +=
  755. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFile);
  756. rccOutputFile += ".cpp";
  757. // Add rcc output file to origin target sources
  758. cmSourceFile* gf = makefile->GetOrCreateSource(rccOutputFile, true);
  759. gf->SetProperty("SKIP_AUTOGEN", "On");
  760. target->AddSource(rccOutputFile);
  761. // Register rcc output file as generated
  762. autogenOutputFiles.push_back(rccOutputFile);
  763. }
  764. if (lg->GetGlobalGenerator()->GetName() == "Ninja"
  765. #if defined(_WIN32) && !defined(__CYGWIN__)
  766. || usePRE_BUILD
  767. #endif
  768. ) {
  769. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  770. RccListInputs(qtMajorVersion, sf, target, depends);
  771. #if defined(_WIN32) && !defined(__CYGWIN__)
  772. // Cannot use PRE_BUILD because the resource files themselves
  773. // may not be sources within the target so VS may not know the
  774. // target needs to re-build at all.
  775. usePRE_BUILD = false;
  776. #endif
  777. }
  778. }
  779. }
  780. }
  781. }
  782. #if defined(_WIN32) && !defined(__CYGWIN__)
  783. if (usePRE_BUILD) {
  784. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  785. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  786. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  787. std::vector<std::string> no_output;
  788. std::vector<std::string> no_byproducts;
  789. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  790. commandLines, autogenComment.c_str(),
  791. workingDirectory.c_str());
  792. cc.SetEscapeOldStyle(false);
  793. cc.SetEscapeAllowMakeVars(true);
  794. target->Target->AddPreBuildCommand(cc);
  795. } else
  796. #endif
  797. {
  798. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  799. autogenTargetName, true, workingDirectory.c_str(),
  800. /*byproducts=*/autogenOutputFiles, depends, commandLines, false,
  801. autogenComment.c_str());
  802. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  803. lg->AddGeneratorTarget(gt);
  804. // Set target folder
  805. const char* autogenFolder =
  806. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  807. if (!autogenFolder) {
  808. autogenFolder =
  809. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  810. }
  811. if (autogenFolder && *autogenFolder) {
  812. autogenTarget->SetProperty("FOLDER", autogenFolder);
  813. } else {
  814. // inherit FOLDER property from target (#13688)
  815. utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
  816. }
  817. target->Target->AddUtility(autogenTargetName);
  818. }
  819. }
  820. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  821. cmGeneratorTarget const* target)
  822. {
  823. cmMakefile* makefile = target->Target->GetMakefile();
  824. // forget the variables added here afterwards again:
  825. cmMakefile::ScopePushPop varScope(makefile);
  826. static_cast<void>(varScope);
  827. std::map<std::string, std::string> configMocIncludes;
  828. std::map<std::string, std::string> configMocDefines;
  829. std::map<std::string, std::string> configUicOptions;
  830. {
  831. // create a custom target for running generators at buildtime:
  832. const std::string autogenTargetName = GetAutogenTargetName(target);
  833. const std::string qtMajorVersion = GetQtMajorVersion(target);
  834. AddDefinitionEscaped(makefile, "_autogen_target_name", autogenTargetName);
  835. AddDefinitionEscaped(makefile, "_origin_target_name", target->GetName());
  836. AddDefinitionEscaped(makefile, "_qt_version_major", qtMajorVersion);
  837. std::vector<std::string> _sources;
  838. std::vector<std::string> _headers;
  839. std::vector<std::string> mocSkipList;
  840. std::vector<std::string> uicSkipList;
  841. if (target->GetPropertyAsBool("AUTOMOC") ||
  842. target->GetPropertyAsBool("AUTOUIC") ||
  843. target->GetPropertyAsBool("AUTORCC")) {
  844. SetupSourceFiles(target, _sources, _headers, mocSkipList, uicSkipList);
  845. }
  846. AddDefinitionEscaped(makefile, "_sources", _sources);
  847. AddDefinitionEscaped(makefile, "_headers", _headers);
  848. if (target->GetPropertyAsBool("AUTOMOC")) {
  849. MocSetupAutoTarget(target, autogenTargetName, qtMajorVersion,
  850. mocSkipList, configMocIncludes, configMocDefines);
  851. }
  852. if (target->GetPropertyAsBool("AUTOUIC")) {
  853. UicSetupAutoTarget(target, qtMajorVersion, uicSkipList,
  854. configUicOptions);
  855. }
  856. if (target->GetPropertyAsBool("AUTORCC")) {
  857. RccSetupAutoTarget(target, qtMajorVersion);
  858. }
  859. }
  860. // Generate config file
  861. std::string inputFile = cmSystemTools::GetCMakeRoot();
  862. inputFile += "/Modules/AutogenInfo.cmake.in";
  863. std::string outputFile = GetAutogenTargetFilesDir(target);
  864. outputFile += "/AutogenInfo.cmake";
  865. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  866. false);
  867. // Append custom config definitions to info file
  868. if (!configMocDefines.empty() || !configMocIncludes.empty() ||
  869. !configUicOptions.empty()) {
  870. // Ensure we have write permission in case .in was read-only.
  871. mode_t perm = 0;
  872. #if defined(_WIN32) && !defined(__CYGWIN__)
  873. mode_t mode_write = S_IWRITE;
  874. #else
  875. mode_t mode_write = S_IWUSR;
  876. #endif
  877. cmSystemTools::GetPermissions(outputFile, perm);
  878. if (!(perm & mode_write)) {
  879. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  880. }
  881. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  882. if (!infoFile) {
  883. std::string error = "Internal CMake error when trying to open file: ";
  884. error += outputFile;
  885. error += " for writing.";
  886. cmSystemTools::Error(error.c_str());
  887. } else {
  888. infoFile << "# Configuration specific options\n";
  889. if (!configMocDefines.empty()) {
  890. for (std::map<std::string, std::string>::iterator
  891. it = configMocDefines.begin(),
  892. end = configMocDefines.end();
  893. it != end; ++it) {
  894. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first << " "
  895. << it->second << ")\n";
  896. }
  897. }
  898. if (!configMocIncludes.empty()) {
  899. for (std::map<std::string, std::string>::iterator
  900. it = configMocIncludes.begin(),
  901. end = configMocIncludes.end();
  902. it != end; ++it) {
  903. infoFile << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
  904. << ")\n";
  905. }
  906. }
  907. if (!configUicOptions.empty()) {
  908. for (std::map<std::string, std::string>::iterator
  909. it = configUicOptions.begin(),
  910. end = configUicOptions.end();
  911. it != end; ++it) {
  912. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " "
  913. << it->second << ")\n";
  914. }
  915. }
  916. }
  917. }
  918. }