cmExportTryCompileFileGenerator.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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)
  19. {
  20. gg->CreateImportedGenerationObjects(mf, targets, this->Exports);
  21. }
  22. bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
  23. {
  24. std::set<cmGeneratorTarget const*> emitted;
  25. std::set<cmGeneratorTarget const*> emittedDeps;
  26. while (!this->Exports.empty()) {
  27. cmGeneratorTarget const* te = this->Exports.back();
  28. this->Exports.pop_back();
  29. if (emitted.insert(te).second) {
  30. emittedDeps.insert(te);
  31. this->GenerateImportTargetCode(os, te, te->GetType());
  32. ImportPropertyMap properties;
  33. #define FIND_TARGETS(PROPERTY) \
  34. this->FindTargets("INTERFACE_" #PROPERTY, te, emittedDeps);
  35. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(FIND_TARGETS)
  36. #undef FIND_TARGETS
  37. this->PopulateProperties(te, properties, emittedDeps);
  38. this->GenerateInterfaceProperties(te, os, properties);
  39. }
  40. }
  41. return true;
  42. }
  43. std::string cmExportTryCompileFileGenerator::FindTargets(
  44. const std::string& propName, cmGeneratorTarget const* tgt,
  45. std::set<cmGeneratorTarget const*>& emitted)
  46. {
  47. const char* prop = tgt->GetProperty(propName);
  48. if (!prop) {
  49. return std::string();
  50. }
  51. cmGeneratorExpression ge;
  52. cmGeneratorExpressionDAGChecker dagChecker(tgt->GetName(), propName, nullptr,
  53. nullptr);
  54. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  55. cmTarget dummyHead("try_compile_dummy_exe", cmStateEnums::EXECUTABLE,
  56. cmTarget::VisibilityNormal, tgt->Target->GetMakefile());
  57. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  58. std::string result = cge->Evaluate(tgt->GetLocalGenerator(), this->Config,
  59. false, &gDummyHead, tgt, &dagChecker);
  60. const std::set<cmGeneratorTarget const*>& allTargets =
  61. cge->GetAllTargetsSeen();
  62. for (cmGeneratorTarget const* target : allTargets) {
  63. if (emitted.insert(target).second) {
  64. this->Exports.push_back(target);
  65. }
  66. }
  67. return result;
  68. }
  69. void cmExportTryCompileFileGenerator::PopulateProperties(
  70. const cmGeneratorTarget* target, ImportPropertyMap& properties,
  71. std::set<cmGeneratorTarget const*>& emitted)
  72. {
  73. std::vector<std::string> props = target->GetPropertyKeys();
  74. for (std::string const& p : props) {
  75. properties[p] = target->GetProperty(p);
  76. if (p.find("IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  77. p.find("IMPORTED_LINK_DEPENDENT_LIBRARIES") == 0 ||
  78. p.find("INTERFACE_LINK_LIBRARIES") == 0) {
  79. std::string evalResult = this->FindTargets(p, target, emitted);
  80. std::vector<std::string> depends;
  81. cmSystemTools::ExpandListArgument(evalResult, depends);
  82. for (std::string const& li : depends) {
  83. cmGeneratorTarget* tgt =
  84. target->GetLocalGenerator()->FindGeneratorTargetToUse(li);
  85. if (tgt && emitted.insert(tgt).second) {
  86. this->Exports.push_back(tgt);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  93. cmGeneratorTarget* target, const std::string& config)
  94. {
  95. std::string install_name_dir;
  96. cmMakefile* mf = target->Target->GetMakefile();
  97. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  98. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  99. }
  100. return install_name_dir;
  101. }