cmExportTryCompileFileGenerator.cxx 5.5 KB

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