cmQtAutoGenGlobalInitializer.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "cmQtAutoGen.h"
  5. #include "cmQtAutoGenInitializer.h"
  6. #include "cmAlgorithms.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmState.h"
  12. #include "cmStateTypes.h"
  13. #include "cmSystemTools.h"
  14. #include "cmTarget.h"
  15. #include "cmake.h"
  16. #include <memory>
  17. #include <utility>
  18. cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
  19. std::vector<cmLocalGenerator*> const& localGenerators)
  20. {
  21. for (cmLocalGenerator* localGen : localGenerators) {
  22. // Detect global autogen and autorcc target names
  23. bool globalAutoGenTarget = false;
  24. bool globalAutoRccTarget = false;
  25. {
  26. cmMakefile* makefile = localGen->GetMakefile();
  27. // Detect global autogen target name
  28. if (cmSystemTools::IsOn(
  29. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET"))) {
  30. std::string targetName =
  31. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET_NAME");
  32. if (targetName.empty()) {
  33. targetName = "autogen";
  34. }
  35. GlobalAutoGenTargets_.emplace(localGen, std::move(targetName));
  36. globalAutoGenTarget = true;
  37. }
  38. // Detect global autorcc target name
  39. if (cmSystemTools::IsOn(
  40. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET"))) {
  41. std::string targetName =
  42. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET_NAME");
  43. if (targetName.empty()) {
  44. targetName = "autorcc";
  45. }
  46. GlobalAutoRccTargets_.emplace(localGen, std::move(targetName));
  47. globalAutoRccTarget = true;
  48. }
  49. }
  50. // Find targets that require AUTOMOC/UIC/RCC processing
  51. for (cmGeneratorTarget* target : localGen->GetGeneratorTargets()) {
  52. // Process only certain target types
  53. switch (target->GetType()) {
  54. case cmStateEnums::EXECUTABLE:
  55. case cmStateEnums::STATIC_LIBRARY:
  56. case cmStateEnums::SHARED_LIBRARY:
  57. case cmStateEnums::MODULE_LIBRARY:
  58. case cmStateEnums::OBJECT_LIBRARY:
  59. // Process target
  60. break;
  61. default:
  62. // Don't process target
  63. continue;
  64. }
  65. if (target->IsImported()) {
  66. // Don't process target
  67. continue;
  68. }
  69. bool const moc = target->GetPropertyAsBool("AUTOMOC");
  70. bool const uic = target->GetPropertyAsBool("AUTOUIC");
  71. bool const rcc = target->GetPropertyAsBool("AUTORCC");
  72. if (moc || uic || rcc) {
  73. std::string const mocExec =
  74. target->GetSafeProperty("AUTOMOC_EXECUTABLE");
  75. std::string const uicExec =
  76. target->GetSafeProperty("AUTOUIC_EXECUTABLE");
  77. std::string const rccExec =
  78. target->GetSafeProperty("AUTORCC_EXECUTABLE");
  79. // We support Qt4, Qt5 and Qt6
  80. auto qtVersion = cmQtAutoGenInitializer::GetQtVersion(target);
  81. bool const validQt = (qtVersion.Major == 4) ||
  82. (qtVersion.Major == 5) || (qtVersion.Major == 6);
  83. bool const mocAvailable = (validQt || !mocExec.empty());
  84. bool const uicAvailable = (validQt || !uicExec.empty());
  85. bool const rccAvailable = (validQt || !rccExec.empty());
  86. bool const mocIsValid = (moc && mocAvailable);
  87. bool const uicIsValid = (uic && uicAvailable);
  88. bool const rccIsValid = (rcc && uicAvailable);
  89. // Disabled AUTOMOC/UIC/RCC warning
  90. bool const mocDisabled = (moc && !mocAvailable);
  91. bool const uicDisabled = (uic && !uicAvailable);
  92. bool const rccDisabled = (rcc && !rccAvailable);
  93. if (mocDisabled || uicDisabled || rccDisabled) {
  94. std::string msg = "AUTOGEN: No valid Qt version found for target ";
  95. msg += target->GetName();
  96. msg += ". ";
  97. {
  98. std::vector<std::string> lst;
  99. if (mocDisabled) {
  100. lst.emplace_back("AUTOMOC");
  101. }
  102. if (uicDisabled) {
  103. lst.emplace_back("AUTOUIC");
  104. }
  105. if (rccDisabled) {
  106. lst.emplace_back("AUTORCC");
  107. }
  108. msg += cmJoin(lst, ", ");
  109. }
  110. msg += " disabled. Consider adding:\n";
  111. if (uicDisabled) {
  112. msg += " find_package(Qt5 COMPONENTS Widgets)\n";
  113. } else {
  114. msg += " find_package(Qt5 COMPONENTS Core)\n";
  115. }
  116. msg += "to your CMakeLists.txt file.";
  117. target->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg);
  118. }
  119. if (mocIsValid || uicIsValid || rccIsValid) {
  120. // Create autogen target initializer
  121. Initializers_.emplace_back(cm::make_unique<cmQtAutoGenInitializer>(
  122. this, target, qtVersion, mocIsValid, uicIsValid, rccIsValid,
  123. globalAutoGenTarget, globalAutoRccTarget));
  124. }
  125. }
  126. }
  127. }
  128. }
  129. cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer()
  130. {
  131. }
  132. void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
  133. cmLocalGenerator* localGen, std::string const& name,
  134. std::string const& comment)
  135. {
  136. // Test if the target already exists
  137. if (localGen->FindGeneratorTargetToUse(name) == nullptr) {
  138. cmMakefile* makefile = localGen->GetMakefile();
  139. // Create utility target
  140. cmTarget* target = makefile->AddUtilityCommand(
  141. name, cmMakefile::TargetOrigin::Generator, true,
  142. makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
  143. std::vector<std::string>() /*output*/,
  144. std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
  145. comment.c_str());
  146. localGen->AddGeneratorTarget(new cmGeneratorTarget(target, localGen));
  147. // Set FOLDER property in the target
  148. {
  149. char const* folder =
  150. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  151. if (folder != nullptr) {
  152. target->SetProperty("FOLDER", folder);
  153. }
  154. }
  155. }
  156. }
  157. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
  158. cmLocalGenerator* localGen, std::string const& targetName)
  159. {
  160. auto it = GlobalAutoGenTargets_.find(localGen);
  161. if (it != GlobalAutoGenTargets_.end()) {
  162. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  163. if (target != nullptr) {
  164. target->Target->AddUtility(targetName, localGen->GetMakefile());
  165. }
  166. }
  167. }
  168. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
  169. cmLocalGenerator* localGen, std::string const& targetName)
  170. {
  171. auto it = GlobalAutoRccTargets_.find(localGen);
  172. if (it != GlobalAutoRccTargets_.end()) {
  173. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  174. if (target != nullptr) {
  175. target->Target->AddUtility(targetName, localGen->GetMakefile());
  176. }
  177. }
  178. }
  179. bool cmQtAutoGenGlobalInitializer::generate()
  180. {
  181. return (InitializeCustomTargets() && SetupCustomTargets());
  182. }
  183. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  184. {
  185. // Initialize global autogen targets
  186. {
  187. std::string const comment = "Global AUTOGEN target";
  188. for (auto const& pair : GlobalAutoGenTargets_) {
  189. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  190. }
  191. }
  192. // Initialize global autorcc targets
  193. {
  194. std::string const comment = "Global AUTORCC target";
  195. for (auto const& pair : GlobalAutoRccTargets_) {
  196. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  197. }
  198. }
  199. // Initialize per target autogen targets
  200. for (auto& initializer : Initializers_) {
  201. if (!initializer->InitCustomTargets()) {
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  208. {
  209. for (auto& initializer : Initializers_) {
  210. if (!initializer->SetupCustomTargets()) {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }