cmQtAutoGenGlobalInitializer.cxx 9.3 KB

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