cmQtAutoGenGlobalInitializer.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "cmQtAutoGen.h"
  5. #include "cmQtAutoGenInitializer.h"
  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 "cmState.h"
  14. #include "cmStateTypes.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include <utility>
  18. #include "cm_memory.hxx"
  19. cmQtAutoGenGlobalInitializer::Keywords::Keywords()
  20. : AUTOMOC("AUTOMOC")
  21. , AUTOUIC("AUTOUIC")
  22. , AUTORCC("AUTORCC")
  23. , AUTOMOC_EXECUTABLE("AUTOMOC_EXECUTABLE")
  24. , AUTOUIC_EXECUTABLE("AUTOUIC_EXECUTABLE")
  25. , AUTORCC_EXECUTABLE("AUTORCC_EXECUTABLE")
  26. , SKIP_AUTOGEN("SKIP_AUTOGEN")
  27. , SKIP_AUTOMOC("SKIP_AUTOMOC")
  28. , SKIP_AUTOUIC("SKIP_AUTOUIC")
  29. , SKIP_AUTORCC("SKIP_AUTORCC")
  30. , AUTOUIC_OPTIONS("AUTOUIC_OPTIONS")
  31. , AUTORCC_OPTIONS("AUTORCC_OPTIONS")
  32. , qrc("qrc")
  33. , ui("ui")
  34. {
  35. }
  36. cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
  37. std::vector<cmLocalGenerator*> const& localGenerators)
  38. {
  39. for (cmLocalGenerator* localGen : localGenerators) {
  40. // Detect global autogen and autorcc target names
  41. bool globalAutoGenTarget = false;
  42. bool globalAutoRccTarget = false;
  43. {
  44. cmMakefile* makefile = localGen->GetMakefile();
  45. // Detect global autogen target name
  46. if (cmSystemTools::IsOn(
  47. 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 (cmSystemTools::IsOn(
  58. 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. std::string msg = "AUTOGEN: No valid Qt version found for target ";
  113. msg += target->GetName();
  114. msg += ". ";
  115. msg += cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled);
  116. msg += " disabled. Consider adding:\n";
  117. {
  118. std::string version = (qtVersion.second == 0)
  119. ? std::string("<QTVERSION>")
  120. : std::to_string(qtVersion.second);
  121. std::string comp = uicDisabled ? "Widgets" : "Core";
  122. msg += " find_package(Qt";
  123. msg += version;
  124. msg += " COMPONENTS ";
  125. msg += comp;
  126. msg += ")\n";
  127. }
  128. msg += "to your CMakeLists.txt file.";
  129. target->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, msg);
  130. }
  131. if (mocIsValid || uicIsValid || rccIsValid) {
  132. // Create autogen target initializer
  133. Initializers_.emplace_back(cm::make_unique<cmQtAutoGenInitializer>(
  134. this, target, qtVersion.first, mocIsValid, uicIsValid, rccIsValid,
  135. globalAutoGenTarget, globalAutoRccTarget));
  136. }
  137. }
  138. }
  139. }
  140. }
  141. cmQtAutoGenGlobalInitializer::~cmQtAutoGenGlobalInitializer() = default;
  142. void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
  143. cmLocalGenerator* localGen, std::string const& name,
  144. std::string const& comment)
  145. {
  146. // Test if the target already exists
  147. if (localGen->FindGeneratorTargetToUse(name) == nullptr) {
  148. cmMakefile* makefile = localGen->GetMakefile();
  149. // Create utility target
  150. cmTarget* target = makefile->AddUtilityCommand(
  151. name, cmMakefile::TargetOrigin::Generator, true,
  152. makefile->GetHomeOutputDirectory().c_str() /*work dir*/,
  153. std::vector<std::string>() /*output*/,
  154. std::vector<std::string>() /*depends*/, cmCustomCommandLines(), false,
  155. comment.c_str());
  156. localGen->AddGeneratorTarget(new cmGeneratorTarget(target, localGen));
  157. // Set FOLDER property in the target
  158. {
  159. char const* folder =
  160. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  161. if (folder != nullptr) {
  162. target->SetProperty("FOLDER", folder);
  163. }
  164. }
  165. }
  166. }
  167. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoGen(
  168. cmLocalGenerator* localGen, std::string const& targetName)
  169. {
  170. auto it = GlobalAutoGenTargets_.find(localGen);
  171. if (it != GlobalAutoGenTargets_.end()) {
  172. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  173. if (target != nullptr) {
  174. target->Target->AddUtility(targetName, localGen->GetMakefile());
  175. }
  176. }
  177. }
  178. void cmQtAutoGenGlobalInitializer::AddToGlobalAutoRcc(
  179. cmLocalGenerator* localGen, std::string const& targetName)
  180. {
  181. auto it = GlobalAutoRccTargets_.find(localGen);
  182. if (it != GlobalAutoRccTargets_.end()) {
  183. cmGeneratorTarget* target = localGen->FindGeneratorTargetToUse(it->second);
  184. if (target != nullptr) {
  185. target->Target->AddUtility(targetName, localGen->GetMakefile());
  186. }
  187. }
  188. }
  189. cmQtAutoGen::CompilerFeaturesHandle
  190. cmQtAutoGenGlobalInitializer::GetCompilerFeatures(
  191. std::string const& generator, std::string const& executable,
  192. std::string& error)
  193. {
  194. // Check if we have cached features
  195. {
  196. auto it = this->CompilerFeatures_.find(executable);
  197. if (it != this->CompilerFeatures_.end()) {
  198. return it->second;
  199. }
  200. }
  201. // Check if the executable exists
  202. if (!cmSystemTools::FileExists(executable, true)) {
  203. error = "The \"";
  204. error += generator;
  205. error += "\" executable ";
  206. error += cmQtAutoGen::Quoted(executable);
  207. error += " does not exist.";
  208. return cmQtAutoGen::CompilerFeaturesHandle();
  209. }
  210. // Test the executable
  211. std::string stdOut;
  212. {
  213. std::string stdErr;
  214. std::vector<std::string> command;
  215. command.emplace_back(executable);
  216. command.emplace_back("-h");
  217. int retVal = 0;
  218. const bool runResult = cmSystemTools::RunSingleCommand(
  219. command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
  220. cmDuration::zero(), cmProcessOutput::Auto);
  221. if (!runResult) {
  222. error = "Test run of \"";
  223. error += generator;
  224. error += "\" executable ";
  225. error += cmQtAutoGen::Quoted(executable) + " failed.\n";
  226. error += cmQtAutoGen::QuotedCommand(command);
  227. error += "\n";
  228. error += stdOut;
  229. error += "\n";
  230. error += stdErr;
  231. return cmQtAutoGen::CompilerFeaturesHandle();
  232. }
  233. }
  234. // Create valid handle
  235. cmQtAutoGen::CompilerFeaturesHandle res =
  236. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  237. res->HelpOutput = std::move(stdOut);
  238. // Register compiler features
  239. this->CompilerFeatures_.emplace(executable, res);
  240. return res;
  241. }
  242. bool cmQtAutoGenGlobalInitializer::generate()
  243. {
  244. return (InitializeCustomTargets() && SetupCustomTargets());
  245. }
  246. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  247. {
  248. // Initialize global autogen targets
  249. {
  250. std::string const comment = "Global AUTOGEN target";
  251. for (auto const& pair : GlobalAutoGenTargets_) {
  252. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  253. }
  254. }
  255. // Initialize global autorcc targets
  256. {
  257. std::string const comment = "Global AUTORCC target";
  258. for (auto const& pair : GlobalAutoRccTargets_) {
  259. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  260. }
  261. }
  262. // Initialize per target autogen targets
  263. for (auto& initializer : Initializers_) {
  264. if (!initializer->InitCustomTargets()) {
  265. return false;
  266. }
  267. }
  268. return true;
  269. }
  270. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  271. {
  272. for (auto& initializer : Initializers_) {
  273. if (!initializer->SetupCustomTargets()) {
  274. return false;
  275. }
  276. }
  277. return true;
  278. }