cmQtAutoGenGlobalInitializer.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "cmCustomCommandLines.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 "cmProperty.h"
  15. #include "cmQtAutoGen.h"
  16. #include "cmQtAutoGenInitializer.h"
  17. #include "cmState.h"
  18. #include "cmStateTypes.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmSystemTools.h"
  21. #include "cmTarget.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* 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. if (languages.count("CSharp")) {
  93. // Don't process target if it's a CSharp target
  94. continue;
  95. }
  96. bool const moc = target->GetPropertyAsBool(this->kw().AUTOMOC);
  97. bool const uic = target->GetPropertyAsBool(this->kw().AUTOUIC);
  98. bool const rcc = target->GetPropertyAsBool(this->kw().AUTORCC);
  99. if (moc || uic || rcc) {
  100. std::string const& mocExec =
  101. target->GetSafeProperty(this->kw().AUTOMOC_EXECUTABLE);
  102. std::string const& uicExec =
  103. target->GetSafeProperty(this->kw().AUTOUIC_EXECUTABLE);
  104. std::string const& rccExec =
  105. target->GetSafeProperty(this->kw().AUTORCC_EXECUTABLE);
  106. // We support Qt4, Qt5 and Qt6
  107. auto qtVersion = cmQtAutoGenInitializer::GetQtVersion(target.get());
  108. bool const validQt = (qtVersion.first.Major == 4) ||
  109. (qtVersion.first.Major == 5) || (qtVersion.first.Major == 6);
  110. bool const mocAvailable = (validQt || !mocExec.empty());
  111. bool const uicAvailable = (validQt || !uicExec.empty());
  112. bool const rccAvailable = (validQt || !rccExec.empty());
  113. bool const mocIsValid = (moc && mocAvailable);
  114. bool const uicIsValid = (uic && uicAvailable);
  115. bool const rccIsValid = (rcc && rccAvailable);
  116. // Disabled AUTOMOC/UIC/RCC warning
  117. bool const mocDisabled = (moc && !mocAvailable);
  118. bool const uicDisabled = (uic && !uicAvailable);
  119. bool const rccDisabled = (rcc && !rccAvailable);
  120. if (mocDisabled || uicDisabled || rccDisabled) {
  121. cmAlphaNum version = (qtVersion.second == 0)
  122. ? cmAlphaNum("<QTVERSION>")
  123. : cmAlphaNum(qtVersion.second);
  124. cmAlphaNum component = uicDisabled ? "Widgets" : "Core";
  125. std::string const msg = cmStrCat(
  126. "AUTOGEN: No valid Qt version found for target ",
  127. target->GetName(), ". ",
  128. cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled),
  129. " disabled. Consider adding:\n", " find_package(Qt", version,
  130. " COMPONENTS ", component, ")\n", "to your CMakeLists.txt file.");
  131. target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  132. }
  133. if (mocIsValid || uicIsValid || rccIsValid) {
  134. // Create autogen target initializer
  135. this->Initializers_.emplace_back(
  136. cm::make_unique<cmQtAutoGenInitializer>(
  137. this, target.get(), qtVersion.first, mocIsValid, uicIsValid,
  138. rccIsValid, globalAutoGenTarget, globalAutoRccTarget));
  139. }
  140. }
  141. }
  142. }
  143. }
  144. cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer() = default;
  145. void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
  146. cmLocalGenerator* localGen, std::string const& name,
  147. std::string const& comment)
  148. {
  149. // Test if the target already exists
  150. if (localGen->FindGeneratorTargetToUse(name) == nullptr) {
  151. cmMakefile* makefile = localGen->GetMakefile();
  152. // Create utility target
  153. std::vector<std::string> no_byproducts;
  154. std::vector<std::string> no_depends;
  155. cmCustomCommandLines no_commands;
  156. cmTarget* target = localGen->AddUtilityCommand(
  157. name, true, makefile->GetHomeOutputDirectory().c_str(), no_byproducts,
  158. no_depends, no_commands, false, comment.c_str());
  159. localGen->AddGeneratorTarget(
  160. cm::make_unique<cmGeneratorTarget>(target, localGen));
  161. // Set FOLDER property in the target
  162. {
  163. cmProp folder =
  164. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  165. if (folder != nullptr) {
  166. target->SetProperty("FOLDER", *folder);
  167. }
  168. }
  169. }
  170. }
  171. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
  172. cmLocalGenerator* localGen, std::string const& targetName)
  173. {
  174. auto it = this->GlobalAutoGenTargets_.find(localGen);
  175. if (it != this->GlobalAutoGenTargets_.end()) {
  176. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  177. if (target != nullptr) {
  178. target->Target->AddUtility(targetName, false, localGen->GetMakefile());
  179. }
  180. }
  181. }
  182. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
  183. cmLocalGenerator* localGen, std::string const& targetName)
  184. {
  185. auto it = this->GlobalAutoRccTargets_.find(localGen);
  186. if (it != this->GlobalAutoRccTargets_.end()) {
  187. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  188. if (target != nullptr) {
  189. target->Target->AddUtility(targetName, false, localGen->GetMakefile());
  190. }
  191. }
  192. }
  193. cmQtAutoGen::CompilerFeaturesHandle
  194. cmQtAutoGenGlobalInitializer::GetCompilerFeatures(
  195. std::string const& generator, std::string const& executable,
  196. std::string& error)
  197. {
  198. // Check if we have cached features
  199. {
  200. auto it = this->CompilerFeatures_.find(executable);
  201. if (it != this->CompilerFeatures_.end()) {
  202. return it->second;
  203. }
  204. }
  205. // Check if the executable exists
  206. if (!cmSystemTools::FileExists(executable, true)) {
  207. error = cmStrCat("The \"", generator, "\" executable ",
  208. cmQtAutoGen::Quoted(executable), " does not exist.");
  209. return cmQtAutoGen::CompilerFeaturesHandle();
  210. }
  211. // Test the executable
  212. std::string stdOut;
  213. {
  214. std::string stdErr;
  215. std::vector<std::string> command;
  216. command.emplace_back(executable);
  217. command.emplace_back("-h");
  218. int retVal = 0;
  219. const bool runResult = cmSystemTools::RunSingleCommand(
  220. command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
  221. cmDuration::zero(), cmProcessOutput::Auto);
  222. if (!runResult) {
  223. error = cmStrCat("Test run of \"", generator, "\" executable ",
  224. cmQtAutoGen::Quoted(executable), " failed.\n",
  225. cmQtAutoGen::QuotedCommand(command), '\n', stdOut, '\n',
  226. stdErr);
  227. return cmQtAutoGen::CompilerFeaturesHandle();
  228. }
  229. }
  230. // Create valid handle
  231. cmQtAutoGen::CompilerFeaturesHandle res =
  232. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  233. res->HelpOutput = std::move(stdOut);
  234. // Register compiler features
  235. this->CompilerFeatures_.emplace(executable, res);
  236. return res;
  237. }
  238. bool cmQtAutoGenGlobalInitializer::generate()
  239. {
  240. return (this->InitializeCustomTargets() && this->SetupCustomTargets());
  241. }
  242. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  243. {
  244. // Initialize global autogen targets
  245. {
  246. std::string const comment = "Global AUTOGEN target";
  247. for (auto const& pair : this->GlobalAutoGenTargets_) {
  248. this->GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  249. }
  250. }
  251. // Initialize global autorcc targets
  252. {
  253. std::string const comment = "Global AUTORCC target";
  254. for (auto const& pair : this->GlobalAutoRccTargets_) {
  255. this->GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  256. }
  257. }
  258. // Initialize per target autogen targets
  259. for (auto& initializer : this->Initializers_) {
  260. if (!initializer->InitCustomTargets()) {
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  267. {
  268. for (auto& initializer : this->Initializers_) {
  269. if (!initializer->SetupCustomTargets()) {
  270. return false;
  271. }
  272. }
  273. return true;
  274. }