cmQtAutoGeneratorInitializer.cxx 33 KB

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