cmExportTryCompileFileGenerator.cxx 5.4 KB

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