cmQtAutoGeneratorInitializer.cxx 33 KB

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