cmExportTryCompileFileGenerator.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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,
  77. propName,
  78. nullptr,
  79. parentDagChecker.get(),
  80. tgt->GetLocalGenerator(),
  81. this->Config,
  82. };
  83. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*prop);
  84. cmTarget dummyHead("try_compile_dummy_exe", cmStateEnums::EXECUTABLE,
  85. cmTarget::Visibility::Normal, tgt->Target->GetMakefile(),
  86. cmTarget::PerConfig::Yes);
  87. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  88. std::string result = cge->Evaluate(tgt->GetLocalGenerator(), this->Config,
  89. &gDummyHead, &dagChecker, tgt, language);
  90. std::set<cmGeneratorTarget const*> const& allTargets =
  91. cge->GetAllTargetsSeen();
  92. for (cmGeneratorTarget const* target : allTargets) {
  93. if (emitted.insert(target).second) {
  94. this->Exports.push_back(target);
  95. }
  96. }
  97. return result;
  98. }
  99. void cmExportTryCompileFileGenerator::PopulateProperties(
  100. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  101. std::set<cmGeneratorTarget const*>& emitted)
  102. {
  103. // Look through all non-special properties.
  104. std::vector<std::string> props = target->GetPropertyKeys();
  105. // Include special properties that might be relevant here.
  106. props.emplace_back("INTERFACE_LINK_LIBRARIES");
  107. props.emplace_back("INTERFACE_LINK_LIBRARIES_DIRECT");
  108. props.emplace_back("INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE");
  109. for (std::string const& p : props) {
  110. cmValue v = target->GetProperty(p);
  111. if (!v) {
  112. continue;
  113. }
  114. properties[p] = *v;
  115. if (cmHasLiteralPrefix(p, "IMPORTED_LINK_INTERFACE_LIBRARIES") ||
  116. cmHasLiteralPrefix(p, "IMPORTED_LINK_DEPENDENT_LIBRARIES") ||
  117. cmHasLiteralPrefix(p, "INTERFACE_LINK_LIBRARIES")) {
  118. std::string evalResult =
  119. this->FindTargets(p, target, std::string(), emitted);
  120. cmList depends{ evalResult };
  121. for (std::string const& li : depends) {
  122. cmGeneratorTarget* tgt =
  123. target->GetLocalGenerator()->FindGeneratorTargetToUse(li);
  124. if (tgt && emitted.insert(tgt).second) {
  125. this->Exports.push_back(tgt);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  132. cmGeneratorTarget const* target, std::string const& config)
  133. {
  134. std::string install_name_dir;
  135. cmMakefile* mf = target->Target->GetMakefile();
  136. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  137. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  138. }
  139. return install_name_dir;
  140. }
  141. std::string cmExportTryCompileFileGenerator::GetFileSetDirectories(
  142. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  143. {
  144. return cmOutputConverter::EscapeForCMake(
  145. cmList::to_string(fileSet->GetDirectoryEntries()));
  146. }
  147. std::string cmExportTryCompileFileGenerator::GetFileSetFiles(
  148. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  149. {
  150. return cmOutputConverter::EscapeForCMake(
  151. cmList::to_string(fileSet->GetFileEntries()));
  152. }