cmQtAutoGenGlobalInitializer.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoGenGlobalInitializer.h"
  4. #include <set>
  5. #include <utility>
  6. #include <cm/memory>
  7. #include "cmCustomCommand.h"
  8. #include "cmDuration.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmLocalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmProcessOutput.h"
  14. #include "cmQtAutoGen.h"
  15. #include "cmQtAutoGenInitializer.h"
  16. #include "cmState.h"
  17. #include "cmStateTypes.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h"
  20. #include "cmTarget.h"
  21. #include "cmValue.h"
  22. cmQtAutoGenGlobalInitializer::Keywords::Keywords()
  23. : AUTOMOC("AUTOMOC")
  24. , AUTOUIC("AUTOUIC")
  25. , AUTORCC("AUTORCC")
  26. , AUTOMOC_EXECUTABLE("AUTOMOC_EXECUTABLE")
  27. , AUTOUIC_EXECUTABLE("AUTOUIC_EXECUTABLE")
  28. , AUTORCC_EXECUTABLE("AUTORCC_EXECUTABLE")
  29. , SKIP_AUTOGEN("SKIP_AUTOGEN")
  30. , SKIP_AUTOMOC("SKIP_AUTOMOC")
  31. , SKIP_AUTOUIC("SKIP_AUTOUIC")
  32. , SKIP_AUTORCC("SKIP_AUTORCC")
  33. , AUTOUIC_OPTIONS("AUTOUIC_OPTIONS")
  34. , AUTORCC_OPTIONS("AUTORCC_OPTIONS")
  35. , qrc("qrc")
  36. , ui("ui")
  37. {
  38. }
  39. cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
  40. std::vector<std::unique_ptr<cmLocalGenerator>> const& localGenerators)
  41. {
  42. for (const auto& localGen : localGenerators) {
  43. // Detect global autogen and autorcc target names
  44. bool globalAutoGenTarget = false;
  45. bool globalAutoRccTarget = false;
  46. {
  47. cmMakefile const* makefile = localGen->GetMakefile();
  48. // Detect global autogen target name
  49. if (makefile->IsOn("CMAKE_GLOBAL_AUTOGEN_TARGET")) {
  50. std::string targetName =
  51. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET_NAME");
  52. if (targetName.empty()) {
  53. targetName = "autogen";
  54. }
  55. this->GlobalAutoGenTargets_.emplace(localGen.get(),
  56. std::move(targetName));
  57. globalAutoGenTarget = true;
  58. }
  59. // Detect global autorcc target name
  60. if (makefile->IsOn("CMAKE_GLOBAL_AUTORCC_TARGET")) {
  61. std::string targetName =
  62. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET_NAME");
  63. if (targetName.empty()) {
  64. targetName = "autorcc";
  65. }
  66. this->GlobalAutoRccTargets_.emplace(localGen.get(),
  67. std::move(targetName));
  68. globalAutoRccTarget = true;
  69. }
  70. }
  71. // Find targets that require AUTOMOC/UIC/RCC processing
  72. for (const auto& target : localGen->GetGeneratorTargets()) {
  73. // Process only certain target types
  74. switch (target->GetType()) {
  75. case cmStateEnums::EXECUTABLE:
  76. case cmStateEnums::STATIC_LIBRARY:
  77. case cmStateEnums::SHARED_LIBRARY:
  78. case cmStateEnums::MODULE_LIBRARY:
  79. case cmStateEnums::OBJECT_LIBRARY:
  80. // Process target
  81. break;
  82. default:
  83. // Don't process target
  84. continue;
  85. }
  86. if (target->IsImported()) {
  87. // Don't process target
  88. continue;
  89. }
  90. std::set<std::string> const& languages =
  91. target->GetAllConfigCompileLanguages();
  92. // cmGeneratorTarget::GetAllConfigCompileLanguages caches the target's
  93. // sources. Clear it so that OBJECT library targets that are AUTOGEN
  94. // initialized after this target get their added mocs_compilation.cpp
  95. // source acknowledged by this target.
  96. target->ClearSourcesCache();
  97. if (languages.count("CSharp")) {
  98. // Don't process target if it's a CSharp target
  99. continue;
  100. }
  101. bool const moc = target->GetPropertyAsBool(this->kw().AUTOMOC);
  102. bool const uic = target->GetPropertyAsBool(this->kw().AUTOUIC);
  103. bool const rcc = target->GetPropertyAsBool(this->kw().AUTORCC);
  104. if (moc || uic || rcc) {
  105. std::string const& mocExec =
  106. target->GetSafeProperty(this->kw().AUTOMOC_EXECUTABLE);
  107. std::string const& uicExec =
  108. target->GetSafeProperty(this->kw().AUTOUIC_EXECUTABLE);
  109. std::string const& rccExec =
  110. target->GetSafeProperty(this->kw().AUTORCC_EXECUTABLE);
  111. // We support Qt4, Qt5 and Qt6
  112. auto const qtVersion =
  113. cmQtAutoGenInitializer::GetQtVersion(target.get(), mocExec);
  114. bool const validQt = (qtVersion.first.Major == 4) ||
  115. (qtVersion.first.Major == 5) || (qtVersion.first.Major == 6);
  116. bool const mocAvailable = (validQt || !mocExec.empty());
  117. bool const uicAvailable = (validQt || !uicExec.empty());
  118. bool const rccAvailable = (validQt || !rccExec.empty());
  119. bool const mocIsValid = (moc && mocAvailable);
  120. bool const uicIsValid = (uic && uicAvailable);
  121. bool const rccIsValid = (rcc && rccAvailable);
  122. // Disabled AUTOMOC/UIC/RCC warning
  123. bool const mocDisabled = (moc && !mocAvailable);
  124. bool const uicDisabled = (uic && !uicAvailable);
  125. bool const rccDisabled = (rcc && !rccAvailable);
  126. if (mocDisabled || uicDisabled || rccDisabled) {
  127. cmAlphaNum version = (qtVersion.second == 0)
  128. ? cmAlphaNum("<QTVERSION>")
  129. : cmAlphaNum(qtVersion.second);
  130. cmAlphaNum component = uicDisabled ? "Widgets" : "Core";
  131. std::string const msg = cmStrCat(
  132. "AUTOGEN: No valid Qt version found for target ",
  133. target->GetName(), ". ",
  134. cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled),
  135. " disabled. Consider adding:\n", " find_package(Qt", version,
  136. " COMPONENTS ", component, ")\n", "to your CMakeLists.txt file.");
  137. target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  138. }
  139. if (mocIsValid || uicIsValid || rccIsValid) {
  140. // Create autogen target initializer
  141. this->Initializers_.emplace_back(
  142. cm::make_unique<cmQtAutoGenInitializer>(
  143. this, target.get(), qtVersion.first, mocIsValid, uicIsValid,
  144. rccIsValid, globalAutoGenTarget, globalAutoRccTarget));
  145. }
  146. }
  147. }
  148. }
  149. }
  150. cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer() = default;
  151. void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
  152. cmLocalGenerator* localGen, std::string const& name,
  153. std::string const& comment)
  154. {
  155. // Test if the target already exists
  156. if (!localGen->FindGeneratorTargetToUse(name)) {
  157. cmMakefile const* makefile = localGen->GetMakefile();
  158. // Create utility target
  159. auto cc = cm::make_unique<cmCustomCommand>();
  160. cc->SetWorkingDirectory(makefile->GetHomeOutputDirectory().c_str());
  161. cc->SetEscapeOldStyle(false);
  162. cc->SetComment(comment.c_str());
  163. cmTarget* target = localGen->AddUtilityCommand(name, true, std::move(cc));
  164. localGen->AddGeneratorTarget(
  165. cm::make_unique<cmGeneratorTarget>(target, localGen));
  166. // Set FOLDER property in the target
  167. {
  168. cmValue folder =
  169. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  170. if (folder) {
  171. target->SetProperty("FOLDER", folder);
  172. }
  173. }
  174. }
  175. }
  176. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
  177. cmLocalGenerator* localGen, std::string const& targetName)
  178. {
  179. auto const it = this->GlobalAutoGenTargets_.find(localGen);
  180. if (it != this->GlobalAutoGenTargets_.end()) {
  181. cmGeneratorTarget const* target =
  182. localGen->FindGeneratorTargetToUse(it->second);
  183. if (target) {
  184. target->Target->AddUtility(targetName, false, localGen->GetMakefile());
  185. }
  186. }
  187. }
  188. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
  189. cmLocalGenerator* localGen, std::string const& targetName)
  190. {
  191. auto const it = this->GlobalAutoRccTargets_.find(localGen);
  192. if (it != this->GlobalAutoRccTargets_.end()) {
  193. cmGeneratorTarget const* target =
  194. localGen->FindGeneratorTargetToUse(it->second);
  195. if (target) {
  196. target->Target->AddUtility(targetName, false, localGen->GetMakefile());
  197. }
  198. }
  199. }
  200. cmQtAutoGen::ConfigStrings<cmQtAutoGen::CompilerFeaturesHandle>
  201. cmQtAutoGenGlobalInitializer::GetCompilerFeatures(
  202. std::string const& generator, cmQtAutoGen::ConfigString const& executable,
  203. std::string& error, bool const isMultiConfig, bool const UseBetterGraph)
  204. {
  205. cmQtAutoGen::ConfigStrings<cmQtAutoGen::CompilerFeaturesHandle> res;
  206. if (isMultiConfig && UseBetterGraph) {
  207. for (auto const& config : executable.Config) {
  208. auto const exe = config.second;
  209. // Check if we have cached features
  210. {
  211. auto it = this->CompilerFeatures_.Config[config.first].find(exe);
  212. if (it != this->CompilerFeatures_.Config[config.first].end()) {
  213. res.Config[config.first] = it->second;
  214. continue;
  215. }
  216. }
  217. // Check if the executable exists
  218. if (!cmSystemTools::FileExists(exe, true)) {
  219. error = cmStrCat("The \"", generator, "\" executable ",
  220. cmQtAutoGen::Quoted(exe), " does not exist.");
  221. res.Config[config.first] = {};
  222. continue;
  223. }
  224. // Test the executable
  225. std::string stdOut;
  226. {
  227. std::string stdErr;
  228. std::vector<std::string> command;
  229. command.emplace_back(exe);
  230. command.emplace_back("-h");
  231. int retVal = 0;
  232. const bool runResult = cmSystemTools::RunSingleCommand(
  233. command, &stdOut, &stdErr, &retVal, nullptr,
  234. cmSystemTools::OUTPUT_NONE, cmDuration::zero(),
  235. cmProcessOutput::Auto);
  236. if (!runResult) {
  237. error = cmStrCat("Test run of \"", generator, "\" executable ",
  238. cmQtAutoGen::Quoted(exe), " failed.\n",
  239. cmQtAutoGen::QuotedCommand(command), '\n', stdOut,
  240. '\n', stdErr);
  241. res.Config[config.first] = {};
  242. continue;
  243. }
  244. }
  245. // Create valid handle
  246. res.Config[config.first] =
  247. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  248. res.Config[config.first]->HelpOutput = std::move(stdOut);
  249. // Register compiler features
  250. this->CompilerFeatures_.Config[config.first].emplace(
  251. exe, res.Config[config.first]);
  252. }
  253. return res;
  254. }
  255. // Check if we have cached features
  256. {
  257. auto const it = this->CompilerFeatures_.Default.find(executable.Default);
  258. if (it != this->CompilerFeatures_.Default.end()) {
  259. res.Default = it->second;
  260. return res;
  261. }
  262. }
  263. // Check if the executable exists
  264. if (!cmSystemTools::FileExists(executable.Default, true)) {
  265. error =
  266. cmStrCat("The \"", generator, "\" executable ",
  267. cmQtAutoGen::Quoted(executable.Default), " does not exist.");
  268. return cmQtAutoGen::ConfigStrings<cmQtAutoGen::CompilerFeaturesHandle>();
  269. }
  270. // Test the executable
  271. std::string stdOut;
  272. {
  273. std::string stdErr;
  274. std::vector<std::string> command;
  275. command.emplace_back(executable.Default);
  276. command.emplace_back("-h");
  277. int retVal = 0;
  278. const bool runResult = cmSystemTools::RunSingleCommand(
  279. command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
  280. cmDuration::zero(), cmProcessOutput::Auto);
  281. if (!runResult) {
  282. error = cmStrCat("Test run of \"", generator, "\" executable ",
  283. cmQtAutoGen::Quoted(executable.Default), " failed.\n",
  284. cmQtAutoGen::QuotedCommand(command), '\n', stdOut, '\n',
  285. stdErr);
  286. return cmQtAutoGen::ConfigStrings<cmQtAutoGen::CompilerFeaturesHandle>();
  287. }
  288. }
  289. res.Default = std::make_shared<cmQtAutoGen::CompilerFeatures>();
  290. res.Default->HelpOutput = std::move(stdOut);
  291. // Register compiler features
  292. this->CompilerFeatures_.Default.emplace(executable.Default, res.Default);
  293. return res;
  294. }
  295. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  296. {
  297. // Initialize global autogen targets
  298. {
  299. std::string const comment = "Global AUTOGEN target";
  300. for (auto const& pair : this->GlobalAutoGenTargets_) {
  301. this->GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  302. }
  303. }
  304. // Initialize global autorcc targets
  305. {
  306. std::string const comment = "Global AUTORCC target";
  307. for (auto const& pair : this->GlobalAutoRccTargets_) {
  308. this->GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  309. }
  310. }
  311. // Initialize per target autogen targets
  312. for (auto& initializer : this->Initializers_) {
  313. if (!initializer->InitCustomTargets()) {
  314. return false;
  315. }
  316. }
  317. return true;
  318. }
  319. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  320. {
  321. for (auto& initializer : this->Initializers_) {
  322. if (!initializer->SetupCustomTargets()) {
  323. return false;
  324. }
  325. }
  326. return true;
  327. }