cmExportTryCompileFileGenerator.cxx 5.6 KB

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