cmExportTryCompileFileGenerator.cxx 5.4 KB

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