cmQtAutoGeneratorInitializer.cxx 33 KB

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