cmQtAutoGeneratorInitializer.cxx 33 KB

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