cmQtAutoGeneratorInitializer.cxx 34 KB

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