cmExportTryCompileFileGenerator.cxx 5.7 KB

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