cmExportTryCompileFileGenerator.cxx 4.5 KB

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