cmQtAutoGeneratorInitializer.cxx 35 KB

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