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 "cmCustomCommandLines.h"
  5. #include "cmCustomCommandTypes.h"
  6. #include "cmDuration.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmMessageType.h"
  11. #include "cmProcessOutput.h"
  12. #include "cmQtAutoGen.h"
  13. #include "cmQtAutoGenInitializer.h"
  14. #include "cmState.h"
  15. #include "cmStateTypes.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmTarget.h"
  19. #include <cm/memory>
  20. #include <utility>
  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<cmLocalGenerator*> const& localGenerators)
  40. {
  41. for (cmLocalGenerator* 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, 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, std::move(targetName));
  65. globalAutoRccTarget = true;
  66. }
  67. }
  68. // Find targets that require AUTOMOC/UIC/RCC processing
  69. for (cmGeneratorTarget* 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);
  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, qtVersion.first, mocIsValid, uicIsValid, rccIsValid,
  128. 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. cmTarget* target = makefile->AddUtilityCommand(
  144. name, cmCommandOrigin::Generator, true,
  145. makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
  146. std::vector<std::string>() /*output*/,
  147. std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
  148. comment.c_str());
  149. localGen->AddGeneratorTarget(new 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. }