cmQtAutoGenGlobalInitializer.cxx 9.8 KB

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