cmQtAutoGenGlobalInitializer.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <memory>
  16. #include <utility>
  17. cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
  18. std::vector<cmLocalGenerator*> const& localGenerators)
  19. {
  20. for (cmLocalGenerator* localGen : localGenerators) {
  21. // Detect global autogen and autorcc target names
  22. bool globalAutoGenTarget = false;
  23. bool globalAutoRccTarget = false;
  24. {
  25. cmMakefile* makefile = localGen->GetMakefile();
  26. // Detect global autogen target name
  27. if (cmSystemTools::IsOn(
  28. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET"))) {
  29. std::string targetName =
  30. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET_NAME");
  31. if (targetName.empty()) {
  32. targetName = "autogen";
  33. }
  34. GlobalAutoGenTargets_.emplace(localGen, std::move(targetName));
  35. globalAutoGenTarget = true;
  36. }
  37. // Detect global autorcc target name
  38. if (cmSystemTools::IsOn(
  39. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET"))) {
  40. std::string targetName =
  41. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET_NAME");
  42. if (targetName.empty()) {
  43. targetName = "autorcc";
  44. }
  45. GlobalAutoRccTargets_.emplace(localGen, std::move(targetName));
  46. globalAutoRccTarget = true;
  47. }
  48. }
  49. // Find targets that require AUTOMOC/UIC/RCC processing
  50. for (cmGeneratorTarget* target : localGen->GetGeneratorTargets()) {
  51. // Process only certain target types
  52. switch (target->GetType()) {
  53. case cmStateEnums::EXECUTABLE:
  54. case cmStateEnums::STATIC_LIBRARY:
  55. case cmStateEnums::SHARED_LIBRARY:
  56. case cmStateEnums::MODULE_LIBRARY:
  57. case cmStateEnums::OBJECT_LIBRARY:
  58. // Process target
  59. break;
  60. default:
  61. // Don't process target
  62. continue;
  63. }
  64. if (target->IsImported()) {
  65. // Don't process target
  66. continue;
  67. }
  68. bool const moc = target->GetPropertyAsBool("AUTOMOC");
  69. bool const uic = target->GetPropertyAsBool("AUTOUIC");
  70. bool const rcc = target->GetPropertyAsBool("AUTORCC");
  71. if (moc || uic || rcc) {
  72. // We support Qt4 and Qt5
  73. auto qtVersion = cmQtAutoGenInitializer::GetQtVersion(target);
  74. if ((qtVersion.Major == 4) || (qtVersion.Major == 5) ||
  75. (qtVersion.Major == 6)) {
  76. // Create autogen target initializer
  77. Initializers_.emplace_back(cm::make_unique<cmQtAutoGenInitializer>(
  78. this, target, qtVersion, moc, uic, rcc, globalAutoGenTarget,
  79. globalAutoRccTarget));
  80. }
  81. }
  82. }
  83. }
  84. }
  85. cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer()
  86. {
  87. }
  88. void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
  89. cmLocalGenerator* localGen, std::string const& name,
  90. std::string const& comment)
  91. {
  92. // Test if the target already exists
  93. if (localGen->FindGeneratorTargetToUse(name) == nullptr) {
  94. cmMakefile* makefile = localGen->GetMakefile();
  95. // Create utility target
  96. cmTarget* target = makefile->AddUtilityCommand(
  97. name, cmMakefile::TargetOrigin::Generator, true,
  98. makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
  99. std::vector<std::string>() /*output*/,
  100. std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
  101. comment.c_str());
  102. localGen->AddGeneratorTarget(new cmGeneratorTarget(target, localGen));
  103. // Set FOLDER property in the target
  104. {
  105. char const* folder =
  106. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  107. if (folder != nullptr) {
  108. target->SetProperty("FOLDER", folder);
  109. }
  110. }
  111. }
  112. }
  113. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
  114. cmLocalGenerator* localGen, std::string const& targetName)
  115. {
  116. auto it = GlobalAutoGenTargets_.find(localGen);
  117. if (it != GlobalAutoGenTargets_.end()) {
  118. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  119. if (target != nullptr) {
  120. target->Target->AddUtility(targetName, localGen->GetMakefile());
  121. }
  122. }
  123. }
  124. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
  125. cmLocalGenerator* localGen, std::string const& targetName)
  126. {
  127. auto it = GlobalAutoRccTargets_.find(localGen);
  128. if (it != GlobalAutoRccTargets_.end()) {
  129. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  130. if (target != nullptr) {
  131. target->Target->AddUtility(targetName, localGen->GetMakefile());
  132. }
  133. }
  134. }
  135. bool cmQtAutoGenGlobalInitializer::generate()
  136. {
  137. return (InitializeCustomTargets() && SetupCustomTargets());
  138. }
  139. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  140. {
  141. // Initialize global autogen targets
  142. {
  143. std::string const comment = "Global AUTOGEN target";
  144. for (auto const& pair : GlobalAutoGenTargets_) {
  145. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  146. }
  147. }
  148. // Initialize global autorcc targets
  149. {
  150. std::string const comment = "Global AUTORCC target";
  151. for (auto const& pair : GlobalAutoRccTargets_) {
  152. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  153. }
  154. }
  155. // Initialize per target autogen targets
  156. for (auto& initializer : Initializers_) {
  157. if (!initializer->InitCustomTargets()) {
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  164. {
  165. for (auto& initializer : Initializers_) {
  166. if (!initializer->SetupCustomTargets()) {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }