cmQtAutoGenGlobalInitializer.cxx 10 KB

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