cmQtAutoGenGlobalInitializer.cxx 9.7 KB

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