cmQtAutoGenGlobalInitializer.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "cmDuration.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmLocalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmProcessOutput.h"
  11. #include "cmQtAutoGen.h"
  12. #include "cmQtAutoGenInitializer.h"
  13. #include "cmState.h"
  14. #include "cmStateTypes.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cm_memory.hxx"
  19. #include <utility>
  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<cmLocalGenerator*> const& localGenerators)
  39. {
  40. for (cmLocalGenerator* 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, 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, std::move(targetName));
  64. globalAutoRccTarget = true;
  65. }
  66. }
  67. // Find targets that require AUTOMOC/UIC/RCC processing
  68. for (cmGeneratorTarget* 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);
  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, qtVersion.first, mocIsValid, uicIsValid, rccIsValid,
  127. 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. cmTarget* target = makefile->AddUtilityCommand(
  143. name, cmMakefile::TargetOrigin::Generator, true,
  144. makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
  145. std::vector<std::string>() /*output*/,
  146. std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
  147. comment.c_str());
  148. localGen->AddGeneratorTarget(new cmGeneratorTarget(target, localGen));
  149. // Set FOLDER property in the target
  150. {
  151. char const* folder =
  152. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  153. if (folder != nullptr) {
  154. target->SetProperty("FOLDER", folder);
  155. }
  156. }
  157. }
  158. }
  159. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
  160. cmLocalGenerator* localGen, std::string const& targetName)
  161. {
  162. auto it = GlobalAutoGenTargets_.find(localGen);
  163. if (it != GlobalAutoGenTargets_.end()) {
  164. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  165. if (target != nullptr) {
  166. target->Target->AddUtility(targetName, localGen->GetMakefile());
  167. }
  168. }
  169. }
  170. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
  171. cmLocalGenerator* localGen, std::string const& targetName)
  172. {
  173. auto it = GlobalAutoRccTargets_.find(localGen);
  174. if (it != GlobalAutoRccTargets_.end()) {
  175. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  176. if (target != nullptr) {
  177. target->Target->AddUtility(targetName, localGen->GetMakefile());
  178. }
  179. }
  180. }
  181. cmQtAutoGen::CompilerFeaturesHandle
  182. cmQtAutoGenGlobalInitializer::GetCompilerFeatures(
  183. std::string const& generator, std::string const& executable,
  184. std::string& error)
  185. {
  186. // Check if we have cached features
  187. {
  188. auto it = this->CompilerFeatures_.find(executable);
  189. if (it != this->CompilerFeatures_.end()) {
  190. return it->second;
  191. }
  192. }
  193. // Check if the executable exists
  194. if (!cmSystemTools::FileExists(executable, true)) {
  195. error = "The \"";
  196. error += generator;
  197. error += "\" executable ";
  198. error += cmQtAutoGen::Quoted(executable);
  199. error += " does not exist.";
  200. return cmQtAutoGen::CompilerFeaturesHandle();
  201. }
  202. // Test the executable
  203. std::string stdOut;
  204. {
  205. std::string stdErr;
  206. std::vector<std::string> command;
  207. command.emplace_back(executable);
  208. command.emplace_back("-h");
  209. int retVal = 0;
  210. const bool runResult = cmSystemTools::RunSingleCommand(
  211. command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
  212. cmDuration::zero(), cmProcessOutput::Auto);
  213. if (!runResult) {
  214. error = "Test run of \"";
  215. error += generator;
  216. error += "\" executable ";
  217. error += cmQtAutoGen::Quoted(executable) + " failed.\n";
  218. error += cmQtAutoGen::QuotedCommand(command);
  219. error += "\n";
  220. error += stdOut;
  221. error += "\n";
  222. error += stdErr;
  223. return cmQtAutoGen::CompilerFeaturesHandle();
  224. }
  225. }
  226. // Create valid handle
  227. cmQtAutoGen::CompilerFeaturesHandle res =
  228. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  229. res->HelpOutput = std::move(stdOut);
  230. // Register compiler features
  231. this->CompilerFeatures_.emplace(executable, res);
  232. return res;
  233. }
  234. bool cmQtAutoGenGlobalInitializer::generate()
  235. {
  236. return (InitializeCustomTargets() && SetupCustomTargets());
  237. }
  238. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  239. {
  240. // Initialize global autogen targets
  241. {
  242. std::string const comment = "Global AUTOGEN target";
  243. for (auto const& pair : GlobalAutoGenTargets_) {
  244. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  245. }
  246. }
  247. // Initialize global autorcc targets
  248. {
  249. std::string const comment = "Global AUTORCC target";
  250. for (auto const& pair : GlobalAutoRccTargets_) {
  251. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  252. }
  253. }
  254. // Initialize per target autogen targets
  255. for (auto& initializer : Initializers_) {
  256. if (!initializer->InitCustomTargets()) {
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  263. {
  264. for (auto& initializer : Initializers_) {
  265. if (!initializer->SetupCustomTargets()) {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }