cmQtAutoGeneratorInitializer.cxx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. // Set target folder
  815. const char* autogenFolder = makefile->GetState()
  816. ->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  817. if (!autogenFolder)
  818. {
  819. autogenFolder = makefile->GetState()
  820. ->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  821. }
  822. if (autogenFolder && *autogenFolder)
  823. {
  824. autogenTarget->SetProperty("FOLDER", autogenFolder);
  825. }
  826. else
  827. {
  828. // inherit FOLDER property from target (#13688)
  829. copyTargetProperty(gt->Target, target->Target, "FOLDER");
  830. }
  831. target->Target->AddUtility(autogenTargetName);
  832. }
  833. }
  834. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  835. cmGeneratorTarget const* target)
  836. {
  837. cmMakefile* makefile = target->Target->GetMakefile();
  838. // forget the variables added here afterwards again:
  839. cmMakefile::ScopePushPop varScope(makefile);
  840. static_cast<void>(varScope);
  841. // create a custom target for running generators at buildtime:
  842. std::string autogenTargetName = GetAutogenTargetName(target);
  843. makefile->AddDefinition("_moc_target_name",
  844. cmOutputConverter::EscapeForCMake(autogenTargetName).c_str());
  845. makefile->AddDefinition("_origin_target_name",
  846. cmOutputConverter::EscapeForCMake(target->GetName()).c_str());
  847. std::string targetDir = GetAutogenTargetDir(target);
  848. const char *qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  849. if (!qtVersion)
  850. {
  851. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  852. }
  853. if (const char *targetQtVersion =
  854. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""))
  855. {
  856. qtVersion = targetQtVersion;
  857. }
  858. if (qtVersion)
  859. {
  860. makefile->AddDefinition("_target_qt_version", qtVersion);
  861. }
  862. std::vector<std::string> skipUic;
  863. std::vector<std::string> skipMoc;
  864. std::vector<std::string> mocSources;
  865. std::vector<std::string> mocHeaders;
  866. std::map<std::string, std::string> configIncludes;
  867. std::map<std::string, std::string> configDefines;
  868. std::map<std::string, std::string> configUicOptions;
  869. if (target->GetPropertyAsBool("AUTOMOC")
  870. || target->GetPropertyAsBool("AUTOUIC")
  871. || target->GetPropertyAsBool("AUTORCC"))
  872. {
  873. SetupSourceFiles(target, skipMoc, mocSources, mocHeaders, skipUic);
  874. }
  875. makefile->AddDefinition("_cpp_files",
  876. cmOutputConverter::EscapeForCMake(cmJoin(mocSources, ";")).c_str());
  877. if (target->GetPropertyAsBool("AUTOMOC"))
  878. {
  879. SetupAutoMocTarget(target, autogenTargetName,
  880. skipMoc, mocHeaders,
  881. configIncludes, configDefines);
  882. }
  883. if (target->GetPropertyAsBool("AUTOUIC"))
  884. {
  885. SetupAutoUicTarget(target, skipUic, configUicOptions);
  886. }
  887. if (target->GetPropertyAsBool("AUTORCC"))
  888. {
  889. SetupAutoRccTarget(target);
  890. }
  891. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  892. std::string inputFile = cmakeRoot;
  893. inputFile += "/Modules/AutogenInfo.cmake.in";
  894. std::string outputFile = targetDir;
  895. outputFile += "/AutogenInfo.cmake";
  896. makefile->AddDefinition("_qt_rcc_inputs",
  897. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  898. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  899. false, true, false);
  900. // Ensure we have write permission in case .in was read-only.
  901. mode_t perm = 0;
  902. #if defined(_WIN32) && !defined(__CYGWIN__)
  903. mode_t mode_write = S_IWRITE;
  904. #else
  905. mode_t mode_write = S_IWUSR;
  906. #endif
  907. cmSystemTools::GetPermissions(outputFile, perm);
  908. if (!(perm & mode_write))
  909. {
  910. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  911. }
  912. if (!configDefines.empty()
  913. || !configIncludes.empty()
  914. || !configUicOptions.empty())
  915. {
  916. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  917. if ( !infoFile )
  918. {
  919. std::string error = "Internal CMake error when trying to open file: ";
  920. error += outputFile.c_str();
  921. error += " for writing.";
  922. cmSystemTools::Error(error.c_str());
  923. return;
  924. }
  925. if (!configDefines.empty())
  926. {
  927. for (std::map<std::string, std::string>::iterator
  928. it = configDefines.begin(), end = configDefines.end();
  929. it != end; ++it)
  930. {
  931. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first <<
  932. " " << it->second << ")\n";
  933. }
  934. }
  935. if (!configIncludes.empty())
  936. {
  937. for (std::map<std::string, std::string>::iterator
  938. it = configIncludes.begin(), end = configIncludes.end();
  939. it != end; ++it)
  940. {
  941. infoFile << "set(AM_MOC_INCLUDES_" << it->first <<
  942. " " << it->second << ")\n";
  943. }
  944. }
  945. if (!configUicOptions.empty())
  946. {
  947. for (std::map<std::string, std::string>::iterator
  948. it = configUicOptions.begin(), end = configUicOptions.end();
  949. it != end; ++it)
  950. {
  951. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first <<
  952. " " << it->second << ")\n";
  953. }
  954. }
  955. }
  956. }