cmQtAutoGeneratorInitializer.cxx 33 KB

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