cmQtAutoGenGlobalInitializer.cxx 11 KB

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