cmExportTryCompileFileGenerator.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "cmGeneratorExpression.h"
  5. #include "cmGeneratorExpressionDAGChecker.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmStateTypes.h"
  11. #include "cmSystemTools.h"
  12. #include "cmTarget.h"
  13. #include <map>
  14. #include <memory> // IWYU pragma: keep
  15. #include <utility>
  16. cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator(
  17. cmGlobalGenerator* gg, const std::vector<std::string>& targets,
  18. cmMakefile* mf, std::set<std::string> const& langs)
  19. : Languages(langs.begin(), langs.end())
  20. {
  21. gg->CreateImportedGenerationObjects(mf, targets, this->Exports);
  22. }
  23. bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
  24. {
  25. std::set<cmGeneratorTarget const*> emitted;
  26. std::set<cmGeneratorTarget const*> emittedDeps;
  27. while (!this->Exports.empty()) {
  28. cmGeneratorTarget const* te = this->Exports.back();
  29. this->Exports.pop_back();
  30. if (emitted.insert(te).second) {
  31. emittedDeps.insert(te);
  32. this->GenerateImportTargetCode(os, te);
  33. ImportPropertyMap properties;
  34. for (std::string const& lang : this->Languages) {
  35. #define FIND_TARGETS(PROPERTY) \
  36. this->FindTargets("INTERFACE_" #PROPERTY, te, lang, emittedDeps);
  37. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(FIND_TARGETS)
  38. #undef FIND_TARGETS
  39. }
  40. this->PopulateProperties(te, properties, emittedDeps);
  41. this->GenerateInterfaceProperties(te, os, properties);
  42. }
  43. }
  44. return true;
  45. }
  46. std::string cmExportTryCompileFileGenerator::FindTargets(
  47. const std::string& propName, cmGeneratorTarget const* tgt,
  48. std::string const& language, std::set<cmGeneratorTarget const*>& emitted)
  49. {
  50. const char* prop = tgt->GetProperty(propName);
  51. if (!prop) {
  52. return std::string();
  53. }
  54. cmGeneratorExpression ge;
  55. cmGeneratorExpressionDAGChecker dagChecker(tgt->GetName(), propName, nullptr,
  56. 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. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  61. std::string result =
  62. cge->Evaluate(tgt->GetLocalGenerator(), this->Config, false, &gDummyHead,
  63. tgt, &dagChecker, 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 (p.find("IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  81. p.find("IMPORTED_LINK_DEPENDENT_LIBRARIES") == 0 ||
  82. p.find("INTERFACE_LINK_LIBRARIES") == 0) {
  83. std::string evalResult =
  84. this->FindTargets(p, target, std::string(), emitted);
  85. std::vector<std::string> depends;
  86. cmSystemTools::ExpandListArgument(evalResult, depends);
  87. for (std::string const& li : depends) {
  88. cmGeneratorTarget* tgt =
  89. target->GetLocalGenerator()->FindGeneratorTargetToUse(li);
  90. if (tgt && emitted.insert(tgt).second) {
  91. this->Exports.push_back(tgt);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  98. cmGeneratorTarget* target, const std::string& config)
  99. {
  100. std::string install_name_dir;
  101. cmMakefile* mf = target->Target->GetMakefile();
  102. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  103. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  104. }
  105. return install_name_dir;
  106. }