cmExportTryCompileFileGenerator.cxx 5.1 KB

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