cmQtAutoGenGlobalInitializer.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 (cmSystemTools::IsOn(
  48. 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 (cmSystemTools::IsOn(
  59. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET"))) {
  60. std::string targetName =
  61. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET_NAME");
  62. if (targetName.empty()) {
  63. targetName = "autorcc";
  64. }
  65. GlobalAutoRccTargets_.emplace(localGen, std::move(targetName));
  66. globalAutoRccTarget = true;
  67. }
  68. }
  69. // Find targets that require AUTOMOC/UIC/RCC processing
  70. for (cmGeneratorTarget* target : localGen->GetGeneratorTargets()) {
  71. // Process only certain target types
  72. switch (target->GetType()) {
  73. case cmStateEnums::EXECUTABLE:
  74. case cmStateEnums::STATIC_LIBRARY:
  75. case cmStateEnums::SHARED_LIBRARY:
  76. case cmStateEnums::MODULE_LIBRARY:
  77. case cmStateEnums::OBJECT_LIBRARY:
  78. // Process target
  79. break;
  80. default:
  81. // Don't process target
  82. continue;
  83. }
  84. if (target->IsImported()) {
  85. // Don't process target
  86. continue;
  87. }
  88. bool const moc = target->GetPropertyAsBool(kw().AUTOMOC);
  89. bool const uic = target->GetPropertyAsBool(kw().AUTOUIC);
  90. bool const rcc = target->GetPropertyAsBool(kw().AUTORCC);
  91. if (moc || uic || rcc) {
  92. std::string const mocExec =
  93. target->GetSafeProperty(kw().AUTOMOC_EXECUTABLE);
  94. std::string const uicExec =
  95. target->GetSafeProperty(kw().AUTOUIC_EXECUTABLE);
  96. std::string const rccExec =
  97. target->GetSafeProperty(kw().AUTORCC_EXECUTABLE);
  98. // We support Qt4, Qt5 and Qt6
  99. auto qtVersion = cmQtAutoGenInitializer::GetQtVersion(target);
  100. bool const validQt = (qtVersion.first.Major == 4) ||
  101. (qtVersion.first.Major == 5) || (qtVersion.first.Major == 6);
  102. bool const mocAvailable = (validQt || !mocExec.empty());
  103. bool const uicAvailable = (validQt || !uicExec.empty());
  104. bool const rccAvailable = (validQt || !rccExec.empty());
  105. bool const mocIsValid = (moc && mocAvailable);
  106. bool const uicIsValid = (uic && uicAvailable);
  107. bool const rccIsValid = (rcc && rccAvailable);
  108. // Disabled AUTOMOC/UIC/RCC warning
  109. bool const mocDisabled = (moc && !mocAvailable);
  110. bool const uicDisabled = (uic && !uicAvailable);
  111. bool const rccDisabled = (rcc && !rccAvailable);
  112. if (mocDisabled || uicDisabled || rccDisabled) {
  113. cmAlphaNum version = (qtVersion.second == 0)
  114. ? cmAlphaNum("<QTVERSION>")
  115. : cmAlphaNum(qtVersion.second);
  116. cmAlphaNum component = uicDisabled ? "Widgets" : "Core";
  117. std::string const msg = cmStrCat(
  118. "AUTOGEN: No valid Qt version found for target ",
  119. target->GetName(), ". ",
  120. cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled),
  121. " disabled. Consider adding:\n", " find_package(Qt", version,
  122. " COMPONENTS ", component, ")\n", "to your CMakeLists.txt file.");
  123. target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  124. }
  125. if (mocIsValid || uicIsValid || rccIsValid) {
  126. // Create autogen target initializer
  127. Initializers_.emplace_back(cm::make_unique<cmQtAutoGenInitializer>(
  128. this, target, qtVersion.first, mocIsValid, uicIsValid, rccIsValid,
  129. globalAutoGenTarget, globalAutoRccTarget));
  130. }
  131. }
  132. }
  133. }
  134. }
  135. cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer() = default;
  136. void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
  137. cmLocalGenerator* localGen, std::string const& name,
  138. std::string const& comment)
  139. {
  140. // Test if the target already exists
  141. if (localGen->FindGeneratorTargetToUse(name) == nullptr) {
  142. cmMakefile* makefile = localGen->GetMakefile();
  143. // Create utility target
  144. cmTarget* target = makefile->AddUtilityCommand(
  145. name, cmMakefile::TargetOrigin::Generator, true,
  146. makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
  147. std::vector<std::string>() /*output*/,
  148. std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
  149. comment.c_str());
  150. localGen->AddGeneratorTarget(new cmGeneratorTarget(target, localGen));
  151. // Set FOLDER property in the target
  152. {
  153. char const* 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, 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, 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 = "The \"";
  198. error += generator;
  199. error += "\" executable ";
  200. error += cmQtAutoGen::Quoted(executable);
  201. error += " does not exist.";
  202. return cmQtAutoGen::CompilerFeaturesHandle();
  203. }
  204. // Test the executable
  205. std::string stdOut;
  206. {
  207. std::string stdErr;
  208. std::vector<std::string> command;
  209. command.emplace_back(executable);
  210. command.emplace_back("-h");
  211. int retVal = 0;
  212. const bool runResult = cmSystemTools::RunSingleCommand(
  213. command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
  214. cmDuration::zero(), cmProcessOutput::Auto);
  215. if (!runResult) {
  216. error = "Test run of \"";
  217. error += generator;
  218. error += "\" executable ";
  219. error += cmQtAutoGen::Quoted(executable) + " failed.\n";
  220. error += cmQtAutoGen::QuotedCommand(command);
  221. error += "\n";
  222. error += stdOut;
  223. error += "\n";
  224. error += stdErr;
  225. return cmQtAutoGen::CompilerFeaturesHandle();
  226. }
  227. }
  228. // Create valid handle
  229. cmQtAutoGen::CompilerFeaturesHandle res =
  230. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  231. res->HelpOutput = std::move(stdOut);
  232. // Register compiler features
  233. this->CompilerFeatures_.emplace(executable, res);
  234. return res;
  235. }
  236. bool cmQtAutoGenGlobalInitializer::generate()
  237. {
  238. return (InitializeCustomTargets() && SetupCustomTargets());
  239. }
  240. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  241. {
  242. // Initialize global autogen targets
  243. {
  244. std::string const comment = "Global AUTOGEN target";
  245. for (auto const& pair : GlobalAutoGenTargets_) {
  246. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  247. }
  248. }
  249. // Initialize global autorcc targets
  250. {
  251. std::string const comment = "Global AUTORCC target";
  252. for (auto const& pair : GlobalAutoRccTargets_) {
  253. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  254. }
  255. }
  256. // Initialize per target autogen targets
  257. for (auto& initializer : Initializers_) {
  258. if (!initializer->InitCustomTargets()) {
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  265. {
  266. for (auto& initializer : Initializers_) {
  267. if (!initializer->SetupCustomTargets()) {
  268. return false;
  269. }
  270. }
  271. return true;
  272. }