1
0

cmQtAutoGeneratorInitializer.cxx 32 KB

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