cmQtAutoGeneratorInitializer.cxx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. std::vector<std::string> qrcEntries;
  444. std::vector<std::string> command;
  445. command.push_back(rccCommand);
  446. command.push_back("-list");
  447. std::string absFile = cmsys::SystemTools::GetRealPath(
  448. sf->GetFullPath());
  449. command.push_back(absFile);
  450. std::string rccStdOut;
  451. std::string rccStdErr;
  452. int retVal = 0;
  453. bool result = cmSystemTools::RunSingleCommand(
  454. command, &rccStdOut, &rccStdErr,
  455. &retVal, 0, cmSystemTools::OUTPUT_NONE);
  456. if (!result || retVal)
  457. {
  458. std::cerr << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  459. << " failed:\n" << rccStdOut << "\n" << rccStdErr << std::endl;
  460. return std::string();
  461. }
  462. {
  463. std::istringstream ostr(rccStdOut);
  464. std::string oline;
  465. while(std::getline(ostr, oline))
  466. {
  467. oline = cmQtAutoGeneratorsStripCR(oline);
  468. if(!oline.empty())
  469. {
  470. qrcEntries.push_back(oline);
  471. }
  472. }
  473. }
  474. {
  475. std::istringstream estr(rccStdErr);
  476. std::string eline;
  477. while(std::getline(estr, eline))
  478. {
  479. eline = cmQtAutoGeneratorsStripCR(eline);
  480. if (cmHasLiteralPrefix(eline, "RCC: Error in"))
  481. {
  482. static std::string searchString = "Cannot find file '";
  483. std::string::size_type pos = eline.find(searchString);
  484. if (pos == std::string::npos)
  485. {
  486. std::cerr << "AUTOGEN: error: Rcc lists unparsable output "
  487. << eline << std::endl;
  488. return std::string();
  489. }
  490. pos += searchString.length();
  491. std::string::size_type sz = eline.size() - pos - 1;
  492. qrcEntries.push_back(eline.substr(pos, sz));
  493. }
  494. }
  495. }
  496. depends.insert(depends.end(), qrcEntries.begin(), qrcEntries.end());
  497. return cmJoin(qrcEntries, "@list_sep@");
  498. }
  499. static std::string ListQt4RccInputs(cmSourceFile* sf,
  500. std::vector<std::string>& depends)
  501. {
  502. const std::string qrcContents = ReadAll(sf->GetFullPath());
  503. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  504. std::string entriesList;
  505. const char* sep = "";
  506. size_t offset = 0;
  507. while (fileMatchRegex.find(qrcContents.c_str() + offset))
  508. {
  509. std::string qrcEntry = fileMatchRegex.match(1);
  510. offset += qrcEntry.size();
  511. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  512. fileReplaceRegex.find(qrcEntry);
  513. std::string tag = fileReplaceRegex.match(1);
  514. qrcEntry = qrcEntry.substr(tag.size());
  515. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str()))
  516. {
  517. qrcEntry = sf->GetLocation().GetDirectory() + "/" + qrcEntry;
  518. }
  519. entriesList += sep;
  520. entriesList += qrcEntry;
  521. sep = "@list_sep@";
  522. depends.push_back(qrcEntry);
  523. }
  524. return entriesList;
  525. }
  526. static void SetupAutoRccTarget(cmGeneratorTarget const* target)
  527. {
  528. std::string _rcc_files;
  529. const char* sepRccFiles = "";
  530. cmMakefile *makefile = target->Target->GetMakefile();
  531. std::vector<cmSourceFile*> srcFiles;
  532. target->GetConfigCommonSourceFiles(srcFiles);
  533. std::string qrcInputs;
  534. const char* qrcInputsSep = "";
  535. std::string rccFileFiles;
  536. std::string rccFileOptions;
  537. const char *optionSep = "";
  538. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  539. std::vector<std::string> rccOptions;
  540. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS"))
  541. {
  542. cmSystemTools::ExpandListArgument(opts, rccOptions);
  543. }
  544. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  545. if (qtMajorVersion == "")
  546. {
  547. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  548. }
  549. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  550. fileIt != srcFiles.end();
  551. ++fileIt)
  552. {
  553. cmSourceFile* sf = *fileIt;
  554. std::string ext = sf->GetExtension();
  555. if (ext == "qrc")
  556. {
  557. std::string absFile = cmsys::SystemTools::GetRealPath(
  558. sf->GetFullPath());
  559. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  560. if (!skip)
  561. {
  562. _rcc_files += sepRccFiles;
  563. _rcc_files += absFile;
  564. sepRccFiles = ";";
  565. if (const char *prop = sf->GetProperty("AUTORCC_OPTIONS"))
  566. {
  567. std::vector<std::string> optsVec;
  568. cmSystemTools::ExpandListArgument(prop, optsVec);
  569. MergeRccOptions(rccOptions, optsVec,
  570. strcmp(qtVersion, "5") == 0);
  571. }
  572. if (!rccOptions.empty())
  573. {
  574. rccFileFiles += optionSep;
  575. rccFileFiles += absFile;
  576. rccFileOptions += optionSep;
  577. }
  578. const char *listSep = "";
  579. for(std::vector<std::string>::const_iterator it = rccOptions.begin();
  580. it != rccOptions.end();
  581. ++it)
  582. {
  583. rccFileOptions += listSep;
  584. rccFileOptions += *it;
  585. listSep = "@list_sep@";
  586. }
  587. optionSep = ";";
  588. std::vector<std::string> depends;
  589. std::string entriesList;
  590. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  591. {
  592. if (qtMajorVersion == "5")
  593. {
  594. entriesList = ListQt5RccInputs(sf, target, depends);
  595. }
  596. else
  597. {
  598. entriesList = ListQt4RccInputs(sf, depends);
  599. }
  600. if (entriesList.empty())
  601. {
  602. return;
  603. }
  604. }
  605. qrcInputs += qrcInputsSep;
  606. qrcInputs += entriesList;
  607. qrcInputsSep = ";";
  608. }
  609. }
  610. }
  611. makefile->AddDefinition("_qt_rcc_inputs_" + target->GetName(),
  612. cmOutputConverter::EscapeForCMake(qrcInputs).c_str());
  613. makefile->AddDefinition("_rcc_files",
  614. cmOutputConverter::EscapeForCMake(_rcc_files).c_str());
  615. makefile->AddDefinition("_qt_rcc_options_files",
  616. cmOutputConverter::EscapeForCMake(rccFileFiles).c_str());
  617. makefile->AddDefinition("_qt_rcc_options_options",
  618. cmOutputConverter::EscapeForCMake(rccFileOptions).c_str());
  619. makefile->AddDefinition("_qt_rcc_executable",
  620. GetRccExecutable(target).c_str());
  621. }
  622. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  623. cmGeneratorTarget* target)
  624. {
  625. cmMakefile* makefile = target->Target->GetMakefile();
  626. if (target->GetPropertyAsBool("AUTOMOC"))
  627. {
  628. std::string automocTargetName = GetAutogenTargetName(target);
  629. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  630. mocCppFile += "/";
  631. mocCppFile += automocTargetName;
  632. mocCppFile += ".cpp";
  633. makefile->GetOrCreateSource(mocCppFile, true);
  634. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  635. mocCppFile.c_str(), false);
  636. target->AddSource(mocCppFile);
  637. }
  638. }
  639. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  640. cmLocalGenerator* lg,
  641. cmGeneratorTarget* target)
  642. {
  643. cmMakefile* makefile = target->Target->GetMakefile();
  644. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  645. if (qtMajorVersion == "")
  646. {
  647. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  648. }
  649. // create a custom target for running generators at buildtime:
  650. std::string autogenTargetName = GetAutogenTargetName(target);
  651. std::string targetDir = GetAutogenTargetDir(target);
  652. cmCustomCommandLine currentLine;
  653. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  654. currentLine.push_back("-E");
  655. currentLine.push_back("cmake_autogen");
  656. currentLine.push_back(targetDir);
  657. currentLine.push_back("$<CONFIGURATION>");
  658. cmCustomCommandLines commandLines;
  659. commandLines.push_back(currentLine);
  660. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  661. "", makefile->GetCurrentBinaryDirectory());
  662. std::vector<std::string> depends;
  663. if (const char *autogenDepends =
  664. target->GetProperty("AUTOGEN_TARGET_DEPENDS"))
  665. {
  666. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  667. }
  668. std::vector<std::string> toolNames;
  669. if (target->GetPropertyAsBool("AUTOMOC"))
  670. {
  671. toolNames.push_back("moc");
  672. }
  673. if (target->GetPropertyAsBool("AUTOUIC"))
  674. {
  675. toolNames.push_back("uic");
  676. }
  677. if (target->GetPropertyAsBool("AUTORCC"))
  678. {
  679. toolNames.push_back("rcc");
  680. }
  681. std::string tools = toolNames[0];
  682. toolNames.erase(toolNames.begin());
  683. while (toolNames.size() > 1)
  684. {
  685. tools += ", " + toolNames[0];
  686. toolNames.erase(toolNames.begin());
  687. }
  688. if (toolNames.size() == 1)
  689. {
  690. tools += " and " + toolNames[0];
  691. }
  692. std::string autogenComment = "Automatic " + tools + " for target ";
  693. autogenComment += target->GetName();
  694. #if defined(_WIN32) && !defined(__CYGWIN__)
  695. bool usePRE_BUILD = false;
  696. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  697. if(gg->GetName().find("Visual Studio") != std::string::npos)
  698. {
  699. cmGlobalVisualStudioGenerator* vsgg =
  700. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  701. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  702. // reduce the number of targets loaded into the IDE.
  703. // This also works around a VS 11 bug that may skip updating the target:
  704. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  705. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  706. if(usePRE_BUILD)
  707. {
  708. for (std::vector<std::string>::iterator it = depends.begin();
  709. it != depends.end(); ++it)
  710. {
  711. if(!makefile->FindTargetToUse(it->c_str()))
  712. {
  713. usePRE_BUILD = false;
  714. break;
  715. }
  716. }
  717. }
  718. }
  719. #endif
  720. std::vector<std::string> rcc_output;
  721. bool const isNinja =
  722. lg->GetGlobalGenerator()->GetName() == "Ninja";
  723. if(isNinja
  724. #if defined(_WIN32) && !defined(__CYGWIN__)
  725. || usePRE_BUILD
  726. #endif
  727. )
  728. {
  729. std::vector<cmSourceFile*> srcFiles;
  730. target->GetConfigCommonSourceFiles(srcFiles);
  731. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  732. fileIt != srcFiles.end();
  733. ++fileIt)
  734. {
  735. cmSourceFile* sf = *fileIt;
  736. std::string absFile = cmsys::SystemTools::GetRealPath(
  737. sf->GetFullPath());
  738. std::string ext = sf->GetExtension();
  739. if (target->GetPropertyAsBool("AUTORCC"))
  740. {
  741. if (ext == "qrc"
  742. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  743. {
  744. std::string basename = cmsys::SystemTools::
  745. GetFilenameWithoutLastExtension(absFile);
  746. std::string rcc_output_dir = target->GetSupportDirectory();
  747. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  748. std::string rcc_output_file = rcc_output_dir;
  749. rcc_output_file += "/qrc_" + basename + ".cpp";
  750. rcc_output.push_back(rcc_output_file);
  751. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  752. {
  753. if (qtMajorVersion == "5")
  754. {
  755. ListQt5RccInputs(sf, target, depends);
  756. }
  757. else
  758. {
  759. ListQt4RccInputs(sf, depends);
  760. }
  761. #if defined(_WIN32) && !defined(__CYGWIN__)
  762. // Cannot use PRE_BUILD because the resource files themselves
  763. // may not be sources within the target so VS may not know the
  764. // target needs to re-build at all.
  765. usePRE_BUILD = false;
  766. #endif
  767. }
  768. }
  769. }
  770. }
  771. }
  772. #if defined(_WIN32) && !defined(__CYGWIN__)
  773. if(usePRE_BUILD)
  774. {
  775. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  776. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  777. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  778. std::vector<std::string> no_output;
  779. std::vector<std::string> no_byproducts;
  780. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  781. commandLines, autogenComment.c_str(),
  782. workingDirectory.c_str());
  783. cc.SetEscapeOldStyle(false);
  784. cc.SetEscapeAllowMakeVars(true);
  785. target->Target->AddPreBuildCommand(cc);
  786. }
  787. else
  788. #endif
  789. {
  790. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  791. autogenTargetName, true,
  792. workingDirectory.c_str(),
  793. /*byproducts=*/rcc_output, depends,
  794. commandLines, false, autogenComment.c_str());
  795. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  796. lg->AddGeneratorTarget(gt);
  797. lg->GetGlobalGenerator()->AddGeneratorTarget(autogenTarget, gt);
  798. // Set target folder
  799. const char* autogenFolder = makefile->GetState()
  800. ->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  801. if (!autogenFolder)
  802. {
  803. autogenFolder = makefile->GetState()
  804. ->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  805. }
  806. if (autogenFolder && *autogenFolder)
  807. {
  808. autogenTarget->SetProperty("FOLDER", autogenFolder);
  809. }
  810. else
  811. {
  812. // inherit FOLDER property from target (#13688)
  813. copyTargetProperty(gt->Target, target->Target, "FOLDER");
  814. }
  815. target->Target->AddUtility(autogenTargetName);
  816. }
  817. }
  818. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  819. cmGeneratorTarget const* target)
  820. {
  821. cmMakefile* makefile = target->Target->GetMakefile();
  822. // forget the variables added here afterwards again:
  823. cmMakefile::ScopePushPop varScope(makefile);
  824. static_cast<void>(varScope);
  825. // create a custom target for running generators at buildtime:
  826. std::string autogenTargetName = GetAutogenTargetName(target);
  827. makefile->AddDefinition("_moc_target_name",
  828. cmOutputConverter::EscapeForCMake(autogenTargetName).c_str());
  829. makefile->AddDefinition("_origin_target_name",
  830. cmOutputConverter::EscapeForCMake(target->GetName()).c_str());
  831. std::string targetDir = GetAutogenTargetDir(target);
  832. const char *qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  833. if (!qtVersion)
  834. {
  835. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  836. }
  837. if (const char *targetQtVersion =
  838. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""))
  839. {
  840. qtVersion = targetQtVersion;
  841. }
  842. if (qtVersion)
  843. {
  844. makefile->AddDefinition("_target_qt_version", qtVersion);
  845. }
  846. std::vector<std::string> skipUic;
  847. std::vector<std::string> skipMoc;
  848. std::vector<std::string> mocSources;
  849. std::vector<std::string> mocHeaders;
  850. std::map<std::string, std::string> configIncludes;
  851. std::map<std::string, std::string> configDefines;
  852. std::map<std::string, std::string> configUicOptions;
  853. if (target->GetPropertyAsBool("AUTOMOC")
  854. || target->GetPropertyAsBool("AUTOUIC")
  855. || target->GetPropertyAsBool("AUTORCC"))
  856. {
  857. SetupSourceFiles(target, skipMoc, mocSources, mocHeaders, skipUic);
  858. }
  859. makefile->AddDefinition("_cpp_files",
  860. cmOutputConverter::EscapeForCMake(cmJoin(mocSources, ";")).c_str());
  861. if (target->GetPropertyAsBool("AUTOMOC"))
  862. {
  863. SetupAutoMocTarget(target, autogenTargetName,
  864. skipMoc, mocHeaders,
  865. configIncludes, configDefines);
  866. }
  867. if (target->GetPropertyAsBool("AUTOUIC"))
  868. {
  869. SetupAutoUicTarget(target, skipUic, configUicOptions);
  870. }
  871. if (target->GetPropertyAsBool("AUTORCC"))
  872. {
  873. SetupAutoRccTarget(target);
  874. }
  875. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  876. std::string inputFile = cmakeRoot;
  877. inputFile += "/Modules/AutogenInfo.cmake.in";
  878. std::string outputFile = targetDir;
  879. outputFile += "/AutogenInfo.cmake";
  880. makefile->AddDefinition("_qt_rcc_inputs",
  881. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  882. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  883. false, true, false);
  884. // Ensure we have write permission in case .in was read-only.
  885. mode_t perm = 0;
  886. #if defined(_WIN32) && !defined(__CYGWIN__)
  887. mode_t mode_write = S_IWRITE;
  888. #else
  889. mode_t mode_write = S_IWUSR;
  890. #endif
  891. cmSystemTools::GetPermissions(outputFile, perm);
  892. if (!(perm & mode_write))
  893. {
  894. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  895. }
  896. if (!configDefines.empty()
  897. || !configIncludes.empty()
  898. || !configUicOptions.empty())
  899. {
  900. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  901. if ( !infoFile )
  902. {
  903. std::string error = "Internal CMake error when trying to open file: ";
  904. error += outputFile.c_str();
  905. error += " for writing.";
  906. cmSystemTools::Error(error.c_str());
  907. return;
  908. }
  909. if (!configDefines.empty())
  910. {
  911. for (std::map<std::string, std::string>::iterator
  912. it = configDefines.begin(), end = configDefines.end();
  913. it != end; ++it)
  914. {
  915. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first <<
  916. " " << it->second << ")\n";
  917. }
  918. }
  919. if (!configIncludes.empty())
  920. {
  921. for (std::map<std::string, std::string>::iterator
  922. it = configIncludes.begin(), end = configIncludes.end();
  923. it != end; ++it)
  924. {
  925. infoFile << "set(AM_MOC_INCLUDES_" << it->first <<
  926. " " << it->second << ")\n";
  927. }
  928. }
  929. if (!configUicOptions.empty())
  930. {
  931. for (std::map<std::string, std::string>::iterator
  932. it = configUicOptions.begin(), end = configUicOptions.end();
  933. it != end; ++it)
  934. {
  935. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first <<
  936. " " << it->second << ")\n";
  937. }
  938. }
  939. }
  940. }