cmQtAutoGenGlobalInitializer.cxx 9.7 KB

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