cmQtAutoGeneratorInitializer.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 http://public.kitware.com/Bug/view.php?id=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. } else if (strcmp(qtVersion, "4") == 0) {
  305. cmGeneratorTarget* qt4Rcc = lg->FindGeneratorTargetToUse("Qt4::rcc");
  306. if (!qt4Rcc) {
  307. cmSystemTools::Error("Qt4::rcc target not found ", targetName.c_str());
  308. return std::string();
  309. }
  310. return qt4Rcc->ImportedGetLocation("");
  311. }
  312. cmSystemTools::Error("The CMAKE_AUTORCC feature supports only Qt 4 and "
  313. "Qt 5 ",
  314. targetName.c_str());
  315. return std::string();
  316. }
  317. static void MergeRccOptions(std::vector<std::string>& opts,
  318. const std::vector<std::string>& fileOpts,
  319. bool isQt5)
  320. {
  321. static const char* valueOptions[] = { "name", "root", "compress",
  322. "threshold" };
  323. std::vector<std::string> extraOpts;
  324. for (std::vector<std::string>::const_iterator it = fileOpts.begin();
  325. it != fileOpts.end(); ++it) {
  326. std::vector<std::string>::iterator existingIt =
  327. std::find(opts.begin(), opts.end(), *it);
  328. if (existingIt != opts.end()) {
  329. const char* o = it->c_str();
  330. if (*o == '-') {
  331. ++o;
  332. }
  333. if (isQt5 && *o == '-') {
  334. ++o;
  335. }
  336. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  337. cmStrCmp(*it)) != cmArrayEnd(valueOptions)) {
  338. assert(existingIt + 1 != opts.end());
  339. *(existingIt + 1) = *(it + 1);
  340. ++it;
  341. }
  342. } else {
  343. extraOpts.push_back(*it);
  344. }
  345. }
  346. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  347. }
  348. static void copyTargetProperty(cmTarget* destinationTarget,
  349. cmTarget* sourceTarget,
  350. const std::string& propertyName)
  351. {
  352. const char* propertyValue = sourceTarget->GetProperty(propertyName);
  353. if (propertyValue) {
  354. destinationTarget->SetProperty(propertyName, propertyValue);
  355. }
  356. }
  357. static std::string cmQtAutoGeneratorsStripCR(std::string const& line)
  358. {
  359. // Strip CR characters rcc may have printed (possibly more than one!).
  360. std::string::size_type cr = line.find('\r');
  361. if (cr != line.npos) {
  362. return line.substr(0, cr);
  363. }
  364. return line;
  365. }
  366. static std::string ReadAll(const std::string& filename)
  367. {
  368. cmsys::ifstream file(filename.c_str());
  369. std::ostringstream stream;
  370. stream << file.rdbuf();
  371. file.close();
  372. return stream.str();
  373. }
  374. static std::string ListQt5RccInputs(cmSourceFile* sf,
  375. cmGeneratorTarget const* target,
  376. std::vector<std::string>& depends)
  377. {
  378. std::string rccCommand = GetRccExecutable(target);
  379. bool hasDashDashList = false;
  380. {
  381. std::vector<std::string> command;
  382. command.push_back(rccCommand);
  383. command.push_back("--help");
  384. std::string rccStdOut;
  385. std::string rccStdErr;
  386. int retVal = 0;
  387. bool result =
  388. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  389. CM_NULLPTR, cmSystemTools::OUTPUT_NONE);
  390. if (result && retVal == 0 &&
  391. rccStdOut.find("--list") != std::string::npos) {
  392. hasDashDashList = true;
  393. }
  394. }
  395. std::vector<std::string> qrcEntries;
  396. std::vector<std::string> command;
  397. command.push_back(rccCommand);
  398. command.push_back(hasDashDashList ? "--list" : "-list");
  399. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  400. command.push_back(absFile);
  401. std::string rccStdOut;
  402. std::string rccStdErr;
  403. int retVal = 0;
  404. bool result =
  405. cmSystemTools::RunSingleCommand(command, &rccStdOut, &rccStdErr, &retVal,
  406. CM_NULLPTR, cmSystemTools::OUTPUT_NONE);
  407. if (!result || retVal) {
  408. std::ostringstream err;
  409. err << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  410. << " failed:\n"
  411. << rccStdOut << "\n"
  412. << rccStdErr << std::endl;
  413. std::cerr << err.str();
  414. return std::string();
  415. }
  416. {
  417. std::istringstream ostr(rccStdOut);
  418. std::string oline;
  419. while (std::getline(ostr, oline)) {
  420. oline = cmQtAutoGeneratorsStripCR(oline);
  421. if (!oline.empty()) {
  422. qrcEntries.push_back(oline);
  423. }
  424. }
  425. }
  426. {
  427. std::istringstream estr(rccStdErr);
  428. std::string eline;
  429. while (std::getline(estr, eline)) {
  430. eline = cmQtAutoGeneratorsStripCR(eline);
  431. if (cmHasLiteralPrefix(eline, "RCC: Error in")) {
  432. static std::string searchString = "Cannot find file '";
  433. std::string::size_type pos = eline.find(searchString);
  434. if (pos == std::string::npos) {
  435. std::ostringstream err;
  436. err << "AUTOGEN: error: Rcc lists unparsable output " << eline
  437. << std::endl;
  438. std::cerr << err.str();
  439. return std::string();
  440. }
  441. pos += searchString.length();
  442. std::string::size_type sz = eline.size() - pos - 1;
  443. qrcEntries.push_back(eline.substr(pos, sz));
  444. }
  445. }
  446. }
  447. depends.insert(depends.end(), qrcEntries.begin(), qrcEntries.end());
  448. return cmJoin(qrcEntries, "@list_sep@");
  449. }
  450. static std::string ListQt4RccInputs(cmSourceFile* sf,
  451. std::vector<std::string>& depends)
  452. {
  453. const std::string qrcContents = ReadAll(sf->GetFullPath());
  454. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  455. std::string entriesList;
  456. const char* sep = "";
  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. entriesList += sep;
  469. entriesList += qrcEntry;
  470. sep = "@list_sep@";
  471. depends.push_back(qrcEntry);
  472. }
  473. return entriesList;
  474. }
  475. static void SetupAutoRccTarget(cmGeneratorTarget const* target)
  476. {
  477. std::string _rcc_files;
  478. const char* sepRccFiles = "";
  479. cmMakefile* makefile = target->Target->GetMakefile();
  480. std::vector<cmSourceFile*> srcFiles;
  481. target->GetConfigCommonSourceFiles(srcFiles);
  482. std::string qrcInputs;
  483. const char* qrcInputsSep = "";
  484. std::string rccFileFiles;
  485. std::string rccFileOptions;
  486. const char* optionSep = "";
  487. const char* qtVersion = makefile->GetDefinition("_target_qt_version");
  488. std::vector<std::string> rccOptions;
  489. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS")) {
  490. cmSystemTools::ExpandListArgument(opts, rccOptions);
  491. }
  492. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  493. if (qtMajorVersion == "") {
  494. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  495. }
  496. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  497. fileIt != srcFiles.end(); ++fileIt) {
  498. cmSourceFile* sf = *fileIt;
  499. std::string ext = sf->GetExtension();
  500. if (ext == "qrc") {
  501. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  502. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  503. if (!skip) {
  504. _rcc_files += sepRccFiles;
  505. _rcc_files += absFile;
  506. sepRccFiles = ";";
  507. if (const char* prop = sf->GetProperty("AUTORCC_OPTIONS")) {
  508. std::vector<std::string> optsVec;
  509. cmSystemTools::ExpandListArgument(prop, optsVec);
  510. MergeRccOptions(rccOptions, optsVec, strcmp(qtVersion, "5") == 0);
  511. }
  512. if (!rccOptions.empty()) {
  513. rccFileFiles += optionSep;
  514. rccFileFiles += absFile;
  515. rccFileOptions += optionSep;
  516. }
  517. const char* listSep = "";
  518. for (std::vector<std::string>::const_iterator it = rccOptions.begin();
  519. it != rccOptions.end(); ++it) {
  520. rccFileOptions += listSep;
  521. rccFileOptions += *it;
  522. listSep = "@list_sep@";
  523. }
  524. optionSep = ";";
  525. std::vector<std::string> depends;
  526. std::string entriesList;
  527. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  528. if (qtMajorVersion == "5") {
  529. entriesList = ListQt5RccInputs(sf, target, depends);
  530. } else {
  531. entriesList = ListQt4RccInputs(sf, depends);
  532. }
  533. if (entriesList.empty()) {
  534. return;
  535. }
  536. }
  537. qrcInputs += qrcInputsSep;
  538. qrcInputs += entriesList;
  539. qrcInputsSep = ";";
  540. }
  541. }
  542. }
  543. makefile->AddDefinition(
  544. "_qt_rcc_inputs_" + target->GetName(),
  545. cmOutputConverter::EscapeForCMake(qrcInputs).c_str());
  546. makefile->AddDefinition(
  547. "_rcc_files", cmOutputConverter::EscapeForCMake(_rcc_files).c_str());
  548. makefile->AddDefinition(
  549. "_qt_rcc_options_files",
  550. cmOutputConverter::EscapeForCMake(rccFileFiles).c_str());
  551. makefile->AddDefinition(
  552. "_qt_rcc_options_options",
  553. cmOutputConverter::EscapeForCMake(rccFileOptions).c_str());
  554. makefile->AddDefinition("_qt_rcc_executable",
  555. GetRccExecutable(target).c_str());
  556. }
  557. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  558. cmGeneratorTarget* target)
  559. {
  560. cmMakefile* makefile = target->Target->GetMakefile();
  561. if (target->GetPropertyAsBool("AUTOMOC")) {
  562. std::string automocTargetName = GetAutogenTargetName(target);
  563. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  564. mocCppFile += "/";
  565. mocCppFile += automocTargetName;
  566. mocCppFile += ".cpp";
  567. makefile->GetOrCreateSource(mocCppFile, true);
  568. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", mocCppFile.c_str(),
  569. false);
  570. target->AddSource(mocCppFile);
  571. }
  572. }
  573. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  574. cmLocalGenerator* lg, cmGeneratorTarget* target)
  575. {
  576. cmMakefile* makefile = target->Target->GetMakefile();
  577. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  578. if (qtMajorVersion == "") {
  579. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  580. }
  581. // create a custom target for running generators at buildtime:
  582. std::string autogenTargetName = GetAutogenTargetName(target);
  583. std::string targetDir = GetAutogenTargetDir(target);
  584. cmCustomCommandLine currentLine;
  585. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  586. currentLine.push_back("-E");
  587. currentLine.push_back("cmake_autogen");
  588. currentLine.push_back(targetDir);
  589. currentLine.push_back("$<CONFIGURATION>");
  590. cmCustomCommandLines commandLines;
  591. commandLines.push_back(currentLine);
  592. std::string workingDirectory =
  593. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  594. std::vector<std::string> depends;
  595. if (const char* autogenDepends =
  596. target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  597. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  598. }
  599. std::vector<std::string> toolNames;
  600. if (target->GetPropertyAsBool("AUTOMOC")) {
  601. toolNames.push_back("moc");
  602. }
  603. if (target->GetPropertyAsBool("AUTOUIC")) {
  604. toolNames.push_back("uic");
  605. }
  606. if (target->GetPropertyAsBool("AUTORCC")) {
  607. toolNames.push_back("rcc");
  608. }
  609. std::string tools = toolNames[0];
  610. toolNames.erase(toolNames.begin());
  611. while (toolNames.size() > 1) {
  612. tools += ", " + toolNames[0];
  613. toolNames.erase(toolNames.begin());
  614. }
  615. if (toolNames.size() == 1) {
  616. tools += " and " + toolNames[0];
  617. }
  618. std::string autogenComment = "Automatic " + tools + " for target ";
  619. autogenComment += target->GetName();
  620. #if defined(_WIN32) && !defined(__CYGWIN__)
  621. bool usePRE_BUILD = false;
  622. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  623. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  624. cmGlobalVisualStudioGenerator* vsgg =
  625. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  626. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  627. // reduce the number of targets loaded into the IDE.
  628. // This also works around a VS 11 bug that may skip updating the target:
  629. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  630. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  631. if (usePRE_BUILD) {
  632. for (std::vector<std::string>::iterator it = depends.begin();
  633. it != depends.end(); ++it) {
  634. if (!makefile->FindTargetToUse(it->c_str())) {
  635. usePRE_BUILD = false;
  636. break;
  637. }
  638. }
  639. }
  640. }
  641. #endif
  642. std::vector<std::string> rcc_output;
  643. bool const isNinja = lg->GetGlobalGenerator()->GetName() == "Ninja";
  644. if (isNinja
  645. #if defined(_WIN32) && !defined(__CYGWIN__)
  646. || usePRE_BUILD
  647. #endif
  648. ) {
  649. std::vector<cmSourceFile*> srcFiles;
  650. target->GetConfigCommonSourceFiles(srcFiles);
  651. cmFilePathUuid fpathUuid(makefile);
  652. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  653. fileIt != srcFiles.end(); ++fileIt) {
  654. cmSourceFile* sf = *fileIt;
  655. std::string absFile = cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  656. std::string ext = sf->GetExtension();
  657. if (target->GetPropertyAsBool("AUTORCC")) {
  658. if (ext == "qrc" &&
  659. !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"))) {
  660. {
  661. std::string rcc_output_file = GetAutogenTargetBuildDir(target);
  662. // Create output directory
  663. cmSystemTools::MakeDirectory(rcc_output_file.c_str());
  664. rcc_output_file += fpathUuid.get(absFile, "qrc_", ".cpp");
  665. rcc_output.push_back(rcc_output_file);
  666. }
  667. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"))) {
  668. if (qtMajorVersion == "5") {
  669. ListQt5RccInputs(sf, target, depends);
  670. } else {
  671. ListQt4RccInputs(sf, depends);
  672. }
  673. #if defined(_WIN32) && !defined(__CYGWIN__)
  674. // Cannot use PRE_BUILD because the resource files themselves
  675. // may not be sources within the target so VS may not know the
  676. // target needs to re-build at all.
  677. usePRE_BUILD = false;
  678. #endif
  679. }
  680. }
  681. }
  682. }
  683. }
  684. #if defined(_WIN32) && !defined(__CYGWIN__)
  685. if (usePRE_BUILD) {
  686. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  687. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  688. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  689. std::vector<std::string> no_output;
  690. std::vector<std::string> no_byproducts;
  691. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  692. commandLines, autogenComment.c_str(),
  693. workingDirectory.c_str());
  694. cc.SetEscapeOldStyle(false);
  695. cc.SetEscapeAllowMakeVars(true);
  696. target->Target->AddPreBuildCommand(cc);
  697. } else
  698. #endif
  699. {
  700. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  701. autogenTargetName, true, workingDirectory.c_str(),
  702. /*byproducts=*/rcc_output, depends, commandLines, false,
  703. autogenComment.c_str());
  704. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  705. lg->AddGeneratorTarget(gt);
  706. // Set target folder
  707. const char* autogenFolder =
  708. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  709. if (!autogenFolder) {
  710. autogenFolder =
  711. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  712. }
  713. if (autogenFolder && *autogenFolder) {
  714. autogenTarget->SetProperty("FOLDER", autogenFolder);
  715. } else {
  716. // inherit FOLDER property from target (#13688)
  717. copyTargetProperty(gt->Target, target->Target, "FOLDER");
  718. }
  719. target->Target->AddUtility(autogenTargetName);
  720. }
  721. }
  722. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  723. cmGeneratorTarget const* target)
  724. {
  725. cmMakefile* makefile = target->Target->GetMakefile();
  726. // forget the variables added here afterwards again:
  727. cmMakefile::ScopePushPop varScope(makefile);
  728. static_cast<void>(varScope);
  729. // create a custom target for running generators at buildtime:
  730. std::string autogenTargetName = GetAutogenTargetName(target);
  731. makefile->AddDefinition(
  732. "_moc_target_name",
  733. cmOutputConverter::EscapeForCMake(autogenTargetName).c_str());
  734. makefile->AddDefinition(
  735. "_origin_target_name",
  736. cmOutputConverter::EscapeForCMake(target->GetName()).c_str());
  737. std::string targetDir = GetAutogenTargetDir(target);
  738. const char* qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  739. if (!qtVersion) {
  740. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  741. }
  742. if (const char* targetQtVersion =
  743. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  744. "")) {
  745. qtVersion = targetQtVersion;
  746. }
  747. if (qtVersion) {
  748. makefile->AddDefinition("_target_qt_version", qtVersion);
  749. }
  750. std::vector<std::string> skipUic;
  751. std::vector<std::string> skipMoc;
  752. std::vector<std::string> mocSources;
  753. std::vector<std::string> mocHeaders;
  754. std::map<std::string, std::string> configIncludes;
  755. std::map<std::string, std::string> configDefines;
  756. std::map<std::string, std::string> configUicOptions;
  757. if (target->GetPropertyAsBool("AUTOMOC") ||
  758. target->GetPropertyAsBool("AUTOUIC") ||
  759. target->GetPropertyAsBool("AUTORCC")) {
  760. SetupSourceFiles(target, skipMoc, mocSources, mocHeaders, skipUic);
  761. }
  762. makefile->AddDefinition(
  763. "_cpp_files",
  764. cmOutputConverter::EscapeForCMake(cmJoin(mocSources, ";")).c_str());
  765. if (target->GetPropertyAsBool("AUTOMOC")) {
  766. SetupAutoMocTarget(target, autogenTargetName, skipMoc, mocHeaders,
  767. configIncludes, configDefines);
  768. }
  769. if (target->GetPropertyAsBool("AUTOUIC")) {
  770. SetupAutoUicTarget(target, skipUic, configUicOptions);
  771. }
  772. if (target->GetPropertyAsBool("AUTORCC")) {
  773. SetupAutoRccTarget(target);
  774. }
  775. std::string inputFile = cmSystemTools::GetCMakeRoot();
  776. inputFile += "/Modules/AutogenInfo.cmake.in";
  777. std::string outputFile = targetDir;
  778. outputFile += "/AutogenInfo.cmake";
  779. makefile->AddDefinition(
  780. "_qt_rcc_inputs",
  781. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  782. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  783. false);
  784. // Ensure we have write permission in case .in was read-only.
  785. mode_t perm = 0;
  786. #if defined(_WIN32) && !defined(__CYGWIN__)
  787. mode_t mode_write = S_IWRITE;
  788. #else
  789. mode_t mode_write = S_IWUSR;
  790. #endif
  791. cmSystemTools::GetPermissions(outputFile, perm);
  792. if (!(perm & mode_write)) {
  793. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  794. }
  795. if (!configDefines.empty() || !configIncludes.empty() ||
  796. !configUicOptions.empty()) {
  797. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  798. if (!infoFile) {
  799. std::string error = "Internal CMake error when trying to open file: ";
  800. error += outputFile;
  801. error += " for writing.";
  802. cmSystemTools::Error(error.c_str());
  803. return;
  804. }
  805. if (!configDefines.empty()) {
  806. for (std::map<std::string, std::string>::iterator
  807. it = configDefines.begin(),
  808. end = configDefines.end();
  809. it != end; ++it) {
  810. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first << " "
  811. << it->second << ")\n";
  812. }
  813. }
  814. if (!configIncludes.empty()) {
  815. for (std::map<std::string, std::string>::iterator
  816. it = configIncludes.begin(),
  817. end = configIncludes.end();
  818. it != end; ++it) {
  819. infoFile << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
  820. << ")\n";
  821. }
  822. }
  823. if (!configUicOptions.empty()) {
  824. for (std::map<std::string, std::string>::iterator
  825. it = configUicOptions.begin(),
  826. end = configUicOptions.end();
  827. it != end; ++it) {
  828. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " "
  829. << it->second << ")\n";
  830. }
  831. }
  832. }
  833. }