cmQtAutoGenGlobalInitializer.cxx 10 KB

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