cmExportTryCompileFileGenerator.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmExportTryCompileFileGenerator.h"
  4. #include <map>
  5. #include <utility>
  6. #include <cm/memory>
  7. #include <cmext/string_view>
  8. #include "cmFileSet.h"
  9. #include "cmGenExContext.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmGeneratorExpressionDAGChecker.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmList.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmOutputConverter.h"
  19. #include "cmStateTypes.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #include "cmTarget.h"
  23. #include "cmValue.h"
  24. cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator(
  25. cmGlobalGenerator* gg, std::vector<std::string> const& targets,
  26. cmMakefile* mf, std::set<std::string> const& langs)
  27. : Languages(langs.begin(), langs.end())
  28. {
  29. gg->CreateImportedGenerationObjects(mf, targets, this->Exports);
  30. }
  31. void cmExportTryCompileFileGenerator::IssueMessage(
  32. MessageType type, std::string const& message) const
  33. {
  34. switch (type) {
  35. case MessageType::FATAL_ERROR:
  36. case MessageType::AUTHOR_ERROR:
  37. case MessageType::INTERNAL_ERROR:
  38. case MessageType::DEPRECATION_ERROR:
  39. cmSystemTools::Error(message);
  40. break;
  41. case MessageType::WARNING:
  42. case MessageType::AUTHOR_WARNING:
  43. case MessageType::DEPRECATION_WARNING:
  44. cmSystemTools::Message(cmStrCat("CMake Warning: "_s, message),
  45. "Warning");
  46. break;
  47. default:
  48. cmSystemTools::Message(message);
  49. }
  50. }
  51. bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
  52. {
  53. std::set<cmGeneratorTarget const*> emitted;
  54. std::set<cmGeneratorTarget const*> emittedDeps;
  55. while (!this->Exports.empty()) {
  56. cmGeneratorTarget const* te = this->Exports.back();
  57. this->Exports.pop_back();
  58. if (emitted.insert(te).second) {
  59. emittedDeps.insert(te);
  60. this->GenerateImportTargetCode(os, te, te->GetType());
  61. ImportPropertyMap properties;
  62. for (std::string const& lang : this->Languages) {
  63. for (auto i : cmGeneratorTarget::BuiltinTransitiveProperties) {
  64. this->FindTargets(std::string(i.second.InterfaceName), te, lang,
  65. emittedDeps);
  66. }
  67. }
  68. this->PopulateProperties(te, properties, emittedDeps);
  69. this->GenerateInterfaceProperties(te, os, properties);
  70. }
  71. }
  72. return true;
  73. }
  74. std::string cmExportTryCompileFileGenerator::FindTargets(
  75. std::string const& propName, cmGeneratorTarget const* tgt,
  76. std::string const& language, std::set<cmGeneratorTarget const*>& emitted)
  77. {
  78. cmValue prop = tgt->GetProperty(propName);
  79. if (!prop) {
  80. return std::string();
  81. }
  82. cm::GenEx::Context context(tgt->LocalGenerator, this->Config, language);
  83. cmGeneratorExpression ge(*tgt->Makefile->GetCMakeInstance());
  84. std::unique_ptr<cmGeneratorExpressionDAGChecker> parentDagChecker;
  85. if (propName == "INTERFACE_LINK_OPTIONS") {
  86. // To please constraint checks of DAGChecker, this property must have
  87. // LINK_OPTIONS property as parent
  88. parentDagChecker = cm::make_unique<cmGeneratorExpressionDAGChecker>(
  89. tgt, "LINK_OPTIONS", nullptr, nullptr, context);
  90. }
  91. cmGeneratorExpressionDAGChecker dagChecker{
  92. tgt, propName, nullptr, parentDagChecker.get(), context,
  93. };
  94. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*prop);
  95. cmTarget dummyHead("try_compile_dummy_exe", cmStateEnums::EXECUTABLE,
  96. cmTarget::Visibility::Normal, tgt->Target->GetMakefile(),
  97. cmTarget::PerConfig::Yes);
  98. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  99. std::string result = cge->Evaluate(context, &dagChecker, &gDummyHead, tgt);
  100. std::set<cmGeneratorTarget const*> const& allTargets =
  101. cge->GetAllTargetsSeen();
  102. for (cmGeneratorTarget const* target : allTargets) {
  103. if (emitted.insert(target).second) {
  104. this->Exports.push_back(target);
  105. }
  106. }
  107. return result;
  108. }
  109. void cmExportTryCompileFileGenerator::PopulateProperties(
  110. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  111. std::set<cmGeneratorTarget const*>& emitted)
  112. {
  113. // Look through all non-special properties.
  114. std::vector<std::string> props = target->GetPropertyKeys();
  115. // Include special properties that might be relevant here.
  116. props.emplace_back("INTERFACE_LINK_LIBRARIES");
  117. props.emplace_back("INTERFACE_LINK_LIBRARIES_DIRECT");
  118. props.emplace_back("INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE");
  119. for (std::string const& p : props) {
  120. cmValue v = target->GetProperty(p);
  121. if (!v) {
  122. continue;
  123. }
  124. properties[p] = *v;
  125. if (cmHasLiteralPrefix(p, "IMPORTED_LINK_INTERFACE_LIBRARIES") ||
  126. cmHasLiteralPrefix(p, "IMPORTED_LINK_DEPENDENT_LIBRARIES") ||
  127. cmHasLiteralPrefix(p, "INTERFACE_LINK_LIBRARIES")) {
  128. std::string evalResult =
  129. this->FindTargets(p, target, std::string(), emitted);
  130. cmList depends{ evalResult };
  131. for (std::string const& li : depends) {
  132. cmGeneratorTarget* tgt =
  133. target->GetLocalGenerator()->FindGeneratorTargetToUse(li);
  134. if (tgt && emitted.insert(tgt).second) {
  135. this->Exports.push_back(tgt);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  142. cmGeneratorTarget const* target, std::string const& config)
  143. {
  144. std::string install_name_dir;
  145. cmMakefile* mf = target->Target->GetMakefile();
  146. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  147. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  148. }
  149. return install_name_dir;
  150. }
  151. std::string cmExportTryCompileFileGenerator::GetFileSetDirectories(
  152. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  153. {
  154. return cmOutputConverter::EscapeForCMake(
  155. cmList::to_string(fileSet->GetDirectoryEntries()));
  156. }
  157. std::string cmExportTryCompileFileGenerator::GetFileSetFiles(
  158. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  159. {
  160. return cmOutputConverter::EscapeForCMake(
  161. cmList::to_string(fileSet->GetFileEntries()));
  162. }