cmQtAutoGenGlobalInitializer.cxx 9.6 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 "cmQtAutoGen.h"
  5. #include "cmQtAutoGenInitializer.h"
  6. #include "cmAlgorithms.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmDuration.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmLocalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmProcessOutput.h"
  14. #include "cmState.h"
  15. #include "cmStateTypes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include <memory>
  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. {
  28. }
  29. cmQtAutoGenGlobalInitializer::cmQtAutoGenGlobalInitializer(
  30. std::vector<cmLocalGenerator*> const& localGenerators)
  31. {
  32. for (cmLocalGenerator* localGen : localGenerators) {
  33. // Detect global autogen and autorcc target names
  34. bool globalAutoGenTarget = false;
  35. bool globalAutoRccTarget = false;
  36. {
  37. cmMakefile* makefile = localGen->GetMakefile();
  38. // Detect global autogen target name
  39. if (cmSystemTools::IsOn(
  40. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET"))) {
  41. std::string targetName =
  42. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTOGEN_TARGET_NAME");
  43. if (targetName.empty()) {
  44. targetName = "autogen";
  45. }
  46. GlobalAutoGenTargets_.emplace(localGen, std::move(targetName));
  47. globalAutoGenTarget = true;
  48. }
  49. // Detect global autorcc target name
  50. if (cmSystemTools::IsOn(
  51. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET"))) {
  52. std::string targetName =
  53. makefile->GetSafeDefinition("CMAKE_GLOBAL_AUTORCC_TARGET_NAME");
  54. if (targetName.empty()) {
  55. targetName = "autorcc";
  56. }
  57. GlobalAutoRccTargets_.emplace(localGen, std::move(targetName));
  58. globalAutoRccTarget = true;
  59. }
  60. }
  61. // Find targets that require AUTOMOC/UIC/RCC processing
  62. for (cmGeneratorTarget* target : localGen->GetGeneratorTargets()) {
  63. // Process only certain target types
  64. switch (target->GetType()) {
  65. case cmStateEnums::EXECUTABLE:
  66. case cmStateEnums::STATIC_LIBRARY:
  67. case cmStateEnums::SHARED_LIBRARY:
  68. case cmStateEnums::MODULE_LIBRARY:
  69. case cmStateEnums::OBJECT_LIBRARY:
  70. // Process target
  71. break;
  72. default:
  73. // Don't process target
  74. continue;
  75. }
  76. if (target->IsImported()) {
  77. // Don't process target
  78. continue;
  79. }
  80. bool const moc = target->GetPropertyAsBool(kw().AUTOMOC);
  81. bool const uic = target->GetPropertyAsBool(kw().AUTOUIC);
  82. bool const rcc = target->GetPropertyAsBool(kw().AUTORCC);
  83. if (moc || uic || rcc) {
  84. std::string const mocExec =
  85. target->GetSafeProperty(kw().AUTOMOC_EXECUTABLE);
  86. std::string const uicExec =
  87. target->GetSafeProperty(kw().AUTOUIC_EXECUTABLE);
  88. std::string const rccExec =
  89. target->GetSafeProperty(kw().AUTORCC_EXECUTABLE);
  90. // We support Qt4, Qt5 and Qt6
  91. auto qtVersion = cmQtAutoGenInitializer::GetQtVersion(target);
  92. bool const validQt = (qtVersion.first.Major == 4) ||
  93. (qtVersion.first.Major == 5) || (qtVersion.first.Major == 6);
  94. bool const mocAvailable = (validQt || !mocExec.empty());
  95. bool const uicAvailable = (validQt || !uicExec.empty());
  96. bool const rccAvailable = (validQt || !rccExec.empty());
  97. bool const mocIsValid = (moc && mocAvailable);
  98. bool const uicIsValid = (uic && uicAvailable);
  99. bool const rccIsValid = (rcc && rccAvailable);
  100. // Disabled AUTOMOC/UIC/RCC warning
  101. bool const mocDisabled = (moc && !mocAvailable);
  102. bool const uicDisabled = (uic && !uicAvailable);
  103. bool const rccDisabled = (rcc && !rccAvailable);
  104. if (mocDisabled || uicDisabled || rccDisabled) {
  105. std::string msg = "AUTOGEN: No valid Qt version found for target ";
  106. msg += target->GetName();
  107. msg += ". ";
  108. msg += cmQtAutoGen::Tools(mocDisabled, uicDisabled, rccDisabled);
  109. msg += " disabled. Consider adding:\n";
  110. {
  111. std::string version = (qtVersion.second == 0)
  112. ? std::string("<QTVERSION>")
  113. : std::to_string(qtVersion.second);
  114. std::string comp = uicDisabled ? "Widgets" : "Core";
  115. msg += " find_package(Qt";
  116. msg += version;
  117. msg += " COMPONENTS ";
  118. msg += comp;
  119. msg += ")\n";
  120. }
  121. msg += "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, cmMakefile::TargetOrigin::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. bool cmQtAutoGenGlobalInitializer::GetExecutableTestOutput(
  183. std::string const& generator, std::string const& executable,
  184. std::string& error, std::string* output)
  185. {
  186. // Check if we have cached output
  187. {
  188. auto it = this->ExecutableTestOutputs_.find(executable);
  189. if (it != this->ExecutableTestOutputs_.end()) {
  190. // Return output on demand
  191. if (output != nullptr) {
  192. *output = it->second;
  193. }
  194. return true;
  195. }
  196. }
  197. // Check if the executable exists
  198. if (!cmSystemTools::FileExists(executable, true)) {
  199. error = "The \"";
  200. error += generator;
  201. error += "\" executable ";
  202. error += cmQtAutoGen::Quoted(executable);
  203. error += " does not exist.";
  204. return false;
  205. }
  206. // Test the executable
  207. std::string stdOut;
  208. {
  209. std::string stdErr;
  210. std::vector<std::string> command;
  211. command.push_back(executable);
  212. command.emplace_back("-h");
  213. int retVal = 0;
  214. const bool runResult = cmSystemTools::RunSingleCommand(
  215. command, &stdOut, &stdErr, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
  216. cmDuration::zero(), cmProcessOutput::Auto);
  217. if (!runResult) {
  218. error = "Test run of \"";
  219. error += generator;
  220. error += "\" executable ";
  221. error += cmQtAutoGen::Quoted(executable) + " failed.\n";
  222. error += cmQtAutoGen::QuotedCommand(command);
  223. error += "\n";
  224. error += stdOut;
  225. error += "\n";
  226. error += stdErr;
  227. return false;
  228. }
  229. }
  230. // Return executable output on demand
  231. if (output != nullptr) {
  232. *output = stdOut;
  233. }
  234. // Register executable and output
  235. this->ExecutableTestOutputs_.emplace(executable, std::move(stdOut));
  236. return true;
  237. }
  238. bool cmQtAutoGenGlobalInitializer::generate()
  239. {
  240. return (InitializeCustomTargets() && SetupCustomTargets());
  241. }
  242. bool cmQtAutoGenGlobalInitializer::InitializeCustomTargets()
  243. {
  244. // Initialize global autogen targets
  245. {
  246. std::string const comment = "Global AUTOGEN target";
  247. for (auto const& pair : GlobalAutoGenTargets_) {
  248. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  249. }
  250. }
  251. // Initialize global autorcc targets
  252. {
  253. std::string const comment = "Global AUTORCC target";
  254. for (auto const& pair : GlobalAutoRccTargets_) {
  255. GetOrCreateGlobalTarget(pair.first, pair.second, comment);
  256. }
  257. }
  258. // Initialize per target autogen targets
  259. for (auto& initializer : Initializers_) {
  260. if (!initializer->InitCustomTargets()) {
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. bool cmQtAutoGenGlobalInitializer::SetupCustomTargets()
  267. {
  268. for (auto& initializer : Initializers_) {
  269. if (!initializer->SetupCustomTargets()) {
  270. return false;
  271. }
  272. }
  273. return true;
  274. }