cmExportTryCompileFileGenerator.cxx 4.1 KB

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