cmQtAutoGeneratorInitializer.cxx 32 KB

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