cmExportTryCompileFileGenerator.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <memory>
  6. #include <utility>
  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. cmGeneratorExpressionDAGChecker dagChecker(tgt, propName, nullptr, nullptr);
  57. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*prop);
  58. cmTarget dummyHead("try_compile_dummy_exe", cmStateEnums::EXECUTABLE,
  59. cmTarget::VisibilityNormal, tgt->Target->GetMakefile(),
  60. cmTarget::PerConfig::Yes);
  61. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  62. std::string result = cge->Evaluate(tgt->GetLocalGenerator(), this->Config,
  63. &gDummyHead, &dagChecker, tgt, language);
  64. const std::set<cmGeneratorTarget const*>& allTargets =
  65. cge->GetAllTargetsSeen();
  66. for (cmGeneratorTarget const* target : allTargets) {
  67. if (emitted.insert(target).second) {
  68. this->Exports.push_back(target);
  69. }
  70. }
  71. return result;
  72. }
  73. void cmExportTryCompileFileGenerator::PopulateProperties(
  74. const cmGeneratorTarget* target, ImportPropertyMap& properties,
  75. std::set<cmGeneratorTarget const*>& emitted)
  76. {
  77. std::vector<std::string> props = target->GetPropertyKeys();
  78. for (std::string const& p : props) {
  79. properties[p] = *target->GetProperty(p);
  80. if (cmHasLiteralPrefix(p, "IMPORTED_LINK_INTERFACE_LIBRARIES") ||
  81. cmHasLiteralPrefix(p, "IMPORTED_LINK_DEPENDENT_LIBRARIES") ||
  82. cmHasLiteralPrefix(p, "INTERFACE_LINK_LIBRARIES")) {
  83. std::string evalResult =
  84. this->FindTargets(p, target, std::string(), emitted);
  85. std::vector<std::string> depends = cmExpandedList(evalResult);
  86. for (std::string const& li : depends) {
  87. cmGeneratorTarget* tgt =
  88. target->GetLocalGenerator()->FindGeneratorTargetToUse(li);
  89. if (tgt && emitted.insert(tgt).second) {
  90. this->Exports.push_back(tgt);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  97. cmGeneratorTarget* target, const std::string& config)
  98. {
  99. std::string install_name_dir;
  100. cmMakefile* mf = target->Target->GetMakefile();
  101. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  102. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  103. }
  104. return install_name_dir;
  105. }